Files
ai/controllers/auto_route_org_test.go
zeekay bd8dbba1fc ai: expose the OpenRouter catalog to paying callers
OpenRouter was seeded as a provider and its upstream already resolved for
chat, embeddings, and rerank, but none of its catalog was reachable: nothing
listed a single one of its models, so the entire lineup was dark.

Expose it as a third model family rather than a second catalog mechanism. The
family value already carries everything discovery needs -- a name, a base URL,
a service key, a TTL-refreshed snapshot -- so openrouter discovers from GET
/v1/models, lists through the same merge, routes through the same passthrough,
and is gated by the same floors as zen and enso. Two things genuinely differ,
and each is a value on the family instead of a branch in the shared machinery.
The dialect differs, because OpenRouter states price per token and advertises
neither a tier nor a funding class, so the family carries its own decoder and
the wire shape is the only thing that varies. The economics differ, because
every model in that catalog is resale: serving one spends real cash. The
decoder therefore stamps prepaid funding and a paid tier on every model it
yields, which is what makes the existing fail-closed funding gate refuse a
caller commerce cannot confirm is paying. That gate is fed, never reimplemented.

Retail is the upstream price times a configurable margin, clamped so it can
never publish below cost, and the upstream price travels beside it as COGS so
the margin ledger books the real spread instead of assuming one. A deployment
that configures no address lists and serves nothing of it.

Also stop two tests from letting a routing decision escape the test that made
it. Both drove a successful route while leaving the default sink installed, a
detached goroutine writing to the process-wide adapter that outlived the test
and raced the next in-memory database teardown. The suite failed roughly once
in twenty-five before this change and reliably under any added timing jitter.
Swapping the sink is what every other routing test already does.
2026-07-19 21:20:52 -07:00

144 lines
5.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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 (
"testing"
"github.com/hanzoai/ai/object"
"github.com/hanzoai/ai/router"
)
// emptyRouterConfig has routing globally off AND no config to route with (no
// prefer table, no endpoint). A per-org "enabled" override must NOT activate
// routing here — there is nothing to route with.
func emptyRouterConfig() *ModelConfig {
return &ModelConfig{
routes: map[string]modelRoute{},
pricing: map[string]modelPrice{},
router: RouterConfigDef{Enabled: false},
}
}
// TestAutoRoutingPrecedenceMatrix proves the full precedence table: the global
// router.enabled flag × the org's OrgSettings preference ("", "enabled",
// "disabled") decide whether an `auto` request is routed. orgAutoRoutingLookup is
// stubbed so the matrix is exercised without a live DB.
func TestAutoRoutingPrecedenceMatrix(t *testing.T) {
prevCfg := globalModelConfig
prevLookup := orgAutoRoutingLookup
prevSink := routingEventSink
defer func() {
globalModelConfig = prevCfg
orgAutoRoutingLookup = prevLookup
routingEventSink = prevSink
}()
// Capture routing events synchronously. The default sink is a DETACHED goroutine
// writing to the process-wide adapter, which outlives this test and races the
// in-memory-DB teardown of whichever test runs next (object.UseMemoryDB's restore
// reinstates the previous adapter). Every other test that resolves a route swaps
// the sink for exactly this reason; this one routes successfully six times.
routingEventSink = func(object.RoutingEvent) {}
const org = "acme"
cases := []struct {
name string
globalOn bool
orgPref string
wantRouted bool
}{
{"global-on/org-unset → route (global decides)", true, object.AutoRoutingUnset, true},
{"global-on/org-enabled → route", true, object.AutoRoutingEnabled, true},
{"global-on/org-disabled → opt out", true, object.AutoRoutingDisabled, false},
{"global-off/org-unset → no route (global decides)", false, object.AutoRoutingUnset, false},
{"global-off/org-enabled → opt in (config present)", false, object.AutoRoutingEnabled, true},
{"global-off/org-disabled → no route", false, object.AutoRoutingDisabled, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
globalModelConfig = routerTestConfig(tc.globalOn)
orgAutoRoutingLookup = func(owner string) string {
if owner == org {
return tc.orgPref
}
return object.AutoRoutingUnset
}
// "please refactor this function" classifies to code → glm-5.2.
got, _, ok := resolveAutoModel("auto", org, "", "", nil, chatReq("auto", "please refactor this function"), router.Slo{})
if ok != tc.wantRouted {
t.Fatalf("resolveAutoModel routed=%v, want %v", ok, tc.wantRouted)
}
if tc.wantRouted && got != "glm-5.2" {
t.Errorf("resolveAutoModel = %q, want glm-5.2", got)
}
if !tc.wantRouted && got != "" {
t.Errorf("resolveAutoModel returned %q with routed=false, want empty", got)
}
})
}
}
// TestAutoRoutingOrgEnabledNeedsConfig proves the guard on the per-org opt-in:
// "enabled" only activates routing when router config is actually present. With
// the global flag off AND an empty router config, "enabled" must not route.
func TestAutoRoutingOrgEnabledNeedsConfig(t *testing.T) {
prevCfg := globalModelConfig
prevLookup := orgAutoRoutingLookup
defer func() {
globalModelConfig = prevCfg
orgAutoRoutingLookup = prevLookup
}()
globalModelConfig = emptyRouterConfig()
orgAutoRoutingLookup = func(string) string { return object.AutoRoutingEnabled }
if _, _, ok := resolveAutoModel("auto", "acme", "", "", nil, chatReq("auto", "hi"), router.Slo{}); ok {
t.Error("resolveAutoModel routed with org-enabled but empty router config, want not-routed")
}
}
// TestAutoRoutingActiveUnit exercises the decision in isolation across the matrix,
// independent of the classifier, so a routing-logic regression is pinpointed here
// rather than only surfacing through resolveAutoModel.
func TestAutoRoutingActiveUnit(t *testing.T) {
on := routerTestConfig(true)
off := routerTestConfig(false)
empty := emptyRouterConfig()
cases := []struct {
cfg *ModelConfig
pref string
want bool
}{
{on, object.AutoRoutingUnset, true},
{on, object.AutoRoutingEnabled, true},
{on, object.AutoRoutingDisabled, false},
{off, object.AutoRoutingUnset, false},
{off, object.AutoRoutingEnabled, true},
{off, object.AutoRoutingDisabled, false},
{empty, object.AutoRoutingEnabled, false},
{empty, object.AutoRoutingUnset, false},
}
for _, tc := range cases {
if got := tc.cfg.AutoRoutingActive(tc.pref); got != tc.want {
t.Errorf("AutoRoutingActive(pref=%q) = %v, want %v", tc.pref, got, tc.want)
}
}
}