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

main
ts 407 lines 15.8 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 Core from "@alchemy.run/test/Core";
5 import * as lambda from "@distilled.cloud/aws/lambda";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Schedule from "effect/Schedule";
11 import CloudWatchTestFunctionLive, { CloudWatchTestFunction, METRIC_NAME } from "./handler";
12
13 // Runtime binding coverage for every CloudWatch capability that can be
14 // exercised against fixture-deployable resources. Two bindings have no
15 // route here (documented, not silently dropped):
16 //
17 // - GetAlarmMuteRule — binding requires a deployed AlarmMuteRule, but
18 // `putAlarmMuteRule` currently rejects every input with an empty-message
19 // ValidationException (see AlarmMuteRule.test.ts). ListAlarmMuteRules is
20 // covered below without a resource.
21 // - GetMetricStream / StartMetricStreams / StopMetricStreams — bindings
22 // require a deployed MetricStream, which needs a live Firehose delivery
23 // stream + IAM roles and is gated behind AWS_TEST_METRICSTREAM at the
24 // resource level (see MetricStream.test.ts). ListMetricStreams is covered
25 // below without a resource.
26
27 const testOptions = { providers: AWS.providers() };
28 const { test, beforeAll, afterAll } = Test.make(testOptions);
29 const sharedStack = Core.scratchStack(testOptions, "CloudWatchBindings");
30
31 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
32 // over 60s on a fresh deploy under parallel-suite load. Budget ~150s of
33 // readiness polling so we don't fail the whole suite on a slow init.
34 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
35
36 let baseUrl: string;
37 let fixtureFunctionName: string | undefined;
38
39 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
40 readonly status: number;
41 readonly body: string;
42 }> {}
43
44 // The shared Lambda fixture occasionally answers a transient 5xx under
45 // full-suite parallel load — a cold re-init or an IAM-propagation race the
46 // handler's `Effect.orDie` surfaces as a 500. Only 5xx is retried; a genuine
47 // 4xx/assertion failure is returned immediately.
48 const send = (request: HttpClientRequest.HttpClientRequest) =>
49 HttpClient.execute(request).pipe(
50 Effect.flatMap((response) =>
51 response.status >= 500
52 ? response.text.pipe(
53 Effect.flatMap((body) =>
54 Effect.fail(new TransientUpstream({ status: response.status, body })),
55 ),
56 )
57 : Effect.succeed(response),
58 ),
59 Effect.retry({
60 while: (e) => e._tag === "TransientUpstream",
61 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
62 }),
63 );
64
65 const getJson = (path: string) =>
66 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
67
68 const postJson = (path: string) =>
69 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
70
71 describe(
72 "CloudWatch Bindings",
73 { tags: ["provider:aws", "provider:aws:cloudwatch", "provider:aws:lambda", "live"] },
74 () => {
75 beforeAll(
76 Effect.gen(function* () {
77 yield* Effect.logInfo("CloudWatch test setup: destroying previous resources");
78 yield* sharedStack.destroy();
79
80 yield* Effect.logInfo("CloudWatch test setup: deploying fixture");
81 const { functionUrl, functionName } = yield* sharedStack.deploy(
82 Effect.gen(function* () {
83 return yield* CloudWatchTestFunction;
84 }).pipe(Effect.provide(CloudWatchTestFunctionLive)),
85 );
86
87 expect(functionUrl).toBeTruthy();
88 baseUrl = functionUrl!.replace(/\/+$/, "");
89 fixtureFunctionName = functionName;
90
91 yield* Effect.logInfo(`CloudWatch test setup: probing readiness at ${baseUrl}/health`);
92 yield* HttpClient.get(`${baseUrl}/health`).pipe(
93 Effect.flatMap((response) =>
94 response.status === 200
95 ? Effect.succeed(response)
96 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
97 ),
98 Effect.retry({ schedule: readinessPolicy }),
99 );
100 }),
101 { timeout: 240_000 },
102 );
103
104 afterAll(
105 Effect.gen(function* () {
106 yield* sharedStack.destroy();
107 // Out-of-band assert-gone: the fixture Lambda (root of the deployed
108 // stack) no longer exists after the trailing destroy. `afterAll`
109 // doesn't run inside `test.provider`'s environment, so provide the
110 // AWS providers (Credentials/Region) explicitly for the distilled call.
111 if (fixtureFunctionName !== undefined) {
112 const gone = yield* Core.withProviders(
113 lambda.getFunction({ FunctionName: fixtureFunctionName }).pipe(
114 Effect.map(() => false),
115 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
116 ),
117 testOptions,
118 "CloudWatchBindings",
119 );
120 expect(gone).toBe(true);
121 }
122 }),
123 { timeout: 120_000 },
124 );
125
126 // ── Metrics ──────────────────────────────────────────────────────────────
127
128 describe("PutMetricData", () => {
129 test.provider("puts a custom metric datum", (_stack) =>
130 Effect.gen(function* () {
131 const response = yield* postJson("/put-metric-data");
132 expect(response).toHaveProperty("ok", true);
133 }),
134 );
135 });
136
137 describe("ListMetrics", () => {
138 test.provider("lists metrics in the fixture namespace", (_stack) =>
139 Effect.gen(function* () {
140 // Freshly-put metric data can take minutes to appear in ListMetrics —
141 // assert the authorized call returns a well-formed collection.
142 const response = (yield* getJson("/list-metrics")) as { metrics: unknown[] };
143 expect(Array.isArray(response.metrics)).toBe(true);
144 }),
145 );
146 });
147
148 describe("GetMetricData", () => {
149 test.provider("queries the fixture metric", (_stack) =>
150 Effect.gen(function* () {
151 const response = (yield* getJson("/get-metric-data")) as { results: { Id?: string }[] };
152 expect(response.results.length).toBe(1);
153 expect(response.results[0].Id).toBe("m1");
154 }),
155 );
156 });
157
158 describe("GetMetricStatistics", () => {
159 test.provider("returns statistics for the fixture metric", (_stack) =>
160 Effect.gen(function* () {
161 const response = (yield* getJson("/get-metric-statistics")) as {
162 label?: string;
163 datapoints: unknown[];
164 };
165 expect(response.label).toBe(METRIC_NAME);
166 expect(Array.isArray(response.datapoints)).toBe(true);
167 }),
168 );
169 });
170
171 describe("GetMetricWidgetImage", () => {
172 test.provider("renders a metric widget image", (_stack) =>
173 Effect.gen(function* () {
174 const response = (yield* getJson("/get-metric-widget-image")) as { bytes: number };
175 expect(response.bytes).toBeGreaterThan(0);
176 }),
177 );
178 });
179
180 // ── Alarms ───────────────────────────────────────────────────────────────
181
182 describe("DescribeAlarms", () => {
183 test.provider("describes the bound alarm", (_stack) =>
184 Effect.gen(function* () {
185 const response = (yield* getJson("/describe-alarms")) as {
186 alarmName: string;
187 alarms: string[];
188 };
189 expect(response.alarms).toContain(response.alarmName);
190 }),
191 );
192 });
193
194 describe("DescribeAlarmsForMetric", () => {
195 test.provider("finds the alarm by its metric", (_stack) =>
196 Effect.gen(function* () {
197 const response = (yield* getJson("/describe-alarms-for-metric")) as {
198 alarmName: string;
199 alarms: string[];
200 };
201 expect(response.alarms).toContain(response.alarmName);
202 }),
203 );
204 });
205
206 describe("SetAlarmState", () => {
207 test.provider("sets the alarm state to OK", (_stack) =>
208 Effect.gen(function* () {
209 const response = yield* postJson("/set-alarm-state");
210 expect(response).toHaveProperty("ok", true);
211 }),
212 );
213 });
214
215 describe("DescribeAlarmHistory", () => {
216 test.provider("returns history for the bound alarm", (_stack) =>
217 Effect.gen(function* () {
218 const response = (yield* getJson("/describe-alarm-history")) as { items: unknown[] };
219 expect(Array.isArray(response.items)).toBe(true);
220 }),
221 );
222 });
223
224 describe("DescribeAlarmContributors", () => {
225 test.provider("is authorized against the bound alarm", (_stack) =>
226 Effect.gen(function* () {
227 // A plain metric alarm has no contributor data; the typed
228 // ValidationException (observed live) or ResourceNotFoundException is
229 // the expected outcome, and a successful contributor list is equally
230 // valid. Any of these proves binding + IAM (auth failures die as 500).
231 const response = (yield* getJson("/describe-alarm-contributors")) as
232 | { ok: true; contributors: unknown[] }
233 | { ok: false; error: string };
234 if (response.ok) {
235 expect(Array.isArray(response.contributors)).toBe(true);
236 } else {
237 expect(["ResourceNotFoundException", "ValidationException"]).toContain(response.error);
238 }
239 }),
240 );
241 });
242
243 describe("DisableAlarmActions", () => {
244 test.provider("disables actions on the bound alarm", (_stack) =>
245 Effect.gen(function* () {
246 const response = yield* postJson("/disable-alarm-actions");
247 expect(response).toHaveProperty("ok", true);
248 }),
249 );
250 });
251
252 describe("EnableAlarmActions", () => {
253 test.provider("re-enables actions on the bound alarm", (_stack) =>
254 Effect.gen(function* () {
255 const response = yield* postJson("/enable-alarm-actions");
256 expect(response).toHaveProperty("ok", true);
257 }),
258 );
259 });
260
261 describe("ListTagsForResource", () => {
262 test.provider("lists the alarm's internal tags", (_stack) =>
263 Effect.gen(function* () {
264 const response = (yield* getJson("/list-tags-for-resource")) as {
265 tags: { Key: string; Value: string }[];
266 };
267 // Every alchemy-managed alarm carries internal branding tags.
268 expect(response.tags.length).toBeGreaterThan(0);
269 }),
270 );
271 });
272
273 // ── Dashboards ───────────────────────────────────────────────────────────
274
275 describe("GetDashboard", () => {
276 test.provider("gets the bound dashboard", (_stack) =>
277 Effect.gen(function* () {
278 const response = (yield* getJson("/get-dashboard")) as {
279 dashboardName: string;
280 name?: string;
281 body?: string;
282 };
283 expect(response.name).toBe(response.dashboardName);
284 expect(response.body).toContain("widgets");
285 }),
286 );
287 });
288
289 describe("ListDashboards", () => {
290 test.provider("lists the deployed dashboard", (_stack) =>
291 Effect.gen(function* () {
292 const response = (yield* getJson("/list-dashboards")) as {
293 dashboardName: string;
294 entries: string[];
295 };
296 expect(response.entries).toContain(response.dashboardName);
297 }),
298 );
299 });
300
301 // ── Contributor Insights ─────────────────────────────────────────────────
302
303 describe("DescribeInsightRules", () => {
304 test.provider("lists the deployed insight rule", (_stack) =>
305 Effect.gen(function* () {
306 const response = (yield* getJson("/describe-insight-rules")) as {
307 ruleName: string;
308 rules: string[];
309 };
310 expect(response.rules).toContain(response.ruleName);
311 }),
312 );
313 });
314
315 describe("GetInsightRuleReport", () => {
316 test.provider("returns a report for the deployed rule", (_stack) =>
317 Effect.gen(function* () {
318 const response = (yield* getJson("/get-insight-rule-report")) as {
319 ok: boolean;
320 contributors: unknown[];
321 };
322 expect(response.ok).toBe(true);
323 expect(Array.isArray(response.contributors)).toBe(true);
324 }),
325 );
326 });
327
328 describe("DisableInsightRules", () => {
329 test.provider("disables the deployed rule without failures", (_stack) =>
330 Effect.gen(function* () {
331 const response = (yield* postJson("/disable-insight-rules")) as { failures: unknown[] };
332 expect(response.failures).toEqual([]);
333 }),
334 );
335 });
336
337 describe("EnableInsightRules", () => {
338 test.provider("re-enables the deployed rule without failures", (_stack) =>
339 Effect.gen(function* () {
340 const response = (yield* postJson("/enable-insight-rules")) as { failures: unknown[] };
341 expect(response.failures).toEqual([]);
342 }),
343 );
344 });
345
346 describe("ListManagedInsightRules", () => {
347 test.provider("is authorized for the probe resource", (_stack) =>
348 Effect.gen(function* () {
349 // Managed Contributor Insights rules only exist for specific AWS
350 // resource types; probing with the alarm ARN either returns rules or
351 // the typed InvalidParameterValueException. Either proves the
352 // binding is wired and authorized (auth failures surface as 500s).
353 const response = (yield* getJson("/list-managed-insight-rules")) as
354 | { ok: true; rules: unknown[] }
355 | { ok: false; error: string };
356 if (response.ok) {
357 expect(Array.isArray(response.rules)).toBe(true);
358 } else {
359 expect(response.error).toBe("InvalidParameterValueException");
360 }
361 }),
362 );
363 });
364
365 // ── Anomaly detectors ────────────────────────────────────────────────────
366
367 describe("DescribeAnomalyDetectors", () => {
368 test.provider("finds the deployed anomaly detector", (_stack) =>
369 Effect.gen(function* () {
370 const response = (yield* getJson("/describe-anomaly-detectors")) as {
371 detectors: {
372 SingleMetricAnomalyDetector?: { MetricName?: string };
373 MetricName?: string;
374 }[];
375 };
376 expect(
377 response.detectors.some(
378 (d) =>
379 d.MetricName === METRIC_NAME ||
380 d.SingleMetricAnomalyDetector?.MetricName === METRIC_NAME,
381 ),
382 ).toBe(true);
383 }),
384 );
385 });
386
387 // ── Alarm mute rules / metric streams ────────────────────────────────────
388
389 describe("ListAlarmMuteRules", () => {
390 test.provider("lists alarm mute rules in the region", (_stack) =>
391 Effect.gen(function* () {
392 const response = (yield* getJson("/list-alarm-mute-rules")) as { summaries: unknown[] };
393 expect(Array.isArray(response.summaries)).toBe(true);
394 }),
395 );
396 });
397
398 describe("ListMetricStreams", () => {
399 test.provider("lists metric streams in the region", (_stack) =>
400 Effect.gen(function* () {
401 const response = (yield* getJson("/list-metric-streams")) as { entries: unknown[] };
402 expect(Array.isArray(response.entries)).toBe(true);
403 }),
404 );
405 });
406 },
407 );