Files
ruvnet__ruflo/tests/hook-handler-runwithtimeout.test.cjs
tjaiyen cb1e93e8db fix(hooks): harden memory/hook helpers (timeout, signals, truncation, cross-platform slug)
FIX 1 — runWithTimeout was inert: it called fn() then clearTimeout()
immediately, so an async callee's hang was never caught (the timer was
cleared before the pending promise settled). Reimplement as a real
Promise.race between the work and the timeout. Document that a synchronous
blocking callee cannot be preempted in-process (the real guard is the
file-size cap in intelligence.cjs). Applied to both hook-handler.cjs copies.

FIX 2 — auto-memory-hook.mjs swallowed ALL unhandled rejections process-wide
via `() => {}`. Keep hooks exit-0 but log the reason under RUFLO_DEBUG/DEBUG
so genuine async bugs are visible.

FIX 3 — no SIGTERM/SIGINT cleanup in the db-touching helpers. Track the active
backend and flush it (JSON persist / SQLite close + WAL flush) on signal,
avoiding half-written stores and stale agentdb.rvf.lock.

FIX 4 — silent value truncation (intelligence.cjs 500/100 chars). Add clip():
appends an ellipsis and warns under debug when it actually cuts.

FIX 5 — projectSlug only handled POSIX '/', so on Windows it never matched
Claude Code's real ~/.claude/projects/<slug> dir and memory bootstrap silently
found nothing. Slugify every non-alphanumeric to '-' to match Claude's
convention (verified: G:\My Drive\...\ruflo-fix -> G--My-Drive-...-ruflo-fix).

Also: export runWithTimeout behind a require.main guard so it is unit-testable,
and add tests/hook-handler-runwithtimeout.test.cjs (node:test, 5 cases incl.
the decisive slow-async timeout case).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-16 08:40:24 -07:00

57 lines
2.1 KiB
JavaScript

'use strict';
/**
* Unit tests for hook-handler.cjs runWithTimeout (FIX 1).
*
* The previous implementation called fn() and clearTimeout(timer) immediately,
* so an async fn returned a *pending* promise that resolved through the race —
* the timeout never fired. The "times out a slow async fn" case below fails
* against the old code (it would return 'late') and passes against the fix.
*
* Uses node:test (built-in) so it runs without installing dependencies.
*/
const test = require('node:test');
const assert = require('node:assert/strict');
const path = require('path');
const { runWithTimeout, INTELLIGENCE_TIMEOUT_MS } = require(
path.join(__dirname, '..', '.claude', 'helpers', 'hook-handler.cjs')
);
test('returns the value for a fast async fn', async () => {
const r = await runWithTimeout(() => Promise.resolve(42), 'fast-async');
assert.equal(r, 42);
});
test('returns the value for a fast sync fn', async () => {
const r = await runWithTimeout(() => 7, 'fast-sync');
assert.equal(r, 7);
});
test('resolves null (never rejects) when fn throws synchronously', async () => {
const r = await runWithTimeout(() => { throw new Error('boom'); }, 'sync-throw');
assert.equal(r, null);
});
test('resolves null (never rejects) when an async fn rejects', async () => {
const r = await runWithTimeout(() => Promise.reject(new Error('boom')), 'async-reject');
assert.equal(r, null);
});
test('times out a slow async fn and resolves null near the timeout', { timeout: 8000 }, async () => {
const start = Date.now();
const r = await runWithTimeout(
() => new Promise((res) => {
// .unref() so the dangling timer never keeps the test process alive
const t = setTimeout(() => res('late'), INTELLIGENCE_TIMEOUT_MS + 2000);
if (t.unref) t.unref();
}),
'slow-async'
);
const elapsed = Date.now() - start;
assert.equal(r, null, "should time out to null, not return the late value");
assert.ok(
elapsed >= INTELLIGENCE_TIMEOUT_MS - 200 && elapsed < INTELLIGENCE_TIMEOUT_MS + 1500,
`should resolve near the ${INTELLIGENCE_TIMEOUT_MS}ms timeout, took ${elapsed}ms`
);
});