|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { mkdtemp, rm } from "node:fs/promises" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import { join } from "node:path" |
| 5 | +import { createRuntime } from "../packages/runtime-core/src/index.js" |
| 6 | +import { executeRecipeWorkflowStep } from "../packages/cli/src/commands/recipe-run-workflow-evidence.js" |
| 7 | +import { createPlaygroundRuntimeBackend } from "../packages/runtime-playground/src/index.js" |
| 8 | +import type { PlaygroundCliModule } from "../packages/runtime-playground/src/playground-cli-runner.js" |
| 9 | + |
| 10 | +const root = await mkdtemp(join(tmpdir(), "wp-codebox-wp-cli-result-")) |
| 11 | +const wordpressDirectory = join(root, "wordpress") |
| 12 | +const files = new Map<string, string>() |
| 13 | +const payload = { schema: "example/validation-result/v1", status: "ok", evidence: { retained: true } } |
| 14 | +let wrapperPath = "" |
| 15 | + |
| 16 | +const cliModule: PlaygroundCliModule = { |
| 17 | + async runCLI() { |
| 18 | + return { |
| 19 | + serverUrl: "http://127.0.0.1:9404", |
| 20 | + playground: { |
| 21 | + async writeFile(path, contents) { |
| 22 | + files.set(path, contents) |
| 23 | + wrapperPath = path |
| 24 | + }, |
| 25 | + unlink(path) { |
| 26 | + files.delete(path) |
| 27 | + }, |
| 28 | + async run(options) { |
| 29 | + assert.ok("scriptPath" in options) |
| 30 | + const scriptPath = options.scriptPath |
| 31 | + assert.equal(files.has(scriptPath), true, "WP-CLI wrapper must exist while the command runs") |
| 32 | + return { |
| 33 | + exitCode: 0, |
| 34 | + errors: "", |
| 35 | + get text() { |
| 36 | + return files.has(scriptPath) ? `${JSON.stringify(payload)}\n` : "" |
| 37 | + }, |
| 38 | + } |
| 39 | + }, |
| 40 | + }, |
| 41 | + async [Symbol.asyncDispose]() {}, |
| 42 | + } |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +const runtime = await createRuntime({ |
| 47 | + backend: "wordpress-playground", |
| 48 | + artifactsDirectory: join(root, "artifacts"), |
| 49 | + environment: { |
| 50 | + kind: "wordpress", |
| 51 | + name: "wp-cli-result", |
| 52 | + version: "mounted-wordpress-source", |
| 53 | + phpVersion: "8.4", |
| 54 | + wordpressInstallMode: "do-not-attempt-installing", |
| 55 | + assets: { wordpressDirectory }, |
| 56 | + blueprint: {}, |
| 57 | + }, |
| 58 | + policy: { |
| 59 | + network: "deny", |
| 60 | + filesystem: "sandbox", |
| 61 | + commands: ["wordpress.wp-cli"], |
| 62 | + secrets: "none", |
| 63 | + approvals: "never", |
| 64 | + }, |
| 65 | +}, createPlaygroundRuntimeBackend({ cliModule })) |
| 66 | + |
| 67 | +try { |
| 68 | + const execution = await executeRecipeWorkflowStep(runtime, { |
| 69 | + phase: "steps", |
| 70 | + index: 0, |
| 71 | + step: { command: "wordpress.wp-cli", args: ["command=example validate --format=json"] }, |
| 72 | + }, root) |
| 73 | + |
| 74 | + assert.equal(execution.exitCode, 0) |
| 75 | + assert.equal(files.has(wrapperPath), false, "completed WP-CLI wrappers must be cleaned up") |
| 76 | + assert.equal(execution.stdout, `${JSON.stringify(payload)}\n`) |
| 77 | + assert.deepEqual(execution.result?.json, payload, "recipe output must retain successful structured WP-CLI output") |
| 78 | +} finally { |
| 79 | + await runtime.destroy() |
| 80 | + await rm(root, { recursive: true, force: true }) |
| 81 | +} |
| 82 | + |
| 83 | +console.log("WP-CLI recipe result survives temporary wrapper cleanup") |
0 commit comments