summaryrefslogtreecommitdiff
path: root/internal/lsp/completion_prefix_strip_test.go
blob: c8e2bd7007846bb19ef760f44e0ba41f3367c955 (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
package lsp

import (
	"encoding/json"
	"testing"

	tut "codeberg.org/snonux/hexai/internal/testutil"
)

func TestStripDuplicateGeneralPrefix_ExactOverlap(t *testing.T) {
	prefix := "func New "
	sugg := "func New() *CustData"
	got := stripDuplicateGeneralPrefix(prefix, sugg)
	// We expect the already typed prefix to be removed from the suggestion.
	if got == sugg {
		t.Fatalf("expected duplicate prefix to be stripped; got unchanged: %q", got)
	}
	if got != "() *CustData" {
		t.Fatalf("got %q want %q", got, "() *CustData")
	}
}

func TestStripDuplicateGeneralPrefix_TokenBoundarySuffix(t *testing.T) {
	prefix := "db."
	sugg := "db.Query()"
	got := stripDuplicateGeneralPrefix(prefix, sugg)
	if got != "Query()" {
		t.Fatalf("got %q want %q", got, "Query()")
	}
}

func TestStripDuplicateAssignmentPrefix_AssignAndWalrus(t *testing.T) {
	// walrus
	if out := stripDuplicateAssignmentPrefix("name := ", "name := compute()"); out != "compute()" {
		t.Fatalf(":= expected compute(), got %q", out)
	}
	// equals
	if out := stripDuplicateAssignmentPrefix("x = ", "x = y+1"); out != "y+1" {
		t.Fatalf("= expected y+1, got %q", out)
	}
}

func TestTryLLMCompletion_ManualInvokeAfterWhitespace_Allows(t *testing.T) {
	s := newTestServer()
	s.compCache = make(map[string]string)
	cfg := s.cfg
	cfg.MaxTokens = 32
	cfg.TriggerCharacters = []string{".", ":", "/", "_"}
	s.cfg = cfg
	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)
	p.Context = json.RawMessage([]byte(`{"triggerKind":1}`))
	items, ok, _ := s.tryLLMCompletion(p, "", line, "", "", "", false, "")
	if !ok {
		t.Fatalf("expected ok=true for manual invoke after whitespace")
	}
	if len(items) == 0 {
		t.Fatalf("expected at least one completion item")
	}
}

func TestTryLLMCompletion_InlinePromptAlwaysTriggers(t *testing.T) {
	s := newTestServer()
	s.compCache = make(map[string]string)
	cfg := s.cfg
	cfg.MaxTokens = 32
	cfg.TriggerCharacters = []string{".", ":", "/", "_"}
	s.cfg = cfg
	s.llmClient = fakeLLM{resp: "replacement"}
	line := "prefix >!do something> suffix"
	// No trigger char immediately before cursor; place cursor at end
	p := CompletionParams{Position: Position{Line: 0, Character: len(line)}, TextDocument: TextDocumentIdentifier{URI: "file://inline.go"}}
	items, ok, _ := s.tryLLMCompletion(p, "", line, "", "", "", false, "")
	if !ok || len(items) == 0 {
		t.Fatalf("expected completion to trigger on inline >!text> prompt")
	}
}

func TestTryLLMCompletion_DoubleOpenEmpty_DoesNotAutoTrigger(t *testing.T) {
	s := newTestServer()
	s.compCache = make(map[string]string)
	cfg := s.cfg
	cfg.MaxTokens = 32
	cfg.TriggerCharacters = []string{".", ":", "/", "_"}
	s.cfg = cfg
	fake := &countingLLM{}
	s.llmClient = fake
	line := ">>!   " // empty content after double-open should not force-trigger
	p := CompletionParams{Position: Position{Line: 0, Character: len(line)}, TextDocument: TextDocumentIdentifier{URI: "file://empty-inline.go"}}
	items, ok, _ := s.tryLLMCompletion(p, "", line, "", "", "", false, "")
	if !ok {
		t.Fatalf("expected ok=true for non-trigger path")
	}
	if len(items) != 0 {
		t.Fatalf("expected no items when inline double-open is empty")
	}
	if fake.calls != 0 {
		t.Fatalf("LLM should not be called; calls=%d", fake.calls)
	}
}

