Files
msd-core/tests/qa/mutations.cjs
Tom Boucher 33fd203ccd test(#2966): loop QA walk — drive real scenarios across all five loop steps (#2976)
* test(#2966): loop QA walk — drive real scenarios across all five loop steps

Adds a headless walk that carries accumulating project state across
discuss -> plan -> execute -> verify -> ship against one temp project,
layered over the existing tests/helpers.cjs runGsdTools substrate.

Findings carry severity. A violation breaks a stated contract and fails
the build; a smell is legal under today's implementation but structurally
questionable, is recorded, and never reddens CI. Without that split an
oracle set derived from current behavior can only ever confirm current
behavior -- the harness could not say "this works and is still wrong".

The end-to-end test asserts the walk produces at least one smell: a QA
harness that reports nothing on a first run against a real engine is far
more likely mis-specified than the engine is perfect. It deliberately does
not pin smell ids or counts, which would re-freeze current behavior.

First run against the real engine: 0 violations, 3 smell classes --
init returns agents_dir outside the project tree; smart-entry emits prose
unconditionally so routing cannot be asserted; state-snapshot reports a
missing STATE.md through a payload key with exit 0.

Also fixes tests/fixtures/index.cjs: createFixture with git:true and
planning:false staged nothing, so the commit failed with "nothing to
commit". That combination was unreachable until greenfield needed it.

Extends RULESET.TESTS.feedback-loop-convergence from estimation to the
loop itself. Design lock: docs/adr/2966-loop-qa-walk.md.

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

* test(#2966): wire fault injection, make perturbations discriminating

Independent review found tests/qa/mutations.cjs entirely unwired: 462
lines exercised only by their own unit tests, with no mutation hook in
the scenario DSL and no scenario applying one, while the module header
and the ADR described fault injection in the present tense. Dead code
documented as live.

Adds a `mutate` step field, three perturbation scenarios, and a wiring
detector: a self-test scenario whose expectations are known-false and
which MUST fail. The previous anti-vacuity check asserted only that the
walk produced a smell, which passes on well-known engine behavior
regardless of whether the harness wiring works.

First perturbation attempt produced zero signal -- progress does not
structurally parse ROADMAP.md, so a corrupted roadmap sailed through. A
perturbation that cannot fail is the same defect in a new costume.
Probes now target roadmap get-phase, and each mutated step runs a clean
baseline first so `mutationObserved` records whether the corruption
changed anything at all.

Also clears four review findings: classify() returned PROSE for exit-0
with empty stdout; `warnings` was structurally unpopulatable on the
success path (execFileSync discards it) and is now documented as
error-path-only; read-only-idempotence passed vacuously when asked to
check idempotence without the data to check it; the ADR miscounted the
oracles.

Discrimination matrix across 8 mutations x 6 commands: bom,
duplicate-phase-id and escaped-pipes are absorbed silently by every
probed surface, and progress / smart-entry / roadmap validate never
reacted to any mutation.

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

* test(#2966): add path-containment guard for scenario-supplied targets

Security review found scenario-supplied paths joined to the temp project
with no containment check. step.mutate.target and agent.write keys were
validated only as non-empty strings, so a target of ../../../../etc/hosts
reached fs.unlinkSync / fs.writeFileSync / fs.symlinkSync outside the
project. The symlink mutation was worst: it read the traversed file, wrote
a sibling copy, deleted the original and symlinked it back.

Not exploitable today -- all shipped scenarios target .planning/ROADMAP.md
and scenarios are repo-committed, not runtime input. Fixed anyway: it is a
live primitive any future scenario or copied helper can reach.

Adds tests/qa/paths.cjs with resolveWithin(): rejects absolute paths, NUL
bytes and empty input, normalizes separators unconditionally, and requires
containment by path segment so a sibling like <base>-evil is not treated as
inside. Non-existent targets resolve via nearest existing ancestor rather
than falling back to a lexical compare. Scenario load now rejects traversing
or absolute targets up front.

oracles.cjs previously carried its own copy of the containment logic; both
now share paths.cjs, since a duplicated containment check is exactly the
divergence class this repo calls out.

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

* test(#2966): complete trajectory corpus, report emission, boundary-aware oracle

Adds the remaining trajectories and drives all 11 mutations end-to-end.
20 scenarios, 72 steps, 0 violations, 25 smells.

Adds qa-report.json with per-step verdicts and a copy-pasteable repro
command, plus --keep / GSD_QA_KEEP=1 to preserve a failing tree. A repro
line for a tree that was not preserved is marked NOT RUNNABLE rather than
emitting a command pointing at a deleted directory.

monotonic-progress is now boundary-aware. Two scenarios had been trimmed
to stop the oracle complaining at a milestone rollover, which destroys the
signal the trajectory exists to produce. Evidence: counters legitimately
reset to zero at milestone complete, but the payload milestone_version
lags until a new ROADMAP.md is written. So the oracle now scopes by
milestone plus workstream, keeps a same-scope decrease as a violation, and
records a boundary crossing as a smell. Both scenarios walk the real
boundary again.

Standards review fixes: oracle findings now carry a structured subject so
tests assert on typed fields instead of substring-matching the free-form
detail string, resolveWithin throws a typed EPATHESCAPE error, and the
absolute-path predicate scenario.cjs had re-implemented now comes from
paths.cjs.

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

* test(#2966): fix silently-vacuous fixtures and guard the class

Every fixture carried its #2371 provenance comment BEFORE the frontmatter
block, and extractFrontmatter returns {} when anything precedes the opening
---. So every scenario reading status/phase/name was operating on an empty
object and reporting green. Nine fixtures repositioned; the comment stays,
it just moves below the closing ---.

Both UAT fixtures lacked a parser-recognized result block, so
evaluateUatPassed saw checks.length===0 and could never return passed:true.
The uat-fail-then-remediate scenario could not have proven a remediation.
Its expect block only inspected blockers, which is empty before AND after,
which is why the corpus never noticed. Both fixtures now carry real result
blocks and the scenario asserts passed and no_uat_artifacts on each side of
the flip.

The actual deliverable is the guard: a fixture-integrity block asserting
every fixture with a frontmatter shape parses to a non-empty object, that
every fixture carries its provenance marker, and that the two UAT fixtures
produce opposite verdicts through the real evaluateUatPassed. The first
guard written required --- at byte 0, which would never have fired on the
regression it exists to prevent; it was rewritten and proven by deliberately
re-breaking a fixture.

No engine defect here. no_uat_artifacts means no parsed check items, not no
UAT files, and it was reporting correctly on fixtures that had none.

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

* test(#2966): make the walk report — smell ratchet, baseline, CI job

The harness computed smells into a gitignored qa-report.json that nothing
read. In CI it surfaced nothing at all: violations failed the build, but the
half of the tool that says "this works and is still wrong" was inert. A QA
tool nobody hears is decoration.

Adds a ratchet on the same idiom this repo already uses three times over
(the regression-test-name allowlist, the emitted-drift acks, the size
baseline): a committed smell-baseline.json, per-PR acknowledgment fragments
under tests/qa/smell-acks/, and a ratchet script wired into CI.

The design invariant is preserved exactly. A smell still never fails a build
on its own merits. What fails is an UNACKNOWLEDGED NEW smell -- the absence
of a decision -- leaving an author two honest exits: fix it, or record a
fragment with a real reason. An empty reason is rejected. The baseline is
shrink-only, so a fixed smell must prune its entry. Violations remain
unacknowledgeable.

Fingerprints are composed only from stable fields (oracle id, scenario,
argv, subject discriminator) -- never temp paths, timestamps or counts.
Verified byte-identical across two runs in separate temp dirs; an unstable
fingerprint would have false-positived every CI run.

CI gains a qa-loop-walk job that runs the suite and the ratchet, uploads the
report with `if: always()` (it matters most when it failed), and renders a
summary a reviewer reads without downloading anything.

Also fixes the report runner invoking main() unconditionally on require, so
importing it double-ran every scenario and clobbered its own output.

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

* test(#2966): every smell terminates in a defect or a fixed detector

The baseline accepted a smell with a free-text reason. That is a mechanism
for designing smells in -- an allowlist nobody revisits. The harness is
brand new, so nothing it found is inherited legacy; every finding is a
FIRST finding. Each must now terminate in exactly one of two states:

  REAL           -> an assigned defect, entry carries the issue number
  FALSE POSITIVE -> the detector is wrong and gets fixed, never baselined

There is no third "accepted with a good explanation" state, so the ratchet
now requires a positive-integer `issue` on every entry. A reason may remain
as a human note but can never substitute. `--update` refuses to invent
issue numbers: a new smell is written with `issue: null` and a TODO, and
the next plain run rejects it, forcing triage rather than accumulation.

Working the 21 existing entries through that rule found 16 were my own
detectors being wrong:

value-hygiene (10) flagged $.agents_dir, a field whose entire contract is
to point at the install tree outside any project. Fixed with a leaf-key
allowlist of contractually-external fields, verified as the only such key
in the init payload. Genuinely unexpected out-of-project paths still smell.

monotonic-progress (6) fired on legitimate boundary crossings -- milestone
v1.0 to v2.0, workstream beta to alpha -- and on one payload carrying no
scope fields at all, where a change cannot even be known. Scope changes now
reset silently and scope-less observations are skipped. The same-scope
decrease remains a violation; that is the real invariant and is regression-
guarded.

The five survivors are real and now tracked: soft-error-exit-zero (#2980),
untyped-success (#2979). Baseline 25 -> 5.

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

* test(#2966): keep the ratchet out of the tarball, unpin the qa CI job

The remote matrix returned failed -- 3 unique failures, identical on
node22 and node24, both root causes in this branch's own diff.

The ratchet lives under scripts/, which ships in the npm tarball, and it
requires three modules under tests/, which does not. In a published
install it is MODULE_NOT_FOUND at load. This is exactly the class the
#2858 guard was added to catch, and it caught it. Fixed the way #2858
fixed the same shape for its own repo-only CI script: a targeted files[]
negation, so the ratchet stays in the repo for CI and out of the tarball.
Not solved by moving or inlining the required modules -- the ratchet must
keep using the same code the harness uses, or the two drift.

Verified both directions: the script is no longer in the pack list, and
build-hooks.js, fix-slash-commands.cjs and gen-capability-registry.cjs are
all still shipped. Over-negating there would have broken installs, since
bin/install.js requires them.

The qa-loop-walk job also carried CI_REBASE_BASE_SHA copied from a
neighbouring job without the paired GSD_EMITTED_BASE, which the #2854
invariant forbids by name: diverging them makes the differential compare a
tree against a baseline from a different commit. The job runs only the qa
suite and the ratchet and invokes no emitted-attribution test, so it needs
no rebase-pinned base at all -- the step was removed rather than paired.

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

* test(#2966): stop monotonic-progress going blind on scope-less payloads

The full remote suite caught a false NEGATIVE I introduced while fixing a
false positive. Silencing the boundary-crossing noise had made the oracle
skip ANY observation lacking milestone fields -- so a minimal payload like
{total_summaries: n} produced no violation at all, and the oracle stopped
catching the exact defect it exists to catch. For a QA tool that is
strictly worse than the noise it replaced.

Scope is only indeterminate when the two observations DISAGREE about
having it:

  both scoped, same scope, decrease -> VIOLATION
  both scoped, different scope      -> reset silently
  NEITHER scoped, decrease          -> VIOLATION   (the regression)
  mixed                             -> skip the comparison

Implementing the mixed case surfaced a second blind spot: advancing the
reference point on a skipped pair lets a scope-less observation sitting
between two same-scope ones mask a real decrease. Mixed now leaves the
reference untouched. All four branches carry explicit coverage; only one
did before, which is why this shipped.

The self-test that failed was right and the code was wrong, so the code
moved. Corpus behavior is unchanged: still 5 smells, 0 new, 0 stale, 0
violations.

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

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-01 16:13:28 -04:00

464 lines
15 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
/**
* mutations.cjs — a catalog of deterministic corruptions applied to a
* planning-artifact string (or to the file on disk, for the two structural
* ones).
*
* WHY THIS FILE EXISTS
* ────────────────────
* A QA harness walks an otherwise-valid scenario and, at one chosen step,
* applies exactly one mutation from `MUTATIONS`. The harness then asserts
* the engine under test degrades to a structured error rather than
* crashing, hanging, or silently producing wrong state. Every mutation here
* is a PURE, DETERMINISTIC transform (content-kind) or a scripted disk
* operation (file-kind) — no `Math.random()`, no `Date.now()`, no hidden
* global state — so a failing run is always reproducible from the mutation
* id alone.
*
* CONTENT-KIND mutations take a string and return a string (except
* `nonsequential-phases`, documented below, which may return the exported
* `NOOP` sentinel). FILE-KIND mutations take `{dir, relPath}` and mutate the
* file at `path.join(dir, relPath)` on disk; they return nothing.
*/
const fs = require('node:fs');
const path = require('node:path');
const { resolveWithin } = require('./paths.cjs');
/** Distinguishable placeholder heading/content used by unicode-headings. */
const UNICODE_HEADING_TEXT = '🚀 مرحبا Ünïcøde';
/** Cell content injected by escaped-pipes: an escaped pipe next to a real one. */
const ESCAPED_PIPE_CELL = 'a \\| b | c';
/**
* Sentinel returned by `apply('nonsequential-phases', text)` when `text`
* contains no recognizable phase-id occurrence to renumber. A mutation that
* silently no-ops on unrecognized input is a vacuous test: returning this
* frozen, identity-comparable sentinel — instead of quietly handing back the
* unchanged string — forces callers to explicitly decide to skip the step
* rather than accidentally asserting against an untouched artifact.
*
* @type {{ readonly noop: true, readonly reason: string }}
*/
const NOOP = Object.freeze({ noop: true, reason: 'no phase-id occurrence found to renumber' });
/**
* Frozen catalog of every mutation this module implements.
* @type {ReadonlyArray<{ id: string, kind: 'content'|'file', describe: string }>}
*/
const MUTATIONS = Object.freeze([
{
id: 'truncate-frontmatter',
kind: 'content',
describe: 'Cuts the text mid-YAML-frontmatter so the closing "---" is missing.',
},
{
id: 'crlf',
kind: 'content',
describe: 'Converts every line ending to CRLF.',
},
{
id: 'bom',
kind: 'content',
describe: 'Prefixes the text with a UTF-8 byte-order-mark character.',
},
{
id: 'empty',
kind: 'content',
describe: 'Replaces the text with an empty string.',
},
{
id: 'duplicate-phase-id',
kind: 'content',
describe: 'Duplicates the first phase-id heading or table row line.',
},
{
id: 'nonsequential-phases',
kind: 'content',
describe: 'Renumbers phase-id occurrences to a non-sequential sequence (1, 3, 7, ...).',
},
{
id: 'unicode-headings',
kind: 'content',
describe: 'Replaces every markdown heading\'s text with mixed unicode/emoji/RTL text.',
},
{
id: 'oversized',
kind: 'content',
describe: 'Pads the text to just over a caller-supplied byte target.',
},
{
id: 'escaped-pipes',
kind: 'content',
describe: 'Injects a table cell containing an escaped pipe next to a real one.',
},
{
id: 'delete',
kind: 'file',
describe: 'Removes the file on disk.',
},
{
id: 'symlink',
kind: 'file',
describe: 'Replaces the file with a symlink (or hardlink fallback) to a sibling copy.',
},
]);
/**
* Returns '\r\n' when `text` already contains at least one CRLF pair,
* otherwise '\n'. Used so line-oriented mutations rejoin with the
* predominant line ending already present rather than forcing one style.
*
* @param {string} text
* @returns {'\r\n'|'\n'}
*/
function detectEol(text) {
return text.includes('\r\n') ? '\r\n' : '\n';
}
/**
* Mutation: truncate-frontmatter.
*
* If `text` opens with a YAML frontmatter block (`---\n ... \n---`), the
* output is cut to a point strictly inside the frontmatter body, before the
* closing delimiter — so the closing `---` (and everything after it,
* including the real document body) is missing. If `text` has no
* frontmatter, a valid-looking opening block is synthesized and then itself
* truncated, so the mutation is never a no-op.
*
* @param {string} text
* @returns {string}
*/
function truncateFrontmatter(text) {
const openMatch = /^---\r?\n/.exec(text);
if (!openMatch) {
const synthetic = '---\r\ntitle: mutated\r\ndescription: truncated-frontmatter\r\n';
return synthetic.slice(0, Math.floor(synthetic.length / 2));
}
const openEnd = openMatch[0].length;
const closeMatch = /\r?\n---\r?\n?/.exec(text.slice(openEnd));
if (!closeMatch) {
// Already has no closer (or is degenerate) — truncate further into
// whatever body remains so the mutation still meaningfully shortens it.
return text.slice(0, Math.max(openEnd, Math.floor(text.length / 2)));
}
const closeStart = openEnd + closeMatch.index;
const cutPoint = Math.max(openEnd, Math.floor((openEnd + closeStart) / 2));
return text.slice(0, cutPoint);
}
/**
* Mutation: crlf.
*
* Normalizes any existing CRLF to LF first, then converts every LF to CRLF —
* so text that already contains CRLF pairs is never doubled into `\r\r\n`,
* and the mutation is idempotent: `crlf(crlf(text)) === crlf(text)`.
*
* @param {string} text
* @returns {string}
*/
function crlf(text) {
return text.replace(/\r\n/g, '\n').replace(/\n/g, '\r\n');
}
/**
* Mutation: bom.
*
* Prefixes `text` with U+FEFF (byte-order-mark).
*
* @param {string} text
* @returns {string}
*/
function bom(text) {
return `${text}`;
}
/**
* Mutation: duplicate-phase-id.
*
* Finds the first line matching a phase-id heading (`## Phase N`) or a
* phase-id table row (`| N | ... |`) and duplicates that whole line
* immediately after itself. If no such line exists, duplicates the first
* non-blank line instead, so the mutation still produces a structural
* duplicate. Returns `text` unchanged only when every line is blank.
*
* @param {string} text
* @returns {string}
*/
function duplicatePhaseId(text) {
const eol = detectEol(text);
const lines = text.split(/\r?\n/);
const headingRe = /^##\s+Phase\s+\d+\b/i;
const rowRe = /^\s*\|\s*\d+\s*\|/;
let idx = lines.findIndex((line) => headingRe.test(line) || rowRe.test(line));
if (idx === -1) {
idx = lines.findIndex((line) => line.trim() !== '');
}
if (idx === -1) return text;
const out = [...lines.slice(0, idx + 1), lines[idx], ...lines.slice(idx + 1)];
return out.join(eol);
}
/**
* Generates the k-th term (0-indexed) of the deterministic non-sequential
* skip series used by `nonsequential-phases`: 1, 3, 7, 15, 31, ... (2^(k+1) - 1).
* The first three terms are exactly the "1, 3, 7" example from the mutation
* catalog; the closed form extends deterministically to any number of
* phase-id occurrences without repeating a value.
*
* @param {number} k zero-based occurrence index
* @returns {number}
*/
function skipSeriesTerm(k) {
return Math.pow(2, k + 1) - 1;
}
/**
* Mutation: nonsequential-phases.
*
* Rewrites every phase-id occurrence — `## Phase N` headings and `| N | ... |`
* table-row ids — to the deterministic skip series 1, 3, 7, 15, ... in order
* of appearance. If `text` contains no phase-id occurrence at all, there is
* nothing to renumber: returns the exported `NOOP` sentinel instead of
* silently handing back the unchanged string, so a caller cannot mistake a
* no-op for a genuine mutation.
*
* @param {string} text
* @returns {string | typeof NOOP}
*/
function nonsequentialPhases(text) {
let occurrence = 0;
let matched = false;
const withHeadings = text.replace(/(##\s+Phase\s+)(\d+)/gi, (_match, prefix) => {
matched = true;
return `${prefix}${skipSeriesTerm(occurrence++)}`;
});
const withRows = withHeadings.replace(/^(\s*\|\s*)(\d+)(\s*\|)/gm, (_match, prefix, _num, suffix) => {
matched = true;
return `${prefix}${skipSeriesTerm(occurrence++)}${suffix}`;
});
return matched ? withRows : NOOP;
}
/**
* Mutation: unicode-headings.
*
* Replaces every markdown heading's text (the part after the `#` run and
* whitespace) with a fixed mixed unicode/emoji/RTL string, preserving the
* heading level and leading whitespace.
*
* @param {string} text
* @returns {string}
*/
function unicodeHeadings(text) {
return text.replace(/^(#{1,6})(\s+).*$/gm, (_match, hashes, ws) => `${hashes}${ws}${UNICODE_HEADING_TEXT}`);
}
/**
* Mutation: oversized.
*
* Pads `text` with a deterministic ASCII filler so the UTF-8 byte length of
* the result is strictly greater than `opts.targetBytes` (exactly one byte
* over when `text` is already at or under the target, since the filler is
* single-byte-per-character). Callers drive their own boundary tests
* (limit-1 / limit / limit+1) by varying `targetBytes` across calls; if
* `text` already exceeds `targetBytes`, it is returned unchanged since it is
* already oversized relative to that target.
*
* @param {string} text
* @param {{ targetBytes: number }} opts
* @returns {string}
*/
function oversized(text, opts) {
if (!opts || !Number.isFinite(opts.targetBytes) || opts.targetBytes < 0) {
throw new Error('apply("oversized", text, opts): opts.targetBytes must be a non-negative finite number');
}
const { targetBytes } = opts;
const currentBytes = Buffer.byteLength(text, 'utf8');
if (currentBytes > targetBytes) return text;
const FILL_CHAR = 'X';
const deficit = targetBytes - currentBytes + 1;
return text + FILL_CHAR.repeat(deficit);
}
/**
* Mutation: escaped-pipes.
*
* Finds the first markdown table row (a line whose trimmed form starts and
* ends with `|`) and appends a cell containing an escaped pipe next to a
* real one (`a \| b | c`) before the row's closing pipe. If no table row
* exists, appends a new one-row table containing that cell.
*
* @param {string} text
* @returns {string}
*/
function escapedPipes(text) {
const eol = detectEol(text);
const lines = text.split(/\r?\n/);
const rowRe = /^\s*\|.*\|\s*$/;
const idx = lines.findIndex((line) => rowRe.test(line));
if (idx === -1) {
const separator = text === '' ? '' : eol;
return `${text}${separator}| ${ESCAPED_PIPE_CELL} |`;
}
const trimmed = lines[idx].replace(/\s+$/, '');
const out = [...lines];
out[idx] = `${trimmed} ${ESCAPED_PIPE_CELL} |`;
return out.join(eol);
}
/**
* Resolves `{dir, relPath}` to an absolute on-disk path via `resolveWithin`
* (`./paths.cjs`) — the single containment guard for this harness. `relPath`
* is scenario-supplied (`step.mutate.target`), so this is the seam both
* `deleteFile` and `symlinkFile` inherit: a traversing `relPath` (e.g.
* `"../../../../etc/hosts"`) throws here rather than reaching
* `fs.unlinkSync` / `fs.symlinkSync` outside `dir`.
*
* @param {string} dir
* @param {string} relPath
* @returns {string}
*/
function resolveTargetPath(dir, relPath) {
return resolveWithin(dir, relPath);
}
/**
* Mutation: delete (file-kind).
*
* Removes the file at `path.join(dir, relPath)`.
*
* @param {{ dir: string, relPath: string }} target
* @returns {void}
*/
function deleteFile(target) {
const filePath = resolveTargetPath(target.dir, target.relPath);
fs.unlinkSync(filePath);
}
/**
* Mutation: symlink (file-kind).
*
* Replaces the file at `path.join(dir, relPath)` with a symlink pointing at
* a sibling file (in the same directory) holding the same bytes. On
* platforms where `fs.symlinkSync` fails with a permission error (e.g.
* Windows without developer mode / elevated privileges), falls back to a
* hard link (`fs.linkSync`).
*
* Never leaves the tree half-mutated: link capability is first proven
* against a disposable probe path in the same directory (which never
* touches the real target). Only once that probe succeeds is the real
* target removed and immediately replaced using the now-proven method — so
* a platform that supports neither symlinks nor hard links throws before
* the target file is ever deleted, and a target that IS deleted is
* guaranteed a same-shaped replacement (barring an out-of-process deletion
* racing this function, which is out of scope for a single-writer QA tool).
*
* @param {{ dir: string, relPath: string }} target
* @returns {void}
*/
function symlinkFile(target) {
const filePath = resolveTargetPath(target.dir, target.relPath);
const bytes = fs.readFileSync(filePath);
const siblingPath = path.join(path.dirname(filePath), `.mutation-sibling-${path.basename(filePath)}`);
fs.writeFileSync(siblingPath, bytes);
const probePath = `${filePath}.mutation-probe`;
try {
fs.unlinkSync(probePath);
} catch {
// No stale probe from a previous failed run — nothing to clean up.
}
let useHardlink = false;
try {
fs.symlinkSync(siblingPath, probePath);
} catch (symlinkErr) {
if (process.platform !== 'win32') throw symlinkErr;
try {
fs.linkSync(siblingPath, probePath);
useHardlink = true;
} catch (linkErr) {
throw new Error(
'symlink mutation unavailable on this platform: '
+ `symlink failed (${symlinkErr.message}) and hardlink fallback failed (${linkErr.message})`,
);
}
}
fs.unlinkSync(probePath);
// Link capability proven above — safe to swap the real target now.
fs.unlinkSync(filePath);
if (useHardlink) {
fs.linkSync(siblingPath, filePath);
} else {
fs.symlinkSync(siblingPath, filePath);
}
}
/**
* Applies a single mutation by id.
*
* - content-kind mutations: `apply(id, input)` where `input` is a string,
* returning the mutated string (or, only for `nonsequential-phases`, the
* `NOOP` sentinel — see that mutation's doc comment).
* - `oversized` additionally takes a third argument: `apply('oversized',
* text, {targetBytes})`.
* - file-kind mutations: `apply(id, {dir, relPath})`, mutating the file on
* disk and returning nothing.
*
* @param {string} id one of MUTATIONS[].id
* @param {string | { dir: string, relPath: string }} input
* @param {{ targetBytes?: number }} [opts]
* @returns {string | void | typeof NOOP}
*/
function apply(id, input, opts) {
const entry = MUTATIONS.find((m) => m.id === id);
if (!entry) {
throw new Error(`apply: unknown mutation id "${String(id)}" (known ids: ${MUTATIONS.map((m) => m.id).join(', ')})`);
}
switch (id) {
case 'truncate-frontmatter':
return truncateFrontmatter(input);
case 'crlf':
return crlf(input);
case 'bom':
return bom(input);
case 'empty':
return '';
case 'duplicate-phase-id':
return duplicatePhaseId(input);
case 'nonsequential-phases':
return nonsequentialPhases(input);
case 'unicode-headings':
return unicodeHeadings(input);
case 'oversized':
return oversized(input, opts);
case 'escaped-pipes':
return escapedPipes(input);
case 'delete':
return deleteFile(input);
case 'symlink':
return symlinkFile(input);
/* istanbul ignore next -- unreachable: entry lookup above already validated id */
default:
throw new Error(`apply: unhandled mutation id "${id}"`);
}
}
module.exports = {
MUTATIONS,
apply,
NOOP,
};