@fantastic.dev/better-stripe 0.30.1 → 0.30.3

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 (49) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +14 -2
  3. package/docs/affiliate-clawbacks.md +5 -5
  4. package/docs/settlement.md +1 -1
  5. package/package.json +2 -2
  6. package/src/component/{affiliate-direct-payout.test.ts → affiliatePayouts/affiliate-direct-payout.test.ts} +6 -7
  7. package/src/component/{affiliate-component-pagination.test.ts → affiliateRecovery/affiliate-component-pagination.test.ts} +7 -10
  8. package/src/component/{affiliate-recovery-lifecycle.test.ts → affiliateRecovery/affiliate-recovery-lifecycle.test.ts} +6 -7
  9. package/src/component/billing/checkout.test.ts +89 -0
  10. package/src/component/billing/invoices.test.ts +294 -0
  11. package/src/component/{store-subscriptions.test.ts → billing/store-subscriptions.test.ts} +2 -3
  12. package/src/component/{subscription-scoping.test.ts → billing/subscription-scoping.test.ts} +2 -4
  13. package/src/component/billing/subscriptions.test.ts +477 -0
  14. package/src/component/{connect.test.ts → connect/connect.test.ts} +2 -4
  15. package/src/component/{fee-routing.test.ts → connect/fee-routing.test.ts} +2 -4
  16. package/src/component/connect/payments.test.ts +88 -0
  17. package/src/component/connect/payouts.test.ts +173 -0
  18. package/src/component/{reversal-races.test.ts → connect/reversal-races.test.ts} +4 -6
  19. package/src/component/{seller-case-ownership.test.ts → connect/seller-case-ownership.test.ts} +2 -3
  20. package/src/component/{transfers.test.ts → connect/transfers.test.ts} +3 -5
  21. package/src/component/core/accounts.test.ts +266 -0
  22. package/src/component/{config-queries.test.ts → core/config-queries.test.ts} +2 -4
  23. package/src/component/{payout-reset-safety.test.ts → core/payout-reset-safety.test.ts} +1 -2
  24. package/src/component/core/reset.test.ts +124 -0
  25. package/src/component/{cost-recovery.test.ts → costRecovery/cost-recovery.test.ts} +1 -2
  26. package/src/component/{currency-migration.test.ts → earnings/currency-migration.test.ts} +2 -3
  27. package/src/component/{currency-totals.test.ts → earnings/currency-totals.test.ts} +2 -3
  28. package/src/component/{earnings-totals.test.ts → earnings/earnings-totals.test.ts} +3 -5
  29. package/src/component/{summary-arithmetic.test.ts → earnings/summary-arithmetic.test.ts} +2 -3
  30. package/src/component/{payout-policy-adoption.test.ts → payoutPolicy/payout-policy-adoption.test.ts} +2 -3
  31. package/src/component/{payout-policy.test.ts → payoutPolicy/payout-policy.test.ts} +1 -2
  32. package/src/component/{payout-adoption.test.ts → payoutRelease/payout-adoption.test.ts} +2 -6
  33. package/src/component/{payout-fees.test.ts → payoutRelease/payout-fees.test.ts} +2 -3
  34. package/src/component/{payout-recovery-integration.test.ts → payoutRelease/payout-recovery-integration.test.ts} +3 -4
  35. package/src/component/{payout-release-snapshot.test.ts → payoutRelease/payout-release-snapshot.test.ts} +1 -2
  36. package/src/component/{payout-release.test.ts → payoutRelease/payout-release.test.ts} +2 -3
  37. package/src/component/{payout-revisions.test.ts → payoutSnapshots/payout-revisions.test.ts} +1 -2
  38. package/src/component/{payout-snapshots.test.ts → payoutSnapshots/payout-snapshots.test.ts} +4 -5
  39. package/src/component/{payout-sync.test.ts → payoutSync/payout-sync.test.ts} +1 -2
  40. package/src/component/{payout-component-pagination.test.ts → payoutWorker/payout-component-pagination.test.ts} +8 -9
  41. package/src/component/{payout-worker.test.ts → payoutWorker/payout-worker.test.ts} +1 -2
  42. package/src/component/products/products.test.ts +239 -0
  43. package/src/component/{read-pagination.test.ts → products/read-pagination.test.ts} +2 -3
  44. package/src/component/{refund-history.test.ts → refundHistory/refund-history.test.ts} +2 -3
  45. package/src/component/{affiliate-clawbacks.test.ts → settlement/affiliate-clawbacks.test.ts} +3 -4
  46. package/src/component/{settlement-claims.test.ts → settlement/settlement-claims.test.ts} +2 -3
  47. package/src/component/webhooks/events.test.ts +121 -0
  48. package/src/component/{triggers.test.ts → webhooks/triggers.test.ts} +2 -4
  49. package/src/component/public.test.ts +0 -1858