func TestHasDoubleSemicolonTrigger_Variants(t *testing.T) {
	if hasDoubleOpenTrigger(">>!", ">!", '>', '>') {
		t.Fatalf("bare double-open should not trigger")
	}
	if hasDoubleOpenTrigger(">>! ", ">!", '>', '>') {
		t.Fatalf("double-open followed by space should not trigger")
	}
	if hasDoubleOpenTrigger(">>!>", ">!", '>', '>') {
		t.Fatalf("';;;' should not trigger (no content)")
	}
	if !hasDoubleOpenTrigger(">>!x>", ">!", '>', '>') {
		t.Fatalf("expected trigger for ';;x;' pattern")
	}
}

func TestBareDoubleOpenPreventsAutoTriggerEvenWithOtherTriggers(t *testing.T) {
	s := newTestServer()
	s.compCache = make(map[string]string)
	cfg := s.cfg
	cfg.MaxTokens = 32
	cfg.TriggerCharacters = []string{".", ":", "/", "_"}
	s.cfg = cfg
	fake := &countingLLM{}
	s.llmClient = fake
	// Place a '.' earlier but also include bare double-open at end; should not auto-trigger
	line := "obj. call >>!"
	p := CompletionParams{Position: Position{Line: 0, Character: len(line)}, TextDocument: TextDocumentIdentifier{URI: "file://bare-ds.go"}}
	items, ok, _ := s.tryLLMCompletion(p, "", line, "", "", "", false, "")
	if !ok {
		t.Fatalf("expected ok=true (handled), but not auto-triggering")
	}
	if len(items) != 0 {
		t.Fatalf("expected no items due to bare double-open")
	}
	if fake.calls != 0 {
		t.Fatalf("LLM should not be called; calls=%d", fake.calls)
	}
}

func TestBareDoubleOpenOnNextLine_PreventsAutoTrigger(t *testing.T) {
	s := newTestServer()
	s.compCache = make(map[string]string)
	cfg := s.cfg
	cfg.MaxTokens = 32
	cfg.TriggerCharacters = []string{".", ":", "/", "_"}
	s.cfg = cfg
	fake := &countingLLM{}
	s.llmClient = fake
	current := "expression := flag.String(\"expression\", \"\", \"Expression to evaluate\")"
	below := ">>!"
	p := CompletionParams{Position: Position{Line: 0, Character: len(current)}, TextDocument: TextDocumentIdentifier{URI: "file://nextline.go"}}
	items, ok, _ := s.tryLLMCompletion(p, "", current, below, "", "", false, "")
	if !ok {
		t.Fatalf("expected ok=true handled")
	}
	if len(items) != 0 {
		t.Fatalf("expected no items due to bare double-open on next line")
	}
	if fake.calls != 0 {
		t.Fatalf("LLM should not be called; calls=%d", fake.calls)
	}
}

func TestBareDoubleOpenPreventsManualInvoke(t *testing.T) {
	s := newTestServer()
	s.compCache = make(map[string]string)
	cfg := s.cfg
	cfg.MaxTokens = 32
	cfg.TriggerCharacters = []string{".", ":", "/", "_"}
	s.cfg = cfg
	fake := &countingLLM{}
	s.llmClient = fake
	line := ">>!"
	p := CompletionParams{Position: Position{Line: 0, Character: len(line)}, TextDocument: TextDocumentIdentifier{URI: "file://bare-ds-manual.go"}}
	// Simulate manual invoke
	p.Context = json.RawMessage([]byte(`{"triggerKind":1}`))
	items, ok, _ := s.tryLLMCompletion(p, "", line, "", "", "", false, "")
	if !ok {
		t.Fatalf("expected ok=true (handled)")
	}
	if len(items) != 0 {
		t.Fatalf("expected no items for bare double-open even with manual invoke")
	}
	if fake.calls != 0 {
		t.Fatalf("LLM should not be called; calls=%d", fake.calls)
	}
}