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

main
ts 237 lines 8.9 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 Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
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 redisCommand = (...args: Parameters<typeof Railway.runRedisCommand>) =>
17 Railway.runRedisCommand(...args).pipe(Effect.timeout("5 seconds"));
18
19 const REDIS_VALUE = "alchemy-railway-redis";
20
21 const readVariables = Query.fn((projectId: string, environmentId: string, serviceId: string) =>
22 RailwayApi.variables({ projectId, environmentId, serviceId, unrendered: true }),
23 );
24
25 const readServiceDeletedAt = Query.fn((id: string) => ({
26 deletedAt: RailwayApi.service({ id }).deletedAt,
27 }));
28
29 const readService = Query.fn((id: string) => {
30 const service = RailwayApi.service({ id });
31 return {
32 id: service.id,
33 name: service.name,
34 projectId: service.projectId,
35 deletedAt: service.deletedAt,
36 };
37 });
38
39 const readServiceInstance = Query.fn((environmentId: string, serviceId: string) => {
40 const instance = RailwayApi.serviceInstance({ environmentId, serviceId });
41 return { serviceId: instance.serviceId, image: instance.source.image };
42 });
43
44 const readTcpProxies = Query.fn((environmentId: string, serviceId: string) =>
45 RailwayApi.tcpProxies({ environmentId, serviceId }).pipe(
46 Query.map((proxy) => ({
47 id: proxy.id,
48 domain: proxy.domain,
49 proxyPort: proxy.proxyPort,
50 applicationPort: proxy.applicationPort,
51 deletedAt: proxy.deletedAt,
52 syncStatus: proxy.syncStatus,
53 })),
54 ),
55 );
56
57 const asVariableMap = (value: unknown): Record<string, string> => {
58 if (value === null || typeof value !== "object" || Array.isArray(value)) {
59 return {};
60 }
61 const out: Record<string, string> = {};
62 for (const [key, item] of Object.entries(value)) {
63 if (typeof item === "string") {
64 out[key] = item;
65 }
66 }
67 return out;
68 };
69
70 const readServiceVariables = (projectId: string, environmentId: string, serviceId: string) =>
71 readVariables(projectId, environmentId, serviceId).pipe(
72 Effect.map(asVariableMap),
73 Effect.catchTag("RailwayNotFound", () => Effect.succeed({} as Record<string, string>)),
74 );
75
76 const waitUntilGone = (serviceId: string) =>
77 readServiceDeletedAt(serviceId).pipe(
78 Effect.map((service) => (service.deletedAt != null ? ("gone" as const) : ("found" as const))),
79 Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)),
80 Effect.repeat({
81 schedule: Schedule.spaced("1 second"),
82 until: (status) => status === "gone",
83 times: 10,
84 }),
85 );
86
87 const waitUntilProxyGone = (environmentId: string, serviceId: string, id: string) =>
88 readTcpProxies(environmentId, serviceId).pipe(
89 Effect.map((items) =>
90 items.some(
91 (proxy) => proxy.id === id && proxy.deletedAt == null && proxy.syncStatus !== "DELETED",
92 )
93 ? ("found" as const)
94 : ("gone" as const),
95 ),
96 Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)),
97 Effect.repeat({
98 schedule: Schedule.spaced("1 second"),
99 until: (status) => status === "gone",
100 times: 10,
101 }),
102 );
103
104 test.provider(
105 "create, set/get via tcp proxy, update, list, and delete redis",
106 (stack) =>
107 Effect.gen(function* () {
108 yield* stack.destroy();
109
110 const created = yield* stack.deploy(
111 Effect.gen(function* () {
112 const { project, environment } = yield* suitePartition;
113 const cache = yield* Railway.Redis("Cache", { project, environment });
114 const proxy = yield* Railway.TcpProxy("CacheProxy", {
115 redis: cache,
116 environment,
117 applicationPort: Railway.REDIS_PORT,
118 });
119 return { project, environment, cache, proxy };
120 }),
121 );
122
123 expect(created.cache.serviceId).toEqual(expect.any(String));
124 expect(created.cache.serviceId.length).toBeGreaterThan(0);
125 expect(created.cache.projectId).toEqual(created.project.projectId);
126 expect(created.cache.environmentId).toEqual(created.environment.environmentId);
127 expect(created.cache.name).toEqual(expect.any(String));
128 expect(created.cache.name.length).toBeGreaterThan(0);
129 expect(created.cache.name.length).toBeLessThanOrEqual(32);
130 expect(created.cache.name).toMatch(/^[a-z][a-z0-9-]*$/);
131 expect(created.cache.port).toEqual(6379);
132 expect(created.cache.privateHost).toEqual(`${created.cache.name}.railway.internal`);
133 expect(created.cache.image).toEqual(expect.stringContaining("redis"));
134 expect(created.proxy.applicationPort).toEqual(6379);
135 expect(created.proxy.serviceId).toEqual(created.cache.serviceId);
136 expect(created.proxy.domain).toEqual(expect.any(String));
137 expect(created.proxy.proxyPort).toEqual(expect.any(Number));
138
139 const fetched = yield* readService(created.cache.serviceId);
140 expect(fetched.id).toEqual(created.cache.serviceId);
141 expect(fetched.name).toEqual(created.cache.name);
142 expect(fetched.projectId).toEqual(created.cache.projectId);
143 expect(fetched.deletedAt).toBeNull();
144
145 const instance = yield* readServiceInstance(
146 created.cache.environmentId,
147 created.cache.serviceId,
148 );
149 expect(instance.serviceId).toEqual(created.cache.serviceId);
150 expect(instance.image).toEqual(expect.stringContaining("redis"));
151
152 const vars = yield* readServiceVariables(
153 created.cache.projectId,
154 created.cache.environmentId,
155 created.cache.serviceId,
156 );
157 const password = vars[Railway.REDIS_PASSWORD_ENV];
158 expect(password !== undefined && password.length > 0).toEqual(true);
159 expect(vars[Railway.REDIS_URL_ENV] !== undefined).toEqual(true);
160
161 const url = Railway.redisConnectionUrl({
162 host: created.proxy.domain,
163 port: created.proxy.proxyPort,
164 password: password!,
165 });
166
167 yield* Effect.logDebug("Redis test: PING started");
168 const pong = yield* redisCommand(url, "PING").pipe(
169 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 3 }),
170 );
171 yield* Effect.logDebug("Redis test: PING completed");
172 expect(String(pong).toUpperCase()).toContain("PONG");
173
174 yield* redisCommand(url, "SET", ["marker", REDIS_VALUE]);
175 const got = yield* redisCommand(url, "GET", ["marker"]);
176 expect(got).toEqual(REDIS_VALUE);
177
178 const provider = yield* Provider.findProvider(Railway.Redis);
179 yield* Effect.logDebug("Redis test: provider.list started");
180 const listed = yield* provider.list();
181 yield* Effect.logDebug("Redis test: provider.list completed");
182 const found = listed.find((row) => row.serviceId === created.cache.serviceId);
183 expect(found).toBeDefined();
184 expect(found?.name).toEqual(created.cache.name);
185 expect(found?.projectId).toEqual(created.cache.projectId);
186
187 const nextName =
188 created.cache.name.slice(0, -1) + (created.cache.name.endsWith("z") ? "y" : "z");
189
190 yield* Effect.logDebug("Redis test: update started");
191 const updated = yield* stack.deploy(
192 Effect.gen(function* () {
193 const { project, environment } = yield* suitePartition;
194 const cache = yield* Railway.Redis("Cache", { project, environment, name: nextName });
195 const proxy = yield* Railway.TcpProxy("CacheProxy", {
196 redis: cache,
197 environment,
198 applicationPort: Railway.REDIS_PORT,
199 });
200 return { project, environment, cache, proxy };
201 }),
202 );
203
204 yield* Effect.logDebug("Redis test: update completed");
205 expect(updated.cache.serviceId).toEqual(created.cache.serviceId);
206 expect(updated.cache.name).toEqual(nextName);
207 expect(updated.cache.privateHost).toEqual(`${nextName}.railway.internal`);
208 expect(updated.proxy.id).toEqual(created.proxy.id);
209
210 const fetchedUpdate = yield* readService(updated.cache.serviceId);
211 expect(fetchedUpdate.name).toEqual(nextName);
212
213 yield* stack.destroy();
214
215 const proxyGone = yield* waitUntilProxyGone(
216 created.environment.environmentId,
217 created.cache.serviceId,
218 created.proxy.id,
219 );
220 expect(proxyGone).toEqual("gone");
221 const gone = yield* waitUntilGone(created.cache.serviceId);
222 expect(gone).toEqual("gone");
223 }).pipe(logLevel),
224 {
225 tags: [
226 "provider:railway",
227 "provider:railway:project",
228 "provider:railway:projectenvironment",
229 "provider:railway:redis",
230 "provider:railway:service",
231 "provider:railway:tcpproxy",
232 "provider:railway:variable",
233 "live",
234 ],
235 timeout: 120_000,
236 },
237 );