|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { chmod, mkdtemp, readFile, rm, writeFile } from "node:fs/promises" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import { join, resolve } from "node:path" |
| 5 | +import { spawn } from "node:child_process" |
| 6 | +import test from "node:test" |
| 7 | + |
| 8 | +const script = resolve("scripts/provision-cloudflare-d1-coordinator.mjs") |
| 9 | + |
| 10 | +test("D1 provisioner creates once and emits a deployable config deterministically", async () => { |
| 11 | + const directory = await mkdtemp(join(tmpdir(), "wp-codebox-d1-provision-")) |
| 12 | + try { |
| 13 | + const wrangler = join(directory, "wrangler") |
| 14 | + const state = join(directory, "state") |
| 15 | + const calls = join(directory, "calls") |
| 16 | + const template = join(directory, "wrangler.d1.jsonc") |
| 17 | + const output = join(directory, "production.json") |
| 18 | + await writeFile(state, "missing") |
| 19 | + await writeFile(calls, "") |
| 20 | + await writeFile(template, `// template\n{"name":"worker","main":"src/worker-d1.ts","d1_databases":[{"binding":"WORDPRESS_STATE_DATABASE","database_name":"wp-codebox-runtime-state","database_id":"00000000-0000-0000-0000-000000000000"}],}`) |
| 21 | + await writeFile(wrangler, `#!${process.execPath}\nimport { appendFileSync, readFileSync, writeFileSync } from "node:fs"; const args=process.argv.slice(2); appendFileSync(${JSON.stringify(calls)}, JSON.stringify(args)+"\\n"); if(args[1]==="list") process.stdout.write(readFileSync(${JSON.stringify(state)},"utf8")==="created"?JSON.stringify([{name:"wp-codebox-runtime-state",uuid:"11111111-2222-3333-4444-555555555555"}]):"[]"); else if(args[1]==="create") writeFileSync(${JSON.stringify(state)},"created");`) |
| 22 | + await chmod(wrangler, 0o755) |
| 23 | + |
| 24 | + const first = await run(["--template", template, "--output", output, "--wrangler", wrangler]) |
| 25 | + const second = await run(["--template", template, "--output", output, "--wrangler", wrangler]) |
| 26 | + assert.equal(first.databaseId, "11111111-2222-3333-4444-555555555555") |
| 27 | + assert.deepEqual(second, first) |
| 28 | + const config = JSON.parse(await readFile(output, "utf8")) |
| 29 | + assert.equal(config.d1_databases[0].database_id, first.databaseId) |
| 30 | + const invocations = (await readFile(calls, "utf8")).trim().split("\n").map(JSON.parse) |
| 31 | + assert.equal(invocations.filter((args) => args[1] === "create").length, 1) |
| 32 | + } finally { |
| 33 | + await rm(directory, { recursive: true, force: true }) |
| 34 | + } |
| 35 | +}) |
| 36 | + |
| 37 | +function run(args) { |
| 38 | + return new Promise((resolveRun, reject) => { |
| 39 | + const child = spawn(process.execPath, [script, ...args], { stdio: ["ignore", "pipe", "pipe"] }) |
| 40 | + let stdout = "" |
| 41 | + let stderr = "" |
| 42 | + child.stdout.setEncoding("utf8") |
| 43 | + child.stderr.setEncoding("utf8") |
| 44 | + child.stdout.on("data", (chunk) => { stdout += chunk }) |
| 45 | + child.stderr.on("data", (chunk) => { stderr += chunk }) |
| 46 | + child.on("error", reject) |
| 47 | + child.on("exit", (code) => code === 0 ? resolveRun(JSON.parse(stdout)) : reject(new Error(stderr))) |
| 48 | + }) |
| 49 | +} |
0 commit comments