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

main
ts 110 lines 4.67 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 Output from "alchemy/Output";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // Anchor the fixture to the repo root regardless of the runner's cwd.
14 const fixtureDir = fileURLToPath(
15 new URL("../../../../../examples/aws-static-site/site", import.meta.url),
16 );
17
18 // Gated: CloudFront Distribution create blocks on Status === "Deployed"
19 // (~5-15 min) and destroy requires disable -> wait -> delete (another
20 // ~5-15 min), so the full Router lifecycle exceeds any sane test budget.
21 // Skipped under --fast (FAST=1) (same gate as the
22 // AWS.CloudFront suites).
23 const runLive = !process.env.FAST;
24
25 describe.skipIf(!runLive)(
26 "AWS.Website.Router",
27 { tags: ["provider:aws", "provider:aws:cloudfront", "provider:aws:website", "live"] },
28 () => {
29 test.provider(
30 "create router with static-site attached via KV routing",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const deployed = yield* stack.deploy(
36 Effect.gen(function* () {
37 const router = yield* AWS.Website.Router("Router", {
38 invalidation: { paths: "all", wait: true },
39 routes: {
40 // An Output url, like `api.functionUrl` — resolved at
41 // deploy time rather than parsed up front.
42 "/api/*": {
43 url: Output.literal("https://example.com/"),
44 rewrite: { regex: "^/api/.*$", to: "/" },
45 },
46 },
47 });
48
49 const site = yield* AWS.Website.StaticSite("DocsSite", {
50 path: fixtureDir,
51 forceDestroy: true,
52 domain: { router },
53 });
54
55 return { site, router };
56 }),
57 );
58
59 expect(deployed.router.distribution.distributionId).toBeDefined();
60 expect(deployed.router.kvStoreArn).toBeDefined();
61
62 // urls contract (cloudfront-default arm): a router without a
63 // domain serves only at the distribution's own URL (its CloudFront
64 // default domain live, a local edge port under the emulator), and
65 // `url` is always `urls[0]`.
66 expect(deployed.router.urls).toEqual([deployed.router.distribution.url]);
67 expect(deployed.router.url).toBe(deployed.router.urls[0]);
68 // A path-only attached site inherits the router's primary URL.
69 expect(deployed.site.urls).toEqual([deployed.router.url]);
70 expect(deployed.site.url).toBe(deployed.site.urls[0]);
71
72 const config = yield* cloudfront.getDistributionConfig({
73 Id: deployed.router.distribution.distributionId,
74 });
75 expect(
76 config.DistributionConfig?.DefaultCacheBehavior?.FunctionAssociations?.Quantity,
77 ).toBeGreaterThanOrEqual(1);
78
79 // The url route wins over the site at `/` and reaches its origin
80 // instead of falling through to the placeholder (#1645).
81 yield* expectUrlContains(`${deployed.router.url}/api/anything`, "Example Domain", {
82 timeout: "120 seconds",
83 label: "url route",
84 });
85
86 yield* stack.destroy();
87 yield* assertDistributionDeleted(deployed.router.distribution.distributionId);
88 }),
89 // Create waits for Status === "Deployed" (~5 min) and destroy is
90 // disable -> wait -> delete (~5-15 min more): 600s was measured too
91 // small — the run died mid-destroy with green assertions.
92 // CloudFront full lifecycle (create + KV-routed assertions + disable +
93 // delete) measures ~6m with bounded polls; generous headroom for
94 // propagation variance. If this ever times out mid-destroy again,
95 // suspect a hung poll first (see the Effect.timeout guards in
96 // Distribution.ts), not CloudFront.
97 { timeout: 1_500_000 },
98 );
99 },
100 );
101
102 const assertDistributionDeleted = (distributionId: string) =>
103 cloudfront.getDistribution({ Id: distributionId }).pipe(
104 Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))),
105 Effect.catchTag("NoSuchDistribution", () => Effect.void),
106 Effect.retry({
107 while: (error) => error instanceof Error && error.message === "DistributionStillExists",
108 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]),
109 }),
110 );