import assert from "node:assert/strict";
import { test } from "bun:test";

import { hwlabRuntimeLaneSpecForNode } from "../hwlab-node-lanes";
import { parseWebProbeOriginName, resolveNodeWebProbeCliOrigin, resolveNodeWebProbeOrigin, webProbeOriginSummary } from "./web-probe-origin";

const spec = hwlabRuntimeLaneSpecForNode("v03", "NC01");

test("web-probe origin names are semantic and bounded", () => {
  assert.equal(parseWebProbeOriginName("internal"), "internal");
  assert.equal(parseWebProbeOriginName("public"), "public");
  assert.equal(parseWebProbeOriginName("native"), "native");
  assert.throws(() => parseWebProbeOriginName("external"), /internal, public, or native/u);
  assert.throws(() => parseWebProbeOriginName("https:\/\/hwlab.example.test"), /internal, public, or native/u);
});

test("NC01 v03 YAML declares internal debugging and public closeout origins", () => {
  assert.equal(spec.webProbe?.defaultOrigin, "internal");
  assert.deepEqual(spec.webProbe?.origins?.internal, {
    mode: "k8s-service-cluster-ip",
    serviceName: "hwlab-cloud-web",
    namespace: "hwlab-v03",
    port: 8080,
    scheme: "http",
    browserProxyMode: "direct",
  });
  assert.deepEqual(spec.webProbe?.origins?.public, {
    mode: "public",
    browserProxyMode: "auto",
  });
});

test("public semantic origin reuses the lane public Web URL", () => {
  const origin = resolveNodeWebProbeOrigin(spec, "public");
  assert.equal(origin.originName, "public");
  assert.equal(origin.originMode, "public");
  assert.equal(origin.url, spec.publicWebUrl);
  assert.equal(origin.browserProxyMode, "auto");
  assert.match(origin.originConfigPath ?? "", /webProbe\.origins\.public/u);
});

test("internal semantic origin resolves the current Service address without YAML IPs", () => {
  const calls: Array<{ namespace: string; serviceName: string }> = [];
  const origin = resolveNodeWebProbeOrigin(spec, "internal", (_spec, namespace, serviceName) => {
    calls.push({ namespace, serviceName });
    return "10.43.99.7";
  });

  assert.deepEqual(calls, [{ namespace: "hwlab-v03", serviceName: "hwlab-cloud-web" }]);
  assert.equal(origin.originName, "internal");
  assert.equal(origin.originMode, "k8s-service-cluster-ip");
  assert.equal(origin.url, "http://10.43.99.7:8080");
  assert.equal(origin.browserProxyMode, "direct");
  assert.equal(origin.browserProxyModeSource, "yaml-origin");
  assert.deepEqual(webProbeOriginSummary(origin).source, {
    namespace: "hwlab-v03",
    serviceName: "hwlab-cloud-web",
    clusterIp: "10.43.99.7",
    port: 8080,
    scheme: "http",
  });
});

test("production internal origin is owned by the production namespace", () => {
  const production = hwlabRuntimeLaneSpecForNode("production", "NC01");
  const origin = resolveNodeWebProbeOrigin(production, "internal", (_spec, namespace) => {
    assert.equal(namespace, "hwlab-production");
    return "10.43.99.8";
  });
  assert.equal(origin.originSource?.namespace, "hwlab-production");
});

test("--origin and custom --url are mutually exclusive", () => {
  assert.throws(
    () => resolveNodeWebProbeCliOrigin(spec, "public", "http://127.0.0.1:4173", undefined),
    /--origin.*--url|--url.*--origin/u,
  );
});

test("custom/local --url remains an explicit one-shot escape hatch", () => {
  const origin = resolveNodeWebProbeCliOrigin(spec, undefined, "http://127.0.0.1:4173", undefined);
  assert.equal(origin.originName, "custom");
  assert.equal(origin.originMode, "custom-url");
  assert.equal(origin.originConfigPath, null);
  assert.equal(origin.url, "http://127.0.0.1:4173");
});
