From 96beddfbe5a516411656830986aeebcfc16a7a82 Mon Sep 17 00:00:00 2001 From: Gergo Magyar Date: Fri, 30 Oct 2026 19:38:42 +0000 Subject: [PATCH] fix: address scenario-map feedback (#3743) --- .../unit/scenario-map/report-json.test.ts | 4 +- .../unit/scenario-map/trace-setup.test.ts | 41 +++++++++++++++++++ gitnexus/tools/scenario-map/report/json.ts | 6 +-- .../scenario-map/value-flow/trace.setup.ts | 3 +- .../tools/scenario-map/value-flow/trace.ts | 2 +- 5 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 gitnexus/test/unit/scenario-map/trace-setup.test.ts diff --git a/gitnexus/test/unit/scenario-map/report-json.test.ts b/gitnexus/test/unit/scenario-map/report-json.test.ts index 2bca9fd9d..e5ed5c452 100644 --- a/gitnexus/test/unit/scenario-map/report-json.test.ts +++ b/gitnexus/test/unit/scenario-map/report-json.test.ts @@ -34,12 +34,14 @@ describe('report JSON', () => { ...entry, keep: [{ test: entry.a, reason: 'intentional control' }], })); + report.fingerprint.pairs[0].details = Array(1_000).fill('nested record detail'); const file = output(); const stringify = JSON.stringify; - // Model the V8 cap at a small scale: rows fit, the complete report and collections do not. + // Model the V8 cap at a small scale, including a collection nested inside one record. const limit = 16_384; expect(stringify(report.fingerprint.pairs).length).toBeGreaterThan(limit); expect(stringify(report.fingerprint.kept).length).toBeGreaterThan(limit); + expect(stringify(report.fingerprint.pairs[0]).length).toBeGreaterThan(limit); const capped = vi.spyOn(JSON, 'stringify').mockImplementation((value, replacer, space) => { const text = stringify(value, replacer, space); if (text !== undefined && text.length > limit) throw new RangeError('Invalid string length'); diff --git a/gitnexus/test/unit/scenario-map/trace-setup.test.ts b/gitnexus/test/unit/scenario-map/trace-setup.test.ts new file mode 100644 index 000000000..5f43d3edc --- /dev/null +++ b/gitnexus/test/unit/scenario-map/trace-setup.test.ts @@ -0,0 +1,41 @@ +import { afterEach, expect, it, vi } from 'vitest'; +import { FIXTURE_CAPTURE_KEY } from '../../../tools/scenario-map/path-coverage/fixture-capture.js'; + +const previousCapture = Reflect.get(globalThis, FIXTURE_CAPTURE_KEY); + +afterEach(() => { + vi.doUnmock('vitest'); + vi.resetModules(); + vi.restoreAllMocks(); + if (previousCapture === undefined) Reflect.deleteProperty(globalThis, FIXTURE_CAPTURE_KEY); + else Reflect.set(globalThis, FIXTURE_CAPTURE_KEY, previousCapture); +}); + +it('captures pipeline fixtures only while a candidate test is active', async () => { + let current: { id: string } | undefined; + // Reload the setup module even when the detector already installed it for this worker. + vi.resetModules(); + const { TraceFixtureRoots } = + await import('../../../tools/scenario-map/value-flow/trace-fixtures.js'); + const capture = vi.spyOn(TraceFixtureRoots.prototype, 'capture').mockImplementation(() => {}); + vi.doMock('vitest', () => ({ + TestRunner: { getCurrentTest: () => current }, + inject: () => ({ taskIds: ['candidate'], preparedRoot: '/prepared', extraRoots: [], wrap: [] }), + aroundAll: vi.fn(), + aroundEach: vi.fn(), + })); + await import('../../../tools/scenario-map/value-flow/trace.setup.js'); + const hook = Reflect.get(globalThis, FIXTURE_CAPTURE_KEY) as (root: string) => void; + + hook('/suite-setup'); + expect(capture).not.toHaveBeenCalled(); + current = { id: 'sibling' }; + hook('/sibling'); + expect(capture).not.toHaveBeenCalled(); + current = { id: 'candidate' }; + hook('/candidate'); + expect(capture).toHaveBeenCalledExactlyOnceWith('/candidate'); + current = undefined; + hook('/suite-teardown'); + expect(capture).toHaveBeenCalledTimes(1); +}); diff --git a/gitnexus/tools/scenario-map/report/json.ts b/gitnexus/tools/scenario-map/report/json.ts index c83c4ae94..69e940ae7 100644 --- a/gitnexus/tools/scenario-map/report/json.ts +++ b/gitnexus/tools/scenario-map/report/json.ts @@ -1,4 +1,4 @@ -/** Report JSON can exceed V8's single-string limit. Keep I/O bounded by individual records. */ +/** Report JSON can exceed V8's single-string limit. Serialize nested collections incrementally. */ import { JSONParser } from '@streamparser/json'; import { randomUUID } from 'node:crypto'; import { createReadStream, createWriteStream } from 'node:fs'; @@ -9,13 +9,13 @@ import { pipeline } from 'node:stream/promises'; import { isRecord } from '../util.js'; import type { ScenarioMapReport } from './report.js'; -/** Report collections (pairs, tests, anchors, etc.) are serialized one entry at a time. */ +/** Recurse through collections so even a single oversized record never becomes one string. */ function* jsonChunks(value: unknown): Generator { if (Array.isArray(value)) { yield '['; for (let i = 0; i < value.length; i++) { if (i > 0) yield ',\n'; - yield JSON.stringify(value[i]) ?? 'null'; + yield* jsonChunks(value[i]); } yield ']'; } else if (isRecord(value)) { diff --git a/gitnexus/tools/scenario-map/value-flow/trace.setup.ts b/gitnexus/tools/scenario-map/value-flow/trace.setup.ts index 3a9b73635..dc25c4fe2 100644 --- a/gitnexus/tools/scenario-map/value-flow/trace.setup.ts +++ b/gitnexus/tools/scenario-map/value-flow/trace.setup.ts @@ -45,8 +45,7 @@ const currentTest = (): CurrentTest | null => { const fixtures = new TraceFixtureRoots(); Reflect.set(globalThis, FIXTURE_CAPTURE_KEY, (root: unknown) => { - const test = TestRunner.getCurrentTest(); - if (test === undefined || candidateTaskIds.has(test.id)) fixtures.capture(root); + if (currentTest() !== null) fixtures.capture(root); }); const recorder = new TraceRecorder({ currentTest, diff --git a/gitnexus/tools/scenario-map/value-flow/trace.ts b/gitnexus/tools/scenario-map/value-flow/trace.ts index e16259956..b8aad065f 100644 --- a/gitnexus/tools/scenario-map/value-flow/trace.ts +++ b/gitnexus/tools/scenario-map/value-flow/trace.ts @@ -6,7 +6,7 @@ * * Run ({@link runTrace}). One throwaway copy, `/trace/prepared`, prepared like a shard's * copy (tools/mutation/prepare, nothing blanked) and verified against the run manifest (run-group.ts - * `verifyPreparedCopy`; a mismatch runs nothing). Every manifest test in the candidate tests' + * `verifyPreparedCopy`; a mismatch runs nothing). Every runnable manifest test in the candidate tests' * files runs twice through vitest-run.ts `runVitest`, each a fresh Vitest instance with the same * selection: a baseline, then the traced run with trace.setup.ts added (`setupFiles`) and its * context provided: the raw-record directory `/trace/raw`, the prepared root, the extra