@fantastic.dev/better-stripe 0.40.0 → 0.41.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.
- package/CHANGELOG.md +10 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +442 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +237 -16
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +442 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +237 -16
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/costRecovery-invoices.d.ts +2 -2
- package/dist/client/costRecovery-invoices.d.ts.map +1 -1
- package/dist/client/costRecovery-invoices.js +4 -1
- package/dist/client/costRecovery-invoices.js.map +1 -1
- package/dist/client/costRecovery.d.ts +5 -2
- package/dist/client/costRecovery.d.ts.map +1 -1
- package/dist/client/costRecovery.js +3 -1
- package/dist/client/costRecovery.js.map +1 -1
- package/dist/client/recoveryWorkflow/client.d.ts +14 -0
- package/dist/client/recoveryWorkflow/client.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/client.js +34 -0
- package/dist/client/recoveryWorkflow/client.js.map +1 -0
- package/dist/client/recoveryWorkflow/contracts.d.ts +63 -0
- package/dist/client/recoveryWorkflow/contracts.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/contracts.js +11 -0
- package/dist/client/recoveryWorkflow/contracts.js.map +1 -0
- package/dist/client/recoveryWorkflow/provider.d.ts +10 -0
- package/dist/client/recoveryWorkflow/provider.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/provider.js +80 -0
- package/dist/client/recoveryWorkflow/provider.js.map +1 -0
- package/dist/client/recoveryWorkflow/settlement.d.ts +6 -0
- package/dist/client/recoveryWorkflow/settlement.d.ts.map +1 -0
- package/dist/client/recoveryWorkflow/settlement.js +43 -0
- package/dist/client/recoveryWorkflow/settlement.js.map +1 -0
- package/dist/component/_generated/api.d.ts +12 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +164 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/balanceFunding/mutations.d.ts +16 -16
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +210 -16
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/bridge.d.ts +21 -0
- package/dist/component/recoveryWorkflow/bridge.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/bridge.js +52 -0
- package/dist/component/recoveryWorkflow/bridge.js.map +1 -0
- package/dist/component/recoveryWorkflow/helpers.d.ts +83 -0
- package/dist/component/recoveryWorkflow/helpers.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/helpers.js +48 -0
- package/dist/component/recoveryWorkflow/helpers.js.map +1 -0
- package/dist/component/recoveryWorkflow/mutations.d.ts +91 -0
- package/dist/component/recoveryWorkflow/mutations.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/mutations.js +177 -0
- package/dist/component/recoveryWorkflow/mutations.js.map +1 -0
- package/dist/component/recoveryWorkflow/queries.d.ts +118 -0
- package/dist/component/recoveryWorkflow/queries.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/queries.js +20 -0
- package/dist/component/recoveryWorkflow/queries.js.map +1 -0
- package/dist/component/recoveryWorkflow/schema.d.ts +653 -0
- package/dist/component/recoveryWorkflow/schema.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/schema.js +81 -0
- package/dist/component/recoveryWorkflow/schema.js.map +1 -0
- package/dist/component/recoveryWorkflow/steps.d.ts +70 -0
- package/dist/component/recoveryWorkflow/steps.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/steps.js +94 -0
- package/dist/component/recoveryWorkflow/steps.js.map +1 -0
- package/dist/component/recoveryWorkflow/workflow.d.ts +22 -0
- package/dist/component/recoveryWorkflow/workflow.d.ts.map +1 -0
- package/dist/component/recoveryWorkflow/workflow.js +56 -0
- package/dist/component/recoveryWorkflow/workflow.js.map +1 -0
- package/dist/component/schema.d.ts +221 -0
- package/dist/component/schema.js +3 -0
- package/dist/component/schema.js.map +1 -1
- package/package.json +1 -1
- package/src/client/costRecovery-invoices.ts +6 -0
- package/src/client/costRecovery.ts +8 -3
- package/src/client/recoveryWorkflow/client.test.ts +37 -0
- package/src/client/recoveryWorkflow/client.ts +76 -0
- package/src/client/recoveryWorkflow/contracts.ts +72 -0
- package/src/client/recoveryWorkflow/provider.test.ts +424 -0
- package/src/client/recoveryWorkflow/provider.ts +119 -0
- package/src/client/recoveryWorkflow/settlement.ts +67 -0
- package/src/component/_generated/api.ts +12 -0
- package/src/component/_generated/component.ts +222 -0
- package/src/component/recoveryWorkflow/bridge.test.ts +481 -0
- package/src/component/recoveryWorkflow/bridge.ts +66 -0
- package/src/component/recoveryWorkflow/helpers.ts +58 -0
- package/src/component/recoveryWorkflow/journal.test.ts +104 -0
- package/src/component/recoveryWorkflow/mutations.ts +224 -0
- package/src/component/recoveryWorkflow/queries.ts +19 -0
- package/src/component/recoveryWorkflow/schema.ts +92 -0
- package/src/component/recoveryWorkflow/state.test.ts +191 -0
- package/src/component/recoveryWorkflow/steps.ts +97 -0
- package/src/component/recoveryWorkflow/workflow.ts +73 -0
- package/src/component/schema.ts +6 -0
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CostRecoveryClient,
|
|
3
|
+
type RecoveryStartOptions,
|
|
4
|
+
} from "../../client/costRecovery.js";
|
|
5
|
+
import type { Component } from "../../client/helpers.js";
|
|
6
|
+
import { invoke } from "./bridge.js";
|
|
7
|
+
import { convexTest } from "convex-test";
|
|
8
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
9
|
+
import {
|
|
10
|
+
anyApi,
|
|
11
|
+
componentsGeneric,
|
|
12
|
+
defineSchema,
|
|
13
|
+
defineTable,
|
|
14
|
+
internalActionGeneric,
|
|
15
|
+
internalMutationGeneric,
|
|
16
|
+
} from "convex/server";
|
|
17
|
+
import { v } from "convex/values";
|
|
18
|
+
import { beforeEach, afterEach, expect, it, vi } from "vitest";
|
|
19
|
+
import { modules, schema } from "../../test.js";
|
|
20
|
+
const nested = componentsGeneric();
|
|
21
|
+
const ref = (op: { operationId: string; version: number }) => ({
|
|
22
|
+
...scope,
|
|
23
|
+
operationId: op.operationId,
|
|
24
|
+
version: op.version,
|
|
25
|
+
});
|
|
26
|
+
const scope = {
|
|
27
|
+
accountId: "acct_a",
|
|
28
|
+
scopeKey: "seller",
|
|
29
|
+
currency: "usd",
|
|
30
|
+
groupKey: "group",
|
|
31
|
+
};
|
|
32
|
+
let calls: string[];
|
|
33
|
+
let result: unknown;
|
|
34
|
+
let providerGate: Promise<void> | null;
|
|
35
|
+
let callbackFailures = 0;
|
|
36
|
+
let loseProviderJournal = false;
|
|
37
|
+
const host = {
|
|
38
|
+
completed: internalMutationGeneric({
|
|
39
|
+
args: {
|
|
40
|
+
...Object.fromEntries(Object.keys(scope).map((k) => [k, v.string()])),
|
|
41
|
+
operationId: v.string(),
|
|
42
|
+
version: v.number(),
|
|
43
|
+
sourceKind: v.string(),
|
|
44
|
+
sourceId: v.string(),
|
|
45
|
+
status: v.string(),
|
|
46
|
+
cancelRequested: v.boolean(),
|
|
47
|
+
outcome: v.string(),
|
|
48
|
+
},
|
|
49
|
+
returns: v.null(),
|
|
50
|
+
handler: async (ctx, args) => {
|
|
51
|
+
if (callbackFailures-- > 0)
|
|
52
|
+
throw new Error("private callback database detail");
|
|
53
|
+
await ctx.db.insert("completions", {
|
|
54
|
+
operationId: args.operationId,
|
|
55
|
+
outcome: args.outcome,
|
|
56
|
+
});
|
|
57
|
+
return null;
|
|
58
|
+
},
|
|
59
|
+
}),
|
|
60
|
+
provider: internalActionGeneric({
|
|
61
|
+
args: {
|
|
62
|
+
...Object.fromEntries(Object.keys(scope).map((k) => [k, v.string()])),
|
|
63
|
+
operationId: v.string(),
|
|
64
|
+
version: v.number(),
|
|
65
|
+
providerApiVersion: v.string(),
|
|
66
|
+
definitionVersion: v.number(),
|
|
67
|
+
phase: v.string(),
|
|
68
|
+
},
|
|
69
|
+
returns: v.any(),
|
|
70
|
+
handler: async (_ctx, args) => {
|
|
71
|
+
calls.push(args.phase);
|
|
72
|
+
if (providerGate) await providerGate;
|
|
73
|
+
if (result instanceof Error) throw result;
|
|
74
|
+
return { ...args, sourceId: "in_a", ...(result as object) };
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
start: internalMutationGeneric({
|
|
78
|
+
args: {},
|
|
79
|
+
returns: v.any(),
|
|
80
|
+
handler: async (ctx) =>
|
|
81
|
+
new CostRecoveryClient(
|
|
82
|
+
nested.betterStripe as unknown as Component,
|
|
83
|
+
).startGroup(ctx, {
|
|
84
|
+
...scope,
|
|
85
|
+
requestKey: "request",
|
|
86
|
+
provider: anyApi.host.provider as RecoveryStartOptions["provider"],
|
|
87
|
+
completion: anyApi.host.completed as RecoveryStartOptions["completion"],
|
|
88
|
+
providerApiVersion: "2026-09-30.endive",
|
|
89
|
+
definitionVersion: 1,
|
|
90
|
+
releaseUncollectible: false,
|
|
91
|
+
maxAttempts: 2,
|
|
92
|
+
retryDelayMs: 1,
|
|
93
|
+
}),
|
|
94
|
+
}),
|
|
95
|
+
};
|
|
96
|
+
async function fixture() {
|
|
97
|
+
const t = convexTest(
|
|
98
|
+
defineSchema({
|
|
99
|
+
completions: defineTable({
|
|
100
|
+
operationId: v.string(),
|
|
101
|
+
outcome: v.string(),
|
|
102
|
+
}),
|
|
103
|
+
}),
|
|
104
|
+
{
|
|
105
|
+
"./_generated/api.ts": async () => ({}),
|
|
106
|
+
"./host.ts": async () => host,
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
t.registerComponent("betterStripe", schema, {
|
|
110
|
+
...modules,
|
|
111
|
+
"./component/recoveryWorkflow/bridge.ts": async () => ({
|
|
112
|
+
invoke: internalActionGeneric({
|
|
113
|
+
args: {
|
|
114
|
+
accountId: v.string(),
|
|
115
|
+
scopeKey: v.string(),
|
|
116
|
+
currency: v.string(),
|
|
117
|
+
groupKey: v.string(),
|
|
118
|
+
operationId: v.string(),
|
|
119
|
+
version: v.number(),
|
|
120
|
+
phase: v.union(v.literal("execute"), v.literal("reconcile")),
|
|
121
|
+
},
|
|
122
|
+
returns: v.any(),
|
|
123
|
+
handler: async (ctx, args) => {
|
|
124
|
+
const result = await (
|
|
125
|
+
invoke as unknown as {
|
|
126
|
+
_handler: (ctx: unknown, args: unknown) => Promise<unknown>;
|
|
127
|
+
}
|
|
128
|
+
)._handler(ctx, args);
|
|
129
|
+
if (loseProviderJournal) {
|
|
130
|
+
loseProviderJournal = false;
|
|
131
|
+
throw new Error("Provider journal unavailable");
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
},
|
|
135
|
+
}),
|
|
136
|
+
}),
|
|
137
|
+
"./component/fixture.ts": async () => ({
|
|
138
|
+
largeGroup: internalMutationGeneric({
|
|
139
|
+
args: {},
|
|
140
|
+
handler: async (ctx) => {
|
|
141
|
+
const group = (await ctx.db.query("recoveryGroups").first())!;
|
|
142
|
+
for (let i = 0; i < 201; i++) {
|
|
143
|
+
const claimKey = `claim:${i}`;
|
|
144
|
+
const claimId = await ctx.db.insert("recoveryClaims", {
|
|
145
|
+
accountId: scope.accountId,
|
|
146
|
+
scopeKey: scope.scopeKey,
|
|
147
|
+
currency: scope.currency,
|
|
148
|
+
claimKey,
|
|
149
|
+
sourceChargeId: `ch_${i}`,
|
|
150
|
+
sourceGross: 100,
|
|
151
|
+
sourceProcessingFee: 1,
|
|
152
|
+
amountDue: 1,
|
|
153
|
+
revision: 1,
|
|
154
|
+
collectedAmount: 0,
|
|
155
|
+
reservedAmount: 1,
|
|
156
|
+
outstanding: 0,
|
|
157
|
+
sign: "zero",
|
|
158
|
+
});
|
|
159
|
+
await ctx.db.insert("recoveryReservations", {
|
|
160
|
+
operationKey: `reservation:${i}`,
|
|
161
|
+
groupKey: scope.groupKey,
|
|
162
|
+
amount: 1,
|
|
163
|
+
limit: 1,
|
|
164
|
+
allocations: [{ claimId, claimKey, amount: 1 }],
|
|
165
|
+
status: "reserved",
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
await ctx.db.patch(group._id, {
|
|
169
|
+
maxDebit: 1000,
|
|
170
|
+
amount: 201,
|
|
171
|
+
pendingOperations: 201,
|
|
172
|
+
});
|
|
173
|
+
const balance = (await ctx.db.query("recoveryBalances").first())!;
|
|
174
|
+
await ctx.db.patch(balance._id, {
|
|
175
|
+
amountDue: 201,
|
|
176
|
+
reservedAmount: 201,
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
}),
|
|
180
|
+
breakBalance: internalMutationGeneric({
|
|
181
|
+
args: {},
|
|
182
|
+
handler: async (ctx) => {
|
|
183
|
+
const balance = await ctx.db.query("recoveryBalances").first();
|
|
184
|
+
await ctx.db.patch(balance!._id, { amountDue: 1 });
|
|
185
|
+
},
|
|
186
|
+
}),
|
|
187
|
+
repairBalance: internalMutationGeneric({
|
|
188
|
+
args: {},
|
|
189
|
+
handler: async (ctx) => {
|
|
190
|
+
const balance = await ctx.db.query("recoveryBalances").first();
|
|
191
|
+
await ctx.db.patch(balance!._id, { amountDue: 0 });
|
|
192
|
+
},
|
|
193
|
+
}),
|
|
194
|
+
}),
|
|
195
|
+
});
|
|
196
|
+
workflowTest.register(t, "betterStripe/workflow");
|
|
197
|
+
await t.run(async (ctx) =>
|
|
198
|
+
ctx.runMutation(nested.betterStripe.costRecovery.preparation.prepare, {
|
|
199
|
+
...scope,
|
|
200
|
+
sourceKind: "invoice",
|
|
201
|
+
sourceId: "in_a",
|
|
202
|
+
maxDebit: 100,
|
|
203
|
+
maxCredit: 0,
|
|
204
|
+
}),
|
|
205
|
+
);
|
|
206
|
+
return t;
|
|
207
|
+
}
|
|
208
|
+
beforeEach(() => {
|
|
209
|
+
vi.useFakeTimers();
|
|
210
|
+
calls = [];
|
|
211
|
+
providerGate = null;
|
|
212
|
+
callbackFailures = 0;
|
|
213
|
+
loseProviderJournal = false;
|
|
214
|
+
result = { status: "ready", outcome: "collect" };
|
|
215
|
+
});
|
|
216
|
+
afterEach(() => vi.useRealTimers());
|
|
217
|
+
it("runs the real nested Workflow once and finishes recovery", async () => {
|
|
218
|
+
const t = await fixture();
|
|
219
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
220
|
+
expect(await t.mutation(anyApi.host.start, {})).toEqual(op);
|
|
221
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
222
|
+
const saved = await t.query(
|
|
223
|
+
nested.betterStripe.recoveryWorkflow.queries.get,
|
|
224
|
+
ref(op),
|
|
225
|
+
);
|
|
226
|
+
expect(saved.status).toBe("completed");
|
|
227
|
+
expect(calls).toEqual(["execute"]);
|
|
228
|
+
});
|
|
229
|
+
it("sanitizes provider exceptions and reconciles without replaying execute", async () => {
|
|
230
|
+
const t = await fixture();
|
|
231
|
+
result = new Error("sk_test_PRIVATE provider response");
|
|
232
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
233
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
234
|
+
expect(
|
|
235
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
236
|
+
.status,
|
|
237
|
+
).toBe("unknown");
|
|
238
|
+
result = { status: "ready", outcome: "collect", secret: "sk_test_PRIVATE" };
|
|
239
|
+
await t.mutation(
|
|
240
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
241
|
+
ref(op),
|
|
242
|
+
);
|
|
243
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
244
|
+
expect(calls).toEqual(["execute", "reconcile"]);
|
|
245
|
+
expect(
|
|
246
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
247
|
+
.status,
|
|
248
|
+
).toBe("unknown");
|
|
249
|
+
});
|
|
250
|
+
it("bounds automatic readiness observations and deduplicates explicit resume", async () => {
|
|
251
|
+
const t = await fixture();
|
|
252
|
+
result = { status: "not_ready", reason: "provider_pending" };
|
|
253
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
254
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
255
|
+
expect(calls).toEqual(["execute", "reconcile"]);
|
|
256
|
+
await t.mutation(
|
|
257
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
258
|
+
ref(op),
|
|
259
|
+
);
|
|
260
|
+
await t.mutation(
|
|
261
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
262
|
+
ref(op),
|
|
263
|
+
);
|
|
264
|
+
result = { status: "ready", outcome: "collect" };
|
|
265
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
266
|
+
expect(calls).toEqual(["execute", "reconcile", "reconcile"]);
|
|
267
|
+
expect(
|
|
268
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
269
|
+
.status,
|
|
270
|
+
).toBe("completed");
|
|
271
|
+
});
|
|
272
|
+
it("cancels before provider dispatch without releasing the reservation", async () => {
|
|
273
|
+
const t = await fixture();
|
|
274
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
275
|
+
await t.mutation(
|
|
276
|
+
nested.betterStripe.recoveryWorkflow.mutations.requestCancel,
|
|
277
|
+
ref(op),
|
|
278
|
+
);
|
|
279
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
280
|
+
expect(calls).toEqual([]);
|
|
281
|
+
expect(
|
|
282
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
283
|
+
.status,
|
|
284
|
+
).toBe("canceled");
|
|
285
|
+
expect(
|
|
286
|
+
(
|
|
287
|
+
await t.query(nested.betterStripe.costRecovery.queries.group, {
|
|
288
|
+
groupKey: scope.groupKey,
|
|
289
|
+
})
|
|
290
|
+
).status,
|
|
291
|
+
).toBe("sealed");
|
|
292
|
+
});
|
|
293
|
+
it("restarts a failed database completion without another provider call", async () => {
|
|
294
|
+
const t = await fixture();
|
|
295
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
296
|
+
// A missing balance will be created by finish; an invalid reserved total
|
|
297
|
+
// instead forces the transactional finish to fail after the provider receipt.
|
|
298
|
+
await t.run(async (ctx) =>
|
|
299
|
+
ctx.runMutation(nested.betterStripe.fixture.breakBalance, {}),
|
|
300
|
+
);
|
|
301
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
302
|
+
expect(calls).toEqual(["execute"]);
|
|
303
|
+
await t.run(async (ctx) =>
|
|
304
|
+
ctx.runMutation(nested.betterStripe.fixture.repairBalance, {}),
|
|
305
|
+
);
|
|
306
|
+
await t.mutation(
|
|
307
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
308
|
+
ref(op),
|
|
309
|
+
);
|
|
310
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
311
|
+
expect(calls).toEqual(["execute"]);
|
|
312
|
+
expect(
|
|
313
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
314
|
+
.status,
|
|
315
|
+
).toBe("completed");
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("collects a verified in-flight effect even after cancellation is requested", async () => {
|
|
319
|
+
let release!: () => void;
|
|
320
|
+
providerGate = new Promise<void>((resolve) => {
|
|
321
|
+
release = resolve;
|
|
322
|
+
});
|
|
323
|
+
const t = await fixture();
|
|
324
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
325
|
+
const drain = t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
326
|
+
await vi.waitFor(() => expect(calls).toHaveLength(1));
|
|
327
|
+
await t.mutation(
|
|
328
|
+
nested.betterStripe.recoveryWorkflow.mutations.requestCancel,
|
|
329
|
+
ref(op),
|
|
330
|
+
);
|
|
331
|
+
release();
|
|
332
|
+
await drain;
|
|
333
|
+
expect(
|
|
334
|
+
await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)),
|
|
335
|
+
).toMatchObject({
|
|
336
|
+
status: "completed",
|
|
337
|
+
cancelRequested: true,
|
|
338
|
+
outcome: "collect",
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
it("never releases an unknown in-flight effect after cancellation", async () => {
|
|
342
|
+
let release!: () => void;
|
|
343
|
+
providerGate = new Promise<void>((resolve) => {
|
|
344
|
+
release = resolve;
|
|
345
|
+
});
|
|
346
|
+
result = new Error("provider outcome lost");
|
|
347
|
+
const t = await fixture();
|
|
348
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
349
|
+
const drain = t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
350
|
+
await vi.waitFor(() => expect(calls).toHaveLength(1));
|
|
351
|
+
await t.mutation(
|
|
352
|
+
nested.betterStripe.recoveryWorkflow.mutations.requestCancel,
|
|
353
|
+
ref(op),
|
|
354
|
+
);
|
|
355
|
+
release();
|
|
356
|
+
await drain;
|
|
357
|
+
expect(
|
|
358
|
+
await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)),
|
|
359
|
+
).toMatchObject({ status: "unknown", cancelRequested: true });
|
|
360
|
+
expect(
|
|
361
|
+
(
|
|
362
|
+
await t.query(nested.betterStripe.costRecovery.queries.group, {
|
|
363
|
+
groupKey: scope.groupKey,
|
|
364
|
+
})
|
|
365
|
+
).status,
|
|
366
|
+
).toBe("sealed");
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("restarts a failed host completion atomically while retaining the provider receipt", async () => {
|
|
370
|
+
const t = await fixture();
|
|
371
|
+
callbackFailures = 1;
|
|
372
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
373
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
374
|
+
expect(calls).toEqual(["execute"]);
|
|
375
|
+
expect(await t.run((ctx) => ctx.db.query("completions").collect())).toEqual(
|
|
376
|
+
[],
|
|
377
|
+
);
|
|
378
|
+
await t.mutation(
|
|
379
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
380
|
+
ref(op),
|
|
381
|
+
);
|
|
382
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
383
|
+
expect(calls).toEqual(["execute"]);
|
|
384
|
+
expect(
|
|
385
|
+
await t.run((ctx) => ctx.db.query("completions").collect()),
|
|
386
|
+
).toMatchObject([{ operationId: op.operationId, outcome: "collect" }]);
|
|
387
|
+
expect(
|
|
388
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
389
|
+
.status,
|
|
390
|
+
).toBe("completed");
|
|
391
|
+
await t.mutation(
|
|
392
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
393
|
+
ref(op),
|
|
394
|
+
);
|
|
395
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
396
|
+
expect(
|
|
397
|
+
await t.run((ctx) => ctx.db.query("completions").collect()),
|
|
398
|
+
).toHaveLength(1);
|
|
399
|
+
});
|
|
400
|
+
it("continues an exhausted segment with reconciliation only and the original operation", async () => {
|
|
401
|
+
const t = await fixture();
|
|
402
|
+
result = new Error("provider outcome unknown");
|
|
403
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
404
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
405
|
+
for (let i = 0; i < 20; i++) {
|
|
406
|
+
await t.mutation(
|
|
407
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
408
|
+
ref(op),
|
|
409
|
+
);
|
|
410
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
411
|
+
}
|
|
412
|
+
result = { status: "ready", outcome: "collect" };
|
|
413
|
+
await t.mutation(
|
|
414
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
415
|
+
ref(op),
|
|
416
|
+
);
|
|
417
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
418
|
+
expect(calls.filter((p) => p === "execute")).toHaveLength(1);
|
|
419
|
+
expect(
|
|
420
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
421
|
+
.status,
|
|
422
|
+
).toBe("completed");
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it("recovers an unjournaled provider success using a new reconcile-only segment", async () => {
|
|
426
|
+
const t = await fixture();
|
|
427
|
+
loseProviderJournal = true;
|
|
428
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
429
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
430
|
+
expect(calls).toEqual(["execute"]);
|
|
431
|
+
await t.mutation(
|
|
432
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
433
|
+
ref(op),
|
|
434
|
+
);
|
|
435
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
436
|
+
expect(calls).toEqual(["execute", "reconcile"]);
|
|
437
|
+
expect(
|
|
438
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
439
|
+
.status,
|
|
440
|
+
).toBe("completed");
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it("continues bounded ledger completion from the persisted receipt without another provider observation", async () => {
|
|
444
|
+
const t = await fixture();
|
|
445
|
+
await t.run((ctx) =>
|
|
446
|
+
ctx.runMutation(nested.betterStripe.fixture.largeGroup, {}),
|
|
447
|
+
);
|
|
448
|
+
const op = await t.mutation(anyApi.host.start, {});
|
|
449
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
450
|
+
expect(calls).toEqual(["execute"]);
|
|
451
|
+
expect(
|
|
452
|
+
(
|
|
453
|
+
await t.query(nested.betterStripe.costRecovery.queries.group, {
|
|
454
|
+
groupKey: scope.groupKey,
|
|
455
|
+
})
|
|
456
|
+
).pendingOperations,
|
|
457
|
+
).toBe(1);
|
|
458
|
+
result = new Error("provider currently unavailable");
|
|
459
|
+
await t.mutation(
|
|
460
|
+
nested.betterStripe.recoveryWorkflow.mutations.resume,
|
|
461
|
+
ref(op),
|
|
462
|
+
);
|
|
463
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
464
|
+
expect(calls).toEqual(["execute"]);
|
|
465
|
+
expect(
|
|
466
|
+
(await t.query(nested.betterStripe.recoveryWorkflow.queries.get, ref(op)))
|
|
467
|
+
.status,
|
|
468
|
+
).toBe("completed");
|
|
469
|
+
expect(
|
|
470
|
+
await t.query(nested.betterStripe.costRecovery.queries.balance, {
|
|
471
|
+
accountId: scope.accountId,
|
|
472
|
+
scopeKey: scope.scopeKey,
|
|
473
|
+
currency: scope.currency,
|
|
474
|
+
}),
|
|
475
|
+
).toMatchObject({
|
|
476
|
+
amountDue: 201,
|
|
477
|
+
collectedAmount: 201,
|
|
478
|
+
reservedAmount: 0,
|
|
479
|
+
outstanding: 0,
|
|
480
|
+
});
|
|
481
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { anyApi, type FunctionHandle } from "convex/server";
|
|
2
|
+
import { internalAction } from "../_generated/server.js";
|
|
3
|
+
import { refFields, phase, receipt } from "./schema.js";
|
|
4
|
+
import type { Infer } from "convex/values";
|
|
5
|
+
import { same } from "./helpers.js";
|
|
6
|
+
const unknown = {
|
|
7
|
+
status: "unknown",
|
|
8
|
+
reason: "provider_outcome_unknown",
|
|
9
|
+
} as const;
|
|
10
|
+
export const invoke = internalAction({
|
|
11
|
+
args: { ...refFields, phase },
|
|
12
|
+
returns: receipt,
|
|
13
|
+
handler: async (ctx, args): Promise<Infer<typeof receipt>> => {
|
|
14
|
+
try {
|
|
15
|
+
const { phase: _phase, ...ref } = args;
|
|
16
|
+
const op = await ctx.runQuery(anyApi.recoveryWorkflow.queries.saved, ref);
|
|
17
|
+
if (
|
|
18
|
+
!(await ctx.runMutation(
|
|
19
|
+
anyApi.recoveryWorkflow.steps.beginProvider,
|
|
20
|
+
args,
|
|
21
|
+
))
|
|
22
|
+
)
|
|
23
|
+
return { status: "canceled" };
|
|
24
|
+
const request = {
|
|
25
|
+
...args,
|
|
26
|
+
providerApiVersion: op.providerApiVersion,
|
|
27
|
+
definitionVersion: op.definitionVersion,
|
|
28
|
+
};
|
|
29
|
+
const result: unknown = await ctx.runAction(
|
|
30
|
+
op.provider as FunctionHandle<"action">,
|
|
31
|
+
request,
|
|
32
|
+
);
|
|
33
|
+
if (!result || typeof result !== "object" || Array.isArray(result))
|
|
34
|
+
return unknown;
|
|
35
|
+
const r = result as Record<string, unknown>;
|
|
36
|
+
const allowed = [
|
|
37
|
+
...Object.keys(request),
|
|
38
|
+
"sourceId",
|
|
39
|
+
"status",
|
|
40
|
+
"outcome",
|
|
41
|
+
"reason",
|
|
42
|
+
];
|
|
43
|
+
if (
|
|
44
|
+
Object.keys(r).some((k) => !allowed.includes(k)) ||
|
|
45
|
+
!same(
|
|
46
|
+
Object.fromEntries(Object.keys(request).map((k) => [k, r[k]])),
|
|
47
|
+
request,
|
|
48
|
+
) ||
|
|
49
|
+
r.sourceId !== op.sourceId
|
|
50
|
+
)
|
|
51
|
+
return unknown;
|
|
52
|
+
if (
|
|
53
|
+
r.status === "ready" &&
|
|
54
|
+
(r.outcome === "collect" || r.outcome === "release") &&
|
|
55
|
+
(op.sourceKind !== "settlement" || r.outcome === "collect")
|
|
56
|
+
)
|
|
57
|
+
return { status: "ready", outcome: r.outcome };
|
|
58
|
+
if (r.status === "not_ready" && r.reason === "provider_pending")
|
|
59
|
+
return { status: "not_ready", reason: "provider_pending" };
|
|
60
|
+
return unknown;
|
|
61
|
+
} catch {
|
|
62
|
+
// Never allow an SDK exception, response object, or host payload into Workflow history.
|
|
63
|
+
return unknown;
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Doc } from "../_generated/dataModel.js";
|
|
2
|
+
import type { QueryCtx, MutationCtx } from "../_generated/server.js";
|
|
3
|
+
import type { Infer } from "convex/values";
|
|
4
|
+
import { v } from "convex/values";
|
|
5
|
+
import { refFields } from "./schema.js";
|
|
6
|
+
const _ref = v.object(refFields);
|
|
7
|
+
export type OperationRef = Infer<typeof _ref>;
|
|
8
|
+
export const identityConflict = {
|
|
9
|
+
status: "conflict",
|
|
10
|
+
reason: "identity_changed",
|
|
11
|
+
retryable: false,
|
|
12
|
+
} as const;
|
|
13
|
+
export function reference(op: Doc<"recoveryWorkflowOperations">): OperationRef {
|
|
14
|
+
return {
|
|
15
|
+
accountId: op.accountId,
|
|
16
|
+
scopeKey: op.scopeKey,
|
|
17
|
+
currency: op.currency,
|
|
18
|
+
groupKey: op.groupKey,
|
|
19
|
+
operationId: op._id,
|
|
20
|
+
version: op.version,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function status(op: Doc<"recoveryWorkflowOperations">) {
|
|
24
|
+
return {
|
|
25
|
+
...reference(op),
|
|
26
|
+
sourceKind: op.sourceKind,
|
|
27
|
+
sourceId: op.sourceId,
|
|
28
|
+
status: op.status,
|
|
29
|
+
cancelRequested: op.cancelRequested,
|
|
30
|
+
...(op.outcome ? { outcome: op.outcome } : {}),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export async function read(ctx: QueryCtx | MutationCtx, args: OperationRef) {
|
|
34
|
+
const op = await ctx.db.get("recoveryWorkflowOperations", args.operationId);
|
|
35
|
+
if (
|
|
36
|
+
!op ||
|
|
37
|
+
Object.entries(reference(op)).some(
|
|
38
|
+
([k, value]) => value !== args[k as keyof OperationRef],
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
throw new Error("Recovery operation identity does not match.");
|
|
42
|
+
return op;
|
|
43
|
+
}
|
|
44
|
+
/** Stable object ordering without admitting arbitrary durable caller payloads. */
|
|
45
|
+
export function same(a: unknown, b: unknown): boolean {
|
|
46
|
+
const canonical = (x: unknown): unknown =>
|
|
47
|
+
Array.isArray(x)
|
|
48
|
+
? x.map(canonical)
|
|
49
|
+
: x && typeof x === "object"
|
|
50
|
+
? Object.fromEntries(
|
|
51
|
+
Object.entries(x)
|
|
52
|
+
.filter(([, v]) => v !== undefined)
|
|
53
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
54
|
+
.map(([k, v]) => [k, canonical(v)]),
|
|
55
|
+
)
|
|
56
|
+
: x;
|
|
57
|
+
return JSON.stringify(canonical(a)) === JSON.stringify(canonical(b));
|
|
58
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
3
|
+
import {
|
|
4
|
+
anyApi,
|
|
5
|
+
componentsGeneric,
|
|
6
|
+
createFunctionHandle,
|
|
7
|
+
defineSchema,
|
|
8
|
+
internalActionGeneric,
|
|
9
|
+
internalMutationGeneric,
|
|
10
|
+
internalQueryGeneric,
|
|
11
|
+
} from "convex/server";
|
|
12
|
+
import { beforeEach, afterEach, expect, it, vi } from "vitest";
|
|
13
|
+
import { v } from "convex/values";
|
|
14
|
+
import { modules, schema } from "../../test.js";
|
|
15
|
+
import { executionWorkflow } from "../durableExecution/runtime.js";
|
|
16
|
+
const nested = componentsGeneric();
|
|
17
|
+
const scope = {
|
|
18
|
+
accountId: "acct_a",
|
|
19
|
+
scopeKey: "seller",
|
|
20
|
+
currency: "usd",
|
|
21
|
+
groupKey: "journal",
|
|
22
|
+
};
|
|
23
|
+
const host = {
|
|
24
|
+
provider: internalActionGeneric({
|
|
25
|
+
args: {
|
|
26
|
+
...Object.fromEntries(Object.keys(scope).map((k) => [k, v.string()])),
|
|
27
|
+
operationId: v.string(),
|
|
28
|
+
version: v.number(),
|
|
29
|
+
providerApiVersion: v.string(),
|
|
30
|
+
definitionVersion: v.number(),
|
|
31
|
+
phase: v.string(),
|
|
32
|
+
},
|
|
33
|
+
returns: v.any(),
|
|
34
|
+
handler: async () => {
|
|
35
|
+
throw new Error("sk_test_PRIVATE accidental provider body");
|
|
36
|
+
},
|
|
37
|
+
}),
|
|
38
|
+
start: internalMutationGeneric({
|
|
39
|
+
args: {},
|
|
40
|
+
returns: v.any(),
|
|
41
|
+
handler: async (ctx) =>
|
|
42
|
+
ctx.runMutation(nested.betterStripe.recoveryWorkflow.mutations.start, {
|
|
43
|
+
...scope,
|
|
44
|
+
requestKey: "journal",
|
|
45
|
+
provider: await createFunctionHandle(anyApi.host.provider),
|
|
46
|
+
providerApiVersion: "2026-09-30.endive",
|
|
47
|
+
definitionVersion: 1,
|
|
48
|
+
releaseUncollectible: false,
|
|
49
|
+
maxAttempts: 3,
|
|
50
|
+
retryDelayMs: 1,
|
|
51
|
+
}),
|
|
52
|
+
}),
|
|
53
|
+
};
|
|
54
|
+
beforeEach(() => vi.useFakeTimers());
|
|
55
|
+
afterEach(() => vi.useRealTimers());
|
|
56
|
+
it("journals only a closed sanitized outcome from a throwing host action", async () => {
|
|
57
|
+
const t = convexTest(defineSchema({}), {
|
|
58
|
+
"./_generated/api.ts": async () => ({}),
|
|
59
|
+
"./host.ts": async () => host,
|
|
60
|
+
});
|
|
61
|
+
t.registerComponent("betterStripe", schema, {
|
|
62
|
+
...modules,
|
|
63
|
+
"./component/fixture.ts": async () => ({
|
|
64
|
+
history: internalQueryGeneric({
|
|
65
|
+
args: {},
|
|
66
|
+
returns: v.any(),
|
|
67
|
+
handler: async (ctx) => {
|
|
68
|
+
const op = (await ctx.db
|
|
69
|
+
.query("recoveryWorkflowOperations")
|
|
70
|
+
.first())!;
|
|
71
|
+
return executionWorkflow.listSteps(ctx, op.workflowId, {
|
|
72
|
+
paginationOpts: { cursor: null, numItems: 100 },
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
}),
|
|
76
|
+
}),
|
|
77
|
+
});
|
|
78
|
+
workflowTest.register(t, "betterStripe/workflow");
|
|
79
|
+
await t.run((ctx) =>
|
|
80
|
+
ctx.runMutation(nested.betterStripe.costRecovery.preparation.prepare, {
|
|
81
|
+
...scope,
|
|
82
|
+
sourceKind: "invoice",
|
|
83
|
+
sourceId: "in_a",
|
|
84
|
+
maxDebit: 100,
|
|
85
|
+
maxCredit: 0,
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
await t.mutation(anyApi.host.start, {});
|
|
89
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
90
|
+
const history = await t.query(nested.betterStripe.fixture.history, {});
|
|
91
|
+
const text = JSON.stringify(history);
|
|
92
|
+
expect(text).not.toContain("sk_test_PRIVATE");
|
|
93
|
+
expect(text).not.toContain("accidental provider body");
|
|
94
|
+
expect(
|
|
95
|
+
history.page.find((step: { name: string }) =>
|
|
96
|
+
step.name.startsWith("provider-"),
|
|
97
|
+
),
|
|
98
|
+
).toMatchObject({
|
|
99
|
+
runResult: {
|
|
100
|
+
kind: "success",
|
|
101
|
+
returnValue: { status: "unknown", reason: "provider_outcome_unknown" },
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
});
|