1import * as Alchemy from "alchemy";
2import * as Prisma from "alchemy/Prisma";
3import * as Test from "alchemy/Test/Bun";
4import { expect } from "bun:test";
5import * as Console from "effect/Console";
6import * as Effect from "effect/Effect";
7import * as HttpClient from "effect/http/HttpClient";
8import Stack from "../alchemy.run.ts";
9
10const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
11 providers: Prisma.providers(),
12 state: Alchemy.localState(),
13 profile: process.env.ALCHEMY_PROFILE,
14});
15const stack = beforeAll(
16 destroy(Stack).pipe(Effect.andThen(deploy(Stack)), Effect.tap(Console.log)),
17 { timeout: 120_000 },
18);
19afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { timeout: 120_000 });
20
21test(
22 "serves the frontend and queries real Postgres",
23 Effect.gen(function* () {
24 const { url, apiUrl } = yield* stack;
25 const page = yield* Test.getWhenReady(String(url));
26 expect(yield* page.text).toContain("Prisma database clock");
27 const health = yield* Test.getWhenReady(`${apiUrl}/api/health`);
28 expect(yield* health.json).toEqual({ ok: true });
29 const response = yield* Test.getWhenReady(`${apiUrl}/api/time`);
30 expect(response.headers["access-control-allow-origin"]).toBe("*");
31 const data = (yield* response.json) as { time: string };
32 expect(data.time).toMatch(/^\d{4}-\d{2}-\d{2} /);
33 const client = yield* HttpClient.HttpClient;
34 expect((yield* client.get(`${apiUrl}/missing`)).status).toBe(404);
35 expect((yield* client.post(`${apiUrl}/api/time`)).status).toBe(405);
36 }),
37 { timeout: 120_000 },
38);