chore(#2928): productionize the CONTEXT.md predicate fact-store and gate it in CI (#2938)

* feat(#2928): port CONTEXT.md predicate fact-store into the src seam

Productionizes the ADR-1671 Option-E reference example as a real module:
src/context-predicates.cts (parser + selector + index builder) compiled to
gsd-core/bin/lib/, plus scripts/gen-context-index.cjs following the repo's
--check/--write drift-guard idiom and wired into lint:generated-sync.

Parser behavior is deliberately prototype-equivalent in this commit so the
next commit's regression matrix binds to the real defects rather than to a
missing module.

Two locked design deviations from the prototype:
- duplicates carry a count, not line numbers
- the committed index carries no line field at all, resolving ADR-1671 open
  question 4: an artifact without line numbers cannot drift on a line shift,
  so promoting --check to a CI gate does not make it routinely red

Also reconciles the one remaining duplicate predicate ID
(RULESET.WORKFLOW_MARKDOWN.FENCES was declared twice; the non-MD040 wording
is removed) so the gate can land fail-closed on duplicates.

Refs #1671

* test(#2928): failing-first matrix for the predicate fact-store

Adds the regression matrix from the phase test plan: parser declaration
forms, fence and comment regions, ID/value grammar boundaries at
limit-1/limit/limit+1, CRLF fidelity, duplicate detection, the drift-guard
CLI, the selector query surface, and four document-shaped fast-check
properties.

Seven rows are RED for behavioral reasons against the ported parser:
indented-bare, star-list, plus-list and numbered-list declaration forms are
dropped; a tilde fence and a four-backtick fence containing a shorter fence
are not skipped; and a multi-line HTML comment is parsed as live. Eleven
selector rows are RED because the query surface is not wired yet.

Negative fixtures come from real repo documents that predate the grammar
(CONTEXT.md, CONTRIBUTING.md's fenced env-assignment examples) per the
fixture-provenance rule, and the property generators are document-shaped
rather than seeded from our own serializer.

Refs #1671

* fix(#2928): consume the shared fence scanner, relocate the index, wire the selector

Drives the failing-first matrix green.

Parser: replaces the ported naive triple-backtick toggle with the shared
markdown-sectionizer fence engine. scanFencedBlocks and FencedBlockRecord
gain an export keyword — the only change to that module, which has 71
upstream dependents — because it already returns line-indexed spans, which
is exactly what a line-reporting parser needs. It also already documents
itself as the second copy of the fence state machine pending consolidation;
adding a third copy here would have been the generative-fix divergence this
repo warns about. A parity suite now pins predicate fence-skipping against
that scanner across eight fence shapes. HTML-comment skipping stays local
because the sectionizer has no comment scanner. Declaration forms widen to
indented-bare, star, plus and numbered list items.

Index location: docs/CONTEXT-INDEX.json, not a module under bin/lib. The
remote matrix run caught the original choice — a committed .cjs there ships
~120KB of CONTEXT.md prose into a runtime module, and two content guards
fired truthfully on it (a leaked .claude install path, and four hardcoded
package-name literals). Neither guard was allowlisted; the artifact moved
instead, mirroring docs/INVENTORY-MANIFEST.json. Nothing at runtime needs to
require it — it is a drift-detection artifact, so the selector parses
CONTEXT.md live and is always current.

Generator: adds a frozen REASON enum and --check --json so the gate's
outcome is asserted structurally instead of by matching prose, and
--context-path/--index-path so tests drive the real CLI against a temp tree
with no filesystem monkeypatching.

Selector: gsd_run query context-predicates with --class/--prefix/--contains,
structured output carrying a matched count, own-property guards, and no
project-root resolution. Registering it exposed that the query dispatch
table and the usage string had drifted: a new parity test found 20 routed
commands missing from the usage list, all added here rather than deferred.

Refs #1671

* test(#2928): lock the newly-public scanFencedBlocks contract

Exporting scanFencedBlocks made it public API for the first time, so it
needs its own contract test independent of the consumer that motivated the
export. Memtrace's co-change analysis flagged the gap: this suite changes
together with markdown-sectionizer.cts 8 times in 90 days and was absent
from the diff.

Covers the documented rules: 0-based indices, -1 for an unterminated fence,
the same-char/>=length/no-trailing-text closer rule, a shorter fence inside
a longer one staying content, CommonMark 4.5 backtick-in-info-string, and
<=3-space indent tolerance.

Refs #1671

* fix(#2928): address both isolated review passes

Two independent reviewers (correctness axis and security axis, neither the
author) found seven findings. All are fixed here with regression tests; none
deferred.

BLOCKER — comment-blind fence scanning caused silent, permanent predicate
loss. The HTML-comment scan and the fence scan ran as two independent passes,
and the fence scanner is comment-blind, so a fence delimiter inside an HTML
comment with no later close read as an unterminated fence and skipped every
remaining line to EOF. Worse, the drift-guard could not catch it: it diffs
against a baseline produced by the same corrupted parse. The two constructs
now interleave in a single pass so each suppresses the other's boundary
detection while active, covered in both directions. The parity suite still
binds this scanner to markdown-sectionizer's for comment-free documents, so
the two cannot diverge unnoticed.

BLOCKER — the selector was not consumed anywhere, leaving the phase's
acceptance criterion unmet. Now wired into the pre-work predicate-citation
step in contributor-standards, which is the repo's actual brief-assembly
path; no code-level brief assembler exists to wire into.

MAJOR — ReDoS with an unauthenticated CI-hang exploit. The predicate-id
regex nested a dot-containing character class inside a dot-prefixed repeat,
so N consecutive dots had exponentially many partitions: 40 dots took 565ms
and growth was exponential. CI runs this parser over a pull request's own
CONTEXT.md, so any contributor could have hung a shared runner with one
line. Replaced with linear per-segment validation. Doubled-dot ids are now
rejected; the real document contains none.

MAJOR — the duplicate-id gate had only ever been proven on synthetic
fixtures. A test now re-inserts the exact line this branch removed and
asserts the real generator names it.

MAJOR — --check together with --write silently let write win, turning the
gate into a writer; a missing path value resolved to the cwd and leaked an
EISDIR stack trace. Both are now clean usage errors.

MINOR — the hoisted skip-list was exported as a live mutable Set; replaced
with a read-only predicate. MINOR — flag-shaped selector values were
unmatchable; the inline --flag=value form now provides the escape hatch.

Refs #1671

* chore(#2928): backfill changeset PR number 2938

---------

Co-authored-by: sim <sim@local>
This commit is contained in:
Tom Boucher
2026-07-31 13:17:01 -04:00
committed by GitHub
parent c043f2946c
commit 05b170e448
22 changed files with 5268 additions and 61 deletions

View File

@@ -3763,3 +3763,78 @@ describe('#2279: map-codebase date stamp instructions overwrite existing dates',
'workflow must instruct agents to overwrite existing dates, not just replace [YYYY-MM-DD] placeholders');
});
});
// ─────────────────────────────────────────────────────────────────────────────
// DEFECT.GENERATIVE-FIX parity guard: HOST_COMMAND_ROUTERS vs TOP_LEVEL_USAGE
// vs SKIP_ROOT_RESOLUTION (#2928 S9)
//
// gsd-tools.cjs's query-command surface is declared across THREE
// independently hand-maintained sites in the same file with no prior parity
// gate between them: the dispatch table (HOST_COMMAND_ROUTERS), the
// `--help` command list (TOP_LEVEL_USAGE), and the project-root-skip list
// (SKIP_ROOT_RESOLUTION). Nothing previously caught a command being wired
// into the dispatch table but omitted from the help string (or vice versa)
// — exactly the generative-fix-divergence shape CLAUDE.md's
// "Generative Fix Divergence" anti-pattern names ("add a parity assertion
// test that fails if the shared constants/arrays/parsers diverge").
//
// This is a STRUCTURAL comparison against the exported constants, not a
// source-text/string-match test, so it stays correct across reformatting
// and is immune to the no-source-grep concern.
// ─────────────────────────────────────────────────────────────────────────────
describe('gsd-tools.cjs dispatch/help/skip-list parity (DEFECT.GENERATIVE-FIX, #2928 S9)', () => {
const { HOST_COMMAND_ROUTERS, TOP_LEVEL_USAGE, skipsRootResolution } = require('../gsd-core/bin/gsd-tools.cjs');
// Parse the "Commands: a, b, c\n\nGlobal flags:" line out of the usage
// string rather than hardcoding a copy of it here — this test must fail
// when the two sites diverge, not silently pass because it re-embeds its
// own stale expectation.
function parseHelpCommandNames(usage) {
const match = usage.match(/Commands: ([\s\S]*?)\n\nGlobal flags:/);
assert.ok(match, 'TOP_LEVEL_USAGE must contain a "Commands: ...\\n\\nGlobal flags:" block');
return match[1]
.split(',')
.map((s) => s.trim())
.filter(Boolean);
}
test('every HOST_COMMAND_ROUTERS entry is listed in the --help command string', () => {
const helpNames = new Set(parseHelpCommandNames(TOP_LEVEL_USAGE));
const missing = Object.keys(HOST_COMMAND_ROUTERS).filter((name) => !helpNames.has(name));
assert.deepEqual(
missing,
[],
`command(s) registered in HOST_COMMAND_ROUTERS but missing from TOP_LEVEL_USAGE's ` +
`"Commands:" list: ${missing.join(', ')}`,
);
});
test('context-predicates is registered in all three hand-maintained sites', () => {
// Concrete regression pin for the command this parity test was added
// alongside (#2928 S9) — a generic diff-based assertion alone would not
// fail if ALL THREE sites were missing an entry simultaneously.
assert.ok(
Object.prototype.hasOwnProperty.call(HOST_COMMAND_ROUTERS, 'context-predicates'),
'context-predicates must be registered in HOST_COMMAND_ROUTERS',
);
assert.ok(
parseHelpCommandNames(TOP_LEVEL_USAGE).includes('context-predicates'),
'context-predicates must be listed in TOP_LEVEL_USAGE',
);
assert.ok(
skipsRootResolution('context-predicates'),
'context-predicates must be in SKIP_ROOT_RESOLUTION (it is a pure repo-root CONTEXT.md ' +
'read, like prompt-budget, and must work with no .planning/ directory present)',
);
});
test('SKIP_ROOT_RESOLUTION is not exported as a mutable live Set (DEFECT.MUTABLE-EXPORTED-SET, #2928)', () => {
const gsdTools = require('../gsd-core/bin/gsd-tools.cjs');
assert.equal(
gsdTools.SKIP_ROOT_RESOLUTION,
undefined,
'the live Set must not be exported directly — only the read-only skipsRootResolution() predicate',
);
assert.equal(typeof gsdTools.skipsRootResolution, 'function');
});
});

View File

@@ -0,0 +1,278 @@
'use strict';
/**
* Integration tests for `gsd_run query context-predicates` — the selector
* surface for the CONTEXT.md predicate fact-store (ADR-1671 S9, #2928 Phase
* 1, rows G1-G17).
*
* NET-NEW COMMAND, EXPECTED RED: `context-predicates` is not yet registered
* in gsd-core/bin/gsd-tools.cjs's HOST_COMMAND_ROUTERS / TOP_LEVEL_USAGE /
* SKIP_ROOT_RESOLUTION (S9's three hand-maintained sites). Every test in this
* file targets the REQUIRED behavior from 40-design.md rows 38-43 and
* currently fails because the command does not exist — that is expected and
* correct for this commit (a failing-first regression matrix), not a
* defect in the test.
*
* Invocation shape: `gsd_run query <command> [args]` maps to
* `node gsd-tools.cjs query <command> [args]` — `query` is a meta-prefix the
* dispatcher strips (gsd-core/bin/gsd-tools.cjs, "Accept `query` as a
* meta-prefix"). This is the same shape every existing query consumer in
* gsd-core/workflows/*.md uses (e.g. `gsd_run query stats.json`,
* `gsd_run query prompt-budget ...`) — mirrored here since no real caller of
* `context-predicates` is wired yet (net-new capability).
*
* Structured assertions: success/failure is asserted via exit code, and via
* `--json-errors` (a real, already-shipped global gsd-tools flag) parsed as
* `{ok, reason, message}` — `reason` is compared against the frozen
* ERROR_REASON enum (gsd-core/bin/lib/io.cjs), never a substring match on
* `message` prose. On success, output is asserted via JSON.parse of stdout
* (gsd-tools' shared `output()` helper always serializes JSON to stdout
* unless --raw is passed) — never a stdout regex.
*/
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { runGsdTools, createTempDir, cleanup } = require('./helpers.cjs');
const { ERROR_REASON } = require('../gsd-core/bin/lib/io.cjs');
const { parsePredicates, selectPredicates } = require('../gsd-core/bin/lib/context-predicates.cjs');
const ROOT = path.resolve(__dirname, '..');
const STACK_FRAME_RE = /\n\s+at\s+\S+\s+\(.*:\d+:\d+\)/;
function queryContextPredicates(args, cwd = ROOT, env = {}) {
return runGsdTools(['query', 'context-predicates', ...args], cwd, env);
}
function queryContextPredicatesJsonErrors(args, cwd = ROOT) {
const r = queryContextPredicates([...args, '--json-errors'], cwd);
let parsedError = null;
try {
parsedError = JSON.parse(r.error);
} catch {
// r.error was not JSON (e.g. a resource-starvation message) — leave null.
}
return { ...r, parsedError };
}
// Independently computed expectation set, from the real CONTEXT.md, via the
// exported pure parser/selector — NOT by parsing the CLI's own rendered text.
const REAL_PREDICATES = parsePredicates(fs.readFileSync(path.join(ROOT, 'CONTEXT.md'), 'utf8')).predicates;
describe('gsd_run query context-predicates (G)', () => {
test('queryReturnsPredicatesForClass', () => {
const expected = selectPredicates(REAL_PREDICATES, { klass: 'META' });
assert.ok(expected.length > 0, 'fixture sanity: META class must exist in the real CONTEXT.md');
const r = queryContextPredicates(['--class', 'META']);
assert.equal(r.success, true, 'query --class META must succeed once the command is wired');
const parsed = JSON.parse(r.output);
const ids = (Array.isArray(parsed) ? parsed : parsed.predicates).map((p) => p.id).sort();
assert.deepEqual(ids, expected.map((p) => p.id).sort());
});
test('queryReturnsPredicatesForDottedPrefix', () => {
const expected = selectPredicates(REAL_PREDICATES, { prefix: 'RULESET.TESTS' });
assert.ok(expected.length > 0, 'fixture sanity: RULESET.TESTS.* must exist in the real CONTEXT.md');
const r = queryContextPredicates(['--prefix', 'RULESET.TESTS']);
assert.equal(r.success, true);
const parsed = JSON.parse(r.output);
const ids = (Array.isArray(parsed) ? parsed : parsed.predicates).map((p) => p.id).sort();
assert.deepEqual(ids, expected.map((p) => p.id).sort());
});
test('queryReturnsPredicatesForFreeTextContains', () => {
const expected = selectPredicates(REAL_PREDICATES, { contains: 'changeset' });
assert.ok(expected.length > 0, 'fixture sanity: "changeset" must appear in some real predicate id/value');
const r = queryContextPredicates(['--contains', 'changeset']);
assert.equal(r.success, true);
const parsed = JSON.parse(r.output);
const ids = (Array.isArray(parsed) ? parsed : parsed.predicates).map((p) => p.id).sort();
assert.deepEqual(ids, expected.map((p) => p.id).sort());
});
test('queryReportsZeroMatchesStructurally', () => {
const r = queryContextPredicates(['--class', 'ZZZ-NO-SUCH-CLASS-EXISTS']);
assert.equal(r.success, true, 'a no-match query is not itself a failure');
const parsed = JSON.parse(r.output);
assert.equal(parsed.matched, 0, 'a no-match result must be structurally distinguishable ({matched: 0}), not silent success or failure');
});
test('queryWithoutSelectorExitsWithUsage', () => {
const r = queryContextPredicatesJsonErrors([]);
assert.equal(r.success, false);
assert.notEqual(r.exitCode, 0);
assert.ok(r.parsedError, 'failure must be structured JSON under --json-errors');
assert.equal(r.parsedError.reason, ERROR_REASON.USAGE, 'missing selector must be a usage error, not an internal/unknown-command error');
});
test('queryWithEmptyClassExitsNonZero', () => {
const r = queryContextPredicatesJsonErrors(['--class', '']);
assert.equal(r.success, false);
assert.equal(r.parsedError && r.parsedError.reason, ERROR_REASON.USAGE);
});
test('queryWithWhitespaceOnlyClassExitsNonZero', () => {
const r = queryContextPredicatesJsonErrors(['--class', ' ']);
assert.equal(r.success, false);
assert.equal(r.parsedError && r.parsedError.reason, ERROR_REASON.USAGE);
});
test('queryWithDuplicateClassFlagsResolvesDeterministically', () => {
const first = queryContextPredicates(['--class', 'META', '--class', 'RULESET']);
const second = queryContextPredicates(['--class', 'META', '--class', 'RULESET']);
assert.equal(first.exitCode, second.exitCode, 'duplicate flags must resolve the same way on every invocation');
assert.equal(first.output, second.output, 'duplicate-flag resolution must be deterministic, not order-of-parse-dependent');
});
test('queryWithConflictingSelectorsAppliesDocumentedPrecedence', () => {
const r = queryContextPredicates(['--class', 'META', '--prefix', 'RULESET.TESTS']);
assert.doesNotMatch(r.error || '', STACK_FRAME_RE, 'conflicting selectors must resolve via documented precedence, never crash');
const repeat = queryContextPredicates(['--class', 'META', '--prefix', 'RULESET.TESTS']);
assert.equal(r.exitCode, repeat.exitCode, 'precedence resolution must be deterministic');
});
test('queryWithMalformedAssignmentExitsNonZero', () => {
const eq = queryContextPredicatesJsonErrors(['--class=']);
assert.equal(eq.success, false);
assert.equal(eq.parsedError && eq.parsedError.reason, ERROR_REASON.USAGE);
const doubleEq = queryContextPredicatesJsonErrors(['--class==A']);
assert.equal(doubleEq.success, false);
assert.equal(doubleEq.parsedError && doubleEq.parsedError.reason, ERROR_REASON.USAGE);
});
test('queryTreatsFlagLikeValueAsMissingValue', () => {
const r = queryContextPredicatesJsonErrors(['--class', '--weird']);
assert.equal(r.success, false, '--weird must not be silently consumed as the --class value');
assert.equal(r.parsedError && r.parsedError.reason, ERROR_REASON.USAGE);
});
// #2928 review finding C: a flag-shaped selector value (e.g. searching CONTEXT.md
// for the literal text "--since") was previously unmatchable — the space-separated
// form always reads a following `--...` token as a missing value, with no escape hatch.
describe('inline-assignment escape hatch for flag-shaped values (#2928 finding C)', () => {
test('contains=value form matches a flag-shaped selector value', () => {
const expected = selectPredicates(REAL_PREDICATES, { contains: '--since' });
assert.ok(expected.length > 0, 'fixture sanity: "--since" must appear in a real CONTEXT.md predicate value');
const r = queryContextPredicates(['--contains=--since']);
assert.equal(r.success, true, '--contains=--since must be accepted, not read as an unknown flag');
const parsed = JSON.parse(r.output);
const ids = parsed.predicates.map((p) => p.id).sort();
assert.deepEqual(ids, expected.map((p) => p.id).sort());
});
test('space-separated form still treats a flag-shaped value as missing (no escape without =)', () => {
const r = queryContextPredicatesJsonErrors(['--contains', '--since']);
assert.equal(r.success, false, '--contains --since (space-separated) must remain a usage error');
assert.equal(r.parsedError && r.parsedError.reason, ERROR_REASON.USAGE);
});
test('contains= with an empty value is still rejected', () => {
const r = queryContextPredicatesJsonErrors(['--contains=']);
assert.equal(r.success, false);
assert.equal(r.parsedError && r.parsedError.reason, ERROR_REASON.USAGE);
});
test('contains== (double-equals) is still rejected, not accepted as literal "=x"', () => {
const r = queryContextPredicatesJsonErrors(['--contains==x']);
assert.equal(r.success, false);
assert.equal(r.parsedError && r.parsedError.reason, ERROR_REASON.USAGE);
});
test('class=/prefix= inline-assignment form also works (not contains-only)', () => {
const expected = selectPredicates(REAL_PREDICATES, { klass: 'META' });
const r = queryContextPredicates(['--class=META']);
assert.equal(r.success, true);
const parsed = JSON.parse(r.output);
assert.deepEqual(
parsed.predicates.map((p) => p.id).sort(),
expected.map((p) => p.id).sort(),
);
});
});
test('queryRejectsPrototypePollutingSelectorKeys', () => {
for (const hostile of ['__proto__', 'constructor', 'prototype']) {
const r = queryContextPredicates(['--class', hostile]);
assert.equal(r.success, true, `${hostile} must be treated as an ordinary (non-matching) class name, not crash`);
const parsed = JSON.parse(r.output);
assert.equal(parsed.matched, 0);
assert.equal(Object.prototype.toString.call({}), '[object Object]', 'sanity: global Object.prototype must be untouched');
}
});
test('queryDoesNotInterpolateShellMetacharacters', () => {
const markerPath = path.join(ROOT, '.gsd-test-shell-injection-marker-2928');
assert.equal(fs.existsSync(markerPath), false, 'fixture sanity: marker must not pre-exist');
const hostileValue = `; touch ${markerPath} #`;
const r = queryContextPredicates(['--contains', hostileValue]);
assert.equal(fs.existsSync(markerPath), false, 'a shell metacharacter payload must never be interpolated into a shell');
assert.doesNotMatch(r.error || '', STACK_FRAME_RE);
});
test('queryHandlesVeryLongSelectorValue', () => {
const longValue = 'x'.repeat(32 * 1024);
const r = queryContextPredicates(['--contains', longValue]);
assert.equal(typeof r.exitCode, 'number', 'a 32K selector value must not hang or crash the process');
assert.doesNotMatch(r.error || '', STACK_FRAME_RE);
});
test('queryHandlesUnicodeSelectorValue', () => {
const unicodeValue = String.fromCodePoint(0x9884, 0x6d4b, 0x5909, 0x6570, 0x1f525);
const r = queryContextPredicates(['--contains', unicodeValue]);
assert.equal(typeof r.exitCode, 'number', 'a Unicode selector value must not crash the process');
assert.doesNotMatch(r.error || '', STACK_FRAME_RE);
});
test('queryWorksFromSubdirectoryWithoutPlanningDir', (t) => {
const tmp = createTempDir('gci-query-subdir-');
t.after(() => cleanup(tmp));
const r = queryContextPredicates(['--class', 'META'], tmp);
assert.equal(r.success, true, 'the read-only selector must not require a .planning/ directory');
});
test('queryFailureOutputContainsNoStackTrace', () => {
const r = queryContextPredicates([]);
assert.equal(r.success, false);
assert.doesNotMatch(r.error || '', STACK_FRAME_RE, 'no bare stack trace in non-debug failure output');
});
});
// allow-test-rule: source-text-is-the-product #2928
// Wiring check for #2928's acceptance criterion — "an agent/orchestrator can invoke the
// selector through a wired `gsd_run query` surface to assemble a brief". Before this, nothing
// in the repo called `context-predicates`; it was a stranded CLI. docs/contributor-standards.md
// §"Pre-work requirements" is the real, tested brief-assembly site governing how an AI-agent
// prompt must cite CONTEXT.md's META.RULE predicates — this asserts it routes through the
// selector instead of a grep-by-eye read, and that the selector actually returns the predicate
// set that site names.
describe('context-predicates wired into a real brief-assembly site (#2928)', () => {
const STANDARDS_DOC = path.join(ROOT, 'docs', 'contributor-standards.md');
const standardsText = fs.readFileSync(STANDARDS_DOC, 'utf8');
test('contributorStandardsRoutesPredicateCitationThroughTheQuerySelector', () => {
assert.ok(
standardsText.includes('node gsd-tools.cjs query context-predicates --class'),
'docs/contributor-standards.md must invoke the context-predicates selector rather than instructing a grep-by-eye read'
);
assert.ok(
standardsText.includes('META.RULE.brief-must-cite-doc') && standardsText.includes('META.RULE.brief-no-paraphrase'),
'the wiring site must name the predicates it expects the selector to surface'
);
});
test('theSelectorActuallyReturnsThePredicatesTheWiringSiteNames', () => {
const r = queryContextPredicates(['--class', 'META']);
assert.equal(r.success, true);
const parsed = JSON.parse(r.output);
const ids = parsed.predicates.map((p) => p.id);
assert.ok(ids.includes('META.RULE.brief-must-cite-doc'));
assert.ok(ids.includes('META.RULE.brief-no-paraphrase'));
});
});

View File

@@ -0,0 +1,228 @@
'use strict';
/**
* Property-based tests for src/context-predicates.cts (compiled to
* gsd-core/bin/lib/context-predicates.cjs).
*
* Document-shaped generators (CONTRIBUTING.md "Fixture provenance #2371"):
* these generators build arbitrary markdown documents out of prose lines,
* fences of varying tick-length, list items with varying markers, and
* predicate-shaped / predicate-lookalike lines — they are NOT seeded from
* this module's own writer/serializer. Seeding a property generator from the
* code under test's own render function make the document shape a constant
* and the property unable to fail; see 50-test-matrix.md Step 1.
*
* Deterministic per CONTRIBUTING.md: seed and numRuns are pinned by
* tests/helpers/fast-check-setup.cjs (seed 42, numRuns 200); failures print
* replay data via fast-check's own counterexample + seed reporting.
*/
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const fc = require('./helpers/fast-check-setup.cjs');
const { parsePredicates, selectPredicates, buildIndex } = require('../gsd-core/bin/lib/context-predicates.cjs');
// ─── Document-shaped generators ────────────────────────────────────────────
// A predicate-shaped line: `CLASS.subkey=value` in one of the recognized
// declaration forms. `asDeclared` controls whether the caller wants this
// specific fixture to be a genuinely-recognized declaration (bare or `- `
// list item — the two forms even today's implementation accepts) so property
// H1/H4 can reason about "predicates the parser actually extracts" without
// depending on the A4-A7 defects under test elsewhere.
const idClassArb = fc.stringMatching(/^[A-Z][A-Z0-9_-]{0,8}$/);
const idSubkeyArb = fc.stringMatching(/^[A-Za-z0-9_-]{1,8}$/);
const valueArb = fc.stringMatching(/^[A-Za-z0-9 _.-]{1,20}$/);
const declaredPredicateLineArb = fc
.tuple(idClassArb, fc.option(idSubkeyArb, { nil: undefined }), valueArb, fc.boolean())
.map(([klass, subkey, value, asListItem]) => {
const id = subkey ? `${klass}.${subkey}` : klass;
const inner = `\`${id}=${value}\``;
return { text: asListItem ? `- ${inner}` : inner, id, klass, value };
});
// A prose line that is NOT a predicate declaration: plain text, a heading, a
// blockquote line, a table row, or an inline mid-prose backtick reference.
const proseLineArb = fc.oneof(
fc.stringMatching(/^[A-Za-z0-9 .,'"()-]{0,40}$/),
idClassArb.map((k) => `# ${k} heading`),
idClassArb.map((k) => `> quoting ${k}`),
idClassArb.map((k) => `| cell | \`${k}.x=y\` |`),
declaredPredicateLineArb.map((p) => `see ${p.text.replace(/^- /, '')} for details`),
);
const fenceTickArb = fc.constantFrom('```', '~~~', '````');
// A whole document assembled from a mix of prose lines and declared
// predicate lines, optionally wrapping a contiguous run in a fence.
function documentArb() {
return fc
.array(fc.oneof({ arbitrary: declaredPredicateLineArb, weight: 2 }, { arbitrary: proseLineArb, weight: 3 }), {
minLength: 0,
maxLength: 12,
})
.map((items) => items);
}
// ─── H1: index preserves every parsed predicate ────────────────────────────
describe('property: parse <-> index contract', () => {
test('indexPreservesEveryParsedPredicate', () => {
fc.assert(
fc.property(documentArb(), (items) => {
const md = items.map((it) => (typeof it === 'string' ? it : it.text)).join('\n');
const parsed = parsePredicates(md);
const index = buildIndex(parsed.predicates);
assert.equal(index.count, parsed.predicates.length);
const parsedIds = parsed.predicates.map((p) => p.id).sort();
const indexIds = index.predicates.map((p) => p.id).sort();
assert.deepEqual(indexIds, parsedIds, 'buildIndex must invent or lose no predicate id');
for (const p of parsed.predicates) {
const inIndex = index.predicates.find((ip) => ip.id === p.id && ip.value === p.value);
assert.ok(inIndex, `predicate ${p.id}=${p.value} from the parse must appear in the index`);
}
}),
);
});
// ─── H2: buildIndex is deterministic and order-independent ────────────────
test('indexSerializationIsOrderIndependentAndDeterministic', () => {
fc.assert(
fc.property(
fc.array(declaredPredicateLineArb, { minLength: 0, maxLength: 10 }),
(decls) => {
// Dedupe by id so this property is not entangled with duplicate
// semantics (covered separately by the E-row unit tests) — the
// property under test here is pure ordering independence.
const seen = new Set();
const uniqueDecls = decls.filter((d) => (seen.has(d.id) ? false : (seen.add(d.id), true)));
const forwardMd = uniqueDecls.map((d) => d.text).join('\n');
const reverseMd = uniqueDecls
.slice()
.reverse()
.map((d) => d.text)
.join('\n');
const forwardIndex = buildIndex(parsePredicates(forwardMd).predicates);
const reverseIndex = buildIndex(parsePredicates(reverseMd).predicates);
assert.deepEqual(forwardIndex, reverseIndex, 'index must not depend on source declaration order');
// Determinism: building twice from the same parsed predicates must
// produce byte-identical JSON serialization.
const parsed = parsePredicates(forwardMd);
const a = JSON.stringify(buildIndex(parsed.predicates));
const b = JSON.stringify(buildIndex(parsed.predicates));
assert.equal(a, b);
},
),
);
});
// ─── H3: fencing a region never increases the predicate count ─────────────
test('fencingRegionNeverIncreasesPredicateCount', () => {
fc.assert(
fc.property(
fc.array(declaredPredicateLineArb, { minLength: 1, maxLength: 6 }),
fc.nat({ max: 5 }),
fenceTickArb,
(decls, wrapAt, fence) => {
const lines = decls.map((d) => d.text);
const before = parsePredicates(lines.join('\n')).predicates.length;
const cut = Math.min(wrapAt, lines.length);
const fenced = [...lines.slice(0, cut), fence, ...lines.slice(cut), fence].join('\n');
const after = parsePredicates(fenced).predicates.length;
assert.ok(after <= before, `fencing must never increase the parsed count (before=${before}, after=${after})`);
},
),
);
});
});
// ─── H5: comment/fence mutual precedence (DEFECT.CONTEXT-PREDICATES-COMMENT-
// FENCE-BLIND, #2928 review) — a predicate genuinely OUTSIDE a wrapper
// (comment or fence) is always parsed live, and a predicate genuinely INSIDE
// it is never parsed live, even when the wrapper's own content contains
// tokens that LOOK LIKE the OTHER construct (fence delimiters inside a
// comment, or comment tokens inside a fence — the two directions the
// interleaved single-pass in `computeSkippedLineFlags` must both get right).
// Document-shaped generator: wraps a marked "inside" predicate between an
// open/close pair of ONE kind, salted with lookalike noise from the OTHER
// kind, with unrelated real predicates before/after the wrapper. ──────────
// Noise lines that look like the OTHER construct's tokens, keyed by which
// kind is being used as the OUTER wrapper for a given run.
const FENCE_NOISE_LINES = ['<!-- not a real comment, just fence content', 'text mentioning --> mid-line', '<!-- nested-looking'];
const COMMENT_NOISE_LINES = ['```', '~~~', '``` info string'];
const wrapperKindArb = fc.constantFrom('fence', 'comment');
describe('property: comment/fence mutual precedence', () => {
test('predicateOutsideWrapperIsAlwaysLiveAndInsideIsNeverLive', () => {
fc.assert(
fc.property(
wrapperKindArb,
fc.array(fc.constantFrom(0, 1, 2), { minLength: 0, maxLength: 3 }),
fc.array(fc.constantFrom(0, 1, 2), { minLength: 0, maxLength: 3 }),
(wrapperKind, beforeNoiseIdx, afterNoiseIdx) => {
const noisePool = wrapperKind === 'fence' ? FENCE_NOISE_LINES : COMMENT_NOISE_LINES;
const open = wrapperKind === 'fence' ? '```' : '<!-- wrapper open';
const close = wrapperKind === 'fence' ? '```' : '-->';
const lines = [
'`OUTSIDE_BEFORE=1`',
open,
...beforeNoiseIdx.map((i) => noisePool[i]),
'`INSIDE_MARKER=2`',
...afterNoiseIdx.map((i) => noisePool[i]),
close,
'`OUTSIDE_AFTER=3`',
];
const md = lines.join('\n');
const r = parsePredicates(md);
const ids = new Set(r.predicates.map((p) => p.id));
assert.ok(ids.has('OUTSIDE_BEFORE'), `OUTSIDE_BEFORE must always be live (wrapper=${wrapperKind})`);
assert.ok(ids.has('OUTSIDE_AFTER'), `OUTSIDE_AFTER must always be live (wrapper=${wrapperKind})`);
assert.ok(
!ids.has('INSIDE_MARKER'),
`INSIDE_MARKER must never be live inside a real ${wrapperKind}, even with ${
wrapperKind === 'fence' ? 'comment' : 'fence'
}-lookalike noise around it`,
);
},
),
);
});
});
describe('property: selectPredicates subset invariant', () => {
test('selectorReturnsOnlyMatchingSubset', () => {
fc.assert(
fc.property(fc.array(declaredPredicateLineArb, { minLength: 0, maxLength: 10 }), idClassArb, (decls, klass) => {
const md = decls.map((d) => d.text).join('\n');
const all = parsePredicates(md).predicates;
const selected = selectPredicates(all, { klass });
for (const p of selected) {
assert.ok(
all.includes(p),
'every selected predicate must be a reference from the original array (subset, not a copy with invented members)',
);
assert.equal(p.klass, klass, `selectPredicates({klass}) must only return predicates whose klass === ${klass}`);
}
}),
);
});
});

View File

@@ -0,0 +1,684 @@
'use strict';
/**
* Unit tests for src/context-predicates.cts (compiled to
* gsd-core/bin/lib/context-predicates.cjs) — the CONTEXT.md predicate
* fact-store parser/validator/selector (ADR-1671, #2928 Phase 1).
*
* FAILING-FIRST: this file targets the REQUIRED production behavior from
* .gsd/phase/chore-2928-context-predicate-store/40-design.md's behavior
* table, not today's prototype-carried-forward behavior. Eight row-groups
* are measured, provable defects in the current implementation and MUST be
* RED until a later commit fixes them: A4, A5, A6, A7 (indented-bare / `*` /
* `+` / numbered-list declaration forms are dropped), B2 (tilde fence not
* skipped), B3 (4-backtick fence containing a 3-backtick line mis-toggles),
* B5 (multi-line HTML comment not skipped).
*
* Fixture provenance (CONTRIBUTING.md #2371): the must-NOT-parse corpus
* (rows A8-A11, negative space, I4) is drawn from real repo documents that
* predate/ignore this grammar — the real CONTEXT.md and the real
* CONTRIBUTING.md — never hand-authored from the grammar spec itself.
*/
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const {
parsePredicates,
selectPredicates,
buildIndex,
} = require('../gsd-core/bin/lib/context-predicates.cjs');
const { scanFencedBlocks } = require('../gsd-core/bin/lib/markdown-sectionizer.cjs');
const ROOT = path.resolve(__dirname, '..');
const CRLF = '\r\n';
// ─── A. Declaration recognition ───────────────────────────────────────────
describe('parsePredicates: declaration recognition (A)', () => {
test('parsesBareBacktickDeclaration', () => {
const r = parsePredicates('`FOO=bar`');
assert.equal(r.predicates.length, 1);
assert.deepEqual(
{ id: r.predicates[0].id, klass: r.predicates[0].klass, value: r.predicates[0].value },
{ id: 'FOO', klass: 'FOO', value: 'bar' },
);
});
test('parsesDashListItemDeclaration', () => {
const r = parsePredicates('- `FOO=bar`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].id, 'FOO');
});
test('parsesIndentedDashListItemDeclaration', () => {
const r = parsePredicates(' - `FOO=bar`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].id, 'FOO');
});
test('parsesIndentedBareDeclaration', () => {
// RED (measured defect): the prototype's bare-form check requires column
// 0 (`line.startsWith('`')` on a line whose only trim is trimEnd()), so
// leading whitespace with no list marker is silently dropped today.
const r = parsePredicates(' `FOO=bar`');
assert.equal(r.predicates.length, 1, 'indented bare declaration must be tolerated (Postel honored on shape)');
assert.equal(r.predicates[0].id, 'FOO');
});
test('parsesStarListItemDeclaration', () => {
// RED (measured defect): today's list-item stripper only recognizes `-`.
const r = parsePredicates('* `FOO=bar`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].id, 'FOO');
});
test('parsesPlusListItemDeclaration', () => {
// RED (measured defect): same as `*`, `+` is not recognized today.
const r = parsePredicates('+ `FOO=bar`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].id, 'FOO');
});
test('parsesNumberedListItemDeclaration', () => {
// RED (measured defect): numbered list markers are not recognized today.
const r = parsePredicates('1. `FOO=bar`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].id, 'FOO');
});
test('ignoresInlineReferenceInsideProse', () => {
const r = parsePredicates('see `FOO=bar` for details');
assert.equal(r.predicates.length, 0, 'an inline mid-prose mention is a reference, not a declaration');
});
test('ignoresPredicateShapeInTableCell', () => {
const r = parsePredicates('| x | `FOO=bar` |');
assert.equal(r.predicates.length, 0);
});
test('ignoresPredicateShapeInHeading', () => {
const md = ['# `FOO=bar`', '`REAL.one=value`'].join('\n');
const r = parsePredicates(md);
assert.equal(r.predicates.length, 1, 'the heading itself must never yield a predicate');
assert.equal(r.predicates[0].id, 'REAL.one');
assert.equal(r.predicates[0].section, '`FOO=bar`', 'the heading text (predicate-shaped or not) becomes the tracked section');
});
test('ignoresPredicateShapeInBlockquote', () => {
const r = parsePredicates('> `FOO=bar`');
assert.equal(r.predicates.length, 0);
});
test('tracksNearestPrecedingSectionHeading', () => {
const md = ['# My Section', '`FOO=bar`'].join('\n');
const r = parsePredicates(md);
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].section, 'My Section');
});
});
// ─── B. Fenced / commented regions ────────────────────────────────────────
describe('parsePredicates: fenced/commented regions (B)', () => {
test('ignoresDeclarationInsideBacktickFence', () => {
const md = ['```', '`FOO=bar`', '```'].join('\n');
const r = parsePredicates(md);
assert.equal(r.predicates.length, 0);
});
test('ignoresDeclarationInsideTildeFence', () => {
// RED (measured defect): the naive toggle only matches triple-backtick lines.
const md = ['~~~', '`FOO=bar`', '~~~'].join('\n');
const r = parsePredicates(md);
assert.equal(r.predicates.length, 0, 'a tilde fence must skip its contents exactly like a backtick fence');
});
test('ignoresDeclarationInsideLongerFenceContainingShorterFence', () => {
// RED (measured defect): a naive backtick-count-agnostic toggle mis-flips
// on the inner 3-backtick line and un-skips the remainder.
const md = ['````', '```', '`FOO=bar`', '```', '````'].join('\n');
const r = parsePredicates(md);
assert.equal(r.predicates.length, 0, 'fence-length awareness must prevent the inner fence from un-skipping the outer one');
});
test('ignoresDeclarationInsideLanguageTaggedFence', () => {
const md = ['```bash', '`FOO=bar`', '```'].join('\n');
const r = parsePredicates(md);
assert.equal(r.predicates.length, 0);
});
test('ignoresDeclarationInsideMultiLineHtmlComment', () => {
// RED (measured defect): the prototype has no HTML-comment awareness at
// all — a predicate-shaped line between `<!--` and `-->` on its own line
// parses as live today.
const md = ['<!--', '`FOO=bar`', '-->'].join('\n');
const r = parsePredicates(md);
assert.equal(r.predicates.length, 0, 'a commented-out predicate must not be read as live');
});
test('ignoresDeclarationInsideSingleLineHtmlComment', () => {
const r = parsePredicates('<!-- `FOO=bar` -->');
assert.equal(r.predicates.length, 0);
});
test('treatsUnclosedFenceAsSkippedToEndOfFile', () => {
const md = ['```', '`FOO=bar`', '`BAZ=qux`'].join('\n');
assert.doesNotThrow(() => parsePredicates(md));
const r = parsePredicates(md);
assert.equal(r.predicates.length, 0, 'everything after an unclosed fence must be treated as skipped, not crash or leak');
});
test('parsesDeclarationInFourSpaceIndentedBlockAsDocumentedLimit', () => {
// Pins the documented limit (design known-limit 1): 4-space indentation
// is NOT treated as a code block, because CONTEXT.md authors real
// predicates as indented list items at that depth.
const r = parsePredicates(' - `FOO=bar`');
assert.equal(r.predicates.length, 1, 'documented limit: 4-space indent is not code, so this must still parse');
assert.equal(r.predicates[0].id, 'FOO');
});
});
// ─── B2. Comment/fence mutual precedence
// (DEFECT.CONTEXT-PREDICATES-COMMENT-FENCE-BLIND — BLOCKER review finding) ──
//
// The HTML-comment scan and the fence scan previously ran as two
// INDEPENDENT passes: `scanFencedBlocks` is comment-blind, so a fence
// delimiter appearing INSIDE an HTML comment (with no later matching close
// in the file) was treated as a real *unterminated* fence — silently
// dropping every remaining predicate to EOF. This is invisible to `--check`
// because `--check` diffs against a baseline generated by the same
// corrupted parse. This suite locks the chosen precedence (module doc
// comment, `computeSkippedLineFlags`): the two constructs are scanned in one
// interleaved pass and mutually suppress each other's open/close detection
// while active.
describe('parsePredicates: comment/fence mutual precedence (B2)', () => {
test('fenceDelimiterInsideCommentWithNoLaterFenceDoesNotSwallowRemainingFile', () => {
// The exact BLOCKER repro: a fence delimiter inside an HTML comment, with
// no later real fence anywhere in the document, must not be treated as
// an unterminated fence — the later real predicate must still parse.
const md = ['<!-- example:', '```', '-->', '`RULESET.REAL.PREDICATE=x`'].join('\n');
const r = parsePredicates(md);
assert.equal(r.predicates.length, 1, 'the fence delimiter inside the comment must not swallow the rest of the file');
assert.equal(r.predicates[0].id, 'RULESET.REAL.PREDICATE');
});
test('fenceDelimiterInsideCommentWithLaterRealFenceStillBehavesCorrectly', () => {
const md = [
'<!-- example:',
'```',
'-->',
'`BEFORE=1`',
'```',
'`INSIDE=2`',
'```',
'`AFTER=3`',
].join('\n');
const r = parsePredicates(md);
const ids = r.predicates.map((p) => p.id);
assert.deepEqual(ids.sort(), ['AFTER', 'BEFORE'], 'the real fence after the comment must still skip its own content');
});
test('arrowInsideRealFencedBlockDoesNotTerminateAnythingAndFenceStillSkipsItsContent', () => {
const md = ['```', '`INSIDE=1`', 'noise line with --> token', '```', '`AFTER=2`'].join('\n');
const r = parsePredicates(md);
const ids = r.predicates.map((p) => p.id);
assert.deepEqual(ids, ['AFTER'], '--> inside real fence content must not open/close a comment; fence must still skip INSIDE');
});
test('commentOpenerInsideRealFencedBlockDoesNotOpenAComment', () => {
const md = ['```', '<!-- looks like a comment opener, but this is fence content', '`INSIDE=1`', '```', '`AFTER=2`'].join(
'\n',
);
const r = parsePredicates(md);
const ids = r.predicates.map((p) => p.id);
assert.deepEqual(ids, ['AFTER'], '<!-- inside real fence content must not open a comment that leaks past the fence close');
});
test('unterminatedCommentAtEofSkipsRemainingLinesWithoutCrashing', () => {
const md = ['`BEFORE=1`', '<!-- unterminated', '`INSIDE=2`', '`ALSOINSIDE=3`'].join('\n');
assert.doesNotThrow(() => parsePredicates(md));
const r = parsePredicates(md);
const ids = r.predicates.map((p) => p.id);
assert.deepEqual(ids, ['BEFORE'], 'an unterminated comment must skip to EOF, not crash and not leak later predicates');
});
test('commentAndFenceTokensOnTheSameLineResolveWithoutInterference', () => {
// A fence-opener line whose CommonMark info string happens to contain a
// full inline HTML comment: the line still starts with backticks, not
// `<!--`, so it opens a real fence (comment text is just info-string
// trailing content) — the fence still closes and skips its own content.
const infoStringMd = ['`BEFORE=1`', '``` <!-- note -->', '`INSIDE=2`', '```', '`AFTER=3`'].join('\n');
const infoStringResult = parsePredicates(infoStringMd);
assert.deepEqual(
infoStringResult.predicates.map((p) => p.id).sort(),
['AFTER', 'BEFORE'],
'a fence opener whose info string contains comment-like text must still open/close as a real fence',
);
// A self-closing single-line HTML comment whose content happens to
// contain a fence-delimiter-shaped token: the line still starts with
// `<!--`, so the whole line is comment content and the embedded
// backticks never open a fence.
const commentMd = ['`BEFORE=1`', '<!-- see ``` for an example -->', '`AFTER=2`'].join('\n');
const commentResult = parsePredicates(commentMd);
assert.deepEqual(
commentResult.predicates.map((p) => p.id).sort(),
['AFTER', 'BEFORE'],
'a single-line comment whose content contains fence-shaped text must not open a fence',
);
});
});
// ─── C. ID / value grammar boundaries ──────────────────────────────────────
describe('parsePredicates: ID/value grammar boundaries (C)', () => {
test('rejectsBacktickContentAtLengthTwo', () => {
// `A=` — backtick content length 2 (limit-1 of the `inner.length > 2` guard).
const r = parsePredicates('`A=`');
assert.equal(r.predicates.length, 0);
});
test('acceptsMinimalBacktickContentAtLengthThree', () => {
// `A=1` — length 3 (limit).
const r = parsePredicates('`A=1`');
assert.equal(r.predicates.length, 1);
assert.deepEqual({ id: r.predicates[0].id, value: r.predicates[0].value }, { id: 'A', value: '1' });
});
test('acceptsBacktickContentAboveMinimumLength', () => {
// `A=12` — length 4 (limit+1).
const r = parsePredicates('`A=12`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].value, '12');
});
test('rejectsEqualsSignAtIndexZero', () => {
// `=value` — eqIdx 0 (limit-1 of the `eqIdx < 1` guard).
const r = parsePredicates('`=value`');
assert.equal(r.predicates.length, 0);
});
test('acceptsEqualsSignAtIndexOne', () => {
// `A=value` — eqIdx 1 (limit).
const r = parsePredicates('`A=value`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].id, 'A');
});
test('acceptsEqualsSignAboveIndexOne', () => {
// `AB=value` — eqIdx 2 (limit+1).
const r = parsePredicates('`AB=value`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].id, 'AB');
});
test('rejectsInlineCodeWithoutEqualsSign', () => {
const r = parsePredicates('`ID`');
assert.equal(r.predicates.length, 0);
});
test('reportsEmptyValueAsMalformedRatherThanDroppingSilently', () => {
const r = parsePredicates('`ID=`');
assert.equal(r.predicates.length, 0);
assert.equal(r.malformed.length, 1, 'an empty value must surface as a diagnostic, not vanish silently');
assert.equal(r.malformed[0].reason, 'empty-value');
});
test('splitsOnFirstEqualsSignOnly', () => {
const r = parsePredicates('`ID=a=b=c`');
assert.equal(r.predicates.length, 1);
assert.deepEqual({ id: r.predicates[0].id, value: r.predicates[0].value }, { id: 'ID', value: 'a=b=c' });
});
test('rejectsLowercaseLeadingIdentifier', () => {
const r = parsePredicates('`foo.bar=x`');
assert.equal(r.predicates.length, 0);
});
test('acceptsLowercaseSubSegments', () => {
const r = parsePredicates('`PRED.k320.rule=x`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].klass, 'PRED');
});
test('acceptsHyphenatedClassSegment', () => {
const r = parsePredicates('`RELEASE-NOTES.x=y`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].klass, 'RELEASE-NOTES');
});
test('rejectsWhitespaceInIdentifier', () => {
const r = parsePredicates('`FOO BAR=x`');
assert.equal(r.predicates.length, 0);
});
test('acceptsSingleSegmentIdentifier', () => {
const r = parsePredicates('`FOO=x`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].klass, r.predicates[0].id);
});
test('preservesTrailingWhitespaceInValueVerbatim', () => {
const r = parsePredicates('`ID=1 `');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].value, '1 ', 'trailing whitespace in the value must not be trimmed');
});
test('acceptsBacktickWithinValue', () => {
const r = parsePredicates('`ID=a`b`');
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].value, 'a`b');
});
test('rejectsPathAndQueryCharactersInIdentifier', () => {
const r = parsePredicates('`A/B?c=d`');
assert.equal(r.predicates.length, 0);
});
test('rejectsDoubledDotEmptySegment', () => {
// DEFECT.CONTEXT-PREDICATES-ID-REDOS (MAJOR review finding): the
// structural id validator (isValidId) splits on '.' and rejects any
// empty segment. This is a deliberate behavior CHANGE vs. the old
// `ID_RE` regex, which accepted `A..b` because `.` was inside the
// subsequent-segment character class. The real repo CONTEXT.md was
// checked (`grep -nE '`[A-Z][A-Za-z0-9_.-]*\\.\\.[A-Za-z0-9_.-]*='
// CONTEXT.md`) and contains ZERO ids with a doubled dot, so this is a
// pure grammar-tightening with no behavior loss against real data —
// pinned here so a future change cannot silently re-loosen it.
const r = parsePredicates('`A..b=value`');
assert.equal(r.predicates.length, 0, 'a doubled dot (empty segment) must be rejected, not silently accepted');
});
test('idGrammarValidationIsLinearTimeAgainstManyConsecutiveDots', () => {
// DEFECT.CONTEXT-PREDICATES-ID-REDOS (MAJOR review finding): the old
// `ID_RE`'s `(?:\.[A-Za-z0-9_.-]+)*` group was exponential in the number
// of consecutive dots (measured: ~565ms for 40 dots). The structural
// per-segment validator is linear. A generous wall-clock bound is used
// only as a smoke check; the load-bearing assertion is that the result
// is a clean rejection (an id-shaped line with 60 consecutive dots has
// an empty segment at every step and must not parse).
const dots = '.'.repeat(60);
const md = `\`A${dots}x=value\``;
const start = Date.now();
const r = parsePredicates(md);
const elapsedMs = Date.now() - start;
assert.equal(r.predicates.length, 0, 'an id with 60 consecutive dots has empty segments and must cleanly reject');
assert.ok(elapsedMs < 1000, `expected well under 1s (linear time), got ${elapsedMs}ms — possible ReDoS regression`);
});
});
// ─── D. CRLF / newline fidelity ────────────────────────────────────────────
describe('parsePredicates: CRLF/newline fidelity (D)', () => {
test('parsesBareDeclarationUnderCrlf', () => {
const r = parsePredicates('`FOO=bar`' + CRLF);
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].id, 'FOO');
});
test('parsesListItemDeclarationUnderCrlf', () => {
const r = parsePredicates('- `FOO=bar`' + CRLF);
assert.equal(r.predicates.length, 1);
});
test('parsesIndentedListDeclarationUnderCrlf', () => {
const r = parsePredicates(' - `FOO=bar`' + CRLF);
assert.equal(r.predicates.length, 1);
});
test('skipsFencedDeclarationUnderCrlf', () => {
const md = ['```', '`FOO=bar`', '```', ''].join(CRLF);
const r = parsePredicates(md);
assert.equal(r.predicates.length, 0);
});
test('skipsBlockquoteDeclarationUnderCrlf', () => {
const r = parsePredicates('> `FOO=bar`' + CRLF);
assert.equal(r.predicates.length, 0);
});
test('tracksSectionAndLineNumbersUnderCrlf', () => {
const md = ['# Section Name', '`FOO=bar`', ''].join(CRLF);
const r = parsePredicates(md);
assert.equal(r.predicates.length, 1);
assert.equal(r.predicates[0].section, 'Section Name');
assert.equal(r.predicates[0].line, 2);
});
test('parsesMixedLfAndCrlfDocument', () => {
const md = '`FOO=bar`' + CRLF + '- `BAZ=qux`' + '\n';
const r = parsePredicates(md);
assert.equal(r.predicates.length, 2);
assert.deepEqual(r.predicates.map((p) => p.id).sort(), ['BAZ', 'FOO']);
});
test('yieldsNoPredicatesForLoneCrDocumentAsDocumentedLimit', () => {
// Pins the documented limit (design known-limit 2): lone-CR-only line
// endings are unsupported; no `\r`-only file exists in this repo.
const md = '`FOO=bar`\r`BAZ=qux`\r';
const r = parsePredicates(md);
assert.equal(r.predicates.length, 0);
});
});
// ─── E. Duplicate detection + validation ───────────────────────────────────
describe('parsePredicates: duplicate detection + validation (E)', () => {
test('reportsDuplicateIdentifierWithDifferentValues', () => {
const md = ['`FOO=a`', '`FOO=b`'].join('\n');
const r = parsePredicates(md);
assert.equal(r.duplicates.length, 1);
assert.deepEqual(r.duplicates[0], { id: 'FOO', count: 2 });
});
test('reportsDuplicateIdentifierEvenWhenValuesAreIdentical', () => {
const md = ['`FOO=a`', '`FOO=a`'].join('\n');
const r = parsePredicates(md);
assert.equal(r.duplicates.length, 1, 'identical-value duplicates must not be silently deduped');
assert.deepEqual(r.duplicates[0], { id: 'FOO', count: 2 });
});
test('reportsDuplicateCountForThreeOccurrences', () => {
const md = ['`FOO=a`', '`FOO=b`', '`FOO=c`'].join('\n');
const r = parsePredicates(md);
assert.equal(r.duplicates.length, 1);
assert.equal(r.duplicates[0].count, 3);
});
test('reportsNoDuplicateForSingleOccurrence', () => {
const r = parsePredicates('`FOO=a`');
assert.equal(r.duplicates.length, 0);
});
test('doesNotCountFenceSkippedOccurrenceAsDuplicate', () => {
const md = ['`FOO=a`', '```', '`FOO=b`', '```'].join('\n');
const r = parsePredicates(md);
assert.equal(r.duplicates.length, 0, 'a fence-skipped occurrence is not a declaration');
assert.equal(r.predicates.length, 1);
});
test('doesNotCountCommentedOccurrenceAsDuplicate', () => {
const md = ['`FOO=a`', '<!-- `FOO=b` -->'].join('\n');
const r = parsePredicates(md);
assert.equal(r.duplicates.length, 0);
assert.equal(r.predicates.length, 1);
});
test('reportsMalformedAndDuplicateDiagnosticsTogether', () => {
const md = ['`FOO=a`', '`FOO=b`', '`BAR=`'].join('\n');
const r = parsePredicates(md);
assert.equal(r.duplicates.length, 1, 'the duplicate path must not be suppressed by the malformed path');
assert.equal(r.malformed.length, 1, 'the malformed path must not be suppressed by the duplicate path');
});
test('realContextMdHasNoDuplicateIdentifiers', () => {
const md = fs.readFileSync(path.join(ROOT, 'CONTEXT.md'), 'utf8');
const r = parsePredicates(md);
assert.equal(r.duplicates.length, 0, 'the real CONTEXT.md must carry no duplicate predicate ids');
});
});
// ─── I. Independence + real-corpus regression ──────────────────────────────
describe('parsePredicates: independence + real-corpus regression (I)', () => {
test('parserHasNoCrossTestSharedState', () => {
// Run in an order that would surface a leaking module-level cache: parse
// a document with a duplicate, then a clean document, then re-parse the
// first — results must be identical each time, regardless of call order.
const dupMd = ['`FOO=a`', '`FOO=b`'].join('\n');
const cleanMd = '`BAR=x`';
const firstPass = parsePredicates(dupMd);
const cleanPass = parsePredicates(cleanMd);
const secondPass = parsePredicates(dupMd);
assert.equal(cleanPass.duplicates.length, 0, 'a clean parse must never see the previous call\'s duplicate');
assert.deepEqual(secondPass.duplicates, firstPass.duplicates, 'repeating the same input must repeat the same result');
assert.equal(secondPass.predicates.length, firstPass.predicates.length);
});
test('realContributingMdYieldsNoPredicates', () => {
// Fixture provenance (#2371): CONTRIBUTING.md contains
// `GITHUB_BASE_REF=next …` (inside a blockquote) and
// `export GSD_BLOCKED_AUTHOR_REGEX='@example-corp\.com$'` (inside a
// fenced bash block) — uppercase, `=`-bearing, backtick-adjacent text
// authored by someone who never heard of this grammar.
const md = fs.readFileSync(path.join(ROOT, 'CONTRIBUTING.md'), 'utf8');
const r = parsePredicates(md);
assert.equal(r.predicates.length, 0, 'a document that never heard of the grammar must yield zero predicates');
});
test('realContextMdParsesFullPredicateSet', () => {
const md = fs.readFileSync(path.join(ROOT, 'CONTEXT.md'), 'utf8');
const r = parsePredicates(md);
assert.equal(r.duplicates.length, 0);
assert.ok(r.predicates.length > 0, 'the real CONTEXT.md must yield a non-empty predicate set');
const classes = new Set(r.predicates.map((p) => p.klass));
assert.ok(classes.size >= 20, `expected >= 20 classes in the live predicate set, got ${classes.size}`);
});
});
// ─── selectPredicates / buildIndex smoke coverage (not in the row matrix,
// but exercised here since they are pure, no-I/O functions covered by unit
// tests per the risk table — the CLI/query surfaces are covered separately). ───
describe('selectPredicates + buildIndex: pure-function smoke coverage', () => {
test('selectPredicates filters by klass/prefix/contains independently', () => {
const r = parsePredicates(['`FOO.a=hello world`', '`FOO.b=other`', '`BAR.a=hello`'].join('\n'));
const byKlass = selectPredicates(r.predicates, { klass: 'FOO' });
assert.equal(byKlass.length, 2);
const byPrefix = selectPredicates(r.predicates, { prefix: 'FOO.a' });
assert.equal(byPrefix.length, 1);
const byContains = selectPredicates(r.predicates, { contains: 'hello' });
assert.equal(byContains.length, 2);
});
test('buildIndex omits the line field from every entry (S5)', () => {
const r = parsePredicates('`FOO=bar`');
const index = buildIndex(r.predicates);
assert.equal(index.predicates.length, 1);
assert.ok(!Object.prototype.hasOwnProperty.call(index.predicates[0], 'line'));
});
});
// ─── Parity: fenced-line determination vs markdown-sectionizer.scanFencedBlocks
// (DEFECT.GENERATIVE-FIX) ───────────────────────────────────────────────────
//
// context-predicates.cts derives its fenced-line skip flags from
// markdown-sectionizer.cts's exported `scanFencedBlocks` seam rather than
// carrying its own copy of the fence state machine. This suite asserts that
// parsePredicates' observable skip/keep decision for every ID-marker line
// agrees with what `scanFencedBlocks` independently reports for that same
// `lines` array, across the fence-shape table below — so a future change to
// the shared scanner cannot silently diverge from predicate parsing.
describe('parsePredicates: fence-skip parity with markdown-sectionizer.scanFencedBlocks', () => {
const BARE_ID_RE = /^`([A-Za-z][A-Za-z0-9._-]*)=/;
function assertFenceParity(name, lines) {
test(name, () => {
const md = lines.join('\n');
const blocks = scanFencedBlocks(lines);
const expectedSkip = new Array(lines.length).fill(false);
for (const block of blocks) {
const end = block.closeLineIdx === -1 ? lines.length - 1 : block.closeLineIdx;
for (let i = block.openLineIdx; i <= end; i++) expectedSkip[i] = true;
}
const r = parsePredicates(md);
const parsedIds = new Set(r.predicates.map((p) => p.id));
let sawMarker = false;
for (let i = 0; i < lines.length; i++) {
const m = BARE_ID_RE.exec(lines[i].trim());
if (!m) continue;
sawMarker = true;
const id = m[1];
if (expectedSkip[i]) {
assert.ok(
!parsedIds.has(id),
`${name}: line ${i} (${id}) is inside a scanFencedBlocks fence and must not be parsed as live`,
);
} else {
assert.ok(
parsedIds.has(id),
`${name}: line ${i} (${id}) is outside any scanFencedBlocks fence and must be parsed as live`,
);
}
}
assert.ok(sawMarker, `${name}: fixture must contain at least one ID marker line`);
});
}
assertFenceParity('3-backtick fence', ['`BEFORE=1`', '```', '`INSIDE=2`', '```', '`AFTER=3`']);
assertFenceParity('3-tilde fence', ['`BEFORE=1`', '~~~', '`INSIDE=2`', '~~~', '`AFTER=3`']);
assertFenceParity('4-backtick fence containing a nested 3-backtick fence', [
'`BEFORE=1`',
'````',
'```',
'`INSIDE=2`',
'```',
'````',
'`AFTER=3`',
]);
assertFenceParity('language-tagged fence', [
'`BEFORE=1`',
'```bash',
'`INSIDE=2`',
'```',
'`AFTER=3`',
]);
assertFenceParity('info string containing a backtick is not a valid opener', [
'`BEFORE=1`',
'``` `evil` ',
'`STILL=2`',
'```',
'`INSIDE=3`',
'```',
'`AFTER=4`',
]);
assertFenceParity('indented (<=3 space) fence', [
'`BEFORE=1`',
' ```',
'`INSIDE=2`',
' ```',
'`AFTER=3`',
]);
assertFenceParity('unterminated fence skips to end of file', [
'`BEFORE=1`',
'```',
'`INSIDE=2`',
'`ALSOINSIDE=3`',
]);
});

View File

@@ -0,0 +1,445 @@
'use strict';
/**
* Integration tests for scripts/gen-context-index.cjs — the CI gate that
* keeps docs/CONTEXT-INDEX.json in sync with the predicates declared in the
* repo-root CONTEXT.md (ADR-1671, #2928 Phase 1, rows F1-F17).
*
* The committed artifact is plain JSON (not a `.cjs` CommonJS module): a
* shipped runtime module is the wrong place for ~120 KB of arbitrary
* CONTEXT.md prose, and embedding it there tripped both
* tests/cline-install.test.cjs (leaked `.claude/hooks/...` path literals) and
* tests/package-name-single-source.test.cjs (hardcoded package-name
* literals) — both true positives against runtime-code content scanning.
* docs/CONTEXT-INDEX.json mirrors docs/INVENTORY-MANIFEST.json's precedent:
* a committed, generated, `--check`-guarded JSON manifest that is not
* runtime code.
*
* Fixture isolation (ADR-1671 Phase 1 commit 3): gen-context-index.cjs now
* accepts `--context-path <p>` / `--index-path <p>` CLI overrides (and the
* same-named parameters on the exported `checkReport`/`buildFreshIndex`
* pure functions), so every test here spawns the real CLI (spawnSync, not an
* engine-direct call — an engine-direct call is false-green for CLI behavior
* per the design's own risk analysis) pointed directly at temp fixture
* files, with NO fs monkeypatching. The prior `--require` preload
* (tests/helpers/gen-context-index-fs-fixture.cjs) redirected two hardcoded
* absolute paths by patching fs.readFileSync/existsSync/writeFileSync — that
* indirection is no longer needed now that the paths are directly
* injectable, and the preload has been deleted.
*
* Prohibited: Raw Text Matching on Test Outputs (CONTRIBUTING.md). This
* generator's `--check --json` mode emits a typed `{ ok, reason, duplicates,
* count, classes }` report — `reason` is always one of the frozen `REASON`
* enum values. Rows F7-F10 assert on `report.reason === REASON.FAIL_X`
* (and, for F7, that `report.duplicates` names the duplicate id) instead of
* exit-code-only / stderr-substring assertions.
*/
const { describe, test, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { execFileSync } = require('node:child_process');
const { createTempDir, cleanup } = require('./helpers.cjs');
const { serializeIndex, buildFreshIndex, checkReport, REASON } = require('../scripts/gen-context-index.cjs');
const ROOT = path.resolve(__dirname, '..');
const SCRIPT = path.join(ROOT, 'scripts', 'gen-context-index.cjs');
const REAL_CONTEXT_PATH = path.join(ROOT, 'CONTEXT.md');
const STACK_FRAME_RE = /\n\s+at\s+\S+\s+\(.*:\d+:\d+\)/;
/**
* Spawn the real gen-context-index.cjs CLI with explicit `--context-path` /
* `--index-path` overrides — no fs monkeypatching, no `--require` preload.
*
* @param {string[]} args - CLI args (e.g. ['--check', '--json']).
* @param {{contextPath?: string, indexPath?: string}} [paths] - absolute
* fixture paths to pass via `--context-path`/`--index-path`. Omit a key to
* leave that seam at its real-repo default (read-only, untouched).
* @returns {{code: number, stdout: string, stderr: string}}
*/
function runGenContextIndex(args, paths = {}) {
const fullArgs = [...args];
if (paths.contextPath !== undefined) fullArgs.push('--context-path', paths.contextPath);
if (paths.indexPath !== undefined) fullArgs.push('--index-path', paths.indexPath);
try {
const stdout = execFileSync(process.execPath, [SCRIPT, ...fullArgs], {
cwd: ROOT,
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'pipe'],
timeout: 30000,
});
return { code: 0, stdout, stderr: '' };
} catch (err) {
return {
code: err.status ?? 1,
stdout: err.stdout ? err.stdout.toString() : '',
stderr: err.stderr ? err.stderr.toString() : '',
};
}
}
/**
* Parse the single JSON line `--check --json` writes to stdout.
*
* @param {string} stdout
* @returns {object}
*/
function parseJsonReport(stdout) {
return JSON.parse(stdout.trim());
}
describe('gen-context-index.cjs REASON enum (three-coordinated-changes lock)', () => {
test('REASON key set is exactly the documented set', () => {
// Locks the documented enum shape (CONTRIBUTING.md three-coordinated-
// changes pattern): adding a reason requires updating this assertion
// too, so the typed surface cannot silently drift from what tests expect.
assert.deepEqual(Object.keys(REASON).sort(), [
'FAIL_CONTEXT_MISSING',
'FAIL_CONTEXT_UNREADABLE',
'FAIL_DUPLICATE_IDS',
'FAIL_INDEX_MISSING',
'FAIL_INDEX_UNPARSEABLE',
'FAIL_LIB_NOT_BUILT',
'FAIL_STALE',
'OK_UP_TO_DATE',
]);
});
test('REASON is frozen', () => {
assert.ok(Object.isFrozen(REASON));
});
});
describe('gen-context-index.cjs --check (F)', () => {
let tmpDir;
beforeEach(() => {
tmpDir = createTempDir('gen-context-index-');
});
afterEach(() => {
cleanup(tmpDir);
});
test('checkExitsZeroWhenIndexIsFresh', () => {
// Read-only against the real, already-fresh repo state — no override
// needed, and nothing is mutated.
const r = runGenContextIndex(['--check']);
assert.equal(r.code, 0);
});
test('checkExitsZeroAfterPureLineShift', () => {
// S5: the committed artifact carries no `line` field, so a pure line
// shift in CONTEXT.md must not perturb the byte-identical serialization.
const real = fs.readFileSync(REAL_CONTEXT_PATH, 'utf8');
const lines = real.split(/\r?\n/);
const shifted = [lines[0], '', '', ...lines.slice(1)].join('\n');
const shiftedPath = path.join(tmpDir, 'CONTEXT-shifted.md');
fs.writeFileSync(shiftedPath, shifted, 'utf8');
const r = runGenContextIndex(['--check'], { contextPath: shiftedPath });
assert.equal(r.code, 0, 'a pure line shift must not fail the gate (Q4 resolution, S5)');
});
test('checkExitsOneWhenPredicateValueChanged', () => {
const real = fs.readFileSync(REAL_CONTEXT_PATH, 'utf8');
const modified = real.replace(
/`RULESET\.PR-SCOPE\.one-concern-per-pr=[^`]*`/,
'`RULESET.PR-SCOPE.one-concern-per-pr=CHANGED VALUE FOR TEST`',
);
assert.notEqual(modified, real, 'fixture setup sanity: the substitution must actually apply');
const modifiedPath = path.join(tmpDir, 'CONTEXT-value-changed.md');
fs.writeFileSync(modifiedPath, modified, 'utf8');
const r = runGenContextIndex(['--check'], { contextPath: modifiedPath });
assert.equal(r.code, 1);
});
test('checkExitsOneWhenPredicateAdded', () => {
const real = fs.readFileSync(REAL_CONTEXT_PATH, 'utf8');
const added = real + '\n`ZZZTEST.added-by-test=value`\n';
const addedPath = path.join(tmpDir, 'CONTEXT-added.md');
fs.writeFileSync(addedPath, added, 'utf8');
const r = runGenContextIndex(['--check'], { contextPath: addedPath });
assert.equal(r.code, 1);
});
test('checkExitsOneWhenPredicateRemoved', () => {
const real = fs.readFileSync(REAL_CONTEXT_PATH, 'utf8');
const removed = real.replace(/`RULESET\.PR-SCOPE\.one-concern-per-pr=[^`]*`\r?\n/, '');
assert.notEqual(removed, real, 'fixture setup sanity: the removal must actually apply');
const removedPath = path.join(tmpDir, 'CONTEXT-removed.md');
fs.writeFileSync(removedPath, removed, 'utf8');
const r = runGenContextIndex(['--check'], { contextPath: removedPath });
assert.equal(r.code, 1);
});
test('checkExitsOneWhenClassSetChanged', () => {
const real = fs.readFileSync(REAL_CONTEXT_PATH, 'utf8');
const classGained = real + '\n`BRANDNEWCLASSFORTEST.x=y`\n';
const classGainedPath = path.join(tmpDir, 'CONTEXT-class-gained.md');
fs.writeFileSync(classGainedPath, classGained, 'utf8');
const r = runGenContextIndex(['--check'], { contextPath: classGainedPath });
assert.equal(r.code, 1);
});
test('checkExitsOneAndNamesDuplicateIdentifier (F7)', () => {
const real = fs.readFileSync(REAL_CONTEXT_PATH, 'utf8');
const dupPath = path.join(tmpDir, 'CONTEXT-dup.md');
const dupIntroduced = real + '\n`RULESET.PR-SCOPE.one-concern-per-pr=duplicate copy for test`\n';
fs.writeFileSync(dupPath, dupIntroduced, 'utf8');
const r = runGenContextIndex(['--check', '--json'], { contextPath: dupPath });
assert.equal(r.code, 1);
assert.doesNotMatch(r.stderr, STACK_FRAME_RE, 'no bare stack trace in non-debug failure output');
const report = parseJsonReport(r.stdout);
assert.equal(report.ok, false);
assert.equal(report.reason, REASON.FAIL_DUPLICATE_IDS);
assert.ok(
report.duplicates.some((d) => d.id === 'RULESET.PR-SCOPE.one-concern-per-pr'),
'report.duplicates must name the duplicate id',
);
});
test('checkExitsOneWithRemedyWhenIndexMissing (F8)', () => {
const missingIndexPath = path.join(tmpDir, 'does-not-exist.json');
const r = runGenContextIndex(['--check', '--json'], { indexPath: missingIndexPath });
assert.equal(r.code, 1);
assert.doesNotMatch(r.stderr, STACK_FRAME_RE);
const report = parseJsonReport(r.stdout);
assert.equal(report.ok, false);
assert.equal(report.reason, REASON.FAIL_INDEX_MISSING);
});
test('checkExitsOneWithNamedReasonWhenIndexCorrupt (F9)', () => {
const corruptIndexPath = path.join(tmpDir, 'corrupt-index.json');
fs.writeFileSync(corruptIndexPath, 'this is not { valid javascript', 'utf8');
const r = runGenContextIndex(['--check', '--json'], { indexPath: corruptIndexPath });
assert.equal(r.code, 1);
assert.doesNotMatch(r.stderr, STACK_FRAME_RE, 'no bare stack trace for a corrupt committed index');
const report = parseJsonReport(r.stdout);
assert.equal(report.ok, false);
assert.equal(report.reason, REASON.FAIL_INDEX_UNPARSEABLE);
});
test('checkExitsOneWhenContextMdMissing (F10)', () => {
const missingContextPath = path.join(tmpDir, 'does-not-exist.md');
const r = runGenContextIndex(['--check', '--json'], { contextPath: missingContextPath });
assert.equal(r.code, 1);
assert.doesNotMatch(r.stderr, STACK_FRAME_RE, 'no bare stack trace when CONTEXT.md is missing');
const report = parseJsonReport(r.stdout);
assert.equal(report.ok, false);
assert.equal(report.reason, REASON.FAIL_CONTEXT_MISSING);
});
test('checkExitsOneWhenContextMdUnreadable', () => {
// Fault injection via the mandated technique (CONTRIBUTING.md /
// CLAUDE.md cross-platform IO-failure rule): monkeypatch fs.readFileSync
// to throw an injected EACCES for one specific fixture path, restore in
// `finally`. Never chmod 0o000 (root bypasses mode bits). This is an
// in-process call to the exported `checkReport` pure function rather
// than a subprocess spawn — a subprocess's fs cannot be monkeypatched
// from the parent test process without a `--require` preload, and
// `checkReport` IS the typed surface under test here, so calling it
// directly is not an engine-direct false-green for CLI *argv* behavior
// (that risk is covered by the spawned-CLI tests above); it is the
// correct level to exercise a fault the CLI itself cannot inject.
const fixtureContextPath = path.join(tmpDir, 'unreadable-context.md');
fs.writeFileSync(fixtureContextPath, '`FOO=bar`\n', 'utf8');
const origReadFileSync = fs.readFileSync;
fs.readFileSync = function patchedReadFileSync(p, ...rest) {
if (p === fixtureContextPath) {
const err = new Error(`EACCES: permission denied, open '${p}' (injected by test, never a real fs fault)`);
err.code = 'EACCES';
throw err;
}
return origReadFileSync.call(fs, p, ...rest);
};
try {
const report = checkReport(fixtureContextPath, path.join(tmpDir, 'unused-index.json'));
assert.equal(report.ok, false);
assert.equal(report.reason, REASON.FAIL_CONTEXT_UNREADABLE);
} finally {
fs.readFileSync = origReadFileSync;
}
});
test('checkIsCrlfAgnostic (F17)', () => {
// F17: a CRLF-committed index compared against the (LF) fresh real
// CONTEXT.md must still exit 0 — comparison is CRLF-normalized.
const freshSerialized = serializeIndex(buildFreshIndex());
const crlfSerialized = freshSerialized.replace(/\n/g, '\r\n');
const crlfIndexPath = path.join(tmpDir, 'context-index-crlf.json');
fs.writeFileSync(crlfIndexPath, crlfSerialized, 'utf8');
const r = runGenContextIndex(['--check'], { indexPath: crlfIndexPath });
assert.equal(r.code, 0, 'CRLF-vs-LF committed/fresh comparison must be normalized, not a false failure');
});
});
describe('gen-context-index.cjs --write / default / usage (F)', () => {
let tmpDir;
beforeEach(() => {
tmpDir = createTempDir('gen-context-index-');
});
afterEach(() => {
cleanup(tmpDir);
});
test('writeThenCheckIsClean', () => {
const writeTarget = path.join(tmpDir, 'write-target.json');
const w = runGenContextIndex(['--write'], { indexPath: writeTarget });
assert.equal(w.code, 0);
assert.ok(fs.existsSync(writeTarget), '--write must create the fixture-redirected index file');
const c = runGenContextIndex(['--check'], { indexPath: writeTarget });
assert.equal(c.code, 0, '--check must be clean immediately after --write');
});
test('writeIsByteIdenticalAcrossRuns', () => {
const target1 = path.join(tmpDir, 'w1.json');
const target2 = path.join(tmpDir, 'w2.json');
assert.equal(runGenContextIndex(['--write'], { indexPath: target1 }).code, 0);
assert.equal(runGenContextIndex(['--write'], { indexPath: target2 }).code, 0);
const content1 = fs.readFileSync(target1, 'utf8');
const content2 = fs.readFileSync(target2, 'utf8');
assert.equal(content1, content2, '--write must be deterministic across independent runs');
});
test('writtenIndexContainsNoLineField', () => {
const target = path.join(tmpDir, 'no-line-field.json');
assert.equal(runGenContextIndex(['--write'], { indexPath: target }).code, 0);
const content = fs.readFileSync(target, 'utf8');
assert.equal(content.includes('"line"'), false, 'the committed artifact must carry no `line` field anywhere (S5)');
});
test('defaultInvocationPrintsIndexToStdout', () => {
// Fully safe against the real repo: default mode only reads CONTEXT.md
// and the compiled predicates lib (read-only) and writes nothing.
const r = runGenContextIndex([]);
assert.equal(r.code, 0);
assert.ok(r.stdout.length > 0);
// Compare against the exact expected serialization (computed the same
// way the CLI does, via the exported pure functions) rather than
// hand-parsing the rendered text — avoids brittle delimiter-scanning
// over a JSON payload that legitimately contains ';' inside string values.
const expected = serializeIndex(buildFreshIndex()) + '\n';
assert.equal(r.stdout, expected);
});
test('unknownFlagExitsWithUsage', () => {
// Safe against the real repo: the unknown-flag branch never reads
// CONTEXT.md or the committed index at all.
const r = runGenContextIndex(['--totally-bogus-flag']);
assert.notEqual(r.code, 0);
assert.doesNotMatch(r.stderr, STACK_FRAME_RE, 'usage output must never be a bare stack trace');
});
// ─── DEFECT.GEN-CONTEXT-INDEX-PARSEARGS-GATE-BYPASS (MAJOR review finding):
// conflicting `--check --write` must be a hard usage error, not a silent
// `--write` win, and a missing/flag-shaped path value must never resolve
// to the cwd (which previously leaked a raw EISDIR stack trace). ────────
test('checkAndWriteTogetherIsUsageErrorNotASilentWrite (a)', () => {
const target = path.join(tmpDir, 'should-not-be-written.json');
const r = runGenContextIndex(['--check', '--write'], { indexPath: target });
assert.notEqual(r.code, 0, '--check --write together must not silently exit 0 as a write');
assert.doesNotMatch(r.stderr, STACK_FRAME_RE, 'usage output must never be a bare stack trace');
assert.equal(fs.existsSync(target), false, '--write must never win over --check and rewrite the index');
});
test('writeAndCheckReversedOrderIsAlsoAUsageError (a)', () => {
const target = path.join(tmpDir, 'should-also-not-be-written.json');
const r = runGenContextIndex(['--write', '--check'], { indexPath: target });
assert.notEqual(r.code, 0, 'conflicting mode flags must be a usage error regardless of order');
assert.doesNotMatch(r.stderr, STACK_FRAME_RE);
assert.equal(fs.existsSync(target), false);
});
test('missingTrailingValueForContextPathIsUsageErrorNotEisdirStackTrace (b)', () => {
const target = path.join(tmpDir, 'should-not-be-written-2.json');
// `--context-path` is the LAST arg: argv[i+1] is undefined, which used
// to resolve to the cwd via `path.resolve(undefined ?? '')`.
const r = runGenContextIndex(['--write', '--index-path', target, '--context-path']);
assert.notEqual(r.code, 0, 'a missing --context-path value must be a usage error');
assert.doesNotMatch(r.stderr, STACK_FRAME_RE, 'must never leak a raw EISDIR (or any) stack trace');
assert.equal(fs.existsSync(target), false, 'no write must happen when the path argument is rejected');
});
test('flagShapedValueForIndexPathIsUsageErrorNotSwallowedAsALiteralPath (b)', () => {
// `--index-path` is immediately followed by another flag rather than a
// path — must be rejected, not silently swallowed as the literal path
// "--json".
const r = runGenContextIndex(['--write', '--index-path', '--json']);
assert.notEqual(r.code, 0, 'a flag-shaped --index-path value must be a usage error');
assert.doesNotMatch(r.stderr, STACK_FRAME_RE);
});
});
// ─── DEFECT.GEN-CONTEXT-INDEX-DUPLICATE-GATE-UNPROVEN (MAJOR review finding):
// `FAIL_DUPLICATE_IDS` was only ever proven against synthetic fixtures — this
// branch hand-deleted the ONE live duplicate
// (`RULESET.WORKFLOW_MARKDOWN.FENCES`) from the real CONTEXT.md, so the
// committed docs/CONTEXT-INDEX.json ships `duplicates: []` and the gate has
// never been shown to catch a REAL duplicate in the real document. This
// suite re-inserts the exact deleted line (recovered from
// `git show origin/next:CONTEXT.md`) into a copy of the REAL CONTEXT.md and
// runs the real generator CLI against it. ───────────────────────────────────
describe('gen-context-index.cjs --check against a real-CONTEXT.md duplicate (real-data proof)', () => {
let tmpDir;
beforeEach(() => {
tmpDir = createTempDir('gen-context-index-real-dup-');
});
afterEach(() => {
cleanup(tmpDir);
});
test('reinsertingTheDeletedRulesetWorkflowMarkdownFencesLineFailsWithNamedDuplicate', () => {
// The exact line this branch deleted from the real CONTEXT.md (does NOT
// mention MD040 — the live line that replaced it does).
const deletedLine =
'`RULESET.WORKFLOW_MARKDOWN.FENCES=when editing shell snippets inside workflow markdown, preserve the opening language fence; malformed fence can create fresh CodeRabbit threads`';
const real = fs.readFileSync(REAL_CONTEXT_PATH, 'utf8');
assert.ok(
real.includes('RULESET.WORKFLOW_MARKDOWN.FENCES'),
'sanity: the real CONTEXT.md must still carry the live (MD040) FENCES line',
);
assert.ok(!real.includes(deletedLine), 'sanity: the deleted line must not already be present verbatim');
const reinserted = real + '\n' + deletedLine + '\n';
const fixturePath = path.join(tmpDir, 'CONTEXT-real-with-reinserted-duplicate.md');
fs.writeFileSync(fixturePath, reinserted, 'utf8');
const r = runGenContextIndex(['--check', '--json'], { contextPath: fixturePath });
assert.equal(r.code, 1, 'a real duplicate reintroduced into the real CONTEXT.md must fail the gate');
assert.doesNotMatch(r.stderr, STACK_FRAME_RE);
const report = parseJsonReport(r.stdout);
assert.equal(report.ok, false);
assert.equal(report.reason, REASON.FAIL_DUPLICATE_IDS);
assert.ok(
report.duplicates.some((d) => d.id === 'RULESET.WORKFLOW_MARKDOWN.FENCES'),
'report.duplicates must name RULESET.WORKFLOW_MARKDOWN.FENCES as the real duplicate',
);
});
});

View File

@@ -33,6 +33,7 @@ const fc = require('./helpers/fast-check-setup.cjs');
const {
stripFencedCode,
extractFencedBlock,
scanFencedBlocks,
tokenizeHeadings,
collectSections,
collectSection,
@@ -350,6 +351,160 @@ describe('extractFencedBlock', () => {
});
});
// ─── scanFencedBlocks (public export) ─────────────────────────────────────────
// #2928: `export` was newly added to this function and its `FencedBlockRecord`
// interface, making it public API for the first time (context-predicates.cts
// consumes it directly). Hyrum's Law: a newly-public contract needs its own
// lock-in test, independent of the consumer that motivated exporting it.
describe('scanFencedBlocks (public export)', () => {
test('is actually exported from the compiled module as a function', () => {
assert.equal(typeof scanFencedBlocks, 'function');
});
test('returns {char, len, infoString, openLineIdx, closeLineIdx} with 0-based line indices', () => {
const lines = [
'before',
'```js',
'const x = 1;',
'```',
'after',
];
const blocks = scanFencedBlocks(lines);
assert.equal(blocks.length, 1);
assert.deepEqual(blocks[0], {
char: '`',
len: 3,
infoString: 'js',
openLineIdx: 1,
closeLineIdx: 3,
});
});
test('closeLineIdx is -1 for an unterminated fence (EOF while open)', () => {
const lines = [
'before',
'```',
'body, never closed',
];
const blocks = scanFencedBlocks(lines);
assert.equal(blocks.length, 1);
assert.equal(blocks[0].openLineIdx, 1);
assert.equal(blocks[0].closeLineIdx, -1);
});
test('requires >=3 backticks or >=3 tildes to open a fence', () => {
assert.deepEqual(scanFencedBlocks(['``', 'not a fence']), []);
assert.deepEqual(scanFencedBlocks(['~~', 'not a fence']), []);
const backtickBlocks = scanFencedBlocks(['```', 'x', '```']);
assert.equal(backtickBlocks.length, 1);
assert.equal(backtickBlocks[0].char, '`');
const tildeBlocks = scanFencedBlocks(['~~~', 'x', '~~~']);
assert.equal(tildeBlocks.length, 1);
assert.equal(tildeBlocks[0].char, '~');
});
test('tolerates up to 3 spaces of indent on the opening delimiter', () => {
const blocks = scanFencedBlocks([' ```', 'body', '```']);
assert.equal(blocks.length, 1);
assert.equal(blocks[0].openLineIdx, 0);
assert.equal(blocks[0].closeLineIdx, 2);
});
test('4+ spaces of indent is not recognised as a fence delimiter', () => {
const blocks = scanFencedBlocks([' ```', 'still not a fence']);
assert.deepEqual(blocks, []);
});
test('a closer must be the same delimiter char with run length >= the opener, and no trailing text', () => {
// Same char, longer run: valid closer.
const longerCloser = scanFencedBlocks(['```', 'body', '`````']);
assert.equal(longerCloser.length, 1);
assert.equal(longerCloser[0].closeLineIdx, 2);
// Same char, shorter run: not a valid closer -> content, fence stays open (EOF -> -1).
const shorterCloser = scanFencedBlocks(['````', 'body', '```']);
assert.equal(shorterCloser.length, 1);
assert.equal(shorterCloser[0].closeLineIdx, -1);
});
test('mismatched delimiter char while a fence is open is CONTENT, not a boundary — a 3-backtick line inside a 4-backtick fence does not close it', () => {
const lines = [
'````outer',
'```coverage',
'nested body',
'```',
'````',
];
const blocks = scanFencedBlocks(lines);
assert.equal(blocks.length, 1, 'only the outer 4-backtick fence is a real block');
assert.equal(blocks[0].char, '`');
assert.equal(blocks[0].len, 4);
assert.equal(blocks[0].openLineIdx, 0);
assert.equal(blocks[0].closeLineIdx, 4);
});
test('a same-char run that is too short, encountered while open, is content not a closer', () => {
const lines = [
'````',
'```',
'still inside',
'````',
];
const blocks = scanFencedBlocks(lines);
assert.equal(blocks.length, 1);
assert.equal(blocks[0].openLineIdx, 0);
assert.equal(blocks[0].closeLineIdx, 3);
});
test('a same-char, sufficient-length run carrying trailing non-whitespace text, encountered while open, is content not a closer', () => {
const lines = [
'```',
'``` still inside (has trailing text)',
'```',
'after',
];
const blocks = scanFencedBlocks(lines);
assert.equal(blocks.length, 1);
assert.equal(blocks[0].openLineIdx, 0);
assert.equal(blocks[0].closeLineIdx, 2);
});
test('CommonMark §4.5: a backtick fence info string must not contain a backtick — such a line is not a valid opener', () => {
const blocks = scanFencedBlocks(['``` has ` a backtick', 'more text']);
assert.deepEqual(blocks, [], 'a backtick in the info string means the line is not a valid opener at all');
});
test('infoString is the trimmed trailing text of the opener', () => {
const blocks = scanFencedBlocks(['``` js and stuff ', 'body', '```']);
assert.equal(blocks.length, 1);
assert.equal(blocks[0].infoString, 'js and stuff');
});
test('multiple sequential blocks in one document are all returned, in order', () => {
const lines = [
'a',
'```',
'code1',
'```',
'b',
'~~~py',
'code2',
'~~~',
'c',
];
const blocks = scanFencedBlocks(lines);
assert.equal(blocks.length, 2);
assert.equal(blocks[0].char, '`');
assert.equal(blocks[0].openLineIdx, 1);
assert.equal(blocks[0].closeLineIdx, 3);
assert.equal(blocks[1].char, '~');
assert.equal(blocks[1].infoString, 'py');
assert.equal(blocks[1].openLineIdx, 5);
assert.equal(blocks[1].closeLineIdx, 7);
});
});
// ─── tokenizeHeadings ─────────────────────────────────────────────────────────
describe('tokenizeHeadings', () => {