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

main
ts 173 lines 5.82 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Function } from "@alchemy.run/aws/CloudFront";
3 import { 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 Drift from "alchemy/Drift";
7 import * as Provider from "alchemy/Provider";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 test.provider(
14 "create and delete a CloudFront Function with key value store associations",
15 (stack) =>
16 Effect.gen(function* () {
17 yield* stack.destroy();
18
19 const deployed = yield* stack.deploy(
20 Effect.gen(function* () {
21 const store = yield* AWS.CloudFront.KeyValueStore("RequestStore", {
22 comment: "request metadata",
23 });
24 const fn = yield* AWS.CloudFront.Function("RequestFn", {
25 comment: "request handler",
26 keyValueStoreArns: [store.keyValueStoreArn],
27 code: `async function handler(event) {
28 return event.request;
29 }`,
30 });
31 return { store, fn };
32 }),
33 );
34
35 const current = yield* cloudfront.describeFunction({
36 Name: deployed.fn.functionName,
37 Stage: "LIVE",
38 });
39 expect(current.FunctionSummary?.Name).toEqual(deployed.fn.functionName);
40 expect(
41 current.FunctionSummary?.FunctionConfig.KeyValueStoreAssociations?.Items?.[0]
42 ?.KeyValueStoreARN,
43 ).toEqual(deployed.store.keyValueStoreArn);
44
45 yield* stack.destroy();
46 yield* assertFunctionDeleted(deployed.fn.functionName);
47 }),
48 {
49 tags: ["provider:aws", "provider:aws:cloudfront", "live"],
50 timeout: 300_000,
51 },
52 );
53
54 // `FunctionConfig.Comment` is patched optional in
55 // submodules/distilled/packages/aws/patches/cloudfront.json — CloudFront omits `Comment`
56 // in `listFunctions` responses for functions created without one, and a
57 // comment-less function anywhere in the account used to fail the decode with
58 // `SchemaError: Missing key ... FunctionConfig.Comment`.
59 test.provider(
60 "list enumerates the deployed CloudFront Function",
61 (stack) =>
62 Effect.gen(function* () {
63 yield* stack.destroy();
64
65 const deployed = yield* stack.deploy(
66 Effect.gen(function* () {
67 return yield* AWS.CloudFront.Function("ListFn", {
68 comment: "list handler",
69 code: `async function handler(event) {
70 return event.request;
71 }`,
72 });
73 }),
74 );
75
76 const provider = yield* Provider.findProvider(Function);
77 const all = yield* provider.list();
78
79 expect(all.some((fn) => fn.functionName === deployed.functionName)).toBe(true);
80
81 yield* stack.destroy();
82 yield* assertFunctionDeleted(deployed.functionName);
83 }),
84 {
85 tags: ["provider:aws", "provider:aws:cloudfront", "live"],
86 timeout: 300_000,
87 },
88 );
89
90 // Regression for #2349: a function's `Status` is rollout state. It reads
91 // `UNASSOCIATED` until a distribution uses it, `IN_PROGRESS` right after a
92 // publish, and `DEPLOYED` once CloudFront finishes the rollout. Drift must not
93 // report a change when only that status moved.
94 test.provider.skipIf(!!process.env.FAST)(
95 "drift reports no change once an associated function finishes rolling out",
96 (stack) =>
97 Effect.gen(function* () {
98 yield* stack.destroy();
99
100 const program = (body: string) =>
101 Effect.gen(function* () {
102 const fn = yield* AWS.CloudFront.Function("RolloutFn", {
103 comment: "rollout handler",
104 code: `async function handler(event) {
105 ${body}
106 return event.request;
107 }`,
108 });
109 const distribution = yield* AWS.CloudFront.Distribution("RolloutDistribution", {
110 origins: [
111 {
112 id: "site",
113 domainName: "example.com",
114 customOriginConfig: { originProtocolPolicy: "https-only" },
115 },
116 ],
117 defaultCacheBehavior: {
118 targetOriginId: "site",
119 viewerProtocolPolicy: "redirect-to-https",
120 functionAssociations: [{ functionArn: fn.functionArn, eventType: "viewer-request" }],
121 },
122 });
123 return { fn, distribution };
124 });
125
126 const expectNoFunctionDrift = Effect.gen(function* () {
127 const live = yield* cloudfront
128 .describeFunction({ Name: deployed.fn.functionName, Stage: "LIVE" })
129 .pipe(
130 Effect.repeat({
131 schedule: Schedule.spaced("5 seconds"),
132 until: (fn) => fn.FunctionSummary?.Status === "DEPLOYED",
133 times: 60,
134 }),
135 );
136 expect(live.FunctionSummary?.Status).toBe("DEPLOYED");
137
138 const drift = yield* Drift.detect({ name: stack.name, stage: stack.stage }).pipe(
139 Effect.provide(stack.state),
140 );
141 expect(drift.resources.RolloutFn?.action).toBe("unchanged");
142 });
143
144 const deployed = yield* stack.deploy(program("// v1"));
145 yield* expectNoFunctionDrift;
146
147 // A code change re-publishes LIVE, which starts a new rollout.
148 yield* stack.deploy(program("// v2"));
149 yield* expectNoFunctionDrift;
150
151 yield* stack.destroy();
152 yield* assertFunctionDeleted(deployed.fn.functionName);
153 }),
154 {
155 tags: ["provider:aws", "provider:aws:cloudfront", "live"],
156 timeout: 900_000,
157 },
158 );
159
160 const assertFunctionDeleted = (name: string) =>
161 cloudfront
162 .describeFunction({
163 Name: name,
164 Stage: "LIVE",
165 })
166 .pipe(
167 Effect.flatMap(() => Effect.fail(new Error("FunctionStillExists"))),
168 Effect.catchTag("NoSuchFunctionExists", () => Effect.void),
169 Effect.retry({
170 while: (error) => error instanceof Error && error.message === "FunctionStillExists",
171 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]),
172 }),
173 );