|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { spawn } from "node:child_process"; |
| 3 | +import { |
| 4 | + cpSync, |
| 5 | + mkdirSync, |
| 6 | + mkdtempSync, |
| 7 | + rmSync, |
| 8 | + symlinkSync, |
| 9 | + writeFileSync, |
| 10 | +} from "node:fs"; |
| 11 | +import { createServer } from "node:http"; |
| 12 | +import type { AddressInfo } from "node:net"; |
| 13 | +import { tmpdir } from "node:os"; |
| 14 | +import { join } from "node:path"; |
| 15 | +import test from "node:test"; |
| 16 | +import { fileURLToPath } from "node:url"; |
| 17 | + |
| 18 | +// `npm run cleanup` deletes platform resources that aren't in the state |
| 19 | +// file. Resources excluded by .vapi-ignore are never written to state, so |
| 20 | +// cleanup must keep them, as push's orphan-protection does. |
| 21 | + |
| 22 | +const REPO = fileURLToPath(new URL("..", import.meta.url)); |
| 23 | +const ORG = "test-cleanup-org"; |
| 24 | +const TRACKED = "11111111-1111-4111-8111-111111111111"; |
| 25 | +const IGNORED_ASSISTANT = "22222222-2222-4222-8222-222222222222"; |
| 26 | +const IGNORED_TOOL = "33333333-3333-4333-8333-333333333333"; |
| 27 | +const ORPHAN = "44444444-4444-4444-8444-444444444444"; |
| 28 | + |
| 29 | +const PLATFORM: Record<string, unknown[]> = { |
| 30 | + "/assistant": [ |
| 31 | + { id: TRACKED, name: "Front Desk" }, |
| 32 | + { id: IGNORED_ASSISTANT, name: "Legacy Bot" }, |
| 33 | + { id: ORPHAN, name: "Old Experiment" }, |
| 34 | + ], |
| 35 | + "/tool": [ |
| 36 | + { id: IGNORED_TOOL, type: "function", function: { name: "legacy_lookup" } }, |
| 37 | + ], |
| 38 | +}; |
| 39 | + |
| 40 | +async function cleanupRun( |
| 41 | + args: string[], |
| 42 | +): Promise<{ code: number | null; output: string; deletes: string[] }> { |
| 43 | + const deletes: string[] = []; |
| 44 | + const server = createServer((req, res) => { |
| 45 | + res.setHeader("content-type", "application/json"); |
| 46 | + if (req.method === "DELETE") { |
| 47 | + deletes.push(req.url ?? ""); |
| 48 | + res.end("{}"); |
| 49 | + return; |
| 50 | + } |
| 51 | + const path = (req.url ?? "").split("?")[0]!; |
| 52 | + res.end(JSON.stringify(PLATFORM[path] ?? [])); |
| 53 | + }); |
| 54 | + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve)); |
| 55 | + const baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; |
| 56 | + const dir = mkdtempSync(join(tmpdir(), "vapi-cleanup-ignore-")); |
| 57 | + try { |
| 58 | + cpSync(join(REPO, "src"), join(dir, "src"), { recursive: true }); |
| 59 | + cpSync(join(REPO, "package.json"), join(dir, "package.json")); |
| 60 | + symlinkSync(join(REPO, "node_modules"), join(dir, "node_modules"), "dir"); |
| 61 | + writeFileSync(join(dir, `.env.${ORG}`), "VAPI_TOKEN=fake-token-not-used\n"); |
| 62 | + writeFileSync( |
| 63 | + join(dir, `.vapi-state.${ORG}.json`), |
| 64 | + JSON.stringify({ assistants: { "front-desk": { uuid: TRACKED } } }), |
| 65 | + ); |
| 66 | + mkdirSync(join(dir, "resources", ORG), { recursive: true }); |
| 67 | + writeFileSync( |
| 68 | + join(dir, "resources", ORG, ".vapi-ignore"), |
| 69 | + "# owned by another team\nassistants/legacy-*\ntools/legacy-*\n", |
| 70 | + ); |
| 71 | + const child = spawn( |
| 72 | + process.execPath, |
| 73 | + ["--import", "tsx", "src/cleanup.ts", ORG, ...args], |
| 74 | + { |
| 75 | + cwd: dir, |
| 76 | + env: { |
| 77 | + ...process.env, |
| 78 | + VAPI_BASE_URL: baseUrl, |
| 79 | + VAPI_TOKEN: "fake-token-not-used", |
| 80 | + }, |
| 81 | + }, |
| 82 | + ); |
| 83 | + let output = ""; |
| 84 | + child.stdout.on("data", (chunk) => (output += chunk)); |
| 85 | + child.stderr.on("data", (chunk) => (output += chunk)); |
| 86 | + const code = await new Promise<number | null>((resolve) => |
| 87 | + child.on("close", resolve), |
| 88 | + ); |
| 89 | + return { code, output, deletes }; |
| 90 | + } finally { |
| 91 | + rmSync(dir, { recursive: true, force: true }); |
| 92 | + await new Promise<void>((resolve) => server.close(() => resolve())); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +test( |
| 97 | + "a destructive cleanup deletes only true orphans and keeps .vapi-ignore matches", |
| 98 | + { timeout: 60_000 }, |
| 99 | + async () => { |
| 100 | + const result = await cleanupRun(["--force", "--confirm", ORG]); |
| 101 | + assert.deepEqual( |
| 102 | + { |
| 103 | + code: result.code, |
| 104 | + deletes: result.deletes, |
| 105 | + retainedAssistant: result.output.includes( |
| 106 | + "Legacy Bot retained (matched .vapi-ignore: assistants/legacy-*)", |
| 107 | + ), |
| 108 | + retainedTool: result.output.includes( |
| 109 | + "legacy_lookup retained (matched .vapi-ignore: tools/legacy-*)", |
| 110 | + ), |
| 111 | + }, |
| 112 | + { |
| 113 | + code: 0, |
| 114 | + deletes: [`/assistant/${ORPHAN}`], |
| 115 | + retainedAssistant: true, |
| 116 | + retainedTool: true, |
| 117 | + }, |
| 118 | + ); |
| 119 | + }, |
| 120 | +); |
| 121 | + |
| 122 | +test( |
| 123 | + "a dry run lists ignored resources as kept, not as orphans", |
| 124 | + { timeout: 60_000 }, |
| 125 | + async () => { |
| 126 | + const result = await cleanupRun([]); |
| 127 | + assert.deepEqual( |
| 128 | + { |
| 129 | + code: result.code, |
| 130 | + deletes: result.deletes, |
| 131 | + orphanListed: result.output.includes( |
| 132 | + `assistants: Old Experiment (${ORPHAN})`, |
| 133 | + ), |
| 134 | + ignoredListed: |
| 135 | + result.output.includes(IGNORED_ASSISTANT) && |
| 136 | + result.output.includes("🗑️ assistants: Legacy Bot"), |
| 137 | + keptSummary: result.output.includes( |
| 138 | + "2 resource(s) not in state were kept because they match .vapi-ignore", |
| 139 | + ), |
| 140 | + }, |
| 141 | + { |
| 142 | + code: 0, |
| 143 | + deletes: [], |
| 144 | + orphanListed: true, |
| 145 | + ignoredListed: false, |
| 146 | + keptSummary: true, |
| 147 | + }, |
| 148 | + ); |
| 149 | + }, |
| 150 | +); |
0 commit comments