#!/usr/bin/env node

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

const root = __dirname;
const ini = fs.readFileSync(path.join(root, "app.ini.fragment"), "utf8");
const jdCandidateIni = fs.readFileSync(
  path.join(root, "candidate-app.jd.ini"),
  "utf8",
);
const enterpriseCandidateIni = fs.readFileSync(
  path.join(root, "candidate-app.enterprise.ini"),
  "utf8",
);
const installer = fs.readFileSync(
  path.join(root, "install-candidate.sh"),
  "utf8",
);
const relayPreparer = fs.readFileSync(
  path.join(root, "prepare-release-on-sg.sh"),
  "utf8",
);
const policy = JSON.parse(
  fs.readFileSync(path.join(root, "update-policy.json"), "utf8"),
);

assert.match(ini, /\[cron\.update_checker\][\s\S]*ENABLED\s*=\s*false/i);
assert.match(jdCandidateIni, /HoloLake Code Channel · Fifth Domain/);
assert.match(enterpriseCandidateIni, /HoloLake Code Channel · Enterprise/);
for (const candidateIni of [jdCandidateIni, enterpriseCandidateIni]) {
  assert.match(
    candidateIni,
    /\[server\][\s\S]*HTTP_ADDR\s*=\s*127\.0\.0\.1[\s\S]*HTTP_PORT\s*=\s*3340/i,
  );
  assert.match(candidateIni, /RUN_USER\s*=\s*guanghu/i);
  assert.match(
    candidateIni,
    /\[cron\.update_checker\][\s\S]*ENABLED\s*=\s*false/i,
  );
  assert.match(candidateIni, /\[actions\][\s\S]*ENABLED\s*=\s*false/i);
  assert.match(candidateIni, /\[mirror\][\s\S]*ENABLED\s*=\s*false/i);
}
assert.match(
  jdCandidateIni,
  /\/var\/lib\/guanghu\/code-channel\/candidates\/hlcc-v16\.0\.1/,
);
assert.match(
  enterpriseCandidateIni,
  /\/var\/lib\/guanghu-enterprise-code-channel\/candidates\/hlcc-v16\.0\.1/,
);
assert.match(
  installer,
  /HLCC_STATE_ROOT="\/var\/lib\/guanghu-enterprise-code-channel"/,
);
assert.match(installer, /HLCC_SOURCE_ARCHIVE="\$\{HLCC_STATE_ROOT\}\/offline-source\/hlcc-v/);
assert.match(installer, /HLCC_VERSION="16\.0\.1"/);
assert.doesNotMatch(installer, /curl|wget|keyserver/);
assert.match(
  installer,
  /HLCC_RELEASE_KEY="EB114F5E6C0DC2BCDD183550A4B61A2DC5923710"/,
);
assert.match(installer, /gpg[\s\S]*--verify/);
assert.match(installer, /HLCC_SOURCE_ARCHIVE[\s\S]*sha256sum --check MANIFEST\.sha256/);
assert.match(installer, /candidate-app\.ini/);
assert.match(installer, /No service was started/);
assert.match(relayPreparer, /RELEASE_URL="https:\/\/code\.forgejo\.org\//);
assert.match(relayPreparer, /id -un\)" != "ubuntu"/);
assert.match(relayPreparer, /forgejo-upstream-all\.bundle/);
assert.match(relayPreparer, /guanghu-code-channel\.bundle/);
assert.match(relayPreparer, /MANIFEST\.sha256/);
assert.match(
  relayPreparer,
  /No service was started and no transfer to a domestic node was attempted/,
);
assert.equal(policy.product_id, "HLP-MOD-CODE-CHANNEL");
assert.equal(policy.official_upstream.automatic_fetch, false);
assert.equal(policy.official_upstream.automatic_merge, false);
assert.equal(policy.official_upstream.automatic_build, false);
assert.equal(policy.official_upstream.automatic_deploy, false);
assert.equal(policy.official_upstream.push_enabled, false);
assert.equal(policy.forgejo_builtin_update_checker.enabled, false);
assert.equal(
  policy.guanghu_update_channel.manifest_url,
  "https://guanghulab.com/api/code-channel/updates/v1/manifest.json",
);
assert.equal(policy.guanghu_update_channel.require_signature, true);
assert.equal(policy.guanghu_update_channel.require_artifact_sha256, true);
assert.equal(policy.guanghu_update_channel.require_rollback_release, true);
assert.equal(
  policy.guanghu_update_channel.fallback_when_unavailable,
  "MANUAL_RELEASE_ONLY",
);
assert.equal(
  policy.source_baseline.upstream_commit,
  "b3d7e4ac3cbccc220703097a51fa4c16bf302579",
);
assert.deepEqual(policy.offline_distribution.domestic_archive_nodes, [
  "JD-FD-PRIMARY",
  "AW-GZ-001",
]);
assert.deepEqual(policy.offline_distribution.runtime_nodes, [
  "JD-FD-PRIMARY",
  "AW-GZ-001",
]);
assert.equal(policy.offline_distribution.require_manifest_sha256, true);
assert.equal(policy.offline_distribution.require_domestic_gpg_reverification, true);
assert.equal(policy.offline_distribution.shared_runtime_data, false);
assert.equal(policy.offline_distribution.automatic_transfer, false);

console.log("HoloLake Code Channel update boundary: PASS");
