Files
llm-intelligence/cmd/server/main_test.go
2026-05-13 14:42:45 +08:00

79 lines
2.0 KiB
Go

package main
import (
"context"
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestSubscriptionPlansHandlerReturnsEnvelope(t *testing.T) {
mux := newMux(
&sql.DB{},
func(context.Context, *sql.DB) ([]modelResponse, error) {
return nil, nil
},
func(context.Context, *sql.DB) ([]subscriptionPlanResponse, error) {
return []subscriptionPlanResponse{
{
PlanFamily: "token_plan",
PlanCode: "token-plan-lite",
PlanName: "通用 Token Plan Lite",
Tier: "Lite",
Provider: "Tencent",
ProviderCN: "腾讯",
Operator: "Tencent Cloud",
OperatorCN: "腾讯云",
Currency: "CNY",
ListPrice: 39,
PriceUnit: "CNY/month",
QuotaValue: 35000000,
QuotaUnit: "tokens/month",
ContextWindow: 0,
ModelScope: []string{"tc-code-latest", "glm-5", "glm-5.1"},
SourceURL: "https://cloud.tencent.com/document/product/1823/130060",
EffectiveDate: "2026-04-27",
},
}, nil
},
)
req := httptest.NewRequest(http.MethodGet, "/api/v1/subscription-plans", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", rec.Code)
}
var payload struct {
Data []subscriptionPlanResponse `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
t.Fatalf("unmarshal response: %v", err)
}
if len(payload.Data) != 1 {
t.Fatalf("expected 1 plan, got %d", len(payload.Data))
}
got := payload.Data[0]
if got.PlanCode != "token-plan-lite" {
t.Fatalf("unexpected plan code: %q", got.PlanCode)
}
if got.ProviderCN != "腾讯" {
t.Fatalf("unexpected providerCN: %q", got.ProviderCN)
}
if got.OperatorCN != "腾讯云" {
t.Fatalf("unexpected operatorCN: %q", got.OperatorCN)
}
if got.ListPrice != 39 {
t.Fatalf("unexpected list price: %v", got.ListPrice)
}
if len(got.ModelScope) != 3 {
t.Fatalf("unexpected model scope length: %d", len(got.ModelScope))
}
}