@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 320 lines 10.1 KB
Raw
1 import * as Fly from "@alchemy.run/fly";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as machines from "@distilled.cloud/fly-io/machines";
5 import * as Provider from "alchemy/Provider";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Redacted from "effect/Redacted";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11 import RedisApi, { Cache, REDIS_VALUE, RedisIp, RedisSite } from "./fixtures/redis-api.ts";
12
13 const { test } = Test.make({ providers: Fly.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 const waitUntilRedisGone = (redisId: string, name: string) =>
18 Fly.findRedisAddOn({ id: redisId, name }).pipe(
19 Effect.map((row) => (row === undefined ? ("gone" as const) : ("found" as const))),
20 Effect.repeat({
21 schedule: Schedule.spaced("2 seconds"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27 const waitUntilAppGone = (appName: string) =>
28 machines.getApp({ app_name: appName }).pipe(
29 Effect.as("found" as const),
30 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
31 Effect.repeat({
32 schedule: Schedule.spaced("1 second"),
33 until: (status) => status === "gone",
34 times: 10,
35 }),
36 );
37
38 test.provider(
39 "lists cheapest redis plan",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43 const plans = yield* Fly.listRedisPlans();
44 expect(plans.length).toBeGreaterThan(0);
45 const cheapest = plans
46 .slice()
47 .sort((left, right) => (left.pricePerMonth ?? 0) - (right.pricePerMonth ?? 0))[0];
48 expect(cheapest?.id).toEqual(expect.any(String));
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 { tags: ["provider:fly", "provider:fly:redis", "live"], timeout: 90_000 },
52 );
53
54 test.provider(
55 "create, update eviction, and delete redis",
56 (stack) =>
57 Effect.gen(function* () {
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 return yield* Fly.Redis("Cache");
63 }),
64 );
65
66 expect(created.redisId).toEqual(expect.any(String));
67 expect(created.redisId.length).toBeGreaterThan(0);
68 expect(created.name).toEqual(expect.any(String));
69 expect(created.name.length).toBeGreaterThan(0);
70 expect(created.primaryRegion).toEqual("iad");
71 expect(created.planName).toEqual(expect.any(String));
72 expect(Redacted.isRedacted(created.url)).toEqual(true);
73
74 const fetched = yield* Fly.findRedisAddOn({
75 id: created.redisId,
76 name: created.name,
77 });
78 expect(fetched?.id).toEqual(created.redisId);
79 expect(fetched?.name).toEqual(created.name);
80 expect(fetched?.primaryRegion).toEqual("iad");
81
82 const provider = yield* Provider.findProvider(Fly.Redis);
83 const listed = yield* provider.list();
84 const found = listed.find((row) => row.redisId === created.redisId);
85 expect(found).toBeDefined();
86 expect(found?.name).toEqual(created.name);
87
88 const updated = yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* Fly.Redis("Cache", {
91 eviction: true,
92 });
93 }),
94 );
95
96 expect(updated.redisId).toEqual(created.redisId);
97 expect(updated.name).toEqual(created.name);
98 expect(updated.eviction).toEqual(true);
99 expect(Redacted.isRedacted(updated.url)).toEqual(true);
100
101 const refetched = yield* Fly.findRedisAddOn({
102 id: updated.redisId,
103 name: updated.name,
104 });
105 expect(refetched?.id).toEqual(updated.redisId);
106 const options = refetched?.options as { eviction?: boolean } | null;
107 expect(options?.eviction).toEqual(true);
108
109 yield* stack.destroy();
110
111 const gone = yield* waitUntilRedisGone(created.redisId, created.name);
112 expect(gone).toEqual("gone");
113 }).pipe(logLevel),
114 { tags: ["provider:fly", "provider:fly:redis", "live"], timeout: 120_000 },
115 );
116
117 test.provider(
118 "replace when name changes",
119 (stack) =>
120 Effect.gen(function* () {
121 yield* stack.destroy();
122
123 const created = yield* stack.deploy(
124 Effect.gen(function* () {
125 return yield* Fly.Redis("ReplaceName");
126 }),
127 );
128
129 const nextName = created.name.slice(0, -1) + (created.name.endsWith("z") ? "y" : "z");
130
131 const replaced = yield* stack.deploy(
132 Effect.gen(function* () {
133 return yield* Fly.Redis("ReplaceName", {
134 name: nextName,
135 });
136 }),
137 );
138
139 expect(replaced.name).toEqual(nextName);
140 expect(replaced.redisId).not.toEqual(created.redisId);
141
142 const oldGone = yield* waitUntilRedisGone(created.redisId, created.name);
143 expect(oldGone).toEqual("gone");
144
145 const fetched = yield* Fly.findRedisAddOn({
146 id: replaced.redisId,
147 name: replaced.name,
148 });
149 expect(fetched?.name).toEqual(nextName);
150
151 yield* stack.destroy();
152
153 const gone = yield* waitUntilRedisGone(replaced.redisId, replaced.name);
154 expect(gone).toEqual("gone");
155 }).pipe(logLevel),
156 { tags: ["provider:fly", "provider:fly:redis", "live"], timeout: 120_000 },
157 );
158
159 test.provider(
160 "replace when primaryRegion changes",
161 (stack) =>
162 Effect.gen(function* () {
163 yield* stack.destroy();
164
165 const created = yield* stack.deploy(
166 Effect.gen(function* () {
167 return yield* Fly.Redis("ReplaceRegion", {
168 primaryRegion: "iad",
169 });
170 }),
171 );
172
173 expect(created.primaryRegion).toEqual("iad");
174
175 const replaced = yield* stack.deploy(
176 Effect.gen(function* () {
177 return yield* Fly.Redis("ReplaceRegion", {
178 primaryRegion: "ewr",
179 });
180 }),
181 );
182
183 expect(replaced.primaryRegion).toEqual("ewr");
184 expect(replaced.redisId).not.toEqual(created.redisId);
185
186 const fetched = yield* Fly.findRedisAddOn({
187 id: replaced.redisId,
188 name: replaced.name,
189 });
190 expect(fetched?.primaryRegion).toEqual("ewr");
191
192 yield* stack.destroy();
193
194 const gone = yield* waitUntilRedisGone(replaced.redisId, replaced.name);
195 expect(gone).toEqual("gone");
196 }).pipe(logLevel),
197 { tags: ["provider:fly", "provider:fly:redis", "live"], timeout: 120_000 },
198 );
199
200 test.provider(
201 "a Service pings Redis and round-trips a key",
202 (stack) =>
203 Effect.gen(function* () {
204 yield* stack.destroy();
205
206 const deployed = yield* stack.deploy(
207 Effect.gen(function* () {
208 const app = yield* RedisSite;
209 const cache = yield* Cache;
210 const ip = yield* RedisIp;
211 const api = yield* RedisApi;
212 return { app, cache, ip, api };
213 }),
214 );
215
216 expect(deployed.cache.redisId).toEqual(expect.any(String));
217 expect(deployed.api.url).toEqual(`https://${deployed.app.appName}.fly.dev`);
218
219 const secrets = yield* machines.listSecrets({
220 app_name: deployed.app.appName,
221 show_secrets: false,
222 });
223 const redisUrl = (secrets.secrets ?? []).find((secret) => secret.name === Fly.REDIS_URL_ENV);
224 expect(redisUrl).toBeDefined();
225 expect(redisUrl?.digest).toEqual(expect.any(String));
226
227 const untilOk = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
228 effect.pipe(
229 Effect.retry({
230 schedule: Schedule.spaced("4 seconds"),
231 times: 10,
232 }),
233 );
234
235 // A just-created Redis can refuse connections for a few seconds.
236 const ping = yield* untilOk(
237 HttpClient.get(`${deployed.api.url}/`).pipe(
238 Effect.flatMap((res) =>
239 res.status === 200 ? res.json : Effect.fail(new Error(`api returned ${res.status}`)),
240 ),
241 Effect.map((value) => value as { pong: boolean; hasOutputUrl: boolean; error?: string }),
242 Effect.flatMap((value) =>
243 value.pong
244 ? Effect.succeed(value)
245 : Effect.fail(new Error(`ping failed: ${value.error}`)),
246 ),
247 ),
248 );
249 expect(ping.pong).toEqual(true);
250 expect(ping.hasOutputUrl).toEqual(true);
251
252 const written = yield* untilOk(
253 HttpClient.get(`${deployed.api.url}/set`).pipe(
254 Effect.flatMap((res) =>
255 res.status === 200 ? res.json : Effect.fail(new Error(`api returned ${res.status}`)),
256 ),
257 Effect.map((value) => value as { ok: boolean }),
258 ),
259 );
260 expect(written.ok).toEqual(true);
261
262 const read = yield* untilOk(
263 HttpClient.get(`${deployed.api.url}/get`).pipe(
264 Effect.flatMap((res) =>
265 res.status === 200 ? res.json : Effect.fail(new Error(`api returned ${res.status}`)),
266 ),
267 Effect.map((value) => value as { ok: boolean; value: string }),
268 ),
269 );
270 expect(read.ok).toEqual(true);
271 expect(read.value).toEqual(REDIS_VALUE);
272
273 yield* stack.destroy();
274
275 const redisGone = yield* waitUntilRedisGone(deployed.cache.redisId, deployed.cache.name);
276 expect(redisGone).toEqual("gone");
277 const appGone = yield* waitUntilAppGone(deployed.app.appName);
278 expect(appGone).toEqual("gone");
279 }).pipe(logLevel),
280 {
281 tags: [
282 "provider:fly",
283 "provider:fly:app",
284 "provider:fly:ipassignment",
285 "provider:fly:machine",
286 "provider:fly:redis",
287 "provider:fly:service",
288 "live",
289 ],
290 timeout: 180_000,
291 },
292 );
293
294 test.provider(
295 "create redis on a fixed plan",
296 (stack) =>
297 Effect.gen(function* () {
298 yield* stack.destroy();
299
300 const plans = yield* Fly.listRedisPlans();
301 const fixed = plans.find((plan) => Fly.isFixedRedisPlan(plan));
302 expect(fixed).toBeDefined();
303
304 const created = yield* stack.deploy(
305 Effect.gen(function* () {
306 return yield* Fly.Redis("FixedCache", {
307 plan: fixed!.id,
308 });
309 }),
310 );
311
312 expect(created.planId).toEqual(fixed!.id);
313
314 yield* stack.destroy();
315
316 const gone = yield* waitUntilRedisGone(created.redisId, created.name);
317 expect(gone).toEqual("gone");
318 }).pipe(logLevel),
319 { tags: ["provider:fly", "provider:fly:redis", "live"], timeout: 120_000 },
320 );