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 | |
| 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')
| -rw-r--r-- | internal/llm/copilot_http_test.go | 38 | ||||
| -rw-r--r-- | internal/llm/openai_http_test.go | 44 |
2 files changed, 81 insertions, 1 deletions
diff --git a/internal/llm/copilot_http_test.go b/internal/llm/copilot_http_test.go index c029a65..4c2b7fe 100644 --- a/internal/llm/copilot_http_test.go +++ b/internal/llm/copilot_http_test.go @@ -6,9 +6,9 @@ import ( "io" "net/http" "net/http/httptest" + "strings" "testing" "time" - "strings" "encoding/base64" ) @@ -72,6 +72,42 @@ func TestCopilot_CodeCompletion_Success(t *testing.T) { } } +func TestCopilot_Chat_MultiChoice_And_ErrorBody(t *testing.T) { + // Chat multi-choice: return two choices; client returns first content + 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 := newCopilot(srv.URL, "gpt-4o-mini", "KEY", f64p(0.1)).(copilotClient) + // Token success + tr := rtFunc2(func(r *http.Request) (*http.Response, error) { + if r.URL.Host == "api.github.com" && r.URL.Path == "/copilot_internal/v2/token" { + rw := httptest.NewRecorder(); _ = json.NewEncoder(rw).Encode(map[string]string{"token":"tok"}); res := rw.Result(); res.StatusCode = 200; return res, nil + } + return http.DefaultTransport.RoundTrip(r) + }) + c.httpClient = &http.Client{Transport: tr, Timeout: 5 * time.Second} + out, err := c.Chat(context.Background(), []Message{{Role: "user", Content: "hi"}}) + if err != nil || out != "FIRST" { t.Fatalf("copilot multi-choice: %v %q", err, out) } + + // Non-2xx with error body + srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(403) + _ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"message":"denied","type":"forbidden"}}) + })) + defer srv2.Close() + c2 := newCopilot(srv2.URL, "gpt-4o-mini", "KEY", f64p(0.1)).(copilotClient) + c2.httpClient = &http.Client{Transport: tr, Timeout: 5 * time.Second} + if _, err := c2.Chat(context.Background(), []Message{{Role:"user", Content:"hi"}}); err == nil { + t.Fatalf("expected error for copilot non-2xx with error body") + } +} + func TestParseJWTExp_AndParseInt64(t *testing.T) { // Valid base64 payload payload := `{"exp": 1700000000}` 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") + } +} |
