// filesystem-only resolver tests (4th recurrence of
// llm-framing-deterministic). Each test seeds a temp configDir on disk and
// asserts the chosen hostname, source, and isApex flag — no driver, no graph.
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { resolvePublicHostname } from "../lib/public-hostname.js";
function writeConfigYml(configDir, body) {
    mkdirSync(join(configDir, "cloudflared"), { recursive: true });
    writeFileSync(join(configDir, "cloudflared", "config.yml"), body);
}
function writeAliasDomains(configDir, list) {
    writeFileSync(join(configDir, "alias-domains.json"), JSON.stringify(list));
}
describe("resolvePublicHostname", () => {
    let configDir;
    beforeEach(() => {
        configDir = mkdtempSync(join(tmpdir(), "public-hostname-test-"));
    });
    afterEach(() => {
        rmSync(configDir, { recursive: true, force: true });
    });
    it("returns the single ingress hostname from cloudflared/config.yml", () => {
        writeConfigYml(configDir, [
            "tunnel: abc-123",
            "ingress:",
            "  - hostname: realagent.app",
            "    service: http://localhost:3000",
            "  - service: http_status:404",
            "",
        ].join("\n"));
        expect(resolvePublicHostname(configDir)).toEqual({
            hostname: "realagent.app",
            isApex: true,
            source: "cloudflared-config.yml",
        });
    });
    it("prefers apex over subdomain when both appear in config.yml", () => {
        writeConfigYml(configDir, [
            "ingress:",
            "  - hostname: public.realagent.app",
            "    service: http://localhost:3000",
            "  - hostname: realagent.app",
            "    service: http://localhost:3000",
            "",
        ].join("\n"));
        const r = resolvePublicHostname(configDir);
        expect(r).toEqual({
            hostname: "realagent.app",
            isApex: true,
            source: "cloudflared-config.yml",
        });
    });
    it("filters localhost and *.local entries from config.yml ingress", () => {
        writeConfigYml(configDir, [
            "ingress:",
            "  - hostname: localhost",
            "    service: http://127.0.0.1:7474",
            "  - hostname: maxy.local",
            "    service: http://127.0.0.1:7475",
            "  - hostname: realagent.app",
            "    service: http://localhost:3000",
            "",
        ].join("\n"));
        expect(resolvePublicHostname(configDir).hostname).toBe("realagent.app");
    });
    it("treats www.<apex> as apex via prefix-strip tiebreak", () => {
        writeConfigYml(configDir, [
            "ingress:",
            "  - hostname: foo.realagent.app",
            "    service: http://localhost:3000",
            "  - hostname: www.realagent.app",
            "    service: http://localhost:3000",
            "",
        ].join("\n"));
        const r = resolvePublicHostname(configDir);
        expect(r.hostname).toBe("www.realagent.app");
        expect(r.isApex).toBe(true);
    });
    it("falls back to alias-domains.json when config.yml is absent", () => {
        writeAliasDomains(configDir, ["realagent.app"]);
        expect(resolvePublicHostname(configDir)).toEqual({
            hostname: "realagent.app",
            isApex: true,
            source: "alias-domains.json",
        });
    });
    it("returns reason=no-tunnel when neither file is populated", () => {
        expect(resolvePublicHostname(configDir)).toEqual({
            hostname: null,
            isApex: null,
            source: null,
            reason: "no-tunnel",
        });
    });
});
//# sourceMappingURL=public-hostname.test.js.map