@fantastic.dev/better-stripe 0.60.0 → 0.61.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +5 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +26 -6
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +13 -3
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +26 -6
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +13 -3
  10. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  11. package/dist/client/usage/creditCapacity.d.ts.map +1 -1
  12. package/dist/client/usage/creditCapacity.js +7 -1
  13. package/dist/client/usage/creditCapacity.js.map +1 -1
  14. package/dist/client/usage/creditClient.d.ts +3 -0
  15. package/dist/client/usage/creditClient.d.ts.map +1 -1
  16. package/dist/client/usage/creditDelivery.d.ts.map +1 -1
  17. package/dist/client/usage/creditDelivery.js +4 -1
  18. package/dist/client/usage/creditDelivery.js.map +1 -1
  19. package/dist/client/usage/creditLifecycle.d.ts.map +1 -1
  20. package/dist/client/usage/creditLifecycle.js +2 -1
  21. package/dist/client/usage/creditLifecycle.js.map +1 -1
  22. package/dist/client/usage/creditNative.d.ts +3 -0
  23. package/dist/client/usage/creditNative.d.ts.map +1 -1
  24. package/dist/client/usage/creditNative.js +3 -0
  25. package/dist/client/usage/creditNative.js.map +1 -1
  26. package/dist/client/usage/creditProvider.d.ts.map +1 -1
  27. package/dist/client/usage/creditProvider.js +2 -1
  28. package/dist/client/usage/creditProvider.js.map +1 -1
  29. package/dist/client/usage/creditRead.d.ts +1 -0
  30. package/dist/client/usage/creditRead.d.ts.map +1 -1
  31. package/dist/client/usage/creditReconciliation.d.ts.map +1 -1
  32. package/dist/client/usage/creditReconciliation.js +11 -3
  33. package/dist/client/usage/creditReconciliation.js.map +1 -1
  34. package/dist/client/usage/creditSettlement.fixtures.d.ts +1 -0
  35. package/dist/client/usage/creditSettlement.fixtures.d.ts.map +1 -1
  36. package/dist/client/usage/creditTransport.fixtures.d.ts +1 -0
  37. package/dist/client/usage/creditTransport.fixtures.d.ts.map +1 -1
  38. package/dist/client/usage/creditTransport.fixtures.js +2 -1
  39. package/dist/client/usage/creditTransport.fixtures.js.map +1 -1
  40. package/dist/client/usage/index.d.ts +2 -0
  41. package/dist/client/usage/index.d.ts.map +1 -1
  42. package/dist/client/usage/subscriptionCancellation.d.ts.map +1 -1
  43. package/dist/client/usage/subscriptionCancellation.js +3 -2
  44. package/dist/client/usage/subscriptionCancellation.js.map +1 -1
  45. package/dist/client/usage/subscriptionCheckout.d.ts.map +1 -1
  46. package/dist/client/usage/subscriptionCheckout.js +3 -2
  47. package/dist/client/usage/subscriptionCheckout.js.map +1 -1
  48. package/dist/client/usage/subscriptionLifecycle.d.ts +1 -0
  49. package/dist/client/usage/subscriptionLifecycle.d.ts.map +1 -1
  50. package/dist/client/usage/subscriptionLifecycle.js +3 -2
  51. package/dist/client/usage/subscriptionLifecycle.js.map +1 -1
  52. package/dist/client/usage/subscriptionProvider.d.ts +2 -0
  53. package/dist/client/usage/subscriptionProvider.d.ts.map +1 -1
  54. package/dist/client/usage/subscriptionProvider.js +9 -2
  55. package/dist/client/usage/subscriptionProvider.js.map +1 -1
  56. package/dist/component/_generated/component.d.ts +21 -0
  57. package/dist/component/_generated/component.d.ts.map +1 -1
  58. package/dist/component/earnings/triggers.d.ts +16 -6
  59. package/dist/component/earnings/triggers.d.ts.map +1 -1
  60. package/dist/component/schema.d.ts +13 -3
  61. package/dist/component/usage/allowance.fixtures.d.ts +4212 -0
  62. package/dist/component/usage/allowance.fixtures.d.ts.map +1 -0
  63. package/dist/component/usage/allowance.fixtures.js +67 -0
  64. package/dist/component/usage/allowance.fixtures.js.map +1 -0
  65. package/dist/component/usageCredits/accounting.fixtures.d.ts +4235 -0
  66. package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -0
  67. package/dist/component/usageCredits/accounting.fixtures.js +89 -0
  68. package/dist/component/usageCredits/accounting.fixtures.js.map +1 -0
  69. package/dist/component/usageCredits/helpers.d.ts +7 -0
  70. package/dist/component/usageCredits/helpers.d.ts.map +1 -1
  71. package/dist/component/usageCredits/helpers.js +5 -0
  72. package/dist/component/usageCredits/helpers.js.map +1 -1
  73. package/dist/component/usageCredits/mutations.d.ts +6 -0
  74. package/dist/component/usageCredits/mutations.d.ts.map +1 -1
  75. package/dist/component/usageCredits/mutations.js +70 -7
  76. package/dist/component/usageCredits/mutations.js.map +1 -1
  77. package/dist/component/usageCredits/purchase.d.ts +1 -0
  78. package/dist/component/usageCredits/purchase.d.ts.map +1 -1
  79. package/dist/component/usageCredits/queries.d.ts +10 -0
  80. package/dist/component/usageCredits/queries.d.ts.map +1 -1
  81. package/dist/component/usageCredits/schema.d.ts +24 -4
  82. package/dist/component/usageCredits/schema.d.ts.map +1 -1
  83. package/dist/component/usageCredits/schema.js +4 -0
  84. package/dist/component/usageCredits/schema.js.map +1 -1
  85. package/dist/component/usageSubscriptions/history.d.ts.map +1 -1
  86. package/dist/component/usageSubscriptions/history.js +3 -1
  87. package/dist/component/usageSubscriptions/history.js.map +1 -1
  88. package/dist/component/usageSubscriptions/mutations.d.ts +5 -0
  89. package/dist/component/usageSubscriptions/mutations.d.ts.map +1 -1
  90. package/dist/component/usageSubscriptions/mutations.js +19 -0
  91. package/dist/component/usageSubscriptions/mutations.js.map +1 -1
  92. package/dist/component/usageSubscriptions/queries.d.ts +2 -0
  93. package/dist/component/usageSubscriptions/queries.d.ts.map +1 -1
  94. package/dist/component/usageSubscriptions/schema.d.ts +7 -2
  95. package/dist/component/usageSubscriptions/schema.d.ts.map +1 -1
  96. package/dist/component/usageSubscriptions/schema.js +1 -0
  97. package/dist/component/usageSubscriptions/schema.js.map +1 -1
  98. package/dist/react/index.d.ts +12 -0
  99. package/dist/react/index.d.ts.map +1 -1
  100. package/dist/react/index.js +7 -0
  101. package/dist/react/index.js.map +1 -1
  102. package/dist/react/workflows/Pagination.d.ts +1 -0
  103. package/dist/react/workflows/Pagination.d.ts.map +1 -1
  104. package/dist/react/workflows/Pagination.js +1 -0
  105. package/dist/react/workflows/Pagination.js.map +1 -1
  106. package/dist/react/workflows/Usage.fixtures.d.ts +17 -0
  107. package/dist/react/workflows/Usage.fixtures.d.ts.map +1 -0
  108. package/dist/react/workflows/Usage.fixtures.js +79 -0
  109. package/dist/react/workflows/Usage.fixtures.js.map +1 -0
  110. package/dist/react/workflows/Usage.types.d.ts +25 -0
  111. package/dist/react/workflows/Usage.types.d.ts.map +1 -0
  112. package/dist/react/workflows/Usage.types.js +11 -0
  113. package/dist/react/workflows/Usage.types.js.map +1 -0
  114. package/dist/react/workflows/UsageAllowance.d.ts +14 -0
  115. package/dist/react/workflows/UsageAllowance.d.ts.map +1 -0
  116. package/dist/react/workflows/UsageAllowance.js +21 -0
  117. package/dist/react/workflows/UsageAllowance.js.map +1 -0
  118. package/dist/react/workflows/UsageBilling.d.ts +20 -0
  119. package/dist/react/workflows/UsageBilling.d.ts.map +1 -0
  120. package/dist/react/workflows/UsageBilling.js +40 -0
  121. package/dist/react/workflows/UsageBilling.js.map +1 -0
  122. package/dist/react/workflows/UsageCredits.d.ts +14 -0
  123. package/dist/react/workflows/UsageCredits.d.ts.map +1 -0
  124. package/dist/react/workflows/UsageCredits.js +25 -0
  125. package/dist/react/workflows/UsageCredits.js.map +1 -0
  126. package/dist/react/workflows/UsageHistory.d.ts +40 -0
  127. package/dist/react/workflows/UsageHistory.d.ts.map +1 -0
  128. package/dist/react/workflows/UsageHistory.js +74 -0
  129. package/dist/react/workflows/UsageHistory.js.map +1 -0
  130. package/dist/react/workflows/UsageSummary.d.ts +19 -0
  131. package/dist/react/workflows/UsageSummary.d.ts.map +1 -0
  132. package/dist/react/workflows/UsageSummary.js +54 -0
  133. package/dist/react/workflows/UsageSummary.js.map +1 -0
  134. package/dist/react/workflows/usage-format.d.ts +11 -0
  135. package/dist/react/workflows/usage-format.d.ts.map +1 -0
  136. package/dist/react/workflows/usage-format.js +43 -0
  137. package/dist/react/workflows/usage-format.js.map +1 -0
  138. package/dist/react/workflows/usage-scope.d.ts +5 -0
  139. package/dist/react/workflows/usage-scope.d.ts.map +1 -0
  140. package/dist/react/workflows/usage-scope.js +26 -0
  141. package/dist/react/workflows/usage-scope.js.map +1 -0
  142. package/docs/README.md +4 -0
  143. package/docs/react-api.md +4 -0
  144. package/docs/usage-credits.md +6 -1
  145. package/docs/usage-react.md +182 -0
  146. package/docs/usage-security.md +130 -0
  147. package/package.json +3 -1
  148. package/src/client/usage/creditBackingIsolation.test.ts +110 -0
  149. package/src/client/usage/creditCapacity.test.ts +80 -1
  150. package/src/client/usage/creditCapacity.ts +10 -1
  151. package/src/client/usage/creditDelivery.ts +4 -0
  152. package/src/client/usage/creditLifecycle.ts +2 -0
  153. package/src/client/usage/creditNative.ts +5 -0
  154. package/src/client/usage/creditProofBounds.test.ts +187 -0
  155. package/src/client/usage/creditProvider.test.ts +74 -0
  156. package/src/client/usage/creditProvider.ts +2 -0
  157. package/src/client/usage/creditReconciliation.ts +18 -3
  158. package/src/client/usage/creditTransport.fixtures.ts +2 -1
  159. package/src/client/usage/index.ts +3 -0
  160. package/src/client/usage/subscriptionCancellation.ts +3 -2
  161. package/src/client/usage/subscriptionCheckout.ts +7 -2
  162. package/src/client/usage/subscriptionClient.test.ts +60 -1
  163. package/src/client/usage/subscriptionLifecycle.ts +3 -2
  164. package/src/client/usage/subscriptionProvider.ts +27 -3
  165. package/src/component/_generated/component.ts +40 -1
  166. package/src/component/usage/allowance.fixtures.ts +77 -0
  167. package/src/component/usage/allowance.test.ts +3 -66
  168. package/src/component/usage/allowanceBounds.test.ts +79 -0
  169. package/src/component/usage/historyIsolation.test.ts +124 -0
  170. package/src/component/usageCredits/accounting.fixtures.ts +96 -0
  171. package/src/component/usageCredits/accounting.test.ts +176 -82
  172. package/src/component/usageCredits/freshness.test.ts +33 -0
  173. package/src/component/usageCredits/helpers.ts +8 -0
  174. package/src/component/usageCredits/mutations.ts +83 -5
  175. package/src/component/usageCredits/quotas.test.ts +63 -0
  176. package/src/component/usageCredits/schema.ts +10 -0
  177. package/src/component/usageSubscriptions/history.test.ts +57 -0
  178. package/src/component/usageSubscriptions/history.ts +3 -1
  179. package/src/component/usageSubscriptions/mutations.ts +23 -0
  180. package/src/component/usageSubscriptions/schema.ts +1 -0
  181. package/src/react/index.ts +20 -0
  182. package/src/react/workflows/Pagination.tsx +2 -0
  183. package/src/react/workflows/Usage.fixtures.ts +88 -0
  184. package/src/react/workflows/Usage.test.tsx +373 -0
  185. package/src/react/workflows/Usage.types.ts +39 -0
  186. package/src/react/workflows/UsageAllowance.tsx +114 -0
  187. package/src/react/workflows/UsageBilling.tsx +90 -0
  188. package/src/react/workflows/UsageCredits.tsx +112 -0
  189. package/src/react/workflows/UsageHistory.test.tsx +206 -0
  190. package/src/react/workflows/UsageHistory.tsx +154 -0
  191. package/src/react/workflows/UsageSummary.tsx +204 -0
  192. package/src/react/workflows/usage-format.ts +48 -0
  193. package/src/react/workflows/usage-scope.ts +42 -0
