| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "reflect" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestKimiK3ReasoningProtocolControlsEffortCapability(t *testing.T) { |
| 12 | e := &ProviderEntry{ |
| 13 | Name: "custom", Kind: "openai", BaseURL: "https://example.com", Model: "kimi-k3", |
| 14 | ReasoningProtocol: ReasoningProtocolKimiK3, |
| 15 | SupportedEfforts: []string{"medium", "ultra"}, DefaultEffort: "ultra", Effort: "ultra", |
| 16 | } |
| 17 | cap := EffortCapabilityForEntry(e) |
| 18 | wantLevels := []string{"auto", "low", "high", "max"} |
| 19 | if !cap.Supported || cap.Default != "max" || !reflect.DeepEqual(cap.Levels, wantLevels) { |
| 20 | t.Fatalf("reasoning_protocol=kimi-k3 capability = %+v, want levels %v with default max", cap, wantLevels) |
| 21 | } |
| 22 | if protocol := ReasoningProtocolForEntry(e); protocol != ReasoningProtocolKimiK3 { |
| 23 | t.Fatalf("protocol = %q, want kimi-k3", protocol) |
| 24 | } |
| 25 | for _, level := range []string{"low", "high", "max"} { |
| 26 | if got, err := NormalizeEffort(e, level); err != nil || got != level { |
| 27 | t.Fatalf("NormalizeEffort(%s) = %q/%v, want %s/nil", level, got, err, level) |
| 28 | } |
| 29 | } |
| 30 | if _, err := NormalizeEffort(e, "medium"); err == nil { |
| 31 | t.Fatal("Kimi K3 reasoning_protocol should reject medium") |
| 32 | } |
| 33 | if _, err := NormalizeEffort(e, "ultra"); err == nil { |
| 34 | t.Fatal("Kimi K3 reasoning_protocol should ignore custom supported_efforts") |
| 35 | } |
| 36 | if got := EffortDisplay(e); got != "ultra" { |
| 37 | t.Fatalf("invalid stored Kimi K3 effort display = %q, want explicit invalid value", got) |
| 38 | } |
| 39 | if got := EffectiveEffort(e); got != "ultra" { |
| 40 | t.Fatalf("invalid stored Kimi K3 effective effort = %q, want invalid value for request validation", got) |
| 41 | } |
| 42 | e.Effort = "high" |
| 43 | if got := EffortDisplay(e); got != "high" { |
| 44 | t.Fatalf("valid stored Kimi K3 effort display = %q, want high", got) |
| 45 | } |
| 46 | if got := EffectiveEffort(e); got != "high" { |
| 47 | t.Fatalf("valid stored Kimi K3 effective effort = %q, want high", got) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestKimiK3ReasoningProtocolPreservesDormantCustomEfforts(t *testing.T) { |
| 52 | path := filepath.Join(t.TempDir(), "config.toml") |
| 53 | c := &Config{} |
| 54 | wantSupported := []string{"medium", "ultra"} |
| 55 | if err := c.UpsertProvider(ProviderEntry{ |
| 56 | Name: "custom-kimi-gateway", Kind: "openai", BaseURL: "https://gateway.example.com/v1", Model: "kimi-k3", |
| 57 | ReasoningProtocol: ReasoningProtocolKimiK3, SupportedEfforts: wantSupported, DefaultEffort: "ultra", |
| 58 | }); err != nil { |
| 59 | t.Fatalf("UpsertProvider: %v", err) |
| 60 | } |
| 61 | if err := c.SaveTo(path); err != nil { |
| 62 | t.Fatalf("SaveTo: %v", err) |
| 63 | } |
| 64 | loaded := LoadForEditWithoutCredentials(path) |
| 65 | got, ok := loaded.Provider("custom-kimi-gateway") |
| 66 | if !ok { |
| 67 | t.Fatal("saved Kimi K3 provider was not reloaded") |
| 68 | } |
| 69 | if !reflect.DeepEqual(got.SupportedEfforts, wantSupported) || got.DefaultEffort != "ultra" { |
| 70 | t.Fatalf("dormant custom efforts were not preserved: %+v", got) |
| 71 | } |
| 72 | cap := EffortCapabilityForEntry(got) |
| 73 | if want := []string{"auto", "low", "high", "max"}; !reflect.DeepEqual(cap.Levels, want) || cap.Default != "max" { |
| 74 | t.Fatalf("reloaded Kimi K3 capability = %+v, want fixed protocol defaults", cap) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestKimiK3ReasoningProtocolRoundTripsConfig(t *testing.T) { |
| 79 | path := filepath.Join(t.TempDir(), "config.toml") |
| 80 | c := &Config{} |
| 81 | if err := c.UpsertProvider(ProviderEntry{ |
| 82 | Name: "custom-kimi-gateway", Kind: "openai", BaseURL: "https://gateway.example.com/v1", Model: "kimi-k3", |
| 83 | ReasoningProtocol: " KIMI-K3 ", SupportedEfforts: []string{"low", "high", "max"}, DefaultEffort: "max", |
| 84 | }); err != nil { |
| 85 | t.Fatalf("UpsertProvider: %v", err) |
| 86 | } |
| 87 | if err := c.SaveTo(path); err != nil { |
| 88 | t.Fatalf("SaveTo: %v", err) |
| 89 | } |
| 90 | loaded := LoadForEditWithoutCredentials(path) |
| 91 | got, ok := loaded.Provider("custom-kimi-gateway") |
| 92 | if !ok { |
| 93 | t.Fatal("saved Kimi K3 provider was not reloaded") |
| 94 | } |
| 95 | if got.ReasoningProtocol != ReasoningProtocolKimiK3 || !reflect.DeepEqual(got.SupportedEfforts, []string{"low", "high", "max"}) || got.DefaultEffort != "max" { |
| 96 | t.Fatalf("reloaded Kimi K3 provider = %+v", got) |
| 97 | } |
| 98 | body, err := os.ReadFile(path) |
| 99 | if err != nil { |
| 100 | t.Fatalf("read saved config: %v", err) |
| 101 | } |
| 102 | if !strings.Contains(string(body), `reasoning_protocol = "kimi-k3"`) { |
| 103 | t.Fatalf("saved config lost Kimi K3 protocol:\n%s", body) |
| 104 | } |
| 105 | } |
| 106 |