File size: 5,795 Bytes
7a1ad33 | 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 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'node:fs/promises';
import path from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { cleanupTmpDir, createTmpDir } from '@google/gemini-cli-test-utils';
import { FileWatcher, type FileWatcherEvent } from './fileWatcher.js';
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const waitForEvent = async (
events: FileWatcherEvent[],
predicate: (event: FileWatcherEvent) => boolean,
timeoutMs = 4000,
) => {
const startedAt = Date.now();
while (Date.now() - startedAt < timeoutMs) {
if (events.some(predicate)) {
return;
}
await sleep(50);
}
throw new Error('Timed out waiting for watcher event');
};
describe('FileWatcher', () => {
const tmpDirs: string[] = [];
afterEach(async () => {
await Promise.all(tmpDirs.map((dir) => cleanupTmpDir(dir)));
tmpDirs.length = 0;
vi.restoreAllMocks();
});
it('should emit relative add and unlink events for files', async () => {
const tmpDir = await createTmpDir({});
tmpDirs.push(tmpDir);
const events: FileWatcherEvent[] = [];
const watcher = new FileWatcher(tmpDir, (event) => {
events.push(event);
});
watcher.start();
await sleep(500);
const fileName = 'new-file.txt';
const filePath = path.join(tmpDir, fileName);
await fs.writeFile(filePath, 'hello');
await sleep(1200);
await fs.rm(filePath, { force: true });
await sleep(1200);
await watcher.close();
expect(events).toContainEqual({ eventType: 'add', relativePath: fileName });
expect(events).toContainEqual({
eventType: 'unlink',
relativePath: fileName,
});
});
it('should skip ignored paths', async () => {
const tmpDir = await createTmpDir({});
tmpDirs.push(tmpDir);
const events: FileWatcherEvent[] = [];
const watcher = new FileWatcher(
tmpDir,
(event) => {
events.push(event);
},
{
shouldIgnore: (relativePath) => relativePath.startsWith('ignored'),
},
);
watcher.start();
await sleep(500);
await fs.writeFile(path.join(tmpDir, 'ignored.txt'), 'x');
await fs.writeFile(path.join(tmpDir, 'kept.txt'), 'x');
await sleep(1200);
await watcher.close();
expect(events.some((event) => event.relativePath === 'ignored.txt')).toBe(
false,
);
expect(events).toContainEqual({
eventType: 'add',
relativePath: 'kept.txt',
});
});
it('should emit addDir and unlinkDir events for directories', async () => {
const tmpDir = await createTmpDir({});
tmpDirs.push(tmpDir);
const events: FileWatcherEvent[] = [];
const watcher = new FileWatcher(tmpDir, (event) => {
events.push(event);
});
watcher.start();
await sleep(500);
const dirName = 'new-folder';
const dirPath = path.join(tmpDir, dirName);
await fs.mkdir(dirPath);
await waitForEvent(
events,
(event) => event.eventType === 'addDir' && event.relativePath === dirName,
);
await fs.rm(dirPath, { recursive: true, force: true });
await waitForEvent(
events,
(event) =>
event.eventType === 'unlinkDir' && event.relativePath === dirName,
);
await watcher.close();
});
it('should normalize nested paths without leading dot prefix', async () => {
const tmpDir = await createTmpDir({});
tmpDirs.push(tmpDir);
const events: FileWatcherEvent[] = [];
const watcher = new FileWatcher(tmpDir, (event) => {
events.push(event);
});
watcher.start();
await sleep(500);
await fs.mkdir(path.join(tmpDir, 'nested'), { recursive: true });
await fs.writeFile(path.join(tmpDir, 'nested', 'file.txt'), 'data');
await waitForEvent(
events,
(event) =>
event.eventType === 'add' && event.relativePath === 'nested/file.txt',
);
const nestedFileEvent = events.find(
(event) =>
event.eventType === 'add' && event.relativePath.endsWith('/file.txt'),
);
expect(nestedFileEvent).toBeDefined();
expect(nestedFileEvent!.relativePath.startsWith('./')).toBe(false);
expect(nestedFileEvent!.relativePath.includes('\\')).toBe(false);
await watcher.close();
});
it('should not emit new events after stop is called', async () => {
const tmpDir = await createTmpDir({});
tmpDirs.push(tmpDir);
const events: FileWatcherEvent[] = [];
const watcher = new FileWatcher(tmpDir, (event) => {
events.push(event);
});
watcher.start();
await sleep(500);
const beforeStopFile = path.join(tmpDir, 'before-stop.txt');
await fs.writeFile(beforeStopFile, 'x');
await waitForEvent(
events,
(event) =>
event.eventType === 'add' && event.relativePath === 'before-stop.txt',
);
await watcher.close();
const afterStopCount = events.length;
await fs.writeFile(path.join(tmpDir, 'after-stop.txt'), 'x');
await sleep(600);
expect(events.length).toBe(afterStopCount);
});
it('should be safe to start and stop multiple times', async () => {
const tmpDir = await createTmpDir({});
tmpDirs.push(tmpDir);
const events: FileWatcherEvent[] = [];
const watcher = new FileWatcher(tmpDir, (event) => {
events.push(event);
});
watcher.start();
watcher.start();
await sleep(500);
await fs.writeFile(path.join(tmpDir, 'idempotent.txt'), 'x');
await waitForEvent(
events,
(event) =>
event.eventType === 'add' && event.relativePath === 'idempotent.txt',
);
await watcher.close();
await watcher.close();
expect(events.length).toBeGreaterThan(0);
});
});
|