import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import test from "node:test";

const siteRoot = new URL("../../", import.meta.url);
const npmCommand =
  process.platform === "win32" ? (process.env.ComSpec ?? "cmd.exe") : "npm";
const npmArguments =
  process.platform === "win32"
    ? ["/d", "/s", "/c", "npm run build"]
    : ["run", "build"];

test("the production worker builds on the current platform", () => {
  const result = spawnSync(npmCommand, npmArguments, {
    cwd: siteRoot,
    encoding: "utf8",
    env: {
      ...process.env,
      NO_COLOR: "1",
    },
  });

  const output = `${result.stdout ?? ""}\n${result.stderr ?? ""}\n${result.error ?? ""}`;
  assert.equal(
    result.status,
    0,
    `Expected a successful cross-platform production build.\n${output}`,
  );
});
