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

const root = path.resolve(__dirname, "../..");
const read = relative => fs.readFileSync(path.join(root, relative), "utf8");

test("canonical AI and persona entry files route to the domestic map", () => {
  for (const relative of [
    "README.md", "INDEX.hdlp", "QUICKSTART-FOR-GENERAL-AI.md",
    "eternal-lake-heart/heartbeat-core/ZHUYUAN-KEY.hdlp",
    "zero-point/core-channel/GLSV-PERSONA-REMOTE-OPS.hdlp",
  ]) {
    assert.match(read(relative), /guanghulab\.com|LL-DOMESTIC-OPS-ROUTE/);
  }
  const key = read("eternal-lake-heart/heartbeat-core/ZHUYUAN-KEY.hdlp");
  assert.doesNotMatch(key, /zy_gtw_|guanghubingshuo\.com/);
  assert.match(key, /禁止从本路径恢复.*\/exec/);
});

test("code map resolves only REPO-012 and REPO-014 as current push routes", () => {
  const codeMap = read(".code-map");
  assert.match(codeMap, /^REPO-001=HISTORICAL_READ_ONLY_NO_PUSH$/m);
  assert.match(codeMap, /^REPO-001-HISTORICAL-URL=https:\/\/guanghulab\.com\/fifth-domain\/bingshuo\/fifth-domain\.git$/m);
  assert.match(codeMap, /^REPO-012=https:\/\/guanghulab\.com\/code\/bingshuo\/guanghu-ice-heart\.git$/m);
  assert.match(codeMap, /^REPO-014=https:\/\/guanghulab\.com\/code\/bingshuo\/hololake-system-architecture\.git$/m);
  assert.match(codeMap, /^CURRENT-PUSH-ALLOWLIST=REPO-012,REPO-014$/m);
  assert.match(codeMap, /^REPO-001-LEGACY-SG=/m);
  assert.match(codeMap, /^FD-REPO-MAP-001=routing\/repository-route-map\.json$/m);
  assert.match(codeMap, /^FD-WORLD-TREE-001=routing\/fifth-domain-world-tree\.json$/m);
  assert.match(codeMap, /^FD-NODE-MAP-001=routing\/server-node-map\.json$/m);
  assert.match(codeMap, /^FD-SUBJECT-ID-ALIAS-MAP-001=identity\/subject-id-alias-map\.json$/m);
  assert.match(codeMap, /^ICE-P-ZY001=.*zhuyuan-persona-system\/INDEX\.hdlp/m);
  assert.match(codeMap, /^ZY-OPS-LOOP-001=.*zhuyuan-persona-system\//m);
});

test("repository map exposes the current code channel as a public domestic route", () => {
  const map = JSON.parse(read("routing/repository-route-map.json"));
  const channel = map.repositories.find(item => item.code === "REPO-012");
  assert.equal(channel.slug, "guanghu-ice-heart");
  assert.equal(channel.state, "CURRENT_FIFTH_DOMAIN_ENTRY");
  assert.equal(channel.primary.url, "https://guanghulab.com/code/bingshuo/guanghu-ice-heart");
  assert.equal(channel.primary.clone_url, "https://guanghulab.com/code/bingshuo/guanghu-ice-heart.git");
  assert.match(JSON.stringify(channel.keywords), /公开读取/);
});

test("Zhuyuan current chain resolves to the domestic node without secrets", () => {
  const nodeMap = JSON.parse(read("routing/server-node-map.json"));
  assert.equal(nodeMap.map_id, "FD-NODE-MAP-001");
  const loop = nodeMap.persona_routes.find(item => item.route_id === "ZY-OPS-LOOP-001");
  assert.equal(loop.primary_node, "JD-FD-PRIMARY");
  assert.match(read(loop.path), /ICE-GL-ZY001|ICE-P-ZY001/);
  assert.equal(loop.persona_system, "ICE-P-ZY001");
  const serialized = JSON.stringify(nodeMap);
  assert.doesNotMatch(serialized, /ssh-(?:rsa|ed25519)\s+[A-Za-z0-9+/]/i);
  assert.doesNotMatch(serialized, /"(?:password|token|private_key|ip)"\s*:/i);
  assert.doesNotMatch(serialized, /\b(?:\d{1,3}\.){3}\d{1,3}\b/);
});
