Files
ai/controllers/model_routes_test.go
zeekayandHanzo Dev 3fbc78dbb1 audio: the deployed speech service becomes a model the endpoints can name
/v1/audio/transcriptions was mounted and the speech service was READY, with
nothing between them: the endpoint resolves `model` through the same route
table as chat, and no audio model was in it. Every call died at "model is not
available" before a byte reached the pod — /v1/models listed no whisper and no
kokoro, and the pod's log held only /healthz.

Three edits close it, each on the mechanism that already registers every other
model:

  - modelRoutes + conf/models.yaml gain whisper, whisper-small and kokoro,
    user-facing name == upstream id like the embeddings passthroughs.
  - initLLMProviders seeds the `speech` row that those routes name, pointing at
    http://speech.hanzo.svc/v1. No ClientSecret: the upstream is a ClusterIP
    with no ingress and no auth, and both relays send an Authorization header
    only when the secret is non-empty.
  - tts gains its OpenAI branch. STT had one (stt/openai.go); TTS knew only
    "Alibaba Cloud", so the same row that could transcribe fell through the TTS
    factory as (nil, nil) and surfaced as "the TTS provider type: OpenAI is not
    supported". Hearing without speaking is half a voice agent.

Both audio paths — HTTP and ZAP — reach these through the object layer, so one
wiring serves both, and every call meters through the one funnel already.

SeededModelProviders projects the whole row rather than the URL alone: the
audio invariants need the Type the factories switch on, and one complete
accessor beats a second projection drifting alongside the first.

TestModelRoutes_ProviderNamesAreKnown now DERIVES the known set from the seed
table and the family list. Its hand-written copy went stale the moment a
provider was seeded correctly, failing backwards — reporting a working route as
unknown, where the cure looks like deleting the route.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-07 06:09:12 -07:00

