Files
ai/controllers/model_access_test.go
zeekayandHanzo Dev dca2c9403f the family refusal names the gate that refused
Three gates guard a family SKU — the subscription floor, the funding
floor, and the access grant — and every refusal spoke the grant's
sentence: 'limited preview — request access'. A caller whose plan was
the blocker was sent to a waitlist that cannot open for them; the cure
for their refusal is an upgrade, and nothing said so. This is how the
app builder read as broken while every gate was doing its job.

familyRefusal is now the one statement of the decision and its order,
returning the sentence of whichever gate refused; familyAccessAllowed
derives from it so predicate and message cannot disagree. The serve
pipe relays that sentence. The request-access instruction survives
only where it is true: an actually-gated SKU without a grant.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-04 18:55:33 -07:00

155 lines
5.9 KiB
Go

// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
package controllers
import (
"strings"
"testing"
iam "github.com/hanzoai/ai/internal/iam"
"github.com/hanzoai/ai/object"
)
// A family SKU that advertises access:"waitlist" is parsed as gated.
func TestZenModelGatedParse(t *testing.T) {
gated := zenWireModel{ID: "enso", Access: "waitlist"}.model()
if !gated.gated() {
t.Fatal("enso (access=waitlist) should parse as gated")
}
open := zenWireModel{ID: "zen5"}.model()
if open.gated() {
t.Fatal("zen5 (no access) should not be gated")
}
}
// familyAccessAllowed is the enforcement decision: a non-gated model is always
// allowed; a gated model with no grant (no DB / no row) is denied — closed by default.
func TestFamilyAccessAllowedDecision(t *testing.T) {
fam := &modelFamily{name: "enso", prefix: "enso"}
fam.byID = map[string]zenModel{
"enso": {ID: "enso", Access: "waitlist"},
"open": {ID: "open"},
}
c := &ApiController{}
// Non-gated model → allowed regardless of caller.
if !c.familyAccessAllowed(fam, "open", "acme", nil) {
t.Fatal("non-gated model must be allowed")
}
// Model unknown to discovery → treated as non-gated (allowed).
if !c.familyAccessAllowed(fam, "mystery", "acme", nil) {
t.Fatal("model unknown to discovery must be allowed (not gated)")
}
// Gated model, no grant row (no DB in test) → denied.
if c.familyAccessAllowed(fam, "enso", "acme", &iam.User{Owner: "acme", Name: "bob"}) {
t.Fatal("gated model without a grant must be denied")
}
}
// FamilyModelGated keys off discovery: a gated SKU is recognized as access-gated.
// Enso is PAID — gating controls ACCESS (a grant unlocks the SKU), never billing;
// there is no comp path, so a granted caller still pays the normal balance gate.
func TestFamilyModelGatedDecision(t *testing.T) {
// Seed the enso family's discovered catalog with a gated SKU.
saved := ensoFam.byID
ensoFam.byID = map[string]zenModel{"enso": {ID: "enso", Access: "waitlist"}}
defer func() { ensoFam.byID = saved }()
if !FamilyModelGated("enso") {
t.Fatal("enso should be recognized as gated")
}
if FamilyModelGated("zen5") {
t.Fatal("zen5 is not gated")
}
}
// The gated denial message tells the caller exactly how to request access.
func TestGatedAccessMessage(t *testing.T) {
msg := gatedAccessMessage("enso")
if !strings.Contains(msg, "limited preview") || !strings.Contains(msg, "POST /v1/models/enso/access") {
t.Fatalf("gated message missing the request-access instruction: %q", msg)
}
}
// mergeModels stamps a gated SKU with the default "waitlist" standing so /v1/models
// advertises the gating to everyone (the caller's real status is overlaid later).
func TestMergeModelsGatedAnnotation(t *testing.T) {
fam := &modelFamily{name: "enso", prefix: "enso"}
fam.byID = map[string]zenModel{
"enso": {ID: "enso", Access: "waitlist", Base: zenTier{}},
}
fam.ids = []string{"enso"}
fam.loaded = true // enabled() is false (no urlKey) so snapshot() uses this data directly
out := fam.mergeModels(nil)
if len(out) != 1 {
t.Fatalf("mergeModels returned %d models, want 1", len(out))
}
if out[0].Access == nil || out[0].Access.State != "waitlist" {
t.Fatalf("gated SKU access = %+v, want state=waitlist", out[0].Access)
}
}
// annotateModelAccess is a no-op without an authenticated caller (nothing to overlay).
func TestAnnotateModelAccessNilUser(t *testing.T) {
models := []modelInfo{{ID: "enso", Access: &modelAccessInfo{State: "waitlist"}}}
annotateModelAccess(models, nil)
if models[0].Access.State != "waitlist" {
t.Fatal("annotate with nil user must not change the default standing")
}
}
// The refusal names the gate that refused: a plan-refused caller is told to
// upgrade, a funding-refused caller is told the capacity is paid-only, and only
// a grant-refused caller is sent to the access waitlist. The old shared message
// sent all three there — including callers whose plan was the blocker, for whom
// that door cannot open.
func TestFamilyRefusalNamesTheGate(t *testing.T) {
catalog := map[string]zenModel{
"enso": {ID: "enso", MinTier: "trial"},
"enso-pre": {ID: "enso-pre", Funding: "prepaid"},
"enso-prev": {ID: "enso-prev", Access: "waitlist"},
}
fam := &modelFamily{name: "enso", prefix: "enso"}
fam.byID = catalog
savedByID := ensoFam.byID
ensoFam.byID = catalog
t.Cleanup(func() { ensoFam.byID = savedByID })
// The funding gate only speaks for models a family SERVES, and serving
// requires a configured provider — point the family at one for the test.
savedProv := ensoFam.providerFn
ensoFam.providerFn = func() *object.Provider { return &object.Provider{ProviderUrl: "http://enso.test"} }
t.Cleanup(func() { ensoFam.providerFn = savedProv })
savedTier := familyTier
familyTier = func(string) string { return "free" }
t.Cleanup(func() { familyTier = savedTier })
c := &ApiController{}
// Plan below the floor → the upgrade sentence, naming the floor, never the waitlist.
msg := c.familyRefusal(fam, "enso", "acme", nil)
if !strings.Contains(msg, "trial") || strings.Contains(msg, "limited preview") {
t.Fatalf("tier refusal = %q, want the upgrade sentence naming trial", msg)
}
// Prepaid capacity, unconfirmed subscriber → the prepaid sentence.
msg = c.familyRefusal(fam, "enso-pre", "acme", nil)
if !strings.Contains(msg, "prepaid") || strings.Contains(msg, "limited preview") {
t.Fatalf("funding refusal = %q, want the prepaid sentence", msg)
}
// Gated SKU without a grant → the request-access sentence, unchanged.
msg = c.familyRefusal(fam, "enso-prev", "acme", &iam.User{Owner: "acme", Name: "bob"})
if !strings.Contains(msg, "limited preview") || !strings.Contains(msg, "POST /v1/models/enso-prev/access") {
t.Fatalf("grant refusal = %q, want the request-access sentence", msg)
}
// A servable SKU refuses nothing: paid caller, no gate in the way.
familyTier = func(string) string { return "pro" }
if msg := c.familyRefusal(fam, "enso", "acme", nil); msg != "" {
t.Fatalf("paid caller refusal = %q, want none", msg)
}
}