Files
smartwatch/test/test.platform.node.ts

115 lines
3.9 KiB
TypeScript
Raw Normal View History

import { tap, expect } from '@git.zone/tstest/tapbundle';
import { NodeWatcher } from '../ts/watchers/watcher.node.js';
import type { IWatchEvent } from '../ts/watchers/interfaces.js';
import * as path from 'path';
import * as fs from 'fs';
const TEST_DIR = path.resolve('./test/assets');
const delay = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
function waitForEvent(
watcher: { events$: { subscribe: Function } },
filter: (e: IWatchEvent) => boolean,
timeoutMs = 5000
): Promise<IWatchEvent> {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
sub.unsubscribe();
reject(new Error(`Timeout waiting for event after ${timeoutMs}ms`));
}, timeoutMs);
const sub = watcher.events$.subscribe((event: IWatchEvent) => {
if (filter(event)) {
clearTimeout(timeout);
sub.unsubscribe();
resolve(event);
}
});
});
}
let watcher: NodeWatcher;
tap.test('NodeWatcher: should create and start', async () => {
watcher = new NodeWatcher({
basePaths: [TEST_DIR],
depth: 4,
followSymlinks: false,
debounceMs: 100,
});
expect(watcher.isWatching).toBeFalse();
await watcher.start();
expect(watcher.isWatching).toBeTrue();
await delay(500);
});
tap.test('NodeWatcher: should emit ready event', async () => {
// Ready event fires during start, so we test isWatching as proxy
expect(watcher.isWatching).toBeTrue();
});
tap.test('NodeWatcher: should detect file creation', async () => {
const file = path.join(TEST_DIR, 'node-add-test.txt');
const eventPromise = waitForEvent(watcher, (e) => e.type === 'add' && e.path.includes('node-add-test.txt'));
await fs.promises.writeFile(file, 'node watcher test');
const event = await eventPromise;
expect(event.type).toEqual('add');
expect(event.path).toInclude('node-add-test.txt');
await fs.promises.unlink(file);
await delay(200);
});
tap.test('NodeWatcher: should detect file modification', async () => {
const file = path.join(TEST_DIR, 'node-change-test.txt');
await fs.promises.writeFile(file, 'initial');
await delay(300);
const eventPromise = waitForEvent(watcher, (e) => e.type === 'change' && e.path.includes('node-change-test.txt'));
await fs.promises.writeFile(file, 'modified');
const event = await eventPromise;
expect(event.type).toEqual('change');
await fs.promises.unlink(file);
await delay(200);
});
tap.test('NodeWatcher: should detect file deletion', async () => {
const file = path.join(TEST_DIR, 'node-unlink-test.txt');
await fs.promises.writeFile(file, 'to delete');
await delay(300);
const eventPromise = waitForEvent(watcher, (e) => e.type === 'unlink' && e.path.includes('node-unlink-test.txt'));
await fs.promises.unlink(file);
const event = await eventPromise;
expect(event.type).toEqual('unlink');
});
tap.test('NodeWatcher: should detect directory creation and removal', async () => {
const dir = path.join(TEST_DIR, 'node-test-subdir');
const addDirPromise = waitForEvent(watcher, (e) => e.type === 'addDir' && e.path.includes('node-test-subdir'));
await fs.promises.mkdir(dir, { recursive: true });
const addEvent = await addDirPromise;
expect(addEvent.type).toEqual('addDir');
await delay(200);
const unlinkDirPromise = waitForEvent(watcher, (e) => e.type === 'unlinkDir' && e.path.includes('node-test-subdir'));
await fs.promises.rmdir(dir);
const unlinkEvent = await unlinkDirPromise;
expect(unlinkEvent.type).toEqual('unlinkDir');
});
tap.test('NodeWatcher: should not be watching after stop', async () => {
await watcher.stop();
expect(watcher.isWatching).toBeFalse();
});
tap.test('NodeWatcher: cleanup', async () => {
for (const name of ['node-add-test.txt', 'node-change-test.txt', 'node-unlink-test.txt']) {
try { await fs.promises.unlink(path.join(TEST_DIR, name)); } catch {}
}
try { await fs.promises.rmdir(path.join(TEST_DIR, 'node-test-subdir')); } catch {}
});
export default tap.start();