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

main
ts 196 lines 6.19 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { RealtimeLogConfig } 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 Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 // The role CloudFront assumes to deliver real-time log records to Kinesis.
12 const logDeliveryRole = (stream: AWS.Kinesis.Stream) =>
13 AWS.IAM.Role("RealtimeLogRole", {
14 assumeRolePolicyDocument: {
15 Version: "2012-10-17",
16 Statement: [
17 {
18 Effect: "Allow",
19 Principal: { Service: "cloudfront.amazonaws.com" },
20 Action: ["sts:AssumeRole"],
21 },
22 ],
23 },
24 inlinePolicies: {
25 KinesisWrite: {
26 Version: "2012-10-17",
27 Statement: [
28 {
29 Effect: "Allow",
30 Action: [
31 "kinesis:DescribeStreamSummary",
32 "kinesis:DescribeStream",
33 "kinesis:PutRecord",
34 "kinesis:PutRecords",
35 ],
36 Resource: [stream.streamArn],
37 },
38 ],
39 },
40 },
41 });
42
43 const assertConfigGone = (name: string) =>
44 cloudfront.getRealtimeLogConfig({ Name: name }).pipe(
45 Effect.flatMap(() => Effect.fail(new Error("realtime log config still exists"))),
46 Effect.catchTag("NoSuchRealtimeLogConfig", () => Effect.void),
47 Effect.retry({
48 while: (e) => e instanceof Error,
49 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
50 }),
51 );
52
53 test.provider(
54 "create, update in place, and delete realtime log config",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 // Create.
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const stream = yield* AWS.Kinesis.Stream("EdgeLogStream", {});
63 const role = yield* logDeliveryRole(stream);
64 return yield* RealtimeLogConfig("EdgeLogConfig", {
65 samplingRate: 100,
66 fields: ["timestamp", "c-ip", "cs-uri-stem", "sc-status"],
67 endpoints: [{ streamArn: stream.streamArn, roleArn: role.roleArn }],
68 });
69 }),
70 );
71
72 expect(created.arn).toContain("realtime-log-config");
73 expect(created.samplingRate).toBe(100);
74 // CloudFront canonicalizes (reorders) the field list.
75 expect([...created.fields].sort()).toEqual(
76 ["timestamp", "c-ip", "cs-uri-stem", "sc-status"].sort(),
77 );
78 expect(created.endpoints).toHaveLength(1);
79
80 // Out-of-band verification.
81 const observed = yield* cloudfront.getRealtimeLogConfig({
82 Name: created.name,
83 });
84 expect(observed.RealtimeLogConfig?.ARN).toBe(created.arn);
85 expect(observed.RealtimeLogConfig?.SamplingRate).toBe(100);
86
87 // Update sampling rate + fields in place (same ARN).
88 const updated = yield* stack.deploy(
89 Effect.gen(function* () {
90 const stream = yield* AWS.Kinesis.Stream("EdgeLogStream", {});
91 const role = yield* logDeliveryRole(stream);
92 return yield* RealtimeLogConfig("EdgeLogConfig", {
93 samplingRate: 25,
94 fields: ["timestamp", "c-ip"],
95 endpoints: [{ streamArn: stream.streamArn, roleArn: role.roleArn }],
96 });
97 }),
98 );
99
100 expect(updated.arn).toBe(created.arn);
101 expect(updated.samplingRate).toBe(25);
102 expect([...updated.fields].sort()).toEqual(["timestamp", "c-ip"].sort());
103
104 const observed2 = yield* cloudfront.getRealtimeLogConfig({
105 Name: created.name,
106 });
107 expect(observed2.RealtimeLogConfig?.SamplingRate).toBe(25);
108 expect([...(observed2.RealtimeLogConfig?.Fields ?? [])].sort()).toEqual(
109 ["timestamp", "c-ip"].sort(),
110 );
111
112 yield* stack.destroy();
113 yield* assertConfigGone(created.name);
114 }),
115 {
116 tags: [
117 "provider:aws",
118 "provider:aws:cloudfront",
119 "provider:aws:iam",
120 "provider:aws:kinesis",
121 "live",
122 ],
123 timeout: 180_000,
124 },
125 );
126
127 test.provider(
128 "changing the name replaces the realtime log config",
129 (stack) =>
130 Effect.gen(function* () {
131 yield* stack.destroy();
132
133 const make = (name: string) =>
134 Effect.gen(function* () {
135 const stream = yield* AWS.Kinesis.Stream("ReplaceLogStream", {});
136 const role = yield* AWS.IAM.Role("ReplaceLogRole", {
137 assumeRolePolicyDocument: {
138 Version: "2012-10-17",
139 Statement: [
140 {
141 Effect: "Allow",
142 Principal: { Service: "cloudfront.amazonaws.com" },
143 Action: ["sts:AssumeRole"],
144 },
145 ],
146 },
147 inlinePolicies: {
148 KinesisWrite: {
149 Version: "2012-10-17",
150 Statement: [
151 {
152 Effect: "Allow",
153 Action: [
154 "kinesis:DescribeStreamSummary",
155 "kinesis:DescribeStream",
156 "kinesis:PutRecord",
157 "kinesis:PutRecords",
158 ],
159 Resource: [stream.streamArn],
160 },
161 ],
162 },
163 },
164 });
165 return yield* RealtimeLogConfig("ReplaceLogConfig", {
166 name,
167 samplingRate: 100,
168 fields: ["timestamp"],
169 endpoints: [{ streamArn: stream.streamArn, roleArn: role.roleArn }],
170 });
171 });
172
173 const created = yield* stack.deploy(make("alchemy-test-rtlc-a"));
174 expect(created.name).toBe("alchemy-test-rtlc-a");
175
176 const replaced = yield* stack.deploy(make("alchemy-test-rtlc-b"));
177 expect(replaced.name).toBe("alchemy-test-rtlc-b");
178 expect(replaced.arn).not.toBe(created.arn);
179
180 // The old configuration must be gone.
181 yield* assertConfigGone("alchemy-test-rtlc-a");
182
183 yield* stack.destroy();
184 yield* assertConfigGone("alchemy-test-rtlc-b");
185 }),
186 {
187 tags: [
188 "provider:aws",
189 "provider:aws:cloudfront",
190 "provider:aws:iam",
191 "provider:aws:kinesis",
192 "live",
193 ],
194 timeout: 180_000,
195 },
196 );