summaryrefslogtreecommitdiff
path: root/internal/lsp/handlers_test.go
blob: 1b5080a31b97bf2df57b153b483de5e7e641c19a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package lsp

import (
    "strings"
    "testing"
)

func TestInParamList(t *testing.T) {
    line := "func foo(a int, b string) int {"
    if !inParamList(line, 15) { // inside params
        t.Fatalf("expected inParamList true for cursor inside params")
    }
    if inParamList(line, 2) { // before 'func'
        t.Fatalf("expected inParamList false for cursor before params")
    }
    if inParamList(line, len(line)) { // after ')'
        t.Fatalf("expected inParamList false for cursor after params")
    }
}

func TestComputeWordStart(t *testing.T) {
    current := "fmt.Prin"
    // Cursor after the word (index 8)
    got := computeWordStart(current, 8)
    // should stop after the dot at index 4
    if want := 4; got != want {
        t.Fatalf("computeWordStart got %d want %d", got, want)
    }
}

func TestComputeTextEditAndFilter_InParams(t *testing.T) {
    current := "func foo(a int, b string) {" // ')' at index 26
    p := CompletionParams{Position: Position{Line: 10, Character: 20}}
    te, filter := computeTextEditAndFilter("x int, y string", true, current, p)

    if te == nil {
        t.Fatalf("expected TextEdit")
    }
    // left should be after '(' which is at index 8
    if te.Range.Start.Line != 10 || te.Range.Start.Character != 9 {
        t.Fatalf("start got line=%d char=%d want line=10 char=9", te.Range.Start.Line, te.Range.Start.Character)
    }
    // right should clamp to cursor (20)
    if te.Range.End.Line != 10 || te.Range.End.Character != 20 {
        t.Fatalf("end got line=%d char=%d want line=10 char=20", te.Range.End.Line, te.Range.End.Character)
    }
    if filter == "" {
        t.Fatalf("expected non-empty filter inside params")
    }
}

func TestComputeTextEditAndFilter_Word(t *testing.T) {
    current := "fmt.Prin"
    p := CompletionParams{Position: Position{Line: 2, Character: len(current)}}
    te, filter := computeTextEditAndFilter("Println", false, current, p)
    if te == nil {
        t.Fatalf("expected TextEdit")
    }
    if te.Range.Start.Character != 4 || te.Range.End.Character != len(current) {
        t.Fatalf("range chars got %d..%d want 4..%d", te.Range.Start.Character, te.Range.End.Character, len(current))
    }
    if filter != "Prin" {
        t.Fatalf("filter got %q want %q", filter, "Prin")
    }
}

func TestLabelForCompletion(t *testing.T) {
    if got := labelForCompletion("Println", "Pri"); got != "Println" {
        t.Fatalf("label mismatch got %q want %q", got, "Println")
    }
    if got := labelForCompletion("Println", "X"); got != "X" {
        t.Fatalf("label mismatch with filter got %q want %q", got, "X")
    }
    if got := labelForCompletion("Println\nmore", ""); got != "Println" {
        t.Fatalf("label firstLine got %q want %q", got, "Println")
    }
}

func TestBuildPrompts_InParams(t *testing.T) {
    p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///t.go"}, Position: Position{Line: 1, Character: 12}}
    sys, user := buildPrompts(true, p, "above", "func foo(", "below", "func foo(")
    if sys == "" || user == "" {
        t.Fatalf("expected non-empty prompts")
    }
    if want := "function signatures"; !contains(sys, want) {
        t.Fatalf("system prompt missing %q: %q", want, sys)
    }
    if want := "parameter list"; !contains(user, want) {
        t.Fatalf("user prompt missing %q: %q", want, user)
    }
}

func TestBuildPrompts_Outside(t *testing.T) {
    p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///t.go"}, Position: Position{Line: 1, Character: 5}}
    sys, user := buildPrompts(false, p, "ab", "cur", "be", "fnctx")
    if sys == "" || user == "" {
        t.Fatalf("expected non-empty prompts")
    }
    if want := "completion engine"; !contains(sys, want) {
        t.Fatalf("system prompt missing %q: %q", want, sys)
    }
    if want := "Provide the next likely code"; !contains(user, want) {
        t.Fatalf("user prompt missing %q: %q", want, user)
    }
}

func TestComputeTextEditAndFilter_NoParensFallback(t *testing.T) {
    current := "func foo bar" // no parentheses
    cursor := len(current)
    p := CompletionParams{Position: Position{Line: 0, Character: cursor}}
    te, filter := computeTextEditAndFilter("baz", true, current, p)
    if te == nil {
        t.Fatalf("expected TextEdit from fallback path")
    }
    // fallback should behave like word edit; start at last space + 1
    lastSpace := strings.LastIndex(current, " ")
    if te.Range.Start.Character != lastSpace+1 || te.Range.End.Character != cursor {
        t.Fatalf("range got %d..%d want %d..%d", te.Range.Start.Character, te.Range.End.Character, lastSpace+1, cursor)
    }
    if filter != "bar" {
        t.Fatalf("filter got %q want %q", filter, "bar")
    }
}

// small helper to avoid importing strings
func contains(s, sub string) bool { return len(s) >= len(sub) && (func() bool { i := 0; for i+len(sub) <= len(s) { if s[i:i+len(sub)] == sub { return true }; i++ }; return false })() }




