GitNexus/gitnexus/tools/mutation/classify/classify.ts
Gergő Magyar bad2422aa1
feat: add the test scenario map and a Vitest 5 mutation runner (#3686)
* feat(ci): add advisory duplicate-test scan to the PR report

Adds `npm run test:dupes` (gitnexus/scripts/test-dupes.ts) and a
comment-only CI job that reports, for the lines a PR changes:

- same-test: same body and same setup as an existing test
- assertions-pinned: every assertion is a literal fn(...) -> matcher
  that another test already makes
- same-fixture-setup: a before* hook builds a fixture another describe
  already builds
- copied-helper: a top-level test helper identical to one elsewhere
- orphan-bench-gate: a bench script with --check that nothing runs

A case's identity includes its hooks, describe-level statements,
wrapper arguments, loop heads, each tables, referenced file-local
declarations and resolved imports, with fixture paths compared by
value. Extra context can only split a group, so identical bodies that
run against different fixtures, mocks or env are not reported.

The ci.yml job is continue-on-error and outside the CI Gate.
ci-report.yml renders its JSON into the sticky CI comment, keeping
only whitelisted kinds and strict path/line shapes because the
artifact comes from PR code. `// dupe-ok: <reason>` silences an
intentional repeat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(mutation): record Vitest 5 and Stryker 10 probe findings

Go/no-go probe for the Stryker Vitest 5 runner: name-filter separator, per-spec task ids, static mutant activation, TypeScript plugin loading in child processes, setup-file meta and hook ordering.

* feat(mutation): prepare a mutable copy and a Vitest mutation config

Prepared copy blanks TypeScript as-assertions (Stryker's instrumenter skips every TSAsExpression) with equal-length spaces, checked by AST equality, and relocates gitnexus-shared/src inside the package so it can be mutated. The mutation Vitest config resolves gitnexus-shared to the relocated sources and takes its test list from a scope file.

* feat(mutation): add a Stryker test runner for Vitest 5

Stryker's vitest-runner 10.0.0 runs zero tests per mutant under Vitest 5 because Vitest 5 matches testNamePattern against ' > '-joined names. This runner selects tests by Vitest task id per file, keeps the configured fork pool, shares the dry-run manifest across Stryker worker processes, attributes hook-time hits to the suite's tests, and records a per-test outcome class in sidecar files. Adds exact devDependencies @stryker-mutator/core and @stryker-mutator/api 10.0.0.

* feat(mutation): add global mutant identities and a span-map check

A global mutant identity (original repo path, original content hash, span, mutator, replacement) makes mutants comparable across Stryker runs. The span-map check proves every mutant of a prepared copy sits on the same span and text in the original source, apart from blanked as-assertions.

* feat(mutation): add a scope driver for Stryker runs

run-scope turns a scope file into a prepared copy, a generated Stryker config (vitest5 or command runner), a Stryker run and normalised results: global mutant ids, runner test ids, sidecar outcome classes, consistency flags and the span-map check.

* fix(mutation): count assertion kills only for killed runs

A run classified timeout, inconclusive, invalid or infrastructure can still carry assertion rows (a test failing after a swallowed hit-limit error, or under a retry). Those kills are not trustworthy, so assertionKilledBy is now empty unless the run's status is killed. Found by the conformance suite.

* feat(mutation): add the runner conformance suite (gate 2)

A fixture project under test/fixtures/mutation-conformance pins 23 runner behaviours (static and collection-time mutants, beforeAll attribution, disableBail, it.each row ids, outcome classes for runtime, hook, collection, timeout, infrastructure, retry and zero-test runs, NoCoverage, environment isolation, runner reuse and manifest sharing) in two scheduling profiles. The path map and scope driver accept an explicit gitnexus/test/fixtures/mutation-* root for it.

* feat(mutation): classify pure-JS unit tests for mutation runs

Walks each unit test's import closure (static imports plus statically resolvable dynamic imports, require and createRequire) and rejects tests that reach native packages, worker threads or child processes, read source or fixture files a prepared copy would break, or contain unresolvable dynamic imports. 247 of 911 unit test files are pure JS.

* feat(mutation): add gate 1 execution-path fidelity and the envelope record

Gate 1 compares a normal Vitest 5 run of the pure-JS category with the runner's unmutated Stryker run: test identities with locations, outcomes, effective config against an allowed-difference list, resolved module paths (everything inside the active sandbox) and a runtime load check for native bindings, worker construction and child processes. It passes on 246 files / 4541 tests and writes test/fixtures/mutation-oracle/envelope.json. The runtime check found commander loading child_process, so the classifier now rejects tests importing commander.

* feat(mutation): add the oracle's command-runner run and classifier

The Vitest 5 cross-check oracle runs its own Stryker run with the command runner (cleanTempDir false). A wrapper runs the scope's tests per mutant with an oracle reporter and writes a per-mutant record, and the oracle classifies every mutant into the KTD17 outcome classes with its own classifier. Oracle code imports no runner code.

* feat(mutation): add the oracle single-test launcher and cost benchmark

launch-single runs one test with one mutant in a fresh process inside the oracle's kept sandbox, selected by file:line plus an anchored name pattern, with the mutant installed before any import, an oracle setup file that proves the installed id and buckets hits (load and collection, hook, per-test), a wall-clock limit and a process-group kill. A launch is valid only when exactly the intended test ran. The benchmark projects the three-scope oracle sample's cost on 4 vCPU. A boundary test keeps oracle code from importing runner code.

* feat(mutation): add the oracle freeze and gate 3 compare

freeze samples mutants per scope with a recorded seed, tops up strata, counts stratum populations from the oracle's own coverage probe, launches every sampled mutant against every scope test (resumable), and writes a manifest keyed by content hashes of the scope's import closure, Vitest configs and prepare tool. compare checks freshness, inventories (tests and global mutant ids) before outcomes, every mutant's class and sampled per-test outcomes, allowing only the four KTD13 normalisations.

* feat(mutation): add gate 4 positive controls and determinism

Nine planted controls, selected from the scopes' own Stryker mutants by global id (three in relocated gitnexus-shared as-const tables), must be killed by exactly their expected killers. Two scheduling profiles (concurrency 1 with unlimited runner reuse, concurrency 3 with one mutant per runner process) must give identical outcome classes and kill sets.

* ci(mutation): add an advisory mutation-runner validation workflow

Runs the runner conformance suite (gate 2) on pull requests that touch the mutation tooling, its fixtures, the gitnexus manifest, lockfile or Vitest config. workflow_dispatch also runs gate 1, the gate 3 oracle compare (or a per-scope re-freeze whose manifests are uploaded, never committed), gate 4 and the oracle cost benchmark. All jobs are advisory. Documents the tooling in TESTING.md and the residual deltas against upstream StrykerJS.

* test(mutation): freeze the gate 3 oracle manifests and record Phase 0 results

Frozen Vitest 5 cross-check oracle manifests for the three pure-JS scopes (seed u7-gate3-1): shared.ts, schema-constants.ts and middleware.ts. Gate 3 passes: all 1,606 mutant classes and all 20,455 sampled per-test outcomes agree with the runner, 0 differences. Adds the approved Test Scenario Map design (updated with Phase 0 findings and gate results) and the Phase 0 implementation plan.

* refactor(mutation): simplify the Phase 0 tooling

Shared CLI/fs helpers for the non-oracle tools, an oracle-local util for isObject and atomic writes, formatMutateEntry and formatDuration reused, a typed isUnusableClass, and hot-path fixes: per-sidecar index in the gate 3 compare, lazy suite-chain in the runner's dry-run hit attribution, one Classifier per freeze, memoised display paths and Vitest lookups. Behaviour unchanged: unit tests, gate 3 compare (byte-identical report) and the conformance suite (23/23) pass.

* fix(review): apply review findings

- Move the duplicate-test scanner out of the published scripts/ directory to gitnexus/tools/test-dupes/ so it no longer ships in the npm package.
- Scope the orphan bench gate to touched lines, like every other scanner finding.
- Let the conformance environment case reach Stryker through an explicit childEnv override, so it exercises the runner's own __STRYKER_ACTIVE_MUTANT__ deletion (a negative check now fails it).
- Do not raise the zero-tests flag when a timeout outside-test row explains the zero.
- Run Stryker in its own process group and signal the group on cancellation.
- Fingerprint conformance inputs and refuse stale artefacts in --compare-only.
- Path-gate the mutation workflow on gitnexus-shared/package.json and the setup action; anchor the PR-report path filter with \A...\z.

* chore(security): mark the runner's provide/inject key as a gitleaks false positive

gitleaks' generic-api-key rule matched PROTOCOL_KEY = 'gitnexusStrykerVitest5' (a Vitest provide/inject key name). Allow the line inline and ignore the fingerprint in the commit that introduced it.

* test(mutation): re-freeze the schema-constants oracle manifest

Merging main changed the scope's import closure (template-arguments.ts),
so gate 3 reported re-freeze required. Re-frozen with seed u7-gate3-2:
72 sampled mutants, 3024 valid observations, 0 invalid. Gate 3 now
passes on all three scopes (shared 1232, schema-constants 72,
middleware 302 mutants) with 0 differences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(plans): add the Test Scenario Map Phase 1 plan

* chore(security): stop the gitleaks allowlist comment from quoting the key

The previous comment quoted the provide/inject key assignment and gitleaks
flagged the comment line itself (generic-api-key). Reword it and allowlist
that historical finding by fingerprint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(mutation): select single tests in the vitest5 runner and keep per-test classes

Phase 1 U1 (KTD2, KTD9, KTD16):
- driver scope accepts optional testIds, passed as the vitest5.testIds
  runner option; the dry run collects, maps ids to task ids, runs only
  those tests and fails before any mutant run on unknown ids
- the dry-run sidecar (schema 2) carries the hook marker: per mutant,
  the tests a suite hook attributed it to
- results.json (schema 2) keeps every sidecar attempt's per-test rows
  (perTest), attemptsDisagree and hookAttributedTests
- gate 2 gains a selection scope and hook-marker cases (26 cases hold)

Gate 3 hashed inputs are untouched.

* refactor(test-dupes): export Layer 0's case extraction for the scenario map

Phase 1 U2 (KTD6): export extract, extractCases, buildFrame, closure,
staticValue, suppressed and dupeOkReason, plus a per-case record with
the describe path, title, context hash, body, each-table node and
enclosing loop head. Layer 0's --all scan output is byte-identical
before and after (1464 files, 114 findings).

* test(mutation): refresh the gate 1 envelope after the runner extensions

Gate 1 re-ran over the current pure-JS category after U1 and passed with
0 differences: 251 files, 4667 tests. The classifier now rejects
conformance-compare.test.ts (imports the child_process-spawning
conformance runner) and php-namespace-extraction.test.ts (now reaches
tree-sitter through main), and accepts the three oracle unit tests.

* feat(scenario-map): add the eligible-test inventory, identity join and anchors

Phase 1 U3 (R1, R2, R15; KTD3, KTD7):
- eligibility = gate 1 envelope intersect a fresh classifier run, with the
  delta; controls mode enrols a fixture's *.case.ts files
- static expansion of it.each/test.each/test.for tables and literal for-of
  loops into per-row instances; anything not statically evaluable is
  unresolved (fail closed)
- a pure join of static instances to runtime records through Phase 0
  test ids; any count or name mismatch makes the tests unsupported
- continuity anchors (no line; row and body fingerprints) and the one
  production-file predicate shared by every later stage
- test-dupes: additive exports (shapeOf, roleOf, eachTableOf, ASTs, an
  isTest option); Layer 0 output byte-identical

Over the repo: 4667 runtime tests, 4597 joined one-to-one, 70 unsupported
(5 tables built at runtime), 0 mismatches.

* test(scenario-map): add the controls fixture project with expected verdicts

Phase 1 U17: one pair per design §8 pure-JS adversarial case (Math.round,
parseInt radix, localeCompare, normalize, Date boundary, regex
alternatives, numeric boundary with a constants-only module, an
expected-only difference, a derived value reaching toFixed, an
enum-member-only difference), an order-dependent pair, an object-key row,
and positive controls (verbatim duplicate, repo arithmetic, Set table
reads). expected.json was written from the design's stated outcomes.
The fixture has its own Vitest project and tsconfig; no repo project
collects it.

* feat(scenario-map): extract per-instance checks and context

Phase 1 U5 (R5, KTD6):
- checks: the shape of each expect(...).matcher(...) with literals
  abstracted and selector literals in the subject kept exact; snapshots
  hashed from inline text or the .snap entry; a documented supported
  matcher list, anything else unsupported; per-row for it.each/test.for
- context: Layer 0's context hash plus body-level mocks/env writes and
  the test call's own modifiers and options
- checksContextKey for the second candidate source

Over the 251 eligible files: 4600 instances in 2.9 s, 183 with an
unsupported form, 37 with an unresolved selector.

* feat(scenario-map): add Layer 1 per-test V8 coverage and candidate retrieval

Phase 1 U4 (R3, R4; KTD4, KTD5):
- a Vitest setup file records per-test V8 block coverage in a prepared copy
  under the mutation config (gitnexus-shared runs from source), with design
  §5.2 attribution: module load discarded, a suite's beforeAll delta taken
  at its first beforeEach, concurrent tests flagged unattributable
- provenance per test: executed files, native-package entries and worker or
  child-process spawn call sites
- candidates: exact Jaccard >= 0.99 (size-ratio and prefix filters) plus
  identical checks-and-context buckets; connected components schedule
  Layer 3 only

Over the 251 eligible files: 4667 tests in 1:55 (82.8 s Vitest), 6281
candidate pairs in 611 groups, largest group 36 tests. Controls: 16/16
pairs are candidates, 0 cross-pair candidates. The directory is
path-coverage/ because coverage/ is ignored by git, prettier and eslint.

* feat(scenario-map): compute the per-instance input facet

Phase 1 U6 (R5, R9; KTD6), porting the input-facet spike without its
hard-coded inputs:
- rules 1-8: tree-sitter shapes of inline source, the COBOL profile,
  best-fit production tables (executed path, 2-hop import closure,
  property columns; never pooled), selector literals (reusing the checks
  predicate), object keys, source order, shared fixtures, fixture files
- numeric literals with 0/1/-1/negatives exact, and the +-1 rule against
  production numeric constants from executed files and the static import
  closure (folded initialisers included)
- discarded values per position with source locations for value flow
- the residual-difference guard: a body difference the facet misses makes
  the pair facet-unequal and both instances unresolved

Controls: all 16 pairs match expected.json. The spike's untouched
holdout-4 still scores 23/23 distinct. Eligible run: 4667 instances in
about 33 s, 245 unresolved.

* feat(scenario-map): run Layer 3 per candidate group and merge raw observations

Phase 1 U7 (R6, R7, R11; KTD7, KTD8, KTD14, KTD17):
- whole-file mutate scope per group from Layer 1 provenance and the
  static closure; executed-but-unmutated repo files mark tests excluded
- a run manifest of the prepared inputs that every shard verifies
- two plain Vitest 5 baselines (fresh instance per run) plus the Stryker
  dry run must agree; the generated config must carry perTest coverage,
  disableBail and static mutants before mutating
- raw observations from every sidecar attempt, keyed by group, run kind,
  lineage, attempt, test and mutant; class disagreement is inconclusive
  and a missing result is unknown, never a non-kill
- deterministic shard planning that never splits a group, with
  group-over-budget and start-only-if-it-fits rules

Controls: all 16 pairs ran as real groups; every kill set matches
expected.json.

* feat(scenario-map): add the full isolation pass for repeat-stage pairs

Phase 1 U8 (R7; KTD8, KTD9):
- select repeat-stage pairs (same coveredBy path, checks, context, facet
  without a residual difference, and the same non-empty kill set)
- per pair one together run and one alone run per test (shared across
  pairs), mutate restricted to the covered universe, a fresh runner
  process per mutant, all behind the same KTD8 baseline and config checks
  (runGroup's steps moved into runMutationRun)
- coverage and outcome class per test and mutant must agree across main,
  together and alone; a missing rerun result is unknown; hook-attributed
  mutants cap the pair at weak

Controls: order-dependent ends disagrees; verbatim-duplicate,
repo-arithmetic and set-non-member agree, matching expected.json.
Measured fresh-process cost: about 5.5 s per mutant.

* feat(scenario-map): add the row-deletion operator

Phase 1 U9 (R8, KTD10):
- find the one supported production table (object literal with plain
  keys, literal Set/Map) holding both differing values; spreads, computed
  keys, Object.assign, writes, getters, proxies, duplicate keys or an
  ambiguous table make the outcome inconclusive
- splice one key's text in a throwaway prepared copy, run the pair with a
  fresh Vitest instance per run after a control run that proves both pass
  and the edited file loaded
- symmetric / asymmetric (Row) / not applicable / inconclusive, with
  RowDeletion global mutant ids
- vitest-run: an opt-in load probe (probeFiles, loadedFiles)

Controls: all 16 pairs match expected.json (object-key-row and
set-membership asymmetric).

* feat(scenario-map): add the static value-flow check

Phase 1 U10 (R9, KTD11): for facet-equal pairs whose discarded values
differ, all sources must come back clean or the pair is vetoed to Review:
- Babel binds each differing value to its entry (call argument, fixture
  field, table key); an expected-only difference is unknown
- the TypeScript 7.0.2 unstable checker resolves entries and callees
  through aliases; a version mismatch degrades the whole run
- a walk stitched across functions over GitNexus PDG REACHING_DEF and
  CALL_SUMMARY edges (pdg_query flows are intra-procedural): opaque
  sinks (lib/@types/node_modules, any regex) veto, truncation, dynamic
  dispatch, a depth cap or a missing PDG layer are unknown or degraded;
  repo code is clean only inside the pair's mutated files
- row-deletion exports scanSupportedTables so table reads mean exactly
  the tables row deletion supports

Controls: 0 mismatches against expected.json. Whole-repo analyze --pdg:
6:49 wall, 8.4 GB peak RSS.

* feat(scenario-map): add the value-flow trace run

Phase 1 U11 (R9, KTD11): one traced Vitest 5 run over the tests of every
facet-equal candidate pair, with a setup file that wraps value-sensitive
built-ins (Math, string transforms and comparisons, RegExp, number
parsing and formatting, JSON, Intl, Date, sort, plus U10's opaque
callees) and records, per test and production call site, the receiver,
arguments and count. Storage and table reads stay unwrapped.
A shared site with a different receiver or argument, or asymmetric
sites or counts, vetoes the pair; a test whose outcome changes under
tracing makes it inconclusive.

Controls: all 13 pairs with a trace expectation match expected.json,
including receivers 24 and 25 reaching toFixed. Overhead on 40 eligible
files: 1.19x pooled, no outcome changes.

* feat(scenario-map): add the verdict engine

Phase 1 U12 (R10, R11, R15; KTD5, KTD8, KTD9, KTD12): a pure function
over merged observations and every stage's evidence. Design §2
precedence as an ordered rule list (Inconclusive, Out of scope, Weak,
Repeat strong, Repeat weak, Row, Review, Distinct), with the value-flow
and trace vetoes as a gate before any repeat category and the residual
difference ending in Review. One verdict per pair with its deciding rule
and every weak disqualifier; per-test Weak, Inconclusive and Subsumed;
per-file gaps recomputed from all observations with the §5.4 wording;
dupe-ok pairs kept with their evidence. Output is deterministic.

Controls: all 16 verdicts and rules match expected.json.

* feat(scenario-map): build the report, issue body and week-over-week diff

Phase 1 U13 (R12, R13; KTD13, KTD15):
- the JSON report: run provenance (the design §7 cache-key inputs),
  completeness and degraded flags, the eligibility delta, Layer 0
  findings, every verdict with its evidence, per-test properties, gaps,
  a seeded spot audit of 10 strong candidates, N of M evaluated and the
  precision-not-measured statement
- continuity: mutant fingerprints without revision or span, universe ids
  from the pair's common universe, evidence hashes per design §5.5
- a markdown rendering and the tracking-issue body (strong repeats, weak
  tests, gaps; truncated at 60,000 characters; a run-id marker so a
  stale run never overwrites a newer body)
- baseline comparison: new, changed, unchanged, resolved, with first-run
  and incompatible-baseline banners

* feat(scenario-map): add the CLI and the end-to-end controls run

Phase 1 U14 (R14, R16; KTD1, KTD14, KTD17): npm run test:scenario-map
with plan, shard, value-flow, merge, all and controls stages exchanging
JSON in a work directory, so CI jobs and a local run produce the same
report. Shards batch groups into one mutation run when their combined
estimate fits, and persist row-deletion and isolation results as they
finish. Exit codes are set explicitly.

controls: all 16 fixture pairs match expected.json (exit 0, 76 min).

* feat(scenario-map): fit the scheduled run into the CI budget

Phase 1 U14 follow-up (KTD14, KTD17), measured on a seeded slice of the
real category; no evidence rule is relaxed:
- value flow runs before the shards and re-plans them, so isolation is
  spent only on pairs that can still be strong (a value-flow veto or
  unknown ends Review, a trace inconclusive ends Inconclusive); a
  missing, stale or degraded value-flow result falls back to the
  provisional plan
- a shards stage prints the final shard count for the CI matrix
- --concurrency and --speed-factor feed the estimate; the main-run cost
  form is fixed + mutated bytes (measured; it does not grow with tests)
- a group whose isolation does not fit still runs its main run and row
  deletion; its pairs get isolation not-run (weak cap), and a shard
  isolates deferred pairs only when their measured universe fits
- groups that execute no production file are not run; their pairs are
  out of scope

Predicted CI run: 9 shards at concurrency 4, 1658-1829 min in total,
largest shard 196-253 min (budget 300), 0 groups over budget.
controls: 16/16 pairs match, 28 min.

* ci(scenario-map): add the weekly Test Scenario Map workflow and a PR controls job

Phase 1 U15 (R13, R14; KTD13, KTD14, KTD17):
- test-scenario-map.yml (weekly + dispatch, advisory): plan, value-flow,
  shards (sizes the matrix from the value-flow re-plan), a shard matrix
  (fail-fast off, max-parallel 10, 300-minute deadline), merge (always,
  against the newest scenario-map-baseline artifact) and upsert-issue
  (default branch only; the only job with issues: write; replaces the
  tracking issue's body only for a complete run that is not older than
  the body's run marker, comments on partial runs, never closes it)
- mutation-runner-validation.yml: a path-gated scenario-map-controls job
  that runs the controls fixture end to end on PRs touching the tooling

actionlint (with shellcheck), zizmor 1.24.1 and the concurrency check
pass. Neither workflow can run before the merge.

* docs(scenario-map): record Phase 1 in the design and the testing guide

Phase 1 U16: design corrections (the stitched PDG walk, the trace's
receivers and scope, row deletion and baselines on plain Vitest 5, the
isolation pass on testIds after value flow), the demote-only additions,
the refreshed gates, the measured Phase 1 costs and the CI topology
change; TESTING.md gains a Test Scenario Map section (stages, controls
fixture, supported matchers, wrapped built-ins, weekly workflow,
dupe-ok).

* fix(scenario-map): store observations compactly so large batches fit in memory

The first full run crashed a shard with "Invalid string length":
observations were materialised for every collected test x every mutant,
including a valid not-covered survival for each pair (1.6M objects for a
batch of 73 selected tests over 21,965 mutants; the batch file needed
about 700 MB of JSON).

- only a run's selected tests produce observations
- each run records its universe (selected tests, mutants with their
  status, reasons and hook marker); only rows, covered or static
  mutants without a row, and hook-named tests are stored explicitly; a
  not-covered survival is derived when read
- every consumer (pair comparison, isolation, verdict engine, report,
  batch split) reads through one index; group observation and isolation
  run files move to schema 2, and old files are rejected with a message
- merge lists an unreadable group result as incomplete instead of
  dropping it; repeat-stage decisions keep comparison summaries only

On the crashing batch: 141,308 explicit observations instead of 1.6M,
12.1 MB instead of an overflow, and a byte-identical verdict report.
controls: 16/16 pairs match.

* docs(scenario-map): record the Phase 1 measured slice; give the real-repo classify test headroom

- design §9: the full-category report comes from the first weekly
  workflow run; the local full run was stopped because it saturated the
  shared machine. Records the measured slice and the whole-category plan
  and value-flow figures.
- classify.test.ts: classify the real repo once in beforeAll with a
  120 s timeout; it takes about 30 s on a busy host and timed out at
  the default 30 s.

* fix(review): apply review findings and simplify the scenario-map tool

Review (ce-code-review, validated findings):
- trace: a baseline or traced run that is invalid, inconclusive, retried
  or has outside-test failures, unhandled errors, unwrapped entries or
  recorder errors makes every pair inconclusive (trace-run-invalid), never
  clean; the value-flow stage reports it and merge marks the run partial
- verdict: tests pin that a not-required static check for differing
  values reads as unknown, plus the worker-spawn, missing-provenance,
  unresolved-input and unknown-hook-marker weak caps
- workflow: the baseline comes only from this workflow's own scheduled or
  dispatched default-branch runs of this repository; an unreadable
  baseline shows the incompatible-baseline banner instead of failing merge
- controls: the oracle fails on an empty expected list, on any unlisted
  repeat verdict and on a case file without an expected pair
- value flow: the PDG analyze budget (25 min) sits below the 50-minute
  CI step; the TypeScript checker server is closed when opening fails
- --concurrency now also bounds every in-process Vitest run (maxWorkers),
  so local runs stay within the requested cores
- merge reads isolation runs tolerantly (an unreadable file makes the run
  partial); a duplicate test title is renamed; a stale TESTING.md heading
  reference is updated

Simplification (behavior-preserving; merge and plan outputs byte-identical
on fixed inputs): one util.ts for shared helpers, a run-level row-deletion
cache, one classify() per plan, memoised universe ids and single-pass
pair/subsumption/report loops.

* fix(test-dupes): keep beforeAll and beforeEach fixture builds apart

A same-fixture-setup key now includes the hook name. A beforeAll fixture is
built once per suite and a beforeEach one per test, so sharing one result
between them would drop per-test isolation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): temporary label-gated pull_request run for PR #3686

schedule and workflow_dispatch only run from the default branch, so the
Test Scenario Map workflow cannot run before this PR merges. Adding the
run-ci label to the PR starts one full run. A pull_request run writes no
tracking issue and no baseline. Remove the trigger before merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): remove the temporary pull_request trigger

The one full run on PR #3686 finished (run 40183982111, complete, 9 of 9
shards). This reverts 5d06d731c.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scenario-map): decide pairs past unknown mutants on a kill witness

