Merge pull request #3607 from gsd-build/feat/3592-test-rewrite-text-existence-checks-into-

test: rewrite alias coverage as behavioral contract
This commit is contained in:
Tom Boucher
2026-05-15 23:11:34 -04:00
committed by GitHub
9 changed files with 440 additions and 2 deletions

View File

@@ -10,7 +10,11 @@
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const crypto = require('node:crypto');
const fs = require('node:fs');
const os = require('node:os');
const path = require('path');
const { spawnSync } = require('node:child_process');
const REPO_ROOT = path.join(__dirname, '..');
const COMMAND_ALIASES_FILE = path.join(
@@ -20,6 +24,7 @@ const COMMAND_ALIASES_FILE = path.join(
'lib',
'command-aliases.generated.cjs',
);
const GSD_TOOLS = path.join(REPO_ROOT, 'get-shit-done', 'bin', 'gsd-tools.cjs');
const MISSING_14 = [
'check.decision-coverage-plan',
@@ -126,3 +131,146 @@ describe('feat-3251: command-aliases.generated.cjs manifest coverage', () => {
);
});
});
function createProject() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3251-dispatch-'));
fs.mkdirSync(path.join(dir, '.planning', 'phases'), { recursive: true });
return dir;
}
function runGsdTools(args, projectDir) {
return spawnSync(process.execPath, [GSD_TOOLS, ...args], {
cwd: projectDir,
encoding: 'utf8',
timeout: 30000,
killSignal: 'SIGKILL',
});
}
function snapshotProjectState(projectDir) {
const files = [];
function walk(dir) {
if (!fs.existsSync(dir)) return;
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
const rel = path.relative(projectDir, full);
if (entry.isDirectory()) walk(full);
else {
files.push({
path: rel,
sha256: crypto.createHash('sha256').update(fs.readFileSync(full)).digest('hex'),
});
}
}
}
walk(projectDir);
return files.sort((a, b) => a.path.localeCompare(b.path));
}
describe('feat-3251: generated aliases dispatch through real gsd-tools behavior', () => {
test('phase.mvp-mode spaced alias resolves CLI flag precedence', () => {
const projectDir = createProject();
try {
const result = runGsdTools(['phase', 'mvp-mode', '1', '--cli-flag'], projectDir);
assert.equal(result.status, 0, result.stderr);
const output = JSON.parse(result.stdout);
assert.deepEqual(output, {
active: true,
source: 'cli_flag',
roadmap_mode: null,
config_mvp_mode: false,
cli_flag_present: true,
});
} finally {
fs.rmSync(projectDir, { recursive: true, force: true });
}
});
test('phase.mvp-mode spaced alias resolves ROADMAP mode without mutating files', () => {
const projectDir = createProject();
try {
fs.writeFileSync(
path.join(projectDir, '.planning', 'ROADMAP.md'),
[
'# Roadmap',
'',
'## v1.0.0',
'',
'### Phase 1: User Auth',
'**Goal:** Users can sign in.',
'**Mode:** mvp',
'',
].join('\n'),
);
const beforeFiles = snapshotProjectState(projectDir);
const result = runGsdTools(['phase', 'mvp-mode', '1'], projectDir);
assert.equal(result.status, 0, result.stderr);
const output = JSON.parse(result.stdout);
assert.equal(output.active, true);
assert.equal(output.source, 'roadmap');
assert.equal(output.roadmap_mode, 'mvp');
assert.equal(output.config_mvp_mode, false);
assert.equal(output.cli_flag_present, false);
assert.deepEqual(snapshotProjectState(projectDir), beforeFiles);
} finally {
fs.rmSync(projectDir, { recursive: true, force: true });
}
});
test('phase.mvp-mode ROADMAP lookup stops before custom-id next phase', () => {
const projectDir = createProject();
try {
fs.writeFileSync(
path.join(projectDir, '.planning', 'ROADMAP.md'),
[
'# Roadmap',
'',
'## v1.0.0',
'',
'### Phase 1: Numeric Phase',
'**Goal:** Users can sign in.',
'',
'### Phase custom-alpha: Custom Phase',
'**Goal:** Custom work.',
'**Mode:** mvp',
'',
].join('\n'),
);
const beforeFiles = snapshotProjectState(projectDir);
const result = runGsdTools(['phase', 'mvp-mode', '1'], projectDir);
assert.equal(result.status, 0, result.stderr);
const output = JSON.parse(result.stdout);
assert.equal(output.active, false);
assert.equal(output.source, 'none');
assert.equal(output.roadmap_mode, null);
assert.deepEqual(snapshotProjectState(projectDir), beforeFiles);
} finally {
fs.rmSync(projectDir, { recursive: true, force: true });
}
});
test('phase.mvp-mode JSON error is typed and leaves project files untouched', () => {
const projectDir = createProject();
try {
const beforeFiles = snapshotProjectState(projectDir);
const result = runGsdTools(['--json-errors', 'phase', 'mvp-mode'], projectDir);
assert.notEqual(result.status, 0);
assert.equal(result.stdout, '');
const error = JSON.parse(result.stderr);
assert.deepEqual(Object.keys(error).sort(), ['message', 'ok', 'reason']);
assert.equal(error.ok, false);
assert.equal(error.reason, 'usage');
assert.equal(typeof error.message, 'string');
assert.equal(/\n\s*at\s/.test(result.stderr), false, 'non-debug failure must not print a stack trace');
assert.deepEqual(snapshotProjectState(projectDir), beforeFiles);
} finally {
fs.rmSync(projectDir, { recursive: true, force: true });
}
});
});

