Files
ai/model/local_dialect_test.go
wbin90620 34b72da2c5 model, object: no panic on a local upstream or a completion model, each token priced once
hanzo-inc/cloud#460 (moved from #116) listed eight defects at c7f85eed. Seven
were still on main; this fixes them.

A LocalModelProvider of type Zen, Enso or Hanzo had no client, and neither did
DeepSeek with a model other than chat or reasoner, so the first call
dereferenced nil. dialect() now names each type's client and flush once, with
typed values in place of an `any` asserted in each loop, and QueryText refuses
a type with no client. That also ends the assertion panics: OpenAI completion
models asserted the answer-only flush while holding the think one, and so did
Local and DigitalOcean completion models in local.go. Both completion loops
now skip a chunk with no choice, and local.go's no longer returns a nil result
with no error when the count fails.

DeepSeek, Mistral, SiliconFlow, Baichuan and BaiduCloud priced the total token
count at both the input and the output rate. The prompt is now priced at the
input rate and the answer at the output rate. gpt-4.5's case sat after
gpt-4's, which also matches it, so gpt-4.5 billed as gpt-4; it now comes first
and bills at the rate the pricing table publishes.

GetMessagesForChats bound the chat names to a bare "{:p0}" WHERE; it is
dbx.In("chat", ...). updateErrorText's branches were inverted, so a record
with no id updated WHERE id = 0. The Guacamole handshake returned nil on a
failed write, and UploadVideo returned "", nil on each failure; both return
the error.

Item 7 (gateway frames) is left: the gateway's own errors are gateway frames
now, but its chat, models and balance fast paths still answer with cloud
frames. That needs hanzoai/gateway's side confirmed first.
2026-11-09 04:15:19 -05:00

95 lines
3.4 KiB
Go

// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
package model
import (
"fmt"
"net/http/httptest"
"strings"
"testing"
)
// answerWithoutPanic runs one QueryText and turns a panic into a test failure,
// so a regression reads as what it is rather than aborting the package binary.
func answerWithoutPanic(t *testing.T, p ModelProvider) (result *ModelResult, err error) {
t.Helper()
defer func() {
if r := recover(); r != nil {
t.Fatalf("QueryText panicked: %v", r)
}
}()
return p.QueryText("hello", httptest.NewRecorder(), nil, "", nil, nil, "en")
}
// Every upstream that Open serves through a LocalModelProvider reaches it with a
// client. Zen, Enso and Hanzo were built as providers of their own type, which
// no branch named, so the first call dereferenced a nil client.
func TestEveryLocalUpstreamHasAClient(t *testing.T) {
for _, u := range []Upstream{Ollama, Local, DigitalOcean, Zen, Enso, Hanzo} {
made, err := Open(Spec{Upstream: u, Model: "m", Secret: "k", URL: "http://127.0.0.1:9/v1"})
if err != nil {
t.Fatalf("%s: %v", u, err)
}
local, ok := made.(*LocalModelProvider)
if !ok {
t.Fatalf("%s: Open made a %T, not a LocalModelProvider; this list is stale", u, made)
}
client, flushThink, flushStandard := local.dialect()
if client == nil {
t.Errorf("%s (type %q) has no client", u, local.typ)
}
if (flushThink == nil) == (flushStandard == nil) {
t.Errorf("%s (type %q) must have exactly one flush", u, local.typ)
}
}
}
// Each type has one client and one flush, and the flush a streaming loop uses is
// the one the type holds. The loops used to re-test the type by name and assert
// a shape: a Local or DigitalOcean completion model held the think flush and was
// asserted to the standard one, and panicked.
func TestEachLocalTypeFlushesWithTheShapeItHolds(t *testing.T) {
for typ, thinks := range map[string]bool{
"Local": true, "DigitalOcean": true, "Custom-think": true,
"Zen": true, "Enso": true, "Hanzo": true,
"Custom": false, "GitHub": false, "Azure": false,
} {
p := &LocalModelProvider{typ: typ, providerUrl: "http://127.0.0.1:9/v1", apiVersion: "2024-02-01"}
client, flushThink, flushStandard := p.dialect()
if client == nil {
t.Errorf("%s has no client", typ)
}
if got := flushThink != nil; got != thinks {
t.Errorf("%s streams reasoning = %v, want %v", typ, got, thinks)
}
if (flushThink == nil) == (flushStandard == nil) {
t.Errorf("%s must have exactly one flush", typ)
}
}
}
// A type with no client is refused before the call, never dereferenced.
func TestAnUnknownLocalTypeIsRefused(t *testing.T) {
for _, typ := range []string{"", "Nobody"} {
p := &LocalModelProvider{typ: typ, subType: "gpt-4o"}
result, err := answerWithoutPanic(t, p)
if err == nil {
t.Errorf("type %q answered %+v with no error", typ, result)
}
}
}
// DeepSeek knows a type for chat and reasoner only. Any other model used to reach
// the local provider with no type and panic; it is refused instead, since its
// answer could not be priced either.
func TestDeepSeekRefusesAModelItCannotServe(t *testing.T) {
p := &DeepSeekProvider{subType: "deepseek-v4", apiKey: "k"}
result, err := answerWithoutPanic(t, p)
if err == nil {
t.Fatalf("deepseek-v4 answered %+v with no error", result)
}
if want := fmt.Sprintf("unknown model type: %s", "deepseek-v4"); !strings.Contains(err.Error(), want) {
t.Errorf("error = %q, want it to name the model (%q)", err, want)
}
}