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 { ResourcePolicy } from "@alchemy.run/aws/XRay"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as xray from "@distilled.cloud/aws/xray"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | const findPolicy = (policyName: string) => |
| 15 | xray.listResourcePolicies.items({}).pipe( |
| 16 | Stream.filter((policy) => policy.PolicyName === policyName), |
| 17 | Stream.runHead, |
| 18 | Effect.map(Option.getOrUndefined), |
| 19 | ); |
| 20 | |
| 21 | class PolicyStillExists extends Data.TaggedError("PolicyStillExists")<{ |
| 22 | readonly policyName: string; |
| 23 | }> {} |
| 24 | |
| 25 | const assertPolicyDeleted = (policyName: string) => |
| 26 | findPolicy(policyName).pipe( |
| 27 | Effect.flatMap((policy) => |
| 28 | policy === undefined ? Effect.void : Effect.fail(new PolicyStillExists({ policyName })), |
| 29 | ), |
| 30 | Effect.retry({ |
| 31 | while: (e) => e._tag === "PolicyStillExists", |
| 32 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | const policyDocument = (actions: string[]) => |
| 37 | JSON.stringify({ |
| 38 | Version: "2012-10-17", |
| 39 | Statement: [ |
| 40 | { |
| 41 | Effect: "Allow", |
| 42 | Principal: { Service: "sns.amazonaws.com" }, |
| 43 | Action: actions, |
| 44 | Resource: "*", |
| 45 | }, |
| 46 | ], |
| 47 | }); |
| 48 | |
| 49 | test.provider( |
| 50 | "create, update document, delete resource policy", |
| 51 | (stack) => |
| 52 | Effect.gen(function* () { |
| 53 | yield* stack.destroy(); |
| 54 | |
| 55 | const created = yield* stack.deploy( |
| 56 | Effect.gen(function* () { |
| 57 | return yield* ResourcePolicy("TestResourcePolicy", { |
| 58 | policyDocument: policyDocument(["xray:PutTraceSegments"]), |
| 59 | }); |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | expect(created.policyName).toBeDefined(); |
| 64 | expect(created.policyRevisionId).toBeDefined(); |
| 65 | |
| 66 | // out-of-band verification via distilled |
| 67 | const live = yield* findPolicy(created.policyName); |
| 68 | expect(live?.PolicyDocument).toContain("xray:PutTraceSegments"); |
| 69 | |
| 70 | // update the document in place (same name, new statement) |
| 71 | const updated = yield* stack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | return yield* ResourcePolicy("TestResourcePolicy", { |
| 74 | policyDocument: policyDocument(["xray:PutTraceSegments", "xray:GetSamplingRules"]), |
| 75 | }); |
| 76 | }), |
| 77 | ); |
| 78 | expect(updated.policyName).toBe(created.policyName); |
| 79 | expect(updated.policyRevisionId).not.toBe(created.policyRevisionId); |
| 80 | |
| 81 | const liveUpdated = yield* findPolicy(created.policyName); |
| 82 | expect(liveUpdated?.PolicyDocument).toContain("xray:GetSamplingRules"); |
| 83 | |
| 84 | // re-deploy with the same document — a no-op that keeps the revision |
| 85 | const noop = yield* stack.deploy( |
| 86 | Effect.gen(function* () { |
| 87 | return yield* ResourcePolicy("TestResourcePolicy", { |
| 88 | policyDocument: policyDocument(["xray:PutTraceSegments", "xray:GetSamplingRules"]), |
| 89 | }); |
| 90 | }), |
| 91 | ); |
| 92 | expect(noop.policyRevisionId).toBe(updated.policyRevisionId); |
| 93 | |
| 94 | yield* stack.destroy(); |
| 95 | yield* assertPolicyDeleted(created.policyName); |
| 96 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.catchCause(() => Effect.void)))), |
| 97 | { tags: ["provider:aws", "provider:aws:xray", "live"], timeout: 120_000 }, |
| 98 | ); |