|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { |
| 3 | + mkdir, |
| 4 | + mkdtemp, |
| 5 | + readdir, |
| 6 | + readFile, |
| 7 | + rm, |
| 8 | + writeFile, |
| 9 | +} from "node:fs/promises"; |
| 10 | +import { tmpdir } from "node:os"; |
| 11 | +import { dirname, join, relative } from "node:path"; |
| 12 | +import test from "node:test"; |
| 13 | +import type { PromotionBindings } from "../src/promotion.ts"; |
| 14 | +import { |
| 15 | + promotionPlanApply, |
| 16 | + promotionPlanBuild, |
| 17 | + promotionStateParse, |
| 18 | +} from "../src/promotion.ts"; |
| 19 | + |
| 20 | +// Golden test for moving an assistant from a lower org into production: the |
| 21 | +// exact files promotion writes into the target org. Each piece is covered |
| 22 | +// elsewhere; this pins the whole written output, so a refactor that drifts |
| 23 | +// any of it (reference canonicalization, dependency selection, credential or |
| 24 | +// phone bindings, deletions, untouched unrelated files, markdown bodies) |
| 25 | +// fails here. Update the expected output only for an intended change. |
| 26 | + |
| 27 | +const FILES: Record<string, string> = { |
| 28 | + "resources/staging/assistants/intake.md": |
| 29 | + "---\nname: Intake\nfirstMessage: Thanks for calling, how can I help?\nmodel:\n provider: openai\n model: gpt-4.1\n toolIds:\n - 11111111-1111-4111-8111-111111111111\n - handoff-to-billing\n tools:\n - type: endCall\nvoice:\n provider: 11labs\n voiceId: sarah\n credentialId: 22222222-2222-4222-8222-222222222222\nphoneNumberIds:\n - staging-phone\n---\n\nYou are the intake agent. Ask for the caller's account number first.\n", |
| 30 | + "resources/staging/assistants/billing.yml": |
| 31 | + "name: Billing\nmodel:\n provider: openai\n model: gpt-4.1\n", |
| 32 | + "resources/staging/tools/lookup.yml": |
| 33 | + "type: function\nfunction:\n name: lookup_account\n parameters:\n type: object\n properties:\n accountNumber:\n type: string\nserver:\n url: https://crm.example.com/vapi\n credentialId: crm\n", |
| 34 | + "resources/staging/tools/handoff-to-billing.yml": |
| 35 | + "type: handoff\ndestinations:\n - type: assistant\n assistantId: 33333333-3333-4333-8333-333333333333\n description: Billing questions\n", |
| 36 | + "resources/staging/tools/staging-only-debug.yml": |
| 37 | + "type: function\nfunction:\n name: debug_dump\n", |
| 38 | + "resources/prod/assistants/intake.md": |
| 39 | + "---\nname: Intake\nmodel:\n provider: openai\n model: gpt-4o\n---\n\nOld prompt.\n", |
| 40 | + "resources/prod/assistants/retired.yml": "name: Retired\n", |
| 41 | + "resources/prod/squads/keep.yml": "name: Unrelated squad\n", |
| 42 | + ".env.staging": "VAPI_PHONE_NUMBER_MAIN=staging-phone\n", |
| 43 | + ".env.prod": "VAPI_PHONE_NUMBER_MAIN=prod-phone\n", |
| 44 | +}; |
| 45 | + |
| 46 | +const SOURCE_STATE = { |
| 47 | + tools: { |
| 48 | + lookup: { |
| 49 | + uuid: "11111111-1111-4111-8111-111111111111", |
| 50 | + }, |
| 51 | + }, |
| 52 | + assistants: { |
| 53 | + billing: { |
| 54 | + uuid: "33333333-3333-4333-8333-333333333333", |
| 55 | + }, |
| 56 | + retired: { |
| 57 | + uuid: "44444444-4444-4444-8444-444444444444", |
| 58 | + }, |
| 59 | + }, |
| 60 | + credentials: { |
| 61 | + eleven: { |
| 62 | + uuid: "22222222-2222-4222-8222-222222222222", |
| 63 | + }, |
| 64 | + crm: { |
| 65 | + uuid: "55555555-5555-4555-8555-555555555555", |
| 66 | + }, |
| 67 | + }, |
| 68 | +}; |
| 69 | + |
| 70 | +const TARGET_STATE = { |
| 71 | + assistants: { |
| 72 | + intake: { |
| 73 | + uuid: "66666666-6666-4666-8666-666666666666", |
| 74 | + }, |
| 75 | + retired: { |
| 76 | + uuid: "77777777-7777-4777-8777-777777777777", |
| 77 | + }, |
| 78 | + }, |
| 79 | + credentials: { |
| 80 | + eleven: { |
| 81 | + uuid: "prod-eleven", |
| 82 | + }, |
| 83 | + crm: { |
| 84 | + uuid: "prod-crm", |
| 85 | + }, |
| 86 | + }, |
| 87 | +}; |
| 88 | + |
| 89 | +const BINDINGS: PromotionBindings = { |
| 90 | + credentials: { |
| 91 | + default: "bind", |
| 92 | + aliases: {}, |
| 93 | + }, |
| 94 | + phoneNumbers: { |
| 95 | + default: "omit", |
| 96 | + aliases: { |
| 97 | + main: "bind", |
| 98 | + }, |
| 99 | + }, |
| 100 | +}; |
| 101 | + |
| 102 | +const EXPECTED_TARGET: Record<string, string> = { |
| 103 | + "assistants/billing.yml": |
| 104 | + "name: Billing\nmodel:\n provider: openai\n model: gpt-4.1\n", |
| 105 | + "assistants/intake.md": [ |
| 106 | + "---", |
| 107 | + "name: Intake", |
| 108 | + "firstMessage: Thanks for calling, how can I help?", |
| 109 | + "model:", |
| 110 | + " provider: openai", |
| 111 | + " model: gpt-4.1", |
| 112 | + " toolIds:", |
| 113 | + " - lookup", |
| 114 | + " - handoff-to-billing", |
| 115 | + " tools:", |
| 116 | + " - type: endCall", |
| 117 | + "voice:", |
| 118 | + " provider: 11labs", |
| 119 | + " voiceId: sarah", |
| 120 | + " credentialId: eleven", |
| 121 | + "phoneNumberIds:", |
| 122 | + " - prod-phone", |
| 123 | + "---", |
| 124 | + "", |
| 125 | + "You are the intake agent. Ask for the caller's account number first.", |
| 126 | + "", |
| 127 | + ].join("\n"), |
| 128 | + "squads/keep.yml": "name: Unrelated squad\n", |
| 129 | + "tools/handoff-to-billing.yml": [ |
| 130 | + "type: handoff", |
| 131 | + "destinations:", |
| 132 | + " - type: assistant", |
| 133 | + " assistantId: billing", |
| 134 | + " description: Billing questions", |
| 135 | + "", |
| 136 | + ].join("\n"), |
| 137 | + "tools/lookup.yml": [ |
| 138 | + "type: function", |
| 139 | + "function:", |
| 140 | + " name: lookup_account", |
| 141 | + " parameters:", |
| 142 | + " type: object", |
| 143 | + " properties:", |
| 144 | + " accountNumber:", |
| 145 | + " type: string", |
| 146 | + "server:", |
| 147 | + " url: https://crm.example.com/vapi", |
| 148 | + " credentialId: crm", |
| 149 | + "", |
| 150 | + ].join("\n"), |
| 151 | +}; |
| 152 | + |
| 153 | +async function fixtureWrite(): Promise<string> { |
| 154 | + const root = await mkdtemp(join(tmpdir(), "vapi-promotion-golden-")); |
| 155 | + for (const [path, content] of Object.entries(FILES)) { |
| 156 | + await mkdir(dirname(join(root, path)), { recursive: true }); |
| 157 | + await writeFile(join(root, path), content); |
| 158 | + } |
| 159 | + return root; |
| 160 | +} |
| 161 | + |
| 162 | +async function filesRead(dir: string): Promise<Record<string, string>> { |
| 163 | + const out: Record<string, string> = {}; |
| 164 | + const walk = async (current: string): Promise<void> => { |
| 165 | + for (const entry of await readdir(current, { withFileTypes: true })) { |
| 166 | + const full = join(current, entry.name); |
| 167 | + if (entry.isDirectory()) await walk(full); |
| 168 | + else out[relative(dir, full)] = await readFile(full, "utf8"); |
| 169 | + } |
| 170 | + }; |
| 171 | + await walk(dir); |
| 172 | + return Object.fromEntries( |
| 173 | + Object.entries(out).sort(([a], [b]) => a.localeCompare(b)), |
| 174 | + ); |
| 175 | +} |
| 176 | + |
| 177 | +function planArgs(root: string) { |
| 178 | + return { |
| 179 | + rootDir: root, |
| 180 | + source: "staging", |
| 181 | + target: "prod", |
| 182 | + patterns: ["assistants/**"], |
| 183 | + sourceState: promotionStateParse(JSON.stringify(SOURCE_STATE)), |
| 184 | + targetState: promotionStateParse(JSON.stringify(TARGET_STATE)), |
| 185 | + bindings: BINDINGS, |
| 186 | + }; |
| 187 | +} |
| 188 | + |
| 189 | +test("promoting an assistant writes exactly the expected target files", async () => { |
| 190 | + const root = await fixtureWrite(); |
| 191 | + try { |
| 192 | + const plan = await promotionPlanBuild(planArgs(root)); |
| 193 | + const changes = plan.changes.map((change) => [change.kind, change.path]); |
| 194 | + await promotionPlanApply(plan); |
| 195 | + assert.deepEqual( |
| 196 | + { |
| 197 | + changes, |
| 198 | + target: await filesRead(join(root, "resources/prod")), |
| 199 | + source: await filesRead(join(root, "resources/staging")), |
| 200 | + }, |
| 201 | + { |
| 202 | + changes: [ |
| 203 | + ["create", "assistants/billing.yml"], |
| 204 | + ["update", "assistants/intake.md"], |
| 205 | + ["delete", "assistants/retired.yml"], |
| 206 | + ["create", "tools/handoff-to-billing.yml"], |
| 207 | + ["create", "tools/lookup.yml"], |
| 208 | + ], |
| 209 | + target: EXPECTED_TARGET, |
| 210 | + // Promotion only ever writes the target org. |
| 211 | + source: Object.fromEntries( |
| 212 | + Object.entries(FILES) |
| 213 | + .filter(([path]) => path.startsWith("resources/staging/")) |
| 214 | + .map(([path, content]): [string, string] => [ |
| 215 | + path.slice("resources/staging/".length), |
| 216 | + content, |
| 217 | + ]) |
| 218 | + .sort(([a], [b]) => a.localeCompare(b)), |
| 219 | + ), |
| 220 | + }, |
| 221 | + ); |
| 222 | + } finally { |
| 223 | + await rm(root, { recursive: true, force: true }); |
| 224 | + } |
| 225 | +}); |
| 226 | + |
| 227 | +test("re-planning right after an apply finds nothing left to change", async () => { |
| 228 | + const root = await fixtureWrite(); |
| 229 | + try { |
| 230 | + await promotionPlanApply(await promotionPlanBuild(planArgs(root))); |
| 231 | + const again = await promotionPlanBuild(planArgs(root)); |
| 232 | + assert.deepEqual(again.changes, []); |
| 233 | + } finally { |
| 234 | + await rm(root, { recursive: true, force: true }); |
| 235 | + } |
| 236 | +}); |
0 commit comments