// acceptance gate for ACCOUNT_ID stamping in the brand systemd unit.
//
// Two invariants this test protects:
//   (a) buildMaxyUnitFile emits a literal `Environment=ACCOUNT_ID=<uuid>` line
//       in the [Service] block when accountId is provided. Pi recovery via
//       `npx -y @rubytech/create-maxy@latest` depends on this line landing —
//       without it, process.env.ACCOUNT_ID is undefined and the
//       writeNodeWithEdges gate rejects every CF-setup graph write at
//       cloudflare-task-tracker.ts:187/326/347/559.
//   (b) buildMaxyUnitFile throws on an empty accountId. Falling through with
//       "" would emit a unit with no ACCOUNT_ID line; the boot validator at
//       platform/ui/server/index.ts would then FATAL reason=missing on every
//       restart, surfacing the same regression class behind a different log
//       prefix. Throwing at build time aborts the install loud, before the
//       broken unit is written.
import test from "node:test";
import assert from "node:assert/strict";
import { buildMaxyUnitFile } from "../port-resolution.js";
const VALID_UUID = "12345678-9abc-def0-1234-56789abcdef0";
function callBuildUnit(accountId) {
    return buildMaxyUnitFile({
        productName: "Maxy",
        brandHostname: "maxy",
        neo4jDedicated: false,
        installDir: "/home/me/maxy",
        persistDir: "/home/me/.maxy",
        port: 19200,
        maxyUiInternalPort: 19201,
        vncDisplay: 99,
        rfbPort: 5900,
        websockifyPort: 6080,
        cdpPort: 9222,
        claudeSessionManagerPort: 19400,
        chromiumBin: "/usr/bin/chromium",
        accountId,
    });
}
test("buildMaxyUnitFile emits Environment=ACCOUNT_ID=<uuid> for a valid accountId", () => {
    const unit = callBuildUnit(VALID_UUID);
    assert.match(unit, /^Environment=ACCOUNT_ID=12345678-9abc-def0-1234-56789abcdef0$/m, "ACCOUNT_ID line missing or malformed");
    // Placement matters: identity (NODE_ENV → ACCOUNT_ID → PORT) so the most
    // fundamental brand identity sits next to the env that selects the brand
    // runtime. Anchored regex against the three adjacent lines.
    assert.match(unit, /Environment=NODE_ENV=production\nEnvironment=ACCOUNT_ID=12345678-9abc-def0-1234-56789abcdef0\nEnvironment=PORT=19200/, "ACCOUNT_ID line is not placed between NODE_ENV and PORT");
});
test("buildMaxyUnitFile throws when accountId is empty", () => {
    assert.throws(() => callBuildUnit(""), /accountId is required/, "expected throw on empty accountId, did not throw");
});
