// tstest:deno:allowAll import { tap, expect } from '@git.zone/tstest/tapbundle'; import { RustWatcher } from '../ts/watchers/watcher.rust.js'; import type { IWatchEvent } from '../ts/watchers/interfaces.js'; import * as path from 'node:path'; import * as fs from 'node:fs'; // This test validates the Rust watcher running under the Deno runtime. const isDeno = typeof (globalThis as any).Deno !== 'undefined'; const TEST_DIR = path.resolve('./test/assets'); const delay = (ms: number) => new Promise((r) => setTimeout(r, ms)); function waitForEvent( watcher: { events$: { subscribe: Function } }, filter: (e: IWatchEvent) => boolean, timeoutMs = 5000 ): Promise { 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 available = false; tap.test('RustWatcher (Deno): check availability', async () => { if (!isDeno) { console.log('Skipping: not Deno runtime'); return; } available = await RustWatcher.isAvailable(); console.log(`[test] Rust binary available: ${available}`); if (!available) { console.log('[test] Skipping Rust watcher tests — binary not found'); } }); let watcher: RustWatcher; let started = false; tap.test('RustWatcher (Deno): should create and start', async () => { if (!isDeno || !available) return; watcher = new RustWatcher({ basePaths: [TEST_DIR], depth: 4, followSymlinks: false, debounceMs: 100, }); expect(watcher.isWatching).toBeFalse(); try { await watcher.start(); started = true; expect(watcher.isWatching).toBeTrue(); await delay(300); } catch (err) { // Deno may block child_process.spawn without --allow-run permission console.log(`[test] RustWatcher spawn failed (likely Deno permission): ${err}`); available = false; } }); tap.test('RustWatcher (Deno): should detect file creation', async () => { if (!isDeno || !available) return; const file = path.join(TEST_DIR, 'rust-deno-add.txt'); const eventPromise = waitForEvent(watcher, (e) => e.type === 'add' && e.path.includes('rust-deno-add.txt')); await fs.promises.writeFile(file, 'rust deno add test'); const event = await eventPromise; expect(event.type).toEqual('add'); expect(event.path).toInclude('rust-deno-add.txt'); await fs.promises.unlink(file); await delay(200); }); tap.test('RustWatcher (Deno): should detect file modification', async () => { if (!isDeno || !available) return; const file = path.join(TEST_DIR, 'rust-deno-change.txt'); await fs.promises.writeFile(file, 'initial'); await delay(300); const eventPromise = waitForEvent(watcher, (e) => e.type === 'change' && e.path.includes('rust-deno-change.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('RustWatcher (Deno): should detect file deletion', async () => { if (!isDeno || !available) return; const file = path.join(TEST_DIR, 'rust-deno-unlink.txt'); await fs.promises.writeFile(file, 'to delete'); await delay(300); const eventPromise = waitForEvent(watcher, (e) => e.type === 'unlink' && e.path.includes('rust-deno-unlink.txt')); await fs.promises.unlink(file); const event = await eventPromise; expect(event.type).toEqual('unlink'); }); tap.test('RustWatcher (Deno): should not be watching after stop', async () => { if (!isDeno || !available) return; await watcher.stop(); expect(watcher.isWatching).toBeFalse(); }); tap.test('RustWatcher (Deno): cleanup', async () => { if (!isDeno) return; for (const name of ['rust-deno-add.txt', 'rust-deno-change.txt', 'rust-deno-unlink.txt']) { try { await fs.promises.unlink(path.join(TEST_DIR, name)); } catch {} } }); export default tap.start();