@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 152 lines 6.2 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 FileSystem from "effect/FileSystem";
7 import * as Path from "effect/Path";
8 import * as Schedule from "effect/Schedule";
9 import * as pathe from "pathe";
10 import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts";
11 import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 // Gated with the rest of the AWS.Website suites: the CloudFront lifecycle
16 // dominates the runtime (create ~5-15 min, destroy ~5-15 min).
17 const runLive = !process.env.FAST;
18
19 const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "astro-app");
20
21 // Clone under the alchemy package so `astro` resolves from the workspace's
22 // hoisted node_modules (the fixture has no node_modules).
23 const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
24
25 const fixtureEntries = [".gitignore", "package.json", "astro.config.mjs", "src", "public"];
26
27 // Skipped under the floci runner: in dev the composite deploys only the
28 // framework dev server (no Lambda/S3/CloudFront), so this test's live
29 // topology assertions are meaningless there. Dev behavior is covered by
30 // the co-located Astro.local.test.ts suite.
31 const runEmulated = process.env.ALCHEMY_TEST_DEV === "1";
32
33 // Plan-only: a static site's topology (no Lambda) is decided at plan time,
34 // so this needs no CloudFront lifecycle and runs outside the live gate.
35 describe.skipIf(runEmulated)(
36 "AWS.Website.Astro plan",
37 { tags: ["provider:aws", "provider:aws:website"] },
38 () => {
39 test.provider(
40 "a config-file output 'static' plans an assets-only site without the astro.output prop",
41 (stack) =>
42 Effect.gen(function* () {
43 const fs = yield* FileSystem.FileSystem;
44 const path = yield* Path.Path;
45
46 const rootDir = yield* cloneFixture(fixtureDir, {
47 prefix: "alchemy-astro-aws-static-config-",
48 tempRoot,
49 entries: fixtureEntries.filter((entry) => entry !== "astro.config.mjs"),
50 });
51 yield* fs.writeFileString(
52 path.join(rootDir, "astro.config.mjs"),
53 'import { defineConfig } from "astro/config";\n\nexport default defineConfig({ output: "static" });\n',
54 );
55
56 const plan = yield* stack.plan(
57 AWS.Website.Astro("AstroStaticConfigSite", { rootDir, forceDestroy: true }),
58 );
59 const types = Object.values(plan.resources).map((node) => node.resource.Type);
60 expect(types).toContain("AWS.S3.Bucket");
61 expect(types.filter((type) => type.startsWith("AWS.Lambda."))).toEqual([]);
62 }),
63 { tags: ["live"] },
64 );
65 },
66 );
67
68 describe.skipIf(!runLive || runEmulated)(
69 "AWS.Website.Astro",
70 { tags: ["provider:aws", "provider:aws:cloudfront", "provider:aws:website", "live"] },
71 () => {
72 test.provider(
73 "deploys SSR on a Lambda Web Function with S3 assets behind CloudFront",
74 (stack) =>
75 Effect.gen(function* () {
76 yield* stack.destroy();
77
78 const rootDir = yield* cloneFixture(fixtureDir, {
79 prefix: "alchemy-astro-aws-",
80 tempRoot,
81 entries: fixtureEntries,
82 });
83
84 const deployed = yield* stack.deploy(
85 Effect.gen(function* () {
86 const site = yield* AWS.Website.Astro("AstroSite", {
87 rootDir,
88 forceDestroy: true,
89 invalidation: { paths: "all", wait: true },
90 });
91 return { site };
92 }),
93 );
94
95 const url = deployed.site.url! as string;
96 expect(url).toMatch(/^https:\/\//);
97 expect(deployed.site.serverUrl).toBeDefined();
98 // The server is a Lambda Web Function (the default).
99 expect(deployed.site.serverUrl).toContain(".lambda-web.");
100 yield* Effect.log(`site url: ${url} | server url: ${deployed.site.serverUrl}`);
101
102 // The Lambda Function URL serves the SSR page directly — isolates
103 // server-function health from the CloudFront edge routing.
104 yield* expectUrlContains(`${deployed.site.serverUrl!}`, "ASTRO_AWS_PAGE_MARKER", {
105 timeout: "120 seconds",
106 label: "SSR direct from Lambda URL",
107 });
108
109 // SSR page rendered by the Lambda through CloudFront.
110 yield* expectUrlContains(`${url}/`, "ASTRO_AWS_PAGE_MARKER", {
111 timeout: "180 seconds",
112 label: "SSR home page",
113 });
114 // API route through the streaming Function URL origin.
115 yield* expectUrlContains(`${url}/api/hello?echo=roundtrip`, "ASTRO_AWS_API_MARKER", {
116 label: "API route",
117 });
118 yield* expectUrlContains(`${url}/api/hello?echo=roundtrip`, "roundtrip", {
119 label: "API route query echo",
120 });
121 // Public file served from S3 via the KV file manifest.
122 yield* expectUrlContains(`${url}/robots.txt`, "astro-aws-robots-marker", {
123 label: "public asset from S3",
124 });
125 // Prerendered page (astro wrote it into dist/client at build time;
126 // the edge router serves it from S3 by directory-index match).
127 yield* expectUrlContains(`${url}/prerendered/`, "ASTRO_AWS_PRERENDERED_MARKER", {
128 label: "prerendered page",
129 });
130
131 const distributionId = deployed.site.distribution!.distributionId;
132
133 if (!process.env.NO_DESTROY) {
134 yield* stack.destroy();
135 yield* assertDistributionDeleted(distributionId);
136 }
137 }),
138 { timeout: 2_400_000 },
139 );
140 },
141 );
142
143 const assertDistributionDeleted = (distributionId: string) =>
144 cloudfront.getDistribution({ Id: distributionId }).pipe(
145 Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))),
146 Effect.catchTag("NoSuchDistribution", () => Effect.void),
147 Effect.retry({
148 while: (error): boolean =>
149 error instanceof Error && error.message === "DistributionStillExists",
150 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]),
151 }),
152 );