View File

@@ -0,0 +1,121 @@
'use strict';
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
const ROOT = path.join(__dirname, '..');
const LINT_SCRIPT = path.join(ROOT, 'scripts', 'lint-pr-check-project-dir.cjs');
const {
checkFiles,
defaultFiles,
findForbiddenCwd,
formatFindings,
} = require(LINT_SCRIPT);
function createFixtureDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-pr-check-lint-'));
}
function runLint(args = []) {
return spawnSync(process.execPath, [LINT_SCRIPT, ...args], { encoding: 'utf8' });
}
describe('lint-pr-check-project-dir', () => {
test('flags cwd parameters and shorthand properties', () => {
const findings = findForbiddenCwd(
[
'function runCheck(args, cwd) {',
' return spawnSync(process.execPath, args, { cwd });',
'}',
].join('\n'),
'fixture.cjs',
);
assert.deepEqual(
findings.map((finding) => [finding.line, finding.column]),
[
[1, 25],
[2, 46],
],
);
});
test('flags cwd option keys even when the value is projectDir', () => {
const findings = findForbiddenCwd(
[
'function runCheck(args, projectDir) {',
' return spawnSync(process.execPath, args, { cwd: projectDir });',
'}',
].join('\n'),
'fixture.cjs',
);
assert.deepEqual(findings, [
{
file: 'fixture.cjs',
line: 2,
column: 46,
source: 'return spawnSync(process.execPath, args, { cwd: projectDir });',
},
]);
});
test('allows projectDir project-root naming without cwd references', () => {
const findings = findForbiddenCwd(
[
'function checkProject(args, projectDir) {',
' return runProjectCheck(args, projectDir);',
'}',
].join('\n'),
'fixture.cjs',
);
assert.deepEqual(findings, []);
});
test('formats diagnostics with file, line, and source', () => {
const output = formatFindings([
{
file: 'scripts/example.cjs',
line: 12,
column: 7,
source: 'const cwd = projectDir;',
},
]);
assert.match(output, /ERROR lint-pr-check-project-dir: 1 forbidden cwd reference/);
assert.match(output, /scripts\/example\.cjs:12:7/);
assert.match(output, /const cwd = projectDir;/);
assert.match(output, /projectDir/);
});
test('checks the real PR-check files without violations', () => {
const files = defaultFiles(ROOT);
assert.ok(files.length > 0, 'expected default PR-check files');
assert.deepEqual(checkFiles(files, { rootDir: ROOT }), []);
});
test('CLI exits non-zero when a passed file contains cwd', () => {
const dir = createFixtureDir();
try {
const file = path.join(dir, 'bad-check.cjs');
fs.writeFileSync(file, 'const cwd = process.env.PROJECT_DIR;\n');
const result = runLint([file]);
assert.notStrictEqual(result.status, 0);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
test('script parses without syntax errors', () => {
const result = spawnSync(process.execPath, ['--check', LINT_SCRIPT], { encoding: 'utf8' });
assert.strictEqual(result.status, 0, result.stderr);
});
});