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 | |
| parent | 09b33e65d92f5fb5b907e49c3d27584615cf2b83 (diff) | |
tests: expand negative SSE and table-driven coverage; add docs/testing.md; use shared fixtures
| -rw-r--r-- | REPORT.md | 4 | ||||
| -rw-r--r-- | docs/testing.md | 16 | ||||
| -rw-r--r-- | internal/llm/copilot_http_test.go | 40 | ||||
| -rw-r--r-- | internal/llm/openai_http_test.go | 14 |
4 files changed, 72 insertions, 2 deletions
@@ -77,7 +77,7 @@ Legend: [ ] pending · [~] in progress · [x] done/partially done - [x] Expand Copilot mocked responses: multi-choice, error object in body; assert parsing and error propagation. 6) General -- [x] Convert repetitive tests to table-driven style where appropriate (e.g., completion prefix/strip; instruction markers; label/filter). +- [x] Convert repetitive tests to table-driven style where appropriate (e.g., completion prefix/strip; instruction markers; label/filter; code fences/inline spans). - [ ] Introduce a shared set of realistic mock responses (multi-line code, markdown, malformed json) and reuse across tests. ## Progress (latest) @@ -103,7 +103,7 @@ Legend: [ ] pending · [~] in progress · [x] done/partially done - [x] 3) lsp e2e chat/document: chat test now uses multi-line reply and validates insertion contains both lines; document resolve uses multi-line docblock. - [x] 4) lsp completion: manual-invoke test now uses a multi-line realistic function signature with body; still passes and exercises formatting. - [x] 5) llm providers: added OpenAI success + SSE stream and Copilot token+chat + Codex SSE tests; coverage ≥80%. Expanded with multi-choice and error-body cases. -- [x] 6) General: introduced shared fixtures (internal/testutil) and added table-driven tests for code fences, inline spans, label selection, prefix stripping, and instruction markers. +- [x] 6) General: introduced shared fixtures (internal/testutil) and added table-driven tests for code fences, inline spans, label selection, prefix stripping, and instruction markers. Documented patterns in docs/testing.md. - [x] Added table-driven tests for instruction marker extraction and prefix stripping. ## Next actions (prioritized) diff --git a/docs/testing.md b/docs/testing.md new file mode 100644 index 0000000..eff6f2e --- /dev/null +++ b/docs/testing.md @@ -0,0 +1,16 @@ +# Testing Guide + +This repository includes a growing test suite designed to be realistic and robust. + +Key patterns: + +- Table‑driven tests: consolidate repetitive scenarios into concise tables (see `internal/lsp/*_table_test.go`). +- Shared fixtures: use `internal/testutil/fixtures.go` for multi‑line docblocks, chat replies, function suggestions, and markdown fences. +- Provider mocks: use `httptest.Server` and/or custom `http.RoundTripper` to simulate OpenAI/Copilot/Ollama responses, including success, stream (SSE), and error cases. +- E2E LSP tests: capture JSON‑RPC frames from the in‑memory server (`captureResponse`, `captureRequest`) and validate code actions, resolves, and chat edits. + +Suggested additions: + +- Expand table‑driven coverage for completion edit computations and label/filter selection. +- Add more negative tests (malformed SSE/JSON payloads) to assert robust error handling. + 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) { |
