|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { spawnSync } from "node:child_process"; |
| 4 | +import { |
| 5 | + mkdtempSync, |
| 6 | + writeFileSync, |
| 7 | + rmSync, |
| 8 | + cpSync, |
| 9 | + symlinkSync, |
| 10 | + existsSync, |
| 11 | + mkdirSync, |
| 12 | +} from "node:fs"; |
| 13 | +import { join, dirname } from "node:path"; |
| 14 | +import { tmpdir } from "node:os"; |
| 15 | +import { fileURLToPath } from "node:url"; |
| 16 | + |
| 17 | +// Stack C — push --dry-run regression coverage. |
| 18 | +// |
| 19 | +// `--dry-run` MUST: |
| 20 | +// 1. Be accepted at parse time (no "Unrecognized argument" error) |
| 21 | +// 2. Print the dry-run mode banner so the operator can't miss it |
| 22 | +// 3. NOT write the state file (a real run would; dry-run never does) |
| 23 | +// 4. NOT fire any actual API calls (verified indirectly by lack of API |
| 24 | +// error output and by no state-file mutation, plus the "would PATCH/ |
| 25 | +// POST/DELETE" log lines) |
| 26 | + |
| 27 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 28 | +const REPO_ROOT = join(__dirname, ".."); |
| 29 | + |
| 30 | +interface Fixture { |
| 31 | + dir: string; |
| 32 | + cleanup: () => void; |
| 33 | +} |
| 34 | + |
| 35 | +function setupFixture(): Fixture { |
| 36 | + const dir = mkdtempSync(join(tmpdir(), "vapi-dry-run-test-")); |
| 37 | + cpSync(join(REPO_ROOT, "src"), join(dir, "src"), { recursive: true }); |
| 38 | + cpSync(join(REPO_ROOT, "package.json"), join(dir, "package.json")); |
| 39 | + symlinkSync(join(REPO_ROOT, "node_modules"), join(dir, "node_modules"), "dir"); |
| 40 | + // Empty resource tree — push has nothing real to do, but parsing and the |
| 41 | + // dry-run banner must still fire correctly. |
| 42 | + mkdirSync(join(dir, "resources", "test-dry-run"), { recursive: true }); |
| 43 | + writeFileSync( |
| 44 | + join(dir, ".env.test-dry-run"), |
| 45 | + "VAPI_TOKEN=fake-token-not-used\n", |
| 46 | + ); |
| 47 | + return { |
| 48 | + dir, |
| 49 | + cleanup: () => rmSync(dir, { recursive: true, force: true }), |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +function runPush( |
| 54 | + cwd: string, |
| 55 | + extraArgs: string[], |
| 56 | +): { code: number | null; stdout: string; stderr: string } { |
| 57 | + const result = spawnSync( |
| 58 | + "node", |
| 59 | + ["--import", "tsx", "src/push.ts", "test-dry-run", ...extraArgs], |
| 60 | + { |
| 61 | + cwd, |
| 62 | + env: { ...process.env, VAPI_TOKEN: "fake-token-not-used" }, |
| 63 | + encoding: "utf-8", |
| 64 | + timeout: 20_000, |
| 65 | + }, |
| 66 | + ); |
| 67 | + return { |
| 68 | + code: result.status, |
| 69 | + stdout: result.stdout || "", |
| 70 | + stderr: result.stderr || "", |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +test("--dry-run is accepted at parse time without unrecognized-arg error", () => { |
| 75 | + const fx = setupFixture(); |
| 76 | + try { |
| 77 | + const res = runPush(fx.dir, ["--dry-run", "--bootstrap"]); |
| 78 | + assert.doesNotMatch(res.stderr, /Unrecognized argument/); |
| 79 | + } finally { |
| 80 | + fx.cleanup(); |
| 81 | + } |
| 82 | +}); |
| 83 | + |
| 84 | +test("--dry-run prints the dry-run banner so the operator sees it", () => { |
| 85 | + const fx = setupFixture(); |
| 86 | + try { |
| 87 | + const res = runPush(fx.dir, ["--dry-run", "--bootstrap"]); |
| 88 | + // The banner mentions DRY-RUN explicitly so it's noisy enough not to be |
| 89 | + // missed in a CI log scroll. |
| 90 | + assert.match(res.stdout, /DRY-RUN/); |
| 91 | + } finally { |
| 92 | + fx.cleanup(); |
| 93 | + } |
| 94 | +}); |
| 95 | + |
| 96 | +test("--dry-run does NOT write the state file", () => { |
| 97 | + const fx = setupFixture(); |
| 98 | + try { |
| 99 | + const stateFilePath = join(fx.dir, ".vapi-state.test-dry-run.json"); |
| 100 | + assert.equal( |
| 101 | + existsSync(stateFilePath), |
| 102 | + false, |
| 103 | + "precondition: state file should not exist before run", |
| 104 | + ); |
| 105 | + |
| 106 | + const res = runPush(fx.dir, ["--dry-run", "--bootstrap"]); |
| 107 | + // Even with --bootstrap, dry-run must skip the state save. Bootstrap |
| 108 | + // would normally write the state file with refreshed credentials/UUIDs; |
| 109 | + // in dry-run we want zero filesystem mutation. |
| 110 | + assert.equal( |
| 111 | + existsSync(stateFilePath), |
| 112 | + false, |
| 113 | + `state file must not be created in dry-run; stdout=${res.stdout}`, |
| 114 | + ); |
| 115 | + } finally { |
| 116 | + fx.cleanup(); |
| 117 | + } |
| 118 | +}); |
0 commit comments