mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
A call through the gateway bills the price of the arm that answered and the platform's fee on top, and nothing else: 5.5% on Hanzo's accounts, 1% on the org's own key, both the host's settings (object.Fee: hanzo, hanzo_byok). A vendor's fee is part of the cost: a call OpenRouter's credit bought cost its stated price plus 5.5%, and the platform's fee goes on that. - Enso answers bill their cost of record (X-Hanzo-Cogs and the buying vendor's fee) times the platform's fee; a maker's answer the same; a resold OpenRouter SKU its vendor price times both fees. An answer that states no cost bills the SKU's listed ceiling. A call billed below its cost writes loss_nano and a warning. - hanzo.cloud_usage gains reasoning, reasoning_tokens, served, vendor, arms and loss_nano: each Enso row names the arm that answered, its rate at the time and the tokens it was paid for, and each arm that failed first. - rates.go prices every Enso SKU at each depth: from (its cheapest arm), up to (its dearest, a long prompt included), and typical (its measured cost a token over the last day, or its last 50 calls), the arms read from the engine's admin catalog. /v1/models lists the typical rate with from, up_to, fee and window beside it; Prices, Price and Blend serve the rest. The board's ledger counts answers, failovers, OpenRouter-bought answers, fee and losses by model and vendor.
208 lines
8.1 KiB
Go
208 lines
8.1 KiB
Go
// Copyright 2023-2026 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 (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
"github.com/hanzoai/ai/object"
|
|
)
|
|
|
|
const rawConnKey = "sk-raw-super-secret-do-not-persist-1234567890"
|
|
|
|
// TestConnectionSpecs pins the exact provider Type/Name mapping the login-manager
|
|
// stores rows as — the canonical Type strings the completion paths key on.
|
|
func TestConnectionSpecs(t *testing.T) {
|
|
want := map[string]struct{ typ, name string }{
|
|
"openai": {"OpenAI", "openai"},
|
|
"anthropic": {"Anthropic", "anthropic"},
|
|
"google": {"Gemini", "google"},
|
|
// OpenAI-compatible providers all type as "OpenAI" pointed at their own URL.
|
|
"mistral": {"OpenAI", "mistral"},
|
|
"together": {"OpenAI", "together"},
|
|
"fireworks": {"OpenAI", "fireworks"},
|
|
"xai": {"OpenAI", "xai"},
|
|
"cohere": {"OpenAI", "cohere"},
|
|
"huggingface": {"OpenAI", "huggingface"},
|
|
}
|
|
for slug, w := range want {
|
|
spec, ok := aiConnSpecFor(slug)
|
|
if !ok {
|
|
t.Fatalf("aiConnSpecFor(%q) not found", slug)
|
|
}
|
|
if spec.typ != w.typ || spec.name != w.name {
|
|
t.Errorf("spec %q = {type:%q name:%q}, want {type:%q name:%q}", slug, spec.typ, spec.name, w.typ, w.name)
|
|
}
|
|
}
|
|
// replicate is deliberately NOT in the allow-list — its predictions API is not
|
|
// OpenAI-compatible, so it cannot back an "OpenAI"-typed BYOK override row.
|
|
if _, ok := aiConnSpecFor("replicate"); ok {
|
|
t.Error("aiConnSpecFor(replicate) = ok, want rejected (not OpenAI-compatible)")
|
|
}
|
|
if _, ok := aiConnSpecFor("notaprovider"); ok {
|
|
t.Error("aiConnSpecFor(notaprovider) = ok, want rejected (unknown)")
|
|
}
|
|
if _, ok := aiConnSpecFor("OpenAI"); !ok {
|
|
t.Error("aiConnSpecFor is not case-insensitive")
|
|
}
|
|
}
|
|
|
|
// TestConnectionSecretNameDeterministic proves the KMS secret name is stable per
|
|
// (org, provider) so upsert + delete address the same secret, and reuses the ONE
|
|
// BYOK sealing convention (byokSecretName).
|
|
func TestConnectionSecretNameDeterministic(t *testing.T) {
|
|
a := connectionSecretName("acme", "openai")
|
|
b := connectionSecretName("acme", "openai")
|
|
if a != b {
|
|
t.Errorf("connectionSecretName not deterministic: %q vs %q", a, b)
|
|
}
|
|
if a != byokSecretName("acme", "openai") {
|
|
t.Errorf("connectionSecretName %q != byokSecretName convention %q", a, byokSecretName("acme", "openai"))
|
|
}
|
|
if connectionSecretName("acme", "openai") == connectionSecretName("acme", "google") {
|
|
t.Error("distinct providers must map to distinct secret names")
|
|
}
|
|
if strings.Contains(a, rawConnKey) {
|
|
t.Error("secret NAME must not embed a key")
|
|
}
|
|
}
|
|
|
|
// TestBuildConnectionProvider_SealInvariant is the SEAL INVARIANT: buildConnection
|
|
// Provider is the ONLY constructor of a connection row, and it copies the sealed
|
|
// "kms://…" reference into ClientSecret verbatim — the raw key is never even an
|
|
// argument, so it can never land in the persisted row.
|
|
func TestBuildConnectionProvider_SealInvariant(t *testing.T) {
|
|
spec, _ := aiConnSpecFor("openai")
|
|
ref := "kms://" + connectionSecretName("acme", "openai")
|
|
|
|
row := buildConnectionProvider("acme", spec, ref)
|
|
|
|
if row.ClientSecret != ref {
|
|
t.Errorf("ClientSecret = %q, want the kms ref %q", row.ClientSecret, ref)
|
|
}
|
|
if !strings.HasPrefix(row.ClientSecret, "kms://") {
|
|
t.Errorf("ClientSecret %q is not a kms:// reference", row.ClientSecret)
|
|
}
|
|
if strings.Contains(row.ClientSecret, rawConnKey) {
|
|
t.Fatalf("SEAL INVARIANT VIOLATED: row persisted the raw key: %q", row.ClientSecret)
|
|
}
|
|
if row.Owner != "acme" || row.Name != "openai" || row.Type != "OpenAI" || row.Category != "Model" || row.State != "Active" {
|
|
t.Errorf("row shape wrong: %+v", row)
|
|
}
|
|
}
|
|
|
|
// TestStoreProviderSecret_FailsClosed proves the handler's fail-closed path: with
|
|
// KMS unconfigured (the unit env), sealing a raw key ERRORS rather than returning a
|
|
// ref — so AddAIConnection returns 503 and NEVER builds/writes a row holding the
|
|
// raw key. The returned ref is empty (no plaintext leaks back to the caller).
|
|
func TestStoreProviderSecret_FailsClosed(t *testing.T) {
|
|
ref, err := object.StoreProviderSecret(connectionSecretName("acme", "openai"), rawConnKey)
|
|
if err == nil {
|
|
t.Skip("KMS is configured in this env — fail-closed path not exercisable here")
|
|
}
|
|
if ref != "" {
|
|
t.Errorf("fail-closed seal returned a non-empty ref %q, want empty (no plaintext)", ref)
|
|
}
|
|
if strings.Contains(err.Error(), rawConnKey) {
|
|
t.Error("error message leaked the raw key")
|
|
}
|
|
}
|
|
|
|
// TestConnectionView_NoLeak is the NO-LEAK INVARIANT: a connected provider's public
|
|
// view reports connected:true yet its serialized JSON contains neither the raw key
|
|
// (resolved env-first) nor the "kms://…" reference. Env-var-first lets ProviderKey
|
|
// Present confirm the key without a live KMS (mirrors ResolveProviderSecret).
|
|
func TestConnectionView_NoLeak(t *testing.T) {
|
|
spec, _ := aiConnSpecFor("openai")
|
|
secretName := connectionSecretName("acme", "openai")
|
|
ref := "kms://" + secretName
|
|
// Env-first: ProviderKeyPresent resolves this without touching KMS.
|
|
t.Setenv(secretName, rawConnKey)
|
|
|
|
row := buildConnectionProvider("acme", spec, ref)
|
|
view := connectionView("openai", row)
|
|
|
|
if !view.Connected {
|
|
t.Fatal("connected = false for a present, Active key, want true")
|
|
}
|
|
if view.AccountLabel != "acme/openai" {
|
|
t.Errorf("account_label = %q, want acme/openai", view.AccountLabel)
|
|
}
|
|
|
|
blob, err := json.Marshal(view)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
js := string(blob)
|
|
if strings.Contains(js, rawConnKey) {
|
|
t.Fatalf("NO-LEAK VIOLATED: response leaked the raw key: %s", js)
|
|
}
|
|
if strings.Contains(js, "kms://") || strings.Contains(js, secretName) {
|
|
t.Fatalf("NO-LEAK VIOLATED: response leaked the kms ref: %s", js)
|
|
}
|
|
|
|
// A missing row → not connected, no label. A deactivated (disconnected) row →
|
|
// not connected even though its sealed ref lingers.
|
|
if v := connectionView("openai", nil); v.Connected {
|
|
t.Error("nil row reported connected")
|
|
}
|
|
disabled := buildConnectionProvider("acme", spec, ref)
|
|
disabled.State = "Disabled"
|
|
if v := connectionView("openai", disabled); v.Connected {
|
|
t.Error("disabled (disconnected) row reported connected")
|
|
}
|
|
}
|
|
|
|
// TestProviderBYO_FeeOnce ties PART 2 (BYO classification) to PART 1 (fee): an
|
|
// org-owned provider is BYO and bills the 1% fee; the global Hanzo built-in is not
|
|
// BYO and bills no surcharge (the full token cost path). FEE-ONCE.
|
|
func TestProviderBYO_FeeOnce(t *testing.T) {
|
|
fees(t, map[string]string{"hanzo_byok": "1"})
|
|
user := &iam.User{Owner: "acme", Name: "z"}
|
|
|
|
// Org's OWN connected account → BYO, fee applies.
|
|
own := &object.Provider{Owner: "acme", Name: "openai"}
|
|
byo, account := providerBYO(own, user)
|
|
if !byo || account != "acme/openai" {
|
|
t.Fatalf("providerBYO(org-owned) = (%v, %q), want (true, acme/openai)", byo, account)
|
|
}
|
|
if fee := platformFeeCents(10000, byo); fee != 100 {
|
|
t.Errorf("BYO fee on 10000c = %d, want 100 (1%%)", fee)
|
|
}
|
|
|
|
// Global Hanzo built-in ("admin") → not BYO, no surcharge.
|
|
global := &object.Provider{Owner: globalProviderOwner, Name: "do-ai"}
|
|
byo, account = providerBYO(global, user)
|
|
if byo || account != "hanzo" {
|
|
t.Fatalf("providerBYO(global) = (%v, %q), want (false, hanzo)", byo, account)
|
|
}
|
|
if fee := platformFeeCents(10000, byo); fee != 0 {
|
|
t.Errorf("Hanzo-served fee = %d, want 0", fee)
|
|
}
|
|
|
|
// Another org's row must NOT be treated as this caller's BYO account.
|
|
foreign := &object.Provider{Owner: "other-org", Name: "openai"}
|
|
if byo, _ := providerBYO(foreign, user); byo {
|
|
t.Error("providerBYO cross-org = true, want false")
|
|
}
|
|
// Nil provider / nil user → Hanzo-served.
|
|
if byo, acct := providerBYO(nil, user); byo || acct != "hanzo" {
|
|
t.Error("providerBYO(nil provider) not Hanzo-served")
|
|
}
|
|
}
|