|
| 1 | +import "./engine-sandbox.ts"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { spawnSync } from "node:child_process"; |
| 4 | +import { existsSync, readdirSync, readFileSync, rmSync } from "node:fs"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import test from "node:test"; |
| 7 | +import { fileURLToPath } from "node:url"; |
| 8 | +import { |
| 9 | + assistantFile, |
| 10 | + assistantPayload, |
| 11 | + cliRun, |
| 12 | + engineRepoCreate, |
| 13 | + stateFile, |
| 14 | + stubStart, |
| 15 | +} from "./blocks-helpers.ts"; |
| 16 | + |
| 17 | +const ORG = "block-integration"; |
| 18 | +const UUID = "aaaaaaaa-1111-4111-8111-111111111111"; |
| 19 | + |
| 20 | +function scriptRun(root: string, script: string): void { |
| 21 | + const result = spawnSync( |
| 22 | + process.execPath, |
| 23 | + [ |
| 24 | + "--import", |
| 25 | + "tsx", |
| 26 | + "--input-type=module", |
| 27 | + "-e", |
| 28 | + ` |
| 29 | + import assert from "node:assert/strict"; |
| 30 | + process.argv = ["node", "test", "${ORG}"]; |
| 31 | + process.env.VAPI_TOKEN = "fake"; |
| 32 | + ${script} |
| 33 | + `, |
| 34 | + ], |
| 35 | + { |
| 36 | + cwd: root, |
| 37 | + env: { ...process.env }, |
| 38 | + encoding: "utf8", |
| 39 | + timeout: 20000, |
| 40 | + }, |
| 41 | + ); |
| 42 | + assert.equal(result.status, 0, result.stdout + result.stderr); |
| 43 | +} |
| 44 | + |
| 45 | +test("adoption expands block aliases before comparing baselines and refuses unsafe aliases", () => { |
| 46 | + const root = engineRepoCreate({ |
| 47 | + "templates/blocks/greeting.md": "Hello.", |
| 48 | + [`resources/${ORG}/assistants/alias.md`]: assistantFile( |
| 49 | + "Triage", |
| 50 | + "@include greeting", |
| 51 | + ), |
| 52 | + }); |
| 53 | + try { |
| 54 | + scriptRun( |
| 55 | + root, |
| 56 | + ` |
| 57 | + const { existsSync, writeFileSync, unlinkSync } = await import("node:fs"); |
| 58 | + const { prepareAliasCleanup } = await import("./src/adopt-alias-files.ts"); |
| 59 | + const { writeBaseline, readBaseline } = await import("./src/hash-store.ts"); |
| 60 | + const { canonicalizeForHash } = await import("./src/canonical.ts"); |
| 61 | + const { hashPayload } = await import("./src/state-serialize.ts"); |
| 62 | + const { createEmptyState } = await import("./src/state.ts"); |
| 63 | + const { RESOURCES_DIR } = await import("./src/config.ts"); |
| 64 | + const path = RESOURCES_DIR + "/assistants/alias.md"; |
| 65 | + const resources = Object.fromEntries(Object.keys(createEmptyState()).filter(k => k !== "credentials").map(k => [k, []])); |
| 66 | + resources.assistants.push({ resourceId: "alias", filePath: path, data: {} }); |
| 67 | + const aliases = [{ type: "assistants", resourceId: "alias", uuid: "${UUID}", adoptedAs: "triage" }]; |
| 68 | + const hash = hashPayload(canonicalizeForHash(${JSON.stringify(assistantPayload(UUID, "Triage", "Hello."))}, createEmptyState(), new Map(), "assistants")); |
| 69 | + assert.deepEqual((await prepareAliasCleanup([], resources)).problems, []); |
| 70 | + assert.equal((await prepareAliasCleanup(aliases, resources)).problems.length, 1); |
| 71 | + await writeBaseline("${ORG}", "${UUID}", hash); |
| 72 | + writeFileSync("templates/blocks/greeting.md", "Edited locally."); |
| 73 | + assert.equal((await prepareAliasCleanup(aliases, resources)).problems.length, 1); |
| 74 | + assert.ok(existsSync(path)); |
| 75 | + unlinkSync("templates/blocks/greeting.md"); |
| 76 | + assert.equal((await prepareAliasCleanup(aliases, resources)).problems.length, 1); |
| 77 | + assert.ok(existsSync(path)); |
| 78 | + writeFileSync("templates/blocks/greeting.md", "Hello."); |
| 79 | + const cleanup = await prepareAliasCleanup(aliases, resources); |
| 80 | + assert.deepEqual(cleanup.problems, []); |
| 81 | + await cleanup.run(); |
| 82 | + assert.equal(existsSync(path), false); |
| 83 | + assert.deepEqual(resources.assistants, []); |
| 84 | + assert.equal(readBaseline("${ORG}", "${UUID}"), undefined); |
| 85 | + `, |
| 86 | + ); |
| 87 | + } finally { |
| 88 | + rmSync(root, { recursive: true, force: true }); |
| 89 | + } |
| 90 | +}); |
| 91 | + |
| 92 | +test("local hashes expand blocks and ignore folder metadata only for assistants", () => { |
| 93 | + const data = { |
| 94 | + name: "Triage", |
| 95 | + model: { messages: [{ role: "system", content: "@include greeting" }] }, |
| 96 | + metadata: { |
| 97 | + folderPath: "user", |
| 98 | + gitops: { owner: "repo", folderPath: "old" }, |
| 99 | + }, |
| 100 | + }; |
| 101 | + const root = engineRepoCreate({ |
| 102 | + "templates/blocks/greeting.md": "Hello.", |
| 103 | + [`resources/${ORG}/assistants/support/triage.yml`]: JSON.stringify(data), |
| 104 | + [`resources/${ORG}/squads/triage.yml`]: JSON.stringify(data), |
| 105 | + [`resources/${ORG}/assistants/empty.yml`]: "{}", |
| 106 | + [`resources/${ORG}/assistants/broken.md`]: assistantFile( |
| 107 | + "Broken", |
| 108 | + "@include missing", |
| 109 | + ), |
| 110 | + }); |
| 111 | + try { |
| 112 | + scriptRun( |
| 113 | + root, |
| 114 | + ` |
| 115 | + const { hashLocalResource } = await import("./src/resources.ts"); |
| 116 | + const { hashPayload } = await import("./src/state-serialize.ts"); |
| 117 | + const expanded = ${JSON.stringify({ ...data, model: { messages: [{ role: "system", content: "Hello." }] } })}; |
| 118 | + assert.equal(hashLocalResource("squads", "triage"), hashPayload(expanded)); |
| 119 | + delete expanded.metadata.gitops.folderPath; |
| 120 | + assert.equal(hashLocalResource("assistants", "support/triage"), hashPayload(expanded)); |
| 121 | + assert.equal(hashLocalResource("assistants", "empty"), hashPayload({})); |
| 122 | + assert.equal(hashLocalResource("assistants", "missing"), null); |
| 123 | + assert.equal(hashLocalResource("assistants", "broken"), null); |
| 124 | + `, |
| 125 | + ); |
| 126 | + } finally { |
| 127 | + rmSync(root, { recursive: true, force: true }); |
| 128 | + } |
| 129 | +}); |
| 130 | + |
| 131 | +test("repeated pull of a subfolder block assistant ignores derived folder metadata", async () => { |
| 132 | + const path = `resources/${ORG}/assistants/support/triage.md`; |
| 133 | + const authored = assistantFile("Triage", "@include greeting"); |
| 134 | + const root = engineRepoCreate({ |
| 135 | + "templates/blocks/greeting.md": "Hello.", |
| 136 | + [path]: authored, |
| 137 | + [`state/${ORG}/state.json`]: stateFile({ "support/triage": UUID }), |
| 138 | + }); |
| 139 | + const stub = await stubStart([ |
| 140 | + { |
| 141 | + method: "GET", |
| 142 | + pathStartsWith: "/assistant", |
| 143 | + body: [ |
| 144 | + { |
| 145 | + ...assistantPayload(UUID, "Triage", "Hello."), |
| 146 | + metadata: { gitops: { folderPath: "support" } }, |
| 147 | + }, |
| 148 | + ], |
| 149 | + }, |
| 150 | + ]); |
| 151 | + try { |
| 152 | + for (let i = 0; i < 2; i++) { |
| 153 | + const result = cliRun(root, "src/pull.ts", [ORG], stub.port); |
| 154 | + assert.equal(result.code, 0, result.output); |
| 155 | + assert.match( |
| 156 | + result.output, |
| 157 | + /support\/triage \(uses blocks, matches the dashboard/, |
| 158 | + ); |
| 159 | + assert.doesNotMatch( |
| 160 | + result.output, |
| 161 | + /support\/triage:|support\/triage \(local ahead/, |
| 162 | + ); |
| 163 | + assert.equal(readFileSync(join(root, path), "utf8"), authored); |
| 164 | + assert.deepEqual( |
| 165 | + readdirSync(join(root, `resources/${ORG}/assistants/support`)), |
| 166 | + ["triage.md"], |
| 167 | + ); |
| 168 | + } |
| 169 | + } finally { |
| 170 | + await stub.stop(); |
| 171 | + rmSync(root, { recursive: true, force: true }); |
| 172 | + } |
| 173 | +}); |
| 174 | + |
| 175 | +test("block-expanded undefined variables block strict validate and push before mutations", async () => { |
| 176 | + const tracked = JSON.parse(stateFile({ triage: UUID })); |
| 177 | + tracked.squads.team = { uuid: "bbbbbbbb-2222-4222-8222-222222222222" }; |
| 178 | + const root = engineRepoCreate({ |
| 179 | + "templates/blocks/greeting.md": "Welcome to {{practice}}.", |
| 180 | + [`resources/${ORG}/assistants/triage.md`]: assistantFile( |
| 181 | + "Triage", |
| 182 | + "@include greeting", |
| 183 | + ), |
| 184 | + [`resources/${ORG}/squads/team.yml`]: |
| 185 | + "name: Team\nmembers:\n - assistantId: triage\n", |
| 186 | + [`state/${ORG}/state.json`]: JSON.stringify(tracked), |
| 187 | + }); |
| 188 | + const assistant = assistantPayload( |
| 189 | + UUID, |
| 190 | + "Triage", |
| 191 | + "Welcome to {{practice}}.", |
| 192 | + ); |
| 193 | + const squad = { |
| 194 | + id: tracked.squads.team.uuid, |
| 195 | + name: "Team", |
| 196 | + members: [{ assistantId: UUID }], |
| 197 | + }; |
| 198 | + const stub = await stubStart([ |
| 199 | + { method: "GET", pathStartsWith: `/assistant/${UUID}`, body: assistant }, |
| 200 | + { method: "GET", pathStartsWith: "/assistant", body: [assistant] }, |
| 201 | + { method: "GET", pathStartsWith: `/squad/${squad.id}`, body: squad }, |
| 202 | + { method: "GET", pathStartsWith: "/squad", body: [squad] }, |
| 203 | + ]); |
| 204 | + try { |
| 205 | + for (const strict of [false, true]) { |
| 206 | + const result = cliRun( |
| 207 | + root, |
| 208 | + "src/validate-cmd.ts", |
| 209 | + [ORG, ...(strict ? ["--strict"] : [])], |
| 210 | + stub.port, |
| 211 | + ); |
| 212 | + assert.equal(result.code, strict ? 1 : 0, result.output); |
| 213 | + assert.match(result.output, /variable-undefined/); |
| 214 | + } |
| 215 | + const advisory = cliRun(root, "src/push.ts", [ORG, "--dry-run"], stub.port); |
| 216 | + assert.equal(advisory.code, 0, advisory.output); |
| 217 | + assert.match(advisory.output, /variable-undefined/); |
| 218 | + for (const flags of [ |
| 219 | + [], |
| 220 | + ["--dry-run"], |
| 221 | + [`resources/${ORG}/assistants/triage.md`], |
| 222 | + ]) { |
| 223 | + const result = cliRun( |
| 224 | + root, |
| 225 | + "src/push.ts", |
| 226 | + [ORG, "--strict", ...flags], |
| 227 | + stub.port, |
| 228 | + ); |
| 229 | + assert.equal(result.code, 1, result.output); |
| 230 | + assert.match(result.output, /variable-undefined/); |
| 231 | + assert.match(result.output, /Nothing was written to the remote API/); |
| 232 | + } |
| 233 | + assert.deepEqual( |
| 234 | + (await stub.requests()).filter((request) => |
| 235 | + /^(POST|PATCH|DELETE) /.test(request), |
| 236 | + ), |
| 237 | + [], |
| 238 | + ); |
| 239 | + } finally { |
| 240 | + await stub.stop(); |
| 241 | + rmSync(root, { recursive: true, force: true }); |
| 242 | + } |
| 243 | +}); |
| 244 | + |
| 245 | +test("block integrations do not restore the removed recanonicalize module", () => { |
| 246 | + const src = fileURLToPath(new URL("../src", import.meta.url)); |
| 247 | + assert.equal(existsSync(join(src, "recanonicalize.ts")), false); |
| 248 | + for (const file of readdirSync(src).filter((file) => file.endsWith(".ts"))) { |
| 249 | + const code = readFileSync(join(src, file), "utf8") |
| 250 | + .split("\n") |
| 251 | + .filter((line) => !line.trimStart().startsWith("//")) |
| 252 | + .join("\n"); |
| 253 | + assert.doesNotMatch( |
| 254 | + code, |
| 255 | + /recanonicalizeStateKeys|formatRecanonicalizeReport|["\x27].*recanonicalize\.ts["\x27]/, |
| 256 | + file, |
| 257 | + ); |
| 258 | + } |
| 259 | +}); |
0 commit comments