mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 00:05:30 +00:00
增量 4a(③ 组 A6+C25+E9 + ⑤ 组 A2 + F4,同批落地): - A2: 新增 AccountModeOllamaLegacy/OllamaCredits 常量与 Account.GetOllamaCloudAccountMode 解析器(非 ollama_cloud/非 apikey 返回空串;未设置或值不认识默认 legacy,存量账号保持受阈值保护) - A6: AllowedSchedulingThresholdPlatforms 纳入 ollama_cloud(4 个消费点 同步生效:评估器门禁、设置校验、存储解析、设置审计 diff) - C25: 评估器新增 ollama 分支 + ollamaCloudThresholdCandidates,仅 legacy 产生 5h/weekly 停调候选(credits 型月度信用池候选恒空);快照缺失/ status!=ok/无窗口数据不停调;取最晚 reset(与 openai/anthropic 同类) - E9: 前端 SchedulingThresholdPlatformType union 与 SCHEDULING_THRESHOLD_PLATFORMS 数组加 ollama_cloud(与后端白名单两侧同步) - F4: 评估器表驱动用例(legacy 停/credits 不停/默认 legacy/坏快照不停)、 ApplyAccountSchedulingThreshold 端到端(temp_unschedulable 落库断言)、 mode 解析器用例、设置校验 ollama_cloud 往返用例 无迁移、无 DTO 变更(account_mode 为自由 JSONB,无 CHECK 约束)。
200 lines
7.3 KiB
Go
200 lines
7.3 KiB
Go
//go:build unit
|
||
|
||
package service
|
||
|
||
import (
|
||
"context"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||
"github.com/stretchr/testify/require"
|
||
)
|
||
|
||
func newSettingServiceForPlatformThresholdTest(seed map[string]string) *SettingService {
|
||
accountSchedulingThresholdsSF.Forget(SettingKeyAccountSchedulingThresholds)
|
||
accountSchedulingThresholdsCache.Store(&cachedAccountSchedulingThresholds{})
|
||
repo := newMockSettingRepo()
|
||
for k, v := range seed {
|
||
repo.data[k] = v
|
||
}
|
||
return NewSettingService(repo, &config.Config{})
|
||
}
|
||
|
||
func TestPlatformSchedulingThresholds_RoundTrip_DefaultsAndStoredValues(t *testing.T) {
|
||
svc := newSettingServiceForPlatformThresholdTest(nil)
|
||
|
||
got := svc.parseSettings(map[string]string{})
|
||
require.Equal(t, map[string]int{
|
||
PlatformOpenAI: 100,
|
||
PlatformAnthropic: 100,
|
||
PlatformGrok: 100,
|
||
}, got.AccountSchedulingThresholds)
|
||
|
||
got = svc.parseSettings(map[string]string{
|
||
SettingKeyAccountSchedulingThresholds: `{"openai":91,"grok":77,"gemini":85,"kiro":99}`,
|
||
})
|
||
require.Equal(t, 91, got.AccountSchedulingThresholds[PlatformOpenAI])
|
||
require.Equal(t, 100, got.AccountSchedulingThresholds[PlatformAnthropic])
|
||
require.Equal(t, 77, got.AccountSchedulingThresholds[PlatformGrok])
|
||
require.NotContains(t, got.AccountSchedulingThresholds, PlatformGemini)
|
||
require.NotContains(t, got.AccountSchedulingThresholds, "kiro")
|
||
}
|
||
|
||
func TestBuildSystemSettingsUpdates_PersistsAccountSchedulingThresholds(t *testing.T) {
|
||
svc := newSettingServiceForPlatformThresholdTest(nil)
|
||
|
||
updates, err := svc.buildSystemSettingsUpdates(context.Background(), &SystemSettings{
|
||
AccountSchedulingThresholds: map[string]int{
|
||
PlatformOpenAI: 91,
|
||
PlatformAnthropic: 88,
|
||
PlatformGrok: 77,
|
||
},
|
||
})
|
||
require.NoError(t, err)
|
||
require.JSONEq(t, `{"openai":91,"anthropic":88,"grok":77}`, updates[SettingKeyAccountSchedulingThresholds])
|
||
}
|
||
|
||
func TestValidateAndNormalizeAccountSchedulingThresholds_FillsMissingPlatforms(t *testing.T) {
|
||
normalized, err := validateAndNormalizeAccountSchedulingThresholds(map[string]int{
|
||
PlatformOpenAI: 91,
|
||
})
|
||
require.NoError(t, err)
|
||
require.Equal(t, 91, normalized[PlatformOpenAI])
|
||
require.Equal(t, 100, normalized[PlatformAnthropic])
|
||
require.Equal(t, 100, normalized[PlatformGrok])
|
||
require.NotContains(t, normalized, PlatformGemini)
|
||
require.NotContains(t, normalized, "kiro")
|
||
require.NotContains(t, normalized, PlatformAntigravity)
|
||
}
|
||
|
||
func TestValidateAndNormalizeAccountSchedulingThresholds_RejectsUnsupportedPlatforms(t *testing.T) {
|
||
_, err := validateAndNormalizeAccountSchedulingThresholds(map[string]int{
|
||
PlatformGemini: 85,
|
||
})
|
||
require.Error(t, err)
|
||
}
|
||
|
||
func TestUpdateSettings_StoresAccountSchedulingThresholds(t *testing.T) {
|
||
svc := newSettingServiceForPlatformThresholdTest(nil)
|
||
|
||
err := svc.UpdateSettings(context.Background(), &SystemSettings{
|
||
AccountSchedulingThresholds: map[string]int{
|
||
PlatformOpenAI: 92,
|
||
PlatformAnthropic: 89,
|
||
PlatformGrok: 76,
|
||
},
|
||
})
|
||
require.NoError(t, err)
|
||
|
||
got := svc.parseSettings(map[string]string{
|
||
SettingKeyAccountSchedulingThresholds: svc.settingRepo.(*mockSettingRepo).data[SettingKeyAccountSchedulingThresholds],
|
||
})
|
||
require.Equal(t, 92, got.AccountSchedulingThresholds[PlatformOpenAI])
|
||
require.Equal(t, 89, got.AccountSchedulingThresholds[PlatformAnthropic])
|
||
require.Equal(t, 76, got.AccountSchedulingThresholds[PlatformGrok])
|
||
require.NotContains(t, got.AccountSchedulingThresholds, "kiro")
|
||
}
|
||
|
||
func TestGetAccountSchedulingThresholds_ReadsStoredValue(t *testing.T) {
|
||
svc := newSettingServiceForPlatformThresholdTest(map[string]string{
|
||
SettingKeyAccountSchedulingThresholds: `{"openai":93,"grok":88,"kiro":87}`,
|
||
})
|
||
|
||
got := svc.GetAccountSchedulingThresholds(context.Background())
|
||
|
||
require.Equal(t, 93, got[PlatformOpenAI])
|
||
require.Equal(t, 100, got[PlatformAnthropic])
|
||
require.Equal(t, 88, got[PlatformGrok])
|
||
require.NotContains(t, got, "kiro")
|
||
}
|
||
|
||
func TestGetAccountSchedulingThresholds_MissingSettingUsesDefaultsAndNormalCacheTTL(t *testing.T) {
|
||
svc := newSettingServiceForPlatformThresholdTest(nil)
|
||
repo := svc.settingRepo.(*mockSettingRepo)
|
||
repo.getValueErr = ErrSettingNotFound
|
||
|
||
got := svc.GetAccountSchedulingThresholds(context.Background())
|
||
require.Equal(t, defaultAccountSchedulingThresholds(), got)
|
||
require.Equal(t, 1, repo.getValueCalls)
|
||
|
||
repo.data[SettingKeyAccountSchedulingThresholds] = `{"openai":91}`
|
||
got = svc.GetAccountSchedulingThresholds(context.Background())
|
||
require.Equal(t, 100, got[PlatformOpenAI], "missing-setting defaults should remain cached for the normal TTL")
|
||
require.Equal(t, 1, repo.getValueCalls)
|
||
|
||
cached, ok := accountSchedulingThresholdsCache.Load().(*cachedAccountSchedulingThresholds)
|
||
require.True(t, ok)
|
||
require.Greater(t, cached.expiresAt, time.Now().Add(accountSchedulingThresholdsCacheTTL-time.Second).UnixNano())
|
||
}
|
||
|
||
func TestUpdateSettings_OmittedAccountSchedulingThresholdsDoesNotCacheDefaults(t *testing.T) {
|
||
svc := newSettingServiceForPlatformThresholdTest(map[string]string{
|
||
SettingKeyAccountSchedulingThresholds: `{"openai":85,"grok":88,"kiro":87}`,
|
||
})
|
||
|
||
err := svc.UpdateSettings(context.Background(), &SystemSettings{
|
||
FrontendURL: "https://example.test",
|
||
})
|
||
require.NoError(t, err)
|
||
|
||
got := svc.GetAccountSchedulingThresholds(context.Background())
|
||
require.Equal(t, 85, got[PlatformOpenAI])
|
||
require.Equal(t, 88, got[PlatformGrok])
|
||
require.NotContains(t, got, "kiro")
|
||
}
|
||
|
||
func TestAccountSchedulingThresholds_InvalidStoredValueUsesSameDefaultsInSettingsAndCache(t *testing.T) {
|
||
svc := newSettingServiceForPlatformThresholdTest(map[string]string{
|
||
SettingKeyAccountSchedulingThresholds: `{"openai":0,"grok":88,"kiro":87}`,
|
||
})
|
||
|
||
settings := svc.parseSettings(map[string]string{
|
||
SettingKeyAccountSchedulingThresholds: `{"openai":0,"grok":88,"kiro":87}`,
|
||
})
|
||
cached := svc.GetAccountSchedulingThresholds(context.Background())
|
||
|
||
require.Equal(t, settings.AccountSchedulingThresholds, cached)
|
||
require.Equal(t, 100, cached[PlatformOpenAI])
|
||
require.Equal(t, 88, cached[PlatformGrok])
|
||
require.NotContains(t, cached, "kiro")
|
||
}
|
||
|
||
func TestGetAccountSchedulingThresholds_NilRepoReturnsDefaults(t *testing.T) {
|
||
svc := &SettingService{}
|
||
got := svc.GetAccountSchedulingThresholds(context.Background())
|
||
require.Equal(t, map[string]int{
|
||
PlatformOpenAI: 100,
|
||
PlatformAnthropic: 100,
|
||
PlatformGrok: 100,
|
||
}, got)
|
||
}
|
||
|
||
func TestValidateAndNormalizeAccountSchedulingThresholds_AcceptsOllamaCloud(t *testing.T) {
|
||
normalized, err := validateAndNormalizeAccountSchedulingThresholds(map[string]int{
|
||
PlatformOllamaCloud: 80,
|
||
})
|
||
require.NoError(t, err)
|
||
require.Equal(t, 80, normalized[PlatformOllamaCloud])
|
||
}
|
||
|
||
func TestAccountSchedulingThresholds_OllamaCloudRoundTrip(t *testing.T) {
|
||
svc := newSettingServiceForPlatformThresholdTest(nil)
|
||
|
||
// A6 验收的服务层等价路径:PUT account_scheduling_thresholds {"ollama_cloud":80}
|
||
// 必须被接受并回读 —— 白名单未纳入 ollama_cloud 时这里被
|
||
// validateAndNormalizeAccountSchedulingThresholds 拒为 unknown platform(400)。
|
||
err := svc.UpdateSettings(context.Background(), &SystemSettings{
|
||
AccountSchedulingThresholds: map[string]int{
|
||
PlatformOllamaCloud: 80,
|
||
},
|
||
})
|
||
require.NoError(t, err)
|
||
|
||
stored := svc.settingRepo.(*mockSettingRepo).data[SettingKeyAccountSchedulingThresholds]
|
||
require.Contains(t, stored, `"ollama_cloud":80`)
|
||
|
||
cached := svc.GetAccountSchedulingThresholds(context.Background())
|
||
require.Equal(t, 80, cached[PlatformOllamaCloud])
|
||
}
|