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 AWS from "@alchemy.run/aws"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as cloudfront from "@distilled.cloud/aws/cloudfront"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import * as pathe from "pathe"; |
| 8 | import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts"; |
| 9 | import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | // Gated with the rest of the AWS.Website suites: the CloudFront lifecycle |
| 14 | // dominates the runtime (create ~5-15 min, destroy ~5-15 min). |
| 15 | const runLive = !process.env.FAST; |
| 16 | |
| 17 | const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "react-router-app"); |
| 18 | |
| 19 | // Clone under the alchemy package so `@react-router/dev`, `react-router`, |
| 20 | // `@react-router/node`, `isbot`, `react`, and `vite` resolve from the |
| 21 | // workspace's node_modules (the fixture has none of its own). |
| 22 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 23 | |
| 24 | const fixtureEntries = [ |
| 25 | ".gitignore", |
| 26 | "package.json", |
| 27 | "react-router.config.ts", |
| 28 | "vite.config.ts", |
| 29 | "app", |
| 30 | "public", |
| 31 | ]; |
| 32 | |
| 33 | // Under the floci runner the standalone composite deploys only the framework |
| 34 | // dev server (no Lambda/S3/CloudFront), so the standalone test below is |
| 35 | // live-only; the shared-Router test runs in both modes because the Router |
| 36 | // pipeline deploys fully against the emulator, which serves the router's |
| 37 | // edge on a local plain-HTTP port. |
| 38 | const runEmulated = process.env.ALCHEMY_TEST_DEV === "1"; |
| 39 | |
| 40 | describe.skipIf(!runLive)( |
| 41 | "AWS.Website.ReactRouter", |
| 42 | { tags: ["provider:aws", "provider:aws:cloudfront", "provider:aws:website", "live"] }, |
| 43 | () => { |
| 44 | test.provider.skipIf(runEmulated)( |
| 45 | "deploys SSR on a Lambda Web Function with S3 assets behind CloudFront", |
| 46 | (stack) => |
| 47 | Effect.gen(function* () { |
| 48 | yield* stack.destroy(); |
| 49 | |
| 50 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 51 | prefix: "alchemy-react-router-aws-", |
| 52 | tempRoot, |
| 53 | entries: fixtureEntries, |
| 54 | }); |
| 55 | |
| 56 | const deployed = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | const site = yield* AWS.Website.ReactRouter("ReactRouterSite", { |
| 59 | rootDir, |
| 60 | forceDestroy: true, |
| 61 | invalidation: { paths: "all", wait: true }, |
| 62 | }); |
| 63 | return { site }; |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | const url = deployed.site.url! as string; |
| 68 | expect(url).toMatch(/^https:\/\//); |
| 69 | expect(deployed.site.serverUrl).toBeDefined(); |
| 70 | // The server is a Lambda Web Function (the default). |
| 71 | expect(deployed.site.serverUrl).toContain(".lambda-web."); |
| 72 | yield* Effect.log(`site url: ${url} | server url: ${deployed.site.serverUrl}`); |
| 73 | |
| 74 | // The Lambda Function URL serves the SSR page directly — isolates |
| 75 | // server-function health from the CloudFront edge routing. |
| 76 | yield* expectUrlContains(`${deployed.site.serverUrl!}`, "REACT_ROUTER_AWS_PAGE_MARKER", { |
| 77 | timeout: "120 seconds", |
| 78 | label: "SSR direct from Lambda URL", |
| 79 | }); |
| 80 | |
| 81 | // SSR page rendered by the Lambda through CloudFront. |
| 82 | yield* expectUrlContains(`${url}/`, "REACT_ROUTER_AWS_PAGE_MARKER", { |
| 83 | timeout: "180 seconds", |
| 84 | label: "SSR home page", |
| 85 | }); |
| 86 | // The fixture's own vite.config.ts applied (its `define` marker). |
| 87 | yield* expectUrlContains(`${url}/`, "config:react-router-aws-user-config-loaded", { |
| 88 | label: "user vite.config.ts applied", |
| 89 | }); |
| 90 | // Resource route through the streaming Function URL origin. |
| 91 | yield* expectUrlContains( |
| 92 | `${url}/api/hello?echo=roundtrip`, |
| 93 | "REACT_ROUTER_AWS_API_MARKER", |
| 94 | { label: "resource route" }, |
| 95 | ); |
| 96 | yield* expectUrlContains(`${url}/api/hello?echo=roundtrip`, "roundtrip", { |
| 97 | label: "resource route query echo", |
| 98 | }); |
| 99 | // Public file served from S3 via the KV file manifest. |
| 100 | yield* expectUrlContains(`${url}/robots.txt`, "react-router-aws-robots-marker", { |
| 101 | label: "public asset from S3", |
| 102 | }); |
| 103 | // Client bundle from S3 — the SSR document modulepreloads it, so a |
| 104 | // broken asset upload breaks hydration silently otherwise. |
| 105 | yield* expectUrlContains(`${url}/`, "/assets/", { label: "client asset reference" }); |
| 106 | |
| 107 | const distributionId = deployed.site.distribution!.distributionId; |
| 108 | |
| 109 | if (!process.env.NO_DESTROY) { |
| 110 | yield* stack.destroy(); |
| 111 | yield* assertDistributionDeleted(distributionId); |
| 112 | } |
| 113 | }), |
| 114 | { timeout: 2_400_000 }, |
| 115 | ); |
| 116 | |
| 117 | test.provider( |
| 118 | "serves SSR through a shared Router distribution with Lambda env applied", |
| 119 | (stack) => |
| 120 | Effect.gen(function* () { |
| 121 | yield* stack.destroy(); |
| 122 | |
| 123 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 124 | prefix: "alchemy-react-router-aws-router-", |
| 125 | tempRoot, |
| 126 | entries: fixtureEntries, |
| 127 | }); |
| 128 | |
| 129 | const deployed = yield* stack.deploy( |
| 130 | Effect.gen(function* () { |
| 131 | const router = yield* AWS.Website.Router("FrontDoor", { |
| 132 | invalidation: { paths: "all", wait: true }, |
| 133 | }); |
| 134 | const site = yield* AWS.Website.ReactRouter("ReactRouterSite", { |
| 135 | rootDir, |
| 136 | forceDestroy: true, |
| 137 | domain: { router }, |
| 138 | env: { REACT_ROUTER_ENV_MARKER: "react-router-aws-live-env-marker" }, |
| 139 | }); |
| 140 | return { router, site }; |
| 141 | }), |
| 142 | ); |
| 143 | |
| 144 | const url = deployed.router.url as string; |
| 145 | // `https://{id}.cloudfront.net` live; the emulator serves the |
| 146 | // router's edge on a local plain-HTTP port. |
| 147 | expect(url).toMatch(runEmulated ? /^http:\/\/localhost:\d+/ : /^https:\/\//); |
| 148 | |
| 149 | // SSR through the ROUTER's distribution (the site registered itself |
| 150 | // in the router's KV store — no site-owned distribution). |
| 151 | expect(deployed.site.distribution).toBeUndefined(); |
| 152 | // Generous budget: a fresh router distribution's KVS association + |
| 153 | // function propagation can lag past 180s on first serve. |
| 154 | yield* expectUrlContains(`${url}/`, "REACT_ROUTER_AWS_PAGE_MARKER", { |
| 155 | timeout: "300 seconds", |
| 156 | label: "SSR via router", |
| 157 | }); |
| 158 | // Lambda env applied on deploy, read from a route loader (parity |
| 159 | // with the dev-server injection asserted in the local suite). |
| 160 | yield* expectUrlContains(`${url}/`, "env:react-router-aws-live-env-marker", { |
| 161 | label: "server.environment on the Lambda", |
| 162 | }); |
| 163 | // The router's defaultTTL-0 cache policy must not cache SSR |
| 164 | // responses: the resource route round-trips with distinct query |
| 165 | // strings, which a day-long cached body would break. |
| 166 | yield* expectUrlContains(`${url}/api/hello?echo=router-one`, "router-one", { |
| 167 | label: "resource route via router (query one)", |
| 168 | }); |
| 169 | yield* expectUrlContains(`${url}/api/hello?echo=router-two`, "router-two", { |
| 170 | label: "resource route via router (query two)", |
| 171 | }); |
| 172 | // Static asset from S3 through the router's edge function. |
| 173 | yield* expectUrlContains(`${url}/robots.txt`, "react-router-aws-robots-marker", { |
| 174 | label: "public asset via router", |
| 175 | }); |
| 176 | |
| 177 | const distributionId = deployed.router.distributionId as string; |
| 178 | |
| 179 | if (!process.env.NO_DESTROY) { |
| 180 | yield* stack.destroy(); |
| 181 | yield* assertDistributionDeleted(distributionId); |
| 182 | } |
| 183 | }), |
| 184 | { timeout: 2_400_000 }, |
| 185 | ); |
| 186 | }, |
| 187 | ); |
| 188 | |
| 189 | const assertDistributionDeleted = (distributionId: string) => |
| 190 | cloudfront.getDistribution({ Id: distributionId }).pipe( |
| 191 | Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))), |
| 192 | Effect.catchTag("NoSuchDistribution", () => Effect.void), |
| 193 | Effect.retry({ |
| 194 | while: (error): boolean => |
| 195 | error instanceof Error && error.message === "DistributionStillExists", |
| 196 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]), |
| 197 | }), |
| 198 | ); |