Files
msd-core/tests/frontmatter.unit.test.cjs
Tom Boucher df04aae5e4 enhancement(#537): migrate all hand-written bin/lib/*.cjs to TypeScript source of truth (ADR-457) (#602)
* enhancement(#537): migrate code-review-flags to TS source of truth

Collapse the hand-written get-shit-done/bin/lib/code-review-flags.cjs to a
TypeScript source of truth (src/code-review-flags.cts), compiled by tsc to a
gitignored .cjs build artifact at the same path, per ADR-457 (build-at-publish).
Second module after the semver-compare pilot (#541).

Behaviour is preserved byte-for-behaviour (characterization test added in
tests/code-review-flags.test.cjs locks the parser quirks). Adds compile-time
type checking: CodeReviewFlags interface + CodeReviewWorkflow literal union.
The require() path is unchanged, so code-review.md and the bug-3727 test keep
working. The emitted .cjs is gitignored and eslint-ignored, mirroring the pilot.

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

* enhancement(#537): migrate 9 leaf bin/lib modules to TS source of truth

ADR-457 build-at-publish, batch 1 (pure leaf modules, 0 sibling-deps):
001-legacy-orphan-files, context-utilization, redaction, artifacts,
command-arg-projection, clock, ui-safety-gate, review-reviewer-selection,
clusters. Each moves to src/*.cts (strict TS, typed), compiled by tsc to a
gitignored .cjs at the same require() path; behaviour preserved byte-for-
behaviour. Adds src/node-globals.d.ts (minimal ambient shim; "types":[]).

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

* chore(#537): add @types/node, drop hand-rolled node-globals shim

ADR-457 migration infra: replace the temporary src/node-globals.d.ts ambient
shim with @types/node@22 + "types":["node"] in tsconfig.build.json. Unblocks
migrating the ~49 remaining bin/lib modules that use node:fs/path/os/
child_process. Build + full suite (3030 pass) + lint all green; no .cts type
changes were needed (real Node types matched the shim).

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

* enhancement(#537): migrate 9 more bin/lib modules to TS (batch 2)

ADR-457 build-at-publish. Clean leaves: installer-migration-report,
prompt-budget. Type-error-prone leaves (were tsconfig.lint-excluded; now
strict-typed and removed from that exclude list): secrets, phase-lifecycle,
workstream-name-policy, decisions, validate, schema-detect. Plus
runtime-name-policy. Strict type fixes narrow unknown->concrete domain types
(no any/ts-ignore); behaviour preserved. Full suite green, lint 0 errors.

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

* enhancement(#537): migrate runtime-slash to TS (cross-import proof)

ADR-457. First cross-module TS->TS import: src/runtime-slash.cts imports
./runtime-name-policy.cjs and tsc resolves the sibling .cts types under strict
(no declaration files; NodeNext .cjs->.cts mapping), emitting a correct
require("./runtime-name-policy.cjs"). Confirms the recipe for coupled modules,
which must be migrated in dependency order (leaves-up). Suite green, lint clean.

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

* enhancement(#537): migrate 10 more bin/lib modules to TS (batch 3)

ADR-457 build-at-publish, Wave-1 leaves: event, workstream-inventory-builder,
plan-scan, fallow-runner, project-root, installer-migration-authoring,
update-context, 000-first-time-baseline, runtime-homes, model-catalog. Strict
typing fixed real issues (narrowing unknown, qualified fs/path calls, removed
unnecessary casts); plan-scan/project-root/workstream-inventory-builder dropped
from tsconfig.lint exclude. Behaviour preserved; suite green, lint 0 errors.

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

* enhancement(#537): migrate 5 large Wave-1 leaves to TS (batch 4)

ADR-457 build-at-publish: configuration, state-document, shell-command-
projection (42 dependents), security, command-aliases. shell-command-
projection keeps a namespace child_process import for mock-intercept
testability. loadConfig/migrateOnDisk emit synchronously (every caller uses
them sync; the one awaited migrateOnDisk caller tolerates a non-Promise) —
full suite (3030 pass) confirms behaviour preserved. configuration/
state-document/command-aliases dropped from tsconfig.lint exclude. Also fixes
the malformed batch-3 changeset frontmatter (type/pr) that failed lint:docs.

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

* enhancement(#537): migrate 6 Wave-2 modules to TS (batch 5)

ADR-457 build-at-publish: config-schema, model-profiles,
002-codex-legacy-hooks-json, logger, active-workstream-store, adr-parser.
First batch importing already-migrated siblings (configuration, model-catalog,
shell-command-projection, redaction, security) via ./sibling.cjs specifiers.
Strict type narrowing (typeof guards over String(unknown)); behaviour
preserved; suite 3030 pass, lint 0 errors.

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

* enhancement(#537): migrate 5 large Wave-2 modules to TS (batch 6)

ADR-457 build-at-publish: graphify, install-profiles, intel,
installer-migrations, worktree-safety. installer-migrations preserves its
dynamic require() loader for numbered migration modules (scoped lint
suppressions). Strict typing (typeof guards over String(unknown)); behaviour
preserved; suite 3030 pass, lint 0 errors. Wave 2 complete.

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

* enhancement(#537): migrate Wave-3 modules to TS (batch 7)

ADR-457 build-at-publish: planning-workspace, runtime-artifact-layout,
command-routing-hub, drift. Uses `import x = require()` for export= siblings;
drift's lazy require of runtime-slash hoisted to a top-level import (verified
non-circular). Behaviour preserved; suite 3030 pass, lint 0 errors.

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

* enhancement(#537): migrate small Wave-4 modules to TS (batch 8)

ADR-457 build-at-publish: cjs-command-router-adapter, phase-command-router,
surface, roadmap-upgrade. Typed the hub router handler results as the HubResult
discriminated union; surface drops 4 genuinely-unused imports. Behaviour
preserved; suite 3030 pass, lint 0 errors.

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

* enhancement(#537): migrate core hub (2.5k LOC, 68 dependents) to TS (batch 9)

ADR-457 build-at-publish: get-shit-done/bin/lib/core.cjs -> src/core.cts,
preserving all 63 exports via export=. All sibling deps already migrated
(shell-command-projection, model-profiles, model-catalog, worktree-safety,
planning-workspace, project-root, configuration, config-schema). Strict types,
no any/ts-ignore; config-schema lazy require hoisted (non-circular). Behaviour
preserved (independently verified: core's shard 3030 pass / 0 fail).

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

* test(#537): make ESLint-coverage + test-sprawl checks migration-aware

#551 test hardcoded 12 now-migrated modules as "hand-written, must be linted";
that invariant is obsoleted by the ADR-457 migration. Rewrite it to a
filesystem-driven invariant that holds at every stage: a bin/lib/*.cjs must be
eslint-ignored IFF it has a src/*.cts source (tsc-generated), else linted
(covers package-identity, which has no TS source). Also eslint-ignore
config-types.cjs (has a src counterpart) and drop the redundant
tests/clock.test.cjs (clock already covered by clock-seam + bug-474 tests),
which tripped the lint-test-file-count ratchet.

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

* enhancement(#537): migrate 9 Wave-5 router/inventory modules to TS (batch 10)

ADR-457 build-at-publish: phases/verify/init/agent/task/validate/roadmap/state
command routers + workstream-inventory. Router handler results typed against
core's exported shapes; behaviour preserved (caught+fixed a --verify boolean
flag regression mid-migration). Full suite green across all shards (only the 4
local gpg-env changeset-notes failures remain; CI passes them).

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

* enhancement(#537): migrate 7 Wave-5 modules to TS (batch 11)

ADR-457 build-at-publish: gap-checker, docs, check-command-router, frontmatter,
learnings, gsd2-import, profile-pipeline. Behaviour preserved; full suite green
across all shards (only the 4 local gpg-env failures remain). Also broadens
atomic-write-coverage.test.cjs to accept the tsc-compiled namespace-import form
while still asserting platformWriteSync is called (safety guard intact).

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

* enhancement(#537): migrate config + profile-output to TS (batch 12)

ADR-457 build-at-publish: config (729 LOC), profile-output (1142 LOC). All
exports preserved; cmdMigrateConfig de-asynced (migrateOnDisk is sync, awaited
caller tolerates it). Behaviour preserved; suite green across all shards
(only the 4 local gpg-env failures). Wave 5 complete.

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

* enhancement(#537): migrate 5 Wave-6 modules to TS (batch 13)

ADR-457 build-at-publish: template, uat, workstream, roadmap, audit. Behaviour
preserved (dead toPosixPath import dropped from audit; inline requires hoisted).
Suite green across all shards (only the 4 local gpg-env failures).

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

* enhancement(#537): migrate commands + state hubs to TS (batch 14)

ADR-457 build-at-publish: commands (1305 LOC), state (2074 LOC, 17 dependents).
All exports preserved; inner requires kept non-hoisted where load-order matters
(install.js, per-call security); acquireStateLock cast inlined to preserve the
err.code source token a structural test inspects. Behaviour preserved; suite
green across all shards (only the 4 local gpg-env failures). Wave 6 complete.

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

* enhancement(#537): migrate milestone to TS (batch 15a, hand-authored)

ADR-457 build-at-publish: milestone -> src/milestone.cts. Authored directly
(subagent capacity was unavailable). Also relaxes core.output()'s 3rd param to
optional, matching its real always-optional call contract (unblocks remaining
2-arg output callers). Behaviour preserved; suite green across all shards
(only the 4 local gpg-env failures).

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

* enhancement(#537): migrate phase, verify, init to TS (batch 15, final modules)

ADR-457 build-at-publish, Wave 7 (the last hubs): phase (1608 LOC), verify
(1615), init (2113). Adds src/package-identity.d.cts so verify can import the
permanently value-baked package-identity.cjs under strict TS.

Fixes two regressions the migration introduced in verify: restore
cmdValidateHealth's `return result` (callers/tests read result.warnings — it is
NOT side-effect-only), and make the bug-3384 source-pattern test tolerant of the
tsc-compiled bracket-notation form of the git_list_failed->W020 branch (behaviour
intact). Full suite green across all shards (only the 4 local gpg-env failures);
lint 0 errors. All 86 migratable bin/lib modules are now TypeScript sources.

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

* chore(#537): finalize ADR-457 migration — retire tsconfig.lint.json

All hand-written bin/lib/*.cjs are now src/*.cts sources, so the checkJs
stopgap tsconfig.lint.json (unused; not wired into eslint, scripts, or CI) is
deleted per ADR-457's final step. Also gitignore the tsc-generated
config-types.cjs (was still committed) for consistency with every other
emitted artifact. package-identity.cjs stays value-baked (declared via
src/package-identity.d.cts). Suite green; #551 ESLint-coverage test green.

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

* fix(#537): add prepare script so unpacked/git installs build bin/lib artifacts

ADR-457 build-at-publish: bin/lib/*.cjs are now gitignored, built by tsc. The
prepack/prepublishOnly hooks cover `npm pack`/publish, but `npm install -g
<dir>` and git installs run the `prepare` lifecycle — which was missing — so the
unpacked install shipped without the compiled .cjs and failed at startup with
"Cannot find module './lib/core.cjs'" (caught by the smoke-unpacked CI job).
Add `prepare` mirroring prepublishOnly (build:lib + build:hooks). prepare does
NOT run for registry consumers (they get the pre-built tarball), only for
source/local/pack installs.

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

* fix(#537): make CI build/lockfile checks work with gitignored bin/lib artifacts

ADR-457 build-at-publish exposed two CI assumptions that bin/lib/*.cjs are
always present on disk:
- check:env's lockfile-sync ran `npm ci --dry-run`, which now triggers the
  `prepare` build (tsc) — but it runs before deps are installed, so tsc is
  absent and it misreported the lockfile as out of sync. Add --ignore-scripts
  (a lockfile check must not build).
- the lint-tests job installs with --ignore-scripts (no prepare build), but
  lint:skill-deps require()s the built install-profiles.cjs. Add an explicit
  `npm run build:lib` step after install.

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

* fix(#537): narrow prepare to build:lib only (unbreak packed-smoke pack step)

prepare running build:hooks emitted "✓ Copying ..." stdout during `npm pack`,
which the install-smoke "Pack root tarball" step captures into $GITHUB_OUTPUT —
breaking it with "Invalid format". build:lib (tsc) is silent on success and is
all the unpacked/source install needs (the smoke-unpacked assertions exercise
gsd-tools, i.e. bin/lib, and tolerate hook setup with `|| true`). Matches
prepack. build:hooks still runs on prepublishOnly for real publishes.

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

* fix(#537): wire Stryker mutation gate to build-at-publish layout

The gate scored 0.00 because it mutated changed bin/lib/*.cjs that (a) were
generated artifacts and (b) included modules with no coverage in the command's
test set. Rework: mutation.yml now derives changed COVERED modules from
src/*.cts and maps them to their built bin/lib/*.cjs; Stryker mutates those
built artifacts with a no-rebuild command (mutating src/*.cts + per-mutant tsc
was ~3x over the 30-min CI budget).

NOTE: with the gate now correctly measuring the covered modules, their actual
mutation score is 42.94% (< break 50) — a pre-existing test-coverage gap
(adr-parser/prompt-budget/etc.), not introduced by this behaviour-preserving
migration. Reaching 50 needs more tests, a threshold/scope change, or a waiver —
a maintainer decision.

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

* test(#537): raise mutation coverage of covered modules above the 50 gate

Adds focused example-based unit tests that kill surviving mutants in the two
lowest-scoring covered modules:
- tests/prompt-budget.unit.test.cjs (112 tests): 17.9% -> 97.9%
- tests/adr-parser.unit.test.cjs (205 tests): 44.7% -> 89.4%
Both wired into stryker.config.mjs's command. Fresh full run over the 6 covered
modules now scores 82.25% (>= break 50); every covered module is >= 68%.

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

* enhancement(#537,#609): parallelize mutation gate via dynamic per-module matrix

The serial Stryker run timed out at 30 min once the migration's added tests
made every mutant re-run ~300 tests. Replace it with a dynamic matrix so the
gate completes well under budget — folded into this PR (was tracked as #609)
because it's a prerequisite for this PR's mutation gate to pass.

- scripts/mutation-matrix.cjs: single source of truth (covered-module -> test
  files) computing changed covered modules from git diff -> {has_work, matrix}.
- mutation.yml: detect -> dynamic `matrix: fromJSON(...)` mutate job (one
  parallel shard per changed module, scoped via MUTATION_TEST_CMD to only that
  module's tests, 15-min/shard) -> summary job that KEEPS the legacy check name
  "Stryker mutation score (changed files only)" so branch protection is
  unchanged. Per-shard jobs report as "Stryker (<module>)".
- stryker.config.mjs: commandRunner.command reads MUTATION_TEST_CMD (falls back
  to the full command locally).

Closes #609.

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

* test(#537,#609): give each mutation shard ≥50% on its own tests; drop blacksmith note

Per-module sharding revealed that active-workstream-store (46.5%) and
frontmatter (7.4%) only cleared 50% in the old serial run via timeout-noise from
the bloated 300-test command; on their own tests they were below the gate. Add
focused unit tests:
- tests/active-workstream-store.unit.test.cjs (115 tests): 46.5% -> 81.9%
- tests/frontmatter.unit.test.cjs (165 tests): 7.4% -> 63.4%
Both wired into scripts/mutation-matrix.cjs (per-module test map) and
stryker.config.mjs DEFAULT_TEST_CMD. All 6 covered modules now clear break:50
with only their own tests (config-schema/context-utilization/prompt-budget/
adr-parser already did). Also removes the leftover blacksmith TODO comment —
GitHub-hosted runners only; speed comes from parallel per-module shards.

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

* test(#537,#609): strengthen prompt-budget tests to clear the gate on its own tests

prompt-budget scored 39.58% when mutation-tested with ONLY its own tests (the
way the per-module CI shard runs it) — an earlier ~98% reading was inflated by
accidentally running the full multi-module command. Add 96 targeted tests to
tests/prompt-budget.unit.test.cjs (exact note-template text, plan-truncation
arithmetic/percentages, drop-block strings, noteInjected/hardFailed booleans):
scoped score 39.58% -> 68.75% (>= break 50). All 6 covered modules now clear
the gate on their own tests.

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

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 11:45:01 -04:00

1126 lines
38 KiB
JavaScript

'use strict';
/**
* Unit tests for frontmatter.cjs
*
* Module: get-shit-done/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 {
extractFrontmatter,
reconstructFrontmatter,
spliceFrontmatter,
parseMustHavesBlock,
FRONTMATTER_SCHEMAS,
} = require('../get-shit-done/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('three schemas exist: plan, summary, verification', () => {
assert.deepEqual(Object.keys(FRONTMATTER_SCHEMAS).sort(), ['plan', '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'));
});
});
// ─── 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');
});
});
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);
});
});
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"');
});
});