diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-02-16 22:49:07 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-02-16 22:49:07 +0900 |
| commit | efe05c1444963c046ab91bf54fa51a794bda58c0 (patch) | |
| tree | 509b48f27d2e888740bea6bfd6f50895705c7472 /worker/php/lib.mjs | |
| parent | db87f85aa7055e597800481b8cc6d006c70bcc88 (diff) | |
| download | phperkaigi-2026-albatross-efe05c1444963c046ab91bf54fa51a794bda58c0.tar.gz phperkaigi-2026-albatross-efe05c1444963c046ab91bf54fa51a794bda58c0.tar.zst phperkaigi-2026-albatross-efe05c1444963c046ab91bf54fa51a794bda58c0.zip | |
test(worker): add unit tests for php and swift workers
Extract testable logic from exec.mjs into lib.mjs (preprocessCode,
createIOCallbacks, buildResult) and add vitest tests. Add Go tests
for models, exec helpers, and handlers in worker/swift. Update
justfiles to include test tasks for local dev and CI.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'worker/php/lib.mjs')
| -rw-r--r-- | worker/php/lib.mjs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/worker/php/lib.mjs b/worker/php/lib.mjs new file mode 100644 index 0000000..4a34733 --- /dev/null +++ b/worker/php/lib.mjs @@ -0,0 +1,79 @@ +const PRELUDE = ` + define('STDIN', fopen('php://stdin', 'r')); + define('STDOUT', fopen('php://stdout', 'r')); + define('STDERR', fopen('php://stderr', 'r')); + + error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED); + + `; + +const BUFFER_MAX = 10 * 1024; + +export function preprocessCode(originalCode) { + if (originalCode.startsWith("<?php")) { + return PRELUDE + originalCode.slice(5); + } + if (originalCode.startsWith("<?")) { + return PRELUDE + originalCode.slice(2); + } + return PRELUDE + originalCode; +} + +export function createIOCallbacks(input) { + let stdinPos = 0; + const stdinBuf = Buffer.from(input); + let stdoutPos = 0; + const stdoutBuf = Buffer.alloc(BUFFER_MAX); + let stderrPos = 0; + const stderrBuf = Buffer.alloc(BUFFER_MAX); + + return { + stdin: () => { + if (stdinBuf.length <= stdinPos) { + return null; + } + return stdinBuf.readUInt8(stdinPos++); + }, + stdout: (asciiCode) => { + if (asciiCode === null) { + return; + } + if (BUFFER_MAX <= stdoutPos) { + return; + } + stdoutBuf.writeUInt8( + asciiCode < 0 ? asciiCode + 256 : asciiCode, + stdoutPos++, + ); + }, + stderr: (asciiCode) => { + if (asciiCode === null) { + return; + } + if (BUFFER_MAX <= stderrPos) { + return; + } + stderrBuf.writeUInt8( + asciiCode < 0 ? asciiCode + 256 : asciiCode, + stderrPos++, + ); + }, + getStdout: () => stdoutBuf.subarray(0, stdoutPos).toString(), + getStderr: () => stderrBuf.subarray(0, stderrPos).toString(), + }; +} + +export function buildResult(err, ccallResult, getStdout, getStderr) { + if (err) { + return { + status: "runtime_error", + stdout: getStdout(), + stderr: `${getStderr()}\n${err.toString()}`, + }; + } + return { + status: ccallResult === 0 ? "success" : "runtime_error", + stdout: getStdout(), + stderr: getStderr(), + }; +} |
