"use strict";

const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const test = require("node:test");
const { recall } = require("./server");

const map = JSON.parse(fs.readFileSync(path.resolve(__dirname, "../../routing/persona-contribution-map.json"), "utf8"));

test("recalls contribution paths without granting authority", () => {
  const matches = recall(map, "六节点灾备");
  assert.equal(matches[0].contribution_id, "ZY-CONTRIB-20260720-001");
  assert.equal(matches[0].grants_execution_authority, false);
});

test("does not guess unknown routes", () => assert.deepEqual(recall(map, "火星工程不存在"), []));

test("recalls the HoloLake 0.2.0 engineering evidence map", () => {
  const matches = recall(map, "HoloLake 0.2.0 工具回执 Windows安装包");
  assert.equal(matches[0].contribution_id, "ZY-CONTRIB-20260722-001");
  assert.equal(matches[0].grants_execution_authority, false);
  assert.ok(matches[0].paths.some(item => item.includes("ZY-BIDIRECTIONAL-COGNITION-007")));
});

test("HTTP surface is GET-only and read-only", async () => {
  const server = require("./server").createServer();
  await new Promise(resolve => server.listen(0, "127.0.0.1", resolve));
  const address = server.address();
  const health = await fetch(`http://127.0.0.1:${address.port}/health`).then(response => response.json());
  assert.equal(health.mode, "read-only");
  const response = await fetch(`http://127.0.0.1:${address.port}/v1/recall?q=${encodeURIComponent("TestFlight Windows")}`).then(item => item.json());
  assert.equal(response.matches[0].contribution_id, "ZY-CONTRIB-20260719-001");
  assert.equal(response.grants_execution_authority, false);
  await new Promise(resolve => server.close(resolve));
});
