Phase 4 of the CJS↔SDK hard-seam migration (parent #3524). Eliminates the `findProjectRoot` duplication that lived at bin/lib/core.cjs:74-140 and sdk/src/query/helpers.ts:497-590, the drift carrier behind historical bugs #1362 and #2561. - sdk/src/project-root/index.ts — source of truth (120 lines, pure-with-sync-fs). Exports findProjectRoot(startDir: string) and FIND_PROJECT_ROOT_MAX_DEPTH constant. - sdk/src/project-root/index.test.ts — 13 vitest pinning fixtures covering all four heuristics, the #1362 guard, malformed config fallback, empty sub_repos, deep nesting, and depth-limit enforcement. - sdk/scripts/gen-project-root.mjs — generator. Captures function body via Function.prototype.toString() from compiled sdk/dist/. Emits CJS preamble for destructured node:fs / node:path / node:os imports. - sdk/scripts/check-project-root-fresh.mjs — freshness check. Imports the generator function directly (Phase 3's cleaner pattern). - get-shit-done/bin/lib/project-root.generated.cjs — generator- emitted CJS mirror. - tests/project-root-generator.test.cjs — 11 parity assertions comparing SDK source and generated CJS for every fixture. - sdk/src/query/helpers.ts: -127 lines. The 94-line inline findProjectRoot plus the FIND_PROJECT_ROOT_MAX_DEPTH constant (originally at line 471) replaced by a single re-export: `export { findProjectRoot } from '../project-root/index.js';` Removed unused `parse as parsePath` import. - get-shit-done/bin/lib/core.cjs: -83 lines net. The 67-line inline findProjectRoot replaced by a single `require('./project-root.generated.cjs')`. The detectSubRepos helper at lines 40-56 stays (used by loadConfig migration). - sdk/package.json: gen:project-root + check:project-root-fresh scripts. - package.json: proxy for the freshness check. - .githooks/pre-commit: drift block. - .github/workflows/test.yml: drift check step after the state-document drift step. - CONTEXT.md: Project-Root Resolution Module entry. - docs/INVENTORY.md, docs/INVENTORY-MANIFEST.json: +1 module count, +1 row. - Full suite: 9226/9226 pass (baseline 9215 + 11 new parity fixtures). - SDK vitest: 1804/1804 pass. - Reader shrink: -127 SDK + -83 CJS = 210 lines of duplication deleted across the two Readers. New shared Module is 120 lines. 1. Depth limit canonicalization. CJS findProjectRoot previously had no explicit walk-up bound (walked until dir === root or homedir). The new Module uses FIND_PROJECT_ROOT_MAX_DEPTH = 10, matching the SDK's pre-existing value. Only affects paths nested more than 10 levels deep from a .planning/ root — a pathological case in practice. None of the existing 22 CJS findProjectRoot tests covered this; the new parity test does. 2. platformReadSync → readFileSync. The old CJS findProjectRoot used the platformReadSync wrapper from shell-command-projection.cjs for reading .planning/config.json, which returns null on read failure. The Module uses raw readFileSync, which throws — caught by the surrounding try/catch that already swallowed errors. Functionally equivalent for the existing code path; no test exercises the null-return semantic. Closes #3553.
154 lines
5.9 KiB
JavaScript
154 lines
5.9 KiB
JavaScript
'use strict';
|
|
/**
|
|
* CJS parity test — project-root module
|
|
*
|
|
* For every fixture from sdk/src/project-root/index.test.ts, asserts that
|
|
* both the SDK (ESM, via dynamic import) and the generated CJS artifact
|
|
* return identical paths. This confirms that the generator correctly
|
|
* captures the function body and that all dependencies (sep, dirname,
|
|
* relative, etc.) are properly shimmed in the CJS preamble.
|
|
*/
|
|
|
|
const { describe, it, before, afterEach } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const os = require('node:os');
|
|
const path = require('node:path');
|
|
|
|
// CJS artifact — synchronous require
|
|
const { findProjectRoot: findProjectRootCjs } = require('../get-shit-done/bin/lib/project-root.generated.cjs');
|
|
|
|
// SDK ESM — loaded once before all tests via dynamic import
|
|
let findProjectRootSdk;
|
|
before(async () => {
|
|
const mod = await import('../sdk/dist/project-root/index.js');
|
|
findProjectRootSdk = mod.findProjectRoot;
|
|
});
|
|
|
|
// ── Fixture helpers ─────────────────────────────────────────────────────────
|
|
|
|
function makeTmp() {
|
|
return fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-parity-'));
|
|
}
|
|
|
|
function writeConfig(dir, content) {
|
|
fs.mkdirSync(path.join(dir, '.planning'), { recursive: true });
|
|
fs.writeFileSync(path.join(dir, '.planning', 'config.json'), JSON.stringify(content));
|
|
}
|
|
|
|
function assertParity(startDir) {
|
|
const sdkResult = findProjectRootSdk(startDir);
|
|
const cjsResult = findProjectRootCjs(startDir);
|
|
assert.strictEqual(
|
|
cjsResult,
|
|
sdkResult,
|
|
`parity failure for startDir="${startDir}": SDK="${sdkResult}" CJS="${cjsResult}"`,
|
|
);
|
|
return sdkResult;
|
|
}
|
|
|
|
// ── Tests ────────────────────────────────────────────────────────────────────
|
|
|
|
describe('project-root CJS/SDK parity', () => {
|
|
let workspace;
|
|
|
|
before(() => {
|
|
workspace = makeTmp();
|
|
});
|
|
|
|
afterEach(() => {
|
|
// Clean the workspace tree and recreate fresh for next test
|
|
try { fs.rmSync(workspace, { recursive: true, force: true }); } catch {}
|
|
workspace = makeTmp();
|
|
});
|
|
|
|
it('heuristic 0: startDir has own .planning/ — returns startDir', () => {
|
|
fs.mkdirSync(path.join(workspace, '.planning'), { recursive: true });
|
|
assertParity(workspace);
|
|
});
|
|
|
|
it('no ancestor .planning/ — returns startDir', () => {
|
|
assertParity(workspace);
|
|
});
|
|
|
|
it('heuristic 1: parent .planning/config.json lists child in sub_repos', () => {
|
|
writeConfig(workspace, { sub_repos: ['child'] });
|
|
const child = path.join(workspace, 'child');
|
|
fs.mkdirSync(path.join(child, '.git'), { recursive: true });
|
|
const result = assertParity(child);
|
|
assert.strictEqual(result, workspace);
|
|
});
|
|
|
|
it('heuristic 1 nested: deeply nested dir inside sub_repo', () => {
|
|
writeConfig(workspace, { sub_repos: ['child'] });
|
|
const nested = path.join(workspace, 'child', 'src', 'utils');
|
|
fs.mkdirSync(path.join(workspace, 'child', '.git'), { recursive: true });
|
|
fs.mkdirSync(nested, { recursive: true });
|
|
const result = assertParity(nested);
|
|
assert.strictEqual(result, workspace);
|
|
});
|
|
|
|
it('heuristic 1 nested key: planning.sub_repos config shape', () => {
|
|
writeConfig(workspace, { planning: { sub_repos: ['child'] } });
|
|
const child = path.join(workspace, 'child');
|
|
fs.mkdirSync(path.join(child, '.git'), { recursive: true });
|
|
const result = assertParity(child);
|
|
assert.strictEqual(result, workspace);
|
|
});
|
|
|
|
it('heuristic 2: multiRepo: true with .git in ancestor chain', () => {
|
|
writeConfig(workspace, { multiRepo: true });
|
|
const child = path.join(workspace, 'child');
|
|
fs.mkdirSync(path.join(child, '.git'), { recursive: true });
|
|
const result = assertParity(child);
|
|
assert.strictEqual(result, workspace);
|
|
});
|
|
|
|
it('heuristic 3: parent has .planning/ and child has .git, no config', () => {
|
|
fs.mkdirSync(path.join(workspace, '.planning'), { recursive: true });
|
|
const child = path.join(workspace, 'child');
|
|
fs.mkdirSync(path.join(child, '.git'), { recursive: true });
|
|
const result = assertParity(child);
|
|
assert.strictEqual(result, workspace);
|
|
});
|
|
|
|
it('malformed config.json falls back to heuristic 3', () => {
|
|
fs.mkdirSync(path.join(workspace, '.planning'), { recursive: true });
|
|
fs.writeFileSync(path.join(workspace, '.planning', 'config.json'), '{ not json');
|
|
const child = path.join(workspace, 'child');
|
|
fs.mkdirSync(path.join(child, '.git'), { recursive: true });
|
|
const result = assertParity(child);
|
|
assert.strictEqual(result, workspace);
|
|
});
|
|
|
|
it('empty sub_repos with no .git — returns startDir', () => {
|
|
writeConfig(workspace, { sub_repos: [] });
|
|
const child = path.join(workspace, 'child');
|
|
fs.mkdirSync(child, { recursive: true });
|
|
const result = assertParity(child);
|
|
assert.strictEqual(result, child);
|
|
});
|
|
|
|
it('#1362: child has own .planning/ — returns child not workspace', () => {
|
|
writeConfig(workspace, { sub_repos: ['child'] });
|
|
const child = path.join(workspace, 'child');
|
|
fs.mkdirSync(path.join(child, '.planning'), { recursive: true });
|
|
const result = assertParity(child);
|
|
assert.strictEqual(result, child);
|
|
});
|
|
|
|
it('depth limit: .planning/ is 12 levels up — returns startDir (depth=10 cap)', () => {
|
|
// CANONICALIZATION NOTE: SDK has FIND_PROJECT_ROOT_MAX_DEPTH=10; CJS now
|
|
// also uses 10 (was unbounded before). At 12 levels the walk stops before
|
|
// reaching workspace, so startDir is returned.
|
|
fs.mkdirSync(path.join(workspace, '.planning'), { recursive: true });
|
|
let dir = workspace;
|
|
for (let i = 1; i <= 12; i++) {
|
|
dir = path.join(dir, `l${i}`);
|
|
}
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
const result = assertParity(dir);
|
|
assert.strictEqual(result, dir);
|
|
});
|
|
});
|