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

main
ts 164 lines 7.03 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 s3 from "@distilled.cloud/aws/s3";
5 import * as Effect from "effect/Effect";
6 import * as Stream from "effect/Stream";
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: CloudFront Distribution create blocks on Status === "Deployed"
14 // (~5-15 min) and destroy requires disable -> wait -> delete (another
15 // ~5-15 min). Skipped under --fast (FAST=1) (same gate
16 // as the AWS.CloudFront suites).
17 const runLive = !process.env.FAST;
18
19 // The emulated pipeline test runs under the floci runner
20 // (`pnpm test:aws:floci`), where every pipeline provider resolves its local
21 // (emulator-backed) variant.
22 const runEmulated = process.env.ALCHEMY_TEST_DEV === "1";
23
24 const viteFixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "vite-app");
25 const staticFixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "static-site");
26
27 // Clone under the alchemy package so `vite` resolves from the workspace's
28 // hoisted node_modules (the fixture has no node_modules).
29 const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
30
31 // Also skipped under the floci runner (`runEmulated`): in dev the composite
32 // deploys nothing but the vite dev server, so the live CloudFront topology
33 // this test asserts never exists. The emulated pipeline test below and
34 // Vite.local.test.ts cover the dev-mode behavior.
35 describe.skipIf(!runLive || runEmulated)(
36 "AWS.Website.Vite",
37 { tags: ["provider:aws", "provider:aws:website", "live"] },
38 () => {
39 test.provider(
40 "deploys the vite build to S3 behind CloudFront with SPA fallback",
41 (stack) =>
42 Effect.gen(function* () {
43 yield* stack.destroy();
44
45 const rootDir = yield* cloneFixture(viteFixtureDir, {
46 prefix: "alchemy-vite-aws-live-",
47 tempRoot,
48 entries: [".gitignore", "package.json", "index.html", "src", "public"],
49 });
50
51 const deployed = yield* stack.deploy(
52 Effect.gen(function* () {
53 const site = yield* AWS.Website.Vite("ViteSite", {
54 rootDir,
55 // Reaches the production build: Vite inlines `VITE_*` vars.
56 env: { VITE_SITE_ENV: "VITE_AWS_SITE_ENV_MARKER" },
57 forceDestroy: true,
58 invalidation: { paths: "all", wait: true },
59 });
60 return { site };
61 }),
62 );
63
64 const url = deployed.site.url! as string;
65 expect(url).toMatch(/^https:\/\//);
66 // Assets-only: the composite never creates a server function.
67 expect(deployed.site.server).toBeUndefined();
68 expect(deployed.site.serverUrl).toBeUndefined();
69
70 // The built index page serves from the edge.
71 yield* expectUrlContains(`${url}/`, "VITE_AWS_PAGE_MARKER", {
72 timeout: "180 seconds",
73 label: "index",
74 });
75 // The site env was inlined into the built client bundle.
76 const bucketName = deployed.site.bucket!.bucketName as string;
77 const listed = yield* s3.listObjectsV2({ Bucket: bucketName });
78 const bundles = (listed.Contents ?? []).flatMap((object) =>
79 object.Key?.endsWith(".js") ? [object.Key] : [],
80 );
81 const sources = yield* Effect.forEach(bundles, (Key) =>
82 s3
83 .getObject({ Bucket: bucketName, Key })
84 .pipe(
85 Effect.flatMap((object) => object.Body!.pipe(Stream.decodeText, Stream.mkString)),
86 ),
87 );
88 expect(sources.some((source) => source.includes("VITE_AWS_SITE_ENV_MARKER"))).toBe(true);
89 // publicDir passthrough landed in the bucket.
90 yield* expectUrlContains(`${url}/robots.txt`, "User-agent", { label: "public asset" });
91 // SPA fallback (the composite's default): misses serve the shell.
92 yield* expectUrlContains(`${url}/missing/client/route`, "VITE_AWS_PAGE_MARKER", {
93 label: "spa fallback",
94 });
95
96 yield* stack.destroy();
97 }),
98 { timeout: 2_400_000 },
99 );
100 },
101 );
102
103 // The Vite composite intentionally never deploys the static pipeline under
104 // `alchemy dev` (dev IS the vite dev server — see Vite.local.test.ts), so
105 // its deploy path cannot be exercised in a dev-mode test. This test pins the
106 // SAME underlying pipeline (`makeKvSite`: bucket + AssetDeployment +
107 // KvEntries + KvRoutesUpdate + distribution + invalidation) against the
108 // emulator via StaticSite, which deploys the full pipeline in dev when no
109 // `dev` prop is set. This is the regression net for non-dualized pipeline
110 // providers — the class of bug that previously only surfaced in
111 // `alchemy dev` of the examples (Files/KvEntries/RoutesUpdate hitting real
112 // AWS from an emulated stack).
113 describe.skipIf(!runEmulated)(
114 "AWS.Website static pipeline (emulated)",
115 { tags: ["provider:aws", "provider:aws:s3", "provider:aws:website", "live"] },
116 () => {
117 test.provider(
118 "deploys the full S3 + CloudFront + KVS pipeline against the emulator",
119 (stack) =>
120 Effect.gen(function* () {
121 yield* stack.destroy();
122
123 const deployed = yield* stack.deploy(
124 Effect.gen(function* () {
125 const site = yield* AWS.Website.StaticSite("PipelineSite", {
126 path: staticFixtureDir,
127 spa: true,
128 forceDestroy: true,
129 invalidation: { paths: "all" },
130 });
131 return { site };
132 }),
133 );
134
135 // Every pipeline resource reconciled against the emulator: the
136 // distribution identity has AWS shape but was minted locally.
137 const bucketName = deployed.site.bucket!.bucketName as string;
138 expect(bucketName).toBeTruthy();
139 expect(deployed.site.distribution!.distributionId).toBeTruthy();
140 expect(deployed.site.files!.version).toBeTruthy();
141
142 // Out-of-band proof the assets landed in the emulator's S3 (the
143 // harness pins distilled to the emulator in dev runs).
144 const listed = yield* s3.listObjectsV2({ Bucket: bucketName });
145 const keys = (listed.Contents ?? []).flatMap((object) =>
146 object.Key !== undefined ? [object.Key] : [],
147 );
148 expect(keys.some((key) => key.endsWith("index.html"))).toBe(true);
149 expect(keys.some((key) => key.endsWith("styles.css"))).toBe(true);
150 expect(keys.some((key) => key.endsWith("about.html"))).toBe(true);
151
152 yield* stack.destroy();
153
154 // The bucket is gone from the emulator after destroy.
155 const gone = yield* s3.listObjectsV2({ Bucket: bucketName }).pipe(
156 Effect.as(false),
157 Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)),
158 );
159 expect(gone).toBe(true);
160 }),
161 { timeout: 300_000 },
162 );
163 },
164 );