Files
msd-core/tests/check-ui-safety-gate.test.cjs
Tom Boucher ff4a57b78c chore(#1671): migrate the remaining 13 LARGE/XL workflows to the fragment model — Phase 6.3 (#3030)
* chore(#2994): fragmentize progress.md forensic audit onto the fragment model

Extract the --forensic-gated forensic_audit step to
workflows/progress/steps/forensic-audit.md behind a section marker, and
repair progress.md's init line to forward --forensic so the atom is
actually true in production rather than only under direct CLI tests.

progress.md shrinks 32630 -> 27207 bytes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* chore(#2994): fragmentize the four manifest-wired workflows

new-project, quick, new-milestone and progress each already had a
dedicated cmdInit* entry point but zero marked sections. Extract nine
gated bodies to workflows/<wf>/steps/ behind section markers and repair
each init line to forward its flags.

Fold --full into the discuss/research/validate facts inside cmdInitQuick
so the when= grammar never sees an OR, per the chunked-mode precedent.

Fixes found while working, per the no-defer rule:
- cmdInitProgress passed no phase info to buildSectionManifestField, so
  state:phase-mvp-mode was permanently false — an atom in the vocabulary
  whose fact could never be computed.
- the quick init router folded flag tokens into the free-text
  description, which the new forwarding would have corrupted.
- a #2508 dispatch note was nested inside quick.md's Agent(prompt=)
  fence, leaking orchestrator guidance into the subagent prompt.
- progress.md had a 3-vs-4 backtick outer-fence imbalance.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* chore(#2994): fragmentize verify-work.md and admit state:ui-phase-active

Wire cmdInitVerifyWork to buildSectionManifestField — it was a dedicated
entry point that never emitted a manifest — and mark two sections.

state:ui-phase-active folds (plan:pre hooks include an active ui step) OR
(the phase dir holds a *-UI-SPEC.md) into one boolean in init.cts, so the
grammar still sees a single operator-free atom. The inner Playwright-MCP
check stays as prose inside the fragment: it is live session state and no
init seam can precompute it.

The MVP false-branch note is a real fallback, not redundant prose, so it
sits outside the marker — gating it away would delete the text needed
precisely when MVP mode is off.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test(#2994): follow moved workflow content in drift guards

Retarget every guard that asserted on content this branch moved into
workflows/<wf>/steps/, mirroring 815b3d897. Each retargeted assertion was
verified to still fail when its step file is blanked, so none was
weakened into vacuity.

Three assertions in verify-mvp-uat were genuinely red. Three more were
worse than red — passing for the wrong reason:
- quick-commit-boundary and worktree-cleanup anchored on indexOf('Step
  5.6'), which matched a later cross-reference and sliced 16069 chars
  that coincidentally held the asserted substrings. Replaced with an
  expandWorkflowSections helper that splices step content back in place.
- phase6-review-capabilities lost its end boundary and widened to EOF.
- playwright-ui-verify matched 'UI' in an unrelated bullet and 'fall
  back' in a subagent-dispatch line after the real content moved.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* chore(#2994): fragmentize code-review and complete-milestone, admit three atoms

Add dedicated cmdInitCodeReview and cmdInitCompleteMilestone entry points
alongside the shared generic ones rather than modifying them — init.phase-op
and init.manager carry a CRITICAL blast radius (179 dependents, 24
processes) and stay byte-identical for their other callers.

Admit flag:--fix, state:fallow-enabled and state:git-create-tag, each with
a consuming section and a fact its own entry point computes.

Both sections had the resolver-in-body hazard: the fallow config-gate and
the git.create_tag check each sat inside the very block being gated, so
gating would have disabled the resolver that decides the gate. Both are
hoisted into init and the bodies now consume the resolved fact.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test(#2994): retarget code-review and milestone drift guards, fix two red tests

Retarget guards that asserted on content moved into steps/, proving
non-vacuity by blanking each step file and confirming failure.

Also fixes two genuinely red tests found while working, per the no-defer
rule:
- workflow-fragments' frozen-vocabulary lock was missing
  state:ui-phase-active, so commit 7ef7f8336 shipped red. Lint and build
  both passed over it, which is why neither is sufficient verification.
- code-review's quick.md capability-hook assertion carried a stale
  delimiter after the 18ff35d20 extraction.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* chore(#2994): fragmentize autonomous.md and admit state:plan-strategy-converge

Five sections share one atom, the pattern plan-phase already uses for
flag:--research-phase. The atom folds --converge OR --cross-ai into a
single boolean in cmdInitAutonomous so the grammar stays operator-free.

cmdInitAutonomous is additive; init.milestone-op, init.manager and
init.phase-op are untouched and still consumed. The $PLAN_STRATEGY bash
resolver is deliberately retained — ungated local-planning bullets still
read it, so the init-side fact supplements it rather than replacing it.

converge-fail-fast required splitting one bash fence so the always-run
CONVERGENCE_ARGS construction stays outside the marker. All three
flag-absent fallbacks were left outside their markers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* chore(#2994): fragmentize review and discuss-phase-assumptions

Admit state:reviewer-instances-configured (two peripheral notes share it;
the core reviewer-lane dispatch stays unmarked — it is the workflow's
primary always-evaluated logic, not an optional branch) and
state:auto-advance-active, which folds --auto OR two config keys into one
boolean so the grammar stays operator-free.

discuss-phase-assumptions was the highest-risk edit in this PR. Its
auto_advance step is a full if/elif/else; gating it whole would have
deleted the flag-absent fallback needed exactly when --auto is off. Split
verified exact: resolvers 636-651 and the 'End here' fallback 668-669 both
stay outside the marker; only 653-667 is gated.

Adds emitted-drift acks for the two files that grew — review.md (+55 B)
and autonomous.md (+737 B from 80799211c, which had none and would have
red-gated the push.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* chore(#2994): fragmentize docs-update, update, transition and new-milestone Part A

Completes the 13-workflow rollout. Three of these had no init call at all
and gained a dedicated entry point plus their first gsd_run query line.

Admits state:is-monorepo and adds state:next-channel, state:workstream-active
and state:flat-mode. Vocabulary 26 -> 30 atoms.

Part A of new-milestone applies when NO workstream is active — the negation
of state:workstream-active. Rather than teach the grammar negation, which is
the Greenspun drift the frozen list exists to prevent, it gets a separate
positively-phrased atom whose fact is the inverse. Part B, which always runs,
stays outside the marker.

flag:--verify-only is deliberately NOT admitted: docs-update has no
contiguous purely-additive region for it, and an atom without a consuming
section is dead vocabulary. Evidence recorded in the slice report.

update.md reuses its existing resolved $GSD_TOOLS rather than prepending the
canonical preamble, which would have clobbered it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(#2994): stop automated-ui-verification re-resolving its own gate, retire dead vocabulary

Two defects the new tests caught.

The automated-ui-verification step re-ran gsd_run loop render-hooks and
recomputed UI_PHASE_ACTIVE inside a body that is only read when that fact
is already true — the circular self-disabling pattern this design forbids,
introduced by 3c654b168. cmdInitVerifyWork now exposes ui_phase_active and
the step consumes it. Its launcher preamble goes too: no gsd_run remains.
The Playwright-MCP check stays as prose — that is live session state.

Dead vocabulary predating this PR: flag:--full and state:needs-codebase-map
were admitted with a gate-1 claim that never materialized. flag:--full is
removed, redundant once quick folds it into discuss/research/validate.
state:needs-codebase-map gets the real consumer it always lacked, gating
new-project's codebase-map offer. Vocabulary 30 -> 29, and no atom is now
without a consuming section.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test(#2994): add the atom-admission, inversion and resolver-hoist gates

The two existing parity guards prove vocabulary/predicate symmetry but
never that a fact is computed — an atom no cmdInit* assembles evaluates
false forever. These close that hole:

- per-atom satisfiability for all 29 atoms, plus an anti-vacuity assertion
  so the loop cannot silently cover zero atoms
- dead-vocabulary check against the shipped manifest
- inversion guard: the flag-absent fallbacks in discuss-phase-assumptions
  and verify-work must stay outside their markers
- data-driven resolver-hoist guard over the shipped manifest, so a future
  extraction cannot reintroduce the circular class
- compound-fold coverage (--full, --cross-ai, --rc, config-only --auto)
- null-vs-[] degraded/computed distinction, and flag value shapes

Also repairs the frozen-vocabulary lock, which was stale and red for the
seven atoms earlier commits on this branch shipped.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs(#2994): add changeset for the fragment-model rollout

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test(#2994): cite the issue on the two new allow-test-rule exemptions

ADR-456 requires an issue ref on the same line as the annotation.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs(#2994): correct the atom-count claims after retiring flag:--full

The vocabulary doc comments still said 30 entries; it is 29 since
flag:--full was removed as dead vocabulary.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(#2994): dedupe the phase-fallback block and harden --ws parsing

Review findings.

MAJOR: the three new init entry points each pasted a verbatim copy of the
guardedFindPhase/guardedGetRoadmapPhase fallback, taking the repo from four
copies to seven — DEFECT.GENERATIVE-FIX. Extracted applyRoadmapFallback and
folded six of the seven; each call site keeps its own field-set via a
closure. Duplication removed rather than papered over with a parity test.
cmdInitPhaseOp stays out: its fallback omits has_reviews, so it is not a
byte-identical copy, and it is CRITICAL-radius.

LOW, pre-existing: GSD_WS captured [^[:space:]]+ and expands unquoted, so a
workstream name holding glob metacharacters would expand against the
filesystem. Narrowed to [A-Za-z0-9._-]+. The unquoted expansion is kept —
it must word-split into two args and vanish when empty.

Also restores the vocabulary ordering convention, and fixes a masked test
bug the mandated run surfaced: the flag-forwarding guard checked only the
first init line per workflow, but new-milestone has two, so a real failure
was reporting exit 0.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(#2994): drop the stale new-milestone emitted-drift ack

new-milestone.md was acked for a +406 B growth measured against an
intermediate commit. Net against origin/next it SHRANK by 8 bytes, so
nothing needed the ack and it explained nothing — which the differential
attribution check reports as a stale acknowledgment, not a pass.

update.md's entry stays: it genuinely grew +703 B.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(#2994): resolve the 15 failures from the full matrix run

All 15 were real and identical on both lanes.

REAL REGRESSION: autonomous.md hit 41479 chars against the #2196 guard's
40960 cap — a CHARS cap distinct from the LARGE tier byte cap, which the
five section stubs pushed it over. Extracted the 3a.5 UI Design Contract
body to references/; now 39968 chars, and the file nets -795 B vs base, so
its growth ack is deleted rather than left stale.

REAL DEFECT: docs referenced /gsd-transition, which is not a live
registered command. Reworded.

STALE FIXTURE: the emission byte-identity test hardcoded two marked
workflows; this branch legitimately marks fifteen. Fixture corrected — the
source was right.

The rest were drift guards over the eight workflows the earlier sweep did
not cover, retargeted at where the content now lives with non-vacuity
proven by blanking each step file and confirming failure. The GSD_WS
forwarding guard was checked as a possible real break and is not one: the
charclass narrowing is intact and forwarding works end to end.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(#2994): drop the ack for a newly-added reference file

A new file's emitted ripple is attributable to the diff that adds it, so
the acknowledgment explained nothing and the differential check reports it
as stale. Removing the last entry removes the fragment — an empty one
signals nothing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(#2994): retarget the UI-contract guards and clear two transitive advisories

The §3a.5 extraction that brought autonomous.md under the #2196 char cap
moved its body to references/autonomous-ui-design-contract.md, so ten
guards in autonomous-ui-steps and check-ui-safety-gate were asserting it
against the host. Retargeted via a combined read, each proven non-vacuous
by blanking the reference file and confirming failure.

This class had already bitten twice on this branch because each sweep was
scoped to the workflows touched at that moment, so this one was
exhaustive: ~70 test files across all 13 workflows, zero further broken or
vacuous assertions found.

Also clears two high transitive advisories the matrix flagged on one lane
— fast-uri GHSA-7p8r-x3mc-p8w7 and three ip-address SSRF/trust-boundary
issues. Both pre-date this branch: package-lock.json was untouched until
now, so the production tree was byte-identical to the base. Lockfile-only,
package.json unchanged, verified against a real npm ci install.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* chore(#2994): backfill changeset pr number to 3030

---------

Co-authored-by: sim <sim@local>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 19:59:58 -04:00

684 lines
32 KiB
JavaScript

'use strict';
/**
* Behavioral tests for the `check ui-safety-gate` subcommand (#1168).
*
* Tests the `computeUiSafetyGate` pure function exported from check-command-router.cjs.
* Uses in-memory tmpdir fixtures — no real CLI subprocess needed.
*
* Return shape: { frontend: bool, hasUiFiles: bool, hasUiSpec: bool, block: bool, message?: string }
* Invariant: block = frontend && hasUiFiles && !hasUiSpec
*
* Per RULESET.TESTS.boundary-coverage: exercises all branches:
* (a) frontend + UI files changed + no spec → block:true
* (b) frontend + UI files changed + spec exists → block:false
* (c) non-frontend → block:false
* (d) frontend + no UI files changed → block:false
*
* Per RULESET.TESTS.coderabbit-fix-prefer: calls the exported function and asserts typed fields.
*/
const { describe, test, before, after } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { cleanup } = require('./helpers.cjs');
const { computeUiSafetyGate } = require('../gsd-core/bin/lib/check-command-router.cjs');
// ─── Helpers ──────────────────────────────────────────────────────────────────
/**
* Create a minimal project dir with:
* .planning/ROADMAP.md — one phase section with `phaseSection` body
* .planning/phases/01-test-phase/ — phase directory
* (optionally) a *-UI-SPEC.md inside the phase dir
*/
function makeProject({ phaseSection = '', hasUiSpec = false } = {}) {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ui-safety-gate-test-'));
const planningDir = path.join(tmpDir, '.planning');
const phasesDir = path.join(planningDir, 'phases');
const phaseDir = path.join(phasesDir, '01-test-phase');
fs.mkdirSync(phaseDir, { recursive: true });
fs.writeFileSync(path.join(planningDir, 'config.json'), JSON.stringify({}), 'utf8');
// Minimal ROADMAP.md with one phase section
const roadmapContent = [
'# Project Roadmap',
'',
'## Phase 1: Test Phase',
'',
phaseSection,
'',
].join('\n');
fs.writeFileSync(path.join(planningDir, 'ROADMAP.md'), roadmapContent, 'utf8');
if (hasUiSpec) {
fs.writeFileSync(path.join(phaseDir, '01-UI-SPEC.md'), '# UI Design Contract\n', 'utf8');
}
return { tmpDir, phaseDir };
}
// ─── Tests ─────────────────────────────────────────────────────────────────────
describe('computeUiSafetyGate — ui.safety-gate check logic (#1168)', () => {
let frontendNoSpec, frontendWithSpec, nonFrontend;
before(() => {
// Branch (a): frontend + no UI-SPEC → tests block behavior
frontendNoSpec = makeProject({
phaseSection: 'Build the user interface and dashboard components for the frontend.',
hasUiSpec: false,
});
// Branch (b): frontend + UI-SPEC exists → block:false
frontendWithSpec = makeProject({
phaseSection: 'Build the frontend dashboard with React components and UI forms.',
hasUiSpec: true,
});
// Branch (c): no frontend indicators → block:false
nonFrontend = makeProject({
phaseSection: 'Add a REST API endpoint and database migration for the user table.',
hasUiSpec: false,
});
});
after(() => {
for (const { tmpDir } of [frontendNoSpec, frontendWithSpec, nonFrontend]) {
try { cleanup(tmpDir); } catch { /* ignore */ }
}
});
describe('return shape', () => {
test('result has required keys: frontend, hasUiFiles, hasUiSpec, block', () => {
const result = computeUiSafetyGate(nonFrontend.tmpDir, '1');
assert.ok(typeof result === 'object' && result !== null, 'result must be an object');
assert.ok(typeof result.frontend === 'boolean', 'frontend must be boolean');
assert.ok(typeof result.hasUiFiles === 'boolean', 'hasUiFiles must be boolean');
assert.ok(typeof result.hasUiSpec === 'boolean', 'hasUiSpec must be boolean');
assert.ok(typeof result.block === 'boolean', 'block must be boolean');
});
test('block invariant: block === frontend && hasUiFiles && !hasUiSpec for all scenarios', () => {
for (const [label, { tmpDir }] of [
['frontendNoSpec', frontendNoSpec],
['frontendWithSpec', frontendWithSpec],
['nonFrontend', nonFrontend],
]) {
const r = computeUiSafetyGate(tmpDir, '1');
assert.strictEqual(
r.block,
r.frontend && r.hasUiFiles && !r.hasUiSpec,
`${label}: block invariant violated — frontend=${r.frontend} hasUiFiles=${r.hasUiFiles} hasUiSpec=${r.hasUiSpec} block=${r.block}`,
);
}
});
});
describe('branch (a) — frontend + no UI-SPEC → gate fires when hasUiFiles', () => {
test('detects frontend indicators in phase section', () => {
const r = computeUiSafetyGate(frontendNoSpec.tmpDir, '1');
assert.strictEqual(r.frontend, true, 'should detect frontend indicators');
});
test('hasUiSpec is false when no *-UI-SPEC.md exists', () => {
const r = computeUiSafetyGate(frontendNoSpec.tmpDir, '1');
assert.strictEqual(r.hasUiSpec, false, 'hasUiSpec must be false');
});
test('block is true when frontend + hasUiFiles + no UI-SPEC', () => {
// hasUiFiles depends on git state; when false, block must also be false (invariant).
// We verify the invariant holds rather than hardcoding the git state.
const r = computeUiSafetyGate(frontendNoSpec.tmpDir, '1');
assert.strictEqual(r.block, r.frontend && r.hasUiFiles && !r.hasUiSpec,
'block invariant: frontend && hasUiFiles && !hasUiSpec');
});
test('message is present when block is true', () => {
const r = computeUiSafetyGate(frontendNoSpec.tmpDir, '1');
if (r.block) {
assert.ok(typeof r.message === 'string' && r.message.length > 0,
'message must be a non-empty string when block is true');
assert.ok(r.message.includes('UI-SPEC'), 'message must reference UI-SPEC');
}
});
});
describe('branch (b) — frontend + UI-SPEC exists → block:false', () => {
test('detects frontend indicators in phase section', () => {
const r = computeUiSafetyGate(frontendWithSpec.tmpDir, '1');
assert.strictEqual(r.frontend, true, 'should detect frontend indicators');
});
test('hasUiSpec is true when *-UI-SPEC.md exists', () => {
const r = computeUiSafetyGate(frontendWithSpec.tmpDir, '1');
assert.strictEqual(r.hasUiSpec, true, 'hasUiSpec must be true');
});
test('block is false when UI-SPEC exists (regardless of hasUiFiles)', () => {
const r = computeUiSafetyGate(frontendWithSpec.tmpDir, '1');
assert.strictEqual(r.block, false, 'block must be false when spec exists');
});
test('message is absent when block is false', () => {
const r = computeUiSafetyGate(frontendWithSpec.tmpDir, '1');
assert.ok(!r.message || r.message === undefined,
'message must be absent when block is false');
});
});
describe('branch (c) — non-frontend phase → block:false', () => {
test('frontend is false for non-UI phase section', () => {
const r = computeUiSafetyGate(nonFrontend.tmpDir, '1');
assert.strictEqual(r.frontend, false, 'should NOT detect frontend indicators');
});
test('block is false for non-frontend phases', () => {
const r = computeUiSafetyGate(nonFrontend.tmpDir, '1');
assert.strictEqual(r.block, false, 'block must be false');
});
});
describe('graceful degradation', () => {
test('non-existent project dir returns frontend:false, block:false (no crash)', () => {
const r = computeUiSafetyGate('/tmp/nonexistent-gsd-test-dir-xyz', '1');
assert.strictEqual(typeof r.frontend, 'boolean', 'frontend must be boolean');
assert.strictEqual(r.frontend, false, 'missing roadmap → no frontend indicators');
assert.strictEqual(r.block, false, 'missing roadmap → block false');
assert.strictEqual(typeof r.hasUiFiles, 'boolean', 'hasUiFiles must be boolean');
assert.strictEqual(typeof r.hasUiSpec, 'boolean', 'hasUiSpec must be boolean');
});
test('missing ROADMAP.md returns frontend:false gracefully (no phaseLookupFailed)', () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ui-safety-nomap-'));
try {
fs.mkdirSync(path.join(tmpDir, '.planning', 'phases', '01-phase'), { recursive: true });
const r = computeUiSafetyGate(tmpDir, '1');
assert.strictEqual(r.frontend, false, 'no ROADMAP → no frontend indicators');
assert.strictEqual(r.block, false, 'no ROADMAP → no block');
assert.ok(
!r.phaseLookupFailed,
'phaseLookupFailed must NOT be set when ROADMAP.md is absent (no-roadmap project is not a lookup failure)',
);
} finally {
try { cleanup(tmpDir); } catch { /* ignore */ }
}
});
test('ROADMAP.md present but phase not found → phaseLookupFailed:true (not silent false)', () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ui-safety-noPhase-'));
try {
const planningDir = path.join(tmpDir, '.planning');
const phasesDir = path.join(planningDir, 'phases');
fs.mkdirSync(path.join(phasesDir, '01-test-phase'), { recursive: true });
fs.writeFileSync(path.join(planningDir, 'ROADMAP.md'), [
'# Project Roadmap',
'',
'## Phase 1: Test Phase',
'',
'Build the frontend dashboard with React components.',
'',
].join('\n'), 'utf8');
// Phase 99 is not in the roadmap
const r = computeUiSafetyGate(tmpDir, '99');
assert.strictEqual(r.phaseLookupFailed, true,
'phaseLookupFailed must be true when ROADMAP.md exists but phase is not found');
assert.strictEqual(r.frontend, false, 'empty section → no frontend indicators');
} finally {
try { cleanup(tmpDir); } catch { /* ignore */ }
}
});
});
describe('routing — ui-safety-gate is routable via check-command-router', () => {
test('routeCheckCommand routes ui-safety-gate (hyphen form)', () => {
const { routeCheckCommand } = require('../gsd-core/bin/lib/check-command-router.cjs');
// Should not throw; just verify routing works (output goes to stdout)
let threw = false;
try {
routeCheckCommand({ args: ['check', 'ui-safety-gate', '1'], cwd: nonFrontend.tmpDir, raw: true });
} catch (err) {
threw = true;
}
assert.strictEqual(threw, false, 'routeCheckCommand must not throw for ui-safety-gate');
});
test('routeCheckCommand routes ui.safety-gate (dot form — normalized to hyphens)', () => {
const { routeCheckCommand } = require('../gsd-core/bin/lib/check-command-router.cjs');
let threw = false;
try {
routeCheckCommand({ args: ['check', 'ui.safety-gate', '1'], cwd: nonFrontend.tmpDir, raw: true });
} catch (err) {
threw = true;
}
assert.strictEqual(threw, false, 'routeCheckCommand must not throw for ui.safety-gate (dot form)');
});
});
});
// ────────────────────────────────────────────────────────────────────────
// Folded from tests/bug-3706-ui-safety-gate-false-positives.test.cjs — consolidation epic #1969 (B3 #1972)
// ────────────────────────────────────────────────────────────────────────
{
const { describe: __foldDescribe } = require('node:test');
__foldDescribe("folded:bug-3706-ui-safety-gate-false-positives (consolidation epic #1969 B3 #1972)", () => {
'use strict';
/**
* Tests for bug #3706 (word-boundary anchoring) and #3718 (cross-shell portability).
*
* Root cause (#3706): grep -iE "UI|..." had no word-boundary anchoring, causing
* false-positives on "Requirements" (contains "ui"), "overview" ("view"), etc.
*
* Root cause (#3718): shell-based invocation (with locale env-var prefix) silently
* degrades on Windows PowerShell — the prefix is not recognised by pwsh.
*
* Fix (#3718, Approach A): gate logic moved to `bin/lib/ui-safety-gate.cjs` (Node.js).
* Reads phase text from STDIN (not argv) to avoid OS ARG_MAX limits.
* Invoked as: printf '%s' "$PHASE_SECTION" | node "${GSD_REPO_ROOT}/bin/lib/ui-safety-gate.cjs"
* Path anchored to repo root via `git rev-parse --show-toplevel`.
*
* Test strategy:
* 1. Import the helper module directly and assert correct results on the full fixture
* matrix (exercises the production code path).
* 2. Spawn the helper as a child process with shell:false and input on stdin to prove
* cross-shell portability — spawnSync with shell:false bypasses any host shell.
* 3. Assert the workflow .md files now invoke `node ... ui-safety-gate.cjs` via stdin
* rather than the old shell-based invocation (structural guard against regression).
*/
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('node:child_process');
const os = require('node:os');
const { cleanup } = require('./helpers.cjs');
const HELPER_PATH = path.join(__dirname, '..', 'bin', 'lib', 'ui-safety-gate.cjs');
const PLAN_PHASE_PATH = path.join(__dirname, '..', 'gsd-core', 'workflows', 'plan-phase.md');
const AUTONOMOUS_PATH = path.join(__dirname, '..', 'gsd-core', 'workflows', 'autonomous.md');
const AUTONOMOUS_UI_DESIGN_CONTRACT_REF_PATH = path.join(
__dirname, '..', 'gsd-core', 'references', 'autonomous-ui-design-contract.md'
);
const { checkUiPresence } = require(HELPER_PATH);
/**
* #2994 fragmentization moved §3a.5's body out of autonomous.md into
* gsd-core/references/autonomous-ui-design-contract.md, leaving an
* unconditional `Read and execute:` pointer in the host. Read host +
* reference file combined so the structural guards below still see the
* real §3a.5 body (and its absence of the retired ui-safety-gate.cjs /
* RUNTIME_DIR / LC_ALL=C grep patterns).
*/
function readAutonomousCombined() {
return fs.readFileSync(AUTONOMOUS_PATH, 'utf-8') +
'\n' + fs.readFileSync(AUTONOMOUS_UI_DESIGN_CONTRACT_REF_PATH, 'utf-8');
}
// ── Helper ────────────────────────────────────────────────────────────────────
/**
* Mirrors the old `hasUiGate` helper for backwards-compatible assertions.
* Returns 0 (UI found) or 1 (not found), matching grep exit code semantics.
*/
function hasUiGate(text) {
return checkUiPresence(text).hasUI ? 0 : 1;
}
/**
* Spawn the helper with shell:false, passing input via stdin.
* Returns the spawnSync result object.
*/
function spawnGate(input) {
return spawnSync(process.execPath, [HELPER_PATH], {
shell: false,
encoding: 'utf-8',
input: input,
});
}
// ── Structural guard — workflow files now invoke Node via stdin ───────────────
describe('Workflow .md structural guard (#3718)', () => {
// allow-test-rule: source-text-is-the-product (see #3706)
// The UI safety gate invocation is embedded in workflow prose-as-code.
// These structural tests guard against regression where someone re-introduces
// the shell-based locale-prefix invocation that silently breaks on Windows PowerShell.
// plan-phase.md (post-#1026): §5.6 now delegates UI gate evaluation to
// `gsd_run check ui-plan-gate` (a CLI command that internally calls checkUiPresence
// from ui-safety-gate.cjs). The direct shell invocation of ui-safety-gate.cjs was
// intentionally removed from the workflow — cross-shell portability is now provided
// by the CLI command layer, not inline shell code.
test('plan-phase.md must invoke check ui-plan-gate (capability-driven UI gate — #1026)', () => {
const content = fs.readFileSync(PLAN_PHASE_PATH, 'utf-8');
assert.ok(
content.includes('check ui-plan-gate'),
'plan-phase.md: §5.6 must delegate to `check ui-plan-gate` (capability-driven, #1026)'
);
assert.ok(
!content.includes('LC_ALL=C grep'),
'plan-phase.md: must NOT contain LC_ALL=C grep — that silently fails on Windows PowerShell (#3718)'
);
// Must NOT reintroduce the old shell-based path-search loop for ui-safety-gate.cjs
assert.ok(
!content.includes('UI_GATE_JS=$(for _c in'),
'plan-phase.md: must NOT contain the old shell-based ui-safety-gate.cjs path-search (old §5.6 pattern)'
);
});
// autonomous.md §3a.5 (post-#1031 cutover): §3a.5 now delegates to
// `loop render-hooks plan:pre` + `check ui-plan-gate` (capability-driven pattern,
// matching plan-phase.md §5.6). The direct shell invocation of ui-safety-gate.cjs
// was intentionally removed — cross-shell portability is provided by the CLI layer.
test('autonomous.md must invoke check ui-plan-gate (capability-driven UI gate — #1031)', () => {
const label = 'autonomous.md';
const content = readAutonomousCombined();
// §3a.5 must delegate to the capability-driven gate, not inline shell code
assert.ok(
content.includes('check ui-plan-gate'),
`${label}: §3a.5 must delegate to \`check ui-plan-gate\` (capability-driven, #1031)`
);
// §3a.5 must dispatch render-hooks plan:pre
assert.ok(
content.includes('loop render-hooks plan:pre'),
`${label}: §3a.5 must dispatch \`loop render-hooks plan:pre\` (capability hook resolution)`
);
// Must NOT reintroduce the old shell-based path-search loop for ui-safety-gate.cjs
assert.ok(
!content.includes('ui-safety-gate.cjs'),
`${label}: must NOT inline ui-safety-gate.cjs invocation — replaced by check ui-plan-gate (#1031)`
);
assert.ok(
!content.includes('UI_GATE_JS=$(for _c in'),
`${label}: must NOT contain the old shell-based ui-safety-gate.cjs path-search (old §3a.5 pattern)`
);
assert.ok(
!content.includes('LC_ALL=C grep'),
`${label}: must NOT contain LC_ALL=C grep — that silently fails on Windows PowerShell (#3718)`
);
});
});
// ── Cross-shell spawn test (#3718) ────────────────────────────────────────────
describe('Cross-shell portability — spawnSync with shell:false, stdin (#3718)', () => {
test('spawn with shell:false + stdin: UI input exits 0', () => {
const result = spawnGate('UI Refactor: migrate all screens');
assert.strictEqual(result.status, 0, `Expected exit 0 (UI found), got ${result.status}. stderr: ${result.stderr}`);
});
test('spawn with shell:false + stdin: non-UI input exits 1', () => {
const result = spawnGate('Requirements: backend REST API only');
assert.strictEqual(result.status, 1, `Expected exit 1 (no UI), got ${result.status}. stderr: ${result.stderr}`);
});
test('spawn with shell:false + stdin: empty input exits 1', () => {
const result = spawnGate('');
assert.strictEqual(result.status, 1, `Expected exit 1 (no UI for empty input), got ${result.status}. stderr: ${result.stderr}`);
});
test('spawn with shell:false + stdin: CRLF line endings handled correctly', () => {
const result = spawnGate('Deploy to the cloud platform\r\nand configure CI/CD.');
assert.strictEqual(result.status, 1, `CRLF "platform" must not trigger gate. stderr: ${result.stderr}`);
});
test('spawn with shell:false + stdin: multi-line input with UI token exits 0', () => {
const multiLine = 'Backend setup\nBuild the analytics dashboard\nand navigation component.';
const result = spawnGate(multiLine);
assert.strictEqual(result.status, 0, `Multi-line input with "dashboard" must exit 0. stderr: ${result.stderr}`);
});
test('spawn with shell:false + stdin: very large input (>100KB) is handled without error', () => {
// Verifies stdin transport does not hit ARG_MAX (argv transport fails above ~1MB on macOS).
// Fixture uses pure backend prose — no standalone UI tokens.
const largeInput = 'Backend service deployment and database migration step.\n'.repeat(3000);
const result = spawnGate(largeInput);
assert.strictEqual(result.status, 1, `Large non-UI input must exit 1, not crash. status: ${result.status}, stderr: ${result.stderr}`);
});
test('spawn with shell:false + stdin: large input with UI token exits 0', () => {
const largeUiInput = 'Backend infrastructure setup.\n'.repeat(2999) + 'Build the analytics dashboard.\n';
const result = spawnGate(largeUiInput);
assert.strictEqual(result.status, 0, `Large input ending with UI token must exit 0. stderr: ${result.stderr}`);
});
});
// ── Behavioral test matrix (via module import) ────────────────────────────────
/**
* Full fixture matrix — exercises the production checkUiPresence() function directly.
*/
function runBehavioralTests(label) {
describe(`${label} — UI gate behavioral matrix`, () => {
// ── False-positive tests (must NOT match) ──────────────────────────────
test('"Requirements" must NOT trigger UI gate (bug #3706 — "ui" substring)', () => {
const phaseSection =
'**Requirements**: The service must expose REST endpoints for authentication.\n' +
'All work is server-side. Database migrations and API contract only.';
assert.strictEqual(hasUiGate(phaseSection), 1, '"Requirements" must not match the UI gate');
});
test('"overview" must NOT trigger UI gate ("view" is a substring)', () => {
assert.strictEqual(hasUiGate('Overview of the data pipeline architecture and backend services.'), 1);
});
test('"performance" must NOT trigger UI gate ("form" is a substring)', () => {
assert.strictEqual(hasUiGate('Performance testing and benchmark analysis for the API layer.'), 1);
});
test('"platform" must NOT trigger UI gate ("form" is a substring)', () => {
assert.strictEqual(hasUiGate('Deploy to the cloud platform and configure CI/CD.'), 1);
});
test('"transform" must NOT trigger UI gate ("form" is a substring)', () => {
assert.strictEqual(hasUiGate('Transform raw event data and write to the warehouse.'), 1);
});
test('"review" must NOT trigger UI gate ("view" is a substring)', () => {
assert.strictEqual(hasUiGate('Code review checklist and PR approval workflow for the API.'), 1);
});
test('"build" must NOT trigger UI gate ("ui" at positions 2-3 of "build")', () => {
assert.strictEqual(hasUiGate('Build the backend service and run integration tests.'), 1);
});
test('"screening" must NOT trigger UI gate ("screen" is a substring)', () => {
assert.strictEqual(hasUiGate('Implement candidate screening criteria for the hiring pipeline.'), 1);
});
test('empty input does NOT trigger UI gate', () => {
assert.strictEqual(hasUiGate(''), 1);
});
test('whitespace-only input does NOT trigger UI gate', () => {
assert.strictEqual(hasUiGate(' \n \t '), 1);
});
test('compound "microfrontend" (no separator) does NOT trigger gate — documented behavior', () => {
assert.strictEqual(
hasUiGate('Build the microfrontend shell application.'), 1,
'"microfrontend" compound must NOT trigger gate'
);
});
// ── True-positive tests (must match) ──────────────────────────────────
test('standalone "UI" token DOES trigger UI gate', () => {
assert.strictEqual(hasUiGate('UI Refactor: migrate all screens to the new design system.'), 0);
});
test('standalone "view" token DOES trigger UI gate', () => {
assert.strictEqual(hasUiGate('Implement the user profile view controller and associated screen.'), 0);
});
test('standalone "form" token DOES trigger UI gate', () => {
assert.strictEqual(hasUiGate('Build a sign-up form with client-side validation.'), 0);
});
test('"dashboard" DOES trigger UI gate', () => {
assert.strictEqual(hasUiGate('Build the analytics dashboard and navigation component.'), 0);
});
test('lowercase "ui" token DOES trigger UI gate (case-insensitive)', () => {
assert.strictEqual(hasUiGate('Redesign the ui for mobile responsiveness.'), 0);
});
test('"non-UI" hyphenated form DOES trigger UI gate (hyphen is a word boundary)', () => {
assert.strictEqual(hasUiGate('This is a non-UI backend service with no visual elements.'), 0);
});
test('standalone "screen" token DOES trigger UI gate', () => {
assert.strictEqual(hasUiGate('Implement the loading screen and splash animation.'), 0);
});
test('hyphenated "micro-frontend" DOES trigger gate (word boundary on hyphen)', () => {
assert.strictEqual(hasUiGate('Build the micro-frontend shell application.'), 0);
});
// ── CRLF / edge case tests ─────────────────────────────────────────────
test('CRLF line endings: non-UI text does not trigger gate', () => {
assert.strictEqual(hasUiGate('Deploy to the cloud platform\r\nand configure CI/CD.'), 1);
});
test('CRLF line endings: UI token on second line triggers gate', () => {
assert.strictEqual(hasUiGate('Backend setup complete.\r\nBuild the analytics dashboard.'), 0);
});
test('leading/trailing whitespace does not affect token detection', () => {
assert.strictEqual(hasUiGate(' UI refactor phase '), 0);
});
});
}
runBehavioralTests('ui-safety-gate.cjs');
// ── Install-dir resolution from a consuming project (#448) ────────────────────
describe('UI gate resolves the helper against RUNTIME_DIR, not the consuming repo (#448)', () => {
const REPO_ROOT = path.join(__dirname, '..');
// Mirrors the §5.6 / §3a.5 resolution. The structural guard above forces the
// workflows to keep using this RUNTIME_DIR-anchored form; this proves the
// candidate path is correct and the helper is actually found + executed when
// the CWD is a consuming project that has no bin/lib of its own.
const GATE_SNIPPET = [
'_GSD_RT="${RUNTIME_DIR:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}"',
'UI_GATE_JS=$(for _c in "$_GSD_RT/gsd-core/bin/lib/ui-safety-gate.cjs" "$_GSD_RT/bin/lib/ui-safety-gate.cjs" "$_GSD_RT/.claude/bin/lib/ui-safety-gate.cjs" "$HOME/.claude/gsd-core/bin/lib/ui-safety-gate.cjs" "$HOME/.claude/bin/lib/ui-safety-gate.cjs"; do [ -f "$_c" ] && { echo "$_c"; break; }; done)',
'if [ -n "$UI_GATE_JS" ]; then printf \'%s\' "$PHASE_SECTION" | node "$UI_GATE_JS" >/dev/null 2>&1; HAS_UI=$?; else HAS_UI=0; fi',
'echo "$HAS_UI"',
].join('\n');
function runGateFrom(consumingDir, phaseSection) {
return spawnSync('bash', ['-c', GATE_SNIPPET], {
cwd: consumingDir,
encoding: 'utf-8',
env: { ...process.env, RUNTIME_DIR: REPO_ROOT, PHASE_SECTION: phaseSection },
});
}
test('UI text is detected (HAS_UI=0) from a project without bin/lib', () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-consuming-'));
try {
const res = runGateFrom(tmp, 'UI Refactor: migrate all screens');
assert.strictEqual(res.status, 0, `bash failed: ${res.stderr}`);
assert.strictEqual(res.stdout.trim(), '0',
'helper must be found via RUNTIME_DIR and report UI present — not silently no-op');
} finally {
cleanup(tmp);
}
});
test('non-UI text returns HAS_UI=1 via the RUNTIME_DIR-resolved helper', () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-consuming-'));
try {
const res = runGateFrom(tmp, 'Requirements: backend REST API only');
assert.strictEqual(res.stdout.trim(), '1');
} finally {
cleanup(tmp);
}
});
test('UI gate found via gsd-core/bin/lib/ in installed layout (no root bin/lib/)', () => {
// Regression for #448: installed RUNTIME_DIR has gsd-core/bin/lib/ but NOT root bin/lib/.
// The probe must find the helper at the installed path, not silently no-op to HAS_UI=0.
const fakeRuntime = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-installed-rt-'));
const consumingProject = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-consuming-'));
try {
const installedLibDir = path.join(fakeRuntime, 'gsd-core', 'bin', 'lib');
fs.mkdirSync(installedLibDir, { recursive: true });
fs.copyFileSync(
path.join(REPO_ROOT, 'gsd-core', 'bin', 'lib', 'ui-safety-gate.cjs'),
path.join(installedLibDir, 'ui-safety-gate.cjs')
);
const res = spawnSync('bash', ['-c', GATE_SNIPPET], {
cwd: consumingProject,
encoding: 'utf-8',
env: { ...process.env, RUNTIME_DIR: fakeRuntime, PHASE_SECTION: 'Build the analytics dashboard' },
});
assert.strictEqual(res.status, 0, `bash failed: ${res.stderr}`);
assert.strictEqual(res.stdout.trim(), '0',
'helper must be found via gsd-core/bin/lib/ in installed layout and report UI present');
} finally {
cleanup(fakeRuntime);
cleanup(consumingProject);
}
});
});
// ── checkUiPresence() return value API ───────────────────────────────────────
describe('checkUiPresence() return value API', () => {
test('returns { hasUI: true, tokens: [...] } when UI found', () => {
const result = checkUiPresence('Build the dashboard and UI form');
assert.strictEqual(result.hasUI, true);
assert.ok(Array.isArray(result.tokens), 'tokens must be an array');
assert.ok(result.tokens.length > 0, 'tokens must be non-empty when hasUI is true');
assert.ok(result.tokens.includes('dashboard') || result.tokens.includes('ui') || result.tokens.includes('form'));
});
test('returns { hasUI: false, tokens: [] } when no UI found', () => {
const result = checkUiPresence('Requirements: backend REST API only');
assert.strictEqual(result.hasUI, false);
assert.deepStrictEqual(result.tokens, []);
});
test('tokens are lowercased', () => {
const result = checkUiPresence('UI Refactor');
assert.ok(result.tokens.every(t => t === t.toLowerCase()), 'All tokens must be lowercase');
});
test('tokens are deduplicated', () => {
const result = checkUiPresence('UI redesign and ui cleanup');
const uiCount = result.tokens.filter(t => t === 'ui').length;
assert.strictEqual(uiCount, 1, 'Duplicate tokens must be deduplicated');
});
test('non-string input returns { hasUI: false, tokens: [] }', () => {
assert.deepStrictEqual(checkUiPresence(null), { hasUI: false, tokens: [] });
assert.deepStrictEqual(checkUiPresence(undefined), { hasUI: false, tokens: [] });
assert.deepStrictEqual(checkUiPresence(42), { hasUI: false, tokens: [] });
});
test('multiple distinct UI tokens on same line are ALL captured', () => {
// "form" and "view" both appear as standalone words on one line.
// Prior exec()-based impl only captured the first match per line.
const result = checkUiPresence('Build a sign-up form with a view controller');
assert.strictEqual(result.hasUI, true, 'hasUI must be true');
assert.ok(result.tokens.includes('form'), `Expected "form" in tokens, got: ${JSON.stringify(result.tokens)}`);
assert.ok(result.tokens.includes('view'), `Expected "view" in tokens, got: ${JSON.stringify(result.tokens)}`);
});
});
});
}