@@ -8978,6 +8978,13 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
8978
8978
  };
8979
8979
  usageCredits: {
8980
8980
  mutations: {
8981
+ beginObservation: FunctionReference<
8982
+ "mutation",
8983
+ "internal",
8984
+ { inputJson: string },
8985
+ number,
8986
+ Name
8987
+ >;
8981
8988
  cancelPurchase: FunctionReference<
8982
8989
  "mutation",
8983
8990
  "internal",
@@ -9051,6 +9058,8 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9051
9058
  paymentIntentId?: string;
9052
9059
  paymentKey?: string;
9053
9060
  paymentProofJson?: string;
9061
+ recoveryReason?:
9062
+ "payment_refunded" | "payment_disputed" | "native_state_changed";
9054
9063
  refundedAmount?: number;
9055
9064
  state: "pending" | "ready" | "frozen" | "canceled";
9056
9065
  termsJson: string;
@@ -9100,7 +9109,12 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9100
9109
  saveObservation: FunctionReference<
9101
9110
  "mutation",
9102
9111
  "internal",
9103
- { inputJson: string; proofJson: string; startedAt: number },
9112
+ {
9113
+ inputJson: string;
9114
+ proofJson: string;
9115
+ proofStartedAt?: number;
9116
+ startedAt: number;
9117
+ },
9104
9118
  string,
9105
9119
  Name
9106
9120
  >;
@@ -9152,6 +9166,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9152
9166
  historyCount?: number;
9153
9167
  key: string;
9154
9168
  method: "direct" | "checkout";
9169
+ observationStartedAt?: number;
9155
9170
  providerId?: string;
9156
9171
  retiredAt?: number;
9157
9172
  retirementInvoiceId?: string;
@@ -9191,9 +9206,12 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9191
9206
  nativeExpired?: boolean;
9192
9207
  nativeObservationJson?: string;
9193
9208
  nativeUnused: boolean;
9209
+ observationConflictAt?: number;
9210
+ observationStartedAt?: number;
9194
9211
  observedAt?: number;
9195
9212
  paymentIntentId: string;
9196
9213
  proofJson: string;
9214
+ proofStartedAt?: number;
9197
9215
  purchaseKey: string;
9198
9216
  reserved: string;
9199
9217
  }>;
@@ -9212,6 +9230,10 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9212
9230
  paymentIntentId?: string;
9213
9231
  paymentKey?: string;
9214
9232
  paymentProofJson?: string;
9233
+ recoveryReason?:
9234
+ | "payment_refunded"
9235
+ | "payment_disputed"
9236
+ | "native_state_changed";
9215
9237
  refundedAmount?: number;
9216
9238
  state: "pending" | "ready" | "frozen" | "canceled";
9217
9239
  termsJson: string;
@@ -9237,6 +9259,8 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9237
9259
  paymentIntentId?: string;
9238
9260
  paymentKey?: string;
9239
9261
  paymentProofJson?: string;
9262
+ recoveryReason?:
9263
+ "payment_refunded" | "payment_disputed" | "native_state_changed";
9240
9264
  refundedAmount?: number;
9241
9265
  state: "pending" | "ready" | "frozen" | "canceled";
9242
9266
  termsJson: string;
@@ -9261,6 +9285,8 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9261
9285
  paymentIntentId?: string;
9262
9286
  paymentKey?: string;
9263
9287
  paymentProofJson?: string;
9288
+ recoveryReason?:
9289
+ "payment_refunded" | "payment_disputed" | "native_state_changed";
9264
9290
  refundedAmount?: number;
9265
9291
  state: "pending" | "ready" | "frozen" | "canceled";
9266
9292
  termsJson: string;
@@ -9313,9 +9339,12 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9313
9339
  nativeExpired?: boolean;
9314
9340
  nativeObservationJson?: string;
9315
9341
  nativeUnused: boolean;
9342
+ observationConflictAt?: number;
9343
+ observationStartedAt?: number;
9316
9344
  observedAt?: number;
9317
9345
  paymentIntentId: string;
9318
9346
  proofJson: string;
9347
+ proofStartedAt?: number;
9319
9348
  purchaseKey: string;
9320
9349
  reserved: string;
9321
9350
  }>;
