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

import (
	"strings"
	"testing"
)

func TestHasDoubleSemicolonTrigger(t *testing.T) {
	cases := []struct {
		line string
		want bool
	}{
		{">>todo> remove this", true},
		{"prefix >>x> suffix", true},
		{">> spaced >", false},
		{"no markers", false},
		{">>x > space before close", false},
	}
	for _, tc := range cases {
		got := hasDoubleOpenTrigger(tc.line)
		if got != tc.want {
			t.Fatalf("hasDoubleOpenTrigger(%q)=%v want %v", tc.line, got, tc.want)
		}
	}
}

func TestCollectSemicolonMarkers(t *testing.T) {
	line := "keep >ok> this and >another> that"
	edits := collectSemicolonMarkers(line, 7)
	if len(edits) != 2 {
		t.Fatalf("expected 2 edits, got %d", len(edits))
	}
	// Validate the first edit aligns with ;ok;
	start := strings.Index(line, ">ok>")
	if start < 0 {
		t.Fatalf("test setup: missing ;ok;")
	}
	if edits[0].Range.Start.Line != 7 || edits[0].Range.Start.Character != start {
		t.Fatalf("first edit start got line=%d char=%d want line=7 char=%d", edits[0].Range.Start.Line, edits[0].Range.Start.Character, start)
	}
}

func TestPromptRemovalEditsForLine_WholeLine(t *testing.T) {
	line := ">>todo> remove this whole line"
	edits := promptRemovalEditsForLine(line, 3)
	if len(edits) != 1 {
		t.Fatalf("expected 1 whole-line edit, got %d", len(edits))
	}
	e := edits[0]
	if e.Range.Start.Line != 3 || e.Range.End.Line != 3 || e.Range.Start.Character != 0 || e.Range.End.Character != len(line) {
		t.Fatalf("unexpected range for whole-line removal: %+v", e.Range)
	}
}

func TestStripCodeFences(t *testing.T) {
	cases := []struct {
		name string
		in   string
		want string
	}{
		{"no fences", "package main\nfunc x(){}", "package main\nfunc x(){}"},
		{"triple backticks no lang", "```\nA\nB\n```", "A\nB"},
		{"triple backticks with lang", "```go\nfmt.Println(\"hi\")\n```", "fmt.Println(\"hi\")"},
		{"leading/trailing spaces", " \n```python\nprint('x')\n```\n ", "print('x')"},
		{"single line fenced", "```go\npackage main\n```", "package main"},
	}
	for _, tc := range cases {
		got := stripCodeFences(tc.in)
		if got != tc.want {
			t.Fatalf("%s: got %q want %q", tc.name, got, tc.want)
		}
	}
}

func TestStripInlineCodeSpan(t *testing.T) {
	cases := []struct {
		name string
		in   string
		want string
	}{
		{"no backticks", "return x + y", "return x + y"},
		{"single inline", "Use `foo(bar)` here", "foo(bar)"},
		{"just inline", "`x := y()`", "x := y()"},
		{"unmatched start", "use `foo(bar) without end", "use `foo(bar) without end"},
		{"multiple spans picks first", "`a` and also `b`", "a"},
		{"leading/trailing spaces", "  text ` z `  ", " z "},
	}
	for _, tc := range cases {
		got := stripInlineCodeSpan(tc.in)
		if got != tc.want {
			t.Fatalf("%s: got %q want %q", tc.name, got, tc.want)
		}
	}
}