Files
smartwatch/test/test.platform.rust.bun.ts

113 lines
3.7 KiB
TypeScript

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 'path';
import * as fs from 'fs';
// This test validates the Rust watcher running under the Bun runtime.
const isBun = typeof (globalThis as any).Bun !== '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 available = false;
tap.test('RustWatcher (Bun): check availability', async () => {
if (!isBun) { console.log('Skipping: not Bun 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;
tap.test('RustWatcher (Bun): should create and start', async () => {
if (!isBun || !available) return;
watcher = new RustWatcher({
basePaths: [TEST_DIR],
depth: 4,
followSymlinks: false,
debounceMs: 100,
});
expect(watcher.isWatching).toBeFalse();
await watcher.start();
expect(watcher.isWatching).toBeTrue();
await delay(300);
});
tap.test('RustWatcher (Bun): should detect file creation', async () => {
if (!isBun || !available) return;
const file = path.join(TEST_DIR, 'rust-bun-add.txt');
const eventPromise = waitForEvent(watcher, (e) => e.type === 'add' && e.path.includes('rust-bun-add.txt'));
await fs.promises.writeFile(file, 'rust bun add test');
const event = await eventPromise;
expect(event.type).toEqual('add');
expect(event.path).toInclude('rust-bun-add.txt');
await fs.promises.unlink(file);
await delay(200);
});
tap.test('RustWatcher (Bun): should detect file modification', async () => {
if (!isBun || !available) return;
const file = path.join(TEST_DIR, 'rust-bun-change.txt');
await fs.promises.writeFile(file, 'initial');
await delay(300);
const eventPromise = waitForEvent(watcher, (e) => e.type === 'change' && e.path.includes('rust-bun-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 (Bun): should detect file deletion', async () => {
if (!isBun || !available) return;
const file = path.join(TEST_DIR, 'rust-bun-unlink.txt');
await fs.promises.writeFile(file, 'to delete');
await delay(300);
const eventPromise = waitForEvent(watcher, (e) => e.type === 'unlink' && e.path.includes('rust-bun-unlink.txt'));
await fs.promises.unlink(file);
const event = await eventPromise;
expect(event.type).toEqual('unlink');
});
tap.test('RustWatcher (Bun): should not be watching after stop', async () => {
if (!isBun || !available) return;
await watcher.stop();
expect(watcher.isWatching).toBeFalse();
});
tap.test('RustWatcher (Bun): cleanup', async () => {
if (!isBun) return;
for (const name of ['rust-bun-add.txt', 'rust-bun-change.txt', 'rust-bun-unlink.txt']) {
try { await fs.promises.unlink(path.join(TEST_DIR, name)); } catch {}
}
});
export default tap.start();