Files
msd-core/tests/frontmatter.unit.test.cjs
Tom Boucher 382bf7c423 fix(#3706): deliver the resolved reasoning effort to OpenCode subagents (#3867)
* test(#3706): failing-first coverage for OpenCode variant emission and frontmatter escaping

* fix(#3706): emit the resolved reasoning effort as OpenCode's variant key

`query resolve-execution` resolved an effort level for every agent, but the
OpenCode bake wrote only `model:` — the effort never reached the generated
agent, so subagents ran at whatever the runtime defaulted the model to. This
is the effort-side twin of the model-side defect fixed in #3705.

The key is written only when an `effort` block is actually configured.
`resolveInstallTimeEffort` always returns a level (the catalog default is
`high`), so gating on its return value would stamp `variant: high` into every
existing OpenCode install — and OpenCode resolves a variant name against a
`variants` map in the user's `opencode.jsonc`, so a value nobody declared is
not a safe default. Gating on `readGsdEffectiveEffortConfig` keeps installs
that never asked for effort routing byte-identical.

Kilo does not receive the key: `EFFORT_ARGV` declares surfaces for claude,
opencode and codex and has no kilo entry. This is deliberately asymmetric with
the model side, where #2794 J8 requires the two runtimes to resolve alike.

Both frontmatter sinks now route through `frontmatterScalar`, which quotes and
escapes any value that is not a plain scalar. The raw interpolation predates
this change, but it was already shown by execution during the #3705 security
review to let a config value containing a newline inject additional top-level
keys (`tools:`, `permission:`) into a generated agent file. This change adds a
second write to that sink, so it is closed here rather than doubled.

* fix(#3706): quote frontmatter values YAML would not read back verbatim

Self-review of the predicate added in the previous commit. Treating
/^[A-Za-z0-9._:/@+-]+$/ as 'safe to emit bare' answers the wrong question:
a value can match it and still not round-trip.

  - A leading '@' is a YAML *reserved* indicator and may not open a plain
    scalar at all, so a scoped ID like '@org/model' emitted bare is a parse
    error, not an ambiguity — the whole agent file becomes unreadable.
  - 'no' / 'y' / 'off' / 'null' resolve to booleans and null, so a variant
    with one of those names would match no entry in the user's variants map.
  - '12:30' resolves to 750 under YAML 1.1 sexagesimal, and ':' is legal
    mid-identifier here, so the form is reachable rather than contrived.

Real model IDs pass every clause and stay bare, so already-generated files
remain byte-identical.

* fix(#3706): route variant through the declared effort seam and cover the live path

Addresses six findings from the isolated review, all confirmed by execution.

The tests were the serious one: they required `../bin/install.js` while the fix
landed in src/, which compiles to gsd-core/bin/lib/. They exercised a different
copy of the converter than the one the bake actually uses, so the whole suite
was green-by-construction against unchanged code and the remote run failed all
13. Every case now runs against BOTH copies from one table, which doubles as the
parity assertion the generative-fix note in runtime-artifact-conversion.cts asks
for, and bin/install.js carries the mirrored change.

Emission no longer hand-rolls the value. It goes through `renderEffortArgv`,
the declared OpenCode effort seam (EFFORT_ARGV.opencode: its own supported set
and clamp). That is what rejects a level that is not a wire value — above all
`inherit`, which per #3533 (10d) means "omit the key and follow the host
default" and was previously written literally, naming a variant that cannot
resolve. Reachable two ways, both now pinned: an agent_overrides entry and a
routing_tier_defaults entry. A bare effort.default does NOT reach a tiered
agent (the #3531 tier ladder answers first), so a test written against
`default` alone asserts nothing — that is pinned too.

The plain-scalar decision moved into frontmatter.cts beside
`scalarNeedsDoubleQuoting` rather than sitting next to it as a second, weaker
predicate. `agentScalarNeedsDoubleQuoting` is a documented superset: it adds a
trailing `:` (read as a nested mapping key, which fails the whole frontmatter),
boolean/null words, and numeric-looking values including YAML 1.1 sexagesimal.

Docs now state the cascade plainly: the gate is on effort being configured at
all, not on the individual agent being named, so every generated OpenCode agent
gets a variant line once any effort block exists.

* test(#3706): assert the two frontmatterScalar copies cannot diverge

A hand-picked adversarial corpus plus a fast-check property over
YAML-significant strings, both run against bin/install.js and the live
src copy. Verified the property can actually fail: mutating one copy's
quoting rule is killed well inside the run budget.

* fix(#3706): close the review findings — predicate, seam, and dead mirror

Third review round; every item below was confirmed by execution.

The scalar predicate was wrong in two families, both found by a round-trip
property test rather than by reading. Basing it on scalarNeedsDoubleQuoting
dropped the "first character must be alphanumeric" clause, so `~`, `.inf`,
`.nan`, `+1`, `-0` and `.5` went out bare and came back as null/floats/ints;
and that base predicate only inspects the FIRST character, so an embedded `: `
(a nested mapping, i.e. a parse error) or ` #` (a comment, i.e. silent
truncation) also passed. Dates round out the set: `2026-08-25` opens
alphanumeric, survives every other clause, and YAML resolves it to a Date.
The property now asserts the contract directly over generated values instead
of trusting an enumerated character list.

The bin/install.js mirror is gone. Its premise was false — install.js already
requires bin/lib at :65 — and it was unreachable besides: install.js's
convertClaudeToOpencodeFrontmatter has no `isAgent: true` call site, because
its agents path resolves converters from the compiled module. It was a third
copy of the YAML rules serving a test rather than a caller, so the file is
back to origin/next and the tests target the live copy only.

Effort clamping moved to `clampEffortForHost`, which renderEffortArgv now
delegates to. The layout was calling renderEffortArgv with a hardcoded 'argv'
to borrow its clamp, which read as if the frontmatter key were gated on the
invocation-time axis. It is not: claude declares effortSurface "argv" and
independently bakes an effort: key. One capability table, one clamp, two
channels that no longer pretend to be each other.

Also corrects an earlier claim of mine: adding EFFORT_RENDERING.opencode would
NOT have made `effort sync` write the wrong key, because it guards on the
runtime name before it ever renders. The seam choice stands on other grounds.
`effort sync` still skips OpenCode, but its stated reason claimed OpenCode
"does not use effort: frontmatter", which this change makes false — so the
message now says what is actually true.

* docs(#3706): restate the changeset around the round-trip contract

* fix(#3706): restore the changeset fragment belonging to #3809

An earlier commit in this branch picked the first file in .changeset/ by
glob order instead of the fragment created for this issue, and overwrote
agile-geese-squeak.md (PR 3815 / #3809) with this change's body. Restored
verbatim from origin/next; this change's text now lives in its own
patient-cranes-parade.md, where it was created.

* feat(#3706): maintain the OpenCode variant key from effort sync

Install bakes the resolved effort into OpenCode agent frontmatter as
`variant:`, so `effort sync` has to maintain it or a config change only takes
effect on reinstall — and its skip message claimed OpenCode does not use
frontmatter effort at all, which this issue made false.

cmdEffortSyncOpencode mirrors the codex branch: resolve per agent, clamp
through the declared OpenCode capability, then write, strip, or skip. A null
target means the key must not exist, which covers both "no effort configured"
and "resolved to inherit or to an unsupported level" — the same states under
which install writes nothing, so sync and install agree by construction.

The frontmatter line-editors are key-parameterised rather than copied:
setEffortFrontmatter / removeEffortFrontmatter are now thin wrappers over the
same internals the variant path uses, and a test pins that the claude `effort:`
behavior did not move. The child-process test harness fixes both HOME and
USERPROFILE, so the hermetic-config assertions cannot pass vacuously on Windows.

* fix(#3706): scope the frontmatter line editors to the matched block

Found by the security review of the sync path, reported as correctness rather
than vulnerability, and reproduced against pre-fix code before being fixed.

Both editors matched the frontmatter with a regex that can match a block after
a preamble, then derived the EOL and the opening-fence length from the START OF
THE FILE. On a CRLF document with a preamble those disagree, the offsets shift
by one byte, and the reassembled document comes back with a mangled fence
(`---\rname: x`). Both now take the EOL from the matched block.

`setFrontmatterKeyLine` additionally did a whole-file `/m` replace when the key
already existed, gated only on the key being present in the frontmatter body —
so a preamble line starting with the same key was rewritten instead of the
frontmatter one. It now replaces inside the frontmatter span only, which is the
hazard `removeFrontmatterKeyLine` already documented and guarded against.

Neither is reachable from an install-written `gsd-*.md` (those begin at byte 0
with `---`), and both predate this change — but the editors are in this diff
because #3706 key-parameterised them, so they are fixed here rather than left
for the next caller to trip over. Three regression tests, each confirmed to
fail against the pre-fix build.

* fix(#3706): treat a present-but-empty key as present, and pin the real seam

Fourth review round.

The MAJOR one: both sync branches read the current value with `(.+?)`, which
needs at least one character, so a key present with an EMPTY value read as
"key absent". When the target was also null the code concluded "already
correct" and skipped — leaving the key in the file, where it reads back as
YAML `null`: exactly the unresolvable-variant state this change exists to
prevent. Whitespace decided whether it fired, since `variant:   ` matched and
`variant:` did not. Presence and value are now separate questions at both the
opencode and the claude branch.

The OpenCode writer now follows the codex branch rather than the claude one:
tmp file plus retryRenameSync with orphan cleanup, and a write failure skips
that agent and is reported instead of aborting the sweep. Same granularity,
same transient-Windows-lock exposure, so the hardened sibling was the right
precedent.

Also: the generic line-editors escape their interpolated key, the JSDoc
stranded by the clampEffortForHost extraction is back on renderEffortArgv, and
a cast that declared a nullable function as non-nullable is corrected.

Tests close the gaps the review listed — empty value (both spellings), CRLF
round-trip through write and strip, the symlink guard, a body line starting
`variant:`, a file with no frontmatter, and the YAML classes that actually
broke the predicate. The new layout-seam test drives the real stage() path and
was verified to FAIL when `variant` is removed from the converter call; a seam
test that survives cutting the seam is worse than none.

* fix(#3706): clear the round-five review findings

No blockers or majors this round; the repo's review gate is zero-tolerance, so
the minors are cleared too.

A duplicated key was only half-stripped: the strip regex had no `g` flag, so a
frontmatter carrying the key twice lost one occurrence, reported success, and
left the "a null target means the key must not exist" invariant false on disk —
converging only on a second run. Such a document is already invalid YAML, so
this is robustness rather than a live corruption path, but a successful sync
has to leave the invariant true.

A run in which every write failed still summarised as `ok`, so a caller could
not tell "nothing to do" from "everything failed". The OpenCode branch now
reports `failed` when any write failed. The write-failure path was also the
newest code in the change with no coverage at all; it now has a test that
injects the failure by monkeypatching the write, per CLAUDE.md §4, rather than
by chmod — mode bits do not bite under root in CI.

`CodexEffortSyncWriteFailure` is renamed `EffortSyncWriteFailure` now that two
branches share it. Removed a guard on the claude concrete path that was
provably unreachable — no member of EFFORT_SET renders null there, so it read
as protection that did not exist. The claude inherit path's presence check is
load-bearing and untouched.

Three stale statements corrected: the OpenCode result shape matches codex's,
not claude's, now that it emits write_failures; the `thread()` test helper now
calls `clampEffortForHost` so it genuinely mirrors the layout instead of
merely claiming to; and a test helper restored `USERPROFILE` by assignment,
writing the literal string "undefined" into the environment on POSIX — it
deletes now.

* fix(#3706): converge the set path, degrade on unreadable files, preserve mode

Rounds five and six of review. No blockers or majors; the review gate is
zero-tolerance, so the minors are cleared too.

`setFrontmatterKeyLine` was the mirror of a defect already fixed in its
sibling: `remove` was made global, `set` was not, so on a frontmatter carrying
the key twice it rewrote the first and left a stale second. Last-wins YAML
readers honour the stale value while the sync's own first-occurrence read
reports "in sync" — permanently non-converging. It now collapses to exactly one
occurrence, in the position of the first, so ordinary single-occurrence
documents stay byte-identical (verified across seven shapes before and after).

An unreadable agent file used to throw and abort the entire sweep, while a
failed WRITE in the same loop degraded into a report. The OpenCode branch now
reports read failures alongside write failures; the claude branch degrades to a
skip without a new result field, because its shape is long-standing and widely
consumed and one bad file aborting the sweep is the actual defect.

The tmp+rename publish dropped the original file's mode — a plain writeFileSync
preserves it, a rename does not — so a 0600 agent came back 0644. Both the
OpenCode and the codex branch now carry the original's permission bits across
the publish, masked with 0o7777: the raw stat mode includes the file-type bits,
and POSIX leaves those unspecified for chmod. Linux is the only OS the remote
matrix runs, so relying on Darwin's tolerance would have been untestable here.

Also documents the `from` contract on EffortSyncChange (null means the key was
absent, '' means present with an empty value — a distinction earlier rounds
introduced and then collapsed in the output), adds OpenCode to the docs
paragraph enumerating where the key is omitted under inherit, and records in a
comment that the 'failed' summary reaches only raw mode and does not change the
exit code, which is a CLI-contract change affecting all three branches and is
deliberately not made here.

* fix(#3706): guard the codex read, close the tmp permission window, rename the failure type

Round seven, plus one thing I found myself.

`cmdEffortSyncCodex` still had an unguarded `fs.readFileSync` — a read fault on
one agent exited 1 and aborted the whole sweep. The claude and opencode
branches were both guarded earlier this round and codex was missed, with the
unguarded read sitting ten lines above the chmod block the previous commit did
edit. It now reports read failures the way the OpenCode branch does, and a read
failure flips its summary to `failed` — which write failures did not do there
either, so both are corrected for consistency.

The tmp file was created at the default mode and only tightened afterwards, so
a 0600 agent's contents sat in a 0644 file for the length of the publish. I
measured the window rather than assuming it, then closed it by passing the
mode at creation. The chmod after the write is deliberately RETAINED and
commented: the `mode` option only applies when the file is actually created, so
a leftover tmp from an earlier crashed run would be truncated and reused at its
old mode, and the chmod is what corrects that.

`EffortSyncWriteFailure` is renamed `EffortSyncFileFailure` — it was typing a
`read_failures` array, the same naming-lie the `Codex…` prefix had last round.

Also pins the codex mode preservation with a test. It only writes on a path
that genuinely rewrites the file, so the fixture is an Anthropic-flavoured
model pin the sync strips, and the test asserts the content changed before
checking the mode — otherwise it would pass on a sync that did nothing.

* fix(#3706): guard the claude writes and share one escaping rule

The security sign-off caught a comment of mine that was factually wrong: the
new claude read guard said the failure is folded in "like the write path in
this same loop does", and there was no write guard in that loop. Rather than
correct the sentence, both claude write sites are now guarded the way the read
is — a failed file is skipped, the sweep continues, and the raw summary token
flips to `failed`. The JSON shape stays frozen deliberately, because it is
long-standing and widely consumed; the token is the channel that can carry the
signal without a compatibility risk, which is the reviewer's own suggestion.

That makes all three branches consistent: reads and writes guarded everywhere,
per-file failures degrade instead of aborting, and every branch reports
`failed` rather than `ok` when something did not sync.

`setFrontmatterKeyLine` interpolated its value raw while the install-side
writer quoted through the shared helpers — two writers of the same frontmatter
key disagreeing on escaping, the divergence class this repo requires closed.
They now share one rule. Verified no churn: all six effort levels are plain
scalars and emit byte-identically, with claude's documented minimal-to-low
clamp the only difference in the table, exactly as before.

* fix(#3706): publish claude agent writes atomically too

Both reviewers found this independently, and it is data loss rather than a
reporting gap. The claude branch wrote in place, so `fs.writeFileSync`'s
O_TRUNC meant a post-open fault left the agent file truncated or half-written:
an injected ENOSPC produced an empty file, and under `ulimit -f` a 60000-byte
agent came back as 512 bytes of wrong content. The guard added earlier this
round then counted that destroyed file as `skipped`, which in JSON mode is
indistinguishable from "already in sync" — so a caller would have read the
sweep as clean while an agent on disk was corrupt.

It now publishes the way the codex and opencode branches already do: write to
a tmp file created at the original's masked mode, chmod, then retryRenameSync,
with the tmp unlinked and the agent skipped on any failure. The corrupting case
is gone rather than merely reported, which matters because this branch
deliberately takes no new result key.

I had claimed all three branches were consistent after the previous commit.
That was true for degradation and reporting and not for atomicity; the reviewer
caught the overclaim. It is true now.

Also sorts the claude file list, which the other two branches already did —
readdir order is platform-dependent, so leaving it unsorted made the reported
`changes` ordering differ across machines for identical inputs.

* chore(#3706): backfill the changeset PR number

pr:0 placeholder replaced with the real PR now that gh api returned it.

* test(#3706): kill the frontmatter mutants this change introduced

CI's Stryker frontmatter shard scored 60.58 against a break floor of 62.
The cause is documented in the lane's own config, from #1882: this PR added a
multi-clause predicate to frontmatter.cts and exported the escaper, but the
tests constraining them live in tests/runtime-converters.test.cjs, which that
shard does not run — so every mutant in the new code was uncovered there even
though the behaviour is tested elsewhere.

The fix is assertions that kill real mutants, per the repo's own instruction,
not a lowered floor and not a Stryker disable: scripts/mutation-matrix.cjs is
untouched. Each clause of agentScalarNeedsDoubleQuoting now has a true case AND
a near-miss that must answer the opposite way, so flipping the clause fails a
specific named test — alnum-first against `a-b`, trailing `:` against `foo:bar`,
embedded `: ` against `a:b`, embedded ` #` against `a#b`, the word list against
`yes1`/`nullish`, the numeric forms against `1a`/`0xzz`, the timestamp against
`2026-08-25x`, plus the case-insensitive spellings that pin the `i` flag.
escapeDoubleQuoted is pinned on exact output, including a case constructed so
that escaping in the wrong ORDER yields a different string.

Two of my expectations were wrong and are asserted as the code actually
behaves: `12:99` is NOT quoted, because the sexagesimal alternative never
range-checks minutes and so does not match — which is right, since YAML would
not read it as sexagesimal either; and `20260825` is quoted by the numeric
clause rather than the timestamp one, being a bare integer.

* chore(#3706): ratchet the frontmatter mutation floor to 65

The lane measured 66.67 on PR 3867 after the mutant-killing unit tests landed —
above its pre-change 63.35 baseline, not merely recovered. Step 3 of this
file's own HOW TO UPDATE procedure says to set minScore = floor(measured) - 1
in the same diff, so 62 becomes 65 and the improvement is locked in rather than
left free to slide back.

The ledger of measured scores now records the new measurement, why the shard
broke in the first place (logic added to frontmatter.cts whose only tests lived
in a file this lane does not run — the same trap the #1882 note describes), and
one discrepancy: step 3 also says to update "the matching RATCHET_BASELINE
entry", but no such declaration exists in this file. The name appears only in
that comment, so minScore and the ledger are all there is to update.

* fix(#3706): update RATCHET_BASELINE alongside the raised floor

The ratchet test caught the previous commit: it raised COVERED['frontmatter']
.minScore to 65 without updating the baseline that mirrors it, which is exactly
the mismatch that guard exists to make visible in review.

I had claimed RATCHET_BASELINE did not exist. It does — in
tests/mutation-matrix-ratchet.test.cjs, not in scripts/mutation-matrix.cjs,
which is the only file I searched before concluding it was a stale reference.
The ledger comment is corrected to say where it lives and to record that the
guard caught the error rather than leaving my wrong claim on the record.

* docs(#3706): put the mutation ledger entries back under their own dates

The 2026-08-25 measurement was spliced into the middle of the 2026-06-14 list,
so adr-parser, config-schema, active-workstream-store and core-utils ended up
sitting under the wrong heading and misattributing their measurement dates.
That ledger is what a future change reads to calibrate a floor, so a wrong date
there is not cosmetic. Each measurement is now under the date it was taken.

Also drops the first-person account of my own mistake from the entry — the
factual half (where RATCHET_BASELINE lives, and that it is updated in the same
diff) is what a reader needs; the confession is not.

---------

Co-authored-by: sim <sim@local>
2026-08-25 19:54:30 -04:00

1750 lines
68 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
/**
* Unit tests for frontmatter.cjs
*
* Module: gsd-core/bin/lib/frontmatter.cjs
*
* Covers:
* - extractFrontmatter: all scalar types, quoted, arrays, nested, edge cases
* - reconstructFrontmatter: exact output for every branch
* - spliceFrontmatter: with/without existing frontmatter
* - parseMustHavesBlock: all branches
* - FRONTMATTER_SCHEMAS: exact keys
*/
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const yaml = require('js-yaml');
const {
extractFrontmatter,
reconstructFrontmatter,
spliceFrontmatter,
stripFrontmatter,
noOpObjectListSetError,
parseMustHavesBlock,
FRONTMATTER_SCHEMAS,
agentScalarNeedsDoubleQuoting,
escapeDoubleQuoted,
} = require('../gsd-core/bin/lib/frontmatter.cjs');
// ─── extractFrontmatter ───────────────────────────────────────────────────────
describe('extractFrontmatter: no frontmatter', () => {
test('plain text returns {}', () => {
assert.deepEqual(extractFrontmatter('just plain text'), {});
});
test('empty string returns {}', () => {
assert.deepEqual(extractFrontmatter(''), {});
});
test('--- not at start returns {}', () => {
assert.deepEqual(extractFrontmatter('content\n---\nkey: val\n---\n'), {});
});
test('--- block without closing delimiter returns {}', () => {
assert.deepEqual(extractFrontmatter('---\ntitle: Hello\nauthor: World\n'), {});
});
test('only --- returns {}', () => {
assert.deepEqual(extractFrontmatter('---\n---'), {});
});
test('empty frontmatter block returns {}', () => {
assert.deepEqual(extractFrontmatter('---\n\n---\nBody'), {});
});
test('heading only returns {}', () => {
assert.deepEqual(extractFrontmatter('# Just a heading\ncontent'), {});
});
});
describe('extractFrontmatter: simple scalar values', () => {
test('single string key-value', () => {
const result = extractFrontmatter('---\ntitle: Hello\n---\nBody');
assert.deepEqual(result, { title: 'Hello' });
});
test('multiple string key-values', () => {
const result = extractFrontmatter('---\ntitle: Hello\nauthor: World\n---\n');
assert.deepEqual(result, { title: 'Hello', author: 'World' });
});
test('numeric string value preserved as string', () => {
const result = extractFrontmatter('---\ncount: 42\n---');
assert.deepEqual(result, { count: '42' });
assert.equal(result.count, '42');
});
test('boolean string value preserved as string', () => {
const result = extractFrontmatter('---\nflag: true\n---');
assert.deepEqual(result, { flag: 'true' });
assert.equal(result.flag, 'true');
});
test('null string value preserved as string', () => {
const result = extractFrontmatter('---\nnone: null\n---');
assert.deepEqual(result, { none: 'null' });
assert.equal(result.none, 'null');
});
test('false string value preserved as string', () => {
const result = extractFrontmatter('---\ndone: false\n---');
assert.deepEqual(result, { done: 'false' });
});
test('value with internal spaces preserved', () => {
const result = extractFrontmatter('---\nphase: phase one\n---');
assert.deepEqual(result, { phase: 'phase one' });
});
test('trailing whitespace in value is trimmed', () => {
const result = extractFrontmatter('---\ntitle: Hello \n---');
assert.deepEqual(result, { title: 'Hello' });
});
test('key with underscore', () => {
const result = extractFrontmatter('---\nmy_key: val\n---');
assert.deepEqual(result, { my_key: 'val' });
});
test('key with hyphen', () => {
const result = extractFrontmatter('---\nmy-key: val\n---');
assert.deepEqual(result, { 'my-key': 'val' });
});
test('key with digits', () => {
const result = extractFrontmatter('---\nkey123: val\n---');
assert.deepEqual(result, { key123: 'val' });
});
test('empty line in frontmatter is skipped', () => {
const result = extractFrontmatter('---\nkey1: val1\n\nkey2: val2\n---');
assert.deepEqual(result, { key1: 'val1', key2: 'val2' });
});
test('body content after closing delimiter is ignored', () => {
const result = extractFrontmatter('---\ntitle: Hello\n---\n# Heading\nContent here');
assert.deepEqual(result, { title: 'Hello' });
assert.equal(Object.keys(result).length, 1);
});
});
describe('extractFrontmatter: quoted values', () => {
test('double-quoted value strips quotes', () => {
const result = extractFrontmatter('---\ntitle: "Hello World"\n---');
assert.deepEqual(result, { title: 'Hello World' });
});
test('single-quoted value strips quotes', () => {
const result = extractFrontmatter("---\ntitle: 'Hello World'\n---");
assert.deepEqual(result, { title: 'Hello World' });
});
test('double-quoted value containing colon', () => {
const result = extractFrontmatter('---\nurl: "http://example.com"\n---');
assert.deepEqual(result, { url: 'http://example.com' });
});
test('unquoted value with no special chars', () => {
const result = extractFrontmatter('---\nname: simple\n---');
assert.deepEqual(result, { name: 'simple' });
assert.equal(result.name, 'simple');
});
});
describe('extractFrontmatter: CRLF line endings', () => {
test('CRLF frontmatter parses correctly', () => {
const result = extractFrontmatter('---\r\ntitle: Hello\r\nauthor: World\r\n---\r\nBody');
assert.deepEqual(result, { title: 'Hello', author: 'World' });
});
test('CRLF with array values', () => {
const result = extractFrontmatter('---\r\ntags: [a, b, c]\r\n---\r\n');
assert.deepEqual(result, { tags: ['a', 'b', 'c'] });
});
});
describe('extractFrontmatter: inline arrays', () => {
test('empty inline array []', () => {
const result = extractFrontmatter('---\ntags: []\n---');
assert.deepEqual(result, { tags: [] });
assert.ok(Array.isArray(result.tags));
assert.equal(result.tags.length, 0);
});
test('single item inline array', () => {
const result = extractFrontmatter('---\ntags: [only]\n---');
assert.deepEqual(result, { tags: ['only'] });
assert.equal(result.tags.length, 1);
});
test('two item inline array', () => {
const result = extractFrontmatter('---\ntags: [a, b]\n---');
assert.deepEqual(result, { tags: ['a', 'b'] });
});
test('three item inline array', () => {
const result = extractFrontmatter('---\ntags: [a, b, c]\n---');
assert.deepEqual(result, { tags: ['a', 'b', 'c'] });
});
test('inline array with spaces around items', () => {
const result = extractFrontmatter('---\ntags: [ a , b , c ]\n---');
assert.deepEqual(result, { tags: ['a', 'b', 'c'] });
});
test('inline array with double-quoted item containing comma', () => {
const result = extractFrontmatter('---\ntags: ["a, b", c]\n---');
assert.deepEqual(result, { tags: ['a, b', 'c'] });
});
test('inline array with single-quoted item containing comma', () => {
const result = extractFrontmatter("---\ntags: ['a, b', c]\n---");
assert.deepEqual(result, { tags: ['a, b', 'c'] });
});
test('inline array with quoted item plus more items', () => {
const result = extractFrontmatter('---\ntags: ["a, b", c, d]\n---');
assert.deepEqual(result, { tags: ['a, b', 'c', 'd'] });
});
test('consecutive commas (empty items filtered)', () => {
const result = extractFrontmatter('---\ntags: [a,,b]\n---');
assert.deepEqual(result, { tags: ['a', 'b'] });
});
test('whitespace-only items filtered', () => {
const result = extractFrontmatter('---\ntags: [ , ]\n---');
assert.deepEqual(result, { tags: [] });
});
test('opening bracket only becomes empty array/object', () => {
const result = extractFrontmatter('---\ntags: [\n---');
assert.deepEqual(result, { tags: [] });
assert.ok(Array.isArray(result.tags));
});
});
describe('extractFrontmatter: dashed list arrays', () => {
test('two-item dashed list', () => {
const result = extractFrontmatter('---\ntags:\n - a\n - b\n---');
assert.deepEqual(result, { tags: ['a', 'b'] });
assert.ok(Array.isArray(result.tags));
});
test('single-item dashed list', () => {
const result = extractFrontmatter('---\ntags:\n - solo\n---');
assert.deepEqual(result, { tags: ['solo'] });
});
test('dashed list with double-quoted item', () => {
const result = extractFrontmatter('---\ntags:\n - "quoted value"\n---');
assert.deepEqual(result, { tags: ['quoted value'] });
});
test('dashed list with single-quoted item', () => {
const result = extractFrontmatter("---\ntags:\n - 'single quoted'\n---");
assert.deepEqual(result, { tags: ['single quoted'] });
});
test('opening bracket followed by dashed list', () => {
const result = extractFrontmatter('---\ntags: [\n - a\n - b\n---');
assert.deepEqual(result, { tags: ['a', 'b'] });
});
});
describe('extractFrontmatter: empty / missing values', () => {
test('empty value becomes empty object {}', () => {
const result = extractFrontmatter('---\ntitle:\n---');
assert.deepEqual(result, { title: {} });
assert.equal(typeof result.title, 'object');
assert.ok(!Array.isArray(result.title));
});
test('empty value followed by next key', () => {
const result = extractFrontmatter('---\ntitle:\nother: val\n---');
assert.equal(typeof result.title, 'object');
assert.equal(result.other, 'val');
});
});
describe('extractFrontmatter: nested objects', () => {
test('one level of nesting', () => {
const result = extractFrontmatter('---\nmeta:\n key: val\n count: 10\n---');
assert.deepEqual(result, { meta: { key: 'val', count: '10' } });
});
test('nested then back to top level', () => {
const result = extractFrontmatter('---\nmeta:\n sub: val\ntop: parent\n---');
assert.deepEqual(result, { meta: { sub: 'val' }, top: 'parent' });
});
test('multiple nested objects', () => {
const result = extractFrontmatter('---\na:\n k1: v1\nb:\n k2: v2\n---');
assert.deepEqual(result, { a: { k1: 'v1' }, b: { k2: 'v2' } });
});
test('two levels of nesting', () => {
const result = extractFrontmatter('---\ntop:\n mid:\n deep: value\n---');
assert.deepEqual(result, { top: { mid: { deep: 'value' } } });
});
test('nested numeric-string value', () => {
const result = extractFrontmatter('---\nmeta:\n count: 42\n---');
assert.deepEqual(result, { meta: { count: '42' } });
});
});
describe('extractFrontmatter: return type invariants', () => {
test('always returns plain object', () => {
const result = extractFrontmatter('random content');
assert.equal(typeof result, 'object');
assert.ok(result !== null);
assert.ok(!Array.isArray(result));
});
test('return value is not null', () => {
const result = extractFrontmatter('');
assert.ok(result !== null);
});
test('top-level dash item (no parent key) is ignored', () => {
const result = extractFrontmatter('---\n- item\n---');
assert.deepEqual(result, {});
});
test('key starting with digit still matches key pattern', () => {
const result = extractFrontmatter('---\n123key: val\n---');
assert.equal(result['123key'], 'val');
});
});
// ─── reconstructFrontmatter ───────────────────────────────────────────────────
describe('reconstructFrontmatter: empty input', () => {
test('empty object returns empty string', () => {
assert.equal(reconstructFrontmatter({}), '');
});
});
describe('reconstructFrontmatter: scalar values', () => {
test('simple string value', () => {
assert.equal(reconstructFrontmatter({ title: 'Hello' }), 'title: Hello');
});
test('numeric string value', () => {
assert.equal(reconstructFrontmatter({ count: '42' }), 'count: 42');
});
test('boolean string value', () => {
assert.equal(reconstructFrontmatter({ flag: 'true' }), 'flag: true');
});
test('null value is skipped', () => {
assert.equal(reconstructFrontmatter({ title: null }), '');
});
test('undefined value is skipped', () => {
assert.equal(reconstructFrontmatter({ title: undefined }), '');
});
test('value containing colon is double-quoted', () => {
assert.equal(reconstructFrontmatter({ url: 'http://example.com' }), 'url: "http://example.com"');
});
test('value containing hash is double-quoted', () => {
assert.equal(reconstructFrontmatter({ name: 'test#1' }), 'name: "test#1"');
});
test('value starting with [ is double-quoted', () => {
assert.equal(reconstructFrontmatter({ val: '[thing]' }), 'val: "[thing]"');
});
test('value starting with { is double-quoted', () => {
assert.equal(reconstructFrontmatter({ val: '{thing}' }), 'val: "{thing}"');
});
test('plain value without special chars is unquoted', () => {
assert.equal(reconstructFrontmatter({ name: 'simple' }), 'name: simple');
});
test('multiple keys produce newline-joined output', () => {
assert.equal(
reconstructFrontmatter({ title: 'Hello', author: 'World' }),
'title: Hello\nauthor: World'
);
});
});
describe('reconstructFrontmatter: arrays', () => {
test('empty array produces key: []', () => {
assert.equal(reconstructFrontmatter({ tags: [] }), 'tags: []');
});
test('two-item short array uses inline format', () => {
assert.equal(reconstructFrontmatter({ tags: ['a', 'b'] }), 'tags: [a, b]');
});
test('three-item short array uses inline format', () => {
assert.equal(reconstructFrontmatter({ tags: ['a', 'b', 'c'] }), 'tags: [a, b, c]');
});
test('three items whose join is exactly < 60 chars uses inline format', () => {
const tags = ['aaa', 'bbb', 'ccc'];
// 'aaa, bbb, ccc' = 13 chars
assert.equal(reconstructFrontmatter({ tags }), 'tags: [aaa, bbb, ccc]');
});
test('three items whose join >= 60 chars uses block format', () => {
const tags = ['aaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbb', 'cccccccccccccccccccc'];
// join is 61+ chars
assert.equal(
reconstructFrontmatter({ tags }),
'tags:\n - aaaaaaaaaaaaaaaaaaa\n - bbbbbbbbbbbbbbbbbbb\n - cccccccccccccccccccc'
);
});
test('four-item array uses block format', () => {
assert.equal(
reconstructFrontmatter({ tags: ['a', 'b', 'c', 'd'] }),
'tags:\n - a\n - b\n - c\n - d'
);
});
test('array item with colon is double-quoted in block format', () => {
// Need >3 items to force block format where quoting applies
const result = reconstructFrontmatter({ tags: ['a:b', 'c', 'd', 'e'] });
assert.ok(result.includes(' - "a:b"'), `Expected quoted item, got: ${result}`);
});
test('array item with hash is double-quoted in block format', () => {
// Need >3 items to force block format where quoting applies
const result = reconstructFrontmatter({ tags: ['a#b', 'c', 'd', 'e'] });
assert.ok(result.includes(' - "a#b"'), `Expected quoted hash item, got: ${result}`);
});
test('two-item array uses inline regardless of special chars', () => {
// Note: inline format for <=3 items uses join without quoting
assert.equal(reconstructFrontmatter({ tags: ['a:b', 'c'] }), 'tags: [a:b, c]');
});
test('array item without colon or hash is unquoted in block format', () => {
const result = reconstructFrontmatter({ tags: ['plain', 'also', 'here', 'fourth'] });
assert.equal(result, 'tags:\n - plain\n - also\n - here\n - fourth');
});
});
describe('reconstructFrontmatter: nested objects', () => {
test('simple nested object', () => {
assert.equal(
reconstructFrontmatter({ meta: { key: 'val', num: '42' } }),
'meta:\n key: val\n num: 42'
);
});
test('nested null subvalue is skipped', () => {
assert.equal(reconstructFrontmatter({ meta: { key: null } }), 'meta:');
});
test('nested undefined subvalue is skipped', () => {
assert.equal(reconstructFrontmatter({ meta: { key: undefined } }), 'meta:');
});
test('nested subvalue with colon is double-quoted', () => {
assert.equal(reconstructFrontmatter({ meta: { url: 'http://x' } }), 'meta:\n url: "http://x"');
});
test('nested subvalue with hash is double-quoted', () => {
assert.equal(reconstructFrontmatter({ meta: { name: 'x#y' } }), 'meta:\n name: "x#y"');
});
test('nested empty sub-array', () => {
assert.equal(reconstructFrontmatter({ meta: { items: [] } }), 'meta:\n items: []');
});
test('nested two-item short sub-array uses inline format', () => {
assert.equal(reconstructFrontmatter({ meta: { items: ['a', 'b'] } }), 'meta:\n items: [a, b]');
});
test('nested four-item sub-array uses block format', () => {
assert.equal(
reconstructFrontmatter({ meta: { items: ['a', 'b', 'c', 'd'] } }),
'meta:\n items:\n - a\n - b\n - c\n - d'
);
});
test('nested three-item short sub-array uses inline', () => {
assert.equal(
reconstructFrontmatter({ meta: { items: ['a', 'b', 'c'] } }),
'meta:\n items: [a, b, c]'
);
});
test('nested three-item long sub-array uses block', () => {
const items = ['aaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbb', 'cccccccccccccccccccc'];
assert.equal(
reconstructFrontmatter({ meta: { items } }),
'meta:\n items:\n - aaaaaaaaaaaaaaaaaaa\n - bbbbbbbbbbbbbbbbbbb\n - cccccccccccccccccccc'
);
});
test('nested nested object (3 levels)', () => {
assert.equal(
reconstructFrontmatter({ top: { mid: { deep: 'value' } } }),
'top:\n mid:\n deep: value'
);
});
test('deeply nested null subvalue skipped', () => {
assert.equal(
reconstructFrontmatter({ top: { mid: { key: null } } }),
'top:\n mid:'
);
});
test('deeply nested empty array', () => {
assert.equal(
reconstructFrontmatter({ top: { mid: { items: [] } } }),
'top:\n mid:\n items: []'
);
});
test('deeply nested array with items', () => {
assert.equal(
reconstructFrontmatter({ top: { mid: { items: ['a', 'b'] } } }),
'top:\n mid:\n items:\n - a\n - b'
);
});
});
// ─── spliceFrontmatter ────────────────────────────────────────────────────────
describe('spliceFrontmatter: no existing frontmatter', () => {
test('prepends frontmatter to plain body', () => {
assert.equal(
spliceFrontmatter('body text', { title: 'Test' }),
'---\ntitle: Test\n---\n\nbody text'
);
});
test('prepends frontmatter to empty string', () => {
assert.equal(
spliceFrontmatter('', { title: 'Test' }),
'---\ntitle: Test\n---\n\n'
);
});
test('prepends frontmatter with empty object', () => {
assert.equal(
spliceFrontmatter('body text', {}),
'---\n\n---\n\nbody text'
);
});
test('prepends multi-key frontmatter', () => {
const result = spliceFrontmatter('# Body', { title: 'T', author: 'A' });
assert.equal(result, '---\ntitle: T\nauthor: A\n---\n\n# Body');
});
});
describe('spliceFrontmatter: existing frontmatter', () => {
test('replaces existing frontmatter, preserves body', () => {
const input = '---\ntitle: Old\n---\n\nBody here';
assert.equal(
spliceFrontmatter(input, { title: 'New' }),
'---\ntitle: New\n---\n\nBody here'
);
});
test('replaces existing multi-key frontmatter', () => {
const input = '---\ntitle: Old\ncount: 5\n---\n\nBody text here';
assert.equal(
spliceFrontmatter(input, { title: 'New', count: '5' }),
'---\ntitle: New\ncount: 5\n---\n\nBody text here'
);
});
test('CRLF existing frontmatter: body CRLF preserved', () => {
const input = '---\r\ntitle: Old\r\n---\r\nBody';
const result = spliceFrontmatter(input, { title: 'New' });
assert.equal(result, '---\ntitle: New\n---\r\nBody');
});
test('new frontmatter uses LF even if original was CRLF', () => {
const input = '---\r\ntitle: Old\r\n---\r\nBody';
const result = spliceFrontmatter(input, { title: 'New' });
assert.ok(result.startsWith('---\ntitle: New\n---'));
});
test('return type is always string', () => {
const result = spliceFrontmatter('hello', { k: 'v' });
assert.equal(typeof result, 'string');
});
});
// ─── parseMustHavesBlock ──────────────────────────────────────────────────────
describe('parseMustHavesBlock: no frontmatter / no block', () => {
test('no frontmatter returns []', () => {
assert.deepEqual(parseMustHavesBlock('just content', 'artifacts'), []);
});
test('empty string returns []', () => {
assert.deepEqual(parseMustHavesBlock('', 'artifacts'), []);
});
test('frontmatter without must_haves returns []', () => {
const doc = '---\ntitle: Hello\n---\nbody';
assert.deepEqual(parseMustHavesBlock(doc, 'artifacts'), []);
});
test('must_haves present but requested block absent returns []', () => {
const doc = '---\nmust_haves:\n other:\n - val\n---';
assert.deepEqual(parseMustHavesBlock(doc, 'artifacts'), []);
});
test('block at same indent as must_haves is rejected', () => {
const doc = '---\nmust_haves:\nartifacts:\n - val\n---';
assert.deepEqual(parseMustHavesBlock(doc, 'artifacts'), []);
});
});
describe('parseMustHavesBlock: string items', () => {
test('two plain string items', () => {
const doc = '---\nmust_haves:\n truths:\n - simple string\n - another string\n---';
assert.deepEqual(parseMustHavesBlock(doc, 'truths'), ['simple string', 'another string']);
});
test('single string item', () => {
const doc = '---\nmust_haves:\n truths:\n - only one\n---';
assert.deepEqual(parseMustHavesBlock(doc, 'truths'), ['only one']);
});
test('double-quoted string items strip quotes', () => {
const doc = '---\nmust_haves:\n truths:\n - "contains: colon"\n - "another: one"\n---';
const result = parseMustHavesBlock(doc, 'truths');
assert.deepEqual(result, ['contains: colon', 'another: one']);
});
test('single-quoted string items strip quotes', () => {
const doc = "---\nmust_haves:\n truths:\n - 'single quoted'\n---";
assert.deepEqual(parseMustHavesBlock(doc, 'truths'), ['single quoted']);
});
test('item without colon treated as plain string', () => {
const doc = '---\nmust_haves:\n truths:\n - plain text here\n---';
assert.deepEqual(parseMustHavesBlock(doc, 'truths'), ['plain text here']);
});
test('item with colon but no space (Class::Method) is plain string', () => {
const doc = '---\nmust_haves:\n truths:\n - Class::Method is used\n---';
assert.deepEqual(parseMustHavesBlock(doc, 'truths'), ['Class::Method is used']);
});
test('item with db:seed (no space after colon) is plain string', () => {
const doc = '---\nmust_haves:\n truths:\n - db:seed task should run\n---';
assert.deepEqual(parseMustHavesBlock(doc, 'truths'), ['db:seed task should run']);
});
});
describe('parseMustHavesBlock: key-value object items', () => {
test('simple kv item on dash line', () => {
const doc = '---\nmust_haves:\n artifacts:\n - path: file.ts\n---';
assert.deepEqual(parseMustHavesBlock(doc, 'artifacts'), [{ path: 'file.ts' }]);
});
test('two kv items', () => {
const doc = '---\nmust_haves:\n artifacts:\n - path: file.ts\n - path: other.ts\n---';
assert.deepEqual(parseMustHavesBlock(doc, 'artifacts'), [{ path: 'file.ts' }, { path: 'other.ts' }]);
});
test('kv item with continuation keys', () => {
const doc = [
'---',
'must_haves:',
' artifacts:',
' - path: file.ts',
' provides: something',
'---',
].join('\n');
assert.deepEqual(parseMustHavesBlock(doc, 'artifacts'), [{ path: 'file.ts', provides: 'something' }]);
});
test('kv item with multiple continuation keys', () => {
const doc = [
'---',
'must_haves:',
' artifacts:',
' - path: file.ts',
' provides: exports X',
' confidence: 90',
'---',
].join('\n');
const result = parseMustHavesBlock(doc, 'artifacts');
assert.equal(result.length, 1);
assert.deepEqual(result[0], { path: 'file.ts', provides: 'exports X', confidence: 90 });
});
test('numeric value in continuation key is parsed as integer', () => {
const doc = [
'---',
'must_haves:',
' artifacts:',
' - path: file.ts',
' line: 42',
'---',
].join('\n');
const result = parseMustHavesBlock(doc, 'artifacts');
assert.equal(result[0].line, 42);
assert.equal(typeof result[0].line, 'number');
});
test('non-numeric continuation value stays string', () => {
const doc = [
'---',
'must_haves:',
' artifacts:',
' - path: file.ts',
' provides: some text',
'---',
].join('\n');
const result = parseMustHavesBlock(doc, 'artifacts');
assert.equal(typeof result[0].provides, 'string');
assert.equal(result[0].provides, 'some text');
});
test('two full kv items with continuations', () => {
const doc = [
'---',
'must_haves:',
' artifacts:',
' - path: file.ts',
' provides: something',
' - path: other.ts',
' provides: other',
'---',
].join('\n');
assert.deepEqual(parseMustHavesBlock(doc, 'artifacts'), [
{ path: 'file.ts', provides: 'something' },
{ path: 'other.ts', provides: 'other' },
]);
});
});
describe('parseMustHavesBlock: nested arrays in items', () => {
test('item with array continuation', () => {
const doc = [
'---',
'must_haves:',
' artifacts:',
' - path: file.ts',
' tags:',
' - tag1',
' - tag2',
'---',
].join('\n');
const result = parseMustHavesBlock(doc, 'artifacts');
assert.equal(result.length, 1);
assert.deepEqual(result[0].tags, ['tag1', 'tag2']);
});
test('item with three array elements in continuation', () => {
const doc = [
'---',
'must_haves:',
' artifacts:',
' - path: file.ts',
' tags:',
' - tag1',
' - tag2',
' - tag3',
'---',
].join('\n');
const result = parseMustHavesBlock(doc, 'artifacts');
assert.deepEqual(result[0].tags, ['tag1', 'tag2', 'tag3']);
});
test('two items where first has array continuation', () => {
const doc = [
'---',
'must_haves:',
' artifacts:',
' - path: file.ts',
' tags:',
' - tag1',
' - path: other.ts',
'---',
].join('\n');
const result = parseMustHavesBlock(doc, 'artifacts');
assert.equal(result.length, 2);
assert.deepEqual(result[0].tags, ['tag1']);
assert.equal(result[1].path, 'other.ts');
});
});
describe('parseMustHavesBlock: return type', () => {
test('always returns an array', () => {
const result = parseMustHavesBlock('no content', 'anything');
assert.ok(Array.isArray(result));
});
test('empty content returns array', () => {
const result = parseMustHavesBlock('', 'anything');
assert.ok(Array.isArray(result));
assert.equal(result.length, 0);
});
});
// ─── FRONTMATTER_SCHEMAS ──────────────────────────────────────────────────────
describe('FRONTMATTER_SCHEMAS', () => {
test('plan schema has required field', () => {
assert.ok('required' in FRONTMATTER_SCHEMAS.plan);
});
test('plan schema has exactly 8 required fields', () => {
assert.equal(FRONTMATTER_SCHEMAS.plan.required.length, 8);
});
test('plan schema required fields are exact', () => {
assert.deepEqual(FRONTMATTER_SCHEMAS.plan.required, [
'phase', 'plan', 'type', 'wave', 'depends_on', 'files_modified', 'autonomous', 'must_haves',
]);
});
test('summary schema has exactly 6 required fields', () => {
assert.equal(FRONTMATTER_SCHEMAS.summary.required.length, 6);
});
test('summary schema required fields are exact', () => {
assert.deepEqual(FRONTMATTER_SCHEMAS.summary.required, [
'phase', 'plan', 'subsystem', 'tags', 'duration', 'completed',
]);
});
test('verification schema has exactly 4 required fields', () => {
assert.equal(FRONTMATTER_SCHEMAS.verification.required.length, 4);
});
test('verification schema required fields are exact', () => {
assert.deepEqual(FRONTMATTER_SCHEMAS.verification.required, [
'phase', 'verified', 'status', 'score',
]);
});
test('four schemas exist: plan, plan-gap-closure, summary, verification (#2847)', () => {
assert.deepEqual(
Object.keys(FRONTMATTER_SCHEMAS).sort(),
['plan', 'plan-gap-closure', 'summary', 'verification']
);
});
test('plan includes phase field', () => {
assert.ok(FRONTMATTER_SCHEMAS.plan.required.includes('phase'));
});
test('plan includes must_haves field', () => {
assert.ok(FRONTMATTER_SCHEMAS.plan.required.includes('must_haves'));
});
test('summary includes completed field', () => {
assert.ok(FRONTMATTER_SCHEMAS.summary.required.includes('completed'));
});
test('verification includes score field', () => {
assert.ok(FRONTMATTER_SCHEMAS.verification.required.includes('score'));
});
test('plan does not include score field', () => {
assert.ok(!FRONTMATTER_SCHEMAS.plan.required.includes('score'));
});
test('verification does not include completed field', () => {
assert.ok(!FRONTMATTER_SCHEMAS.verification.required.includes('completed'));
});
});
// ─── FRONTMATTER_SCHEMAS['plan-gap-closure'] (#2847) ──────────────────────────
//
// #2847: --gaps does not load planner-gap-closure.md's requirement into the
// only machine-checked gate — gap-closure plans could pass validation
// without `gap_closure: true`, so a subsequent `--gaps-only` execute run
// silently matched zero plans. Fix: a dedicated schema that requires every
// 'plan' field plus `gap_closure`, kept separate so standard/reviews-mode
// plans (validated against 'plan', asserted unchanged above) are unaffected.
describe("FRONTMATTER_SCHEMAS['plan-gap-closure'] (#2847)", () => {
// #2847 review: "has required field", "has exactly 9 required fields",
// "includes gap_closure field", and "is a superset of every plan-required
// field" were deleted here. Each was strictly subsumed by the deepEqual
// exact-list test below (a 9-element array that deepEquals a literal
// containing 'gap_closure' trivially has 9 elements, a 'required' key,
// and includes 'gap_closure' — none of those checks could ever fail
// independently of the deepEqual one). The "superset" test was additionally
// tautological on its own: plan-gap-closure.required is LITERALLY
// `[...PLAN_REQUIRED_FIELDS, 'gap_closure']` (src/frontmatter.cts), so it
// cannot fail while that spread exists, regardless of what the deepEqual
// test above catches.
test('plan-gap-closure schema required fields are exact', () => {
assert.deepEqual(FRONTMATTER_SCHEMAS['plan-gap-closure'].required, [
'phase', 'plan', 'type', 'wave', 'depends_on', 'files_modified', 'autonomous', 'must_haves', 'gap_closure',
]);
});
test('plan schema (standard/reviews mode) does NOT include gap_closure — unaffected by #2847 fix', () => {
assert.ok(!FRONTMATTER_SCHEMAS.plan.required.includes('gap_closure'));
});
// requiredValues (#2847 review finding): presence alone let `gap_closure: false`
// validate as valid:true — --gaps-only filters strictly on gap_closure === true,
// so that was a live reproduction of #2847's reported symptom, one value away.
test('plan-gap-closure requires the value "true" for gap_closure, not mere presence', () => {
assert.ok('requiredValues' in FRONTMATTER_SCHEMAS['plan-gap-closure']);
assert.equal(FRONTMATTER_SCHEMAS['plan-gap-closure'].requiredValues.gap_closure, 'true');
});
test('plan schema has no requiredValues — every field is presence-only, unaffected by #2847 fix', () => {
assert.equal(FRONTMATTER_SCHEMAS.plan.requiredValues, undefined);
});
test('summary and verification schemas have no requiredValues — presence-only, unaffected', () => {
assert.equal(FRONTMATTER_SCHEMAS.summary.requiredValues, undefined);
assert.equal(FRONTMATTER_SCHEMAS.verification.requiredValues, undefined);
});
});
// ─── Tight branch / boundary tests ───────────────────────────────────────────
describe('reconstructFrontmatter: array length boundary (<=3 vs >3)', () => {
test('exactly 3 items: uses inline format', () => {
const result = reconstructFrontmatter({ x: ['a', 'b', 'c'] });
assert.equal(result, 'x: [a, b, c]');
assert.ok(!result.includes('\n - '));
});
test('exactly 4 items: uses block format', () => {
const result = reconstructFrontmatter({ x: ['a', 'b', 'c', 'd'] });
assert.ok(result.includes(' - a'));
assert.ok(result.includes(' - b'));
assert.ok(result.includes(' - c'));
assert.ok(result.includes(' - d'));
});
test('exactly 1 item: uses inline format', () => {
const result = reconstructFrontmatter({ x: ['only'] });
assert.equal(result, 'x: [only]');
});
});
describe('reconstructFrontmatter: array join length boundary (< 60)', () => {
test('3 items joining to exactly 59 chars uses inline', () => {
// 59 chars: 'aaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbb, ccccccccccccccccccc' = 60 chars, need 59
const a = 'aaaaaaaaaaaaaaaaaa'; // 18
const b = 'bbbbbbbbbbbbbbbbbb'; // 18
const c = 'ccccccccccccccccccc'; // 19 => join = 18+18+19 + 4 (', ', ', ') = 18+2+18+2+19 = 59
const joined = [a, b, c].join(', ');
assert.equal(joined.length, 59);
const result = reconstructFrontmatter({ x: [a, b, c] });
assert.equal(result, `x: [${joined}]`);
});
test('3 items joining to exactly 60 chars uses block', () => {
// 'x' repeated: 19, 19, 18 = 56 + 4 = 60
const x = 'aaaaaaaaaaaaaaaaaaa'; // 19
const y = 'bbbbbbbbbbbbbbbbbbb'; // 19
const z = 'cccccccccccccccccc'; // 18 => 19+2+19+2+18 = 60
const joined2 = [x, y, z].join(', ');
assert.equal(joined2.length, 60);
const result2 = reconstructFrontmatter({ x: [x, y, z] });
// 60 is NOT < 60, so should use block format
assert.ok(result2.startsWith('x:\n - '), `Expected block format, got: ${result2}`);
});
});
describe('reconstructFrontmatter: subarray length boundary', () => {
test('nested 3 items short uses inline', () => {
const result = reconstructFrontmatter({ meta: { x: ['a', 'b', 'c'] } });
assert.equal(result, 'meta:\n x: [a, b, c]');
});
test('nested 4 items uses block', () => {
const result = reconstructFrontmatter({ meta: { x: ['a', 'b', 'c', 'd'] } });
assert.equal(result, 'meta:\n x:\n - a\n - b\n - c\n - d');
});
});
describe('spliceFrontmatter: exact delimiter handling', () => {
test('output always starts with ---', () => {
const result = spliceFrontmatter('', { k: 'v' });
assert.ok(result.startsWith('---\n'));
});
test('existing frontmatter: output uses LF delimiters', () => {
const input = '---\ntitle: Old\n---\nbody';
const result = spliceFrontmatter(input, { title: 'New' });
assert.ok(result.startsWith('---\ntitle: New\n---'));
});
test('no existing frontmatter: body follows after double newline', () => {
const result = spliceFrontmatter('body', { title: 'T' });
assert.equal(result, '---\ntitle: T\n---\n\nbody');
});
test('existing frontmatter: body immediately follows closing ---', () => {
const input = '---\ntitle: T\n---\nbody line';
const result = spliceFrontmatter(input, { k: 'v' });
assert.equal(result, '---\nk: v\n---\nbody line');
});
});
// spliceFrontmatter per-key identity preservation + fail-closed (#1572). These exercise
// sliceTopLevelFrontmatterSegments, the per-key deepEqual/preserve/regenerate/drop/append
// loop, and regenerateFrontmatterKey's "[object Object]" fail-closed directly.
describe('spliceFrontmatter: per-key preservation + fail-closed (#1572)', () => {
const PLAN = [
'---', 'phase: 1', 'wave: 1',
'must_haves:', ' artifacts:', ' - path: src/foo.ts', ' provides: the foo',
'---', '# body', '',
].join('\n');
test('unchanged object-list key keeps its original raw text (provides survives) when a scalar sibling changes', () => {
const parsed = extractFrontmatter(PLAN);
parsed.wave = '2'; // mutate one scalar; must_haves flattened-projection unchanged
const out = spliceFrontmatter(PLAN, parsed);
// must_haves.artifacts raw preserved verbatim (provides intact) — NOT regenerated.
assert.deepEqual(parseMustHavesBlock(out, 'artifacts'), [{ path: 'src/foo.ts', provides: 'the foo' }]);
// the changed scalar WAS regenerated.
assert.ok(/^wave: 2$/m.test(out), 'changed scalar wave must be regenerated to 2');
// original ordering preserved (phase before wave before must_haves).
const phaseIdx = out.indexOf('phase:');
const waveIdx = out.indexOf('wave:');
const mhIdx = out.indexOf('must_haves:');
assert.ok(phaseIdx < waveIdx && waveIdx < mhIdx, 'top-level key order preserved');
});
test('a changed scalar regenerates only that key (no other key touched)', () => {
const out = spliceFrontmatter(PLAN, { ...extractFrontmatter(PLAN), phase: '9' });
assert.ok(/^phase: 9$/m.test(out));
// wave unchanged → still 1
assert.ok(/^wave: 1$/m.test(out));
});
test('keys absent from newObj are dropped (key set is defined by newObj)', () => {
const out = spliceFrontmatter(PLAN, { phase: '1' });
assert.ok(/^phase: 1$/m.test(out));
assert.ok(!/wave:/.test(out), 'wave (absent from newObj) must be dropped');
assert.ok(!/must_haves:/.test(out), 'must_haves (absent from newObj) must be dropped');
});
test('genuinely-new keys (not in original) are appended', () => {
const out = spliceFrontmatter(PLAN, { ...extractFrontmatter(PLAN), brand_new: 'x' });
assert.ok(/^brand_new: x$/m.test(out), 'new key appended');
// existing keys still present
assert.ok(/^phase: 1$/m.test(out));
});
test('a nested indented block stays attached to its parent key (segment slicer respects indentation)', () => {
const multi = '---\na: 1\nmust_haves:\n artifacts:\n - path: x\n provides: y\nb: 2\n---\n';
const out = spliceFrontmatter(multi, { ...extractFrontmatter(multi), b: '3' });
// The indented artifacts block must be preserved as part of must_haves (not split off),
// and b regenerated. proves the slicer grouped the nested lines under must_haves.
assert.deepEqual(parseMustHavesBlock(out, 'artifacts'), [{ path: 'x', provides: 'y' }]);
assert.ok(/^b: 3$/m.test(out));
assert.ok(/^a: 1$/m.test(out));
});
test('whole-document no-op returns the input verbatim', () => {
const out = spliceFrontmatter(PLAN, extractFrontmatter(PLAN));
assert.equal(out, PLAN);
});
test('changing must_haves to an unrepresentable object-list fails closed (throws, no [object Object])', () => {
const newObj = { ...extractFrontmatter(PLAN), must_haves: { artifacts: [{ path: 'p', provides: 'q' }] } };
assert.throws(
() => spliceFrontmatter(PLAN, newObj),
/cannot faithfully serialize key "must_haves"/,
'a changed object-list key must fail closed rather than emit [object Object]',
);
});
test('no-frontmatter path also fails closed for an unrepresentable object-list value', () => {
assert.throws(
() => spliceFrontmatter('body only', { must_haves: { artifacts: [{ path: 'p' }] } }),
/cannot faithfully serialize the requested frontmatter/,
'generating fresh frontmatter with an object-list must fail closed',
);
});
});
describe('extractFrontmatter: complex real-world documents', () => {
test('plan document', () => {
const doc = [
'---',
'phase: 1',
'plan: my-plan',
'type: feature',
'wave: 1',
'depends_on: []',
'files_modified: []',
'autonomous: true',
'must_haves:',
' artifacts:',
' - path: src/foo.ts',
' provides: foo',
'---',
'# Plan body',
].join('\n');
const result = extractFrontmatter(doc);
assert.equal(result.phase, '1');
assert.equal(result.plan, 'my-plan');
assert.equal(result.type, 'feature');
assert.equal(result.wave, '1');
assert.ok(Array.isArray(result.depends_on));
assert.equal(result.depends_on.length, 0);
assert.ok(Array.isArray(result.files_modified));
assert.equal(result.autonomous, 'true');
});
test('summary document', () => {
const doc = [
'---',
'phase: 2',
'plan: my-plan',
'subsystem: auth',
'tags: [security, backend]',
'duration: 120',
'completed: true',
'---',
].join('\n');
const result = extractFrontmatter(doc);
assert.equal(result.phase, '2');
assert.equal(result.subsystem, 'auth');
assert.deepEqual(result.tags, ['security', 'backend']);
assert.equal(result['duration'], '120');
assert.equal(result.completed, 'true');
});
test('verification document', () => {
const doc = [
'---',
'phase: 3',
'verified: true',
'status: pass',
'score: 95',
'---',
].join('\n');
const result = extractFrontmatter(doc);
assert.equal(result.verified, 'true');
assert.equal(result.status, 'pass');
assert.equal(result.score, '95');
});
});
describe('reconstructFrontmatter: round-trip', () => {
test('simple key-value round-trip', () => {
const original = { title: 'Hello', author: 'World' };
const reconstructed = reconstructFrontmatter(original);
const doc = `---\n${reconstructed}\n---\n`;
const parsed = extractFrontmatter(doc);
assert.equal(parsed.title, 'Hello');
assert.equal(parsed.author, 'World');
});
test('value with colon round-trips through quoting', () => {
const original = { url: 'http://example.com' };
const reconstructed = reconstructFrontmatter(original);
assert.equal(reconstructed, 'url: "http://example.com"');
const doc = `---\n${reconstructed}\n---\n`;
const parsed = extractFrontmatter(doc);
assert.equal(parsed.url, 'http://example.com');
});
test('array round-trip (inline)', () => {
const original = { tags: ['a', 'b', 'c'] };
const reconstructed = reconstructFrontmatter(original);
const doc = `---\n${reconstructed}\n---\n`;
const parsed = extractFrontmatter(doc);
assert.deepEqual(parsed.tags, ['a', 'b', 'c']);
});
test('empty array round-trip', () => {
const original = { tags: [] };
const reconstructed = reconstructFrontmatter(original);
assert.equal(reconstructed, 'tags: []');
const doc = `---\n${reconstructed}\n---\n`;
const parsed = extractFrontmatter(doc);
assert.ok(Array.isArray(parsed.tags));
assert.equal(parsed.tags.length, 0);
});
});
// #1779 — reconstructFrontmatter must emit valid YAML for scalars and block-
// array items that carry an embedded `"`/`\`, a control char, an empty string,
// a leading YAML indicator, or surrounding whitespace. The project's own lossy
// `extractFrontmatter` tolerates the broken output, which is exactly why these
// assert round-trip through a STRICT parser (js-yaml) instead — the strict path
// fails on the unescaped/bare emission and passes only once every wrap site
// escapes and every unsafe-bare value is routed through the quoted form.
describe('reconstructFrontmatter: strict-YAML round-trip (#1779)', () => {
// Serialize via the production path, then load with a strict YAML parser.
const strictRoundTrip = (obj) => yaml.load(reconstructFrontmatter(obj));
test('top-level scalar with indicator + embedded quotes (the reported case)', () => {
const obj = { upstream: 'https://x (Tom; "Git. Ship. Done")' };
assert.deepEqual(strictRoundTrip(obj), obj);
});
test('nested scalar with indicator + embedded quotes', () => {
const obj = { meta: { note: 'see: "the docs"' } };
assert.deepEqual(strictRoundTrip(obj), obj);
});
test('top-level block-array item with indicator + embedded quotes', () => {
// 4 items forces block form (the inline `[a, b]` branch caps at 3).
const obj = { tags: ['a: "1"', 'b: "2"', 'c: "3"', 'd: "4"'] };
assert.deepEqual(strictRoundTrip(obj), obj);
});
test('nested block-array item with indicator + embedded quotes', () => {
const obj = { meta: { tags: ['a: "1"', 'b: "2"', 'c: "3"', 'd: "4"'] } };
assert.deepEqual(strictRoundTrip(obj), obj);
});
test('embedded backslash is escaped (not just quotes)', () => {
const obj = { path: 'a:\\b\\c "x"' };
assert.deepEqual(strictRoundTrip(obj), obj);
});
test('control chars in a wrapped value are escaped (newline, tab, NUL)', () => {
const obj = { note: 'line1: a\nline2\twith\x00nul' };
assert.deepEqual(strictRoundTrip(obj), obj);
});
test('CRLF round-trips (locks the \\r escape for Windows-authored values)', () => {
const obj = { note: 'line1: a\r\nline2' };
assert.deepEqual(strictRoundTrip(obj), obj);
});
test('empty string round-trips as "" (bare `k:` would reload as null)', () => {
assert.deepEqual(strictRoundTrip({ k: '' }), { k: '' });
});
test('embedded/leading quote with NO indicator still round-trips', () => {
assert.deepEqual(strictRoundTrip({ k: '"x' }), { k: '"x' });
assert.deepEqual(strictRoundTrip({ k: "'leading single" }), { k: "'leading single" });
assert.deepEqual(strictRoundTrip({ k: 'say "hi" there' }), { k: 'say "hi" there' });
});
test('leading YAML indicators with no `:`/`#` still round-trip', () => {
for (const v of ['>x', '|x', '&anchor', '*alias', '!tag', '[flow', '{map', '%pct', '@reserved', '`tick']) {
assert.deepEqual(strictRoundTrip({ k: v }), { k: v }, `value ${JSON.stringify(v)}`);
}
});
test('leading/trailing whitespace is preserved (bare would be trimmed)', () => {
assert.deepEqual(strictRoundTrip({ k: ' leading' }), { k: ' leading' });
assert.deepEqual(strictRoundTrip({ k: 'trailing ' }), { k: 'trailing ' });
});
test('plain values without indicators are unaffected (no spurious quoting)', () => {
const obj = { name: 'simple-value', label: 'plain' };
assert.equal(reconstructFrontmatter(obj), 'name: simple-value\nlabel: plain');
assert.deepEqual(yaml.load(reconstructFrontmatter(obj)), obj);
});
});
// #3497 — escape amplification. `escapeDoubleQuoted` escapes `\`/`"`/control
// chars on every serialize (#1779), but `parseYamlRegion` only stripped the
// outer quote delimiters and never un-escaped the interior, so parse ∘ serialize
// was NOT the identity: every read-modify-write cycle doubled the backslashes
// (b → 2b+1, i.e. 2ⁿ−1 after n round-trips). A `last_activity_desc` containing
// one embedded quote grew STATE.md to 134 MB in 26 state writes and OOMed
// state.record-session. These tests pin the lossy-parser round trip (the actual
// write seam: extractFrontmatter → reconstructFrontmatter/spliceFrontmatter),
// not the strict-YAML path — the amplification lived in the project's own
// parse/serialize pair.
describe('frontmatter round-trip: escape amplification (#3497)', () => {
// One full document round trip through the production write seam:
// parse the frontmatter out of the document, re-serialize it back in.
const roundTripDoc = (content) => {
const fm = extractFrontmatter(content);
return `---\n${reconstructFrontmatter(fm)}\n---\nbody\n`;
};
test('single round trip is byte-exact for embedded double quotes', () => {
const original = 'Fixed "the bug" in parser';
const fm = extractFrontmatter(`---\ndesc: "Fixed \\"the bug\\" in parser"\n---\nbody\n`);
assert.equal(fm.desc, original);
assert.equal(reconstructFrontmatter({ desc: original }), 'desc: "Fixed \\"the bug\\" in parser"');
});
test('single round trip is byte-exact for embedded backslash', () => {
const original = 'path a:\\b\\c plus "quote"';
const fm = extractFrontmatter(`---\ndesc: "path a:\\\\b\\\\c plus \\"quote\\""\n---\nbody\n`);
assert.equal(fm.desc, original);
assert.equal(extractFrontmatter(`---\n${reconstructFrontmatter({ desc: original })}\n---\n`).desc, original);
});
test('single round trip is byte-exact for newline/tab/CR/control chars', () => {
const original = 'l1\nl2\ttab\rCR\x00NUL\x7fDEL';
const serialized = reconstructFrontmatter({ desc: original });
assert.equal(extractFrontmatter(`---\n${serialized}\n---\n`).desc, original);
});
test('repeated serialize→parse cycles do not grow (26 cycles, the reported OOM window)', () => {
let content = '---\ndesc: "he said \\"hi\\" and \\"bye\\""\n---\nbody\n';
const firstPass = roundTripDoc(content);
let current = firstPass;
for (let i = 0; i < 26; i++) {
current = roundTripDoc(current);
// After the first cycle the document must be a fixed point: byte-identical
// forever. Before the fix, backslashes followed b → 2b+1 and unbounded
// cycling hit a 134 M-char line within 26 passes (asserting per cycle,
// rather than only after the loop, so the buggy failure is a small clear
// diff on cycle 1 instead of a runner OOM).
assert.equal(current, firstPass, `cycle ${i + 1} changed the document`);
}
assert.equal(extractFrontmatter(current).desc, 'he said "hi" and "bye"');
});
test('block array items with quotes/backslashes round-trip and stay stable', () => {
// 4 items force the block `- "..."` form (inline caps at 3), which has its
// own escape call site and its own quote-strip on parse.
const original = ['a: "1"', 'b:\\path "x"', 'c: "3"', 'd: "4"'];
const serialized = reconstructFrontmatter({ tags: original });
assert.deepEqual(extractFrontmatter(`---\n${serialized}\n---\n`).tags, original);
const reparsed = extractFrontmatter(`---\n${serialized}\n---\n`);
assert.equal(reconstructFrontmatter(reparsed), serialized);
});
test('nested object subvalue with quotes/backslashes round-trips and stays stable', () => {
const original = { meta: { note: 'see: "the \\\\docs\\\\"' } };
const serialized = reconstructFrontmatter(original);
assert.deepEqual(extractFrontmatter(`---\n${serialized}\n---\n`).meta, original.meta);
const reparsed = extractFrontmatter(`---\n${serialized}\n---\n`);
assert.equal(reconstructFrontmatter(reparsed), serialized);
});
test('spliceFrontmatter write path is a fixed point under repeated no-op writes', () => {
// The state.cjs seam: read file → merge → spliceFrontmatter → write.
// Repeating it must not change bytes once the first write lands.
let content = `---\ndesc: "he said \\"hi\\""\nphase: executing\n---\nbody\n`;
content = spliceFrontmatter(content, { ...extractFrontmatter(content), phase: 'executing' });
const firstWrite = content;
for (let i = 0; i < 10; i++) {
content = spliceFrontmatter(content, { ...extractFrontmatter(content), phase: 'executing' });
}
assert.equal(content, firstWrite);
assert.equal(extractFrontmatter(content).desc, 'he said "hi"');
});
test('plain scalars that never need quoting still round-trip unquoted and unchanged', () => {
const obj = { name: 'simple-value', wave: 'W1', count: '42' };
const serialized = reconstructFrontmatter(obj);
assert.equal(serialized, 'name: simple-value\nwave: W1\ncount: 42');
assert.deepEqual(extractFrontmatter(`---\n${serialized}\n---\n`), obj);
});
test('single-quoted scalars keep their existing parse behavior (quotes stripped, no escape processing)', () => {
// The writer never emits single-quoted output; hand-authored files keep
// the historical strip-only behavior. A single-quoted scalar has no
// escape processing in YAML (only '' → '), and changing that is out of
// scope for #3497 — this pins the current contract so the unescape fix
// cannot silently broaden into single-quote handling.
const fm = extractFrontmatter("---\ntitle: 'C:\\real\\path'\n---");
assert.equal(fm.title, 'C:\\real\\path');
});
});
describe('extractFrontmatter: boundary — dash at start of file', () => {
test('--- at byte 0 is treated as frontmatter', () => {
const result = extractFrontmatter('---\nkey: val\n---\n');
assert.deepEqual(result, { key: 'val' });
});
test('content before --- means no frontmatter', () => {
const result = extractFrontmatter(' ---\nkey: val\n---\n');
assert.deepEqual(result, {});
});
test('newline before --- means no frontmatter', () => {
const result = extractFrontmatter('\n---\nkey: val\n---\n');
assert.deepEqual(result, {});
});
});
describe('parseMustHavesBlock: item accumulation', () => {
test('last item pushed after loop ends', () => {
const doc = '---\nmust_haves:\n truths:\n - only item\n---';
const result = parseMustHavesBlock(doc, 'truths');
assert.equal(result.length, 1);
assert.equal(result[0], 'only item');
});
test('items are pushed in order', () => {
const doc = '---\nmust_haves:\n truths:\n - first\n - second\n - third\n---';
const result = parseMustHavesBlock(doc, 'truths');
assert.equal(result[0], 'first');
assert.equal(result[1], 'second');
assert.equal(result[2], 'third');
});
test('three items total count', () => {
const doc = '---\nmust_haves:\n truths:\n - a\n - b\n - c\n---';
assert.equal(parseMustHavesBlock(doc, 'truths').length, 3);
});
});
describe('parseMustHavesBlock: indent stopping logic', () => {
test('items after block ends at same/lower indent are not included', () => {
const doc = [
'---',
'must_haves:',
' truths:',
' - item one',
'other_key: val',
'---',
].join('\n');
const result = parseMustHavesBlock(doc, 'truths');
assert.equal(result.length, 1);
assert.equal(result[0], 'item one');
});
});
describe('reconstructFrontmatter: deeply nested subsubval null/undefined', () => {
test('3rd level null subsubval skipped', () => {
const result = reconstructFrontmatter({ top: { mid: { key: null } } });
assert.equal(result, 'top:\n mid:');
});
});
describe('reconstructFrontmatter: nested subval plain string', () => {
test('nested subval without special chars unquoted', () => {
const result = reconstructFrontmatter({ meta: { name: 'plain' } });
assert.equal(result, 'meta:\n name: plain');
});
test('nested subval with colon quoted', () => {
const result = reconstructFrontmatter({ meta: { ref: 'type: value' } });
assert.equal(result, 'meta:\n ref: "type: value"');
});
test('nested subval with hash quoted', () => {
const result = reconstructFrontmatter({ meta: { tag: 'issue#42' } });
assert.equal(result, 'meta:\n tag: "issue#42"');
});
});
// noOpObjectListSetError (#1660) — pure detection helper, unit-tested directly because the
// cmdFrontmatterSet path is not in Stryker's property/unit set.
describe('noOpObjectListSetError (#1660)', () => {
const ORIG = '---\nphase: 1\n---\n';
test('changed content (real update) → null', () => {
assert.equal(noOpObjectListSetError(ORIG, ORIG + 'x', { must_haves: 1 }), null);
});
test('scalar value no-op → null (idempotent scalar sets are fine)', () => {
for (const v of [1, 'str', true, 0, '']) assert.equal(noOpObjectListSetError(ORIG, ORIG, v), null, `scalar ${JSON.stringify(v)}`);
});
test('scalar-array value no-op → null (scalar arrays round-trip faithfully)', () => {
assert.equal(noOpObjectListSetError(ORIG, ORIG, ['a', 'b']), null);
assert.equal(noOpObjectListSetError(ORIG, ORIG, []), null);
});
test('null value no-op → null', () => {
assert.equal(noOpObjectListSetError(ORIG, ORIG, null), null);
});
test('dict value no-op → error message naming the object-list round-trip limit', () => {
const msg = noOpObjectListSetError(ORIG, ORIG, { artifacts: [{ path: 'p' }] });
assert.equal(typeof msg, 'string');
assert.ok(msg.includes('had no effect'), msg);
assert.ok(msg.includes('object-list'), msg);
assert.ok(msg.includes('Edit the file directly'), msg);
});
});
// ─── stripFrontmatter ─────────────────────────────────────────────────────────
describe('stripFrontmatter', () => {
const stacked = ['---', 'a: 1', '---', '---', 'b: 2', '---', '', 'Real body.'].join('\n');
test('strips a single block', () => {
assert.strictEqual(stripFrontmatter(['---', 'a: 1', '---', '', 'Body.'].join('\n')), 'Body.');
});
test('is CRLF-tolerant', () => {
const crlf = ['---', 'a: 1', '---', '', 'Body.'].join('\r\n');
assert.strictEqual(stripFrontmatter(crlf), 'Body.');
});
test('defaults to stripping every stacked block (corruption recovery)', () => {
assert.strictEqual(stripFrontmatter(stacked), 'Real body.');
});
test('an omitted options argument keeps the greedy default', () => {
// Back-compat: state.cts and state-transition.cts call this with one arg.
assert.strictEqual(stripFrontmatter(stacked, {}), 'Real body.');
});
test('once: true stops after the first block', () => {
assert.strictEqual(
stripFrontmatter(stacked, { once: true }),
['---', 'b: 2', '---', '', 'Real body.'].join('\n'),
);
});
test('once: false is the greedy default', () => {
assert.strictEqual(stripFrontmatter(stacked, { once: false }), 'Real body.');
});
test('returns content unchanged when there is no frontmatter', () => {
const plain = ['Just prose.', '', 'More prose.'].join('\n');
assert.strictEqual(stripFrontmatter(plain), plain);
assert.strictEqual(stripFrontmatter(plain, { once: true }), plain);
});
test('leaves an unterminated block alone under both modes', () => {
const unterminated = ['---', 'a: 1', 'b: 2'].join('\n');
assert.strictEqual(stripFrontmatter(unterminated), unterminated);
assert.strictEqual(stripFrontmatter(unterminated, { once: true }), unterminated);
});
test('empty string round-trips', () => {
assert.strictEqual(stripFrontmatter(''), '');
});
});
// ─── agentScalarNeedsDoubleQuoting (#3706) ────────────────────────────────────
//
// Mutant-killing discipline: every boolean clause gets a true case AND a
// near-miss false case one edit away, so flipping any operator/character
// class in the source changes at least one assertion here.
describe('agentScalarNeedsDoubleQuoting: delegates to scalarNeedsDoubleQuoting', () => {
test('empty string needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting(''), true);
});
test('embedded double quote needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a"b'), true);
});
test('embedded backslash needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a\\b'), true);
});
test('embedded control char needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a\u0001b'), true);
});
test('leading whitespace needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting(' abc'), true);
});
test('trailing whitespace needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('abc '), true);
});
for (const c of ['#', '&', '*', '!', '|', '>', '%', '@', '`', '[', ']', '{', '}', ',', "'"]) {
test(`leading indicator "${c}" needs quoting`, () => {
assert.equal(agentScalarNeedsDoubleQuoting(`${c}foo`), true);
});
}
test('near-miss: plain word does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('plainword'), false);
});
});
describe('agentScalarNeedsDoubleQuoting: first character must be alphanumeric', () => {
for (const v of ['~', '.inf', '.nan', '+1', '-1', '-0', '.5']) {
test(`non-alphanumeric first char "${v}" needs quoting`, () => {
assert.equal(agentScalarNeedsDoubleQuoting(v), true);
});
}
test('near-miss: alphanumeric-first with internal hyphen does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a-b'), false);
});
test('near-miss: digit-first non-numeric word does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('0abc'), false);
});
test('near-miss: alphanumeric-first with trailing hyphen does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('x-'), false);
});
});
describe('agentScalarNeedsDoubleQuoting: trailing colon', () => {
test('a bare trailing colon needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('foo:'), true);
});
test('near-miss: colon followed by more text does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('foo:bar'), false);
});
test('near-miss: single-char key:value shape does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a:b'), false);
});
});
describe('agentScalarNeedsDoubleQuoting: embedded ": " (colon + whitespace)', () => {
test('colon followed by space needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a: b'), true);
});
test('colon followed by tab needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a:\tb'), true);
});
test('near-miss: colon with no following whitespace does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a:b'), false);
});
});
describe('agentScalarNeedsDoubleQuoting: embedded " #" (whitespace + hash)', () => {
test('space followed by hash needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a #b'), true);
});
test('tab followed by hash needs quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a\t#b'), true);
});
test('near-miss: hash with no preceding whitespace does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a#b'), false);
});
});
describe('agentScalarNeedsDoubleQuoting: boolean/null words (case-insensitive)', () => {
for (const v of ['y', 'n', 'yes', 'no', 'true', 'false', 'on', 'off', 'null']) {
test(`lowercase word "${v}" needs quoting`, () => {
assert.equal(agentScalarNeedsDoubleQuoting(v), true);
});
}
for (const v of ['YES', 'No', 'TRUE', 'Null']) {
test(`mixed-case word "${v}" needs quoting (case-insensitivity)`, () => {
assert.equal(agentScalarNeedsDoubleQuoting(v), true);
});
}
test('near-miss: "yes1" is not an exact word match', () => {
assert.equal(agentScalarNeedsDoubleQuoting('yes1'), false);
});
test('near-miss: "nope" is not an exact word match', () => {
assert.equal(agentScalarNeedsDoubleQuoting('nope'), false);
});
test('near-miss: "nullish" is not an exact word match', () => {
assert.equal(agentScalarNeedsDoubleQuoting('nullish'), false);
});
test('near-miss: "onward" is not an exact word match', () => {
assert.equal(agentScalarNeedsDoubleQuoting('onward'), false);
});
});
describe('agentScalarNeedsDoubleQuoting: numeric-looking values', () => {
for (const v of [
'1', '123', '1.5', '1.', '1e5', '1E5', '1e+5', '1e-5', '1_000',
'0x1F', '0b101', '0o17', '0X1f', '12:30', '1:2:3', '12:30.5',
]) {
test(`numeric form "${v}" needs quoting`, () => {
assert.equal(agentScalarNeedsDoubleQuoting(v), true);
});
}
test('near-miss: "1a" (digit then letter) does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('1a'), false);
});
test('near-miss: "a1" (letter then digit) does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('a1'), false);
});
test('near-miss: "x1e5" (non-digit first char) does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('x1e5'), false);
});
test('near-miss: "0xzz" (invalid hex digits) does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('0xzz'), false);
});
// Verified against the real implementation: the sexagesimal group only
// consumes a leading [0-5]? then one mandatory digit per ":" segment, so
// "12:99" cannot fully match YAML_NUMERIC_RE (the second "9" is left over)
// and no other clause fires either. The code does NOT reject an
// out-of-range (60-99) minute-like group — pinned here as actual behavior,
// not the originally assumed "false because minutes must be 0-59".
test('"12:99" does not need quoting (sexagesimal regex cannot consume the trailing digit)', () => {
assert.equal(agentScalarNeedsDoubleQuoting('12:99'), false);
});
});
describe('agentScalarNeedsDoubleQuoting: YAML timestamp', () => {
for (const v of [
'2026-08-25', '2026-8-5', '2026-08-25T10:00:00Z', '2026-08-25 10:00:00',
]) {
test(`timestamp form "${v}" needs quoting`, () => {
assert.equal(agentScalarNeedsDoubleQuoting(v), true);
});
}
test('near-miss: "2026-08" (missing day) does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('2026-08'), false);
});
// Verified against the real implementation: this string never reaches the
// timestamp clause at all — the pure-digit numeric clause (YAML_NUMERIC_RE's
// first alternative matches any \d[\d_]* string) fires first and returns
// true. So "20260825" IS true, but for a different reason than "looks like
// a date"; it does not exercise YAML_TIMESTAMP_RE.
test('"20260825" (no hyphens) needs quoting via the numeric clause, not the timestamp clause', () => {
assert.equal(agentScalarNeedsDoubleQuoting('20260825'), true);
});
// This is the case that actually pins YAML_TIMESTAMP_RE's trailing
// `(?:[Tt ].*)?$` anchor: the numeric clause cannot match (hyphens present,
// no colon), so only the timestamp regex is left to decide, and it rejects
// trailing text that isn't introduced by "T"/"t"/" ".
test('"2026-08-25x" (trailing junk not preceded by T/t/space) does not need quoting', () => {
assert.equal(agentScalarNeedsDoubleQuoting('2026-08-25x'), false);
});
});
describe('agentScalarNeedsDoubleQuoting: real-world values that must stay unquoted', () => {
for (const v of [
'sonnet', 'synthetic/hf:zai-org/GLM-5.2', 'gpt-5.6-luna', 'claude-opus-5',
'high', 'xhigh', 'minimal', 'a.b', 'GLM-5.2',
]) {
test(`"${v}" does not need quoting`, () => {
assert.equal(agentScalarNeedsDoubleQuoting(v), false);
});
}
});
// ─── escapeDoubleQuoted (#1779 / #3497) ────────────────────────────────────────
describe('escapeDoubleQuoted: exact output strings', () => {
test('backslash is escaped before the quote it precedes (ordering matters)', () => {
// Input: a, \, ", b. If the quote were escaped BEFORE the backslash, the
// backslash added in front of the quote would then itself get doubled by
// a subsequent backslash pass, producing a different (wrong) string. The
// real order (backslash first, then quote) yields exactly 3 backslashes
// followed by the quote.
const input = 'a' + '\\' + '"' + 'b';
const expected = 'a' + '\\'.repeat(3) + '"' + 'b';
assert.equal(escapeDoubleQuoted(input), expected);
});
test('double quote alone', () => {
assert.equal(escapeDoubleQuoted('"'), '\\"');
});
test('newline alone', () => {
assert.equal(escapeDoubleQuoted('\n'), '\\n');
});
test('tab alone', () => {
assert.equal(escapeDoubleQuoted('\t'), '\\t');
});
test('carriage return alone', () => {
assert.equal(escapeDoubleQuoted('\r'), '\\r');
});
test('a C0 control char (0x01) becomes lowercase zero-padded \\xHH', () => {
assert.equal(escapeDoubleQuoted('\u0001'), '\\x01');
});
test('DEL (0x7f) becomes \\x7f', () => {
assert.equal(escapeDoubleQuoted('\u007f'), '\\x7f');
});
test('plain string with no specials is returned unchanged', () => {
assert.equal(escapeDoubleQuoted('plain'), 'plain');
});
test('combined input exercising every escape in one pass', () => {
const input = 'a\\b"c\nd\te\rf\u0001g\u007fh';
const expected = 'a\\\\b\\"c\\nd\\te\\rf\\x01g\\x7fh';
assert.equal(escapeDoubleQuoted(input), expected);
});
});