258 lines
8.8 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"strings"
"testing"
"github.com/hanzoai/ai/object"
)
// ── resolveModelRoute ────────────────────────────────────────────────────────
func TestResolveModelRoute_KnownModels(t *testing.T) {
cases := []struct {
input string
wantProvider string
wantModel string
wantPremium bool
}{
// DO-AI free-tier
{"gpt-4o", "do-ai", "openai-gpt-4o", false},
{"gpt-5", "do-ai", "openai-gpt-5", false},
{"claude-opus-4-6", "do-ai", "anthropic-claude-opus-4.6", false},
{"qwen3-32b", "do-ai", "alibaba-qwen3-32b", false},
// Aliases (free-tier)
{"openai/gpt-4o", "do-ai", "openai-gpt-4o", false},
{"anthropic/claude-opus-4-6", "do-ai", "anthropic-claude-opus-4.6", false},
// Fireworks premium
{"fireworks/glm-5", "fireworks", "accounts/fireworks/models/glm-5", true},
{"fireworks/qwen3-8b", "fireworks", "accounts/fireworks/models/qwen3-8b", true},
// OpenAI direct premium
{"openai-direct/gpt-5", "openai-direct", "gpt-5", true},
{"openai-direct/o3", "openai-direct", "o3", true},
// Zen branded premium (routed through DigitalOcean GenAI / "do-ai")
// zen3-omni used to resolve to glm-5 — a TEXT model. An "omni" alias that
// serves a text-only model is a promise we do not keep. DO's actual
// omni model is nemotron-3-nano-omni (verified working).
}
for _, tc := range cases {
t.Run(tc.input, func(t *testing.T) {
route := resolveModelRoute(tc.input)
if route == nil {
t.Fatalf("resolveModelRoute(%q) = nil, want non-nil", tc.input)
}
if route.providerName != tc.wantProvider {
t.Errorf("providerName = %q, want %q", route.providerName, tc.wantProvider)
}
if route.upstreamModel != tc.wantModel {
t.Errorf("upstreamModel = %q, want %q", route.upstreamModel, tc.wantModel)
}
if route.premium != tc.wantPremium {
t.Errorf("premium = %v, want %v", route.premium, tc.wantPremium)
}
})
}
}
func TestResolveModelRoute_CaseInsensitive(t *testing.T) {
// Keys in the map are lowercase; make sure uppercase input still resolves.
route := resolveModelRoute("GPT-4O")
if route == nil {
t.Fatal("resolveModelRoute(\"GPT-4O\") = nil, want match")
}
if route.providerName != "do-ai" {
t.Errorf("providerName = %q, want \"do-ai\"", route.providerName)
}
}
func TestResolveModelRoute_UnknownReturnsNil(t *testing.T) {
unknowns := []string{
"nonexistent-model",
"",
"gpt-99",
"fireworks/nonexistent",
}
for _, name := range unknowns {
if route := resolveModelRoute(name); route != nil {
t.Errorf("resolveModelRoute(%q) = %+v, want nil", name, route)
}
}
}
// ── Routing table integrity ──────────────────────────────────────────────────
func TestModelRoutes_KeysAreLowercase(t *testing.T) {
for key := range modelRoutes {
if key != strings.ToLower(key) {
t.Errorf("routing table key %q is not lowercase", key)
}
}
}
func TestModelRoutes_NoDuplicateUpstreamForSameProvider(t *testing.T) {
// Guard against accidentally mapping two user-facing names to the exact
// same (provider, upstream) pair—aliases are fine when intentional, but
// catch accidental copy-paste duplication within a single provider section.
//
// We keep this test informational: it logs duplicates rather than failing,
// since some aliases intentionally share an upstream (e.g. "gpt-4o" and
// "openai/gpt-4o"). If this ever fires unexpectedly, investigate.
type key struct{ provider, upstream string }
seen := make(map[key][]string)
for name, route := range modelRoutes {
k := key{route.providerName, route.upstreamModel}
seen[k] = append(seen[k], name)
}
for k, names := range seen {
if len(names) > 2 {
// More than 2 user-facing names pointing to the exact same upstream
// is suspicious. Log, don't fail—but make it visible.
t.Logf("WARNING: %d names share provider=%q upstream=%q: %v",
len(names), k.provider, k.upstream, names)
}
}
}
func TestModelRoutes_NoEmptyFields(t *testing.T) {
for name, route := range modelRoutes {
if route.providerName == "" {
t.Errorf("model %q has empty providerName", name)
}
if route.upstreamModel == "" {
t.Errorf("model %q has empty upstreamModel", name)
}
}
}
// TestModelRoutes_ProviderNamesAreKnown asserts every route names a provider
// something actually creates: a seeded row (initLLMProviders) or a model family
// (whose address is deployment config, so it has no row).
//
// The set is DERIVED from those two sources rather than hand-listed. A
// hand-written copy of the seed table is the second-list bug this package has
// already paid for twice — familyForProviderType and GetModelProviderByName were
// both rewritten to remove one — and it fails backwards: seeding a provider
// correctly makes the copy stale, so the test reports a WORKING route as unknown
// and the cure looks like deleting the route.
func TestModelRoutes_ProviderNamesAreKnown(t *testing.T) {
known := map[string]bool{}
for name := range object.SeededModelProviders() {
known[name] = true
}
for _, name := range object.FamilyProviderNames() {
known[name] = true
}
for name, route := range modelRoutes {
if !known[route.providerName] {
t.Errorf("model %q uses unknown provider %q", name, route.providerName)
}
}
}
// ── listAvailableModels ──────────────────────────────────────────────────────
func TestListAvailableModels_ReturnsSortedList(t *testing.T) {
models := listAvailableModels()
if len(models) == 0 {
t.Fatal("listAvailableModels() returned empty slice")
}
// Count visible (non-hidden) models in the routing table
visibleCount := 0
for _, route := range modelRoutes {
if !route.hidden {
visibleCount++
}
}
if cfg := GetModelConfig(); cfg != nil {
visibleCount = len(cfg.ListModels())
}
if len(models) != visibleCount {
t.Errorf("listAvailableModels() returned %d models, want %d",
len(models), visibleCount)
}
// Verify sorted by ID
for i := 1; i < len(models); i++ {
if models[i].ID < models[i-1].ID {
t.Errorf("models not sorted: %q comes after %q",
models[i].ID, models[i-1].ID)
}
}
// Verify JSON object field
for _, m := range models {
if m.Object != "model" {
t.Errorf("model %q has Object=%q, want \"model\"", m.ID, m.Object)
}
if m.OwnedBy == "" {
t.Errorf("model %q has empty OwnedBy", m.ID)
}
}
}
func TestListAvailableModels_CountSanity(t *testing.T) {
models := listAvailableModels()
// As of 2026-02: 41 visible models (hidden aliases/prefixed routes excluded from listing).
// Adjust if routes are added/removed. This is a canary for unexpected drift.
if len(models) < 30 {
t.Errorf("expected at least 30 visible models, got %d", len(models))
}
}
// ── resolveModelRouteForOrg ──────────────────────────────────────────────────
func TestResolveModelRouteForOrg_FallsBackToStatic(t *testing.T) {
// When DB adapter is nil (as in tests), resolveModelRouteForOrg should
// fall back to the static routing table for any org.
route := resolveModelRouteForOrg("gpt-4o", "some-org")
if route == nil {
t.Fatal("resolveModelRouteForOrg(\"gpt-4o\", \"some-org\") = nil, want non-nil from static fallback")
}
if route.providerName != "do-ai" {
t.Errorf("provider = %q, want %q", route.providerName, "do-ai")
}
}
func TestResolveModelRouteForOrg_UnknownModelReturnsNil(t *testing.T) {
route := resolveModelRouteForOrg("nonexistent-model-xyz", "hanzo")
if route != nil {
t.Errorf("resolveModelRouteForOrg(\"nonexistent-model-xyz\", \"hanzo\") = %+v, want nil", route)
}
}
func TestResolveModelRouteForOrg_EmptyOrgFallsBack(t *testing.T) {
// Empty org resolves from the static map
route := resolveModelRouteForOrg("glm-5.2", "")
if route == nil {
t.Fatal("resolveModelRouteForOrg(\"glm-5.2\", \"\") = nil, want non-nil")
}
if route.providerName != "do-ai" {
t.Errorf("provider = %q, want %q", route.providerName, "do-ai")
}
if !route.premium {
t.Error("glm-5.2 should be premium")
}
}