@@ -9838,6 +9867,13 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9838
9867
  null,
9839
9868
  Name
9840
9869
  >;
9870
+ beginObservation: FunctionReference<
9871
+ "mutation",
9872
+ "internal",
9873
+ { key: string },
9874
+ number,
9875
+ Name
9876
+ >;
9841
9877
  completeCancellation: FunctionReference<
9842
9878
  "mutation",
9843
9879
  "internal",
@@ -9866,6 +9902,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9866
9902
  historyCount?: number;
9867
9903
  key: string;
9868
9904
  method: "direct" | "checkout";
9905
+ observationStartedAt?: number;
9869
9906
  providerId?: string;
9870
9907
  retiredAt?: number;
9871
9908
  retirementInvoiceId?: string;
@@ -9941,6 +9978,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9941
9978
  historyCount?: number;
9942
9979
  key: string;
9943
9980
  method: "direct" | "checkout";
9981
+ observationStartedAt?: number;
9944
9982
  providerId?: string;
9945
9983
  retiredAt?: number;
9946
9984
  retirementInvoiceId?: string;
@@ -10003,6 +10041,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
10003
10041
  historyCount?: number;
10004
10042
  key: string;
10005
10043
  method: "direct" | "checkout";
10044
+ observationStartedAt?: number;
10006
10045
  providerId?: string;
