| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "reasonix/internal/config" |
| 12 | "reasonix/internal/provider" |
| 13 | ) |
| 14 | |
| 15 | func TestNewProviderAppliesExplicitKimiK3RequestContractToCustomGateway(t *testing.T) { |
| 16 | var gotReq map[string]any |
| 17 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | if err := json.NewDecoder(r.Body).Decode(&gotReq); err != nil { |
| 19 | t.Fatalf("decode request: %v", err) |
| 20 | } |
| 21 | w.Header().Set("Content-Type", "text/event-stream") |
| 22 | _, _ = w.Write([]byte("data: {\"choices\":[{\"delta\":{\"reasoning_content\":\"thinking\"}}]}\n\ndata: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\ndata: [DONE]\n\n")) |
| 23 | })) |
| 24 | defer srv.Close() |
| 25 | |
| 26 | p, err := NewProvider(&config.ProviderEntry{ |
| 27 | Name: "custom-kimi-gateway", Kind: "openai", BaseURL: srv.URL, Model: "kimi-k3", |
| 28 | ReasoningProtocol: config.ReasoningProtocolKimiK3, |
| 29 | SupportedEfforts: []string{"medium", "ultra"}, DefaultEffort: "ultra", |
| 30 | }) |
| 31 | if err != nil { |
| 32 | t.Fatalf("NewProvider: %v", err) |
| 33 | } |
| 34 | if !provider.RequiresReasoningRoundTrip(p) { |
| 35 | t.Fatal("custom Kimi K3 provider must advertise reasoning round-trip") |
| 36 | } |
| 37 | ch, err := p.Stream(context.Background(), provider.Request{ |
| 38 | Messages: []provider.Message{{Role: provider.RoleUser, Content: "hi"}}, |
| 39 | Temperature: provider.TemperaturePtr(0.3), MaxTokens: 2048, |
| 40 | }) |
| 41 | if err != nil { |
| 42 | t.Fatalf("Stream: %v", err) |
| 43 | } |
| 44 | var reasoning strings.Builder |
| 45 | for chunk := range ch { |
| 46 | if chunk.Type == provider.ChunkError { |
| 47 | t.Fatalf("stream error: %v", chunk.Err) |
| 48 | } |
| 49 | if chunk.Type == provider.ChunkReasoning { |
| 50 | reasoning.WriteString(chunk.Text) |
| 51 | } |
| 52 | } |
| 53 | if reasoning.String() != "thinking" { |
| 54 | t.Fatalf("reasoning stream = %q, want thinking", reasoning.String()) |
| 55 | } |
| 56 | if gotReq["reasoning_effort"] != "max" || gotReq["max_completion_tokens"] != float64(2048) { |
| 57 | t.Fatalf("custom Kimi K3 request = %+v, want protocol-default max effort and max_completion_tokens", gotReq) |
| 58 | } |
| 59 | for _, field := range []string{"temperature", "max_tokens"} { |
| 60 | if _, ok := gotReq[field]; ok { |
| 61 | t.Fatalf("custom Kimi K3 request must omit %q: %+v", field, gotReq) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 |