| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | func TestDefaultCacheTTL(t *testing.T) { |
| 9 | cases := []struct { |
| 10 | url string |
| 11 | want time.Duration |
| 12 | }{ |
| 13 | {"https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1", 5 * time.Minute}, |
| 14 | {"https://dashscope.aliyuncs.com/compatible-mode/v1", 5 * time.Minute}, |
| 15 | {"https://api.deepseek.com", 24 * time.Hour}, |
| 16 | {"https://api.anthropic.com", 5 * time.Minute}, |
| 17 | {"https://unknown.example.com/v1", 24 * time.Hour}, |
| 18 | {"", 24 * time.Hour}, |
| 19 | } |
| 20 | for _, tc := range cases { |
| 21 | if got := DefaultCacheTTL(tc.url); got != tc.want { |
| 22 | t.Errorf("DefaultCacheTTL(%q) = %v, want %v", tc.url, got, tc.want) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestEffectiveCacheTTLVendorDefault(t *testing.T) { |
| 28 | e := &ProviderEntry{BaseURL: "https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"} |
| 29 | if got := e.EffectiveCacheTTL(); got != 5*time.Minute { |
| 30 | t.Fatalf("DashScope default = %v, want 5m", got) |
| 31 | } |
| 32 | e2 := &ProviderEntry{BaseURL: "https://api.deepseek.com"} |
| 33 | if got := e2.EffectiveCacheTTL(); got != 24*time.Hour { |
| 34 | t.Fatalf("DeepSeek default = %v, want 24h (legacy)", got) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestEffectiveCacheTTLOverride(t *testing.T) { |
| 39 | e := &ProviderEntry{BaseURL: "https://api.deepseek.com", CacheTTLMinutes: 30} |
| 40 | if got := e.EffectiveCacheTTL(); got != 30*time.Minute { |
| 41 | t.Fatalf("override = %v, want 30m", got) |
| 42 | } |
| 43 | // Zero falls through to vendor default. |
| 44 | e2 := &ProviderEntry{BaseURL: "https://api.deepseek.com", CacheTTLMinutes: 0} |
| 45 | if got := e2.EffectiveCacheTTL(); got != 24*time.Hour { |
| 46 | t.Fatalf("zero override = %v, want 24h (vendor default)", got) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestDetectCacheVendor(t *testing.T) { |
| 51 | cases := []struct { |
| 52 | url string |
| 53 | want string |
| 54 | }{ |
| 55 | {"https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1", "dashscope"}, |
| 56 | {"https://dashscope.aliyuncs.com/api/v1", "dashscope"}, |
| 57 | {"https://api.deepseek.com", "deepseek"}, |
| 58 | {"https://api.anthropic.com", "anthropic"}, |
| 59 | {"https://openrouter.ai/api/v1", ""}, |
| 60 | {"https://dashscope.aliyuncs.com.attacker.example/v1", ""}, |
| 61 | {"https://example.com/?u=api.anthropic.com", ""}, |
| 62 | {"", ""}, |
| 63 | } |
| 64 | for _, tc := range cases { |
| 65 | if got := detectCacheVendor(tc.url); got != tc.want { |
| 66 | t.Errorf("detectCacheVendor(%q) = %q, want %q", tc.url, got, tc.want) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 |