|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import { join } from "node:path" |
| 5 | +import { buildAgentTaskRecipe } from "../packages/runtime-core/src/agent-task-recipe.js" |
| 6 | +import { normalizeTaskInput } from "../packages/runtime-core/src/task-input.js" |
| 7 | +import { runRecipeRunCommand } from "../packages/cli/src/commands/recipe-run.js" |
| 8 | + |
| 9 | +const root = mkdtempSync(join(tmpdir(), "wp-codebox-agent-runtime-signal-smoke-")) |
| 10 | + |
| 11 | +try { |
| 12 | + const agentRecipe = buildAgentTaskRecipe({ |
| 13 | + goal: "report agent runtime signal", |
| 14 | + runtime_env: { EXISTING_RUNTIME_ENV: "preserved", WP_CODEBOX_AGENT_RUNTIME: "caller-value" }, |
| 15 | + }, normalizeTaskInput({ goal: "report agent runtime signal" }), "latest") |
| 16 | + assert.equal(agentRecipe.inputs?.runtimeEnv?.WP_CODEBOX_AGENT_RUNTIME, "1") |
| 17 | + assert.equal(agentRecipe.inputs?.runtimeEnv?.EXISTING_RUNTIME_ENV, "preserved") |
| 18 | + assert.ok(!agentRecipe.inputs?.secretEnv?.includes("WP_CODEBOX_AGENT_RUNTIME")) |
| 19 | + |
| 20 | + agentRecipe.workflow.steps = [{ |
| 21 | + command: "wp-codebox.agent-sandbox-run", |
| 22 | + args: [ |
| 23 | + "task=report agent runtime signal", |
| 24 | + "code=echo getenv('WP_CODEBOX_AGENT_RUNTIME') ?: 'unset';", |
| 25 | + ], |
| 26 | + }] |
| 27 | + const agentRecipePath = join(root, "agent-runtime-recipe.json") |
| 28 | + writeFileSync(agentRecipePath, JSON.stringify(agentRecipe, null, 2)) |
| 29 | + |
| 30 | + const agentOutput = await runRecipe(agentRecipePath) |
| 31 | + assert.equal(agentOutput.success, true) |
| 32 | + const agentPayload = JSON.parse(String(agentOutput.executions?.[0]?.stdout ?? "{}")) as { output?: string } |
| 33 | + assert.equal(agentPayload.output, "1") |
| 34 | + |
| 35 | + const frontendRecipePath = join(root, "frontend-run-php-recipe.json") |
| 36 | + writeFileSync(frontendRecipePath, JSON.stringify({ |
| 37 | + schema: "wp-codebox/workspace-recipe/v1", |
| 38 | + runtime: { backend: "wordpress-playground", wp: "latest", blueprint: { steps: [] } }, |
| 39 | + inputs: {}, |
| 40 | + workflow: { |
| 41 | + steps: [{ |
| 42 | + command: "wordpress.run-php", |
| 43 | + args: ["code=echo getenv('WP_CODEBOX_AGENT_RUNTIME') ?: 'unset';"], |
| 44 | + }], |
| 45 | + }, |
| 46 | + }, null, 2)) |
| 47 | + |
| 48 | + const frontendOutput = await runRecipe(frontendRecipePath) |
| 49 | + assert.equal(frontendOutput.success, true) |
| 50 | + assert.equal(frontendOutput.executions?.[0]?.stdout, "unset") |
| 51 | + |
| 52 | + console.log("agent-runtime-signal-smoke: ok") |
| 53 | +} finally { |
| 54 | + rmSync(root, { recursive: true, force: true }) |
| 55 | +} |
| 56 | + |
| 57 | +async function runRecipe(recipePath: string): Promise<{ success?: boolean; executions?: Array<{ stdout?: string }> }> { |
| 58 | + const output = await captureStdout(async () => await runRecipeRunCommand(["--recipe", recipePath, "--json"])) |
| 59 | + return JSON.parse(output) as { success?: boolean; executions?: Array<{ stdout?: string }> } |
| 60 | +} |
| 61 | + |
| 62 | +async function captureStdout(callback: () => Promise<unknown>): Promise<string> { |
| 63 | + const originalWrite = process.stdout.write.bind(process.stdout) |
| 64 | + let stdout = "" |
| 65 | + ;(process.stdout.write as typeof process.stdout.write) = ((chunk: string | Uint8Array, encodingOrCallback?: BufferEncoding | ((error?: Error | null) => void), callback?: (error?: Error | null) => void) => { |
| 66 | + stdout += typeof chunk === "string" ? chunk : chunk.toString() |
| 67 | + if (typeof encodingOrCallback === "function") { |
| 68 | + encodingOrCallback() |
| 69 | + } else if (callback) { |
| 70 | + callback() |
| 71 | + } |
| 72 | + return true |
| 73 | + }) as typeof process.stdout.write |
| 74 | + try { |
| 75 | + await callback() |
| 76 | + return stdout |
| 77 | + } finally { |
| 78 | + process.stdout.write = originalWrite |
| 79 | + } |
| 80 | +} |
0 commit comments