-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathwatcher.test.ts
More file actions
302 lines (232 loc) · 8.87 KB
/
watcher.test.ts
File metadata and controls
302 lines (232 loc) · 8.87 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
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import { FileWatcher, GitHeadWatcher, FileChange, createWatcherWithIndexer } from "../src/watcher/index.js";
import { ParsedCodebaseIndexConfig } from "../src/config/schema.js";
const createTestConfig = (overrides: Partial<ParsedCodebaseIndexConfig> = {}): ParsedCodebaseIndexConfig => ({
embeddingProvider: "auto",
embeddingModel: undefined,
scope: "project",
include: ["**/*.ts", "**/*.js"],
exclude: [],
indexing: {
autoIndex: false,
watchFiles: true,
maxFileSize: 1048576,
maxChunksPerFile: 100,
semanticOnly: false,
retries: 3,
retryDelayMs: 1000,
autoGc: true,
gcIntervalDays: 7,
gcOrphanThreshold: 100,
requireProjectMarker: true,
},
search: {
maxResults: 20,
minScore: 0.1,
includeContext: true,
hybridWeight: 0.5,
contextLines: 0,
},
debug: {
enabled: false,
logLevel: "info",
logSearch: true,
logEmbedding: true,
logCache: true,
logGc: true,
logBranch: true,
metrics: true,
},
...overrides,
});
describe("FileWatcher", () => {
let tempDir: string;
let watcher: FileWatcher;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "watcher-test-"));
fs.mkdirSync(path.join(tempDir, "src"), { recursive: true });
});
afterEach(() => {
watcher?.stop();
fs.rmSync(tempDir, { recursive: true, force: true });
});
describe("constructor and lifecycle", () => {
it("should create watcher without starting", () => {
watcher = new FileWatcher(tempDir, createTestConfig());
expect(watcher.isRunning()).toBe(false);
});
it("should start and stop correctly", () => {
watcher = new FileWatcher(tempDir, createTestConfig());
const handler = vi.fn();
watcher.start(handler);
expect(watcher.isRunning()).toBe(true);
watcher.stop();
expect(watcher.isRunning()).toBe(false);
});
it("should not start twice", () => {
watcher = new FileWatcher(tempDir, createTestConfig());
const handler1 = vi.fn();
const handler2 = vi.fn();
watcher.start(handler1);
watcher.start(handler2);
expect(watcher.isRunning()).toBe(true);
});
it("should clear pending changes on stop", () => {
watcher = new FileWatcher(tempDir, createTestConfig());
const handler = vi.fn();
watcher.start(handler);
watcher.stop();
expect(watcher.isRunning()).toBe(false);
});
});
describe("file filtering", () => {
it("should only watch files matching include patterns", async () => {
const changes: FileChange[] = [];
watcher = new FileWatcher(tempDir, createTestConfig({ include: ["**/*.ts"] }));
watcher.start(async (c) => {
changes.push(...c);
});
await new Promise((r) => setTimeout(r, 100));
fs.writeFileSync(path.join(tempDir, "src", "test.ts"), "const x = 1;");
fs.writeFileSync(path.join(tempDir, "src", "test.md"), "# README");
await new Promise((r) => setTimeout(r, 1500));
const tsChanges = changes.filter((c) => c.path.endsWith(".ts"));
const mdChanges = changes.filter((c) => c.path.endsWith(".md"));
expect(tsChanges.length).toBeGreaterThanOrEqual(0);
expect(mdChanges.length).toBe(0);
});
it("should include matching root-level files", async () => {
const changes: FileChange[] = [];
watcher = new FileWatcher(tempDir, createTestConfig({ include: ["**/*.ts"] }));
watcher.start(async (c) => {
changes.push(...c);
});
await new Promise((r) => setTimeout(r, 100));
fs.writeFileSync(path.join(tempDir, "root.ts"), "export const root = 1;");
await new Promise((r) => setTimeout(r, 1500));
expect(changes.some((c) => c.path.endsWith("root.ts"))).toBe(true);
});
});
describe("createWatcherWithIndexer", () => {
it("uses the latest indexer instance for file-triggered reindexing", async () => {
const staleIndexer = {
index: vi.fn().mockResolvedValue(undefined),
};
const refreshedIndexer = {
index: vi.fn().mockResolvedValue(undefined),
};
let currentIndexer = staleIndexer;
const combinedWatcher = createWatcherWithIndexer(
() => currentIndexer,
tempDir,
createTestConfig()
);
await new Promise((r) => setTimeout(r, 100));
currentIndexer = refreshedIndexer;
fs.writeFileSync(path.join(tempDir, "src", "reindex-me.ts"), "export const value = 1;");
await new Promise((r) => setTimeout(r, 1500));
expect(refreshedIndexer.index).toHaveBeenCalledTimes(1);
expect(staleIndexer.index).not.toHaveBeenCalled();
combinedWatcher.stop();
});
it("stops the watcher cleanly after start", () => {
const indexer = {
index: vi.fn().mockResolvedValue(undefined),
};
const combinedWatcher = createWatcherWithIndexer(
() => indexer,
tempDir,
createTestConfig()
);
expect(combinedWatcher.fileWatcher.isRunning()).toBe(true);
expect(combinedWatcher.gitWatcher?.isRunning() ?? false).toBe(false);
combinedWatcher.stop();
expect(combinedWatcher.fileWatcher.isRunning()).toBe(false);
expect(combinedWatcher.gitWatcher?.isRunning() ?? false).toBe(false);
});
});
});
describe("GitHeadWatcher", () => {
let tempDir: string;
let watcher: GitHeadWatcher;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "git-watcher-test-"));
});
afterEach(() => {
watcher?.stop();
fs.rmSync(tempDir, { recursive: true, force: true });
});
describe("constructor and lifecycle", () => {
it("should create watcher without starting", () => {
watcher = new GitHeadWatcher(tempDir);
expect(watcher.isRunning()).toBe(false);
});
it("should not start for non-git directory", () => {
watcher = new GitHeadWatcher(tempDir);
const handler = vi.fn();
watcher.start(handler);
expect(watcher.isRunning()).toBe(false);
});
it("should start for git directory", () => {
fs.mkdirSync(path.join(tempDir, ".git", "refs", "heads"), { recursive: true });
fs.writeFileSync(path.join(tempDir, ".git", "HEAD"), "ref: refs/heads/main\n");
watcher = new GitHeadWatcher(tempDir);
const handler = vi.fn();
watcher.start(handler);
expect(watcher.isRunning()).toBe(true);
});
it("should stop correctly", () => {
fs.mkdirSync(path.join(tempDir, ".git", "refs", "heads"), { recursive: true });
fs.writeFileSync(path.join(tempDir, ".git", "HEAD"), "ref: refs/heads/main\n");
watcher = new GitHeadWatcher(tempDir);
watcher.start(vi.fn());
watcher.stop();
expect(watcher.isRunning()).toBe(false);
});
it("should not start twice", () => {
fs.mkdirSync(path.join(tempDir, ".git", "refs", "heads"), { recursive: true });
fs.writeFileSync(path.join(tempDir, ".git", "HEAD"), "ref: refs/heads/main\n");
watcher = new GitHeadWatcher(tempDir);
const handler1 = vi.fn();
const handler2 = vi.fn();
watcher.start(handler1);
watcher.start(handler2);
expect(watcher.isRunning()).toBe(true);
});
});
describe("branch tracking", () => {
it("should return current branch after start", () => {
fs.mkdirSync(path.join(tempDir, ".git", "refs", "heads"), { recursive: true });
fs.writeFileSync(path.join(tempDir, ".git", "HEAD"), "ref: refs/heads/main\n");
watcher = new GitHeadWatcher(tempDir);
watcher.start(vi.fn());
expect(watcher.getCurrentBranch()).toBe("main");
});
it("should return null before start", () => {
fs.mkdirSync(path.join(tempDir, ".git", "refs", "heads"), { recursive: true });
fs.writeFileSync(path.join(tempDir, ".git", "HEAD"), "ref: refs/heads/main\n");
watcher = new GitHeadWatcher(tempDir);
expect(watcher.getCurrentBranch()).toBe(null);
});
it("should detect branch change when HEAD is modified", async () => {
fs.mkdirSync(path.join(tempDir, ".git", "refs", "heads"), { recursive: true });
fs.writeFileSync(path.join(tempDir, ".git", "HEAD"), "ref: refs/heads/main\n");
const branchChanges: Array<{ old: string | null; new: string }> = [];
watcher = new GitHeadWatcher(tempDir);
watcher.start(async (oldBranch, newBranch) => {
branchChanges.push({ old: oldBranch, new: newBranch });
});
await new Promise((r) => setTimeout(r, 100));
fs.writeFileSync(path.join(tempDir, ".git", "HEAD"), "ref: refs/heads/feature\n");
await new Promise((r) => setTimeout(r, 500));
expect(branchChanges.length).toBeGreaterThanOrEqual(0);
if (branchChanges.length > 0) {
expect(branchChanges[0].old).toBe("main");
expect(branchChanges[0].new).toBe("feature");
}
});
});
});