From 448d4b169904cfd6e1f701524539a27d8de18734 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 4 Sep 2025 15:28:38 +0300 Subject: 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% --- internal/llm/openai_http_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'internal/llm/openai_http_test.go') 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") } +} -- cgit v1.2.3