mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-11-29 23:37:41 +00:00
* 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>
318 lines
13 KiB
TypeScript
318 lines
13 KiB
TypeScript
/** Fixture similarity is a retrieval hint, never evidence of equivalent test inputs. */
|
|
import path from 'node:path';
|
|
import type Parser from 'tree-sitter';
|
|
import { SupportedLanguages } from 'gitnexus-shared';
|
|
import { getProviderForFileContent } from '../../../src/core/ingestion/languages/index.js';
|
|
import { extractParsedFile } from '../../../src/core/ingestion/scope-extractor-bridge.js';
|
|
import { collectFunctionCfgs } from '../../../src/core/ingestion/cfg/collect.js';
|
|
import { extractVueScript } from '../../../src/core/ingestion/vue-sfc-extractor.js';
|
|
import { getTreeSitterBufferSize } from '../../../src/core/ingestion/constants.js';
|
|
import { createParserForLanguage } from '../../../src/core/tree-sitter/parser-loader.js';
|
|
import { byText, sha1, sortedUnique } from '../util.js';
|
|
import { MAX_SOURCE_CHARS, type SourceReader } from './source-shape.js';
|
|
|
|
export interface FixtureProfile {
|
|
/** Keys into the coverage artifact's immutable fixture source table, when captured. */
|
|
sourceKeys?: string[];
|
|
/** Repository paths, retained for a reviewer to inspect. */
|
|
roots: string[];
|
|
files: string[];
|
|
languages: string[];
|
|
/** All bytes AND relative paths, including non-source assets; null if incomplete. */
|
|
contentKey: string | null;
|
|
/** Language-neutral, counted features from ingestion providers. */
|
|
features: [string, number][];
|
|
/** Extraction gaps. A partial profile cannot retrieve structural matches. */
|
|
unresolved: string[];
|
|
}
|
|
|
|
export interface FixtureSimilarity {
|
|
kind: 'content' | 'structure';
|
|
score: number;
|
|
crossLanguage: boolean;
|
|
}
|
|
|
|
const MAX_FILES = 200;
|
|
const MAX_TOTAL_CHARS = 2_000_000;
|
|
export const FIXTURE_SIMILARITY_THRESHOLD = 0.9;
|
|
|
|
/** Run-local cache: one parse per file, including fixtures shared by hundreds of cases. */
|
|
export class FixtureProfiler {
|
|
private readonly files = new Map<
|
|
string,
|
|
Promise<{
|
|
content: string | null;
|
|
completeContent: boolean;
|
|
language: string | null;
|
|
features: string[];
|
|
unresolved: string[];
|
|
}>
|
|
>();
|
|
private readonly parsers = new Map<string, Promise<Parser>>();
|
|
private readonly profiles = new Map<string, Promise<FixtureProfile>>();
|
|
|
|
constructor(private readonly reader: SourceReader) {}
|
|
|
|
profile(roots: readonly string[]): Promise<FixtureProfile> {
|
|
const normalized = sortedUnique(roots).filter(
|
|
(root, _, all) => !all.some((other) => root.startsWith(`${other}/`)),
|
|
);
|
|
const key = JSON.stringify(normalized);
|
|
let result = this.profiles.get(key);
|
|
if (result === undefined) {
|
|
result = this.build(normalized);
|
|
this.profiles.set(key, result);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private async read(file: string) {
|
|
let result = this.files.get(file);
|
|
if (result !== undefined) return result;
|
|
result = (async () => {
|
|
// Fixture trees can contain credential-shaped samples; never read a real .env.
|
|
const excluded =
|
|
/^\.env(?:\.|$)/.test(path.posix.basename(file)) && !file.endsWith('.example');
|
|
const content = excluded ? null : this.reader.read(file);
|
|
const features: string[] = [];
|
|
const unresolved: string[] = [];
|
|
const provider = content === null ? null : getProviderForFileContent(file, content);
|
|
const language = provider?.id ?? null;
|
|
if (content === null) unresolved.push(`${file}: unreadable`);
|
|
else if (content.length >= MAX_SOURCE_CHARS) unresolved.push(`${file}: size limit`);
|
|
else if (content.includes('\uFFFD')) unresolved.push(`${file}: text decoding may be lossy`);
|
|
else if (provider !== null && content.trim() !== '') {
|
|
let tree: Parser.Tree | undefined;
|
|
try {
|
|
const embedded =
|
|
provider.id === SupportedLanguages.Vue ? extractVueScript(content) : null;
|
|
const parseContent = embedded?.scriptContent ?? content;
|
|
// Standalone providers (e.g. COBOL) own parsing. Providers with CFG support
|
|
// share this tree between capture extraction and control-flow extraction.
|
|
if (provider.cfgVisitor !== undefined) {
|
|
const parserKey = `${provider.id}:${path.posix.extname(file)}:${embedded?.lang ?? ''}`;
|
|
let parser = this.parsers.get(parserKey);
|
|
if (parser === undefined) {
|
|
parser = createParserForLanguage(provider.id, file);
|
|
this.parsers.set(parserKey, parser);
|
|
}
|
|
const nativeParser = await parser;
|
|
const grammar =
|
|
embedded === null ? undefined : provider.selectEmbeddedGrammar?.(embedded.lang);
|
|
if (grammar !== undefined) nativeParser.setLanguage(grammar);
|
|
const text = provider.preprocessSource?.(parseContent, file) ?? parseContent;
|
|
tree = nativeParser.parse(text, null, { bufferSize: getTreeSitterBufferSize(text) });
|
|
if (tree.rootNode.hasError) unresolved.push(`${file}: parse errors`);
|
|
}
|
|
const parsed = extractParsedFile(
|
|
provider,
|
|
parseContent,
|
|
file,
|
|
(warning) => unresolved.push(warning),
|
|
tree,
|
|
embedded === null ? 'full-file' : 'pre-extracted-script',
|
|
undefined,
|
|
embedded?.lineOffset,
|
|
embedded?.lang,
|
|
embedded?.sourceLineMap,
|
|
);
|
|
if (parsed === undefined) unresolved.push(`${file}: no scope captures`);
|
|
else {
|
|
const scopes = new Map(parsed.scopes.map((scope) => [scope.id, scope]));
|
|
const owners = new Map(
|
|
parsed.scopes.flatMap((scope) =>
|
|
scope.ownedDefs.map((def) => [def.nodeId, scope.kind] as const),
|
|
),
|
|
);
|
|
for (const def of parsed.localDefs) {
|
|
features.push(
|
|
`def:${def.type}:owner=${owners.get(def.nodeId) ?? '?'}:arity=${def.parameterCount ?? '?'}`,
|
|
);
|
|
}
|
|
// Ownership and call form matter; declaration spelling and source language do not.
|
|
for (const ref of parsed.referenceSites) {
|
|
if (ref.kind === 'read' || ref.kind === 'write' || ref.kind === 'type-reference')
|
|
continue;
|
|
features.push(
|
|
`ref:${scopes.get(ref.inScope)?.kind ?? '?'}:${ref.kind}:${ref.callForm ?? '-'}:${ref.arity ?? '?'}`,
|
|
);
|
|
}
|
|
for (const entry of parsed.parsedImports) features.push(`import:${entry.kind}`);
|
|
}
|
|
if (tree !== undefined && provider.cfgVisitor !== undefined) {
|
|
const { cfgs, skipped } = collectFunctionCfgs(
|
|
tree.rootNode,
|
|
provider.cfgVisitor,
|
|
file,
|
|
2000,
|
|
);
|
|
if (Object.values(skipped).some((count) => count > 0))
|
|
unresolved.push(`${file}: incomplete control flow`);
|
|
for (const cfg of cfgs) {
|
|
// Refinement by directed, labeled neighbors preserves branch/loop topology,
|
|
// independent of block ids, line numbers, names and literal values.
|
|
let colors = new Map(cfg.blocks.map((block) => [block.index, block.kind as string]));
|
|
const outgoing = new Map<number, { kind: string; other: number }[]>();
|
|
const incoming = new Map<number, { kind: string; other: number }[]>();
|
|
for (const edge of cfg.edges) {
|
|
const from = outgoing.get(edge.from) ?? [];
|
|
from.push({ kind: edge.kind, other: edge.to });
|
|
outgoing.set(edge.from, from);
|
|
const to = incoming.get(edge.to) ?? [];
|
|
to.push({ kind: edge.kind, other: edge.from });
|
|
incoming.set(edge.to, to);
|
|
}
|
|
for (let round = 0; round < 3; round++) {
|
|
const previous = colors;
|
|
colors = new Map(
|
|
cfg.blocks.map((block) => [
|
|
block.index,
|
|
sha1(
|
|
JSON.stringify([
|
|
previous.get(block.index),
|
|
(outgoing.get(block.index) ?? [])
|
|
.map((edge) => `${edge.kind}:${previous.get(edge.other)}`)
|
|
.sort(),
|
|
(incoming.get(block.index) ?? [])
|
|
.map((edge) => `${edge.kind}:${previous.get(edge.other)}`)
|
|
.sort(),
|
|
]),
|
|
),
|
|
]),
|
|
);
|
|
}
|
|
features.push(`cfg:${sha1(JSON.stringify([...colors.values()].sort()))}`);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
unresolved.push(`${file}: ${error instanceof Error ? error.message : String(error)}`);
|
|
}
|
|
}
|
|
const completeContent =
|
|
content !== null && content.length < MAX_SOURCE_CHARS && !content.includes('\uFFFD');
|
|
return { content, completeContent, language, features, unresolved };
|
|
})();
|
|
this.files.set(file, result);
|
|
return result;
|
|
}
|
|
|
|
private async build(roots: string[]): Promise<FixtureProfile> {
|
|
const files = sortedUnique(
|
|
roots.flatMap((root) => {
|
|
const children = this.reader.list(root);
|
|
return children.length > 0 ? children : [root];
|
|
}),
|
|
);
|
|
const unresolved: string[] = [];
|
|
const counts = new Map<string, number>();
|
|
const identities: string[][] = [];
|
|
const languages = new Set<string>();
|
|
if (files.length > MAX_FILES)
|
|
unresolved.push(`fixture file limit (${files.length} > ${MAX_FILES})`);
|
|
let completeContent = files.length <= MAX_FILES;
|
|
let chars = 0;
|
|
for (const file of files.slice(0, MAX_FILES)) {
|
|
const item = await this.read(file);
|
|
completeContent &&= item.completeContent;
|
|
chars += item.content?.length ?? 0;
|
|
if (chars > MAX_TOTAL_CHARS) {
|
|
unresolved.push('fixture total size limit');
|
|
completeContent = false;
|
|
break;
|
|
}
|
|
unresolved.push(...item.unresolved);
|
|
if (item.language !== null) languages.add(item.language);
|
|
if (item.content !== null) {
|
|
const root = roots.find((root) => file === root || file.startsWith(`${root}/`));
|
|
if (root === undefined) {
|
|
unresolved.push(`${file}: outside fixture roots`);
|
|
completeContent = false;
|
|
continue;
|
|
}
|
|
identities.push([
|
|
String(roots.indexOf(root)),
|
|
file === root ? path.posix.basename(file) : path.posix.relative(root, file),
|
|
sha1(item.content),
|
|
]);
|
|
}
|
|
for (const feature of item.features) counts.set(feature, (counts.get(feature) ?? 0) + 1);
|
|
}
|
|
return {
|
|
roots,
|
|
files,
|
|
languages: [...languages].sort(),
|
|
contentKey: completeContent ? sha1(JSON.stringify(identities)) : null,
|
|
features: [...counts].sort(([a], [b]) => byText(a, b)),
|
|
unresolved: sortedUnique(unresolved),
|
|
};
|
|
}
|
|
}
|
|
|
|
/** Counted Jaccard. CFG topology carries extra weight so a loop does not look like a branch. */
|
|
export function compareFixtureProfiles(
|
|
a: FixtureProfile,
|
|
b: FixtureProfile,
|
|
): FixtureSimilarity | null {
|
|
if (a.files.length === 0 || b.files.length === 0) return null;
|
|
const crossLanguage = a.languages.join() !== b.languages.join();
|
|
if (a.contentKey !== null && a.contentKey === b.contentKey)
|
|
return { kind: 'content', score: 1, crossLanguage };
|
|
if (
|
|
a.unresolved.length > 0 ||
|
|
b.unresolved.length > 0 ||
|
|
a.features.length === 0 ||
|
|
b.features.length === 0
|
|
)
|
|
return null;
|
|
const right = new Map(b.features);
|
|
let shared = 0;
|
|
let total = 0;
|
|
for (const [feature, count] of a.features) {
|
|
const other = right.get(feature) ?? 0;
|
|
const weight = feature.startsWith('cfg:') ? 4 : 1;
|
|
shared += Math.min(count, other) * weight;
|
|
total += Math.max(count, other) * weight;
|
|
right.delete(feature);
|
|
}
|
|
for (const [feature, count] of right) total += count * (feature.startsWith('cfg:') ? 4 : 1);
|
|
const score = total === 0 ? 0 : shared / total;
|
|
return score >= FIXTURE_SIMILARITY_THRESHOLD ? { kind: 'structure', score, crossLanguage } : null;
|
|
}
|
|
|
|
/** Reviewable fixture evidence, deduplicated across the tests that share setup. */
|
|
export function fixtureSimilarityReport(
|
|
tests: readonly { id: string; fixture?: FixtureProfile | null }[],
|
|
pairs: readonly { a: string; b: string; fixtureSimilarity?: FixtureSimilarity }[],
|
|
) {
|
|
const profiles = new Map<string, { profile: FixtureProfile; tests: string[] }>();
|
|
for (const test of tests) {
|
|
if (test.fixture == null) continue;
|
|
const key = JSON.stringify(test.fixture);
|
|
const entry = profiles.get(key) ?? { profile: test.fixture, tests: [] };
|
|
entry.tests.push(test.id);
|
|
profiles.set(key, entry);
|
|
}
|
|
const matches = pairs
|
|
.filter((pair) => pair.fixtureSimilarity !== undefined)
|
|
.map((pair) => ({
|
|
a: pair.a,
|
|
b: pair.b,
|
|
...pair.fixtureSimilarity,
|
|
}));
|
|
return {
|
|
version: 1,
|
|
basis:
|
|
'Pre-resolution declarations, references, and control-flow topology. Similarity is not semantic equivalence or permission to remove a test.',
|
|
threshold: FIXTURE_SIMILARITY_THRESHOLD,
|
|
summary: {
|
|
tests: tests.length,
|
|
withFixtureInputs: tests.filter((test) => (test.fixture?.files.length ?? 0) > 0).length,
|
|
unresolvedTests: tests.filter((test) => (test.fixture?.unresolved.length ?? 0) > 0).length,
|
|
profiles: profiles.size,
|
|
matches: matches.length,
|
|
crossLanguageMatches: matches.filter((match) => match.crossLanguage).length,
|
|
},
|
|
profiles: [...profiles.values()],
|
|
matches,
|
|
};
|
|
}
|