diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-04 16:04:58 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-04 16:04:58 +0300 |
| commit | bf53cf2a673af254d7a08bc3b2ab815a08f66117 (patch) | |
| tree | 3c29faaaaa6777d9a9346c90bc7cba88978d8477 /internal/llm/openai_http_test.go | |
| parent | 448d4b169904cfd6e1f701524539a27d8de18734 (diff) | |
tests: add shared test fixtures, expand provider breadth (multi-choice, error bodies), add LSP rewrite/diagnostics realism and table-driven tests
Diffstat (limited to 'internal/llm/openai_http_test.go')
| -rw-r--r-- | internal/llm/openai_http_test.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/llm/openai_http_test.go b/internal/llm/openai_http_test.go index 7ae34be..78830ba 100644 --- a/internal/llm/openai_http_test.go +++ b/internal/llm/openai_http_test.go @@ -47,3 +47,47 @@ func TestHandleOpenAINon2xx_NoErrorBody(t *testing.T) { resp := &http.Response{StatusCode: 500, Body: io.NopCloser(strings.NewReader("{}"))} if err := handleOpenAINon2xx(resp, time.Now()); err == nil { t.Fatalf("expected http error") } } + +func TestOpenAI_ChatStream_SSE_ErrorChunk(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, "data: {\"error\":{\"message\":\"oops\"}}\n\n") + io.WriteString(w, "data: [DONE]\n") + })) + defer srv.Close() + c := newOpenAI(srv.URL, "g", "KEY", f64p(0.2)).(openAIClient) + c.httpClient = srv.Client() + var got string + if err := c.ChatStream(context.Background(), []Message{{Role:"user", Content:"hi"}}, func(s string){ got += s }); err == nil { + t.Fatalf("expected error due to error chunk") + } +} + +func TestOpenAI_Chat_MultiChoiceAndErrorBody(t *testing.T) { + // Multi-choice success: return two choices with different finish reasons + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(map[string]any{ + "choices": []map[string]any{ + {"index": 0, "finish_reason": "stop", "message": map[string]string{"role": "assistant", "content": "FIRST"}}, + {"index": 1, "finish_reason": "length", "message": map[string]string{"role": "assistant", "content": "SECOND"}}, + }, + }) + })) + defer srv.Close() + c := newOpenAI(srv.URL, "g", "KEY", f64p(0.2)).(openAIClient) + c.httpClient = srv.Client() + out, err := c.Chat(context.Background(), []Message{{Role: "user", Content: "hi"}}) + if err != nil || out != "FIRST" { t.Fatalf("openai multi-choice: %v %q", err, out) } + + // Error body case: non-2xx with error message + srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(400) + _ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"message": "bad", "type": "invalid"}}) + })) + defer srv2.Close() + c2 := newOpenAI(srv2.URL, "g", "KEY", f64p(0.2)).(openAIClient) + c2.httpClient = srv2.Client() + if _, err := c2.Chat(context.Background(), []Message{{Role: "user", Content: "hi"}}); err == nil { + t.Fatalf("expected error from non-2xx with error body") + } +} |
