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 { InvestigationGroup } from "@alchemy.run/aws/AIOps"; |
| 3 | import type { PolicyStatement } from "@alchemy.run/aws/IAM/Policy"; |
| 4 | import { Role } from "@alchemy.run/aws/IAM/Role"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as aiops from "@distilled.cloud/aws/aiops"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import type * as Duration from "effect/Duration"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | // Ungated typed-error probe: prove the distilled error union carries the |
| 16 | // not-found tag this provider's read/delete paths depend on. Runs in every |
| 17 | // CI pass at near-zero cost, unlike the gated lifecycle below. |
| 18 | test.provider( |
| 19 | "getInvestigationGroup on a nonexistent group fails with ResourceNotFoundException", |
| 20 | () => |
| 21 | Effect.gen(function* () { |
| 22 | const error = yield* Effect.flip( |
| 23 | aiops.getInvestigationGroup({ |
| 24 | identifier: "nonexistent0000000000", |
| 25 | }), |
| 26 | ); |
| 27 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 28 | }), |
| 29 | { tags: ["provider:aws", "provider:aws:aiops", "live"] }, |
| 30 | ); |
| 31 | |
| 32 | const findGroup = (arn: string) => |
| 33 | aiops |
| 34 | .getInvestigationGroup({ identifier: arn }) |
| 35 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 36 | |
| 37 | class GroupStillExists extends Data.TaggedError("GroupStillExists")<{ |
| 38 | readonly arn: string; |
| 39 | }> {} |
| 40 | |
| 41 | const assertGroupDeleted = (arn: string) => |
| 42 | findGroup(arn).pipe( |
| 43 | Effect.flatMap((group) => |
| 44 | group === undefined ? Effect.void : Effect.fail(new GroupStillExists({ arn })), |
| 45 | ), |
| 46 | Effect.retry({ |
| 47 | while: (e) => e._tag === "GroupStillExists", |
| 48 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 49 | }), |
| 50 | ); |
| 51 | |
| 52 | // An account can hold only ONE investigation group per Region, so the live |
| 53 | // lifecycle would collide with any pre-existing group (or a concurrent CI |
| 54 | // run). Gated behind AWS_TEST_AIOPS=1; always destroys what it created. |
| 55 | test.provider.skipIf(!process.env.AWS_TEST_AIOPS)( |
| 56 | "create, update, replace (delete-first), delete investigation group", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const deployGroup = (props: { |
| 62 | retention: Duration.Input; |
| 63 | tagKeyBoundaries: string[]; |
| 64 | isCloudTrailEventHistoryEnabled?: boolean; |
| 65 | policy?: PolicyStatement[]; |
| 66 | tags: Record<string, string>; |
| 67 | }) => |
| 68 | stack.deploy( |
| 69 | Effect.gen(function* () { |
| 70 | const role = yield* Role("AIOpsRole", { |
| 71 | assumeRolePolicyDocument: { |
| 72 | Version: "2012-10-17", |
| 73 | Statement: [ |
| 74 | { |
| 75 | Effect: "Allow", |
| 76 | Principal: { Service: "aiops.amazonaws.com" }, |
| 77 | Action: ["sts:AssumeRole"], |
| 78 | }, |
| 79 | ], |
| 80 | }, |
| 81 | managedPolicyArns: ["arn:aws:iam::aws:policy/AIOpsAssistantPolicy"], |
| 82 | }); |
| 83 | const group = yield* InvestigationGroup("Investigations", { |
| 84 | roleArn: role.roleArn, |
| 85 | ...props, |
| 86 | }); |
| 87 | return { group }; |
| 88 | }), |
| 89 | ); |
| 90 | |
| 91 | const { group } = yield* deployGroup({ |
| 92 | retention: "7 days", |
| 93 | tagKeyBoundaries: ["Application"], |
| 94 | tags: { Environment: "test" }, |
| 95 | }); |
| 96 | |
| 97 | expect(group.name).toBeDefined(); |
| 98 | expect(group.arn).toContain(":investigation-group/"); |
| 99 | expect(group.retentionInDays).toBe(7); |
| 100 | |
| 101 | // Out-of-band verification via distilled. |
| 102 | const created = yield* findGroup(group.arn); |
| 103 | expect(created?.name).toBe(group.name); |
| 104 | expect(created?.retentionInDays).toBe(7); |
| 105 | expect(created?.tagKeyBoundaries).toEqual(["Application"]); |
| 106 | const tags = yield* aiops |
| 107 | .listTagsForResource({ resourceArn: group.arn }) |
| 108 | .pipe(Effect.map((r) => r.tags ?? {})); |
| 109 | expect(tags.Environment).toBe("test"); |
| 110 | expect(tags["alchemy::id"]).toBe("Investigations"); |
| 111 | |
| 112 | // Update mutable aspects in place: tag key boundaries, CloudTrail |
| 113 | // event history, resource policy, and tags. |
| 114 | const alarmPolicyStatement: PolicyStatement = { |
| 115 | Effect: "Allow", |
| 116 | Principal: { Service: "aiops.alarms.cloudwatch.amazonaws.com" }, |
| 117 | Action: ["aiops:CreateInvestigation", "aiops:CreateInvestigationEvent"], |
| 118 | Resource: "*", |
| 119 | }; |
| 120 | const { group: updated } = yield* deployGroup({ |
| 121 | retention: "7 days", |
| 122 | tagKeyBoundaries: ["Application", "Service"], |
| 123 | isCloudTrailEventHistoryEnabled: false, |
| 124 | policy: [alarmPolicyStatement], |
| 125 | tags: { Environment: "test", Team: "obs" }, |
| 126 | }); |
| 127 | expect(updated.arn).toBe(group.arn); |
| 128 | |
| 129 | const afterUpdate = yield* findGroup(group.arn); |
| 130 | expect(afterUpdate?.tagKeyBoundaries).toEqual(["Application", "Service"]); |
| 131 | expect(afterUpdate?.isCloudTrailEventHistoryEnabled).toBe(false); |
| 132 | const updatedTags = yield* aiops |
| 133 | .listTagsForResource({ resourceArn: group.arn }) |
| 134 | .pipe(Effect.map((r) => r.tags ?? {})); |
| 135 | expect(updatedTags.Team).toBe("obs"); |
| 136 | |
| 137 | // Out-of-band: the resource policy landed on the group. |
| 138 | const policyAfterUpdate = yield* aiops.getInvestigationGroupPolicy({ |
| 139 | identifier: group.arn, |
| 140 | }); |
| 141 | expect(JSON.parse(policyAfterUpdate.policy ?? "{}").Statement).toMatchObject([ |
| 142 | alarmPolicyStatement, |
| 143 | ]); |
| 144 | |
| 145 | // `policy: []` deletes the attached resource policy. |
| 146 | yield* deployGroup({ |
| 147 | retention: "7 days", |
| 148 | tagKeyBoundaries: ["Application", "Service"], |
| 149 | isCloudTrailEventHistoryEnabled: false, |
| 150 | policy: [], |
| 151 | tags: { Environment: "test", Team: "obs" }, |
| 152 | }); |
| 153 | const policyAfterDelete = yield* aiops |
| 154 | .getInvestigationGroupPolicy({ identifier: group.arn }) |
| 155 | .pipe( |
| 156 | Effect.map((r) => r.policy), |
| 157 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)), |
| 158 | ); |
| 159 | expect(policyAfterDelete).toBeUndefined(); |
| 160 | |
| 161 | // The retention period has no update API — changing it replaces the |
| 162 | // group (delete-first, because only one group may exist per Region). |
| 163 | const { group: replaced } = yield* deployGroup({ |
| 164 | retention: "14 days", |
| 165 | tagKeyBoundaries: ["Application", "Service"], |
| 166 | isCloudTrailEventHistoryEnabled: false, |
| 167 | tags: { Environment: "test", Team: "obs" }, |
| 168 | }); |
| 169 | expect(replaced.arn).not.toBe(group.arn); |
| 170 | expect(replaced.retentionInDays).toBe(14); |
| 171 | const afterReplace = yield* findGroup(replaced.arn); |
| 172 | expect(afterReplace?.retentionInDays).toBe(14); |
| 173 | yield* assertGroupDeleted(group.arn); |
| 174 | |
| 175 | yield* stack.destroy(); |
| 176 | yield* assertGroupDeleted(replaced.arn); |
| 177 | }), |
| 178 | { |
| 179 | tags: ["provider:aws", "provider:aws:aiops", "provider:aws:iam", "live"], |
| 180 | timeout: 600_000, |
| 181 | }, |
| 182 | ); |