@fantastic.dev/better-stripe 0.41.0 → 0.43.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 (209) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +1 -1
  3. package/dist/client/balanceFundingWorkflow/client.d.ts +172 -0
  4. package/dist/client/balanceFundingWorkflow/client.d.ts.map +1 -0
  5. package/dist/client/balanceFundingWorkflow/client.js +289 -0
  6. package/dist/client/balanceFundingWorkflow/client.js.map +1 -0
  7. package/dist/client/billing/balanceFunding.d.ts +5 -0
  8. package/dist/client/billing/balanceFunding.d.ts.map +1 -1
  9. package/dist/client/billing/balanceFunding.js +30 -0
  10. package/dist/client/billing/balanceFunding.js.map +1 -1
  11. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +1388 -2
  12. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  13. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +710 -17
  14. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  15. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +1420 -34
  16. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  17. package/dist/client/connect/payoutSyncTestHelpers.d.ts +694 -1
  18. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  19. package/dist/client/costRecovery-invoices.d.ts +3 -2
  20. package/dist/client/costRecovery-invoices.d.ts.map +1 -1
  21. package/dist/client/costRecovery-invoices.js +10 -5
  22. package/dist/client/costRecovery-invoices.js.map +1 -1
  23. package/dist/client/costRecovery.d.ts +4 -2
  24. package/dist/client/costRecovery.d.ts.map +1 -1
  25. package/dist/client/costRecovery.js +2 -2
  26. package/dist/client/costRecovery.js.map +1 -1
  27. package/dist/client/index.d.ts +3 -0
  28. package/dist/client/index.d.ts.map +1 -1
  29. package/dist/client/index.js +4 -0
  30. package/dist/client/index.js.map +1 -1
  31. package/dist/client/processingCostReconciliation/client.d.ts +14 -0
  32. package/dist/client/processingCostReconciliation/client.d.ts.map +1 -0
  33. package/dist/client/processingCostReconciliation/client.js +35 -0
  34. package/dist/client/processingCostReconciliation/client.js.map +1 -0
  35. package/dist/client/processingCostReconciliation/contracts.d.ts +172 -0
  36. package/dist/client/processingCostReconciliation/contracts.d.ts.map +1 -0
  37. package/dist/client/processingCostReconciliation/contracts.js +55 -0
  38. package/dist/client/processingCostReconciliation/contracts.js.map +1 -0
  39. package/dist/client/processingCostReconciliation/invoiceFunding.d.ts +15 -0
  40. package/dist/client/processingCostReconciliation/invoiceFunding.d.ts.map +1 -0
  41. package/dist/client/processingCostReconciliation/invoiceFunding.js +195 -0
  42. package/dist/client/processingCostReconciliation/invoiceFunding.js.map +1 -0
  43. package/dist/client/processingCostReconciliation/provider.d.ts +9 -0
  44. package/dist/client/processingCostReconciliation/provider.d.ts.map +1 -0
  45. package/dist/client/processingCostReconciliation/provider.js +148 -0
  46. package/dist/client/processingCostReconciliation/provider.js.map +1 -0
  47. package/dist/client/settlement/processingCostHistory.d.ts +12 -0
  48. package/dist/client/settlement/processingCostHistory.d.ts.map +1 -0
  49. package/dist/client/settlement/processingCostHistory.js +20 -0
  50. package/dist/client/settlement/processingCostHistory.js.map +1 -0
  51. package/dist/client/settlement/processingCosts.d.ts +3 -0
  52. package/dist/client/settlement/processingCosts.d.ts.map +1 -1
  53. package/dist/client/settlement/processingCosts.js +27 -7
  54. package/dist/client/settlement/processingCosts.js.map +1 -1
  55. package/dist/component/_generated/api.d.ts +36 -0
  56. package/dist/component/_generated/api.d.ts.map +1 -1
  57. package/dist/component/_generated/api.js.map +1 -1
  58. package/dist/component/_generated/component.d.ts +988 -0
  59. package/dist/component/_generated/component.d.ts.map +1 -1
  60. package/dist/component/balanceFunding/helpers.d.ts +42 -0
  61. package/dist/component/balanceFunding/helpers.d.ts.map +1 -1
  62. package/dist/component/balanceFunding/internal/actions.d.ts +39 -0
  63. package/dist/component/balanceFunding/internal/actions.d.ts.map +1 -0
  64. package/dist/component/balanceFunding/internal/actions.js +80 -0
  65. package/dist/component/balanceFunding/internal/actions.js.map +1 -0
  66. package/dist/component/balanceFunding/internal/mutations.d.ts +55 -0
  67. package/dist/component/balanceFunding/internal/mutations.d.ts.map +1 -0
  68. package/dist/component/balanceFunding/internal/mutations.js +54 -0
  69. package/dist/component/balanceFunding/internal/mutations.js.map +1 -0
  70. package/dist/component/balanceFunding/internal/queries.d.ts +116 -0
  71. package/dist/component/balanceFunding/internal/queries.d.ts.map +1 -0
  72. package/dist/component/balanceFunding/internal/queries.js +35 -0
  73. package/dist/component/balanceFunding/internal/queries.js.map +1 -0
  74. package/dist/component/balanceFunding/internal/workflow.d.ts +10 -0
  75. package/dist/component/balanceFunding/internal/workflow.d.ts.map +1 -0
  76. package/dist/component/balanceFunding/internal/workflow.js +31 -0
  77. package/dist/component/balanceFunding/internal/workflow.js.map +1 -0
  78. package/dist/component/balanceFunding/lifecycleHandlers.d.ts +126 -0
  79. package/dist/component/balanceFunding/lifecycleHandlers.d.ts.map +1 -1
  80. package/dist/component/balanceFunding/mutations.d.ts +419 -16
  81. package/dist/component/balanceFunding/mutations.d.ts.map +1 -1
  82. package/dist/component/balanceFunding/mutations.js +24 -0
  83. package/dist/component/balanceFunding/mutations.js.map +1 -1
  84. package/dist/component/balanceFunding/preparationHandlers.d.ts +42 -0
  85. package/dist/component/balanceFunding/preparationHandlers.d.ts.map +1 -1
  86. package/dist/component/balanceFunding/queries.d.ts +78 -0
  87. package/dist/component/balanceFunding/queries.d.ts.map +1 -1
  88. package/dist/component/balanceFunding/queries.js +35 -0
  89. package/dist/component/balanceFunding/queries.js.map +1 -1
  90. package/dist/component/balanceFunding/receipt.d.ts +5 -0
  91. package/dist/component/balanceFunding/receipt.d.ts.map +1 -0
  92. package/dist/component/balanceFunding/receipt.js +37 -0
  93. package/dist/component/balanceFunding/receipt.js.map +1 -0
  94. package/dist/component/balanceFunding/schema.d.ts +506 -2
  95. package/dist/component/balanceFunding/schema.d.ts.map +1 -1
  96. package/dist/component/balanceFunding/schema.js +2 -0
  97. package/dist/component/balanceFunding/schema.js.map +1 -1
  98. package/dist/component/balanceFunding/stateHandlers.d.ts +42 -0
  99. package/dist/component/balanceFunding/stateHandlers.d.ts.map +1 -1
  100. package/dist/component/balanceFunding/workflowControl.d.ts +19 -0
  101. package/dist/component/balanceFunding/workflowControl.d.ts.map +1 -0
  102. package/dist/component/balanceFunding/workflowControl.js +149 -0
  103. package/dist/component/balanceFunding/workflowControl.js.map +1 -0
  104. package/dist/component/balanceFunding/workflowSchema.d.ts +356 -0
  105. package/dist/component/balanceFunding/workflowSchema.d.ts.map +1 -0
  106. package/dist/component/balanceFunding/workflowSchema.js +48 -0
  107. package/dist/component/balanceFunding/workflowSchema.js.map +1 -0
  108. package/dist/component/balanceFunding/workflowState.d.ts +157 -0
  109. package/dist/component/balanceFunding/workflowState.d.ts.map +1 -0
  110. package/dist/component/balanceFunding/workflowState.js +194 -0
  111. package/dist/component/balanceFunding/workflowState.js.map +1 -0
  112. package/dist/component/balanceFunding/workflowValidators.d.ts +306 -0
  113. package/dist/component/balanceFunding/workflowValidators.d.ts.map +1 -0
  114. package/dist/component/balanceFunding/workflowValidators.js +27 -0
  115. package/dist/component/balanceFunding/workflowValidators.js.map +1 -0
  116. package/dist/component/earnings/triggers.d.ts +388 -2
  117. package/dist/component/earnings/triggers.d.ts.map +1 -1
  118. package/dist/component/payoutRelease/queries.d.ts +2 -2
  119. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  120. package/dist/component/processingCostReconciliation/adoption.d.ts +29 -0
  121. package/dist/component/processingCostReconciliation/adoption.d.ts.map +1 -0
  122. package/dist/component/processingCostReconciliation/adoption.js +58 -0
  123. package/dist/component/processingCostReconciliation/adoption.js.map +1 -0
  124. package/dist/component/processingCostReconciliation/bridge.d.ts +45 -0
  125. package/dist/component/processingCostReconciliation/bridge.d.ts.map +1 -0
  126. package/dist/component/processingCostReconciliation/bridge.js +41 -0
  127. package/dist/component/processingCostReconciliation/bridge.js.map +1 -0
  128. package/dist/component/processingCostReconciliation/helpers.d.ts +327 -0
  129. package/dist/component/processingCostReconciliation/helpers.d.ts.map +1 -0
  130. package/dist/component/processingCostReconciliation/helpers.js +39 -0
  131. package/dist/component/processingCostReconciliation/helpers.js.map +1 -0
  132. package/dist/component/processingCostReconciliation/mutations.d.ts +303 -0
  133. package/dist/component/processingCostReconciliation/mutations.d.ts.map +1 -0
  134. package/dist/component/processingCostReconciliation/mutations.js +121 -0
  135. package/dist/component/processingCostReconciliation/mutations.js.map +1 -0
  136. package/dist/component/processingCostReconciliation/queries.d.ts +422 -0
  137. package/dist/component/processingCostReconciliation/queries.d.ts.map +1 -0
  138. package/dist/component/processingCostReconciliation/queries.js +19 -0
  139. package/dist/component/processingCostReconciliation/queries.js.map +1 -0
  140. package/dist/component/processingCostReconciliation/rollback.d.ts +21 -0
  141. package/dist/component/processingCostReconciliation/rollback.d.ts.map +1 -0
  142. package/dist/component/processingCostReconciliation/rollback.js +54 -0
  143. package/dist/component/processingCostReconciliation/rollback.js.map +1 -0
  144. package/dist/component/processingCostReconciliation/schema.d.ts +2460 -0
  145. package/dist/component/processingCostReconciliation/schema.d.ts.map +1 -0
  146. package/dist/component/processingCostReconciliation/schema.js +137 -0
  147. package/dist/component/processingCostReconciliation/schema.js.map +1 -0
  148. package/dist/component/processingCostReconciliation/steps.d.ts +306 -0
  149. package/dist/component/processingCostReconciliation/steps.d.ts.map +1 -0
  150. package/dist/component/processingCostReconciliation/steps.js +149 -0
  151. package/dist/component/processingCostReconciliation/steps.js.map +1 -0
  152. package/dist/component/processingCostReconciliation/validation.d.ts +13 -0
  153. package/dist/component/processingCostReconciliation/validation.d.ts.map +1 -0
  154. package/dist/component/processingCostReconciliation/validation.js +61 -0
  155. package/dist/component/processingCostReconciliation/validation.js.map +1 -0
  156. package/dist/component/processingCostReconciliation/workflow.d.ts +105 -0
  157. package/dist/component/processingCostReconciliation/workflow.d.ts.map +1 -0
  158. package/dist/component/processingCostReconciliation/workflow.js +33 -0
  159. package/dist/component/processingCostReconciliation/workflow.js.map +1 -0
  160. package/dist/component/schema.d.ts +694 -1
  161. package/dist/component/schema.js +2 -0
  162. package/dist/component/schema.js.map +1 -1
  163. package/package.json +1 -1
  164. package/src/client/balanceFundingWorkflow/capture.test.ts +144 -0
  165. package/src/client/balanceFundingWorkflow/client.ts +419 -0
  166. package/src/client/billing/balanceFunding.ts +50 -0
  167. package/src/client/costRecovery-invoices.ts +19 -4
  168. package/src/client/costRecovery.ts +4 -2
  169. package/src/client/index.ts +15 -0
  170. package/src/client/processingCostReconciliation/client.ts +75 -0
  171. package/src/client/processingCostReconciliation/contracts.ts +176 -0
  172. package/src/client/processingCostReconciliation/invoiceFunding.ts +270 -0
  173. package/src/client/processingCostReconciliation/invoiceProvider.test.ts +353 -0
  174. package/src/client/processingCostReconciliation/provider.test.ts +324 -0
  175. package/src/client/processingCostReconciliation/provider.ts +178 -0
  176. package/src/client/settlement/processingCostHistory.ts +31 -0
  177. package/src/client/settlement/processingCosts.ts +50 -15
  178. package/src/component/_generated/api.ts +36 -0
  179. package/src/component/_generated/component.ts +1094 -0
  180. package/src/component/balanceFunding/internal/actions.ts +107 -0
  181. package/src/component/balanceFunding/internal/mutations.ts +66 -0
  182. package/src/component/balanceFunding/internal/queries.ts +34 -0
  183. package/src/component/balanceFunding/internal/workflow.ts +49 -0
  184. package/src/component/balanceFunding/mutations.ts +34 -0
  185. package/src/component/balanceFunding/queries.ts +38 -0
  186. package/src/component/balanceFunding/receipt.ts +48 -0
  187. package/src/component/balanceFunding/schema.ts +2 -0
  188. package/src/component/balanceFunding/scopeLookup.test.ts +94 -0
  189. package/src/component/balanceFunding/workflow.fixtures.ts +366 -0
  190. package/src/component/balanceFunding/workflow.test.ts +552 -0
  191. package/src/component/balanceFunding/workflowControl.ts +198 -0
  192. package/src/component/balanceFunding/workflowSchema.ts +78 -0
  193. package/src/component/balanceFunding/workflowState.ts +285 -0
  194. package/src/component/balanceFunding/workflowValidators.ts +40 -0
  195. package/src/component/processingCostReconciliation/adoption.test.ts +206 -0
  196. package/src/component/processingCostReconciliation/adoption.ts +77 -0
  197. package/src/component/processingCostReconciliation/bridge.ts +50 -0
  198. package/src/component/processingCostReconciliation/helpers.ts +55 -0
  199. package/src/component/processingCostReconciliation/invoice.test.ts +152 -0
  200. package/src/component/processingCostReconciliation/mutations.ts +148 -0
  201. package/src/component/processingCostReconciliation/queries.ts +18 -0
  202. package/src/component/processingCostReconciliation/rollback.ts +72 -0
  203. package/src/component/processingCostReconciliation/schema.ts +191 -0
  204. package/src/component/processingCostReconciliation/state.test.ts +407 -0
  205. package/src/component/processingCostReconciliation/steps.ts +168 -0
  206. package/src/component/processingCostReconciliation/validation.ts +79 -0
  207. package/src/component/processingCostReconciliation/workflow.test.ts +329 -0
  208. package/src/component/processingCostReconciliation/workflow.ts +50 -0
  209. package/src/component/schema.ts +2 -0
