@fantastic.dev/better-stripe 0.34.0 → 0.35.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 (258) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +1 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +486 -2
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +243 -1
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +486 -2
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +243 -1
  10. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  11. package/dist/client/connect/refundCommandEvidence.d.ts +14 -0
  12. package/dist/client/connect/refundCommandEvidence.d.ts.map +1 -0
  13. package/dist/client/connect/refundCommandEvidence.js +20 -0
  14. package/dist/client/connect/refundCommandEvidence.js.map +1 -0
  15. package/dist/client/connect/refundCommandNormalization.d.ts +11 -0
  16. package/dist/client/connect/refundCommandNormalization.d.ts.map +1 -0
  17. package/dist/client/connect/refundCommandNormalization.js +12 -0
  18. package/dist/client/connect/refundCommandNormalization.js.map +1 -0
  19. package/dist/client/financialObservations.d.ts +42 -0
  20. package/dist/client/financialObservations.d.ts.map +1 -0
  21. package/dist/client/financialObservations.js +58 -0
  22. package/dist/client/financialObservations.js.map +1 -0
  23. package/dist/client/financialOwnership.d.ts +52 -0
  24. package/dist/client/financialOwnership.d.ts.map +1 -0
  25. package/dist/client/financialOwnership.js +72 -0
  26. package/dist/client/financialOwnership.js.map +1 -0
  27. package/dist/client/index.d.ts +2 -2
  28. package/dist/client/index.d.ts.map +1 -1
  29. package/dist/client/index.js +2 -2
  30. package/dist/client/index.js.map +1 -1
  31. package/dist/client/observationPagination.d.ts +10 -0
  32. package/dist/client/observationPagination.d.ts.map +1 -0
  33. package/dist/client/observationPagination.js +12 -0
  34. package/dist/client/observationPagination.js.map +1 -0
  35. package/dist/client/publicTypes.d.ts +5 -0
  36. package/dist/client/publicTypes.d.ts.map +1 -1
  37. package/dist/client/readContracts.d.ts +29 -0
  38. package/dist/client/readContracts.d.ts.map +1 -1
  39. package/dist/client/readContracts.js +10 -0
  40. package/dist/client/readContracts.js.map +1 -1
  41. package/dist/client/transferObservations.d.ts +59 -0
  42. package/dist/client/transferObservations.d.ts.map +1 -0
  43. package/dist/client/transferObservations.js +82 -0
  44. package/dist/client/transferObservations.js.map +1 -0
  45. package/dist/component/_generated/component.d.ts +506 -0
  46. package/dist/component/_generated/component.d.ts.map +1 -1
  47. package/dist/component/affiliateRecovery/mutations.d.ts.map +1 -1
  48. package/dist/component/affiliateRecovery/mutations.js +4 -1
  49. package/dist/component/affiliateRecovery/mutations.js.map +1 -1
  50. package/dist/component/connect/mutations.d.ts.map +1 -1
  51. package/dist/component/connect/mutations.js +18 -2
  52. package/dist/component/connect/mutations.js.map +1 -1
  53. package/dist/component/connect/queries.d.ts +8 -0
  54. package/dist/component/connect/queries.d.ts.map +1 -1
  55. package/dist/component/connect/reversalClaims.d.ts.map +1 -1
  56. package/dist/component/connect/reversalClaims.js +7 -1
  57. package/dist/component/connect/reversalClaims.js.map +1 -1
  58. package/dist/component/connect/reversalReceipts.d.ts.map +1 -1
  59. package/dist/component/connect/reversalReceipts.js +2 -0
  60. package/dist/component/connect/reversalReceipts.js.map +1 -1
  61. package/dist/component/connect/schema.d.ts +11 -1
  62. package/dist/component/connect/schema.d.ts.map +1 -1
  63. package/dist/component/connect/schema.js +9 -1
  64. package/dist/component/connect/schema.js.map +1 -1
  65. package/dist/component/connect/validators.d.ts +9 -1
  66. package/dist/component/connect/validators.d.ts.map +1 -1
  67. package/dist/component/connect/validators.js +2 -0
  68. package/dist/component/connect/validators.js.map +1 -1
  69. package/dist/component/earnings/triggers.d.ts +426 -2
  70. package/dist/component/earnings/triggers.d.ts.map +1 -1
  71. package/dist/component/earnings/triggers.js +7 -1
  72. package/dist/component/earnings/triggers.js.map +1 -1
  73. package/dist/component/financialObservations/continuation.d.ts +7 -0
  74. package/dist/component/financialObservations/continuation.d.ts.map +1 -0
  75. package/dist/component/financialObservations/continuation.js +131 -0
  76. package/dist/component/financialObservations/continuation.js.map +1 -0
  77. package/dist/component/financialObservations/generations.d.ts +6 -0
  78. package/dist/component/financialObservations/generations.d.ts.map +1 -0
  79. package/dist/component/financialObservations/generations.js +46 -0
  80. package/dist/component/financialObservations/generations.js.map +1 -0
  81. package/dist/component/financialObservations/mutations.d.ts +62 -0
  82. package/dist/component/financialObservations/mutations.d.ts.map +1 -0
  83. package/dist/component/financialObservations/mutations.js +69 -0
  84. package/dist/component/financialObservations/mutations.js.map +1 -0
  85. package/dist/component/financialObservations/queries.d.ts +307 -0
  86. package/dist/component/financialObservations/queries.d.ts.map +1 -0
  87. package/dist/component/financialObservations/queries.js +219 -0
  88. package/dist/component/financialObservations/queries.js.map +1 -0
  89. package/dist/component/financialObservations/schema.d.ts +182 -0
  90. package/dist/component/financialObservations/schema.d.ts.map +1 -0
  91. package/dist/component/financialObservations/schema.js +53 -0
  92. package/dist/component/financialObservations/schema.js.map +1 -0
  93. package/dist/component/financialObservations/settlement.d.ts +7 -0
  94. package/dist/component/financialObservations/settlement.d.ts.map +1 -0
  95. package/dist/component/financialObservations/settlement.js +118 -0
  96. package/dist/component/financialObservations/settlement.js.map +1 -0
  97. package/dist/component/financialObservations/settlementHelpers.d.ts +46 -0
  98. package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -0
  99. package/dist/component/financialObservations/settlementHelpers.js +70 -0
  100. package/dist/component/financialObservations/settlementHelpers.js.map +1 -0
  101. package/dist/component/financialObservations/transferBatches.d.ts +12 -0
  102. package/dist/component/financialObservations/transferBatches.d.ts.map +1 -0
  103. package/dist/component/financialObservations/transferBatches.js +76 -0
  104. package/dist/component/financialObservations/transferBatches.js.map +1 -0
  105. package/dist/component/financialObservations/transferContinuation.d.ts +5 -0
  106. package/dist/component/financialObservations/transferContinuation.d.ts.map +1 -0
  107. package/dist/component/financialObservations/transferContinuation.js +35 -0
  108. package/dist/component/financialObservations/transferContinuation.js.map +1 -0
  109. package/dist/component/financialObservations/transferContinuationState.d.ts +8 -0
  110. package/dist/component/financialObservations/transferContinuationState.d.ts.map +1 -0
  111. package/dist/component/financialObservations/transferContinuationState.js +69 -0
  112. package/dist/component/financialObservations/transferContinuationState.js.map +1 -0
  113. package/dist/component/financialObservations/transferContinuationSteps.d.ts +5 -0
  114. package/dist/component/financialObservations/transferContinuationSteps.d.ts.map +1 -0
  115. package/dist/component/financialObservations/transferContinuationSteps.js +174 -0
  116. package/dist/component/financialObservations/transferContinuationSteps.js.map +1 -0
  117. package/dist/component/financialObservations/transferHelpers.d.ts +18 -0
  118. package/dist/component/financialObservations/transferHelpers.d.ts.map +1 -0
  119. package/dist/component/financialObservations/transferHelpers.js +37 -0
  120. package/dist/component/financialObservations/transferHelpers.js.map +1 -0
  121. package/dist/component/financialObservations/transferReferenceSchema.d.ts +56 -0
  122. package/dist/component/financialObservations/transferReferenceSchema.d.ts.map +1 -0
  123. package/dist/component/financialObservations/transferReferenceSchema.js +21 -0
  124. package/dist/component/financialObservations/transferReferenceSchema.js.map +1 -0
  125. package/dist/component/financialObservations/transferSchema.d.ts +237 -0
  126. package/dist/component/financialObservations/transferSchema.d.ts.map +1 -0
  127. package/dist/component/financialObservations/transferSchema.js +65 -0
  128. package/dist/component/financialObservations/transferSchema.js.map +1 -0
  129. package/dist/component/financialObservations/transfers.d.ts +7 -0
  130. package/dist/component/financialObservations/transfers.d.ts.map +1 -0
  131. package/dist/component/financialObservations/transfers.js +79 -0
  132. package/dist/component/financialObservations/transfers.js.map +1 -0
  133. package/dist/component/financialOwnership/helpers.d.ts +37 -0
  134. package/dist/component/financialOwnership/helpers.d.ts.map +1 -0
  135. package/dist/component/financialOwnership/helpers.js +76 -0
  136. package/dist/component/financialOwnership/helpers.js.map +1 -0
  137. package/dist/component/financialOwnership/mutations.d.ts +28 -0
  138. package/dist/component/financialOwnership/mutations.d.ts.map +1 -0
  139. package/dist/component/financialOwnership/mutations.js +74 -0
  140. package/dist/component/financialOwnership/mutations.js.map +1 -0
  141. package/dist/component/financialOwnership/queries.d.ts +57 -0
  142. package/dist/component/financialOwnership/queries.d.ts.map +1 -0
  143. package/dist/component/financialOwnership/queries.js +50 -0
  144. package/dist/component/financialOwnership/queries.js.map +1 -0
  145. package/dist/component/financialOwnership/schema.d.ts +140 -0
  146. package/dist/component/financialOwnership/schema.d.ts.map +1 -0
  147. package/dist/component/financialOwnership/schema.js +35 -0
  148. package/dist/component/financialOwnership/schema.js.map +1 -0
  149. package/dist/component/payoutRelease/queries.d.ts +2 -2
  150. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  151. package/dist/component/refundHistory/commandBinding.d.ts +17 -0
  152. package/dist/component/refundHistory/commandBinding.d.ts.map +1 -0
  153. package/dist/component/refundHistory/commandBinding.js +41 -0
  154. package/dist/component/refundHistory/commandBinding.js.map +1 -0
  155. package/dist/component/refundHistory/commandEvidence.d.ts +50 -0
  156. package/dist/component/refundHistory/commandEvidence.d.ts.map +1 -0
  157. package/dist/component/refundHistory/commandEvidence.js +54 -0
  158. package/dist/component/refundHistory/commandEvidence.js.map +1 -0
  159. package/dist/component/refundHistory/commandEvidenceValidator.d.ts +89 -0
  160. package/dist/component/refundHistory/commandEvidenceValidator.d.ts.map +1 -0
  161. package/dist/component/refundHistory/commandEvidenceValidator.js +8 -0
  162. package/dist/component/refundHistory/commandEvidenceValidator.js.map +1 -0
  163. package/dist/component/refundHistory/commandFields.d.ts +8 -0
  164. package/dist/component/refundHistory/commandFields.d.ts.map +1 -0
  165. package/dist/component/refundHistory/commandFields.js +9 -0
  166. package/dist/component/refundHistory/commandFields.js.map +1 -0
  167. package/dist/component/refundHistory/mutations.d.ts +8 -0
  168. package/dist/component/refundHistory/mutations.d.ts.map +1 -0
  169. package/dist/component/refundHistory/mutations.js +25 -0
  170. package/dist/component/refundHistory/mutations.js.map +1 -0
  171. package/dist/component/refundHistory/queries.d.ts +51 -0
  172. package/dist/component/refundHistory/queries.d.ts.map +1 -1
  173. package/dist/component/refundHistory/queries.js +14 -0
  174. package/dist/component/refundHistory/queries.js.map +1 -1
  175. package/dist/component/schema.d.ts +243 -1
  176. package/dist/component/schema.js +12 -0
  177. package/dist/component/schema.js.map +1 -1
  178. package/dist/component/settlement/mutations.d.ts.map +1 -1
  179. package/dist/component/settlement/mutations.js +5 -1
  180. package/dist/component/settlement/mutations.js.map +1 -1
  181. package/dist/shared/refundCommandScope.d.ts +10 -0
  182. package/dist/shared/refundCommandScope.d.ts.map +1 -0
  183. package/dist/shared/refundCommandScope.js +9 -0
  184. package/dist/shared/refundCommandScope.js.map +1 -0
  185. package/docs/financial-observations.md +159 -0
  186. package/docs/financial-ownership.md +54 -0
  187. package/docs/refund-command-evidence.md +49 -0
  188. package/package.json +5 -2
  189. package/src/client/connect/refundCommandEvidence.ts +41 -0
  190. package/src/client/connect/refundCommandNormalization.ts +28 -0
  191. package/src/client/financialObservations.test.ts +109 -0
  192. package/src/client/financialObservations.ts +106 -0
  193. package/src/client/financialOwnership.test.ts +80 -0
  194. package/src/client/financialOwnership.ts +116 -0
  195. package/src/client/index.ts +2 -2
  196. package/src/client/observationPagination.ts +11 -0
  197. package/src/client/publicTypes.ts +37 -0
  198. package/src/client/readContracts.ts +21 -0
  199. package/src/client/refund-command-evidence.test.ts +68 -0
  200. package/src/client/refund-command-normalization.test.ts +61 -0
  201. package/src/client/transferContinuation.test.ts +94 -0
  202. package/src/client/transferObservations.test.ts +98 -0
  203. package/src/client/transferObservations.ts +180 -0
  204. package/src/component/_generated/api.ts +44 -0
  205. package/src/component/_generated/component.ts +590 -0
  206. package/src/component/affiliateRecovery/mutations.ts +10 -1
  207. package/src/component/connect/mutations.ts +29 -2
  208. package/src/component/connect/reversalClaims.ts +19 -1
  209. package/src/component/connect/reversalReceipts.ts +2 -0
  210. package/src/component/connect/schema.ts +9 -1
  211. package/src/component/connect/validators.ts +2 -0
  212. package/src/component/core/reset.test.ts +8 -1
  213. package/src/component/earnings/earnings-totals.test.ts +14 -10
  214. package/src/component/earnings/triggers.ts +9 -1
  215. package/src/component/financialObservations/continuation.test.ts +445 -0
  216. package/src/component/financialObservations/continuation.ts +159 -0
  217. package/src/component/financialObservations/generations.ts +53 -0
  218. package/src/component/financialObservations/mutations.ts +75 -0
  219. package/src/component/financialObservations/observation-boundaries.test.ts +466 -0
  220. package/src/component/financialObservations/queries.ts +246 -0
  221. package/src/component/financialObservations/schema.ts +68 -0
  222. package/src/component/financialObservations/settlement-observations.test.ts +86 -0
  223. package/src/component/financialObservations/settlement.ts +142 -0
  224. package/src/component/financialObservations/settlementHelpers.ts +92 -0
  225. package/src/component/financialObservations/transfer-batches.test.ts +61 -0
  226. package/src/component/financialObservations/transfer-boundaries.test.ts +116 -0
  227. package/src/component/financialObservations/transfer-continuation-guards.test.ts +428 -0
  228. package/src/component/financialObservations/transfer-continuation.test.ts +154 -0
  229. package/src/component/financialObservations/transfer-observations.test.ts +212 -0
  230. package/src/component/financialObservations/transfer-overflow.test.ts +181 -0
  231. package/src/component/financialObservations/transfer-writers.test.ts +162 -0
  232. package/src/component/financialObservations/transferBatches.ts +96 -0
  233. package/src/component/financialObservations/transferContinuation.ts +53 -0
  234. package/src/component/financialObservations/transferContinuationState.ts +85 -0
  235. package/src/component/financialObservations/transferContinuationSteps.ts +232 -0
  236. package/src/component/financialObservations/transferHelpers.ts +38 -0
  237. package/src/component/financialObservations/transferReferenceSchema.ts +20 -0
  238. package/src/component/financialObservations/transferSchema.ts +74 -0
  239. package/src/component/financialObservations/transfers.ts +99 -0
  240. package/src/component/financialOwnership/financial-ownership.test.ts +102 -0
  241. package/src/component/financialOwnership/helpers.ts +98 -0
  242. package/src/component/financialOwnership/mutations.ts +83 -0
  243. package/src/component/financialOwnership/ownership-boundaries.test.ts +308 -0
  244. package/src/component/financialOwnership/queries.ts +65 -0
  245. package/src/component/financialOwnership/schema.ts +35 -0
  246. package/src/component/refundHistory/command-evidence.test.ts +288 -0
  247. package/src/component/refundHistory/command-normalization-resource.test.ts +77 -0
  248. package/src/component/refundHistory/command-normalization.test.ts +229 -0
  249. package/src/component/refundHistory/command-provenance.test.ts +59 -0
  250. package/src/component/refundHistory/commandBinding.ts +54 -0
  251. package/src/component/refundHistory/commandEvidence.ts +71 -0
  252. package/src/component/refundHistory/commandEvidenceValidator.ts +18 -0
  253. package/src/component/refundHistory/commandFields.ts +9 -0
  254. package/src/component/refundHistory/mutations.ts +33 -0
  255. package/src/component/refundHistory/queries.ts +15 -0
  256. package/src/component/schema.ts +22 -0
  257. package/src/component/settlement/mutations.ts +5 -1
  258. package/src/shared/refundCommandScope.ts +19 -0
