import assert from "node:assert/strict";
import test from "node:test";

import {
  readDevM3CardinalityInputs,
  validateDevM3Cardinality
} from "./validate-dev-m3-cardinality.mjs";

const baseline = await readDevM3CardinalityInputs();

function clone(value) {
  return JSON.parse(JSON.stringify(value));
}

function cloneInputs() {
  return clone(baseline);
}

function assertRejected(inputs, pattern) {
  assert.throws(() => validateDevM3Cardinality(inputs), pattern);
}

test("accepts the checked-in DEV deploy service mappings", () => {
  validateDevM3Cardinality(cloneInputs());
});

test("rejects duplicate deploy service IDs", () => {
  const inputs = cloneInputs();
  inputs.deploy.services.push({ ...inputs.deploy.services[0] });

  assertRejected(inputs, /serviceId must be unique/u);
});

test("rejects k3s mappings for undeclared services", () => {
  const inputs = cloneInputs();
  inputs.deploy.k3s.serviceMappings[0].serviceId = "hwlab-missing";

  assertRejected(inputs, /references a declared service/u);
});

test("rejects missing v02 boot script declarations", () => {
  const inputs = cloneInputs();
  const serviceId = inputs.deploy.lanes.v02.envReuseServices[0];
  delete inputs.deploy.lanes.v02.bootScripts[serviceId];

  assertRejected(inputs, new RegExp(`bootScripts\\.${serviceId} is required`, "u"));
});
