| 1 | import * as Alchemy from "alchemy"; |
| 2 | import * as Prisma from "alchemy/Prisma"; |
| 3 | import * as Test from "alchemy/Test/Bun"; |
| 4 | import { expect } from "bun:test"; |
| 5 | import * as Console from "effect/Console"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import Stack from "../alchemy.run.ts"; |
| 8 | |
| 9 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 10 | providers: Prisma.providers(), |
| 11 | state: Alchemy.localState(), |
| 12 | profile: process.env.ALCHEMY_PROFILE, |
| 13 | }); |
| 14 | |
| 15 | const stack = beforeAll( |
| 16 | destroy(Stack).pipe(Effect.andThen(deploy(Stack)), Effect.tap(Console.log)), |
| 17 | { |
| 18 | timeout: 120_000, |
| 19 | }, |
| 20 | ); |
| 21 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { timeout: 120_000 }); |
| 22 | |
| 23 | const base = Effect.map(stack, ({ url }) => { |
| 24 | if (!url) throw new Error("Expected a deployed website URL"); |
| 25 | return String(url).replace(/\/+$/, ""); |
| 26 | }); |
| 27 | |
| 28 | test( |
| 29 | "serves the application shell", |
| 30 | Effect.gen(function* () { |
| 31 | const url = yield* base; |
| 32 | const response = yield* Test.getWhenReady(url); |
| 33 | expect(response.status).toBe(200); |
| 34 | expect(yield* response.text).toContain("Foldkit on Prisma"); |
| 35 | }), |
| 36 | { timeout: 120_000 }, |
| 37 | ); |
| 38 | |
| 39 | test( |
| 40 | "serves a static asset without falling back to HTML", |
| 41 | Effect.gen(function* () { |
| 42 | const url = yield* base; |
| 43 | const response = yield* Test.getWhenReady(`${url}/example.json`); |
| 44 | expect(response.status).toBe(200); |
| 45 | expect(yield* response.json).toEqual({ |
| 46 | framework: "Foldkit", |
| 47 | greeting: "Hello from Foldkit on Prisma!", |
| 48 | }); |
| 49 | }), |
| 50 | { timeout: 120_000 }, |
| 51 | ); |
| 52 | |
| 53 | test( |
| 54 | "serves the compiled browser entry", |
| 55 | Effect.gen(function* () { |
| 56 | const url = yield* base; |
| 57 | const page = yield* Test.getWhenReady(url); |
| 58 | const html = yield* page.text; |
| 59 | const source = /<script[^>]+src="([^"]+\.js)"/.exec(html)?.[1]; |
| 60 | expect(source).toBeDefined(); |
| 61 | if (!source) throw new Error("Expected a compiled JavaScript entry"); |
| 62 | const script = yield* Test.getWhenReady(new URL(source, url).href); |
| 63 | expect(script.status).toBe(200); |
| 64 | const body = yield* script.text; |
| 65 | expect(body.length).toBeGreaterThan(100); |
| 66 | expect(body.trimStart().startsWith("<!doctype")).toBe(false); |
| 67 | }), |
| 68 | { timeout: 120_000 }, |
| 69 | ); |