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 Cause from "effect/Cause"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Fiber from "effect/Fiber"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as Ref from "effect/Ref"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as pathe from "pathe"; |
| 12 | import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts"; |
| 13 | import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: AWS.providers() }); |
| 16 | |
| 17 | // Gated with the rest of the AWS.Website suites: the CloudFront lifecycle |
| 18 | // dominates the runtime (create ~5-15 min, destroy ~5-15 min). |
| 19 | const runLive = !process.env.FAST; |
| 20 | |
| 21 | const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "nuxt-app"); |
| 22 | |
| 23 | // Clone under the alchemy package so `nuxt`/`nitropack` resolve from the |
| 24 | // workspace's hoisted node_modules (the fixture has no node_modules). |
| 25 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 26 | |
| 27 | const fixtureEntries = [".gitignore", "package.json", "nuxt.config.ts", "app", "server", "public"]; |
| 28 | |
| 29 | // Under the floci runner the standalone composite deploys only the framework |
| 30 | // dev server (no Lambda/S3/CloudFront), so the standalone test below is |
| 31 | // live-only; the shared-Router test runs in both modes because the Router |
| 32 | // pipeline deploys fully against the emulator, which serves the router's |
| 33 | // edge on a local plain-HTTP port. |
| 34 | const runEmulated = process.env.ALCHEMY_TEST_DEV === "1"; |
| 35 | |
| 36 | describe.skipIf(!runLive)( |
| 37 | "AWS.Website.Nuxt", |
| 38 | { tags: ["provider:aws", "provider:aws:cloudfront", "provider:aws:website", "live"] }, |
| 39 | () => { |
| 40 | test.provider.skipIf(runEmulated)( |
| 41 | "deploys SSR on a Lambda Web Function with S3 assets behind CloudFront", |
| 42 | (stack) => |
| 43 | Effect.gen(function* () { |
| 44 | yield* stack.destroy(); |
| 45 | |
| 46 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 47 | prefix: "alchemy-nuxt-aws-", |
| 48 | tempRoot, |
| 49 | entries: fixtureEntries, |
| 50 | }); |
| 51 | |
| 52 | const deployed = yield* stack.deploy( |
| 53 | Effect.gen(function* () { |
| 54 | const site = yield* AWS.Website.Nuxt("NuxtSite", { |
| 55 | rootDir, |
| 56 | forceDestroy: true, |
| 57 | invalidation: { paths: "all", wait: true }, |
| 58 | }); |
| 59 | return { site }; |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | const url = deployed.site.url! as string; |
| 64 | expect(url).toMatch(/^https:\/\//); |
| 65 | expect(deployed.site.serverUrl).toBeDefined(); |
| 66 | // The server is a Lambda Web Function (the default). |
| 67 | expect(deployed.site.serverUrl).toContain(".lambda-web."); |
| 68 | yield* Effect.log(`site url: ${url} | server url: ${deployed.site.serverUrl}`); |
| 69 | |
| 70 | // The Lambda Function URL serves the SSR page directly — isolates |
| 71 | // server-function health from the CloudFront edge routing. |
| 72 | yield* expectUrlContains(`${deployed.site.serverUrl!}`, "NUXT_AWS_PAGE_MARKER", { |
| 73 | timeout: "120 seconds", |
| 74 | label: "SSR direct from Lambda URL", |
| 75 | }); |
| 76 | |
| 77 | // SSR page rendered by the Lambda through CloudFront. |
| 78 | yield* expectUrlContains(`${url}/`, "NUXT_AWS_PAGE_MARKER", { |
| 79 | timeout: "180 seconds", |
| 80 | label: "SSR home page", |
| 81 | }); |
| 82 | // The fixture's own nuxt.config.ts applied (runtimeConfig marker). |
| 83 | yield* expectUrlContains(`${url}/`, "config:nuxt-aws-user-config-loaded", { |
| 84 | label: "user nuxt.config.ts applied", |
| 85 | }); |
| 86 | // Server API route through the streaming Function URL origin. |
| 87 | yield* expectUrlContains(`${url}/api/hello?echo=roundtrip`, "NUXT_AWS_API_MARKER", { |
| 88 | label: "API route", |
| 89 | }); |
| 90 | yield* expectUrlContains(`${url}/api/hello?echo=roundtrip`, "roundtrip", { |
| 91 | label: "API route query echo", |
| 92 | }); |
| 93 | // Public file served from S3 via the KV file manifest. |
| 94 | yield* expectUrlContains(`${url}/robots.txt`, "nuxt-aws-robots-marker", { |
| 95 | label: "public asset from S3", |
| 96 | }); |
| 97 | // Prerendered page (nitro wrote it into .output/public at build |
| 98 | // time; the edge router serves it from S3 by exact match). |
| 99 | yield* expectUrlContains(`${url}/prerendered`, "NUXT_AWS_PRERENDERED_MARKER", { |
| 100 | label: "prerendered page", |
| 101 | }); |
| 102 | |
| 103 | const distributionId = deployed.site.distribution!.distributionId; |
| 104 | |
| 105 | if (!process.env.NO_DESTROY) { |
| 106 | yield* stack.destroy(); |
| 107 | yield* assertDistributionDeleted(distributionId); |
| 108 | } |
| 109 | }), |
| 110 | { timeout: 2_400_000 }, |
| 111 | ); |
| 112 | |
| 113 | test.provider.skipIf(runEmulated)( |
| 114 | 'moves a standalone site from server: "function" onto a Web Function, then several Regions, without downtime', |
| 115 | (stack) => |
| 116 | Effect.gen(function* () { |
| 117 | yield* stack.destroy(); |
| 118 | |
| 119 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 120 | prefix: "alchemy-nuxt-aws-fn-", |
| 121 | tempRoot, |
| 122 | entries: fixtureEntries, |
| 123 | }); |
| 124 | const deploy = (props: { server: "function" } | { regions?: string[] }) => |
| 125 | stack.deploy( |
| 126 | Effect.gen(function* () { |
| 127 | const site = yield* AWS.Website.Nuxt("NuxtFunctionSite", { |
| 128 | rootDir, |
| 129 | ...props, |
| 130 | forceDestroy: true, |
| 131 | invalidation: { paths: "all", wait: true }, |
| 132 | }); |
| 133 | return { site }; |
| 134 | }), |
| 135 | ); |
| 136 | |
| 137 | const deployed = yield* deploy({ server: "function" }); |
| 138 | expect(deployed.site.serverUrl).toContain(".lambda-url."); |
| 139 | const url = `${deployed.site.url!}/`; |
| 140 | yield* expectUrlContains(url, "NUXT_AWS_PAGE_MARKER", { |
| 141 | timeout: "180 seconds", |
| 142 | label: "SSR home page", |
| 143 | }); |
| 144 | |
| 145 | // Dropping `server` moves the site onto a Web Function; the |
| 146 | // distribution (and its domain) stays, and the site keeps |
| 147 | // answering throughout. |
| 148 | const { result: migrated, failures: forward } = yield* probeDuring( |
| 149 | url, |
| 150 | "NUXT_AWS_PAGE_MARKER", |
| 151 | deploy({}), |
| 152 | ); |
| 153 | expect(migrated.site.serverUrl).toContain(".lambda-web."); |
| 154 | expect(migrated.site.distribution!.distributionId).toBe( |
| 155 | deployed.site.distribution!.distributionId, |
| 156 | ); |
| 157 | expectNoDowntime(forward); |
| 158 | |
| 159 | // Adding Regions gives the server one global domain; CloudFront |
| 160 | // moves to it and the site's URL stays. |
| 161 | const { result: global, failures: regional } = yield* probeDuring( |
| 162 | url, |
| 163 | "NUXT_AWS_PAGE_MARKER", |
| 164 | deploy({ regions: ["us-east-1"] }), |
| 165 | ); |
| 166 | expect(global.site.serverUrl).toContain(".lambda-web.global."); |
| 167 | expect(global.site.distribution!.distributionId).toBe( |
| 168 | deployed.site.distribution!.distributionId, |
| 169 | ); |
| 170 | expectNoDowntime(regional); |
| 171 | |
| 172 | const distributionId = deployed.site.distribution!.distributionId; |
| 173 | yield* stack.destroy(); |
| 174 | yield* assertDistributionDeleted(distributionId); |
| 175 | }), |
| 176 | { timeout: 2_400_000 }, |
| 177 | ); |
| 178 | |
| 179 | test.provider.skipIf(runEmulated)( |
| 180 | 'moves a Router-attached site from server: "function" onto a Web Function without downtime', |
| 181 | (stack) => |
| 182 | Effect.gen(function* () { |
| 183 | yield* stack.destroy(); |
| 184 | |
| 185 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 186 | prefix: "alchemy-nuxt-aws-router-switch-", |
| 187 | tempRoot, |
| 188 | entries: fixtureEntries, |
| 189 | }); |
| 190 | const deploy = (server: "function" | undefined) => |
| 191 | stack.deploy( |
| 192 | Effect.gen(function* () { |
| 193 | const router = yield* AWS.Website.Router("FrontDoor", { |
| 194 | invalidation: { paths: "all", wait: true }, |
| 195 | }); |
| 196 | const site = yield* AWS.Website.Nuxt("NuxtSite", { |
| 197 | rootDir, |
| 198 | ...(server === "function" ? { server: "function" as const } : {}), |
| 199 | forceDestroy: true, |
| 200 | domain: { router }, |
| 201 | }); |
| 202 | return { router, site }; |
| 203 | }), |
| 204 | ); |
| 205 | |
| 206 | const deployed = yield* deploy("function"); |
| 207 | expect(deployed.site.serverUrl).toContain(".lambda-url."); |
| 208 | const url = `${deployed.router.url as string}/`; |
| 209 | yield* expectUrlContains(url, "NUXT_AWS_PAGE_MARKER", { |
| 210 | timeout: "600 seconds", |
| 211 | label: "SSR via router", |
| 212 | }); |
| 213 | |
| 214 | const { result: migrated, failures: forward } = yield* probeDuring( |
| 215 | url, |
| 216 | "NUXT_AWS_PAGE_MARKER", |
| 217 | deploy(undefined), |
| 218 | ); |
| 219 | expect(migrated.site.serverUrl).toContain(".lambda-web."); |
| 220 | expect(migrated.router.distribution.distributionId).toBe( |
| 221 | deployed.router.distribution.distributionId, |
| 222 | ); |
| 223 | expectNoDowntime(forward); |
| 224 | |
| 225 | const distributionId = deployed.router.distribution.distributionId; |
| 226 | yield* stack.destroy(); |
| 227 | yield* assertDistributionDeleted(distributionId); |
| 228 | }), |
| 229 | { timeout: 2_400_000 }, |
| 230 | ); |
| 231 | |
| 232 | test.provider( |
| 233 | "serves SSR through a shared Router distribution with Lambda env applied", |
| 234 | (stack) => |
| 235 | Effect.gen(function* () { |
| 236 | yield* stack.destroy(); |
| 237 | |
| 238 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 239 | prefix: "alchemy-nuxt-aws-router-", |
| 240 | tempRoot, |
| 241 | entries: fixtureEntries, |
| 242 | }); |
| 243 | |
| 244 | const deployed = yield* stack.deploy( |
| 245 | Effect.gen(function* () { |
| 246 | const router = yield* AWS.Website.Router("FrontDoor", { |
| 247 | invalidation: { paths: "all", wait: true }, |
| 248 | }); |
| 249 | const site = yield* AWS.Website.Nuxt("NuxtSite", { |
| 250 | rootDir, |
| 251 | forceDestroy: true, |
| 252 | domain: { router }, |
| 253 | env: { NUXT_PUBLIC_ENV_MARKER: "nuxt-aws-live-env-marker" }, |
| 254 | }); |
| 255 | return { router, site }; |
| 256 | }), |
| 257 | ); |
| 258 | |
| 259 | const url = deployed.router.url as string; |
| 260 | // `https://{id}.cloudfront.net` live; the emulator serves the |
| 261 | // router's edge on a local plain-HTTP port. |
| 262 | expect(url).toMatch(runEmulated ? /^http:\/\/localhost:\d+/ : /^https:\/\//); |
| 263 | |
| 264 | // SSR through the ROUTER's distribution (the site registered |
| 265 | // itself in the router's KV store — no site-owned distribution). |
| 266 | expect(deployed.site.distribution).toBeUndefined(); |
| 267 | // Generous budget: a fresh router distribution's KVS association + |
| 268 | // function propagation can lag past 180s on first serve. |
| 269 | yield* expectUrlContains(`${url}/`, "NUXT_AWS_PAGE_MARKER", { |
| 270 | timeout: "300 seconds", |
| 271 | label: "SSR via router", |
| 272 | }); |
| 273 | // Lambda env applied on deploy (parity with the dev-server |
| 274 | // injection asserted in Nuxt.local.test.ts). |
| 275 | yield* expectUrlContains(`${url}/`, "env:nuxt-aws-live-env-marker", { |
| 276 | label: "server.environment on the Lambda", |
| 277 | }); |
| 278 | // The router's defaultTTL-0 cache policy must not cache SSR |
| 279 | // responses: two fetches of an SSR page with a per-request value |
| 280 | // would prove staleness, but the page is deterministic — instead |
| 281 | // assert the API route (dynamic, no cache-control) round-trips |
| 282 | // with distinct query strings, which a day-long cached body under |
| 283 | // CachingOptimized would break. |
| 284 | yield* expectUrlContains(`${url}/api/hello?echo=router-one`, "router-one", { |
| 285 | label: "API via router (query one)", |
| 286 | }); |
| 287 | yield* expectUrlContains(`${url}/api/hello?echo=router-two`, "router-two", { |
| 288 | label: "API via router (query two)", |
| 289 | }); |
| 290 | // Static asset from S3 through the router's edge function. |
| 291 | yield* expectUrlContains(`${url}/robots.txt`, "nuxt-aws-robots-marker", { |
| 292 | label: "public asset via router", |
| 293 | }); |
| 294 | // Prerendered page from S3 by exact match. |
| 295 | yield* expectUrlContains(`${url}/prerendered`, "NUXT_AWS_PRERENDERED_MARKER", { |
| 296 | label: "prerendered page via router", |
| 297 | }); |
| 298 | |
| 299 | const distributionId = deployed.router.distributionId as string; |
| 300 | |
| 301 | if (!process.env.NO_DESTROY) { |
| 302 | yield* stack.destroy(); |
| 303 | yield* assertDistributionDeleted(distributionId); |
| 304 | } |
| 305 | }), |
| 306 | { timeout: 2_400_000 }, |
| 307 | ); |
| 308 | }, |
| 309 | ); |
| 310 | |
| 311 | const assertDistributionDeleted = (distributionId: string) => |
| 312 | cloudfront.getDistribution({ Id: distributionId }).pipe( |
| 313 | Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))), |
| 314 | Effect.catchTag("NoSuchDistribution", () => Effect.void), |
| 315 | Effect.retry({ |
| 316 | while: (error): boolean => |
| 317 | error instanceof Error && error.message === "DistributionStillExists", |
| 318 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]), |
| 319 | }), |
| 320 | ); |
| 321 | |
| 322 | /** |
| 323 | * A just-created server endpoint rejects CloudFront with a Function URL |
| 324 | * authorization 403 for a few seconds after AWS reports it Active, and there |
| 325 | * is no control-plane signal to wait for. A cutover may hit a short run of |
| 326 | * exactly that response; anything else, or a longer run, is downtime. |
| 327 | */ |
| 328 | const isEndpointSettling = (failure: string) => |
| 329 | failure.includes(" 403 ") && failure.includes("Function URL authorization"); |
| 330 | |
| 331 | const expectNoDowntime = (failures: string[]) => { |
| 332 | expect(failures.filter((failure) => !isEndpointSettling(failure))).toEqual([]); |
| 333 | expect(failures.filter(isEndpointSettling).length).toBeLessThanOrEqual(15); |
| 334 | }; |
| 335 | |
| 336 | /** |
| 337 | * Run `effect` while requesting `url` once a second, and report every |
| 338 | * request that did not return the page (status and time). |
| 339 | */ |
| 340 | const probeDuring = <A, E, R>(url: string, marker: string, effect: Effect.Effect<A, E, R>) => |
| 341 | Effect.gen(function* () { |
| 342 | const client = yield* HttpClient.HttpClient; |
| 343 | const failures = yield* Ref.make<string[]>([]); |
| 344 | const once = client.get(url).pipe( |
| 345 | Effect.flatMap((response) => |
| 346 | Effect.map(response.text, (body) => |
| 347 | response.status === 200 && body.includes(marker) |
| 348 | ? undefined |
| 349 | : // Who answered (CloudFront vs the origin) and what it said. |
| 350 | `${response.status} [${response.headers["x-cache"] ?? ""}] ${body.slice(0, 160)}`, |
| 351 | ), |
| 352 | ), |
| 353 | Effect.catch((error) => Effect.succeed(`error ${String(error)}`)), |
| 354 | Effect.flatMap((failure) => |
| 355 | failure === undefined |
| 356 | ? Effect.void |
| 357 | : Ref.update(failures, (all) => [...all, `${new Date().toISOString()} ${failure}`]), |
| 358 | ), |
| 359 | ); |
| 360 | const prober = yield* Effect.forkChild(once.pipe(Effect.repeat(Schedule.spaced("1 second")))); |
| 361 | const result = yield* effect; |
| 362 | yield* Fiber.interrupt(prober); |
| 363 | return { result, failures: yield* Ref.get(failures) }; |
| 364 | }); |
| 365 | |
| 366 | test.provider( |
| 367 | 'regions with server: "function" is a type error, and fails at plan in untyped JS', |
| 368 | (stack) => |
| 369 | Effect.gen(function* () { |
| 370 | const exit = yield* stack |
| 371 | .plan( |
| 372 | AWS.Website.Nuxt("RegionsOnFunction", { |
| 373 | server: "function", |
| 374 | // @ts-expect-error `regions` only exists on the Web Function server form. |
| 375 | regions: ["us-east-1"], |
| 376 | }), |
| 377 | ) |
| 378 | .pipe(Effect.exit); |
| 379 | expect(exit._tag).toBe("Failure"); |
| 380 | expect(exit._tag === "Failure" ? String(Cause.squash(exit.cause)) : "").toContain( |
| 381 | "regions needs the Web Function server", |
| 382 | ); |
| 383 | }), |
| 384 | { tags: ["provider:aws", "provider:aws:website", "live"] }, |
| 385 | ); |
| 386 | |
| 387 | // Type-level: each server form only accepts its own props. |
| 388 | export const _serverFormTypeChecks = () => [ |
| 389 | AWS.Website.Nuxt("Web", { regions: ["us-east-1"], maxEnvironments: 10 }), |
| 390 | AWS.Website.Nuxt("Fn", { server: "function", memorySize: 4096, architecture: "x86_64" }), |
| 391 | // @ts-expect-error `memorySize` only exists on the `server: "function"` form. |
| 392 | AWS.Website.Nuxt("WebWithMemory", { memorySize: 4096 }), |
| 393 | // @ts-expect-error `maxEnvironments` only exists on the Web Function form. |
| 394 | AWS.Website.Nuxt("FnWithEnvironments", { server: "function", maxEnvironments: 10 }), |
| 395 | // @ts-expect-error `imageOptimization` only exists on the `server: "function"` form. |
| 396 | AWS.Website.Nextjs("NextWebWithImageMemory", { imageOptimization: { memorySize: 2048 } }), |
| 397 | ]; |