1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3
4import {
5 managedEndpointDigestInput,
6 managedEndpointForHostname,
7 managedEndpointHostname,
8 isManagedEndpointHostname,
9 managedEndpointTunnelName,
10 managedEndpointTunnelNamePrefix,
11 relayOwnsManagedEndpointZone,
12 RelayPublicDomainLabelTooLongError,
13 relayPublicDomainForStage,
14 relayResourceNameForStage,
15} from "./deploymentConfig.ts";
16
17const isRelayPublicDomainLabelTooLongError = Schema.is(RelayPublicDomainLabelTooLongError);
18
19describe("relayPublicDomainForStage", () => {
20 it("uses the canonical relay hostname for production", () => {
21 expect(relayPublicDomainForStage("prod", ".example.com.")).toBe("relay.example.com");
22 });
23
24 it("isolates personal stages below the imported zone", () => {
25 expect(relayPublicDomainForStage("dev_julius", "example.com")).toBe(
26 "relay-dev-julius.example.com",
27 );
28 });
29
30 it("reports the stage and derived DNS label when the label is too long", () => {
31 const stage = `dev_${"x".repeat(60)}`;
32 let error: unknown;
33
34 try {
35 relayPublicDomainForStage(stage, "example.com");
36 } catch (cause) {
37 error = cause;
38 }
39
40 if (!isRelayPublicDomainLabelTooLongError(error)) {
41 throw error;
42 }
43 expect(error).toMatchObject({
44 stage,
45 label: `relay-dev-${"x".repeat(60)}`,
46 maxLength: 63,
47 });
48 expect(error.message).toBe(
49 `Relay stage '${stage}' produces custom domain label 'relay-dev-${"x".repeat(60)}' (70 characters), exceeding the DNS label limit of 63.`,
50 );
51 });
52});
53
54describe("relayOwnsManagedEndpointZone", () => {
55 it("keeps the shared Cloudflare zone owned by production", () => {
56 expect(relayOwnsManagedEndpointZone("prod")).toBe(true);
57 expect(relayOwnsManagedEndpointZone("dev_julius")).toBe(false);
58 });
59});
60
61describe("relayResourceNameForStage", () => {
62 it("isolates production and personal stages", () => {
63 expect(relayResourceNameForStage("t3-code-relay-traces", "prod")).toBe(
64 "t3-code-relay-traces-prod",
65 );
66 expect(relayResourceNameForStage("t3-code-relay-traces", "dev_julius")).toBe(
67 "t3-code-relay-traces-dev-julius",
68 );
69 });
70});
71
72describe("managed endpoint names", () => {
73 it("uses the stage slug and a stable stage-scoped digest suffix", () => {
74 const hash = "ABCDEF0123456789ABCDEF0123456789";
75
76 expect(managedEndpointDigestInput("dev_julius", "user_123", "env_123")).toBe(
77 "dev_julius:user_123:env_123",
78 );
79 expect(managedEndpointHostname("dev_julius", ".example.com.", hash)).toBe(
80 "dev-julius-abcdef0123456789.example.com",
81 );
82 expect(managedEndpointHostname("prod", "t3coderelay.com", hash)).toBe(
83 "prod-abcdef0123456789.t3coderelay.com",
84 );
85 expect(managedEndpointTunnelName("dev_julius", hash)).toBe(
86 "t3coderelay-managedendpoint-dev-julius-abcdef0123456789",
87 );
88 expect(managedEndpointTunnelNamePrefix("dev_julius")).toBe(
89 "t3coderelay-managedendpoint-dev-julius-",
90 );
91 });
92
93 it("keeps the DNS label within the provider limit for long stage names", () => {
94 const hostname = managedEndpointHostname(
95 "dev_" + "x".repeat(100),
96 "example.com",
97 "a".repeat(64),
98 );
99
100 expect(hostname.split(".")[0]?.length).toBeLessThanOrEqual(63);
101 expect(hostname).toMatch(/-a{16}\.example\.com$/);
102 });
103
104 it("accepts allocated hostnames within the relay zone", () => {
105 expect(
106 isManagedEndpointHostname("dev-julius-abcdef0123456789.example.com", "example.com"),
107 ).toBe(true);
108 expect(managedEndpointForHostname("dev-julius-abcdef0123456789.example.com")).toEqual({
109 httpBaseUrl: "https://dev-julius-abcdef0123456789.example.com/",
110 wsBaseUrl: "wss://dev-julius-abcdef0123456789.example.com/ws",
111 providerKind: "cloudflare_tunnel",
112 });
113 });
114
115 it("rejects hostnames outside the relay zone", () => {
116 expect(isManagedEndpointHostname("internal.example.net", "example.com")).toBe(false);
117 expect(isManagedEndpointHostname("example.com.attacker.test", "example.com")).toBe(false);
118 expect(isManagedEndpointHostname("dev-julius.example.com.", "example.com")).toBe(false);
119 });
120});