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

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