diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-04 16:16:23 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-04 16:16:23 +0300 |
| commit | 2a6ff853c20e6c1c780c69affdadacda2db202b6 (patch) | |
| tree | b987323524c026dd86280e28cb9f696fc3fade5b /internal | |
| parent | 09b33e65d92f5fb5b907e49c3d27584615cf2b83 (diff) | |
tests: expand negative SSE and table-driven coverage; add docs/testing.md; use shared fixtures
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/llm/copilot_http_test.go | 40 | ||||
| -rw-r--r-- | internal/llm/openai_http_test.go | 14 |
2 files changed, 54 insertions, 0 deletions
diff --git a/internal/llm/copilot_http_test.go b/internal/llm/copilot_http_test.go index 4c2b7fe..30144d1 100644 --- a/internal/llm/copilot_http_test.go +++ b/internal/llm/copilot_http_test.go @@ -108,6 +108,46 @@ func TestCopilot_Chat_MultiChoice_And_ErrorBody(t *testing.T) { } } +func TestCopilot_CodeCompletion_MalformedAndEmpty(t *testing.T) { + c := newCopilot("https://api.githubcopilot.com", "gpt-4o-mini", "API", 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 + } + if r.URL.Host == "copilot-proxy.githubusercontent.com" && strings.HasSuffix(r.URL.Path, "/v1/engines/copilot-codex/completions") { + rw := httptest.NewRecorder() + // malformed line + rw.WriteString("data: {bad}\n") + // done; should produce empty suggestions + rw.WriteString("data: [DONE]\n") + 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.CodeCompletion(context.Background(), "p", "s", 1, "go", 0.1) + if err != nil { t.Fatalf("unexpected error: %v", err) } + if len(out) != 0 { t.Fatalf("expected empty suggestions, got %#v", out) } + + // Now include one good chunk after malformed + tr2 := 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 + } + if r.URL.Host == "copilot-proxy.githubusercontent.com" && strings.HasSuffix(r.URL.Path, "/v1/engines/copilot-codex/completions") { + rw := httptest.NewRecorder() + rw.WriteString("data: {bad}\n") + rw.WriteString("data: {\"choices\":[{\"index\":0,\"text\":\"OK\"}]}\n") + rw.WriteString("data: [DONE]\n") + res := rw.Result(); res.StatusCode = 200; return res, nil + } + return http.DefaultTransport.RoundTrip(r) + }) + c.httpClient = &http.Client{Transport: tr2, Timeout: 5 * time.Second} + out2, err := c.CodeCompletion(context.Background(), "p", "s", 1, "go", 0.1) + if err != nil || len(out2) != 1 || out2[0] != "OK" { t.Fatalf("unexpected: %v %#v", err, out2) } +} + 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 78830ba..45f0c99 100644 --- a/internal/llm/openai_http_test.go +++ b/internal/llm/openai_http_test.go @@ -63,6 +63,20 @@ func TestOpenAI_ChatStream_SSE_ErrorChunk(t *testing.T) { } } +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) { + w.WriteHeader(200) + io.WriteString(w, "{invalid") + })) + 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 decode error for invalid JSON body") + } +} + 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) { |
