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 { AnomalyMonitor } from "@alchemy.run/aws/CostExplorer/AnomalyMonitor"; |
| 3 | import { AnomalySubscription } from "@alchemy.run/aws/CostExplorer/AnomalySubscription"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as ce from "@distilled.cloud/aws/cost-explorer"; |
| 7 | import { Region as AwsRegion } from "@distilled.cloud/aws/Region"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | // Cost Explorer is served exclusively from us-east-1. |
| 14 | const pin = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 15 | effect.pipe(Effect.provideService(AwsRegion, Effect.succeed("us-east-1"))); |
| 16 | |
| 17 | const subscriptionName = "alchemy-test-anomaly-subscription"; |
| 18 | |
| 19 | const getSubscription = (subscriptionArn: string) => |
| 20 | pin(ce.getAnomalySubscriptions({ SubscriptionArnList: [subscriptionArn] })).pipe( |
| 21 | Effect.map((r) => r.AnomalySubscriptions[0]), |
| 22 | Effect.catchTag("UnknownSubscriptionException", () => Effect.succeed(undefined)), |
| 23 | ); |
| 24 | |
| 25 | // Typed wait-until-gone on delete. |
| 26 | const assertSubscriptionGone = (subscriptionArn: string) => |
| 27 | Effect.gen(function* () { |
| 28 | const found = yield* getSubscription(subscriptionArn); |
| 29 | if (found !== undefined) { |
| 30 | return yield* Effect.fail(new Error(`subscription '${subscriptionArn}' still exists`)); |
| 31 | } |
| 32 | }).pipe( |
| 33 | Effect.retry({ |
| 34 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(5)]), |
| 35 | }), |
| 36 | ); |
| 37 | |
| 38 | const makeStack = (frequency: "DAILY" | "WEEKLY", impactThreshold: string) => |
| 39 | Effect.gen(function* () { |
| 40 | const monitor = yield* AnomalyMonitor("SubMonitor", { |
| 41 | monitorName: "alchemy-test-subscription-monitor", |
| 42 | monitorType: "CUSTOM", |
| 43 | monitorSpecification: { |
| 44 | Tags: { Key: "CostCenter", Values: ["alchemy-test-sub"] }, |
| 45 | }, |
| 46 | }); |
| 47 | const subscription = yield* AnomalySubscription("Subscription", { |
| 48 | subscriptionName, |
| 49 | monitorArnList: [monitor.monitorArn], |
| 50 | frequency, |
| 51 | subscribers: [{ type: "EMAIL", address: "anomaly-test@example.com" }], |
| 52 | thresholdExpression: { |
| 53 | Dimensions: { |
| 54 | Key: "ANOMALY_TOTAL_IMPACT_ABSOLUTE", |
| 55 | MatchOptions: ["GREATER_THAN_OR_EQUAL"], |
| 56 | Values: [impactThreshold], |
| 57 | }, |
| 58 | }, |
| 59 | tags: { fixture: "cost-explorer-anomaly-subscription" }, |
| 60 | }); |
| 61 | return { |
| 62 | monitorArn: monitor.monitorArn, |
| 63 | subscriptionArn: subscription.subscriptionArn, |
| 64 | }; |
| 65 | }); |
| 66 | |
| 67 | test.provider( |
| 68 | "lifecycle: create subscription on a monitor, update frequency + threshold, destroy", |
| 69 | (stack) => |
| 70 | Effect.gen(function* () { |
| 71 | yield* stack.destroy(); |
| 72 | |
| 73 | const deployed = yield* stack.deploy(makeStack("DAILY", "100")); |
| 74 | |
| 75 | // Out-of-band verification via distilled. |
| 76 | const created = yield* getSubscription(deployed.subscriptionArn); |
| 77 | expect(created?.SubscriptionName).toBe(subscriptionName); |
| 78 | expect(created?.Frequency).toBe("DAILY"); |
| 79 | expect(created?.MonitorArnList).toEqual([deployed.monitorArn]); |
| 80 | expect(created?.Subscribers[0]?.Address).toBe("anomaly-test@example.com"); |
| 81 | expect(created?.Subscribers[0]?.Type).toBe("EMAIL"); |
| 82 | |
| 83 | // Update — frequency and threshold expression are mutable in place. |
| 84 | const updated = yield* stack.deploy(makeStack("WEEKLY", "250")); |
| 85 | expect(updated.subscriptionArn).toBe(deployed.subscriptionArn); |
| 86 | const afterUpdate = yield* getSubscription(deployed.subscriptionArn); |
| 87 | expect(afterUpdate?.Frequency).toBe("WEEKLY"); |
| 88 | expect(afterUpdate?.ThresholdExpression?.Dimensions?.Values).toEqual(["250"]); |
| 89 | |
| 90 | // Destroy — subscription and monitor are gone. |
| 91 | yield* stack.destroy(); |
| 92 | yield* assertSubscriptionGone(deployed.subscriptionArn); |
| 93 | }), |
| 94 | { |
| 95 | tags: ["provider:aws", "provider:aws:costexplorer", "live"], |
| 96 | timeout: 120_000, |
| 97 | }, |
| 98 | ); |