import { readFileSync } from "node:fs"; import { hostOwnerId, labelHostname, panelFromHost, portHostname, portLabel, portSlotFromHost, portUrl, previewHostname, previewLabel, previewUrl, publicSlotFromHost, sandboxHostname, sandboxIdFromUrl, sandboxIdOfDaemonUrl, syncFolder, } from "./hostnames.js"; const ID = "abc123def456"; // The cases netd's Rust router is held to as well (_sandbox/netd, route.rs), read from the one file both run. interface HostCase { readonly host: string; readonly sandboxId?: string; readonly panel?: string; readonly port?: string; readonly public?: string; // The sandbox the name routes to (hostOwnerId), and the minted id it carries as a daemon's own address. readonly owner?: string; readonly daemon?: string; } const HOST_CASES = JSON.parse(readFileSync(new URL("./hostnames.fixture.json", import.meta.url), "utf8")) as HostCase[]; test.each(HOST_CASES)("the shared host cases parse as netd parses them: $host", (hostCase) => { expect({ panel: panelFromHost(hostCase.host, hostCase.sandboxId), port: portSlotFromHost(hostCase.host, hostCase.sandboxId), public: publicSlotFromHost(hostCase.host, hostCase.sandboxId), }).toEqual({ panel: hostCase.panel, port: hostCase.port, public: hostCase.public }); }); test.each(HOST_CASES)("the shared host cases name their owner, and a daemon address its minted id: $host", (hostCase) => { expect({ owner: hostOwnerId(hostCase.host), daemon: sandboxIdOfDaemonUrl(hostCase.host) }).toEqual({ owner: hostCase.owner, daemon: hostCase.daemon, }); }); test("a daemon address is read with or without its scheme, and an unparsable one names no sandbox", () => { expect(sandboxIdOfDaemonUrl(`https://sandbox-82789f4106b4.radarsu.com`)).toBe(`82789f4106b4`); expect(sandboxIdOfDaemonUrl(`sandbox-82789f4106b4.radarsu.com/`)).toBe(`82789f4106b4`); expect(sandboxIdOfDaemonUrl(`https://sandbox.example.com`)).toBeUndefined(); expect(sandboxIdOfDaemonUrl(`http://[not a host`)).toBeUndefined(); }); test("preview and port hostnames round-trip through their Host-header parsers", () => { expect(panelFromHost(`${previewHostname("shop--web", ID, "example.com")}:443`, ID)).toBe("shop--web"); expect(portSlotFromHost(`${portHostname("a", ID, "example.com")}:443`, ID)).toBe("a"); // The id-less form (loopback tests / provider-deployed workspaces fronting the proxy themselves). expect(panelFromHost("preview-app.example.com", undefined)).toBe("app"); expect(portSlotFromHost("port-a.example.com", undefined)).toBe("a"); }); test("the two schemes never bleed into each other, and stray/wrong-id hosts parse to nothing", () => { expect(portSlotFromHost(`preview-app-${ID}.example.com`, ID)).toBeUndefined(); expect(panelFromHost(`port-a-${ID}.example.com`, ID)).toBeUndefined(); expect(panelFromHost("app.example.com", ID)).toBeUndefined(); expect(panelFromHost(`preview-app-000000000000.example.com`, ID)).toBeUndefined(); expect(portSlotFromHost(`port-a-000000000000.example.com`, ID)).toBeUndefined(); expect(panelFromHost(undefined, ID)).toBeUndefined(); }); test("labels are the mintable unit: hostname =