10007
10046
  retiredAt?: number;
10008
10047
  retirementInvoiceId?: string;
@@ -0,0 +1,77 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { vi } from "vitest";
4
+ import { schema, modules } from "../../test.js";
5
+ import {
6
+ request,
7
+ nativeSubscription,
8
+ } from "../../client/usage/subscription.fixtures.js";
9
+ const mutation = (name: string) =>
10
+ makeFunctionReference<"mutation">(`usageAllowance/mutations:${name}`);
11
+ const policy = {
12
+ reset: "billing_period",
13
+ rollover: "none",
14
+ entitled: true,
15
+ overage: "allow",
16
+ limit: { mode: "hard", quantity: "12" },
17
+ };
18
+ export async function setupAllowance() {
19
+ vi.useFakeTimers();
20
+ vi.setSystemTime(700000);
21
+ const t = convexTest(schema, modules);
22
+ const meter = {
23
+ ...request.meters[0],
24
+ binding: {
25
+ ...request.meters[0].binding,
26
+ plan: {
27
+ key: "calls",
28
+ version: "v1",
29
+ model: "included_overage",
30
+ unit: { name: "call", precision: 0 },
31
+ rate: { currency: "usd", unitAmountDecimal: "2" },
32
+ includedQuantity: "10",
33
+ reset: "billing_period",
34
+ },
35
+ },
36
+ };
37
+ const intent = await t.mutation(
38
+ makeFunctionReference<"mutation">("usageSubscriptions/mutations:prepare"),
39
+ {
40
+ termsJson: JSON.stringify({ ...request, meters: [meter] }),
41
+ method: "direct",
42
+ },
43
+ );
44
+ await t.mutation(
45
+ makeFunctionReference<"mutation">("usageSubscriptions/mutations:save"),
46
+ {
47
+ key: intent.key,
48
+ nativeJson: JSON.stringify(nativeSubscription()),
49
+ startedAt: Date.now(),
50
+ },
51
+ );
52
+ const subscription = {
53
+ subscriptionId: "sub_calls",
54
+ subscriptionItemId: "si_calls",
55
+ stripePriceId: "price_calls",
56
+ period: { start: 600, end: 1200 },
57
+ interval: "month",
58
+ };
59
+ const bindingJson = JSON.stringify({ ...meter, subscription });
60
+ const event = {
61
+ ...meter,
62
+ sourceEventId: "work1",
63
+ quantity: "6",
64
+ occurredAt: 700,
65
+ };
66
+ const args = {
67
+ inputJson: JSON.stringify(event),
68
+ subscriptionJson: JSON.stringify(subscription),
69
+ };
70
+ const configure = (value = policy, expectedRevision = 0) =>
71
+ t.mutation(mutation("configure"), {
72
+ bindingJson,
73
+ policyJson: JSON.stringify(value),
74
+ expectedRevision,
75
+ });
76
+ return { t, meter, event, subscription, bindingJson, args, configure };
77
+ }
@@ -1,11 +1,7 @@
1
- import { convexTest } from "convex-test";
1
+ import { setupAllowance as setup } from "./allowance.fixtures.js";
2
2
  import { makeFunctionReference } from "convex/server";
