diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-05 20:55:01 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-05 20:55:01 +0300 |
| commit | 95e0633abaf5779c17c133f94037f38b73c72d3e (patch) | |
| tree | 64c11d662aa9e1cb2ea94ccdd6c452d53dbf71e6 /internal/llm | |
| parent | d99bdf981dbee7e038e4a8e262504c1a15047c38 (diff) | |
tests: add more negative provider cases and table-driven LSP coverage; assert headers; add indent postprocess test
Diffstat (limited to 'internal/llm')
| -rw-r--r-- | internal/llm/copilot_http_test.go | 18 | ||||
| -rw-r--r-- | internal/llm/openai_http_test.go | 28 |
2 files changed, 46 insertions, 0 deletions
diff --git a/internal/llm/copilot_http_test.go b/internal/llm/copilot_http_test.go index 2e46d68..53f831c 100644 --- a/internal/llm/copilot_http_test.go +++ b/internal/llm/copilot_http_test.go @@ -108,6 +108,24 @@ func TestCopilot_Chat_MultiChoice_And_ErrorBody(t *testing.T) { } } +func TestCopilot_Chat_NoChoices_Error(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(map[string]any{"choices": []any{}}) + })) + defer srv.Close() + c := newCopilot(srv.URL, "gpt-4o-mini", "KEY", f64p(0.1)).(copilotClient) + 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} + if _, err := c.Chat(context.Background(), []Message{{Role:"user", Content:"hi"}}); err == nil { + t.Fatalf("expected error when no choices returned") + } +} + func TestCopilot_Chat_DecodeError_StatusOK(t *testing.T) { // Chat returns 200 but invalid JSON; expect decode error srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/internal/llm/openai_http_test.go b/internal/llm/openai_http_test.go index 45f0c99..808bb2b 100644 --- a/internal/llm/openai_http_test.go +++ b/internal/llm/openai_http_test.go @@ -63,6 +63,34 @@ func TestOpenAI_ChatStream_SSE_ErrorChunk(t *testing.T) { } } +func TestOpenAI_Chat_NoChoices_Error(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(map[string]any{"choices": []any{}}) + })) + defer srv.Close() + c := newOpenAI(srv.URL, "g", "KEY", f64p(0.2)).(openAIClient) + c.httpClient = srv.Client() + if _, err := c.Chat(context.Background(), []Message{{Role:"user", Content:"hi"}}); err == nil { + t.Fatalf("expected error when choices empty") + } +} + +func TestOpenAI_ChatStream_SSE_EmptyDelta_NoError(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: {\\\"choices\\\":[{\\\"delta\\\":{\\\"content\\\":\\\"\\\"}}]}\\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("unexpected error for empty delta: %v", err) + } + if got != "" { t.Fatalf("expected no output for empty delta, got %q", got) } +} + func TestOpenAI_Chat_DecodeError_StatusOK(t *testing.T) { // Return status 200 but invalid JSON body; Chat should return an error srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
