GitNexus/gitnexus/tools/scenario-map/consolidation/assertions.ts
Gergő Magyar bd9586e008
fix: recover duplicate candidates and expose test inventory gaps (#3743)
* fix: recover missed test scenarios

Run the full core test inventory and retrieve identical callback bodies
across setup contexts. Separate assertion evidence from context and
retain conservative input, path and runtime provenance checks.

Restore prepared test inputs and report missing runtime collection.
Add the audited recall corpus and inventory regressions.

* Address PR review feedback (#3743)

Correct counted nouns in the inventory report, document fully skipped modules, and include unhandled runner errors in the incomplete-collection warning.

* Address PR review feedback (#3743)

Force-stage explicit temporary fixtures despite inherited Git ignores, and isolate recorded-file collection-error accounting in the inventory tests.

* fix(review): simplify selection and guard mixed artifacts

* feat(test): detect fixture consolidation opportunities

Capture source inputs used by integration tests and compare fixture clones,
executed blocks, and assertion subjects and values. Report partial overlaps
with the unique checks and fixture spans that consolidation must preserve.
Keep cross-language structural suggestions separate from observed equality.

Use pinned jscpd with direct pair verification when longer representatives
hide partial clones. Retain capture gaps and native/worker limitations.

Validate with 1,245 detector regressions, 28 pipeline pair controls, and 12
manually labeled comparisons. Scan 30,698 core cases and find 3,111 opportunity
groups; exclude the capture's 103 failed and 89 skipped cases.

* Fix scenario-map review findings (#3743)

Resolve assertion aliases by lexical binding and normalize duplicate static object keys. Capture fixture files through checked descriptors with bounded reads and guaranteed cleanup.

* fix: address test duplicate audit false signals

* refactor(test): reuse assertion binding analysis

Reuse lexical bindings within each evidence extraction while retaining
assertion-specific offsets and expansion limits. Deduplicate fixture
coverage endpoints numerically without string conversion.

Preserve complete assertion evidence across 29,286 static instances and
weighted coverage across 10,000 generated interval cases.

* Fix scenario-map review findings (#3743)

Bind consolidation evidence to the assertion call accepted by the check
extractor, so unsupported same-line calls cannot supply incorrect subjects
or expectations. Keep source provenance private and serialized checks intact.

Exercise omitted plugins alongside instrumented Vitest runs.

* fix: retain input-shaped scenario controls (#3743)

Compare assertion subject structure separately from exact input-bearing
assertion keys, so changing literal inputs retains table-row and boundary
classification. Preserve selectors, callee identity and regex values.

Keep the control expectations unchanged. Add regressions for literal
inputs, queried subjects and nested regex arguments; bump the fingerprint
engine version so reclassified findings resurface.

* test: give scenario cases unique titles

Include the parameterized case index so repeated input expressions and truncated audit paths cannot collide in the CI execution inventory.

* fix: scope assertion subjects to their bindings

Prevent same-named declarations in different files or scopes from
becoming exact assertion matches. Preserve same-binding consolidation
and advance the fingerprint engine to version 5.

* fix: raise full-suite planning heap budget

The 30,953-case replay aborts at the default 4 GiB heap. Give only
the Plan step 8 GiB and document the same local invocation.

The identical four coverage slices complete in 4m34s at 8.2 GiB
peak RSS, retaining all 116,409 candidate pairs.

* fix: stream full-suite scenario reports

The full inventory produces a 663 MiB report, exceeding V8's
single-string limit. Write records incrementally and stream only
required fields when reading baselines and publishing issue updates.

Replay preserves all 116,398 pairs from 30,953 collected cases.
The report remains partial when the trace run fails.

* fix: preserve test lifecycles during capture

Forced test repeats changed cache and file state. Capture ordinary runs,
retain runnable siblings during tracing, and keep retry-bearing files
inconclusive. Preserve failure details and mark failed captures partial.

Use the original JSON serializer so test mocks cannot break fixture reads.

* fix: limit trace capture to candidates

Keep sibling tests in both passes for suite state, while recording only candidate task IDs. This prevents unrelated timing checks and large corpus tests from failing under tracing.

* ci: budget full-suite trace collection

* fix: address scenario-map feedback (#3743)

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-10-30 21:06:08 +00:00

419 lines
16 KiB
TypeScript

import * as t from '@babel/types';
import traverse, { type Binding } from '@babel/traverse';
import { generate } from '@babel/generator';
import { resolveName } from '../../test-dupes/test-dupes.js';
import type { RowValue } from '../types.js';
import {
abstractNode,
assertionCallOf,
buildScope,
rowValueOf,
type CheckSubject,
type InstanceChecks,
type Scope,
} from '../facet/checks.js';
import { sha1, sortedUnique } from '../util.js';
export interface AssertionAtom {
line: number;
shape: string;
shapeKey: string;
/** Subject structure with literal imported-call inputs abstracted; selectors remain exact. */
subjectShapeKey?: string;
/** Exact expected literals/snapshot and subject shape, or null for unresolved values. */
key: string | null;
expected: string | null;
observed?: string | null;
}
export interface AssertionEvidence {
atoms: AssertionAtom[];
unresolved: string[];
}
function canonicalRow(row: RowValue): unknown | undefined {
if (row.kind === 'unresolved') return undefined;
if (row.kind === 'array') {
const items = row.items.map(canonicalRow);
return items.includes(undefined) ? undefined : { kind: 'array', items };
}
if (row.kind === 'object') {
const values = new Map<string, unknown>();
for (const entry of row.entries) {
const value = canonicalRow(entry.value);
if (value === undefined) return undefined;
values.set(entry.key, value);
}
const entries = [...values].map(([key, value]) => ({ key, value }));
return { kind: 'object', entries: entries.sort((a, b) => a.key.localeCompare(b.key)) };
}
return row;
}
function literal(node: t.Node, scope: Scope): unknown | undefined {
if (t.isTSAsExpression(node) || t.isTSNonNullExpression(node) || t.isTSTypeAssertion(node))
return literal(node.expression, scope);
const row = rowValueOf(node, scope);
if (row !== null) return row === 'unknown' ? undefined : canonicalRow(row);
if (t.isStringLiteral(node)) return { kind: 'string', value: node.value };
if (t.isNumericLiteral(node)) return { kind: 'number', text: String(node.value) };
if (t.isBooleanLiteral(node)) return { kind: 'boolean', value: node.value };
if (t.isNullLiteral(node)) return { kind: 'null' };
if (t.isBigIntLiteral(node)) return { kind: 'bigint', text: String(node.value) };
if (t.isRegExpLiteral(node)) return { kind: 'regexp', pattern: node.pattern, flags: node.flags };
if (
t.isIdentifier(node, { name: 'undefined' }) &&
!scope.locals.has('undefined') &&
resolveName('undefined', scope.record.chain) === undefined
)
return { kind: 'undefined' };
if (t.isUnaryExpression(node) && ['-', '+', '!'].includes(node.operator)) {
if (t.isNumericLiteral(node.argument) && node.operator !== '!') {
const value = node.operator === '-' ? -node.argument.value : +node.argument.value;
return { kind: 'number', text: Object.is(value, -0) ? '-0' : String(value) };
}
if (t.isBigIntLiteral(node.argument) && node.operator === '-')
return { kind: 'bigint', text: String(-BigInt(node.argument.value)) };
return undefined;
}
if (
t.isCallExpression(node) &&
t.isMemberExpression(node.callee) &&
!node.callee.computed &&
t.isIdentifier(node.callee.object, { name: 'expect' }) &&
t.isIdentifier(node.callee.property) &&
[
'objectContaining',
'arrayContaining',
'stringContaining',
'stringMatching',
'closeTo',
].includes(node.callee.property.name)
) {
const args = node.arguments.map((arg) => literal(arg, scope));
return args.includes(undefined) ? undefined : { asymmetric: node.callee.property.name, args };
}
if (t.isTemplateLiteral(node) && node.expressions.length === 0)
return { kind: 'string', value: node.quasis[0]?.value.cooked ?? node.quasis[0]?.value.raw };
if (t.isArrayExpression(node)) {
const items = node.elements.map((item) =>
item === null ? { kind: 'hole' } : literal(item, scope),
);
return items.includes(undefined) ? undefined : { kind: 'array', items };
}
if (t.isObjectExpression(node)) {
const values = new Map<string, unknown>();
for (const item of node.properties) {
if (!t.isObjectProperty(item) || item.computed) return undefined;
const key = t.isIdentifier(item.key)
? item.key.name
: t.isStringLiteral(item.key) || t.isNumericLiteral(item.key)
? String(item.key.value)
: null;
const value = literal(item.value, scope);
if (key === null || value === undefined) return undefined;
values.set(key, value);
}
const entries = [...values].map(([key, value]) => ({ key, value }));
return { kind: 'object', entries: entries.sort((a, b) => a.key.localeCompare(b.key)) };
}
return undefined;
}
/** Expand preceding immutable const bindings in their lexical scope. */
function localOrigins(
subject: CheckSubject,
scope: Scope,
): (node: t.Node, before: number) => t.Node {
const references = new WeakSet<t.Node>();
const declarations = new WeakMap<t.Node, Binding>();
traverse(t.file(t.program([t.expressionStatement(subject.record.fn)])), {
Identifier(path) {
if (path.isReferencedIdentifier()) references.add(path.node);
const binding = path.scope.getBinding(path.node.name);
if (binding) declarations.set(path.node, binding);
},
});
let nextBinding = 0;
let visited = 0;
const expand = (
node: t.Node,
before: number,
seen = new Set<Binding>(),
bindings = new Map<Binding, t.Identifier>(),
): t.Node => {
const unknown = (): t.Identifier => {
const marker = t.identifier('_unknown');
marker.name = '<unknown>';
return marker;
};
if (++visited > 5000 || seen.size > 32) {
scope.unresolved.add('assertion origin expansion limit reached');
return unknown();
}
const declaration = declarations.get(node);
if (references.has(node) && declaration) {
const bound = bindings.get(declaration);
if (bound) return bound;
// The inventory binds only the test callback's own parameters to table rows.
if (declaration.kind === 'param' && declaration.scope.block === subject.record.fn)
return node;
const decl = declaration.path.node;
if (
declaration.kind !== 'const' ||
!declaration.constant ||
!t.isVariableDeclarator(decl) ||
!t.isIdentifier(decl.id) ||
!decl.init ||
(decl.end ?? Infinity) >= before ||
seen.has(declaration)
)
return unknown();
return expand(decl.init, before, new Set([...seen, declaration]), bindings);
}
const copy = t.cloneNode(node, false);
if (t.isFunction(copy) && copy.params.every((param) => t.isIdentifier(param))) {
bindings = new Map(bindings);
copy.params = copy.params.map((param) => {
const marker = t.identifier('_bound');
marker.name = `<bound:${nextBinding++}>`;
const binding = declarations.get(param);
if (binding) bindings.set(binding, marker);
return marker;
});
}
const object = copy as unknown as Record<string, unknown>;
for (const key of t.VISITOR_KEYS[node.type] ?? []) {
// Noncomputed property names are selectors, not references to local bindings.
if (
(key === 'property' &&
(t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) &&
!node.computed) ||
(key === 'key' && t.isObjectProperty(node) && !node.computed)
)
continue;
const value = object[key];
object[key] = Array.isArray(value)
? value.map((child) => (t.isNode(child) ? expand(child, before, seen, bindings) : child))
: t.isNode(value)
? expand(value, before, seen, bindings)
: value;
}
return copy;
};
return (node, before) => {
nextBinding = 0;
visited = 0;
return expand(node, before);
};
}
function helperIdentity(
fn: t.FunctionDeclaration | t.FunctionExpression | t.ArrowFunctionExpression,
declaration: t.Node,
scope: Scope,
): string | null {
const copy = t.cloneNode(fn, true);
const statement = t.isFunctionDeclaration(copy) ? copy : t.expressionStatement(copy);
const frame = scope.record.chain.findIndex((item) =>
[...item.decls.values()].some((decl) => decl.kind === 'node' && decl.node === declaration),
);
if (frame < 0) return null;
const chain = scope.record.chain.slice(0, frame + 1);
let closed = true;
traverse(t.file(t.program([statement])), {
ReferencedIdentifier(path) {
if (path.scope.getBinding(path.node.name)) return;
const decl = resolveName(path.node.name, chain);
if (decl?.kind === 'import') path.node.name = `<${decl.key}>`;
else closed = false;
},
});
return closed ? sha1(generate(copy, { compact: true, comments: false }).code) : null;
}
function containsRegExp(value: unknown): boolean {
if (value === null || typeof value !== 'object') return false;
if (Array.isArray(value)) return value.some(containsRegExp);
const record = value as Record<string, unknown>;
return record.kind === 'regexp' || Object.values(record).some(containsRegExp);
}
function preserveValues(node: t.Node, scope: Scope, abstractInputs = false): t.Node {
const value = literal(node, scope);
if (value !== undefined) {
const marker = t.identifier('_value');
marker.name = `<value:${JSON.stringify(value)}>`;
return marker;
}
if (t.isIdentifier(node) && !scope.locals.has(node.name)) {
const decl = resolveName(node.name, scope.record.chain);
if (decl?.kind === 'import') {
const marker = t.identifier('_import');
marker.name = `<${decl.key}>`;
return marker;
}
if (decl?.kind === 'node') {
const fn = t.isVariableDeclarator(decl.node) ? decl.node.init : decl.node;
if (
t.isFunctionDeclaration(fn) ||
t.isFunctionExpression(fn) ||
t.isArrowFunctionExpression(fn)
) {
const marker = t.identifier('_local');
const identity = helperIdentity(fn, decl.node, scope);
marker.name =
identity === null
? `<local:${scope.record.file}:${decl.node.start}:${node.name}>`
: `<helper:${identity}>`;
return marker;
}
// Frames do not prove constant values or immutability. Preserve the binding,
// so repeated reads can match without equating names in different scopes.
const marker = t.identifier('_local');
marker.name = `<local:${scope.record.file}:${decl.node.start}:${node.name}>`;
return marker;
}
}
if (t.isTemplateLiteral(node)) {
const marker = t.identifier('_template');
marker.name = '<template>';
return t.callExpression(
marker,
node.quasis.flatMap((quasi, index) => [
preserveValues(
t.stringLiteral(quasi.value.cooked ?? quasi.value.raw),
scope,
abstractInputs,
),
...(node.expressions[index]
? [preserveValues(node.expressions[index], scope, abstractInputs) as t.Expression]
: []),
]) as t.Expression[],
);
}
const copy = t.cloneNode(node, false);
const object = copy as unknown as Record<string, unknown>;
const literalInputs =
abstractInputs &&
t.isCallExpression(node) &&
t.isIdentifier(node.callee) &&
!scope.locals.has(node.callee.name) &&
resolveName(node.callee.name, scope.record.chain)?.kind === 'import' &&
node.arguments.every((arg) => {
const value = literal(arg, scope);
return value !== undefined && !containsRegExp(value);
});
for (const key of t.VISITOR_KEYS[node.type] ?? []) {
if (
(key === 'property' &&
(t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) &&
!node.computed) ||
(key === 'key' && t.isObjectProperty(node) && !node.computed)
)
continue;
const child = object[key];
object[key] = Array.isArray(child)
? child.map((v) =>
!t.isNode(v)
? v
: literalInputs && key === 'arguments'
? abstractNode(v, scope, false, false)
: preserveValues(v, scope, abstractInputs),
)
: t.isNode(child)
? preserveValues(child, scope, abstractInputs)
: child;
}
return copy;
}
function assertedSubject(call: t.CallExpression): t.Node | null {
let node: t.Node = call.callee;
while (t.isMemberExpression(node)) node = node.object;
if (!t.isCallExpression(node)) return null;
if (
!t.isIdentifier(node.callee, { name: 'expect' }) &&
!(
t.isMemberExpression(node.callee) &&
!node.callee.computed &&
t.isIdentifier(node.callee.object, { name: 'expect' }) &&
t.isIdentifier(node.callee.property, { name: 'soft' })
)
)
return null;
const first = node.arguments[0];
return first && !t.isSpreadElement(first) && !t.isArgumentPlaceholder(first) ? first : null;
}
/** Retain exact expected literals; never equate assertions solely because both expect a number. */
export function assertionEvidence(
subject: CheckSubject,
checks: InstanceChecks,
): AssertionEvidence {
const scope = buildScope(subject);
const unresolved = [
...checks.unresolved,
...checks.unsupported.map((form) => `${form.line}: ${form.reason}`),
];
const atoms: AssertionAtom[] = [];
let origins: ReturnType<typeof localOrigins> | undefined;
for (const check of checks.checks) {
if (check.matcher === '') continue; // Counts of assertions are not behavioral checks.
const call = assertionCallOf(check);
if (call === null) {
unresolved.push(`${check.line}: assertion call not located`);
continue;
}
const expand = (origins ??= localOrigins(subject, scope));
const before = call.start ?? 0;
const values = call.arguments.map((arg) => literal(expand(arg, before), scope));
const expected =
check.snapshot !== null
? `snapshot:${check.snapshot}`
: values.includes(undefined)
? null
: JSON.stringify(values);
if (expected === null) unresolved.push(`${check.line}: expected value is not static`);
// Existing check.shape has the assertion parser's selector/negation rules.
const observed = assertedSubject(call);
const expanded = observed === null ? null : expand(observed, before);
const originNode =
expanded === null ? null : abstractNode(preserveValues(expanded, scope), scope, true, false);
let knownOrigin = originNode !== null;
if (originNode !== null)
t.traverseFast(originNode, (node) => {
if (t.isIdentifier(node) && /^(?:<v\d+>|<unknown>)$/.test(node.name)) knownOrigin = false;
});
const origin = originNode === null ? null : generate(originNode, { compact: true }).code;
const subjectShape =
expanded === null || !knownOrigin
? check.shape
: generate(abstractNode(preserveValues(expanded, scope, true), scope, true, false), {
compact: true,
}).code;
if (!knownOrigin) unresolved.push(`${check.line}: asserted local origin is unresolved`);
const shapeKey = sha1(
JSON.stringify([
knownOrigin ? origin : check.shape,
check.form,
check.modifiers,
check.matcher,
]),
);
atoms.push({
line: check.line,
shape: check.shape,
shapeKey,
subjectShapeKey: sha1(
JSON.stringify([subjectShape, check.form, check.modifiers, check.matcher]),
),
expected,
observed: origin,
key:
expected === null || !knownOrigin
? null
: sha1(JSON.stringify([origin, check.form, check.modifiers, check.matcher, expected])),
});
}
return { atoms, unresolved: sortedUnique([...unresolved, ...scope.unresolved]) };
}