@@ -0,0 +1,552 @@
1
+ // @vitest-environment edge-runtime
2
+ import { anyApi } from "convex/server";
3
+ import { expect, it, vi } from "vitest";
4
+ import {
5
+ nested,
6
+ scope,
7
+ stripeSubscriptionId,
8
+ original,
9
+ setup,
10
+ fixture,
11
+ } from "./workflow.fixtures.js";
12
+ import { STRIPE_CUSTOMER_PREVIEW_API_VERSION as version } from "../../client/apiVersion.js";
13
+ it("persists original restoration before nested workflow provisioning and finishes only from verified receipts", async () => {
14
+ const t = setup();
15
+ const started = await t.action(anyApi.host.prepare, {
16
+ requestKey: "prepare-once",
17
+ });
18
+ expect(started.status).toBe("accepted");
19
+ expect(started.workflowId).toEqual(expect.any(String));
20
+ const lookup = {
21
+ ...scope,
22
+ stripeSubscriptionId,
23
+ providerApiVersion: version,
24
+ };
25
+ const before = await t.query(
26
+ nested.betterStripe.balanceFunding.queries.readScopePreparation,
27
+ lookup,
28
+ );
29
+ expect(before).toMatchObject({
30
+ preparation: original,
31
+ verified: false,
32
+ consentReference: "original-consent",
33
+ activeOperationId: started.operation.operationId,
34
+ });
35
+ expect(fixture.requests.every((r) => r.method === "GET")).toBe(true);
36
+ const operation = await t.query(
37
+ nested.betterStripe.balanceFunding.queries.inspectOperation,
38
+ { ...started.operation, stripeSubscriptionId },
39
+ );
40
+ expect(operation.providerKeys.create).toEqual(expect.any(String));
41
+ expect(operation.providerKeys.attach).toEqual(expect.any(String));
42
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
43
+ expect(
44
+ await t.query(nested.betterStripe.balanceFunding.queries.getPreparation, {
45
+ ...started.operation,
46
+ stripeSubscriptionId,
47
+ }),
48
+ ).toMatchObject({
49
+ status: "prepared",
50
+ operation: started.operation,
51
+ preparationId: before._id,
52
+ });
53
+ const after = await t.query(
54
+ nested.betterStripe.balanceFunding.queries.readScopePreparation,
55
+ lookup,
56
+ );
57
+ expect(after.preparation).toEqual({
58
+ ...original,
59
+ paymentMethodId: "pm_balance",
60
+ });
61
+ expect(after.verified).toBe(true);
62
+ const writes = fixture.requests.filter((r) => r.method === "POST");
63
+ expect(writes).toEqual([
64
+ {
65
+ method: "POST",
66
+ path: "/v1/payment_methods",
67
+ version,
68
+ key: operation.providerKeys.create,
69
+ },
70
+ {
71
+ method: "POST",
72
+ path: "/v1/payment_methods/pm_balance/attach",
73
+ version,
74
+ key: operation.providerKeys.attach,
75
+ },
76
+ ]);
77
+ expect(fixture.requests.every((r) => r.version === version)).toBe(true);
78
+ });
79
+
80
+ it("joins duplicate public fixture.requests without recapturing settings or creating another workflow", async () => {
81
+ const t = setup();
82
+ const first = await t.action(anyApi.host.prepare, {
83
+ requestKey: "prepare-once",
84
+ });
85
+ const reads = fixture.requests.length;
86
+ expect(
87
+ await t.action(anyApi.host.prepare, { requestKey: "prepare-once" }),
88
+ ).toEqual(first);
89
+ expect(fixture.requests).toHaveLength(reads);
90
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
91
+ expect(
92
+ fixture.requests.filter(
93
+ (r) => r.method === "POST" && r.path === "/v1/payment_methods",
94
+ ),
95
+ ).toHaveLength(1);
96
+ });
97
+
98
+ it("holds a lost create ID across resume and cancellation without reissuing the effect", async () => {
99
+ fixture.unknownPhase = "create";
100
+ const t = setup();
101
+ const first = await t.action(anyApi.host.prepare, {
102
+ requestKey: "lost-create",
103
+ });
104
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
105
+ const ref = { ...first.operation, stripeSubscriptionId };
106
+ expect(
107
+ await t.query(
108
+ nested.betterStripe.balanceFunding.queries.getPreparation,
109
+ ref,
110
+ ),
111
+ ).toMatchObject({ status: "unknown" });
112
+ const control = {
113
+ operationId: first.operation.operationId,
114
+ version: first.operation.version,
115
+ };
116
+ await t.action(anyApi.host.resume, control);
117
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
118
+ await t.action(anyApi.host.cancel, control);
119
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
120
+ const saved = await t.query(
121
+ nested.betterStripe.balanceFunding.queries.readScopePreparation,
122
+ { ...scope, stripeSubscriptionId, providerApiVersion: version },
123
+ );
124
+ expect(saved).toMatchObject({
125
+ preparation: original,
126
+ verified: false,
127
+ activeOperationId: first.operation.operationId,
128
+ });
129
+ expect(
130
+ fixture.requests.filter(
131
+ (r) => r.method === "POST" && r.path === "/v1/payment_methods",
132
+ ),
133
+ ).toHaveLength(1);
134
+ const history = await t.query(
135
+ nested.betterStripe.fundingWorkflowFixture.history,
136
+ { id: first.workflowId },
137
+ );
138
+ expect(JSON.stringify(history)).not.toContain("raw_provider_private_failure");
139
+ });
140
+
141
+ it("reconciles a lost attachment response by known-ID readback without another attach", async () => {
142
+ fixture.unknownPhase = "attach";
143
+ const t = setup();
144
+ const first = await t.action(anyApi.host.prepare, {
145
+ requestKey: "lost-attach",
146
+ });
147
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
148
+ const ref = { ...first.operation, stripeSubscriptionId };
149
+ expect(
150
+ await t.query(
151
+ nested.betterStripe.balanceFunding.queries.getPreparation,
152
+ ref,
153
+ ),
154
+ ).toMatchObject({ status: "unknown" });
155
+ await t.action(anyApi.host.resume, {
156
+ operationId: first.operation.operationId,
157
+ version: first.operation.version,
158
+ });
159
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
160
+ expect(
161
+ await t.query(
162
+ nested.betterStripe.balanceFunding.queries.getPreparation,
163
+ ref,
164
+ ),
165
+ ).toMatchObject({ status: "prepared" });
166
+ expect(fixture.requests.filter((r) => r.method === "POST")).toHaveLength(2);
167
+ expect(
168
+ fixture.requests.filter(
169
+ (r) => r.path.endsWith("/attach") && r.method === "POST",
170
+ ),
171
+ ).toHaveLength(1);
172
+ });
173
+
174
+ it("allowlists host receipts before they reach the durable workflow journal", async () => {
175
+ fixture.extraFields = true;
176
+ const t = setup();
177
+ const first = await t.action(anyApi.host.prepare, {
178
+ requestKey: "sanitized",
179
+ });
180
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
181
+ expect(
182
+ await t.query(nested.betterStripe.balanceFunding.queries.getPreparation, {
183
+ ...first.operation,
184
+ stripeSubscriptionId,
185
+ }),
186
+ ).toMatchObject({ status: "prepared" });
187
+ const history = await t.query(
188
+ nested.betterStripe.fundingWorkflowFixture.history,
189
+ { id: first.workflowId },
190
+ );
191
+ expect(JSON.stringify(history)).not.toContain("raw_provider_private_field");
192
+ });
193
+
194
+ it("resumes a failed database finish from retained receipts without replaying provider writes", async () => {
195
+ const t = setup(true);
196
+ const first = await t.action(anyApi.host.prepare, {
197
+ requestKey: "finish-retry",
198
+ });
199
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
200
+ expect(
201
+ await t.query(nested.betterStripe.fundingWorkflowFixture.status, {
202
+ id: first.workflowId,
203
+ }),
204
+ ).toMatchObject({ type: "failed" });
205
+ expect(fixture.requests.filter((r) => r.method === "POST")).toHaveLength(2);
206
+ await t.action(anyApi.host.resume, {
207
+ operationId: first.operation.operationId,
208
+ version: first.operation.version,
209
+ });
210
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
211
+ expect(
212
+ await t.query(nested.betterStripe.balanceFunding.queries.getPreparation, {
213
+ ...first.operation,
214
+ stripeSubscriptionId,
215
+ }),
216
+ ).toMatchObject({ status: "prepared" });
217
+ expect(
218
+ await t.query(nested.betterStripe.fundingWorkflowFixture.status, {
219
+ id: first.workflowId,
220
+ }),
221
+ ).toMatchObject({ type: "completed" });
222
+ expect(fixture.requests.filter((r) => r.method === "POST")).toHaveLength(2);
223
+ });
224
+
225
+ it("enables and restores the exact original settings using separate durable operation versions", async () => {
226
+ const t = setup();
227
+ const prepared = await t.action(anyApi.host.prepare, {
228
+ requestKey: "apply-prepare",
229
+ });
230
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
231
+ const saved = await t.query(
232
+ nested.betterStripe.balanceFunding.queries.getPreparation,
233
+ { ...prepared.operation, stripeSubscriptionId },
234
+ );
235
+ const first = await t.action(anyApi.host.apply, {
236
+ preparationId: saved.preparationId,
237
+ enabled: true,
238
+ requestKey: "enable",
239
+ });
240
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
241
+ expect(fixture.enabled).toBe(true);
242
+ expect(
243
+ await t.query(nested.betterStripe.balanceFunding.queries.getOperation, {
244
+ ...first.operation,
245
+ stripeSubscriptionId,
246
+ }),
247
+ ).toMatchObject({ state: "succeeded" });
248
+ const second = await t.action(anyApi.host.apply, {
249
+ preparationId: saved.preparationId,
250
+ enabled: false,
251
+ requestKey: "disable",
252
+ });
253
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
254
+ expect(fixture.enabled).toBe(false);
255
+ expect(second.operation.version).toBeGreaterThan(first.operation.version);
256
+ expect(
257
+ fixture.requests
258
+ .filter((r) => r.path.includes("subscriptions") && r.method === "POST")
259
+ .map((r) => r.key),
260
+ ).toEqual([
261
+ expect.stringContaining(":enable"),
262
+ expect.stringContaining(":disable"),
263
+ ]);
264
+ });
265
+ it("reconciles an unknown apply by readback without another provider update", async () => {
266
+ const t = setup();
267
+ const prepared = await t.action(anyApi.host.prepare, {
268
+ requestKey: "unknown-apply-prepare",
269
+ });
270
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
271
+ const saved = await t.query(
272
+ nested.betterStripe.balanceFunding.queries.getPreparation,
273
+ { ...prepared.operation, stripeSubscriptionId },
274
+ );
275
+ fixture.lostApply = true;
276
+ const first = await t.action(anyApi.host.apply, {
277
+ preparationId: saved.preparationId,
278
+ enabled: true,
279
+ requestKey: "unknown-enable",
280
+ });
281
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
282
+ expect(
283
+ await t.query(nested.betterStripe.balanceFunding.queries.getOperation, {
284
+ ...first.operation,
285
+ stripeSubscriptionId,
286
+ }),
287
+ ).toMatchObject({ state: "unknown" });
288
+ await t.action(anyApi.host.resume, {
289
+ operationId: first.operation.operationId,
290
+ version: first.operation.version,
291
+ });
292
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
293
+ expect(
294
+ await t.query(nested.betterStripe.balanceFunding.queries.getOperation, {
295
+ ...first.operation,
296
+ stripeSubscriptionId,
297
+ }),
298
+ ).toMatchObject({ state: "succeeded" });
299
+ expect(
300
+ fixture.requests.filter(
301
+ (r) => r.path.includes("subscriptions") && r.method === "POST",
302
+ ),
303
+ ).toHaveLength(1);
304
+ });
305
+
306
+ it("holds in-flight cancellation without releasing original restoration or starting an attach", async () => {
307
+ const t = setup();
308
+ fixture.cancelPhase = "create";
309
+ const first = await t.action(anyApi.host.prepare, {
310
+ requestKey: "cancel-inflight",
311
+ });
312
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
313
+ expect(
314
+ await t.action(anyApi.host.inspect, {
315
+ operationId: first.operation.operationId,
316
+ version: first.operation.version,
317
+ }),
318
+ ).toMatchObject({ state: "unknown", cancelRequested: true });
319
+ await t.action(anyApi.host.resume, {
320
+ operationId: first.operation.operationId,
321
+ version: first.operation.version,
322
+ });
323
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
324
+ expect(fixture.requests.filter((r) => r.method === "POST")).toHaveLength(1);
325
+ const saved = await t.query(
326
+ nested.betterStripe.balanceFunding.queries.readScopePreparation,
327
+ { ...scope, stripeSubscriptionId, providerApiVersion: version },
328
+ );
329
+ expect(saved).toMatchObject({
330
+ preparation: original,
331
+ verified: false,
332
+ activeOperationId: first.operation.operationId,
333
+ });
334
+ });
335
+ it("finishes from a retained positive attachment receipt after cancellation races its response", async () => {
336
+ const t = setup();
337
+ fixture.cancelPhase = "attach";
338
+ const first = await t.action(anyApi.host.prepare, {
339
+ requestKey: "cancel-after-attach",
340
+ });
341
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
342
+ await t.action(anyApi.host.resume, {
343
+ operationId: first.operation.operationId,
344
+ version: first.operation.version,
345
+ });
346
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
347
+ expect(
348
+ await t.query(nested.betterStripe.balanceFunding.queries.getPreparation, {
349
+ ...first.operation,
350
+ stripeSubscriptionId,
351
+ }),
352
+ ).toMatchObject({ status: "prepared" });
353
+ expect(fixture.requests.filter((r) => r.method === "POST")).toHaveLength(2);
354
+ });
355
+ it("rejects changed definition and stale version before provider replay", async () => {
356
+ const t = setup();
357
+ fixture.unknownPhase = "create";
358
+ const first = await t.action(anyApi.host.prepare, {
359
+ requestKey: "changed-definition",
360
+ });
361
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
362
+ const args = {
363
+ ...first.operation,
364
+ stripeSubscriptionId,
365
+ providerApiVersion: version,
366
+ definitionVersion: "balance-funding/v2",
367
+ };
368
+ expect(
369
+ await t.mutation(
370
+ nested.betterStripe.balanceFunding.mutations.resumeWorkflow,
371
+ args,
372
+ ),
373
+ ).toMatchObject({ status: "conflict", reason: "revision_changed" });
374
+ expect(
375
+ await t.mutation(
376
+ nested.betterStripe.balanceFunding.mutations.resumeWorkflow,
377
+ {
378
+ ...args,
379
+ definitionVersion: "balance-funding/v1",
380
+ version: first.operation.version + 1,
381
+ },
382
+ ),
383
+ ).toMatchObject({ status: "conflict" });
384
+ expect(fixture.requests.filter((r) => r.method === "POST")).toHaveLength(1);
385
+ });
386
+ it("keeps an unknown effect held after workflow retention is cleaned", async () => {
387
+ const t = setup();
388
+ fixture.unknownPhase = "create";
389
+ const first = await t.action(anyApi.host.prepare, {
390
+ requestKey: "expired-history",
391
+ });
392
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
393
+ await t.mutation(nested.betterStripe.fundingWorkflowFixture.cleanup, {
394
+ id: first.workflowId,
395
+ });
396
+ expect(
397
+ await t.action(anyApi.host.resume, {
398
+ operationId: first.operation.operationId,
399
+ version: first.operation.version,
400
+ }),
401
+ ).toMatchObject({
402
+ status: "unknown",
403
+ reason: "history_incomplete",
404
+ retryable: false,
405
+ });
406
+ expect(fixture.requests.filter((r) => r.method === "POST")).toHaveLength(1);
407
+ });
408
+
409
+ it("rolls back a failed workflow start so retry can bind the persisted intent", async () => {
410
+ const { executionWorkflow } = await import("../durableExecution/runtime.js");
411
+ const start = vi
412
+ .spyOn(executionWorkflow, "start")
413
+ .mockRejectedValueOnce(new Error("controlled scheduling failure"));
414
+ const t = setup();
415
+ try {
416
+ await expect(
417
+ t.action(anyApi.host.prepare, { requestKey: "failed-start" }),
418
+ ).rejects.toThrow();
419
+ const saved = await t.query(
420
+ nested.betterStripe.balanceFunding.queries.readScopePreparation,
421
+ { ...scope, stripeSubscriptionId, providerApiVersion: version },
422
+ );
423
+ const prior = await t.query(
424
+ nested.betterStripe.balanceFunding.queries.inspectOperation,
425
+ {
426
+ ...scope,
427
+ operationId: saved.activeOperationId,
428
+ version: saved.operationVersion,
429
+ stripeSubscriptionId,
430
+ },
431
+ );
432
+ expect(prior.execution).toBeUndefined();
433
+ const reads = fixture.requests.length;
434
+ const retried = await t.action(anyApi.host.prepare, {
435
+ requestKey: "failed-start",
436
+ });
437
+ expect(retried.workflowId).toEqual(expect.any(String));
438
+ expect(fixture.requests).toHaveLength(reads);
439
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
440
+ expect(
441
+ await t.query(nested.betterStripe.balanceFunding.queries.getPreparation, {
442
+ ...retried.operation,
443
+ stripeSubscriptionId,
444
+ }),
445
+ ).toMatchObject({ status: "prepared" });
446
+ } finally {
447
+ start.mockRestore();
448
+ }
449
+ });
450
+
451
+ it("resumes a paused pre-dispatch phase after unpause without inventing an unknown provider effect", async () => {
452
+ const t = setup();
453
+ const first = await t.action(anyApi.host.prepare, {
454
+ requestKey: "paused-before-create",
455
+ });
456
+ const saved = await t.query(
457
+ nested.betterStripe.balanceFunding.queries.readScopePreparation,
458
+ { ...scope, stripeSubscriptionId, providerApiVersion: version },
459
+ );
460
+ const control = { ...scope, stripeSubscriptionId, preparationId: saved._id };
461
+ await t.mutation(
462
+ nested.betterStripe.balanceFunding.mutations.setStartsPaused,
463
+ { ...control, paused: true },
464
+ );
465
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
466
+ expect(fixture.requests.filter((r) => r.method === "POST")).toHaveLength(0);
467
+ const blocked = await t.query(
468
+ nested.betterStripe.balanceFunding.queries.inspectOperation,
469
+ { ...first.operation, stripeSubscriptionId },
470
+ );
471
+ expect(blocked.execution.create).toBeUndefined();
472
+ await t.mutation(
473
+ nested.betterStripe.balanceFunding.mutations.setStartsPaused,
474
+ { ...control, paused: false },
475
+ );
476
+ await t.action(anyApi.host.resume, {
477
+ operationId: first.operation.operationId,
478
+ version: first.operation.version,
479
+ });
480
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
481
+ expect(
482
+ await t.query(nested.betterStripe.balanceFunding.queries.getPreparation, {
483
+ ...first.operation,
484
+ stripeSubscriptionId,
485
+ }),
486
+ ).toMatchObject({ status: "prepared" });
487
+ expect(fixture.requests.filter((r) => r.method === "POST")).toHaveLength(2);
488
+ });
489
+
490
+ it("recovers a positive journal receipt when cancellation races a failed receipt write", async () => {
491
+ const t = setup(false, true);
492
+ const first = await t.action(anyApi.host.prepare, {
493
+ requestKey: "cancel-receipt-store-outage",
494
+ });
495
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
496
+ await t.action(anyApi.host.cancel, {
497
+ operationId: first.operation.operationId,
498
+ version: first.operation.version,
499
+ });
500
+ await t.action(anyApi.host.resume, {
501
+ operationId: first.operation.operationId,
502
+ version: first.operation.version,
503
+ });
504
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
505
+ expect(
506
+ await t.query(nested.betterStripe.balanceFunding.queries.getPreparation, {
507
+ ...first.operation,
508
+ stripeSubscriptionId,
509
+ }),
510
+ ).toMatchObject({ status: "prepared" });
511
+ expect(fixture.requests.filter((r) => r.method === "POST")).toHaveLength(2);
512
+ });
513
+
514
+ it("finishes apply from a restored positive journal when no record step was scheduled", async () => {
515
+ const t = setup(false, true, true);
516
+ const prepared = await t.action(anyApi.host.prepare, {
517
+ requestKey: "checkpoint-prepare",
518
+ });
519
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
520
+ const saved = await t.query(
521
+ nested.betterStripe.balanceFunding.queries.getPreparation,
522
+ { ...prepared.operation, stripeSubscriptionId },
523
+ );
524
+ fixture.checkpointFault = true;
525
+ const applied = await t.action(anyApi.host.apply, {
526
+ preparationId: saved.preparationId,
527
+ enabled: true,
528
+ requestKey: "checkpoint-enable",
529
+ });
530
+ await t.finishAllScheduledFunctions(vi.runAllTimers);
531
+ await t.action(anyApi.host.cancel, {
532
+ operationId: applied.operation.operationId,
533
+ version: applied.operation.version,
534
+ });
535
+ expect(
536
+ await t.action(anyApi.host.resume, {
537
+ operationId: applied.operation.operationId,
538
+ version: applied.operation.version,
539
+ }),
540
+ ).toMatchObject({ status: "accepted" });
541
+ expect(
542
+ await t.action(anyApi.host.inspect, {
543
+ operationId: applied.operation.operationId,
544
+ version: applied.operation.version,
545
+ }),
546
+ ).toMatchObject({ state: "succeeded" });
547
+ expect(
548
+ fixture.requests.filter(
549
+ (r) => r.path.includes("subscriptions") && r.method === "POST",
550
+ ),
551
+ ).toHaveLength(1);
552
+ });