@@ -1,10 +1,9 @@
1
1
  import { convexTest } from "convex-test";
2
2
  import { expect, it } from "vitest";
3
3
 
4
- import { api } from "./_generated/api.js";
5
- import schema from "./schema.js";
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
6
6
 
7
- const modules = import.meta.glob("./**/*.*s");
8
7
  const max = Number.MAX_SAFE_INTEGER;
9
8
  const transfer = (stripeTransferId: string, amount: number) => ({
10
9
  stripeTransferId,
@@ -2,9 +2,8 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it } from "vitest";
5
- import schema from "./schema.js";
6
- import { transferFingerprint } from "./payoutRelease/helpers.js";
7
- const modules = import.meta.glob("./**/*.*s");
5
+ import { schema, modules } from "../../test.js";
6
+ import { transferFingerprint } from "../payoutRelease/helpers.js";
8
7
  const m = (name: string) =>
9
8
  makeFunctionReference<"mutation">(`payoutPolicy/mutations:${name}`);
10
9
  const scope = { accountId: "acct_one", ownerKey: "owner" };
@@ -2,8 +2,7 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it } from "vitest";
5
- import schema from "./schema.js";
6
- const modules = import.meta.glob("./**/*.*s");
5
+ import { schema, modules } from "../../test.js";
7
6
  const m = (name: string) =>
8
7
  makeFunctionReference<"mutation">(`payoutPolicy/mutations:${name}`);
9
8
  const q = (name: string) =>
@@ -2,12 +2,8 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it } from "vitest";
5
- import schema from "./schema.js";
6
- import {
7
- transferFingerprint,
8
- payoutClaimFingerprint,
9
- } from "./payoutRelease/helpers.js";
10
- const modules = import.meta.glob("./**/*.*s");
5
+ import { schema, modules } from "../../test.js";
6
+ import { transferFingerprint, payoutClaimFingerprint } from "./helpers.js";
11
7
  const scope = { accountId: "acct_one", ownerKey: "owner" };
12
8
 
13
9
  async function setup() {
@@ -2,9 +2,8 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it } from "vitest";
5
- import schema from "./schema.js";
6
- import { transferFingerprint } from "./payoutRelease/helpers.js";
7
- const modules = import.meta.glob("./**/*.*s");
5
+ import { schema, modules } from "../../test.js";
6
+ import { transferFingerprint } from "./helpers.js";
8
7
  const m = (name: string) =>
9
8
  makeFunctionReference<"mutation">(`payoutRelease/mutations:${name}`);
10
9
  const scope = { accountId: "acct_recipient", ownerKey: "user_1" };
@@ -2,10 +2,9 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it } from "vitest";
5
- import schema from "./schema.js";
6
- import { changeBalance } from "./affiliateRecovery/accounting.js";
7
- import { transferFingerprint } from "./payoutRelease/helpers.js";
8
- const modules = import.meta.glob("./**/*.*s");
5
+ import { schema, modules } from "../../test.js";
6
+ import { changeBalance } from "../affiliateRecovery/accounting.js";
7
+ import { transferFingerprint } from "./helpers.js";
9
8
  const m = (name: string) =>
10
9
  makeFunctionReference<"mutation">(`payoutRelease/mutations:${name}`);
