import { describe, expect, it, vi } from "vitest"; import type { DomainCommandPayload } from "./contracts.js"; import { createPortalDomainController, DomainControllerFailure, DOMAIN_MAX_ATTEMPTS, retryDelayForAttempt } from "./controller.js"; import { DomainProbeError, type DomainProbes } from "./probes.js"; import type { TraefikConfigWriter } from "./traefik.js"; const command: DomainCommandPayload = { version: 1, claimId: "claim_12345678", tenantId: "tenant_12345678", generation: 2, hostname: "portal.example.com", action: "provision", }; const CERTIFICATE_EXPIRES_AT = "2026-10-01T00:00:00.000Z"; describe("portal-domain lifecycle controller", () => { it("does not acknowledge provision until DNS, atomic config and HTTPS succeed", async () => { const calls: string[] = []; const writer = { applyProvision: vi.fn(async () => { calls.push("write"); return { disposition: "applied", configHash: "hash" }; }), readAppliedState: vi.fn(async () => ({ generation: command.generation, action: command.action, hostname: command.hostname, tenantId: command.tenantId })), } as unknown as TraefikConfigWriter; const probes: DomainProbes = { verifyDns: vi.fn(async () => { calls.push("dns"); }), verifyHttps: vi.fn(async () => { calls.push("https"); return { certificateExpiresAt: CERTIFICATE_EXPIRES_AT }; }), }; const controller = createPortalDomainController({ writer, probes, now: () => new Date("2026-07-16T10:00:00.000Z") }); await expect(controller.handle(command, 1)).resolves.toMatchObject({ status: "active", configHash: "hash", certificateExpiresAt: CERTIFICATE_EXPIRES_AT, attempt: 1, }); expect(calls).toEqual(["dns", "write", "https"]); }); it("returns stale without attempting HTTPS and deprovisions without DNS", async () => { const writer = { applyProvision: vi.fn().mockResolvedValue({ disposition: "stale" }), applyDeprovision: vi.fn().mockResolvedValue({ disposition: "applied" }), } as unknown as TraefikConfigWriter; const probes: DomainProbes = { verifyDns: vi.fn(), verifyHttps: vi.fn() }; const controller = createPortalDomainController({ writer, probes }); await expect(controller.handle(command, 1)).resolves.toMatchObject({ status: "stale" }); expect(probes.verifyHttps).not.toHaveBeenCalled(); await expect(controller.handle({ ...command, action: "deprovision" }, 1)).resolves.toMatchObject({ status: "deprovisioned" }); expect(probes.verifyDns).toHaveBeenCalledTimes(1); }); it("repairs routing before HTTPS and rechecks ownership after the probe", async () => { const calls: string[] = []; let routingRepaired = false; const writer = { readAppliedState: vi.fn(async () => { calls.push("state"); return { generation: command.generation, action: "provision", hostname: command.hostname }; }), recordRevalidation: vi.fn(async () => { calls.push("repair"); routingRepaired = true; return { disposition: "unchanged" }; }), } as unknown as TraefikConfigWriter; const probes: DomainProbes = { verifyDns: vi.fn(async () => { calls.push("dns"); }), verifyHttps: vi.fn(async () => { calls.push("https"); if (!routingRepaired) throw new DomainProbeError("https_health_failed", "stale backend"); return { certificateExpiresAt: CERTIFICATE_EXPIRES_AT }; }), }; const controller = createPortalDomainController({ writer, probes }); await expect(controller.handle({ ...command, action: "revalidate" }, 2)).resolves.toMatchObject({ status: "healthy", certificateExpiresAt: CERTIFICATE_EXPIRES_AT, }); expect(calls).toEqual(["dns", "repair", "https", "state"]); }); it("returns stale before HTTPS and after a concurrent deprovision during the probe", async () => { const staleWriter = { recordRevalidation: vi.fn().mockResolvedValue({ disposition: "stale" }), } as unknown as TraefikConfigWriter; const staleProbes: DomainProbes = { verifyDns: vi.fn(), verifyHttps: vi.fn() }; const staleController = createPortalDomainController({ writer: staleWriter, probes: staleProbes }); await expect(staleController.handle({ ...command, action: "revalidate" }, 1)).resolves.toMatchObject({ status: "stale" }); expect(staleProbes.verifyHttps).not.toHaveBeenCalled(); const racedWriter = { recordRevalidation: vi.fn().mockResolvedValue({ disposition: "unchanged" }), readAppliedState: vi.fn().mockResolvedValue({ generation: command.generation + 1, action: "deprovision", hostname: command.hostname, tenantId: command.tenantId, }), } as unknown as TraefikConfigWriter; const racedProbes: DomainProbes = { verifyDns: vi.fn(), verifyHttps: vi.fn().mockResolvedValue({ certificateExpiresAt: CERTIFICATE_EXPIRES_AT }), }; const racedController = createPortalDomainController({ writer: racedWriter, probes: racedProbes }); await expect(racedController.handle({ ...command, action: "revalidate" }, 1)).resolves.toMatchObject({ status: "stale" }); }); it("does not emit an active acknowledgement if a newer generation wins during HTTPS probing", async () => { const writer = { applyProvision: vi.fn().mockResolvedValue({ disposition: "applied", configHash: "hash" }), readAppliedState: vi.fn().mockResolvedValue({ generation: 3, action: "deprovision", hostname: command.hostname, tenantId: command.tenantId }), } as unknown as TraefikConfigWriter; const probes: DomainProbes = { verifyDns: vi.fn(), verifyHttps: vi.fn() }; const controller = createPortalDomainController({ writer, probes }); await expect(controller.handle(command, 1)).resolves.toMatchObject({ status: "stale" }); }); it("classifies probe failures as retryable without leaking the underlying error", async () => { const writer = {} as TraefikConfigWriter; const probes: DomainProbes = { verifyDns: vi.fn().mockRejectedValue(new DomainProbeError("dns_lookup_failed", "DNS provider detail")), verifyHttps: vi.fn(), }; const controller = createPortalDomainController({ writer, probes }); await expect(controller.handle(command, 1)).rejects.toEqual( expect.objectContaining({ reasonCode: "dns_lookup_failed", retryable: true, }) ); await controller.handle(command, 1).catch((error: DomainControllerFailure) => { expect(error).toBeInstanceOf(DomainControllerFailure); }); }); it("uses the required 15m, 1h, 6h retry schedule", () => { expect(DOMAIN_MAX_ATTEMPTS).toBe(4); expect([1, 2, 3, 4].map(retryDelayForAttempt)).toEqual([900_000, 3_600_000, 21_600_000, 21_600_000]); }); });