import assert from 'node:assert/strict'; import {setTimeout as delay} from 'node:timers/promises'; import {RunningClock,K2100_MIN_RUNNING_MS} from './k2100-running-clock.mjs'; assert.equal(K2100_MIN_RUNNING_MS, 7_200_000, 'unchanged two-hour admission boundary'); let now = 10; const c = new RunningClock(() => now); assert.throws(() => c.pauseBeforeStop()); assert.throws(() => c.resumeAfterConfirmed('paused')); c.resumeAfterConfirmed('running'); assert.throws(() => c.resumeAfterConfirmed('running')); now += 1_000; c.pauseBeforeStop(); now += 10_000_000; assert.equal(c.snapshot().durationMet, false); assert.equal(c.snapshot().runningMilliseconds, 1_000); assert.throws(() => c.pauseBeforeStop()); c.resumeAfterConfirmed('running'); now += K2100_MIN_RUNNING_MS - 1_001; assert.equal(c.snapshot().durationMet, false); now++; assert.equal(c.snapshot().durationMet, true); c.pauseBeforeStop(); const complete = c.snapshot(); assert.equal(complete.runningMilliseconds, K2100_MIN_RUNNING_MS); assert.equal(complete.excludedMilliseconds, 10_000_000); assert.equal(complete.segments.length, 2); complete.segments[0].milliseconds = 0; assert.equal(c.snapshot().segments[0].milliseconds, 1_000); // Invalid clock samples cannot commit a transition or accumulated time. const stable = c.snapshot(); for (const bad of [now - 1, NaN, Infinity, -1]) { const saved = now; now = bad; assert.throws(() => c.resumeAfterConfirmed('running')); now = saved; assert.deepEqual(c.snapshot(), stable); } c.resumeAfterConfirmed('running');const running = c.snapshot(); const saved = now; now--; assert.throws(() => c.pauseBeforeStop()); now = saved;assert.deepEqual(c.snapshot(), running); // Real-clock smoke: a paused interval changes wall time only. This is a short // utility test, and makes no 7,200-second guest or host admission claim. const real = new RunningClock();real.resumeAfterConfirmed('running'); await delay(20);real.pauseBeforeStop();const before = real.snapshot(); await delay(30);const paused = real.snapshot(); assert.equal(paused.runningMilliseconds, before.runningMilliseconds); assert.ok(paused.excludedMilliseconds > before.excludedMilliseconds); real.resumeAfterConfirmed('running');await delay(20);real.pauseBeforeStop(); const after = real.snapshot();assert.ok(after.runningMilliseconds > before.runningMilliseconds); assert.equal(after.durationMet, false); console.log(JSON.stringify({status:'RUNNING_CLOCK_HOST_TESTS_PASSED',modeledBoundaryMilliseconds:K2100_MIN_RUNNING_MS,realClock:after,guestAccepted:false}));