| 1 | import * as AWS from "@/AWS"; |
| 2 | import { Budget } from "@/AWS/Budgets/Budget.ts"; |
| 3 | import * as Provider from "@/Provider"; |
| 4 | import * as Test from "@/Test/Alchemy"; |
| 5 | import * as budgets from "@distilled.cloud/aws/budgets"; |
| 6 | import { expect } from "alchemy-test"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | const budgetName = "alchemy-test-budget-lifecycle"; |
| 14 | |
| 15 | const getBudget = (accountId: string) => |
| 16 | budgets.describeBudget({ AccountId: accountId, BudgetName: budgetName }).pipe( |
| 17 | Effect.map((r) => r.Budget), |
| 18 | Effect.catchTag("NotFoundException", () => Effect.succeed(undefined)), |
| 19 | ); |
| 20 | |
| 21 | const getTags = (arn: string) => |
| 22 | budgets.listTagsForResource({ ResourceARN: arn }).pipe( |
| 23 | Effect.map((r) => |
| 24 | Object.fromEntries((r.ResourceTags ?? []).map((t) => [t.Key, t.Value])), |
| 25 | ), |
| 26 | Effect.catchTag("NotFoundException", () => |
| 27 | Effect.succeed({} as Record<string, string>), |
| 28 | ), |
| 29 | ); |
| 30 | |
| 31 | // The notification/subscriber listings are eventually consistent for a few |
| 32 | // seconds after a write — poll boundedly instead of asserting immediately. |
| 33 | const consistencyPolicy = { |
| 34 | schedule: Schedule.spaced("2 seconds"), |
| 35 | times: 15, |
| 36 | } as const; |
| 37 | |
| 38 | const getNotifications = (accountId: string) => |
| 39 | budgets |
| 40 | .describeNotificationsForBudget({ |
| 41 | AccountId: accountId, |
| 42 | BudgetName: budgetName, |
| 43 | }) |
| 44 | .pipe( |
| 45 | Effect.map((r) => r.Notifications ?? []), |
| 46 | Effect.catchTag("NotFoundException", () => Effect.succeed([])), |
| 47 | ); |
| 48 | |
| 49 | const getSubscribers = (accountId: string, threshold: number) => |
| 50 | budgets |
| 51 | .describeSubscribersForNotification({ |
| 52 | AccountId: accountId, |
| 53 | BudgetName: budgetName, |
| 54 | Notification: { |
| 55 | NotificationType: "ACTUAL", |
| 56 | ComparisonOperator: "GREATER_THAN", |
| 57 | Threshold: threshold, |
| 58 | ThresholdType: "PERCENTAGE", |
| 59 | }, |
| 60 | }) |
| 61 | .pipe( |
| 62 | Effect.map((r) => |
| 63 | // `Subscriber.Address` is sensitive in distilled — unwrap the |
| 64 | // Redacted for comparison. |
| 65 | (r.Subscribers ?? []).map((s) => |
| 66 | Redacted.isRedacted(s.Address) |
| 67 | ? Redacted.value(s.Address) |
| 68 | : s.Address, |
| 69 | ), |
| 70 | ), |
| 71 | Effect.catchTag("NotFoundException", () => Effect.succeed([])), |
| 72 | ); |
| 73 | |
| 74 | const deployBudget = (options: { |
| 75 | amount: string; |
| 76 | subscriberAddress?: string; |
| 77 | tags?: Record<string, string>; |
| 78 | }) => |
| 79 | Effect.gen(function* () { |
| 80 | const budget = yield* Budget("LifecycleBudget", { |
| 81 | budgetName, |
| 82 | budgetType: "COST", |
| 83 | timeUnit: "MONTHLY", |
| 84 | budgetLimit: { amount: options.amount, unit: "USD" }, |
| 85 | tags: options.tags, |
| 86 | notifications: options.subscriberAddress |
| 87 | ? [ |
| 88 | { |
| 89 | notificationType: "ACTUAL", |
| 90 | comparisonOperator: "GREATER_THAN", |
| 91 | threshold: 80, |
| 92 | thresholdType: "PERCENTAGE", |
| 93 | subscribers: [ |
| 94 | { |
| 95 | subscriptionType: "EMAIL", |
| 96 | address: options.subscriberAddress, |
| 97 | }, |
| 98 | ], |
| 99 | }, |
| 100 | ] |
| 101 | : undefined, |
| 102 | }); |
| 103 | return { accountId: budget.accountId, budgetArn: budget.budgetArn }; |
| 104 | }); |
| 105 | |
| 106 | test.provider( |
| 107 | "lifecycle: create with notification+tags, sync subscribers+tags, drop notification, destroy", |
| 108 | (stack) => |
| 109 | Effect.gen(function* () { |
| 110 | yield* stack.destroy(); |
| 111 | |
| 112 | const deployed = yield* stack.deploy( |
| 113 | deployBudget({ |
| 114 | amount: "100", |
| 115 | subscriberAddress: "budget-test@example.com", |
| 116 | tags: { Team: "alchemy-test" }, |
| 117 | }), |
| 118 | ); |
| 119 | |
| 120 | const accountId = deployed.accountId; |
| 121 | expect(deployed.budgetArn).toBe( |
| 122 | `arn:aws:budgets::${accountId}:budget/${budgetName}`, |
| 123 | ); |
| 124 | |
| 125 | // Out-of-band verification via distilled. |
| 126 | const created = yield* getBudget(accountId); |
| 127 | expect(created?.BudgetName).toBe(budgetName); |
| 128 | expect(Number(created?.BudgetLimit?.Amount)).toBe(100); |
| 129 | expect(created?.BudgetLimit?.Unit).toBe("USD"); |
| 130 | expect(created?.TimeUnit).toBe("MONTHLY"); |
| 131 | |
| 132 | const notifications = yield* getNotifications(accountId).pipe( |
| 133 | Effect.repeat({ |
| 134 | ...consistencyPolicy, |
| 135 | until: (n): boolean => n.length > 0, |
| 136 | }), |
| 137 | ); |
| 138 | expect(notifications.length).toBe(1); |
| 139 | expect(notifications[0]?.Threshold).toBe(80); |
| 140 | |
| 141 | // Tags — user tag plus the internal alchemy brand. |
| 142 | const createdTags = yield* getTags(deployed.budgetArn); |
| 143 | expect(createdTags.Team).toBe("alchemy-test"); |
| 144 | expect( |
| 145 | Object.keys(createdTags).some((k) => k.startsWith("alchemy:")), |
| 146 | ).toBe(true); |
| 147 | |
| 148 | // Canonical list() coverage. |
| 149 | const provider = yield* Provider.findProvider(Budget); |
| 150 | const all = yield* provider.list(); |
| 151 | expect(all.some((b) => b.budgetName === budgetName)).toBe(true); |
| 152 | |
| 153 | // Update — raise the limit, change the notification's subscriber |
| 154 | // (exercises subscriber sync on a kept notification), update the tag. |
| 155 | yield* stack.deploy( |
| 156 | deployBudget({ |
| 157 | amount: "250", |
| 158 | subscriberAddress: "budget-test-updated@example.com", |
| 159 | tags: { Team: "alchemy-test-updated" }, |
| 160 | }), |
| 161 | ); |
| 162 | |
| 163 | const updated = yield* getBudget(accountId); |
| 164 | expect(Number(updated?.BudgetLimit?.Amount)).toBe(250); |
| 165 | |
| 166 | const subscribers = yield* getSubscribers(accountId, 80).pipe( |
| 167 | Effect.repeat({ |
| 168 | ...consistencyPolicy, |
| 169 | until: (s): boolean => |
| 170 | s.length === 1 && s[0] === "budget-test-updated@example.com", |
| 171 | }), |
| 172 | ); |
| 173 | expect(subscribers).toEqual(["budget-test-updated@example.com"]); |
| 174 | |
| 175 | const updatedTags = yield* getTags(deployed.budgetArn); |
| 176 | expect(updatedTags.Team).toBe("alchemy-test-updated"); |
| 177 | |
| 178 | // Update — drop the notification entirely. |
| 179 | yield* stack.deploy(deployBudget({ amount: "250" })); |
| 180 | |
| 181 | const afterDrop = yield* getNotifications(accountId).pipe( |
| 182 | Effect.repeat({ |
| 183 | ...consistencyPolicy, |
| 184 | until: (n): boolean => n.length === 0, |
| 185 | }), |
| 186 | ); |
| 187 | expect(afterDrop.length).toBe(0); |
| 188 | |
| 189 | // Destroy — the budget is gone. |
| 190 | yield* stack.destroy(); |
| 191 | const after = yield* getBudget(accountId); |
| 192 | expect(after).toBeUndefined(); |
| 193 | }), |
| 194 | { tags: ["provider:aws", "provider:aws:budgets", "live"], timeout: 180_000 }, |
| 195 | ); |