Files
oh-my-pi/packages/coding-agent/test/worker-selector.test.ts
T
2026-07-25 00:42:23 +00:00

62 lines
2.3 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test";
import { runCli } from "../src/cli";
// The worker-host re-entry seam dispatches any `__omp_worker_*` selector to
// `runWorkerEntrypoint`. An unrecognized selector must fail loudly rather than
// exit 0 with empty output, so a stale/mistyped selector cannot look healthy to
// a parent process or install smoke path (issue #5712).
describe("worker selector dispatch", () => {
beforeEach(() => {
process.exitCode = 0;
});
afterEach(() => {
vi.restoreAllMocks();
process.exitCode = 0;
});
it("fails with a nonzero exit and stderr error on an unknown selector", async () => {
const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
await runCli(["__omp_worker_does_not_exist"]);
expect(process.exitCode).toBe(1);
expect(stderr).toHaveBeenCalledWith("Error: unknown worker selector: __omp_worker_does_not_exist\n");
});
it("leaves normal root flags untouched", async () => {
const stdout = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
await runCli(["--version"]);
expect(process.exitCode).toBe(0);
expect(stdout).toHaveBeenCalled();
expect(stderr).not.toHaveBeenCalledWith(expect.stringContaining("unknown worker selector"));
});
});
describe("computer worker entry", () => {
it("is side-effect-free to import and exposes a named start function", async () => {
// Dynamic import intentionally exercises module evaluation without a worker parent port.
const entry = await import("../src/tools/computer/worker-entry");
expect(entry.startComputerWorker).toBeFunction();
});
it("dispatches an immediately posted message through the CLI worker selector", async () => {
const worker = new Worker(new URL("../src/cli.ts", import.meta.url).href, {
type: "module",
argv: ["__omp_worker_computer"],
});
const response = Promise.withResolvers<unknown>();
worker.addEventListener("message", event => response.resolve(event.data));
try {
worker.postMessage({ type: "ping", id: "computer-worker-selector" });
expect(await response.promise).toEqual({ type: "pong", id: "computer-worker-selector" });
} finally {
worker.terminate();
}
});
});