11
10
  const pm = (name: string) =>
@@ -2,8 +2,7 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it } from "vitest";
5
- import schema from "./schema.js";
6
- const modules = import.meta.glob("./**/*.*s");
5
+ import { schema, modules } from "../../test.js";
7
6
  const snapshot = makeFunctionReference<"query">(
8
7
  "payoutRelease/queries:snapshot",
9
8
  );
@@ -2,9 +2,8 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it } from "vitest";
5
- import schema from "./schema.js";
6
- import { transferFingerprint } from "./payoutRelease/helpers.js";
7
- const modules = import.meta.glob("./**/*.*s");
5
+ import { schema, modules } from "../../test.js";
6
+ import { transferFingerprint } from "./helpers.js";
8
7
  const m = (name: string) =>
9
8
  makeFunctionReference<"mutation">(`payoutRelease/mutations:${name}`);
10
9
  const pm = (name: string) =>
@@ -2,8 +2,7 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it } from "vitest";
5
- import schema from "./schema.js";
6
- const modules = import.meta.glob("./**/*.*s");
5
+ import { schema, modules } from "../../test.js";
7
6
  const m = (name: string) =>
8
7
  makeFunctionReference<"mutation">(`connect/mutations:${name}`);
9
8
  const transfer = (id = "tr_one", extra = {}) => ({
@@ -2,11 +2,10 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it, vi } from "vitest";
5
- import type { Doc } from "./_generated/dataModel.js";
6
- import schema from "./schema.js";
7
- import { cacheSource } from "./payoutSnapshots/localState.js";
8
- import { transferFingerprint } from "./payoutRelease/helpers.js";
9
- const modules = import.meta.glob("./**/*.*s");
5
+ import type { Doc } from "../_generated/dataModel.js";
6
+ import { schema, modules } from "../../test.js";
7
+ import { cacheSource } from "./localState.js";
8
+ import { transferFingerprint } from "../payoutRelease/helpers.js";
10
9
  const m = (name: string) =>
11
10
  makeFunctionReference<"mutation">(`payoutSnapshots/mutations:${name}`);
12
11
  const cm = (name: string) =>
@@ -2,8 +2,7 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it, vi } from "vitest";
5
- import schema from "./schema.js";
6
- const modules = import.meta.glob("./**/*.*s");
5
+ import { schema, modules } from "../../test.js";
7
6
  const scope = { stripePayoutId: "po_one", accountId: "acct_owner" };