3
3
  import { afterEach, expect, it, vi } from "vitest";
4
- import { schema, modules } from "../../test.js";
5
- import {
6
- request,
7
- nativeSubscription,
8
- } from "../../client/usage/subscription.fixtures.js";
4
+ import { nativeSubscription } from "../../client/usage/subscription.fixtures.js";
9
5
  const mutation = (name: string) =>
10
6
  makeFunctionReference<"mutation">(`usageAllowance/mutations:${name}`);
11
7
  const status = makeFunctionReference<"query">("usageAllowance/queries:status");
@@ -17,66 +13,7 @@ const policy = {
17
13
  overage: "allow",
18
14
  limit: { mode: "hard", quantity: "12" },
19
15
  };
20
- async function setup() {
21
- vi.useFakeTimers();
22
- vi.setSystemTime(700000);
23
- const t = convexTest(schema, modules);
24
- const meter = {
25
- ...request.meters[0],
26
- binding: {
27
- ...request.meters[0].binding,
28
- plan: {
29
- key: "calls",
30
- version: "v1",
31
- model: "included_overage",
32
- unit: { name: "call", precision: 0 },
33
- rate: { currency: "usd", unitAmountDecimal: "2" },
34
- includedQuantity: "10",
35
- reset: "billing_period",
36
- },
37
- },
38
- };
39
- const intent = await t.mutation(
40
- makeFunctionReference<"mutation">("usageSubscriptions/mutations:prepare"),
41
- {
42
- termsJson: JSON.stringify({ ...request, meters: [meter] }),
43
- method: "direct",
44
- },
45
- );
46
- await t.mutation(
47
- makeFunctionReference<"mutation">("usageSubscriptions/mutations:save"),
48
- {
49
- key: intent.key,
50
- nativeJson: JSON.stringify(nativeSubscription()),
51
- startedAt: Date.now(),
52
- },
53
- );
54
- const subscription = {
55
- subscriptionId: "sub_calls",
56
- subscriptionItemId: "si_calls",
57
- stripePriceId: "price_calls",
58
- period: { start: 600, end: 1200 },
59
- interval: "month",
60
- };
61
- const bindingJson = JSON.stringify({ ...meter, subscription });
62
- const event = {
63
- ...meter,
64
- sourceEventId: "work1",
65
- quantity: "6",
66
- occurredAt: 700,
67
- };
68
- const args = {
69
- inputJson: JSON.stringify(event),
70
- subscriptionJson: JSON.stringify(subscription),
71
- };
72
- const configure = (value = policy, expectedRevision = 0) =>
73
- t.mutation(mutation("configure"), {
74
- bindingJson,
75
- policyJson: JSON.stringify(value),
76
- expectedRevision,
77
- });
78
- return { t, meter, event, subscription, bindingJson, args, configure };
79
- }
16
+
80
17
  afterEach(() => vi.useRealTimers());
