|
| 1 | +/** |
| 2 | + * TDD test for the `usage_limit` block on `balances.limit_reached`. |
| 3 | + * |
| 4 | + * Contract under test: |
| 5 | + * New types/fields: |
| 6 | + * - limit_reached.filter (schema catch-up; already emitted for filtered caps) |
| 7 | + * - limit_reached.usage_limit { limit, interval, anchor, usage, remaining, window_start_at, window_end_at } |
| 8 | + * present iff limit_type is usage_limit; absent for included / spend_limit / max_purchase |
| 9 | + * |
| 10 | + * Pre-impl red: payload has no usage_limit block. |
| 11 | + * Post-impl green: checkLimitReached reads the resolved window limit off the FullSubject. |
| 12 | + */ |
| 13 | + |
| 14 | +import { afterAll, beforeAll, expect, test } from "bun:test"; |
| 15 | +import { |
| 16 | + ApiVersion, |
| 17 | + EntInterval, |
| 18 | + getUsageWindowBounds, |
| 19 | + ResetInterval, |
| 20 | +} from "@autumn/shared"; |
| 21 | +import { |
| 22 | + getTestSvixAppId, |
| 23 | + setupWebhookTest, |
| 24 | + type WebhookTestSetup, |
| 25 | + waitForWebhook, |
| 26 | +} from "@tests/integration/utils/svixWebhookTestUtils.js"; |
| 27 | +import { TestFeature } from "@tests/setup/v2Features.js"; |
| 28 | +import { items } from "@tests/utils/fixtures/items.js"; |
| 29 | +import { products } from "@tests/utils/fixtures/products.js"; |
| 30 | +import { timeout } from "@tests/utils/genUtils.js"; |
| 31 | +import ctx from "@tests/utils/testInitUtils/createTestContext.js"; |
| 32 | +import { initScenario, s } from "@tests/utils/testInitUtils/initScenario.js"; |
| 33 | +import chalk from "chalk"; |
| 34 | +import { AutumnInt } from "@/external/autumn/autumnCli.js"; |
| 35 | +import { setCustomerUsageLimit } from "../../utils/usage-limit-utils/customerUsageLimitUtils.js"; |
| 36 | + |
| 37 | +const autumnV2_3 = new AutumnInt({ version: ApiVersion.V2_3 }); |
| 38 | + |
| 39 | +type LimitReachedPayload = { |
| 40 | + type: string; |
| 41 | + data: { |
| 42 | + customer_id: string; |
| 43 | + feature_id: string; |
| 44 | + limit_type: string; |
| 45 | + entity_id?: string; |
| 46 | + filter?: { properties: Record<string, string> }; |
| 47 | + usage_limit?: { |
| 48 | + limit: number; |
| 49 | + interval: string; |
| 50 | + anchor: string; |
| 51 | + usage: number; |
| 52 | + remaining: number; |
| 53 | + window_start_at: number; |
| 54 | + window_end_at: number; |
| 55 | + }; |
| 56 | + }; |
| 57 | +}; |
| 58 | + |
| 59 | +let webhook: WebhookTestSetup; |
| 60 | +let playToken: string; |
| 61 | + |
| 62 | +beforeAll(async () => { |
| 63 | + const appId = getTestSvixAppId({ svixConfig: ctx.org.svix_config }); |
| 64 | + webhook = await setupWebhookTest({ |
| 65 | + appId, |
| 66 | + filterTypes: ["balances.limit_reached"], |
| 67 | + }); |
| 68 | + playToken = webhook.playToken; |
| 69 | +}); |
| 70 | + |
| 71 | +afterAll(async () => { |
| 72 | + await webhook?.cleanup(); |
| 73 | +}); |
| 74 | + |
| 75 | +const waitForLimitReached = ({ |
| 76 | + customerId, |
| 77 | + limitType, |
| 78 | +}: { |
| 79 | + customerId: string; |
| 80 | + limitType: string; |
| 81 | +}) => |
| 82 | + waitForWebhook<LimitReachedPayload>({ |
| 83 | + token: playToken, |
| 84 | + predicate: (payload) => |
| 85 | + payload.type === "balances.limit_reached" && |
| 86 | + payload.data?.customer_id === customerId && |
| 87 | + payload.data?.limit_type === limitType, |
| 88 | + timeoutMs: 15000, |
| 89 | + }); |
| 90 | + |
| 91 | +test(`${chalk.yellowBright("limit-reached-ul1: a usage_limit block describes the cap that blocked")}`, async () => { |
| 92 | + const customerId = "lr-ul-block-1"; |
| 93 | + const plan = products.base({ |
| 94 | + id: "lr-ul-block", |
| 95 | + items: [items.monthlyMessages({ includedUsage: 10000 })], |
| 96 | + }); |
| 97 | + await initScenario({ |
| 98 | + customerId, |
| 99 | + setup: [s.customer({ testClock: false }), s.products({ list: [plan] })], |
| 100 | + actions: [s.billing.attach({ productId: plan.id })], |
| 101 | + }); |
| 102 | + await setCustomerUsageLimit({ |
| 103 | + autumn: autumnV2_3, |
| 104 | + customerId, |
| 105 | + featureId: TestFeature.Messages, |
| 106 | + limit: 5, |
| 107 | + interval: ResetInterval.Day, |
| 108 | + anchor: "utc", |
| 109 | + }); |
| 110 | + |
| 111 | + await autumnV2_3.track({ |
| 112 | + customer_id: customerId, |
| 113 | + feature_id: TestFeature.Messages, |
| 114 | + value: 5, |
| 115 | + }); |
| 116 | + |
| 117 | + const result = await waitForLimitReached({ |
| 118 | + customerId, |
| 119 | + limitType: "usage_limit", |
| 120 | + }); |
| 121 | + expect(result).not.toBeNull(); |
| 122 | + const bounds = getUsageWindowBounds({ |
| 123 | + interval: EntInterval.Day, |
| 124 | + now: Date.now(), |
| 125 | + }); |
| 126 | + expect(result!.payload.data.filter).toBeUndefined(); |
| 127 | + expect(result!.payload.data.usage_limit).toEqual({ |
| 128 | + limit: 5, |
| 129 | + interval: "day", |
| 130 | + anchor: "utc", |
| 131 | + usage: 5, |
| 132 | + remaining: 0, |
| 133 | + window_start_at: bounds.windowStartAt, |
| 134 | + window_end_at: bounds.windowEndAt, |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +test(`${chalk.yellowBright("limit-reached-ul2: a filtered cap echoes its filter and filtered counter")}`, async () => { |
| 139 | + const customerId = "lr-ul-filter-1"; |
| 140 | + const plan = products.base({ |
| 141 | + id: "lr-ul-filter", |
| 142 | + items: [items.monthlyMessages({ includedUsage: 10000 })], |
| 143 | + }); |
| 144 | + await initScenario({ |
| 145 | + customerId, |
| 146 | + setup: [s.customer({ testClock: false }), s.products({ list: [plan] })], |
| 147 | + actions: [s.billing.attach({ productId: plan.id })], |
| 148 | + }); |
| 149 | + await timeout(2000); |
| 150 | + await autumnV2_3.customers.update(customerId, { |
| 151 | + billing_controls: { |
| 152 | + usage_limits: [ |
| 153 | + { |
| 154 | + feature_id: TestFeature.Messages, |
| 155 | + enabled: true, |
| 156 | + limit: 5, |
| 157 | + interval: ResetInterval.Day, |
| 158 | + anchor: "utc", |
| 159 | + filter: { properties: { apiKeyId: "key-a" } }, |
| 160 | + }, |
| 161 | + ], |
| 162 | + }, |
| 163 | + }); |
| 164 | + await timeout(3000); |
| 165 | + |
| 166 | + await autumnV2_3.track({ |
| 167 | + customer_id: customerId, |
| 168 | + feature_id: TestFeature.Messages, |
| 169 | + value: 5, |
| 170 | + properties: { apiKeyId: "key-a" }, |
| 171 | + }); |
| 172 | + |
| 173 | + const result = await waitForLimitReached({ |
| 174 | + customerId, |
| 175 | + limitType: "usage_limit", |
| 176 | + }); |
| 177 | + expect(result).not.toBeNull(); |
| 178 | + expect(result!.payload.data.filter).toEqual({ |
| 179 | + properties: { apiKeyId: "key-a" }, |
| 180 | + }); |
| 181 | + expect(result!.payload.data.usage_limit?.limit).toBe(5); |
| 182 | + expect(result!.payload.data.usage_limit?.usage).toBe(5); |
| 183 | + expect(result!.payload.data.usage_limit?.remaining).toBe(0); |
| 184 | +}); |
| 185 | + |
| 186 | +test(`${chalk.yellowBright("limit-reached-ul3: an included-allowance block carries no usage_limit")}`, async () => { |
| 187 | + const customerId = "lr-ul-included-1"; |
| 188 | + const plan = products.base({ |
| 189 | + id: "lr-ul-included", |
| 190 | + items: [items.monthlyMessages({ includedUsage: 100 })], |
| 191 | + }); |
| 192 | + await initScenario({ |
| 193 | + customerId, |
| 194 | + setup: [s.customer({ testClock: false }), s.products({ list: [plan] })], |
| 195 | + actions: [s.billing.attach({ productId: plan.id })], |
| 196 | + }); |
| 197 | + |
| 198 | + await autumnV2_3.track({ |
| 199 | + customer_id: customerId, |
| 200 | + feature_id: TestFeature.Messages, |
| 201 | + value: 100, |
| 202 | + }); |
| 203 | + |
| 204 | + const result = await waitForLimitReached({ |
| 205 | + customerId, |
| 206 | + limitType: "included", |
| 207 | + }); |
| 208 | + expect(result).not.toBeNull(); |
| 209 | + expect(result!.payload.data.usage_limit).toBeUndefined(); |
| 210 | +}); |
0 commit comments