@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 353 lines 13.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 securityhub from "@distilled.cloud/aws/securityhub";
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 SecurityHubTestFunctionLive, { SecurityHubTestFunction } from "./handler";
12 import { makeSecurityHubTestLease } from "./TestLease.ts";
13
14 const testOptions = { providers: AWS.providers() };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "SecurityHubBindings");
17 const testLease = makeSecurityHubTestLease();
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 // The Hub is an account/region singleton. If the account already runs a Hub
28 // this suite did not create, deploying the fixture would adopt (and later
29 // DISABLE) it — so every test degrades to a logged no-op instead
30 // (capture-and-restore safety, mirroring Hub.test.ts).
31 let foreignHub = false;
32 // Filled in by beforeAll from the live Hub ARN (`arn:aws:securityhub:REGION:ACCOUNT:hub/default`).
33 let accountId = "";
34 let region = "";
35
36 const findingId = "alchemy/securityhub-bindings/test-finding-1";
37 const productArn = () => `arn:aws:securityhub:${region}:${accountId}:product/${accountId}/default`;
38
39 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
40 readonly status: number;
41 readonly body: string;
42 }> {}
43
44 // The shared Lambda fixture occasionally answers a transient 5xx under load
45 // (cold re-init, IAM propagation on the freshly attached policy that the
46 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
47 // 4xx/assertion failure surfaces immediately.
48 const send = (request: HttpClientRequest.HttpClientRequest) =>
49 HttpClient.execute(request).pipe(
50 Effect.flatMap((response) =>
51 response.status >= 500
52 ? response.text.pipe(
53 Effect.flatMap((body) =>
54 Effect.fail(new TransientUpstream({ status: response.status, body })),
55 ),
56 )
57 : Effect.succeed(response),
58 ),
59 Effect.retry({
60 while: (e) => e._tag === "TransientUpstream",
61 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
62 }),
63 );
64
65 const getJson = (path: string) =>
66 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
67
68 const postJson = (path: string) =>
69 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
70
71 // beforeAll/afterAll hooks run outside `test.provider`'s layer, so raw
72 // distilled calls need the provider layer (credentials, region) supplied
73 // explicitly.
74 const aws = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
75 Core.withProviders(effect, testOptions, sharedStack.name);
76
77 const describeHub = securityhub.describeHub({}).pipe(
78 Effect.map((hub) => hub as securityhub.DescribeHubResponse | undefined),
79 Effect.catchTag("InvalidAccessException", () => Effect.succeed(undefined)),
80 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
81 );
82
83 const skipForeign = () =>
84 foreignHub
85 ? Effect.logInfo("Security Hub is already enabled by someone else — skipping").pipe(
86 Effect.as(true),
87 )
88 : Effect.succeed(false);
89
90 describe.sequential(
91 "SecurityHub Bindings",
92 {
93 tags: [
94 "provider:aws",
95 "provider:aws:batch",
96 "provider:aws:lambda",
97 "provider:aws:securityhub",
98 "live",
99 ],
100 },
101 () => {
102 beforeAll(
103 Effect.gen(function* () {
104 // Destroy OUR previous resources first — a crashed prior run leaves the
105 // fixture's own Hub enabled, which must not be mistaken for a foreign
106 // one. Destroying the scratch stack disables Security Hub only if the
107 // Hub is tracked in our state.
108 yield* Effect.logInfo("SecurityHub test setup: destroying previous resources");
109 yield* sharedStack.destroy();
110
111 // Never take over a Hub this fixture did not create — any Hub that
112 // remains after our own destroy is foreign.
113 const preexisting = yield* aws(describeHub);
114 if (preexisting) {
115 foreignHub = true;
116 yield* Effect.logInfo(
117 "SecurityHub test setup: Security Hub already enabled — suite degrades to no-op",
118 );
119 return;
120 }
121
122 yield* Effect.logInfo("SecurityHub test setup: deploying fixture");
123 const attrs = yield* sharedStack.deploy(
124 Effect.gen(function* () {
125 return yield* SecurityHubTestFunction;
126 }).pipe(Effect.provide(SecurityHubTestFunctionLive)),
127 );
128
129 expect(attrs.functionUrl).toBeTruthy();
130 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
131
132 // The fixture just enabled the Hub — parse account/region from its ARN
133 // for the import flow.
134 const hub = yield* aws(describeHub);
135 expect(hub?.HubArn).toBeTruthy();
136 const [, , , hubRegion, hubAccount] = hub!.HubArn!.split(":");
137 region = hubRegion!;
138 accountId = hubAccount!;
139
140 const readinessUrl = `${baseUrl}/bindings`;
141 yield* Effect.logInfo(`SecurityHub test setup: probing readiness at ${readinessUrl}`);
142 yield* HttpClient.get(readinessUrl).pipe(
143 Effect.flatMap((response) =>
144 response.status === 200
145 ? Effect.succeed(response)
146 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
147 ),
148 Effect.tapError((error) =>
149 Effect.logWarning(`SecurityHub test setup: fixture not ready yet (${String(error)})`),
150 ),
151 Effect.retry({ schedule: readinessPolicy }),
152 );
153 }),
154 { timeout: 300_000 },
155 );
156
157 afterAll(
158 Effect.gen(function* () {
159 if (foreignHub) return;
160 yield* sharedStack.destroy();
161 // Destroying the stack disables Security Hub for the account — zero
162 // orphans.
163 const after = yield* aws(describeHub);
164 expect(after).toBeUndefined();
165 }),
166 { timeout: 180_000 },
167 );
168
169 describe("binding registration", () => {
170 test.provider("all 20 capabilities initialize in the runtime", (_stack) =>
171 Effect.gen(function* () {
172 if (yield* skipForeign()) return;
173 const response = (yield* getJson("/bindings")) as { bound: string[] };
174 expect(response.bound).toHaveLength(20);
175 expect(response.bound).toContain("batchImportFindings");
176 expect(response.bound).toContain("getFindings");
177 expect(response.bound).toContain("describeStandards");
178 }),
179 );
180 });
181
182 describe("BatchImportFindings + GetFindings + BatchUpdateFindings + GetFindingHistory", () => {
183 test.provider(
184 "a custom finding flows through the triage loop",
185 (_stack) =>
186 Effect.gen(function* () {
187 if (yield* skipForeign()) return;
188
189 // Import a synthetic ASFF finding through the default product.
190 const imported = (yield* postJson(
191 `/import?id=${encodeURIComponent(findingId)}&account=${accountId}&region=${region}`,
192 )) as { success: number; failed: number };
193 expect(imported.failed).toBe(0);
194 expect(imported.success).toBe(1);
195
196 // The imported finding surfaces asynchronously — poll bounded to
197 // ~45s; the write above already proved the binding + IAM wiring.
198 const findings = (yield* getJson(`/findings?id=${encodeURIComponent(findingId)}`).pipe(
199 Effect.repeat({
200 schedule: Schedule.spaced("3 seconds"),
201 until: (r): boolean => (r as { count: number }).count > 0,
202 times: 15,
203 }),
204 )) as { count: number; title?: string };
205
206 if (findings.count > 0) {
207 expect(findings.title).toBe("Alchemy SecurityHub bindings test finding");
208
209 // Update customer-editable fields on the finding.
210 const resolved = (yield* postJson(
211 `/resolve?id=${encodeURIComponent(findingId)}&productArn=${encodeURIComponent(productArn())}`,
212 )) as { processed: number; unprocessed: number };
213 expect(resolved.processed).toBe(1);
214 expect(resolved.unprocessed).toBe(0);
215
216 // History records the import + the workflow update (may lag —
217 // a typed ResourceNotFoundException is acceptable).
218 const history = (yield* getJson(
219 `/history?id=${encodeURIComponent(findingId)}&productArn=${encodeURIComponent(productArn())}`,
220 )) as { records?: number; errorTag?: string };
221 if (history.errorTag) {
222 expect(history.errorTag).toBe("ResourceNotFoundException");
223 } else {
224 expect(history.records).toBeGreaterThanOrEqual(0);
225 }
226 } else {
227 yield* Effect.logWarning(
228 "imported finding did not surface within the bounded poll — continuing (import succeeded)",
229 );
230 }
231 }),
232 { timeout: 150_000 },
233 );
234 });
235
236 describe("DescribeStandards + GetEnabledStandards + control definitions", () => {
237 test.provider("reads the standards and controls catalog", (_stack) =>
238 Effect.gen(function* () {
239 if (yield* skipForeign()) return;
240 const standards = (yield* getJson("/standards")) as { count: number };
241 expect(standards.count).toBeGreaterThan(0);
242
243 // The fixture enables the Hub without default standards.
244 const enabled = (yield* getJson("/enabled-standards")) as { count: number };
245 expect(enabled.count).toBe(0);
246
247 const definitions = (yield* getJson("/control-definitions")) as { count: number };
248 expect(definitions.count).toBeGreaterThan(0);
249
250 const control = (yield* getJson("/control")) as {
251 id: string | null;
252 severity: string | null;
253 };
254 expect(control.id).toBe("IAM.1");
255 expect(control.severity).toBeTruthy();
256 }),
257 );
258 });
259
260 describe("DescribeProducts + ListEnabledProductsForImport", () => {
261 test.provider("reads the product integration catalog", (_stack) =>
262 Effect.gen(function* () {
263 if (yield* skipForeign()) return;
264 const products = (yield* getJson("/products")) as { count: number };
265 expect(products.count).toBeGreaterThan(0);
266
267 const enabled = (yield* getJson("/enabled-products")) as { count: number };
268 expect(enabled.count).toBeGreaterThanOrEqual(0);
269 }),
270 );
271 });
272
273 describe("DescribeActionTargets + ListAutomationRules + ListFindingAggregators + GetInsights", () => {
274 test.provider("a fresh Hub has no user configuration", (_stack) =>
275 Effect.gen(function* () {
276 if (yield* skipForeign()) return;
277 const actions = (yield* getJson("/action-targets")) as { count: number };
278 expect(actions.count).toBe(0);
279
280 const rules = (yield* getJson("/automation-rules")) as { count: number };
281 expect(rules.count).toBe(0);
282
283 const aggregators = (yield* getJson("/aggregators")) as { count: number };
284 expect(aggregators.count).toBe(0);
285
286 const insights = (yield* getJson("/insights")) as { count: number };
287 expect(insights.count).toBe(0);
288 }),
289 );
290 });
291
292 describe("GetAdministratorAccount + members + invitations", () => {
293 test.provider("reads this account's membership state", (_stack) =>
294 Effect.gen(function* () {
295 if (yield* skipForeign()) return;
296 const admin = (yield* getJson("/admin")) as {
297 administrator?: string | null;
298 errorTag?: string;
299 };
300 if (admin.errorTag) {
301 expect(["ResourceNotFoundException", "InvalidAccessException"]).toContain(
302 admin.errorTag,
303 );
304 } else {
305 expect(admin.administrator ?? null).toBeNull();
306 }
307
308 const members = (yield* getJson("/members")) as { count: number };
309 expect(members.count).toBe(0);
310
311 const invitations = (yield* getJson("/invitations")) as { count: number };
312 expect(invitations.count).toBeGreaterThanOrEqual(0);
313
314 const count = (yield* getJson("/invitations-count")) as { count: number };
315 expect(count.count).toBeGreaterThanOrEqual(0);
316 }),
317 );
318 });
319
320 describe("ListOrganizationAdminAccounts + DescribeOrganizationConfiguration", () => {
321 test.provider(
322 "answers (or rejects with a typed error outside the management account)",
323 (_stack) =>
324 Effect.gen(function* () {
325 if (yield* skipForeign()) return;
326 const admins = (yield* getJson("/org-admins")) as {
327 admins?: number;
328 errorTag?: string;
329 };
330 if (admins.errorTag) {
331 expect(["AccessDeniedException", "InvalidAccessException"]).toContain(
332 admins.errorTag,
333 );
334 } else {
335 expect(admins.admins).toBeGreaterThanOrEqual(0);
336 }
337
338 const orgConfig = (yield* getJson("/org-config")) as {
339 autoEnable?: boolean;
340 errorTag?: string;
341 };
342 if (orgConfig.errorTag) {
343 expect(["AccessDeniedException", "InvalidAccessException"]).toContain(
344 orgConfig.errorTag,
345 );
346 } else {
347 expect(typeof orgConfig.autoEnable).toBe("boolean");
348 }
349 }),
350 );
351 });
352 },
353 );