diff options
| author | Paul Buetow <paul@buetow.org> | 2025-08-18 09:28:48 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-08-18 09:28:48 +0300 |
| commit | 96ace6c7019a914e21b25fa94ddfc4ee9239c2fb (patch) | |
| tree | 30550bcab30c91e917a4d8b3feccda829a364437 /internal/llm/openai_test.go | |
| parent | 6d29ac7e4b2604b5c7df50f33f8ef2357709faf2 (diff) | |
refactor(lsp,llm,hexailsp,appconfig): split long funcs; add tests
- Extract helpers to keep funcs <=50 lines; no behavior changes
- Add tests for prompt removal, code actions, and LLM request builders
- Table-drive TestInParamList; run gofmt
Diffstat (limited to 'internal/llm/openai_test.go')
| -rw-r--r-- | internal/llm/openai_test.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/llm/openai_test.go b/internal/llm/openai_test.go new file mode 100644 index 0000000..f50b171 --- /dev/null +++ b/internal/llm/openai_test.go @@ -0,0 +1,44 @@ +package llm + +import ( + "bytes" + "encoding/json" + "io" + "net/http" + "strings" + "testing" + "time" +) + +func f64p(v float64) *float64 { return &v } + +func TestBuildOAChatRequest_TempFallbackAndFields(t *testing.T) { + o := Options{Model: "m1", Temperature: 0, MaxTokens: 42, Stop: []string{"END"}} + msgs := []Message{{Role: "user", Content: "hi"}} + req := buildOAChatRequest(o, msgs, f64p(0.3), false) + if req.Model != "m1" || req.Stream { t.Fatalf("model/stream mismatch: %+v", req) } + if req.Temperature == nil || *req.Temperature != 0.3 { t.Fatalf("expected default temp 0.3, got %#v", req.Temperature) } + if req.MaxTokens == nil || *req.MaxTokens != 42 { t.Fatalf("expected max tokens 42") } + if len(req.Stop) != 1 || req.Stop[0] != "END" { t.Fatalf("stop not propagated: %#v", req.Stop) } + if len(req.Messages) != 1 || req.Messages[0].Content != "hi" { t.Fatalf("messages not copied") } + + // stream on + req2 := buildOAChatRequest(o, msgs, f64p(0.3), true) + if !req2.Stream { t.Fatalf("expected stream=true") } +} + +func TestHandleOpenAINon2xx_WithAPIError(t *testing.T) { + api := oaChatResponse{Error: &struct{ Message string `json:"message"`; Type string `json:"type"`; Param any `json:"param"`; Code any `json:"code"` }{Message: "bad", Type: "invalid"}} + b, _ := json.Marshal(api) + resp := &http.Response{StatusCode: 400, Body: io.NopCloser(bytes.NewReader(b))} + if err := handleOpenAINon2xx(resp, time.Now()); err == nil { t.Fatalf("expected error for non-2xx with body") } +} + +func TestParseOpenAIStream_DeliversChunks(t *testing.T) { + stream := "data: {\"choices\":[{\"delta\":{\"content\":\"Hi\"}}]}\n\n" + + "data: [DONE]\n" + resp := &http.Response{Body: io.NopCloser(strings.NewReader(stream))} + var got strings.Builder + if err := parseOpenAIStream(resp, time.Now(), func(s string){ got.WriteString(s) }); err != nil { t.Fatalf("unexpected error: %v", err) } + if got.String() != "Hi" { t.Fatalf("got %q want %q", got.String(), "Hi") } +} |
