| 1 | import * as AWS from "@/AWS"; |
| 2 | import { Alarm, CompositeAlarm } from "@/AWS/CloudWatch"; |
| 3 | import * as Provider from "@/Provider"; |
| 4 | import * as Test from "@/Test/Alchemy"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as cloudwatch from "@distilled.cloud/aws/cloudwatch"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Output from "@/Output"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: AWS.providers() }); |
| 11 | |
| 12 | // list() enumerates every CompositeAlarm in the account/region via |
| 13 | // `describeAlarms` (AlarmTypes filtered to CompositeAlarm). We deploy a real |
| 14 | // composite alarm (referencing a real metric alarm so the rule validates), |
| 15 | // resolve the provider from context via the typed `findProvider`, call |
| 16 | // `list()`, and assert the deployed alarm appears in the exhaustively |
| 17 | // paginated result. |
| 18 | test.provider( |
| 19 | "list enumerates the deployed composite alarm", |
| 20 | (stack) => |
| 21 | Effect.gen(function* () { |
| 22 | yield* stack.destroy(); |
| 23 | |
| 24 | const deployed = yield* stack.deploy( |
| 25 | Effect.gen(function* () { |
| 26 | const metric = yield* Alarm("ListMetricAlarm", { |
| 27 | name: "alchemy-test-composite-list-metric", |
| 28 | MetricName: "Errors", |
| 29 | Namespace: "AWS/Lambda", |
| 30 | Statistic: "Sum", |
| 31 | Period: 60, |
| 32 | EvaluationPeriods: 1, |
| 33 | Threshold: 1, |
| 34 | ComparisonOperator: "GreaterThanOrEqualToThreshold", |
| 35 | }); |
| 36 | const composite = yield* CompositeAlarm("ListCompositeAlarm", { |
| 37 | name: "alchemy-test-composite-list", |
| 38 | AlarmRule: Output.interpolate`ALARM("${metric.alarmName}")`, |
| 39 | }); |
| 40 | return composite; |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | const provider = yield* Provider.findProvider(CompositeAlarm); |
| 45 | const all = yield* provider.list(); |
| 46 | |
| 47 | expect(all.some((a) => a.alarmName === deployed.alarmName)).toBe(true); |
| 48 | |
| 49 | yield* stack.destroy(); |
| 50 | |
| 51 | // Out-of-band assert-gone: both the composite alarm and its member |
| 52 | // metric alarm are deleted after the final destroy. |
| 53 | const gone = yield* cloudwatch.describeAlarms({ |
| 54 | AlarmNames: [ |
| 55 | "alchemy-test-composite-list", |
| 56 | "alchemy-test-composite-list-metric", |
| 57 | ], |
| 58 | AlarmTypes: ["CompositeAlarm", "MetricAlarm"], |
| 59 | }); |
| 60 | expect(gone.CompositeAlarms ?? []).toEqual([]); |
| 61 | expect(gone.MetricAlarms ?? []).toEqual([]); |
| 62 | }), |
| 63 | { tags: ["provider:aws", "provider:aws:cloudwatch", "live"] }, |
| 64 | ); |