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