"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const root = path.resolve(__dirname, "..");

test("six independent recovery nodes are declared", () => {
  const policy = JSON.parse(fs.readFileSync(path.join(root, "recovery-policy.json")));
  assert.equal(policy.recovery_nodes.length, 6);
  assert.equal(new Set(policy.recovery_nodes).size, 6);
  assert.ok(policy.forbidden.includes("shared-recovery-private-key"));
  assert.ok(policy.forbidden.includes("private-key-on-personal-computer"));
  assert.ok(policy.forbidden.includes("reverse-access-from-enterprise"));
});

test("forced command rejects arbitrary shell", () => {
  const entry = fs.readFileSync(path.join(root, "jd-recovery-entry.sh"), "utf8");
  assert.match(entry, /SSH_ORIGINAL_COMMAND/);
  assert.match(entry, /recovery action denied/);
  assert.doesNotMatch(entry, /eval /);
});

test("recovery runner only restores fixed local backup paths", () => {
  const runner = fs.readFileSync(path.join(root, "jd-recovery-runner"), "utf8");
  assert.match(runner, /sha256sum -c/);
  assert.match(runner, /restore-authz/);
  assert.match(runner, /install-owner-access/);
  assert.match(runner, /restore-navigation-map/);
  assert.doesNotMatch(runner, /eval /);
  assert.doesNotMatch(runner, /\$2/);
});

test("six-node bootstrap uses unique keys and forced command restrictions", () => {
  const bootstrap = fs.readFileSync(path.join(root, "bootstrap-six-node-recovery.sh"), "utf8");
  assert.match(bootstrap, /restrict,command=/);
  assert.match(bootstrap, /source_ip/);
  assert.match(bootstrap, /test -f \/etc\/guanghu\/secrets\/jd-recovery\/to-jd-ed25519 \|\| ssh-keygen/);
  assert.doesNotMatch(bootstrap, /StrictHostKeyChecking no/);
});
