| 1 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 2 | import * as Test from "alchemy/Test/Bun"; |
| 3 | import { expect } from "bun:test"; |
| 4 | import * as Console from "effect/Console"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import Stack from "../alchemy.run.ts"; |
| 9 | |
| 10 | // Fresh `workers.dev` URLs transiently 404 while the route propagates. |
| 11 | // `Test.getWhenReady` fails on that cold-start window and retries until the |
| 12 | // worker serves a real response. |
| 13 | const { getWhenReady } = Test; |
| 14 | |
| 15 | class AssetNotReady extends Data.TaggedError("AssetNotReady")<{ |
| 16 | body: string; |
| 17 | }> {} |
| 18 | |
| 19 | // While the static-asset manifest is still propagating, Cloudflare can serve |
| 20 | // placeholder content with a 200 — the status alone can't distinguish "not |
| 21 | // yet" from "served", so retry until the body matches. |
| 22 | const getBodyWhenReady = (url: string, expected: string) => |
| 23 | Effect.gen(function* () { |
| 24 | const res = yield* getWhenReady(url); |
| 25 | expect(res.status).toBe(200); |
| 26 | const body = yield* res.text; |
| 27 | if (!body.includes(expected)) { |
| 28 | return yield* Effect.fail(new AssetNotReady({ body })); |
| 29 | } |
| 30 | return body; |
| 31 | }).pipe( |
| 32 | Effect.retry({ |
| 33 | while: (error) => error instanceof AssetNotReady, |
| 34 | schedule: Schedule.max([ |
| 35 | Schedule.min([ |
| 36 | Schedule.exponential("500 millis"), |
| 37 | Schedule.spaced("3 seconds"), |
| 38 | ]), |
| 39 | Schedule.recurs(20), |
| 40 | ]), |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 45 | providers: Cloudflare.providers(), |
| 46 | state: Cloudflare.state(), |
| 47 | }); |
| 48 | |
| 49 | // The first deploy runs the full Vite build, so give the hook more headroom |
| 50 | // than the default 120s. |
| 51 | const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), { |
| 52 | timeout: 600_000, |
| 53 | }); |
| 54 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 55 | |
| 56 | const base = Effect.map(stack, ({ url }) => { |
| 57 | if (!url) throw new Error("expected the site to expose a workers.dev url"); |
| 58 | return url.replace(/\/+$/, ""); |
| 59 | }); |
| 60 | |
| 61 | test( |
| 62 | "deploys and exposes a url", |
| 63 | Effect.gen(function* () { |
| 64 | const { url } = yield* stack; |
| 65 | expect(url).toBeString(); |
| 66 | }), |
| 67 | { timeout: 180_000 }, |
| 68 | ); |
| 69 | |
| 70 | test( |
| 71 | "serves the SPA index", |
| 72 | Effect.gen(function* () { |
| 73 | const url = yield* base; |
| 74 | // The Vue app is a client-rendered SPA: the served HTML is the built |
| 75 | // index.html with the app mount point and hashed asset links. |
| 76 | const html = yield* getBodyWhenReady(url, '<div id="app">'); |
| 77 | expect(html).toContain('<div id="app">'); |
| 78 | }), |
| 79 | { timeout: 180_000 }, |
| 80 | ); |
| 81 | |
| 82 | test( |
| 83 | "compiles tailwind from vite.config.ts", |
| 84 | Effect.gen(function* () { |
| 85 | const url = yield* base; |
| 86 | const html = yield* getBodyWhenReady(url, '<div id="app">'); |
| 87 | |
| 88 | // In an SPA the utility classes live in the JS bundle, not the HTML, so |
| 89 | // the proof that the @tailwindcss/vite plugin from the project's own |
| 90 | // vite.config.ts ran is the compiled rule in the linked stylesheet. |
| 91 | const link = html.match( |
| 92 | /<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/, |
| 93 | ); |
| 94 | expect(link).not.toBeNull(); |
| 95 | const href = link![1]!; |
| 96 | const cssUrl = href.startsWith("http") |
| 97 | ? href |
| 98 | : `${url}${href.startsWith("/") ? "" : "/"}${href}`; |
| 99 | const css = yield* getBodyWhenReady(cssUrl, ".text-3xl"); |
| 100 | expect(css).toContain(".text-3xl"); |
| 101 | }), |
| 102 | { timeout: 180_000 }, |
| 103 | ); |