@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 359 lines 14.2 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 macie2 from "@distilled.cloud/aws/macie2";
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 Macie2TestFunctionLive, { Macie2TestFunction } from "./handler";
12 import { makeMacie2TestLease } from "./TestLease.ts";
13
14 const testOptions = { providers: AWS.providers() };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "Macie2Bindings");
17 const testLease = makeMacie2TestLease();
18
19 // This budget covers waiting behind the other Macie files, not an AWS API
20 // operation. Bindings can legitimately hold the singleton lease for several
21 // minutes under a full parallel sweep (setup + sample-finding poll + cleanup).
22 beforeAll(testLease.acquire, { timeout: 3_600_000 });
23 afterAll(testLease.release);
24
25 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
26 // over 60s on a fresh deploy.
27 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
28
29 let baseUrl: string;
30 // The Macie session is an account/region singleton with no tags, so ownership
31 // cannot be verified from the cloud. If the account already runs a session
32 // this suite did not create, deploying the fixture would adopt (and later
33 // DISABLE) it — so every test degrades to a logged no-op instead
34 // (capture-and-restore safety, mirroring Session.test.ts).
35 let foreignSession = false;
36
37 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
38 readonly status: number;
39 readonly body: string;
40 }> {}
41
42 // The shared Lambda fixture occasionally answers a transient 5xx under load
43 // (cold re-init, IAM propagation on the freshly attached policy that the
44 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
45 // 4xx/assertion failure surfaces immediately.
46 const send = (request: HttpClientRequest.HttpClientRequest) =>
47 HttpClient.execute(request).pipe(
48 Effect.flatMap((response) =>
49 response.status >= 500
50 ? response.text.pipe(
51 Effect.flatMap((body) =>
52 Effect.fail(new TransientUpstream({ status: response.status, body })),
53 ),
54 )
55 : Effect.succeed(response),
56 ),
57 Effect.retry({
58 while: (e) => e._tag === "TransientUpstream",
59 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
60 }),
61 );
62
63 const getJson = (path: string) =>
64 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
65
66 const postJson = (path: string) =>
67 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
68
69 // beforeAll/afterAll hooks run outside `test.provider`'s layer, so raw
70 // distilled calls need the provider layer (credentials, region) supplied
71 // explicitly.
72 const aws = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
73 Core.withProviders(effect, testOptions, sharedStack.name);
74
75 const getSession = macie2.getMacieSession({}).pipe(
76 Effect.map((s) => s as macie2.GetMacieSessionResponse | undefined),
77 Effect.catchTag("AccessDeniedException", () => Effect.succeed(undefined)),
78 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
79 );
80
81 const skipForeign = () =>
82 foreignSession
83 ? Effect.logInfo("Macie is already enabled by someone else — skipping").pipe(Effect.as(true))
84 : Effect.succeed(false);
85
86 describe.sequential(
87 "Macie2 Bindings",
88 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:macie2", "live"] },
89 () => {
90 beforeAll(
91 Effect.gen(function* () {
92 // Destroy OUR previous resources first — a crashed prior run leaves the
93 // fixture's own session enabled, which must not be mistaken for a
94 // foreign one. Destroying the scratch stack disables Macie only if the
95 // session is tracked in our state.
96 yield* Effect.logInfo("Macie2 test setup: destroying previous resources");
97 yield* sharedStack.destroy();
98
99 // Never take over a Macie session this fixture did not create — any
100 // session that remains after our own destroy is foreign.
101 const preexisting = yield* aws(getSession);
102 if (preexisting) {
103 foreignSession = true;
104 yield* Effect.logInfo(
105 "Macie2 test setup: Macie already enabled — suite degrades to no-op",
106 );
107 return;
108 }
109
110 yield* Effect.logInfo("Macie2 test setup: deploying fixture");
111 const attrs = yield* sharedStack.deploy(
112 Effect.gen(function* () {
113 return yield* Macie2TestFunction;
114 }).pipe(Effect.provide(Macie2TestFunctionLive)),
115 );
116
117 expect(attrs.functionUrl).toBeTruthy();
118 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
119
120 const readinessUrl = `${baseUrl}/bindings`;
121 yield* Effect.logInfo(`Macie2 test setup: probing readiness at ${readinessUrl}`);
122 yield* HttpClient.get(readinessUrl).pipe(
123 Effect.flatMap((response) =>
124 response.status === 200
125 ? Effect.succeed(response)
126 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
127 ),
128 Effect.tapError((error) =>
129 Effect.logWarning(`Macie2 test setup: fixture not ready yet (${String(error)})`),
130 ),
131 Effect.retry({ schedule: readinessPolicy }),
132 );
133 }),
134 { timeout: 300_000 },
135 );
136
137 afterAll(
138 Effect.gen(function* () {
139 if (foreignSession) return;
140 yield* sharedStack.destroy();
141 // Destroying the stack disables Macie for the account — zero orphans.
142 const after = yield* aws(getSession);
143 expect(after).toBeUndefined();
144 }),
145 { timeout: 180_000 },
146 );
147
148 describe("binding registration", () => {
149 test.provider("all 22 capabilities initialize in the runtime", (_stack) =>
150 Effect.gen(function* () {
151 if (yield* skipForeign()) return;
152 const response = (yield* getJson("/bindings")) as { bound: string[] };
153 expect(response.bound).toHaveLength(22);
154 expect(response.bound).toContain("createSampleFindings");
155 expect(response.bound).toContain("listFindings");
156 expect(response.bound).toContain("testCustomDataIdentifier");
157 }),
158 );
159 });
160
161 describe("TestCustomDataIdentifier", () => {
162 test.provider("a detection regex round-trips with match counts", (_stack) =>
163 Effect.gen(function* () {
164 if (yield* skipForeign()) return;
165 const response = (yield* postJson("/test-identifier")) as { matchCount: number };
166 expect(response.matchCount).toBe(2);
167 }),
168 );
169 });
170
171 describe("CreateSampleFindings + ListFindings + GetFindings + GetFindingStatistics", () => {
172 test.provider(
173 "sample findings flow through the triage loop",
174 (_stack) =>
175 Effect.gen(function* () {
176 if (yield* skipForeign()) return;
177
178 // Generate sample findings.
179 const sample = (yield* postJson("/sample")) as { ok: boolean };
180 expect(sample.ok).toBe(true);
181
182 // Sample findings surface asynchronously (documented up to ~5
183 // minutes) — poll bounded to ~60s; the writes above already proved
184 // the binding + IAM wiring, so a slow surface is not a failure.
185 const findings = (yield* getJson("/findings").pipe(
186 Effect.repeat({
187 schedule: Schedule.spaced("3 seconds"),
188 until: (r): boolean => (r as { count: number }).count > 0,
189 times: 20,
190 }),
191 )) as { count: number; ids: string[] };
192
193 if (findings.count > 0) {
194 // Hydrate the full finding document.
195 const detail = (yield* getJson(
196 `/finding-detail?id=${encodeURIComponent(findings.ids[0]!)}`,
197 )) as { count: number; type?: string; sample: boolean };
198 expect(detail.count).toBe(1);
199 expect(detail.sample).toBe(true);
200 } else {
201 yield* Effect.logWarning(
202 "Macie sample findings did not surface within the bounded poll — continuing (writes succeeded)",
203 );
204 }
205
206 // Statistics grouped by severity answer either way.
207 const stats = (yield* getJson("/finding-stats")) as { groups: number };
208 expect(stats.groups).toBeGreaterThanOrEqual(0);
209 }),
210 { timeout: 150_000 },
211 );
212 });
213
214 describe("GetBucketStatistics + SearchResources", () => {
215 test.provider("reads Macie's S3 inventory", (_stack) =>
216 Effect.gen(function* () {
217 if (yield* skipForeign()) return;
218 const buckets = (yield* getJson("/buckets")) as { bucketCount: number };
219 expect(buckets.bucketCount).toBeGreaterThanOrEqual(0);
220
221 const search = (yield* getJson("/search")) as { matches: number };
222 expect(search.matches).toBeGreaterThanOrEqual(0);
223 }),
224 );
225 });
226
227 describe("ListManagedDataIdentifiers", () => {
228 test.provider("lists Macie's managed data identifiers", (_stack) =>
229 Effect.gen(function* () {
230 if (yield* skipForeign()) return;
231 const response = (yield* getJson("/managed-identifiers")) as { count: number };
232 expect(response.count).toBeGreaterThan(0);
233 }),
234 );
235 });
236
237 describe("ListAllowLists + ListFindingsFilters + ListClassificationJobs", () => {
238 test.provider("a fresh session has no user configuration", (_stack) =>
239 Effect.gen(function* () {
240 if (yield* skipForeign()) return;
241 const allowLists = (yield* getJson("/allow-lists")) as { count: number };
242 expect(allowLists.count).toBe(0);
243
244 const filters = (yield* getJson("/findings-filters")) as { count: number };
245 expect(filters.count).toBe(0);
246
247 const jobs = (yield* getJson("/jobs")) as { count: number };
248 expect(jobs.count).toBeGreaterThanOrEqual(0);
249 }),
250 );
251 });
252
253 describe("GetClassificationExportConfiguration + GetUsageTotals", () => {
254 test.provider("reads export configuration and usage totals", (_stack) =>
255 Effect.gen(function* () {
256 if (yield* skipForeign()) return;
257 const exportConfig = (yield* getJson("/export-config")) as { configured: boolean };
258 expect(exportConfig.configured).toBe(false);
259
260 const usage = (yield* getJson("/usage")) as { totals: number };
261 expect(usage.totals).toBeGreaterThanOrEqual(0);
262 }),
263 );
264 });
265
266 describe("GetAutomatedDiscoveryConfiguration + ListClassificationScopes + GetRevealConfiguration", () => {
267 test.provider("reads the discovery and reveal configuration", (_stack) =>
268 Effect.gen(function* () {
269 if (yield* skipForeign()) return;
270 const discovery = (yield* getJson("/auto-discovery")) as { status: string | null };
271 expect(discovery.status).not.toBeUndefined();
272
273 const scopes = (yield* getJson("/scopes")) as { count: number };
274 expect(scopes.count).toBeGreaterThanOrEqual(0);
275
276 const reveal = (yield* getJson("/reveal")) as {
277 status?: string | null;
278 errorTag?: string;
279 };
280 if (reveal.errorTag) {
281 // A fresh session rejects the read until sample retrieval is
282 // configured — the typed tag proves the binding + IAM wiring.
283 expect(reveal.errorTag).toBe("AccessDeniedException");
284 } else {
285 expect(["ENABLED", "DISABLED", null]).toContain(reveal.status);
286 }
287 }),
288 );
289 });
290
291 describe("GetAdministratorAccount", () => {
292 test.provider(
293 "a standalone account has no administrator (typed outcome either way)",
294 (_stack) =>
295 Effect.gen(function* () {
296 if (yield* skipForeign()) return;
297 const response = (yield* getJson("/admin")) as {
298 administrator?: string | null;
299 errorTag?: string;
300 };
301 if (response.errorTag) {
302 expect(["ResourceNotFoundException", "AccessDeniedException"]).toContain(
303 response.errorTag,
304 );
305 } else {
306 expect(response.administrator ?? null).toBeNull();
307 }
308 }),
309 );
310 });
311
312 describe("GetInvitationsCount + ListInvitations + ListMembers", () => {
313 test.provider("reads this account's membership state", (_stack) =>
314 Effect.gen(function* () {
315 if (yield* skipForeign()) return;
316 const count = (yield* getJson("/invitations-count")) as { count: number };
317 expect(count.count).toBeGreaterThanOrEqual(0);
318
319 const invitations = (yield* getJson("/invitations")) as { count: number };
320 expect(invitations.count).toBeGreaterThanOrEqual(0);
321
322 const members = (yield* getJson("/members")) as { count: number };
323 expect(members.count).toBe(0);
324 }),
325 );
326 });
327
328 describe("ListOrganizationAdminAccounts + DescribeOrganizationConfiguration", () => {
329 test.provider(
330 "answers (or rejects with a typed error outside the management account)",
331 (_stack) =>
332 Effect.gen(function* () {
333 if (yield* skipForeign()) return;
334 const admins = (yield* getJson("/org-admins")) as {
335 admins?: number;
336 errorTag?: string;
337 };
338 if (admins.errorTag) {
339 expect(["AccessDeniedException", "ValidationException"]).toContain(admins.errorTag);
340 } else {
341 expect(admins.admins).toBeGreaterThanOrEqual(0);
342 }
343
344 const orgConfig = (yield* getJson("/org-config")) as {
345 autoEnable?: boolean;
346 errorTag?: string;
347 };
348 if (orgConfig.errorTag) {
349 expect(["AccessDeniedException", "ValidationException"]).toContain(
350 orgConfig.errorTag,
351 );
352 } else {
353 expect(typeof orgConfig.autoEnable).toBe("boolean");
354 }
355 }),
356 );
357 });
358 },
359 );