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

main
ts 97 lines 3.75 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 S3 from "@distilled.cloud/aws/s3";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import * as pathe from "pathe";
9 import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // Gated with the rest of the AWS.Website suites: the CloudFront lifecycle
14 // dominates the runtime.
15 const runLive = !process.env.FAST;
16
17 // Built in place: vinext and its RSC toolchain resolve only from the
18 // example's own node_modules (build output is gitignored there).
19 const rootDir = pathe.resolve(import.meta.dirname, "../../../../../examples/aws-website-vinext");
20
21 const memo = {
22 include: ["app/**", "public/**", "package.json", "vite.config.ts", "tsconfig.json"],
23 };
24
25 describe.skipIf(!runLive)(
26 "AWS.Website.Vinext",
27 { tags: ["provider:aws", "provider:aws:cloudfront", "provider:aws:website", "live"] },
28 () => {
29 test.provider(
30 "deploys App Router SSR on a Lambda Web Function with the S3 data cache",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const deployed = yield* stack.deploy(
36 Effect.gen(function* () {
37 const site = yield* AWS.Website.Vinext("VinextSite", {
38 rootDir,
39 memo,
40 forceDestroy: true,
41 invalidation: { paths: "all", wait: true },
42 env: { GREETING: "Hello from vinext on a Web Function!" },
43 });
44 return { site };
45 }),
46 );
47
48 const url = deployed.site.url! as string;
49 // The server is a Lambda Web Function (the default).
50 expect(deployed.site.serverUrl).toContain(".lambda-web.");
51
52 // Directly from the Web Function, then through CloudFront.
53 yield* expectUrlContains(`${deployed.site.serverUrl!}`, "Hello from vinext on AWS!", {
54 timeout: "120 seconds",
55 label: "SSR direct from the Web Function",
56 });
57 yield* expectUrlContains(`${url}/`, "Hello from vinext on AWS!", {
58 timeout: "180 seconds",
59 label: "SSR page",
60 });
61 // A route handler reading the server's environment.
62 yield* expectUrlContains(
63 `${url}/api/hello?name=Alchemy`,
64 "Hello from vinext on a Web Function!",
65 { label: "route handler env" },
66 );
67
68 // The data cache writes to its S3 bucket from the Web Function.
69 const cacheBucket = deployed.site.cacheBucket!.bucketName;
70 const cached = yield* S3.listObjectsV2({ Bucket: cacheBucket }).pipe(
71 Effect.map((r) => r.KeyCount ?? 0),
72 Effect.repeat({
73 schedule: Schedule.spaced("3 seconds"),
74 until: (count) => count > 0,
75 times: 20,
76 }),
77 );
78 expect(cached).toBeGreaterThan(0);
79
80 const distributionId = deployed.site.distribution!.distributionId;
81 yield* stack.destroy();
82 yield* assertDistributionDeleted(distributionId);
83 }),
84 { timeout: 2_400_000 },
85 );
86 },
87 );
88
89 const assertDistributionDeleted = (distributionId: string) =>
90 cloudfront.getDistribution({ Id: distributionId }).pipe(
91 Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))),
92 Effect.catchTag("NoSuchDistribution", () => Effect.void),
93 Effect.retry({
94 while: (error) => error instanceof Error && error.message === "DistributionStillExists",
95 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]),
96 }),
97 );