The best way to control Claude Code, Codex and more from any computer or phone

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