import assert from "node:assert/strict";
import test from "node:test";
import { formatFailure, startCraftos, startFakeOpencode, startServer } from "./harness.js";

// Full-boot e2e: a real CraftOS computer runs the real daemons/cloud.lua daemon
// (driven by cloud.* settings) and the real `cloud health` program against the
// real gateway, with a healthy fake-opencode behind it.
test("full boot: settings -> daemon -> cloud health prints serverOk/opencodeOk true", async () => {
  const opencode = await startFakeOpencode({ status: 200 });
  const server = await startServer({ serverPassword: "boot-secret", opencodeUrl: opencode.url });
  const craftos = startCraftos("full-boot.lua", {
    computerId: 11,
    computerLabel: "boot",
    shellArgs: [server.baseUrl, "boot-secret"],
    timeoutMs: 20_000,
  });
  try {
    const result = await craftos.done;
    assert.match(result.output, /serverOk:\s*true/, formatFailure("expected serverOk: true", result.output));
    assert.match(result.output, /opencodeOk:\s*true/, formatFailure("expected opencodeOk: true", result.output));
  } finally {
    craftos.abort();
    await craftos.done.catch(() => undefined);
    await server.close();
    await opencode.close();
  }
});
