| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/config" |
| 11 | "reasonix/internal/provider" |
| 12 | ) |
| 13 | |
| 14 | func TestNewProviderBuildsSCNetOpenAIDeepSeekRequestContract(t *testing.T) { |
| 15 | var gotReq map[string]any |
| 16 | var gotAuth, gotAPIKey, gotPath string |
| 17 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | gotAuth = r.Header.Get("Authorization") |
| 19 | gotAPIKey = r.Header.Get("x-api-key") |
| 20 | gotPath = r.URL.Path |
| 21 | if err := json.NewDecoder(r.Body).Decode(&gotReq); err != nil { |
| 22 | t.Errorf("decode request: %v", err) |
| 23 | } |
| 24 | w.Header().Set("Content-Type", "text/event-stream") |
| 25 | _, _ = w.Write([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\ndata: [DONE]\n\n")) |
| 26 | })) |
| 27 | defer srv.Close() |
| 28 | |
| 29 | preset, ok := config.CuratedProviderPreset("scnet") |
| 30 | if !ok || len(preset.Entries) != 1 { |
| 31 | t.Fatalf("SCNet preset = %+v", preset) |
| 32 | } |
| 33 | var cfg config.Config |
| 34 | if err := cfg.UpsertProvider(preset.Entries[0]); err != nil { |
| 35 | t.Fatalf("UpsertProvider: %v", err) |
| 36 | } |
| 37 | entry, ok := cfg.ResolveModel("scnet/DeepSeek-V4-Flash") |
| 38 | if !ok { |
| 39 | t.Fatal("ResolveModel(scnet/DeepSeek-V4-Flash) failed") |
| 40 | } |
| 41 | entry.BaseURL = srv.URL + "/api/llm/v1" |
| 42 | entry.ModelsURL = "" |
| 43 | t.Setenv(entry.APIKeyEnv, "scnet-test-key") |
| 44 | entry.ResolveAPIKeyFromProcessEnvForProbe() |
| 45 | p, err := NewProvider(entry) |
| 46 | if err != nil { |
| 47 | t.Fatalf("NewProvider: %v", err) |
| 48 | } |
| 49 | ch, err := p.Stream(context.Background(), provider.Request{ |
| 50 | Messages: []provider.Message{{Role: provider.RoleUser, Content: "hi"}}, |
| 51 | }) |
| 52 | if err != nil { |
| 53 | t.Fatalf("Stream: %v", err) |
| 54 | } |
| 55 | for chunk := range ch { |
| 56 | if chunk.Type == provider.ChunkError { |
| 57 | t.Fatalf("stream error: %v", chunk.Err) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if gotPath != "/api/llm/v1/chat/completions" { |
| 62 | t.Fatalf("request path = %q, want /api/llm/v1/chat/completions", gotPath) |
| 63 | } |
| 64 | if gotAuth != "Bearer scnet-test-key" { |
| 65 | t.Fatalf("Authorization = %q, want Bearer scnet-test-key", gotAuth) |
| 66 | } |
| 67 | if gotAPIKey != "" { |
| 68 | t.Fatalf("x-api-key = %q, want omitted", gotAPIKey) |
| 69 | } |
| 70 | if got := gotReq["reasoning_effort"]; got != "high" { |
| 71 | t.Fatalf("reasoning_effort = %#v, want high: %+v", got, gotReq) |
| 72 | } |
| 73 | if _, ok := gotReq["thinking"]; ok { |
| 74 | t.Fatalf("SCNet OpenAI request must omit DeepSeek-native thinking: %+v", gotReq) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestNewProviderBuildsSCNetAnthropicRequestContract(t *testing.T) { |
| 79 | var gotReq map[string]any |
| 80 | var gotAuth, gotAPIKey, gotPath string |
| 81 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 82 | gotAuth = r.Header.Get("Authorization") |
| 83 | gotAPIKey = r.Header.Get("x-api-key") |
| 84 | gotPath = r.URL.Path |
| 85 | if err := json.NewDecoder(r.Body).Decode(&gotReq); err != nil { |
| 86 | t.Errorf("decode request: %v", err) |
| 87 | } |
| 88 | w.Header().Set("Content-Type", "text/event-stream") |
| 89 | _, _ = w.Write([]byte(`event: message_start |
| 90 | data: {"type":"message_start","message":{"usage":{"input_tokens":1,"output_tokens":0}}} |
| 91 | |
| 92 | event: message_delta |
| 93 | data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}} |
| 94 | |
| 95 | event: message_stop |
| 96 | data: {"type":"message_stop"} |
| 97 | |
| 98 | `)) |
| 99 | })) |
| 100 | defer srv.Close() |
| 101 | |
| 102 | preset, ok := config.CuratedProviderPreset("scnet-anthropic") |
| 103 | if !ok || len(preset.Entries) != 1 { |
| 104 | t.Fatalf("SCNet Anthropic preset = %+v", preset) |
| 105 | } |
| 106 | entry := preset.Entries[0] |
| 107 | entry.BaseURL = srv.URL |
| 108 | entry.Model = entry.DefaultModel() |
| 109 | t.Setenv(entry.APIKeyEnv, "scnet-test-key") |
| 110 | entry.ResolveAPIKeyFromProcessEnvForProbe() |
| 111 | p, err := NewProvider(&entry) |
| 112 | if err != nil { |
| 113 | t.Fatalf("NewProvider: %v", err) |
| 114 | } |
| 115 | ch, err := p.Stream(context.Background(), provider.Request{ |
| 116 | Messages: []provider.Message{{Role: provider.RoleUser, Content: "hi"}}, |
| 117 | }) |
| 118 | if err != nil { |
| 119 | t.Fatalf("Stream: %v", err) |
| 120 | } |
| 121 | for chunk := range ch { |
| 122 | if chunk.Type == provider.ChunkError { |
| 123 | t.Fatalf("stream error: %v", chunk.Err) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if gotPath != "/v1/messages" { |
| 128 | t.Fatalf("request path = %q, want /v1/messages", gotPath) |
| 129 | } |
| 130 | if gotAuth != "Bearer scnet-test-key" { |
| 131 | t.Fatalf("Authorization = %q, want Bearer scnet-test-key", gotAuth) |
| 132 | } |
| 133 | if gotAPIKey != "" { |
| 134 | t.Fatalf("x-api-key = %q, want omitted", gotAPIKey) |
| 135 | } |
| 136 | if _, ok := gotReq["thinking"]; ok { |
| 137 | t.Fatalf("SCNet MiniMax request must omit thinking: %+v", gotReq) |
| 138 | } |
| 139 | } |
| 140 |