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

main
ts 224 lines 8.37 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 eventbridge from "@distilled.cloud/aws/eventbridge";
6 import * as lambda from "@distilled.cloud/aws/lambda";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as HttpClient from "effect/http/HttpClient";
10 import * as HttpClientRequest from "effect/http/HttpClientRequest";
11 import * as Schedule from "effect/Schedule";
12 import DevOpsGuruTestFunctionLive, { DevOpsGuruTestFunction } from "./handler";
13
14 const testOptions = { providers: AWS.providers() };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "DevOpsGuruBindings");
17
18 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
19 // over 60s on a fresh deploy.
20 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
21
22 let baseUrl: string;
23 let functionArn: string;
24
25 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
26 readonly status: number;
27 readonly body: string;
28 }> {}
29
30 class FunctionStillExists extends Data.TaggedError("FunctionStillExists") {}
31
32 // The shared Lambda fixture occasionally answers a transient 5xx under load
33 // (cold re-init, IAM propagation on the freshly attached policy that the
34 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
35 // 4xx/assertion failure surfaces immediately.
36 const send = (request: HttpClientRequest.HttpClientRequest) =>
37 HttpClient.execute(request).pipe(
38 Effect.flatMap((response) =>
39 response.status >= 500
40 ? response.text.pipe(
41 Effect.flatMap((body) =>
42 Effect.fail(new TransientUpstream({ status: response.status, body })),
43 ),
44 )
45 : Effect.succeed(response),
46 ),
47 Effect.retry({
48 while: (e) => e._tag === "TransientUpstream",
49 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
50 }),
51 );
52
53 const getJson = (path: string) =>
54 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
55
56 describe.sequential(
57 "DevOpsGuru Bindings",
58 {
59 tags: [
60 "provider:aws",
61 "provider:aws:devopsguru",
62 "provider:aws:eventbridge",
63 "provider:aws:lambda",
64 "live",
65 ],
66 },
67 () => {
68 beforeAll(
69 Effect.gen(function* () {
70 yield* Effect.logInfo("DevOpsGuru test setup: destroying previous resources");
71 yield* sharedStack.destroy();
72
73 yield* Effect.logInfo("DevOpsGuru test setup: deploying fixture");
74 const attrs = yield* sharedStack.deploy(
75 Effect.gen(function* () {
76 return yield* DevOpsGuruTestFunction;
77 }).pipe(Effect.provide(DevOpsGuruTestFunctionLive)),
78 );
79
80 expect(attrs.functionUrl).toBeTruthy();
81 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
82 functionArn = attrs.functionArn;
83
84 const readinessUrl = `${baseUrl}/bindings`;
85 yield* Effect.logInfo(`DevOpsGuru test setup: probing readiness at ${readinessUrl}`);
86 yield* HttpClient.get(readinessUrl).pipe(
87 Effect.flatMap((response) =>
88 response.status === 200
89 ? Effect.succeed(response)
90 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
91 ),
92 Effect.tapError((error) =>
93 Effect.logWarning(`DevOpsGuru test setup: fixture not ready yet (${String(error)})`),
94 ),
95 Effect.retry({ schedule: readinessPolicy }),
96 );
97 }),
98 { timeout: 240_000 },
99 );
100
101 afterAll(
102 Effect.gen(function* () {
103 yield* sharedStack.destroy();
104 // Assert gone (skipped when beforeAll never got far enough to deploy):
105 // the fixture Lambda answers with the typed not-found tag, and the
106 // event-source's EventBridge rule no longer targets it. afterAll runs
107 // outside `test.provider`'s layer, so raw distilled calls need the
108 // provider layer (credentials, region) supplied explicitly.
109 if (functionArn) {
110 yield* Core.withProviders(
111 Effect.gen(function* () {
112 yield* lambda.getFunction({ FunctionName: functionArn }).pipe(
113 Effect.flatMap(() => Effect.fail(new FunctionStillExists())),
114 Effect.retry({
115 while: (error) => error._tag === "FunctionStillExists",
116 schedule: Schedule.exponential("500 millis"),
117 times: 8,
118 }),
119 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
120 );
121 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
122 TargetArn: functionArn,
123 });
124 expect(RuleNames ?? []).toHaveLength(0);
125 }),
126 testOptions,
127 sharedStack.name,
128 );
129 }
130 }),
131 { timeout: 120_000 },
132 );
133
134 describe("binding registration", () => {
135 test.provider("all 22 capabilities initialize in the runtime", (_stack) =>
136 Effect.gen(function* () {
137 const response = (yield* getJson("/bindings")) as { bound: string[] };
138 expect(response.bound).toHaveLength(22);
139 expect(response.bound).toContain("describeAccountHealth");
140 expect(response.bound).toContain("searchInsights");
141 expect(response.bound).toContain("putFeedback");
142 }),
143 );
144 });
145
146 describe("DescribeAccountHealth", () => {
147 test.provider("reads the account's insight counters", (_stack) =>
148 Effect.gen(function* () {
149 const response = (yield* getJson("/health")) as {
150 openReactiveInsights: number;
151 openProactiveInsights: number;
152 metricsAnalyzed: number;
153 };
154 expect(response.openReactiveInsights).toBeGreaterThanOrEqual(0);
155 expect(response.openProactiveInsights).toBeGreaterThanOrEqual(0);
156 expect(response.metricsAnalyzed).toBeGreaterThanOrEqual(0);
157 }),
158 );
159 });
160
161 describe("DescribeAccountOverview", () => {
162 test.provider("summarizes the trailing 24 hours", (_stack) =>
163 Effect.gen(function* () {
164 const response = (yield* getJson("/overview")) as {
165 reactiveInsights: number;
166 proactiveInsights: number;
167 };
168 expect(response.reactiveInsights).toBeGreaterThanOrEqual(0);
169 expect(response.proactiveInsights).toBeGreaterThanOrEqual(0);
170 }),
171 );
172 });
173
174 describe("ListInsights", () => {
175 test.provider("lists ongoing reactive insights", (_stack) =>
176 Effect.gen(function* () {
177 const response = (yield* getJson("/insights")) as { count: number };
178 expect(response.count).toBeGreaterThanOrEqual(0);
179 }),
180 );
181 });
182
183 describe("SearchInsights", () => {
184 test.provider("searches reactive insights by time range", (_stack) =>
185 Effect.gen(function* () {
186 const response = (yield* getJson("/search")) as { count: number };
187 expect(response.count).toBeGreaterThanOrEqual(0);
188 }),
189 );
190 });
191
192 describe("DescribeResourceCollectionHealth", () => {
193 test.provider("reads per-stack coverage health", (_stack) =>
194 Effect.gen(function* () {
195 const response = (yield* getJson("/collection-health")) as { stacks: number };
196 expect(response.stacks).toBeGreaterThanOrEqual(0);
197 }),
198 );
199 });
200
201 describe("ListMonitoredResources", () => {
202 test.provider("lists analyzed resources (typed not-found -> zero coverage)", (_stack) =>
203 Effect.gen(function* () {
204 const response = (yield* getJson("/monitored")) as { count: number };
205 expect(response.count).toBeGreaterThanOrEqual(0);
206 }),
207 );
208 });
209
210 describe("consumeInsightEvents", () => {
211 test.provider("the deploy created an EventBridge rule targeting the function", (_stack) =>
212 Effect.gen(function* () {
213 // Out-of-band via distilled: the fixture's consumeInsightEvents
214 // must have materialized as a rule on the default bus with the
215 // Lambda as target.
216 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
217 TargetArn: functionArn,
218 });
219 expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1);
220 }),
221 );
222 });
223 },
224 );