8
7
  const payout = {
9
8
  ...scope,
@@ -6,15 +6,14 @@ import type {
6
6
  RegisteredQuery,
7
7
  RegisteredMutation,
8
8
  } from "convex/server";
9
- import type { MutationCtx } from "./_generated/server.js";
10
- import { componentDb } from "../testing/component-db.js";
11
- import schema from "./schema.js";
12
- import { policies, changing, recovering } from "./payoutWorker/queries.js";
13
- import { due } from "./payoutPolicy/queries.js";
14
- import { pendingPayouts } from "./affiliatePayouts/queries.js";
15
- import { listClaims } from "./costRecovery/queries.js";
16
- import { begin, advance } from "./payoutSnapshots/mutations.js";
17
- const modules = import.meta.glob("./**/*.*s");
9
+ import type { MutationCtx } from "../_generated/server.js";
10
+ import { componentDb } from "../../testing/component-db.js";
11
+ import { schema, modules } from "../../test.js";
12
+ import { policies, changing, recovering } from "./queries.js";
13
+ import { due } from "../payoutPolicy/queries.js";
14
+ import { pendingPayouts } from "../affiliatePayouts/queries.js";
15
+ import { listClaims } from "../costRecovery/queries.js";
16
+ import { begin, advance } from "../payoutSnapshots/mutations.js";
18
17
  function handler<Args extends DefaultFunctionArgs, Result>(
19
18
  fn:
20
19
  | RegisteredQuery<"public", Args, Result>
@@ -2,8 +2,7 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { makeFunctionReference } from "convex/server";
4
4
  import { expect, it, vi, afterEach } from "vitest";
5
- import schema from "./schema.js";
6
- const modules = import.meta.glob("./**/*.*s");
5
+ import { schema, modules } from "../../test.js";
7
6
  const m = (name: string) =>
8
7
  makeFunctionReference<"mutation">(`payoutWorker/mutations:${name}`);
9
8
  const q = (name: string) =>
@@ -0,0 +1,239 @@
1
+ // @vitest-environment edge-runtime
2
+ import { convexTest } from "convex-test";
3
+ import { describe, expect, it } from "vitest";
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
6
+
7
+ describe("products — product and price mutations and queries", () => {
8
+ it("upsertProduct + getProductByStripeId round trip", async () => {
9
+ const t = convexTest(schema, modules);
10
+
11
+ await t.mutation(api.products.mutations.upsertProduct, {
12
+ stripeProductId: "prod_001",
13
+ name: "Pro Plan",
14
+ active: true,
15
+ });
16
+
17
+ const product = await t.query(api.products.queries.getProductByStripeId, {
18
+ stripeProductId: "prod_001",
19
+ });
20
+
21
+ expect(product).not.toBeNull();
22
+ expect(product!.name).toBe("Pro Plan");
23
+ expect(product!.active).toBe(true);
24
+ });
25
+
26
+ it("upsertPrice + listPricesByProduct", async () => {
27
+ const t = convexTest(schema, modules);
28
+
29
+ await t.mutation(api.products.mutations.upsertProduct, {
30
+ stripeProductId: "prod_002",
31
+ name: "Starter Plan",
32
+ active: true,
33
+ });
34
+
35
+ await t.mutation(api.products.mutations.upsertPrice, {
36
+ stripePriceId: "price_001",
37
+ productId: "internal_prod_002",
38
+ stripeProductId: "prod_002",
39
+ unitAmount: 1000,
40
+ currency: "usd",
41
+ active: true,
42
+ type: "recurring",
43
+ interval: "month",
44
+ });
45
+
46
+ await t.mutation(api.products.mutations.upsertPrice, {
47
+ stripePriceId: "price_002",
48
+ productId: "internal_prod_002",
49
+ stripeProductId: "prod_002",
50
+ unitAmount: 10000,
51
+ currency: "usd",
52
+ active: true,
53
+ type: "recurring",
54
+ interval: "year",
55
+ });
56
+
57
+ const prices = await t.query(api.products.queries.listPricesByProduct, {
58
+ stripeProductId: "prod_002",
59
+ });
60
+
61
+ expect(prices).toHaveLength(2);
62
+ });
63
+ });
64
+
65
+ describe("products — listProducts with filters", () => {
66
+ it("filters by active status", async () => {
67
+ const t = convexTest(schema, modules);
68
+
69
+ await t.mutation(api.products.mutations.upsertProduct, {
70
+ stripeProductId: "prod_active",
71
+ name: "Active Product",
72
+ active: true,
73
+ });
74
+
75
+ await t.mutation(api.products.mutations.upsertProduct, {
76
+ stripeProductId: "prod_inactive",
77
+ name: "Inactive Product",
78
+ active: false,
79
+ });
80
+
81
+ const activeProducts = await t.query(api.products.queries.listProducts, {
82
+ active: true,
83
+ });
84
+ const inactiveProducts = await t.query(api.products.queries.listProducts, {
85
+ active: false,
86
+ });
87
+
88
+ expect(activeProducts).toHaveLength(1);
89
+ expect(activeProducts[0].name).toBe("Active Product");
90
+ expect(inactiveProducts).toHaveLength(1);
91
+ expect(inactiveProducts[0].name).toBe("Inactive Product");
92
+ });
93
+
94
+ it("filters by accountId", async () => {
95
+ const t = convexTest(schema, modules);
96
+
97
+ await t.mutation(api.products.mutations.upsertProduct, {
98
+ stripeProductId: "prod_acct_a",
99
+ name: "Account A Product",
100
+ active: true,
101
+ accountId: "acct_a",
102
+ });
103
+
104
+ await t.mutation(api.products.mutations.upsertProduct, {
105
+ stripeProductId: "prod_acct_b",
106
+ name: "Account B Product",
107
+ active: true,
108
+ accountId: "acct_b",
109
+ });
110
+
111
+ const products = await t.query(api.products.queries.listProducts, {
112
+ accountId: "acct_a",
113
+ });
114
+
115
+ expect(products).toHaveLength(1);
116
+ expect(products[0].name).toBe("Account A Product");
117
+ });
118
+ });
119
+
120
+ describe("products — price queries", () => {
121
+ it("getPriceByStripeId round trip", async () => {
122
+ const t = convexTest(schema, modules);
123
+
124
+ await t.mutation(api.products.mutations.upsertPrice, {
125
+ stripePriceId: "price_lookup",
126
+ productId: "internal_prod",
127
+ stripeProductId: "prod_lookup",
128
+ unitAmount: 5000,
129
+ currency: "eur",
130
+ active: true,
131
+ type: "one_time",
132
+ });
133
+
134
+ const price = await t.query(api.products.queries.getPriceByStripeId, {
135
+ stripePriceId: "price_lookup",
136
+ });
137
+
138
+ expect(price).not.toBeNull();
139
+ expect(price!.unitAmount).toBe(5000);
140
+ expect(price!.currency).toBe("eur");
141
+ expect(price!.type).toBe("one_time");
142
+ });
143
+
144
+ it("listPrices filters by active", async () => {
145
+ const t = convexTest(schema, modules);
146
+
147
+ await t.mutation(api.products.mutations.upsertPrice, {
148
+ stripePriceId: "price_active",
149
+ productId: "prod_x",
150
+ stripeProductId: "prod_x",
151
+ unitAmount: 100,
152
+ currency: "usd",
153
+ active: true,
154
+ type: "one_time",
155
+ });
156
+
157
+ await t.mutation(api.products.mutations.upsertPrice, {
158
+ stripePriceId: "price_archived",
159
+ productId: "prod_x",
160
+ stripeProductId: "prod_x",
161
+ unitAmount: 200,
162
+ currency: "usd",
163
+ active: false,
164
+ type: "one_time",
165
+ });
166
+
167
+ const activePrices = await t.query(api.products.queries.listPrices, {
168
+ active: true,
169
+ });
170
+ expect(activePrices).toHaveLength(1);
171
+ expect(activePrices[0].stripePriceId).toBe("price_active");
172
+ });
173
+
174
+ it("upsertPrice updates existing price", async () => {
175
+ const t = convexTest(schema, modules);
176
+
177
+ await t.mutation(api.products.mutations.upsertPrice, {
178
+ stripePriceId: "price_upd",
179
+ productId: "prod_u",
180
+ stripeProductId: "prod_u",
181
+ unitAmount: 1000,
182
+ currency: "usd",
183
+ active: true,
184
+ type: "recurring",
185
+ interval: "month",
186
+ });
187
+
188
+ await t.mutation(api.products.mutations.upsertPrice, {
189
+ stripePriceId: "price_upd",
190
+ productId: "prod_u",
191
+ stripeProductId: "prod_u",
192
+ unitAmount: 1000,
193
+ currency: "usd",
194
+ active: false,
195
+ type: "recurring",
196
+ interval: "month",
197
+ });
198
+
199
+ const price = await t.query(api.products.queries.getPriceByStripeId, {
200
+ stripePriceId: "price_upd",
201
+ });
202
+
203
+ expect(price!.active).toBe(false);
204
+ });
205
+ });
206
+
207
+ describe("regression — listProducts returns complete results beyond limit", () => {
208
+ it("returns all inactive products even when active ones fill the default limit", async () => {
209
+ const t = convexTest(schema, modules);
210
+ const accountId = "acct_regression_prod";
211
+
212
+ // Insert 55 active then 5 inactive products for the same accountId.
213
+ for (let i = 0; i < 55; i++) {
214
+ await t.mutation(api.products.mutations.upsertProduct, {
215
+ stripeProductId: `prod_active_${i}`,
216
+ accountId,
217
+ name: `Active Product ${i}`,
218
+ active: true,
219
+ });
220
+ }
221
+ for (let i = 0; i < 5; i++) {
222
+ await t.mutation(api.products.mutations.upsertProduct, {
223
+ stripeProductId: `prod_inactive_${i}`,
224
+ accountId,
225
+ name: `Inactive Product ${i}`,
226
+ active: false,
227
+ });
228
+ }
229
+
230
+ const result = await t.query(api.products.queries.listProducts, {
231
+ accountId,
232
+ active: false,
233
+ });
234
+
235
+ // Old code: take(50) → JS filter on active===false → returns 0. New: compound index → 5.
236
+ expect(result).toHaveLength(5);
237
+ expect(result.every((p) => p.active === false)).toBe(true);
238
+ });
239
+ });
@@ -1,9 +1,8 @@
1
1
  // @vitest-environment edge-runtime
2
2
  import { convexTest } from "convex-test";
3
3
  import { expect, it } from "vitest";
4
- import { api } from "./_generated/api.js";
5
- import schema from "./schema.js";
6
- const modules = import.meta.glob("./**/*.*s");
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
7
6
 
8
7
  it("traverses account/status payout history beyond the cap without duplicates", async () => {
9
8
  const t = convexTest(schema, modules);
@@ -1,10 +1,9 @@
1
1
  import { convexTest } from "convex-test";
2
2
  import { afterEach, expect, it, vi } from "vitest";
3
3
 
4
- import { api } from "./_generated/api.js";
5
- import schema from "./schema.js";
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
6
6
 
7
- const modules = import.meta.glob("./**/*.*s");
8
7
  const paginate = api.refundHistory.queries.paginateRefunds;
9
8
  afterEach(() => vi.useRealTimers());
10
9
  const rows = [
@@ -1,11 +1,10 @@
1
1
  // @vitest-environment edge-runtime
2
2
  import { convexTest } from "convex-test";
3
3
  import { describe, expect, it } from "vitest";
4
- import { api } from "./_generated/api.js";
5
- import schema from "./schema.js";
6
- import { proportionalCommission } from "./settlement/clawbacks.js";
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
6
+ import { proportionalCommission } from "./clawbacks.js";
7
7
 
8
- const modules = import.meta.glob("./**/*.*s");
9
8
  const setup = async (currency = "usd", commission = 1001) => {
10
9
  const t = convexTest(schema, modules);
11
10
  await t.mutation(api.settlement.mutations.reserveSettlementAllocation, {
@@ -1,9 +1,8 @@
1
1
  // @vitest-environment edge-runtime
2
2
  import { convexTest } from "convex-test";
3
3
  import { expect, it } from "vitest";
4
- import { api } from "./_generated/api.js";
5
- import schema from "./schema.js";
6
- const modules = import.meta.glob("./**/*.*s");
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
7
6
  it("keeps a claimed allocation immutable and isolated per source charge", async () => {
8
7
  const t = convexTest(schema, modules);
9
8
  const opts = {
@@ -0,0 +1,121 @@
1
+ // @vitest-environment edge-runtime
2
+ import { convexTest } from "convex-test";
3
+ import { describe, expect, it } from "vitest";
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
6
+
7
+ describe("webhooks — listWebhookEvents", () => {
8
+ it("returns all events in descending order", async () => {
9
+ const t = convexTest(schema, modules);
10
+
11
+ await t.run(async (ctx) => {
12
+ await ctx.db.insert("webhookEvents", {
13
+ stripeEventId: "evt_list_1",
14
+ eventType: "invoice.paid",
15
+ status: "processed",
16
+ processedAt: Date.now() - 1000,
17
+ });
18
+ await ctx.db.insert("webhookEvents", {
19
+ stripeEventId: "evt_list_2",
20
+ eventType: "checkout.session.completed",
21
+ status: "processed",
22
+ processedAt: Date.now(),
23
+ });
24
+ });
25
+
26
+ const events = await t.query(api.webhooks.queries.listWebhookEvents, {});
27
+
28
+ expect(events).toHaveLength(2);
29
+ // Most recent first
30
+ expect(events[0].stripeEventId).toBe("evt_list_2");
31
+ });
32
+
33
+ it("filters by eventType", async () => {
34
+ const t = convexTest(schema, modules);
35
+
36
+ await t.run(async (ctx) => {
37
+ await ctx.db.insert("webhookEvents", {
38
+ stripeEventId: "evt_type_1",
39
+ eventType: "invoice.paid",
40
+ status: "processed",
41
+ processedAt: Date.now(),
42
+ });
43
+ await ctx.db.insert("webhookEvents", {
44
+ stripeEventId: "evt_type_2",
45
+ eventType: "checkout.session.completed",
46
+ status: "processed",
47
+ processedAt: Date.now(),
48
+ });
49
+ });
50
+
51
+ const invoiceEvents = await t.query(
52
+ api.webhooks.queries.listWebhookEvents,
53
+ {
54
+ eventType: "invoice.paid",
55
+ },
56
+ );
57
+
58
+ expect(invoiceEvents).toHaveLength(1);
59
+ expect(invoiceEvents[0].eventType).toBe("invoice.paid");
60
+ });
61
+
62
+ it("filters by status", async () => {
63
+ const t = convexTest(schema, modules);
64
+
65
+ await t.run(async (ctx) => {
66
+ await ctx.db.insert("webhookEvents", {
67
+ stripeEventId: "evt_st_1",
68
+ eventType: "invoice.paid",
69
+ status: "processed",
70
+ processedAt: Date.now(),
71
+ });
72
+ await ctx.db.insert("webhookEvents", {
73
+ stripeEventId: "evt_st_2",
74
+ eventType: "invoice.paid",
75
+ status: "failed",
76
+ processedAt: Date.now(),
77
+ lastError: "Something went wrong",
78
+ });
79
+ });
80
+
81
+ const failed = await t.query(api.webhooks.queries.listWebhookEvents, {
82
+ status: "failed",
83
+ });
84
+
85
+ expect(failed).toHaveLength(1);
86
+ expect(failed[0].stripeEventId).toBe("evt_st_2");
87
+ });
88
+
89
+ it("combines eventType and status filters", async () => {
90
+ const t = convexTest(schema, modules);
91
+
92
+ await t.run(async (ctx) => {
93
+ await ctx.db.insert("webhookEvents", {
94
+ stripeEventId: "evt_combo_1",
95
+ eventType: "invoice.paid",
96
+ status: "processed",
97
+ processedAt: Date.now(),
98
+ });
99
+ await ctx.db.insert("webhookEvents", {
100
+ stripeEventId: "evt_combo_2",
101
+ eventType: "invoice.paid",
102
+ status: "failed",
103
+ processedAt: Date.now(),
104
+ });
105
+ await ctx.db.insert("webhookEvents", {
106
+ stripeEventId: "evt_combo_3",
107
+ eventType: "checkout.session.completed",
108
+ status: "failed",
109
+ processedAt: Date.now(),
110
+ });
111
+ });
112
+
113
+ const result = await t.query(api.webhooks.queries.listWebhookEvents, {
114
+ eventType: "invoice.paid",
115
+ status: "failed",
116
+ });
117
+
118
+ expect(result).toHaveLength(1);
119
+ expect(result[0].stripeEventId).toBe("evt_combo_2");
120
+ });
121
+ });
@@ -2,10 +2,8 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { afterEach, describe, expect, it, vi } from "vitest";
4
4
 
5
- import { api } from "./_generated/api.js";
6
- import schema from "./schema.js";
7
-
8
- const modules = import.meta.glob("./**/*.*s");
5
+ import { api } from "../_generated/api.js";
6
+ import { schema, modules } from "../../test.js";
9
7
 
10
8
  describe("webhook event ledger", () => {
11
9
  it("insertWebhookEvent: creates new entry with processing status", async () => {