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

main
ts 106 lines 3.27 KB
Raw
1 import { expect } from "bun:test";
2 import * as Fly from "@alchemy.run/fly";
3 import * as Test from "@alchemy.run/test/Bun";
4 import * as Alchemy from "alchemy";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import Stack from "../alchemy.run.ts";
11
12 const { getWhenReady } = Test;
13
14 class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
15 body: string;
16 }> {}
17
18 const getBodyWhenReady = (url: string, expected: string) =>
19 Effect.gen(function* () {
20 const res = yield* getWhenReady(url);
21 expect(res.status).toBe(200);
22 const body = yield* res.text;
23 if (!body.includes(expected)) {
24 return yield* Effect.fail(new AssetNotReady({ body }));
25 }
26 return body;
27 }).pipe(
28 Effect.retry({
29 while: (error) => error instanceof AssetNotReady,
30 schedule: Schedule.max([
31 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
32 Schedule.recurs(20),
33 ]),
34 }),
35 );
36
37 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
38 providers: Fly.providers(),
39 state: Alchemy.localState(),
40 profile: process.env.ALCHEMY_PROFILE,
41 });
42
43 const stack = beforeAll(deploy(Stack), { timeout: 240_000 });
44
45 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
46 timeout: 180_000,
47 });
48
49 const executeOk = (request: HttpClientRequest.HttpClientRequest) =>
50 Effect.gen(function* () {
51 const client = yield* HttpClient.HttpClient;
52 return yield* client.execute(request).pipe(
53 Effect.flatMap((res) =>
54 res.status === 200 || res.status === 201
55 ? Effect.succeed(res)
56 : Effect.fail(new Error(`HTTP ${res.status}`)),
57 ),
58 Effect.retry({
59 schedule: Schedule.spaced("2 seconds"),
60 times: 45,
61 }),
62 );
63 });
64
65 test(
66 "deploys the SPA and the API",
67 Effect.gen(function* () {
68 const { url, apiUrl } = yield* stack;
69 expect(url).toBeString();
70 expect(url).toMatch(/^https:\/\/.+\.fly\.dev$/);
71 expect(apiUrl).toBeString();
72 expect(apiUrl).toMatch(/^https:\/\/.+\.fly\.dev$/);
73 }),
74 { timeout: 120_000 },
75 );
76
77 test(
78 "SPA renders the Notes heading",
79 Effect.gen(function* () {
80 const { url } = yield* stack;
81 if (!url) throw new Error("expected the site to expose a fly.dev url");
82 const html = yield* getBodyWhenReady(String(url).replace(/\/+$/, ""), "Notes — Fly");
83 expect(html).toContain("Notes — Fly");
84 }),
85 { timeout: 120_000 },
86 );
87
88 test(
89 "API persists a note in Postgres",
90 Effect.gen(function* () {
91 const { apiUrl } = yield* stack;
92 if (!apiUrl) throw new Error("expected the API url");
93 const base = String(apiUrl).replace(/\/+$/, "");
94 const marker = `fly-note-${crypto.randomUUID()}`;
95 const created = yield* executeOk(
96 HttpClientRequest.post(`${base}/notes`).pipe(
97 HttpClientRequest.bodyJsonUnsafe({ body: marker }),
98 ),
99 );
100 expect(created.status).toBe(201);
101 const listed = yield* executeOk(HttpClientRequest.get(`${base}/notes`));
102 const payload = (yield* listed.json) as { notes: Array<{ body: string }> };
103 expect(payload.notes.some((note) => note.body === marker)).toBe(true);
104 }),
105 { timeout: 180_000 },
106 );