The best way to control Claude Code, Codex and more from any computer or phone
| 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 guardduty from "@distilled.cloud/aws/guardduty"; |
| 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 GuardDutyTestFunctionLive, { GuardDutyTestFunction } from "./handler"; |
| 12 | import { makeGuardDutyTestLease } from "./TestLease.ts"; |
| 13 | |
| 14 | const testOptions = { providers: AWS.providers() }; |
| 15 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 16 | const sharedStack = Core.scratchStack(testOptions, "GuardDutyBindings"); |
| 17 | const testLease = makeGuardDutyTestLease(); |
| 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 accountId: string; |
| 28 | // The GuardDuty detector is an account/region singleton. If the account |
| 29 | // already runs a detector this suite did not create, deploying the fixture |
| 30 | // would adopt (and later DELETE) it — so every test degrades to a logged |
| 31 | // no-op instead (capture-and-restore safety, mirroring Detector.test.ts). |
| 32 | let foreignDetectorId: string | undefined; |
| 33 | |
| 34 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 35 | readonly status: number; |
| 36 | readonly body: string; |
| 37 | }> {} |
| 38 | |
| 39 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 40 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 41 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 42 | // 4xx/assertion failure surfaces immediately. |
| 43 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 44 | HttpClient.execute(request).pipe( |
| 45 | Effect.flatMap((response) => |
| 46 | response.status >= 500 |
| 47 | ? response.text.pipe( |
| 48 | Effect.flatMap((body) => |
| 49 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 50 | ), |
| 51 | ) |
| 52 | : Effect.succeed(response), |
| 53 | ), |
| 54 | Effect.retry({ |
| 55 | while: (e) => e._tag === "TransientUpstream", |
| 56 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | const getJson = (path: string) => |
| 61 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 62 | |
| 63 | const postJson = (path: string) => |
| 64 | send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 65 | |
| 66 | // beforeAll/afterAll hooks run outside `test.provider`'s layer, so raw |
| 67 | // distilled calls need the provider layer (credentials, region) supplied |
| 68 | // explicitly. |
| 69 | const aws = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 70 | Core.withProviders(effect, testOptions, sharedStack.name); |
| 71 | |
| 72 | const skipForeign = () => |
| 73 | foreignDetectorId |
| 74 | ? Effect.logInfo( |
| 75 | `GuardDuty detector ${foreignDetectorId} already exists and is not ours — skipping`, |
| 76 | ).pipe(Effect.as(true)) |
| 77 | : Effect.succeed(false); |
| 78 | |
| 79 | describe.sequential( |
| 80 | "GuardDuty Bindings", |
| 81 | { tags: ["provider:aws", "provider:aws:guardduty", "provider:aws:lambda", "live"] }, |
| 82 | () => { |
| 83 | beforeAll( |
| 84 | Effect.gen(function* () { |
| 85 | // Never take over a detector this fixture did not create. |
| 86 | const preexisting = (yield* aws(guardduty.listDetectors({}))).DetectorIds?.[0]; |
| 87 | if (preexisting) { |
| 88 | const detector = yield* aws(guardduty.getDetector({ DetectorId: preexisting })); |
| 89 | if (detector.Tags?.["fixture"] !== "guardduty-bindings") { |
| 90 | foreignDetectorId = preexisting; |
| 91 | yield* Effect.logInfo( |
| 92 | `GuardDuty test setup: foreign detector ${preexisting} present — suite degrades to no-op`, |
| 93 | ); |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | yield* Effect.logInfo("GuardDuty test setup: destroying previous resources"); |
| 99 | yield* sharedStack.destroy(); |
| 100 | |
| 101 | yield* Effect.logInfo("GuardDuty test setup: deploying fixture"); |
| 102 | const attrs = yield* sharedStack.deploy( |
| 103 | Effect.gen(function* () { |
| 104 | return yield* GuardDutyTestFunction; |
| 105 | }).pipe(Effect.provide(GuardDutyTestFunctionLive)), |
| 106 | ); |
| 107 | |
| 108 | expect(attrs.functionUrl).toBeTruthy(); |
| 109 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 110 | accountId = attrs.roleArn.split(":")[4]!; |
| 111 | |
| 112 | const readinessUrl = `${baseUrl}/bindings`; |
| 113 | yield* Effect.logInfo(`GuardDuty test setup: probing readiness at ${readinessUrl}`); |
| 114 | yield* HttpClient.get(readinessUrl).pipe( |
| 115 | Effect.flatMap((response) => |
| 116 | response.status === 200 |
| 117 | ? Effect.succeed(response) |
| 118 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 119 | ), |
| 120 | Effect.tapError((error) => |
| 121 | Effect.logWarning(`GuardDuty test setup: fixture not ready yet (${String(error)})`), |
| 122 | ), |
| 123 | Effect.retry({ schedule: readinessPolicy }), |
| 124 | ); |
| 125 | }), |
| 126 | { timeout: 240_000 }, |
| 127 | ); |
| 128 | |
| 129 | afterAll( |
| 130 | Effect.gen(function* () { |
| 131 | if (foreignDetectorId) return; |
| 132 | yield* sharedStack.destroy(); |
| 133 | }), |
| 134 | { timeout: 120_000 }, |
| 135 | ); |
| 136 | |
| 137 | describe("binding registration", () => { |
| 138 | test.provider("all 19 capabilities initialize in the runtime", (_stack) => |
| 139 | Effect.gen(function* () { |
| 140 | if (yield* skipForeign()) return; |
| 141 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 142 | expect(response.bound).toHaveLength(19); |
| 143 | expect(response.bound).toContain("createSampleFindings"); |
| 144 | expect(response.bound).toContain("listFindings"); |
| 145 | expect(response.bound).toContain("getInvitationsCount"); |
| 146 | }), |
| 147 | ); |
| 148 | }); |
| 149 | |
| 150 | describe("CreateSampleFindings + ListFindings + GetFindings + ArchiveFindings", () => { |
| 151 | test.provider( |
| 152 | "sample finding round-trips through the triage loop (injected detector id)", |
| 153 | (_stack) => |
| 154 | Effect.gen(function* () { |
| 155 | if (yield* skipForeign()) return; |
| 156 | |
| 157 | // Generate a sample finding on our own detector. |
| 158 | const sample = (yield* postJson("/sample")) as { ok: boolean }; |
| 159 | expect(sample.ok).toBe(true); |
| 160 | |
| 161 | // Sample findings surface asynchronously — poll bounded. |
| 162 | const findings = (yield* getJson("/findings").pipe( |
| 163 | Effect.repeat({ |
| 164 | schedule: Schedule.spaced("3 seconds"), |
| 165 | until: (r): boolean => (r as { count: number }).count > 0, |
| 166 | times: 20, |
| 167 | }), |
| 168 | )) as { count: number; ids: string[] }; |
| 169 | expect(findings.count).toBeGreaterThan(0); |
| 170 | |
| 171 | // Hydrate the full finding document. |
| 172 | const detail = (yield* getJson( |
| 173 | `/finding-detail?id=${encodeURIComponent(findings.ids[0]!)}`, |
| 174 | )) as { count: number; type?: string; archived: boolean }; |
| 175 | expect(detail.count).toBe(1); |
| 176 | expect(detail.type).toBe("Recon:EC2/PortProbeUnprotectedPort"); |
| 177 | |
| 178 | // Statistics can lag behind the finding itself. |
| 179 | const stats = (yield* getJson("/stats").pipe( |
| 180 | Effect.repeat({ |
| 181 | schedule: Schedule.spaced("3 seconds"), |
| 182 | until: (r): boolean => (r as { severities: string[] }).severities.length > 0, |
| 183 | times: 10, |
| 184 | }), |
| 185 | )) as { severities: string[] }; |
| 186 | expect(stats.severities.length).toBeGreaterThan(0); |
| 187 | |
| 188 | // Archive everything we generated — a real write. |
| 189 | const archived = (yield* postJson("/archive")) as { archived: number }; |
| 190 | expect(archived.archived).toBeGreaterThan(0); |
| 191 | }), |
| 192 | { timeout: 120_000 }, |
| 193 | ); |
| 194 | }); |
| 195 | |
| 196 | describe("ListMembers", () => { |
| 197 | test.provider("a standalone detector has no members", (_stack) => |
| 198 | Effect.gen(function* () { |
| 199 | if (yield* skipForeign()) return; |
| 200 | const response = (yield* getJson("/members")) as { count: number }; |
| 201 | expect(response.count).toBe(0); |
| 202 | }), |
| 203 | ); |
| 204 | }); |
| 205 | |
| 206 | describe("GetAdministratorAccount", () => { |
| 207 | test.provider( |
| 208 | "a standalone account has no administrator (typed outcome either way)", |
| 209 | (_stack) => |
| 210 | Effect.gen(function* () { |
| 211 | if (yield* skipForeign()) return; |
| 212 | const response = (yield* getJson("/admin")) as { |
| 213 | administrator?: string | null; |
| 214 | errorTag?: string; |
| 215 | }; |
| 216 | if (response.errorTag) { |
| 217 | // GuardDuty rejects the call outright for a standalone |
| 218 | // (non-member) account. |
| 219 | expect(["BadRequestException", "AccessDeniedException"]).toContain(response.errorTag); |
| 220 | } else { |
| 221 | expect(response.administrator ?? null).toBeNull(); |
| 222 | } |
| 223 | }), |
| 224 | ); |
| 225 | }); |
| 226 | |
| 227 | describe("GetMalwareScanSettings", () => { |
| 228 | test.provider("reads the detector's malware scan settings", (_stack) => |
| 229 | Effect.gen(function* () { |
| 230 | if (yield* skipForeign()) return; |
| 231 | const response = (yield* getJson("/malware-settings")) as { |
| 232 | ebsSnapshotPreservation?: string | null; |
| 233 | errorTag?: string; |
| 234 | }; |
| 235 | if (response.errorTag) { |
| 236 | // Malware Protection may not be enabled on a fresh detector. |
| 237 | expect(["BadRequestException"]).toContain(response.errorTag); |
| 238 | } else { |
| 239 | expect(response).toHaveProperty("ebsSnapshotPreservation"); |
| 240 | } |
| 241 | }), |
| 242 | ); |
| 243 | }); |
| 244 | |
| 245 | describe("GetUsageStatistics", () => { |
| 246 | test.provider("reports usage grouped by data source", (_stack) => |
| 247 | Effect.gen(function* () { |
| 248 | if (yield* skipForeign()) return; |
| 249 | const response = (yield* getJson("/usage")) as { dataSources: number }; |
| 250 | expect(response.dataSources).toBeGreaterThanOrEqual(0); |
| 251 | }), |
| 252 | ); |
| 253 | }); |
| 254 | |
| 255 | describe("ListCoverage + GetRemainingFreeTrialDays", () => { |
| 256 | test.provider("reads coverage and free-trial state", (_stack) => |
| 257 | Effect.gen(function* () { |
| 258 | if (yield* skipForeign()) return; |
| 259 | const coverage = (yield* getJson("/coverage")) as { resources: number }; |
| 260 | expect(coverage.resources).toBeGreaterThanOrEqual(0); |
| 261 | |
| 262 | const freeTrial = (yield* getJson(`/free-trial?account=${accountId}`)) as { |
| 263 | accounts: number; |
| 264 | }; |
| 265 | expect(freeTrial.accounts).toBeGreaterThanOrEqual(0); |
| 266 | }), |
| 267 | ); |
| 268 | }); |
| 269 | |
| 270 | describe("ListInvestigations", () => { |
| 271 | test.provider("a fresh detector has no investigations", (_stack) => |
| 272 | Effect.gen(function* () { |
| 273 | if (yield* skipForeign()) return; |
| 274 | const response = (yield* getJson("/investigations")) as { |
| 275 | count?: number; |
| 276 | errorTag?: string; |
| 277 | }; |
| 278 | if (response.errorTag) { |
| 279 | // Extended Threat Detection may be unavailable for the account. |
| 280 | expect(["BadRequestException", "AccessDeniedException"]).toContain(response.errorTag); |
| 281 | } else { |
| 282 | expect(response.count).toBe(0); |
| 283 | } |
| 284 | }), |
| 285 | ); |
| 286 | }); |
| 287 | |
| 288 | describe("DescribeOrganizationConfiguration", () => { |
| 289 | test.provider( |
| 290 | "answers (or rejects with a typed error for a non-delegated account)", |
| 291 | (_stack) => |
| 292 | Effect.gen(function* () { |
| 293 | if (yield* skipForeign()) return; |
| 294 | const response = (yield* getJson("/org-config")) as { |
| 295 | autoEnable?: boolean; |
| 296 | errorTag?: string; |
| 297 | }; |
| 298 | if (response.errorTag) { |
| 299 | expect(["BadRequestException", "AccessDeniedException"]).toContain(response.errorTag); |
| 300 | } else { |
| 301 | expect(typeof response.autoEnable).toBe("boolean"); |
| 302 | } |
| 303 | }), |
| 304 | ); |
| 305 | }); |
| 306 | |
| 307 | describe("ListOrganizationAdminAccounts + GetOrganizationStatistics", () => { |
| 308 | test.provider( |
| 309 | "answers (or rejects with a typed error outside the management account)", |
| 310 | (_stack) => |
| 311 | Effect.gen(function* () { |
| 312 | if (yield* skipForeign()) return; |
| 313 | const admins = (yield* getJson("/org-admins")) as { |
| 314 | admins?: number; |
| 315 | errorTag?: string; |
| 316 | }; |
| 317 | if (admins.errorTag) { |
| 318 | expect(["BadRequestException", "AccessDeniedException"]).toContain(admins.errorTag); |
| 319 | } else { |
| 320 | expect(admins.admins).toBeGreaterThanOrEqual(0); |
| 321 | } |
| 322 | |
| 323 | const stats = (yield* getJson("/org-stats")) as { |
| 324 | activeAccounts?: number; |
| 325 | errorTag?: string; |
| 326 | }; |
| 327 | if (stats.errorTag) { |
| 328 | expect(["BadRequestException", "AccessDeniedException"]).toContain(stats.errorTag); |
| 329 | } else { |
| 330 | expect(stats.activeAccounts).toBeGreaterThanOrEqual(0); |
| 331 | } |
| 332 | }), |
| 333 | ); |
| 334 | }); |
| 335 | |
| 336 | describe("GetInvitationsCount + ListInvitations", () => { |
| 337 | test.provider("reads this account's invitation state", (_stack) => |
| 338 | Effect.gen(function* () { |
| 339 | if (yield* skipForeign()) return; |
| 340 | const count = (yield* getJson("/invitations-count")) as { count: number }; |
| 341 | expect(count.count).toBeGreaterThanOrEqual(0); |
| 342 | |
| 343 | const invitations = (yield* getJson("/invitations")) as { count: number }; |
| 344 | expect(invitations.count).toBeGreaterThanOrEqual(0); |
| 345 | }), |
| 346 | ); |
| 347 | }); |
| 348 | }, |
| 349 | ); |