Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340/** Tests for `./assertion-source.ts`: the total string helpers (`extractLocationSubstring`, `isIntegerString`), the backward source-window extraction, and the node-only error-tree reader that splices an assertion's source line into failure output. Frame parsing has no standalone optional-returning function (absence is a `continue` guard inside the reader), so its edge cases (`file://` URL, missing column, native frame) are exercised through `readAssertionSites`. @module */
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import { extractAssertionExpression, extractLocationSubstring, isIntegerString, readAssertionSites,} from '@monochromatic-dev/module-test-diagnostic';
/** Writes `lines` to a unique temp `.ts` file and returns its absolute path, so a synthetic stack frame can point at a real readable file. The name is keyed by pid and `index` to avoid collisions across the suite without relying on randomness. @param lines - file contents, one element per line @param index - per-test discriminator folded into the filename @returns absolute path of the written fixture @example ```ts const path = await writeTempSource({ lines: ['expect(x,)', ' .toBe(2,);'], index: 0, }); ``` */async function writeTempSource({ lines, index,}: { readonly lines: readonly string[]; readonly index: number;},): Promise<string> { /** Node fs imported here (not at module top) so this module stays loadable where filesystem access is absent. */ const { writeFile, } = await import('node:fs/promises'); /** System temp directory, the disposable home for fixture files this test reads back. */ const { tmpdir, } = await import('node:os'); /** Absolute fixture path, unique per process and test index. */ const path = `${tmpdir()}/assertion-source-${String(process.pid,)}-${String(index,)}.ts`; await writeFile( path, lines.join('\n',), 'utf8', ); return path;}
/** Removes a temp fixture, ignoring errors so cleanup never masks a real assertion failure. @param path - absolute fixture path to delete */async function removeTempSource(path: string,): Promise<void> { /** Node fs imported lazily, matching {@link writeTempSource}. */ const { rm, } = await import('node:fs/promises'); try { await rm(path,); } catch (error: unknown) { if (!Error.isError(error,)) throw error; // best-effort cleanup; the OS reclaims tmp regardless }}
await describe({ name: 'assertion-source', children: [ //region extractLocationSubstring
it({ name: 'extracts the parenthesised location body', fn: async () => { expect(extractLocationSubstring('at fn (/abs/file.ts:21:12)',),) .toBe('/abs/file.ts:21:12',); }, },),
it({ name: 'strips a file:// URL scheme from the location body', fn: async () => { expect(extractLocationSubstring('at fn (file:///abs/file.ts:21:12)',),) .toBe('/abs/file.ts:21:12',); }, },),
it({ name: 'handles the bare at <path> form without a function name', fn: async () => { expect(extractLocationSubstring('at /abs/file.ts:9:3',),) .toBe('/abs/file.ts:9:3',); }, },),
//endregion extractLocationSubstring
//region isIntegerString
it({ name: 'accepts a run of digits and rejects empty or non-digit input', fn: async () => { expect(isIntegerString('21',),).toBe(true,); expect(isIntegerString('',),).toBe(false,); expect(isIntegerString('a1',),).toBe(false,); expect(isIntegerString('1.2',),).toBe(false,); }, },),
//endregion isIntegerString
//region extractAssertionExpression
it({ name: 'joins a multi-line assertion back to its expect( opener', fn: async () => { expect(extractAssertionExpression({ sourceLines: [ 'const c = 3;', 'expect(errorSpy.callCount,)', ' .toBe(2,);', ], lineNumber: 3, },),) .toBe('expect(errorSpy.callCount,) .toBe(2,);',); }, },),
it({ name: 'returns a single-line assertion unchanged but trimmed', fn: async () => { expect(extractAssertionExpression({ sourceLines: [' expect(x,).toBe(2,);',], lineNumber: 1, },),) .toBe('expect(x,).toBe(2,);',); }, },),
it({ name: 'falls back to the target line when no expect( is in range', fn: async () => { expect(extractAssertionExpression({ sourceLines: [ 'const a = 1;', 'doSomething(a,);', ], lineNumber: 2, },),) .toBe('doSomething(a,);',); }, },),
it({ name: 'does not reach an expect( beyond the lookback window', fn: async () => { /** 14 lines: an `expect(` opener on line 1, then 12 filler lines, then the matcher on line 14, so the opener sits one line past the 12-line lookback and must not be folded in. */ const sourceLines = [ 'expect(farAway,)', ...Array.from({ length: 12, }, function filler(_unused, i,) { return `const filler${String(i,)} = ${String(i,)};`; },), ' .toBe(0,);', ]; expect(extractAssertionExpression({ sourceLines, lineNumber: 14, },),) .toBe('.toBe(0,);',); }, },),
it({ name: 'returns empty string when the line is out of range', fn: async () => { expect(extractAssertionExpression({ sourceLines: ['expect(x,).toBe(1,);',], lineNumber: 99, },),) .toBe('',); }, },),
//endregion extractAssertionExpression
//region readAssertionSites
it({ name: 'reads the assertion site from the first non-harness frame', fn: async () => { const path = await writeTempSource({ lines: [ 'const errorSpyCallCount = 3;', 'expect(errorSpyCallCount,)', ' .toBe(2,);', ], index: 0, },); const error = new Error('expected 3 to equal 2',); // Harness matcher frame first (must be skipped), then the user frame. error.stack = [ 'Error: expected 3 to equal 2', ' at toBe (package/module/test/src/expect-matchers-core.ts:70:10)', ` at fn (${path}:3:12)`, ] .join('\n',); const sites = await readAssertionSites(error,); /** Site recovered for the root error, asserted present before reading its fields. */ const site = sites.get(error,); expect(site,).toBeDefined(); expect(site?.expression,).toBe('expect(errorSpyCallCount,) .toBe(2,);',); expect(site?.location,).toContain(':3',); await removeTempSource(path,); }, },),
it({ name: 'parses a file:// URL frame and a frame without a column', fn: async () => { const path = await writeTempSource({ lines: [ 'expect(value,)', ' .toBe(expected,);', ], index: 1, },); /** file:// frame carrying only `path:line` (no column), exercising both parse branches at once. */ const error = new Error('boom',); error.stack = [ 'Error: boom', ` at fn (file://${path}:2)`, ] .join('\n',); const sites = await readAssertionSites(error,); expect(sites.get(error,)?.expression,).toBe('expect(value,) .toBe(expected,);',); await removeTempSource(path,); }, },),
it({ name: 'skips a native frame and resolves the next readable frame', fn: async () => { const path = await writeTempSource({ lines: ['expect(ok,).toBe(true,);',], index: 2, },); const error = new Error('boom',); error.stack = [ 'Error: boom', ' at processTicksAndRejections (node:internal/process/task_queues)', ` at fn (${path}:1:1)`, ] .join('\n',); const sites = await readAssertionSites(error,); expect(sites.get(error,)?.expression,).toBe('expect(ok,).toBe(true,);',); await removeTempSource(path,); }, },),
it({ name: 'reads sites for both the error and its cause', fn: async () => { const path = await writeTempSource({ lines: [ 'expect(value,)', ' .toBe(expected,);', ], index: 3, },); const root = new Error('root assertion',); root.stack = [ 'Error: root assertion', ` at fn (${path}:2:5)`, ] .join('\n',); const outer = new Error('wrapper', { cause: root, },); outer.stack = [ 'Error: wrapper', ` at fn (${path}:2:5)`, ] .join('\n',); const sites = await readAssertionSites(outer,); expect(sites.get(outer,)?.expression,).toBe('expect(value,) .toBe(expected,);',); expect(sites.get(root,)?.expression,).toBe('expect(value,) .toBe(expected,);',); await removeTempSource(path,); }, },),
it({ name: 'yields no site when no frame resolves to a readable file', fn: async () => { const error = new Error('boom',); error.stack = [ 'Error: boom', ' at fn (/no/such/path/missing.ts:9:1)', ] .join('\n',); const sites = await readAssertionSites(error,); expect(sites.get(error,),).toBeUndefined(); }, },),
//endregion readAssertionSites ],},);