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 { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as incidents from "@distilled.cloud/aws/ssm-incidents"; |
| 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 Result from "effect/Result"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import IncidentsTestFunctionLive, { IncidentsTestFunction } from "./handler"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | |
| 16 | // Incident records are runtime entities: their ARNs embed the response-plan |
| 17 | // name plus a UUID minted by StartIncident. These well-formed-but-nonexistent |
| 18 | // ARNs exercise every binding operation's TYPED error union (or its |
| 19 | // empty-list/idempotent success) without needing Incident Manager onboarding |
| 20 | // — which CreateReplicationSet's deprecation (Nov 7, 2025) makes impossible |
| 21 | // for accounts that were not already onboarded. |
| 22 | const fakeArns = Effect.gen(function* () { |
| 23 | const { accountId } = yield* AWSEnvironment.current; |
| 24 | return { |
| 25 | planArn: `arn:aws:ssm-incidents::${accountId}:response-plan/alchemy-nonexistent-probe`, |
| 26 | recordArn: `arn:aws:ssm-incidents::${accountId}:incident-record/alchemy-nonexistent-probe/11111111-1111-1111-1111-111111111111`, |
| 27 | }; |
| 28 | }); |
| 29 | |
| 30 | // -- Ungated typed-error probes ----------------------------------------------- |
| 31 | // Prove that every operation the new bindings wrap either succeeds with an |
| 32 | // empty result or fails with the typed tag alchemy code relies on. These run |
| 33 | // in every CI pass at near-zero cost, unlike the gated lifecycle below. |
| 34 | |
| 35 | test.provider( |
| 36 | "startIncident on a nonexistent response plan fails with ResourceNotFoundException", |
| 37 | () => |
| 38 | Effect.gen(function* () { |
| 39 | const { planArn } = yield* fakeArns; |
| 40 | const error = yield* Effect.flip(incidents.startIncident({ responsePlanArn: planArn })); |
| 41 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 42 | }), |
| 43 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 44 | ); |
| 45 | |
| 46 | test.provider( |
| 47 | "listIncidentRecords answers with a summary list", |
| 48 | () => |
| 49 | Effect.gen(function* () { |
| 50 | const listed = yield* incidents.listIncidentRecords({}); |
| 51 | expect(Array.isArray(listed.incidentRecordSummaries)).toBe(true); |
| 52 | }), |
| 53 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 54 | ); |
| 55 | |
| 56 | test.provider( |
| 57 | "getIncidentRecord on a nonexistent record fails with ResourceNotFoundException", |
| 58 | () => |
| 59 | Effect.gen(function* () { |
| 60 | const { recordArn } = yield* fakeArns; |
| 61 | const error = yield* Effect.flip(incidents.getIncidentRecord({ arn: recordArn })); |
| 62 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 63 | }), |
| 64 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 65 | ); |
| 66 | |
| 67 | test.provider( |
| 68 | "updateIncidentRecord on a nonexistent record fails with ResourceNotFoundException", |
| 69 | () => |
| 70 | Effect.gen(function* () { |
| 71 | const { recordArn } = yield* fakeArns; |
| 72 | const error = yield* Effect.flip( |
| 73 | incidents.updateIncidentRecord({ arn: recordArn, title: "probe" }), |
| 74 | ); |
| 75 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 76 | }), |
| 77 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 78 | ); |
| 79 | |
| 80 | test.provider( |
| 81 | "deleteIncidentRecord on a nonexistent record is idempotent", |
| 82 | () => |
| 83 | Effect.gen(function* () { |
| 84 | const { recordArn } = yield* fakeArns; |
| 85 | const result = yield* Effect.result(incidents.deleteIncidentRecord({ arn: recordArn })); |
| 86 | expect(Result.isSuccess(result)).toBe(true); |
| 87 | }), |
| 88 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 89 | ); |
| 90 | |
| 91 | test.provider( |
| 92 | "createTimelineEvent on a nonexistent record fails with ResourceNotFoundException", |
| 93 | () => |
| 94 | Effect.gen(function* () { |
| 95 | const { recordArn } = yield* fakeArns; |
| 96 | const error = yield* Effect.flip( |
| 97 | incidents.createTimelineEvent({ |
| 98 | incidentRecordArn: recordArn, |
| 99 | eventTime: new Date(), |
| 100 | eventType: "Custom Event", |
| 101 | eventData: JSON.stringify({ note: "probe" }), |
| 102 | }), |
| 103 | ); |
| 104 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 105 | }), |
| 106 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 107 | ); |
| 108 | |
| 109 | test.provider( |
| 110 | "getTimelineEvent on a nonexistent record fails with ResourceNotFoundException", |
| 111 | () => |
| 112 | Effect.gen(function* () { |
| 113 | const { recordArn } = yield* fakeArns; |
| 114 | const error = yield* Effect.flip( |
| 115 | incidents.getTimelineEvent({ |
| 116 | incidentRecordArn: recordArn, |
| 117 | eventId: "11111111-1111-1111-1111-111111111111", |
| 118 | }), |
| 119 | ); |
| 120 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 121 | }), |
| 122 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 123 | ); |
| 124 | |
| 125 | test.provider( |
| 126 | "updateTimelineEvent on a nonexistent record fails with ResourceNotFoundException", |
| 127 | () => |
| 128 | Effect.gen(function* () { |
| 129 | const { recordArn } = yield* fakeArns; |
| 130 | const error = yield* Effect.flip( |
| 131 | incidents.updateTimelineEvent({ |
| 132 | incidentRecordArn: recordArn, |
| 133 | eventId: "11111111-1111-1111-1111-111111111111", |
| 134 | eventData: JSON.stringify({ note: "probe" }), |
| 135 | }), |
| 136 | ); |
| 137 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 138 | }), |
| 139 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 140 | ); |
| 141 | |
| 142 | test.provider( |
| 143 | "deleteTimelineEvent on a nonexistent record is idempotent", |
| 144 | () => |
| 145 | Effect.gen(function* () { |
| 146 | const { recordArn } = yield* fakeArns; |
| 147 | const result = yield* Effect.result( |
| 148 | incidents.deleteTimelineEvent({ |
| 149 | incidentRecordArn: recordArn, |
| 150 | eventId: "11111111-1111-1111-1111-111111111111", |
| 151 | }), |
| 152 | ); |
| 153 | expect(Result.isSuccess(result)).toBe(true); |
| 154 | }), |
| 155 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 156 | ); |
| 157 | |
| 158 | test.provider( |
| 159 | "listTimelineEvents on a nonexistent record returns an empty list", |
| 160 | () => |
| 161 | Effect.gen(function* () { |
| 162 | const { recordArn } = yield* fakeArns; |
| 163 | const listed = yield* incidents.listTimelineEvents({ incidentRecordArn: recordArn }); |
| 164 | expect(listed.eventSummaries).toHaveLength(0); |
| 165 | }), |
| 166 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 167 | ); |
| 168 | |
| 169 | test.provider( |
| 170 | "listRelatedItems on a nonexistent record returns an empty list", |
| 171 | () => |
| 172 | Effect.gen(function* () { |
| 173 | const { recordArn } = yield* fakeArns; |
| 174 | const listed = yield* incidents.listRelatedItems({ incidentRecordArn: recordArn }); |
| 175 | expect(listed.relatedItems).toHaveLength(0); |
| 176 | }), |
| 177 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 178 | ); |
| 179 | |
| 180 | test.provider( |
| 181 | "updateRelatedItems on a nonexistent record fails with ResourceNotFoundException", |
| 182 | () => |
| 183 | Effect.gen(function* () { |
| 184 | const { recordArn } = yield* fakeArns; |
| 185 | const error = yield* Effect.flip( |
| 186 | incidents.updateRelatedItems({ |
| 187 | incidentRecordArn: recordArn, |
| 188 | relatedItemsUpdate: { |
| 189 | itemToAdd: { |
| 190 | title: "probe", |
| 191 | identifier: { type: "OTHER", value: { url: "https://alchemy.run" } }, |
| 192 | }, |
| 193 | }, |
| 194 | }), |
| 195 | ); |
| 196 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 197 | }), |
| 198 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 199 | ); |
| 200 | |
| 201 | test.provider( |
| 202 | "listIncidentFindings on a nonexistent record fails with ResourceNotFoundException", |
| 203 | () => |
| 204 | Effect.gen(function* () { |
| 205 | const { recordArn } = yield* fakeArns; |
| 206 | const error = yield* Effect.flip( |
| 207 | incidents.listIncidentFindings({ incidentRecordArn: recordArn }), |
| 208 | ); |
| 209 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 210 | }), |
| 211 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 212 | ); |
| 213 | |
| 214 | test.provider( |
| 215 | "batchGetIncidentFindings on a nonexistent record fails with ResourceNotFoundException", |
| 216 | () => |
| 217 | Effect.gen(function* () { |
| 218 | const { recordArn } = yield* fakeArns; |
| 219 | const error = yield* Effect.flip( |
| 220 | incidents.batchGetIncidentFindings({ |
| 221 | incidentRecordArn: recordArn, |
| 222 | findingIds: ["11111111-1111-1111-1111-111111111111"], |
| 223 | }), |
| 224 | ); |
| 225 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 226 | }), |
| 227 | { tags: ["provider:aws", "provider:aws:ssmincidents", "live"] }, |
| 228 | ); |
| 229 | |
| 230 | // -- Gated end-to-end lifecycle ----------------------------------------------- |
| 231 | // Deploys the Lambda fixture (replication set + response plan + all 14 |
| 232 | // bindings) and drives the incident data plane over HTTP. Gated behind |
| 233 | // AWS_TEST_INCIDENT_MANAGER=1 AND a grandfathered account: AWS deprecated |
| 234 | // CreateReplicationSet on Nov 7, 2025, so accounts not already onboarded to |
| 235 | // Incident Manager can no longer onboard (this test then degrades to the |
| 236 | // typed deprecation probe). It also never touches a replication set it did |
| 237 | // not create (capture-and-restore safety). |
| 238 | |
| 239 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 240 | readonly status: number; |
| 241 | readonly body: string; |
| 242 | }> {} |
| 243 | |
| 244 | // The Lambda fixture occasionally answers a transient 5xx (cold re-init, IAM |
| 245 | // propagation on the freshly attached policy). Retry only 5xx; a genuine 4xx |
| 246 | // or assertion failure surfaces immediately. |
| 247 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 248 | HttpClient.execute(request).pipe( |
| 249 | Effect.flatMap((response) => |
| 250 | response.status >= 500 |
| 251 | ? response.text.pipe( |
| 252 | Effect.flatMap((body) => |
| 253 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 254 | ), |
| 255 | ) |
| 256 | : Effect.succeed(response), |
| 257 | ), |
| 258 | Effect.retry({ |
| 259 | while: (e): boolean => e._tag === "TransientUpstream", |
| 260 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 261 | }), |
| 262 | ); |
| 263 | |
| 264 | const json = (request: HttpClientRequest.HttpClientRequest) => |
| 265 | send(request).pipe(Effect.flatMap((r) => r.json)); |
| 266 | |
| 267 | test.provider.skipIf(!process.env.AWS_TEST_INCIDENT_MANAGER)( |
| 268 | "lifecycle: deployed Lambda drives the incident data plane end-to-end", |
| 269 | (stack) => |
| 270 | Effect.gen(function* () { |
| 271 | // Never take over a replication set this test did not create. |
| 272 | const preexisting = yield* incidents.listReplicationSets({}); |
| 273 | if (preexisting.replicationSetArns.length > 0) { |
| 274 | yield* Effect.logInfo( |
| 275 | `Incident Manager already onboarded (${preexisting.replicationSetArns[0]}) — skipping destructive bindings lifecycle`, |
| 276 | ); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | // Probe the deprecated onboarding path; only a grandfathered account |
| 281 | // can run the lifecycle below. |
| 282 | const { region } = yield* AWSEnvironment.current; |
| 283 | const onboarded = yield* Effect.result( |
| 284 | incidents.createReplicationSet({ regions: { [region]: {} } }), |
| 285 | ); |
| 286 | if (Result.isFailure(onboarded)) { |
| 287 | expect(onboarded.failure._tag).toBe("UnsupportedOperationException"); |
| 288 | yield* Effect.logInfo( |
| 289 | "CreateReplicationSet is deprecated and this account is not onboarded — skipping bindings lifecycle", |
| 290 | ); |
| 291 | return; |
| 292 | } |
| 293 | // The probe onboarded the account (untagged set) — remove it so the |
| 294 | // alchemy-managed deploy below starts from a clean slate. |
| 295 | const probeArn = onboarded.success.arn; |
| 296 | yield* incidents.getReplicationSet({ arn: probeArn }).pipe( |
| 297 | Effect.map((r) => r.replicationSet.status), |
| 298 | Effect.repeat({ |
| 299 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(60)]), |
| 300 | until: (status): boolean => status !== "CREATING" && status !== "UPDATING", |
| 301 | }), |
| 302 | ); |
| 303 | yield* incidents.deleteReplicationSet({ arn: probeArn }); |
| 304 | yield* incidents.getReplicationSet({ arn: probeArn }).pipe( |
| 305 | Effect.map((r) => r.replicationSet.status), |
| 306 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("GONE" as const)), |
| 307 | Effect.repeat({ |
| 308 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(60)]), |
| 309 | until: (status): boolean => status === "GONE", |
| 310 | }), |
| 311 | ); |
| 312 | |
| 313 | yield* stack.destroy(); |
| 314 | |
| 315 | // Phase 1 — onboard Incident Manager (the fixture's response plan |
| 316 | // needs an ACTIVE replication set). |
| 317 | yield* stack.deploy( |
| 318 | Effect.gen(function* () { |
| 319 | return yield* AWS.SSMIncidents.ReplicationSet("Incidents", {}); |
| 320 | }), |
| 321 | ); |
| 322 | |
| 323 | // Phase 2 — deploy the fixture Lambda + response plan + bindings. |
| 324 | const attrs = yield* stack.deploy( |
| 325 | Effect.gen(function* () { |
| 326 | return yield* IncidentsTestFunction; |
| 327 | }).pipe(Effect.provide(IncidentsTestFunctionLive)), |
| 328 | ); |
| 329 | expect(attrs.functionUrl).toBeTruthy(); |
| 330 | const baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 331 | |
| 332 | // Readiness: fresh function URLs take a while to start serving 200s. |
| 333 | yield* HttpClient.get(`${baseUrl}/bindings`).pipe( |
| 334 | Effect.flatMap((response) => |
| 335 | response.status === 200 |
| 336 | ? Effect.succeed(response) |
| 337 | : Effect.fail(new Error(`not ready: ${response.status}`)), |
| 338 | ), |
| 339 | Effect.retry({ |
| 340 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]), |
| 341 | }), |
| 342 | ); |
| 343 | |
| 344 | const bindings = (yield* json(HttpClientRequest.get(`${baseUrl}/bindings`))) as { |
| 345 | bound: string[]; |
| 346 | }; |
| 347 | expect(bindings.bound).toHaveLength(14); |
| 348 | |
| 349 | // StartIncident |
| 350 | const started = (yield* json(HttpClientRequest.post(`${baseUrl}/start`))) as { |
| 351 | incidentRecordArn: string; |
| 352 | }; |
| 353 | expect(started.incidentRecordArn).toContain(":incident-record/"); |
| 354 | const arn = encodeURIComponent(started.incidentRecordArn); |
| 355 | |
| 356 | // ListIncidentRecords — poll until the new record is listed. |
| 357 | yield* json(HttpClientRequest.get(`${baseUrl}/records`)).pipe( |
| 358 | Effect.repeat({ |
| 359 | schedule: Schedule.spaced("3 seconds"), |
| 360 | until: (r): boolean => (r as { arns: string[] }).arns.includes(started.incidentRecordArn), |
| 361 | times: 20, |
| 362 | }), |
| 363 | ); |
| 364 | |
| 365 | // GetIncidentRecord |
| 366 | const record = (yield* json(HttpClientRequest.get(`${baseUrl}/record?arn=${arn}`))) as { |
| 367 | title: string; |
| 368 | status: string; |
| 369 | }; |
| 370 | expect(record.title).toBe("alchemy bindings test incident"); |
| 371 | expect(record.status).toBe("OPEN"); |
| 372 | |
| 373 | // CreateTimelineEvent / GetTimelineEvent / UpdateTimelineEvent / |
| 374 | // ListTimelineEvents |
| 375 | const created = (yield* json(HttpClientRequest.post(`${baseUrl}/timeline?arn=${arn}`))) as { |
| 376 | eventId: string; |
| 377 | }; |
| 378 | expect(created.eventId).toBeTruthy(); |
| 379 | const eventId = encodeURIComponent(created.eventId); |
| 380 | const event = (yield* json( |
| 381 | HttpClientRequest.get(`${baseUrl}/timeline?arn=${arn}&eventId=${eventId}`), |
| 382 | )) as { eventType: string }; |
| 383 | expect(event.eventType).toBe("Custom Event"); |
| 384 | yield* json( |
| 385 | HttpClientRequest.post(`${baseUrl}/timeline-update?arn=${arn}&eventId=${eventId}`), |
| 386 | ); |
| 387 | const events = (yield* json( |
| 388 | HttpClientRequest.get(`${baseUrl}/timeline-events?arn=${arn}`), |
| 389 | )) as { count: number }; |
| 390 | expect(events.count).toBeGreaterThanOrEqual(1); |
| 391 | |
| 392 | // Related items + findings |
| 393 | yield* json(HttpClientRequest.post(`${baseUrl}/related?arn=${arn}`)); |
| 394 | const related = (yield* json(HttpClientRequest.get(`${baseUrl}/related?arn=${arn}`))) as { |
| 395 | count: number; |
| 396 | }; |
| 397 | expect(related.count).toBeGreaterThanOrEqual(1); |
| 398 | const findings = (yield* json(HttpClientRequest.get(`${baseUrl}/findings?arn=${arn}`))) as { |
| 399 | count: number; |
| 400 | }; |
| 401 | expect(findings.count).toBeGreaterThanOrEqual(0); |
| 402 | |
| 403 | // Resolve, clean up the timeline event and the record. |
| 404 | yield* json(HttpClientRequest.post(`${baseUrl}/resolve?arn=${arn}`)); |
| 405 | yield* json(HttpClientRequest.delete(`${baseUrl}/timeline?arn=${arn}&eventId=${eventId}`)); |
| 406 | yield* json(HttpClientRequest.delete(`${baseUrl}/record?arn=${arn}`)); |
| 407 | |
| 408 | // Destroy — response plan and Lambda first, then offboarding. |
| 409 | yield* stack.destroy(); |
| 410 | const after = yield* incidents.listReplicationSets({}); |
| 411 | expect(after.replicationSetArns).toHaveLength(0); |
| 412 | }), |
| 413 | // onboarding (~1-2 min) + Lambda deploy + offboarding (~1-2 min). |
| 414 | { |
| 415 | tags: [ |
| 416 | "provider:aws", |
| 417 | "provider:aws:batch", |
| 418 | "provider:aws:lambda", |
| 419 | "provider:aws:ssmincidents", |
| 420 | "live", |
| 421 | ], |
| 422 | timeout: 900_000, |
| 423 | }, |
| 424 | ); |