@@ -0,0 +1,288 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { expect, it } from "vitest";
4
+
5
+ import { api } from "../_generated/api.js";
6
+ import { schema, modules } from "../../test.js";
7
+
8
+ const read = makeFunctionReference<
9
+ "query",
10
+ {
11
+ commandKey: string;
12
+ accountId: string;
13
+ stripePaymentIntentId: string;
14
+ stripeChargeId: string;
15
+ currency: string;
16
+ },
17
+ {
18
+ status: string;
19
+ reason?: string;
20
+ refund?: {
21
+ stripeRefundId: string;
22
+ amount: number;
23
+ currency: string;
24
+ status: string;
25
+ };
26
+ }
27
+ >("refundHistory/queries:getRefundCommandEvidence");
28
+ const scope = {
29
+ commandKey: "dojo-refund:original_command:v1",
30
+ accountId: "acct_original",
31
+ stripePaymentIntentId: "pi_original",
32
+ stripeChargeId: "ch_original",
33
+ currency: "jpy",
34
+ };
35
+ const refund = {
36
+ stripeRefundId: "re_original",
37
+ accountId: scope.accountId,
38
+ stripePaymentIntentId: scope.stripePaymentIntentId,
39
+ stripeChargeId: scope.stripeChargeId,
40
+ amount: 500,
41
+ currency: scope.currency,
42
+ status: "pending" as const,
43
+ metadata: {
44
+ betterStripeRefundCommand: scope.commandKey,
45
+ betterStripeRefundFingerprint: "a".repeat(64),
46
+ },
47
+ };
48
+
49
+ it("observes original units and pending state through exact command evidence without declaring completed money", async () => {
50
+ const t = convexTest(schema, modules);
51
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
52
+ expect(await t.query(read, scope)).toMatchObject({
53
+ status: "observed",
54
+ refund: {
55
+ stripeRefundId: "re_original",
56
+ amount: 500,
57
+ currency: "jpy",
58
+ status: "pending",
59
+ },
60
+ });
61
+ });
62
+
63
+ it("holds exact-key uniqueness when an unnormalized legacy metadata duplicate exists", async () => {
64
+ const t = convexTest(schema, modules);
65
+ await t.run((ctx) =>
66
+ ctx.db.insert("refunds", { ...refund, stripeRefundId: "re_legacy" }),
67
+ );
68
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
69
+ expect(await t.query(read, scope)).toEqual({
70
+ status: "unavailable",
71
+ reason: "normalization_incomplete",
72
+ });
73
+ const actual = await t.run((ctx) => ctx.db.query("refunds").take(3));
74
+ expect(actual).toHaveLength(2);
75
+ expect(
76
+ actual.every(
77
+ (row) => row.metadata.betterStripeRefundCommand === scope.commandKey,
78
+ ),
79
+ ).toBe(true);
80
+ });
81
+
82
+ it("observes metadata enrichment of an already-read row independently of a history cursor", async () => {
83
+ const t = convexTest(schema, modules);
84
+ await t.mutation(api.connect.mutations.upsertRefund, {
85
+ ...refund,
86
+ metadata: {},
87
+ });
88
+ const first = await t.query(api.refundHistory.queries.paginateRefunds, {
89
+ accountId: scope.accountId,
90
+ stripePaymentIntentId: scope.stripePaymentIntentId,
91
+ paginationOpts: { numItems: 1, cursor: null },
92
+ });
93
+ expect(first.page[0]?.metadata).toEqual({});
94
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
95
+ expect(await t.query(read, scope)).toMatchObject({
96
+ status: "observed",
97
+ refund: { stripeRefundId: refund.stripeRefundId },
98
+ });
99
+ });
100
+
101
+ it("observes a later refund without restarting an exhausted historical scan", async () => {
102
+ const t = convexTest(schema, modules);
103
+ await t.mutation(api.connect.mutations.upsertRefund, {
104
+ ...refund,
105
+ stripeRefundId: "re_earlier",
106
+ metadata: {},
107
+ });
108
+ const first = await t.query(api.refundHistory.queries.paginateRefunds, {
109
+ accountId: scope.accountId,
110
+ stripePaymentIntentId: scope.stripePaymentIntentId,
111
+ paginationOpts: { numItems: 1, cursor: null },
112
+ });
113
+ const terminal = first.isDone
114
+ ? first
115
+ : await t.query(api.refundHistory.queries.paginateRefunds, {
116
+ accountId: scope.accountId,
117
+ stripePaymentIntentId: scope.stripePaymentIntentId,
118
+ paginationOpts: { numItems: 1, cursor: first.continueCursor },
119
+ });
120
+ expect(terminal.isDone).toBe(true);
121
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
122
+ expect(await t.query(read, scope)).toMatchObject({
123
+ status: "observed",
124
+ refund: { stripeRefundId: refund.stripeRefundId },
125
+ });
126
+ });
127
+
128
+ it("detects duplicates before filtering foreign scope into apparent unique evidence", async () => {
129
+ const t = convexTest(schema, modules);
130
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
131
+ await t.mutation(api.connect.mutations.upsertRefund, {
132
+ ...refund,
133
+ stripeRefundId: "re_foreign",
134
+ accountId: "acct_foreign",
135
+ });
136
+ expect(await t.query(read, scope)).toEqual({
137
+ status: "unavailable",
138
+ reason: "ambiguous",
139
+ });
140
+ });
141
+
142
+ it.each([
143
+ "accountId",
144
+ "stripePaymentIntentId",
145
+ "stripeChargeId",
146
+ "currency",
147
+ ] as const)("reports foreign %s evidence instead of missing", async (field) => {
148
+ const t = convexTest(schema, modules);
149
+ await t.mutation(api.connect.mutations.upsertRefund, {
150
+ ...refund,
151
+ [field]: field === "currency" ? "usd" : "foreign",
152
+ });
153
+ expect(await t.query(read, scope)).toEqual({
154
+ status: "unavailable",
155
+ reason: "scope_mismatch",
156
+ });
157
+ });
158
+
159
+ it("preserves the known key on an omitted-metadata partial upsert", async () => {
160
+ const t = convexTest(schema, modules);
161
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
162
+ const { metadata, ...partial } = refund;
163
+ void metadata;
164
+ await t.mutation(api.connect.mutations.upsertRefund, {
165
+ ...partial,
166
+ status: "succeeded",
167
+ });
168
+ expect(await t.query(read, scope)).toMatchObject({
169
+ status: "observed",
170
+ refund: { status: "succeeded" },
171
+ });
172
+ });
173
+
174
+ it.each([
175
+ {},
176
+ null,
177
+ { betterStripeRefundCommand: "changed_command_key_123" },
178
+ { betterStripeRefundCommand: false },
179
+ ])("latches conflicting explicit metadata enrichment %#", async (metadata) => {
180
+ const t = convexTest(schema, modules);
181
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
182
+ await t.mutation(api.connect.mutations.upsertRefund, { ...refund, metadata });
183
+ expect(await t.query(read, scope)).toEqual({
184
+ status: "unavailable",
185
+ reason: "correlation_conflict",
186
+ });
187
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
188
+ expect(await t.query(read, scope)).toEqual({
189
+ status: "unavailable",
190
+ reason: "correlation_conflict",
191
+ });
192
+ });
193
+
194
+ it("keeps absent indexed evidence explicitly unavailable", async () => {
195
+ const t = convexTest(schema, modules);
196
+ expect(await t.query(read, scope)).toEqual({
197
+ status: "unavailable",
198
+ reason: "missing",
199
+ });
200
+ });
201
+
202
+ it("normalizes legacy first-known metadata before applying contradictory new metadata", async () => {
203
+ const t = convexTest(schema, modules);
204
+ await t.run((ctx) => ctx.db.insert("refunds", refund));
205
+ await t.mutation(api.connect.mutations.upsertRefund, {
206
+ ...refund,
207
+ metadata: { betterStripeRefundCommand: "different_command_key_123" },
208
+ });
209
+ expect(await t.query(read, scope)).toEqual({
210
+ status: "unavailable",
211
+ reason: "correlation_conflict",
212
+ });
213
+ });
214
+
215
+ it("normalizes legacy metadata on a partial upsert without inventing provider-creation provenance", async () => {
216
+ const t = convexTest(schema, modules);
217
+ await t.run((ctx) => ctx.db.insert("refunds", refund));
218
+ const { metadata, ...partial } = refund;
219
+ void metadata;
220
+ await t.mutation(api.connect.mutations.upsertRefund, partial);
221
+ expect(await t.query(read, scope)).toMatchObject({ status: "observed" });
222
+ });
223
+
224
+ it("holds global legacy coverage even for an unrelated unnormalized refund", async () => {
225
+ const t = convexTest(schema, modules);
226
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
227
+ await t.run((ctx) =>
228
+ ctx.db.insert("refunds", {
229
+ ...refund,
230
+ stripeRefundId: "re_legacy_unrelated",
231
+ metadata: {},
232
+ }),
233
+ );
234
+ expect(await t.query(read, scope)).toEqual({
235
+ status: "unavailable",
236
+ reason: "normalization_incomplete",
237
+ });
238
+ });
239
+
240
+ it("reports actual legacy duplicates after both records have been re-observed", async () => {
241
+ const t = convexTest(schema, modules);
242
+ await t.run((ctx) =>
243
+ ctx.db.insert("refunds", { ...refund, stripeRefundId: "re_legacy" }),
244
+ );
245
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
246
+ expect(await t.query(read, scope)).toEqual({
247
+ status: "unavailable",
248
+ reason: "normalization_incomplete",
249
+ });
250
+ await t.mutation(api.connect.mutations.upsertRefund, {
251
+ ...refund,
252
+ stripeRefundId: "re_legacy",
253
+ });
254
+ expect(await t.query(read, scope)).toEqual({
255
+ status: "unavailable",
256
+ reason: "ambiguous",
257
+ });
258
+ });
259
+
260
+ it.each([0, -1, 0.5, NaN, Number.MAX_SAFE_INTEGER + 1])(
261
+ "keeps malformed observed amount %s unavailable",
262
+ async (amount) => {
263
+ const t = convexTest(schema, modules);
264
+ await t.mutation(api.connect.mutations.upsertRefund, { ...refund, amount });
265
+ expect(await t.query(read, scope)).toEqual({
266
+ status: "unavailable",
267
+ reason: "invalid_receipt",
268
+ });
269
+ },
270
+ );
271
+
272
+ it("rejects duplicated provider receipt identity even when its indexed command keys differ", async () => {
273
+ const t = convexTest(schema, modules);
274
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
275
+ await t.run((ctx) =>
276
+ ctx.db.insert("refunds", {
277
+ ...refund,
278
+ metadata: { betterStripeRefundCommand: "legacy_other_command_123" },
279
+ refundCommandNormalizationVersion: 1,
280
+ refundCommandKey: "legacy_other_command_123",
281
+ refundCommandObservedAt: Date.now(),
282
+ }),
283
+ );
284
+ expect(await t.query(read, scope)).toEqual({
285
+ status: "unavailable",
286
+ reason: "ambiguous",
287
+ });
288
+ });
@@ -0,0 +1,77 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { expect, it, vi } from "vitest";
4
+ import type { MutationCtx } from "../_generated/server.js";
5
+ import { api } from "../_generated/api.js";
6
+ import { schema, modules } from "../../test.js";
7
+ import { normalizeRefundCommandBindings } from "./mutations.js";
8
+
9
+ it("propagates a simulated resource failure atomically and keeps the legacy coverage hold", async () => {
10
+ const t = convexTest(schema, modules);
11
+ const scope = {
12
+ commandKey: "dojo-refund:resource_command:v1",
13
+ accountId: "acct_original",
14
+ stripePaymentIntentId: "pi_original",
15
+ stripeChargeId: "ch_original",
16
+ currency: "jpy",
17
+ };
18
+ const rows = await t.run(async (ctx) => {
19
+ const ids = [];
20
+ for (let i = 0; i < 2; i++)
21
+ ids.push(
22
+ await ctx.db.insert("refunds", {
23
+ stripeRefundId: `re_resource${i}`,
24
+ accountId: scope.accountId,
25
+ stripePaymentIntentId: scope.stripePaymentIntentId,
26
+ stripeChargeId: scope.stripeChargeId,
27
+ currency: scope.currency,
28
+ amount: 100,
29
+ status: "pending",
30
+ metadata: { betterStripeRefundCommand: scope.commandKey },
31
+ }),
32
+ );
33
+ return Promise.all(ids.map((id) => ctx.db.get("refunds", id)));
34
+ });
35
+ const failure = new Error(
36
+ "Simulated native resource limit; no real capacity claim",
37
+ );
38
+ const handler = (
39
+ normalizeRefundCommandBindings as unknown as {
40
+ _handler: (
41
+ ctx: MutationCtx,
42
+ args: { limit: number },
43
+ ) => Promise<{ normalized: number; hasMore: boolean }>;
44
+ }
45
+ )._handler;
46
+ await expect(
47
+ t.mutation(async (ctx) => {
48
+ const originalPatch = ctx.db.patch.bind(ctx.db);
49
+ const patch = vi
50
+ .spyOn(ctx.db, "patch")
51
+ .mockImplementationOnce(originalPatch)
52
+ .mockRejectedValueOnce(failure);
53
+ try {
54
+ return await handler(ctx, { limit: 2 });
55
+ } finally {
56
+ patch.mockRestore();
57
+ }
58
+ }),
59
+ ).rejects.toBe(failure);
60
+ expect(
61
+ await t.run((ctx) =>
62
+ Promise.all(rows.map((row) => ctx.db.get("refunds", row!._id))),
63
+ ),
64
+ ).toEqual(rows);
65
+ expect(
66
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
67
+ ).toEqual({ status: "unavailable", reason: "normalization_incomplete" });
68
+ const normalize = makeFunctionReference<
69
+ "mutation",
70
+ { limit: number },
71
+ { normalized: number; hasMore: boolean }
72
+ >("refundHistory/mutations:normalizeRefundCommandBindings");
73
+ expect(await t.mutation(normalize, { limit: 1 })).toEqual({
74
+ normalized: 1,
75
+ hasMore: true,
76
+ });
77
+ });
@@ -0,0 +1,229 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { expect, it } from "vitest";
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
6
+
7
+ const normalize = makeFunctionReference<
8
+ "mutation",
9
+ { limit: number },
10
+ { normalized: number; hasMore: boolean }
11
+ >("refundHistory/mutations:normalizeRefundCommandBindings");
12
+ const scope = {
13
+ commandKey: "dojo-refund:legacy_command:v1",
14
+ accountId: "acct_original",
15
+ stripePaymentIntentId: "pi_original",
16
+ stripeChargeId: "ch_original",
17
+ currency: "jpy",
18
+ };
19
+ const refund = {
20
+ stripeRefundId: "re_legacy",
21
+ accountId: scope.accountId,
22
+ stripePaymentIntentId: scope.stripePaymentIntentId,
23
+ stripeChargeId: scope.stripeChargeId,
24
+ currency: scope.currency,
25
+ amount: 500,
26
+ status: "pending" as const,
27
+ metadata: { betterStripeRefundCommand: scope.commandKey },
28
+ };
29
+
30
+ it("drains only the bounded pending rows across multiple invocations", async () => {
31
+ const t = convexTest(schema, modules);
32
+ const ids = await t.run(async (ctx) => {
33
+ const ids = [];
34
+ for (let i = 0; i < 3; i++)
35
+ ids.push(
36
+ await ctx.db.insert("refunds", {
37
+ ...refund,
38
+ stripeRefundId: `re_legacy${i}`,
39
+ }),
40
+ );
41
+ return ids;
42
+ });
43
+ expect(await t.mutation(normalize, { limit: 2 })).toEqual({
44
+ normalized: 2,
45
+ hasMore: true,
46
+ });
47
+ const rows = await t.run(async (ctx) =>
48
+ Promise.all(ids.map((id) => ctx.db.get("refunds", id))),
49
+ );
50
+ expect(
51
+ rows.filter((row) => row?.refundCommandNormalizationVersion === 1),
52
+ ).toHaveLength(2);
53
+ expect(await t.mutation(normalize, { limit: 2 })).toEqual({
54
+ normalized: 1,
55
+ hasMore: false,
56
+ });
57
+ expect(await t.mutation(normalize, { limit: 2 })).toEqual({
58
+ normalized: 0,
59
+ hasMore: false,
60
+ });
61
+ });
62
+
63
+ it.each([0, -1, 101, 1.5, NaN, Infinity])(
64
+ "rejects invalid limit %s before modifying rows",
65
+ async (limit) => {
66
+ const t = convexTest(schema, modules);
67
+ const id = await t.run((ctx) => ctx.db.insert("refunds", refund));
68
+ const before = await t.run((ctx) => ctx.db.get("refunds", id));
69
+ await expect(t.mutation(normalize, { limit })).rejects.toThrow();
70
+ expect(await t.run((ctx) => ctx.db.get("refunds", id))).toEqual(before);
71
+ },
72
+ );
73
+
74
+ it("keeps old plus new duplicates held until normalization, then ambiguous", async () => {
75
+ const t = convexTest(schema, modules);
76
+ await t.run((ctx) => ctx.db.insert("refunds", refund));
77
+ await t.mutation(api.connect.mutations.upsertRefund, {
78
+ ...refund,
79
+ stripeRefundId: "re_new",
80
+ });
81
+ expect(
82
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
83
+ ).toEqual({ status: "unavailable", reason: "normalization_incomplete" });
84
+ expect(await t.mutation(normalize, { limit: 1 })).toEqual({
85
+ normalized: 1,
86
+ hasMore: false,
87
+ });
88
+ expect(
89
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
90
+ ).toEqual({ status: "unavailable", reason: "ambiguous" });
91
+ });
92
+
93
+ it.each([
94
+ undefined,
95
+ {},
96
+ { betterStripeRefundCommand: 123 },
97
+ { betterStripeRefundCommand: scope.commandKey },
98
+ ])(
99
+ "normalizes current legacy metadata without touching original financial fields: %j",
100
+ async (metadata) => {
101
+ const t = convexTest(schema, modules);
102
+ const id = await t.run((ctx) =>
103
+ ctx.db.insert("refunds", { ...refund, metadata }),
104
+ );
105
+ const before = await t.run((ctx) => ctx.db.get("refunds", id));
106
+ const started = Date.now();
107
+ await t.mutation(normalize, { limit: 1 });
108
+ const after = await t.run((ctx) => ctx.db.get("refunds", id));
109
+ const {
110
+ refundCommandNormalizationVersion,
111
+ refundCommandKey,
112
+ refundCommandConflict,
113
+ refundCommandObservedAt,
114
+ ...raw
115
+ } = after!;
116
+ expect(raw).toEqual(before);
117
+ expect(refundCommandNormalizationVersion).toBe(1);
118
+ expect(refundCommandConflict).toBe(
119
+ metadata?.betterStripeRefundCommand === 123,
120
+ );
121
+ if (typeof metadata?.betterStripeRefundCommand === "string") {
122
+ expect(refundCommandKey).toBe(scope.commandKey);
123
+ expect(refundCommandObservedAt).toBeGreaterThanOrEqual(started);
124
+ expect(refundCommandObservedAt).toBeLessThanOrEqual(Date.now());
125
+ } else {
126
+ expect(refundCommandKey).toBeUndefined();
127
+ expect(refundCommandObservedAt).toBeUndefined();
128
+ }
129
+ await t.mutation(normalize, { limit: 1 });
130
+ expect(await t.run((ctx) => ctx.db.get("refunds", id))).toEqual(after);
131
+ },
132
+ );
133
+
134
+ it("preserves first-known projection and latches contradictory legacy raw metadata", async () => {
135
+ const t = convexTest(schema, modules);
136
+ const id = await t.run((ctx) =>
137
+ ctx.db.insert("refunds", {
138
+ ...refund,
139
+ refundCommandKey: "dojo-refund:older_command:v1",
140
+ refundCommandObservedAt: 42,
141
+ }),
142
+ );
143
+ await t.mutation(normalize, { limit: 1 });
144
+ expect(await t.run((ctx) => ctx.db.get("refunds", id))).toMatchObject({
145
+ refundCommandKey: "dojo-refund:older_command:v1",
146
+ refundCommandObservedAt: 42,
147
+ refundCommandConflict: true,
148
+ metadata: refund.metadata,
149
+ });
150
+ });
151
+
152
+ it("retains late enrichment and conflict behavior after a missing-metadata legacy drain", async () => {
153
+ const t = convexTest(schema, modules);
154
+ await t.run((ctx) => ctx.db.insert("refunds", { ...refund, metadata: {} }));
155
+ await t.mutation(normalize, { limit: 1 });
156
+ expect(
157
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
158
+ ).toEqual({ status: "unavailable", reason: "missing" });
159
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
160
+ expect(
161
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
162
+ ).toMatchObject({ status: "observed" });
163
+ await t.mutation(api.connect.mutations.upsertRefund, {
164
+ ...refund,
165
+ metadata: {},
166
+ });
167
+ expect(
168
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
169
+ ).toEqual({ status: "unavailable", reason: "correlation_conflict" });
170
+ });
171
+
172
+ it("serializes concurrent bounded drains and never rewrites normalized rows", async () => {
173
+ const t = convexTest(schema, modules);
174
+ const ids = await t.run(async (ctx) => {
175
+ const ids = [];
176
+ for (let i = 0; i < 3; i++)
177
+ ids.push(
178
+ await ctx.db.insert("refunds", {
179
+ ...refund,
180
+ stripeRefundId: `re_concurrent${i}`,
181
+ }),
182
+ );
183
+ return ids;
184
+ });
185
+ const results = await Promise.all([
186
+ t.mutation(normalize, { limit: 2 }),
187
+ t.mutation(normalize, { limit: 2 }),
188
+ ]);
189
+ expect(results.reduce((total, result) => total + result.normalized, 0)).toBe(
190
+ 3,
191
+ );
192
+ for (const id of ids)
193
+ expect(await t.run((ctx) => ctx.db.get("refunds", id))).toMatchObject({
194
+ refundCommandNormalizationVersion: 1,
195
+ });
196
+ expect(await t.mutation(normalize, { limit: 1 })).toEqual({
197
+ normalized: 0,
198
+ hasMore: false,
199
+ });
200
+ });
201
+
202
+ it("accepts the maximum batch and continues one remaining row without a lifetime cap", async () => {
203
+ const t = convexTest(schema, modules);
204
+ await t.run(async (ctx) => {
205
+ for (let i = 0; i < 101; i++)
206
+ await ctx.db.insert("refunds", {
207
+ ...refund,
208
+ stripeRefundId: `re_max${i}`,
209
+ });
210
+ });
211
+ expect(await t.mutation(normalize, { limit: 100 })).toEqual({
212
+ normalized: 100,
213
+ hasMore: true,
214
+ });
215
+ expect(await t.mutation(normalize, { limit: 1 })).toEqual({
216
+ normalized: 1,
217
+ hasMore: false,
218
+ });
219
+ await t.run((ctx) =>
220
+ ctx.db.insert("refunds", { ...refund, stripeRefundId: "re_late" }),
221
+ );
222
+ expect(
223
+ await t.query(api.refundHistory.queries.getRefundCommandEvidence, scope),
224
+ ).toEqual({ status: "unavailable", reason: "normalization_incomplete" });
225
+ expect(await t.mutation(normalize, { limit: 1 })).toEqual({
226
+ normalized: 1,
227
+ hasMore: false,
228
+ });
229
+ });
@@ -0,0 +1,59 @@
1
+ import { convexTest } from "convex-test";
2
+ import { expect, it } from "vitest";
3
+ import { api } from "../_generated/api.js";
4
+ import { schema, modules } from "../../test.js";
5
+ import { refundCommandBinding } from "./commandBinding.js";
6
+ const key = "dojo-refund:provenance_command:v1";
7
+ const metadata = { betterStripeRefundCommand: key };
8
+ const scope = {
9
+ commandKey: key,
10
+ accountId: "acct_original",
11
+ stripePaymentIntentId: "pi_original",
12
+ stripeChargeId: "ch_original",
13
+ currency: "jpy",
14
+ };
15
+ const read = api.refundHistory.queries.getRefundCommandEvidence;
16
+
17
+ it("preserves first observed binding time without treating legacy insertion as provider creation", () => {
18
+ const first = refundCommandBinding({ metadata }, {}, 2000);
19
+ expect(first.refundCommandObservedAt).toBe(2000);
20
+ const later = refundCommandBinding(
21
+ { ...first, metadata },
22
+ { metadata },
23
+ 3000,
24
+ );
25
+ expect(later.refundCommandObservedAt).toBe(2000);
26
+ });
27
+
28
+ it.each([-1, NaN, Infinity, Number.MAX_SAFE_INTEGER])(
29
+ "holds malformed or future observed binding time %s",
30
+ async (observedAt) => {
31
+ const t = convexTest(schema, modules);
32
+ await t.mutation(api.connect.mutations.upsertRefund, {
33
+ stripeRefundId: "re_original",
34
+ accountId: scope.accountId,
35
+ stripePaymentIntentId: scope.stripePaymentIntentId,
36
+ stripeChargeId: scope.stripeChargeId,
37
+ currency: "jpy",
38
+ amount: 125,
39
+ status: "succeeded",
40
+ metadata,
41
+ });
42
+ await t.run(async (ctx) => {
43
+ const row = await ctx.db
44
+ .query("refunds")
45
+ .withIndex("by_stripe_refund_id", (q) =>
46
+ q.eq("stripeRefundId", "re_original"),
47
+ )
48
+ .unique();
49
+ if (!row) throw new Error("Missing QA refund");
50
+ await ctx.db.patch("refunds", row._id, {
51
+ refundCommandObservedAt: observedAt,
52
+ });
53
+ });
54
+ expect(await t.query(read, scope)).toEqual({
55
+ status: "unavailable",
56
+ reason: "invalid_receipt",
57
+ });
58
+ },
59
+ );