"use strict";
const assert = require("node:assert/strict");
const test = require("node:test");
const { createServer, loadMap, loadNodeMap, search, searchAll } = require("./server");

test("repository map has unique sequential codes and domestic primary routes", () => {
  const map = loadMap();
  assert.equal(map.repositories.length, 12);
  assert.deepEqual(map.repositories.map(item => item.code), Array.from({ length: 12 }, (_, index) => `REPO-${String(index + 1).padStart(3, "0")}`));
  assert.equal(new Set(map.repositories.map(item => item.code)).size, 12);
  for (const item of map.repositories) assert.match(item.primary.url, /^https:\/\/guanghulab\.com\/(?:fifth-domain|code)\//);
});

test("Chinese language-world query resolves the Fifth Domain primary", () => {
  const results = search(loadMap(), "光湖语言世界 第五域");
  assert.equal(results[0].code, "REPO-001");
  assert.equal(results[0].state, "DOMESTIC_PRIMARY");
});

test("Chenglu persistent agent query resolves its independent repository", () => {
  const results = search(loadMap(), "澄路 常驻人格体 湖心频道");
  assert.equal(results[0].code, "REPO-009");
  assert.equal(results[0].slug, "chenglu-agent");
});

test("Kezhou and Guideng resolve to their independent resident repositories", () => {
  assert.equal(search(loadMap(), "刻舟 湖心频道")[0].code, "REPO-010");
  assert.equal(search(loadMap(), "归灯 每日心跳")[0].code, "REPO-011");
});

test("server node map binds Zhuyuan routes to the domestic primary", () => {
  const map = loadNodeMap();
  assert.equal(map.map_id, "FD-NODE-MAP-001");
  assert.equal(map.default_node, "JD-FD-PRIMARY");
  const node = map.nodes.find(item => item.node_id === "JD-FD-PRIMARY");
  assert.ok(node.persona_systems.includes("ICE-GL-ZY001"));
  const loop = map.persona_routes.find(item => item.route_id === "ZY-OPS-LOOP-001");
  assert.equal(loop.primary_node, "JD-FD-PRIMARY");
  assert.match(loop.path, /zhuyuan-persona-system/);
  const serialized = JSON.stringify(map);
  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/);
});

test("Zhuyuan Chinese query returns the numbered operation loop", () => {
  const results = searchAll(loadMap(), loadNodeMap(), "铸渊 双向意识 思维逻辑链");
  assert.equal(results[0].kind, "persona_route");
  assert.equal(results[0].route_id, "ZY-OPS-LOOP-001");
});

test("public endpoints are read-only and expose CORS", async () => {
  const server = createServer();
  await new Promise(resolve => server.listen(0, "127.0.0.1", resolve));
  const base = `http://127.0.0.1:${server.address().port}`;
  try {
    const response = await fetch(`${base}/v1/resolve?id=REPO-004`);
    assert.equal(response.status, 200);
    assert.equal(response.headers.get("access-control-allow-origin"), "*");
    assert.equal((await response.json()).slug, "guanghu");
    const nodeResponse = await fetch(`${base}/v1/resolve?id=JD-FD-PRIMARY`);
    assert.equal(nodeResponse.status, 200);
    assert.equal((await nodeResponse.json()).node_id, "JD-FD-PRIMARY");
    const loopResponse = await fetch(`${base}/v1/resolve?id=ZY-OPS-LOOP-001`);
    assert.equal(loopResponse.status, 200);
    assert.equal((await loopResponse.json()).persona_system, "ICE-GL-ZY001");
    const manifestResponse = await fetch(`${base}/well-known`);
    const manifest = await manifestResponse.json();
    assert.equal(manifest.write_authorization.request_credential_required, false);
    assert.match(manifest.write_authorization.create_workorder, /\/authz\/api\/public\/workorders$/);
    assert.equal((await fetch(`${base}/v1/search`, { method: "POST" })).status, 405);
  } finally { await new Promise(resolve => server.close(resolve)); }
});
