174 lines
7.9 KiB
JavaScript
174 lines
7.9 KiB
JavaScript
/** debug.test.mjs — self-tests for scripts/debug/ using built-in node:test (zero external deps). */
|
|
import assert from 'node:assert/strict'
|
|
import { execFileSync, spawnSync } from 'node:child_process'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import test from 'node:test'
|
|
import { checkDebugHygiene } from './check.mjs'
|
|
import { DEBUG_HOMES, REPO_ROOT, resolveStorageRoot } from './paths.mjs'
|
|
import { TEMPORARY_RETENTION_MS, createTemporaryFiles } from './temporary-files.mjs'
|
|
import { createWorkStore } from './work-store.mjs'
|
|
import { createWorkCli } from './work.mjs'
|
|
|
|
const STORAGE = resolveStorageRoot()
|
|
|
|
function withSandbox(fn) {
|
|
const hostTmp = createTemporaryFiles(STORAGE)
|
|
const sandbox = hostTmp.create('debug-selftest', process.pid)
|
|
try {
|
|
fs.writeFileSync(path.join(sandbox, 'README.md'), '# Sandbox\n')
|
|
fs.mkdirSync(path.join(sandbox, 'internal', 'app'), { recursive: true })
|
|
fs.writeFileSync(path.join(sandbox, 'internal', 'app', 'sample.go'), 'package app\n')
|
|
fs.writeFileSync(path.join(sandbox, 'internal', 'app', 'sample_test.go'), 'package app\n')
|
|
return fn(sandbox)
|
|
} finally {
|
|
hostTmp.close(sandbox)
|
|
}
|
|
}
|
|
|
|
test('temporary-files: create, refresh, gc expired inactive, protect active and symlinks', () => {
|
|
withSandbox((sandbox) => {
|
|
const files = createTemporaryFiles(sandbox)
|
|
const dir = files.create('probe-run', process.pid)
|
|
assert.equal(fs.existsSync(dir), true)
|
|
assert.equal(fs.existsSync(path.join(dir, '.session.json')), true)
|
|
|
|
// Active process is not collected even if mtime is backdated.
|
|
const oldTime = new Date(Date.now() - TEMPORARY_RETENTION_MS - 60_000)
|
|
const recordPath = path.join(dir, '.session.json')
|
|
const rec = JSON.parse(fs.readFileSync(recordPath, 'utf8'))
|
|
rec.createdAt = oldTime.toISOString()
|
|
rec.updatedAt = oldTime.toISOString()
|
|
fs.writeFileSync(recordPath, `${JSON.stringify(rec, null, 2)}\n`)
|
|
fs.utimesSync(recordPath, oldTime, oldTime)
|
|
fs.utimesSync(dir, oldTime, oldTime)
|
|
assert.deepEqual(files.collect(false), [])
|
|
|
|
// Create an inactive expired session using a guaranteed dead pid.
|
|
const staleDir = files.create('stale-run', 99999999)
|
|
const staleRecPath = path.join(staleDir, '.session.json')
|
|
const staleRec = JSON.parse(fs.readFileSync(staleRecPath, 'utf8'))
|
|
staleRec.createdAt = oldTime.toISOString()
|
|
staleRec.updatedAt = oldTime.toISOString()
|
|
fs.writeFileSync(staleRecPath, `${JSON.stringify(staleRec, null, 2)}\n`)
|
|
fs.utimesSync(staleRecPath, oldTime, oldTime)
|
|
fs.utimesSync(staleDir, oldTime, oldTime)
|
|
|
|
assert.equal(files.collect(false).length, 1)
|
|
assert.equal(files.collect(true).length, 1)
|
|
assert.equal(fs.existsSync(staleDir), false)
|
|
|
|
// Link inside a session blocks deletion.
|
|
const targetOutside = path.join(sandbox, 'internal')
|
|
const linkPath = path.join(dir, 'linked-dir')
|
|
fs.symlinkSync(targetOutside, linkPath, process.platform === 'win32' ? 'junction' : 'dir')
|
|
assert.throws(() => files.close(dir), /filesystem link is protected/)
|
|
fs.unlinkSync(linkPath)
|
|
|
|
files.close(dir)
|
|
assert.equal(fs.existsSync(dir), false)
|
|
})
|
|
})
|
|
|
|
test('work order lifecycle: draft -> active -> verify -> review -> close and overlap guard', () => {
|
|
withSandbox((sandbox) => {
|
|
const store = createWorkStore(sandbox)
|
|
const cli = createWorkCli(store)
|
|
|
|
const id = cli.run(['new', 'whpx-probe', '--title', 'WHPX probe', '--paths', 'internal/app/sample.go', '--gate', 'go'])
|
|
assert.match(id, /^\d{8}-whpx-probe$/)
|
|
const orderDir = path.join(sandbox, '.debug', 'work', id)
|
|
assert.equal(fs.existsSync(path.join(orderDir, 'work.md')), true)
|
|
assert.equal(fs.existsSync(path.join(orderDir, 'refs')), true)
|
|
assert.equal(fs.existsSync(path.join(orderDir, 'shots')), true)
|
|
assert.equal(fs.existsSync(path.join(orderDir, 'tmp')), true)
|
|
|
|
cli.run(['start', id, '--owner', 'agent-a'])
|
|
cli.run(['verify', id, '--command', 'go test ./internal/app', '--result', 'pass'])
|
|
|
|
// Second active order owning an overlapping path without depends fails check.
|
|
const id2 = cli.run(['new', 'whpx-other', '--title', 'Overlap check', '--paths', 'internal/app', '--gate', 'go'])
|
|
cli.run(['start', id2, '--owner', 'agent-b'])
|
|
const overlapCheck = store.checkWorkOrders()
|
|
assert.ok(overlapCheck.problems.some((p) => p.includes('overlapping paths')))
|
|
|
|
// Clean up second order manually via review -> close after adding acceptance & verification.
|
|
const order2 = store.readOrder(path.join(sandbox, '.debug', 'work', id2))
|
|
store.writeOrder(order2, {
|
|
...order2.meta,
|
|
depends: [id],
|
|
acceptance: [{ id: 'A1', text: 'unit test passes', check: 'test:internal/app/sample_test.go' }],
|
|
})
|
|
store.syncIndex()
|
|
cli.run(['verify', id2, '--command', 'go test ./internal/app', '--result', 'pass'])
|
|
cli.run(['review', id2, '--reviewer', 'reviewer'])
|
|
cli.run(['close', id2])
|
|
assert.equal(fs.existsSync(path.join(sandbox, '.debug', 'work', id2)), false)
|
|
|
|
// Add acceptance criterion to first order and close cleanly.
|
|
const order1 = store.readOrder(orderDir)
|
|
store.writeOrder(order1, {
|
|
...order1.meta,
|
|
acceptance: [{ id: 'A1', text: 'unit test passes', check: 'test:internal/app/sample_test.go' }],
|
|
})
|
|
store.syncIndex()
|
|
assert.deepEqual(store.checkWorkOrders().problems, [])
|
|
|
|
cli.run(['review', id, '--reviewer', 'reviewer'])
|
|
cli.run(['close', id])
|
|
assert.equal(fs.existsSync(orderDir), false)
|
|
assert.deepEqual(store.checkWorkOrders().problems, [])
|
|
})
|
|
})
|
|
|
|
test('checkDebugHygiene: rejects stray .debug entries and concrete debug citations in durable docs', () => {
|
|
withSandbox((sandbox) => {
|
|
for (const home of DEBUG_HOMES) {
|
|
fs.mkdirSync(path.join(sandbox, '.debug', home), { recursive: true })
|
|
}
|
|
assert.deepEqual(checkDebugHygiene(sandbox).problems, [])
|
|
|
|
// Stray top-level file under .debug/ must fail.
|
|
const stray = path.join(sandbox, '.debug', 'stray.log')
|
|
fs.writeFileSync(stray, 'oops\n')
|
|
const strayRes = checkDebugHygiene(sandbox)
|
|
assert.ok(strayRes.problems.some((p) => p.includes('outside declared homes')))
|
|
fs.rmSync(stray)
|
|
|
|
// Concrete work order citation in README.md must fail.
|
|
fs.writeFileSync(path.join(sandbox, 'README.md'), 'See .debug/work/20261129-sample-task for details.\n')
|
|
const docRes = checkDebugHygiene(sandbox)
|
|
assert.ok(docRes.problems.some((p) => p.includes('durable documents never reference concrete .debug/ instances')))
|
|
|
|
// Broken local relative link and "- **Title**: description" slogan bullet must fail.
|
|
fs.writeFileSync(path.join(sandbox, 'README.md'), '# Title\n\n- **Slogan**: prose after colon.\n\nSee [missing](./docs/nope.md) and [bad anchor](#missing).\n')
|
|
const rotRes = checkDebugHygiene(sandbox)
|
|
assert.ok(rotRes.problems.some((p) => p.includes('plain declarative sentence')))
|
|
assert.ok(rotRes.problems.some((p) => p.includes('broken relative link "./docs/nope.md"')))
|
|
assert.ok(rotRes.problems.some((p) => p.includes('broken heading anchor "#missing"')))
|
|
})
|
|
})
|
|
|
|
test('tmp.mjs run CLI: cleans up on success and preserves evidence on failure', () => {
|
|
const tmpCli = path.join(REPO_ROOT, 'scripts', 'debug', 'tmp.mjs')
|
|
|
|
const ok = spawnSync(process.execPath, [tmpCli, 'run', 'cli-ok', '--', process.execPath, '-e', 'process.exit(0)'], {
|
|
encoding: 'utf8',
|
|
})
|
|
assert.equal(ok.status, 0)
|
|
const okMatch = ok.stderr.match(/temporary: (.+)/)
|
|
assert.ok(okMatch)
|
|
assert.equal(fs.existsSync(okMatch[1].trim()), false)
|
|
|
|
const fail = spawnSync(process.execPath, [tmpCli, 'run', 'cli-fail', '--', process.execPath, '-e', 'process.exit(2)'], {
|
|
encoding: 'utf8',
|
|
})
|
|
assert.equal(fail.status, 2)
|
|
const failMatch = fail.stderr.match(/temporary: (.+)/)
|
|
assert.ok(failMatch)
|
|
const keptDir = failMatch[1].trim()
|
|
assert.equal(fs.existsSync(keptDir), true)
|
|
execFileSync(process.execPath, [tmpCli, 'close', keptDir])
|
|
assert.equal(fs.existsSync(keptDir), false)
|
|
})
|