| 1 | import * as Alchemy from "alchemy"; |
| 2 | import * as Fly from "alchemy/Fly"; |
| 3 | import * as Test from "alchemy/Test/Bun"; |
| 4 | import { expect } from "bun:test"; |
| 5 | import * as Console from "effect/Console"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import Stack from "../alchemy.run.ts"; |
| 10 | |
| 11 | // A fresh Fly deployment (and the Fly Machine behind it) can serve |
| 12 | // transient 404/5xx responses while it propagates. `Test.getWhenReady` |
| 13 | // fails on that cold-start window and retries until the site serves a |
| 14 | // real response. |
| 15 | const { getWhenReady } = Test; |
| 16 | |
| 17 | class AssetNotReady extends Data.TaggedError("AssetNotReady")<{ |
| 18 | body: string; |
| 19 | }> {} |
| 20 | |
| 21 | // While the asset manifest and Fly edge caches are still |
| 22 | // propagating, a 200 body can be stale — the status alone can't |
| 23 | // distinguish "not yet" from "served", so retry until the body matches. |
| 24 | const getBodyWhenReady = (url: string, expected: string) => |
| 25 | Effect.gen(function* () { |
| 26 | const res = yield* getWhenReady(url); |
| 27 | expect(res.status).toBe(200); |
| 28 | const body = yield* res.text; |
| 29 | if (!body.includes(expected)) { |
| 30 | return yield* Effect.fail(new AssetNotReady({ body })); |
| 31 | } |
| 32 | return body; |
| 33 | }).pipe( |
| 34 | Effect.retry({ |
| 35 | while: (error) => error instanceof AssetNotReady, |
| 36 | schedule: Schedule.max([ |
| 37 | Schedule.min([ |
| 38 | Schedule.exponential("500 millis"), |
| 39 | Schedule.spaced("3 seconds"), |
| 40 | ]), |
| 41 | Schedule.recurs(20), |
| 42 | ]), |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 47 | providers: Fly.providers(), |
| 48 | state: Alchemy.localState(), |
| 49 | profile: process.env.ALCHEMY_PROFILE, |
| 50 | }); |
| 51 | |
| 52 | // The first deploy runs the full Astro build AND creates a Fly |
| 53 | // distribution (~5-10 minutes), so give the hook far more headroom than |
| 54 | // the default 120s. |
| 55 | const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), { |
| 56 | timeout: 600_000, |
| 57 | }); |
| 58 | // Deleting the Fly deployment takes several minutes too. |
| 59 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { |
| 60 | timeout: 600_000, |
| 61 | }); |
| 62 | |
| 63 | const base = Effect.map(stack, ({ url }) => { |
| 64 | if (!url) throw new Error("expected the site to expose a url"); |
| 65 | return String(url).replace(/\/+$/, ""); |
| 66 | }); |
| 67 | |
| 68 | test( |
| 69 | "deploys and exposes a url", |
| 70 | Effect.gen(function* () { |
| 71 | const { url } = yield* stack; |
| 72 | expect(url).toBeString(); |
| 73 | }), |
| 74 | { timeout: 180_000 }, |
| 75 | ); |
| 76 | |
| 77 | test( |
| 78 | "serves the server-rendered home page", |
| 79 | Effect.gen(function* () { |
| 80 | const url = yield* base; |
| 81 | const res = yield* getWhenReady(url); |
| 82 | expect(res.status).toBe(200); |
| 83 | const html = yield* res.text; |
| 84 | // The `GREETING` env value from alchemy.run.ts, read via |
| 85 | // `process.env` in the page frontmatter — proves the Fly Machine |
| 86 | // rendered it at request time. |
| 87 | expect(html).toContain("Hello from Astro on Fly!"); |
| 88 | expect(html).toContain("server-rendered in a Fly Machine"); |
| 89 | }), |
| 90 | { timeout: 180_000 }, |
| 91 | ); |
| 92 | |
| 93 | test( |
| 94 | "serves the prerendered about page", |
| 95 | Effect.gen(function* () { |
| 96 | const url = yield* base; |
| 97 | const res = yield* getWhenReady(`${url}/about/`); |
| 98 | expect(res.status).toBe(200); |
| 99 | }), |
| 100 | { timeout: 180_000 }, |
| 101 | ); |
| 102 | |
| 103 | test( |
| 104 | "compiles tailwind from astro.config.ts", |
| 105 | Effect.gen(function* () { |
| 106 | const url = yield* base; |
| 107 | // The utility class in the markup proves the page shipped with Tailwind |
| 108 | // classes; wait until the deployed HTML includes it. |
| 109 | const html = yield* getBodyWhenReady(url, "text-3xl"); |
| 110 | |
| 111 | // Astro either links an external compiled stylesheet or inlines small |
| 112 | // ones as a <style> block — accept both, but the compiled rule for the |
| 113 | // utility must be served either way. That rule only exists if the |
| 114 | // @tailwindcss/vite plugin from the project's own astro.config.ts ran. |
| 115 | const link = html.match( |
| 116 | /<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/, |
| 117 | ); |
| 118 | if (link) { |
| 119 | const href = link[1]!; |
| 120 | const cssUrl = href.startsWith("http") |
| 121 | ? href |
| 122 | : `${url}${href.startsWith("/") ? "" : "/"}${href}`; |
| 123 | const css = yield* getBodyWhenReady(cssUrl, ".text-3xl"); |
| 124 | expect(css).toContain(".text-3xl"); |
| 125 | } else { |
| 126 | const style = html.match(/<style[^>]*>([\s\S]*?)<\/style>/); |
| 127 | expect(style).not.toBeNull(); |
| 128 | expect(style![1]).toContain(".text-3xl"); |
| 129 | } |
| 130 | }), |
| 131 | { timeout: 180_000 }, |
| 132 | ); |
| 133 | |
| 134 | test( |
| 135 | "serves a static asset from public/", |
| 136 | Effect.gen(function* () { |
| 137 | const url = yield* base; |
| 138 | const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *"); |
| 139 | expect(body).toContain("User-agent: *"); |
| 140 | }), |
| 141 | { timeout: 180_000 }, |
| 142 | ); |