diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-04 16:04:58 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-04 16:04:58 +0300 |
| commit | bf53cf2a673af254d7a08bc3b2ab815a08f66117 (patch) | |
| tree | 3c29faaaaa6777d9a9346c90bc7cba88978d8477 /internal/lsp | |
| parent | 448d4b169904cfd6e1f701524539a27d8de18734 (diff) | |
tests: add shared test fixtures, expand provider breadth (multi-choice, error bodies), add LSP rewrite/diagnostics realism and table-driven tests
Diffstat (limited to 'internal/lsp')
| -rw-r--r-- | internal/lsp/codeaction_more_test.go | 3 | ||||
| -rw-r--r-- | internal/lsp/completion_prefix_strip_test.go | 7 | ||||
| -rw-r--r-- | internal/lsp/handlers_end_to_end_test.go | 7 | ||||
| -rw-r--r-- | internal/lsp/init_and_trigger_test.go | 52 | ||||
| -rw-r--r-- | internal/lsp/instruction_table_test.go | 25 | ||||
| -rw-r--r-- | internal/lsp/prefix_table_test.go | 24 | ||||
| -rw-r--r-- | internal/lsp/rewrite_diagnostics_realism_test.go | 62 |
7 files changed, 174 insertions, 6 deletions
diff --git a/internal/lsp/codeaction_more_test.go b/internal/lsp/codeaction_more_test.go index 387afb5..412d988 100644 --- a/internal/lsp/codeaction_more_test.go +++ b/internal/lsp/codeaction_more_test.go @@ -5,11 +5,12 @@ import ( "path/filepath" "strings" "testing" + tut "codeberg.org/snonux/hexai/internal/testutil" ) func TestBuildDocumentCodeAction_AndResolve(t *testing.T) { s := newTestServer() - s.llmClient = fakeLLM{resp: "// doc\nfunc add(a,b int) int { return a+b }"} + s.llmClient = fakeLLM{resp: tut.MultilineDocBlock()+"\n"+"func add(a,b int) int { return a+b }"} uri := "file:///doc.go" s.setDocument(uri, "package x\nfunc add(a,b int) int {return a+b}") p := CodeActionParams{TextDocument: TextDocumentIdentifier{URI: uri}, Range: Range{Start: Position{Line:1, Character:0}, End: Position{Line:1, Character:10}}} diff --git a/internal/lsp/completion_prefix_strip_test.go b/internal/lsp/completion_prefix_strip_test.go index 64cca49..99a08d6 100644 --- a/internal/lsp/completion_prefix_strip_test.go +++ b/internal/lsp/completion_prefix_strip_test.go @@ -1,8 +1,9 @@ package lsp import ( - "encoding/json" - "testing" + "encoding/json" + "testing" + tut "codeberg.org/snonux/hexai/internal/testutil" ) func TestStripDuplicateGeneralPrefix_ExactOverlap(t *testing.T) { @@ -40,7 +41,7 @@ func TestStripDuplicateAssignmentPrefix_AssignAndWalrus(t *testing.T) { func TestTryLLMCompletion_ManualInvokeAfterWhitespace_Allows(t *testing.T) { s := &Server{maxTokens: 32, triggerChars: []string{".", ":", "/", "_"}, compCache: make(map[string]string)} - s.llmClient = fakeLLM{resp: "() *CustData"} + s.llmClient = fakeLLM{resp: tut.MultilineFunctionSuggestion()} line := "func fib(i int) " // cursor after space p := CompletionParams{Position: Position{Line: 0, Character: len(line)}, TextDocument: TextDocumentIdentifier{URI: "file://x.go"}} // Simulate manual user invocation (TriggerKind=1) diff --git a/internal/lsp/handlers_end_to_end_test.go b/internal/lsp/handlers_end_to_end_test.go index ba4a0bc..73478e9 100644 --- a/internal/lsp/handlers_end_to_end_test.go +++ b/internal/lsp/handlers_end_to_end_test.go @@ -9,6 +9,7 @@ import ( "strings" "testing" "time" + tut "codeberg.org/snonux/hexai/internal/testutil" ) // captureResponse decodes a single LSP Response from the server's output buffer. @@ -190,7 +191,7 @@ func TestHandle_Dispatch_Initialize(t *testing.T) { func TestDetectAndHandleChat_InsertsReply(t *testing.T) { var out bytes.Buffer s := &Server{logger: log.New(io.Discard, "", 0), docs: make(map[string]*document), out: &out} - s.llmClient = fakeLLM{resp: "Hello"} + s.llmClient = fakeLLM{resp: tut.MultilineChatReply()} uri := "file:///chat.go" // Place a prompt line with a supported trigger at EOL, then a blank line s.setDocument(uri, "What time?>\n\n") @@ -208,7 +209,9 @@ func TestDetectAndHandleChat_InsertsReply(t *testing.T) { if len(we.Changes) == 0 { t.Fatalf("expected changes in edit") } edits := we.Changes[uri] if len(edits) != 2 { t.Fatalf("expected 2 edits (delete+insert), got %d", len(edits)) } - if !strings.Contains(edits[1].NewText, "> Hello") { t.Fatalf("expected reply insertion with '> Hello', got %q", edits[1].NewText) } + if !strings.Contains(edits[1].NewText, "> Hello") || !strings.Contains(edits[1].NewText, "multi-line reply") { + t.Fatalf("expected multi-line reply insertion, got %q", edits[1].NewText) + } } func TestHandleCodeActionResolve_Diagnostics(t *testing.T) { diff --git a/internal/lsp/init_and_trigger_test.go b/internal/lsp/init_and_trigger_test.go new file mode 100644 index 0000000..cdc907e --- /dev/null +++ b/internal/lsp/init_and_trigger_test.go @@ -0,0 +1,52 @@ +package lsp + +import ( + "bytes" + "encoding/json" + "io" + "log" + "testing" +) + +func TestHandleInitialize_Capabilities(t *testing.T) { + var out bytes.Buffer + s := &Server{logger: log.New(io.Discard, "", 0), docs: make(map[string]*document), out: &out} + s.triggerChars = []string{".", ":"} + req := Request{JSONRPC: "2.0", ID: json.RawMessage("7"), Method: "initialize"} + out.Reset() + s.handleInitialize(req) + resp := captureResponse(t, &out) + var init InitializeResult + b, _ := json.Marshal(resp.Result) + if err := json.Unmarshal(b, &init); err != nil { t.Fatalf("decode init: %v", err) } + if init.Capabilities.CodeActionProvider == nil { t.Fatalf("expected codeActionProvider") } + // CodeActionProvider is any; re-marshal to struct + var cap struct{ ResolveProvider bool `json:"resolveProvider"` } + cb, _ := json.Marshal(init.Capabilities.CodeActionProvider) + _ = json.Unmarshal(cb, &cap) + if !cap.ResolveProvider { t.Fatalf("expected resolveProvider=true") } + if init.Capabilities.CompletionProvider == nil || len(init.Capabilities.CompletionProvider.TriggerCharacters) == 0 { + t.Fatalf("expected trigger characters") } +} + +func TestIsTriggerEvent_Variants(t *testing.T) { + s := newTestServer() + s.triggerChars = []string{".", ":"} + // 1) Manual invoke via context + ctx := struct{ TriggerKind int `json:"triggerKind"` }{TriggerKind:1} + raw, _ := json.Marshal(ctx) + p := CompletionParams{Position: Position{Line:0, Character:1}, Context: json.RawMessage(raw)} + if !s.isTriggerEvent(p, "a") { t.Fatalf("manual invoke should trigger") } + // 2) TriggerCharacter present and allowed + ctx2 := struct{ TriggerKind int `json:"triggerKind"`; TriggerCharacter string `json:"triggerCharacter"` }{TriggerKind:2, TriggerCharacter: "."} + raw2, _ := json.Marshal(ctx2) + p2 := CompletionParams{Position: Position{Line:0, Character:1}, Context: json.RawMessage(raw2)} + if !s.isTriggerEvent(p2, "a.") { t.Fatalf("trigger char should trigger") } + // 3) Fallback char left of cursor + p3 := CompletionParams{Position: Position{Line:0, Character:3}} + if !s.isTriggerEvent(p3, "ab:") { t.Fatalf("fallback char should trigger") } + // 4) Bare ';;' disables trigger + p4 := CompletionParams{Position: Position{Line:0, Character:2}} + if s.isTriggerEvent(p4, ";;") { t.Fatalf("bare ;; should not trigger") } +} + diff --git a/internal/lsp/instruction_table_test.go b/internal/lsp/instruction_table_test.go new file mode 100644 index 0000000..e92ffde --- /dev/null +++ b/internal/lsp/instruction_table_test.go @@ -0,0 +1,25 @@ +package lsp + +import "testing" + +func TestFindFirstInstructionInLine_Table(t *testing.T) { + cases := []struct{ + name string + line string + instr string + }{ + {"strict_semicolon", ";do; trailing", "do"}, + {"c_block", "x /* add docs */ y", "add docs"}, + {"html_comment", "<!-- fix --> code", "fix"}, + {"slash_slash", "code // please refactor", "please refactor"}, + {"hash", "# summarize", "summarize"}, + {"double_dash", "-- rewrite quickly", "rewrite quickly"}, + } + for _, c := range cases { + instr, _, ok := findFirstInstructionInLine(c.line) + if !ok || instr != c.instr { + t.Fatalf("%s: got %q ok=%v", c.name, instr, ok) + } + } +} + diff --git a/internal/lsp/prefix_table_test.go b/internal/lsp/prefix_table_test.go new file mode 100644 index 0000000..0ca23d2 --- /dev/null +++ b/internal/lsp/prefix_table_test.go @@ -0,0 +1,24 @@ +package lsp + +import "testing" + +func TestPrefixStripping_Table(t *testing.T) { + cases := []struct{ name, prefix, sugg, want string }{ + {"assign_walrus", "name := ", "name := compute()", "compute()"}, + {"assign_equals", "x = ", "x = y+1", "y+1"}, + {"general_db", "db.", "db.Query()", "Query()"}, + {"general_func", "func New ", "func New() *T", "() *T"}, + } + for _, c := range cases { + var got string + if c.name == "assign_walrus" || c.name == "assign_equals" { + got = stripDuplicateAssignmentPrefix(c.prefix, c.sugg) + } else { + got = stripDuplicateGeneralPrefix(c.prefix, c.sugg) + } + if got != c.want { + t.Fatalf("%s: got %q want %q", c.name, got, c.want) + } + } +} + diff --git a/internal/lsp/rewrite_diagnostics_realism_test.go b/internal/lsp/rewrite_diagnostics_realism_test.go new file mode 100644 index 0000000..87ff571 --- /dev/null +++ b/internal/lsp/rewrite_diagnostics_realism_test.go @@ -0,0 +1,62 @@ +package lsp + +import ( + "encoding/json" + "testing" +) + +func TestResolveRewrite_MultiLine_PreservesRange(t *testing.T) { + s := newTestServer() + s.llmClient = fakeLLM{resp: "line1\nline2"} + uri := "file:///x.go" + s.setDocument(uri, "package p\nvar a=1\n") + r := Range{Start: Position{Line:1, Character:0}, End: Position{Line:1, Character:5}} + 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: r, Instruction: "expand", Selection: "var a"} + raw, _ := json.Marshal(payload) + ca := CodeAction{Title: "Hexai: rewrite selection", Data: raw} + resolved, ok := s.resolveCodeAction(ca) + if !ok || resolved.Edit == nil { t.Fatalf("expected resolved rewrite edit") } + edits := resolved.Edit.Changes[uri] + if len(edits) != 1 { t.Fatalf("expected 1 edit") } + if edits[0].Range != r { t.Fatalf("range mismatch: got %+v want %+v", edits[0].Range, r) } + if edits[0].NewText == "" || !containsNewline(edits[0].NewText) { + t.Fatalf("expected multi-line replacement text, got %q", edits[0].NewText) + } +} + +func TestResolveDiagnostics_MultiLine_PreservesRange(t *testing.T) { + s := newTestServer() + s.llmClient = fakeLLM{resp: "fixed\nvalue"} + uri := "file:///x.go" + s.setDocument(uri, "package p\nvar x = 1\n") + r := Range{Start: Position{Line:1, Character:0}, End: Position{Line:1, Character:10}} + 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: r, Selection: "var x = 1", Diagnostics: []Diagnostic{{Range: Range{Start: Position{Line:1}, End: Position{Line:1, Character:5}}, Message: "msg"}}} + raw, _ := json.Marshal(payload) + ca := CodeAction{Title: "Hexai: resolve diagnostics", Data: raw} + resolved, ok := s.resolveCodeAction(ca) + if !ok || resolved.Edit == nil { t.Fatalf("expected resolved diagnostics edit") } + edits := resolved.Edit.Changes[uri] + if len(edits) != 1 { t.Fatalf("expected 1 edit") } + if edits[0].Range != r { t.Fatalf("range mismatch: got %+v want %+v", edits[0].Range, r) } + if edits[0].NewText == "" || !containsNewline(edits[0].NewText) { + t.Fatalf("expected multi-line replacement text, got %q", edits[0].NewText) + } +} + +func containsNewline(s string) bool { + for i := 0; i < len(s); i++ { if s[i] == '\n' { return true } } + return false +} + |
