#!/usr/bin/env node

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

const root = __dirname;
const bootstrap = fs.readFileSync(path.join(root, "hlcc-bootstrap.py"), "utf8");
const ini = fs.readFileSync(path.join(root, "app.ini"), "utf8");
const unit = fs.readFileSync(path.join(root, "hlcc-jd-candidate.service"), "utf8");
const manifest = JSON.parse(
  fs.readFileSync(
    path.join(
      root,
      "../../../deployment/requests/HLCC-JD-CANDIDATE-INITIAL-PROVISION-20260723.json",
    ),
    "utf8",
  ),
);
const offlineReceipt = JSON.parse(
  fs.readFileSync(
    path.join(
      root,
      "../../../deployment/receipts/HLCC-BS-SG-003-OFFLINE-PACK-20260723.json",
    ),
    "utf8",
  ),
);

assert.match(bootstrap, /VERSION = "16\.0\.1"/);
assert.match(bootstrap, /EB114F5E6C0DC2BCDD183550A4B61A2DC5923710/);
assert.match(bootstrap, /MANIFEST\.sha256/);
assert.match(bootstrap, /gpg"[\s\S]*"--verify"/);
assert.match(bootstrap, /127\.0\.0\.1", 3341/);
assert.match(bootstrap, /127\.0\.0\.1:3340\/api\/healthz/);
assert.match(bootstrap, /payload\.get\("status"\) == "pass"/);
assert.doesNotMatch(bootstrap, /127\.0\.0\.1:3340\/api\/v1\/version/);
assert.match(bootstrap, /forgejo-upstream-all\.bundle/);
assert.match(bootstrap, /guanghu-code-channel\.bundle/);
assert.match(bootstrap, /package_profile": "full-offline-v16\.0\.1"/);
assert.match(bootstrap, /OWNER_NAME = "bingshuo"/);
assert.match(bootstrap, /SEED_COMMIT_NUMBER = "HLCC-ICE-000001"/);
assert.match(bootstrap, /SEED_CONTRIBUTION_NUMBER = "ZY-CONTRIB-20260723-001"/);
assert.match(bootstrap, /以来光者贡献链启用冰朔第五域个人子频道/);
assert.match(bootstrap, /owner-identity-source\.db/);
assert.match(bootstrap, /password_hash_preserved/);
assert.match(bootstrap, /access_tokens_migrated": False/);
assert.match(bootstrap, /repositories_migrated": False/);
assert.match(bootstrap, /delete from access_token/);
assert.doesNotMatch(
  bootstrap,
  /print\s*\([^)]*token|stderr\.write\s*\([^)]*token|stdout\.write\s*\([^)]*token/,
);
for (const artifact of offlineReceipt.artifacts) {
  assert.match(artifact.sha256, /^[0-9a-f]{64}$/);
  assert.match(
    bootstrap,
    new RegExp(`: "${artifact.sha256}"`),
  );
}
assert.match(offlineReceipt.release.manifest_sha256, /^[0-9a-f]{64}$/);
assert.match(
  bootstrap,
  new RegExp(`"MANIFEST\\.sha256": "${offlineReceipt.release.manifest_sha256}"`),
);
assert.match(ini, /APP_NAME = 光湖代码频道/);
assert.match(ini, /ROOT_URL = https:\/\/guanghulab\.com\/code\//);
assert.match(ini, /REQUIRE_SIGNIN_VIEW = false/);
assert.match(ini, /SHOW_FOOTER_BRANDING = false/);
assert.match(ini, /SHOW_FOOTER_VERSION = false/);
assert.match(ini, /\[cron\.update_checker\][\s\S]*ENABLED = false/);
assert.match(unit, /^User=guanghu$/m);
assert.match(unit, /^ProtectSystem=strict$/m);
assert.match(unit, /^ReadOnlyPaths=__RELEASE_ROOT__$/m);
assert.match(
  unit,
  /^ReadWritePaths=\/var\/lib\/guanghu\/personas\/guanghu\/hlcc-v16\.0\.1$/m,
);
assert.equal(manifest.target_node, "JD-FD-PRIMARY");
assert.equal(manifest.runtime_check.url, "http://127.0.0.1:3341/health");
assert.equal(manifest.module.unit, "hlcc-jd-candidate.service");
assert.equal(manifest.module.run_user, "guanghu");

console.log("HoloLake Code Channel JD candidate package: PASS");