func TestCollectPromptRemovalEdits(t *testing.T) {
    s := newTestServer()
    uri := "file:///x.go"
    src := `keep ;tag; this and ;another; that
no markers here`
    s.setDocument(uri, src)
    edits := s.collectPromptRemovalEdits(uri)
    if len(edits) != 2 {
        t.Fatalf("expected 2 edits, got %d", len(edits))
    }
    // First occurrence ;tag;
    e0 := edits[0]
    if e0.Range.Start.Line != 0 {
        t.Fatalf("e0 start line=%d want 0", e0.Range.Start.Line)
    }
    if s.getDocument(uri).lines[0][e0.Range.Start.Character:e0.Range.Start.Character+1] != ";" {
        t.Fatalf("e0 start not at ;")
    }
}

func TestCollectPromptRemovalEdits_SkipSpacedMarkers(t *testing.T) {
    s := newTestServer()
    uri := "file:///y.go"
    // Only ;ok; should be removed; "; spaced ;" must be ignored
    src := `prefix ;ok; middle ; spaced ; suffix`
    s.setDocument(uri, src)
    edits := s.collectPromptRemovalEdits(uri)
    if len(edits) != 1 {
        t.Fatalf("expected 1 edit (only ;ok;), got %d", len(edits))
    }
    // Ensure the removed region starts at the first ';' of ;ok;
    line := s.getDocument(uri).lines[0]
    wantStart := strings.Index(line, ";ok;")
    if wantStart < 0 {
        t.Fatalf("test setup: could not find ;ok; in %q", line)
    }
    if edits[0].Range.Start.Line != 0 || edits[0].Range.Start.Character != wantStart {
        t.Fatalf("unexpected first edit start: got line=%d char=%d want line=0 char=%d", edits[0].Range.Start.Line, edits[0].Range.Start.Character, wantStart)
    }
}

func TestCollectPromptRemovalEdits_DoubleSemicolonRemovesWholeLine(t *testing.T) {
    s := newTestServer()
    uri := "file:///z.go"
    line0 := "keep"
    line1 := ";;todo; remove this whole line"
    line2 := "keep ;ok; end"
    src := strings.Join([]string{line0, line1, line2}, "\n")
    s.setDocument(uri, src)
    edits := s.collectPromptRemovalEdits(uri)
    if len(edits) != 2 {
        t.Fatalf("expected 2 edits (whole line + ;ok;), got %d", len(edits))
    }
    // Find the whole-line removal for line1
    found := false
    for _, e := range edits {
        if e.Range.Start.Line == 1 && e.Range.Start.Character == 0 && e.Range.End.Line == 1 && e.Range.End.Character == len(line1) {
            found = true
            break
        }
    }
    if !found {
        t.Fatalf("did not find whole-line removal edit for line 1")
    }
}

func TestCollectPromptRemovalEdits_SkipSpacedDouble(t *testing.T) {
    s := newTestServer()
    uri := "file:///w.go"
    src := "prefix ;; spaced ; suffix"
    s.setDocument(uri, src)
    edits := s.collectPromptRemovalEdits(uri)
    if len(edits) != 0 {
        t.Fatalf("expected 0 edits for spaced double-semicolon trigger, got %d", len(edits))
    }
}

func TestInstructionFromSelection_OrderPreference(t *testing.T) {
    // Earliest wins within a line
    line := "code /*block first*/ // later ;tag;"
    instr, cleaned := instructionFromSelection(line)
    if instr != "block first" {
        t.Fatalf("want block comment instr, got %q", instr)
    }
    if strings.Contains(cleaned, "block first") {
        t.Fatalf("cleaned should not contain the block comment")
    }
}

func TestInstructionFromSelection_SemicolonBeatsCommentIfEarlier(t *testing.T) {
    line := ";do this;// later"
    instr, cleaned := instructionFromSelection(line)
    if instr != "do this" {
        t.Fatalf("want semicolon instr, got %q", instr)
    }
    if strings.Contains(cleaned, ";do this;") {
        t.Fatalf("cleaned should have semicolon tag removed")
    }
}

func TestInstructionFromSelection_HTMLAndLineComments(t *testing.T) {
    line := "prefix <!-- html note --> suffix"
    instr, cleaned := instructionFromSelection(line)
    if instr != "html note" {
        t.Fatalf("want html note, got %q", instr)
    }
    if strings.Contains(cleaned, "<!--") || strings.Contains(cleaned, "-->") {
        t.Fatalf("cleaned should remove html comment markers")
    }
}

func TestStripDuplicateAssignmentPrefix(t *testing.T) {
    prefix := "matrix := "
    sug := "matrix := NewMatrix(2,2)"
    got := stripDuplicateAssignmentPrefix(prefix, sug)
    if got != "NewMatrix(2,2)" {
        t.Fatalf("dup strip failed: got %q", got)
    }
    // '=' variant
    prefix2 := "x = "
    sug2 := "x = y + 1"
    got2 := stripDuplicateAssignmentPrefix(prefix2, sug2)
    if got2 != "y + 1" {
        t.Fatalf("dup strip '=' failed: got %q", got2)
    }
}