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", "astro-app");
18
19// Clone under the alchemy package so `astro` resolves from the workspace's
20// 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 "astro.config.mjs",
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 Astro.local.test.ts suite.
35const runEmulated = process.env.ALCHEMY_TEST_DEV === "1";
36
37describe.skipIf(!runLive || runEmulated)(
38 "AWS.Website.Astro",
39 {
40 tags: [
41 "provider:aws",
42 "provider:aws:cloudfront",
43 "provider:aws:website",
44 "live",
45 ],
46 },
47 () => {
48 test.provider(
49 "deploys 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-astro-aws-",
56 tempRoot,
57 entries: fixtureEntries,
58 });
59
60 const deployed = yield* stack.deploy(
61 Effect.gen(function* () {
62 const site = yield* AWS.Website.Astro("AstroSite", {
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 "ASTRO_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}/`, "ASTRO_AWS_PAGE_MARKER", {
91 timeout: "180 seconds",
92 label: "SSR home page",
93 });
94 // API route through the streaming Function URL origin.
95 yield* expectUrlContains(
96 `${url}/api/hello?echo=roundtrip`,
97 "ASTRO_AWS_API_MARKER",
98 { label: "API route" },
99 );
100 yield* expectUrlContains(
101 `${url}/api/hello?echo=roundtrip`,
102 "roundtrip",
103 { label: "API route query echo" },
104 );
105 // Public file served from S3 via the KV file manifest.
106 yield* expectUrlContains(
107 `${url}/robots.txt`,
108 "astro-aws-robots-marker",
109 {
110 label: "public asset from S3",
111 },
112 );
113 // Prerendered page (astro wrote it into dist/client at build time;
114 // the edge router serves it from S3 by directory-index match).
115 yield* expectUrlContains(
116 `${url}/prerendered/`,
117 "ASTRO_AWS_PRERENDERED_MARKER",
118 { label: "prerendered page" },
119 );
120
121 const distributionId = deployed.site.distribution!.distributionId;
122
123 if (!process.env.NO_DESTROY) {
124 yield* stack.destroy();
125 yield* assertDistributionDeleted(distributionId);
126 }
127 }),
128 { timeout: 2_400_000 },
129 );
130 },
131);
132
133const assertDistributionDeleted = (distributionId: string) =>
134 cloudfront.getDistribution({ Id: distributionId }).pipe(
135 Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))),
136 Effect.catchTag("NoSuchDistribution", () => Effect.void),
137 Effect.retry({
138 while: (error): boolean =>
139 error instanceof Error && error.message === "DistributionStillExists",
140 schedule: Schedule.max([
141 Schedule.fixed("10 seconds"),
142 Schedule.recurs(60),
143 ]),
144 }),
145 );