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

main
ts 115 lines 3.53 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 /**
5 * LIVE counterpart to test/dev.test.ts: deploys the exact same orders app
6 * to real AWS (Test.make + deploy, no CLI) — VPC, shared ALB, Fargate
7 * services, DynamoDB table, one-shot tasks — and drives the same HTTP
8 * surface against the real load balancer.
9 *
10 * Requires AWS credentials and docker (the Api/SeedTask/Report images are
11 * built locally and pushed to ECR). The first deploy provisions an ALB and
12 * two Fargate services (~5-10 minutes).
13 */
14 import * as Alchemy from "alchemy";
15 import * as Effect from "effect/Effect";
16 import * as HttpClient from "effect/http/HttpClient";
17 import * as Schedule from "effect/Schedule";
18 import Stack from "../alchemy.run.ts";
19
20 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
21 providers: AWS.providers(),
22 state: Alchemy.localState(),
23 });
24
25 // The first deploy builds + pushes container images and waits for two
26 // Fargate services to stabilize behind the ALB.
27 const stack = beforeAll(deploy(Stack), { timeout: 1_800_000 });
28 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
29 timeout: 1_800_000,
30 });
31
32 type Outputs = {
33 url: string;
34 apiUrl: string;
35 seedUrl: string;
36 tableName: string;
37 };
38
39 const outputs = Effect.map(stack, (out) => out as unknown as Outputs);
40
41 /** GET with retries — fresh ALB targets take a moment to pass health checks. */
42 const getJson = Effect.fn(function* (url: string) {
43 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
44 const res = yield* client.get(url).pipe(
45 Effect.retry({
46 schedule: Schedule.min([Schedule.exponential("1 second"), Schedule.spaced("5 seconds")]),
47 times: 30,
48 }),
49 );
50 return yield* res.json;
51 });
52
53 test(
54 "the Api service serves /api/orders through the shared ALB",
55 Effect.gen(function* () {
56 const { apiUrl } = yield* outputs;
57 const body = (yield* getJson(apiUrl)) as {
58 count: number;
59 orders: unknown[];
60 };
61 expect(body.count).toBeNumber();
62 }),
63 { timeout: 300_000 },
64 );
65
66 test(
67 "the Web service serves the catch-all route",
68 Effect.gen(function* () {
69 const { url } = yield* outputs;
70 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
71 const res = yield* client.get(url).pipe(
72 Effect.retry({
73 schedule: Schedule.min([Schedule.exponential("1 second"), Schedule.spaced("5 seconds")]),
74 times: 30,
75 }),
76 );
77 const text = yield* res.text;
78 expect(text).toContain("Server");
79 }),
80 { timeout: 300_000 },
81 );
82
83 test(
84 "POST /api/seed launches the SeedTask and seeds the table",
85 Effect.gen(function* () {
86 const { apiUrl, seedUrl } = yield* outputs;
87 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
88
89 const seedRes = yield* client.post(seedUrl).pipe(
90 Effect.retry({
91 schedule: Schedule.spaced("5 seconds"),
92 times: 12,
93 }),
94 );
95 const seed = (yield* seedRes.json) as {
96 taskArn?: string;
97 failures: unknown[];
98 };
99 expect(seed.failures).toEqual([]);
100 expect(seed.taskArn).toBeString();
101
102 // The one-shot Fargate task must pull its image and run to completion
103 // before the orders appear.
104 const count = yield* getJson(apiUrl).pipe(
105 Effect.map((body) => (body as { count: number }).count),
106 Effect.repeat({
107 schedule: Schedule.spaced("5 seconds"),
108 until: (count) => count >= 3,
109 times: 60,
110 }),
111 );
112 expect(count).toBeGreaterThanOrEqual(3);
113 }),
114 { timeout: 600_000 },
115 );