Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(backup): run the #6499 full-slot probe tests on a virtual clock

garrytana33776a123bf
diff --git a/test/backup-verification.serial.test.ts b/test/backup-verification.serial.test.ts
index ca4461a75d..6d6564e791 100644
--- a/test/backup-verification.serial.test.ts
+++ b/test/backup-verification.serial.test.ts
@@ -314,10 +314,30 @@ function slowRemote(answerMs: number, timeouts: number[]): ExecFileStub {
};
}
+/**
+ * `slowRemote` on a virtual clock: each probe advances `Date.now()` by exactly `answerMs` and answers on the next tick, so the
+ * sweep deadline sees only probe time. Real local git work between probes on a loaded runner can't eat the 4th probe's slot.
+ */
+function virtualSlowRemote(answerMs: number, timeouts: number[]): { restore: () => void } {
+ let clock = Date.now();
+ const time = spyOn(Date, 'now').mockImplementation(() => clock);
+ const probe = spyOn(childProcess, 'execFile').mockImplementation(((_file: string, args: string[], options: { timeout: number }, callback: (error: Error | null, stdout: string) => void) => {
+ timeouts.push(options.timeout);
+ const head = git(args[args.indexOf('-C') + 1], 'rev-parse', 'HEAD');
+ const ref = args[args.length - 1];
+ clock += Math.min(answerMs, options.timeout);
+ setImmediate(() => answerMs < options.timeout
+ ? callback(null, `${head}\t${ref}\n`)
+ : callback(Object.assign(new Error('killed'), { killed: true, signal: 'SIGTERM', code: null }), ''));
+ return {};
+ }) as unknown as typeof childProcess.execFile);
+ return { restore: () => { probe.mockRestore(); time.mockRestore(); } };
+}
+
test('a remote that would not get its full slot is deferred as budget_exhausted, never probed on a sub-slot (#6499)', async () => {
const roots = await Promise.all(Array.from({ length: 5 }, async (_, i) => (await repository(`slow-${i}`)).root));
const timeouts: number[] = [];
- const probe = spyOn(childProcess, 'execFile').mockImplementation(slowRemote(1_700, timeouts) as unknown as typeof childProcess.execFile);
+ const probe = virtualSlowRemote(1_700, timeouts);
try {
const result = await computeBackupCoverage(engine(roots), verified);
expect(timeouts).toEqual([BACKUP_REMOTE_TIMEOUT_MS, BACKUP_REMOTE_TIMEOUT_MS, BACKUP_REMOTE_TIMEOUT_MS, BACKUP_REMOTE_TIMEOUT_MS]);
@@ -325,7 +345,7 @@ test('a remote that would not get its full slot is deferred as budget_exhausted,
expect(states.filter(s => s === 'verified')).toHaveLength(4);
expect(states).toContain('budget_exhausted');
expect(states).not.toContain('unavailable');
- } finally { probe.mockRestore(); }
+ } finally { probe.restore(); }
}, 20_000);
test('a deferred probe keeps first-pass verified evidence; a transport failure still supersedes it (#6499)', async () => {
@@ -333,13 +353,13 @@ test('a deferred probe keeps first-pass verified evidence; a transport failure s
const first = await getBackupStatus(engine(roots), { ...verified, forceRefresh: true });
expect(first.assets.filter(a => a.verification?.state === 'verified')).toHaveLength(5);
const timeouts: number[] = [];
- const probe = spyOn(childProcess, 'execFile').mockImplementation(slowRemote(1_700, timeouts) as unknown as typeof childProcess.execFile);
+ const probe = virtualSlowRemote(1_700, timeouts);
try {
const second = await getBackupStatus(engine(roots), { ...verified, forceRefresh: true, now: new Date(now.getTime() + 1000) });
expect(timeouts).toHaveLength(4);
expect(second.assets.filter(a => a.kind === 'source_repo').map(a => a.verification?.state)).toEqual(['verified', 'verified', 'verified', 'verified', 'verified']);
expect(second.totals.recoverable_repos).toBe(5);
- } finally { probe.mockRestore(); }
+ } finally { probe.restore(); }
const failing = spyOn(childProcess, 'execFile').mockImplementation(((_f: string, _a: string[], options: { timeout: number }, callback: (error: Error | null, stdout: string) => void) => {
timeouts.push(options.timeout);
setTimeout(() => callback(Object.assign(new Error('fatal: could not read from remote'), { code: 128 }), ''), 10);