// tests/helpers/timing.mjs // A timing assertion that tolerates a busy machine without loosening its limit. // // Load only ever ADDS time to a run, so the fastest of a few runs is the best // reading of what the code itself costs. `fastestWithin` measures up to // `tries` runs and passes as soon as one is under the limit. A real slowness // is slow every time and still fails; one run slowed by four other test suites // does not (measured S123 and in wave 2, 2026-10-08: SC #14 red under load // 95-177, green alone). /** * @param {number} limitMs - the limit; a run must be strictly under it * @param {() => Promise} measure - runs once, resolves to its elapsed ms * @param {number} [tries=3] * @returns {Promise<{ok: boolean, fastest: number, runs: number[]}>} */ export async function fastestWithin(limitMs, measure, tries = 3) { const runs = []; for (let i = 0; i < tries; i++) { runs.push(await measure()); if (runs[runs.length - 1] < limitMs) break; } const fastest = Math.min(...runs); return { ok: fastest < limitMs, fastest, runs }; }