const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");

const root = path.resolve(__dirname, "..");
const map = JSON.parse(
  fs.readFileSync(path.join(__dirname, "hololake-language-persona-os-map.json"), "utf8"),
);
const repositories = JSON.parse(
  fs.readFileSync(path.join(__dirname, "repository-route-map.json"), "utf8"),
);
const worldTree = JSON.parse(
  fs.readFileSync(path.join(__dirname, "fifth-domain-world-tree.json"), "utf8"),
);

assert.equal(map.map_id, "HLP-LPOS-MAP-001");
assert.equal(map.fifth_domain_entry.repository_id, repositories.default_repository);
assert.deepEqual(map.concept_ancestry, [
  "GLS-0810",
  "GLS-0233",
  "GLS-0235",
  "GLS-0236",
  "GLS-0245",
]);
assert.equal(map.canonical_product_source.repository_id, "REPO-014");
assert.match(map.canonical_product_source.source_baseline, /^[0-9a-f]{40}$/);
assert.equal(
  map.canonical_product_source.source_baseline,
  "1cb3950e67c2f3feab65b8932c5cabcc2cafc33a",
);
assert.deepEqual(map.native_application_lifecycle, [
  "resolve",
  "authorize",
  "fetch",
  "verify",
  "assemble",
  "mount",
  "run",
  "checkpoint",
  "receipt",
  "unmount_or_pin",
]);

const repo8 = repositories.historical_repositories.find((item) => item.code === "REPO-008");
assert.ok(repo8, "REPO-008 must remain registered");
assert.equal(repo8.state, "HISTORICAL_READ_ONLY_NO_PUSH_ROUTE_CLOSED");
assert.equal(repo8.replaced_by, "REPO-014");
assert.equal(
  repo8.product_architecture_map.path,
  "routing/hololake-language-persona-os-map.json",
);
const repo14 = repositories.repositories.find((item) => item.code === "REPO-014");
assert.ok(repo14, "REPO-014 must remain registered");
assert.equal(repo14.state, "PUBLIC_SOURCE_AND_ARCHITECTURE_PRIMARY");

const productNode = worldTree.nodes.find(
  (item) => item.object_id === "GH-AIOS-LIGHTHOUSE-020",
);
assert.ok(productNode, "HoloLake product node must exist in the Fifth Domain world tree");
assert.equal(productNode.repository_id, "REPO-014");
assert.equal(productNode.repository_path, "product-source/hololake-platform");

const fifthDomainRoute = worldTree.edges.find(
  (edge) =>
    edge.from === "FIFTH_DOMAIN" &&
    edge.relation === "routes_to_product_architecture" &&
    edge.to === "GH-AIOS-LIGHTHOUSE-020",
);
assert.ok(fifthDomainRoute, "Fifth Domain must route to the HoloLake product architecture");

assert.ok(
  fs.existsSync(
    path.join(root, "gls", "GLS-0245-HOLOLAKE-LANGUAGE-PERSONA-OPERATING-SYSTEM-PRODUCT-MAPPING.hdlp"),
  ),
);

console.log("hololake-language-persona-os-map: ok");
