From 5e966f50111adf6e2cb2683fe588f6fe033fa931 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 6 Sep 2025 13:18:21 +0300 Subject: fix unit test coverage --- internal/lsp/codeaction_prompts_test.go | 171 ++++++++++++++++---------------- 1 file changed, 85 insertions(+), 86 deletions(-) (limited to 'internal/lsp/codeaction_prompts_test.go') diff --git a/internal/lsp/codeaction_prompts_test.go b/internal/lsp/codeaction_prompts_test.go index 6b2ce8c..bbfad10 100644 --- a/internal/lsp/codeaction_prompts_test.go +++ b/internal/lsp/codeaction_prompts_test.go @@ -1,102 +1,101 @@ package lsp import ( - "encoding/json" - "testing" + "encoding/json" + "testing" ) func TestResolveCodeAction_UsesRewritePrompts(t *testing.T) { - s := newTestServer() - cap := &captureLLM{} - s.llmClient = cap - s.promptRewriteSystem = "RSYS" - s.promptRewriteUser = "RUSER {{instruction}} {{selection}}" - uri := "file:///x.go" - s.setDocument(uri, "package p\nvar a=1\n") - payload := struct { - Type string `json:"type"` - URI string `json:"uri"` - Range Range `json:"range"` - Instruction string `json:"instruction"` - Selection string `json:"selection"` - }{Type: "rewrite", URI: uri, Range: Range{Start: Position{Line: 1}, End: Position{Line: 1, Character: 5}}, Instruction: "do it", Selection: "var a"} - raw, _ := json.Marshal(payload) - ca := CodeAction{Title: "Hexai: rewrite selection", Data: raw} - _, _ = s.resolveCodeAction(ca) - if len(cap.msgs) < 2 { - t.Fatalf("expected chat messages") - } - if cap.msgs[0].Content != "RSYS" || cap.msgs[1].Role != "user" || cap.msgs[1].Content != "RUSER do it var a" { - t.Fatalf("unexpected rewrite prompts: %#v", cap.msgs) - } + s := newTestServer() + cap := &captureLLM{} + s.llmClient = cap + s.promptRewriteSystem = "RSYS" + s.promptRewriteUser = "RUSER {{instruction}} {{selection}}" + uri := "file:///x.go" + s.setDocument(uri, "package p\nvar a=1\n") + payload := struct { + Type string `json:"type"` + URI string `json:"uri"` + Range Range `json:"range"` + Instruction string `json:"instruction"` + Selection string `json:"selection"` + }{Type: "rewrite", URI: uri, Range: Range{Start: Position{Line: 1}, End: Position{Line: 1, Character: 5}}, Instruction: "do it", Selection: "var a"} + raw, _ := json.Marshal(payload) + ca := CodeAction{Title: "Hexai: rewrite selection", Data: raw} + _, _ = s.resolveCodeAction(ca) + if len(cap.msgs) < 2 { + t.Fatalf("expected chat messages") + } + if cap.msgs[0].Content != "RSYS" || cap.msgs[1].Role != "user" || cap.msgs[1].Content != "RUSER do it var a" { + t.Fatalf("unexpected rewrite prompts: %#v", cap.msgs) + } } func TestResolveCodeAction_UsesDiagnosticsPrompts(t *testing.T) { - s := newTestServer() - cap := &captureLLM{} - s.llmClient = cap - s.promptDiagnosticsSystem = "DSYS" - s.promptDiagnosticsUser = "DUSER {{diagnostics}} {{selection}}" - uri := "file:///x.go" - s.setDocument(uri, "package p\nvar a=1\n") - payload := struct { - Type string `json:"type"` - URI string `json:"uri"` - Range Range `json:"range"` - Selection string `json:"selection"` - Diagnostics []Diagnostic `json:"diagnostics"` - }{Type: "diagnostics", URI: uri, Range: Range{Start: Position{Line: 1}}, Selection: "var a", Diagnostics: []Diagnostic{{Message: "oops1"}, {Message: "oops2"}}} - raw, _ := json.Marshal(payload) - ca := CodeAction{Title: "Hexai: resolve diagnostics", Data: raw} - _, _ = s.resolveCodeAction(ca) - if len(cap.msgs) < 2 { - t.Fatalf("expected chat messages") - } - if cap.msgs[0].Content != "DSYS" || cap.msgs[1].Role != "user" { - t.Fatalf("unexpected diagnostics prompts: %#v", cap.msgs) - } - if got := cap.msgs[1].Content; !(contains(got, "oops1") && contains(got, "oops2") && contains(got, "var a")) { - t.Fatalf("diagnostics/user content mismatch: %q", got) - } + s := newTestServer() + cap := &captureLLM{} + s.llmClient = cap + s.promptDiagnosticsSystem = "DSYS" + s.promptDiagnosticsUser = "DUSER {{diagnostics}} {{selection}}" + uri := "file:///x.go" + s.setDocument(uri, "package p\nvar a=1\n") + payload := struct { + Type string `json:"type"` + URI string `json:"uri"` + Range Range `json:"range"` + Selection string `json:"selection"` + Diagnostics []Diagnostic `json:"diagnostics"` + }{Type: "diagnostics", URI: uri, Range: Range{Start: Position{Line: 1}}, Selection: "var a", Diagnostics: []Diagnostic{{Message: "oops1"}, {Message: "oops2"}}} + raw, _ := json.Marshal(payload) + ca := CodeAction{Title: "Hexai: resolve diagnostics", Data: raw} + _, _ = s.resolveCodeAction(ca) + if len(cap.msgs) < 2 { + t.Fatalf("expected chat messages") + } + if cap.msgs[0].Content != "DSYS" || cap.msgs[1].Role != "user" { + t.Fatalf("unexpected diagnostics prompts: %#v", cap.msgs) + } + if got := cap.msgs[1].Content; !(contains(got, "oops1") && contains(got, "oops2") && contains(got, "var a")) { + t.Fatalf("diagnostics/user content mismatch: %q", got) + } } func TestResolveCodeAction_UsesDocumentPrompts(t *testing.T) { - s := newTestServer() - cap := &captureLLM{} - s.llmClient = cap - s.promptDocumentSystem = "DOCSYS" - s.promptDocumentUser = "DOCUSER {{selection}}" - uri := "file:///x.go" - s.setDocument(uri, "package p\nvar a=1\n") - payload := struct { - Type string `json:"type"` - URI string `json:"uri"` - Range Range `json:"range"` - Selection string `json:"selection"` - }{Type: "document", URI: uri, Range: Range{Start: Position{Line: 1}}, Selection: "var a"} - raw, _ := json.Marshal(payload) - ca := CodeAction{Title: "Hexai: document selection", Data: raw} - _, _ = s.resolveCodeAction(ca) - if len(cap.msgs) < 2 { - t.Fatalf("expected chat messages") - } - if cap.msgs[0].Content != "DOCSYS" || cap.msgs[1].Content != "DOCUSER var a" { - t.Fatalf("unexpected document prompts: %#v", cap.msgs) - } + s := newTestServer() + cap := &captureLLM{} + s.llmClient = cap + s.promptDocumentSystem = "DOCSYS" + s.promptDocumentUser = "DOCUSER {{selection}}" + uri := "file:///x.go" + s.setDocument(uri, "package p\nvar a=1\n") + payload := struct { + Type string `json:"type"` + URI string `json:"uri"` + Range Range `json:"range"` + Selection string `json:"selection"` + }{Type: "document", URI: uri, Range: Range{Start: Position{Line: 1}}, Selection: "var a"} + raw, _ := json.Marshal(payload) + ca := CodeAction{Title: "Hexai: document selection", Data: raw} + _, _ = s.resolveCodeAction(ca) + if len(cap.msgs) < 2 { + t.Fatalf("expected chat messages") + } + if cap.msgs[0].Content != "DOCSYS" || cap.msgs[1].Content != "DOCUSER var a" { + t.Fatalf("unexpected document prompts: %#v", cap.msgs) + } } func TestGenerateGoTest_UsesPrompts(t *testing.T) { - s := newTestServer() - cap := &captureLLM{} - s.llmClient = cap - s.promptGoTestSystem = "GTSYS" - s.promptGoTestUser = "GTUSER {{function}}" - _ = s.generateGoTestFunction("func Add(a,b int) int {return a+b}") - if len(cap.msgs) < 2 { - t.Fatalf("expected chat messages") - } - if cap.msgs[0].Content != "GTSYS" || !contains(cap.msgs[1].Content, "func Add") { - t.Fatalf("unexpected gotest prompts: %#v", cap.msgs) - } + s := newTestServer() + cap := &captureLLM{} + s.llmClient = cap + s.promptGoTestSystem = "GTSYS" + s.promptGoTestUser = "GTUSER {{function}}" + _ = s.generateGoTestFunction("func Add(a,b int) int {return a+b}") + if len(cap.msgs) < 2 { + t.Fatalf("expected chat messages") + } + if cap.msgs[0].Content != "GTSYS" || !contains(cap.msgs[1].Content, "func Add") { + t.Fatalf("unexpected gotest prompts: %#v", cap.msgs) + } } - -- cgit v1.2.3