Files
smartwatch/test/test.inode.ts

144 lines
4.6 KiB
TypeScript
Raw Permalink Normal View History

import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as smartwatch from '../ts/index.js';
import * as smartfile from '@push.rocks/smartfile';
import * as smartrx from '@push.rocks/smartrx';
import * as fs from 'fs';
import * as path from 'path';
// Skip in CI
if (process.env.CI) {
process.exit(0);
}
const TEST_DIR = './test/assets';
// Helper to delay
const delay = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
// Helper to wait for an event with timeout (filters by filename)
async function waitForFileEvent<T extends [string, ...any[]]>(
observable: smartrx.rxjs.Observable<T>,
expectedFile: string,
timeoutMs: number = 5000
): Promise<T> {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
subscription.unsubscribe();
reject(new Error(`Timeout waiting for event on ${expectedFile} after ${timeoutMs}ms`));
}, timeoutMs);
const subscription = observable.subscribe((value) => {
const [filePath] = value;
if (filePath.includes(expectedFile)) {
clearTimeout(timeout);
subscription.unsubscribe();
resolve(value);
}
});
});
}
let testSmartwatch: smartwatch.Smartwatch;
// ===========================================
// INODE CHANGE DETECTION TESTS
// ===========================================
tap.test('setup: start watcher', async () => {
testSmartwatch = new smartwatch.Smartwatch([`${TEST_DIR}/**/*.txt`]);
await testSmartwatch.start();
expect(testSmartwatch.status).toEqual('watching');
// Wait for chokidar to be ready
await delay(500);
});
tap.test('should detect delete+recreate as change event (atomic handling)', async () => {
// Chokidar with atomic: true handles delete+recreate as a single change event
// This is the expected behavior for editor save patterns
const testFile = path.join(TEST_DIR, 'inode-test.txt');
// Clean up any leftover file from previous runs
try { await fs.promises.unlink(testFile); } catch {}
await delay(100);
// Create initial file
await smartfile.memory.toFs('initial content', testFile);
await delay(300);
// Get the initial inode
const initialStats = await fs.promises.stat(testFile);
const initialInode = initialStats.ino;
console.log(`[test] Initial inode: ${initialInode}`);
// Chokidar's atomic handling will emit a single 'change' event
const changeObservable = await testSmartwatch.getObservableFor('change');
const eventPromise = waitForFileEvent(changeObservable, 'inode-test.txt', 3000);
// Delete and recreate (this creates a new inode)
await fs.promises.unlink(testFile);
await smartfile.memory.toFs('recreated content', testFile);
// Check inode changed
const newStats = await fs.promises.stat(testFile);
const newInode = newStats.ino;
console.log(`[test] New inode: ${newInode}`);
expect(newInode).not.toEqual(initialInode);
// Chokidar detects this as a change (atomic write pattern)
const [filePath] = await eventPromise;
expect(filePath).toInclude('inode-test.txt');
console.log(`[test] Detected change event for delete+recreate (atomic handling)`);
// Cleanup
await fs.promises.unlink(testFile);
await delay(200);
});
tap.test('should detect atomic write pattern (temp file + rename)', async () => {
// This simulates what Claude Code and many editors do:
// 1. Write to temp file (file.txt.tmp.12345)
// 2. Rename temp file to target file
const testFile = path.join(TEST_DIR, 'atomic-test.txt');
const tempFile = path.join(TEST_DIR, 'atomic-test.txt.tmp.12345');
// Create initial file
await smartfile.memory.toFs('initial content', testFile);
await delay(300);
const changeObservable = await testSmartwatch.getObservableFor('change');
const eventPromise = waitForFileEvent(changeObservable, 'atomic-test.txt', 3000);
// Atomic write: create temp file then rename
await smartfile.memory.toFs('atomic content', tempFile);
await fs.promises.rename(tempFile, testFile);
// Should detect the change to the target file
const [filePath] = await eventPromise;
expect(filePath).toInclude('atomic-test.txt');
expect(filePath).not.toInclude('.tmp.');
// Cleanup
await fs.promises.unlink(testFile);
});
tap.test('teardown: stop watcher', async () => {
await testSmartwatch.stop();
expect(testSmartwatch.status).toEqual('idle');
});
tap.test('cleanup: remove test files', async () => {
const files = await fs.promises.readdir(TEST_DIR);
for (const file of files) {
if (file.startsWith('inode-') || file.startsWith('atomic-')) {
try {
await fs.promises.unlink(path.join(TEST_DIR, file));
} catch {
// Ignore
}
}
}
});
export default tap.start();