From 0e8972c6fae1e2ff59137ccc1914b7bbcf3fbb63 Mon Sep 17 00:00:00 2001 From: David Snelling Date: Tue, 23 Jun 2026 16:02:07 -0700 Subject: [PATCH] test(8.0): de-flake the VFS path-cache timing assertion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `should cache paths for fast repeated access` compared a single cold vs warm readFile with Date.now() (ms resolution). Both reads are sub-millisecond, so the warm read intermittently measured 1ms vs the cold 0ms and the `time2 <= time1` assertion failed on rounding noise — a non-deterministic gate that blocked a clean release. Replace it with the average of 100 warm reads via performance.now() against a generous absolute bound (cached path reads are sub-ms), plus a content round-trip check. Same intent (cached access is fast), no single-shot ms flake. Co-Authored-By: Claude Opus 4.8 --- tests/vfs/vfs.unit.test.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/vfs/vfs.unit.test.ts b/tests/vfs/vfs.unit.test.ts index c2f49c9f..cc201de5 100644 --- a/tests/vfs/vfs.unit.test.ts +++ b/tests/vfs/vfs.unit.test.ts @@ -396,19 +396,19 @@ describe('VirtualFileSystem - Production Tests', () => { await vfs.mkdir('/cached') await vfs.writeFile(path, 'cached content') - // First read (cold cache) - const start1 = Date.now() - await vfs.readFile(path) - const time1 = Date.now() - start1 + // Prime the path cache + verify the content round-trips. + expect((await vfs.readFile(path)).toString()).toBe('cached content') - // Second read (warm cache) - const start2 = Date.now() - await vfs.readFile(path) - const time2 = Date.now() - start2 + // Cached repeated access is sub-millisecond. Average many reads so the result + // doesn't hinge on Date.now()'s millisecond rounding of a single sub-ms op — the + // old cold-vs-warm compare spuriously failed when both rounded to 0/1ms. A + // generous absolute bound still catches a regression to per-read disk/index work. + const ITERATIONS = 100 + const start = performance.now() + for (let i = 0; i < ITERATIONS; i++) await vfs.readFile(path) + const avgWarmMs = (performance.now() - start) / ITERATIONS - // Cache should make second read faster - expect(time2).toBeLessThanOrEqual(time1) - console.log(`Cold read: ${time1}ms, Warm read: ${time2}ms`) + expect(avgWarmMs).toBeLessThan(5) }) })