ktg-plugin-marketplace/plugins/ultraplan-local/tests/lib/cleanup.test.mjs
Kjell Tore Guttormsen 5f74a670ab feat(voyage)!: rename produced_by field values + validator comments [skip-docs]
- commands/trekexecute.md: produced_by literals -> trekexecute (4 occurrences)
- commands/trekendsession.md: produced_by literals -> trekendsession (2 occurrences)
- tests/validators/next-session-prompt-validator.test.mjs: 11 'ultraexecute-local' refs -> 'trekexecute'
- tests/commands/trekcontinue.test.mjs: 3 fixture strings updated
- tests/lib/cleanup.test.mjs: 1 fixture string updated
- lib/validators/next-session-prompt-validator.mjs: producer-list comment
- docs/HANDOVER-CONTRACTS.md line 432: example producer names updated

Part of voyage-rebrand session 2 (W3.4 / Step 6).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-05 14:42:21 +02:00

134 lines
5.7 KiB
JavaScript

// tests/lib/cleanup.test.mjs
// Unit tests for lib/util/cleanup.mjs (Bug 4).
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, existsSync, unlinkSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { cleanupProject } from '../../lib/util/cleanup.mjs';
function buildProject(dir, status) {
mkdirSync(dir, { recursive: true });
const stateObj = {
schema_version: 1,
project: dir,
next_session_brief_path: join(dir, 'brief.md'),
next_session_label: 'Cleanup test fixture',
status,
updated_at: '2026-05-04T16:00:00.000Z',
};
writeFileSync(join(dir, '.session-state.local.json'), JSON.stringify(stateObj, null, 2));
writeFileSync(join(dir, 'NEXT-SESSION-PROMPT.local.md'),
`---\nproduced_by: trekexecute\nproduced_at: 2026-05-04T16:00:00.000Z\n---\n\n# Done\n`);
return dir;
}
test('cleanupProject — dry-run on completed project lists candidates without deleting', () => {
const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
try {
const dir = buildProject(join(root, 'project-a'), 'completed');
const r = cleanupProject(dir, { dryRun: true });
assert.equal(r.valid, true, JSON.stringify(r.errors));
assert.equal(r.parsed.wouldDelete.length, 2);
assert.deepEqual(r.parsed.deleted, []);
// Files MUST still exist.
assert.equal(existsSync(join(dir, '.session-state.local.json')), true);
assert.equal(existsSync(join(dir, 'NEXT-SESSION-PROMPT.local.md')), true);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('cleanupProject — confirm-mode deletes both candidate files', () => {
const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
try {
const dir = buildProject(join(root, 'project-b'), 'completed');
const r = cleanupProject(dir, { dryRun: false, confirm: true });
assert.equal(r.valid, true, JSON.stringify(r.errors));
assert.equal(r.parsed.deleted.length, 2);
assert.equal(existsSync(join(dir, '.session-state.local.json')), false);
assert.equal(existsSync(join(dir, 'NEXT-SESSION-PROMPT.local.md')), false);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('cleanupProject — idempotent re-run after partial cleanup succeeds with deleted: []', () => {
const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
try {
const dir = buildProject(join(root, 'project-c'), 'completed');
// First confirm-mode deletes the prompt file BUT we still have the state file.
// Manually remove the prompt file FIRST so the state file (still completed) is
// the only candidate left after first cleanup.
unlinkSync(join(dir, 'NEXT-SESSION-PROMPT.local.md'));
const first = cleanupProject(dir, { dryRun: false, confirm: true });
assert.equal(first.valid, true);
assert.equal(first.parsed.deleted.length, 1, 'first cleanup deletes only the state file (prompt was pre-removed)');
// Second invocation must fail — no state file → CLEANUP_NO_STATE_FILE.
// This is the documented "fully cleaned" terminal state and is NOT an error
// for the operator (they can ignore CLEANUP_NO_STATE_FILE), but the function
// signals it deterministically.
const second = cleanupProject(dir, { dryRun: false, confirm: true });
assert.equal(second.valid, false);
assert.ok(second.errors.find(e => e.code === 'CLEANUP_NO_STATE_FILE'));
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('cleanupProject — refuses on status: in_progress (CLEANUP_NOT_COMPLETED)', () => {
const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
try {
const dir = buildProject(join(root, 'project-d'), 'in_progress');
const r = cleanupProject(dir, { dryRun: false, confirm: true });
assert.equal(r.valid, false);
assert.ok(r.errors.find(e => e.code === 'CLEANUP_NOT_COMPLETED'));
// Files MUST still exist (refusal must not partially clean).
assert.equal(existsSync(join(dir, '.session-state.local.json')), true);
assert.equal(existsSync(join(dir, 'NEXT-SESSION-PROMPT.local.md')), true);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('cleanupProject — refuses dryRun: false without confirm: true (CLEANUP_REQUIRES_CONFIRM)', () => {
const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
try {
const dir = buildProject(join(root, 'project-e'), 'completed');
const r = cleanupProject(dir, { dryRun: false }); // no confirm
assert.equal(r.valid, false);
assert.ok(r.errors.find(e => e.code === 'CLEANUP_REQUIRES_CONFIRM'));
assert.equal(existsSync(join(dir, '.session-state.local.json')), true);
assert.equal(existsSync(join(dir, 'NEXT-SESSION-PROMPT.local.md')), true);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('cleanupProject — defaults to dry-run when opts is omitted', () => {
const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
try {
const dir = buildProject(join(root, 'project-f'), 'completed');
const r = cleanupProject(dir);
assert.equal(r.valid, true);
assert.deepEqual(r.parsed.deleted, []);
assert.equal(r.parsed.wouldDelete.length, 2);
assert.equal(existsSync(join(dir, '.session-state.local.json')), true);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test('cleanupProject — missing state file returns CLEANUP_NO_STATE_FILE', () => {
const root = mkdtempSync(join(tmpdir(), 'cleanup-'));
try {
const dir = join(root, 'project-empty');
mkdirSync(dir, { recursive: true });
const r = cleanupProject(dir);
assert.equal(r.valid, false);
assert.ok(r.errors.find(e => e.code === 'CLEANUP_NO_STATE_FILE'));
} finally {
rmSync(root, { recursive: true, force: true });
}
});