@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 207 lines 7.06 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 Schedule from "effect/Schedule";
9 import { suitePartition } from "./suiteProject.ts";
10
11 const { test } = Test.make({ providers: Railway.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const readTcpProxies = Query.fn((environmentId: string, serviceId: string) =>
16 RailwayApi.tcpProxies({ environmentId, serviceId }).pipe(
17 Query.map((proxy) => ({
18 id: proxy.id,
19 domain: proxy.domain,
20 proxyPort: proxy.proxyPort,
21 applicationPort: proxy.applicationPort,
22 deletedAt: proxy.deletedAt,
23 syncStatus: proxy.syncStatus,
24 })),
25 ),
26 );
27
28 const createService = Query.fn(
29 (input: { projectId: string; environmentId: string; image: string }) => ({
30 id: RailwayApi.serviceCreate({
31 input: {
32 projectId: input.projectId,
33 environmentId: input.environmentId,
34 source: { image: input.image },
35 },
36 }).id,
37 }),
38 );
39
40 const listLive = (environmentId: string, serviceId: string) =>
41 readTcpProxies(environmentId, serviceId).pipe(
42 Effect.map((items) =>
43 items
44 .filter((proxy) => proxy.deletedAt == null && proxy.syncStatus !== "DELETED")
45 .map((proxy) => ({ ...proxy, domain: proxy.domain.replace(/\.+$/, "") })),
46 ),
47 Effect.catchTag("RailwayNotFound", () => Effect.succeed([])),
48 );
49
50 const waitUntilProxyGone = (environmentId: string, serviceId: string, id: string) =>
51 listLive(environmentId, serviceId).pipe(
52 Effect.map((items) =>
53 items.some((proxy) => proxy.id === id) ? ("found" as const) : ("gone" as const),
54 ),
55 Effect.repeat({
56 schedule: Schedule.spaced("1 second"),
57 until: (status) => status === "gone",
58 times: 10,
59 }),
60 );
61
62 const createTargetService = (projectId: string, environmentId: string) =>
63 createService({ projectId, environmentId, image: "redis:7-alpine" });
64
65 test.provider(
66 "create, update, and delete a tcp proxy",
67 (stack) =>
68 Effect.gen(function* () {
69 yield* stack.destroy();
70
71 const { project, environment } = yield* stack.deploy(suitePartition);
72
73 const service = yield* createTargetService(project.projectId, environment.environmentId);
74
75 const created = yield* stack.deploy(
76 Effect.gen(function* () {
77 const { project: site, environment } = yield* suitePartition;
78 const proxy = yield* Railway.TcpProxy("DbProxy", {
79 service: { serviceId: service.id },
80 environment,
81 applicationPort: 6379,
82 });
83 return { project: site, environment, proxy };
84 }),
85 );
86
87 expect(created.proxy.id).toEqual(expect.any(String));
88 expect(created.proxy.id.length).toBeGreaterThan(0);
89 expect(created.proxy.domain).toEqual(expect.any(String));
90 expect(created.proxy.domain.length).toBeGreaterThan(0);
91 expect(created.proxy.proxyPort).toEqual(expect.any(Number));
92 expect(created.proxy.proxyPort).toBeGreaterThan(0);
93 expect(created.proxy.applicationPort).toEqual(6379);
94 expect(created.proxy.serviceId).toEqual(service.id);
95 expect(created.proxy.environmentId).toEqual(environment.environmentId);
96
97 const listed = yield* listLive(environment.environmentId, service.id);
98 const fetched = listed.find((proxy) => proxy.id === created.proxy.id);
99 expect(fetched).toBeDefined();
100 expect(fetched?.domain).toEqual(created.proxy.domain);
101 expect(fetched?.proxyPort).toEqual(created.proxy.proxyPort);
102 expect(fetched?.applicationPort).toEqual(6379);
103
104 const updated = yield* stack.deploy(
105 Effect.gen(function* () {
106 const { project: site, environment } = yield* suitePartition;
107 const proxy = yield* Railway.TcpProxy("DbProxy", {
108 service: { serviceId: service.id },
109 environment,
110 applicationPort: 6379,
111 });
112 return { project: site, environment, proxy };
113 }),
114 );
115
116 expect(updated.proxy.id).toEqual(created.proxy.id);
117 expect(updated.proxy.domain).toEqual(created.proxy.domain);
118 expect(updated.proxy.proxyPort).toEqual(created.proxy.proxyPort);
119 expect(updated.proxy.applicationPort).toEqual(6379);
120 expect(updated.project.projectId).toEqual(project.projectId);
121
122 yield* stack.destroy();
123
124 const proxyGone = yield* waitUntilProxyGone(
125 environment.environmentId,
126 service.id,
127 created.proxy.id,
128 );
129 expect(proxyGone).toEqual("gone");
130 }).pipe(logLevel),
131 {
132 tags: [
133 "provider:railway",
134 "provider:railway:project",
135 "provider:railway:projectenvironment",
136 "provider:railway:tcpproxy",
137 "live",
138 ],
139 timeout: 120_000,
140 },
141 );
142
143 test.provider(
144 "replace when applicationPort changes",
145 (stack) =>
146 Effect.gen(function* () {
147 yield* stack.destroy();
148
149 const { project, environment } = yield* stack.deploy(suitePartition);
150
151 const service = yield* createTargetService(project.projectId, environment.environmentId);
152
153 const created = yield* stack.deploy(
154 Effect.gen(function* () {
155 const { project: site, environment } = yield* suitePartition;
156 const proxy = yield* Railway.TcpProxy("DbProxy", {
157 postgres: { serviceId: service.id },
158 environment,
159 applicationPort: 6379,
160 });
161 return { project: site, environment, proxy };
162 }),
163 );
164
165 const replaced = yield* stack.deploy(
166 Effect.gen(function* () {
167 const { project: site, environment } = yield* suitePartition;
168 const proxy = yield* Railway.TcpProxy("DbProxy", {
169 postgres: { serviceId: service.id },
170 environment,
171 applicationPort: 5432,
172 });
173 return { project: site, environment, proxy };
174 }),
175 );
176
177 expect(replaced.proxy.applicationPort).toEqual(5432);
178 expect(replaced.proxy.id).not.toEqual(created.proxy.id);
179 expect(replaced.proxy.domain).toEqual(expect.any(String));
180 expect(replaced.proxy.proxyPort).toEqual(expect.any(Number));
181
182 const listed = yield* listLive(environment.environmentId, service.id);
183 const next = listed.find((proxy) => proxy.id === replaced.proxy.id);
184 expect(next).toBeDefined();
185 expect(next?.applicationPort).toEqual(5432);
186 expect(listed.some((proxy) => proxy.id === created.proxy.id)).toEqual(false);
187
188 yield* stack.destroy();
189
190 const proxyGone = yield* waitUntilProxyGone(
191 environment.environmentId,
192 service.id,
193 replaced.proxy.id,
194 );
195 expect(proxyGone).toEqual("gone");
196 }).pipe(logLevel),
197 {
198 tags: [
199 "provider:railway",
200 "provider:railway:project",
201 "provider:railway:projectenvironment",
202 "provider:railway:tcpproxy",
203 "live",
204 ],
205 timeout: 120_000,
206 },
207 );