import test from "node:test";
import assert from "node:assert/strict";

import {
  DEFAULT_STAGING_BASE_URL,
  EXPECTED_INSTALL_VERSION,
  INSTALLER_SCRIPT_REQUIRED_SNIPPETS,
  buildChecks,
  normalizeBaseUrl,
  parseArgs,
  validateArtifactBodyAgainstManifest,
  validateArtifactManifestBody,
  validateInstallerScriptBody,
} from "./staging-edge-gateway-smoke.mjs";

test("normalizeBaseUrl strips trailing slashes", () => {
  assert.equal(normalizeBaseUrl("https://api.truckwash.io:4433///"), DEFAULT_STAGING_BASE_URL);
});

test("parseArgs accepts base URL and install token", () => {
  const options = parseArgs([
    "--base-url",
    "https://staging.example.test/",
    "--install-token",
    "token-123",
  ]);

  assert.equal(options.baseUrl, "https://staging.example.test/");
  assert.equal(options.installToken, "token-123");
  assert.equal(options.help, false);
});

test("buildChecks targets the public staging endpoints", () => {
  const checks = buildChecks(DEFAULT_STAGING_BASE_URL, "abc 123");

  assert.deepEqual(checks.map((check) => check.url), [
    "https://api.truckwash.io:4433/ping",
    "https://api.truckwash.io:4433/edge-agent/artifacts/manifest.json",
    "https://api.truckwash.io:4433/edge-agent/artifacts/agent.php",
    "https://api.truckwash.io:4433/edge-agent/artifacts/truckwash-edge-agent.service",
    "https://api.truckwash.io:4433/edge-agent/install.sh?token=abc%20123",
  ]);
});

test("validateArtifactManifestBody requires v3 install artifacts", () => {
  const artifacts = [
    "agent.php",
    "lan-worker.php",
    "auto-updater.php",
    "docker-compose.gateway.yml",
    "Dockerfile.edge-agent",
    "Dockerfile.lan-worker",
    "Dockerfile.auto-updater",
    "gateway-launcher.sh",
    "truckwash-edge-gateway-stack.service",
    "truckwash-edge-agent.service",
  ].map((name) => ({ name, sha256: "abc", bytes: 1 }));

  const manifest = validateArtifactManifestBody(JSON.stringify({
    version: EXPECTED_INSTALL_VERSION,
    artifacts,
  }));

  assert.equal(manifest.version, EXPECTED_INSTALL_VERSION);
});

test("validateArtifactBodyAgainstManifest verifies size and hash", () => {
  const body = Buffer.from("hello");
  const manifest = {
    artifacts: [{
      name: "agent.php",
      sha256: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
      bytes: body.length,
    }],
  };

  assert.equal(validateArtifactBodyAgainstManifest(manifest, "agent.php", body).name, "agent.php");
});

test("validateInstallerScriptBody requires install-session reporting wiring", () => {
  const script = `
    INSTALL_STATUS_URL="https://api.truckwash.io:4433/edge-agent/install-token/status"
    fetch_http "Download artifact manifest" "https://api.truckwash.io:4433/edge-agent/artifacts/manifest.json"
    report_install_status "FAILED"
    begin_install_phase "VERIFY_TOKEN" "Verifying install token"
    begin_install_phase "VERIFY_ARTIFACTS" "Verifying edge gateway artifacts"
    begin_install_phase "WAIT_FOR_CLAIM" "Waiting for gateway heartbeat and claim"
    verify_manifest_artifact
    ${EXPECTED_INSTALL_VERSION}
  `;

  assert.deepEqual(validateInstallerScriptBody(script), INSTALLER_SCRIPT_REQUIRED_SNIPPETS);
});

test("validateInstallerScriptBody rejects missing installer status hooks", () => {
  assert.throws(
    () => validateInstallerScriptBody("echo hello"),
    /Installer script is missing required status wiring/
  );
});
