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 { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 3 | import { ReplicationSet } from "@alchemy.run/aws/SSMIncidents/ReplicationSet"; |
| 4 | import { ResponsePlan } from "@alchemy.run/aws/SSMIncidents/ResponsePlan"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as incidents from "@distilled.cloud/aws/ssm-incidents"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Result from "effect/Result"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | // Ungated typed-error probes: prove the distilled error union carries the |
| 16 | // tags this provider's read/delete paths depend on. Run in every CI pass at |
| 17 | // near-zero cost, unlike the gated lifecycle below. |
| 18 | test.provider( |
| 19 | "listReplicationSets succeeds whether or not Incident Manager is onboarded", |
| 20 | () => |
| 21 | Effect.gen(function* () { |
| 22 | const listed = yield* incidents.listReplicationSets({}); |
| 23 | expect(Array.isArray(listed.replicationSetArns)).toBe(true); |
| 24 | }), |
| 25 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getReplicationSet on a nonexistent arn fails with ResourceNotFoundException", |
| 30 | () => |
| 31 | Effect.gen(function* () { |
| 32 | const { accountId } = yield* AWSEnvironment.current; |
| 33 | const error = yield* Effect.flip( |
| 34 | incidents.getReplicationSet({ |
| 35 | arn: `arn:aws:ssm-incidents::${accountId}:replication-set/00000000-0000-4000-8000-000000000000`, |
| 36 | }), |
| 37 | ); |
| 38 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 39 | }), |
| 40 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 41 | ); |
| 42 | |
| 43 | test.provider( |
| 44 | "getResponsePlan on a nonexistent arn fails with ResourceNotFoundException", |
| 45 | () => |
| 46 | Effect.gen(function* () { |
| 47 | const { accountId } = yield* AWSEnvironment.current; |
| 48 | // Response plan ARNs are region-less (they live in the replication set). |
| 49 | const error = yield* Effect.flip( |
| 50 | incidents.getResponsePlan({ |
| 51 | arn: `arn:aws:ssm-incidents::${accountId}:response-plan/alchemy-nonexistent-probe`, |
| 52 | }), |
| 53 | ); |
| 54 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 55 | }), |
| 56 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 57 | ); |
| 58 | |
| 59 | // The replication set is the account/region singleton that ONBOARDS Incident |
| 60 | // Manager (create) and OFFBOARDS it account-wide (delete, ~1-2 minutes). The |
| 61 | // destructive lifecycle is gated behind AWS_TEST_INCIDENT_MANAGER=1 and only |
| 62 | // runs when the account is not already onboarded — it must never delete a |
| 63 | // replication set the user already operates (capture-and-restore safety). |
| 64 | test.provider.skipIf(!process.env.AWS_TEST_INCIDENT_MANAGER)( |
| 65 | "lifecycle: onboard Incident Manager, deploy response plan, offboard", |
| 66 | (stack) => |
| 67 | Effect.gen(function* () { |
| 68 | const preexisting = yield* incidents.listReplicationSets({}); |
| 69 | if (preexisting.replicationSetArns.length > 0) { |
| 70 | yield* Effect.logInfo( |
| 71 | `Incident Manager already onboarded (${preexisting.replicationSetArns[0]}) — skipping destructive lifecycle test`, |
| 72 | ); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // AWS deprecated CreateReplicationSet on Nov 7, 2025 — accounts not |
| 77 | // already onboarded to Incident Manager can no longer onboard. Probe |
| 78 | // once and assert the typed deprecation tag; only a grandfathered |
| 79 | // (pre-deprecation) account can exercise the full lifecycle below. |
| 80 | const { region } = yield* AWSEnvironment.current; |
| 81 | const onboarded = yield* Effect.result( |
| 82 | incidents.createReplicationSet({ regions: { [region]: {} } }), |
| 83 | ); |
| 84 | if (Result.isFailure(onboarded)) { |
| 85 | expect(onboarded.failure._tag).toBe("UnsupportedOperationException"); |
| 86 | yield* Effect.logInfo( |
| 87 | "CreateReplicationSet is deprecated (Nov 7, 2025) and this account is not onboarded to Incident Manager — skipping lifecycle", |
| 88 | ); |
| 89 | return; |
| 90 | } |
| 91 | // The probe unexpectedly onboarded the account (untagged set) — remove |
| 92 | // it so the alchemy-managed lifecycle below starts from a clean slate. |
| 93 | yield* incidents.getReplicationSet({ arn: onboarded.success.arn }).pipe( |
| 94 | Effect.map((r) => r.replicationSet.status), |
| 95 | Effect.repeat({ |
| 96 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(60)]), |
| 97 | until: (status) => status !== "CREATING" && status !== "UPDATING", |
| 98 | }), |
| 99 | ); |
| 100 | yield* incidents.deleteReplicationSet({ arn: onboarded.success.arn }); |
| 101 | yield* incidents.getReplicationSet({ arn: onboarded.success.arn }).pipe( |
| 102 | Effect.map((r) => r.replicationSet.status), |
| 103 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("GONE" as const)), |
| 104 | Effect.repeat({ |
| 105 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(60)]), |
| 106 | until: (status) => status === "GONE", |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | yield* stack.destroy(); |
| 111 | |
| 112 | // Create — onboard Incident Manager in the ambient region. |
| 113 | const created = yield* stack.deploy( |
| 114 | Effect.gen(function* () { |
| 115 | return yield* ReplicationSet("Incidents", { tags: { env: "test" } }); |
| 116 | }), |
| 117 | ); |
| 118 | expect(created.arn).toContain(":replication-set/"); |
| 119 | expect(created.status).toBe("ACTIVE"); |
| 120 | expect(created.regionNames).toContain(region); |
| 121 | expect(created.deletionProtected).toBe(false); |
| 122 | |
| 123 | // Out-of-band verification via distilled. |
| 124 | const live = yield* incidents.getReplicationSet({ arn: created.arn }); |
| 125 | expect(live.replicationSet.status).toBe("ACTIVE"); |
| 126 | const tags = yield* incidents.listTagsForResource({ resourceArn: created.arn }); |
| 127 | expect(tags.tags["alchemy::id"]).toBe("Incidents"); |
| 128 | expect(tags.tags["env"]).toBe("test"); |
| 129 | |
| 130 | // Canonical list() coverage. |
| 131 | const provider = yield* Provider.findProvider(ReplicationSet); |
| 132 | const all = yield* provider.list(); |
| 133 | expect(all.some((rs) => rs.arn === created.arn)).toBe(true); |
| 134 | |
| 135 | // Update — enable deletion protection + retag, and add a response |
| 136 | // plan now that the account is onboarded. |
| 137 | const planName = "alchemy-test-response-plan"; |
| 138 | const { plan } = yield* stack.deploy( |
| 139 | Effect.gen(function* () { |
| 140 | const replicationSet = yield* ReplicationSet("Incidents", { |
| 141 | deletionProtected: true, |
| 142 | tags: { env: "prod" }, |
| 143 | }); |
| 144 | const plan = yield* ResponsePlan("Critical", { |
| 145 | name: planName, |
| 146 | displayName: "Critical incidents", |
| 147 | incidentTemplate: { |
| 148 | title: "Critical failure", |
| 149 | impact: 3, |
| 150 | summary: "Automated test response plan", |
| 151 | }, |
| 152 | tags: { fixture: "ssm-incidents" }, |
| 153 | }); |
| 154 | return { replicationSet, plan }; |
| 155 | }), |
| 156 | ); |
| 157 | expect(plan.arn).toContain(":response-plan/"); |
| 158 | expect(plan.name).toBe(planName); |
| 159 | |
| 160 | const afterUpdate = yield* incidents.getReplicationSet({ arn: created.arn }); |
| 161 | expect(afterUpdate.replicationSet.deletionProtected).toBe(true); |
| 162 | const updatedTags = yield* incidents.listTagsForResource({ resourceArn: created.arn }); |
| 163 | expect(updatedTags.tags["env"]).toBe("prod"); |
| 164 | |
| 165 | const livePlan = yield* incidents.getResponsePlan({ arn: plan.arn }); |
| 166 | expect(livePlan.name).toBe(planName); |
| 167 | expect(livePlan.displayName).toBe("Critical incidents"); |
| 168 | expect(livePlan.incidentTemplate.title).toBe("Critical failure"); |
| 169 | expect(livePlan.incidentTemplate.impact).toBe(3); |
| 170 | |
| 171 | // Update the response plan in place. |
| 172 | yield* stack.deploy( |
| 173 | Effect.gen(function* () { |
| 174 | const replicationSet = yield* ReplicationSet("Incidents", { |
| 175 | deletionProtected: true, |
| 176 | tags: { env: "prod" }, |
| 177 | }); |
| 178 | const plan = yield* ResponsePlan("Critical", { |
| 179 | name: planName, |
| 180 | displayName: "Critical incidents (updated)", |
| 181 | incidentTemplate: { |
| 182 | title: "Critical failure", |
| 183 | impact: 2, |
| 184 | summary: "Automated test response plan (updated)", |
| 185 | }, |
| 186 | tags: { fixture: "ssm-incidents" }, |
| 187 | }); |
| 188 | return { replicationSet, plan }; |
| 189 | }), |
| 190 | ); |
| 191 | const updatedPlan = yield* incidents.getResponsePlan({ arn: plan.arn }); |
| 192 | expect(updatedPlan.displayName).toBe("Critical incidents (updated)"); |
| 193 | expect(updatedPlan.incidentTemplate.impact).toBe(2); |
| 194 | |
| 195 | // Destroy — the response plan is deleted first, deletion protection is |
| 196 | // lifted automatically, and Incident Manager is offboarded. The |
| 197 | // provider's delete waits until the set is fully gone. |
| 198 | yield* stack.destroy(); |
| 199 | const planError = yield* Effect.flip(incidents.getResponsePlan({ arn: plan.arn })); |
| 200 | expect(planError._tag).toBe("ResourceNotFoundException"); |
| 201 | const after = yield* incidents.listReplicationSets({}); |
| 202 | expect(after.replicationSetArns).toHaveLength(0); |
| 203 | }), |
| 204 | // onboarding (~1-2 min) + updates + offboarding (~1-2 min). |
| 205 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"], timeout: 900_000 }, |
| 206 | ); |