Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

deploymentConfig.test.ts

116 lines4.0 KB

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