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

main
ts 128 lines 4.72 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as cloudwatch from "@distilled.cloud/aws/cloudwatch";
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 HttpBody from "effect/http/HttpBody";
9 import * as HttpClient from "effect/http/HttpClient";
10 import * as Schedule from "effect/Schedule";
11 import {
12 MetricSinkFunction,
13 MetricSinkFunctionLive,
14 SINK_METRIC_NAME,
15 SINK_NAMESPACE,
16 } from "./metric-sink-handler";
17
18 const { test } = Test.make({ providers: AWS.providers() });
19
20 class FunctionNotReady extends Data.TaggedError("FunctionNotReady") {}
21
22 class MetricsNotVisible extends Data.TaggedError("MetricsNotVisible")<{
23 readonly observed: number;
24 readonly expected: number;
25 }> {}
26
27 const waitForFunctionReady = (url: string) =>
28 HttpClient.get(url).pipe(
29 Effect.flatMap((response) =>
30 response.status === 200 ? Effect.void : Effect.fail(new FunctionNotReady()),
31 ),
32 Effect.retry({
33 while: (error) => error._tag === "FunctionNotReady",
34 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]),
35 }),
36 );
37
38 /**
39 * Out-of-band verification: poll GetMetricStatistics until the summed
40 * `Sum` across datapoints for the run's unique dimension reaches the
41 * published total. Standard-resolution data is typically queryable within
42 * seconds to ~2 minutes of PutMetricData; the poll is bounded at ~3 min.
43 */
44 const waitForMetricSum = Effect.fn(function* (runId: string, expected: number, startTime: Date) {
45 return yield* Effect.gen(function* () {
46 const stats = yield* cloudwatch.getMetricStatistics({
47 Namespace: SINK_NAMESPACE,
48 MetricName: SINK_METRIC_NAME,
49 Dimensions: [{ Name: "Run", Value: runId }],
50 StartTime: startTime,
51 EndTime: new Date(Date.now() + 60_000),
52 Period: 60,
53 Statistics: ["Sum", "SampleCount"],
54 });
55 const observed = (stats.Datapoints ?? []).reduce((total, point) => total + (point.Sum ?? 0), 0);
56 if (observed < expected) {
57 return yield* Effect.fail(new MetricsNotVisible({ observed, expected }));
58 }
59 return observed;
60 }).pipe(
61 Effect.retry({
62 while: (e) => e._tag === "MetricsNotVisible",
63 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(36)]),
64 }),
65 );
66 });
67
68 test.provider(
69 "MetricSink batches >1000 datums through a deployed Lambda",
70 (stack) =>
71 Effect.gen(function* () {
72 // Leading destroy reconciles away any prior partial/crashed deployment.
73 yield* stack.destroy();
74
75 const fn = yield* stack.deploy(
76 MetricSinkFunction.pipe(Effect.provide(MetricSinkFunctionLive)),
77 );
78 const baseUrl = fn.functionUrl!.replace(/\/+$/, "");
79
80 yield* waitForFunctionReady(`${baseUrl}/ready`);
81
82 // 1500 datums > the PutMetricData limit of 1000 per call, so the
83 // batched sink must split the stream into 2 sequential API calls
84 // (1000 + 500). PutMetricData is all-or-nothing, so Sum === 1500
85 // proves both calls landed.
86 const runId = crypto.randomUUID();
87 const count = 1500;
88 const startTime = new Date(Date.now() - 5 * 60_000);
89
90 const response = yield* HttpClient.post(`${baseUrl}/sink`, {
91 body: yield* HttpBody.json({ runId, count }),
92 }).pipe(
93 Effect.flatMap((result) =>
94 result.status === 200 ? Effect.succeed(result) : Effect.fail("not ready"),
95 ),
96 // Fresh function URLs can serve transient non-200s while IAM and
97 // DNS propagate — retry only that window.
98 Effect.retry({
99 while: (error) => error === "not ready",
100 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(30)]),
101 }),
102 Effect.flatMap((result) => result.json),
103 );
104
105 expect((response as any).ok).toBe(true);
106 expect((response as any).count).toBe(count);
107
108 const observed = yield* waitForMetricSum(runId, count, startTime);
109 expect(observed).toBe(count);
110
111 yield* stack.destroy();
112
113 // Out-of-band assert-gone: the deployed Lambda no longer exists after
114 // the final destroy (custom metrics themselves are not deletable and
115 // age out on their own).
116 const gone = yield* lambda.getFunction({ FunctionName: fn.functionName }).pipe(
117 Effect.map(() => false),
118 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
119 );
120 expect(gone).toBe(true);
121 }),
122 // Deploy (~60-120s) + readiness poll (bounded ~150s) + metric visibility
123 // poll (bounded ~180s) + destroy. All waits are bounded.
124 {
125 tags: ["provider:aws", "provider:aws:cloudwatch", "provider:aws:lambda", "live"],
126 timeout: 420_000,
127 },
128 );