// acceptance gate for the env-vs-disk validator. Pure-function
// shape (no I/O, no env read, no exit) keeps the test fast and lets the
// caller (platform/ui/server/index.ts) own the side-effects (console.error +
// process.exit). The four cases cover every observable boot state — missing,
// no-on-disk-account, mismatch, ok — with no fallback path.

import test from "node:test";
import assert from "node:assert/strict";
import { validateAccountIdEnv } from "../index.js";

const UUID_A = "11111111-2222-3333-4444-555555555555";
const UUID_B = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";

test("validateAccountIdEnv ok when env matches a disk account", () => {
  const result = validateAccountIdEnv(UUID_A, [UUID_A]);
  assert.deepEqual(result, { ok: true, envId: UUID_A, diskIds: [UUID_A] });
});

test("validateAccountIdEnv FATAL reason=missing when env is undefined", () => {
  const result = validateAccountIdEnv(undefined, [UUID_A]);
  assert.equal(result.ok, false);
  assert.equal(result.ok ? null : result.reason, "missing");
  assert.equal(result.envId, null);
});

test("validateAccountIdEnv FATAL reason=missing when env is the empty string", () => {
  // Empty string is the systemd shape when `Environment=ACCOUNT_ID=` lands
  // without a value (e.g. an installer regression that interpolates an empty
  // template variable). Same FATAL classification as undefined — we never
  // accept a falsy env value and silently degrade.
  const result = validateAccountIdEnv("", [UUID_A]);
  assert.equal(result.ok, false);
  assert.equal(result.ok ? null : result.reason, "missing");
});

test("validateAccountIdEnv FATAL reason=no-on-disk-account when disk is empty", () => {
  const result = validateAccountIdEnv(UUID_A, []);
  assert.equal(result.ok, false);
  assert.equal(result.ok ? null : result.reason, "no-on-disk-account");
  assert.equal(result.envId, UUID_A);
  assert.deepEqual(result.diskIds, []);
});

test("validateAccountIdEnv FATAL reason=mismatch when env not in disk list", () => {
  const result = validateAccountIdEnv(UUID_A, [UUID_B]);
  assert.equal(result.ok, false);
  assert.equal(result.ok ? null : result.reason, "mismatch");
  assert.equal(result.envId, UUID_A);
  assert.deepEqual(result.diskIds, [UUID_B]);
});

test("validateAccountIdEnv ok when env matches one of multiple disk accounts", () => {
  // Phase 0 invariant is single-account, but the validator is shape-agnostic;
  // future multi-account expansion should not require a code change here.
  const result = validateAccountIdEnv(UUID_B, [UUID_A, UUID_B]);
  assert.equal(result.ok, true);
});
