@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, 04:45 UTC

main
ts 196 lines 6.34 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 Alchemy from "alchemy";
6 import { Stack as StackService } from "alchemy/Stack";
7 import { Stage } from "alchemy/Stage";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as HttpClient from "effect/http/HttpClient";
11 import * as Layer from "effect/Layer";
12 import { MinimumLogLevel } from "effect/References";
13 import * as Schedule from "effect/Schedule";
14 import Api from "./fixtures/app/api.ts";
15 import { Marker, PublicIp, SECRET_NAME, Site, VOLUME_PATH } from "./fixtures/app/shared.ts";
16 import Worker from "./fixtures/app/worker.ts";
17
18 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
19 providers: Fly.providers(),
20 });
21
22 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
23
24 // Out-of-band verification resolves Fly credentials the same way the stack
25 // does — through the Alchemy profile via `Fly.providers()` — not from
26 // `FLY_API_TOKEN`, which a laptop running off a profile does not have.
27 const distilled = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
28 effect.pipe(
29 Effect.provide(
30 Fly.providers().pipe(
31 Layer.provideMerge(
32 Layer.succeed(StackService, {
33 name: "FlyAppFixtureVerify",
34 stage: "test",
35 resources: {},
36 bindings: {},
37 actions: {},
38 }),
39 ),
40 Layer.provideMerge(Layer.succeed(Stage, "test")),
41 ),
42 ),
43 );
44
45 class ApiNotReady extends Data.TaggedError("ApiNotReady")<{
46 status: number;
47 }> {}
48
49 const Stack = Alchemy.Stack(
50 "FlyAppFixture",
51 {
52 providers: Fly.providers(),
53 state: Alchemy.localState(),
54 },
55 Effect.gen(function* () {
56 const site = yield* Site;
57 const secret = yield* Marker;
58 const ip = yield* PublicIp;
59 const worker = yield* Worker;
60 const api = yield* Api;
61 return {
62 appName: site.appName,
63 appId: site.appId,
64 appUrl: site.url,
65 workerMounts: worker.mounts,
66 secretName: secret.name,
67 ip: ip.ip,
68 workerMachineId: worker.machineId,
69 workerName: worker.name,
70 workerState: worker.state,
71 apiMachineId: api.machineId,
72 apiName: api.name,
73 apiState: api.state,
74 apiUrl: api.url,
75 apiRegion: api.region,
76 };
77 }),
78 );
79
80 const stack = beforeAll(deploy(Stack), { timeout: 180_000 });
81
82 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
83 timeout: 120_000,
84 });
85
86 test(
87 "deploys an effectful app and serves over HTTP",
88 Effect.gen(function* () {
89 const out = yield* stack;
90
91 expect(out.appName).toEqual(expect.any(String));
92 expect(out.appName).toMatch(/^[a-z][a-z0-9-]*$/);
93 expect(out.appName.length).toBeLessThanOrEqual(30);
94 expect(out.appId).toEqual(expect.any(String));
95 expect(out.workerMounts[0]?.volumeId).toEqual(expect.any(String));
96 expect(out.secretName).toEqual(SECRET_NAME);
97 expect(out.ip).toEqual(expect.any(String));
98 expect(out.workerMachineId).toEqual(expect.any(String));
99 expect(out.apiMachineId).toEqual(expect.any(String));
100 expect(out.apiMachineId).not.toEqual(out.workerMachineId);
101 expect(out.apiName).not.toEqual(out.workerName);
102 expect(out.workerState).toEqual("started");
103 expect(out.apiState).toEqual("started");
104 expect(out.apiRegion).toEqual("iad");
105 expect(out.apiUrl).toEqual(`https://${out.appName}.fly.dev`);
106
107 const liveApp = yield* distilled(
108 machines.getApp({
109 app_name: out.appName,
110 }),
111 );
112 expect(liveApp.name).toEqual(out.appName);
113 expect(liveApp.id).toEqual(out.appId);
114
115 const liveApi = yield* distilled(
116 machines.getMachine({
117 app_name: out.appName,
118 machine_id: out.apiMachineId,
119 }),
120 );
121 expect(liveApi.id).toEqual(out.apiMachineId);
122 expect(liveApi.state).toEqual("started");
123 expect(liveApi.config?.metadata?.["alchemy.type"]).toEqual("Fly.Service");
124 expect(liveApi.config?.guest?.cpus).toEqual(1);
125 expect(liveApi.config?.guest?.memory_mb).toEqual(256);
126
127 const liveWorker = yield* distilled(
128 machines.getMachine({
129 app_name: out.appName,
130 machine_id: out.workerMachineId,
131 }),
132 );
133 expect(liveWorker.id).toEqual(out.workerMachineId);
134 expect(liveWorker.state).toEqual("started");
135 expect(liveWorker.config?.metadata?.["alchemy.type"]).toEqual("Fly.Service");
136 expect(liveWorker.config?.mounts?.[0]?.path).toEqual(VOLUME_PATH);
137 expect(liveWorker.config?.mounts?.[0]?.volume).toEqual(out.workerMounts[0]?.volumeId);
138
139 const liveVolume = yield* distilled(
140 machines.getVolumeById({
141 app_name: out.appName,
142 volume_id: out.workerMounts[0]!.volumeId,
143 }),
144 );
145 expect(liveVolume.id).toEqual(out.workerMounts[0]?.volumeId);
146 expect(liveVolume.attached_machine_id).toEqual(out.workerMachineId);
147
148 const liveSecret = yield* distilled(
149 machines.getSecret({
150 app_name: out.appName,
151 secret_name: out.secretName,
152 show_secrets: false,
153 }),
154 );
155 expect(liveSecret.name).toEqual(SECRET_NAME);
156 expect(liveSecret.value).toBeUndefined();
157
158 const client = yield* HttpClient.HttpClient;
159 const url = out.apiUrl ?? `https://${out.appName}.fly.dev`;
160
161 const body = yield* client.get(`${url}/health`).pipe(
162 Effect.timeoutOrElse({
163 duration: "8 seconds",
164 orElse: () => Effect.fail(new ApiNotReady({ status: 0 })),
165 }),
166 Effect.flatMap((res) =>
167 res.status === 200
168 ? res.json.pipe(Effect.mapError(() => new ApiNotReady({ status: res.status })))
169 : Effect.fail(new ApiNotReady({ status: res.status })),
170 ),
171 Effect.retry({
172 while: (e) => e._tag === "ApiNotReady",
173 schedule: Schedule.exponential("500 millis").pipe(
174 Schedule.upTo({ duration: "45 seconds" }),
175 ),
176 times: 10,
177 }),
178 Effect.map((value) => value as { ok: boolean; name: string }),
179 );
180 expect(body.ok).toEqual(true);
181 expect(body.name).toEqual(SECRET_NAME);
182 }).pipe(logLevel),
183 {
184 tags: [
185 "provider:fly",
186 "provider:fly:app",
187 "provider:fly:ipassignment",
188 "provider:fly:machine",
189 "provider:fly:secret",
190 "provider:fly:service",
191 "provider:fly:volume",
192 "live",
193 ],
194 timeout: 120_000,
195 },
196 );