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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196/** Stack-frame classification shared by the failure formatter ({@link ../format-error.ts}) and the assertion-site source reader ({@link ../assertion-source.ts}). Holds the fragments that identify the harness's own dispatch machinery plus the getter-safe property reader both modules need. Split into its own module so `assertion-source.ts` can reuse the classifier without importing `format-error.ts`, which would create a cycle (`format-error.ts` already imports `assertion-source.ts` to enrich failures with their source line). @module */
//region Getter-safe property read
/** Reads a property from an error-like object, returning `undefined` when the key is absent or when accessing it throws. Error objects can carry a throwing getter (or a `Proxy` `get` trap) on `.message`, `.cause`, `.stack`, and similar; reading them directly would let the getter's throw escape mid-walk. `Reflect.get` returns `undefined` for an absent key, and the `try` swallows a throwing read, so callers degrade gracefully (treat the field as missing) instead of propagating the trap. @param source - object to read from @param key - property name to read @returns property value, or `undefined` when absent or unreadable @mutates source - `Reflect.get` may invoke caller-defined getters or proxy traps. @example ```ts readProperty({ source: new Error('boom'), key: 'message', }) // 'boom' readProperty({ source: {}, key: 'message', }) // undefined ``` */export function readProperty({ source, key,}: { readonly source: object; readonly key: string;},): unknown { try { return Reflect.get( source, key, ); } catch (error: unknown) { if (!Error.isError(error,)) throw error; return undefined; }}
//endregion Getter-safe property read
//region Harness frame fragments
/** Substrings identifying stack frames inside the harness's bundled output or the vendored assertion stack. Any frame containing one of these is dispatch machinery, never the failing user code, so it is dropped from rendered traces. Three categories: - Runner, assertion, sandbox, and diagnostic bundles, under workspace paths (`package/module/test-expect/dist/`) and installed package paths (`module-test-expect/dist/`). The source-export counterpart lives in {@link HARNESS_SOURCE_FRAGMENTS}, which needs the test-file guard. - The vendored assertion stack. `expect()` lands in `chai`, which dispatches through `chai-as-promised` and `sinon-chai`, and `sinon` spies feed the sinon-chai matchers. These package paths identify assertion dispatch rather than the user's failing assertion. - `p-limit`, the concurrency limiter `describe.ts` uses to fan tests out. Its frames sit below the harness runner on every failure and carry no diagnostic value. Fragments match with substring `includes`, collapsing pnpm's virtual store layout (`node_modules/.pnpm/chai@6.2.2/node_modules/chai/index.js`) and the flat / hoisted layout (`node_modules/chai/index.js`) under one entry per package. The trailing slash on each package fragment prevents `chai/` matching `chai-as-promised/` and `sinon/` matching `sinon-chai/`. */export const HARNESS_INTERNAL_FRAGMENTS: readonly string[] = [ 'package/module/test/dist/', 'module-test/dist/', 'package/module/test-expect/dist/', 'module-test-expect/dist/', 'package/module/test-sandbox/dist/', 'module-test-sandbox/dist/', 'package/module/test-diagnostic/dist/', 'module-test-diagnostic/dist/', 'node_modules/chai/', 'node_modules/chai-as-promised/', 'node_modules/sinon-chai/', 'node_modules/sinon/', 'node_modules/p-limit/',];
/** Substrings identifying frames inside the harness's own **source** dispatch files (`it.ts`, `describe.ts`, `descriptor.ts`, `expect*.ts`, `format-error.ts`, ...). Consumers import the harness via `/ts` exports, which map to `src/`, so those dispatch frames must also be hidden. Both workspace and installed package forms are listed for the runner and every extracted responsibility. Unlike {@link HARNESS_INTERNAL_FRAGMENTS} these match only when the frame is **not** a test file: the harness's own test suite lives under `package/module/test/src/` too, and those frames are the user code when module/test tests itself. {@link isTestFileFrame} gates the match. */export const HARNESS_SOURCE_FRAGMENTS: readonly string[] = [ 'package/module/test/src/', 'module-test/src/', 'package/module/test-expect/src/', 'module-test-expect/src/', 'package/module/test-sandbox/src/', 'module-test-sandbox/src/', 'package/module/test-diagnostic/src/', 'module-test-diagnostic/src/',];
//endregion Harness frame fragments
//region Frame classification
/** True when the frame points at a test file (`*.test.ts`, `*.unit.test.ts`, `*.property.unit.test.ts`, ...). Used to spare the harness's own test frames from the source-dispatch filter: those frames are the user code when module/test tests itself. @param frame - trimmed stack frame line @returns whether the frame's path contains a `.test.` segment @example ```ts isTestFileFrame('at fn (package/foo/src/a.unit.test.ts:9:1)') // true isTestFileFrame('at fn (package/module/test/src/it.ts:9:1)') // false ``` */export function isTestFileFrame(frame: string,): boolean { return frame.includes('.test.',);}
/** True when the frame belongs to the harness's dispatch machinery and should be dropped from a rendered trace. A frame qualifies when it matches a bundle / vendored-assertion fragment unconditionally, or a source-dispatch fragment while not being a test file. The failing user code is the interesting site; everything this function flags is the plumbing under it. @param frame - trimmed stack frame line @returns whether the frame should be dropped @example ```ts isHarnessInternalFrame('at toBe (package/module/test/src/expect-matchers-core.ts:70:10)') // true isHarnessInternalFrame('at fn (package/module/test/src/it.unit.test.ts:21:12)') // false isHarnessInternalFrame('at userFn (package/foo/src/bar.ts:9:19)') // false ``` */export function isHarnessInternalFrame(frame: string,): boolean { if (HARNESS_INTERNAL_FRAGMENTS.some(function frameContainsBundle(fragment,) { return frame.includes(fragment,); },)) return true;
if ((!isTestFileFrame(frame,)) && HARNESS_SOURCE_FRAGMENTS.some(function frameContainsSource(fragment,) { return frame.includes(fragment,); },)) return true;
return false;}
//endregion Frame classification