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

main
ts 338 lines 12.7 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 detective from "@distilled.cloud/aws/detective";
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 DetectiveTestFunctionLive, { DetectiveTestFunction } from "./handler";
12 import { makeDetectiveTestLease } from "./TestLease.ts";
13
14 const testOptions = { providers: AWS.providers() };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "DetectiveBindings");
17 const testLease = makeDetectiveTestLease();
18
19 beforeAll(testLease.acquire, { timeout: 240_000 });
20 afterAll(testLease.release);
21
22 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
23 // over 60s on a fresh deploy.
24 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
25
26 let baseUrl: string;
27 let roleArn: string;
28 let graphArn: string;
29 // The Detective behavior graph is an account/region singleton. If the
30 // account already runs a graph this suite did not create, deploying the
31 // fixture would adopt (and later DELETE) it — so every test degrades to a
32 // logged no-op instead (capture-and-restore safety, mirroring Graph.test.ts).
33 let foreignGraphArn: string | undefined;
34
35 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
36 readonly status: number;
37 readonly body: string;
38 }> {}
39
40 // The shared Lambda fixture occasionally answers a transient 5xx under load
41 // (cold re-init, IAM propagation on the freshly attached policy that the
42 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
43 // 4xx/assertion failure surfaces immediately.
44 const send = (request: HttpClientRequest.HttpClientRequest) =>
45 HttpClient.execute(request).pipe(
46 Effect.flatMap((response) =>
47 response.status >= 500
48 ? response.text.pipe(
49 Effect.flatMap((body) =>
50 Effect.fail(new TransientUpstream({ status: response.status, body })),
51 ),
52 )
53 : Effect.succeed(response),
54 ),
55 Effect.retry({
56 while: (e) => e._tag === "TransientUpstream",
57 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
58 }),
59 );
60
61 const getJson = (path: string) =>
62 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
63
64 const postJson = (path: string) =>
65 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
66
67 // beforeAll/afterAll hooks run outside `test.provider`'s layer, so raw
68 // distilled calls need the provider layer (credentials, region) supplied
69 // explicitly.
70 const aws = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
71 Core.withProviders(effect, testOptions, sharedStack.name);
72
73 const skipForeign = () =>
74 foreignGraphArn
75 ? Effect.logInfo(
76 `Detective graph ${foreignGraphArn} already exists and is not ours — skipping`,
77 ).pipe(Effect.as(true))
78 : Effect.succeed(false);
79
80 describe.sequential(
81 "Detective Bindings",
82 {
83 tags: [
84 "provider:aws",
85 "provider:aws:batch",
86 "provider:aws:detective",
87 "provider:aws:lambda",
88 "live",
89 ],
90 },
91 () => {
92 beforeAll(
93 Effect.gen(function* () {
94 // Never take over a behavior graph this fixture did not create.
95 const preexisting = (yield* aws(detective.listGraphs({}))).GraphList?.[0];
96 if (preexisting?.Arn) {
97 const tags = yield* aws(detective.listTagsForResource({ ResourceArn: preexisting.Arn }));
98 if (tags.Tags?.["fixture"] !== "detective-bindings") {
99 foreignGraphArn = preexisting.Arn;
100 yield* Effect.logInfo(
101 `Detective test setup: foreign graph ${preexisting.Arn} present — suite degrades to no-op`,
102 );
103 return;
104 }
105 }
106
107 yield* Effect.logInfo("Detective test setup: destroying previous resources");
108 yield* sharedStack.destroy();
109
110 yield* Effect.logInfo("Detective test setup: deploying fixture");
111 const attrs = yield* sharedStack.deploy(
112 Effect.gen(function* () {
113 return yield* DetectiveTestFunction;
114 }).pipe(Effect.provide(DetectiveTestFunctionLive)),
115 );
116
117 expect(attrs.functionUrl).toBeTruthy();
118 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
119 roleArn = attrs.roleArn;
120
121 // Out-of-band: the fixture created the account's (single) graph.
122 const graphs = yield* aws(detective.listGraphs({}));
123 graphArn = graphs.GraphList?.[0]?.Arn ?? "";
124 expect(graphArn).toContain(":graph:");
125
126 const readinessUrl = `${baseUrl}/bindings`;
127 yield* Effect.logInfo(`Detective test setup: probing readiness at ${readinessUrl}`);
128 yield* HttpClient.get(readinessUrl).pipe(
129 Effect.flatMap((response) =>
130 response.status === 200
131 ? Effect.succeed(response)
132 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
133 ),
134 Effect.tapError((error) =>
135 Effect.logWarning(`Detective test setup: fixture not ready yet (${String(error)})`),
136 ),
137 Effect.retry({ schedule: readinessPolicy }),
138 );
139 }),
140 { timeout: 240_000 },
141 );
142
143 afterAll(
144 Effect.gen(function* () {
145 if (foreignGraphArn) return;
146 yield* sharedStack.destroy();
147 }),
148 { timeout: 120_000 },
149 );
150
151 describe("binding registration", () => {
152 test.provider("all 23 capabilities initialize in the runtime", (_stack) =>
153 Effect.gen(function* () {
154 if (yield* skipForeign()) return;
155 const response = (yield* getJson("/bindings")) as { bound: string[] };
156 expect(response.bound).toHaveLength(23);
157 expect(response.bound).toContain("startInvestigation");
158 expect(response.bound).toContain("acceptInvitation");
159 expect(response.bound).toContain("enableOrganizationAdminAccount");
160 }),
161 );
162 });
163
164 describe("ListMembers", () => {
165 test.provider("enumerates the graph's member accounts (injected graph arn)", (_stack) =>
166 Effect.gen(function* () {
167 if (yield* skipForeign()) return;
168 // An organization account may see auto-enabled members even on a
169 // freshly created graph, so assert shape rather than emptiness.
170 const response = (yield* getJson("/members")) as { count: number };
171 expect(typeof response.count).toBe("number");
172 expect(response.count).toBeGreaterThanOrEqual(0);
173 }),
174 );
175 });
176
177 describe("GetMembers", () => {
178 test.provider(
179 "a non-member account comes back as unprocessed (injected graph arn)",
180 (_stack) =>
181 Effect.gen(function* () {
182 if (yield* skipForeign()) return;
183 const response = (yield* getJson("/member-status")) as {
184 members: number;
185 unprocessed: number;
186 };
187 expect(response.members).toBe(0);
188 expect(response.unprocessed).toBe(1);
189 }),
190 );
191 });
192
193 describe("ListDatasourcePackages", () => {
194 test.provider("the graph ingests the core package", (_stack) =>
195 Effect.gen(function* () {
196 if (yield* skipForeign()) return;
197 const response = (yield* getJson("/datasources")) as { packages: string[] };
198 expect(response.packages).toContain("DETECTIVE_CORE");
199 }),
200 );
201 });
202
203 describe("BatchGetGraphMemberDatasources", () => {
204 test.provider("reads member ingest history for the graph", (_stack) =>
205 Effect.gen(function* () {
206 if (yield* skipForeign()) return;
207 const response = (yield* getJson("/graph-member-datasources")) as {
208 memberDatasources?: number;
209 unprocessed?: number;
210 errorTag?: string;
211 };
212 if (response.errorTag) {
213 // A non-member account id may be rejected outright instead of
214 // reported as unprocessed.
215 expect(["ValidationException", "ResourceNotFoundException"]).toContain(
216 response.errorTag,
217 );
218 } else {
219 expect(response.memberDatasources).toBe(0);
220 }
221 }),
222 );
223 });
224
225 describe("ListInvestigations", () => {
226 test.provider("a fresh graph has no investigations", (_stack) =>
227 Effect.gen(function* () {
228 if (yield* skipForeign()) return;
229 const response = (yield* getJson("/investigations")) as { count: number };
230 expect(response.count).toBe(0);
231 }),
232 );
233 });
234
235 describe("StartInvestigation", () => {
236 test.provider(
237 "triggers triage of the fixture's own role (typed outcome either way)",
238 (_stack) =>
239 Effect.gen(function* () {
240 if (yield* skipForeign()) return;
241 const response = (yield* postJson(
242 `/investigate?entity=${encodeURIComponent(roleArn)}`,
243 )) as { investigationId?: string; errorTag?: string };
244 if (response.errorTag) {
245 // A brand-new graph has ingested (almost) no data, so Detective
246 // typically cannot resolve the entity yet.
247 expect([
248 "ValidationException",
249 "ResourceNotFoundException",
250 "TooManyRequestsException",
251 "InternalServerException",
252 ]).toContain(response.errorTag);
253 } else {
254 expect(response.investigationId).toBeTruthy();
255 }
256 }),
257 );
258 });
259
260 describe("ListInvitations", () => {
261 test.provider("enumerates this account's behavior-graph invitations", (_stack) =>
262 Effect.gen(function* () {
263 if (yield* skipForeign()) return;
264 // The shared test account can carry standing invitations from
265 // other admin accounts — assert shape rather than emptiness.
266 const response = (yield* getJson("/invitations")) as { count: number };
267 expect(typeof response.count).toBe("number");
268 expect(response.count).toBeGreaterThanOrEqual(0);
269 }),
270 );
271 });
272
273 describe("BatchGetMembershipDatasources", () => {
274 test.provider("the admin account is not a member of its own graph", (_stack) =>
275 Effect.gen(function* () {
276 if (yield* skipForeign()) return;
277 const response = (yield* getJson(
278 `/membership-datasources?graphArn=${encodeURIComponent(graphArn)}`,
279 )) as { membershipDatasources?: number; unprocessedGraphs?: number; errorTag?: string };
280 if (response.errorTag) {
281 expect(["ValidationException", "ResourceNotFoundException"]).toContain(
282 response.errorTag,
283 );
284 } else {
285 expect(
286 (response.membershipDatasources ?? 0) + (response.unprocessedGraphs ?? 0),
287 ).toBeGreaterThanOrEqual(0);
288 }
289 }),
290 );
291 });
292
293 describe("DescribeOrganizationConfiguration", () => {
294 test.provider(
295 "answers (or rejects with a typed error for a non-delegated account)",
296 (_stack) =>
297 Effect.gen(function* () {
298 if (yield* skipForeign()) return;
299 const response = (yield* getJson("/org-config")) as {
300 autoEnable?: boolean;
301 errorTag?: string;
302 };
303 if (response.errorTag) {
304 // Only the organization's delegated Detective administrator may
305 // call this — a standalone test account gets a typed rejection,
306 // which still proves the binding + IAM wiring end-to-end.
307 expect([
308 "ValidationException",
309 "AccessDeniedException",
310 "TooManyRequestsException",
311 ]).toContain(response.errorTag);
312 } else {
313 expect(typeof response.autoEnable).toBe("boolean");
314 }
315 }),
316 );
317 });
318
319 describe("ListOrganizationAdminAccounts", () => {
320 test.provider(
321 "answers (or rejects with a typed error outside the management account)",
322 (_stack) =>
323 Effect.gen(function* () {
324 if (yield* skipForeign()) return;
325 const response = (yield* getJson("/org-admins")) as {
326 administrators?: number;
327 errorTag?: string;
328 };
329 if (response.errorTag) {
330 expect(["ValidationException", "AccessDeniedException"]).toContain(response.errorTag);
331 } else {
332 expect(response.administrators).toBeGreaterThanOrEqual(0);
333 }
334 }),
335 );
336 });
337 },
338 );