"use strict";

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

const html = fs.readFileSync(path.join(__dirname, "..", "index.html"), "utf8");
const styles = fs.readFileSync(path.join(__dirname, "..", "styles.css"), "utf8");
const robots = fs.readFileSync(path.join(__dirname, "..", "robots.txt"), "utf8");
const llms = fs.readFileSync(path.join(__dirname, "..", "llms.txt"), "utf8");

test("front door keeps the legally required ICP link", () => {
  assert.match(html, /陕ICP备2025071211号-1/);
  assert.match(html, /https:\/\/beian\.miit\.gov\.cn\//);
});

test("front door routes primary actions to the domestic repository", () => {
  assert.match(html, /href="\/fifth-domain\/bingshuo\/fifth-domain"/);
  assert.match(html, /国内第五域主节点/);
  assert.doesNotMatch(html, /href="\/jd\/"/);
});

test("front door separates the domestic primary repository from historical repositories", () => {
  assert.match(html, /href="\/fifth-domain\/bingshuo\/fifth-domain"/);
  assert.match(html, /第五域国内主代码仓库/);
  assert.match(html, /历史仓库/);
  assert.match(html, /href="https:\/\/guanghubingshuo\.com\/code\/"/);
});

test("AI and search cards stay on the domestic domain", () => {
  assert.match(html, /href="\/api\/ai\/"/);
  assert.match(html, /href="\/api\/ai\/v1\/search\?q=/);
  assert.doesNotMatch(html, /href="https:\/\/guanghubingshuo\.com\/global-search\//);
});

test("public site advertises machine-readable AI discovery", () => {
  assert.match(html, /SearchAction/);
  assert.match(html, /\.well-known\/guanghu\.json/);
  assert.match(robots, /Allow: \/api\/ai\//);
  assert.match(robots, /Disallow: \/authz\//);
  assert.match(llms, /REPO-001/);
  assert.match(llms, /api\/ai\/v1\/repositories/);
});

test("public copy uses the domestic Fifth Domain name instead of the cloud vendor", () => {
  assert.doesNotMatch(html, /京东云/);
  assert.match(html, /国内第五域主节点/);
});

test("public page never embeds infrastructure addresses or credentials", () => {
  assert.doesNotMatch(html, /(?:\d{1,3}\.){3}\d{1,3}/);
  assert.doesNotMatch(html, /zy_gtw_/);
  assert.doesNotMatch(html, /password|private[_ -]?key|token/i);
});

test("front door has responsive card layout", () => {
  assert.match(styles, /grid-template-columns:\s*repeat\(2/);
  assert.match(styles, /@media\s*\(max-width:\s*760px\)/);
});