81
18
  it("reserves a hard cap atomically and commits included usage and overage exactly once", async () => {
82
19
  const { t, args, configure, bindingJson, event } = await setup();
@@ -0,0 +1,79 @@
1
+ import { makeFunctionReference } from "convex/server";
2
+ import { afterEach, expect, it, vi } from "vitest";
3
+ import { setupAllowance } from "./allowance.fixtures.js";
4
+ const mutation = (name: string) =>
5
+ makeFunctionReference<"mutation">(`usageAllowance/mutations:${name}`);
6
+ const reconciliation = makeFunctionReference<"query">(
7
+ "usageAllowance/queries:reconciliation",
8
+ );
9
+ const record = makeFunctionReference<"mutation">("usage/mutations:record");
10
+ afterEach(() => vi.useRealTimers());
11
+ for (const commit of [false, true])
12
+ it(`matches exactly 500 ${commit ? "accepted events" : "open reservations"} and holds 501 without repairing`, async () => {
13
+ const { t, event, subscription, bindingJson, configure } =
14
+ await setupAllowance();
15
+ await configure({
16
+ reset: "billing_period",
17
+ rollover: "none",
18
+ entitled: true,
19
+ overage: "allow",
20
+ limit: { mode: "hard", quantity: "1000" },
21
+ });
22
+ const add = async (i: number) => {
23
+ const args = {
24
+ inputJson: JSON.stringify({
25
+ ...event,
26
+ sourceEventId: `bound_${i}`,
27
+ quantity: "1",
28
+ }),
29
+ subscriptionJson: JSON.stringify(subscription),
30
+ };
31
+ const reserved = await t.mutation(mutation("reserve"), args);
32
+ expect(reserved.state).toBe("reserved");
33
+ if (commit)
34
+ await t.mutation(record, {
35
+ ...args,
36
+ reservationId: reserved.reservationId,
37
+ });
38
+ };
39
+ for (let i = 0; i < 500; i++) await add(i);
40
+ expect(await t.query(reconciliation, { bindingJson })).toMatchObject({
41
+ status: "matched",
42
+ consumedScaledQuantity: commit ? "500" : "0",
43
+ reservedScaledQuantity: commit ? "0" : "500",
44
+ repaired: false,
45
+ });
46
+ await add(500);
47
+ expect(await t.query(reconciliation, { bindingJson })).toMatchObject({
48
+ status: "held",
49
+ reason: "local_evidence_incomplete",
50
+ consumedScaledQuantity: commit ? "501" : "0",
51
+ reservedScaledQuantity: commit ? "0" : "501",
52
+ repaired: false,
53
+ });
54
+ }, 30000);
55
+ it("commits the 100000th allowance audit entry and rolls back one over the retained quota", async () => {
56
+ const { t, configure, args } = await setupAllowance();
57
+ await configure();
58
+ await t.run(async (ctx) => {
59
+ const row = (await ctx.db.query("usageAllowancePolicies").first())!;
60
+ await ctx.db.patch(row._id, { historyCount: 99999 });
61
+ });
62
+ const reservation = await t.mutation(mutation("reserve"), args);
63
+ expect(reservation.state).toBe("reserved");
64
+ await expect(
65
+ t.mutation(record, { ...args, reservationId: reservation.reservationId }),
66
+ ).rejects.toThrow(/history quota/i);
67
+ await t.run(async (ctx) => {
68
+ expect(await ctx.db.query("usageEvents").first()).toBeNull();
69
+ expect(await ctx.db.query("usageOutbox").first()).toBeNull();
70
+ expect(await ctx.db.query("usageReservations").first()).toMatchObject({
71
+ state: "reserved",
72
+ });
73
+ expect(await ctx.db.query("usageAllowancePolicies").first()).toMatchObject({
74
+ historyCount: 100000,
75
+ consumed: "0",
76
+ reserved: "6",
77
+ });
78
+ });
79
+ });
@@ -0,0 +1,124 @@
1
+ import { makeFunctionReference } from "convex/server";
2
+ import { afterEach, expect, it, vi } from "vitest";
3
+ import { setupAllowance } from "./allowance.fixtures.js";
4
+ import {
5
+ request,
6
+ nativeSubscription,
7
+ } from "../../client/usage/subscription.fixtures.js";
8
+ const prepare = makeFunctionReference<"mutation">(
9
+ "usageSubscriptions/mutations:prepare",
10
+ );
11
+ const save = makeFunctionReference<"mutation">(
12
+ "usageSubscriptions/mutations:save",
13
+ );
14
+ afterEach(() => vi.useRealTimers());
15
+ it("keeps later subscription and allowance history pages inside the requested tenant when cursors are reused", async () => {
16
+ const { t, meter, subscription, configure, bindingJson } =
17
+ await setupAllowance();
18
+ await configure();
19
+ await configure(
20
+ {
21
+ reset: "billing_period",
22
+ rollover: "none",
23
+ entitled: true,
24
+ overage: "allow",
25
+ limit: { mode: "hard", quantity: "13" },
26
+ },
27
+ 1,
28
+ );
29
+ const originalIntent = await t.mutation(prepare, {
30
+ termsJson: JSON.stringify({ ...request, meters: [meter] }),
31
+ method: "direct",
32
+ });
33
+ const renewed = nativeSubscription();
34
+ renewed.cancel_at_period_end = true;
35
+ await t.mutation(save, {
36
+ key: originalIntent.key,
37
+ nativeJson: JSON.stringify(renewed),
38
+ startedAt: Date.now() + 1,
39
+ });
40
+ const foreign = {
41
+ ...meter,
42
+ scope: { ...meter.scope, customerAccount: "acct_other" },
43
+ binding: { ...meter.binding, stripeCustomerId: "cus_other" },
44
+ };
45
+ const other = await t.mutation(prepare, {
46
+ termsJson: JSON.stringify({ ...request, meters: [foreign] }),
47
+ method: "direct",
48
+ });
49
+ const native = nativeSubscription();
50
+ native.id = "sub_other";
51
+ native.customer = "cus_other";
52
+ native.customer_account = "acct_other";
53
+ await t.mutation(save, {
54
+ key: other.key,
55
+ nativeJson: JSON.stringify(native),
56
+ startedAt: Date.now(),
57
+ });
58
+ const otherBinding = JSON.stringify({
59
+ ...foreign,
60
+ subscription: { ...subscription, subscriptionId: "sub_other" },
61
+ });
62
+ await t.mutation(
63
+ makeFunctionReference<"mutation">("usageAllowance/mutations:configure"),
64
+ {
65
+ bindingJson: otherBinding,
66
+ policyJson: JSON.stringify({
67
+ reset: "billing_period",
68
+ rollover: "none",
69
+ entitled: true,
70
+ overage: "allow",
71
+ limit: { mode: "none" },
72
+ }),
73
+ expectedRevision: 0,
74
+ },
75
+ );
76
+ for (const kind of ["subscription", "allowance"]) {
77
+ const query = makeFunctionReference<"query">(
78
+ kind === "subscription"
79
+ ? "usageSubscriptions/queries:history"
80
+ : "usageAllowance/queries:readHistory",
81
+ );
82
+ const args =
83
+ kind === "subscription"
84
+ ? {
85
+ scope: {
86
+ stripeAccountId: meter.scope.stripeAccountId,
87
+ customerAccount: meter.scope.customerAccount,
88
+ livemode: false,
89
+ },
90
+ requestId: "signup1",
91
+ kind: "subscription",
92
+ }
93
+ : { bindingJson };
94
+ const otherArgs =
95
+ kind === "subscription"
96
+ ? {
97
+ ...args,
98
+ scope: {
99
+ stripeAccountId: meter.scope.stripeAccountId,
100
+ customerAccount: "acct_other",
101
+ livemode: false,
102
+ },
103
+ }
104
+ : { bindingJson: otherBinding };
105
+ const first = await t.query(query, {
106
+ ...args,
107
+ paginationOpts: { numItems: 1, cursor: null },
108
+ });
109
+ expect(first.isDone).toBe(false);
110
+ const paginationOpts = { numItems: 1, cursor: first.continueCursor };
111
+ const next = await t.query(query, { ...args, paginationOpts });
112
+ expect(next).toEqual(await t.query(query, { ...args, paginationOpts }));
113
+ expect(next.page).toHaveLength(1);
114
+ try {
115
+ const otherPage = await t.query(query, { ...otherArgs, paginationOpts });
116
+ for (const row of otherPage.page) {
117
+ if (kind === "subscription") expect(row.intentKey).toBe(other.key);
118
+ else expect(row.key).not.toBe(first.page[0].key);
119
+ }
120
+ } catch (error) {
121
+ expect(String(error)).toMatch(/cursor|range|index|position|pagination/i);
122
+ }
123
+ }
124
+ });
@@ -0,0 +1,96 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { vi } from "vitest";
4
+ import { schema, modules } from "../../test.js";
5
+ import {
6
+ request,
7
+ nativeSubscription,
8
+ } from "../../client/usage/subscription.fixtures.js";
9
+ const ref = (name: string) =>
10
+ makeFunctionReference<"mutation">(`usageCredits/mutations:${name}`);
11
+ const prepare = makeFunctionReference<"mutation">(
12
+ "usageSubscriptions/mutations:prepare",
13
+ );
14
+ const save = makeFunctionReference<"mutation">(
15
+ "usageSubscriptions/mutations:save",
16
+ );
17
+ export async function setupCredits() {
18
+ vi.useFakeTimers();
19
+ vi.setSystemTime(700000);
20
+ const t = convexTest(schema, modules);
21
+ const meter = {
22
+ scope: request.meters[0].scope,
23
+ binding: {
24
+ ...request.meters[0].binding,
25
+ plan: {
26
+ key: "calls",
27
+ version: "v1",
28
+ model: "prepaid",
29
+ unit: { name: "call", precision: 0 },
30
+ rate: { currency: "usd", unitAmountDecimal: "2" },
31
+ insufficientBalance: "reject",
32
+ } as const,
33
+ },
34
+ };
35
+ const terms = { ...request, meters: [meter] };
36
+ const intent = await t.mutation(prepare, {
37
+ termsJson: JSON.stringify(terms),
38
+ method: "direct",
39
+ });
40
+ await t.mutation(save, {
41
+ key: intent.key,
42
+ nativeJson: JSON.stringify(nativeSubscription()),
43
+ startedAt: Date.now(),
44
+ });
45
+ const purchase = {
46
+ ...meter,
47
+ purchaseId: "topup1",
48
+ quantity: "10",
49
+ ownership: "issuer",
50
+ rollover: "retain_original_terms",
51
+ };
52
+ const row = await t.mutation(ref("preparePurchase"), {
53
+ inputJson: JSON.stringify(purchase),
54
+ });
55
+ await t.mutation(ref("pinPayment"), {
56
+ key: row.key,
57
+ paymentIntentId: "pi_topup1",
58
+ });
59
+ const proof = {
60
+ paymentIntentId: "pi_topup1",
61
+ chargeId: "ch_topup1",
62
+ amount: 20,
63
+ currency: "usd",
64
+ customerAccount: "acct_buyer",
65
+ livemode: false,
66
+ };
67
+ await t.mutation(ref("verifyPayment"), {
68
+ key: row.key,
69
+ proofJson: JSON.stringify(proof),
70
+ });
71
+ await t.mutation(ref("pinGrant"), {
72
+ key: row.key,
73
+ proofJson: JSON.stringify({
74
+ ...proof,
75
+ creditGrantId: "credgr_topup1",
76
+ stripePriceId: "price_calls",
77
+ createdAt: 700,
78
+ effectiveAt: 700,
79
+ expiresAt: null,
80
+ }),
81
+ });
82
+ const subscription = {
83
+ subscriptionId: "sub_calls",
84
+ subscriptionItemId: "si_calls",
85
+ stripePriceId: "price_calls",
86
+ period: { start: 600, end: 1200 },
87
+ interval: "month",
88
+ };
89
+ const event = {
90
+ ...meter,
91
+ sourceEventId: "work1",
92
+ quantity: "6",
93
+ occurredAt: 700,
94
+ };
95
+ return { t, meter, purchase, row, proof, subscription, event };
96
+ }