A mutant both tests ran cleanly, killed by assertion by exactly one, proves
the kill sets differ whatever the unknown and runtime-class mutants hold.
kills-unknown and non-assertion-outcome now leave such a pair Inconclusive
no longer; it goes on to Row or Distinct. A pair's empty kill set only
decides Weak on a fully known run. Verdict engine 2.

Re-merged from the artifacts of run 40183982111: Inconclusive 4532 -> 1469;
every pair engine 1 decided keeps its verdict, rule and details, and the
repeat candidates are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scenario-map): record a test file's import failure instead of a missing result

A static mutant that breaks a module at import makes its test files fail to
collect: the runner writes one file-level killed-outside-test:collection row
and no per-test rows, so every test of the file was a missing result. Those
were every missing-result observation of run 40183982111 (597 mutants, 675
same-file pairs left Inconclusive by them alone).

Extraction now records each test of a failed file with that class. When both
tests of a pair failed to import, neither ran, so the mutant cannot tell them
apart: comparePair counts it as a non-kill for both rather than another
class. Verdict engine 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): temporary label-gated pull_request run for PR #3686

schedule and workflow_dispatch only run from the default branch, so the
Test Scenario Map workflow cannot run before this PR merges. Adding the
run-ci label to the PR starts one full run. A pull_request run writes no
tracking issue and no baseline. Remove the trigger before merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): remove the temporary pull_request trigger again

