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