Files
msd-core/tests/core-utils.test.cjs
Cody Anderson 612fcb00f7 fix(#2232): cap phase-token continuation segments at exactly 2 digits (all sites) (#2254)
* fix(#2232): cap phase-token continuation segments at exactly 2 digits (all sites)

A phase whose slug's first word is a ≥2-digit number (dir
14-2026-photos-performance, roadmap phase "2026 Photos & Performance" →
slug 2026-photos-…) had its phase token over-collected as "14-2026"
instead of "14", so every phase-locating verb (init.plan-phase,
init.execute-phase, phase-plan-index, state.planned-phase,
roadmap.annotate-dependencies) resolved phase_dir=null / plan_count=0
while the directory existed. This is the residual case #2043 explicitly
scoped out: its ≥2-digit continuation gate (\d{2,}) distinguishes
single-digit slug words but not multi-digit ones (years, counts).

The structural distinguisher: getPhaseDirFromPhaseId writes sub-phase and
plan continuation segments zero-padded to EXACTLY 2 digits, so a genuine
continuation's digit run is exactly 2 — \d{2}(?!\d). The (?!\d) guard
caps the run without anchoring what follows, so each call site keeps its
own trailing grammar (letter suffixes, dotted sub-phases, boundaries).

Shared-source, not hand-synced: the grammar lives once in phase-id.cts as
PHASE_CONTINUATION_SEGMENT_SOURCE / isPhaseContinuationSegment (the #2121
single-owner seam), consumed by all five #2043 sites:
- phase-id.cts extractPhaseToken (the reported repro)
- validate.cts PHASE_TOKEN_FROM_DIR_RE + canonicalPlanStem
- roadmap-parser.cts isDirInMilestone numericRe (hyphenated mode)
- core-utils.cts + phase.cts extractCanonicalPlanId (paired plan
  component only — the LEADING phase component keeps unbounded \d{2,};
  phase numbers ≥100 are legitimate)

Digit-width policy, resolved per triage and locked by boundary tests at
1/2/3/4-digit continuation widths across all sites: sub-phase/plan
numbers ≥100 are out of the dir-token grammar. validate.cts
phaseDirNameRe's leading \d{2,} is intentionally untouched — it encodes
the write-side padding of the leading dir number, not the continuation
heuristic, and has no year collision.

Fixes #2232

Claude-Session: https://claude.ai/code/session_017KaYUJnfzV3JVVuQnhkcjg

* chore(#2232): add changeset for PR #2254

Claude-Session: https://claude.ai/code/session_017KaYUJnfzV3JVVuQnhkcjg

* test(#2232): parity gate + fast-check properties for the continuation cap

Addresses trek-e's review on PR #2254 (M1, M2, B1). Test-only — the fix
itself was verified as a true root-cause fix, so no source changes.

M1 — drift/parity enforcement for the new shared constant.
scripts/lint-phase-id-drift.cjs guards PHASE_NUMBER_TOKEN_SOURCE only; its
TOKEN_DRIFT_RE cannot match a bare \d{2,} re-derivation, so a future edit
reintroducing a raw digit-cap at a consuming site would pass lint + CI
silently. Extending the lint was rejected: \d{2,} legitimately appears at
the intentionally-unbounded LEADING-token sites (validate phaseDirNameRe,
core-utils/phase tokenRe), so a textual guard would need sanctions on
correct code and would flag by spelling rather than by behaviour.

Instead, per the repo's *-parity.test.cjs precedent, added
tests/phase-continuation-parity.test.cjs: a shared digit-width corpus
(1/2/3/4/5) asserting every consuming surface's notion of "is this segment
absorbed" equals isPhaseContinuationSegment(). Covers all five #2043 sites:
extractPhaseToken, PHASE_TOKEN_FROM_DIR_RE, canonicalPlanStem,
extractCanonicalPlanId (paired component), and roadmap isDirInMilestone
(hyphenated mode, on a real ROADMAP fixture). The corpus states the policy
independently of the regex, so it fails on divergence rather than mirroring
whatever the code does.

Failing-first verified: reverting PHASE_TOKEN_FROM_DIR_RE to \d{2,} fails 3
parity tests; reverting the owner constant itself fails 11 across parity +
properties + examples.

M2 — fast-check properties for the changed parser (4 added to
phase-id.test.cjs, following its existing inline fc precedent):
- biconditional: a segment is absorbed IFF its digit run is exactly 2
- the owner agrees with observable extraction for every digit run
- metamorphic: a write-side getPhaseDirFromPhaseId dir round-trips to its
  own normalizePhaseName id — ties the cap to the zero-padding convention
  it mirrors, so a change to the write-side width fails loudly
- metamorphic: the round-trip holds when the phase name leads with a year
  (the #2232 bug itself, generatively)
Digit runs are generated as digit strings (not String(int)) so leading-zero
forms like "02" — the whole point of the rule — are actually exercised.

B1 — GitGuardian red. The session-trailer hypothesis is disproven: the same
Claude-Session trailer rides 3 commits now merged to next via #2173, whose
GitGuardian check PASSED. GitGuardian's own comment names
tests/phase-id.test.cjs:260 — the synthetic dir literal 'M1-14-2026-photos'
tripping the generic high-entropy detector. Composed it from parts; the
assertion is unchanged, only the source spelling.

Refs #2232

Claude-Session: https://claude.ai/code/session_019SkiJk38YWAbmxHrGxEmuU

* test(#2232): name the parity gate after the invariant, not the phase module

CI caught two failures from the new parity test, both one root cause:
lint-test-file-count caps each production module at 2 test files (primary +
one integration, per the #3740 consolidation). The file was named
phase-continuation-parity.test.cjs, and the linter clusters a test to a
production module by name prefix — "phase-*" bound it to src/phase.cts,
whose cluster (phase.test.cjs + phase-dependency-levels.test.cjs) was
already at the cap, making 3. That tripped the lint-tests job AND the
ubuntu-24 unit lane, where tests/lint-test-file-count.test.cjs is a
meta-test asserting the linter exits 0 against the real repo.

Renamed to continuation-grammar-parity.test.cjs, matching the convention
the repo's other cross-cutting parity gates already follow: they are named
after the INVARIANT, not a module — capability-precedence-parity,
agent-classification-parity, and runtime-launcher-parity all have no
corresponding src/*.cts, so they cluster to nothing. The gate tests a
grammar shared ACROSS phase-id/validate/core-utils/roadmap-parser rather
than the phase module specifically, so the invariant-name is also the
semantically correct home. Not allowlisted: a novel offender belongs under
the cap, not ratcheted into the exemption list.

Content unchanged — same 12 assertions across the same 5 surfaces.

Refs #2232

Claude-Session: https://claude.ai/code/session_019SkiJk38YWAbmxHrGxEmuU

---------

Co-authored-by: Tom Boucher <trekkie@nomorestars.com>
2026-07-15 15:33:58 -04:00

630 lines
26 KiB
JavaScript

/**
* Tests for src/core-utils.cts (compiled to gsd-core/bin/lib/core-utils.cjs).
*
* Verifies behavioural contracts of the utilities extracted from core.cjs
* per ADR-857 rollout phase 2c (#877):
* - toPosixPath
* - detectSubRepos
* - extractOneLinerFromBody
* - pathExistsInternal
* - generateSlugInternal
* - filterPlanFiles
* - filterSummaryFiles
* - getPhaseFileStats
* - readSubdirectories
* - timeAgo
* - extractCanonicalPlanId (private — only via coreUtils, NOT via core)
* - core.cjs re-export shims resolve to the exact same functions (shim-identity)
*
* Adversarial inputs per QA matrix: path-traversal-like names, unicode,
* decimal phase ids, missing/empty dirs, fs edge cases.
* Uses helpers.cjs createTempProject/cleanup for filesystem tests.
*/
'use strict';
const { test, describe, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const coreUtils = require('../gsd-core/bin/lib/core-utils.cjs');
const { cleanup } = require('./helpers.cjs');
// ─── toPosixPath ─────────────────────────────────────────────────────────────
describe('toPosixPath', () => {
test('forward-slash paths are unchanged', () => {
assert.strictEqual(coreUtils.toPosixPath('foo/bar/baz'), 'foo/bar/baz');
});
test('empty string returns empty string', () => {
assert.strictEqual(coreUtils.toPosixPath(''), '');
});
test('single segment (no separators) is unchanged', () => {
assert.strictEqual(coreUtils.toPosixPath('file.txt'), 'file.txt');
});
test('platform path.sep is normalized to /', () => {
// On POSIX this is a no-op; on Windows it converts backslashes.
const sep = path.sep;
const p = ['a', 'b', 'c'].join(sep);
assert.strictEqual(coreUtils.toPosixPath(p), 'a/b/c');
});
test('adversarial: path-traversal-like string with backslash separators', () => {
// On POSIX, path.sep === '/' so backslashes are treated as literal characters
// and toPosixPath leaves them as-is (split on '/' only finds one token).
// On Windows (where path.sep === '\\'), backslashes would be normalized to '/'.
// Either way, the result is a string and does not throw.
const result = coreUtils.toPosixPath('..\\..\\etc\\passwd');
assert.strictEqual(typeof result, 'string');
if (path.sep === '\\') {
// Windows: separators normalized
assert.ok(result.includes('/'));
assert.ok(!result.includes('\\'));
} else {
// POSIX: backslash is a literal char, not a separator
assert.ok(result.includes('\\'));
}
});
test('unicode in path segments passes through', () => {
const result = coreUtils.toPosixPath('中文/path/to/file');
assert.strictEqual(result, '中文/path/to/file');
});
});
// ─── detectSubRepos ───────────────────────────────────────────────────────────
describe('detectSubRepos', () => {
let tmpDir;
afterEach(() => { if (tmpDir) { cleanup(tmpDir); tmpDir = null; } });
test('returns empty array for directory with no sub-repos', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
assert.deepEqual(coreUtils.detectSubRepos(tmpDir), []);
});
test('returns empty array for non-existent directory', () => {
assert.deepEqual(coreUtils.detectSubRepos('/nonexistent-path-xyz-' + Date.now()), []);
});
test('detects directory with .git as sub-repo', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
const subDir = path.join(tmpDir, 'myrepo');
fs.mkdirSync(subDir);
fs.mkdirSync(path.join(subDir, '.git'));
assert.deepEqual(coreUtils.detectSubRepos(tmpDir), ['myrepo']);
});
test('excludes hidden directories', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
const hiddenDir = path.join(tmpDir, '.hidden');
fs.mkdirSync(hiddenDir);
fs.mkdirSync(path.join(hiddenDir, '.git'));
assert.deepEqual(coreUtils.detectSubRepos(tmpDir), []);
});
test('excludes node_modules', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
const nmDir = path.join(tmpDir, 'node_modules');
fs.mkdirSync(nmDir);
fs.mkdirSync(path.join(nmDir, '.git'));
assert.deepEqual(coreUtils.detectSubRepos(tmpDir), []);
});
test('returns sorted results for multiple sub-repos', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
for (const name of ['z-repo', 'a-repo', 'm-repo']) {
const subDir = path.join(tmpDir, name);
fs.mkdirSync(subDir);
fs.mkdirSync(path.join(subDir, '.git'));
}
assert.deepEqual(coreUtils.detectSubRepos(tmpDir), ['a-repo', 'm-repo', 'z-repo']);
});
test('adversarial: directory name with path-traversal-like characters', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
// Create a subdirectory that doesn't start with '.' and isn't node_modules
const subDir = path.join(tmpDir, 'normal-dir');
fs.mkdirSync(subDir);
// No .git, so not a sub-repo
assert.deepEqual(coreUtils.detectSubRepos(tmpDir), []);
});
});
// ─── pathExistsInternal ───────────────────────────────────────────────────────
describe('pathExistsInternal', () => {
let tmpDir;
afterEach(() => { if (tmpDir) { cleanup(tmpDir); tmpDir = null; } });
test('returns true for an existing file', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
const fp = path.join(tmpDir, 'file.txt');
fs.writeFileSync(fp, 'hello');
assert.strictEqual(coreUtils.pathExistsInternal(tmpDir, 'file.txt'), true);
});
test('returns true for an existing directory', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
const subDir = path.join(tmpDir, 'subdir');
fs.mkdirSync(subDir);
assert.strictEqual(coreUtils.pathExistsInternal(tmpDir, 'subdir'), true);
});
test('returns false for a non-existent path', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
assert.strictEqual(coreUtils.pathExistsInternal(tmpDir, 'nope.txt'), false);
});
test('handles absolute targetPath', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
assert.strictEqual(coreUtils.pathExistsInternal(tmpDir, tmpDir), true);
});
test('adversarial: path traversal attempt returns false (no such file)', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
// Traversal resolves via path.join — no crash, just correct false/true
const result = coreUtils.pathExistsInternal(tmpDir, '../nonexistent');
assert.strictEqual(typeof result, 'boolean');
});
});
// ─── generateSlugInternal ─────────────────────────────────────────────────────
describe('generateSlugInternal', () => {
test('null → null', () => {
assert.strictEqual(coreUtils.generateSlugInternal(null), null);
});
test('undefined → null', () => {
assert.strictEqual(coreUtils.generateSlugInternal(undefined), null);
});
test('empty string → null', () => {
assert.strictEqual(coreUtils.generateSlugInternal(''), null);
});
test('lowercases and replaces non-alphanumeric with hyphens', () => {
assert.strictEqual(coreUtils.generateSlugInternal('Hello World!'), 'hello-world');
});
test('strips leading and trailing hyphens', () => {
assert.strictEqual(coreUtils.generateSlugInternal(' Hello '), 'hello');
});
test('truncates at 60 characters', () => {
const long = 'a'.repeat(100);
const result = coreUtils.generateSlugInternal(long);
assert.ok(result !== null && result.length <= 60);
});
test('unicode characters are replaced with hyphens', () => {
const result = coreUtils.generateSlugInternal('中文phase');
assert.ok(typeof result === 'string');
assert.ok(!result.includes('中'));
});
test('preserves numbers in slug', () => {
assert.strictEqual(coreUtils.generateSlugInternal('Phase 42 Done'), 'phase-42-done');
});
});
// ─── filterPlanFiles ──────────────────────────────────────────────────────────
describe('filterPlanFiles', () => {
test('returns only PLAN.md and *-PLAN.md files', () => {
const files = ['PLAN.md', '01-PLAN.md', 'SUMMARY.md', 'README.md', 'foo-PLAN.md'];
assert.deepEqual(coreUtils.filterPlanFiles(files), ['PLAN.md', '01-PLAN.md', 'foo-PLAN.md']);
});
test('empty array → empty array', () => {
assert.deepEqual(coreUtils.filterPlanFiles([]), []);
});
test('no matching files → empty array', () => {
assert.deepEqual(coreUtils.filterPlanFiles(['SUMMARY.md', 'CONTEXT.md']), []);
});
test('case-sensitive: plan.md is not matched', () => {
assert.deepEqual(coreUtils.filterPlanFiles(['plan.md', 'Plan.md']), []);
});
});
// ─── filterSummaryFiles ───────────────────────────────────────────────────────
describe('filterSummaryFiles', () => {
test('returns only SUMMARY.md and *-SUMMARY.md files', () => {
const files = ['SUMMARY.md', '01-SUMMARY.md', 'PLAN.md', 'foo-SUMMARY.md'];
assert.deepEqual(coreUtils.filterSummaryFiles(files), ['SUMMARY.md', '01-SUMMARY.md', 'foo-SUMMARY.md']);
});
test('empty array → empty array', () => {
assert.deepEqual(coreUtils.filterSummaryFiles([]), []);
});
test('no matching files → empty array', () => {
assert.deepEqual(coreUtils.filterSummaryFiles(['PLAN.md', 'CONTEXT.md']), []);
});
});
// ─── readSubdirectories ───────────────────────────────────────────────────────
describe('readSubdirectories', () => {
let tmpDir;
afterEach(() => { if (tmpDir) { cleanup(tmpDir); tmpDir = null; } });
test('returns [] for non-existent directory', () => {
assert.deepEqual(coreUtils.readSubdirectories('/nonexistent-xyz-' + Date.now()), []);
});
test('returns [] for empty directory', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
assert.deepEqual(coreUtils.readSubdirectories(tmpDir), []);
});
test('returns only directory names, not files', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
fs.mkdirSync(path.join(tmpDir, 'subdir'));
fs.writeFileSync(path.join(tmpDir, 'file.txt'), '');
const result = coreUtils.readSubdirectories(tmpDir);
assert.deepEqual(result, ['subdir']);
});
test('sort=false returns dirs in filesystem order', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
fs.mkdirSync(path.join(tmpDir, '02-phase'));
fs.mkdirSync(path.join(tmpDir, '01-phase'));
const result = coreUtils.readSubdirectories(tmpDir, false);
assert.strictEqual(result.length, 2);
});
test('sort=true orders by comparePhaseNum', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
for (const name of ['10-phase', '02-phase', '01-phase']) {
fs.mkdirSync(path.join(tmpDir, name));
}
const result = coreUtils.readSubdirectories(tmpDir, true);
assert.deepEqual(result, ['01-phase', '02-phase', '10-phase']);
});
test('sort=true handles decimal phase ids correctly', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
for (const name of ['01.2-phase', '01.10-phase', '01.1-phase']) {
fs.mkdirSync(path.join(tmpDir, name));
}
const result = coreUtils.readSubdirectories(tmpDir, true);
// Decimal ordering: 01.1 < 01.2 < 01.10
assert.deepEqual(result, ['01.1-phase', '01.2-phase', '01.10-phase']);
});
});
// ─── getPhaseFileStats ────────────────────────────────────────────────────────
describe('getPhaseFileStats', () => {
let tmpDir;
afterEach(() => { if (tmpDir) { cleanup(tmpDir); tmpDir = null; } });
test('returns empty arrays and false flags for empty directory', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
const stats = coreUtils.getPhaseFileStats(tmpDir);
assert.deepEqual(stats.plans, []);
assert.deepEqual(stats.summaries, []);
assert.strictEqual(stats.hasResearch, false);
assert.strictEqual(stats.hasContext, false);
assert.strictEqual(stats.hasVerification, false);
assert.strictEqual(stats.hasReviews, false);
});
test('detects PLAN.md files', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
fs.writeFileSync(path.join(tmpDir, 'PLAN.md'), '');
fs.writeFileSync(path.join(tmpDir, '01-PLAN.md'), '');
const stats = coreUtils.getPhaseFileStats(tmpDir);
assert.deepEqual(stats.plans.sort(), ['01-PLAN.md', 'PLAN.md']);
});
test('detects SUMMARY.md files', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
fs.writeFileSync(path.join(tmpDir, 'SUMMARY.md'), '');
const stats = coreUtils.getPhaseFileStats(tmpDir);
assert.deepEqual(stats.summaries, ['SUMMARY.md']);
});
test('detects RESEARCH.md', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
fs.writeFileSync(path.join(tmpDir, 'RESEARCH.md'), '');
const stats = coreUtils.getPhaseFileStats(tmpDir);
assert.strictEqual(stats.hasResearch, true);
});
test('detects *-RESEARCH.md', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
fs.writeFileSync(path.join(tmpDir, 'feature-RESEARCH.md'), '');
const stats = coreUtils.getPhaseFileStats(tmpDir);
assert.strictEqual(stats.hasResearch, true);
});
test('detects VERIFICATION.md', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
fs.writeFileSync(path.join(tmpDir, 'VERIFICATION.md'), '');
const stats = coreUtils.getPhaseFileStats(tmpDir);
assert.strictEqual(stats.hasVerification, true);
});
test('detects REVIEWS.md', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
fs.writeFileSync(path.join(tmpDir, 'REVIEWS.md'), '');
const stats = coreUtils.getPhaseFileStats(tmpDir);
assert.strictEqual(stats.hasReviews, true);
});
test('detects CONTEXT.md via findContextMdIn', () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cu-test-'));
fs.writeFileSync(path.join(tmpDir, 'CONTEXT.md'), '');
const stats = coreUtils.getPhaseFileStats(tmpDir);
assert.strictEqual(stats.hasContext, true);
});
});
// ─── extractOneLinerFromBody ──────────────────────────────────────────────────
describe('extractOneLinerFromBody', () => {
test('null → null', () => {
assert.strictEqual(coreUtils.extractOneLinerFromBody(null), null);
});
test('undefined → null', () => {
assert.strictEqual(coreUtils.extractOneLinerFromBody(undefined), null);
});
test('empty string → null', () => {
assert.strictEqual(coreUtils.extractOneLinerFromBody(''), null);
});
test('extracts bold text after a heading as one-liner', () => {
const content = '# Phase Title\n\n**Implement the feature**\n\nMore details here.\n';
assert.strictEqual(coreUtils.extractOneLinerFromBody(content), 'Implement the feature');
});
test('returns null when no bold text after heading', () => {
const content = '# Phase Title\n\nSome prose without bold.\n';
assert.strictEqual(coreUtils.extractOneLinerFromBody(content), null);
});
test('strips frontmatter before searching', () => {
const content = '---\nstatus: done\n---\n# Title\n\n**One liner here**\n';
assert.strictEqual(coreUtils.extractOneLinerFromBody(content), 'One liner here');
});
test('when bold ends with colon, returns text after the bold', () => {
const content = '# Title\n\n**Objective:** Complete the work\n';
assert.strictEqual(coreUtils.extractOneLinerFromBody(content), 'Complete the work');
});
test('CRLF line endings are normalized', () => {
const content = '# Title\r\n\r\n**Bold line**\r\nmore\r\n';
assert.strictEqual(coreUtils.extractOneLinerFromBody(content), 'Bold line');
});
test('adversarial: unicode in bold text', () => {
const content = '# Title\n\n**中文 one-liner**\n\nMore.\n';
assert.strictEqual(coreUtils.extractOneLinerFromBody(content), '中文 one-liner');
});
});
// ─── timeAgo ─────────────────────────────────────────────────────────────────
describe('timeAgo', () => {
function daysAgo(n) {
return new Date(Date.now() - n * 24 * 60 * 60 * 1000);
}
function minutesAgo(n) {
return new Date(Date.now() - n * 60 * 1000);
}
function hoursAgo(n) {
return new Date(Date.now() - n * 60 * 60 * 1000);
}
function secondsAgo(n) {
return new Date(Date.now() - n * 1000);
}
test('"just now" for < 5 seconds', () => {
assert.strictEqual(coreUtils.timeAgo(secondsAgo(2)), 'just now');
});
test('"X seconds ago" for < 60 seconds', () => {
const result = coreUtils.timeAgo(secondsAgo(30));
assert.ok(result.endsWith('seconds ago'), `Expected "X seconds ago", got: ${result}`);
});
test('"1 minute ago" for ~1 minute', () => {
assert.strictEqual(coreUtils.timeAgo(minutesAgo(1)), '1 minute ago');
});
test('"X minutes ago" for < 60 minutes', () => {
const result = coreUtils.timeAgo(minutesAgo(30));
assert.ok(result.endsWith('minutes ago'), `Expected "X minutes ago", got: ${result}`);
});
test('"1 hour ago" for ~1 hour', () => {
assert.strictEqual(coreUtils.timeAgo(hoursAgo(1)), '1 hour ago');
});
test('"X hours ago" for < 24 hours', () => {
const result = coreUtils.timeAgo(hoursAgo(10));
assert.ok(result.endsWith('hours ago'), `Expected "X hours ago", got: ${result}`);
});
test('"1 day ago" for ~1 day', () => {
assert.strictEqual(coreUtils.timeAgo(daysAgo(1)), '1 day ago');
});
test('"X days ago" for < 30 days', () => {
const result = coreUtils.timeAgo(daysAgo(15));
assert.ok(result.endsWith('days ago'), `Expected "X days ago", got: ${result}`);
});
test('"1 month ago" for ~30 days', () => {
assert.strictEqual(coreUtils.timeAgo(daysAgo(30)), '1 month ago');
});
test('"X months ago" for < 12 months', () => {
const result = coreUtils.timeAgo(daysAgo(180));
assert.ok(result.endsWith('months ago'), `Expected "X months ago", got: ${result}`);
});
test('"1 year ago" for ~365 days', () => {
assert.strictEqual(coreUtils.timeAgo(daysAgo(365)), '1 year ago');
});
test('"X years ago" for multiple years', () => {
const result = coreUtils.timeAgo(daysAgo(730));
assert.ok(result.endsWith('years ago'), `Expected "X years ago", got: ${result}`);
});
});
// ─── extractCanonicalPlanId ───────────────────────────────────────────────────
describe('extractCanonicalPlanId', () => {
test('strips -PLAN.md suffix and returns basename', () => {
// '01-feature-PLAN.md' → base = '01-feature', no two adjacent phase tokens
assert.strictEqual(coreUtils.extractCanonicalPlanId('01-feature-PLAN.md'), '01-feature');
});
test('strips -SUMMARY.md suffix', () => {
assert.strictEqual(coreUtils.extractCanonicalPlanId('01-SUMMARY.md'), '01');
});
test('strips .md suffix for plain md file', () => {
assert.strictEqual(coreUtils.extractCanonicalPlanId('01.md'), '01');
});
test('returns base when no phase token found', () => {
assert.strictEqual(coreUtils.extractCanonicalPlanId('no-phase-token.md'), 'no-phase-token');
});
test('extracts canonical id with two adjacent phase tokens', () => {
// e.g. phase 01 plan 02: filename = "01-02-PLAN.md"
const result = coreUtils.extractCanonicalPlanId('01-02-PLAN.md');
assert.strictEqual(result, '01-02');
});
test('adversarial: decimal phase id tokens', () => {
// "01.1" matches the token regex (\d+[A-Z]?(\.\d+)*)
const result = coreUtils.extractCanonicalPlanId('01.1-PLAN.md');
assert.ok(typeof result === 'string');
});
test('adversarial: unicode filename returns some string', () => {
const result = coreUtils.extractCanonicalPlanId('中文-phase.md');
assert.ok(typeof result === 'string');
});
test('adversarial: path-traversal-like filename treated as literal', () => {
// extractCanonicalPlanId operates on a filename string (not a real path).
// The function does not sanitize slashes — it strips .md suffixes and
// attempts to find phase tokens. The result is a string (no crash).
const result = coreUtils.extractCanonicalPlanId('../../../etc/passwd');
assert.ok(typeof result === 'string');
assert.ok(result.length > 0);
});
test('rejects single-digit slug word (#2043)', () => {
// "46-6-rs-pipeline-orchestrator" is not a valid phase-token filename shape
// (the "6" is a slug word, not a sub-phase segment) — must not collapse to
// the pre-fix, buggy "46-6".
assert.notStrictEqual(
coreUtils.extractCanonicalPlanId('46-6-rs-pipeline-orchestrator'),
'46-6',
);
// Legit multi-segment phase-token filenames are still extracted correctly,
// including a single-digit letter-suffix phase id ("3A") — the "3A" token is
// found and paired with its zero-padded plan index, not left unpaired.
assert.strictEqual(coreUtils.extractCanonicalPlanId('01-02-PLAN.md'), '01-02');
assert.strictEqual(coreUtils.extractCanonicalPlanId('3A-01-feature-PLAN.md'), '3A-01');
});
test('does not pair a ≥3-digit slug word as a plan component (#2232)', () => {
// A year-leading slug word ("14-2026-photos-…") is not a plan component —
// must not collapse to the bogus "14-2026".
assert.notStrictEqual(
coreUtils.extractCanonicalPlanId('14-2026-photos-performance-SUMMARY.md'),
'14-2026',
);
assert.notStrictEqual(coreUtils.extractCanonicalPlanId('05-100-slug-PLAN.md'), '05-100');
// The LEADING phase component stays unbounded (\d{2,}) — only the paired
// continuation is width-capped, so phase ≥100 plan files still pair.
assert.strictEqual(coreUtils.extractCanonicalPlanId('100-01-extra-slug-PLAN.md'), '100-01');
assert.strictEqual(coreUtils.extractCanonicalPlanId('01-02-PLAN.md'), '01-02');
});
});
// ─── countMatchedSummaries (#1988) ───────────────────────────────────────────
// Lives in core-utils.test.cjs (not roadmap.test.cjs) so the Stryker core-utils
// shard — which runs only this file — actually covers the helper's mutants.
describe('countMatchedSummaries — stray non-plan summaries excluded (#1988)', () => {
const { countMatchedSummaries } = coreUtils;
test('counts only summaries that are the PLAN→SUMMARY partner of a plan', () => {
const plans = ['30-01-PLAN.md', '30-02-PLAN.md', '30-10-PLAN.md'];
const summaries = ['30-01-SUMMARY.md', '30-FIX-CR02-SUMMARY.md', '30-GAPCLOSURE-SUMMARY.md'];
assert.strictEqual(countMatchedSummaries(plans, summaries), 1);
});
test('all plans have a partner summary → counts every plan', () => {
const plans = ['01-01-PLAN.md', '01-02-PLAN.md'];
const summaries = ['01-01-SUMMARY.md', '01-02-SUMMARY.md'];
assert.strictEqual(countMatchedSummaries(plans, summaries), 2);
});
test('nested layout pairing preserved (plans/PLAN-NN ↔ plans/SUMMARY-NN)', () => {
const plans = ['plans/PLAN-01.md', 'plans/PLAN-02.md'];
const summaries = ['plans/SUMMARY-01.md'];
assert.strictEqual(countMatchedSummaries(plans, summaries), 1);
});
test('extended layout pairing (N-PLAN-MM-slug ↔ N-MM-SUMMARY)', () => {
const plans = ['3-PLAN-01-setup.md', '5-PLAN-02-migrations.md'];
const summaries = ['3-01-SUMMARY.md', '5-02-SUMMARY.md'];
assert.strictEqual(countMatchedSummaries(plans, summaries), 2);
});
test('extended layout: only the matching plan counts (no cross-pairing)', () => {
const plans = ['3-PLAN-01-setup.md', '3-PLAN-02-seed.md'];
const summaries = ['3-01-SUMMARY.md']; // only plan 01 has a summary
assert.strictEqual(countMatchedSummaries(plans, summaries), 1);
});
test('bare PLAN.md ↔ SUMMARY.md', () => {
assert.strictEqual(countMatchedSummaries(['PLAN.md'], ['SUMMARY.md']), 1);
assert.strictEqual(countMatchedSummaries(['PLAN.md'], []), 0);
});
test('bare PLAN.md ↔ PLAN-SUMMARY.md (stem-suffix convention)', () => {
assert.strictEqual(countMatchedSummaries(['PLAN.md'], ['PLAN-SUMMARY.md']), 1);
});
test('legacy <N>-PLAN-<NN> ↔ <N>-PLAN-<NN>-SUMMARY', () => {
const plans = ['14-PLAN-01.md', '14-PLAN-02.md'];
const summaries = ['14-PLAN-01-SUMMARY.md', '14-PLAN-02-SUMMARY.md'];
assert.strictEqual(countMatchedSummaries(plans, summaries), 2);
});
test('stray summaries never count when no plan partners exist', () => {
const plans = ['30-01-PLAN.md'];
const strays = ['30-FIX-CR02-SUMMARY.md', '30-GAPCLOSURE-SUMMARY.md', '30-01-EXTRA-SUMMARY.md'];
assert.strictEqual(countMatchedSummaries(plans, strays), 0);
});
test('absolute path (leading slash) still pairs via the dir split', () => {
// Guards the lastIndexOf('/') >= 0 boundary (slash at index 0).
assert.strictEqual(countMatchedSummaries(['/abs/PLAN-01.md'], ['/abs/SUMMARY-01.md']), 1);
});
});