The engine 3 run on PR #3686 finished (run 40208819019, complete, 9 of 9
shards). This reverts 0e76acc09.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(scenario-map): record the engine 3 run's results

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): scenario fingerprint verdicts (same path, input, expectation)

Two tests are duplicates when they run the same execution path on the same
input data and check the same expectation. The fingerprint engine decides
that from Layer 1 block sets, Layer 2 facets and discarded values, the
checks-and-context key and the trace run, with no mutation: duplicate,
possible-duplicate (anything unresolved, fail-closed), table-row (other
input values), different-checks, distinct.

On run 40208819019 the fully resolved triple names 26 pairs: the run's 13
strong repeats and 13 more mutation could not decide, none contradicted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): lead the report with the scenario fingerprint

The report (schema 2) carries every pair's fingerprint verdict with its
continuity key and the run's mutation scope. The markdown and the tracking
issue lead with duplicates and same-scenario-different-checks pairs; the
mutation sections (strong repeats, weak tests, gaps) render only when the
run gathered mutation evidence. The spot audit samples duplicates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): mutation scope (none, possible-duplicates, all)

plan and all take --mutation. The plan computes every pair's fingerprint
before the trace and packs only the scope's target: no group (none), every
group (all), or the groups with a possible duplicate, trimmed to the tests
of those pairs and the files they executed. Value flow re-plans a narrower
scope after the trace (a usable trace only narrows it) and builds the PDG
index for scope all only. Merge reports every pair's fingerprint and the
mutation verdicts of the pairs whose tests ran in a mutated group. A plan
with nothing to mutate has no shard; all skips the shard stage then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): check fingerprint verdicts in the controls oracle

expected.json gives every control its fingerprint verdict and rule, written
from the fingerprint rules before any run: verbatim-duplicate is the one
duplicate, enum-member a possible duplicate (residual difference),
numeric-boundary distinct, the other 13 table rows. The controls stage
compares both verdicts per pair and fails on an unlisted fingerprint
duplicate. Local controls run: all 16 pairs match on both.

value-flow checks for dist/ only when it builds the PDG index (scope
all); controls, and all with scope all, still check before any stage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): fingerprint weekly, mutation on dispatch or label

The weekly run is the fingerprint only (mutation scope none: no build, no
shards). Workflow dispatch takes a mutation input (none, possible-duplicates,
all). Adding the scenario-map label to a pull request starts the deep run
over the possible duplicates; it writes no issue and no baseline, and every
run puts the issue-body view in the job summary. Pull request runs get their
own concurrency slot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(scenario-map): the scenario fingerprint as the primary verdict

The design defines a duplicate as the same execution path, input data and
expectation, records the maintainer decisions of 2026-10-25 (fingerprint
weekly, mutation on dispatch or label) and the local measurement: the
fingerprint-only run took 8.2 min over all 4,632 eligible tests and named
the 26 duplicates the evidence predicted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ci): place a single shard artifact in the scenario-map merge

download-artifact unpacks a pattern that matches one artifact into the
path itself, not into a directory named after it. With a one-shard plan
(the scenario-map label's possible-duplicates run) the placement step found
no shard directory, so merge reported the shard missing and every mutated
pair Inconclusive (run 40268120818). The step now uses the download path
when no artifact directory exists. A local re-merge of that run's artifacts
gives the complete result, recorded in the design doc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): fingerprint every unit and integration test

The fingerprint needs no Stryker run, so its scope is no longer the
mutation runner's validated envelope. Eligibility now carries
fingerprintFiles (every *.test.ts under test/unit and test/integration)
next to the mutation-eligible files; a verdict input carries eligible
(mutation), fingerprintEligible and passed.

- Layer 1 and the trace run the base config's lbug-db files in a
  partition of their own with one worker (partitions.ts), the rest
  with --concurrency workers, and merge both runs.
- New fingerprint rule not-passing: a test that did not pass in
  Layer 1 makes its pairs Inconclusive.
- The mutation target keeps only pairs whose two tests are
  mutation-eligible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): run Layer 1 in CI slices; load vendor and dist in integration runs

A sample of the integration tests showed two problems with fingerprinting
every unit and integration test:

- Half the sampled files failed to load: the prepare step (gate-3 hashed)
  leaves vendor/ and dist/ out of the prepared copy, and integration tests
  load leiden, the parse worker and the built CLI. Layer 1 and the trace now
  copy both from the checkout in normal mode, and every stage that runs tests
  (coverage, plan without --coverage, value-flow, all, controls) requires a
  built dist/.
- One plan job cannot hold Layer 1: 44 of 435 integration files ran past
  35 minutes. A new coverage stage (--index i --of n) runs one slice, split
  like the CI test shards (test/helpers/shard-balance.ts), and
  plan --coverage <dir> merges the slice files: one block table, indices
  remapped, files and tests in file order. A slice that did not report leaves
  its files unrecorded. The workflow runs 12 coverage slices before the plan.

The sequential partition now reads every fileParallelism: false project of
the base config (lbug-db and cli-e2e), not lbug-db alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(scenario-map): order-dependent controls pair is Inconclusive on the fingerprint

The not-passing fingerprint rule (a test that did not pass in Layer 1 holds
its pairs Inconclusive) came after expected.json was written. Layer 1 runs
every test twice in a row (repeats 1, design §5.2); the order-dependent
fixture's shared counter makes each test's second attempt fail, and Vitest
reports a test failed when any repetition fails. So rule 2, not-passing,
decides the pair before input-values-differ. The expectation follows from
the rule and the fixture's code; the CI controls run that reported the
difference is run 40278026899.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(scenario-map): skip Layer 1 for files no fingerprint finding can come from

Past the path rule, a pair is a duplicate, a possible duplicate or a table
row only when both checks-and-context keys are equal or one is unknown, and
the key is static (facet/checks.ts over the static inventory). Layer 1 now
runs only the files that hold a test whose key another test shares or that
has none, plus every file the static view cannot vouch for: a call whose
instance count is not static, a file snapshot read, a call at registration
level that is not the test API (a helper such as defineRenameFixtureTests or
describeGrammarPresence, or an it alias), or a parse failure. Mutation scope
all and the controls still run every file; the coverage jobs get the run's
--mutation so they slice the same files as the plan.

On the current checkout this skips 449 of 1,399 test files, about 24% of the
weighted Layer 1 cost. On the 250 unit files of the last full run, the static
keys reproduce all 3,313 runtime keys and select the same 172 files; runtime
collection (vitest list --no-static-parse) of the skipped files finds no test
the static inventory misses. Given up: different-checks pairs with a test of
a skipped file (0 on the last full unit run). The report says how many files
Layer 1 ran.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(scenario-map): diff-scope the labelled pull request run

A labelled pull request asks whether its own tests duplicate anything, yet
the run fingerprinted the whole suite (12 coverage slices, hours of runner
time). plan --changed-since <ref> now scopes it to the diff:

- the changed test files are the fingerprint's files the diff since the
  merge base of ref and HEAD added or modified;
- Layer 1 runs the changed files the prefilter keeps plus every file with
  a test whose checks-and-context key a changed test shares;
- candidate pairs are kept only when they have a test of a changed file,
  before the groups form, so the mutation target, the trace and the report
  follow;
- a diff that changes no test file writes an empty work file and an empty,
  complete report.

The workflow skips the coverage matrix for pull requests and passes the
merge commit's first parent (fetch-depth 2); the plan job builds dist/ and
runs Layer 1 itself. The report names the changed files. Locally, over the
two test files of the previous commit, the run took 4 min 41 s end to end
(Layer 1 over 2 files 1.3 min, trace 1.1 min) on a loaded shared host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(scenario-map): size Layer 1 at 4 coverage slices, not 12

Twelve slices came from an estimate of three times the CI coverage time,
built on a local integration sample that other suites on the shared host
had slowed down. The test workflow runs the whole unit and integration
suite with coverage in three 7-8 minute shards (ci.yml run 30014110469),
and an instrumented Layer 1 run shows its overhead is small: an
integration file's work sits in beforeAll, which Layer 1 neither repeats
nor snapshots per test (resolvers/rust: 10.1 s of coverage snapshots
against about 190 s for the file). With each slice paying about five
minutes of setup, twelve slices spent more on setup than on tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): report the Layer 1 tests that got no fingerprint

Tests that Layer 1 saw but did not run dropped out of the candidates
without a word: skipped ones (skip, skipIf, todo; the GITNEXUS_BENCH
benchmark suites, which run only in the test workflow's benchmarks job)
and unfinished ones (pending, because their file's run ended first). The
report's eligibility section now counts both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scenario-map): close gaps a code review found in the Layer 1 work

- The registration guard let a suite whose callback is not written
  inline (describe('x', defineCases)) through, so a file whose tests the
  inventory cannot see could be skipped. Such a suite now keeps its file.
- An empty coverage slice exited 2 and uploaded nothing; it now writes
  an empty slice file.
- --changed-since was accepted with the controls fixture, whose run must
  cover every file; that is now a usage error.
- The report counted the files of a coverage slice that did not report
  as run. It now says how many files wrote no Layer 1 record.
- A diff that only changes a snapshot file scoped nothing; a changed
  __snapshots__/<name>.snap now counts as a change of its test file.
- The diff-scope report claimed no other file can hold a changed test's
  duplicate. Files whose tests have no static key are not run, so it now
  says a possible duplicate there is not looked for.

Not changed: mutation shards never get integration tests, because
mutationTarget keeps only pairs of two mutation-eligible tests; a slice
that disagrees with the plan stops the merge on purpose (the slices
header now says so).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(scenario-map): cut setup cost and duplication in the Layer 1 work

A four-angle cleanup review (reuse, simplification, efficiency,
altitude) of the Layer 1 slices, prefilter and diff scope.

Fixed along the way: a shard's row-deletion batch ran every planned pair
of a complete group, including pairs whose tests the mutation target had
trimmed away (an integration test, or a pair outside the scope); it now
runs only pairs whose two tests the trimmed group holds (shardRowPlans).

Setup cost:
- Coverage jobs ran the Phase 0 classifier twice (about 33 s each) just
  to list the fingerprint's files; fingerprintScope lists them directly.
- The plan recomputed the prefilter (about 30 s) to rebuild the slice
  split. Slice files now carry Layer 1's file list, the mutation scope
  and the revision; the plan checks those against its own run.
- The prefilter re-parsed every test file. The unseen-registration check
  now lives in the static inventory (StaticInventory.incomplete), on the
  ASTs it holds and test-dupes' roleOf, which also catches
  test.describe with a named callback and it('x', namedFn).

Simplification: runCoverage writes the empty work file itself; reuse
partitionFiles, codeList, writeJson and path.matchesGlob (the hand-made
glob matcher read ** differently from Vitest); sameList moves to util;
copyBuildOutput owns the controls rule; slices are read before the work
directory is emptied, so --coverage no longer has to lie outside --work;
a dead sort is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(scenario-map): record the Layer 1 cleanup review in the fingerprint plan

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): full recall for diff-scoped pull request runs

A labelled pull request's run left out unchanged files whose tests have
no static checks-and-context key (helper-registered tests, dynamic it.each
counts, file snapshots, no assertion key), so a changed test's possible
duplicate in one was never looked for. Such a test can pair with any
changed test, so the diff scope now runs every one of those files too.

That floor is 230 of 1,407 test files, about 59% of the suite's weighted
Layer 1 cost with 59 sequential files: too much for the plan job alone.
A labelled pull request therefore runs the coverage slices as well:
coverage --changed-since records the changed files in each slice, and the
plan takes the diff scope from the slices (plan --changed-since stays for
a local all run). The plan job no longer builds dist/ or fetches history.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): temporary owner-only test run on PR #3686

