const test = require('node:test'); const assert = require('node:assert/strict'); const { waitForCondition } = require('../erp_condition_wait'); test('waitForCondition returns as soon as the condition is true', async () => { let checks = 0; let waits = 0; let now = 0; const result = await waitForCondition( async () => { checks += 1; return checks === 3 ? 'ready' : false; }, { description: 'sample condition', timeoutMs: 5000, intervalMs: 100, now: () => now, wait: async (ms) => { waits += 1; now += ms; }, } ); assert.equal(result, 'ready'); assert.equal(checks, 3); assert.equal(waits, 2); }); test('waitForCondition times out with the last condition error', async () => { let now = 0; await assert.rejects( () => waitForCondition( async () => { throw new Error('not ready yet'); }, { description: 'never-ready condition', timeoutMs: 250, intervalMs: 100, now: () => now, wait: async (ms) => { now += ms; }, } ), /Timed out waiting for never-ready condition after 250ms: not ready yet/ ); });