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

main
ts 130 lines 4.93 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { OriginRequestPolicy } from "@alchemy.run/aws/CloudFront";
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 Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 describe(
13 "AWS.CloudFront.OriginRequestPolicy",
14 { tags: ["provider:aws", "provider:aws:cloudfront", "live"] },
15 () => {
16 test.provider(
17 "create, update, and delete an origin request policy",
18 (stack) =>
19 Effect.gen(function* () {
20 yield* stack.destroy();
21
22 const created = yield* stack.deploy(
23 Effect.gen(function* () {
24 return yield* OriginRequestPolicy("AppOriginRequest", {
25 comment: "initial",
26 headersConfig: {
27 HeaderBehavior: "whitelist",
28 Headers: { Quantity: 1, Items: ["Authorization"] },
29 },
30 cookiesConfig: { CookieBehavior: "none" },
31 queryStringsConfig: { QueryStringBehavior: "all" },
32 });
33 }),
34 );
35
36 const initial = yield* cloudfront.getOriginRequestPolicy({
37 Id: created.originRequestPolicyId,
38 });
39 expect(initial.OriginRequestPolicy?.Id).toEqual(created.originRequestPolicyId);
40 expect(initial.OriginRequestPolicy?.OriginRequestPolicyConfig?.Comment).toEqual(
41 "initial",
42 );
43 expect(
44 initial.OriginRequestPolicy?.OriginRequestPolicyConfig?.HeadersConfig?.Headers?.Items,
45 ).toEqual(["Authorization"]);
46
47 const updated = yield* stack.deploy(
48 Effect.gen(function* () {
49 return yield* OriginRequestPolicy("AppOriginRequest", {
50 comment: "updated",
51 headersConfig: {
52 HeaderBehavior: "whitelist",
53 Headers: { Quantity: 2, Items: ["Authorization", "Accept-Language"] },
54 },
55 cookiesConfig: { CookieBehavior: "all" },
56 queryStringsConfig: { QueryStringBehavior: "all" },
57 });
58 }),
59 );
60
61 expect(updated.originRequestPolicyId).toEqual(created.originRequestPolicyId);
62
63 // Control-plane reads are eventually consistent — poll until the
64 // update is visible, then assert.
65 const after = yield* cloudfront
66 .getOriginRequestPolicy({ Id: updated.originRequestPolicyId })
67 .pipe(
68 Effect.repeat({
69 schedule: Schedule.fixed("2 seconds"),
70 until: (r) =>
71 r.OriginRequestPolicy?.OriginRequestPolicyConfig?.Comment === "updated",
72 times: 15,
73 }),
74 );
75 expect(after.OriginRequestPolicy?.OriginRequestPolicyConfig?.Comment).toEqual("updated");
76 expect(
77 after.OriginRequestPolicy?.OriginRequestPolicyConfig?.HeadersConfig?.Headers?.Items,
78 ).toEqual(["Authorization", "Accept-Language"]);
79 expect(
80 after.OriginRequestPolicy?.OriginRequestPolicyConfig?.CookiesConfig?.CookieBehavior,
81 ).toEqual("all");
82
83 yield* stack.destroy();
84 yield* assertOriginRequestPolicyDeleted(updated.originRequestPolicyId);
85 }),
86 300_000,
87 );
88
89 test.provider(
90 "list enumerates the deployed origin request policy",
91 (stack) =>
92 Effect.gen(function* () {
93 yield* stack.destroy();
94
95 const deployed = yield* stack.deploy(
96 Effect.gen(function* () {
97 return yield* OriginRequestPolicy("ListOriginRequest", {
98 comment: "list",
99 headersConfig: { HeaderBehavior: "none" },
100 cookiesConfig: { CookieBehavior: "none" },
101 queryStringsConfig: { QueryStringBehavior: "none" },
102 });
103 }),
104 );
105
106 const provider = yield* Provider.findProvider(OriginRequestPolicy);
107 const all = yield* provider.list();
108
109 expect(all.some((p) => p.originRequestPolicyId === deployed.originRequestPolicyId)).toBe(
110 true,
111 );
112
113 yield* stack.destroy();
114 yield* assertOriginRequestPolicyDeleted(deployed.originRequestPolicyId);
115 }),
116 { timeout: 300_000 },
117 );
118 },
119 );
120
121 const assertOriginRequestPolicyDeleted = (id: string) =>
122 cloudfront.getOriginRequestPolicy({ Id: id }).pipe(
123 Effect.flatMap(() => Effect.fail(new Error("OriginRequestPolicyStillExists"))),
124 Effect.catchTag("NoSuchOriginRequestPolicy", () => Effect.void),
125 Effect.retry({
126 while: (error) =>
127 error instanceof Error && error.message === "OriginRequestPolicyStillExists",
128 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]),
129 }),
130 );