From b1c01ba89f79e6fefb9df6eea9743a0c9cc7934e Mon Sep 17 00:00:00 2001 From: DeepView Autofix <276251120+deepview-autofix@users.noreply.github.com> Date: Thu, 16 Apr 2026 19:30:15 +0300 Subject: [PATCH] fix(mock): honor configurable flushInterval in SnapshotRecorder The SnapshotRecorder constructor accepted a `flushInterval` option (and documented a 30s default), but `#scheduleFlush` always used a hardcoded 1000ms timeout, so the option had no effect on auto-flush timing. Use `this.flushInterval` in `#scheduleFlush` so the configured value actually controls how often snapshots are written to disk. Co-Authored-By: Claude Co-Authored-By: DeepView Autofix <276251120+deepview-autofix@users.noreply.github.com> Co-Authored-By: Nikita Skovoroda Signed-off-by: Nikita Skovoroda --- lib/mock/snapshot-recorder.js | 2 +- test/snapshot-testing.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/mock/snapshot-recorder.js b/lib/mock/snapshot-recorder.js index b5d07fae381..f1f4196e7a8 100644 --- a/lib/mock/snapshot-recorder.js +++ b/lib/mock/snapshot-recorder.js @@ -555,7 +555,7 @@ class SnapshotRecorder { } else { this.#flushTimeout = null } - }, 1000) // 1 second debounce + }, this.flushInterval) } /** diff --git a/test/snapshot-testing.js b/test/snapshot-testing.js index 8bb6c815c01..38486648c00 100644 --- a/test/snapshot-testing.js +++ b/test/snapshot-testing.js @@ -688,6 +688,41 @@ describe('SnapshotAgent - Advanced Features', () => { assert.strictEqual(snapshots[0].snapshot.request.url, `${origin}/autoflush-test`, 'Auto-flushed snapshot should have correct URL') }) + + it('auto-flush honors configured flushInterval', async (t) => { + const server = createTestServer((req, res) => { + res.writeHead(200, { 'content-type': 'text/plain' }) + res.end('flush interval test') + }) + + const { port } = await setupServer(server) + const origin = `http://127.0.0.1:${port}` + const snapshotPath = createSnapshotPath('flush-interval') + + setupCleanup(t, { server, snapshotPath }) + + const agent = new SnapshotAgent({ + mode: 'record', + snapshotPath, + autoFlush: true, + flushInterval: 100 + }) + + const originalDispatcher = getGlobalDispatcher() + setupCleanup(t, { agent, originalDispatcher }) + setGlobalDispatcher(agent) + + await request(`${origin}/flush-interval-test`) + + // Wait long enough for a 100ms interval to fire, but well below the + // previously hardcoded 1000ms debounce - regression check. + await new Promise(resolve => setTimeout(resolve, 400)) + + const fileData = await readFile(snapshotPath, 'utf8') + const snapshots = JSON.parse(fileData) + assert.strictEqual(snapshots.length, 1, + 'Snapshot should be auto-flushed within configured flushInterval') + }) }) describe('SnapshotAgent - Header Management', () => {