| 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 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 * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import Stack from "../alchemy.run.ts"; |
| 11 | |
| 12 | const { getWhenReady, executeWhenReady } = Test; |
| 13 | |
| 14 | class AssetNotReady extends Data.TaggedError("AssetNotReady")<{ |
| 15 | body: string; |
| 16 | }> {} |
| 17 | |
| 18 | const getBodyWhenReady = (url: string, expected: string) => |
| 19 | Effect.gen(function* () { |
| 20 | const res = yield* getWhenReady(url); |
| 21 | expect(res.status).toBe(200); |
| 22 | const body = yield* res.text; |
| 23 | if (!body.includes(expected)) { |
| 24 | return yield* Effect.fail(new AssetNotReady({ body })); |
| 25 | } |
| 26 | return body; |
| 27 | }).pipe( |
| 28 | Effect.retry({ |
| 29 | while: (error) => error instanceof AssetNotReady, |
| 30 | schedule: Schedule.min([ |
| 31 | Schedule.exponential("500 millis"), |
| 32 | Schedule.spaced("5 seconds"), |
| 33 | ]), |
| 34 | times: 10, |
| 35 | }), |
| 36 | ); |
| 37 | |
| 38 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 39 | profile: process.env.ALCHEMY_PROFILE, |
| 40 | providers: Cloudflare.providers(), |
| 41 | state: Cloudflare.state(), |
| 42 | }); |
| 43 | |
| 44 | // Pre-deploy cleanup must not close the suite's shared runtime scope. |
| 45 | const stack = beforeAll( |
| 46 | Effect.sync(Test.defaultStage).pipe( |
| 47 | Effect.flatMap((stage) => Alchemy.destroy({ stack: Stack, stage })), |
| 48 | Effect.andThen(deploy(Stack)), |
| 49 | Effect.tap(Console.log), |
| 50 | ), |
| 51 | { |
| 52 | timeout: 600_000, |
| 53 | }, |
| 54 | ); |
| 55 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 56 | |
| 57 | const base = Effect.map(stack, ({ url }) => { |
| 58 | if (!url) throw new Error("expected the site to expose a workers.dev url"); |
| 59 | return String(url).replace(/\/+$/, ""); |
| 60 | }); |
| 61 | |
| 62 | test( |
| 63 | "deploys and exposes a url", |
| 64 | Effect.gen(function* () { |
| 65 | const { url } = yield* stack; |
| 66 | expect(url).toBeString(); |
| 67 | }), |
| 68 | { timeout: 180_000 }, |
| 69 | ); |
| 70 | |
| 71 | test( |
| 72 | "serves the server-rendered home page", |
| 73 | Effect.gen(function* () { |
| 74 | const url = yield* base; |
| 75 | const html = yield* getBodyWhenReady( |
| 76 | url, |
| 77 | "Hello from vinext on Cloudflare!", |
| 78 | ); |
| 79 | expect(html).toContain('data-testid="increment"'); |
| 80 | }), |
| 81 | { timeout: 180_000 }, |
| 82 | ); |
| 83 | |
| 84 | test( |
| 85 | "serves the static page", |
| 86 | Effect.gen(function* () { |
| 87 | const url = yield* base; |
| 88 | const html = yield* getBodyWhenReady( |
| 89 | `${url}/static`, |
| 90 | "Prerendered at deploy", |
| 91 | ); |
| 92 | expect(html).toContain("This page is statically generated."); |
| 93 | }), |
| 94 | { timeout: 180_000 }, |
| 95 | ); |
| 96 | |
| 97 | test( |
| 98 | "serves the cached ISR page", |
| 99 | Effect.gen(function* () { |
| 100 | const url = yield* base; |
| 101 | const html = yield* getBodyWhenReady(`${url}/isr`, "revalidate=30"); |
| 102 | expect(html).toContain('data-testid="isr-time"'); |
| 103 | expect(html).toContain("Refresh cache now"); |
| 104 | |
| 105 | const stamp = (body: string) => { |
| 106 | const match = body.match( |
| 107 | /data-testid="isr-time"[^>]*>[\s\S]*?<time dateTime="([^"]+)"/, |
| 108 | ); |
| 109 | return match?.[1]?.trim(); |
| 110 | }; |
| 111 | const first = stamp(html); |
| 112 | expect(first).toBeTruthy(); |
| 113 | |
| 114 | // Second fetch should reuse the seeded / KV-backed ISR entry. |
| 115 | const again = yield* getBodyWhenReady(`${url}/isr`, "revalidate=30"); |
| 116 | expect(stamp(again)).toBe(first); |
| 117 | }), |
| 118 | { timeout: 180_000 }, |
| 119 | ); |
| 120 | |
| 121 | test( |
| 122 | "serves the use-cache page", |
| 123 | Effect.gen(function* () { |
| 124 | const url = yield* base; |
| 125 | const html = yield* getBodyWhenReady(`${url}/use-cache`, "use cache"); |
| 126 | expect(html).toContain('data-testid="use-cache-cached"'); |
| 127 | }), |
| 128 | { timeout: 180_000 }, |
| 129 | ); |
| 130 | |
| 131 | test( |
| 132 | "serves the notes page", |
| 133 | Effect.gen(function* () { |
| 134 | const url = yield* base; |
| 135 | const html = yield* getBodyWhenReady(`${url}/notes`, "Notes"); |
| 136 | expect(html).toContain("Add"); |
| 137 | }), |
| 138 | { timeout: 180_000 }, |
| 139 | ); |
| 140 | |
| 141 | test( |
| 142 | "serves the dynamic API route", |
| 143 | Effect.gen(function* () { |
| 144 | const url = yield* base; |
| 145 | const res = yield* getWhenReady(`${url}/api/hello`); |
| 146 | expect(res.status).toBe(200); |
| 147 | const body = (yield* res.json) as { message: string; noteCount: number }; |
| 148 | expect(body.message).toBe("Hello from vinext on Cloudflare!"); |
| 149 | expect(body.noteCount).toBeNumber(); |
| 150 | }), |
| 151 | { timeout: 180_000 }, |
| 152 | ); |
| 153 | |
| 154 | test( |
| 155 | "blocks /admin with proxy", |
| 156 | Effect.gen(function* () { |
| 157 | const url = yield* base; |
| 158 | const res = yield* getWhenReady(`${url}/admin`); |
| 159 | expect(res.status).toBe(403); |
| 160 | const body = yield* res.text; |
| 161 | expect(body).toContain("Blocked by proxy"); |
| 162 | }), |
| 163 | { timeout: 180_000 }, |
| 164 | ); |
| 165 | |
| 166 | test( |
| 167 | "serves a static asset from public/", |
| 168 | Effect.gen(function* () { |
| 169 | const url = yield* base; |
| 170 | const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *"); |
| 171 | expect(body).toContain("User-agent: *"); |
| 172 | }), |
| 173 | { timeout: 180_000 }, |
| 174 | ); |
| 175 | |
| 176 | test( |
| 177 | "writes and lists a D1 note", |
| 178 | Effect.gen(function* () { |
| 179 | const url = yield* base; |
| 180 | const title = "vinext-d1-note"; |
| 181 | const created = yield* executeWhenReady( |
| 182 | HttpClientRequest.post(`${url}/api/notes`).pipe( |
| 183 | HttpClientRequest.bodyJsonUnsafe({ title }), |
| 184 | ), |
| 185 | ); |
| 186 | expect(created.status).toBe(201); |
| 187 | const listed = yield* getWhenReady(`${url}/api/notes`); |
| 188 | expect(listed.status).toBe(200); |
| 189 | const body = (yield* listed.json) as { |
| 190 | notes: Array<{ title: string }>; |
| 191 | }; |
| 192 | const createdBody = (yield* created.json) as { id: string; title: string }; |
| 193 | expect(body.notes.some((note) => note.title === title)).toBe(true); |
| 194 | |
| 195 | const deleted = yield* executeWhenReady( |
| 196 | HttpClientRequest.delete(`${url}/api/notes/${createdBody.id}`), |
| 197 | ); |
| 198 | expect(deleted.status).toBe(204); |
| 199 | const after = (yield* getWhenReady(`${url}/api/notes`).pipe( |
| 200 | Effect.flatMap((res) => res.json), |
| 201 | )) as { notes: Array<{ id: string }> }; |
| 202 | expect(after.notes.some((note) => note.id === createdBody.id)).toBe(false); |
| 203 | const kv = yield* getWhenReady(`${url}/api/kv/note:${createdBody.id}`); |
| 204 | const kvBody = (yield* kv.json) as { value: string | null }; |
| 205 | expect(kvBody.value).toBeNull(); |
| 206 | }), |
| 207 | { timeout: 180_000 }, |
| 208 | ); |
| 209 | |
| 210 | test( |
| 211 | "round-trips a KV value", |
| 212 | Effect.gen(function* () { |
| 213 | const url = yield* base; |
| 214 | const put = yield* executeWhenReady( |
| 215 | HttpClientRequest.put(`${url}/api/kv/ping`).pipe( |
| 216 | HttpClientRequest.bodyText("pong"), |
| 217 | ), |
| 218 | ); |
| 219 | expect(put.status).toBe(201); |
| 220 | const res = yield* getWhenReady(`${url}/api/kv/ping`); |
| 221 | expect(res.status).toBe(200); |
| 222 | const body = (yield* res.json) as { value: string }; |
| 223 | expect(body.value).toBe("pong"); |
| 224 | }), |
| 225 | { timeout: 180_000 }, |
| 226 | ); |