1import * as Alchemy from "alchemy";
2import * as Fly from "alchemy/Fly";
3import * as Test from "alchemy/Test/Bun";
4import { expect } from "bun:test";
5import * as Effect from "effect/Effect";
6import * as Schedule from "effect/Schedule";
7import * as HttpClient from "effect/http/HttpClient";
8import Stack from "../alchemy.run.ts";
9import { SECRET_NAME } from "../src/shared.ts";
10
11const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
12 providers: Fly.providers(),
13 state: Alchemy.localState(),
14 profile: process.env.ALCHEMY_PROFILE,
15});
16
17const fetchOk = (url: string) =>
18 Effect.gen(function* () {
19 const client = yield* HttpClient.HttpClient;
20 return yield* client.get(url).pipe(
21 Effect.flatMap((res) =>
22 res.status === 200
23 ? Effect.succeed(res)
24 : Effect.fail(new Error(`HTTP ${res.status}`)),
25 ),
26 Effect.retry({
27 schedule: Schedule.exponential("500 millis"),
28 times: 20,
29 }),
30 );
31 });
32
33const stack = beforeAll(deploy(Stack), { timeout: 300_000 });
34
35afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
36 timeout: 180_000,
37});
38
39test(
40 "deploys Api + Worker and serves /health over fly.dev",
41 Effect.gen(function* () {
42 const out = yield* stack;
43 expect(out.appName).toBeString();
44 expect(out.apiUrl).toBe(`https://${out.appName}.fly.dev`);
45 expect(out.ip).toBeString();
46 expect(out.secretName).toBe(SECRET_NAME);
47 expect(out.workerMounts[0]?.volumeId).toBeString();
48 expect(out.apiMachineId).not.toBe(out.workerMachineId);
49
50 const health = yield* fetchOk(`${out.apiUrl}/health`);
51 expect(health.status).toBe(200);
52 const body = (yield* health.json) as { ok: boolean; name: string };
53 expect(body.ok).toBe(true);
54 expect(body.name).toBe(SECRET_NAME);
55 }),
56 { timeout: 180_000 },
57);
58
59test(
60 "Fly.Secret is injected as env and present on /secret",
61 Effect.gen(function* () {
62 const out = yield* stack;
63 const response = yield* fetchOk(`${out.apiUrl}/secret`);
64 expect(response.status).toBe(200);
65 const body = (yield* response.json) as { ok: boolean; name: string };
66 expect(body.ok).toBe(true);
67 expect(body.name).toBe(SECRET_NAME);
68 }),
69 { timeout: 60_000 },
70);