@fantastic.dev/better-stripe 0.33.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 (314) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +30 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +522 -2
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +261 -1
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +522 -2
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +261 -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/core/accountClosures.d.ts +15 -0
  20. package/dist/client/core/accountClosures.d.ts.map +1 -0
  21. package/dist/client/core/accountClosures.js +10 -0
  22. package/dist/client/core/accountClosures.js.map +1 -0
  23. package/dist/client/core/accounts.d.ts.map +1 -1
  24. package/dist/client/core/accounts.js +15 -23
  25. package/dist/client/core/accounts.js.map +1 -1
  26. package/dist/client/core/personalAccountMapping.d.ts +8 -0
  27. package/dist/client/core/personalAccountMapping.d.ts.map +1 -0
  28. package/dist/client/core/personalAccountMapping.js +11 -0
  29. package/dist/client/core/personalAccountMapping.js.map +1 -0
  30. package/dist/client/financialObservations.d.ts +42 -0
  31. package/dist/client/financialObservations.d.ts.map +1 -0
  32. package/dist/client/financialObservations.js +58 -0
  33. package/dist/client/financialObservations.js.map +1 -0
  34. package/dist/client/financialOwnership.d.ts +52 -0
  35. package/dist/client/financialOwnership.d.ts.map +1 -0
  36. package/dist/client/financialOwnership.js +72 -0
  37. package/dist/client/financialOwnership.js.map +1 -0
  38. package/dist/client/index.d.ts +57 -4
  39. package/dist/client/index.d.ts.map +1 -1
  40. package/dist/client/index.js +15 -2
  41. package/dist/client/index.js.map +1 -1
  42. package/dist/client/observationPagination.d.ts +10 -0
  43. package/dist/client/observationPagination.d.ts.map +1 -0
  44. package/dist/client/observationPagination.js +12 -0
  45. package/dist/client/observationPagination.js.map +1 -0
  46. package/dist/client/publicTypes.d.ts +10 -0
  47. package/dist/client/publicTypes.d.ts.map +1 -0
  48. package/dist/client/publicTypes.js +2 -0
  49. package/dist/client/publicTypes.js.map +1 -0
  50. package/dist/client/readContracts.d.ts +29 -0
  51. package/dist/client/readContracts.d.ts.map +1 -1
  52. package/dist/client/readContracts.js +10 -0
  53. package/dist/client/readContracts.js.map +1 -1
  54. package/dist/client/transferObservations.d.ts +59 -0
  55. package/dist/client/transferObservations.d.ts.map +1 -0
  56. package/dist/client/transferObservations.js +82 -0
  57. package/dist/client/transferObservations.js.map +1 -0
  58. package/dist/component/_generated/component.d.ts +594 -0
  59. package/dist/component/_generated/component.d.ts.map +1 -1
  60. package/dist/component/accountClosure/helpers.d.ts +18 -0
  61. package/dist/component/accountClosure/helpers.d.ts.map +1 -0
  62. package/dist/component/accountClosure/helpers.js +18 -0
  63. package/dist/component/accountClosure/helpers.js.map +1 -0
  64. package/dist/component/accountClosure/mutations.d.ts +20 -0
  65. package/dist/component/accountClosure/mutations.d.ts.map +1 -0
  66. package/dist/component/accountClosure/mutations.js +55 -0
  67. package/dist/component/accountClosure/mutations.js.map +1 -0
  68. package/dist/component/accountClosure/queries.d.ts +34 -0
  69. package/dist/component/accountClosure/queries.d.ts.map +1 -0
  70. package/dist/component/accountClosure/queries.js +36 -0
  71. package/dist/component/accountClosure/queries.js.map +1 -0
  72. package/dist/component/accountClosure/schema.d.ts +46 -0
  73. package/dist/component/accountClosure/schema.d.ts.map +1 -0
  74. package/dist/component/accountClosure/schema.js +20 -0
  75. package/dist/component/accountClosure/schema.js.map +1 -0
  76. package/dist/component/affiliateRecovery/mutations.d.ts.map +1 -1
  77. package/dist/component/affiliateRecovery/mutations.js +4 -1
  78. package/dist/component/affiliateRecovery/mutations.js.map +1 -1
  79. package/dist/component/connect/mutations.d.ts.map +1 -1
  80. package/dist/component/connect/mutations.js +18 -2
  81. package/dist/component/connect/mutations.js.map +1 -1
  82. package/dist/component/connect/queries.d.ts +8 -0
  83. package/dist/component/connect/queries.d.ts.map +1 -1
  84. package/dist/component/connect/reversalClaims.d.ts.map +1 -1
  85. package/dist/component/connect/reversalClaims.js +7 -1
  86. package/dist/component/connect/reversalClaims.js.map +1 -1
  87. package/dist/component/connect/reversalReceipts.d.ts.map +1 -1
  88. package/dist/component/connect/reversalReceipts.js +2 -0
  89. package/dist/component/connect/reversalReceipts.js.map +1 -1
  90. package/dist/component/connect/schema.d.ts +11 -1
  91. package/dist/component/connect/schema.d.ts.map +1 -1
  92. package/dist/component/connect/schema.js +9 -1
  93. package/dist/component/connect/schema.js.map +1 -1
  94. package/dist/component/connect/validators.d.ts +9 -1
  95. package/dist/component/connect/validators.d.ts.map +1 -1
  96. package/dist/component/connect/validators.js +2 -0
  97. package/dist/component/connect/validators.js.map +1 -1
  98. package/dist/component/core/mutations.d.ts.map +1 -1
  99. package/dist/component/core/mutations.js +5 -0
  100. package/dist/component/core/mutations.js.map +1 -1
  101. package/dist/component/core/personalAccountMapping.d.ts +95 -0
  102. package/dist/component/core/personalAccountMapping.d.ts.map +1 -0
  103. package/dist/component/core/personalAccountMapping.js +54 -0
  104. package/dist/component/core/personalAccountMapping.js.map +1 -0
  105. package/dist/component/core/queries.d.ts +27 -0
  106. package/dist/component/core/queries.d.ts.map +1 -1
  107. package/dist/component/core/queries.js +7 -0
  108. package/dist/component/core/queries.js.map +1 -1
  109. package/dist/component/earnings/triggers.d.ts +468 -2
  110. package/dist/component/earnings/triggers.d.ts.map +1 -1
  111. package/dist/component/earnings/triggers.js +7 -1
  112. package/dist/component/earnings/triggers.js.map +1 -1
  113. package/dist/component/financialObservations/continuation.d.ts +7 -0
  114. package/dist/component/financialObservations/continuation.d.ts.map +1 -0
  115. package/dist/component/financialObservations/continuation.js +131 -0
  116. package/dist/component/financialObservations/continuation.js.map +1 -0
  117. package/dist/component/financialObservations/generations.d.ts +6 -0
  118. package/dist/component/financialObservations/generations.d.ts.map +1 -0
  119. package/dist/component/financialObservations/generations.js +46 -0
  120. package/dist/component/financialObservations/generations.js.map +1 -0
  121. package/dist/component/financialObservations/mutations.d.ts +62 -0
  122. package/dist/component/financialObservations/mutations.d.ts.map +1 -0
  123. package/dist/component/financialObservations/mutations.js +69 -0
  124. package/dist/component/financialObservations/mutations.js.map +1 -0
  125. package/dist/component/financialObservations/queries.d.ts +307 -0
  126. package/dist/component/financialObservations/queries.d.ts.map +1 -0
  127. package/dist/component/financialObservations/queries.js +219 -0
  128. package/dist/component/financialObservations/queries.js.map +1 -0
  129. package/dist/component/financialObservations/schema.d.ts +182 -0
  130. package/dist/component/financialObservations/schema.d.ts.map +1 -0
  131. package/dist/component/financialObservations/schema.js +53 -0
  132. package/dist/component/financialObservations/schema.js.map +1 -0
  133. package/dist/component/financialObservations/settlement.d.ts +7 -0
  134. package/dist/component/financialObservations/settlement.d.ts.map +1 -0
  135. package/dist/component/financialObservations/settlement.js +118 -0
  136. package/dist/component/financialObservations/settlement.js.map +1 -0
  137. package/dist/component/financialObservations/settlementHelpers.d.ts +46 -0
  138. package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -0
  139. package/dist/component/financialObservations/settlementHelpers.js +70 -0
  140. package/dist/component/financialObservations/settlementHelpers.js.map +1 -0
  141. package/dist/component/financialObservations/transferBatches.d.ts +12 -0
  142. package/dist/component/financialObservations/transferBatches.d.ts.map +1 -0
  143. package/dist/component/financialObservations/transferBatches.js +76 -0
  144. package/dist/component/financialObservations/transferBatches.js.map +1 -0
  145. package/dist/component/financialObservations/transferContinuation.d.ts +5 -0
  146. package/dist/component/financialObservations/transferContinuation.d.ts.map +1 -0
  147. package/dist/component/financialObservations/transferContinuation.js +35 -0
  148. package/dist/component/financialObservations/transferContinuation.js.map +1 -0
  149. package/dist/component/financialObservations/transferContinuationState.d.ts +8 -0
  150. package/dist/component/financialObservations/transferContinuationState.d.ts.map +1 -0
  151. package/dist/component/financialObservations/transferContinuationState.js +69 -0
  152. package/dist/component/financialObservations/transferContinuationState.js.map +1 -0
  153. package/dist/component/financialObservations/transferContinuationSteps.d.ts +5 -0
  154. package/dist/component/financialObservations/transferContinuationSteps.d.ts.map +1 -0
  155. package/dist/component/financialObservations/transferContinuationSteps.js +174 -0
  156. package/dist/component/financialObservations/transferContinuationSteps.js.map +1 -0
  157. package/dist/component/financialObservations/transferHelpers.d.ts +18 -0
  158. package/dist/component/financialObservations/transferHelpers.d.ts.map +1 -0
  159. package/dist/component/financialObservations/transferHelpers.js +37 -0
  160. package/dist/component/financialObservations/transferHelpers.js.map +1 -0
  161. package/dist/component/financialObservations/transferReferenceSchema.d.ts +56 -0
  162. package/dist/component/financialObservations/transferReferenceSchema.d.ts.map +1 -0
  163. package/dist/component/financialObservations/transferReferenceSchema.js +21 -0
  164. package/dist/component/financialObservations/transferReferenceSchema.js.map +1 -0
  165. package/dist/component/financialObservations/transferSchema.d.ts +237 -0
  166. package/dist/component/financialObservations/transferSchema.d.ts.map +1 -0
  167. package/dist/component/financialObservations/transferSchema.js +65 -0
  168. package/dist/component/financialObservations/transferSchema.js.map +1 -0
  169. package/dist/component/financialObservations/transfers.d.ts +7 -0
  170. package/dist/component/financialObservations/transfers.d.ts.map +1 -0
  171. package/dist/component/financialObservations/transfers.js +79 -0
  172. package/dist/component/financialObservations/transfers.js.map +1 -0
  173. package/dist/component/financialOwnership/helpers.d.ts +37 -0
  174. package/dist/component/financialOwnership/helpers.d.ts.map +1 -0
  175. package/dist/component/financialOwnership/helpers.js +76 -0
  176. package/dist/component/financialOwnership/helpers.js.map +1 -0
  177. package/dist/component/financialOwnership/mutations.d.ts +28 -0
  178. package/dist/component/financialOwnership/mutations.d.ts.map +1 -0
  179. package/dist/component/financialOwnership/mutations.js +74 -0
  180. package/dist/component/financialOwnership/mutations.js.map +1 -0
  181. package/dist/component/financialOwnership/queries.d.ts +57 -0
  182. package/dist/component/financialOwnership/queries.d.ts.map +1 -0
  183. package/dist/component/financialOwnership/queries.js +50 -0
  184. package/dist/component/financialOwnership/queries.js.map +1 -0
  185. package/dist/component/financialOwnership/schema.d.ts +140 -0
  186. package/dist/component/financialOwnership/schema.d.ts.map +1 -0
  187. package/dist/component/financialOwnership/schema.js +35 -0
  188. package/dist/component/financialOwnership/schema.js.map +1 -0
  189. package/dist/component/payoutRelease/queries.d.ts +2 -2
  190. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  191. package/dist/component/refundHistory/commandBinding.d.ts +17 -0
  192. package/dist/component/refundHistory/commandBinding.d.ts.map +1 -0
  193. package/dist/component/refundHistory/commandBinding.js +41 -0
  194. package/dist/component/refundHistory/commandBinding.js.map +1 -0
  195. package/dist/component/refundHistory/commandEvidence.d.ts +50 -0
  196. package/dist/component/refundHistory/commandEvidence.d.ts.map +1 -0
  197. package/dist/component/refundHistory/commandEvidence.js +54 -0
  198. package/dist/component/refundHistory/commandEvidence.js.map +1 -0
  199. package/dist/component/refundHistory/commandEvidenceValidator.d.ts +89 -0
  200. package/dist/component/refundHistory/commandEvidenceValidator.d.ts.map +1 -0
  201. package/dist/component/refundHistory/commandEvidenceValidator.js +8 -0
  202. package/dist/component/refundHistory/commandEvidenceValidator.js.map +1 -0
  203. package/dist/component/refundHistory/commandFields.d.ts +8 -0
  204. package/dist/component/refundHistory/commandFields.d.ts.map +1 -0
  205. package/dist/component/refundHistory/commandFields.js +9 -0
  206. package/dist/component/refundHistory/commandFields.js.map +1 -0
  207. package/dist/component/refundHistory/mutations.d.ts +8 -0
  208. package/dist/component/refundHistory/mutations.d.ts.map +1 -0
  209. package/dist/component/refundHistory/mutations.js +25 -0
  210. package/dist/component/refundHistory/mutations.js.map +1 -0
  211. package/dist/component/refundHistory/queries.d.ts +51 -0
  212. package/dist/component/refundHistory/queries.d.ts.map +1 -1
  213. package/dist/component/refundHistory/queries.js +14 -0
  214. package/dist/component/refundHistory/queries.js.map +1 -1
  215. package/dist/component/schema.d.ts +261 -1
  216. package/dist/component/schema.js +14 -0
  217. package/dist/component/schema.js.map +1 -1
  218. package/dist/component/settlement/mutations.d.ts.map +1 -1
  219. package/dist/component/settlement/mutations.js +5 -1
  220. package/dist/component/settlement/mutations.js.map +1 -1
  221. package/dist/shared/refundCommandScope.d.ts +10 -0
  222. package/dist/shared/refundCommandScope.d.ts.map +1 -0
  223. package/dist/shared/refundCommandScope.js +9 -0
  224. package/dist/shared/refundCommandScope.js.map +1 -0
  225. package/docs/financial-observations.md +159 -0
  226. package/docs/financial-ownership.md +54 -0
  227. package/docs/personal-account-mapping.md +54 -0
  228. package/docs/refund-command-evidence.md +49 -0
  229. package/package.json +6 -2
  230. package/src/client/connect/refundCommandEvidence.ts +41 -0
  231. package/src/client/connect/refundCommandNormalization.ts +28 -0
  232. package/src/client/core/account-close-orchestration.test.ts +131 -0
  233. package/src/client/core/account-closure.test.ts +317 -0
  234. package/src/client/core/accountClosures.ts +37 -0
  235. package/src/client/core/accounts.test.ts +0 -111
  236. package/src/client/core/accounts.ts +24 -29
  237. package/src/client/core/personal-account-mapping.test.ts +71 -0
  238. package/src/client/core/personalAccountMapping.ts +23 -0
  239. package/src/client/financialObservations.test.ts +109 -0
  240. package/src/client/financialObservations.ts +106 -0
  241. package/src/client/financialOwnership.test.ts +80 -0
  242. package/src/client/financialOwnership.ts +116 -0
  243. package/src/client/index.ts +38 -35
  244. package/src/client/observationPagination.ts +11 -0
  245. package/src/client/publicTypes.ts +72 -0
  246. package/src/client/readContracts.ts +21 -0
  247. package/src/client/refund-command-evidence.test.ts +68 -0
  248. package/src/client/refund-command-normalization.test.ts +61 -0
  249. package/src/client/transferContinuation.test.ts +94 -0
  250. package/src/client/transferObservations.test.ts +98 -0
  251. package/src/client/transferObservations.ts +180 -0
  252. package/src/component/_generated/api.ts +52 -0
  253. package/src/component/_generated/component.ts +716 -0
  254. package/src/component/accountClosure/helpers.ts +21 -0
  255. package/src/component/accountClosure/mutations.ts +56 -0
  256. package/src/component/accountClosure/queries.ts +43 -0
  257. package/src/component/accountClosure/schema.ts +20 -0
  258. package/src/component/affiliateRecovery/mutations.ts +10 -1
  259. package/src/component/connect/mutations.ts +29 -2
  260. package/src/component/connect/reversalClaims.ts +19 -1
  261. package/src/component/connect/reversalReceipts.ts +2 -0
  262. package/src/component/connect/schema.ts +9 -1
  263. package/src/component/connect/validators.ts +2 -0
  264. package/src/component/core/mutations.ts +3 -0
  265. package/src/component/core/personal-account-mapping.test.ts +173 -0
  266. package/src/component/core/personalAccountMapping.ts +81 -0
  267. package/src/component/core/queries.ts +11 -0
  268. package/src/component/core/reset.test.ts +8 -1
  269. package/src/component/earnings/earnings-totals.test.ts +14 -10
  270. package/src/component/earnings/triggers.ts +9 -1
  271. package/src/component/financialObservations/continuation.test.ts +445 -0
  272. package/src/component/financialObservations/continuation.ts +159 -0
  273. package/src/component/financialObservations/generations.ts +53 -0
  274. package/src/component/financialObservations/mutations.ts +75 -0
  275. package/src/component/financialObservations/observation-boundaries.test.ts +466 -0
  276. package/src/component/financialObservations/queries.ts +246 -0
  277. package/src/component/financialObservations/schema.ts +68 -0
  278. package/src/component/financialObservations/settlement-observations.test.ts +86 -0
  279. package/src/component/financialObservations/settlement.ts +142 -0
  280. package/src/component/financialObservations/settlementHelpers.ts +92 -0
  281. package/src/component/financialObservations/transfer-batches.test.ts +61 -0
  282. package/src/component/financialObservations/transfer-boundaries.test.ts +116 -0
  283. package/src/component/financialObservations/transfer-continuation-guards.test.ts +428 -0
  284. package/src/component/financialObservations/transfer-continuation.test.ts +154 -0
  285. package/src/component/financialObservations/transfer-observations.test.ts +212 -0
  286. package/src/component/financialObservations/transfer-overflow.test.ts +181 -0
  287. package/src/component/financialObservations/transfer-writers.test.ts +162 -0
  288. package/src/component/financialObservations/transferBatches.ts +96 -0
  289. package/src/component/financialObservations/transferContinuation.ts +53 -0
  290. package/src/component/financialObservations/transferContinuationState.ts +85 -0
  291. package/src/component/financialObservations/transferContinuationSteps.ts +232 -0
  292. package/src/component/financialObservations/transferHelpers.ts +38 -0
  293. package/src/component/financialObservations/transferReferenceSchema.ts +20 -0
  294. package/src/component/financialObservations/transferSchema.ts +74 -0
  295. package/src/component/financialObservations/transfers.ts +99 -0
  296. package/src/component/financialOwnership/financial-ownership.test.ts +102 -0
  297. package/src/component/financialOwnership/helpers.ts +98 -0
  298. package/src/component/financialOwnership/mutations.ts +83 -0
  299. package/src/component/financialOwnership/ownership-boundaries.test.ts +308 -0
  300. package/src/component/financialOwnership/queries.ts +65 -0
  301. package/src/component/financialOwnership/schema.ts +35 -0
  302. package/src/component/refundHistory/command-evidence.test.ts +288 -0
  303. package/src/component/refundHistory/command-normalization-resource.test.ts +77 -0
  304. package/src/component/refundHistory/command-normalization.test.ts +229 -0
  305. package/src/component/refundHistory/command-provenance.test.ts +59 -0
  306. package/src/component/refundHistory/commandBinding.ts +54 -0
  307. package/src/component/refundHistory/commandEvidence.ts +71 -0
  308. package/src/component/refundHistory/commandEvidenceValidator.ts +18 -0
  309. package/src/component/refundHistory/commandFields.ts +9 -0
  310. package/src/component/refundHistory/mutations.ts +33 -0
  311. package/src/component/refundHistory/queries.ts +15 -0
  312. package/src/component/schema.ts +24 -0
  313. package/src/component/settlement/mutations.ts +5 -1
  314. package/src/shared/refundCommandScope.ts +19 -0
