Files
smartwatch/test/test.platform.deno.ts

120 lines
4.0 KiB
TypeScript

// tstest:deno:allowAll
import { tap, expect } from '@git.zone/tstest/tapbundle';
import type { IWatchEvent } from '../ts/watchers/interfaces.js';
import * as path from 'node:path';
import * as fs from 'node:fs';
// This test requires the Deno runtime
const isDeno = typeof (globalThis as any).Deno !== 'undefined';
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: any;
tap.test('DenoWatcher: should create and start', async () => {
if (!isDeno) { console.log('Skipping: not Deno runtime'); return; }
const { DenoWatcher } = await import('../ts/watchers/watcher.deno.js');
watcher = new DenoWatcher({
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('DenoWatcher: should detect file creation', async () => {
if (!isDeno) return;
const file = path.join(TEST_DIR, 'deno-add-test.txt');
// Deno.watchFs may report new files as 'create', 'any', or 'modify' depending on platform
const eventPromise = waitForEvent(
watcher,
(e) => (e.type === 'add' || e.type === 'change') && e.path.includes('deno-add-test.txt'),
10000,
);
await fs.promises.writeFile(file, 'deno watcher test');
const event = await eventPromise;
expect(event.path).toInclude('deno-add-test.txt');
await fs.promises.unlink(file);
await delay(200);
});
tap.test('DenoWatcher: should detect file modification', async () => {
if (!isDeno) return;
const file = path.join(TEST_DIR, 'deno-change-test.txt');
await fs.promises.writeFile(file, 'initial');
await delay(300);
const eventPromise = waitForEvent(watcher, (e) => e.type === 'change' && e.path.includes('deno-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('DenoWatcher: should detect file deletion', async () => {
if (!isDeno) return;
const file = path.join(TEST_DIR, 'deno-unlink-test.txt');
await fs.promises.writeFile(file, 'to delete');
await delay(300);
const eventPromise = waitForEvent(watcher, (e) => e.type === 'unlink' && e.path.includes('deno-unlink-test.txt'));
await fs.promises.unlink(file);
const event = await eventPromise;
expect(event.type).toEqual('unlink');
});
tap.test('DenoWatcher: should detect directory creation', async () => {
if (!isDeno) return;
const dir = path.join(TEST_DIR, 'deno-test-subdir');
const addDirPromise = waitForEvent(watcher, (e) => e.type === 'addDir' && e.path.includes('deno-test-subdir'));
await fs.promises.mkdir(dir, { recursive: true });
const event = await addDirPromise;
expect(event.type).toEqual('addDir');
await delay(200);
await fs.promises.rmdir(dir);
await delay(200);
});
tap.test('DenoWatcher: should not be watching after stop', async () => {
if (!isDeno) return;
await watcher.stop();
expect(watcher.isWatching).toBeFalse();
});
tap.test('DenoWatcher: cleanup', async () => {
if (!isDeno) return;
for (const name of ['deno-add-test.txt', 'deno-change-test.txt', 'deno-unlink-test.txt']) {
try { await fs.promises.unlink(path.join(TEST_DIR, name)); } catch {}
}
try { await fs.promises.rmdir(path.join(TEST_DIR, 'deno-test-subdir')); } catch {}
});
export default tap.start();