From 9a6c001df38ce5afb0e879f33911e9a32f8d5d27 Mon Sep 17 00:00:00 2001 From: Gergo Magyar Date: Tue, 27 Oct 2026 18:51:00 +0000 Subject: [PATCH 01/10] 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. --- .../fixtures/scenario-map-recall/audit.json | 566 ++++++++++++++++++ .../test/unit/scenario-map/candidates.test.ts | 18 +- gitnexus/test/unit/scenario-map/cli.test.ts | 37 +- .../scenario-map/coverage-inventory.test.ts | 69 +++ .../unit/scenario-map/fingerprint.test.ts | 60 ++ .../test/unit/scenario-map/partitions.test.ts | 70 +++ .../unit/scenario-map/recall-audit.test.ts | 53 ++ .../test/unit/scenario-map/report.test.ts | 2 +- gitnexus/tools/scenario-map/facet/body-key.ts | 8 + .../inventory/coverage-inventory.ts | 60 ++ .../scenario-map/path-coverage/candidates.ts | 31 +- .../path-coverage/coverage.setup.ts | 8 +- .../scenario-map/path-coverage/partitions.ts | 63 +- .../scenario-map/path-coverage/prefilter.ts | 26 +- .../path-coverage/run-coverage.ts | 2 + .../tools/scenario-map/report/markdown.ts | 26 +- gitnexus/tools/scenario-map/report/report.ts | 10 +- gitnexus/tools/scenario-map/scenario-map.ts | 57 +- .../tools/scenario-map/verdict/fingerprint.ts | 66 +- .../tools/scenario-map/verdict/verdict.ts | 2 + 20 files changed, 1157 insertions(+), 77 deletions(-) create mode 100644 gitnexus/test/fixtures/scenario-map-recall/audit.json create mode 100644 gitnexus/test/unit/scenario-map/coverage-inventory.test.ts create mode 100644 gitnexus/test/unit/scenario-map/recall-audit.test.ts create mode 100644 gitnexus/tools/scenario-map/facet/body-key.ts create mode 100644 gitnexus/tools/scenario-map/inventory/coverage-inventory.ts diff --git a/gitnexus/test/fixtures/scenario-map-recall/audit.json b/gitnexus/test/fixtures/scenario-map-recall/audit.json new file mode 100644 index 000000000..9a96d7f82 --- /dev/null +++ b/gitnexus/test/fixtures/scenario-map-recall/audit.json @@ -0,0 +1,566 @@ +{ + "revision": "f354e66ff4a9515c8e4b33c4d39a94fc56846444", + "method": "Independently generated callback candidates; source review of setup, helper targets, fixture paths, inputs and assertions. Hand-adjudicated subset, not an exhaustive semantic oracle.", + "positives": [ + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Java > detects User class with save and getName methods", + "b": "test/integration/resolvers/java.test.ts#Java cross-file binding propagation > detects User class with save and getName methods", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Kotlin > detects User class with save and getName methods", + "b": "test/integration/resolvers/kotlin.test.ts#Kotlin cross-file binding propagation > detects User class with save and getName methods", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: JavaScript > detects User class with save and getName methods", + "b": "test/integration/resolvers/javascript.test.ts#JavaScript cross-file binding propagation > detects User class with save and getName methods", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: PHP > detects User class with save and getName methods", + "b": "test/integration/resolvers/php.test.ts#PHP cross-file binding propagation > detects User class with save and getName methods", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Rust > resolves u.save() in process() to User#save via cross-file return type", + "b": "test/integration/resolvers/rust.test.ts#Rust cross-file binding propagation > resolves u.save() in process() to User#save via cross-file return type propagation", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Ruby > resolves user.save in process() to User#save via cross-file return type", + "b": "test/integration/resolvers/ruby.test.ts#Ruby cross-file binding propagation > resolves user.save in process to User#save via cross-file propagation", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/resolvers/rust-scope.test.ts#Rust scope: impl block method classification > classifies save as a function owned by User (HAS_METHOD edge)", + "b": "test/integration/resolvers/rust-scope.test.ts#Rust scope: impl block method classification > emits HAS_METHOD edge from User to save", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Kotlin > resolves u.save() in run() to User#save via cross-file return type", + "b": "test/integration/resolvers/kotlin.test.ts#Kotlin cross-file binding propagation > resolves u.save() in run() to User#save via cross-file return type propagation", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Python > resolves u.save() in run() to User#save via cross-file return type", + "b": "test/integration/resolvers/python.test.ts#Python cross-file binding propagation > resolves u.save() in run() to User#save via cross-file return type propagation", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: JavaScript > resolves u.save() in run() to User#save via cross-file return type", + "b": "test/integration/resolvers/javascript.test.ts#JavaScript cross-file binding propagation > resolves u.save() in run() to User#save via cross-file return type propagation", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: C++ > detects User class with save and get_name methods", + "b": "test/integration/resolvers/cpp.test.ts#C++ cross-file binding propagation > detects User class with save and get_name methods", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Ruby > detects User class with save and get_name methods", + "b": "test/integration/resolvers/ruby.test.ts#Ruby cross-file binding propagation > detects User class with save and get_name methods", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > bare [] prefix with spaces only \u2192 undefined", + "b": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > empty array suffix \u2192 undefined", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > bare [] prefix with spaces only \u2192 undefined", + "b": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > [] suffix with no base \u2192 undefined", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > bare [] prefix with spaces only \u2192 undefined", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > returns undefined for just brackets: []", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > empty array suffix \u2192 undefined", + "b": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > [] suffix with no base \u2192 undefined", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > empty array suffix \u2192 undefined", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > returns undefined for just brackets: []", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > [] suffix with no base \u2192 undefined", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > returns undefined for just brackets: []", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Rust > detects get_user function and process function", + "b": "test/integration/resolvers/rust.test.ts#Rust cross-file binding propagation > detects get_user and process functions", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/resolvers/java.test.ts#Java same-arity overload cross-file and chain resolution > crossFileByName() emits exactly one CALLS edge to find(String) in DbLookup", + "b": "test/integration/resolvers/java.test.ts#Java same-arity overloads (worker path) > crossFileByName() \u2192 find(String) \u2014 sequential path", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/resolvers/java.test.ts#Java same-arity overload cross-file and chain resolution > emits METHOD_IMPLEMENTS from DbLookup.find(int) \u2192 ILookup.find(int)", + "b": "test/integration/resolvers/java.test.ts#Java same-arity overloads (worker path) > METHOD_IMPLEMENTS edges match interface methods \u2014 sequential path", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/unit/pdg-mode-flip.test.ts#pdgModeMismatch \u2014 M1\u2192M2 stamp upgrade (#2082 M2, pure) > an identical resolved M2 config compares equal (steady state keeps incremental)", + "b": "test/unit/pdg-mode-flip.test.ts#pdgModeMismatch \u2014 M2\u2192M3 stamp upgrade (#2083 M3 U5, pure) > an identical resolved M3 config compares equal (steady state keeps incremental)", + "category": "import-initialization", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/unit/pdg-mode-flip.test.ts#pdgModeMismatch \u2014 M1\u2192M2 stamp upgrade (#2082 M2, pure) > an identical resolved M2 config compares equal (steady state keeps incremental)", + "b": "test/unit/pdg-mode-flip.test.ts#pdgModeMismatch \u2014 pre-#2201\u2192SSA reaching-defs solver upgrade (#2201 review R3, pure) > an identical post-#2201 stamp compares equal (no spurious re-analysis churn)", + "category": "import-initialization", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/unit/pdg-mode-flip.test.ts#pdgModeMismatch \u2014 M2\u2192M3 stamp upgrade (#2083 M3 U5, pure) > an identical resolved M3 config compares equal (steady state keeps incremental)", + "b": "test/unit/pdg-mode-flip.test.ts#pdgModeMismatch \u2014 pre-#2201\u2192SSA reaching-defs solver upgrade (#2201 review R3, pure) > an identical post-#2201 stamp compares equal (no spurious re-analysis churn)", + "category": "import-initialization", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Python > detects User class with save and get_name methods", + "b": "test/integration/resolvers/python.test.ts#Python cross-file binding propagation > detects User class with save and get_name methods", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Python > detects get_user function and run function", + "b": "test/integration/resolvers/python.test.ts#Python cross-file binding propagation > detects get_user and run functions", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Python > resolves u.get_name() in run() to User#get_name via cross-file return type", + "b": "test/integration/resolvers/python.test.ts#Python cross-file binding propagation > resolves u.get_name() in run() to User#get_name via cross-file return type propagation", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Go > detects User struct with Save and GetName methods", + "b": "test/integration/resolvers/go.test.ts#Go cross-file binding propagation > detects User struct with Save and GetName methods", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Go > resolves user.Save() in main() to User#Save via cross-file return type", + "b": "test/integration/resolvers/go.test.ts#Go cross-file binding propagation > resolves user.Save() in main() to User#Save via cross-file propagation", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: Rust > detects User struct with save and get_name methods", + "b": "test/integration/resolvers/rust.test.ts#Rust cross-file binding propagation > detects User struct with save and get_name methods", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: JavaScript > detects getUser factory and run function", + "b": "test/integration/resolvers/javascript.test.ts#JavaScript cross-file binding propagation > detects getUser and run functions", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/cross-file-binding.test.ts#Phase 9 \u2014 Cross-File Call-Result Binding: C# > detects User class with Save and GetName methods", + "b": "test/integration/resolvers/csharp.test.ts#C# cross-file binding propagation > detects User class with Save and GetName methods", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/resolvers/java.test.ts#Java same-arity overload cross-file and chain resolution > crossFileById() emits exactly one CALLS edge to find(int) in DbLookup", + "b": "test/integration/resolvers/java.test.ts#Java same-arity overloads (worker path) > crossFileById() \u2192 find(int) \u2014 sequential path", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/resolvers/javascript-object-properties.test.ts#JavaScript plain-object property access (A1/A5) > emits NOTHING when two objects share the key name", + "b": "test/integration/resolvers/javascript-object-properties.test.ts#JavaScript plain-object property access (A1/A5) > scope narrowing for multi-candidate names (R2) > keeps refusing two same-named keys in the reading file", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/resolvers/rust.test.ts#Rust qualified paths resolve against the module tree (#2730) > ignores a function-local fn of the same name in the target module (H3)", + "b": "test/integration/resolvers/rust.test.ts#Rust qualified paths resolve against the module tree (#2730) > follows a `pub use` re-export through to the original definition", + "category": "integration-same-fixture", + "basis": "Same fixture directory or shared PipelineResult, identical runPipelineFromRepo setup and matching assertions. CROSS_FILE_FIXTURES resolves to the same directory through both definitions." + }, + { + "a": "test/integration/tree-sitter-languages.test.ts#Tree-sitter multi-language parsing > unhappy path > handles empty string file path", + "b": "test/unit/ingestion-utils.test.ts#getLanguageFromFilename > unsupported > returns null for empty string", + "category": "irrelevant-setup", + "basis": "Both import getLanguageFromFilename from gitnexus-shared, pass empty string and assert toBeNull. Loading a parser in an unrelated beforeAll does not change this pure function." + }, + { + "a": "test/unit/analyze-api.test.ts#analyze API logic > rejects when job already active for different repo", + "b": "test/unit/analyze-job.test.ts#JobManager > enforces single-slot concurrency", + "category": "cleanup-context", + "basis": "Both instantiate JobManager before each test, use identical URLs and job transitions, and assert the same result. Extra mock/timer restoration happens after the assertions." + }, + { + "a": "test/unit/analyze-api.test.ts#analyze API logic > returns existing job for same repo URL", + "b": "test/unit/analyze-job.test.ts#JobManager > returns existing job for same repoUrl when active", + "category": "cleanup-context", + "basis": "Both instantiate JobManager before each test, use identical URLs and job transitions, and assert the same result. Extra mock/timer restoration happens after the assertions." + }, + { + "a": "test/unit/cobol-preprocessor.test.ts#extractCobolSymbolsWithRegex > Linkage & Data Flow > PROCEDURE DIVISION USING RETURNING excludes return value from USING list", + "b": "test/unit/cobol-preprocessor.test.ts#extractCobolSymbolsWithRegex > PROCEDURE DIVISION USING edge cases > excludes RETURNING value from USING parameter list", + "category": "same-file", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/unit/embedder.test.ts#embedder > getEmbeddingDims > returns 384 (MiniLM default)", + "b": "test/unit/http-embedder.test.ts#HTTP embedding backend > MCP embedder > returns 384 dimensions by default", + "category": "cleanup-context", + "basis": "Same getEmbeddingDims import, default environment and 384 assertion; additional afterEach restoration adds no scenario input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > array suffix (TypeScript / Java / C#) > User[] \u2192 User", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > strips array suffix: User[] \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > array suffix (TypeScript / Java / C#) > string[] \u2192 string", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > strips array suffix for primitive: string[] \u2192 string", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > Go slice prefix > []User \u2192 User", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > strips Go slice prefix: []User \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > Go slice prefix > []string \u2192 string", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > strips Go slice prefix for primitive: []string \u2192 string", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > Swift array sugar > [User] \u2192 User", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > unwraps Swift array sugar: [User] \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > generic angle-bracket containers > Array \u2192 User", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > unwraps single-arg generic angle: Array \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > generic angle-bracket containers > Vec \u2192 User (Rust)", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > unwraps single-arg generic angle: Rust Vec \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > generic angle-bracket containers > vector \u2192 User (C++)", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > unwraps single-arg generic angle: C++ vector \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > Python subscript-style generics > List[User] \u2192 User", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > unwraps Python subscript: List[User] \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > multi-argument generics \u2014 default returns last (value) arg > Map \u2192 User (default: last/value arg)", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > returns last arg by default for Map: \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > multi-argument generics \u2014 default returns last (value) arg > Map \u2192 String (pos=first: key arg)", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > returns first arg with pos=first for Map: \u2192 String", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > multi-argument generics \u2014 default returns last (value) arg > Map> \u2192 undefined (last arg is nested generic)", + "b": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > cross-bracket nesting (bracket depth fix) > Map> \u2192 undefined (default: last arg is nested generic)", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > multi-argument generics \u2014 default returns last (value) arg > Map> \u2192 String (pos=first: key arg)", + "b": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > cross-bracket nesting (bracket depth fix) > Map> \u2192 String (pos=first: key arg)", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > empty string \u2192 undefined", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > returns undefined for empty string", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/extract-element-type-from-string.test.ts#extractElementTypeFromString > edge cases \u2014 return undefined > plain type name (no container) \u2192 undefined", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > returns undefined when no brackets are present: User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/group/storage.test.ts#Group storage > getGroupDir returns correct path for group name", + "b": "test/unit/group/storage.test.ts#Group storage > getGroupDir rejects invalid names > test_getGroupDir_valid_name_returns_path", + "category": "same-file", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/unit/has-method.test.ts#findEnclosingClassId > Rust > finds enclosing impl_item for a method", + "b": "test/unit/has-method.test.ts#findEnclosingClassId > Rust > still picks struct name for plain impl Struct (no trait)", + "category": "native-parser", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/unit/index-repo-command.test.ts#indexCommand > fails when meta.json is missing and --force is not set", + "b": "test/unit/index-repo-command.test.ts#indexCommand > fails without --force when LadybugDB exists but metadata is missing", + "category": "same-file", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/unit/model/registration-table.test.ts#behavior group isolation > Impl hooks write to types.registerImpl, never to types.registerClass", + "b": "test/unit/model/registration-table.test.ts#hook behavior (real registries, no mocks) > implHook writes to types.registerImpl, NOT types.registerClass", + "category": "same-file", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/unit/scope-resolution/function-local-import-chain.test.ts#Python: a function-local import reaches the IMPORTS reason > `def f(): from m import X` is deferred", + "b": "test/unit/scope-resolution/function-local-import-chain.test.ts#Rust: a function-local `use` is NOT deferred > Python still defers on the same run", + "category": "native-parser", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/unit/security.test.ts#path traversal (isTestFilePath as proxy for path handling) > isTestFilePath returns false for nodes without a filePath", + "b": "test/unit/test-file-path.test.ts#isTestFilePath \u2014 shared predicate > returns false for nullish input rather than throwing", + "category": "reexport-context", + "basis": "local-backend.ts imports and directly re-exports the same isTestFilePath function. Both tests pass undefined, null and empty string and assert false three times." + }, + { + "a": "test/unit/server.test.ts#server error handling > createMCPServer reads version from package.json", + "b": "test/unit/server.test.ts#prompt registration > server registers detect_impact and generate_map prompts", + "category": "same-file", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > returns undefined when base is not a simple word: user model[]", + "b": "test/unit/shared-type-extractors.test.ts#extractElementTypeFromString > returns undefined for complex non-word base before []: user model[]", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > strips \"| null\": User | null \u2192 User", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > strips User | null \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > strips \"| undefined\": User | undefined \u2192 User", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > strips User | undefined \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > strips both \"| null | undefined\": User | null | undefined \u2192 User", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > strips User | null | undefined \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > strips nullable suffix: User? \u2192 User", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > strips User? \u2192 User", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > returns undefined for genuine union: User | Repo", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > refuses genuine union User | Repo \u2192 undefined", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > returns undefined for bare \"null\"", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > returns undefined for null alone", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > returns undefined for bare \"undefined\"", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > returns undefined for undefined alone", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > returns undefined for bare \"None\"", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > returns undefined for None alone", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > returns undefined for bare \"nil\"", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > returns undefined for nil alone", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > returns undefined for bare \"void\"", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > returns undefined for void alone", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > returns undefined for empty string", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > returns undefined for empty string", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/shared-type-extractors.test.ts#stripNullable > returns the type name unchanged when no nullable markers: User \u2192 User", + "b": "test/unit/type-env.test.ts#buildTypeEnv > stripNullable > passes through User unchanged", + "category": "pure-helper", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/stdout-silence.test.ts#stdout silencing > handles nested silence/restore (reference counting)", + "b": "test/unit/stdout-silence.test.ts#stdout silencing > simulates embedder + pool-adapter concurrent usage without conflict", + "category": "same-file", + "basis": "Identical scenario body in one file, using the same helpers, input data and assertions. Enclosing setup has no relevant difference." + }, + { + "a": "test/unit/type-env.test.ts#buildTypeEnv > constructor inference (Tier 1 fallback) > Rust if-let / while-let pattern bindings > extracts binding from if let Some(x) = opt via Phase 5.2 pattern binding", + "b": "test/unit/type-env.test.ts#buildTypeEnv > constructor inference (Tier 1 fallback) > Rust if-let / while-let pattern bindings > Phase 5.2: extracts binding from if let Some(x) = opt where opt: Option", + "category": "native-parser", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/type-env.test.ts#buildTypeEnv > previously-skipped limitations (now resolved) > TS instanceof narrowing: if (x instanceof User) \u2014 first-writer-wins, not block-scoped", + "b": "test/unit/type-env.test.ts#buildTypeEnv > null-check narrowing via patternOverrides (Phase C Task 7) > TS: instanceof still works alongside null-check narrowing", + "category": "native-parser", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/type-env.test.ts#buildTypeEnv > previously-skipped limitations (now resolved) > Rust for with .iter(): for user in users.iter() \u2014 call_expression iterable", + "b": "test/unit/type-env.test.ts#buildTypeEnv > container descriptor-aware type arg selection > Vec.iter() resolves to element type (User) \u2014 arity 1 always returns last", + "category": "native-parser", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/type-env.test.ts#buildTypeEnv > method-aware type arg selection (.keys() vs .values()) > Rust for key in map.keys() resolves to key type (String)", + "b": "test/unit/type-env.test.ts#buildTypeEnv > container descriptor-aware type arg selection > HashMap.keys() resolves to key type (String) via descriptor", + "category": "native-parser", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/type-env.test.ts#buildTypeEnv > method-aware type arg selection (.keys() vs .values()) > Rust for user in map.values() resolves to value type (User)", + "b": "test/unit/type-env.test.ts#buildTypeEnv > container descriptor-aware type arg selection > HashMap.values() resolves to value type (User) via descriptor", + "category": "native-parser", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + }, + { + "a": "test/unit/type-env.test.ts#buildTypeEnv > Kotlin when/is pattern binding (Phase 6) > when (x) { is User -> } binds x to User", + "b": "test/unit/type-env.test.ts#buildTypeEnv > null-check narrowing via patternOverrides (Phase C Task 7) > Kotlin: when/is still works alongside null-check narrowing", + "category": "native-parser", + "basis": "Same production function, literal inputs and expected values; source callbacks are identical. No enclosing hooks change the input." + } + ], + "negatives": [ + { + "a": "test/integration/resolvers/swift.test.ts#Swift return type inference > resolves user.save() to User#save via return type of getUser() -> User", + "b": "test/integration/resolvers/swift.test.ts#Swift return-type inference via function return type > resolves user.save() to User#save via return type of getUser()", + "audit": "distinct", + "reason": "Different fixtures: swift-return-type versus swift-return-type-inference; latter tests competing methods." + }, + { + "a": "test/unit/cursor-hook.test.ts#Cursor hook source regressions > uses npx.cmd for Windows", + "b": "test/unit/factory-plugin.test.ts#Factory hook source regressions > invokes npx.cmd directly on Windows instead of a shell", + "audit": "distinct", + "reason": "The captured source is a different integration hook implementation." + }, + { + "a": "test/unit/cursor-hook.test.ts#Cursor hook source regressions > passes pattern after end-of-options marker (--)", + "b": "test/unit/factory-plugin.test.ts#Factory hook source regressions > passes the pattern after the -- end-of-options marker", + "audit": "distinct", + "reason": "The captured source is a different integration hook implementation." + }, + { + "a": "test/unit/scope-resolution/c/c-captures.test.ts#emitCScopeCaptures \u2014 scopes > captures translation_unit as @scope.module", + "b": "test/unit/scope-resolution/cpp/cpp-captures.test.ts#emitCppScopeCaptures \u2014 scopes > captures translation_unit as @scope.module", + "audit": "distinct", + "reason": "Same snippet and assertion text, but different language parser/provider helpers." + }, + { + "a": "test/unit/scope-resolution/c/c-captures.test.ts#emitCScopeCaptures \u2014 scopes > captures struct_specifier as @scope.class", + "b": "test/unit/scope-resolution/cpp/cpp-captures.test.ts#emitCppScopeCaptures \u2014 scopes > captures struct_specifier as @scope.class", + "audit": "distinct", + "reason": "Same snippet and assertion text, but different language parser/provider helpers." + }, + { + "a": "test/unit/scope-resolution/c/c-captures.test.ts#emitCScopeCaptures \u2014 other declarations > captures typedef as @declaration.typedef", + "b": "test/unit/scope-resolution/cpp/cpp-captures.test.ts#emitCppScopeCaptures \u2014 typedef/alias declarations > captures typedef as @declaration.typedef", + "audit": "distinct", + "reason": "Same snippet and assertion text, but different language parser/provider helpers." + }, + { + "a": "test/unit/scope-resolution/c/c-captures.test.ts#emitCScopeCaptures \u2014 other declarations > captures variable with initializer as @declaration.variable", + "b": "test/unit/scope-resolution/cpp/cpp-captures.test.ts#emitCppScopeCaptures \u2014 variable declarations > captures variable with initializer", + "audit": "distinct", + "reason": "Same snippet and assertion text, but different language parser/provider helpers." + }, + { + "a": "test/unit/scope-resolution/c/c-captures.test.ts#emitCScopeCaptures \u2014 other declarations > captures macro as @declaration.macro", + "b": "test/unit/scope-resolution/cpp/cpp-captures.test.ts#emitCppScopeCaptures \u2014 macro declarations > captures #define as @declaration.macro", + "audit": "distinct", + "reason": "Same snippet and assertion text, but different language parser/provider helpers." + }, + { + "a": "test/unit/setup.test.ts#setupQoder > writes the MCP entry to ~/.qoder.json", + "b": "test/unit/setup.test.ts#setupDroid (Factory) > writes the MCP entry to ~/.factory/mcp.json under mcpServers", + "audit": "distinct", + "reason": "Different selected editors and configPath helpers: Qoder versus Factory." + }, + { + "a": "test/unit/setup.test.ts#setupQoder > leaves a corrupt ~/.qoder.json untouched", + "b": "test/unit/setup.test.ts#setupDroid (Factory) > leaves a corrupt ~/.factory/mcp.json untouched", + "audit": "distinct", + "reason": "Different selected editors and configPath helpers: Qoder versus Factory." + }, + { + "a": "test/unit/verify-workflow-run-pr-identity.test.ts#commit-fork-prebuilds.yml contract > does not call commits/{sha}/pulls (empty for fork SHAs; comments may name it)", + "b": "test/unit/verify-workflow-run-pr-identity.test.ts#pr-autofix-publish.yml contract > does not call commits/{sha}/pulls (empty for fork SHAs; comments may name it)", + "audit": "distinct", + "reason": "The captured workflow source is a different file in each suite." + } + ] +} diff --git a/gitnexus/test/unit/scenario-map/candidates.test.ts b/gitnexus/test/unit/scenario-map/candidates.test.ts index 66f247341..2c594031e 100644 --- a/gitnexus/test/unit/scenario-map/candidates.test.ts +++ b/gitnexus/test/unit/scenario-map/candidates.test.ts @@ -80,6 +80,22 @@ describe('jaccard', () => { }); describe('findCandidates: Jaccard source', () => { + it('retrieves the same body across cleanup/import contexts and dissimilar setup coverage', () => { + const result = findCandidates([ + test('direct', [1, 2], { checksContext: 'direct-context', bodyKey: 'same-body' }), + test('reexport', [1, 2, 3, 4], { checksContext: 'reexport-context', bodyKey: 'same-body' }), + test('other', [5], { checksContext: 'other-context', bodyKey: 'different-body' }), + ]); + expect(result.pairs).toEqual([ + expect.objectContaining({ + a: 'direct', + b: 'reexport', + sources: ['body'], + identicalBlocks: false, + }), + ]); + }); + it.each([ { name: 'one block removed (199/200)', b: range(0, 199) }, { name: 'one block replaced (199/201)', b: [...range(0, 199), 500] }, @@ -147,7 +163,7 @@ describe('findCandidates: checks-and-context source', () => { test('b', [1, 2], { checksContext: 'k' }), ]); expect(result.pairs.map((pair) => pair.sources)).toEqual([['jaccard', 'checks-context']]); - expect(result.stats.bySource).toEqual({ jaccard: 0, checksContext: 0, both: 1 }); + expect(result.stats.bySource).toEqual({ jaccard: 0, checksContext: 0, both: 1, body: 0 }); }); }); diff --git a/gitnexus/test/unit/scenario-map/cli.test.ts b/gitnexus/test/unit/scenario-map/cli.test.ts index 727f9904d..ed30e09e6 100644 --- a/gitnexus/test/unit/scenario-map/cli.test.ts +++ b/gitnexus/test/unit/scenario-map/cli.test.ts @@ -336,6 +336,8 @@ describe('runCli usage', () => { }); it.each([ + { mutation: 'none' as const, kind: 'normal', mode: { kind: 'normal' as const } }, + { mutation: 'possible-duplicates' as const, kind: 'normal', mode: { kind: 'normal' as const } }, { mutation: 'all' as const, kind: 'normal', mode: { kind: 'normal' as const } }, { mutation: 'none' as const, @@ -531,7 +533,18 @@ function writeTwoShardRun(work: string, replan: ShardReplan | null = null): void extraRoots: [], repeats: 1, blocks: ['gitnexus/src/a.ts#f@0'], - files: [], + files: plan.eligibility.fingerprintFiles.map((file) => ({ + file, + recorded: true, + errors: [], + unattributed: { + known: true, + executedFiles: [], + nativeEntries: [], + workerSpawns: [], + excludedCandidates: [], + }, + })), tests: tests.map((id, i) => coverageTest(id, i + 1)), unhandledErrors: [], }; @@ -620,6 +633,28 @@ function writeTwoShardRun(work: string, replan: ShardReplan | null = null): void } describe('merge stage', () => { + it('marks missing coverage partial and reports cases independently of candidate pairs', async () => { + const { deps, work } = setup(); + writeTwoShardRun(work, ONE_SHARD_REPLAN); + const layout = workLayout(work); + const coverage = JSON.parse(readFileSync(layout.coverage, 'utf8')) as CoverageWorkFile; + coverage.files[0].recorded = false; + writeFileSync(layout.coverage, JSON.stringify(coverage)); + const code = await runCli(['merge', '--work', work], deps); + const report = JSON.parse(readFileSync(layout.reportJson, 'utf8')) as ScenarioMapReport; + expect(code).toBe(1); + expect(report.completeness.partial).toBe(true); + expect(report.completeness.shards.incomplete).toContainEqual({ + shard: 'coverage', + reason: '1 files missing or errored; 0 unhandled errors', + }); + expect(report.inventory?.tests.collected).toBe(6); + expect(report.inventory?.files.unrecorded).toEqual(['test/unit/a.test.ts']); + expect(readFileSync(layout.reportMarkdown, 'utf8')).toContain( + 'Runtime inventory: 6 test cases collected', + ); + }); + it('reports the fingerprint of every pair and no mutation verdict for a fingerprint-only run', async () => { const { deps, work } = setup(); writeTwoShardRun(work); diff --git a/gitnexus/test/unit/scenario-map/coverage-inventory.test.ts b/gitnexus/test/unit/scenario-map/coverage-inventory.test.ts new file mode 100644 index 000000000..e52e5beff --- /dev/null +++ b/gitnexus/test/unit/scenario-map/coverage-inventory.test.ts @@ -0,0 +1,69 @@ +import { describe, expect, it } from 'vitest'; +import { coverageInventory } from '../../../tools/scenario-map/inventory/coverage-inventory.js'; +import type { CoverageWorkFile } from '../../../tools/scenario-map/path-coverage/run-coverage.js'; + +const provenance = { + known: false, + executedFiles: [], + nativeEntries: [], + workerSpawns: [], + excludedCandidates: [], +}; +const work = ( + state: 'passed' | 'skipped', + recorded: boolean, + errors: string[] = [], +): CoverageWorkFile => ({ + schemaVersion: 1, + mode: 'normal', + extraRoots: [], + repeats: 1, + blocks: [], + unhandledErrors: [], + files: [{ file: 'a.test.ts', recorded, errors, unattributed: provenance }], + tests: [ + { + id: 'a.test.ts#a', + taskId: 'a', + file: 'a.test.ts', + fullName: 'a', + location: null, + state, + concurrent: false, + attempts: recorded ? 2 : 0, + blocks: [], + suiteBlocks: [], + provenance, + }, + ], +}); + +describe('runtime inventory accounting', () => { + it('does not certify collection after an unhandled runner error', () => { + const input = work('passed', true); + input.unhandledErrors.push('worker exited unexpectedly'); + expect(coverageInventory(input, ['a.test.ts']).collectionComplete).toBe(false); + }); + + it('counts cases independently of files and exposes prefilter omissions', () => { + const result = coverageInventory(work('passed', true), ['a.test.ts', 'b.test.ts']); + expect(result.tests).toEqual({ collected: 1, passed: 1, failed: 0, skipped: 0, pending: 0 }); + expect(result.files.notScheduled).toEqual(['b.test.ts']); + expect(result.collectionComplete).toBe(false); + }); + + it('accounts for fully skipped benchmarks without pretending they executed', () => { + const result = coverageInventory(work('skipped', false), ['a.test.ts']); + expect(result.collectionComplete).toBe(true); + expect(result.tests).toMatchObject({ collected: 1, passed: 0, skipped: 1 }); + }); + + it.each([ + { state: 'passed' as const, errors: [] }, + { state: 'skipped' as const, errors: ['collection failed'] }, + ])('does not hide missing records or collection errors: $state $errors', ({ state, errors }) => { + const result = coverageInventory(work(state, false, errors), ['a.test.ts']); + expect(result.files.unrecorded).toEqual(['a.test.ts']); + expect(result.collectionComplete).toBe(false); + }); +}); diff --git a/gitnexus/test/unit/scenario-map/fingerprint.test.ts b/gitnexus/test/unit/scenario-map/fingerprint.test.ts index 7a021bf80..40d282d1b 100644 --- a/gitnexus/test/unit/scenario-map/fingerprint.test.ts +++ b/gitnexus/test/unit/scenario-map/fingerprint.test.ts @@ -127,6 +127,66 @@ const CASES: { rule: FingerprintRule; verdict: FingerprintVerdict; edit: WorldEd ]; describe('fingerprint rules', () => { + it('keeps equal assertions with different setup as a review lead, not different checks', () => { + const pair = verdictOf({ + a: { checks: 'same-expectation' }, + b: { checks: 'same-expectation', checksContext: 'cleanup-context' }, + }); + expect([pair.verdict, pair.rule, pair.evidence.checks]).toEqual([ + 'possible-duplicate', + 'context-differs', + 'same', + ]); + }); + + it('cannot confirm a duplicate when assertion-only keys exist but context is missing', () => { + const pair = verdictOf({ + a: { checks: 'same-expectation', checksContext: null }, + b: { checks: 'same-expectation', checksContext: null }, + }); + expect([pair.verdict, pair.rule]).toEqual(['possible-duplicate', 'context-unknown']); + }); + + it('retains known input differences when setup also differs', () => { + const pair = verdictOf({ + a: { checks: 'same-expectation' }, + b: { + checks: 'same-expectation', + checksContext: 'other-setup', + facet: facet(['Number[]', 'Number']), + }, + }); + expect([pair.verdict, pair.rule]).toEqual(['table-row', 'input-values-differ']); + }); + + it.each(['args-differ', 'receiver-differs'] as const)( + 'retains a proven runtime value difference (%s) when setup also differs', + (reason) => { + const pair = verdictOf({ + a: { checks: 'same-expectation' }, + b: { checks: 'same-expectation', checksContext: 'other-setup' }, + pair: { trace: { status: 'veto', reasons: [reason] } }, + }); + expect([pair.verdict, pair.rule]).toEqual(['table-row', 'runtime-values-differ']); + }, + ); + + it('does not prove distinctness from incomplete native/worker paths', () => { + const pair = verdictOf({ + a: { provenance: { known: true, nativeEntries: ['tree-sitter.node'], workerSpawns: [] } }, + pair: { identicalBlocks: false }, + }); + expect([pair.verdict, pair.rule]).toEqual(['possible-duplicate', 'path-incomplete']); + }); + + it.each(['asymmetric-sites', 'asymmetric-counts'] as const)( + 'does not call a cold/warm trace difference (%s) different input values', + (reason) => { + const pair = verdictOf({ pair: { trace: { status: 'veto', reasons: [reason] } } }); + expect([pair.verdict, pair.rule]).toEqual(['possible-duplicate', 'trace-unknown']); + }, + ); + it.each(CASES)('rule $rule decides $verdict', ({ rule, verdict, edit }) => { const decided = verdictOf(edit); expect([decided.verdict, decided.rule]).toEqual([verdict, rule]); diff --git a/gitnexus/test/unit/scenario-map/partitions.test.ts b/gitnexus/test/unit/scenario-map/partitions.test.ts index eccda8ddf..24e31c18b 100644 --- a/gitnexus/test/unit/scenario-map/partitions.test.ts +++ b/gitnexus/test/unit/scenario-map/partitions.test.ts @@ -5,6 +5,7 @@ * glob matching, the split, the merge of partial runs, the partitioned runner and the build output * copied into a prepared package. */ +import { execFileSync } from 'node:child_process'; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import os from 'node:os'; import path from 'node:path'; @@ -182,6 +183,73 @@ describe('copyBuildOutput', () => { for (const dir of temps.splice(0)) rmSync(dir, { recursive: true, force: true }); }); + it('gives inventory guards a private index without sharing Git configuration or writes', () => { + const base = mkdtempSync(path.join(os.tmpdir(), 'scenario-map-git-inventory-')); + temps.push(base); + const checkout = path.join(base, 'checkout'); + const prepared = path.join(base, 'prepared'); + for (const root of [checkout, prepared]) { + mkdirSync(path.join(root, 'gitnexus'), { recursive: true }); + writeFileSync(path.join(root, 'gitnexus', 'case.ts'), 'export const value = 1;'); + } + writeFileSync(path.join(checkout, 'public-manifest.json'), '{}'); + writeFileSync(path.join(checkout, '.env'), 'fixture-only-private-value'); + writeFileSync(path.join(prepared, 'gitnexus', 'case.ts'), 'export const value = 2;'); + execFileSync('git', ['init', '--quiet', checkout]); + const git = (root: string, ...args: string[]) => + execFileSync('git', args, { cwd: root, encoding: 'utf8' }).trim(); + git(checkout, 'add', 'gitnexus/case.ts', 'public-manifest.json', '.env'); + git(checkout, 'remote', 'add', 'origin', 'https://example.com/source.git'); + copyBuildOutput('normal', path.join(checkout, 'gitnexus'), path.join(prepared, 'gitnexus')); + expect(git(prepared, 'rev-parse', '--show-toplevel')).toBe(prepared.split(path.sep).join('/')); + expect(git(prepared, 'ls-files', '--stage')).toBe( + git(checkout, 'ls-files', '--stage', '--', 'gitnexus/case.ts', 'public-manifest.json'), + ); + expect(git(prepared, 'remote')).toBe(''); + expect(existsSync(path.join(prepared, '.env'))).toBe(false); + expect(readFileSync(path.join(prepared, 'public-manifest.json'), 'utf8')).toBe('{}'); + expect(readFileSync(path.join(prepared, 'gitnexus', 'case.ts'), 'utf8')).toContain('value = 2'); + git(prepared, 'update-index', '--force-remove', 'gitnexus/case.ts'); + expect(git(checkout, 'ls-files').split('\n')).toEqual([ + '.env', + 'gitnexus/case.ts', + 'public-manifest.json', + ]); + }); + + it('preserves repository contract fixtures needed during test collection', () => { + const base = mkdtempSync(path.join(os.tmpdir(), 'scenario-map-contract-fixtures-')); + temps.push(base); + const checkout = path.join(base, 'checkout', 'gitnexus'); + const prepared = path.join(base, 'prepared', 'gitnexus'); + mkdirSync(checkout, { recursive: true }); + mkdirSync(prepared, { recursive: true }); + const fixtures = [ + '.github/workflows/ci-tests.yml', + '.claude/skills/gitnexus-plan/SKILL.md', + 'gitnexus-factory-plugin/.factory-plugin/plugin.json', + 'Dockerfile.cli', + 'render.yaml', + 'AGENTS.md', + 'CLAUDE.md', + 'eval/workflow_bench/run-evolution.sh', + 'gitnexus/prebuilds/bundled/manifest.json', + ]; + for (const file of fixtures) { + const target = path.join(checkout, '..', file); + mkdirSync(path.dirname(target), { recursive: true }); + writeFileSync(target, file); + } + writeFileSync(path.join(checkout, '..', '.env'), 'private'); + execFileSync('git', ['init', '--quiet', path.dirname(checkout)]); + execFileSync('git', ['add', '--', ...fixtures], { cwd: path.dirname(checkout) }); + copyBuildOutput('normal', checkout, prepared); + expect(fixtures.map((file) => readFileSync(path.join(prepared, '..', file), 'utf8'))).toEqual( + fixtures, + ); + expect(existsSync(path.join(prepared, '..', '.env'))).toBe(false); + }); + it('copies vendor/ and dist/ that the checkout has into a normal-mode copy only', () => { const base = mkdtempSync(path.join(os.tmpdir(), 'scenario-map-build-output-')); temps.push(base); @@ -218,6 +286,8 @@ describe('copyBuildOutput', () => { mkdirSync(dir, { recursive: true }); writeFileSync(path.join(dir, 'registry-query.cjs'), plugin); } + execFileSync('git', ['init', '--quiet', path.dirname(checkout)]); + execFileSync('git', ['add', '--', ...hooks], { cwd: path.dirname(checkout) }); copyBuildOutput('normal', checkout, prepared); expect( hooks.map((plugin) => diff --git a/gitnexus/test/unit/scenario-map/recall-audit.test.ts b/gitnexus/test/unit/scenario-map/recall-audit.test.ts new file mode 100644 index 000000000..395b40e5f --- /dev/null +++ b/gitnexus/test/unit/scenario-map/recall-audit.test.ts @@ -0,0 +1,53 @@ +/** Source-adjudicated pairs from the PR #3686 audit. Retrieval is not a duplicate verdict. */ +import { readFileSync } from 'node:fs'; +import { fileURLToPath } from 'node:url'; +import { describe, expect, it } from 'vitest'; +import { bodyKeyOf } from '../../../tools/scenario-map/facet/body-key.js'; +import { + checksContextKey, + fsModuleReader, + fsSnapshotReader, +} from '../../../tools/scenario-map/facet/checks.js'; +import { loadStaticInventory } from '../../../tools/scenario-map/inventory/cases.js'; +import { findCandidates } from '../../../tools/scenario-map/path-coverage/candidates.js'; +import { layer1Files } from '../../../tools/scenario-map/path-coverage/prefilter.js'; + +const root = fileURLToPath(new URL('../../../../', import.meta.url)); +const audit = JSON.parse( + readFileSync(new URL('../../fixtures/scenario-map-recall/audit.json', import.meta.url), 'utf8'), +) as { + positives: { a: string; b: string; category: string; basis: string }[]; +}; +const ids = new Set(audit.positives.flatMap(({ a, b }) => [a, b])); +const files = [...new Set([...ids].map((id) => id.slice(0, id.indexOf('#'))))]; +const inventory = loadStaticInventory(root, files); +const options = { modules: fsModuleReader(root), snapshots: fsSnapshotReader(root) }; +const sites = new Map(inventory.sites.map((site) => [`${site.file}#${site.fullName}`, site])); + +describe('manual recall audit: candidate retrieval', () => { + it('includes the audited files despite differing hooks and re-export contexts', () => { + expect(layer1Files(root, files).skipped).toEqual([]); + }); + + it.each(audit.positives)( + 'retrieves $a and $b without relying on warm setup coverage', + ({ a, b }) => { + const tests = [a, b].map((id) => { + const site = sites.get(id); + expect(site, `audited test moved or disappeared: ${id}`).toBeDefined(); + if (!site) throw new Error(id); + return { + id, + blocks: [], // Do not let Jaccard hide a static retrieval regression. + concurrent: false, + checksContext: checksContextKey( + { record: site.record, instance: site.instances?.[0] ?? null, fullName: site.fullName }, + options, + ), + bodyKey: bodyKeyOf(site.record), + }; + }); + expect(findCandidates(tests).pairs).toHaveLength(1); + }, + ); +}); diff --git a/gitnexus/test/unit/scenario-map/report.test.ts b/gitnexus/test/unit/scenario-map/report.test.ts index 5fb8434de..d43740c2f 100644 --- a/gitnexus/test/unit/scenario-map/report.test.ts +++ b/gitnexus/test/unit/scenario-map/report.test.ts @@ -354,7 +354,7 @@ describe('renderMarkdown', () => { { scope: 'a whole-suite run that skipped files', overrides: { layer1Files: 0 }, - lines: ['Layer 1 ran 0 of them; every test of the other 1 has a checks-and-context key'], + lines: ['Layer 1 ran 0 of them; the other 1 files were not compared.'], }, { scope: 'a diff-scoped run', diff --git a/gitnexus/tools/scenario-map/facet/body-key.ts b/gitnexus/tools/scenario-map/facet/body-key.ts new file mode 100644 index 000000000..1f709a646 --- /dev/null +++ b/gitnexus/tools/scenario-map/facet/body-key.ts @@ -0,0 +1,8 @@ +/** + * Retrieval hint only: the same callback body can occur behind different hooks or imports. + * It proves neither equal context nor equal input. Verdicts still require all runtime gates. + */ +import type { CaseRecord } from '../../test-dupes/test-dupes.js'; +import { sha1 } from '../util.js'; + +export const bodyKeyOf = (record: CaseRecord): string => sha1(record.body); diff --git a/gitnexus/tools/scenario-map/inventory/coverage-inventory.ts b/gitnexus/tools/scenario-map/inventory/coverage-inventory.ts new file mode 100644 index 000000000..9fb271284 --- /dev/null +++ b/gitnexus/tools/scenario-map/inventory/coverage-inventory.ts @@ -0,0 +1,60 @@ +import type { CoverageWorkFile, TestState } from '../path-coverage/run-coverage.js'; +import { sortedUnique } from '../util.js'; + +/** Runtime counts, separate from file counts and from the subset that forms candidate pairs. */ +export interface CoverageInventory { + files: { + inScope: number; + scheduled: number; + notScheduled: string[]; + unrecorded: string[]; + errors: string[]; + }; + tests: { collected: number } & Record; + /** False when some files may contain cases that the runtime inventory never saw. */ + collectionComplete: boolean; +} + +export function coverageInventory( + work: CoverageWorkFile, + scope: readonly string[], +): CoverageInventory { + const scheduled = new Set(work.files.map((file) => file.file)); + const testsByFile = new Map(); + const tests = { collected: work.tests.length, passed: 0, failed: 0, skipped: 0, pending: 0 }; + for (const test of work.tests) { + tests[test.state]++; + const bucket = testsByFile.get(test.file) ?? []; + bucket.push(test); + testsByFile.set(test.file, bucket); + } + const files = { + inScope: new Set(scope).size, + scheduled: scheduled.size, + notScheduled: sortedUnique(scope.filter((file) => !scheduled.has(file))), + unrecorded: work.files + .filter((file) => { + if (file.recorded) return false; + const own = testsByFile.get(file.file) ?? []; + // Vitest does not invoke aroundAll for a fully skipped module (e.g. opt-in benchmarks). + return !( + file.errors.length === 0 && + own.length > 0 && + own.every((test) => test.state === 'skipped') + ); + }) + .map((file) => file.file) + .sort(), + errors: work.files + .filter((file) => file.errors.length > 0) + .map((file) => file.file) + .sort(), + }; + return { + files, + tests, + collectionComplete: + files.notScheduled.length + files.unrecorded.length + files.errors.length === 0 && + work.unhandledErrors.length === 0, + }; +} diff --git a/gitnexus/tools/scenario-map/path-coverage/candidates.ts b/gitnexus/tools/scenario-map/path-coverage/candidates.ts index 915ef938e..151427b02 100644 --- a/gitnexus/tools/scenario-map/path-coverage/candidates.ts +++ b/gitnexus/tools/scenario-map/path-coverage/candidates.ts @@ -1,7 +1,7 @@ /** * Layer 1 candidate retrieval of the scenario map (U4, R3, KTD5, KTD17, design §5.2). * - * Two sources make a pair of tests a candidate: + * Three sources make a pair of tests a candidate: * - `jaccard`: exact Jaccard similarity of their V8 block sets (the work file's block indices, * run-coverage.ts) of at least {@link JACCARD_THRESHOLD}, checked in integers. All pairs are * found without comparing every pair: blocks are ordered rarest first (by how many tests cover @@ -11,7 +11,9 @@ * filters drop no qualifying pair. Empty block sets (tests that ran no production code) never * pair through this source; * - `checks-context`: both tests have the same checks-and-context key (facet/checks.ts - * `checksContextKey`), whatever their blocks. + * `checksContextKey`), whatever their blocks; + * - `body`: identical callback text, even when setup/import context and blocks differ. This + * source only retrieves review leads; it cannot prove equal inputs or expectations. * * Each pair records its exact Jaccard (null when a set is empty), whether the block sets are * identical (KTD5: a pair that is not caps at weak evidence) and whether it is path-unattributable @@ -28,7 +30,7 @@ import { ascending, byText, sha1 } from '../util.js'; /** Jaccard >= 99 / 100 makes a candidate (R3). */ export const JACCARD_THRESHOLD = Object.freeze({ numerator: 99, denominator: 100 }); -export type CandidateSource = 'jaccard' | 'checks-context'; +export type CandidateSource = 'jaccard' | 'checks-context' | 'body'; export interface CandidateTest { /** Phase 0 test id. */ @@ -38,13 +40,15 @@ export interface CandidateTest { concurrent: boolean; /** Checks-and-context key; null when the test has none (no call site, no assertion). */ checksContext: string | null; + /** Same callback text, independent of surrounding setup. Retrieval only, never proof. */ + bodyKey?: string | null; } export interface CandidatePair { /** The smaller test id. */ a: string; b: string; - /** In the order `jaccard`, `checks-context`. */ + /** In the order `jaccard`, `checks-context`, `body`. */ sources: CandidateSource[]; /** Exact Jaccard of the block sets; null when either set is empty. */ jaccard: number | null; @@ -68,8 +72,8 @@ export interface CandidateStats { /** Tests given. */ tests: number; pairs: number; - /** Pairs found only by Jaccard, only by checks and context, and by both. */ - bySource: { jaccard: number; checksContext: number; both: number }; + /** Disjoint counts: Jaccard only, checks/context only, both, or any body match. */ + bySource: { jaccard: number; checksContext: number; both: number; body: number }; groups: number; /** The group with the most tests, then the most pairs, then the smallest id. */ largestGroup: { id: string; tests: number; pairs: number } | null; @@ -159,12 +163,15 @@ function jaccardPairs(items: readonly Indexed[]): [number, number][] { return found; } -/** Index pairs (into `items`) sharing a non-null checks-and-context key. */ -function checksContextPairs(items: readonly Indexed[]): [number, number][] { +/** Index pairs (into `items`) sharing a non-null key of the selected field. */ +function checksContextPairs( + items: readonly Indexed[], + field: 'checksContext' | 'bodyKey' = 'checksContext', +): [number, number][] { const buckets = new Map(); items.forEach((item, index) => { - const key = item.test.checksContext; - if (key === null) return; + const key = item.test[field]; + if (key == null) return; const bucket = buckets.get(key); if (bucket) bucket.push(index); else buckets.set(key, [index]); @@ -244,6 +251,7 @@ export function findCandidates( }; add(jaccardPairs(items), 'jaccard'); add(checksContextPairs(items), 'checks-context'); + add(checksContextPairs(items, 'bodyKey'), 'body'); const raw = [...sources.values()] .sort((p, q) => p.i - q.i || p.j - q.j) @@ -256,7 +264,7 @@ export function findCandidates( return { a: x.test.id, b: y.test.id, - sources: (['jaccard', 'checks-context'] as const).filter((s) => found.has(s)), + sources: (['jaccard', 'checks-context', 'body'] as const).filter((s) => found.has(s)), jaccard: empty ? null : shared / union, identicalBlocks: shared === x.blocks.length && shared === y.blocks.length, pathUnattributable: x.test.concurrent || y.test.concurrent, @@ -291,6 +299,7 @@ export function findCandidates( jaccard: count(['jaccard']), checksContext: count(['checks-context']), both: count(['jaccard', 'checks-context']), + body: pairs.filter((pair) => pair.sources.includes('body')).length, }, groups: groups.length, largestGroup: diff --git a/gitnexus/tools/scenario-map/path-coverage/coverage.setup.ts b/gitnexus/tools/scenario-map/path-coverage/coverage.setup.ts index 6513676a9..97b6f3250 100644 --- a/gitnexus/tools/scenario-map/path-coverage/coverage.setup.ts +++ b/gitnexus/tools/scenario-map/path-coverage/coverage.setup.ts @@ -28,6 +28,7 @@ import { createHash } from 'node:crypto'; import { Session } from 'node:inspector/promises'; import { syncBuiltinESMExports } from 'node:module'; +import path from 'node:path'; import { fileURLToPath } from 'node:url'; import type { WorkerOptions } from 'node:worker_threads'; import { @@ -60,9 +61,12 @@ if (context === undefined || context === null) { ); } +// Avoid Vite's browser rewrite of new URL(..., import.meta.url), which injects a +// reference to `self` while this local binding is still being initialized in jsdom. +const hookFile = fileURLToPath(import.meta.url); const self = new Set( - [import.meta.url, new URL('./records.ts', import.meta.url).href].map( - (url) => scriptPath(url) ?? fileURLToPath(url), + [hookFile, path.join(path.dirname(hookFile), 'records.ts')].map( + (file) => scriptPath(file) ?? file, ), ); const isSelf = (file: string): boolean => self.has(file); diff --git a/gitnexus/tools/scenario-map/path-coverage/partitions.ts b/gitnexus/tools/scenario-map/path-coverage/partitions.ts index 11a425957..ff1b5e007 100644 --- a/gitnexus/tools/scenario-map/path-coverage/partitions.ts +++ b/gitnexus/tools/scenario-map/path-coverage/partitions.ts @@ -18,7 +18,8 @@ * Library only; pure but for {@link sequentialMatcherFor}, which reads the base config module, * {@link runVitestPartitioned}, which runs the injected Vitest runner, and {@link copyBuildOutput}. */ -import { cpSync, existsSync } from 'node:fs'; +import { execFileSync } from 'node:child_process'; +import { cpSync, existsSync, mkdirSync } from 'node:fs'; import path from 'node:path'; import baseConfig from '../../../vitest.config.js'; import { parseTestId } from '../../mutation/runner/test-ids.js'; @@ -118,7 +119,53 @@ export async function runVitestPartitioned( } /** Package entries the prepare step leaves out and integration tests load (E6). */ -const BUILD_OUTPUT = ['vendor', 'dist'] as const; +const BUILD_OUTPUT = ['vendor', 'prebuilds', 'dist'] as const; + +/** Restore tracked inventory inputs without sharing the checkout's writable Git state. */ +function copyGitInventory(checkoutRoot: string, preparedRoot: string): void { + if (!existsSync(path.join(checkoutRoot, '.git'))) return; + const env = { ...process.env }; + for (const key of [ + 'GIT_DIR', + 'GIT_COMMON_DIR', + 'GIT_WORK_TREE', + 'GIT_INDEX_FILE', + 'GIT_OBJECT_DIRECTORY', + 'GIT_ALTERNATE_OBJECT_DIRECTORIES', + ]) + delete env[key]; + const listing = execFileSync('git', ['ls-files', '--stage', '-z'], { + cwd: checkoutRoot, + env, + encoding: 'utf8', + maxBuffer: 32 * 1024 * 1024, + }); + const entries: string[] = []; + for (const entry of listing.split('\0')) { + const tab = entry.indexOf('\t'); + if (tab < 0 || entry.startsWith('160000 ')) continue; // Do not enter submodule repositories. + const file = entry.slice(tab + 1); + const basename = path.basename(file); + if (basename.startsWith('.env') && !basename.endsWith('.example')) continue; + const from = path.join(checkoutRoot, file); + const to = path.join(preparedRoot, file); + // Repo-wide source guards need the public files outside the prepared package too. + // Existing prepared files may be mutated: never overwrite them with checkout bytes. + if (!existsSync(to) && existsSync(from)) { + mkdirSync(path.dirname(to), { recursive: true }); + cpSync(from, to, { verbatimSymlinks: true }); + } + if (existsSync(to)) entries.push(entry); + } + execFileSync('git', ['init', '--quiet', '--template=', preparedRoot], { env }); + execFileSync('git', ['read-tree', '--empty'], { cwd: preparedRoot, env }); + // Index metadata only: no source object store, history, hooks or remote configuration. + execFileSync('git', ['update-index', '-z', '--index-info'], { + cwd: preparedRoot, + env, + input: entries.length === 0 ? '' : `${entries.join('\0')}\0`, + }); +} /** * Copies {@link BUILD_OUTPUT} from the checkout's package into a prepared package of a normal-mode @@ -135,15 +182,5 @@ export function copyBuildOutput( const from = path.join(checkoutPackage, entry); if (existsSync(from)) cpSync(from, path.join(preparedPackage, entry), { recursive: true }); } - // Package tests compare and execute the shipped copies of the registry hook. - for (const plugin of [ - 'gitnexus-claude-plugin', - 'gitnexus-cursor-integration', - 'gitnexus-factory-plugin', - ]) { - const from = path.join(checkoutPackage, '..', plugin, 'hooks'); - if (existsSync(from)) { - cpSync(from, path.join(preparedPackage, '..', plugin, 'hooks'), { recursive: true }); - } - } + copyGitInventory(path.resolve(checkoutPackage, '..'), path.resolve(preparedPackage, '..')); } diff --git a/gitnexus/tools/scenario-map/path-coverage/prefilter.ts b/gitnexus/tools/scenario-map/path-coverage/prefilter.ts index 3a6d6c6dd..6ca64a74c 100644 --- a/gitnexus/tools/scenario-map/path-coverage/prefilter.ts +++ b/gitnexus/tools/scenario-map/path-coverage/prefilter.ts @@ -1,23 +1,15 @@ /** * The test files Layer 1 must run for the fingerprint (fingerprint plan E7). * - * Past the path rule, a pair is a duplicate, a possible duplicate or a table row only when both - * tests have the same checks-and-context key or a key is unknown: `checks-differ` makes every other - * pair Distinct unless its inputs are equal (verdict/fingerprint.ts). The key is static (KTD5, - * facet/checks.ts over the static inventory), so a file whose every test has a key no other test - * shares needs no run. A file stays when one of its tests has a key another test shares or no key, - * when a call's instance count is not static, when a body reads a file snapshot (the runtime full - * name picks the entry, and the static one may differ), when it may register tests the inventory - * cannot see (inventory/cases.ts `incomplete`: a helper such as - * test/helpers/rename-integration-cases.ts `defineRenameFixtureTests`), or when it did not parse. - * - * Given up: `different-checks` (the same path and input, other checks) between a test of a skipped - * file and any other test; the last full run over the unit tests had none in 6,264 candidate pairs. - * The plan and coverage stages apply it unless mutation scope is `all` or the run is the controls. + * Used only for diff-scoped runs. Keep files sharing an assertion/context key or an exact + * callback-body key, plus files whose test inventory or checks are unknown. Body matches are + * retrieval hints: setup, inputs, paths and trace evidence still decide the verdict. Whole-suite + * runs execute every fingerprint file so a unique static context cannot hide a runtime match. * * Library only; it reads the test files and their snapshot and helper modules. */ import { checksContextKey, fsModuleReader, fsSnapshotReader } from '../facet/checks.js'; +import { bodyKeyOf } from '../facet/body-key.js'; import { loadStaticInventory } from '../inventory/cases.js'; /** Matchers that read a `.snap` file entry named after the runtime test name. */ @@ -27,7 +19,7 @@ export interface Layer1Files { /** The files Layer 1 runs, in input order. */ files: string[]; /** - * The others: every test has a checks-and-context key no other test shares, or (diff scope) a + * The others: every test has static retrieval keys no other test shares, or (diff scope) a * static key, none of which a changed file's test has. */ skipped: string[]; @@ -60,7 +52,11 @@ export function layer1Files( options, ); if (key === null) unknown.add(site.file); - else keys.push({ file: site.file, key }); + else + keys.push( + { file: site.file, key: `context:${key}` }, + { file: site.file, key: `body:${bodyKeyOf(site.record)}` }, + ); } } const count = new Map(); diff --git a/gitnexus/tools/scenario-map/path-coverage/run-coverage.ts b/gitnexus/tools/scenario-map/path-coverage/run-coverage.ts index 1c4842632..48b29daa4 100644 --- a/gitnexus/tools/scenario-map/path-coverage/run-coverage.ts +++ b/gitnexus/tools/scenario-map/path-coverage/run-coverage.ts @@ -465,6 +465,7 @@ export function checksContextKeys( export function candidateTestsOf( work: CoverageWorkFile, keys: ReadonlyMap, + bodyKeys?: ReadonlyMap, ): CandidateTest[] { return work.tests .filter((test) => test.attempts > 0 || test.state === 'passed' || test.state === 'failed') @@ -473,6 +474,7 @@ export function candidateTestsOf( blocks: test.blocks, concurrent: test.concurrent, checksContext: keys.get(test.id) ?? null, + ...(bodyKeys === undefined ? {} : { bodyKey: bodyKeys.get(test.id) ?? null }), })); } diff --git a/gitnexus/tools/scenario-map/report/markdown.ts b/gitnexus/tools/scenario-map/report/markdown.ts index ec3d1ef43..741f4e61b 100644 --- a/gitnexus/tools/scenario-map/report/markdown.ts +++ b/gitnexus/tools/scenario-map/report/markdown.ts @@ -505,12 +505,26 @@ function layer0Lines(report: ScenarioMapReport): string[] { function eligibilitySection(report: ScenarioMapReport): string[] { const { eligibility } = report; + const inventory = report.inventory; const lines = [ '', '## Eligibility', '', `${eligibility.mode} mode: the fingerprint covers ${plural(eligibility.fingerprintFiles, 'test file')}; ` + `${plural(eligibility.files.length, 'test file')} are mutation-eligible.`, + ...(inventory == null + ? [] + : [ + `Runtime inventory: ${inventory.tests.collected} test cases collected; ` + + `${inventory.tests.passed} passed, ${inventory.tests.failed} failed, ` + + `${inventory.tests.skipped} skipped, ${inventory.tests.pending} pending.`, + ...(inventory.collectionComplete + ? [] + : [ + 'Collection is incomplete: omitted or errored files may contain additional cases. ' + + 'These counts are not a whole-suite total.', + ]), + ]), ...(eligibility.changedFiles === null ? [] : [ @@ -521,15 +535,13 @@ function eligibilitySection(report: ScenarioMapReport): string[] { ? [] : [ eligibility.changedFiles === null - ? `Layer 1 ran ${eligibility.layer1Files} of them; every test of the other ` + - `${eligibility.fingerprintFiles - eligibility.layer1Files} has a checks-and-context ` + - 'key no other test shares, so none of their pairs can be a duplicate, a possible ' + - 'duplicate or a table row.' + ? `Layer 1 ran ${eligibility.layer1Files} of them; the other ` + + `${eligibility.fingerprintFiles - eligibility.layer1Files} files were not compared. ` + + 'No duplicate-recall claim applies to their tests.' : `Layer 1 ran ${eligibility.layer1Files} of them: the changed files whose tests can ` + - "pair with another test, the files sharing their tests' checks-and-context keys, " + + "pair with another test, the files sharing their tests' checks-and-context or body keys, " + 'and every file with a test of no static key (helper-registered, dynamic ' + - "`it.each` counts, file snapshots), which can pair with any test. No other file's " + - "test can be a changed test's duplicate, possible duplicate or table row.", + '`it.each` counts, file snapshots), which can pair with any test. Other files were not compared.', ]), ...(eligibility.unrecordedFiles === 0 ? [] diff --git a/gitnexus/tools/scenario-map/report/report.ts b/gitnexus/tools/scenario-map/report/report.ts index faeefab21..dd56ce35b 100644 --- a/gitnexus/tools/scenario-map/report/report.ts +++ b/gitnexus/tools/scenario-map/report/report.ts @@ -45,6 +45,7 @@ import os from 'node:os'; import path from 'node:path'; import { readJsonFile } from '../../mutation/shared/fs.js'; import type { Finding, Kind } from '../../test-dupes/test-dupes.js'; +import type { CoverageInventory } from '../inventory/coverage-inventory.js'; import { MAIN_LINEAGE, comparePair, @@ -296,7 +297,9 @@ export interface ReportInput { seed: string; provenance: CollectedProvenance; eligibility: Eligibility; - /** How many test files Layer 1 was given (the work file's files; E7 skips the rest). */ + /** All collected runtime cases, including tests that formed no candidate pair. */ + inventory?: CoverageInventory; + /** How many test files Layer 1 was given (every fingerprint file for a whole-suite run). */ layer1Files: number; /** How many of them wrote no record: a failed run, or a coverage slice that did not report. */ unrecordedFiles: number; @@ -441,6 +444,7 @@ export interface FingerprintSection { } export interface ScenarioMapReport { + inventory: CoverageInventory | null; schemaVersion: number; engineVersion: number; run: RunInfo; @@ -460,8 +464,7 @@ export interface ScenarioMapReport { /** How many test files the fingerprint covers. */ fingerprintFiles: number; /** - * How many of them Layer 1 ran; the others hold only tests whose checks-and-context key no - * other test shares (path-coverage/prefilter.ts). + * How many of them Layer 1 ran. A diff scope can select fewer; omitted tests were not compared. */ layer1Files: number; /** How many of those wrote no record (a failed run, or a coverage slice that did not report). */ @@ -942,6 +945,7 @@ export function buildReport(input: ReportInput): ScenarioMapReport { return { schemaVersion: SCENARIO_MAP_SCHEMA_VERSION, + inventory: input.inventory ?? null, engineVersion, run: { ...input.run }, seed: input.seed, diff --git a/gitnexus/tools/scenario-map/scenario-map.ts b/gitnexus/tools/scenario-map/scenario-map.ts index 41d7f837f..4c3c0217a 100644 --- a/gitnexus/tools/scenario-map/scenario-map.ts +++ b/gitnexus/tools/scenario-map/scenario-map.ts @@ -18,11 +18,9 @@ * plan eligibility (R1); the Layer 0 scan (test-dupes over the `--all` corpus plus the * eligible files, findings on eligible files kept); a prepared copy and the run * manifest built right after it (KTD14); Layer 1 over {@link layer1FilesOf}: every - * fingerprint file for `--mutation all` and controls, otherwise only the files with a - * test whose static checks-and-context key another test shares or that has none - * (path-coverage/prefilter.ts; E7), or with `--changed-since ` (a diff-scoped + * fingerprint file in whole-suite mode, or with `--changed-since ` (a diff-scoped * run, E8: the test files added or modified since the merge base of `ref` and HEAD) - * only those files and the files sharing their tests' keys, the candidate pairs + * only those files and the files sharing their tests' context or body keys, the candidate pairs * then kept only when they have a test of a changed file (path-coverage `runCoverage`, or * with `--coverage ` the merge of the coverage stage's slice files in that * directory, read before the work directory is emptied and checked against the @@ -30,7 +28,7 @@ * prefilter, and a slice that did not report leaves its files unrecorded); the * static inventory and runtime join (U3); checks and context (U5, with the file * snapshot and helper-module readers); input facets (U6); candidates (U4, Jaccard - * and checks-and-context sources); each group's KTD7 mutate scope (U7); the + * checks-and-context and body sources); each group's KTD7 mutate scope (U7); the * row-deletion plans of the facet-equal pairs (U9); the mutation target of * `--mutation` ({@link mutationTarget}: every group, none, or the groups with a * possible duplicate on the fingerprint before the trace, trimmed to its tests and the @@ -294,6 +292,8 @@ import { type ManifestCheck, type ScenarioRunManifest, } from './mutation/run-group.js'; +import { bodyKeyOf } from './facet/body-key.js'; +import { coverageInventory } from './inventory/coverage-inventory.js'; import { findCandidates, type CandidatePair } from './path-coverage/candidates.js'; import { DEFAULT_FIXTURE_ROOT, @@ -895,6 +895,9 @@ export interface Layer2Test { joinProblems: JoinProblem[]; inventoryUnresolved: string[]; checksContext: string | null; + /** Assertion-only key and body retrieval hint; absent in older work files. */ + checks?: string | null; + bodyKey?: string | null; unsupportedForms: UnsupportedForm[]; unresolvedSelectors: string[]; facet: InputFacet | null; @@ -1507,6 +1510,7 @@ export function assembleVerdictInput(input: VerdictAssembly): VerdictInput { concurrent: test.concurrent, excludedFiles: excluded.get(test.id) ?? [], checksContext: record.checksContext, + checks: record.checks, unsupportedForms: record.unsupportedForms, unresolvedSelectors: record.unresolvedSelectors, facet: record.facet, @@ -1673,6 +1677,7 @@ const BEST_TRACE: VerdictPairInput['trace'] = { status: 'clean', reasons: [] }; function layer2Maps(tests: readonly Layer2Test[]): { facets: Map; checksContext: Map; + bodyKeys: Map; } { return { facets: new Map( @@ -1681,6 +1686,7 @@ function layer2Maps(tests: readonly Layer2Test[]): { ), ), checksContext: new Map(tests.map((test) => [test.id, test.checksContext])), + bodyKeys: new Map(tests.map((test) => [test.id, test.bodyKey ?? null])), }; } @@ -1782,6 +1788,8 @@ async function computeLayer2(repoRoot: string, work: CoverageWorkFile): Promise< for (const instance of instances) { const checkSubject = subjectOf(instance); const checks = checkSubject === null ? null : computeChecks(checkSubject, checkOptions); + const hasChecks = + checks !== null && (checks.checks.length > 0 || checks.unsupported.length > 0); const facetSubject = facetSubjectOf(instance, provenance.get(instance.id) ?? null); if (facetSubject !== null) subjects.set(instance.id, facetSubject); tests.push({ @@ -1793,6 +1801,8 @@ async function computeLayer2(repoRoot: string, work: CoverageWorkFile): Promise< joinProblems: instance.unsupported, inventoryUnresolved: instance.unresolved, checksContext: checksContextKey(instance, checkOptions), + checks: hasChecks ? checks.fingerprint : null, + bodyKey: checkSubject === null || !hasChecks ? null : bodyKeyOf(checkSubject.record), unsupportedForms: checks?.unsupported ?? [], unresolvedSelectors: checks?.unresolved ?? [], facet: facetSubject === null ? null : await computeFacet(facetSubject, facetContext), @@ -1808,9 +1818,9 @@ const facetEqual = (facets: ReadonlyMap, pair: CandidatePair }; /** - * The test files Layer 1 runs (E7): path-coverage/prefilter.ts's for a diff-scoped run (E8, the - * changed files and their key partners) and for mutation scope `none` and `possible-duplicates`; - * every fingerprint file for scope `all` and in controls mode (every candidate group's evidence). + * Whole-suite runs execute every fingerprint file. Static context uniqueness is not proof that + * no duplicate scenario exists: cleanup hooks and re-exported imports can change that key. + * Diff-scoped runs retain the prefilter's changed-file and candidate-partner selection. */ export function layer1FilesOf( repoRoot: string, @@ -1819,17 +1829,16 @@ export function layer1FilesOf( mode: RunMode, changed: ReadonlySet | null = null, ): string[] { - if (changed === null && (mutation === 'all' || mode.kind === 'controls')) return [...files]; + if (changed === null) return [...files]; return layer1Files(repoRoot, files, changed).files; } /** Why Layer 1 left `skipped` files out (the plan's log); empty when it left none. */ function skippedNote(skipped: number, diffScoped: boolean): string { if (skipped === 0) return ''; - if (diffScoped) return `; the other ${skipped} can hold no finding with a changed test`; return ( - `; the other ${skipped} hold only tests whose checks-and-context key no other test shares, ` + - 'so none of their pairs can be a duplicate' + `; ${skipped} files excluded by ${diffScoped ? 'diff' : 'imported coverage'} selection; ` + + 'excluded tests have not been compared' ); } @@ -1974,12 +1983,13 @@ export async function runCoverageStage( work, }; writeJson(layout.coverageSlice(options.index), slice); - const failed = work.files.filter((file) => !file.recorded || file.errors.length > 0); + const inventory = coverageInventory(work, files); + const failed = sortedUnique([...inventory.files.unrecorded, ...inventory.files.errors]); log( `coverage: ${work.tests.length} tests, ${failed.length} files failed or unrecorded, ` + minutes(wallClockMs), ); - return failed.length === 0; + return failed.length === 0 && work.unhandledErrors.length === 0; } /** @@ -2114,10 +2124,10 @@ export async function runPlanStage( writeScenarioRunManifest(layout.root, manifest); const layer2 = await timed('layer 2', () => computeLayer2(repoRoot, work)); - const { facets, checksContext } = layer2Maps(layer2.tests); + const { facets, checksContext, bodyKeys } = layer2Maps(layer2.tests); const candidates = await timed('candidates', () => findCandidates( - candidateTestsOf(work, checksContext), + candidateTestsOf(work, checksContext, bodyKeys), (id) => changed === null || changed.has(parseTestId(id).file), ), ); @@ -3362,6 +3372,18 @@ export async function runMergeStage( }); const fingerprint = computeFingerprints(evidence); const verdicts = computeVerdicts(mutationEvidence(plan.mutation, evidence, shardPlan.groups)); + const inventory = coverageInventory(work, plan.eligibility.fingerprintFiles); + const missingCoverage = sortedUnique([ + ...inventory.files.unrecorded, + ...inventory.files.errors, + ...(plan.changedFiles === null ? inventory.files.notScheduled : []), + ]); + if (missingCoverage.length > 0 || work.unhandledErrors.length > 0) { + incomplete.push({ + shard: 'coverage', + reason: `${missingCoverage.length} files missing or errored; ${work.unhandledErrors.length} unhandled errors`, + }); + } const read = (file: string): string | null => { try { @@ -3381,13 +3403,14 @@ export async function runMergeStage( sourceRevision: ctx.sourceRevision(), }), eligibility: plan.eligibility, + inventory, // The M of "N of M eligible tests evaluated": the mutation-eligible tests. eligibleTests: work.tests .filter((test) => mutationEligible.has(test.file)) .map((test) => test.id), shards: { planned: shardPlan.shards.length, complete: completeShards, incomplete }, layer1Files: work.files.length, - unrecordedFiles: work.files.filter((file) => !file.recorded).length, + unrecordedFiles: inventory.files.unrecorded.length, changedFiles: plan.changedFiles, notRun: { skipped: work.tests.filter((test) => test.state === 'skipped').length, diff --git a/gitnexus/tools/scenario-map/verdict/fingerprint.ts b/gitnexus/tools/scenario-map/verdict/fingerprint.ts index 6c5325c1a..4063fb229 100644 --- a/gitnexus/tools/scenario-map/verdict/fingerprint.ts +++ b/gitnexus/tools/scenario-map/verdict/fingerprint.ts @@ -12,18 +12,21 @@ * discarded values (every literal, row value and resolved constant), compared with * the residual-difference guard (D1, ./verdict.ts `facetRelation`), and the trace * run's comparison of the values reaching opaque built-ins at runtime (U11); - * expectation its checks-and-context key (U5: matchers, selectors, mocks and environment); the + * expectation its assertion key (U5: matchers and selectors), gated separately on setup context; the * expected values are part of the discarded values. * * Per pair, the first rule that holds ({@link FINGERPRINT_RULES}): * out-of-scope a test outside the fingerprint's scope; * inconclusive a test did not pass its Layer 1 run (`not-passing`), or ran concurrently * (`path-unattributable`); - * distinct the block sets differ (`path-differs`); + * possible-duplicate equal assertions but missing/different context, or differing blocks with + * incomplete native/worker provenance, unless input values are known to differ; + * distinct the remaining block sets differ (`path-differs`); * different-checks same path and the same, resolved input with a clean trace, other checks; * distinct other checks otherwise (`checks-differ`), or another call shape; * table-row same shape, other values (`input-values-differ`), or equal static values - * that differ at runtime (`runtime-values-differ`, a trace veto); + * that differ at runtime (`runtime-values-differ`, arguments or receiver); + * asymmetric sites/counts alone remain trace uncertainty; * possible-duplicate nothing known differs, but a checks key or facet is missing, an input is * unresolved (a facet, inventory or selector input, a residual difference, * an unsupported assertion or join, a path V8 does not fully show), or the @@ -39,7 +42,7 @@ import { byText, sortedUnique } from '../util.js'; import { facetRelation, type VerdictPairInput, type VerdictTestInput } from './verdict.js'; /** Folded into the report: a rule change resurfaces every pair it reclassifies. */ -export const FINGERPRINT_ENGINE_VERSION = 1; +export const FINGERPRINT_ENGINE_VERSION = 2; export type FingerprintVerdict = | 'duplicate' @@ -66,6 +69,9 @@ export type FingerprintRule = | 'not-passing' | 'path-unattributable' | 'path-differs' + | 'path-incomplete' + | 'context-differs' + | 'context-unknown' | 'same-input-different-checks' | 'checks-differ' | 'input-shape-differs' @@ -120,6 +126,7 @@ export type FingerprintTestInput = Pick< | 'provenance' | 'concurrent' | 'checksContext' + | 'checks' | 'unsupportedForms' | 'unresolvedSelectors' | 'facet' @@ -156,6 +163,7 @@ interface Facts { evidence: FingerprintEvidence; /** Same input as far as the extraction sees: equal facet and values, nothing unresolved. */ sameInput: boolean; + runtimeValuesDiffer: boolean; } interface Rule { @@ -200,6 +208,45 @@ export const FINGERPRINT_RULES: readonly Rule[] = [ ...(f.pair.pathUnattributable ? ['path unattributable'] : []), ]), }, + { + verdict: 'possible-duplicate', + decide: (f) => + when( + f.evidence.checks === 'same' && + f.evidence.values !== 'differ' && + !f.runtimeValuesDiffer && + (f.a.checksContext === null || f.b.checksContext === null), + 'context-unknown', + ['Assertion keys are known, but setup/import context is missing'], + ), + }, + { + verdict: 'possible-duplicate', + decide: (f) => + when( + f.evidence.checks === 'same' && + f.a.checksContext !== f.b.checksContext && + f.evidence.values !== 'differ' && + !f.runtimeValuesDiffer, + 'context-differs', + ['Assertions match, but setup/import contexts differ; equivalence is unproven'], + ), + }, + { + verdict: 'possible-duplicate', + decide: (f) => + when( + f.evidence.path === 'differs' && + f.evidence.values !== 'differ' && + !f.runtimeValuesDiffer && + f.evidence.checks !== 'differs' && + f.evidence.unresolved.some((item) => item.includes(': path: ')), + 'path-incomplete', + [ + 'V8 block sets differ, but native/worker or unknown provenance prevents a complete comparison', + ], + ), + }, { verdict: 'distinct', decide: (f) => when(f.evidence.path === 'differs', 'path-differs', ['V8 block sets differ']), @@ -228,7 +275,7 @@ export const FINGERPRINT_RULES: readonly Rule[] = [ verdict: 'table-row', decide: (f) => when( - f.evidence.trace === 'veto' && f.evidence.values === 'equal', + f.runtimeValuesDiffer && f.evidence.values === 'equal', 'runtime-values-differ', f.pair.trace?.reasons ?? [], ), @@ -280,7 +327,9 @@ function factsOf( b: FingerprintTestInput, ): Facts { const relation = facetRelation(a.facet, b.facet); - const keys = [a.checksContext, b.checksContext]; + // Older artifacts have only the combined key. Never compare a new assertion key with one. + const legacy = a.checks === undefined && b.checks === undefined; + const keys = legacy ? [a.checksContext, b.checksContext] : [a.checks ?? null, b.checks ?? null]; const unresolved = sortedUnique([...unresolvedOf(a), ...unresolvedOf(b)]); const evidence: FingerprintEvidence = { path: pair.identicalBlocks ? 'same' : 'differs', @@ -296,6 +345,11 @@ function factsOf( a, b, evidence, + runtimeValuesDiffer: + pair.trace?.status === 'veto' && + pair.trace.reasons.some( + (reason) => reason === 'args-differ' || reason === 'receiver-differs', + ), sameInput: relation.facet === 'equal' && relation.discarded === 'equal' && unresolved.length === 0, }; diff --git a/gitnexus/tools/scenario-map/verdict/verdict.ts b/gitnexus/tools/scenario-map/verdict/verdict.ts index e6edee759..ca84f20b7 100644 --- a/gitnexus/tools/scenario-map/verdict/verdict.ts +++ b/gitnexus/tools/scenario-map/verdict/verdict.ts @@ -220,6 +220,8 @@ export interface VerdictTestInput { excludedFiles: readonly string[]; /** U5 checks-and-context key; null when the test has none. */ checksContext: string | null; + /** Assertion fingerprint without setup context; absent in older stage artifacts. */ + checks?: string | null; /** U5 unsupported assertion forms (`InstanceChecks.unsupported`). */ unsupportedForms: readonly UnsupportedForm[]; /** U5 selectors whose instance value is not static (`InstanceChecks.unresolved`). */ From 3b9ea785d170fd58074d6938d75e339c62bda064 Mon Sep 17 00:00:00 2001 From: Gergo Magyar Date: Tue, 27 Oct 2026 19:48:12 +0000 Subject: [PATCH 02/10] 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. --- gitnexus/test/unit/scenario-map/report.test.ts | 2 +- gitnexus/tools/scenario-map/report/markdown.ts | 9 +++++---- gitnexus/tools/scenario-map/scenario-map.ts | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/gitnexus/test/unit/scenario-map/report.test.ts b/gitnexus/test/unit/scenario-map/report.test.ts index d43740c2f..39001e98b 100644 --- a/gitnexus/test/unit/scenario-map/report.test.ts +++ b/gitnexus/test/unit/scenario-map/report.test.ts @@ -354,7 +354,7 @@ describe('renderMarkdown', () => { { scope: 'a whole-suite run that skipped files', overrides: { layer1Files: 0 }, - lines: ['Layer 1 ran 0 of them; the other 1 files were not compared.'], + lines: ['Layer 1 ran 0 of them; the other 1 file was not compared.'], }, { scope: 'a diff-scoped run', diff --git a/gitnexus/tools/scenario-map/report/markdown.ts b/gitnexus/tools/scenario-map/report/markdown.ts index 741f4e61b..45198600d 100644 --- a/gitnexus/tools/scenario-map/report/markdown.ts +++ b/gitnexus/tools/scenario-map/report/markdown.ts @@ -506,6 +506,7 @@ function layer0Lines(report: ScenarioMapReport): string[] { function eligibilitySection(report: ScenarioMapReport): string[] { const { eligibility } = report; const inventory = report.inventory; + const uncomparedFiles = eligibility.fingerprintFiles - eligibility.layer1Files; const lines = [ '', '## Eligibility', @@ -515,14 +516,14 @@ function eligibilitySection(report: ScenarioMapReport): string[] { ...(inventory == null ? [] : [ - `Runtime inventory: ${inventory.tests.collected} test cases collected; ` + + `Runtime inventory: ${plural(inventory.tests.collected, 'test case')} collected; ` + `${inventory.tests.passed} passed, ${inventory.tests.failed} failed, ` + `${inventory.tests.skipped} skipped, ${inventory.tests.pending} pending.`, ...(inventory.collectionComplete ? [] : [ - 'Collection is incomplete: omitted or errored files may contain additional cases. ' + - 'These counts are not a whole-suite total.', + 'Collection is incomplete: files may be omitted, unrecorded or errored, ' + + 'or the runner may have unhandled errors. These counts are not a verified whole-suite total.', ]), ]), ...(eligibility.changedFiles === null @@ -536,7 +537,7 @@ function eligibilitySection(report: ScenarioMapReport): string[] { : [ eligibility.changedFiles === null ? `Layer 1 ran ${eligibility.layer1Files} of them; the other ` + - `${eligibility.fingerprintFiles - eligibility.layer1Files} files were not compared. ` + + `${plural(uncomparedFiles, 'file')} ${uncomparedFiles === 1 ? 'was' : 'were'} not compared. ` + 'No duplicate-recall claim applies to their tests.' : `Layer 1 ran ${eligibility.layer1Files} of them: the changed files whose tests can ` + "pair with another test, the files sharing their tests' checks-and-context or body keys, " + diff --git a/gitnexus/tools/scenario-map/scenario-map.ts b/gitnexus/tools/scenario-map/scenario-map.ts index 4c3c0217a..df9c757fe 100644 --- a/gitnexus/tools/scenario-map/scenario-map.ts +++ b/gitnexus/tools/scenario-map/scenario-map.ts @@ -1933,8 +1933,8 @@ export interface CoverageStageOptions { /** * The coverage stage: Layer 1 over slice `index` of `of` (path-coverage/slices.ts), written as - * the slice file the plan stage's `--coverage` merges. True when every file of the slice was - * recorded without errors. + * the slice file the plan stage's `--coverage` merges. True when every slice file is accounted + * for without file or unhandled runner errors. Fully skipped modules need no execution record. */ export async function runCoverageStage( ctx: StageContext, From a8ccee931e28e7a136da1e6092d3b8165f07121c Mon Sep 17 00:00:00 2001 From: Gergo Magyar Date: Tue, 27 Oct 2026 20:20:16 +0000 Subject: [PATCH 03/10] 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. --- gitnexus/test/unit/scenario-map/coverage-inventory.test.ts | 7 +++++++ gitnexus/test/unit/scenario-map/partitions.test.ts | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/gitnexus/test/unit/scenario-map/coverage-inventory.test.ts b/gitnexus/test/unit/scenario-map/coverage-inventory.test.ts index e52e5beff..a40e5bd36 100644 --- a/gitnexus/test/unit/scenario-map/coverage-inventory.test.ts +++ b/gitnexus/test/unit/scenario-map/coverage-inventory.test.ts @@ -58,6 +58,13 @@ describe('runtime inventory accounting', () => { expect(result.tests).toMatchObject({ collected: 1, passed: 0, skipped: 1 }); }); + it('reports collection errors even when the file has an execution record', () => { + const result = coverageInventory(work('passed', true, ['collection failed']), ['a.test.ts']); + expect(result.files.unrecorded).toEqual([]); + expect(result.files.errors).toEqual(['a.test.ts']); + expect(result.collectionComplete).toBe(false); + }); + it.each([ { state: 'passed' as const, errors: [] }, { state: 'skipped' as const, errors: ['collection failed'] }, diff --git a/gitnexus/test/unit/scenario-map/partitions.test.ts b/gitnexus/test/unit/scenario-map/partitions.test.ts index 24e31c18b..6e2404afe 100644 --- a/gitnexus/test/unit/scenario-map/partitions.test.ts +++ b/gitnexus/test/unit/scenario-map/partitions.test.ts @@ -198,7 +198,7 @@ describe('copyBuildOutput', () => { execFileSync('git', ['init', '--quiet', checkout]); const git = (root: string, ...args: string[]) => execFileSync('git', args, { cwd: root, encoding: 'utf8' }).trim(); - git(checkout, 'add', 'gitnexus/case.ts', 'public-manifest.json', '.env'); + git(checkout, 'add', '--force', '--', 'gitnexus/case.ts', 'public-manifest.json', '.env'); git(checkout, 'remote', 'add', 'origin', 'https://example.com/source.git'); copyBuildOutput('normal', path.join(checkout, 'gitnexus'), path.join(prepared, 'gitnexus')); expect(git(prepared, 'rev-parse', '--show-toplevel')).toBe(prepared.split(path.sep).join('/')); @@ -242,7 +242,7 @@ describe('copyBuildOutput', () => { } writeFileSync(path.join(checkout, '..', '.env'), 'private'); execFileSync('git', ['init', '--quiet', path.dirname(checkout)]); - execFileSync('git', ['add', '--', ...fixtures], { cwd: path.dirname(checkout) }); + execFileSync('git', ['add', '--force', '--', ...fixtures], { cwd: path.dirname(checkout) }); copyBuildOutput('normal', checkout, prepared); expect(fixtures.map((file) => readFileSync(path.join(prepared, '..', file), 'utf8'))).toEqual( fixtures, From 5b6f0db1d149c8ed476dc3ee45ea368cdc19c866 Mon Sep 17 00:00:00 2001 From: Gergo Magyar Date: Tue, 27 Oct 2026 21:12:16 +0000 Subject: [PATCH 04/10] fix(review): simplify selection and guard mixed artifacts --- gitnexus/test/unit/scenario-map/cli.test.ts | 13 ++----------- gitnexus/test/unit/scenario-map/fingerprint.test.ts | 12 ++++++++++++ gitnexus/tools/scenario-map/scenario-map.ts | 12 +----------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/gitnexus/test/unit/scenario-map/cli.test.ts b/gitnexus/test/unit/scenario-map/cli.test.ts index ed30e09e6..80f559fa0 100644 --- a/gitnexus/test/unit/scenario-map/cli.test.ts +++ b/gitnexus/test/unit/scenario-map/cli.test.ts @@ -335,18 +335,9 @@ describe('runCli usage', () => { ).toEqual(['HEAD^1', 'HEAD^1', 'HEAD^1']); }); - it.each([ - { mutation: 'none' as const, kind: 'normal', mode: { kind: 'normal' as const } }, - { mutation: 'possible-duplicates' as const, kind: 'normal', mode: { kind: 'normal' as const } }, - { mutation: 'all' as const, kind: 'normal', mode: { kind: 'normal' as const } }, - { - mutation: 'none' as const, - kind: 'controls', - mode: { kind: 'controls' as const, fixtureRoot: 'gitnexus/test/fixtures/x' }, - }, - ])('runs every file in Layer 1 for mutation $mutation in $kind mode', ({ mutation, mode }) => { + it('runs every file in Layer 1 for a whole-suite run', () => { const files = ['test/unit/a.test.ts', 'test/unit/b.test.ts']; - expect(layer1FilesOf('/no-repo', files, mutation, mode)).toEqual(files); + expect(layer1FilesOf('/no-repo', files)).toEqual(files); }); it('exits 2 for a work directory inside the checkout', async () => { diff --git a/gitnexus/test/unit/scenario-map/fingerprint.test.ts b/gitnexus/test/unit/scenario-map/fingerprint.test.ts index 40d282d1b..509588b57 100644 --- a/gitnexus/test/unit/scenario-map/fingerprint.test.ts +++ b/gitnexus/test/unit/scenario-map/fingerprint.test.ts @@ -127,6 +127,18 @@ const CASES: { rule: FingerprintRule; verdict: FingerprintVerdict; edit: WorldEd ]; describe('fingerprint rules', () => { + it.each(['a', 'b'] as const)( + 'does not compare an assertion-only key on side %s with a legacy combined key', + (side) => { + const pair = verdictOf({ [side]: { checks: 'checks-toBe' } }); + expect([pair.verdict, pair.rule, pair.evidence.checks]).toEqual([ + 'possible-duplicate', + 'checks-unknown', + 'missing', + ]); + }, + ); + it('keeps equal assertions with different setup as a review lead, not different checks', () => { const pair = verdictOf({ a: { checks: 'same-expectation' }, diff --git a/gitnexus/tools/scenario-map/scenario-map.ts b/gitnexus/tools/scenario-map/scenario-map.ts index df9c757fe..0eb3074bb 100644 --- a/gitnexus/tools/scenario-map/scenario-map.ts +++ b/gitnexus/tools/scenario-map/scenario-map.ts @@ -1825,8 +1825,6 @@ const facetEqual = (facets: ReadonlyMap, pair: CandidatePair export function layer1FilesOf( repoRoot: string, files: readonly string[], - mutation: MutationScope, - mode: RunMode, changed: ReadonlySet | null = null, ): string[] { if (changed === null) return [...files]; @@ -1950,8 +1948,6 @@ export async function runCoverageStage( const layer1 = layer1FilesOf( repoRoot, fingerprintFiles, - options.mutation, - options.mode, changed === null ? null : new Set(changed), ); const assignment = coverageSlices( @@ -2093,13 +2089,7 @@ export async function runPlanStage( const layer1 = slices?.[0].layer1 ?? (await timed('layer 1 files', () => - layer1FilesOf( - repoRoot, - eligibility.fingerprintFiles, - options.mutation, - options.mode, - changed, - ), + layer1FilesOf(repoRoot, eligibility.fingerprintFiles, changed), )); log( `plan: Layer 1 runs ${layer1.length} of ${eligibility.fingerprintFiles.length} test files` + From 24e05c76a078a8956dd148c3eb6fb309e431f292 Mon Sep 17 00:00:00 2001 From: Gergo Magyar Date: Wed, 28 Oct 2026 14:02:14 +0000 Subject: [PATCH 05/10] 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. --- gitnexus/package-lock.json | 139 +++++ gitnexus/package.json | 1 + .../scenario-map-consolidation-inputs.test.ts | 57 ++ .../scenario-map-runtime-inputs.test.ts | 46 ++ .../unit/scenario-map/consolidation.test.ts | 468 +++++++++++++++ .../scenario-map/fixture-similarity.test.ts | 365 ++++++++++++ .../scenario-map/runtime-fixtures.test.ts | 315 +++++++++++ gitnexus/tools/scenario-map/README.md | 174 ++++++ .../scenario-map/consolidation/assertions.ts | 328 +++++++++++ .../consolidation/fixture-clones.ts | 535 ++++++++++++++++++ .../consolidation/fixture-sources.ts | 27 + .../scenario-map/consolidation/report.ts | 367 ++++++++++++ .../tools/scenario-map/consolidation/run.ts | 27 + .../consolidation/verify-controls.ts | 79 +++ .../scenario-map/facet/fixture-inputs.ts | 313 ++++++++++ .../scenario-map/facet/fixture-profile.ts | 318 +++++++++++ .../scenario-map/facet/runtime-fixtures.ts | 116 ++++ .../scenario-map/path-coverage/candidates.ts | 83 ++- .../path-coverage/coverage.setup.ts | 35 +- .../path-coverage/fixture-capture.ts | 234 ++++++++ .../path-coverage/fixture-plugin.ts | 53 ++ .../scenario-map/path-coverage/prefilter.ts | 13 + .../scenario-map/path-coverage/records.ts | 11 +- .../path-coverage/run-coverage.ts | 42 +- .../scenario-map/path-coverage/slices.ts | 8 + gitnexus/tools/scenario-map/scenario-map.ts | 63 ++- 26 files changed, 4195 insertions(+), 22 deletions(-) create mode 100644 gitnexus/test/integration/scenario-map-consolidation-inputs.test.ts create mode 100644 gitnexus/test/integration/scenario-map-runtime-inputs.test.ts create mode 100644 gitnexus/test/unit/scenario-map/consolidation.test.ts create mode 100644 gitnexus/test/unit/scenario-map/fixture-similarity.test.ts create mode 100644 gitnexus/test/unit/scenario-map/runtime-fixtures.test.ts create mode 100644 gitnexus/tools/scenario-map/README.md create mode 100644 gitnexus/tools/scenario-map/consolidation/assertions.ts create mode 100644 gitnexus/tools/scenario-map/consolidation/fixture-clones.ts create mode 100644 gitnexus/tools/scenario-map/consolidation/fixture-sources.ts create mode 100644 gitnexus/tools/scenario-map/consolidation/report.ts create mode 100644 gitnexus/tools/scenario-map/consolidation/run.ts create mode 100644 gitnexus/tools/scenario-map/consolidation/verify-controls.ts create mode 100644 gitnexus/tools/scenario-map/facet/fixture-inputs.ts create mode 100644 gitnexus/tools/scenario-map/facet/fixture-profile.ts create mode 100644 gitnexus/tools/scenario-map/facet/runtime-fixtures.ts create mode 100644 gitnexus/tools/scenario-map/path-coverage/fixture-capture.ts create mode 100644 gitnexus/tools/scenario-map/path-coverage/fixture-plugin.ts diff --git a/gitnexus/package-lock.json b/gitnexus/package-lock.json index 6fe3badb6..cfb2b16f7 100644 --- a/gitnexus/package-lock.json +++ b/gitnexus/package-lock.json @@ -83,6 +83,7 @@ "@types/proxy-addr": "^2.0.3", "@vitest/coverage-v8": "^5.0.0", "gitnexus-shared": "file:../gitnexus-shared", + "jscpd": "5.4.2", "jsdom": "26.1.0", "tsx": "^4.0.0", "typescript": "^7.0.2", @@ -4748,6 +4749,144 @@ "js-yaml": "bin/js-yaml.mjs" } }, + "node_modules/jscpd": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/jscpd/-/jscpd-5.4.2.tgz", + "integrity": "sha512-bxZ1ObA4NyWBcM7Zh+QoGrdcMovbCxrDofFaLo2Lwgk/Axe/e45VYu8EM+mpPx2CpU/QGCoktONRnaRZl8Yq2w==", + "dev": true, + "license": "MIT", + "bin": { + "jscpd": "run-jscpd.js" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://opencollective.com/jscpd" + }, + "optionalDependencies": { + "jscpd-darwin-arm64": "5.4.2", + "jscpd-darwin-x64": "5.4.2", + "jscpd-linux-arm64-gnu": "5.4.2", + "jscpd-linux-arm64-musl": "5.4.2", + "jscpd-linux-x64-gnu": "5.4.2", + "jscpd-linux-x64-musl": "5.4.2", + "jscpd-windows-arm64-msvc": "5.4.2", + "jscpd-windows-x64-msvc": "5.4.2" + } + }, + "node_modules/jscpd-darwin-arm64": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/jscpd-darwin-arm64/-/jscpd-darwin-arm64-5.4.2.tgz", + "integrity": "sha512-EGoPYQZqESW2kJfxKZrUYsYWhzq2Irk6sBQQWZ7VPAGj2wwk3n8HtDP2FnJOiHWZtSz55I8zQWldzIrD55B5Vw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/jscpd-darwin-x64": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/jscpd-darwin-x64/-/jscpd-darwin-x64-5.4.2.tgz", + "integrity": "sha512-/bz/rgoahqeK0p5r+KjeTTS59d7bZpMxD7hQ4CaQ0qKZWBZIyu7hRdExzJ7/KrCEJ5cmRogvXX1jhFqldYLNXw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/jscpd-linux-arm64-gnu": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/jscpd-linux-arm64-gnu/-/jscpd-linux-arm64-gnu-5.4.2.tgz", + "integrity": "sha512-YW/eX+mOYZmT/A3QEARYmJQoVQNV/cfecKCobNXZcD3mE57drpKHmJnvuL9uS85z0cUKWGCM57JVkVz6Y9oxxw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/jscpd-linux-arm64-musl": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/jscpd-linux-arm64-musl/-/jscpd-linux-arm64-musl-5.4.2.tgz", + "integrity": "sha512-jOLTO8YVRz65NaeYNeXoIf13yOPWhbM61xKXpv51DXQ2BXce6XfgPaJVeDCOM5s+8mt6aDtq0BZ8zptNeqGuIw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/jscpd-linux-x64-gnu": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/jscpd-linux-x64-gnu/-/jscpd-linux-x64-gnu-5.4.2.tgz", + "integrity": "sha512-eAmKLxx+pIOEX4lZ342CCCjL5bvf+a3gdmaaVHXiGIx2Zr3zKkF32YkfuN2sCvCui5I/wnely80XsRvVVFO47Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/jscpd-linux-x64-musl": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/jscpd-linux-x64-musl/-/jscpd-linux-x64-musl-5.4.2.tgz", + "integrity": "sha512-ers9YFscD6DrmhyjBDXo/L+yBKcclHdWTFLMulYVGLcoGRKfBFhmK08MI3ajN/GMmCpJu7rkDYKL3egdfE8OIw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/jscpd-windows-arm64-msvc": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/jscpd-windows-arm64-msvc/-/jscpd-windows-arm64-msvc-5.4.2.tgz", + "integrity": "sha512-kOhVw4cVhN8T9jr3OVThB3pPpbff0ITHTwpJ/+4EGzMkbf0y6QtPpHDzRgiHJj7u1FSTBXTI6VhPhDUhT/os1g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/jscpd-windows-x64-msvc": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/jscpd-windows-x64-msvc/-/jscpd-windows-x64-msvc-5.4.2.tgz", + "integrity": "sha512-BuR5DCV7F/G5ukMEIzCrz43ea5C0t0ALcP7v+opOWefoi4aiQYCEmtoRwfXMM/zjOmNYmvJBDDfeH05U9uuzEg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, "node_modules/jsdom": { "version": "26.1.0", "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-26.1.0.tgz", diff --git a/gitnexus/package.json b/gitnexus/package.json index c849ea300..56f87690a 100644 --- a/gitnexus/package.json +++ b/gitnexus/package.json @@ -147,6 +147,7 @@ "@types/proxy-addr": "^2.0.3", "@vitest/coverage-v8": "^5.0.0", "gitnexus-shared": "file:../gitnexus-shared", + "jscpd": "5.4.2", "jsdom": "26.1.0", "tsx": "^4.0.0", "typescript": "^7.0.2", diff --git a/gitnexus/test/integration/scenario-map-consolidation-inputs.test.ts b/gitnexus/test/integration/scenario-map-consolidation-inputs.test.ts new file mode 100644 index 000000000..ae7a0feee --- /dev/null +++ b/gitnexus/test/integration/scenario-map-consolidation-inputs.test.ts @@ -0,0 +1,57 @@ +import { expect, it } from 'vitest'; +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { runPipelineFromRepo } from '../../src/core/ingestion/pipeline.js'; + +// Ground truth controls: fixture content changes independently of the checks below. +const BASE = 'export function calculate(value: number) { return value + 1; }\n'; +const RENAMED = 'export function compute(amount: number) { return amount + 1; }\n'; +const EXTRA = + BASE + 'export function extra(value: number) { while (value > 0) { value--; } return value; }\n'; +const UNRELATED = 'export interface Settings { enabled: boolean; label: string; }\n'; +let count = 0; + +async function ingest(source: string, extension = 'ts'): Promise { + const root = mkdtempSync(path.join(tmpdir(), 'consolidation-control-')); + try { + writeFileSync(path.join(root, `main.${extension}`), source); + count = (await runPipelineFromRepo(root, () => {}, { skipGraphPhases: true })).graph.nodeCount; + } finally { + rmSync(root, { recursive: true, force: true }); + } +} + +it('exact A', async () => { + await ingest(BASE); + expect(count).toBeGreaterThan(0); +}); +it('exact B', async () => { + await ingest(BASE); + expect(count).toBeGreaterThan(0); +}); +it('renamed fixture', async () => { + await ingest(RENAMED); + expect(count).toBeGreaterThan(0); +}); +it('partial fixture and unique check', async () => { + await ingest(EXTRA); + expect(count).toBeGreaterThan(0); + expect(count).toBeGreaterThan(2); +}); +it('different expectation', async () => { + await ingest(BASE); + expect(count).toBeGreaterThan(1); +}); +it('unrelated assertion', async () => { + await ingest(BASE); + expect(count).toBeTypeOf('number'); +}); +it('unrelated fixture', async () => { + await ingest(UNRELATED); + expect(count).toBeGreaterThan(0); +}); +it('equivalent Python scenario', async () => { + await ingest('def calculate(value):\n return value + 1\n', 'py'); + expect(count).toBeGreaterThan(0); +}); diff --git a/gitnexus/test/integration/scenario-map-runtime-inputs.test.ts b/gitnexus/test/integration/scenario-map-runtime-inputs.test.ts new file mode 100644 index 000000000..2ed05765c --- /dev/null +++ b/gitnexus/test/integration/scenario-map-runtime-inputs.test.ts @@ -0,0 +1,46 @@ +import { beforeAll, describe, expect, it } from 'vitest'; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { readFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +// Positive/negative controls for the scenario-map runtime fixture recorder. +import { runPipelineFromRepo } from '../../src/core/ingestion/pipeline.js'; + +const inputs = [ + ['typescript', 'ts', 'export function twice(x: number) { return x + x; }'], + ['python', 'py', 'def twice(x):\n return x + x\n'], + ['loop', 'ts', 'export function twice(x: number) { while(x > 0) { x--; } return x; }'], +] as const; + +describe.each(inputs)('%s generated repository', (_name, extension, source) => { + let count = 0; + beforeAll(async () => { + const root = mkdtempSync(path.join(tmpdir(), 'scenario-runtime-')); + try { + writeFileSync(path.join(root, `main.${extension}`), source); + count = (await runPipelineFromRepo(root, () => {}, { skipGraphPhases: true })).graph + .nodeCount; + } finally { + rmSync(root, { recursive: true, force: true }); + } + }, 60_000); + it('finds declarations', () => { + expect(count).toBeGreaterThan(0); + }); + it('retains the result after fixture cleanup', () => { + expect(count).toBeGreaterThan(0); + }); +}); + +it.each(['sync', 'promise'])('dynamic %s read survives cleanup', async (api) => { + const root = mkdtempSync(path.join(tmpdir(), 'scenario-runtime-read-')); + let text: string; + try { + const file = path.join(root, 'main.ts'); + writeFileSync(file, inputs[0][2]); + text = api === 'sync' ? readFileSync(file, 'utf8') : await readFile(file, 'utf8'); + } finally { + rmSync(root, { recursive: true, force: true }); + } + expect(text).toContain('function twice'); +}); diff --git a/gitnexus/test/unit/scenario-map/consolidation.test.ts b/gitnexus/test/unit/scenario-map/consolidation.test.ts new file mode 100644 index 000000000..6c47de6eb --- /dev/null +++ b/gitnexus/test/unit/scenario-map/consolidation.test.ts @@ -0,0 +1,468 @@ +import { expect, it } from 'vitest'; +import { tmpdir } from 'node:os'; +import { mkdtempSync, rmSync } from 'node:fs'; +import path from 'node:path'; +import { assertionEvidence } from '../../../tools/scenario-map/consolidation/assertions.js'; +import { + FixtureCloneIndex, + FixtureCloneCandidates, + verifyFixtureClonePairs, + runFixtureClones, + type CloneRun, +} from '../../../tools/scenario-map/consolidation/fixture-clones.js'; +import { + capturedFixtureSources, + mergeFixtureSources, + type FixtureSources, +} from '../../../tools/scenario-map/consolidation/fixture-sources.js'; +import { consolidationReport, overlap } from '../../../tools/scenario-map/consolidation/report.js'; +import { computeChecks } from '../../../tools/scenario-map/facet/checks.js'; +import { expandCases } from '../../../tools/scenario-map/inventory/cases.js'; +import { sha1 } from '../../../tools/scenario-map/util.js'; +import type { FixtureProfile } from '../../../tools/scenario-map/facet/fixture-profile.js'; +import type { + CoverageWorkFile, + CoverageTest, +} from '../../../tools/scenario-map/path-coverage/run-coverage.js'; +import type { Layer2Test } from '../../../tools/scenario-map/scenario-map.js'; + +const SOURCE = 'export function calculate(value: number) { return value + 1; }\n'; +const files: [string, string][] = [['main.ts', SOURCE]]; +const key = sha1(JSON.stringify(files)); +const sources: FixtureSources = { [key]: files }; +const profile: FixtureProfile = { + sourceKeys: [key], + roots: ['/tmp/input'], + files: ['/tmp/input/main.ts'], + contentKey: 'same', + languages: ['typescript'], + features: [['def:Function', 1]], + unresolved: [], +}; +const noClones: CloneRun = { tool: 'jscpd', version: 'test', sourceCount: 1, clones: [] }; +const atom = (value = '2', shape = 'count') => ({ + line: 1, + shape, + shapeKey: shape, + key: `${shape}:${value}`, + expected: value, +}); +const evidence = (atoms = [atom()]) => ({ atoms, unresolved: [] }); +const test = (id: string, blocks = [0, 1]): CoverageTest => ({ + id, + taskId: id, + file: 'test/input.test.ts', + fullName: id, + location: null, + state: 'passed', + concurrent: false, + attempts: 2, + blocks, + suiteBlocks: [], + provenance: { + known: true, + executedFiles: [], + nativeEntries: [], + workerSpawns: [], + excludedCandidates: [], + }, +}); +const detail = (id: string, assertions = evidence(), fixture = profile): Layer2Test => ({ + id, + file: 'test/input.test.ts', + anchor: null, + notOracleValidated: false, + dupeOk: null, + joinProblems: [], + inventoryUnresolved: [], + checksContext: null, + unsupportedForms: [], + unresolvedSelectors: [], + facet: null, + fixture, + assertionEvidence: assertions, +}); +const work = (tests: CoverageTest[]): CoverageWorkFile => ({ + schemaVersion: 1, + fixtureCaptureVersion: 1, + fixtureSources: sources, + mode: 'normal', + extraRoots: [], + repeats: 1, + blocks: [ + 'src/a.ts#one@0|1-2:1', + 'src/a.ts#two@2|3-4:1', + 'src/a.ts#miss@4|5-6:0', + 'src/b.ts#three@0|1-2:1', + ], + files: [], + tests, + unhandledErrors: [], +}); +const report = (tests: CoverageTest[], details: Layer2Test[]) => + consolidationReport(work(tests), details, new FixtureCloneIndex(sources, noClones)); + +it('requires all three axes for observed equality and groups repeated cases without expanding every pair', () => { + const tests = Array.from({ length: 100 }, (_, i) => test(String(i))); + const result = report( + tests, + tests.map((t) => detail(t.id)), + ); + expect(result.opportunities).toHaveLength(1); + expect(result.opportunities[0]).toMatchObject({ + kind: 'same-observed-input-path-assertions', + representedPairs: 4950, + path: { coverageA: 1, coverageB: 1 }, + assertions: { coverageA: 1, coverageB: 1 }, + }); +}); + +it('does not mistake shared uncovered branch ranges for shared execution', () => { + const result = report([test('a', [0, 2]), test('b', [3, 2])], [detail('a'), detail('b')]); + expect(result.opportunities).toEqual([]); +}); + +it('reports partial shared checks and retains unique assertions and executed branches', () => { + const result = report( + [test('a', [0, 1]), test('b', [0, 3])], + [ + detail('a', evidence([atom(), atom('a', 'only-a')])), + detail('b', evidence([atom(), atom('b', 'only-b')])), + ], + ); + expect(result.opportunities[0]).toMatchObject({ + kind: 'extract-shared-scenario', + path: { coverageA: 0.5, coverageB: 0.5 }, + assertions: { shared: 1, totalA: 2, totalB: 2 }, + }); + const found = result.opportunities[0]; + expect([...found.onlyAssertionsA, ...found.onlyAssertionsB].join()).toContain('only-a'); + expect([...found.onlyAssertionsA, ...found.onlyAssertionsB].join()).toContain('only-b'); + expect([...found.onlyBlocksA, ...found.onlyBlocksB]).toContain('src/a.ts#two@2|3-4:1'); +}); + +it('keeps different expected values as parameter rows, not equal assertions', () => { + const result = report([test('a'), test('b')], [detail('a'), detail('b', evidence([atom('3')]))]); + expect(result.opportunities[0]).toMatchObject({ + kind: 'parameterize-expectations', + assertions: { shared: 0 }, + assertionShapes: { shared: 1 }, + }); +}); + +it('rejects disjoint checks, failed cases, concurrent paths, missing inputs, and unresolved expectations', () => { + expect( + report([test('a'), test('b')], [detail('a'), detail('b', evidence([atom('2', 'different')]))]) + .opportunities, + ).toEqual([]); + for (const patch of [{ state: 'failed' as const }, { concurrent: true }]) + expect( + report([test('a'), { ...test('b'), ...patch }], [detail('a'), detail('b')]).opportunities, + ).toEqual([]); + expect( + report( + [test('a'), test('b')], + [detail('a'), detail('b', evidence(), { ...profile, unresolved: ['missing file'] })], + ).opportunities, + ).toEqual([]); + expect( + report( + [test('a'), test('b')], + [detail('a'), detail('b', evidence([{ ...atom(), key: null, expected: null }]))], + ).opportunities, + ).toEqual([]); +}); + +it('counts repeated assertions as a multiset and handles empty evidence honestly', () => { + expect(overlap(['a', 'a', 'b'], ['a', 'b'])).toMatchObject({ + shared: 2, + totalA: 3, + totalB: 2, + coverageB: 1, + }); + expect(overlap([], [])).toMatchObject({ shared: 0, jaccard: 0 }); +}); + +it('merges immutable source tables and rejects altered source hashes', () => { + expect(mergeFixtureSources([sources, sources])).toEqual(sources); + expect(() => mergeFixtureSources([{ [key]: [['main.ts', 'changed']] }])).toThrow('hash mismatch'); + expect(capturedFixtureSources([])).toEqual({}); +}); + +it('measures clone coverage using weighted byte spans instead of whole matching lines', () => { + const otherFiles: [string, string][] = [['main.ts', SOURCE.replace('+', '-')]]; + const otherKey = sha1(JSON.stringify(otherFiles)); + const run: CloneRun = { + ...noClones, + sourceCount: 2, + clones: [ + { + a: { source: key, file: 'main.ts', start: 0, end: 20, startLine: 1, endLine: 1 }, + b: { source: otherKey, file: 'main.ts', start: 0, end: 20, startLine: 1, endLine: 1 }, + kind: 'similar', + similarity: 0.5, + tokens: 12, + }, + ], + }; + const index = new FixtureCloneIndex({ ...sources, [otherKey]: otherFiles }, run); + const found = index.compare(profile, { + ...profile, + sourceKeys: [otherKey], + contentKey: 'different', + }); + expect(found?.coverageA).toBeGreaterThan(0); + expect(found?.coverageA).toBeLessThan(0.5); + expect(found?.kind).toBe('clone'); +}); + +it('runs the pinned external clone engine on renamed and partial fixture functions', async () => { + const root = mkdtempSync(path.join(tmpdir(), 'fixture-clone-test-')); + const renamed: [string, string][] = [ + ['main.ts', SOURCE.replaceAll('calculate', 'compute').replaceAll('value', 'amount')], + ]; + const longer: [string, string][] = [ + [ + 'main.ts', + SOURCE + '\nexport function extra(x: number) { while (x > 0) { x--; } return x; }\n', + ], + ]; + const renamedKey = sha1(JSON.stringify(renamed)), + longerKey = sha1(JSON.stringify(longer)); + try { + const all = { ...sources, [renamedKey]: renamed, [longerKey]: longer }; + const run = await runFixtureClones(all, root); + expect(run.version).toContain('5.4.2'); + const index = new FixtureCloneIndex(all, run); + expect( + index.compare(profile, { ...profile, sourceKeys: [renamedKey], contentKey: 'renamed' }) + ?.coverageA, + ).toBeGreaterThan(0.5); + const partial = index.compare(profile, { + ...profile, + sourceKeys: [longerKey], + contentKey: 'longer', + }); + expect(partial?.coverageA).toBeGreaterThan(0.5); + expect(partial?.coverageB).toBeLessThan(0.8); + expect(partial?.unmatchedB.some((span) => span.end > SOURCE.length)).toBe(true); + // The external report may connect both to a representative, omitting this pair. + const transitive = index.compare( + { ...profile, sourceKeys: [renamedKey], contentKey: 'renamed' }, + { ...profile, sourceKeys: [longerKey], contentKey: 'longer' }, + ); + expect(transitive?.coverageA).toBeGreaterThan(0.5); + expect(transitive?.coverageB).toBeLessThan(0.8); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); + +it('verifies contained representative spans without projecting offsets or treating near misses as transitive', async () => { + const root = mkdtempSync(path.join(tmpdir(), 'fixture-contained-test-')); + const longer: [string, string][] = [ + ['main.ts', SOURCE + 'export function extra(x: number) { while (x > 0) x--; return x; }'], + ]; + const decoy: [string, string][] = [['decoy.ts', '// unrelated wrapper\n' + longer[0][1]]]; + const longerKey = sha1(JSON.stringify(longer)), + decoyKey = sha1(JSON.stringify(decoy)); + const all = { ...sources, [longerKey]: longer, [decoyKey]: decoy }; + const b = { ...profile, sourceKeys: [longerKey], contentKey: 'longer' }; + const span = (source: string, file: string, start: number, end: number) => ({ + source, + file, + start, + end, + startLine: 1, + endLine: 2, + }); + try { + const exported = await runFixtureClones(all, root); + // The full corpus can choose the same representative with DIFFERENT bounds. + // These representative links retrieve a candidate, never manufactured clone spans. + const run: CloneRun = { + ...exported, + clones: [ + { + a: span(key, 'main.ts', 0, SOURCE.length - 1), + b: span(decoyKey, 'decoy.ts', 21, 21 + SOURCE.length - 1), + kind: 'renamed', + similarity: 1, + tokens: 13, + }, + { + a: span(longerKey, 'main.ts', 0, SOURCE.length + 10), + b: span(decoyKey, 'decoy.ts', 21, 21 + SOURCE.length + 10), + kind: 'renamed', + similarity: 1, + tokens: 17, + }, + ], + }; + expect(new FixtureCloneIndex(all, run).compare(profile, b)).toBeNull(); + const candidates = new FixtureCloneCandidates(run); + consolidationReport( + { ...work([test('a'), test('b')]), fixtureSources: all }, + [detail('a'), detail('b', evidence(), b)], + new FixtureCloneIndex(all, run), + (a, b) => candidates.consider(a, b), + ); + expect(candidates.pairs()).toEqual([[key, longerKey].sort()]); + const verified = await verifyFixtureClonePairs( + all, + run, + candidates.pairs(), + path.join(root, 'pairs'), + ); + const result = new FixtureCloneIndex(all, verified).compare(profile, b); + expect(result?.coverageA).toBeGreaterThan(0.8); + expect(result?.coverageB).toBeLessThan(0.8); + expect(result?.unmatchedB.some((s) => s.end > SOURCE.length)).toBe(true); + expect(verified.verifiedPairs).toBe(1); + const nearMiss = new FixtureCloneCandidates({ + ...run, + clones: run.clones.map((c) => ({ ...c, kind: 'similar', similarity: 0.9 })), + }); + nearMiss.consider(profile, b); + expect(nearMiss.pairs()).toEqual([]); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); + +function assertions(source: string) { + return expandCases([{ path: 'gitnexus/test/unit/controls.test.ts', source }]).sites.flatMap( + (site) => + (site.instances ?? []).map((instance) => { + const subject = { record: site.record, instance, fullName: site.fullName }; + return assertionEvidence(subject, computeChecks(subject)); + }), + ); +} + +it('resolves asserted const origins and distinguishes different queried quantities', () => { + const found = assertions(` + it('a', () => { const n = graph.nodes.length; expect(n).toBe(2); }); + it('b', () => { const m = graph.nodes.length; expect(m).toBe(2); }); + it('c', () => { const n = graph.edges.length; expect(n).toBe(2); }); + it('d', () => { let n = graph.nodes.length; n++; expect(n).toBe(2); }); + `); + expect(found[0].atoms[0].key).toBeTruthy(); + expect(found[1].atoms[0].key).toBe(found[0].atoms[0].key); + expect(found[2].atoms[0].key).not.toBe(found[0].atoms[0].key); + expect(found[3].atoms[0].key).toBeNull(); +}); + +it('retains exact expected values, negation, selectors, and row values', () => { + const found = assertions(` + it.each([2, 3])('row %i', (n) => { expect(graph.nodes.length).toBe(n); }); + it('literal', () => { expect(graph.nodes.length).toBe(2); }); + it('negative', () => { expect(graph.nodes.length).not.toBe(2); }); + it('selector', () => { expect(graph.get('Function')).toHaveLength(2); }); + it('other selector', () => { expect(graph.get('Class')).toHaveLength(2); }); + `); + expect(found[0].atoms[0].key).toBe(found[2].atoms[0].key); + expect(found[0].atoms[0].key).not.toBe(found[1].atoms[0].key); + expect(found[2].atoms[0].key).not.toBe(found[3].atoms[0].key); + expect(found[4].atoms[0].key).not.toBe(found[5].atoms[0].key); +}); + +it('canonicalizes object and signed numeric expectations and rejects unknown expected values', () => { + const found = assertions(` + it.each([{ b: 2, a: -1 }])('row', (value) => { expect(result).toEqual(value); }); + it('literal', () => { expect(result).toEqual({ a: -1, b: 2 }); }); + it('unknown', () => { expect(result).toEqual(calculateExpected()); }); + it('shadow', () => { const undefined = calculateExpected(); expect(result).toBe(undefined); }); + `); + expect(found[0].atoms[0].expected).toBe(found[1].atoms[0].expected); + expect(found[2].atoms[0].key).toBeNull(); + expect(found[3].atoms[0].key).toBeNull(); +}); + +it('does not hide unknown checks or extra repeated assertions in partial overlaps', () => { + const a = evidence([atom(), atom(), { ...atom(), key: null, expected: null }]); + const result = report([test('a'), test('b')], [detail('a', a), detail('b')]); + const found = result.opportunities[0]; + expect(found.assertions.shared).toBe(1); + expect([found.assertions.totalA, found.assertions.totalB].sort()).toEqual([1, 3]); + expect(found.onlyAssertionsA.length + found.onlyAssertionsB.length).toBe(2); + expect(found.kind).not.toBe('same-observed-input-path-assertions'); +}); + +it('resolves const query chains and lexically bound callback parameters without conflating selectors', () => { + const found = assertions(` + it('a', () => { const calls = getRelationships(result, 'CALLS'); const edge = calls.find(c => c.target === 'save'); expect(edge).toBeDefined(); }); + it('b', () => { const relations = getRelationships(result, 'CALLS'); const match = relations.find(e => e.target === 'save'); expect(match).toBeDefined(); }); + it('c', () => { const calls = getRelationships(result, 'CALLS'); const edge = calls.find(c => c.target === 'getName'); expect(edge).toBeDefined(); }); + it('d', () => { const calls = getRelationships(result, 'IMPORTS'); const edge = calls.find(c => c.target === 'save'); expect(edge).toBeDefined(); }); + `); + expect(found[0].atoms[0].key).toBeTruthy(); + expect(found[1].atoms[0].key).toBe(found[0].atoms[0].key); + expect(found[2].atoms[0].key).not.toBe(found[0].atoms[0].key); + expect(found[3].atoms[0].key).not.toBe(found[0].atoms[0].key); +}); + +it('retains template text when tracing asserted query chains', () => { + const a = assertions( + "it('a', () => { const paths = items.map(e => `${e.source} -> ${e.target}`); expect(paths.some(p => p.includes('service'))).toBe(true); });", + )[0]; + const b = assertions( + "it('b', () => { const paths = items.map(e => `${e.source} => ${e.target}`); expect(paths.some(p => p.includes('service'))).toBe(true); });", + )[0]; + expect(a.atoms[0].key).toBeTruthy(); + expect(a.atoms[0].key).not.toBe(b.atoms[0].key); +}); + +it('does not infer clone families through near-miss similarity', () => { + const thirdFiles: [string, string][] = [['main.ts', SOURCE.replace('+ 1', '* 2')]]; + const third = sha1(JSON.stringify(thirdFiles)); + const middleFiles: [string, string][] = [['main.ts', SOURCE.replace('+ 1', '- 1')]]; + const middle = sha1(JSON.stringify(middleFiles)); + const span = (source: string) => ({ + source, + file: 'main.ts', + start: 0, + end: 20, + startLine: 1, + endLine: 1, + }); + const run: CloneRun = { + ...noClones, + sourceCount: 3, + clones: [ + { a: span(key), b: span(middle), kind: 'similar', similarity: 0.9, tokens: 12 }, + { a: span(middle), b: span(third), kind: 'similar', similarity: 0.9, tokens: 12 }, + ], + }; + const index = new FixtureCloneIndex( + { ...sources, [middle]: middleFiles, [third]: thirdFiles }, + run, + ); + expect( + index.compare(profile, { ...profile, sourceKeys: [third], contentKey: 'third' }), + ).toBeNull(); +}); + +it('surfaces one shared check in a partial scenario and retains all other expectations', () => { + const found = report( + [test('a'), test('b')], + [ + detail('a', evidence([atom(), atom('a', 'unique-a'), atom('b', 'unique-b')])), + detail( + 'b', + evidence([atom(), atom('c', 'unique-c'), atom('d', 'unique-d'), atom('e', 'unique-e')]), + ), + ], + ).opportunities[0]; + expect(found.assertions).toMatchObject({ shared: 1 }); + expect(found.onlyAssertionsA.length + found.onlyAssertionsB.length).toBe(5); +}); + +it('keeps negative zero distinct and locates the real assertion on a shared line', () => { + const found = assertions(` + it('a', () => { helper(result).toBe(99); expect(result).toBe(-0); }); + it('b', () => { expect(result).toBe(0); }); + `); + expect(found[0].atoms[0].expected).toContain('"text":"-0"'); + expect(found[0].atoms[0].key).not.toBe(found[1].atoms[0].key); +}); diff --git a/gitnexus/test/unit/scenario-map/fixture-similarity.test.ts b/gitnexus/test/unit/scenario-map/fixture-similarity.test.ts new file mode 100644 index 000000000..550142873 --- /dev/null +++ b/gitnexus/test/unit/scenario-map/fixture-similarity.test.ts @@ -0,0 +1,365 @@ +import { describe, expect, it } from 'vitest'; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { + FixtureProfiler, + compareFixtureProfiles, + fixtureSimilarityReport, +} from '../../../tools/scenario-map/facet/fixture-profile.js'; +import { + FixtureInputs, + withFixtureIdentity, +} from '../../../tools/scenario-map/facet/fixture-inputs.js'; +import { expandCases } from '../../../tools/scenario-map/inventory/cases.js'; +import { findCandidates } from '../../../tools/scenario-map/path-coverage/candidates.js'; +import { + computeFingerprints, + type FingerprintTestInput, +} from '../../../tools/scenario-map/verdict/fingerprint.js'; +import type { + FacetSources, + FacetSubject, + InputFacet, +} from '../../../tools/scenario-map/facet/input-facet.js'; +import { SupportedLanguages } from 'gitnexus-shared'; +import { layer1Files } from '../../../tools/scenario-map/path-coverage/prefilter.js'; +import { isLanguageAvailable } from '../../../src/core/tree-sitter/parser-loader.js'; + +const ROOT = 'gitnexus/test/fixtures'; +const FILE = 'gitnexus/test/integration/example.test.ts'; +const sources = (files: Record): FacetSources => ({ + read: (file) => files[file] ?? null, + list: (dir) => + Object.keys(files) + .filter((file) => file.startsWith(`${dir}/`)) + .sort(), +}); +const subjects = (source: string): FacetSubject[] => + expandCases([{ path: FILE, source }]).sites.flatMap((site) => + (site.instances ?? []).map((instance, index) => ({ + id: `${site.fullName}:${index}`, + file: site.file, + record: site.record, + instance, + executedFiles: [], + })), + ); +const FIXTURES = { + [`${ROOT}/ts/main.ts`]: 'export function double(x: number) { return x + x; }', + [`${ROOT}/renamed/main.ts`]: + '// renamed\nexport function twice(value: number) { return value + value; }', + [`${ROOT}/py/main.py`]: 'def twice(value):\n return value + value\n', + [`${ROOT}/loop/main.ts`]: + 'export function twice(value: number) { while (value > 0) { value--; } return value; }', +}; + +// Exercise the real native providers, including standalone COBOL and embedded Vue scripts. +const LANGUAGE_FIXTURES: Record = { + typescript: ['ts', 'function twice(x: number) { return x + x; }'], + javascript: ['js', 'function twice(x) { return x + x; }'], + python: ['py', 'def twice(x):\n return x + x\n'], + go: ['go', 'package main\nfunc twice(x int) int { return x + x }'], + rust: ['rs', 'fn twice(x: i32) -> i32 { return x + x; }'], + java: ['java', 'class Sample { static int twice(int x) { return x + x; } }'], + csharp: ['cs', 'class Sample { static int Twice(int x) { return x + x; } }'], + c: ['c', 'int twice(int x) { return x + x; }'], + cpp: ['cpp', 'int twice(int x) { return x + x; }'], + kotlin: ['kt', 'fun twice(x: Int): Int { return x + x }'], + php: ['php', ' Int { return x + x }'], + dart: ['dart', 'int twice(int x) { return x + x; }'], + zig: ['zig', 'fn twice(x: i32) i32 { return x + x; }'], + 'objective-c': [ + 'm', + '@interface Sample\n- (int)twice:(int)x;\n@end\n@implementation Sample\n- (int)twice:(int)x { return x + x; }\n@end', + ], + vue: [ + 'vue', + '', + ], + cobol: [ + 'cbl', + ' IDENTIFICATION DIVISION.\n PROGRAM-ID. SAMPLE.\n PROCEDURE DIVISION.\n MAIN-PARA.\n DISPLAY "HI".\n STOP RUN.\n', + ], +}; + +it.each(Object.entries(LANGUAGE_FIXTURES))( + 'profiles real %s fixtures with the ingestion provider', + async (language, [extension, content]) => { + const root = `${ROOT}/language-${language}`; + const profiler = new FixtureProfiler(sources({ [`${root}/main.${extension}`]: content })); + const result = await profiler.profile([root]); + if ( + language !== SupportedLanguages.Cobol && + !isLanguageAvailable(language as SupportedLanguages, `main.${extension}`) + ) { + expect(result.contentKey).not.toBeNull(); + expect(result.unresolved.length).toBeGreaterThan(0); + return; + } + expect(result.unresolved).toEqual([]); + expect(result.languages).toEqual([language]); + expect(result.features.some(([feature]) => feature.startsWith('def:'))).toBe(true); + expect(result.contentKey).not.toBeNull(); + }, +); + +describe('fixture similarity', () => { + it('retrieves renamed and cross-language fixtures, without conflating a loop with straight-line code', async () => { + const profiler = new FixtureProfiler(sources(FIXTURES)); + const [ts, renamed, py, loop] = await Promise.all( + ['ts', 'renamed', 'py', 'loop'].map((name) => profiler.profile([`${ROOT}/${name}`])), + ); + expect(compareFixtureProfiles(ts, renamed)).toEqual({ + kind: 'structure', + score: 1, + crossLanguage: false, + }); + expect(compareFixtureProfiles(ts, py)).toEqual({ + kind: 'structure', + score: 1, + crossLanguage: true, + }); + expect(compareFixtureProfiles(ts, loop)).toBeNull(); + expect(new Set([ts.contentKey, renamed.contentKey, py.contentKey]).size).toBe(3); + const candidate = (id: string, fixture: typeof ts, checks = 'assertion') => ({ + id, + blocks: [id.charCodeAt(0)], + concurrent: false, + checksContext: id, + bodyKey: id, + checks, + fixture, + }); + const found = findCandidates([ + candidate('a', ts), + candidate('b', py), + candidate('c', loop), + candidate('d', py, 'different'), + ]); + expect(found.pairs).toHaveLength(1); + expect(found.pairs[0]).toMatchObject({ + a: 'a', + b: 'b', + sources: ['fixture'], + fixtureSimilarity: { crossLanguage: true }, + }); + expect(found.stats.bySource.fixture).toBe(1); + const report = fixtureSimilarityReport( + [ + { id: 'a', fixture: ts }, + { id: 'b', fixture: py }, + { id: 'another', fixture: ts }, + ], + found.pairs, + ); + expect(report.summary).toMatchObject({ + tests: 3, + profiles: 2, + matches: 1, + crossLanguageMatches: 1, + }); + expect(report.profiles.find((entry) => entry.profile === ts)?.tests).toEqual(['a', 'another']); + expect(findCandidates([candidate('a', ts), candidate('b', py)], () => false).pairs).toEqual([]); + }); + + it('keeps file multiplicity, layout, and non-source assets in exact input identity', async () => { + const profiler = new FixtureProfiler( + sources({ + [`${ROOT}/a/main.ts`]: FIXTURES[`${ROOT}/ts/main.ts`], + [`${ROOT}/b/main.ts`]: FIXTURES[`${ROOT}/ts/main.ts`], + [`${ROOT}/c/nested/main.ts`]: FIXTURES[`${ROOT}/ts/main.ts`], + [`${ROOT}/d/main.ts`]: FIXTURES[`${ROOT}/ts/main.ts`], + [`${ROOT}/d/package.json`]: '{"type":"module"}', + [`${ROOT}/e/one.ts`]: FIXTURES[`${ROOT}/ts/main.ts`], + [`${ROOT}/e/two.ts`]: FIXTURES[`${ROOT}/ts/main.ts`], + }), + ); + const [a, b, c, d, e] = await Promise.all( + ['a', 'b', 'c', 'd', 'e'].map((name) => profiler.profile([`${ROOT}/${name}`])), + ); + expect(compareFixtureProfiles(a, b)?.kind).toBe('content'); + expect(new Set([a.contentKey, c.contentKey, d.contentKey, e.contentKey]).size).toBe(4); + expect(compareFixtureProfiles(a, e)).toBeNull(); + }); + + it('reports missing, malformed, and oversized fixtures instead of matching partial profiles', async () => { + const profiler = new FixtureProfiler( + sources({ + ...FIXTURES, + [`${ROOT}/broken/main.ts`]: 'export function () { return (', + [`${ROOT}/large/main.ts`]: ' '.repeat(200_001), + }), + ); + const baseline = await profiler.profile([`${ROOT}/ts`]); + for (const root of ['missing', 'broken', 'large']) { + const result = await profiler.profile([`${ROOT}/${root}`]); + if (root === 'broken') { + expect(result.contentKey).not.toBeNull(); + expect(compareFixtureProfiles(result, result)?.kind).toBe('content'); + } else expect(result.contentKey).toBeNull(); + expect(result.unresolved.length).toBeGreaterThan(0); + expect(compareFixtureProfiles(result, baseline)).toBeNull(); + } + }); +}); + +it('retains fixture partners in diff selection even when their setup and callbacks differ', () => { + const root = mkdtempSync(path.join(tmpdir(), 'fixture-similarity-')); + try { + const files = ['test/integration/a.test.ts', 'test/integration/b.test.ts']; + files.forEach((file, i) => { + const target = path.join(root, 'gitnexus', file); + mkdirSync(path.dirname(target), { recursive: true }); + writeFileSync( + target, + `import path from 'node:path'; + beforeAll(() => setup(${i})); + it('test', () => { const content${i} = readFileSync(path.join(__dirname, '..', 'fixtures', '${i}.ts')); expect(content${i}).toBeDefined(); });`, + ); + mkdirSync(path.join(root, 'gitnexus/test/fixtures'), { recursive: true }); + writeFileSync( + path.join(root, `gitnexus/test/fixtures/${i}.ts`), + `function f${i}() { return 1; }`, + ); + }); + expect(layer1Files(root, files, new Set([files[0]]))).toEqual({ files, skipped: [] }); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); + +describe('fixture input discovery', () => { + it('does not resolve an unknown helper/callback argument using a shadowed outer constant', () => { + const inputs = new FixtureInputs(sources(FIXTURES)); + const [subject] = subjects(` + const root = '${ROOT}/ts'; + const load = (root) => runPipelineFromRepo(root); + it('unknown', () => { load(dynamicRoot()); values.forEach((root) => readFileSync(root)); }); + `); + expect(inputs.resolve(subject).roots).toEqual([]); + expect(inputs.resolve(subject).unresolved).toHaveLength(2); + }); + it('follows imported roots and local setup constants without collecting assertion strings', async () => { + const inputs = new FixtureInputs( + sources({ + ...FIXTURES, + 'gitnexus/test/integration/helpers.ts': `import path from 'node:path'; export const FIXTURES = path.resolve(__dirname, '..', 'fixtures');`, + }), + ); + const [subject] = subjects(` + import path from 'node:path'; import { FIXTURES } from './helpers.js'; + describe('suite', () => { + beforeAll(() => { const root = path.join(FIXTURES, 'ts'); runPipelineFromRepo(root); }); + it('one', () => { expect(result).toContain('${ROOT}/py'); }); + }); + `); + expect(inputs.resolve(subject)).toEqual({ roots: [`${ROOT}/ts`], unresolved: [] }); + expect((await inputs.profile(subject))?.languages).toEqual(['typescript']); + }); + + it('resolves per-row directories and both calls to an imported helper', () => { + const inputs = new FixtureInputs( + sources({ + ...FIXTURES, + 'gitnexus/test/integration/helpers.ts': `import path from 'node:path'; export function analyzeFixture(name) { return runPipelineFromRepo(path.join(__dirname, '..', 'fixtures', name)); }`, + }), + ); + const rows = subjects(`import path from 'node:path'; it.each(['ts','py'])('row %s', (name) => { + const root = path.join(__dirname, '..', 'fixtures', name); runPipelineFromRepo(root); expect(result).toBeDefined(); + });`); + expect(rows.map((row) => inputs.resolve(row).roots)).toEqual([[`${ROOT}/ts`], [`${ROOT}/py`]]); + const [helper] = subjects(`import { analyzeFixture } from './helpers.js'; it('both', () => { + analyzeFixture('ts'); analyzeFixture('py'); expect(result).toBeDefined(); + });`); + expect(inputs.resolve(helper)).toEqual({ roots: [`${ROOT}/py`, `${ROOT}/ts`], unresolved: [] }); + }); + + it('handles file URL inputs and exposes dynamically chosen inputs', () => { + const inputs = new FixtureInputs(sources(FIXTURES)); + const [known, unknown] = + subjects(`it('url', () => { readFileSync(new URL('../fixtures/ts/main.ts', import.meta.url)); }); + it('dynamic', () => { runPipelineFromRepo(selectFixture()); });`); + expect(inputs.resolve(known)).toEqual({ roots: [`${ROOT}/ts/main.ts`], unresolved: [] }); + expect(inputs.resolve(unknown).unresolved).toEqual([ + expect.stringContaining('unresolved fixture input'), + ]); + }); + + it('does not crash on spread arguments and resolves aliased path and ingestion imports', () => { + const inputs = new FixtureInputs(sources(FIXTURES)); + const [spread, alias] = subjects(` + import { join as combine } from 'node:path'; import { runPipelineFromRepo as ingest } from '../../src/core/ingestion/pipeline.js'; + it('spread', () => { runPipelineFromRepo(...args); }); + it('alias', () => { ingest(combine(__dirname, '..', 'fixtures', 'ts')); }); + `); + expect(inputs.resolve(spread).unresolved).toHaveLength(1); + expect(inputs.resolve(alias)).toEqual({ roots: [`${ROOT}/ts`], unresolved: [] }); + }); + + it('handles bigint rows and keeps hook constants separate from test-local shadowing', () => { + const inputs = new FixtureInputs(sources(FIXTURES)); + const rows = + subjects(`import path from 'node:path'; const root = path.join(__dirname, '..', 'fixtures', 'ts'); + beforeAll(() => runPipelineFromRepo(root)); + it.each([1n, 2n])('bigint %s', (n) => { const root = 'not-a-fixture'; expect(n).toBeDefined(); });`); + expect(rows.map((row) => inputs.resolve(row))).toEqual([ + { roots: [`${ROOT}/ts`], unresolved: [] }, + { roots: [`${ROOT}/ts`], unresolved: [] }, + ]); + }); +}); + +it.each([ + ['ts', 'py', 'input-shape-differs'], + ['broken', 'broken', 'input-unresolved'], +])( + 'does not confirm %s/%s fixtures despite equal paths, assertions, and clean traces', + async (left, right, rule) => { + const profiler = new FixtureProfiler( + sources({ ...FIXTURES, [`${ROOT}/broken/main.ts`]: 'function () {' }), + ); + const [a, b] = await Promise.all( + [left, right].map((name) => profiler.profile([`${ROOT}/${name}`])), + ); + const facet: InputFacet = { + fingerprint: 'same', + parts: { language: '-', source: [], fixtures: [], data: [], calls: [], order: [] }, + discarded: [], + unresolved: [], + skeleton: [], + }; + const test = (id: string, profile: typeof a): FingerprintTestInput => ({ + id, + fingerprintEligible: true, + passed: true, + dupeOk: null, + joinProblems: [], + inventoryUnresolved: [], + provenance: { known: true, nativeEntries: [], workerSpawns: [] }, + concurrent: false, + checksContext: 'same', + checks: 'same', + unsupportedForms: [], + unresolvedSelectors: [], + facet: withFixtureIdentity(facet, profile), + }); + const result = computeFingerprints({ + tests: [test('a', a), test('b', b)], + pairs: [ + { + a: 'a', + b: 'b', + group: 'g', + identicalBlocks: true, + pathUnattributable: false, + trace: { status: 'clean', reasons: [] }, + }, + ], + }); + expect(result.pairs[0].verdict).not.toBe('duplicate'); + expect(result.pairs[0].rule).toBe(rule); + }, +); diff --git a/gitnexus/test/unit/scenario-map/runtime-fixtures.test.ts b/gitnexus/test/unit/scenario-map/runtime-fixtures.test.ts new file mode 100644 index 000000000..a27b8545c --- /dev/null +++ b/gitnexus/test/unit/scenario-map/runtime-fixtures.test.ts @@ -0,0 +1,315 @@ +import { afterEach, expect, it } from 'vitest'; +import { + mkdtempSync, + mkdirSync, + readFile, + readFileSync, + rmSync, + symlinkSync, + writeFileSync, +} from 'node:fs'; +import { readFile as readAsync } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { pathToFileURL } from 'node:url'; +import { FixtureCapture } from '../../../tools/scenario-map/path-coverage/fixture-capture.js'; +import { fixtureCapturePlugin } from '../../../tools/scenario-map/path-coverage/fixture-plugin.js'; +import { + CoverageAttributor, + CoverageInterner, + type RawCoverageRecord, + type WindowDelta, +} from '../../../tools/scenario-map/path-coverage/records.js'; +import { + profileRuntimeFixtures, + runtimeFixtureFor, +} from '../../../tools/scenario-map/facet/runtime-fixtures.js'; +import { compareFixtureProfiles } from '../../../tools/scenario-map/facet/fixture-profile.js'; +import { buildWorkFile } from '../../../tools/scenario-map/path-coverage/run-coverage.js'; +import { + mergeCoverageSlices, + type CoverageSliceFile, +} from '../../../tools/scenario-map/path-coverage/slices.js'; + +const roots: string[] = []; +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); +const repo = (files: Record) => { + const root = mkdtempSync(path.join(tmpdir(), 'runtime-fixture-')); + roots.push(root); + for (const [name, content] of Object.entries(files)) { + const file = path.join(root, name); + mkdirSync(path.dirname(file), { recursive: true }); + writeFileSync(file, content); + } + return root; +}; +const delta = (fixtures: string[] = []): WindowDelta => ({ + blocks: new Set(), + scripts: new Set(), + natives: new Set(), + spawns: [], + fixtures, +}); +const record = (capture: FixtureCapture): RawCoverageRecord => { + const owner = new CoverageAttributor(); + owner.testStart('test', ['file'], false, delta()); + owner.testEnd('test', delta(capture.drain())); + return { + ...owner.record('/prep/gitnexus/test/input.test.ts', new CoverageInterner([], new Set())), + fixtureSnapshots: capture.snapshots, + }; +}; +const profile = async (raw: RawCoverageRecord, index = 0, concurrent = false) => { + const result = runtimeFixtureFor( + raw, + raw.tests[index], + (await profileRuntimeFixtures([raw])).get(raw), + concurrent, + ); + if (result === null) throw new Error('expected captured fixture'); + return result; +}; +const TS = 'export function twice(x: number) { return x + x; }'; +const PY = 'def twice(x):\n return x + x\n'; + +it('retains generated, deleted multilingual input contents and distinguishes control flow', async () => { + const profiles = []; + for (const files of [ + { 'main.ts': TS }, + { 'main.py': PY }, + { 'main.ts': 'export function twice(x: number) { while(x > 0) { x--; } return x; }' }, + ]) { + const root = repo(files); + const capture = new FixtureCapture(); + capture.repository(root); + const raw = record(capture); + rmSync(root, { recursive: true }); + profiles.push(await profile(JSON.parse(JSON.stringify(raw)))); + } + expect(compareFixtureProfiles(profiles[0], profiles[1])).toMatchObject({ + kind: 'structure', + crossLanguage: true, + }); + expect(compareFixtureProfiles(profiles[0], profiles[2])).toBeNull(); + expect(profiles.map((p) => p.unresolved)).toEqual([[], [], []]); + expect(profiles[0].contentKey).not.toBe(profiles[1].contentKey); +}); + +it('keeps input versions at the same path and ignores random temporary root names', async () => { + const first = repo({ 'main.ts': TS }); + const second = repo({ 'main.ts': TS }); + const capture = new FixtureCapture(); + capture.repository(first); + const a = record(capture); + capture.repository(second); + const b = record(capture); + writeFileSync(path.join(first, 'main.ts'), TS.replace('x + x', 'x + 1')); + capture.repository(first); + const c = record(capture); + expect((await profile(a)).contentKey).toBe((await profile(b)).contentKey); + expect((await profile(a)).contentKey).not.toBe((await profile(c)).contentKey); +}); + +it('compares repeat inputs by content rather than temporary root names', async () => { + const capture = new FixtureCapture(); + capture.repository(repo({ 'main.ts': TS })); + const cold = capture.drain(); + capture.repository(repo({ 'main.ts': TS })); + const raw = record(capture); + raw.tests[0].all.fixtures = [...cold, ...(raw.tests[0].warm.fixtures ?? [])]; + expect((await profile(raw)).unresolved).toEqual([]); + capture.repository(repo({ 'main.ts': TS.replace('x + x', 'x + 1') })); + raw.tests[0].all.fixtures.push(...capture.drain()); + expect((await profile(raw)).unresolved).toContain( + 'runtime fixture inputs changed between attempts', + ); +}); + +it('filters module loading and production housekeeping from direct-read capture', () => { + const capture = new FixtureCapture(); + expect( + capture.accepts( + 'Error\n at read (/repo/tools/scenario-map/path-coverage/fixture-capture.ts:1:1)\n at helper (/repo/test/helpers/fixture.ts:8:1)', + ), + ).toBe(true); + expect( + capture.accepts( + 'Error\n at load (/repo/src/core/parser-loader.ts:8:1)\n at test (/repo/test/example.ts:9:1)', + ), + ).toBe(false); + expect(capture.accepts('Error\n at load (/repo/node_modules/vite/test.js:8:1)')).toBe(false); +}); + +it('marks bounded captures incomplete instead of hashing a truncated repository as complete', async () => { + const root = repo( + Object.fromEntries(Array.from({ length: 201 }, (_, i) => [`file${i}.txt`, 'sample'])), + ); + const capture = new FixtureCapture(); + capture.repository(root); + const p = await profile(record(capture)); + expect(p.contentKey).toBeNull(); + expect(p.unresolved).toContain('runtime repository capture limit'); + expect(p.files.length).toBeLessThanOrEqual(200); +}); + +it('bounds large returned buffers before allocating a decoded copy', () => { + const capture = new FixtureCapture(); + const buffer = Buffer.alloc(200_000); + buffer.toString = () => { + throw new Error('must not decode oversized input'); + }; + expect(() => capture.read('/tmp/large.ts', buffer)).not.toThrow(); + expect(Object.values(capture.snapshots)).toMatchObject([ + { files: [], unresolved: ['oversized text'] }, + ]); +}); + +it('captures real sync, promise, callback and URL reads without changing results or errors', async () => { + const root = repo({ 'main.ts': TS }); + const file = path.join(root, 'main.ts'); + const capture = new FixtureCapture(); + const restore = capture.install(); + try { + expect(readFileSync(file, 'utf8')).toBe(TS); + expect((await readAsync(pathToFileURL(file))).toString()).toBe(TS); + expect( + await new Promise((resolve, reject) => + readFile(file, 'utf8', (error, text) => (error ? reject(error) : resolve(text))), + ), + ).toBe(TS); + expect(() => readFileSync(path.join(root, 'missing.ts'))).toThrow(); + await expect(readAsync(path.join(root, 'missing.ts'))).rejects.toThrow(); + } finally { + restore(); + } + const p = await profile(record(capture)); + expect(p.languages).toEqual(['typescript']); + expect(p.files).toEqual([file]); + expect(p.unresolved).toEqual([]); +}); + +it('reports excluded, missing, oversized and symbolic inputs without leaking contents', async () => { + const root = repo({ + '.env': 'never-retain-this', + 'large.ts': 'x'.repeat(200_000), + 'main.ts': TS, + }); + if (process.platform !== 'win32') + symlinkSync(path.join(root, 'main.ts'), path.join(root, 'alias.ts')); + const capture = new FixtureCapture(); + capture.repository(root); + capture.repository(path.join(root, 'missing')); + const raw = record(capture); + expect(JSON.stringify(raw)).not.toContain('never-retain-this'); + const p = await profile(raw); + expect(p.contentKey).toBeNull(); + expect(p.unresolved.join()).toMatch(/environment/); + expect(p.unresolved.join()).toMatch(/size limit/); + if (process.platform !== 'win32') expect(p.unresolved.join()).toMatch(/symbolic/); + expect(p.unresolved.join()).toMatch(/unreadable/); +}); + +it('attributes nested setup to its suite and retains warm-attempt and concurrent gaps', async () => { + const capture = new FixtureCapture(); + const attributor = new CoverageAttributor(); + const inner = repo({ 'main.ts': TS }); + capture.inSuite('inner', () => capture.repository(inner)); + attributor.testStart('a', ['file', 'outer', 'inner'], false, delta(capture.drain())); + attributor.testEnd('a', delta()); + attributor.testStart('b', ['file', 'outer'], false, delta()); + attributor.testEnd('b', delta()); + const raw = { + ...attributor.record('/prep/gitnexus/test/input.test.ts', new CoverageInterner([], new Set())), + fixtureSnapshots: capture.snapshots, + }; + expect((await profile(raw)).unresolved).toEqual([]); + expect(runtimeFixtureFor(raw, raw.tests[1], undefined, false)).toBeNull(); + expect((await profile(raw, 0, true)).contentKey).toBeNull(); + const profiles = await profileRuntimeFixtures([raw]); + const work = buildWorkFile({ + mode: { kind: 'normal' }, + preparedRoot: '/prep', + repeats: 1, + unhandledErrors: [], + raws: [raw], + fixtureProfiles: profiles, + modules: [ + { + file: 'test/input.test.ts', + errors: [], + tests: raw.tests.map((t) => ({ + taskId: t.taskId, + fullName: t.taskId, + location: undefined, + concurrent: false, + state: 'passed', + })), + }, + ], + }); + expect(work.tests[0].runtimeFixture?.languages).toEqual(['typescript']); + expect(work.tests[1].runtimeFixture).toBeNull(); + raw.tests[0].all.fixtures = ['missing']; + expect((await profile(raw)).unresolved.join()).toContain('changed between attempts'); +}); + +it('leaves old records compatible and refuses missing snapshot evidence', async () => { + const capture = new FixtureCapture(); + capture.repository(repo({ 'main.ts': TS })); + const raw = record(capture); + expect(runtimeFixtureFor(raw, raw.tests[0], undefined, false)?.contentKey).toBeNull(); + delete raw.fixtureSnapshots; + expect(runtimeFixtureFor(raw, raw.tests[0], undefined, false)).toBeNull(); +}); + +it('preserves capture versions and rejects mixed instrumented and archived coverage slices', () => { + const slice = (index: number, captured: boolean): CoverageSliceFile => ({ + schemaVersion: 1, + index, + of: 2, + layer1: [], + mutation: 'none', + changed: null, + revision: null, + work: { + schemaVersion: 1, + ...(captured ? { fixtureCaptureVersion: 1 } : {}), + mode: 'normal', + extraRoots: [], + repeats: 1, + blocks: [], + files: [], + tests: [], + unhandledErrors: [], + }, + }); + expect( + mergeCoverageSlices([[], []], [slice(0, true), slice(1, true)]).fixtureCaptureVersion, + ).toBe(1); + expect( + mergeCoverageSlices([[], []], [slice(0, false), slice(1, false)]).fixtureCaptureVersion, + ).toBeUndefined(); + expect(() => mergeCoverageSlices([[], []], [slice(0, true), slice(1, false)])).toThrow( + 'not the run', + ); +}); + +it('instruments the pipeline entry with a source map and rejects entry point drift', async () => { + const plugin = fixtureCapturePlugin('/prep/src/core/ingestion/pipeline.ts'); + const transform = plugin.transform; + if (typeof transform !== 'function') throw new Error('missing transform'); + const result = await Reflect.apply(transform, {}, [ + 'export const runPipelineFromRepo = async (root: string) => { return root; };', + '/prep/src/core/ingestion/pipeline.ts', + ]); + expect(result.code).toContain('globalThis["__gitnexusScenarioFixtureCapture"]?.(root)'); + expect(JSON.parse(result.map).sources).toEqual(['/prep/src/core/ingestion/pipeline.ts']); + expect(() => + Reflect.apply(transform, {}, [ + 'export const changed = 1;', + '/prep/src/core/ingestion/pipeline.ts', + ]), + ).toThrow('entry point'); +}); diff --git a/gitnexus/tools/scenario-map/README.md b/gitnexus/tools/scenario-map/README.md new file mode 100644 index 000000000..517228c1e --- /dev/null +++ b/gitnexus/tools/scenario-map/README.md @@ -0,0 +1,174 @@ +# Scenario-map fixture comparison + +The `plan` stage discovers source fixtures read by tests and adds fixture similarity +to candidate retrieval. Run it through the existing CLI: + +```sh +cd gitnexus +npm run test:scenario-map -- plan --work /tmp/gitnexus-scenario-map --mutation none +``` + +This runs coverage for the test inventory. To reuse coverage slices from the same +checkout, add `--coverage /path/to/coverage-slices`. +All slices must use the same capture version; mixing old coverage with newly instrumented +coverage is rejected because instrumentation changes V8 block offsets. + +## Evidence + +- `fixtures.json`: fixture paths, languages, extraction gaps, counted features, + associated tests, and similarity matches. Profiles are deduplicated across tests. +- `coverage/candidates.json`: pairs retrieved through the `fixture` source, with the score, + content/structure match kind, and whether their languages differ. +- `layer2.json`: per-test fixture evidence and input identity used by later stages. +- Fresh coverage records also contain `runtimeFixture` profiles. Raw records retain + bounded input snapshots, so profiling does not depend on temporary files surviving. + +Discovery follows constants, imported test helpers, setup hooks, static string rows, +path joins, and file URLs for ingestion, file-read, and copy calls. It reads source +corpora anywhere under `gitnexus/test/`, including integration-local fixtures. +It never executes a helper to discover its inputs. Diff-scoped runs retain these +tests until their fixture profiles can be compared. + +Fresh coverage also captures generated and dynamically selected inputs. A coverage-only +Vite transform snapshots the repository passed to `runPipelineFromRepo` before ingestion, +including runs that dispatch parsing to workers. It preserves relative file layout and +non-source assets. Pass-through wrappers retain the returned contents of synchronous, +promise, and callback `readFile` calls made directly by tests or test helpers, including +file URLs. Production files on disk are not modified by instrumentation. + +Snapshots follow coverage windows: the last test attempt and its suite setup feed that +test's profile. Setup hooks carry their suite identity across async calls, so nested +setup does not leak into sibling suites. Changed input contents across attempts, +concurrent tests, and reads outside attributable windows remain explicit gaps. Temporary +directory names do not change input identity. The planner parses each distinct captured +input through the same language providers used for static fixtures, after the test ends. + +The profiler reuses GitNexus's language providers for declarations, reference kinds, +call forms, imports, and control-flow graphs. Counts preserve multiplicity. Three +rounds of directed, labeled CFG-neighbor refinement give a topology signature +independent of block IDs and source positions. This is a structural heuristic, +not a graph-isomorphism or program-equivalence proof. + +Two tests become fixture candidates only when their assertion-only keys match and +their fixture profiles have weighted multiset Jaccard similarity of at least 0.90. +CFG signatures have weight 4; other features have weight 1. Exact content matches +also retrieve candidates. File profiles and pair scores are cached within a run. + +Similarity omits identifier spelling and literal values. **Exact fixture text and +relative file layout are separately included in the input identity**, including +non-source assets. A similar fixture cannot make different inputs a confirmed +duplicate. Cross-language cases often provide intentional provider coverage; +a match is a review lead, not a recommendation to remove either test. + +## Consolidation opportunities, including partial matches + +The `plan` stage also writes `consolidation.html` and `consolidation.json`. These +compare three independent axes: captured fixture contents, executed JavaScript +blocks, and individual assertions. A fixture match alone does not create a lead. +The HTML report can be filtered by test name or recommendation. It shows both +directions of overlap and the assertions, blocks, and fixture spans that remain +unique. Groups represent repeated evidence without enumerating every test pair. + +Fixture text is compared locally with the pinned **jscpd 5.4.2** development +dependency. Exact, consistently renamed, and near-miss fragments contribute to +byte overlap; comments and whitespace are ignored by the clone engine. The +coverage denominator conservatively includes all non-whitespace captured bytes. +Overlapping fragments count once; near-miss scores discount their contribution. +Representative exact/renamed clone links form families, so a missing direct link +does not hide another member. When representative spans overlap with different +boundaries, pairs that pass the assertion and execution gates are compared directly +in a separate two-input scan. This avoids inventing byte mappings or losing a partial +match when the corpus chooses a longer representative. The raw pair reports and +verification count are retained. Near-miss links are never treated as transitive. +No source is uploaded and no embedding model is downloaded. + +Source snapshots are content-addressed in `coverage.json` and merged across slices. +The external engine's raw report and exported inputs are under `fixture-clones/`. +The report records captured/scanned file counts and formats: unsupported formats +and fragments below the 12-token threshold can be missed. Source identities still +recognize identical complete inputs independently of tokenization. + +Across languages, GitNexus provider features supply a separate, weaker +`share-language-scenario` suggestion. Full matches require 0.90 weighted Jaccard; +partial matches require shared CFG topology and 0.90 feature containment in one +direction. These are structural scenario leads, not cross-language semantic proofs. +Each language remains a test case when consolidating into a shared harness. + +Assertions retain exact expected literals, row values, snapshots, negation and +selectors. Single preceding `const` bindings and scoped callback parameters are +traced, so `find(edge => edge.target === 'save')` does not equal a `getName` query. +Unknown expected values or local origins cannot become equal assertion atoms. +Unknown checks remain in overlap denominators. This comparison does not execute +arbitrary expected-value expressions or establish helper semantic equivalence. + +Default retrieval requires 0.50 fixture containment and 0.50 executed-block +containment in either direction, plus 0.25 exact-assertion containment. Fully known +assertions with different expected values can instead qualify through 0.80 +predicate-shape containment, producing parameterization leads. Thresholds and +missing evidence are included in the JSON. This supports sharing a subset of +checks while retaining the remaining assertions and fixture branches. + +Executed blocks exclude uncovered V8 ranges. They describe observed coverage, +not call order or every possible execution. Native work, spawned workers, input +capture gaps, unsupported checks and inventory joins stay visible as limitations. +Even a complete match is a statement about the captured run; consolidation output +does not authorize deleting tests or bypass existing duplicate-confirmation gates. + +Run a complete capture and generate a report (or use the existing coverage-slice +workflow to distribute the capture): + +```sh +node --import tsx tools/scenario-map/scenario-map.ts coverage \ + --index 0 --of 1 --work /tmp/scenario-coverage --mutation none --concurrency 2 +node --import tsx tools/scenario-map/scenario-map.ts plan \ + --coverage /tmp/scenario-coverage --work /tmp/scenario-plan --mutation none +node --import tsx tools/scenario-map/consolidation/verify-controls.ts \ + /tmp/scenario-plan/consolidation.json +``` + +The acceptance command checks all 28 pairs among eight real pipeline controls: +15 exact/renamed/partial/cross-language opportunities and 13 negative pairs. It +also checks that unique evidence and runtime limitations survive in the report. + +## Capture and proof limits + +Archived coverage without runtime snapshots still uses static discovery. Unresolved static +paths (including generated inputs) require a fresh coverage run to obtain runtime evidence. +Missing parsers, parse errors, lossy text +decoding, and extraction limits are recorded as unresolved. Partial profiles do +not retrieve structural matches. Identical, completely read text can still retrieve +an exact-content candidate when parsing fails; that failure remains unresolved +for duplicate confirmation. Incomplete reads never establish input identity. Existing +path, context, assertion, runtime-value, and passing-test checks still apply. + +Runtime capture retrieves review candidates; it does not establish exhaustive input +coverage. Streams, file-descriptor reads, inputs generated inside child processes, and +repository mutations after the entry snapshot are not fully observed. Runtime evidence +cannot clear static input gaps or confirm duplicates by itself. A complete static profile +corroborated by identical runtime contents retains its existing confirmation checks. +Skipped or failed tests cannot establish duplicate safety. + +Structural fingerprints inherit provider behavior, including parser or CFG instability; +exact text and external clone spans are separate evidence. The comparison uses pre-resolution features. It does not prove cross-file binding, +data-flow, operator, or runtime equivalence. Providers without CFG support contribute +their available capture features. Identical counts can produce false-positive +review leads, and different provider representations can miss similar fixtures. + +Limits per profile: 200 files, fewer than 200,000 characters per file, 2,000,000 +characters total, and 2,000 lines per CFG function. Runtime capture additionally bounds +each test file to 20 MB of unique snapshot text and 2,000 events; repository snapshots +visit at most 1,000 entries and 30 directory levels. Symlinks are reported as incomplete; +`.git`, `.gitnexus`, and `node_modules` directories are omitted. Helper traversal is bounded to +five calls and constant evaluation to eight steps. `.env` files other than +`.env.example` are excluded and reported as incomplete. + +Validate with: + +```sh +npm test -- test/unit/scenario-map --maxWorkers=2 +npm run typecheck:tests +# Fresh real-pipeline controls, including generated repositories deleted in setup: +node --import tsx tools/scenario-map/path-coverage/run-coverage.ts \ + --file test/integration/scenario-map-runtime-inputs.test.ts \ + --out /tmp/scenario-map-runtime-controls --concurrency 1 +``` diff --git a/gitnexus/tools/scenario-map/consolidation/assertions.ts b/gitnexus/tools/scenario-map/consolidation/assertions.ts new file mode 100644 index 000000000..674a1b85a --- /dev/null +++ b/gitnexus/tools/scenario-map/consolidation/assertions.ts @@ -0,0 +1,328 @@ +import * as t from '@babel/types'; +import { generate } from '@babel/generator'; +import { resolveName } from '../../test-dupes/test-dupes.js'; +import type { RowValue } from '../types.js'; +import { + abstractNode, + 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; + /** 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 entries = row.entries + .map(({ key, value }) => ({ key, value: canonicalRow(value) })) + .sort((a, b) => a.key.localeCompare(b.key)); + return entries.some((entry) => entry.value === undefined) + ? undefined + : { kind: 'object', entries }; + } + 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 entries: { key: string; value: 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; + entries.push({ key, value }); + } + return { kind: 'object', entries: entries.sort((a, b) => a.key.localeCompare(b.key)) }; + } + return undefined; +} + +/** Expand only single, preceding const bindings. Ambiguous scopes and mutable locals stay unknown. */ +function localOrigins( + subject: CheckSubject, + scope: Scope, + before: number, +): (node: t.Node) => t.Node { + const declarations = new Map(); + t.traverseFast(subject.record.fn.body, (node) => { + if (!t.isVariableDeclaration(node)) return; + for (const decl of node.declarations) { + if (!t.isIdentifier(decl.id)) continue; + const init = node.kind === 'const' && (decl.end ?? Infinity) < before ? decl.init : null; + declarations.set(decl.id.name, declarations.has(decl.id.name) ? null : (init ?? null)); + } + }); + let nextBinding = 0; + let visited = 0; + const expand = ( + node: t.Node, + seen = new Set(), + bindings = new Map(), + ): t.Node => { + if (++visited > 5000 || seen.size > 32) { + scope.unresolved.add('assertion origin expansion limit reached'); + const unknown = t.identifier('_unknown'); + unknown.name = ''; + return unknown; + } + const bound = t.isIdentifier(node) ? bindings.get(node.name) : undefined; + if (bound !== undefined) return bound; + if (t.isIdentifier(node) && scope.locals.has(node.name)) { + const init = declarations.get(node.name); + if (!init || seen.has(node.name)) return node; + return expand(init, new Set([...seen, node.name]), 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 = ``; + bindings.set((param as t.Identifier).name, marker); + return marker; + }); + } + const object = copy as unknown as Record; + 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, seen, bindings) : child)) + : t.isNode(value) + ? expand(value, seen, bindings) + : value; + } + return copy; + }; + return (node) => { + nextBinding = 0; + visited = 0; + return expand(node); + }; +} + +function preserveValues(node: t.Node, scope: Scope): t.Node { + const value = literal(node, scope); + if (value !== undefined) { + const marker = t.identifier('_value'); + marker.name = ``; + 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 (t.isTemplateLiteral(node)) { + const marker = t.identifier('_template'); + marker.name = '