summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-09-04 09:39:57 +0300
committerPaul Buetow <paul@buetow.org>2025-09-04 09:39:57 +0300
commit3c322b7046669a77c276ce05469bfc2db0b446b2 (patch)
treeda3e37ee62534aa05118a7e8c72ed7e3bcb55d96
parented804886d732997822c94564d1e73b159bf49927 (diff)
tests(lsp): add diagnostics action builder and completion message/prefix tests; lsp ~72%
-rw-r--r--internal/lsp/completion_messages_test.go58
-rw-r--r--internal/lsp/diagnostics_action_test.go30
2 files changed, 88 insertions, 0 deletions
diff --git a/internal/lsp/completion_messages_test.go b/internal/lsp/completion_messages_test.go
new file mode 100644
index 0000000..eb16ccc
--- /dev/null
+++ b/internal/lsp/completion_messages_test.go
@@ -0,0 +1,58 @@
+package lsp
+
+import (
+ "testing"
+)
+
+func TestBuildCompletionMessages_InlinePromptOverridesSys(t *testing.T) {
+ s := newTestServer()
+ p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///x"}, Position: Position{Line:0, Character:1}}
+ msgs := s.buildCompletionMessages(true, false, "", false, p, "above", "current", "below", "func f")
+ if len(msgs) < 2 { t.Fatalf("expected messages") }
+ if msgs[0].Role != "system" || msgs[1].Role != "user" { t.Fatalf("unexpected roles") }
+ if want := "precise code completion/refactoring engine"; !contains(msgs[0].Content, want) {
+ t.Fatalf("inline sys not applied")
+ }
+}
+
+func TestBuildCompletionMessages_ExtraContextIncluded(t *testing.T) {
+ s := newTestServer()
+ p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///x"}, Position: Position{Line:0, Character:1}}
+ msgs := s.buildCompletionMessages(false, true, "EXTRA", false, p, "a", "b", "c", "f")
+ found := false
+ for _, m := range msgs { if m.Role == "user" && contains(m.Content, "Additional context:") { found = true } }
+ if !found { t.Fatalf("missing extra context message") }
+}
+
+func TestPrefixHeuristic_AllVariants(t *testing.T) {
+ s := newTestServer()
+ // manual invoke requires at least min prefix; set to 2
+ s.manualInvokeMinPrefix = 2
+ cur := "a"
+ p := CompletionParams{Position: Position{Line:0, Character:1}}
+ if s.prefixHeuristicAllows(false, cur, p, true) { t.Fatalf("should require >=2 prefix on manual invoke") }
+ // structural triggers allow without prefix
+ if !s.prefixHeuristicAllows(false, "fmt.", CompletionParams{Position: Position{Line:0, Character:4}}, false) { t.Fatalf("dot trigger should allow") }
+}
+
+func TestBuildDocString_Contents(t *testing.T) {
+ s := newTestServer()
+ p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///x"}, Position: Position{Line:3, Character:7}}
+ got := s.buildDocString(p, "above", "current", "below", "func ctx")
+ if !contains(got, "file: file:///x") || !contains(got, "line: 3") || !contains(got, "function: func ctx") {
+ t.Fatalf("unexpected doc string: %q", got)
+ }
+}
+
+func contains(s, sub string) bool { return len(s) >= len(sub) && (s == sub || (len(sub) > 0 && (stringIndex(s, sub) >= 0))) }
+func stringIndex(s, sub string) int { return len([]rune(s[:])) - len([]rune(s[:])) + (func() int { return intIndex(s, sub) })() }
+func intIndex(s, sub string) int { return Index(s, sub) }
+
+// Go's strings.Index is fine; wrapped to avoid extra imports in this small test.
+func Index(s, sub string) int {
+ for i := 0; i+len(sub) <= len(s); i++ {
+ if s[i:i+len(sub)] == sub { return i }
+ }
+ return -1
+}
+
diff --git a/internal/lsp/diagnostics_action_test.go b/internal/lsp/diagnostics_action_test.go
new file mode 100644
index 0000000..1a9201f
--- /dev/null
+++ b/internal/lsp/diagnostics_action_test.go
@@ -0,0 +1,30 @@
+package lsp
+
+import (
+ "encoding/json"
+ "io"
+ "log"
+ "testing"
+)
+
+func TestHandleCodeAction_ListsDiagnosticsActionWhenOverlap(t *testing.T) {
+ s := &Server{logger: log.New(io.Discard, "", 0), docs: make(map[string]*document)}
+ s.llmClient = fakeLLM{resp: "fixed"}
+ uri := "file:///x.go"
+ s.setDocument(uri, "package p\nvar a=1\n")
+ // Selection overlaps line 1
+ sel := Range{Start: Position{Line:1, Character:0}, End: Position{Line:1, Character:5}}
+ // Provide diagnostics in the action context with one overlapping
+ ctx := CodeActionContext{Diagnostics: []Diagnostic{
+ {Range: Range{Start: Position{Line:1, Character:0}, End: Position{Line:1, Character:3}}, Message: "in"},
+ {Range: Range{Start: Position{Line:0, Character:0}, End: Position{Line:0, Character:1}}, Message: "out"},
+ }}
+ rawCtx, _ := json.Marshal(ctx)
+ p := CodeActionParams{TextDocument: TextDocumentIdentifier{URI: uri}, Range: sel, Context: json.RawMessage(rawCtx)}
+ ca := s.buildDiagnosticsCodeAction(p, "var a=1")
+ if ca == nil { t.Fatalf("expected diagnostics action") }
+ // Resolve should produce an edit
+ resolved, ok := s.resolveCodeAction(*ca)
+ if !ok || resolved.Edit == nil { t.Fatalf("expected resolved edit from diagnostics") }
+}
+