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

main
ts 138 lines 3.75 KB
Raw
1 import { expect } from "bun:test";
2 import * as AWS from "@alchemy.run/aws";
3 import * as Test from "@alchemy.run/test/Bun";
4 import * as S3 from "@distilled.cloud/aws/s3";
5 import * as Alchemy from "alchemy";
6 import * as Console from "effect/Console";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
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.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
31 times: 10,
32 }),
33 );
34
35 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
36 profile: process.env.ALCHEMY_PROFILE,
37 providers: AWS.providers(),
38 state: AWS.state(),
39 });
40
41 // Pre-deploy cleanup must not close the suite's shared runtime scope.
42 const stack = beforeAll(
43 Effect.sync(Test.defaultStage).pipe(
44 Effect.flatMap((stage) => Alchemy.destroy({ stack: Stack, stage })),
45 Effect.andThen(deploy(Stack)),
46 Effect.tap(Console.log),
47 ),
48 {
49 timeout: 1_200_000,
50 },
51 );
52 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
53 timeout: 1_200_000,
54 });
55
56 const base = Effect.map(stack, ({ url }) => {
57 if (!url) throw new Error("expected the site to expose a CloudFront url");
58 return String(url).replace(/\/+$/, "");
59 });
60
61 test(
62 "deploys and exposes a url",
63 Effect.gen(function* () {
64 const { url } = yield* stack;
65 expect(url).toBeString();
66 }),
67 { timeout: 180_000 },
68 );
69
70 test(
71 "serves the home page",
72 Effect.gen(function* () {
73 const url = yield* base;
74 const html = yield* getBodyWhenReady(url, "Hello from vinext on AWS!");
75 expect(html).toContain("vinext on AWS");
76 }),
77 { timeout: 180_000 },
78 );
79
80 test(
81 "serves the ISR page",
82 Effect.gen(function* () {
83 const url = yield* base;
84 const html = yield* getBodyWhenReady(`${url}/isr`, "ISR");
85 expect(html).toContain("revalidate 60s");
86 }),
87 { timeout: 180_000 },
88 );
89
90 test.provider(
91 "persists ISR entries in the S3 cache",
92 () =>
93 Effect.gen(function* () {
94 const { cacheBucketName } = yield* stack;
95 if (!cacheBucketName) {
96 throw new Error("expected the site to expose its ISR cache bucket");
97 }
98 const url = yield* base;
99 yield* getBodyWhenReady(`${url}/isr`, "ISR");
100 const objects = yield* S3.listObjectsV2({
101 Bucket: cacheBucketName,
102 Prefix: "vinext-cache/",
103 }).pipe(
104 Effect.repeat({
105 schedule: Schedule.spaced("1 second"),
106 times: 10,
107 until: (result) => (result.Contents?.length ?? 0) > 0,
108 }),
109 );
110 expect(objects.Contents?.some((object) => object.Key?.includes("isr"))).toBe(true);
111 }),
112 { timeout: 60_000 },
113 );
114
115 test(
116 "serves the dynamic API route",
117 Effect.gen(function* () {
118 const url = yield* base;
119 const res = yield* getWhenReady(`${url}/api/hello?name=Alchemy`);
120 expect(res.status).toBe(200);
121 const body = yield* res.json;
122 expect(body).toEqual({
123 name: "Alchemy",
124 greeting: "Hello from vinext on AWS!",
125 });
126 }),
127 { timeout: 180_000 },
128 );
129
130 test(
131 "serves a static asset from public/",
132 Effect.gen(function* () {
133 const url = yield* base;
134 const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *");
135 expect(body).toContain("User-agent: *");
136 }),
137 { timeout: 180_000 },
138 );