import { resolve } from "node:path";
import { Worker } from "node:worker_threads";

const worker = new Worker(resolve("apps/worker/dist/worker.js"));
const timeout = setTimeout(() => {
  void worker.terminate();
  throw new Error("Worker probe timed out.");
}, 5_000);

const reply = await new Promise((resolveReply, reject) => {
  worker.once("error", reject);
  worker.once("message", resolveReply);
  worker.postMessage("ping");
});

clearTimeout(timeout);
await worker.terminate();

if (reply !== "pong") {
  throw new Error(`Unexpected Worker probe reply: ${String(reply)}`);
}

console.log(JSON.stringify({ reply, status: "passed" }));
