mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Every billing refusal is object.BillingError: message, type, code, class, model, topup_url, actions. The wallet's refusal (denied), the policy's (limitReached) and the hold inside a handler (ApiController.refuseFunds: chat, images, speech, embeddings, video) write it, and refuse puts X-Hanzo-Usage, X-Hanzo-Usage-Reason and X-Hanzo-Topup-Url on every 402 that lacks them. The handover of a signed-in app's Free chat to Enso is the host's call per class (object.SetHandover); with no hook it stays on. Off for a class the app gets the 402 a program gets. /v1/models rows carry endpoint, the path the model is called at, so a chat picker lists the rows whose endpoint is /v1/chat/completions. Claude-Session: https://claude.ai/code/session_015XYKLnjAuGqkk4ssA1Jusy
142 lines
5.9 KiB
Go
142 lines
5.9 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 object
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestInsufficientBalanceMessage: genuine insufficiency invites adding credits at the
|
|
// wallet link, embeds the per-modality noun, and carries the 402/insufficient_balance
|
|
// verdict. The plain form (noun "") is the router-filter copy.
|
|
func TestInsufficientBalanceMessage(t *testing.T) {
|
|
n := InsufficientBalance("api.hanzo.ai", "hanzo", "image cost")
|
|
if n.Code != CodeInsufficientBalance {
|
|
t.Errorf("code=%q, want %q", n.Code, CodeInsufficientBalance)
|
|
}
|
|
if n.Status != http.StatusPaymentRequired {
|
|
t.Errorf("status=%d, want 402", n.Status)
|
|
}
|
|
if !strings.Contains(n.Message, "image cost") {
|
|
t.Errorf("message must carry the noun, got %q", n.Message)
|
|
}
|
|
if !strings.Contains(n.Message, "Add credits") || !strings.Contains(n.Message, PayURL("api.hanzo.ai", "hanzo")) {
|
|
t.Errorf("message must invite adding credits at the wallet link, got %q", n.Message)
|
|
}
|
|
|
|
plain := InsufficientBalance("api.hanzo.ai", "hanzo", "")
|
|
if strings.Contains(plain.Message, "estimated") {
|
|
t.Errorf("plain form must omit the noun clause, got %q", plain.Message)
|
|
}
|
|
if !strings.HasPrefix(plain.Message, "Insufficient balance. Add credits") {
|
|
t.Errorf("plain form copy drift, got %q", plain.Message)
|
|
}
|
|
}
|
|
|
|
// TestBalanceUnavailableMessage: a lookup failure is retryable (503) with the DISTINCT
|
|
// balance_unavailable code and, critically, does NOT tell the caller to add credits —
|
|
// the funded-wallet-402 fix. It carries no wallet link (adding credits is not the fix).
|
|
func TestBalanceUnavailableMessage(t *testing.T) {
|
|
n := BalanceUnavailable()
|
|
if n.Code != CodeBalanceUnavailable {
|
|
t.Errorf("code=%q, want %q", n.Code, CodeBalanceUnavailable)
|
|
}
|
|
if n.Status != http.StatusServiceUnavailable {
|
|
t.Errorf("status=%d, want 503 (retryable)", n.Status)
|
|
}
|
|
if strings.Contains(strings.ToLower(n.Message), "add credits") {
|
|
t.Errorf("transient denial must NOT say add credits, got %q", n.Message)
|
|
}
|
|
if strings.Contains(n.Message, "hanzo.ai/pay") {
|
|
t.Errorf("transient denial carries no wallet link, got %q", n.Message)
|
|
}
|
|
if !strings.Contains(strings.ToLower(n.Message), "retry") {
|
|
t.Errorf("transient denial should invite a retry, got %q", n.Message)
|
|
}
|
|
}
|
|
|
|
// TestPayURL: the deep-link is the caller's OWN wallet, resolved by the pay SPA from
|
|
// the authenticated identity. The pay SPA has no /<org> route today (pay/src/main.tsx),
|
|
// so PayURL returns the bare wallet root for every org — never a broken /<org> path,
|
|
// and never another org's id in the URL. An anonymous denial (org "") degrades to the
|
|
// same bare link.
|
|
func TestPayURL(t *testing.T) {
|
|
for _, org := range []string{"hanzo", "acme", "", "weird/slug with spaces", "a?b#c"} {
|
|
if got := PayURL("api.hanzo.ai", org); got != payBaseURL {
|
|
t.Errorf("PayURL(%q)=%q, want the bare wallet root %q (no /<org> route exists yet)", org, got, payBaseURL)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestErrorJSONShape locks the raw-HTTP wire shape the router balance filter emits:
|
|
// {"error":{"message":..,"type":"billing_error","code":..}}. The hanzo CLI switches on
|
|
// "code", so the keys and the always-"billing_error" type must not drift. The message
|
|
// is JSON-marshaled (never string-concatenated), so it stays correctly escaped.
|
|
func TestErrorJSONShape(t *testing.T) {
|
|
for _, n := range []BillingNotice{InsufficientBalance("api.hanzo.ai", "hanzo", "cost"), BalanceUnavailable()} {
|
|
var parsed struct {
|
|
Error struct {
|
|
Message string `json:"message"`
|
|
Type string `json:"type"`
|
|
Code string `json:"code"`
|
|
} `json:"error"`
|
|
}
|
|
if err := json.Unmarshal(n.ErrorJSON(), &parsed); err != nil {
|
|
t.Fatalf("ErrorJSON is not valid JSON: %v (%s)", err, n.ErrorJSON())
|
|
}
|
|
if parsed.Error.Type != "billing_error" {
|
|
t.Errorf("wire type=%q, want billing_error", parsed.Error.Type)
|
|
}
|
|
if parsed.Error.Code != n.Code {
|
|
t.Errorf("wire code=%q, want %q", parsed.Error.Code, n.Code)
|
|
}
|
|
if parsed.Error.Message != n.Message {
|
|
t.Errorf("wire message=%q, want %q (escaping must round-trip)", parsed.Error.Message, n.Message)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestInsufficientBalanceNamesTheTopUp: a refusal that adds funds is the remedy for says
|
|
// where, in the body (topup_url and the topup action) and on the wire (the headers), and
|
|
// names the model and class it is about. One that does not (a failed lookup) names none.
|
|
func TestInsufficientBalanceNamesTheTopUp(t *testing.T) {
|
|
n := InsufficientBalance("api.hanzo.ai", "hanzo", "").For("anthropic/claude-opus-5.5", ClassPremium)
|
|
var body struct {
|
|
Error BillingError `json:"error"`
|
|
}
|
|
if err := json.Unmarshal(n.ErrorJSON(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pay := PayURL("api.hanzo.ai", "hanzo")
|
|
e := body.Error
|
|
if e.Code != CodeInsufficientBalance || e.TopupURL != pay || e.Model != "anthropic/claude-opus-5.5" || e.Class != ClassPremium {
|
|
t.Errorf("body %+v", e)
|
|
}
|
|
if len(e.Actions) != 1 || e.Actions[0] != TopupAction(pay) {
|
|
t.Errorf("actions %+v, want the top-up alone", e.Actions)
|
|
}
|
|
h := n.Headers()
|
|
if h["X-Hanzo-Usage-Reason"] != CodeInsufficientBalance || h["X-Hanzo-Topup-Url"] != pay || h["X-Hanzo-Usage"] != "limited" || h["X-Hanzo-Usage-Class"] != ClassPremium {
|
|
t.Errorf("headers %v", h)
|
|
}
|
|
|
|
if u := BalanceUnavailable(); u.Headers() != nil || strings.Contains(string(u.ErrorJSON()), "topup") {
|
|
t.Errorf("a failed lookup names a top-up: %v %s", u.Headers(), u.ErrorJSON())
|
|
}
|
|
}
|