import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import test from "node:test";

const root = resolve(import.meta.dirname, "..");
const read = (relativePath) => readFileSync(resolve(root, relativePath), "utf8");
const readiness = JSON.parse(read("MAINNET_READINESS.json"));
const release = JSON.parse(read("V3_SECURITY_RELEASE.json"));

test("mainnet and partner deployment remain fail-closed", () => {
  assert.equal(readiness.canonicalNetwork, "solana:mainnet");
  assert.equal(readiness.assessmentScope, "source-only");
  assert.equal(readiness.programDispositionManifest, "v3-program-dispositions.json");
  assert.equal(readiness.supportedCandidateCount, 6);
  assert.equal(readiness.executableEvidenceAvailable, false);
  assert.equal(readiness.asOfCommit, null);
  assert.equal(readiness.asOfCommitReason, "unavailable-until-clean-committed-candidate");
  assert.equal(readiness.v3Program.status, "not_deployed");
  assert.deepEqual(readiness.v3Program.mainnetProgramIds, []);
  assert.equal(readiness.optionalPartnerDeployedAuditedProgram.status, "not_ready");
  assert.equal(readiness.optionalPartnerDeployedAuditedProgram.partnerApproval, false);
  assert.equal(readiness.optionalPartnerDeployedAuditedProgram.independentAudit, false);
  assert.equal(readiness.optionalPartnerDeployedAuditedProgram.programId, null);
  assert.equal(readiness.optionalPartnerDeployedAuditedProgram.deploymentSignature, null);

  assert.equal(release.status, "blocked");
  assert.equal(release.programDispositionManifest, "v3-program-dispositions.json");
  assert.equal(release.programDispositionEvidenceScope, "source-only");
  assert.equal(release.supportedCandidateCount, 6);
  assert.equal(release.redesignRequiredCount, 4);
  assert.equal(release.retiredCount, 5);
  assert.equal(release.executableEvidenceAvailable, false);
  assert.deepEqual(release.mainnetProgramIds, []);
  assert.deepEqual(release.deploymentEvidence, []);
  assert.equal(release.independentHumanAuditComplete, false);
  assert.equal(release.partnerDeployment.approved, false);
  assert.equal(release.partnerDeployment.upgradeAuthorityCustody, null);
});

test("no source configuration presents a mainnet deployment path", () => {
  const anchorToml = read("Anchor.toml");
  const packageJson = JSON.parse(read("package.json"));
  const deployWrapper = read("scripts/deploy-blocked.mjs");

  assert.doesNotMatch(anchorToml, /^\[programs\.mainnet\]/m);
  assert.doesNotMatch(anchorToml, /^\[programs\.devnet\]/m);
  assert.doesNotMatch(anchorToml, /^\[programs\.localnet\]/m);
  assert.equal(packageJson.scripts.deploy, "node scripts/deploy-blocked.mjs");
  assert.match(deployWrapper, /process\.exitCode = 1/);
  assert.doesNotMatch(deployWrapper, /(?:anchor|solana)\s+deploy/);
});

test("local rehearsal is explicitly non-release and local-RPC-only", () => {
  const versions = read("rehearsal-versions.env");
  const localScript = read("scripts/local-validator-rehearsal.sh");
  const verifier = read("scripts/verify-local-validator.mjs");
  const artifactVerifier = read("scripts/verify-artifacts.mjs");

  assert.match(versions, /^SOLANA_VERSION=2\.3\.0$/m);
  assert.match(versions, /^ANCHOR_VERSION=0\.32\.1$/m);
  assert.match(versions, /^RUST_VERSION=1\.89\.0$/m);
  assert.match(versions, /^LOCAL_RPC_URL=http:\/\/127\.0\.0\.1:8899$/m);
  assert.match(localScript, /Refusing non-local RPC URL/);
  assert.match(localScript, /--bind-address 127\.0\.0\.1/);
  assert.match(localScript, /--bpf-program/);
  assert.match(verifier, /BPFLoaderUpgradeab1e11111111111111111111111/);
  assert.match(verifier, /agave-test-validator-disabled-upgrade-sentinel/);
  assert.match(verifier, /has a usable or unexpected upgrade authority/);
  assert.match(verifier, /ProgramData must start with ELF magic/);
  assert.match(artifactVerifier, /deployableRelease: false/);
  assert.match(artifactVerifier, /createHash\("sha256"\)/);
});

test("historical success suites are excluded from active commands", () => {
  const packageJson = read("package.json");
  const archiveNotice = read("historical/tests/README.md");

  assert.doesNotMatch(packageJson, /e2e_functional|vaultfire\.ts|x402-solana-regression/);
  assert.match(archiveNotice, /excluded from npm, CI, container, and release-package/);
  assert.match(archiveNotice, /pre-remediation evidence tombstones/);
});

test("historical transcript is unmistakably superseded", () => {
  const historicalRecord = read("TEST_RESULTS.md");
  assert.match(historicalRecord, /Evidence Tombstone/);
  assert.match(historicalRecord, /removed from the active release surface/);
  assert.doesNotMatch(historicalRecord, /ALL 15 PROGRAMS FULLY FUNCTIONAL|program is deployed/);
});
