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
|
package lsp
import (
"io"
"log"
"os"
"path/filepath"
"testing"
"codeberg.org/snonux/hexai/internal/appconfig"
"codeberg.org/snonux/hexai/internal/ignore"
"codeberg.org/snonux/hexai/internal/llm"
)
// newIgnoreTestServer creates a Server with an ignore checker configured
// from the given gitRoot and extra patterns.
func newIgnoreTestServer(gitRoot string, useGI bool, extra []string, notifyIgnored *bool) *Server {
cfg := appconfig.App{
IgnoreLSPNotify: notifyIgnored,
InlineOpen: ">!",
InlineClose: ">",
ChatSuffix: ">",
ChatPrefixes: []string{"?", "!", ":", ";"},
}
s := &Server{
logger: log.New(io.Discard, "", 0),
docs: make(map[string]*document),
cfg: cfg,
altClients: make(map[string]llm.Client),
ignoreChecker: ignore.New(gitRoot, useGI, extra),
}
return s
}
func boolPtr(b bool) *bool { return &b }
func TestHandleCompletion_IgnoredFile_WithNotify(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("*.log\n"), 0o644); err != nil {
t.Fatal(err)
}
s := newIgnoreTestServer(dir, true, nil, boolPtr(true))
uri := "file://" + filepath.Join(dir, "debug.log")
ignored, _ := s.isFileIgnored(uri)
if !ignored {
t.Fatal("expected file to be ignored")
}
// Verify notify is enabled
if !s.ignoreLSPNotifyEnabled() {
t.Fatal("expected LSP notify enabled")
}
}
func TestHandleCompletion_IgnoredFile_WithoutNotify(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("*.log\n"), 0o644); err != nil {
t.Fatal(err)
}
s := newIgnoreTestServer(dir, true, nil, boolPtr(false))
uri := "file://" + filepath.Join(dir, "debug.log")
ignored, _ := s.isFileIgnored(uri)
if !ignored {
t.Fatal("expected file to be ignored")
}
if s.ignoreLSPNotifyEnabled() {
t.Fatal("expected LSP notify disabled")
}
}
func TestHandleCompletion_NonIgnoredFile(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("*.log\n"), 0o644); err != nil {
t.Fatal(err)
}
s := newIgnoreTestServer(dir, true, nil, boolPtr(true))
uri := "file://" + filepath.Join(dir, "main.go")
ignored, _ := s.isFileIgnored(uri)
if ignored {
t.Fatal("main.go should not be ignored")
}
}
func TestHandleCodeAction_IgnoredFile(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("*.log\n"), 0o644); err != nil {
t.Fatal(err)
}
s := newIgnoreTestServer(dir, true, nil, nil)
uri := "file://" + filepath.Join(dir, "app.log")
ignored, reason := s.isFileIgnored(uri)
if !ignored {
t.Fatal("expected app.log to be ignored")
}
if reason != "matched .gitignore pattern" {
t.Errorf("unexpected reason: %s", reason)
}
}
func TestHandleDidOpen_IgnoredFile(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, ".gitignore"), []byte("*.log\n"), 0o644); err != nil {
t.Fatal(err)
}
s := newIgnoreTestServer(dir, true, nil, nil)
uri := "file://" + filepath.Join(dir, "app.log")
// Simulate didOpen — document should be stored even if ignored
s.setDocument(uri, "log content")
d := s.getDocument(uri)
if d == nil {
t.Fatal("document should be stored even for ignored files")
}
ignored, _ := s.isFileIgnored(uri)
if !ignored {
t.Fatal("expected app.log to be ignored")
}
}
func TestIsFileIgnored_NoChecker(t *testing.T) {
s := &Server{
logger: log.New(io.Discard, "", 0),
docs: make(map[string]*document),
altClients: make(map[string]llm.Client),
// ignoreChecker is nil
}
ignored, reason := s.isFileIgnored("file:///some/file.log")
if ignored {
t.Fatal("nil checker should not ignore anything")
}
if reason != "" {
t.Errorf("expected empty reason, got %q", reason)
}
}
func TestUriToPath(t *testing.T) {
tests := []struct {
uri string
want string
}{
{"file:///home/user/file.go", "/home/user/file.go"},
{"file:///tmp/test.log", "/tmp/test.log"},
{"", ""},
{"https://example.com", ""},
{"file:///path/with%20space/file.go", "/path/with space/file.go"},
}
for _, tc := range tests {
got := uriToPath(tc.uri)
if got != tc.want {
t.Errorf("uriToPath(%q) = %q, want %q", tc.uri, got, tc.want)
}
}
}
func TestIgnoreLSPNotifyEnabled_NilConfig(t *testing.T) {
// When IgnoreLSPNotify is nil, defaults to true
s := &Server{
logger: log.New(io.Discard, "", 0),
docs: make(map[string]*document),
altClients: make(map[string]llm.Client),
cfg: appconfig.App{},
}
if !s.ignoreLSPNotifyEnabled() {
t.Error("expected notify enabled when config is nil (default)")
}
}
|