diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-06 10:56:27 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-06 10:56:27 +0300 |
| commit | 320de746f7a2985b60c8564a0e65bdf231e840b7 (patch) | |
| tree | e70bcf50813dba411afa2934e774383124bbc99e /internal/lsp/completion_messages_test.go | |
| parent | 06247527d5170f329b454b42f59a3e4434ab1f4b (diff) | |
use gofumpt
Diffstat (limited to 'internal/lsp/completion_messages_test.go')
| -rw-r--r-- | internal/lsp/completion_messages_test.go | 116 |
1 files changed, 71 insertions, 45 deletions
diff --git a/internal/lsp/completion_messages_test.go b/internal/lsp/completion_messages_test.go index e9ec3e5..28908d5 100644 --- a/internal/lsp/completion_messages_test.go +++ b/internal/lsp/completion_messages_test.go @@ -1,73 +1,99 @@ package lsp import ( - "testing" + "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") - } + 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") } + 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") } + 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) - } + 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 TestBuildPrompts_InParams(t *testing.T) { - p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///x"}, Position: Position{Line:0, Character:5}} - sys, user := buildPrompts(true, p, "a", "func f(x)", "c", "func f(x)") - if !contains(sys, "function signatures") || !contains(user, "parameter list") { t.Fatalf("unexpected in-params prompts") } + p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///x"}, Position: Position{Line: 0, Character: 5}} + sys, user := buildPrompts(true, p, "a", "func f(x)", "c", "func f(x)") + if !contains(sys, "function signatures") || !contains(user, "parameter list") { + t.Fatalf("unexpected in-params prompts") + } } func TestPostProcessCompletion_CodeFencesAndDuplicates(t *testing.T) { - s := newTestServer() - // code fences - cleaned := s.postProcessCompletion("```go\nname := value\n```", "", "") - if cleaned == "" { t.Fatalf("expected non-empty after fence removal") } - // duplicate assignment prefix strip - cleaned2 := s.postProcessCompletion("name := other", "name := ", "name := ") - if cleaned2 == "" || cleaned2 == "name := other" { t.Fatalf("expected duplicate assignment prefix stripped: %q", cleaned2) } + s := newTestServer() + // code fences + cleaned := s.postProcessCompletion("```go\nname := value\n```", "", "") + if cleaned == "" { + t.Fatalf("expected non-empty after fence removal") + } + // duplicate assignment prefix strip + cleaned2 := s.postProcessCompletion("name := other", "name := ", "name := ") + if cleaned2 == "" || cleaned2 == "name := other" { + t.Fatalf("expected duplicate assignment prefix stripped: %q", cleaned2) + } } -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 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 + for i := 0; i+len(sub) <= len(s); i++ { + if s[i:i+len(sub)] == sub { + return i + } + } + return -1 } |
