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", "waku-app"); |
| 18 | |
| 19 | // Clone under the alchemy package so `waku`/`react` resolve from the |
| 20 | // workspace's hoisted node_modules (the fixture has no node_modules). |
| 21 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 22 | |
| 23 | const fixtureEntries = [".gitignore", "package.json", "tsconfig.json", "src", "public"]; |
| 24 | |
| 25 | // Skipped under the floci runner: in dev the composite deploys only the |
| 26 | // framework dev server (no Lambda/S3/CloudFront), so this test's live |
| 27 | // topology assertions are meaningless there. Dev behavior is covered by |
| 28 | // the co-located Waku.local.test.ts suite. |
| 29 | const runEmulated = process.env.ALCHEMY_TEST_DEV === "1"; |
| 30 | |
| 31 | describe.skipIf(!runLive || runEmulated)( |
| 32 | "AWS.Website.Waku", |
| 33 | { tags: ["provider:aws", "provider:aws:cloudfront", "provider:aws:website", "live"] }, |
| 34 | () => { |
| 35 | test.provider( |
| 36 | "deploys RSC SSR on a Lambda Web Function with S3 assets behind CloudFront", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | yield* stack.destroy(); |
| 40 | |
| 41 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 42 | prefix: "alchemy-waku-aws-", |
| 43 | tempRoot, |
| 44 | entries: fixtureEntries, |
| 45 | }); |
| 46 | |
| 47 | const deployed = yield* stack.deploy( |
| 48 | Effect.gen(function* () { |
| 49 | const site = yield* AWS.Website.Waku("WakuSite", { |
| 50 | rootDir, |
| 51 | forceDestroy: true, |
| 52 | invalidation: { paths: "all", wait: true }, |
| 53 | }); |
| 54 | return { site }; |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | const url = deployed.site.url! as string; |
| 59 | expect(url).toMatch(/^https:\/\//); |
| 60 | expect(deployed.site.serverUrl).toBeDefined(); |
| 61 | // The server is a Lambda Web Function (the default). |
| 62 | expect(deployed.site.serverUrl).toContain(".lambda-web."); |
| 63 | yield* Effect.log(`site url: ${url} | server url: ${deployed.site.serverUrl}`); |
| 64 | |
| 65 | // The Lambda Function URL serves the SSR page directly — isolates |
| 66 | // server-function health from the CloudFront edge routing. |
| 67 | yield* expectUrlContains(`${deployed.site.serverUrl!}`, "WAKU_AWS_PAGE_MARKER", { |
| 68 | timeout: "120 seconds", |
| 69 | label: "SSR direct from Lambda URL", |
| 70 | }); |
| 71 | |
| 72 | // SSR page rendered by the Lambda through CloudFront. |
| 73 | yield* expectUrlContains(`${url}/`, "WAKU_AWS_PAGE_MARKER", { |
| 74 | timeout: "180 seconds", |
| 75 | label: "SSR home page", |
| 76 | }); |
| 77 | // API route (waku's `_api` pattern, `_api` prefix stripped) through |
| 78 | // the streaming Function URL origin. |
| 79 | yield* expectUrlContains(`${url}/echo?echo=roundtrip`, "WAKU_AWS_API_MARKER", { |
| 80 | label: "API route", |
| 81 | }); |
| 82 | yield* expectUrlContains(`${url}/echo?echo=roundtrip`, "roundtrip", { |
| 83 | label: "API route query echo", |
| 84 | }); |
| 85 | // Public file served from S3 via the KV file manifest. |
| 86 | yield* expectUrlContains(`${url}/robots.txt`, "waku-aws-robots-marker", { |
| 87 | label: "public asset from S3", |
| 88 | }); |
| 89 | // SSG page (waku prerendered it into dist/public/about/index.html at |
| 90 | // build time; the edge router's postfix lookup serves it from S3 at |
| 91 | // the extensionless URL waku links to). |
| 92 | yield* expectUrlContains(`${url}/about`, "WAKU_AWS_STATIC_MARKER", { label: "SSG page" }); |
| 93 | |
| 94 | const distributionId = deployed.site.distribution!.distributionId; |
| 95 | |
| 96 | if (!process.env.NO_DESTROY) { |
| 97 | yield* stack.destroy(); |
| 98 | yield* assertDistributionDeleted(distributionId); |
| 99 | } |
| 100 | }), |
| 101 | { timeout: 2_400_000 }, |
| 102 | ); |
| 103 | }, |
| 104 | ); |
| 105 | |
| 106 | const assertDistributionDeleted = (distributionId: string) => |
| 107 | cloudfront.getDistribution({ Id: distributionId }).pipe( |
| 108 | Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))), |
| 109 | Effect.catchTag("NoSuchDistribution", () => Effect.void), |
| 110 | Effect.retry({ |
| 111 | while: (error): boolean => |
| 112 | error instanceof Error && error.message === "DistributionStillExists", |
| 113 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]), |
| 114 | }), |
| 115 | ); |