refactor(#1191): inject clock/reset testability seams + handle valid-null settings (#1233)

* refactor(#1191): inject clock/reset testability seams + handle valid-null settings

- worktree-safety reapOrphanWorktrees: injectable deps.nowMs clock for deterministic stale-lock boundary tests (mirrors snapshotWorktreeInventory's options.nowMs).

- active-workstream-store: _resetControllingTtyCacheForTests() seam clears the memoized controlling-TTY probe cache; test replaces require.cache busting.

- gen-capability-registry: export stripGeneratedComment (additive); test imports the real helper + equivalence assertion, keeping the deliberate drift oracle.

- install.js readSettings: a successfully-parsed JSON null is treated as empty settings ({}) instead of being mis-reported as malformed; genuine parse failures still warn. readSettings/stripJsonComments exported (GSD_TEST_MODE-guarded require) for real behavioral tests.

Closes #1191

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* chore(#1191): add changeset for valid-null settings fix (#1233)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#1191): replace Stryker-incompatible structural reset test with behavioral isTTY-spy

The seam-2 reset test read the BUILT active-workstream-store.cjs and grepped for 'didProbeControllingTtyToken = false' — Stryker instruments that file so the literal is absent, failing the mutation DRY RUN. Replaced with a behavioral test that spies on process.stdin.isTTY access count to prove a post-reset probe re-runs (kills the didProbe-reset mutant) without reading source text. Local stryker: dry run passes, score 85.21% >= 80.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-06-14 20:52:56 -04:00
committed by GitHub
parent 55eb1fd72e
commit b783410815
9 changed files with 453 additions and 68 deletions

View File

@@ -448,6 +448,101 @@ describe('bug-3707: startup orphan sweep is wired into workflow entry points', (
});
});
// ─── Suite 3b: nowMs clock-injection BOUNDARY tests (#1191) ──────────────────
//
// These tests inject both `nowMs` and `mtimeSafe` so no real clock is read.
// The staleness guard is: nowMs - lockMtime.getTime() < reapMtimeGuardMs.
//
// REAP_MTIME_GUARD_MS = 5 * 60 * 1000 = 300000 ms.
//
// We use a fixed lockMtime of 1000 ms (epoch+1s) and compute nowMs values that
// are exactly 1 ms inside (age = 299999 ms < 300000) vs exactly 1 ms outside
// (age = 300000 ms, NOT < 300000) the guard boundary.
const KNOWN_REAP_MTIME_GUARD_MS = 5 * 60 * 1000; // 300000 ms — mirrors SUT constant
const FIXED_LOCK_MTIME_MS = 1000; // 1970-01-01T00:00:01.000Z
const FIXED_LOCK_DATE = new Date(FIXED_LOCK_MTIME_MS);
describe('bug-3707: reapOrphanWorktrees — nowMs clock-injection BOUNDARY tests (#1191)', () => {
let tmpBase;
beforeEach(() => {
tmpBase = fs.mkdtempSync(path.join(resolvedTmpDir(), 'gsd-3707-nowms-'));
});
afterEach(() => {
cleanup(tmpBase);
});
// ── Just-inside boundary: age = guard - 1 → skip (lock_too_fresh) ───────────
test('skips when injected nowMs places lock age just inside guard (age < guard)', () => {
// age = nowMs - FIXED_LOCK_MTIME_MS = (FIXED_LOCK_MTIME_MS + KNOWN_REAP_MTIME_GUARD_MS - 1) - FIXED_LOCK_MTIME_MS
// = KNOWN_REAP_MTIME_GUARD_MS - 1 = 299999 ms → 299999 < 300000 → SKIP
const nowMs = FIXED_LOCK_MTIME_MS + KNOWN_REAP_MTIME_GUARD_MS - 1;
const repoDir = path.join(tmpBase, 'repo-inside');
const wtDir = path.join(tmpBase, 'wt-inside-guard');
const branchName = 'worktree-boundary-inside';
initRepo(repoDir);
addWorktree(repoDir, wtDir, branchName);
commitInWorktree(wtDir, 'inside.txt');
mergeIntoMain(repoDir, branchName);
const metaDir = worktreeMeta(repoDir, wtDir);
const lockedFile = path.join(metaDir, 'locked');
fs.writeFileSync(lockedFile, String(deadPid()));
// Inject both nowMs and mtimeSafe — no real clock is read
const result = reapOrphanWorktrees(repoDir, {
nowMs,
mtimeSafe: () => FIXED_LOCK_DATE,
});
assert.ok(Array.isArray(result), 'must return an array');
const entry = result.find((r) => canonicalPath(r.path) === canonicalPath(wtDir));
assert.ok(entry, 'worktree must appear in results');
assert.equal(entry.status, 'skipped', `status must be skipped when age=${nowMs - FIXED_LOCK_MTIME_MS}ms < guard=${KNOWN_REAP_MTIME_GUARD_MS}ms`);
assert.equal(entry.reason, 'lock_too_fresh', 'reason must be lock_too_fresh');
assert.ok(fs.existsSync(wtDir), 'worktree directory must still exist (not reaped)');
});
// ── Just-outside boundary: age = guard → reap (age NOT < guard) ─────────────
test('reaps when injected nowMs places lock age exactly at guard boundary (age === guard)', () => {
// age = nowMs - FIXED_LOCK_MTIME_MS = (FIXED_LOCK_MTIME_MS + KNOWN_REAP_MTIME_GUARD_MS) - FIXED_LOCK_MTIME_MS
// = KNOWN_REAP_MTIME_GUARD_MS = 300000 ms → 300000 NOT < 300000 → PROCEED TO REAP
const nowMs = FIXED_LOCK_MTIME_MS + KNOWN_REAP_MTIME_GUARD_MS;
const repoDir = path.join(tmpBase, 'repo-outside');
const wtDir = path.join(tmpBase, 'wt-outside-guard');
const branchName = 'worktree-boundary-outside';
initRepo(repoDir);
addWorktree(repoDir, wtDir, branchName);
commitInWorktree(wtDir, 'outside.txt');
mergeIntoMain(repoDir, branchName);
const metaDir = worktreeMeta(repoDir, wtDir);
const lockedFile = path.join(metaDir, 'locked');
// Use deadPid() — a truly dead process — so PID check passes and reap proceeds
fs.writeFileSync(lockedFile, String(deadPid()));
const wtDirCanonical = canonicalPath(wtDir);
// Inject both nowMs and mtimeSafe — no real clock is read
const result = reapOrphanWorktrees(repoDir, {
nowMs,
mtimeSafe: () => FIXED_LOCK_DATE,
});
assert.ok(Array.isArray(result), 'must return an array');
const entry = result.find((r) => canonicalPath(r.path) === wtDirCanonical);
assert.ok(entry, 'worktree must appear in results');
assert.equal(entry.status, 'reaped', `status must be reaped when age=${nowMs - FIXED_LOCK_MTIME_MS}ms >= guard=${KNOWN_REAP_MTIME_GUARD_MS}ms`);
assert.ok(!fs.existsSync(wtDir), 'worktree directory must be removed after reaping');
});
});
// ─── Suite 4: Adversarial gap tests ──────────────────────────────────────────
describe('bug-3707: reapOrphanWorktrees — adversarial edge cases', () => {