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