| 1 | import * as Alchemy from "alchemy"; |
| 2 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 3 | import * as Test from "alchemy/Test/Bun"; |
| 4 | import { expect } from "bun:test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import { frameworks } from "../frameworks.ts"; |
| 8 | import Stack from "../alchemy.run.ts"; |
| 9 | |
| 10 | const stage = "dev-service-bindings"; |
| 11 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 12 | providers: Cloudflare.providers(), |
| 13 | profile: process.env.ALCHEMY_PROFILE, |
| 14 | stage, |
| 15 | dev: true, |
| 16 | }); |
| 17 | |
| 18 | const stack = beforeAll( |
| 19 | Alchemy.destroy({ stack: Stack, stage }).pipe(Effect.andThen(deploy(Stack))), |
| 20 | { timeout: 120_000 }, |
| 21 | ); |
| 22 | afterAll(destroy(Stack)); |
| 23 | |
| 24 | for (const { name, marker } of frameworks) { |
| 25 | test( |
| 26 | `${name}: the local Website serves its page directly`, |
| 27 | Effect.gen(function* () { |
| 28 | const { websiteUrl } = (yield* stack)[name]!; |
| 29 | expect(websiteUrl).toMatch(/^http:\/\/(localhost|127\.0\.0\.1):\d+/); |
| 30 | const response = yield* Test.getWhenReady(String(websiteUrl), { |
| 31 | times: 3, |
| 32 | }); |
| 33 | expect(response.status).toBe(200); |
| 34 | expect(yield* response.text).toContain(marker); |
| 35 | }).pipe(Effect.timeout("30 seconds")), |
| 36 | { timeout: 35_000 }, |
| 37 | ); |
| 38 | test( |
| 39 | `${name}: the Gateway reaches the Website through its service binding`, |
| 40 | Effect.gen(function* () { |
| 41 | const { gatewayUrl } = (yield* stack)[name]!; |
| 42 | const response = yield* HttpClient.get(String(gatewayUrl)); |
| 43 | const body = yield* response.text; |
| 44 | expect(response.status, body).toBe(200); |
| 45 | expect(body).toContain(marker); |
| 46 | }).pipe(Effect.timeout("10 seconds")), |
| 47 | { timeout: 15_000 }, |
| 48 | ); |
| 49 | } |