TEMPORARY, remove before merge. The scenario-map-test label starts the
whole-suite run (no diff scope) with mutation possible-duplicates, so
every stage (coverage slices, plan, value flow, mutation shards, merge)
can be tested on CI before the workflow reaches the default branch, where
workflow_dispatch would serve. It runs only when magyargergo adds the
label on PR #3686; the label on any other pull request, or by anyone
else, leaves every job skipped. The scenario-map label keeps its
diff-scoped run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: stabilize scenario-map test selection

* fix: exclude retry tests from scenario traces

* fix: keep captured loop inputs unresolved

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:46:58 +00:00

1755 lines
67 KiB
TypeScript

/**
* Pure-JS category classifier (U5, KTD11, KTD19).
*
* Decides which `gitnexus/test/unit` files a mutation run can use in a prepared copy
* (tools/mutation/prepare) without native code, workers, child processes, or file reads that the
* prepared copy breaks. Gate 1 (U6) records the modules each accepted test loads at runtime and
* fails on a native binding, worker or child_process load, so a miss here surfaces there.
*
* Walk. Each test file's import closure, parsed with @babel/parser:
* - `import` declarations, except `import type`, type-only specifiers, and value imports whose
* bindings are never used as values (the TypeScript transform erases those, because the repo
* does not set `verbatimModuleSyntax`); `export ... from`; `import x = require()`.
* - `import()`, `require()`, `createRequire(<base>)` results called directly or through a const,
* `vi.importActual()`, `vi.importMock()`, and `vi.mock()`/`vi.doMock()` unless the factory
* takes no parameter (automocking and `importOriginal` load the real module).
* A specifier must evaluate statically: a string literal, a template or `+` concatenation of
* those, an identifier whose binding (resolved lexically; a redeclared name is unknown) is a
* `const` initialised with such a value, or an absolute path or file URL built from
* `import.meta.url`, `__dirname`, `process.cwd()`, `path.join/resolve/dirname/sep`, `new URL()`,
* `fileURLToPath()` and `pathToFileURL().href`. Type assertions are looked through.
* Anything else is "unresolvable dynamic import". Relative specifiers resolve like Vite (exact
* file, `.js` to `.ts`/`.tsx`, appended extensions, directory `index`); `gitnexus-shared[/x]`
* resolves through that package's `exports` (`./dist/X.js` maps to `src/X.ts`, KTD8); node
* builtins and other bare packages are leaves (package code is not walked).
*
* Rejection reasons:
* - `native package: <name>`: a bare package on {@link KNOWN_NATIVE_PACKAGES}, or one that ships a
* `.node` file, a `binding.gyp` or `"gypfile": true`, or reaches such a package through the
* installed packages of its `dependencies`/`optionalDependencies`. `vitest` and `@vitest/*` are
* the harness and exempt.
* - `native binding: <path>`: an import resolves to a `.node` file.
* - `spawns processes: child_process|cluster`: the closure imports that builtin, or a package on
* {@link KNOWN_PROCESS_PACKAGES} (its own code loads `child_process` when imported).
* - `spawns workers: worker_threads`: the closure imports `Worker` from `worker_threads`, or the
* whole module (default/namespace binding, non-destructured `require`/`import()`).
* - `imports outside gitnexus/`, `imports from ignored dir: <dir>/`: a relative import leaves the
* package (gitnexus-shared/src is reachable only through its alias, and a shared module may
* not leave gitnexus-shared/src), or lands in a directory the Stryker sandbox omits
* (stryker-config.ts `DEFAULT_IGNORED_DIRS`).
* - `unresolved import: <specifier>`, `unsupported import specifier: <specifier>`,
* `unresolvable dynamic import`, `parse error`.
* - Reads, checked in test-side modules only (every module of the closure outside `gitnexus/src`
* and `gitnexus-shared/src`; production modules that import fs do not count):
* `reads source under src/`, `reads test fixtures`, `reads ignored dir: <dir>/`,
* `reads source outside gitnexus/`, `unresolvable file path`. Two rules: (a) every path
* expression anchored to a location (`__dirname`, `__filename`, `import.meta.url|dirname|
* filename`, `process.cwd()`, or a const bound to one) is evaluated through the functions above,
* templates and `+`, whether or not it reaches an fs call; (b) the first argument of an
* `fs`/`fs/promises` call that is a relative literal resolves against the package dir (the
* Vitest root and cwd). When a trailing part is not static, the known prefix decides, and a
* prefix that contains a checked directory (the package root, `test/`, anything above the
* package) is "unresolvable file path".
*
* Limits: paths built by helper functions or passed through parameters are not followed, and a
* production function reading a path the test hands it is not seen; an unknown suffix is assumed
* not to contain `..`; package internals are not walked for child_process or worker use (gate 1
* sees them, and the packages it finds go on {@link KNOWN_PROCESS_PACKAGES}); `vi.mock` factories
* do not neutralise a module other imports load (conservative); Babel parse failures reject the
* file; hook and test counts look at the test file only.
*
* CLI (from gitnexus/):
* node --import tsx tools/mutation/classify/classify.ts [--root test/unit] [--file <path>]...
* [--out <file.json>] [--closure]
* Without `--out` the JSON goes to stdout; with it, a summary does. `--closure` keeps each file's
* closure, package and builtin lists in the JSON.
*/
import { readFileSync, readdirSync, realpathSync, statSync, writeFileSync } from 'node:fs';
import { builtinModules } from 'node:module';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
import { parse, type ParserOptions } from '@babel/parser';
import {
VISITOR_KEYS,
type CallExpression,
type ExportNamedDeclaration,
type File,
type ImportDeclaration,
type MemberExpression,
type NewExpression,
type Node,
} from '@babel/types';
import { DEFAULT_IGNORED_DIRS } from '../driver/stryker-config.js';
import { errorMessage, isMainModule } from '../shared/cli.js';
export const CLASSIFY_SCHEMA_VERSION = 1;
/**
* Packages rejected by name, installed or not, so the verdict does not depend on what a checkout
* installed; detection on disk adds the rest. `@huggingface/transformers` always depends on
* `onnxruntime-node` and `sharp`.
*/
export const KNOWN_NATIVE_PACKAGES: readonly RegExp[] = Object.freeze([
/^tree-sitter$/,
/^tree-sitter-/,
/^@ladybugdb\//,
/^onnxruntime-/,
/^@huggingface\/transformers$/,
/^sharp$/,
/^fsevents$/,
/^node-gyp-build$/,
/^bindings$/,
]);
/**
* Pure-JS packages whose own code loads `child_process` when imported. The walk does not enter
* package code, so gate 1's runtime load check (tools/mutation/fidelity) finds them; each entry
* names its evidence. `commander`: lib/command.js requires `node:child_process` at the top (for
* executable subcommands); gate 1 saw it load in test/unit/cli-command-banner.test.ts.
*/
export const KNOWN_PROCESS_PACKAGES: readonly RegExp[] = Object.freeze([/^commander$/]);
/** The test harness itself: always loaded, never a reason to reject. */
const isHarnessPackage = (name: string): boolean =>
name === 'vitest' || name.startsWith('@vitest/');
const SHARED_PACKAGE = 'gitnexus-shared';
const PROCESS_BUILTINS = new Set(['child_process', 'cluster']);
const WORKER_BUILTIN = 'worker_threads';
const FS_MODULES = new Set(['fs', 'fs/promises']);
const PATH_MODULES = new Set(['path', 'url']);
const PATH_ONLY = new Set(['path']);
const BUILTINS = new Set(builtinModules);
/** Vite's default `resolve.extensions` order. */
const APPENDED_EXTENSIONS = ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'];
const TS_FOR_JS: Readonly<Record<string, readonly string[]>> = {
'.js': ['.ts', '.tsx'],
'.jsx': ['.tsx'],
'.mjs': ['.mts'],
'.cjs': ['.cts'],
};
const WALKED_EXTENSIONS = new Set(['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs']);
const HOOK_NAMES = ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'] as const;
const TEST_ROOTS = new Set(['it', 'test']);
const EACH_ROOTS = new Set(['it', 'test', 'describe', 'suite']);
// ── Public types ───────────────────────────────────────────────────────────
export type HookCounts = Record<(typeof HOOK_NAMES)[number], number>;
export interface Evidence {
reason: string;
/** Repo-relative POSIX path of the module where the reason was found. */
module: string;
line: number;
detail: string;
/** Import chain from the test file to {@link module}, both included. */
via: string[];
}
export interface FileVerdict {
/** Package-relative POSIX path, e.g. `test/unit/schema.test.ts`. */
file: string;
status: 'accepted' | 'rejected';
/** Sorted, distinct; empty when accepted. */
reasons: string[];
/** The first occurrence of each reason in breadth-first closure order, sorted by reason. */
evidence: Evidence[];
hooks: HookCounts;
/** `.each`/`.for` calls on `it`/`test`/`describe`/`suite` in the test file. */
eachCalls: number;
/** Outermost `it`/`test` call sites in the test file (an `it.each` row set counts once). */
testCalls: number;
/** Repo-relative local modules and data files of the import closure, test file included. */
closure: string[];
/** Bare packages the closure imports (leaves). */
packages: string[];
/** Node builtins the closure imports, without the `node:` prefix. */
builtins: string[];
}
export interface ClassificationSummary {
total: number;
accepted: number;
rejected: number;
/** Rejected files per reason; a file counts once for each of its reasons. */
byReason: Record<string, number>;
/** Rejected files per reason category (the text before `: `). */
byCategory: Record<string, number>;
}
export interface Classification {
schemaVersion: typeof CLASSIFY_SCHEMA_VERSION;
/** Package-relative directory that was listed, or null for an explicit file list. */
testRoot: string | null;
files: FileVerdict[];
accepted: string[];
rejected: { file: string; reasons: string[] }[];
acceptedWithBeforeAll: string[];
summary: ClassificationSummary;
}
export interface ClassifyOptions {
/** Directory holding `gitnexus/` and `gitnexus-shared/`. */
repoRoot: string;
/** Package-relative test files; defaults to every `*.test.ts` under {@link testRoot}. */
testFiles?: readonly string[];
/** Package-relative directory to list; default `test/unit`. */
testRoot?: string;
}
// ── Static values ──────────────────────────────────────────────────────────
/**
* What an expression evaluates to: plain text, an absolute file path (or file URL) anchored to a
* known location, or unknown. A `file` value with `exact: false` is a prefix: the expression
* names something inside `abs` whose remaining segments are not static.
*/
type Value =
| { kind: 'text'; text: string }
| { kind: 'file'; abs: string; url: boolean; exact: boolean }
| { kind: 'unknown' };
const UNKNOWN: Value = { kind: 'unknown' };
const text = (value: string): Value => ({ kind: 'text', text: value });
const file = (abs: string, url = false): Value => ({ kind: 'file', abs, url, exact: true });
/** A name's declaration; a `const` keeps the scopes around it to evaluate its initialiser. */
type Binding =
| { kind: 'const'; init: Node | null | undefined; scopes: readonly Node[] }
| { kind: 'import'; source: string; imported: string }
| { kind: 'other' };
const OTHER: Binding = { kind: 'other' };
type EdgeTarget =
| { kind: 'specifier'; specifier: string; fromDir: string }
| { kind: 'file'; abs: string };
interface ImportEdge {
target: EdgeTarget;
line: number;
/** Imported names when known; null when the importer can reach every export. */
names: readonly string[] | null;
}
interface Finding {
reason: string;
line: number;
detail: string;
}
interface ModuleFacts {
edges: ImportEdge[];
/** Reasons that apply wherever the module sits (unresolvable dynamic imports, parse errors). */
findings: Finding[];
/** Read-rule hits; computed for test-side modules only. */
reads: Finding[];
hooks: HookCounts;
eachCalls: number;
testCalls: number;
}
type Resolved =
| { kind: 'module'; abs: string }
| { kind: 'leaf'; abs: string }
| { kind: 'builtin'; name: string }
| { kind: 'package'; name: string }
| { kind: 'reject'; reason: string; detail: string };
// ── AST helpers ────────────────────────────────────────────────────────────
/** Keys holding type syntax, which never runs. */
const TYPE_KEYS = new Set([
'typeAnnotation',
'typeParameters',
'typeArguments',
'returnType',
'superTypeArguments',
'superTypeParameters',
'implements',
'predicate',
]);
/** Declarations that are types only. */
const TYPE_ONLY_NODES = new Set([
'TSInterfaceDeclaration',
'TSTypeAliasDeclaration',
'TSDeclareFunction',
'TSDeclareMethod',
'TSIndexSignature',
'TSImportType',
]);
const KEYED_NODES = new Set([
'ObjectProperty',
'ObjectMethod',
'ClassProperty',
'ClassMethod',
'ClassAccessorProperty',
'ClassPrivateProperty',
'ClassPrivateMethod',
]);
const FUNCTION_NODES = new Set([
'FunctionDeclaration',
'FunctionExpression',
'ArrowFunctionExpression',
'ObjectMethod',
'ClassMethod',
'ClassPrivateMethod',
]);
/** Nodes that open a lexical scope (`var` goes to the nearest function or the program). */
const SCOPE_NODES = new Set([
'Program',
'BlockStatement',
'CatchClause',
'ForStatement',
'ForInStatement',
'ForOfStatement',
'SwitchStatement',
'StaticBlock',
...FUNCTION_NODES,
]);
/** Type-only wrappers around a runtime expression. */
const TS_WRAPPERS = new Set([
'TSAsExpression',
'TSSatisfiesExpression',
'TSNonNullExpression',
'TSTypeAssertion',
'ParenthesizedExpression',
]);
const isNode = (value: unknown): value is Node =>
typeof value === 'object' &&
value !== null &&
typeof (value as { type?: unknown }).type === 'string';
const flag = (node: Node, key: 'computed' | 'declare'): boolean =>
(node as unknown as Record<string, unknown>)[key] === true;
/** Child keys of `node` that hold runtime values (identifiers in name positions are skipped). */
function valueKeys(node: Node): readonly string[] {
const keys = VISITOR_KEYS[node.type] ?? [];
if (node.type === 'MetaProperty') return [];
return keys.filter((key) => {
if (TYPE_KEYS.has(key)) return false;
if (key === 'property' && !flag(node, 'computed')) {
return node.type !== 'MemberExpression' && node.type !== 'OptionalMemberExpression';
}
if (key === 'key' && KEYED_NODES.has(node.type)) return flag(node, 'computed');
if (key === 'label') return false;
if (node.type === 'ExportSpecifier') return key === 'local';
if (node.type === 'TSEnumMember') return key !== 'id';
return true;
});
}
/** `scopes` lists the scope nodes around `node`, outermost first, excluding `node` itself. */
type Visitor = (node: Node, ancestors: readonly Node[], scopes: readonly Node[]) => boolean;
/** Depth-first walk over runtime syntax; the visitor returns false to skip a node's children. */
function walk(root: Node, visit: Visitor): void {
const ancestors: Node[] = [];
const scopes: Node[] = [];
const go = (node: Node): void => {
if (TYPE_ONLY_NODES.has(node.type) || flag(node, 'declare')) return;
if (!visit(node, ancestors, scopes)) return;
const scoped = SCOPE_NODES.has(node.type);
ancestors.push(node);
if (scoped) scopes.push(node);
for (const key of valueKeys(node)) {
const child = (node as unknown as Record<string, unknown>)[key];
if (Array.isArray(child)) {
for (const item of child) if (isNode(item)) go(item);
} else if (isNode(child)) {
go(child);
}
}
if (scoped) scopes.pop();
ancestors.pop();
};
go(root);
}
/** `node` without type-only wrappers (`x as T`, `x!`, `(x)`). */
function unwrap(node: Node | null | undefined): Node | null | undefined {
let current = node;
while (current && TS_WRAPPERS.has(current.type)) {
current = (current as Node & { expression: Node }).expression;
}
return current;
}
/** The module a `require('m')` / `await import('m')` initialiser loads, or null. */
function loadedModule(init: Node | null | undefined): string | null {
const unwrapped = unwrap(init);
const call = unwrapped?.type === 'AwaitExpression' ? unwrap(unwrapped.argument) : unwrapped;
if (call?.type === 'ImportExpression' && call.source.type === 'StringLiteral') {
return call.source.value;
}
if (
call?.type === 'CallExpression' &&
call.callee.type === 'Identifier' &&
call.callee.name === 'require' &&
call.arguments[0]?.type === 'StringLiteral'
) {
return call.arguments[0].value;
}
return null;
}
const lineOf = (node: Node): number => node.loc?.start.line ?? 0;
/** Strips `node:` and maps `path/posix` to `path` so module checks see one name. */
const normaliseModule = (source: string): string => {
const bare = source.startsWith('node:') ? source.slice('node:'.length) : source;
return bare === 'path/posix' ? 'path' : bare;
};
const propertyName = (node: MemberExpression): string | null =>
!node.computed && node.property.type === 'Identifier' ? node.property.name : null;
const keyName = (key: Node): string | null =>
key.type === 'Identifier' ? key.name : key.type === 'StringLiteral' ? key.value : null;
/** Root identifier of a callee chain such as `it.concurrent.each(rows)`. */
function calleeRoot(node: Node): string | null {
if (node.type === 'Identifier') return node.name;
if (node.type === 'MemberExpression') return calleeRoot(node.object);
if (node.type === 'CallExpression') return calleeRoot(node.callee);
return null;
}
const isCreateRequireCall = (node: Node | null | undefined): node is CallExpression =>
node?.type === 'CallExpression' &&
((node.callee.type === 'Identifier' && node.callee.name === 'createRequire') ||
(node.callee.type === 'MemberExpression' && propertyName(node.callee) === 'createRequire'));
const toPosix = (value: string): string => value.split(path.sep).join('/');
/** True when POSIX `rel` is `dir` or inside it. */
const isWithin = (rel: string, dir: string): boolean => rel === dir || rel.startsWith(`${dir}/`);
/** True when POSIX `rel` is `dir` or one of its ancestors (`''` is the root). */
const contains = (rel: string, dir: string): boolean => rel === '' || isWithin(dir, rel);
const isInside = (abs: string, dir: string): boolean => {
const rel = path.relative(dir, abs);
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
};
/** `@scope/name` or `name` of a bare specifier, or null when it is not a package specifier. */
function packageName(specifier: string): string | null {
const segments = specifier.split('/');
if (specifier.startsWith('@')) {
return segments.length >= 2 && segments[1] !== '' ? `${segments[0]}/${segments[1]}` : null;
}
return segments[0] === '' || specifier.startsWith('#') ? null : segments[0];
}
// ── Module analysis ────────────────────────────────────────────────────────
interface AnalysisContext {
packageDir: string;
/** Read rules apply (the module is not production code). */
testSide: boolean;
/** Repo-relative display path. */
display: (abs: string) => string;
}
/** Facts of one module: import edges, dynamic-import failures, read-rule hits and test API use. */
class ModuleAnalysis {
private readonly scopeBindings = new Map<Node, Map<string, Binding[]>>();
private readonly references = new Map<string, number>();
private readonly values = new Map<Binding, Value>();
private readonly evaluating = new Set<Binding>();
private readonly skipped = new WeakSet<Node>();
private readonly calleeCalls = new WeakSet<Node>();
readonly facts: ModuleFacts = {
edges: [],
findings: [],
reads: [],
hooks: { beforeAll: 0, beforeEach: 0, afterAll: 0, afterEach: 0 },
eachCalls: 0,
testCalls: 0,
};
constructor(
private readonly file: string,
private readonly source: string,
ast: File,
private readonly context: AnalysisContext,
) {
walk(ast.program, (node, ancestors, scopes) => this.collect(node, ancestors, scopes));
walk(ast.program, (node, ancestors, scopes) => this.analyse(node, ancestors, scopes));
}
// Pass 1: bindings per scope, and value references.
private bind(scope: Node | undefined, name: string, binding: Binding): void {
if (!scope) return;
let names = this.scopeBindings.get(scope);
if (!names) {
names = new Map();
this.scopeBindings.set(scope, names);
}
const list = names.get(name);
if (list) list.push(binding);
else names.set(name, [binding]);
}
private bindPattern(scope: Node | undefined, pattern: Node | null | undefined): void {
if (!pattern) return;
switch (pattern.type) {
case 'Identifier':
this.bind(scope, pattern.name, { kind: 'other' });
return;
case 'ObjectPattern':
for (const property of pattern.properties) {
this.bindPattern(
scope,
property.type === 'RestElement' ? property.argument : property.value,
);
}
return;
case 'ArrayPattern':
for (const element of pattern.elements) this.bindPattern(scope, element);
return;
case 'AssignmentPattern':
this.bindPattern(scope, pattern.left);
return;
case 'RestElement':
this.bindPattern(scope, pattern.argument);
return;
case 'TSParameterProperty':
this.bindPattern(scope, pattern.parameter);
return;
default:
return;
}
}
private collect(node: Node, ancestors: readonly Node[], scopes: readonly Node[]): boolean {
const current = scopes[scopes.length - 1];
switch (node.type) {
case 'ImportDeclaration':
if (node.importKind !== 'type') {
for (const specifier of node.specifiers) {
if (specifier.type === 'ImportSpecifier' && specifier.importKind === 'type') continue;
this.bind(current, specifier.local.name, {
kind: 'import',
source: node.source.value,
imported:
specifier.type === 'ImportDefaultSpecifier'
? 'default'
: specifier.type === 'ImportNamespaceSpecifier'
? '*'
: (keyName(specifier.imported) ?? '*'),
});
}
}
return false;
case 'TSImportEqualsDeclaration':
if (node.moduleReference.type === 'TSExternalModuleReference') {
this.bind(current, node.id.name, {
kind: 'import',
source: node.moduleReference.expression.value,
imported: '*',
});
}
return false;
case 'ExportAllDeclaration':
return false;
case 'ExportNamedDeclaration':
return !node.source;
case 'Identifier':
this.references.set(node.name, (this.references.get(node.name) ?? 0) + 1);
return true;
case 'VariableDeclarator': {
const declaration = ancestors[ancestors.length - 1];
const kind = declaration?.type === 'VariableDeclaration' ? declaration.kind : 'var';
const scope =
kind === 'var'
? [...scopes].reverse().find((s) => s.type === 'Program' || FUNCTION_NODES.has(s.type))
: current;
const loaded = loadedModule(node.init);
if (node.id.type === 'Identifier') {
this.bind(
scope,
node.id.name,
kind === 'const' ? { kind: 'const', init: node.init, scopes: [...scopes] } : OTHER,
);
} else if (node.id.type === 'ObjectPattern' && loaded !== null) {
for (const property of node.id.properties) {
const name =
property.type === 'ObjectProperty' && !property.computed
? keyName(property.key)
: null;
if (property.type === 'ObjectProperty' && name !== null) {
if (property.value.type === 'Identifier') {
this.bind(scope, property.value.name, {
kind: 'import',
source: loaded,
imported: name,
});
} else {
this.bindPattern(scope, property.value);
}
} else {
this.bindPattern(
scope,
property.type === 'RestElement' ? property.argument : property,
);
}
}
} else {
this.bindPattern(scope, node.id);
}
return true;
}
case 'ClassDeclaration':
case 'TSEnumDeclaration':
if (node.id?.type === 'Identifier') this.bind(current, node.id.name, OTHER);
return true;
case 'CatchClause':
this.bindPattern(node, node.param);
return true;
default:
if (FUNCTION_NODES.has(node.type)) {
const fn = node as Node & { id?: Node | null; params: readonly Node[] };
this.bindPattern(node.type === 'FunctionDeclaration' ? current : node, fn.id);
for (const param of fn.params) this.bindPattern(node, param);
}
return true;
}
}
/** The binding `name` resolves to from a position inside `scopes` (outermost first). */
private lookup(name: string, scopes: readonly Node[]): Binding | 'global' | 'ambiguous' {
for (let index = scopes.length - 1; index >= 0; index -= 1) {
const list = this.scopeBindings.get(scopes[index])?.get(name);
if (list) return list.length === 1 ? list[0] : 'ambiguous';
}
return 'global';
}
/** The module and export an identifier stands for, when its binding is import-like. */
private refersTo(
name: string,
scopes: readonly Node[],
): { module: string; imported: string } | null {
const binding = this.lookup(name, scopes);
if (typeof binding === 'string') return null;
if (binding.kind === 'import') {
return { module: normaliseModule(binding.source), imported: binding.imported };
}
if (binding.kind === 'const') {
const loaded = loadedModule(binding.init);
return loaded === null ? null : { module: normaliseModule(loaded), imported: '*' };
}
return null;
}
private isModuleObject(node: Node, modules: ReadonlySet<string>, scopes: readonly Node[]) {
if (node.type !== 'Identifier') return false;
const target = this.refersTo(node.name, scopes);
return (
target !== null &&
modules.has(target.module) &&
(target.imported === '*' || target.imported === 'default')
);
}
// Static evaluation.
private evaluateIdentifier(name: string, scopes: readonly Node[]): Value {
const binding = this.lookup(name, scopes);
if (binding === 'global') {
if (name === '__dirname') return file(path.dirname(this.file));
if (name === '__filename') return file(this.file);
return UNKNOWN;
}
if (binding === 'ambiguous' || binding.kind !== 'const') return UNKNOWN;
const cached = this.values.get(binding);
if (cached) return cached;
if (this.evaluating.has(binding)) return UNKNOWN;
this.evaluating.add(binding);
const value = this.evaluate(binding.init, binding.scopes);
this.evaluating.delete(binding);
this.values.set(binding, value);
return value;
}
/** `path`/`url` function a callee names, or `cwd` for `process.cwd`. */
private pathFunction(callee: Node, scopes: readonly Node[]): string | null {
if (callee.type === 'Identifier') {
const target = this.refersTo(callee.name, scopes);
return target && (target.module === 'path' || target.module === 'url')
? target.imported
: null;
}
if (callee.type !== 'MemberExpression') return null;
const name = propertyName(callee);
if (name === null) return null;
const object = callee.object;
if (
object.type === 'Identifier' &&
object.name === 'process' &&
this.lookup('process', scopes) === 'global'
) {
return name === 'cwd' ? 'cwd' : null;
}
if (this.isModuleObject(object, PATH_MODULES, scopes)) return name;
if (
object.type === 'MemberExpression' &&
propertyName(object) === 'posix' &&
this.isModuleObject(object.object, PATH_ONLY, scopes)
) {
return name;
}
return null;
}
private evaluate(node: Node | null | undefined, scopes: readonly Node[]): Value {
if (!node) return UNKNOWN;
switch (node.type) {
case 'StringLiteral':
return text(node.value);
case 'TemplateLiteral': {
const parts: Value[] = [];
node.quasis.forEach((quasi, index) => {
parts.push(quasi.value.cooked == null ? UNKNOWN : text(quasi.value.cooked));
const expression = node.expressions[index];
if (expression) parts.push(this.evaluate(expression, scopes));
});
return this.concat(parts);
}
case 'BinaryExpression':
return node.operator === '+'
? this.concat([this.evaluate(node.left, scopes), this.evaluate(node.right, scopes)])
: UNKNOWN;
case 'Identifier':
return this.evaluateIdentifier(node.name, scopes);
case 'TSAsExpression':
case 'TSSatisfiesExpression':
case 'TSNonNullExpression':
case 'TSTypeAssertion':
case 'ParenthesizedExpression':
return this.evaluate(node.expression, scopes);
case 'MemberExpression':
return this.evaluateMember(node, scopes);
case 'CallExpression':
return this.evaluateCall(node, scopes);
case 'NewExpression':
return this.evaluateNew(node, scopes);
default:
return UNKNOWN;
}
}
private evaluateMember(node: MemberExpression, scopes: readonly Node[]): Value {
const name = propertyName(node);
if (
node.object.type === 'MetaProperty' &&
node.object.meta.name === 'import' &&
node.object.property.name === 'meta'
) {
if (name === 'url') return file(this.file, true);
if (name === 'filename') return file(this.file);
if (name === 'dirname') return file(path.dirname(this.file));
return UNKNOWN;
}
if (
(name === 'sep' || name === 'delimiter') &&
this.isModuleObject(node.object, PATH_ONLY, scopes)
) {
return text(name === 'sep' ? path.sep : path.delimiter);
}
const object = this.evaluate(node.object, scopes);
if (object.kind !== 'file' || !object.url) return UNKNOWN;
if (name === 'href') return object;
if (name === 'pathname') return { ...object, url: false };
return UNKNOWN;
}
private evaluateCall(node: CallExpression, scopes: readonly Node[]): Value {
if (
node.callee.type === 'MemberExpression' &&
propertyName(node.callee) === 'toString' &&
node.arguments.length === 0
) {
const object = this.evaluate(node.callee.object, scopes);
return object.kind === 'file' && object.url ? object : UNKNOWN;
}
const fn = this.pathFunction(node.callee, scopes);
if (fn === null) return UNKNOWN;
const args = node.arguments.map((arg) => this.evaluate(arg, scopes));
switch (fn) {
case 'cwd':
return file(this.context.packageDir);
case 'join':
return this.combine(args, 'join');
case 'resolve':
return this.combine(args, 'resolve');
case 'normalize':
return args[0]?.kind === 'text'
? text(path.normalize(args[0].text))
: args[0]?.kind === 'file' && !args[0].url
? { ...args[0], abs: path.normalize(args[0].abs) }
: UNKNOWN;
case 'dirname': {
const arg = args[0];
if (arg?.kind === 'text') return text(path.dirname(arg.text));
if (arg?.kind !== 'file') return UNKNOWN;
return arg.exact ? { ...arg, abs: path.dirname(arg.abs) } : arg;
}
case 'fileURLToPath':
return args[0]?.kind === 'file' && args[0].url ? { ...args[0], url: false } : UNKNOWN;
case 'pathToFileURL':
return args[0]?.kind === 'file' && !args[0].url ? { ...args[0], url: true } : UNKNOWN;
default:
return UNKNOWN;
}
}
private isUrlConstructor(callee: Node, scopes: readonly Node[]): boolean {
if (callee.type !== 'Identifier') return false;
if (callee.name === 'URL' && this.lookup('URL', scopes) === 'global') return true;
const target = this.refersTo(callee.name, scopes);
return target?.module === 'url' && target.imported === 'URL';
}
private evaluateNew(node: NewExpression, scopes: readonly Node[]): Value {
if (!this.isUrlConstructor(node.callee, scopes) || node.arguments.length !== 2) return UNKNOWN;
const spec = this.evaluate(node.arguments[0], scopes);
const base = this.evaluate(node.arguments[1], scopes);
if (base.kind !== 'file' || !base.url) return UNKNOWN;
if (!base.exact) return base;
if (spec.kind === 'unknown') return { ...base, abs: path.dirname(base.abs), exact: false };
if (spec.kind !== 'text' || /^[a-z][a-z0-9+.-]*:/i.test(spec.text)) return UNKNOWN;
return file(path.resolve(path.dirname(base.abs), spec.text), true);
}
/** `path.join` / `path.resolve` over evaluated arguments. */
private combine(parts: readonly Value[], mode: 'join' | 'resolve'): Value {
let acc: Value | undefined;
for (const part of parts) {
if (acc?.kind === 'file' && !acc.exact) return acc;
if (part.kind === 'unknown') {
return acc?.kind === 'file' ? { ...acc, exact: false } : UNKNOWN;
}
if (part.kind === 'file') {
if (part.url || (acc !== undefined && mode === 'join')) return UNKNOWN;
acc = part;
} else if (acc === undefined || (mode === 'resolve' && path.isAbsolute(part.text))) {
acc = part;
} else if (acc.kind === 'file') {
acc = { ...acc, abs: path.join(acc.abs, part.text) };
} else if (acc.kind === 'text') {
acc = text(path.join(acc.text, part.text));
}
}
if (acc === undefined) return mode === 'resolve' ? file(this.context.packageDir) : text('.');
if (mode === 'resolve' && acc.kind === 'text' && !path.isAbsolute(acc.text)) {
return file(path.resolve(this.context.packageDir, acc.text));
}
return acc;
}
/** String concatenation (templates and `+`). */
private concat(parts: readonly Value[]): Value {
let acc: Value = text('');
for (const part of parts) {
if (acc.kind === 'unknown' || (acc.kind === 'file' && !acc.exact)) return acc;
if (part.kind === 'unknown') {
if (acc.kind !== 'file') return UNKNOWN;
const prefix = /[\\/]$/.test(acc.abs) ? acc.abs : path.dirname(acc.abs);
return { ...acc, abs: path.normalize(prefix), exact: false };
}
if (part.kind === 'file') {
if (acc.kind !== 'text' || acc.text !== '') return UNKNOWN;
acc = part;
} else {
acc =
acc.kind === 'file' ? { ...acc, abs: acc.abs + part.text } : text(acc.text + part.text);
}
}
return acc.kind === 'file' && acc.exact ? { ...acc, abs: path.normalize(acc.abs) } : acc;
}
// Pass 2: edges, reads and test API.
private snippet(node: Node): string {
const raw = this.source.slice(node.start ?? 0, node.end ?? 0).replace(/\s+/g, ' ');
return raw.length > 100 ? `${raw.slice(0, 97)}...` : raw;
}
private edge(specifier: string, line: number, names: readonly string[] | null): void {
this.facts.edges.push({
target: { kind: 'specifier', specifier, fromDir: path.dirname(this.file) },
line,
names,
});
}
/** A load whose specifier is an expression; `base` is null when it is not statically known. */
private loadEdge(
arg: Node | undefined,
base: string | null,
node: Node,
names: readonly string[] | null,
scopes: readonly Node[],
): void {
const line = lineOf(node);
const value = arg ? this.evaluate(arg, scopes) : UNKNOWN;
if (value.kind === 'text' && (base !== null || !value.text.startsWith('.'))) {
this.facts.edges.push({
target: {
kind: 'specifier',
specifier: value.text,
fromDir: path.dirname(base ?? this.file),
},
line,
names,
});
} else if (value.kind === 'file' && value.exact) {
this.facts.edges.push({ target: { kind: 'file', abs: value.abs }, line, names });
} else {
this.facts.findings.push({
reason: 'unresolvable dynamic import',
line,
detail: this.snippet(arg ?? node),
});
}
if (arg) this.skipped.add(arg);
}
/** Names a `require()`/`import()` result is destructured into, or null for the whole module. */
private destructuredNames(node: Node, ancestors: readonly Node[]): readonly string[] | null {
let child = node;
let index = ancestors.length - 1;
while (
index >= 0 &&
(ancestors[index].type === 'AwaitExpression' || TS_WRAPPERS.has(ancestors[index].type))
) {
child = ancestors[index];
index -= 1;
}
const parent = ancestors[index];
if (parent?.type === 'MemberExpression' && parent.object === child) {
const name = propertyName(parent);
return name === null ? null : [name];
}
if (parent?.type !== 'VariableDeclarator' || parent.init !== child) return null;
if (parent.id.type !== 'ObjectPattern') return null;
const names: string[] = [];
for (const property of parent.id.properties) {
const name = property.type === 'ObjectProperty' ? keyName(property.key) : null;
if (name === null) return null;
names.push(name);
}
return names;
}
private importDeclaration(node: ImportDeclaration): void {
if (node.importKind === 'type') return;
if (node.specifiers.length === 0) {
this.edge(node.source.value, lineOf(node), []);
return;
}
const used = node.specifiers.filter(
(specifier) =>
!(specifier.type === 'ImportSpecifier' && specifier.importKind === 'type') &&
(this.references.get(specifier.local.name) ?? 0) > 0,
);
if (used.length === 0) return;
const names: string[] = [];
for (const specifier of used) {
if (specifier.type !== 'ImportSpecifier') {
this.edge(node.source.value, lineOf(node), null);
return;
}
names.push(keyName(specifier.imported) ?? '*');
}
this.edge(node.source.value, lineOf(node), names);
}
private reexport(node: ExportNamedDeclaration): void {
if (!node.source || node.exportKind === 'type') return;
const names: string[] = [];
for (const specifier of node.specifiers) {
if (specifier.type === 'ExportSpecifier') {
if (specifier.exportKind !== 'type') names.push(keyName(specifier.local) ?? '*');
} else {
this.edge(node.source.value, lineOf(node), null);
return;
}
}
if (names.length > 0) this.edge(node.source.value, lineOf(node), names);
}
/** Base file of a `createRequire(...)` call, or null when it is not static. */
private requireBase(call: CallExpression, scopes: readonly Node[]): string | null {
const value = this.evaluate(call.arguments[0], scopes);
return value.kind === 'file' && value.exact ? value.abs : null;
}
/** Load semantics of a call: the base its specifier resolves against, or null for none. */
private loadCall(node: CallExpression, scopes: readonly Node[]): { base: string | null } | null {
const callee = node.callee;
if (callee.type === 'Identifier') {
const binding = this.lookup(callee.name, scopes);
if (binding === 'global') return callee.name === 'require' ? { base: this.file } : null;
const init = typeof binding === 'string' || binding.kind !== 'const' ? null : binding;
const call = init ? unwrap(init.init) : null;
return init && isCreateRequireCall(call)
? { base: this.requireBase(call, init.scopes) }
: null;
}
if (isCreateRequireCall(callee)) return { base: this.requireBase(callee, scopes) };
if (
callee.type !== 'MemberExpression' ||
callee.object.type !== 'Identifier' ||
callee.object.name !== 'vi'
) {
return null;
}
switch (propertyName(callee)) {
case 'importActual':
case 'importMock':
return { base: this.file };
case 'mock':
case 'doMock': {
const factory = node.arguments[1];
const loadsOriginal =
!factory ||
!FUNCTION_NODES.has(factory.type) ||
(factory as Node & { params: readonly Node[] }).params.length > 0;
return loadsOriginal ? { base: this.file } : null;
}
default:
return null;
}
}
private isFsCall(callee: Node, scopes: readonly Node[]): boolean {
if (callee.type === 'Identifier') {
const target = this.refersTo(callee.name, scopes);
return target !== null && FS_MODULES.has(target.module) && target.imported !== '*';
}
if (callee.type !== 'MemberExpression') return false;
const object = callee.object;
return (
this.isModuleObject(object, FS_MODULES, scopes) ||
(object.type === 'MemberExpression' &&
propertyName(object) === 'promises' &&
this.isModuleObject(object.object, FS_MODULES, scopes))
);
}
private countTestApi(node: CallExpression): void {
const callee = node.callee;
if (callee.type === 'Identifier') {
const hook = HOOK_NAMES.find((name) => name === callee.name);
if (hook) this.facts.hooks[hook] += 1;
}
if (callee.type === 'CallExpression') this.calleeCalls.add(callee);
if (callee.type === 'MemberExpression') {
const name = propertyName(callee);
const root = calleeRoot(callee.object);
if ((name === 'each' || name === 'for') && root !== null && EACH_ROOTS.has(root)) {
this.facts.eachCalls += 1;
}
}
const root = calleeRoot(callee);
if (root !== null && TEST_ROOTS.has(root) && !this.calleeCalls.has(node)) {
this.facts.testCalls += 1;
}
}
private recordRead(node: Node, abs: string, exact: boolean): void {
const rel = toPosix(path.relative(this.context.packageDir, abs));
let reason: string | null = null;
if (rel === '..' || rel.startsWith('../') || path.isAbsolute(rel)) {
// A prefix above the package may still end inside it.
reason =
!exact && isInside(this.context.packageDir, abs)
? 'unresolvable file path'
: 'reads source outside gitnexus/';
} else if (isWithin(rel, 'src')) {
reason = 'reads source under src/';
} else {
const ignored = DEFAULT_IGNORED_DIRS.find((dir) => isWithin(rel, dir));
if (ignored === 'test/fixtures') reason = 'reads test fixtures';
else if (ignored !== undefined) reason = `reads ignored dir: ${ignored}/`;
else if (!exact && ['src', ...DEFAULT_IGNORED_DIRS].some((dir) => contains(rel, dir))) {
reason = 'unresolvable file path';
}
}
if (reason !== null) {
this.facts.reads.push({
reason,
line: lineOf(node),
detail: `${this.snippet(node)} -> ${this.context.display(abs)}${exact ? '' : '/...'}`,
});
}
}
/** Read rule (a): evaluates an anchored path expression; returns false when it was decided. */
private pathExpression(node: Node, scopes: readonly Node[]): boolean {
const value = this.evaluate(node, scopes);
if (value.kind !== 'file') return value.kind === 'unknown';
this.recordRead(node, value.abs, value.exact);
return false;
}
private isPathExpression(node: Node, scopes: readonly Node[]): boolean {
switch (node.type) {
case 'Identifier':
case 'MemberExpression':
case 'TemplateLiteral':
return true;
case 'BinaryExpression':
return node.operator === '+';
case 'NewExpression':
return this.isUrlConstructor(node.callee, scopes);
case 'CallExpression':
return (
this.pathFunction(node.callee, scopes) !== null ||
(node.callee.type === 'MemberExpression' && propertyName(node.callee) === 'toString')
);
default:
return false;
}
}
private analyse(node: Node, ancestors: readonly Node[], scopes: readonly Node[]): boolean {
if (this.skipped.has(node)) return false;
switch (node.type) {
case 'ImportDeclaration':
this.importDeclaration(node);
return false;
case 'ExportNamedDeclaration':
if (!node.source) return true;
this.reexport(node);
return false;
case 'ExportAllDeclaration':
if (node.exportKind !== 'type') this.edge(node.source.value, lineOf(node), null);
return false;
case 'TSImportEqualsDeclaration':
if (
node.importKind !== 'type' &&
node.moduleReference.type === 'TSExternalModuleReference' &&
(this.references.get(node.id.name) ?? 0) > 0
) {
this.edge(node.moduleReference.expression.value, lineOf(node), null);
}
return false;
case 'ImportExpression':
this.loadEdge(
node.source,
this.file,
node,
this.destructuredNames(node, ancestors),
scopes,
);
return false;
case 'CallExpression': {
this.countTestApi(node);
const load = this.loadCall(node, scopes);
if (load) {
const arg = node.arguments[0];
this.loadEdge(arg, load.base, node, this.destructuredNames(node, ancestors), scopes);
}
if (!this.context.testSide) return true;
const first = node.arguments[0];
if (first && this.isFsCall(node.callee, scopes)) {
const value = this.evaluate(first, scopes);
if (value.kind === 'text' && !path.isAbsolute(value.text) && !value.text.includes(':')) {
this.recordRead(first, path.resolve(this.context.packageDir, value.text), true);
}
}
return this.isPathExpression(node, scopes) ? this.pathExpression(node, scopes) : true;
}
default:
if (this.context.testSide && this.isPathExpression(node, scopes)) {
return this.pathExpression(node, scopes);
}
return true;
}
}
}
// ── Classifier ─────────────────────────────────────────────────────────────
const PARSER_PLUGINS: NonNullable<ParserOptions['plugins']> = ['typescript', 'decorators-legacy'];
function parseModule(abs: string, source: string): File {
const ext = path.extname(abs);
return parse(source, {
sourceType: ext === '.cjs' || ext === '.cts' ? 'commonjs' : 'module',
plugins: ext === '.tsx' || ext === '.jsx' ? [...PARSER_PLUGINS, 'jsx'] : PARSER_PLUGINS,
errorRecovery: false,
});
}
const emptyFacts = (finding: Finding): ModuleFacts => ({
edges: [],
findings: [finding],
reads: [],
hooks: { beforeAll: 0, beforeEach: 0, afterAll: 0, afterEach: 0 },
eachCalls: 0,
testCalls: 0,
});
const reject = (reason: string, detail: string): Resolved => ({ kind: 'reject', reason, detail });
/** Native hit of a package: the native package, the dependency chain to it and why. */
interface NativeHit {
pkg: string;
chain: string[];
why: string;
}
/** Classifies test files of one repo checkout; caches parsed modules and resolutions. */
export class Classifier {
readonly packageDir: string;
private readonly sharedDir: string;
private readonly sharedSrcDir: string;
private readonly productionDirs: readonly string[];
private readonly factsCache = new Map<string, ModuleFacts>();
private readonly resolutions = new Map<string, Resolved>();
private readonly kinds = new Map<string, 'file' | 'dir' | null>();
private readonly directNative = new Map<string, string | null>();
private readonly packageNative = new Map<string, NativeHit | null>();
private readonly displays = new Map<string, string>();
private sharedExports: ReadonlyMap<string, string> | undefined;
constructor(readonly repoRoot: string) {
this.packageDir = path.join(repoRoot, 'gitnexus');
this.sharedDir = path.join(repoRoot, SHARED_PACKAGE);
this.sharedSrcDir = path.join(this.sharedDir, 'src');
this.productionDirs = [path.join(this.packageDir, 'src'), this.sharedSrcDir];
}
private display(abs: string): string {
let shown = this.displays.get(abs);
if (shown === undefined) {
shown = isInside(abs, this.repoRoot) ? toPosix(path.relative(this.repoRoot, abs)) : abs;
this.displays.set(abs, shown);
}
return shown;
}
private kind(abs: string): 'file' | 'dir' | null {
let kind = this.kinds.get(abs);
if (kind === undefined) {
const stat = statSync(abs, { throwIfNoEntry: false });
kind = stat?.isFile() ? 'file' : stat?.isDirectory() ? 'dir' : null;
this.kinds.set(abs, kind);
}
return kind;
}
private facts(abs: string): ModuleFacts {
const cached = this.factsCache.get(abs);
if (cached) return cached;
let facts: ModuleFacts;
try {
const source = readFileSync(abs, 'utf8');
facts = new ModuleAnalysis(abs, source, parseModule(abs, source), {
packageDir: this.packageDir,
testSide: !this.productionDirs.some((dir) => isInside(abs, dir)),
display: (target) => this.display(target),
}).facts;
} catch (error) {
const message = errorMessage(error);
facts = emptyFacts({ reason: 'parse error', line: 0, detail: message.split('\n')[0] });
}
this.factsCache.set(abs, facts);
return facts;
}
// Resolution.
private findFile(abs: string): string | null {
if (this.kind(abs) === 'file') return abs;
const ext = path.extname(abs);
for (const tsExt of TS_FOR_JS[ext] ?? []) {
const candidate = abs.slice(0, -ext.length) + tsExt;
if (this.kind(candidate) === 'file') return candidate;
}
for (const appended of APPENDED_EXTENSIONS) {
if (this.kind(abs + appended) === 'file') return abs + appended;
}
if (this.kind(abs) === 'dir') {
for (const appended of APPENDED_EXTENSIONS) {
const index = path.join(abs, `index${appended}`);
if (this.kind(index) === 'file') return index;
}
}
return null;
}
private locationReason(importer: string, target: string): string | null {
if (isInside(importer, this.sharedSrcDir)) {
const allowed =
isInside(target, this.sharedSrcDir) || target === path.join(this.sharedDir, 'package.json');
return allowed ? null : 'imports outside gitnexus/';
}
if (!isInside(target, this.packageDir)) return 'imports outside gitnexus/';
const rel = toPosix(path.relative(this.packageDir, target));
const ignored = DEFAULT_IGNORED_DIRS.find((dir) => isWithin(rel, dir));
return ignored === undefined ? null : `imports from ignored dir: ${ignored}/`;
}
private resolveFile(importer: string, abs: string, shown: string, aliased: boolean): Resolved {
const found = this.findFile(abs);
if (found === null) return reject(`unresolved import: ${shown}`, `imports ${shown}`);
if (found.endsWith('.node')) {
return reject(`native binding: ${this.display(found)}`, `imports ${shown}`);
}
const location = aliased ? null : this.locationReason(importer, found);
if (location !== null) {
return reject(location, `imports ${shown} -> ${this.display(found)}`);
}
return WALKED_EXTENSIONS.has(path.extname(found)) && !found.endsWith('.d.ts')
? { kind: 'module', abs: found }
: { kind: 'leaf', abs: found };
}
/** `exports` subpath of gitnexus-shared to its `src/` file (`./dist/X.js` -> `src/X.ts`). */
private sharedExportMap(): ReadonlyMap<string, string> {
if (this.sharedExports) return this.sharedExports;
const map = new Map<string, string>();
try {
const manifest: unknown = JSON.parse(
readFileSync(path.join(this.sharedDir, 'package.json'), 'utf8'),
);
const exportsField =
typeof manifest === 'object' && manifest !== null && 'exports' in manifest
? manifest.exports
: undefined;
if (typeof exportsField === 'object' && exportsField !== null) {
for (const [subpath, target] of Object.entries(exportsField)) {
const resolved: unknown =
typeof target === 'string'
? target
: typeof target === 'object' && target !== null && 'default' in target
? target.default
: undefined;
const match = typeof resolved === 'string' ? /^\.\/dist\/(.+)\.js$/.exec(resolved) : null;
if (match) map.set(subpath, path.join(this.sharedSrcDir, `${match[1]}.ts`));
}
}
} catch {
// A missing or broken manifest leaves the alias unresolved.
}
this.sharedExports = map;
return map;
}
private findPackageDir(fromDir: string, name: string): string | null {
let dir = fromDir;
for (;;) {
const candidate = path.join(dir, 'node_modules', name);
if (this.kind(path.join(candidate, 'package.json')) === 'file') return candidate;
const parent = path.dirname(dir);
if (parent === dir) return null;
dir = parent;
}
}
private readManifest(dir: string): Record<string, unknown> {
try {
const data: unknown = JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8'));
return typeof data === 'object' && data !== null ? (data as Record<string, unknown>) : {};
} catch {
return {};
}
}
/** First `.node` file of a package, outside nested `node_modules`, in sorted order. */
private findNodeBinary(dir: string, relative = ''): string | null {
let entries;
try {
entries = readdirSync(path.join(dir, relative), { withFileTypes: true });
} catch {
return null;
}
const sorted = [...entries].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
for (const entry of sorted) {
const child = relative === '' ? entry.name : `${relative}/${entry.name}`;
if (entry.isFile() && entry.name.endsWith('.node')) return child;
if (entry.isDirectory() && entry.name !== 'node_modules' && !entry.name.startsWith('.')) {
const found = this.findNodeBinary(dir, child);
if (found !== null) return found;
}
}
return null;
}
/** Why a package itself is native, or null. */
private nativeSelf(dir: string, name: string): string | null {
const key = `${dir}\0${name}`;
const cached = this.directNative.get(key);
if (cached !== undefined) return cached;
let why: string | null = null;
if (KNOWN_NATIVE_PACKAGES.some((pattern) => pattern.test(name))) why = 'known native package';
else if (this.readManifest(dir).gypfile === true) why = 'declares gypfile';
else if (this.kind(path.join(dir, 'binding.gyp')) === 'file') why = 'has binding.gyp';
else {
const binary = this.findNodeBinary(dir);
if (binary !== null) why = `ships ${binary}`;
}
this.directNative.set(key, why);
return why;
}
/** Breadth-first over installed runtime dependencies; the first native package found. */
private nativeOf(rootDir: string, rootName: string): NativeHit | null {
const cached = this.packageNative.get(rootDir);
if (cached !== undefined) return cached;
const realOf = (dir: string): string => {
try {
return realpathSync(dir);
} catch {
return dir;
}
};
const queue: { dir: string; name: string; chain: string[] }[] = [
{ dir: rootDir, name: rootName, chain: [rootName] },
];
const seen = new Set([realOf(rootDir)]);
let hit: NativeHit | null = null;
for (let index = 0; index < queue.length && hit === null; index += 1) {
const { dir, name, chain } = queue[index];
const why = this.nativeSelf(dir, name);
if (why !== null) {
hit = { pkg: name, chain, why };
break;
}
const manifest = this.readManifest(dir);
const deps = new Set<string>();
for (const field of ['dependencies', 'optionalDependencies']) {
const value = manifest[field];
if (typeof value === 'object' && value !== null) {
for (const dep of Object.keys(value)) deps.add(dep);
}
}
for (const dep of [...deps].sort()) {
const depDir = this.findPackageDir(dir, dep);
if (depDir === null || seen.has(realOf(depDir))) continue;
seen.add(realOf(depDir));
queue.push({ dir: depDir, name: dep, chain: [...chain, dep] });
}
}
this.packageNative.set(rootDir, hit);
return hit;
}
private resolveSpecifier(importer: string, specifier: string, fromDir: string): Resolved {
if (specifier.startsWith('.') || path.isAbsolute(specifier)) {
if (/[?#]/.test(specifier)) {
return reject(`unsupported import specifier: ${specifier}`, `imports ${specifier}`);
}
return this.resolveFile(importer, path.resolve(fromDir, specifier), specifier, false);
}
if (specifier.startsWith('file:')) {
let abs: string;
try {
abs = fileURLToPath(specifier);
} catch {
return reject(`unresolved import: ${specifier}`, `imports ${specifier}`);
}
return this.resolveFile(importer, abs, specifier, false);
}
if (specifier.startsWith('node:') || BUILTINS.has(specifier)) {
return { kind: 'builtin', name: normaliseModule(specifier) };
}
const name = packageName(specifier);
if (name === null) return reject(`unresolved import: ${specifier}`, `imports ${specifier}`);
if (name === SHARED_PACKAGE) {
const subpath = specifier === name ? '.' : `.${specifier.slice(name.length)}`;
const target = this.sharedExportMap().get(subpath);
return target === undefined
? reject(`unresolved import: ${specifier}`, `imports ${specifier} (not in exports)`)
: this.resolveFile(importer, target, specifier, true);
}
if (isHarnessPackage(name)) return { kind: 'package', name };
if (KNOWN_NATIVE_PACKAGES.some((pattern) => pattern.test(name))) {
return reject(`native package: ${name}`, `imports ${specifier} (known native package)`);
}
if (KNOWN_PROCESS_PACKAGES.some((pattern) => pattern.test(name))) {
return reject(
'spawns processes: child_process',
`imports ${specifier} (the package loads node:child_process when imported)`,
);
}
const dir = this.findPackageDir(fromDir, name);
if (dir === null) {
return reject(`unresolved import: ${specifier}`, `imports ${specifier} (not installed)`);
}
const native = this.nativeOf(dir, name);
return native === null
? { kind: 'package', name }
: reject(
`native package: ${native.pkg}`,
`imports ${specifier} (${native.chain.join(' -> ')}: ${native.why})`,
);
}
private resolve(importer: string, target: EdgeTarget): Resolved {
const zone = isInside(importer, this.sharedSrcDir) ? 'shared' : 'package';
const key =
target.kind === 'file'
? `${zone}\0file\0${target.abs}`
: `${zone}\0${target.fromDir}\0${target.specifier}`;
let resolved = this.resolutions.get(key);
if (resolved === undefined) {
resolved =
target.kind === 'file'
? this.resolveFile(importer, target.abs, this.display(target.abs), false)
: this.resolveSpecifier(importer, target.specifier, target.fromDir);
this.resolutions.set(key, resolved);
}
return resolved;
}
// Closure walk.
/** Classifies one package-relative test file. */
classifyFile(testFile: string): FileVerdict {
const testAbs = path.join(this.packageDir, testFile);
const parents = new Map<string, string | null>([[testAbs, null]]);
const queue = [testAbs];
const closure = new Set<string>();
const packages = new Set<string>();
const builtins = new Set<string>();
const evidence = new Map<string, Evidence>();
const chain = (abs: string): string[] => {
const via: string[] = [];
for (let at: string | null | undefined = abs; at; at = parents.get(at)) {
via.unshift(this.display(at));
}
return via;
};
const record = (module: string, finding: Finding): void => {
if (evidence.has(finding.reason)) return;
evidence.set(finding.reason, {
reason: finding.reason,
module: this.display(module),
line: finding.line,
detail: finding.detail,
via: chain(module),
});
};
if (this.kind(testAbs) !== 'file') {
record(testAbs, { reason: 'parse error', line: 0, detail: 'test file not found' });
queue.length = 0;
}
for (let index = 0; index < queue.length; index += 1) {
const module = queue[index];
closure.add(this.display(module));
const facts = this.facts(module);
for (const finding of facts.findings) record(module, finding);
for (const finding of facts.reads) record(module, finding);
for (const edge of facts.edges) {
const resolved = this.resolve(module, edge.target);
switch (resolved.kind) {
case 'module':
if (!parents.has(resolved.abs)) {
parents.set(resolved.abs, module);
queue.push(resolved.abs);
}
break;
case 'leaf':
closure.add(this.display(resolved.abs));
break;
case 'package':
packages.add(resolved.name);
break;
case 'reject':
record(module, { reason: resolved.reason, line: edge.line, detail: resolved.detail });
break;
case 'builtin':
builtins.add(resolved.name);
if (PROCESS_BUILTINS.has(resolved.name)) {
record(module, {
reason: `spawns processes: ${resolved.name}`,
line: edge.line,
detail: `imports node:${resolved.name}`,
});
}
if (
resolved.name === WORKER_BUILTIN &&
(edge.names === null || edge.names.includes('Worker'))
) {
record(module, {
reason: `spawns workers: ${WORKER_BUILTIN}`,
line: edge.line,
detail: `imports ${edge.names === null ? 'all of' : 'Worker from'} node:${WORKER_BUILTIN}`,
});
}
break;
}
}
}
const sortedEvidence = [...evidence.values()].sort((a, b) =>
a.reason < b.reason ? -1 : a.reason > b.reason ? 1 : 0,
);
const testFacts = this.kind(testAbs) === 'file' ? this.facts(testAbs) : undefined;
return {
file: testFile,
status: sortedEvidence.length === 0 ? 'accepted' : 'rejected',
reasons: sortedEvidence.map((entry) => entry.reason),
evidence: sortedEvidence,
hooks: {
...(testFacts?.hooks ?? { beforeAll: 0, beforeEach: 0, afterAll: 0, afterEach: 0 }),
},
eachCalls: testFacts?.eachCalls ?? 0,
testCalls: testFacts?.testCalls ?? 0,
closure: [...closure].sort(),
packages: [...packages].sort(),
builtins: [...builtins].sort(),
};
}
}
// ── Classification ─────────────────────────────────────────────────────────
/** Every `*.test.ts` file under a package-relative directory, package-relative and sorted. */
export function listTestFiles(packageDir: string, testRoot: string): string[] {
const found: string[] = [];
const visit = (relative: string): void => {
for (const entry of readdirSync(path.join(packageDir, relative), { withFileTypes: true })) {
const child = `${relative}/${entry.name}`;
if (entry.isDirectory() && entry.name !== 'node_modules') visit(child);
else if (entry.isFile() && entry.name.endsWith('.test.ts')) found.push(child);
}
};
visit(testRoot.replace(/\/+$/, ''));
return found.sort();
}
const countBy = (keys: readonly string[]): Record<string, number> => {
const counts = new Map<string, number>();
for (const key of keys) counts.set(key, (counts.get(key) ?? 0) + 1);
return Object.fromEntries([...counts.entries()].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
};
const categoryOf = (reason: string): string => {
const index = reason.indexOf(': ');
return index === -1 ? reason : reason.slice(0, index);
};
/** Classifies the test files of a checkout; output order is deterministic (sorted by path). */
export function classify(options: ClassifyOptions): Classification {
const classifier = new Classifier(options.repoRoot);
const testRoot = options.testFiles ? null : (options.testRoot ?? 'test/unit');
const testFiles = options.testFiles
? [...new Set(options.testFiles)].sort()
: listTestFiles(classifier.packageDir, testRoot ?? 'test/unit');
const files = testFiles.map((testFile) => classifier.classifyFile(testFile));
const accepted = files.filter((verdict) => verdict.status === 'accepted');
const rejected = files.filter((verdict) => verdict.status === 'rejected');
return {
schemaVersion: CLASSIFY_SCHEMA_VERSION,
testRoot,
files,
accepted: accepted.map((verdict) => verdict.file),
rejected: rejected.map(({ file: testFile, reasons }) => ({ file: testFile, reasons })),
acceptedWithBeforeAll: accepted
.filter((verdict) => verdict.hooks.beforeAll > 0)
.map((verdict) => verdict.file),
summary: {
total: files.length,
accepted: accepted.length,
rejected: rejected.length,
byReason: countBy(rejected.flatMap((verdict) => verdict.reasons)),
byCategory: countBy(
rejected.flatMap((verdict) => [...new Set(verdict.reasons.map(categoryOf))]),
),
},
};
}
/** Human-readable summary: counts, rejection categories and accepted files with `beforeAll`. */
export function renderSummary(result: Classification): string {
const { summary } = result;
const lines = [
`classify: ${summary.total} test files: ${summary.accepted} accepted, ${summary.rejected} rejected`,
'rejected files per reason category (a file counts once per category):',
...Object.entries(summary.byCategory).map(([category, count]) => ` ${category}: ${count}`),
`accepted files with beforeAll: ${result.acceptedWithBeforeAll.length}`,
];
const byFile = new Map(result.files.map((verdict) => [verdict.file, verdict]));
for (const testFile of result.acceptedWithBeforeAll) {
const verdict = byFile.get(testFile);
lines.push(
` ${testFile} (beforeAll ${verdict?.hooks.beforeAll ?? 0}, test call sites ` +
`${verdict?.testCalls ?? 0}, each ${verdict?.eachCalls ?? 0}, closure ` +
`${verdict?.closure.length ?? 0} files)`,
);
}
return lines.join('\n');
}
/** JSON form for the CLI; drops closure lists unless asked for. */
export function toJson(result: Classification, withClosure: boolean): string {
const files = withClosure
? result.files
: result.files.map(
({ closure: _closure, packages: _packages, builtins: _builtins, ...rest }) => rest,
);
return `${JSON.stringify({ ...result, files }, null, 2)}\n`;
}
// ── CLI ────────────────────────────────────────────────────────────────────
if (isMainModule(import.meta.url)) {
const usage =
'usage: node --import tsx tools/mutation/classify/classify.ts [--root <dir>] ' +
'[--file <package-relative path>]... [--out <file.json>] [--closure]';
let values: { root?: string; file?: string[]; out?: string; closure?: boolean };
try {
({ values } = parseArgs({
options: {
root: { type: 'string' },
file: { type: 'string', multiple: true },
out: { type: 'string' },
closure: { type: 'boolean', default: false },
},
strict: true,
}));
} catch (error) {
console.error(`${errorMessage(error)}\n${usage}`);
process.exit(2);
}
const repoRoot = fileURLToPath(new URL('../../../../', import.meta.url));
try {
const result = classify({
repoRoot,
testFiles: values.file && values.file.length > 0 ? values.file : undefined,
testRoot: values.root,
});
const json = toJson(result, values.closure === true);
if (values.out) {
writeFileSync(path.resolve(values.out), json);
console.log(renderSummary(result));
} else {
process.stdout.write(json);
}
} catch (error) {
console.error(errorMessage(error));
process.exitCode = 2;
}
}