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

main
ts 281 lines 10.9 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 AuditManagerTestFunctionLive, { AuditManagerTestFunction } from "./handler";
11 import AuditManagerAssessmentTestFunctionLive, {
12 AuditManagerAssessmentTestFunction,
13 } from "./handler-assessment";
14
15 const testOptions = { providers: AWS.providers() };
16 const { test, beforeAll, afterAll } = Test.make(testOptions);
17 const sharedStack = Core.scratchStack(testOptions, "AuditManagerBindings");
18 const assessmentStack = Core.scratchStack(testOptions, "AuditManagerAssessmentBindings");
19
20 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
21 // over 60s on a fresh deploy.
22 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
23
24 let baseUrl: string;
25
26 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
27 readonly status: number;
28 readonly body: string;
29 }> {}
30
31 // The shared Lambda fixture occasionally answers a transient 5xx under load
32 // (cold re-init, IAM propagation on the freshly attached policy). Retry only
33 // 5xx; a genuine 4xx/assertion failure surfaces immediately.
34 const send = (request: HttpClientRequest.HttpClientRequest) =>
35 HttpClient.execute(request).pipe(
36 Effect.flatMap((response) =>
37 response.status >= 500
38 ? response.text.pipe(
39 Effect.flatMap((body) =>
40 Effect.fail(new TransientUpstream({ status: response.status, body })),
41 ),
42 )
43 : Effect.succeed(response),
44 ),
45 Effect.retry({
46 while: (e) => e._tag === "TransientUpstream",
47 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
48 }),
49 );
50
51 const getJson = (path: string) =>
52 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
53
54 // The testing account is NOT registered with Audit Manager, and the service
55 // entered maintenance mode on 2026-04-30 so it can never be registered. On
56 // unregistered accounts every data-plane operation answers the typed
57 // AccessDeniedException; a registered account (AWS_TEST_AUDITMANAGER=1)
58 // answers real data. Both prove the binding + IAM grant + typed union
59 // end-to-end.
60 const expectOkOrAccessDenied = (response: unknown, extraTags: string[] = []) => {
61 const r = response as { ok: boolean; tag?: string };
62 if (!r.ok) {
63 expect(["AccessDeniedException", ...extraTags]).toContain(r.tag);
64 }
65 };
66
67 describe.sequential(
68 "AuditManager Bindings",
69 { tags: ["provider:aws", "provider:aws:auditmanager", "provider:aws:lambda", "live"] },
70 () => {
71 beforeAll(
72 Effect.gen(function* () {
73 yield* Effect.logInfo("AuditManager test setup: destroying previous resources");
74 yield* sharedStack.destroy();
75
76 yield* Effect.logInfo("AuditManager test setup: deploying fixture");
77 const { functionUrl } = yield* sharedStack.deploy(
78 Effect.gen(function* () {
79 return yield* AuditManagerTestFunction;
80 }).pipe(Effect.provide(AuditManagerTestFunctionLive)),
81 );
82
83 expect(functionUrl).toBeTruthy();
84 baseUrl = functionUrl!.replace(/\/+$/, "");
85
86 const readinessUrl = `${baseUrl}/bindings`;
87 yield* Effect.logInfo(`AuditManager test setup: probing readiness at ${readinessUrl}`);
88 yield* HttpClient.get(readinessUrl).pipe(
89 Effect.flatMap((response) =>
90 response.status === 200
91 ? Effect.succeed(response)
92 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
93 ),
94 Effect.tapError((error) =>
95 Effect.logWarning(`AuditManager test setup: fixture not ready yet (${String(error)})`),
96 ),
97 Effect.retry({ schedule: readinessPolicy }),
98 );
99 }),
100 { timeout: 240_000 },
101 );
102
103 afterAll(sharedStack.destroy(), { timeout: 120_000 });
104
105 describe("binding registration", () => {
106 test.provider("all 11 account-level capabilities initialize", (_stack) =>
107 Effect.gen(function* () {
108 const response = yield* getJson("/bindings");
109 expect((response as any).bound).toHaveLength(11);
110 }),
111 );
112 });
113
114 describe("GetAccountStatus", () => {
115 test.provider("succeeds end-to-end through the binding's IAM grant", (_stack) =>
116 Effect.gen(function* () {
117 // GetAccountStatus answers even on unregistered accounts — this is
118 // a REAL data-plane success through the attached policy.
119 const response = (yield* getJson("/account-status")) as { ok: boolean; status: string };
120 expect(response.ok).toBe(true);
121 expect(["ACTIVE", "INACTIVE", "PENDING_ACTIVATION"]).toContain(response.status);
122 }),
123 );
124 });
125
126 describe("GetServicesInScope", () => {
127 test.provider("answers data or the typed access tag", (_stack) =>
128 Effect.gen(function* () {
129 expectOkOrAccessDenied(yield* getJson("/services-in-scope"));
130 }),
131 );
132 });
133
134 describe("GetInsights", () => {
135 test.provider("answers data or the typed access tag", (_stack) =>
136 Effect.gen(function* () {
137 expectOkOrAccessDenied(yield* getJson("/insights"));
138 }),
139 );
140 });
141
142 describe("ListControlDomainInsights", () => {
143 test.provider("answers data or the typed access tag", (_stack) =>
144 Effect.gen(function* () {
145 expectOkOrAccessDenied(yield* getJson("/control-domain-insights"));
146 }),
147 );
148 });
149
150 describe("ListControlInsightsByControlDomain", () => {
151 test.provider("answers the typed tag for a nonexistent domain", (_stack) =>
152 Effect.gen(function* () {
153 expectOkOrAccessDenied(yield* getJson("/control-insights"), [
154 "ResourceNotFoundException",
155 "ValidationException",
156 ]);
157 }),
158 );
159 });
160
161 describe("GetDelegations", () => {
162 test.provider("answers data or the typed access tag", (_stack) =>
163 Effect.gen(function* () {
164 expectOkOrAccessDenied(yield* getJson("/delegations"));
165 }),
166 );
167 });
168
169 describe("GetEvidenceFileUploadUrl", () => {
170 test.provider("answers a presigned URL or the typed tag", (_stack) =>
171 Effect.gen(function* () {
172 const response = (yield* getJson("/upload-url")) as {
173 ok: boolean;
174 hasUrl?: boolean;
175 tag?: string;
176 };
177 if (response.ok) {
178 expect(response.hasUrl).toBe(true);
179 } else {
180 expect(["AccessDeniedException"]).toContain(response.tag);
181 }
182 }),
183 );
184 });
185
186 describe("ListAssessmentReports", () => {
187 test.provider("answers data or the typed access tag", (_stack) =>
188 Effect.gen(function* () {
189 expectOkOrAccessDenied(yield* getJson("/reports"));
190 }),
191 );
192 });
193
194 describe("ValidateAssessmentReportIntegrity", () => {
195 test.provider("answers the typed tag for a nonexistent report path", (_stack) =>
196 Effect.gen(function* () {
197 const response = (yield* getJson("/validate-report")) as { ok: boolean; tag?: string };
198 // Never ok — the S3 path doesn't exist; on unregistered accounts
199 // the request is rejected before it is even looked at.
200 expect(response.ok).toBe(false);
201 expect([
202 "AccessDeniedException",
203 "ResourceNotFoundException",
204 "ValidationException",
205 ]).toContain(response.tag);
206 }),
207 );
208 });
209
210 describe("ListKeywordsForDataSource", () => {
211 test.provider("answers keywords or the typed access tag", (_stack) =>
212 Effect.gen(function* () {
213 expectOkOrAccessDenied(yield* getJson("/keywords"));
214 }),
215 );
216 });
217
218 describe("ListNotifications", () => {
219 test.provider("answers data or the typed access tag", (_stack) =>
220 Effect.gen(function* () {
221 expectOkOrAccessDenied(yield* getJson("/notifications"));
222 }),
223 );
224 });
225
226 // Assessment-scoped bindings need an ACTIVE Audit Manager registration to
227 // deploy an Assessment. The testing account can never register (service in
228 // maintenance mode since 2026-04-30) — every deploy attempt fails with the
229 // typed AccessDeniedException on CreateControl. Run on a registered
230 // account with AWS_TEST_AUDITMANAGER=1.
231 describe(
232 "assessment-scoped bindings",
233 { tags: ["provider:aws:batch", "provider:aws:iam", "provider:aws:s3"] },
234 () => {
235 test.provider.skipIf(!process.env.AWS_TEST_AUDITMANAGER)(
236 "all 22 capabilities bind and the reads answer live data",
237 (_stack) =>
238 Effect.gen(function* () {
239 yield* assessmentStack.destroy();
240 const cleanup = assessmentStack.destroy().pipe(Effect.catchCause(() => Effect.void));
241
242 yield* Effect.gen(function* () {
243 const { functionUrl } = yield* assessmentStack.deploy(
244 Effect.gen(function* () {
245 return yield* AuditManagerAssessmentTestFunction;
246 }).pipe(Effect.provide(AuditManagerAssessmentTestFunctionLive)),
247 );
248 const assessmentBaseUrl = functionUrl!.replace(/\/+$/, "");
249
250 const ready = yield* HttpClient.get(`${assessmentBaseUrl}/bindings`).pipe(
251 Effect.flatMap((response) =>
252 response.status === 200
253 ? response.json
254 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
255 ),
256 Effect.retry({ schedule: readinessPolicy }),
257 );
258 expect((ready as any).bound).toHaveLength(22);
259
260 const folders = (yield* send(
261 HttpClientRequest.get(`${assessmentBaseUrl}/evidence-folders`),
262 ).pipe(Effect.flatMap((r) => r.json))) as { count: number };
263 expect(folders.count).toBeGreaterThanOrEqual(0);
264
265 const insights = (yield* send(
266 HttpClientRequest.get(`${assessmentBaseUrl}/insights`),
267 ).pipe(Effect.flatMap((r) => r.json))) as { totalAssessmentControlsCount: number };
268 expect(insights.totalAssessmentControlsCount).toBeGreaterThanOrEqual(0);
269
270 const changelogs = (yield* send(
271 HttpClientRequest.get(`${assessmentBaseUrl}/changelogs`),
272 ).pipe(Effect.flatMap((r) => r.json))) as { count: number };
273 expect(changelogs.count).toBeGreaterThanOrEqual(0);
274 }).pipe(Effect.ensuring(cleanup));
275 }),
276 { timeout: 600_000 },
277 );
278 },
279 );
280 },
281 );