The best way to control Claude Code, Codex and more from any computer or phone

main
ts 249 lines 8.59 KB
Raw
1 import * as Railway from "@alchemy.run/railway";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import { Query } from "@distilled.cloud/core/query";
5 import { Railway as RailwayApi } from "@distilled.cloud/railway";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Result from "effect/Result";
9 import * as Schedule from "effect/Schedule";
10 import { suitePartition } from "./suiteProject.ts";
11
12 const { test } = Test.make({ providers: Railway.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 const TEST_DOMAIN = process.env.RAILWAY_TEST_DOMAIN;
17
18 const readCustomDomains = Query.fn(
19 (input: { environmentId: string; projectId: string; serviceId: string }) => ({
20 customDomains: RailwayApi.domains(input).customDomains.pipe(
21 Query.map((domain) => ({
22 id: domain.id,
23 domain: domain.domain,
24 targetPort: domain.targetPort,
25 deletedAt: domain.deletedAt,
26 syncStatus: domain.syncStatus,
27 })),
28 ),
29 }),
30 );
31
32 const readCustomDomainDeletion = Query.fn((id: string, projectId: string) => {
33 const domain = RailwayApi.customDomain({ id, projectId });
34 return { deletedAt: domain.deletedAt, syncStatus: domain.syncStatus };
35 });
36
37 const readCustomDomain = Query.fn((id: string, projectId: string) => {
38 const domain = RailwayApi.customDomain({ id, projectId });
39 return { id: domain.id, domain: domain.domain, targetPort: domain.targetPort };
40 });
41
42 const readCustomDomainVerification = Query.fn((id: string, projectId: string) => {
43 const domain = RailwayApi.customDomain({ id, projectId });
44 return { domain: domain.domain, status: { verified: domain.status.verified } };
45 });
46
47 const createCustomDomain = Query.fn(
48 (input: { domain: string; environmentId: string; projectId: string; serviceId: string }) => ({
49 id: RailwayApi.customDomainCreate({ input }).id,
50 }),
51 );
52
53 const createService = Query.fn(
54 (input: { projectId: string; environmentId: string; image: string }) => ({
55 id: RailwayApi.serviceCreate({
56 input: {
57 projectId: input.projectId,
58 environmentId: input.environmentId,
59 source: { image: input.image },
60 },
61 }).id,
62 }),
63 );
64
65 const listLive = (environmentId: string, projectId: string, serviceId: string) =>
66 readCustomDomains({ environmentId, projectId, serviceId }).pipe(
67 Effect.map((result) =>
68 result.customDomains.filter(
69 (domain) => domain.deletedAt == null && domain.syncStatus !== "DELETED",
70 ),
71 ),
72 Effect.catchTag("RailwayNotFound", () => Effect.succeed([])),
73 );
74
75 const waitUntilDomainGone = (customDomainId: string, projectId: string) =>
76 readCustomDomainDeletion(customDomainId, projectId).pipe(
77 Effect.map((domain) =>
78 domain.deletedAt != null || domain.syncStatus === "DELETED"
79 ? ("gone" as const)
80 : ("found" as const),
81 ),
82 Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)),
83 Effect.repeat({
84 schedule: Schedule.spaced("1 second"),
85 until: (status) => status === "gone",
86 times: 10,
87 }),
88 );
89
90 const createTargetService = (projectId: string, environmentId: string) =>
91 createService({ projectId, environmentId, image: "hashicorp/http-echo" });
92
93 test.provider(
94 "create, update targetPort, and delete a custom domain",
95 (stack) =>
96 Effect.gen(function* () {
97 yield* stack.destroy();
98
99 const { project, environment } = yield* stack.deploy(suitePartition);
100
101 const service = yield* createTargetService(project.projectId, environment.environmentId);
102
103 const rejected = yield* Effect.result(
104 createCustomDomain({
105 domain: "not a hostname",
106 environmentId: environment.environmentId,
107 projectId: project.projectId,
108 serviceId: service.id,
109 }),
110 );
111 expect(Result.isFailure(rejected)).toBe(true);
112 if (Result.isFailure(rejected)) {
113 expect(rejected.failure._tag === "RailwayValidationError").toBe(true);
114 }
115
116 // The suite project is shared. Derive the hostname from this test's
117 // environment so another partition's domain cannot collide with it.
118 const hostname = `graphql-${environment.environmentId.slice(0, 8)}.alchemy-test-2.us`;
119
120 const created = yield* stack.deploy(
121 Effect.gen(function* () {
122 const { project: site, environment } = yield* suitePartition;
123 const domain = yield* Railway.CustomDomain("Www", {
124 service: { serviceId: service.id },
125 environment,
126 domain: hostname,
127 targetPort: 5678,
128 });
129 return { project: site, domain };
130 }),
131 );
132
133 expect(created.domain.customDomainId).toEqual(expect.any(String));
134 expect(created.domain.customDomainId.length).toBeGreaterThan(0);
135 expect(created.domain.domain).toEqual(hostname);
136 expect(created.domain.serviceId).toEqual(service.id);
137 expect(created.domain.projectId).toEqual(project.projectId);
138 expect(created.domain.environmentId).toEqual(environment.environmentId);
139 expect(created.domain.targetPort).toEqual(5678);
140 expect(created.domain.url).toEqual(`https://${hostname}`);
141
142 const listed = yield* listLive(environment.environmentId, project.projectId, service.id);
143 const fetched = listed.find((item) => item.id === created.domain.customDomainId);
144 expect(fetched).toBeDefined();
145 expect(fetched?.domain).toEqual(hostname);
146 expect(fetched?.targetPort).toEqual(5678);
147
148 const outOfBand = yield* readCustomDomain(created.domain.customDomainId, project.projectId);
149 expect(outOfBand.id).toEqual(created.domain.customDomainId);
150 expect(outOfBand.domain).toEqual(hostname);
151 expect(outOfBand.targetPort).toEqual(5678);
152
153 const updated = yield* stack.deploy(
154 Effect.gen(function* () {
155 const { project: site, environment } = yield* suitePartition;
156 const domain = yield* Railway.CustomDomain("Www", {
157 service: { serviceId: service.id },
158 environment,
159 domain: hostname,
160 targetPort: 8080,
161 });
162 return { project: site, domain };
163 }),
164 );
165
166 expect(updated.domain.customDomainId).toEqual(created.domain.customDomainId);
167 expect(updated.domain.targetPort).toEqual(8080);
168 expect(updated.domain.domain).toEqual(hostname);
169 expect(updated.project.projectId).toEqual(project.projectId);
170
171 const fetchedUpdate = yield* readCustomDomain(
172 updated.domain.customDomainId,
173 project.projectId,
174 );
175 expect(fetchedUpdate.targetPort).toEqual(8080);
176
177 yield* stack.destroy();
178
179 const domainGone = yield* waitUntilDomainGone(
180 created.domain.customDomainId,
181 project.projectId,
182 );
183 expect(domainGone).toEqual("gone");
184 }).pipe(logLevel),
185 {
186 tags: [
187 "provider:railway",
188 "provider:railway:customdomain",
189 "provider:railway:project",
190 "provider:railway:projectenvironment",
191 "live",
192 ],
193 timeout: 120_000,
194 },
195 );
196
197 test.provider.skipIf(!TEST_DOMAIN)(
198 "ACME verifies when RAILWAY_TEST_DOMAIN is set",
199 (stack) =>
200 Effect.gen(function* () {
201 yield* stack.destroy();
202
203 const hostname = TEST_DOMAIN!;
204
205 const { project, environment } = yield* stack.deploy(suitePartition);
206
207 const service = yield* createTargetService(project.projectId, environment.environmentId);
208
209 const created = yield* stack.deploy(
210 Effect.gen(function* () {
211 const { project: site, environment } = yield* suitePartition;
212 const domain = yield* Railway.CustomDomain("Www", {
213 service: { serviceId: service.id },
214 environment,
215 domain: hostname,
216 });
217 return { project: site, domain };
218 }),
219 );
220
221 expect(created.domain.domain).toEqual(hostname);
222 expect(created.domain.customDomainId.length).toBeGreaterThan(0);
223
224 const fetched = yield* readCustomDomainVerification(
225 created.domain.customDomainId,
226 project.projectId,
227 );
228 expect(fetched.domain).toEqual(hostname);
229 expect(fetched.status.verified === true || fetched.status.verified === false).toBe(true);
230
231 yield* stack.destroy();
232
233 const domainGone = yield* waitUntilDomainGone(
234 created.domain.customDomainId,
235 project.projectId,
236 );
237 expect(domainGone).toEqual("gone");
238 }).pipe(logLevel),
239 {
240 tags: [
241 "provider:railway",
242 "provider:railway:customdomain",
243 "provider:railway:project",
244 "provider:railway:projectenvironment",
245 "live",
246 ],
247 timeout: 120_000,
248 },
249 );