Files
msd-core/tests/temp-subdir.test.cjs
Tom Boucher a28dcec981 chore(#597): replace count-based ratchet guards with AST lint + named-set allowlists (#603)
The windows-test-parity ratchet greps test source for fs.rmSync-without-
maxRetries (and six other Windows-portability anti-patterns), failing when an
integer offender COUNT exceeds a frozen baseline (rmSync: 95). A count ratchet
is a Goodhart metric: fixing one offender and adding another keeps the count
constant, so a new defect slips through green. Replace it — and every other
count ratchet in the repo — with a layered, masking-proof design.

Behavioral seam test
- tests/helpers-cleanup.test.cjs proves helpers.cleanup() carries the Windows
  EBUSY retry budget. cleanup() delegates retries to Node's fs.rmSync via
  maxRetries (it owns no loop), so the test asserts the option contract
  (recursive/force/maxRetries>0/retryDelay>0) + real-FS removal + the cwd-guard,
  rather than a loop that does not exist. The EBUSY risk is now tested ONCE at
  the helper, not approximated textually at every call site.

Write-time ESLint rule (AST-accurate, replaces the grep)
- eslint-rules/no-raw-rmsync-in-tests.cjs (error in tests/**/*.test.cjs) bans
  raw fs.rmSync, steering to cleanup(). Catches member, computed (fs['rmSync']),
  destructured and aliased forms; escape hatch is inline
  `// eslint-disable-next-line local/no-raw-rmsync-in-tests -- <reason>` only.
- Migrated 336 raw fs.rmSync teardown calls across ~116 test files to cleanup().
  ~18 genuinely load-bearing sites (mid-test SUT/fault-injection removals,
  error-swallowing or name-colliding local teardown helpers) keep the raw call
  with an inline eslint-disable + reason.

Shared anti-ratchet primitive
- scripts/lib/allowlist-ratchet.cjs:
  - assertWithinAllowlist: fails on NOVEL ids (new offender introduced) AND on
    STALE ids (a known offender was fixed but not pruned) — identity, not count,
    and a ratchet DOWN toward zero.
  - assertTightCeiling: a size/length budget whose ceiling must stay within a
    grace band of the high-water mark, so budgets may only tighten, never creep.

Ratchets converted onto the primitive
- windows-test-parity-guard.test.cjs: rmSync rule deleted (now ESLint-enforced);
  the remaining six patterns moved from integer baselines to named-set
  allowlists with ratchet-down.
- scripts/lint-test-file-count.{cjs,allowlist.json}: per-module integer counts →
  named filename sets (closes the swap-a-file-keep-the-count blind spot); a
  module dropping under cap now FAILS to force pruning its allowlist entry.
- enh-2790 skill-count `<= 63` → named skill allowlist (ratchets toward ~58).

Size budgets hardened (tighten-only)
- agent-size / workflow-size / feat-3039 help-tiered: ceilings lowered to the
  current high-water mark and an assertTightCeiling anti-creep check added per
  tier. Fixed external-contract limits (description ≤100 chars, agent ≤100 KB)
  are intentionally left as-is — they are not grandfathered creeping budgets.

No user-facing behavior change (tests + tooling only); no USER_FACING_PREFIXES
touched, so no changeset fragment is required.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-01 22:43:49 -04:00

138 lines
5.6 KiB
JavaScript

/**
* GSD Tools Tests - dedicated temp subdirectory
*
* Tests for issue #1975: GSD temp files should use a dedicated
* subdirectory (path.join(os.tmpdir(), 'gsd')) instead of writing
* directly to os.tmpdir().
*/
const { test, describe, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const os = require('os');
const {
reapStaleTempFiles,
} = require('../get-shit-done/bin/lib/core.cjs');
const GSD_TEMP_DIR = path.join(os.tmpdir(), 'gsd');
// ─── Dedicated temp subdirectory ────────────────────────────────────────────
describe('dedicated gsd temp subdirectory', () => {
describe('output() temp file placement', () => {
// output() writes to tmpfile when JSON > 50KB. We test indirectly by
// checking that reapStaleTempFiles scans the subdirectory.
test('gsd temp subdirectory path is os.tmpdir()/gsd', () => {
// The GSD_TEMP_DIR constant should resolve to <tmpdir>/gsd
assert.strictEqual(GSD_TEMP_DIR, path.join(os.tmpdir(), 'gsd'));
});
});
describe('reapStaleTempFiles with subdirectory', () => {
let testPrefix;
beforeEach(() => {
testPrefix = `gsd-tempsub-test-${Date.now()}-`;
// Ensure the gsd subdirectory exists for test setup
fs.mkdirSync(GSD_TEMP_DIR, { recursive: true });
});
test('removes stale files from gsd subdirectory', () => {
const stalePath = path.join(GSD_TEMP_DIR, `${testPrefix}stale.json`);
fs.writeFileSync(stalePath, '{}');
const oldTime = new Date(Date.now() - 10 * 60 * 1000);
fs.utimesSync(stalePath, oldTime, oldTime);
reapStaleTempFiles(testPrefix, { maxAgeMs: 5 * 60 * 1000 });
assert.ok(!fs.existsSync(stalePath), 'stale file in gsd subdir should be removed');
});
test('preserves fresh files in gsd subdirectory', () => {
const freshPath = path.join(GSD_TEMP_DIR, `${testPrefix}fresh.json`);
fs.writeFileSync(freshPath, '{}');
reapStaleTempFiles(testPrefix, { maxAgeMs: 5 * 60 * 1000 });
assert.ok(fs.existsSync(freshPath), 'fresh file in gsd subdir should be preserved');
// Clean up
fs.unlinkSync(freshPath);
});
test('removes stale directories from gsd subdirectory', () => {
const staleDir = path.join(GSD_TEMP_DIR, `${testPrefix}dir`);
fs.mkdirSync(staleDir, { recursive: true });
const oldTime = new Date(Date.now() - 10 * 60 * 1000);
fs.utimesSync(staleDir, oldTime, oldTime);
reapStaleTempFiles(testPrefix, { maxAgeMs: 5 * 60 * 1000 });
assert.ok(!fs.existsSync(staleDir), 'stale directory in gsd subdir should be removed');
});
test('creates gsd subdirectory if it does not exist', () => {
// Use a unique nested path to avoid interfering with other tests
const uniqueSubdir = path.join(os.tmpdir(), `gsd-creation-test-${Date.now()}`);
// Verify it does not exist
if (fs.existsSync(uniqueSubdir)) {
// eslint-disable-next-line local/no-raw-rmsync-in-tests -- mid-test pre-condition reset: ensures uniqueSubdir is absent before testing SUT creation behavior
fs.rmSync(uniqueSubdir, { recursive: true, force: true });
}
assert.ok(!fs.existsSync(uniqueSubdir), 'test subdir should not exist before test');
// reapStaleTempFiles should not throw even if subdir does not exist
// (it gets created or handled gracefully)
assert.doesNotThrow(() => {
reapStaleTempFiles(`gsd-creation-test-${Date.now()}-`, { maxAgeMs: 0 });
});
});
test('does not scan system tmpdir root for gsd- files', () => {
// Place a stale file in the OLD location (system tmpdir root)
const oldLocationPath = path.join(os.tmpdir(), `${testPrefix}old-location.json`);
fs.writeFileSync(oldLocationPath, '{}');
const oldTime = new Date(Date.now() - 10 * 60 * 1000);
fs.utimesSync(oldLocationPath, oldTime, oldTime);
// reapStaleTempFiles should NOT remove files from the old location
// because it now only scans the gsd subdirectory
reapStaleTempFiles(testPrefix, { maxAgeMs: 5 * 60 * 1000 });
// The file in the old location should still exist (not scanned)
assert.ok(
fs.existsSync(oldLocationPath),
'files in system tmpdir root should NOT be scanned by reapStaleTempFiles'
);
// Clean up manually
fs.unlinkSync(oldLocationPath);
});
test('backward compat: reapStaleTempFilesLegacy cleans old location', () => {
// Place a stale file in the old location (system tmpdir root)
const oldLocationPath = path.join(os.tmpdir(), `${testPrefix}legacy.json`);
fs.writeFileSync(oldLocationPath, '{}');
const oldTime = new Date(Date.now() - 10 * 60 * 1000);
fs.utimesSync(oldLocationPath, oldTime, oldTime);
// The legacy reap function should still clean old-location files
// We import it if exported, or verify the main reap handles both
const core = require('../get-shit-done/bin/lib/core.cjs');
if (typeof core.reapStaleTempFilesLegacy === 'function') {
core.reapStaleTempFilesLegacy(testPrefix, { maxAgeMs: 5 * 60 * 1000 });
assert.ok(!fs.existsSync(oldLocationPath), 'legacy reap should clean old location');
} else {
// If no separate legacy function, the main output() should do a one-time
// migration sweep. We just verify the export shape is correct.
assert.ok(typeof core.reapStaleTempFiles === 'function');
// Clean up manually since we're not testing migration here
fs.unlinkSync(oldLocationPath);
}
});
});
});