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

main
ts 159 lines 6.05 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 Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import AIOpsTestFunctionLive, { AIOpsTestFunction } from "./fixtures/handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "AIOpsBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy.
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 // The Lambda fixture occasionally answers a transient 5xx under load (cold
28 // re-init, IAM propagation on the freshly attached policy that the handler's
29 // `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx/assertion
30 // failure surfaces immediately.
31 const send = (request: HttpClientRequest.HttpClientRequest) =>
32 HttpClient.execute(request).pipe(
33 Effect.flatMap((response) =>
34 response.status >= 500
35 ? response.text.pipe(
36 Effect.flatMap((body) =>
37 Effect.fail(new TransientUpstream({ status: response.status, body })),
38 ),
39 )
40 : Effect.succeed(response),
41 ),
42 Effect.retry({
43 while: (e) => e._tag === "TransientUpstream",
44 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
45 }),
46 );
47
48 const getJson = (path: string) =>
49 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
50
51 // AWS enforces ONE AIOps investigation group per account per Region, so this
52 // file must never overlap with InvestigationGroup.test.ts (whose live
53 // lifecycle is gated behind AWS_TEST_AIOPS and destroys its group on the way
54 // in and out). The single-fork suite run executes files sequentially and this
55 // file destroys its group on the way in and out.
56 describe.sequential(
57 "AIOps Bindings",
58 {
59 tags: ["provider:aws", "provider:aws:aiops", "provider:aws:iam", "provider:aws:lambda", "live"],
60 },
61 () => {
62 beforeAll(
63 Effect.gen(function* () {
64 yield* Effect.logInfo("AIOps test setup: destroying previous resources");
65 yield* sharedStack.destroy();
66
67 yield* Effect.logInfo("AIOps test setup: deploying fixture");
68 const attrs = yield* sharedStack.deploy(
69 Effect.gen(function* () {
70 return yield* AIOpsTestFunction;
71 }).pipe(Effect.provide(AIOpsTestFunctionLive)),
72 );
73
74 expect(attrs.functionUrl).toBeTruthy();
75 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
76
77 const readinessUrl = `${baseUrl}/bindings`;
78 yield* Effect.logInfo(`AIOps test setup: probing readiness at ${readinessUrl}`);
79 yield* HttpClient.get(readinessUrl).pipe(
80 Effect.flatMap((response) =>
81 response.status === 200
82 ? Effect.succeed(response)
83 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
84 ),
85 Effect.tapError((error) =>
86 Effect.logWarning(`AIOps test setup: fixture not ready yet (${String(error)})`),
87 ),
88 Effect.retry({ schedule: readinessPolicy }),
89 );
90 }),
91 { timeout: 240_000 },
92 );
93
94 afterAll(sharedStack.destroy(), { timeout: 240_000 });
95
96 describe("binding registration", () => {
97 test.provider("the capabilities initialize in the runtime", (_stack) =>
98 Effect.gen(function* () {
99 const response = (yield* getJson("/bindings")) as { bound: string[] };
100 expect(response.bound).toEqual([
101 "getInvestigationGroup",
102 "getInvestigationGroupPolicy",
103 "listTagsForResource",
104 "listInvestigationGroups",
105 ]);
106 }),
107 );
108 });
109
110 describe("GetInvestigationGroup", () => {
111 test.provider("reads the bound group's configuration (injected group ARN)", (_stack) =>
112 Effect.gen(function* () {
113 const response = (yield* getJson("/group")) as {
114 name: string;
115 arn: string;
116 retentionInDays: number;
117 };
118 expect(response.arn).toContain(":investigation-group/");
119 expect(response.name).toBeTruthy();
120 expect(response.retentionInDays).toBe(7);
121 }),
122 );
123 });
124
125 describe("GetInvestigationGroupPolicy", () => {
126 test.provider(
127 "reads the group's resource policy (typed not-found when unattached)",
128 (_stack) =>
129 Effect.gen(function* () {
130 // The fixture attaches no resource policy, so the runtime observes
131 // the typed ResourceNotFoundException — proving both the IAM grant
132 // and the injected group ARN end-to-end.
133 const response = (yield* getJson("/policy")) as { hasPolicy: boolean };
134 expect(response.hasPolicy).toBe(false);
135 }),
136 );
137 });
138
139 describe("ListTagsForResource", () => {
140 test.provider("reads the bound group's tags", (_stack) =>
141 Effect.gen(function* () {
142 const response = (yield* getJson("/tags")) as { tags: Record<string, string> };
143 expect(response.tags.Purpose).toBe("bindings-test");
144 expect(response.tags["alchemy::id"]).toBe("BindingGroup");
145 }),
146 );
147 });
148
149 describe("ListInvestigationGroups", () => {
150 test.provider("enumerates the Region's investigation groups (account-level)", (_stack) =>
151 Effect.gen(function* () {
152 const group = (yield* getJson("/group")) as { arn: string };
153 const response = (yield* getJson("/groups")) as { groupArns: string[] };
154 expect(response.groupArns).toContain(group.arn);
155 }),
156 );
157 });
158 },
159 );