Files
ai_academy/internal/models/api/usage_log_test.go
T
runny 7ec21f35b0 chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
2026-11-16 18:12:23 +08:00

35 lines
1008 B
Go

package api
import (
"context"
"testing"
"academy/internal/types"
)
func TestUsageAttributionEmptyOutsideSessions(t *testing.T) {
// Calls without session or principal context (document parsing, title
// generation, background jobs) must keep the usage line byte-identical.
if got := usageAttribution(context.Background()); got != "" {
t.Fatalf("expected empty suffix, got %q", got)
}
}
func TestUsageAttributionCarriesSessionAndPrincipal(t *testing.T) {
ctx := types.WithSessionID(context.Background(), "sess-123")
ctx = types.WithPrincipal(ctx, types.Principal{Type: types.PrincipalAPIExternalUser, ID: "10000:42"})
got := usageAttribution(ctx)
want := ", session_id=sess-123, principal=api_external_user:10000:42"
if got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestUsageAttributionSessionOnly(t *testing.T) {
ctx := types.WithSessionID(context.Background(), "sess-9")
if got := usageAttribution(ctx); got != ", session_id=sess-9" {
t.Fatalf("got %q", got)
}
}