@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 443 lines 16.7 KB
Raw
1 import * as Drizzle from "@alchemy.run/drizzle/Postgres";
2 import * as Railway from "@alchemy.run/railway";
3 import { RailwayAuth } from "@alchemy.run/railway/AuthProvider";
4 import { fromAuthProvider } from "@alchemy.run/railway/Credentials";
5 import { resolveConnectionOptions } from "@alchemy.run/sql/PostgresTls";
6 import { describe, expect } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import { Query } from "@distilled.cloud/core/query";
9 import { GraphQLLive, Railway as RailwayApi } from "@distilled.cloud/railway";
10 import * as PgClient from "@effect/sql-pg/PgClient";
11 import * as Alchemy from "alchemy";
12 import * as Provider from "alchemy/Provider";
13 import * as Data from "effect/Data";
14 import * as Effect from "effect/Effect";
15 import * as HttpClient from "effect/http/HttpClient";
16 import * as Layer from "effect/Layer";
17 import * as Redacted from "effect/Redacted";
18 import { MinimumLogLevel } from "effect/References";
19 import * as Schedule from "effect/Schedule";
20 import { PostgresFn } from "./fixtures/async-postgres-fn.ts";
21 import PostgresApi, { Db, Site } from "./fixtures/postgres-api.ts";
22 import { suitePartition } from "./suiteProject.ts";
23 import { waitUntilVolumeGone } from "./waitUntilVolumeGone.ts";
24
25 const { test } = Test.make({ providers: Railway.providers() });
26
27 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
28
29 const distilled = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
30 effect.pipe(
31 Effect.provide(Layer.merge(GraphQLLive, fromAuthProvider().pipe(Layer.provide(RailwayAuth)))),
32 );
33
34 const firstOk = (rows: unknown): unknown => {
35 if (!Array.isArray(rows) || rows[0] == null || typeof rows[0] !== "object") {
36 return undefined;
37 }
38 return (rows[0] as { ok?: unknown }).ok;
39 };
40
41 const selectOne = Effect.fn(
42 function* (_url: string) {
43 const sql = yield* PgClient.PgClient;
44 return yield* sql`select 1 as ok`;
45 },
46 (effect, url) =>
47 effect.pipe(
48 Effect.provide(
49 PgClient.layer({
50 ...resolveConnectionOptions(Redacted.make(url), { rejectUnauthorized: false }),
51 // Bound each attempt so the retry schedule (not a hung socket) owns
52 // the readiness budget.
53 connectTimeout: "10 seconds",
54 }),
55 ),
56 // Allow the new database and TCP proxy to become ready. Ten retries
57 // bound backoff to 50 seconds; each connection attempt also has a timeout.
58 Effect.retry({ schedule: Schedule.spaced("5 seconds"), times: 10 }),
59 ),
60 );
61
62 const readVariables = Query.fn((projectId: string, environmentId: string, serviceId: string) =>
63 RailwayApi.variables({ projectId, environmentId, serviceId, unrendered: true }),
64 );
65
66 const readServiceDeletedAt = Query.fn((id: string) => ({
67 deletedAt: RailwayApi.service({ id }).deletedAt,
68 }));
69
70 const readService = Query.fn((id: string) => {
71 const service = RailwayApi.service({ id });
72 return {
73 id: service.id,
74 name: service.name,
75 projectId: service.projectId,
76 deletedAt: service.deletedAt,
77 };
78 });
79
80 const readServiceInstance = Query.fn((environmentId: string, serviceId: string) => {
81 const instance = RailwayApi.serviceInstance({ environmentId, serviceId });
82 return { serviceId: instance.serviceId, image: instance.source.image };
83 });
84
85 const readVolumeInstance = Query.fn((id: string) => {
86 const volume = RailwayApi.volumeInstance({ id });
87 return {
88 id: volume.id,
89 volumeId: volume.volumeId,
90 mountPath: volume.mountPath,
91 serviceId: volume.serviceId,
92 };
93 });
94
95 const readTcpProxies = Query.fn((environmentId: string, serviceId: string) =>
96 RailwayApi.tcpProxies({ environmentId, serviceId }).pipe(
97 Query.map((proxy) => ({
98 id: proxy.id,
99 domain: proxy.domain,
100 proxyPort: proxy.proxyPort,
101 applicationPort: proxy.applicationPort,
102 deletedAt: proxy.deletedAt,
103 syncStatus: proxy.syncStatus,
104 })),
105 ),
106 );
107
108 const asVariableMap = (value: unknown): Record<string, string> => {
109 if (value === null || typeof value !== "object" || Array.isArray(value)) {
110 return {};
111 }
112 const out: Record<string, string> = {};
113 for (const [key, item] of Object.entries(value)) {
114 if (typeof item === "string") {
115 out[key] = item;
116 }
117 }
118 return out;
119 };
120
121 const readServiceVariables = (projectId: string, environmentId: string, serviceId: string) =>
122 readVariables(projectId, environmentId, serviceId).pipe(
123 Effect.map(asVariableMap),
124 Effect.catchTag("RailwayNotFound", () => Effect.succeed({} as Record<string, string>)),
125 );
126
127 const waitUntilServiceGone = (serviceId: string) =>
128 readServiceDeletedAt(serviceId).pipe(
129 Effect.map((service) => (service.deletedAt != null ? ("gone" as const) : ("found" as const))),
130 Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)),
131 Effect.repeat({
132 schedule: Schedule.spaced("1 second"),
133 until: (status) => status === "gone",
134 times: 10,
135 }),
136 );
137
138 class NotReady extends Data.TaggedError("NotReady")<{ status: number; body?: unknown }> {
139 override get message() {
140 return this.body === undefined
141 ? `status ${this.status}`
142 : `status ${this.status}: ${JSON.stringify(this.body)}`;
143 }
144 }
145
146 // Deploy the database first, then extend the same stack with its clients.
147 // Each setup phase gets its own bounded budget; a remote image build no
148 // longer shares its deadline with database and volume provisioning.
149 const DatabaseFixtureStack = Alchemy.Stack(
150 "RailwayPostgresFixture",
151 { providers: Railway.providers(), state: Alchemy.localState() },
152 Effect.gen(function* () {
153 return yield* Db;
154 }),
155 );
156
157 const FixtureStack = Alchemy.Stack(
158 "RailwayPostgresFixture",
159 { providers: Railway.providers(), state: Alchemy.localState() },
160 Effect.gen(function* () {
161 const project = yield* Site;
162 const db = yield* Db;
163 const api = yield* PostgresApi;
164 const fn = yield* PostgresFn;
165 return {
166 projectId: project.projectId,
167 environmentId: db.environmentId,
168 serviceId: api.serviceId,
169 url: api.url,
170 publicConnectionUri: db.publicConnectionUri,
171 functionId: fn.serviceId,
172 functionUrl: fn.url,
173 mode: "effect" as const,
174 };
175 }),
176 );
177
178 test.provider(
179 "create, select 1, update, list, and delete postgres",
180 (stack) =>
181 Effect.gen(function* () {
182 yield* stack.destroy();
183
184 const created = yield* stack.deploy(
185 Effect.gen(function* () {
186 const { project, environment } = yield* suitePartition;
187 const db = yield* Railway.Postgres("Db", { project, environment });
188 return { project, environment, db };
189 }),
190 );
191
192 expect(created.db.serviceId).toEqual(expect.any(String));
193 expect(created.db.serviceId.length).toBeGreaterThan(0);
194 expect(created.db.projectId).toEqual(created.project.projectId);
195 expect(created.db.environmentId).toEqual(created.environment.environmentId);
196 expect(created.db.name).toEqual(expect.any(String));
197 expect(created.db.name.length).toBeGreaterThan(0);
198 expect(created.db.name.length).toBeLessThanOrEqual(32);
199 expect(created.db.name).toMatch(/^[a-z][a-z0-9-]*$/);
200 expect(created.db.user).toEqual("postgres");
201 expect(created.db.database).toEqual("railway");
202 expect(created.db.volumeId.length).toBeGreaterThan(0);
203 expect(created.db.volumeInstanceId.length).toBeGreaterThan(0);
204 expect(created.db.tcpProxyId).toEqual(expect.any(String));
205 expect(created.db.tcpProxyDomain).toEqual(expect.any(String));
206 expect(created.db.tcpProxyDomain).toContain("proxy.rlwy.net");
207 expect(created.db.tcpProxyPort).toEqual(expect.any(Number));
208 expect(created.db.tcpProxyPort).toBeGreaterThan(0);
209 expect(created.db.connectionUri.includes(`${created.db.name}.railway.internal`)).toEqual(
210 true,
211 );
212 expect(created.db.publicConnectionUri.includes(created.db.tcpProxyDomain!)).toEqual(true);
213
214 const fetched = yield* readService(created.db.serviceId);
215 expect(fetched.id).toEqual(created.db.serviceId);
216 expect(fetched.name).toEqual(created.db.name);
217 expect(fetched.projectId).toEqual(created.db.projectId);
218 expect(fetched.deletedAt).toBeNull();
219
220 const instance = yield* readServiceInstance(created.db.environmentId, created.db.serviceId);
221 expect(instance.serviceId).toEqual(created.db.serviceId);
222 expect(instance.image).toEqual(expect.stringContaining("postgres-ssl"));
223
224 const volume = yield* readVolumeInstance(created.db.volumeInstanceId);
225 expect(volume.id).toEqual(created.db.volumeInstanceId);
226 expect(volume.volumeId).toEqual(created.db.volumeId);
227 expect(volume.mountPath).toEqual("/var/lib/postgresql/data");
228 expect(volume.serviceId).toEqual(created.db.serviceId);
229
230 const proxies = yield* readTcpProxies(created.db.environmentId, created.db.serviceId);
231 const liveProxy = proxies.find(
232 (proxy) => proxy.deletedAt == null && proxy.syncStatus !== "DELETED",
233 );
234 expect(liveProxy).toBeDefined();
235 expect(liveProxy?.id).toEqual(created.db.tcpProxyId);
236 expect(liveProxy?.applicationPort).toEqual(5432);
237
238 const provider = yield* Provider.findProvider(Railway.Postgres);
239 const listed = yield* provider.list();
240 const found = listed.find((row) => row.serviceId === created.db.serviceId);
241 expect(found).toBeDefined();
242 expect(found?.name).toEqual(created.db.name);
243 expect(found?.projectId).toEqual(created.db.projectId);
244
245 const rows = yield* selectOne(created.db.publicConnectionUri);
246 expect(firstOk(rows)).toEqual(1);
247
248 const nextName = created.db.name.slice(0, -1) + (created.db.name.endsWith("z") ? "y" : "z");
249
250 const updated = yield* stack.deploy(
251 Effect.gen(function* () {
252 const { project, environment } = yield* suitePartition;
253 const db = yield* Railway.Postgres("Db", { project, environment, name: nextName });
254 return { project, environment, db };
255 }),
256 );
257
258 expect(updated.db.serviceId).toEqual(created.db.serviceId);
259 expect(updated.db.name).toEqual(nextName);
260 expect(updated.db.projectId).toEqual(created.db.projectId);
261 expect(updated.db.volumeId).toEqual(created.db.volumeId);
262 expect(updated.db.connectionUri.includes(`${nextName}.railway.internal`)).toEqual(true);
263
264 const fetchedUpdate = yield* readService(updated.db.serviceId);
265 expect(fetchedUpdate.id).toEqual(updated.db.serviceId);
266 expect(fetchedUpdate.name).toEqual(nextName);
267
268 yield* stack.destroy();
269
270 const gone = yield* waitUntilServiceGone(created.db.serviceId);
271 expect(gone).toEqual("gone");
272 const volumeGone = yield* waitUntilVolumeGone(created.db.volumeInstanceId);
273 expect(volumeGone).toEqual("gone");
274 }).pipe(logLevel),
275 {
276 tags: [
277 "provider:railway",
278 "provider:railway:postgres",
279 "provider:railway:project",
280 "provider:railway:projectenvironment",
281 "provider:railway:service",
282 "live",
283 ],
284 timeout: 120_000,
285 },
286 );
287
288 // Runtime fixture failures must not prevent the independent resource lifecycle test.
289 describe(
290 "ConnectPostgres runtime integrations",
291 {
292 tags: [
293 "provider:railway",
294 "provider:railway:function",
295 "provider:railway:postgres",
296 "provider:railway:project",
297 "provider:railway:projectenvironment",
298 "provider:railway:service",
299 "provider:railway:variable",
300 "live",
301 ],
302 },
303 () => {
304 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
305 providers: Railway.providers(),
306 });
307
308 beforeAll(deploy(DatabaseFixtureStack), { timeout: 120_000 });
309 const fixture = beforeAll(deploy(FixtureStack), { timeout: 120_000 });
310 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(FixtureStack), { timeout: 120_000 });
311
312 test(
313 "a Service connects and SELECTs through ConnectPostgres",
314 Effect.gen(function* () {
315 const out = yield* fixture;
316 expect(out.serviceId).toEqual(expect.any(String));
317 expect(out.serviceId.length).toBeGreaterThan(0);
318 expect(out.url).toEqual(expect.any(String));
319 expect(out.url).toContain("up.railway.app");
320
321 const fetched = yield* distilled(readService(out.serviceId));
322 expect(fetched.id).toEqual(out.serviceId);
323 expect(fetched.deletedAt).toBeNull();
324
325 const vars = yield* distilled(
326 readServiceVariables(out.projectId, out.environmentId, out.serviceId),
327 );
328 expect((vars[Railway.DATABASE_URL_SECRET] ?? "").length).toBeGreaterThan(0);
329
330 const client = yield* HttpClient.HttpClient;
331 const get = (path: string) =>
332 client.get(`${out.url}${path}`).pipe(
333 Effect.timeoutOrElse({
334 duration: "8 seconds",
335 orElse: () => Effect.fail(new NotReady({ status: 0 })),
336 }),
337 Effect.flatMap((res) =>
338 res.status === 200
339 ? res.json.pipe(Effect.mapError(() => new NotReady({ status: res.status })))
340 : Effect.fail(new NotReady({ status: res.status })),
341 ),
342 Effect.retry({
343 while: (e) =>
344 e._tag === "NotReady" &&
345 (e.status === 0 || e.status === 404 || e.status === 502 || e.status === 503),
346 schedule: Schedule.exponential("500 millis").pipe(
347 Schedule.upTo({ duration: "45 seconds" }),
348 ),
349 times: 10,
350 }),
351 );
352
353 const getText = client.get(out.url!).pipe(
354 Effect.timeoutOrElse({
355 duration: "8 seconds",
356 orElse: () => Effect.fail(new NotReady({ status: 0 })),
357 }),
358 Effect.flatMap((res) =>
359 res.status === 200
360 ? res.text.pipe(Effect.mapError(() => new NotReady({ status: res.status })))
361 : Effect.fail(new NotReady({ status: res.status })),
362 ),
363 Effect.retry({
364 while: (e) =>
365 e._tag === "NotReady" &&
366 (e.status === 0 || e.status === 404 || e.status === 502 || e.status === 503),
367 schedule: Schedule.exponential("500 millis").pipe(
368 Schedule.upTo({ duration: "45 seconds" }),
369 ),
370 times: 10,
371 }),
372 );
373
374 if (out.mode === "effect") {
375 const ping = (yield* get("/ping")) as { ok?: boolean };
376 expect(ping.ok).toEqual(true);
377
378 const health = (yield* get("/health")) as { rows?: unknown };
379 expect(firstOk(health.rows)).toEqual(1);
380 } else {
381 // Public image fallback: docker push is impossible without a
382 // registry. HTTP health is hashicorp/http-echo; SELECT 1 runs
383 // over the public TCP proxy. ConnectPostgres still packed
384 // DATABASE_URL onto the Service.
385 const body = yield* getText;
386 expect(typeof body).toEqual("string");
387 expect(body.length).toBeGreaterThan(0);
388 }
389
390 const rows = yield* selectOne(out.publicConnectionUri);
391 expect(firstOk(rows)).toEqual(1);
392 }).pipe(logLevel),
393 { timeout: 120_000 },
394 );
395
396 test(
397 "an async Function SELECTs through env.DATABASE_URL",
398 Effect.gen(function* () {
399 const out = yield* fixture;
400 expect(out.functionId).toEqual(expect.any(String));
401 expect(out.functionUrl).toEqual(expect.any(String));
402 expect(out.functionUrl).toContain("up.railway.app");
403
404 const vars = yield* distilled(
405 readServiceVariables(out.projectId, out.environmentId, out.functionId),
406 );
407 expect((vars[Railway.DATABASE_URL_SECRET] ?? "").length).toBeGreaterThan(0);
408
409 const client = yield* HttpClient.HttpClient;
410 const get = (path: string) =>
411 client.get(`${out.functionUrl}${path}`).pipe(
412 Effect.timeoutOrElse({
413 duration: "8 seconds",
414 orElse: () => Effect.fail(new NotReady({ status: 0 })),
415 }),
416 Effect.flatMap((res) =>
417 res.status === 200
418 ? res.json.pipe(Effect.mapError(() => new NotReady({ status: res.status })))
419 : res.text.pipe(
420 Effect.catch(() => Effect.succeed("")),
421 Effect.flatMap((body) =>
422 Effect.fail(new NotReady({ status: res.status, body })),
423 ),
424 ),
425 ),
426 Effect.retry({
427 while: (e) =>
428 e._tag === "NotReady" &&
429 (e.status === 0 || e.status === 404 || e.status === 502 || e.status === 503),
430 schedule: Schedule.exponential("500 millis").pipe(
431 Schedule.upTo({ duration: "45 seconds" }),
432 ),
433 times: 10,
434 }),
435 );
436
437 const health = (yield* get("/")) as { rows?: unknown };
438 expect(firstOk(health.rows)).toEqual(1);
439 }).pipe(logLevel),
440 { timeout: 120_000 },
441 );
442 },
443 );