import { afterEach, describe, expect, it } from 'vitest'; import { existsSync, mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { tmpdir } from 'node:os'; import { cleanupOwnedRunDir, createOwnedRunDir } from './e2e-run-dir.mjs'; describe('E2E owned run directory cleanup', () => { const fixtureParents: string[] = []; const fixtureParent = (prefix: string) => { const directory = mkdtempSync(join(tmpdir(), prefix)); fixtureParents.push(directory); return directory; }; afterEach(() => { for (const directory of fixtureParents.splice(0)) { // Test-only cleanup: every path was minted and retained by fixtureParent. rmSync(directory, { recursive: true, force: true }); expect(existsSync(directory)).toBe(false); } }); it('deletes only an owned directory and is idempotent', () => { const root = fixtureParent('e2e-cache-'); const owned = createOwnedRunDir(root, 'maestro-e2e-'); expect(cleanupOwnedRunDir({ cacheRoot: root, directory: owned.directory, prefix: 'maestro-e2e-', token: owned.token })).toBe(true); expect(cleanupOwnedRunDir({ cacheRoot: root, directory: owned.directory, prefix: 'maestro-e2e-', token: owned.token })).toBe(false); }); it('rejects root, external existing directories, bad tokens, and symlink traversal', () => { const root = fixtureParent('e2e-cache-'); const external = fixtureParent('maestro-e2e-external-'); writeFileSync(join(external, 'keep'), 'keep'); const owned = createOwnedRunDir(root, 'maestro-e2e-'); expect(cleanupOwnedRunDir({ cacheRoot: root, directory: root, prefix: 'maestro-e2e-', token: owned.token })).toBe(false); expect(cleanupOwnedRunDir({ cacheRoot: root, directory: external, prefix: 'maestro-e2e-', token: owned.token })).toBe(false); expect(cleanupOwnedRunDir({ cacheRoot: root, directory: owned.directory, prefix: 'maestro-e2e-', token: 'wrong' })).toBe(false); const link = join(root, 'maestro-e2e-link'); symlinkSync(external, link, 'dir'); expect(cleanupOwnedRunDir({ cacheRoot: root, directory: link, prefix: 'maestro-e2e-', token: owned.token })).toBe(false); mkdirSync(join(owned.directory, 'still-here')); }); });