The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { describe } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Data from "effect/Data"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as pathe from "pathe"; |
| 10 | import { cloneFixture } from "../Utils/Fixture.ts"; |
| 11 | import { expectUrlContains } from "../Utils/Http.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 14 | |
| 15 | const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures/assets-only"); |
| 16 | |
| 17 | class NotFoundAssertionFailed extends Data.TaggedError("NotFoundAssertionFailed")<{ |
| 18 | url: string; |
| 19 | status: number; |
| 20 | bodyExcerpt: string; |
| 21 | }> {} |
| 22 | |
| 23 | /** |
| 24 | * Assert `url` serves the custom 404 page: status 404 with the fixture's |
| 25 | * `404.html` marker in the body. `expectUrlContains` requires `res.ok`, |
| 26 | * so this needs its own retry loop. |
| 27 | */ |
| 28 | const expectCustom404 = (url: string) => |
| 29 | Effect.tryPromise({ |
| 30 | try: async (signal) => { |
| 31 | const res = await fetch(url, { signal, cache: "no-store" }); |
| 32 | const body = await res.text(); |
| 33 | if (res.status !== 404 || !body.includes("alchemy-assets-only-404")) { |
| 34 | throw new NotFoundAssertionFailed({ |
| 35 | url, |
| 36 | status: res.status, |
| 37 | bodyExcerpt: body.slice(0, 240), |
| 38 | }); |
| 39 | } |
| 40 | }, |
| 41 | catch: (e) => |
| 42 | e instanceof NotFoundAssertionFailed |
| 43 | ? e |
| 44 | : new NotFoundAssertionFailed({ |
| 45 | url, |
| 46 | status: 0, |
| 47 | bodyExcerpt: e instanceof Error ? e.message : String(e), |
| 48 | }), |
| 49 | }).pipe(Effect.retry({ schedule: Schedule.exponential("750 millis", 1.5), times: 10 })); |
| 50 | |
| 51 | describe.concurrent( |
| 52 | "Cloudflare.Worker assets-only", |
| 53 | { tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] }, |
| 54 | () => { |
| 55 | test.provider( |
| 56 | "deploys, updates, and converts an assets-only Worker", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | const fs = yield* FileSystem.FileSystem; |
| 60 | const path = yield* Path.Path; |
| 61 | |
| 62 | yield* stack.destroy(); |
| 63 | |
| 64 | const deploy = (props: { |
| 65 | assets: string | { directory: string; notFoundHandling?: "404-page" }; |
| 66 | script?: string; |
| 67 | }) => |
| 68 | stack.deploy( |
| 69 | Effect.gen(function* () { |
| 70 | return yield* Cloudflare.Worker("AssetsOnly", { |
| 71 | ...props, |
| 72 | workersDev: true, |
| 73 | compatibility: { date: "2024-01-01" }, |
| 74 | }); |
| 75 | }), |
| 76 | ); |
| 77 | |
| 78 | // 1. Fresh assets-only deploy: no main, no script — Cloudflare's |
| 79 | // asset layer serves everything, including the custom 404 page. |
| 80 | const worker = yield* deploy({ |
| 81 | assets: { directory: fixtureDir, notFoundHandling: "404-page" }, |
| 82 | }); |
| 83 | const url = worker.url!; |
| 84 | yield* expectUrlContains(`${url}/`, "alchemy-assets-only-index"); |
| 85 | yield* expectCustom404(`${url}/does-not-exist`); |
| 86 | |
| 87 | // 2. Update: editing an asset must redeploy the new content. |
| 88 | const dir = yield* cloneFixture(fixtureDir, { prefix: "alchemy-assets-only-" }); |
| 89 | yield* fs.writeFileString( |
| 90 | path.join(dir, "index.html"), |
| 91 | "<html><body>alchemy-assets-only-index-v2</body></html>", |
| 92 | ); |
| 93 | yield* deploy({ assets: { directory: dir, notFoundHandling: "404-page" } }); |
| 94 | yield* expectUrlContains(`${url}/`, "alchemy-assets-only-index-v2", { |
| 95 | label: "updated asset", |
| 96 | }); |
| 97 | |
| 98 | // 3. Convert to a script Worker: unmatched routes now invoke the |
| 99 | // script instead of the asset layer's 404 handling. |
| 100 | yield* deploy({ |
| 101 | assets: dir, |
| 102 | script: `export default { fetch: () => new Response("alchemy-assets-only-script") };`, |
| 103 | }); |
| 104 | yield* expectUrlContains(`${url}/does-not-exist`, "alchemy-assets-only-script", { |
| 105 | label: "script fallback after conversion", |
| 106 | }); |
| 107 | |
| 108 | // 4. Convert back to assets-only: the stored bundle hash must not |
| 109 | // mask the change, and the asset layer owns 404s again. |
| 110 | yield* deploy({ assets: { directory: dir, notFoundHandling: "404-page" } }); |
| 111 | yield* expectCustom404(`${url}/does-not-exist`); |
| 112 | yield* expectUrlContains(`${url}/`, "alchemy-assets-only-index-v2", { |
| 113 | label: "assets serve after conversion back", |
| 114 | }); |
| 115 | |
| 116 | yield* stack.destroy(); |
| 117 | }), |
| 118 | { timeout: 360_000 }, |
| 119 | ); |
| 120 | |
| 121 | test.provider( |
| 122 | "class form deploys an assets-only Worker", |
| 123 | (stack) => |
| 124 | Effect.gen(function* () { |
| 125 | yield* stack.destroy(); |
| 126 | |
| 127 | class Site extends Cloudflare.Worker<Site>()("AssetsOnlyClass", { |
| 128 | assets: { directory: fixtureDir, notFoundHandling: "404-page" }, |
| 129 | workersDev: true, |
| 130 | compatibility: { date: "2024-01-01" }, |
| 131 | }) {} |
| 132 | |
| 133 | const worker = yield* stack.deploy( |
| 134 | Effect.gen(function* () { |
| 135 | return yield* Site; |
| 136 | }), |
| 137 | ); |
| 138 | const url = worker.url!; |
| 139 | yield* expectUrlContains(`${url}/`, "alchemy-assets-only-index"); |
| 140 | yield* expectCustom404(`${url}/does-not-exist`); |
| 141 | |
| 142 | yield* stack.destroy(); |
| 143 | }), |
| 144 | { timeout: 240_000 }, |
| 145 | ); |
| 146 | }, |
| 147 | ); |