* test(control): 消除测试 fixture 中的重复密钥派生 - internal/control:fixture 构造中 388 次重复执行 PBKDF2(10 万轮 SHA256)密钥派生,改为共享单例,仅派生一次 - 新增 internal/testutil/encryptiontest,提供按 key material 值 memoize 的共享 encryption.Service,横向覆盖 gateway、subscription、 execution/cpa 中同样存在的重复派生(PBKDF2 占比最高达 79%) - aesService 构造后只读,跨 fixture(含并行测试)共享安全;container 包同类调用经排查确认属于被测装配流程本身,不适用本次改造 * test(state): 迁移测试改用共享 schema 模板 state/loader 中 openMigratedDatabase 被 21 次调用,每次都完整走一遍 迁移注册表;改为复用 sqlitetest.OpenMigrated 的模板缓存,与 control、 requestlog 部分测试已采用的方式一致。app、container、requestlog 中同 类调用点经逐点排查,均属被测对象本身或依赖真实文件/账本状态,不适用。 * ci(release): 消除 CI 与发布流水线中的耗时浪费 - race-tests 独立 setup-go 缓存命名空间,避免与 test/database-contract 共用 key 导致 race-instrumented GOCACHE 永远无法写回 - 移除 publish-images 中零命中的 GHA buildx image cache,同时消除其 失败会拖累发布状态判定为 partial/blocked 的关联风险 - 三个只读 job(仅用 buildx imagetools 查询)移除多余的 buildx 初始化 - native-artifact-smoke 改为按需下载单个 binary + 独立 checksums, 不再下载完整 release-assets * test(control): 第一批测试标记 t.Parallel(35 个文件) internal/control 651 个顶层测试里只有 5.8% 标了 t.Parallel(),其余 在包内是排队串行执行的;race-tests 总耗时现在完全由该包的最长执行时 间决定(213s),是唯一剩余的瓶颈。 本批覆盖 35 个文件、299 个函数。排除 18 个不适用的函数: - 16 个直接操作全局 logrus 状态的测试(catalog_sync_test.go 等 8 个 文件),继续保持串行;Go 的 t.Parallel() 调度语义保证未标记的测试 会在所有并行测试开始前完整跑完,两者不会交叉执行 - external_database_integration_test.go 的 2 个测试依赖 GPT_LOAD_DATABASE_TEST_DSN:race-tests 中因未设置而被 t.Skip,标记 并行无收益;database-contract job 中会共享同一个外部数据库实例真 实执行,标记并行有正确性风险(唯一索引冲突/断言假设独占) 已排查确认安全:t.Setenv/os.Setenv 均为 0 处,无硬编码端口占用,每个 fixture 走独立 :memory: 数据库;本地 -count=3 重复验证无 flaky。 剩余约 297 个函数(36 个文件)作为第二批,待本批 CI 实测确认收益后 再决定是否推进。 * test(control): 第二批测试标记 t.Parallel,补齐排除说明(36 个文件) 覆盖剩余 297 个函数、36 个文件,加上第一批的 299 个,internal/control 除 18 个明确排除的函数外全部标记 t.Parallel()。 为 18 个排除函数逐一补充一行注释说明原因,避免日后被误认为遗漏而被 顺手加上 t.Parallel(): - 16 个劫持全局 logrus 状态的测试 - 2 个依赖 GPT_LOAD_DATABASE_TEST_DSN 共享外部数据库的测试 本地 -count=3 重复验证、全量 go test -count=1 . ./internal/...(58 包 ok)均通过。 * fix(release): 采纳 Review 反馈,修正文档措辞与产物保留期 - encryptiontest.Service 的文档注释修正为明确说明缓存仅在单个包的 测试二进制内有效,不跨包共享(每个包的 go test 是独立进程) - native-artifact-smoke 依赖的 binary-*、release-checksums 保留期 从 1 天提高到与 release-assets 一致的 14 天:失败的 release run 若延迟超过 1 天才重试,此前较短的保留期会导致产物过期、下载失败, 阻塞发布流程 以下反馈经核实判定为不成立,未采纳: - validation_test.go 中两个测试被指"竞争全局并发槽位"——经查 newValidationWorkerForTest 每次调用构造全新独立实例(worker、 channel、mutation coordinator 等),validationConcurrency 只是 编译期常量,两个测试之间不存在任何运行时共享状态
212 lines
8.6 KiB
Go
212 lines
8.6 KiB
Go
package control
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"gpt-load/internal/catalog"
|
|
"gpt-load/internal/channel"
|
|
"gpt-load/internal/pricing"
|
|
)
|
|
|
|
func TestPriceIdentityForChannelModelRequiresRegisteredChannel(t *testing.T) {
|
|
t.Parallel()
|
|
identity, err := PriceIdentityForChannelModel(string(channel.OpenAICompatible), "shared")
|
|
if err != nil || identity != (pricing.Identity{
|
|
ChannelID: string(channel.OpenAICompatible), ModelID: "shared",
|
|
}) {
|
|
t.Fatalf("PriceIdentityForChannelModel() = %#v, %v", identity, err)
|
|
}
|
|
if _, err := PriceIdentityForChannelModel("unknown", "shared"); err == nil {
|
|
t.Fatal("PriceIdentityForChannelModel() accepted an unregistered channel")
|
|
}
|
|
}
|
|
|
|
func TestResolveAutomaticPriceForIdentityUsesOfficialChannelCatalogProvider(t *testing.T) {
|
|
t.Parallel()
|
|
openAICost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(1)}}
|
|
anthropicCost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(2)}}
|
|
snapshot := &catalog.Snapshot{Providers: map[string]catalog.Provider{
|
|
"openai": {ID: "openai", Models: map[string]catalog.Model{
|
|
"shared": {ID: "shared", Cost: openAICost},
|
|
}},
|
|
"anthropic": {ID: "anthropic", Models: map[string]catalog.Model{
|
|
"shared": {ID: "shared", Cost: anthropicCost},
|
|
}},
|
|
}}
|
|
|
|
match, ok := resolveAutomaticPriceForIdentity(snapshot, pricing.Identity{
|
|
ChannelID: string(channel.Anthropic), ModelID: "shared",
|
|
})
|
|
if !ok || match.cost != anthropicCost || match.providerID != "anthropic" ||
|
|
match.source != ModelPriceMatchSourceChannelCatalogProvider {
|
|
t.Fatalf("official channel match = %#v, %t", match, ok)
|
|
}
|
|
}
|
|
|
|
func TestResolveAutomaticPriceForIdentityUsesVolcengineOfficialSupplement(t *testing.T) {
|
|
t.Parallel()
|
|
snapshot, err := catalog.MergeOfficial(&catalog.Snapshot{})
|
|
if err != nil {
|
|
t.Fatalf("MergeOfficial() error = %v", err)
|
|
}
|
|
match, ok := resolveAutomaticPriceForIdentity(snapshot, pricing.Identity{
|
|
ChannelID: string(channel.Volcengine), ModelID: "doubao-seed-2-1-pro-260628",
|
|
})
|
|
if !ok || match.providerID != "volcengine" ||
|
|
match.source != ModelPriceMatchSourceChannelCatalogProvider || match.cost == nil ||
|
|
!match.cost.Prices.Input.Set ||
|
|
match.cost.Prices.Input.NanoUSDPerMillion != 889_902_497 {
|
|
t.Fatalf("Volcengine official price match = %#v, %t", match, ok)
|
|
}
|
|
}
|
|
|
|
func TestResolveAutomaticPriceForIdentityFallsBackWhenChannelCatalogProviderMisses(t *testing.T) {
|
|
t.Parallel()
|
|
openAICost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(1)}}
|
|
snapshot := &catalog.Snapshot{Providers: map[string]catalog.Provider{
|
|
"openai": {ID: "openai", Models: map[string]catalog.Model{
|
|
"custom-model": {ID: "custom-model", Cost: openAICost},
|
|
}},
|
|
}}
|
|
|
|
match, ok := resolveAutomaticPriceForIdentity(snapshot, pricing.Identity{
|
|
ChannelID: string(channel.Anthropic), ModelID: "custom-model",
|
|
})
|
|
if !ok || match.cost != openAICost || match.providerID != "openai" ||
|
|
match.source != ModelPriceMatchSourceProviderPriorityFallback {
|
|
t.Fatalf("official channel fallback = %#v, %t", match, ok)
|
|
}
|
|
}
|
|
|
|
func TestResolveAutomaticPriceForIdentityFallsBackForCompatibleChannel(t *testing.T) {
|
|
t.Parallel()
|
|
openAICost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(1)}}
|
|
snapshot := &catalog.Snapshot{Providers: map[string]catalog.Provider{
|
|
"openai": {ID: "openai", Models: map[string]catalog.Model{
|
|
"shared": {ID: "shared", Cost: openAICost},
|
|
}},
|
|
}}
|
|
|
|
match, ok := resolveAutomaticPriceForIdentity(snapshot, pricing.Identity{
|
|
ChannelID: string(channel.OpenAICompatible), ModelID: "shared",
|
|
})
|
|
if !ok || match.cost != openAICost || match.providerID != "openai" ||
|
|
match.source != ModelPriceMatchSourceProviderPriorityFallback {
|
|
t.Fatalf("compatible channel match = %#v, %t", match, ok)
|
|
}
|
|
}
|
|
|
|
func TestResolveAutomaticPriceForIdentityTreatsCodexOpenAIPriceAsReference(t *testing.T) {
|
|
t.Parallel()
|
|
openAICost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(1)}}
|
|
snapshot := &catalog.Snapshot{Providers: map[string]catalog.Provider{
|
|
"openai": {ID: "openai", Models: map[string]catalog.Model{
|
|
"gpt-codex": {ID: "gpt-codex", Cost: openAICost},
|
|
}},
|
|
}}
|
|
|
|
match, ok := resolveAutomaticPriceForIdentity(snapshot, pricing.Identity{
|
|
ChannelID: string(channel.Codex), ModelID: "gpt-codex",
|
|
})
|
|
if !ok || match.cost != openAICost || match.providerID != "openai" ||
|
|
match.source != ModelPriceMatchSourceProviderPriorityFallback {
|
|
t.Fatalf("Codex price reference = %#v, %t", match, ok)
|
|
}
|
|
}
|
|
|
|
func TestResolveAutomaticPriceForIdentityTreatsClaudeAnthropicPriceAsReference(t *testing.T) {
|
|
t.Parallel()
|
|
anthropicCost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(1)}}
|
|
snapshot := &catalog.Snapshot{Providers: map[string]catalog.Provider{
|
|
"anthropic": {ID: "anthropic", Models: map[string]catalog.Model{
|
|
"claude-subscription": {ID: "claude-subscription", Cost: anthropicCost},
|
|
}},
|
|
}}
|
|
|
|
match, ok := resolveAutomaticPriceForIdentity(snapshot, pricing.Identity{
|
|
ChannelID: string(channel.Claude), ModelID: "claude-subscription",
|
|
})
|
|
if !ok || match.cost != anthropicCost || match.providerID != "anthropic" ||
|
|
match.source != ModelPriceMatchSourceProviderPriorityFallback {
|
|
t.Fatalf("Claude price reference = %#v, %t", match, ok)
|
|
}
|
|
}
|
|
|
|
func TestResolveAutomaticPriceForIdentityTreatsAntigravityGooglePriceAsReference(t *testing.T) {
|
|
t.Parallel()
|
|
googleCost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(1)}}
|
|
snapshot := &catalog.Snapshot{Providers: map[string]catalog.Provider{
|
|
"google": {ID: "google", Models: map[string]catalog.Model{
|
|
"gemini-antigravity": {ID: "gemini-antigravity", Cost: googleCost},
|
|
}},
|
|
}}
|
|
|
|
match, ok := resolveAutomaticPriceForIdentity(snapshot, pricing.Identity{
|
|
ChannelID: string(channel.Antigravity), ModelID: "gemini-antigravity",
|
|
})
|
|
if !ok || match.cost != googleCost || match.providerID != "google" ||
|
|
match.source != ModelPriceMatchSourceProviderPriorityFallback {
|
|
t.Fatalf("Antigravity price reference = %#v, %t", match, ok)
|
|
}
|
|
}
|
|
|
|
func TestResolveAutomaticPriceForIdentityTreatsGrokXAIPriceAsReference(t *testing.T) {
|
|
t.Parallel()
|
|
xaiCost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(1)}}
|
|
snapshot := &catalog.Snapshot{Providers: map[string]catalog.Provider{
|
|
"xai": {ID: "xai", Models: map[string]catalog.Model{
|
|
"grok-4.3": {ID: "grok-4.3", Cost: xaiCost},
|
|
}},
|
|
}}
|
|
|
|
match, ok := resolveAutomaticPriceForIdentity(snapshot, pricing.Identity{
|
|
ChannelID: string(channel.Grok), ModelID: "grok-4.3",
|
|
})
|
|
if !ok || match.cost != xaiCost || match.providerID != "xai" ||
|
|
match.source != ModelPriceMatchSourceProviderPriorityFallback {
|
|
t.Fatalf("Grok price reference = %#v, %t", match, ok)
|
|
}
|
|
}
|
|
|
|
func TestCompatibleAutomaticPriceUsesGlobalPriority(t *testing.T) {
|
|
t.Parallel()
|
|
openAICost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(1)}}
|
|
alphaCost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(2)}}
|
|
snapshot := &catalog.Snapshot{Providers: map[string]catalog.Provider{
|
|
"alpha": {ID: "alpha", Models: map[string]catalog.Model{"shared": {ID: "shared", Cost: alphaCost}}},
|
|
"openai": {ID: "openai", Models: map[string]catalog.Model{"shared": {ID: "shared", Cost: openAICost}}},
|
|
}}
|
|
want := *snapshot
|
|
want.Providers = make(map[string]catalog.Provider, len(snapshot.Providers))
|
|
for id, provider := range snapshot.Providers {
|
|
want.Providers[id] = provider
|
|
}
|
|
match, ok := resolveAutomaticPriceForIdentity(snapshot, pricing.Identity{
|
|
ChannelID: string(channel.OpenAICompatible), ModelID: "shared",
|
|
})
|
|
if !ok || match.cost != openAICost || match.providerID != "openai" ||
|
|
match.source != ModelPriceMatchSourceProviderPriorityFallback {
|
|
t.Fatalf("compatible price = (%p, %q, %q, %t), want openai fallback", match.cost, match.providerID, match.source, ok)
|
|
}
|
|
if !reflect.DeepEqual(snapshot, &want) {
|
|
t.Fatal("global price resolution mutated catalog snapshot")
|
|
}
|
|
}
|
|
|
|
func TestCompatibleAutomaticPriceFallsBackToStableProviderID(t *testing.T) {
|
|
t.Parallel()
|
|
alphaCost := &catalog.ModelCost{Prices: pricing.Prices{Input: priceTestValue(1)}}
|
|
snapshot := &catalog.Snapshot{Providers: map[string]catalog.Provider{
|
|
"zeta": {ID: "zeta", Models: map[string]catalog.Model{"shared": {ID: "shared", Cost: &catalog.ModelCost{}}}},
|
|
"alpha": {ID: "alpha", Models: map[string]catalog.Model{"shared": {ID: "shared", Cost: alphaCost}}},
|
|
}}
|
|
match, ok := resolveAutomaticPriceForIdentity(snapshot, pricing.Identity{
|
|
ChannelID: string(channel.OpenAICompatible), ModelID: "shared",
|
|
})
|
|
if !ok || match.cost != alphaCost || match.providerID != "alpha" ||
|
|
match.source != ModelPriceMatchSourceProviderPriorityFallback {
|
|
t.Fatalf("compatible fallback = (%p, %q, %q, %t)", match.cost, match.providerID, match.source, ok)
|
|
}
|
|
}
|