@@ -0,0 +1,116 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { expect, it } from "vitest";
4
+ import { modules, schema } from "../../test.js";
5
+ import { observeTransfer } from "./transfers.js";
6
+ const upsert = makeFunctionReference<"mutation">(
7
+ "connect/mutations:upsertTransfer",
8
+ );
9
+ const source = makeFunctionReference<"query">(
10
+ "financialObservations/queries:transfer",
11
+ );
12
+ const bucket = makeFunctionReference<"query">(
13
+ "financialObservations/queries:transferBucket",
14
+ );
15
+ function input(i = 0) {
16
+ return {
17
+ stripeTransferId: `tr_${i}`,
18
+ destinationAccountId: "opaque",
19
+ amount: 1,
20
+ currency: "usd",
21
+ status: "paid" as const,
22
+ };
23
+ }
24
+ it("preserves ambiguous observation rows instead of choosing or repairing one", async () => {
25
+ const t = convexTest(schema, modules);
26
+ await t.mutation(upsert, input());
27
+ const f = await t.run(async (ctx) => {
28
+ const original = (
29
+ await ctx.db.query("financialTransferObservations").take(1)
30
+ )[0];
31
+ const { _id, _creationTime, ...fields } = original;
32
+ await ctx.db.insert("financialTransferObservations", fields);
33
+ return {
34
+ original,
35
+ rows: await ctx.db.query("financialTransferObservations").collect(),
36
+ };
37
+ });
38
+ await t.mutation(upsert, { ...input(), metadata: { late: true } });
39
+ expect(
40
+ await t.query(source, { transferId: f.original.transferId }),
41
+ ).toMatchObject({ coverage: "unknown", observation: null });
42
+ expect(
43
+ await t.run((ctx) =>
44
+ ctx.db.query("financialTransferObservations").collect(),
45
+ ),
46
+ ).toEqual(f.rows);
47
+ expect(
48
+ (await t.run((ctx) => ctx.db.get("transfers", f.original.transferId)))
49
+ ?.metadata,
50
+ ).toEqual({ late: true });
51
+ });
52
+ it("marks exhausted generations unknown while preserving the actual financial update", async () => {
53
+ const t = convexTest(schema, modules);
54
+ await t.mutation(upsert, input());
55
+ const row = await t.run(async (ctx) => {
56
+ const r = (await ctx.db.query("financialTransferObservations").take(1))[0];
57
+ await ctx.db.patch("financialTransferObservations", r._id, {
58
+ generation: Number.MAX_SAFE_INTEGER,
59
+ });
60
+ return r;
61
+ });
62
+ await t.mutation(upsert, { ...input(), metadata: { late: true } });
63
+ expect(await t.query(source, { transferId: row.transferId })).toMatchObject({
64
+ coverage: "unknown",
65
+ observation: null,
66
+ });
67
+ expect(
68
+ await t.run((ctx) => ctx.db.get("financialTransferObservations", row._id)),
69
+ ).toMatchObject({ generation: 0, isContradictory: true });
70
+ expect(
71
+ (await t.run((ctx) => ctx.db.get("transfers", row.transferId)))?.metadata,
72
+ ).toEqual({ late: true });
73
+ });
74
+ it("retains native cursor metadata and visits every exact bucket row across page bounds", async () => {
75
+ const t = convexTest(schema, modules);
76
+ await t.run(async (ctx) => {
77
+ for (let i = 0; i < 105; i++) {
78
+ const id = await ctx.db.insert("transfers", {
79
+ ...input(i),
80
+ totalsRecorded: true,
81
+ });
82
+ await observeTransfer(ctx, (await ctx.db.get("transfers", id))!);
83
+ }
84
+ });
85
+ const first = await t.query(bucket, {
86
+ bucketKey: "opaque",
87
+ paginationOpts: { numItems: 100, cursor: null, id: 19 },
88
+ });
89
+ expect(first.page).toHaveLength(100);
90
+ expect(first.isDone).toBe(false);
91
+ expect(first.continueCursor).toEqual(expect.any(String));
92
+ const next = await t.query(bucket, {
93
+ bucketKey: "opaque",
94
+ paginationOpts: { numItems: 100, cursor: first.continueCursor, id: 19 },
95
+ });
96
+ expect(next.page).toHaveLength(5);
97
+ expect(next.isDone).toBe(true);
98
+ expect(
99
+ new Set([...first.page, ...next.page].map((r) => r.transferId)).size,
100
+ ).toBe(105);
101
+ expect(first).toMatchObject({ coverage: "unknown", hasDeferredWork: false });
102
+ await expect(
103
+ t.query(bucket, {
104
+ bucketKey: "opaque",
105
+ paginationOpts: { numItems: 101, cursor: null },
106
+ }),
107
+ ).rejects.toThrow();
108
+ expect(
109
+ (
110
+ await t.query(bucket, {
111
+ bucketKey: "foreign",
112
+ paginationOpts: { numItems: 25, cursor: null },
113
+ })
114
+ ).page,
115
+ ).toEqual([]);
116
+ });
@@ -0,0 +1,428 @@
1
+ import { deferTransferFacts } from "./transferBatches.js";
2
+ import { convexTest } from "convex-test";
3
+ import { makeFunctionReference } from "convex/server";
4
+ import { expect, it } from "vitest";
5
+ import { schema, modules } from "../../test.js";
6
+ import { continueTransferBatch } from "./transferContinuation.js";
7
+ const resume = makeFunctionReference<"mutation">(
8
+ "financialObservations/mutations:resumeTransferBatch",
9
+ );
10
+ const upsert = makeFunctionReference<"mutation">(
11
+ "connect/mutations:upsertTransfer",
12
+ );
13
+ const register = makeFunctionReference<"mutation">(
14
+ "financialOwnership/mutations:register",
15
+ );
16
+ async function fixture() {
17
+ const t = convexTest(schema, modules);
18
+ const ids = await t.run(async (ctx) => {
19
+ const ids = [];
20
+ for (let i = 0; i < 101; i++)
21
+ ids.push(
22
+ await ctx.db.insert("transfers", {
23
+ stripeTransferId: `tr_${i}`,
24
+ sourceChargeId: "ch",
25
+ destinationAccountId: "opaque",
26
+ amount: 1,
27
+ currency: "usd",
28
+ status: "paid",
29
+ totalsRecorded: true,
30
+ }),
31
+ );
32
+ await ctx.db.insert("currencyTotals", {
33
+ accountId: "opaque",
34
+ currency: "usd",
35
+ kind: "earnings",
36
+ gross: 101,
37
+ reversed: 0,
38
+ paidOut: 0,
39
+ count: 101,
40
+ });
41
+ return ids;
42
+ });
43
+ await t.mutation(
44
+ makeFunctionReference<"mutation">("connect/mutations:claimReversalSlices"),
45
+ { operationId: "op", sourceChargeId: "ch", mode: { kind: "full" } },
46
+ );
47
+ const batch = await t.run(
48
+ async (ctx) => (await ctx.db.query("financialTransferBatches").take(1))[0],
49
+ );
50
+ return { t, ids, batch };
51
+ }
52
+ function fact() {
53
+ return {
54
+ stripeTransferId: "tr_0",
55
+ sourceChargeId: "ch",
56
+ destinationAccountId: "opaque",
57
+ amount: 1,
58
+ currency: "usd",
59
+ status: "paid",
60
+ };
61
+ }
62
+ it("holds instead of absorbing an inter-commit financial write", async () => {
63
+ const { t, batch } = await fixture();
64
+ const first = await t.mutation(resume, {
65
+ batchId: batch._id,
66
+ expectedGeneration: 1,
67
+ });
68
+ await t.mutation(upsert, { ...fact(), metadata: { late: true } });
69
+ const held = await t.mutation(resume, {
70
+ batchId: batch._id,
71
+ expectedGeneration: first.batch.generation,
72
+ });
73
+ expect(held).toMatchObject({
74
+ coverage: "unknown",
75
+ hasDeferredWork: true,
76
+ batch: {
77
+ state: "pending",
78
+ isContradictory: true,
79
+ continuation: {
80
+ state: "held",
81
+ nextReference: 50,
82
+ validatedReferences: 0,
83
+ },
84
+ },
85
+ });
86
+ });
87
+ it("preserves an explicit prior unknown after a late first registration", async () => {
88
+ const { t, ids, batch } = await fixture();
89
+ await t.mutation(upsert, fact());
90
+ await t.mutation(register, {
91
+ bucketKey: "opaque",
92
+ kind: "logical",
93
+ userId: "late_owner",
94
+ source: { integration: "host", recordId: "late" },
95
+ });
96
+ await t.mutation(resume, { batchId: batch._id, expectedGeneration: 1 });
97
+ const observation = await t.run(
98
+ async (ctx) =>
99
+ (
100
+ await ctx.db
101
+ .query("financialTransferObservations")
102
+ .withIndex("by_transfer_id", (q) => q.eq("transferId", ids[0]))
103
+ .take(2)
104
+ )[0],
105
+ );
106
+ expect(observation.ownershipStatus).toBe("unknown");
107
+ expect(observation.userId).toBeUndefined();
108
+ expect(observation.registrationId).toBeUndefined();
109
+ });
110
+ it("holds a missing original transfer without selecting a replacement", async () => {
111
+ const { t, ids, batch } = await fixture();
112
+ await t.run((ctx) => ctx.db.delete("transfers", ids[0]));
113
+ await expect(
114
+ t.mutation(resume, { batchId: batch._id, expectedGeneration: 1 }),
115
+ ).resolves.toMatchObject({
116
+ coverage: "unknown",
117
+ hasDeferredWork: true,
118
+ batch: {
119
+ isContradictory: true,
120
+ continuation: { state: "held", nextReference: 0 },
121
+ },
122
+ });
123
+ });
124
+ it("holds duplicate original references across indexed batches", async () => {
125
+ const { t, ids, batch } = await fixture();
126
+ const refs = [...ids];
127
+ refs[60] = refs[0];
128
+ await t.run((ctx) =>
129
+ ctx.db.patch("financialTransferBatches", batch._id, { transferIds: refs }),
130
+ );
131
+ const first = await t.mutation(resume, {
132
+ batchId: batch._id,
133
+ expectedGeneration: 1,
134
+ });
135
+ const held = await t.mutation(resume, {
136
+ batchId: batch._id,
137
+ expectedGeneration: first.batch.generation,
138
+ });
139
+ expect(held.batch.continuation.state).toBe("held");
140
+ expect(held.hasDeferredWork).toBe(true);
141
+ });
142
+ it("validates committed reference slots rather than advancing a counter over a gap", async () => {
143
+ const { t, batch } = await fixture();
144
+ let r = await t.mutation(resume, {
145
+ batchId: batch._id,
146
+ expectedGeneration: 1,
147
+ });
148
+ for (let i = 0; i < 2; i++)
149
+ r = await t.mutation(resume, {
150
+ batchId: batch._id,
151
+ expectedGeneration: r.batch.generation,
152
+ });
153
+ await t.run(async (ctx) => {
154
+ const row = (
155
+ await ctx.db
156
+ .query("financialTransferBatchReferences")
157
+ .withIndex("by_batch_position", (q) =>
158
+ q.eq("batchId", batch._id).eq("position", 0),
159
+ )
160
+ .take(1)
161
+ )[0];
162
+ await ctx.db.delete("financialTransferBatchReferences", row._id);
163
+ });
164
+ r = await t.mutation(resume, {
165
+ batchId: batch._id,
166
+ expectedGeneration: r.batch.generation,
167
+ });
168
+ expect(r.batch.continuation.state).toBe("held");
169
+ expect(r.hasDeferredWork).toBe(true);
170
+ });
171
+ it("rolls back every cursor/reference/observation write before commit and resumes safely", async () => {
172
+ const { t, batch } = await fixture();
173
+ const before = await t.run((ctx) =>
174
+ ctx.db.query("financialObservationGenerations").collect(),
175
+ );
176
+ await expect(
177
+ t.run(async (ctx) => {
178
+ await continueTransferBatch(ctx, batch, 1);
179
+ throw new Error("synthetic precommit interruption");
180
+ }),
181
+ ).rejects.toThrow("synthetic precommit interruption");
182
+ expect(
183
+ await t.run((ctx) =>
184
+ ctx.db.query("financialTransferBatchReferences").take(1),
185
+ ),
186
+ ).toEqual([]);
187
+ expect(
188
+ await t.run((ctx) => ctx.db.query("financialTransferObservations").take(1)),
189
+ ).toEqual([]);
190
+ expect(
191
+ await t.run((ctx) =>
192
+ ctx.db.query("financialObservationGenerations").collect(),
193
+ ),
194
+ ).toEqual(before);
195
+ expect(
196
+ await t.run((ctx) => ctx.db.get("financialTransferBatches", batch._id)),
197
+ ).toEqual(batch);
198
+ expect(
199
+ await t.mutation(resume, { batchId: batch._id, expectedGeneration: 1 }),
200
+ ).toMatchObject({ batch: { continuation: { nextReference: 50 } } });
201
+ });
202
+ it("holds contradictory observation identity instead of trusting cached hashes", async () => {
203
+ const { t, batch } = await fixture();
204
+ let r = await t.mutation(resume, {
205
+ batchId: batch._id,
206
+ expectedGeneration: 1,
207
+ });
208
+ for (let i = 0; i < 2; i++)
209
+ r = await t.mutation(resume, {
210
+ batchId: batch._id,
211
+ expectedGeneration: r.batch.generation,
212
+ });
213
+ await t.run(async (ctx) => {
214
+ const ref = (
215
+ await ctx.db
216
+ .query("financialTransferBatchReferences")
217
+ .withIndex("by_batch_position", (q) =>
218
+ q.eq("batchId", batch._id).eq("position", 0),
219
+ )
220
+ .take(1)
221
+ )[0];
222
+ await ctx.db.patch("financialTransferObservations", ref.observationId, {
223
+ bucketKey: "foreign",
224
+ });
225
+ });
226
+ const held = await t.mutation(resume, {
227
+ batchId: batch._id,
228
+ expectedGeneration: r.batch.generation,
229
+ });
230
+ expect(held.batch.continuation.state).toBe("held");
231
+ expect(held.hasDeferredWork).toBe(true);
232
+ });
233
+ it("refuses structurally impossible complete cursors before the historical early return", async () => {
234
+ const { t, batch } = await fixture();
235
+ const first = await t.mutation(resume, {
236
+ batchId: batch._id,
237
+ expectedGeneration: 1,
238
+ });
239
+ await t.run((ctx) =>
240
+ ctx.db.patch("financialTransferBatches", batch._id, {
241
+ state: "complete",
242
+ continuation: { ...first.batch.continuation, state: "complete" },
243
+ }),
244
+ );
245
+ expect(
246
+ await t.mutation(resume, { batchId: batch._id, expectedGeneration: 2 }),
247
+ ).toMatchObject({
248
+ coverage: "unknown",
249
+ hasDeferredWork: true,
250
+ batch: {
251
+ state: "pending",
252
+ isContradictory: true,
253
+ continuation: { state: "held", nextReference: 50 },
254
+ },
255
+ });
256
+ });
257
+ it("retains a completed historical traversal after later financial facts without claiming freshness", async () => {
258
+ const { t, batch } = await fixture();
259
+ let r = await t.mutation(resume, {
260
+ batchId: batch._id,
261
+ expectedGeneration: 1,
262
+ });
263
+ while (r.batch.state !== "complete")
264
+ r = await t.mutation(resume, {
265
+ batchId: batch._id,
266
+ expectedGeneration: r.batch.generation,
267
+ });
268
+ await t.mutation(upsert, { ...fact(), metadata: { later: true } });
269
+ const historic = await t.mutation(resume, {
270
+ batchId: batch._id,
271
+ expectedGeneration: r.batch.generation,
272
+ });
273
+ expect(historic.batch).toEqual(r.batch);
274
+ expect(historic.coverage).toBe("unknown");
275
+ expect(historic.producerGeneration).toBeGreaterThan(r.producerGeneration);
276
+ });
277
+ it("keeps registered pins when a contradictory replacement cannot prove the old owner", async () => {
278
+ const { t, batch, ids } = await fixture();
279
+ await t.mutation(register, {
280
+ bucketKey: "opaque",
281
+ kind: "logical",
282
+ userId: "original",
283
+ source: { integration: "host", recordId: "original" },
284
+ });
285
+ await t.mutation(upsert, fact());
286
+ await t.run(async (ctx) => {
287
+ const registration = (await ctx.db.query("financialOwnership").take(1))[0];
288
+ await ctx.db.patch("financialOwnership", registration._id, {
289
+ userId: "replacement",
290
+ });
291
+ });
292
+ const held = await t.mutation(resume, {
293
+ batchId: batch._id,
294
+ expectedGeneration: 1,
295
+ });
296
+ expect(held.batch.continuation.state).toBe("held");
297
+ const original = await t.run(
298
+ async (ctx) =>
299
+ (
300
+ await ctx.db
301
+ .query("financialTransferObservations")
302
+ .withIndex("by_transfer_id", (q) => q.eq("transferId", ids[0]))
303
+ .take(1)
304
+ )[0],
305
+ );
306
+ expect(original.userId).toBe("original");
307
+ });
308
+ it.each([0, 1.5, Number.MAX_SAFE_INTEGER + 1])(
309
+ "rejects invalid expected generation %s before progress",
310
+ async (expectedGeneration) => {
311
+ const { t, batch } = await fixture();
312
+ await expect(
313
+ t.mutation(resume, { batchId: batch._id, expectedGeneration }),
314
+ ).rejects.toThrow("Invalid observation generation");
315
+ expect(
316
+ await t.run((ctx) => ctx.db.get("financialTransferBatches", batch._id)),
317
+ ).toEqual(batch);
318
+ },
319
+ );
320
+
321
+ it("retains pending invalidation when a completed producer reference becomes contradictory", async () => {
322
+ const { t, batch, ids } = await fixture();
323
+ let r = await t.mutation(resume, {
324
+ batchId: batch._id,
325
+ expectedGeneration: 1,
326
+ });
327
+ while (r.batch.state !== "complete")
328
+ r = await t.mutation(resume, {
329
+ batchId: batch._id,
330
+ expectedGeneration: r.batch.generation,
331
+ });
332
+ await t.run(async (ctx) => {
333
+ const operation = (
334
+ await ctx.db
335
+ .query("transferReversalOps")
336
+ .withIndex("by_operation_id", (q) => q.eq("operationId", "op"))
337
+ .take(1)
338
+ )[0];
339
+ await deferTransferFacts(
340
+ ctx,
341
+ operation,
342
+ ids.slice(0, 100),
343
+ "transfer_frontier_claim",
344
+ );
345
+ });
346
+ r = await t.mutation(resume, {
347
+ batchId: batch._id,
348
+ expectedGeneration: r.batch.generation + 1,
349
+ });
350
+ expect(r).toMatchObject({
351
+ coverage: "unknown",
352
+ hasDeferredWork: true,
353
+ batch: {
354
+ state: "pending",
355
+ isContradictory: true,
356
+ referenceCount: 101,
357
+ continuation: {
358
+ state: "held",
359
+ nextReference: 101,
360
+ validatedReferences: 101,
361
+ },
362
+ },
363
+ });
364
+ expect(r.batch.transferIds).toEqual(ids);
365
+ });
366
+ it("holds duplicate producer markers without selecting an alternative source", async () => {
367
+ const { t, batch } = await fixture();
368
+ await t.run(async (ctx) => {
369
+ const { _id, _creationTime, ...fields } = batch;
370
+ await ctx.db.insert("financialTransferBatches", fields);
371
+ });
372
+ const r = await t.mutation(resume, {
373
+ batchId: batch._id,
374
+ expectedGeneration: 1,
375
+ });
376
+ expect(r.batch.continuation.state).toBe("held");
377
+ expect(r.hasDeferredWork).toBe(true);
378
+ expect(
379
+ await t.run((ctx) =>
380
+ ctx.db.query("financialTransferBatchReferences").take(1),
381
+ ),
382
+ ).toEqual([]);
383
+ });
384
+ it("holds a duplicated committed slot and preserves both receipts", async () => {
385
+ const { t, batch } = await fixture();
386
+ let r = await t.mutation(resume, {
387
+ batchId: batch._id,
388
+ expectedGeneration: 1,
389
+ });
390
+ for (let i = 0; i < 2; i++)
391
+ r = await t.mutation(resume, {
392
+ batchId: batch._id,
393
+ expectedGeneration: r.batch.generation,
394
+ });
395
+ const before = await t.run(async (ctx) => {
396
+ const original = (
397
+ await ctx.db
398
+ .query("financialTransferBatchReferences")
399
+ .withIndex("by_batch_position", (q) =>
400
+ q.eq("batchId", batch._id).eq("position", 0),
401
+ )
402
+ .take(1)
403
+ )[0];
404
+ const { _id, _creationTime, ...fields } = original;
405
+ await ctx.db.insert("financialTransferBatchReferences", fields);
406
+ return ctx.db
407
+ .query("financialTransferBatchReferences")
408
+ .withIndex("by_batch_position", (q) =>
409
+ q.eq("batchId", batch._id).eq("position", 0),
410
+ )
411
+ .take(3);
412
+ });
413
+ const held = await t.mutation(resume, {
414
+ batchId: batch._id,
415
+ expectedGeneration: r.batch.generation,
416
+ });
417
+ expect(held.batch.continuation.state).toBe("held");
418
+ expect(
419
+ await t.run((ctx) =>
420
+ ctx.db
421
+ .query("financialTransferBatchReferences")
422
+ .withIndex("by_batch_position", (q) =>
423
+ q.eq("batchId", batch._id).eq("position", 0),
424
+ )
425
+ .take(3),
426
+ ),
427
+ ).toEqual(before);
428
+ });
@@ -0,0 +1,154 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { expect, it } from "vitest";
4
+ import { modules, schema } from "../../test.js";
5
+ const resume = makeFunctionReference<"mutation">(
6
+ "financialObservations/mutations:resumeTransferBatch",
7
+ );
8
+ const batches = makeFunctionReference<"query">(
9
+ "financialObservations/queries:transferBatches",
10
+ );
11
+ const references = makeFunctionReference<"query">(
12
+ "financialObservations/queries:transferReferences",
13
+ );
14
+ const claim = makeFunctionReference<"mutation">(
15
+ "connect/mutations:claimReversalSlices",
16
+ );
17
+ const release = makeFunctionReference<"mutation">(
18
+ "connect/mutations:releaseReversalClaim",
19
+ );
20
+ async function fixture(kind: "claim" | "release") {
21
+ const t = convexTest(schema, modules);
22
+ await t.run(async (ctx) => {
23
+ for (let i = 0; i < 101; i++)
24
+ await ctx.db.insert("transfers", {
25
+ stripeTransferId: `tr_${i}`,
26
+ sourceChargeId: "ch",
27
+ destinationAccountId: "opaque",
28
+ amount: 1,
29
+ currency: "usd",
30
+ role: "store",
31
+ status: "paid",
32
+ totalsRecorded: true,
33
+ ...(kind === "release" ? { reversalClaimedAmount: 1 } : {}),
34
+ });
35
+ await ctx.db.insert("currencyTotals", {
36
+ accountId: "opaque",
37
+ currency: "usd",
38
+ kind: "earnings",
39
+ gross: 101,
40
+ reversed: 0,
41
+ paidOut: 0,
42
+ count: 101,
43
+ });
44
+ if (kind === "release")
45
+ await ctx.db.insert("transferReversalOps", {
46
+ operationId: "op",
47
+ sourceChargeId: "ch",
48
+ slices: Array.from({ length: 101 }, (_, i) => ({
49
+ stripeTransferId: `tr_${i}`,
50
+ from: 0,
51
+ to: 1,
52
+ })),
53
+ });
54
+ });
55
+ if (kind === "claim")
56
+ await t.mutation(claim, {
57
+ operationId: "op",
58
+ sourceChargeId: "ch",
59
+ mode: { kind: "full" },
60
+ });
61
+ else
62
+ await t.mutation(release, {
63
+ operationId: "op",
64
+ verified: Array.from({ length: 101 }, (_, i) => ({
65
+ stripeTransferId: `tr_${i}`,
66
+ amountReversed: 0,
67
+ })),
68
+ });
69
+ const page = await t.query(batches, {
70
+ operationId: "op",
71
+ paginationOpts: { numItems: 25, cursor: null },
72
+ });
73
+ return {
74
+ t,
75
+ batch: page.page[0],
76
+ financial: await t.run((ctx) => ctx.db.query("transfers").take(102)),
77
+ totals: await t.run((ctx) => ctx.db.query("currencyTotals").take(2)),
78
+ };
79
+ }
80
+ it.each(["claim", "release"] as const)(
81
+ "visits and validates every %s reference across separate bounded commits",
82
+ async (kind) => {
83
+ const { t, batch, financial, totals } = await fixture(kind);
84
+ let result = await t.mutation(resume, {
85
+ batchId: batch._id,
86
+ expectedGeneration: 1,
87
+ });
88
+ expect(result).toMatchObject({
89
+ coverage: "unknown",
90
+ hasDeferredWork: true,
91
+ batch: {
92
+ generation: 2,
93
+ state: "pending",
94
+ continuation: {
95
+ state: "indexing",
96
+ nextReference: 50,
97
+ validatedReferences: 0,
98
+ },
99
+ },
100
+ });
101
+ expect(
102
+ await t.mutation(resume, { batchId: batch._id, expectedGeneration: 1 }),
103
+ ).toEqual(result);
104
+ let calls = 1;
105
+ while (result.batch.state !== "complete") {
106
+ result = await t.mutation(resume, {
107
+ batchId: batch._id,
108
+ expectedGeneration: result.batch.generation,
109
+ });
110
+ calls++;
111
+ expect(calls).toBeLessThan(10);
112
+ }
113
+ expect(calls).toBe(6);
114
+ expect(result).toMatchObject({
115
+ coverage: "unknown",
116
+ hasDeferredWork: false,
117
+ batch: {
118
+ referenceCount: 101,
119
+ continuation: {
120
+ state: "complete",
121
+ nextReference: 101,
122
+ validatedReferences: 101,
123
+ },
124
+ },
125
+ });
126
+ const first = await t.query(references, {
127
+ batchId: batch._id,
128
+ paginationOpts: { numItems: 100, cursor: null },
129
+ });
130
+ const second = await t.query(references, {
131
+ batchId: batch._id,
132
+ paginationOpts: { numItems: 100, cursor: first.continueCursor },
133
+ });
134
+ const all = [...first.page, ...second.page];
135
+ expect(all).toHaveLength(101);
136
+ expect(new Set(all.map((r) => r.transferId)).size).toBe(101);
137
+ expect(all.map((r) => r.position)).toEqual(
138
+ Array.from({ length: 101 }, (_, i) => i),
139
+ );
140
+ expect(second.isDone).toBe(true);
141
+ expect(await t.run((ctx) => ctx.db.query("transfers").take(102))).toEqual(
142
+ financial,
143
+ );
144
+ expect(
145
+ await t.run((ctx) => ctx.db.query("currencyTotals").take(2)),
146
+ ).toEqual(totals);
147
+ expect(
148
+ await t.mutation(resume, {
149
+ batchId: batch._id,
150
+ expectedGeneration: result.batch.generation,
151
+ }),
152
+ ).toEqual(result);
153
+ },
154
+ );