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
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import type { PolicyDocument } from "@alchemy.run/aws/IAM/Policy"; |
| 3 | import { ResourcePolicy } from "@alchemy.run/aws/Logs/ResourcePolicy"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as logs from "@distilled.cloud/aws/cloudwatch-logs"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | // CAUTION: at most 10 resource policies per region per account. This suite |
| 14 | // uses ONE deterministic policy name and guarantees deletion via |
| 15 | // stack.destroy() at the start, at the end, and on error — never leak. |
| 16 | const POLICY_NAME = "alchemy-test-logs-resource-policy"; |
| 17 | |
| 18 | const findPolicy = Effect.fn(function* (policyName: string) { |
| 19 | // quota is 10 policies per region — one call is exhaustive |
| 20 | const described = yield* logs.describeResourcePolicies({ limit: 50 }); |
| 21 | return (described.resourcePolicies ?? []).find((policy) => policy.policyName === policyName); |
| 22 | }); |
| 23 | |
| 24 | class ResourcePolicyStillExists extends Data.TaggedError("ResourcePolicyStillExists")<{ |
| 25 | readonly policyName: string; |
| 26 | }> {} |
| 27 | |
| 28 | const assertPolicyDeleted = (policyName: string) => |
| 29 | findPolicy(policyName).pipe( |
| 30 | Effect.flatMap((policy) => |
| 31 | policy === undefined |
| 32 | ? Effect.void |
| 33 | : Effect.fail(new ResourcePolicyStillExists({ policyName })), |
| 34 | ), |
| 35 | Effect.retry({ |
| 36 | while: (e) => e._tag === "ResourcePolicyStillExists", |
| 37 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | const route53Document = (sid: string): PolicyDocument => ({ |
| 42 | Version: "2012-10-17", |
| 43 | Statement: [ |
| 44 | { |
| 45 | Sid: sid, |
| 46 | Effect: "Allow", |
| 47 | Principal: { Service: "route53.amazonaws.com" }, |
| 48 | Action: ["logs:CreateLogStream", "logs:PutLogEvents"], |
| 49 | Resource: ["arn:aws:logs:*:*:log-group:/aws/route53/alchemy-test-*"], |
| 50 | }, |
| 51 | ], |
| 52 | }); |
| 53 | |
| 54 | test.provider( |
| 55 | "create, update document, delete resource policy", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | yield* stack.destroy(); |
| 59 | |
| 60 | const created = yield* stack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | return yield* ResourcePolicy("Route53QueryLogging", { |
| 63 | policyName: POLICY_NAME, |
| 64 | policyDocument: route53Document("AlchemyTestV1"), |
| 65 | }); |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | expect(created.policyName).toBe(POLICY_NAME); |
| 70 | |
| 71 | // out-of-band verification via distilled |
| 72 | const observedCreated = yield* findPolicy(POLICY_NAME); |
| 73 | expect(observedCreated?.policyDocument).toContain("AlchemyTestV1"); |
| 74 | expect(observedCreated?.policyDocument).toContain("route53.amazonaws.com"); |
| 75 | |
| 76 | // update the document in place (same policy name) |
| 77 | const updated = yield* stack.deploy( |
| 78 | Effect.gen(function* () { |
| 79 | return yield* ResourcePolicy("Route53QueryLogging", { |
| 80 | policyName: POLICY_NAME, |
| 81 | policyDocument: route53Document("AlchemyTestV2"), |
| 82 | }); |
| 83 | }), |
| 84 | ); |
| 85 | expect(updated.policyName).toBe(POLICY_NAME); |
| 86 | |
| 87 | const observedUpdated = yield* findPolicy(POLICY_NAME); |
| 88 | expect(observedUpdated?.policyDocument).toContain("AlchemyTestV2"); |
| 89 | |
| 90 | yield* stack.destroy(); |
| 91 | yield* assertPolicyDeleted(POLICY_NAME); |
| 92 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 93 | { |
| 94 | tags: ["provider:aws", "provider:aws:iam", "provider:aws:logs", "live"], |
| 95 | timeout: 120_000, |
| 96 | }, |
| 97 | ); |