From efe05c1444963c046ab91bf54fa51a794bda58c0 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 16 Feb 2026 22:49:07 +0900 Subject: 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 --- worker/php/exec.mjs | 78 ++++++----------------------------------------------- 1 file changed, 8 insertions(+), 70 deletions(-) (limited to 'worker/php/exec.mjs') diff --git a/worker/php/exec.mjs b/worker/php/exec.mjs index d8ca899..21bd93f 100644 --- a/worker/php/exec.mjs +++ b/worker/php/exec.mjs @@ -1,65 +1,14 @@ import PHPWasm from "./php-wasm.js"; +import { buildResult, createIOCallbacks, preprocessCode } from "./lib.mjs"; process.once("message", async ({ code: originalCode, input }) => { - 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); - - `; - - // remove php tag - let code; - if (originalCode.startsWith(" { - if (stdinBuf.length <= stdinPos) { - return null; - } - return stdinBuf.readUInt8(stdinPos++); - }, - stdout: (asciiCode) => { - if (asciiCode === null) { - return; // flush - } - if (BUFFER_MAX <= stdoutPos) { - return; // ignore - } - stdoutBuf.writeUInt8( - asciiCode < 0 ? asciiCode + 256 : asciiCode, - stdoutPos++, - ); - }, - stderr: (asciiCode) => { - if (asciiCode === null) { - return; // flush - } - if (BUFFER_MAX <= stderrPos) { - return; // ignore - } - stderrBuf.writeUInt8( - asciiCode < 0 ? asciiCode + 256 : asciiCode, - stderrPos++, - ); - }, + stdin: io.stdin, + stdout: io.stdout, + stderr: io.stderr, }); let err; @@ -69,17 +18,6 @@ process.once("message", async ({ code: originalCode, input }) => { } catch (e) { err = e; } - if (err) { - process.send({ - status: "runtime_error", - stdout: stdoutBuf.subarray(0, stdoutPos).toString(), - stderr: `${stderrBuf.subarray(0, stderrPos).toString()}\n${err.toString()}`, - }); - } else { - process.send({ - status: result === 0 ? "success" : "runtime_error", - stdout: stdoutBuf.subarray(0, stdoutPos).toString(), - stderr: stderrBuf.subarray(0, stderrPos).toString(), - }); - } + + process.send(buildResult(err, result, io.getStdout, io.getStderr)); }); -- cgit v1.3.1