import { spawn } from "child_process";

const commands = [
  {
    label: "smoke",
    cmd: "node",
    args: ["product/site/smoke-test.cjs"],
    env: {},
  },
  {
    label: "e2e-desktop",
    cmd: "node",
    args: ["product/site/e2e-test.mjs"],
    env: {
      SQUAREMCP_E2E_PROFILE: "desktop",
      SQUAREMCP_E2E_SCREENSHOT: "/tmp/squaremcp-e2e-desktop.png",
    },
  },
  {
    label: "e2e-mobile",
    cmd: "node",
    args: ["product/site/e2e-test.mjs"],
    env: {
      SQUAREMCP_E2E_PROFILE: "mobile",
      SQUAREMCP_E2E_SCREENSHOT: "/tmp/squaremcp-e2e-mobile.png",
    },
  },
];

function runCommand({ label, cmd, args, env }) {
  return new Promise((resolve, reject) => {
    const child = spawn(cmd, args, {
      stdio: "inherit",
      env: { ...process.env, ...env },
    });

    child.on("exit", (code) => {
      if (code === 0) {
        resolve();
        return;
      }
      reject(new Error(`${label} failed with exit code ${code}`));
    });
    child.on("error", reject);
  });
}

async function main() {
  for (const command of commands) {
    console.log(`\n==> ${command.label}`);
    await runCommand(command);
  }

  console.log("\nsquaremcp product site verification: PASS");
  console.log("desktop_screenshot: /tmp/squaremcp-e2e-desktop.png");
  console.log("mobile_screenshot: /tmp/squaremcp-e2e-mobile.png");
}

main().catch((error) => {
  console.error(`squaremcp product site verification: FAIL: ${error.message}`);
  process.exit(1);
});
