|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { runWithTemporaryWpCliScript, shellArgv } from "../packages/runtime-playground/dist/wp-cli-command-handlers.js" |
| 3 | + |
| 4 | +class MemoryFilesystem { |
| 5 | + readonly files = new Map<string, string>() |
| 6 | + readonly writtenPaths: string[] = [] |
| 7 | + readonly unlinkedPaths: string[] = [] |
| 8 | + |
| 9 | + async writeFile(path: string, contents: string): Promise<void> { |
| 10 | + if (this.files.has(path)) { |
| 11 | + throw new Error(`Could not write to ${JSON.stringify(path)}: File exists.`) |
| 12 | + } |
| 13 | + this.files.set(path, contents) |
| 14 | + this.writtenPaths.push(path) |
| 15 | + } |
| 16 | + |
| 17 | + unlink(path: string): void { |
| 18 | + this.files.delete(path) |
| 19 | + this.unlinkedPaths.push(path) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +const oldPaths = Array.from({ length: 16 }, () => "/tmp/wp-codebox-wp-cli-2.php") |
| 24 | +assert.equal(new Set(oldPaths).size, 1, "the old command-count suffix reproduces the shared wrapper path") |
| 25 | + |
| 26 | +const filesystem = new MemoryFilesystem() |
| 27 | +let invocationCount = 0 |
| 28 | +for (const { concurrency, iterations, repetitions } of [ |
| 29 | + { concurrency: 2, iterations: 128, repetitions: 3 }, |
| 30 | + { concurrency: 6, iterations: 256, repetitions: 3 }, |
| 31 | + { concurrency: 16, iterations: 512, repetitions: 2 }, |
| 32 | + { concurrency: 64, iterations: 1_024, repetitions: 1 }, |
| 33 | +]) { |
| 34 | + for (let repetition = 0; repetition < repetitions; repetition++) { |
| 35 | + const markers = Array.from({ length: iterations }, (_, index) => `marker-${concurrency}-${repetition}-${index.toString().padStart(6, "0")}`) |
| 36 | + const outputs = await runBounded(markers, concurrency, async (marker, index) => { |
| 37 | + invocationCount++ |
| 38 | + return runWithTemporaryWpCliScript( |
| 39 | + filesystem, |
| 40 | + "runtime/shared/../../unsafe", |
| 41 | + ["eval", `echo ${JSON.stringify(marker)};`], |
| 42 | + async (scriptPath) => { |
| 43 | + await new Promise((resolve) => setTimeout(resolve, index % 5)) |
| 44 | + const script = filesystem.files.get(scriptPath) |
| 45 | + assert.ok(script, `wrapper must exist while ${marker} executes`) |
| 46 | + assert.match(scriptPath, /^\/tmp\/wp-codebox-wp-cli-runtime-shared-------unsafe-[a-f0-9]{32}\.php$/) |
| 47 | + assert.ok(script.includes(marker), `${marker} must execute only its own payload`) |
| 48 | + const otherMarker = markers[(index + 1) % markers.length] |
| 49 | + assert.equal(script.includes(otherMarker), false, `${marker} must not execute ${otherMarker}`) |
| 50 | + return marker |
| 51 | + }, |
| 52 | + ) |
| 53 | + }) |
| 54 | + assert.deepEqual(outputs, markers) |
| 55 | + assert.equal(filesystem.files.size, 0, `concurrency ${concurrency} must return the temp directory to baseline`) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +assert.equal(new Set(filesystem.writtenPaths).size, invocationCount) |
| 60 | +assert.deepEqual(new Set(filesystem.unlinkedPaths), new Set(filesystem.writtenPaths)) |
| 61 | +assert.throws(() => shellArgv("eval 'unterminated"), /Unclosed quote/, "malformed WP-CLI input must fail before allocating a wrapper") |
| 62 | + |
| 63 | +const nonzero = await runWithTemporaryWpCliScript(filesystem, "runtime-nonzero", ["eval", "exit(7)"], async () => ({ exitCode: 7, text: "", errors: "intentional" })) |
| 64 | +assert.deepEqual(nonzero, { exitCode: 7, text: "", errors: "intentional" }, "structured nonzero responses must remain compatible") |
| 65 | +assert.equal(filesystem.files.size, 0, "structured nonzero responses must clean up their wrapper") |
| 66 | + |
| 67 | +for (const failure of [ |
| 68 | + new Error("wrapper execution failure"), |
| 69 | + new Error("runtime command exceeded timeout"), |
| 70 | + new Error("runtime execution was aborted"), |
| 71 | + new Error("adapter exception"), |
| 72 | +]) { |
| 73 | + await assert.rejects( |
| 74 | + runWithTemporaryWpCliScript(filesystem, "runtime-failures", ["eval", "broken"], async () => { throw failure }), |
| 75 | + failure, |
| 76 | + ) |
| 77 | + assert.equal(filesystem.files.size, 0, `${failure.message} must clean up its owned wrapper`) |
| 78 | +} |
| 79 | + |
| 80 | +let unlinkedAfterRejectedWrite = false |
| 81 | +await assert.rejects( |
| 82 | + runWithTemporaryWpCliScript({ |
| 83 | + async writeFile() { |
| 84 | + throw new Error("File exists") |
| 85 | + }, |
| 86 | + unlink() { |
| 87 | + unlinkedAfterRejectedWrite = true |
| 88 | + }, |
| 89 | + }, "runtime-write-failure", ["version"], async () => "unreachable"), |
| 90 | + /File exists/, |
| 91 | +) |
| 92 | +assert.equal(unlinkedAfterRejectedWrite, false, "a rejected write must not claim or remove another invocation's file") |
| 93 | + |
| 94 | +console.log(`WP-CLI temporary wrapper stress ok: ${invocationCount} isolated invocations at concurrency 2, 6, 16, and 64`) |
| 95 | + |
| 96 | +async function runBounded<T, R>(items: T[], concurrency: number, operation: (item: T, index: number) => Promise<R>): Promise<R[]> { |
| 97 | + const outputs = new Array<R>(items.length) |
| 98 | + let nextIndex = 0 |
| 99 | + await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, async () => { |
| 100 | + while (nextIndex < items.length) { |
| 101 | + const index = nextIndex++ |
| 102 | + outputs[index] = await operation(items[index], index) |
| 103 | + } |
| 104 | + })) |
| 105 | + return outputs |
| 106 | +} |
0 commit comments