diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-04 15:28:38 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-04 15:28:38 +0300 |
| commit | 448d4b169904cfd6e1f701524539a27d8de18734 (patch) | |
| tree | c102c6d9e228565f54600b3aba173ccdfb75ede6 /internal/llm/openai_http_test.go | |
| parent | 48fac4b473e2564e2e82dad36668277f1071ddd0 (diff) | |
tests(llm): raise coverage to >=80%\n- Add OpenAI/Copilot HTTP success + stream + token tests\n- Cover With* options and NewFromConfig success paths\n- llm package now ~80.3%
Diffstat (limited to 'internal/llm/openai_http_test.go')
| -rw-r--r-- | internal/llm/openai_http_test.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/internal/llm/openai_http_test.go b/internal/llm/openai_http_test.go index 4989067..7ae34be 100644 --- a/internal/llm/openai_http_test.go +++ b/internal/llm/openai_http_test.go @@ -3,9 +3,12 @@ package llm import ( "context" "encoding/json" + "io" "net/http" "net/http/httptest" "testing" + "strings" + "time" ) func TestOpenAI_Chat_Success(t *testing.T) { @@ -25,3 +28,22 @@ func TestOpenAI_Chat_MissingKey(t *testing.T) { if _, err := c.Chat(context.Background(), []Message{{Role:"user", Content:"hi"}}); err == nil { t.Fatalf("expected error for missing key") } } +func TestOpenAI_ChatStream_SSE(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Return SSE-like stream + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"Hi\"}}]}\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 + err := c.ChatStream(context.Background(), []Message{{Role:"user", Content:"hi"}}, func(s string){ got += s }) + if err != nil || got != "Hi" { t.Fatalf("chat stream: %v %q", err, got) } +} + +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") } +} |
