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

main
ts 122 lines 5.08 KB
Raw
1 import { fileURLToPath } from "node:url";
2 import * as AWS from "@alchemy.run/aws";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as cloudfront from "@distilled.cloud/aws/cloudfront";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import { expectDirectStatus, expectUrlContains } from "../../Cloudflare/Utils/Http.ts";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 // Gated: CloudFront Distribution create blocks on Status === "Deployed"
13 // (~5-15 min) and destroy requires disable -> wait -> delete (another
14 // ~5-15 min). Skipped under --fast (FAST=1) (same gate
15 // as the AWS.CloudFront suites).
16 const runLive = !process.env.FAST;
17
18 // Under the floci runner the full pipeline (bucket + files + KVS +
19 // distribution) deploys against the emulator, which serves the
20 // distribution's edge on a local plain-HTTP port — so the whole test runs
21 // in both modes; only the URL-shape assertions differ.
22 const runEmulated = process.env.ALCHEMY_TEST_DEV === "1";
23
24 const fixtureDir = fileURLToPath(new URL("../Website/fixtures/static-site", import.meta.url));
25
26 const INDEX_MARKER = "alchemy-aws-staticsite-index-marker";
27 const ABOUT_MARKER = "alchemy-aws-staticsite-about-marker";
28 const CSS_MARKER = "alchemy-aws-staticsite-css-marker";
29
30 describe.skipIf(!runLive)(
31 "AWS.Website.StaticSite",
32 { tags: ["provider:aws", "provider:aws:cloudfront", "provider:aws:website", "live"] },
33 () => {
34 test.provider(
35 "standalone SPA serves index, pretty URLs, and falls back on misses; errorPage update returns real 404s",
36 (stack) =>
37 Effect.gen(function* () {
38 yield* stack.destroy();
39
40 // Phase 1: standalone SPA. Misses fall back to index.html with 200.
41 const spa = yield* stack.deploy(
42 Effect.gen(function* () {
43 const site = yield* AWS.Website.StaticSite("Site", {
44 path: fixtureDir,
45 spa: true,
46 forceDestroy: true,
47 invalidation: { paths: "all", wait: true },
48 });
49 return { site };
50 }),
51 );
52
53 const url = spa.site.url! as string;
54 // `https://{id}.cloudfront.net` live; the emulator serves the
55 // distribution's edge on a local plain-HTTP port.
56 expect(url).toMatch(runEmulated ? /^http:\/\/localhost:\d+/ : /^https:\/\//);
57
58 // urls contract (cloudfront-default arm): a domain-less site
59 // serves only at the distribution's own URL, and `url` is
60 // always `urls[0]`.
61 expect(spa.site.urls).toEqual([spa.site.distribution!.url]);
62 expect(url).toBe(spa.site.urls[0]);
63
64 // Root serves the index page. Generous first-request budget: the
65 // distribution just reached Deployed but edge propagation can lag.
66 yield* expectUrlContains(`${url}/`, INDEX_MARKER, {
67 timeout: "180 seconds",
68 label: "index",
69 });
70 // Exact asset paths hit S3 (proves the OAC bucket policy works).
71 yield* expectUrlContains(`${url}/styles.css`, CSS_MARKER, { label: "css asset" });
72 // Pretty URL: /about -> about.html via the edge KV lookup.
73 yield* expectUrlContains(`${url}/about`, ABOUT_MARKER, { label: "pretty url" });
74 // SPA fallback: unknown path serves the app shell with a 200.
75 yield* expectUrlContains(`${url}/missing/client/route`, INDEX_MARKER, {
76 label: "spa fallback",
77 });
78
79 // Phase 2: switch to a real-404 static site with an error page.
80 const site404 = yield* stack.deploy(
81 Effect.gen(function* () {
82 const site = yield* AWS.Website.StaticSite("Site", {
83 path: fixtureDir,
84 errorPage: "404.html",
85 forceDestroy: true,
86 invalidation: { paths: "all", wait: true },
87 });
88 return { site };
89 }),
90 );
91
92 const url404 = site404.site.url! as string;
93 // Content still serves.
94 yield* expectUrlContains(`${url404}/about`, ABOUT_MARKER, {
95 label: "pretty url after update",
96 });
97 // Misses now return a real 404 status.
98 yield* expectDirectStatus(`${url404}/definitely/not/here`, 404, {
99 timeout: "120 seconds",
100 label: "404 status",
101 });
102
103 const distributionId = site404.site.distribution!.distributionId;
104
105 yield* stack.destroy();
106 yield* assertDistributionDeleted(distributionId);
107 }),
108 { timeout: 2_400_000 },
109 );
110 },
111 );
112
113 const assertDistributionDeleted = (distributionId: string) =>
114 cloudfront.getDistribution({ Id: distributionId }).pipe(
115 Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))),
116 Effect.catchTag("NoSuchDistribution", () => Effect.void),
117 Effect.retry({
118 while: (error): boolean =>
119 error instanceof Error && error.message === "DistributionStillExists",
120 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]),
121 }),
122 );