-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_test.go
More file actions
332 lines (276 loc) · 9.15 KB
/
Copy pathsync_test.go
File metadata and controls
332 lines (276 loc) · 9.15 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"io"
"log/slog"
"os"
"path/filepath"
"testing"
"time"
"github.com/lexandro/codeindex-mcp/ignore"
"github.com/lexandro/codeindex-mcp/index"
)
func testLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(io.Discard, nil))
}
func testIgnoreMatcher(rootDir string) *ignore.Matcher {
return ignore.NewMatcher(ignore.MatcherOptions{
RootDir: rootDir,
MaxFileSizeBytes: 1024 * 1024,
})
}
func Test_performSyncVerification_DetectsMissingFiles(t *testing.T) {
tmpDir := t.TempDir()
logger := testLogger()
matcher := testIgnoreMatcher(tmpDir)
fileIndex := index.NewFileIndex()
contentIndex, err := index.NewContentIndex()
if err != nil {
t.Fatal(err)
}
defer contentIndex.Close()
// Create a file on disk but don't index it
filePath := filepath.Join(tmpDir, "missing.go")
os.WriteFile(filePath, []byte("package main\n"), 0644)
result := performSyncVerification(tmpDir, fileIndex, contentIndex, matcher, nil, logger)
if result.MissingFiles != 1 {
t.Errorf("expected 1 missing file, got %d", result.MissingFiles)
}
if result.StaleFiles != 0 {
t.Errorf("expected 0 stale files, got %d", result.StaleFiles)
}
if result.ModifiedFiles != 0 {
t.Errorf("expected 0 modified files, got %d", result.ModifiedFiles)
}
// Verify the file was actually indexed
if fileIndex.GetFile("missing.go") == nil {
t.Error("expected missing.go to be indexed after sync")
}
}
func Test_performSyncVerification_DetectsStaleFiles(t *testing.T) {
tmpDir := t.TempDir()
logger := testLogger()
matcher := testIgnoreMatcher(tmpDir)
fileIndex := index.NewFileIndex()
contentIndex, err := index.NewContentIndex()
if err != nil {
t.Fatal(err)
}
defer contentIndex.Close()
// Add a file to the index that doesn't exist on disk
fileIndex.AddFile(&index.IndexedFile{
Path: filepath.Join(tmpDir, "deleted.go"),
RelativePath: "deleted.go",
Language: "Go",
SizeBytes: 100,
ModTime: time.Now(),
LineCount: 5,
})
contentIndex.IndexFile("deleted.go", "package main\n", "Go")
result := performSyncVerification(tmpDir, fileIndex, contentIndex, matcher, nil, logger)
if result.StaleFiles != 1 {
t.Errorf("expected 1 stale file, got %d", result.StaleFiles)
}
if result.MissingFiles != 0 {
t.Errorf("expected 0 missing files, got %d", result.MissingFiles)
}
// Verify the file was removed from index
if fileIndex.GetFile("deleted.go") != nil {
t.Error("expected deleted.go to be removed from index after sync")
}
}
func Test_performSyncVerification_DetectsModifiedFiles(t *testing.T) {
tmpDir := t.TempDir()
logger := testLogger()
matcher := testIgnoreMatcher(tmpDir)
fileIndex := index.NewFileIndex()
contentIndex, err := index.NewContentIndex()
if err != nil {
t.Fatal(err)
}
defer contentIndex.Close()
// Create and index a file
filePath := filepath.Join(tmpDir, "modified.go")
os.WriteFile(filePath, []byte("package main\n"), 0644)
info, _ := os.Stat(filePath)
fileIndex.AddFile(&index.IndexedFile{
Path: filePath,
RelativePath: "modified.go",
Language: "Go",
SizeBytes: info.Size(),
ModTime: info.ModTime().Add(-1 * time.Hour), // old ModTime
LineCount: 1,
})
contentIndex.IndexFile("modified.go", "package main\n", "Go")
result := performSyncVerification(tmpDir, fileIndex, contentIndex, matcher, nil, logger)
if result.ModifiedFiles != 1 {
t.Errorf("expected 1 modified file, got %d", result.ModifiedFiles)
}
if result.MissingFiles != 0 {
t.Errorf("expected 0 missing files, got %d", result.MissingFiles)
}
if result.StaleFiles != 0 {
t.Errorf("expected 0 stale files, got %d", result.StaleFiles)
}
}
func Test_performSyncVerification_InSyncReturnsZeros(t *testing.T) {
tmpDir := t.TempDir()
logger := testLogger()
matcher := testIgnoreMatcher(tmpDir)
fileIndex := index.NewFileIndex()
contentIndex, err := index.NewContentIndex()
if err != nil {
t.Fatal(err)
}
defer contentIndex.Close()
// Create and properly index a file
filePath := filepath.Join(tmpDir, "synced.go")
os.WriteFile(filePath, []byte("package main\n"), 0644)
info, _ := os.Stat(filePath)
fileIndex.AddFile(&index.IndexedFile{
Path: filePath,
RelativePath: "synced.go",
Language: "Go",
SizeBytes: info.Size(),
ModTime: info.ModTime(),
LineCount: 1,
})
contentIndex.IndexFile("synced.go", "package main\n", "Go")
result := performSyncVerification(tmpDir, fileIndex, contentIndex, matcher, nil, logger)
if result.MissingFiles != 0 {
t.Errorf("expected 0 missing files, got %d", result.MissingFiles)
}
if result.StaleFiles != 0 {
t.Errorf("expected 0 stale files, got %d", result.StaleFiles)
}
if result.ModifiedFiles != 0 {
t.Errorf("expected 0 modified files, got %d", result.ModifiedFiles)
}
}
func Test_performSyncVerification_SkipsBinaryFiles(t *testing.T) {
tmpDir := t.TempDir()
logger := testLogger()
matcher := testIgnoreMatcher(tmpDir)
fileIndex := index.NewFileIndex()
contentIndex, err := index.NewContentIndex()
if err != nil {
t.Fatal(err)
}
defer contentIndex.Close()
// Create a binary file (contains null bytes)
binaryPath := filepath.Join(tmpDir, "image.dat")
binaryData := []byte{0x89, 0x50, 0x4E, 0x47, 0x00, 0x0A, 0x1A, 0x0A}
os.WriteFile(binaryPath, binaryData, 0644)
result := performSyncVerification(tmpDir, fileIndex, contentIndex, matcher, nil, logger)
// Binary file should not count as missing (it's skipped by indexSingleFile)
if result.MissingFiles != 0 {
t.Errorf("expected 0 missing files (binary skipped), got %d", result.MissingFiles)
}
if fileIndex.GetFile("image.dat") != nil {
t.Error("expected binary file to NOT be indexed")
}
}
func Test_performSyncVerification_SkipsIgnoredDirectories(t *testing.T) {
tmpDir := t.TempDir()
logger := testLogger()
matcher := testIgnoreMatcher(tmpDir)
fileIndex := index.NewFileIndex()
contentIndex, err := index.NewContentIndex()
if err != nil {
t.Fatal(err)
}
defer contentIndex.Close()
// Create node_modules directory with a file (default ignored)
nodeModulesDir := filepath.Join(tmpDir, "node_modules")
os.Mkdir(nodeModulesDir, 0755)
os.WriteFile(filepath.Join(nodeModulesDir, "index.js"), []byte("module.exports = {};\n"), 0644)
// Create a normal file
os.WriteFile(filepath.Join(tmpDir, "main.go"), []byte("package main\n"), 0644)
result := performSyncVerification(tmpDir, fileIndex, contentIndex, matcher, nil, logger)
if result.MissingFiles != 1 {
t.Errorf("expected 1 missing file (main.go only), got %d", result.MissingFiles)
}
if fileIndex.GetFile("node_modules/index.js") != nil {
t.Error("expected files in node_modules to be ignored")
}
if fileIndex.GetFile("main.go") == nil {
t.Error("expected main.go to be indexed")
}
}
func Test_performSyncVerification_SkipsTooLargeFiles(t *testing.T) {
tmpDir := t.TempDir()
logger := testLogger()
// Matcher with 100 byte size limit
matcher := ignore.NewMatcher(ignore.MatcherOptions{
RootDir: tmpDir,
MaxFileSizeBytes: 100,
})
fileIndex := index.NewFileIndex()
contentIndex, err := index.NewContentIndex()
if err != nil {
t.Fatal(err)
}
defer contentIndex.Close()
// Create a small file (under limit)
os.WriteFile(filepath.Join(tmpDir, "small.go"), []byte("package main\n"), 0644)
// Create a large file (over limit)
largeContent := make([]byte, 200)
for i := range largeContent {
largeContent[i] = 'x'
}
os.WriteFile(filepath.Join(tmpDir, "large.go"), largeContent, 0644)
result := performSyncVerification(tmpDir, fileIndex, contentIndex, matcher, nil, logger)
if result.MissingFiles != 1 {
t.Errorf("expected 1 missing file (small.go only), got %d", result.MissingFiles)
}
if fileIndex.GetFile("large.go") != nil {
t.Error("expected large.go to be skipped (too large)")
}
}
func Test_performSyncVerification_EmptyDirectory(t *testing.T) {
tmpDir := t.TempDir()
logger := testLogger()
matcher := testIgnoreMatcher(tmpDir)
fileIndex := index.NewFileIndex()
contentIndex, err := index.NewContentIndex()
if err != nil {
t.Fatal(err)
}
defer contentIndex.Close()
result := performSyncVerification(tmpDir, fileIndex, contentIndex, matcher, nil, logger)
if result.MissingFiles != 0 {
t.Errorf("expected 0 missing files, got %d", result.MissingFiles)
}
if result.StaleFiles != 0 {
t.Errorf("expected 0 stale files, got %d", result.StaleFiles)
}
if result.ModifiedFiles != 0 {
t.Errorf("expected 0 modified files, got %d", result.ModifiedFiles)
}
// Duration is always set (may be 0 on fast machines with empty dirs, that's fine)
}
func Test_runPeriodicSync_StopsOnChannelClose(t *testing.T) {
tmpDir := t.TempDir()
logger := testLogger()
matcher := testIgnoreMatcher(tmpDir)
fileIndex := index.NewFileIndex()
contentIndex, err := index.NewContentIndex()
if err != nil {
t.Fatal(err)
}
defer contentIndex.Close()
stop := make(chan struct{})
done := make(chan struct{})
go func() {
runPeriodicSync(1, tmpDir, fileIndex, contentIndex, matcher, nil, logger, stop)
close(done)
}()
// Close stop channel to signal shutdown
close(stop)
// Wait for goroutine to finish with timeout
select {
case <-done:
// OK - goroutine stopped cleanly
case <-time.After(3 * time.Second):
t.Fatal("runPeriodicSync did not stop within 3 seconds after closing stop channel")
}
}