import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";

const routes = JSON.parse(await readFile(new URL("../data/routes.json", import.meta.url), "utf8"));

test("ships the exact 27-page ecosystem", () => {
  assert.equal(routes.pages.length, 27);
  assert.equal(routes.pages.filter((page) => page.category === "commercial").length, 12);
  assert.equal(routes.pages.filter((page) => page.category === "journey").length, 10);
  assert.equal(routes.pages.filter((page) => page.category === "trust").length, 5);
  assert.equal(new Set(routes.pages.map((page) => page.route)).size, 27);
});

test("ships exactly ten controlled A/B variants", () => {
  assert.equal(routes.ab_variants.length, 10);
  const groups = Object.groupBy(routes.ab_variants, (item) => item.route);
  assert.equal(Object.keys(groups).length, 5);
  for (const variants of Object.values(groups)) {
    assert.deepEqual(variants.map((item) => item.variant).sort(), ["A", "B"]);
    assert.ok(variants.every((item) => item.hypothesis && item.primary_metric));
  }
});

test("keeps commerce and Event schema behind verified gates", () => {
  assert.equal(routes.ticket_model.publication_state, "gated");
  assert.match(routes.implementation_notes.checkout_gate, /Só ativar/);
  assert.match(routes.implementation_notes.purchase_rule, /webhook/);
  assert.match(routes.implementation_notes.event_schema_gate, /Só emitir Event/);
});

test("contains twelve non-clinical diagnosis questions", () => {
  assert.equal(routes.diagnosis.questions.length, 12);
  assert.match(routes.diagnosis.disclaimer, /Não avalia saúde mental/);
  assert.equal(new Set(routes.diagnosis.questions.map((question) => question.id)).size, 12);
});
