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