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