@fantastic.dev/better-stripe 0.52.0 → 0.54.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 +12 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +160 -2
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +80 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +160 -2
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +80 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +2 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/products/catalogClient.d.ts +23 -0
- package/dist/client/products/catalogClient.d.ts.map +1 -1
- package/dist/client/products/catalogClient.js +32 -0
- package/dist/client/products/catalogClient.js.map +1 -1
- package/dist/client/products/fixedPrice.d.ts +4 -0
- package/dist/client/products/fixedPrice.d.ts.map +1 -0
- package/dist/client/products/fixedPrice.js +10 -0
- package/dist/client/products/fixedPrice.js.map +1 -0
- package/dist/client/products/products.d.ts.map +1 -1
- package/dist/client/products/products.js +3 -0
- package/dist/client/products/products.js.map +1 -1
- package/dist/client/usage/catalog.d.ts +7 -0
- package/dist/client/usage/catalog.d.ts.map +1 -0
- package/dist/client/usage/catalog.js +55 -0
- package/dist/client/usage/catalog.js.map +1 -0
- package/dist/client/usage/catalogLifecycle.d.ts +25 -0
- package/dist/client/usage/catalogLifecycle.d.ts.map +1 -0
- package/dist/client/usage/catalogLifecycle.js +64 -0
- package/dist/client/usage/catalogLifecycle.js.map +1 -0
- package/dist/client/usage/catalogMath.d.ts +3 -0
- package/dist/client/usage/catalogMath.d.ts.map +1 -0
- package/dist/client/usage/catalogMath.js +21 -0
- package/dist/client/usage/catalogMath.js.map +1 -0
- package/dist/client/usage/catalogPrices.d.ts +5 -0
- package/dist/client/usage/catalogPrices.d.ts.map +1 -0
- package/dist/client/usage/catalogPrices.js +178 -0
- package/dist/client/usage/catalogPrices.js.map +1 -0
- package/dist/client/usage/catalogRead.d.ts +25 -0
- package/dist/client/usage/catalogRead.d.ts.map +1 -0
- package/dist/client/usage/catalogRead.js +71 -0
- package/dist/client/usage/catalogRead.js.map +1 -0
- package/dist/client/usage/catalogTypes.d.ts +97 -0
- package/dist/client/usage/catalogTypes.d.ts.map +1 -0
- package/dist/client/usage/catalogTypes.js +106 -0
- package/dist/client/usage/catalogTypes.js.map +1 -0
- package/dist/client/usage/client.d.ts +2 -2
- package/dist/client/usage/client.d.ts.map +1 -1
- package/dist/client/usage/client.js +3 -3
- package/dist/client/usage/client.js.map +1 -1
- package/dist/client/usage/deliveryClient.d.ts +64 -0
- package/dist/client/usage/deliveryClient.d.ts.map +1 -0
- package/dist/client/usage/deliveryClient.js +60 -0
- package/dist/client/usage/deliveryClient.js.map +1 -0
- package/dist/client/usage/deliveryProtocol.d.ts +130 -0
- package/dist/client/usage/deliveryProtocol.d.ts.map +1 -0
- package/dist/client/usage/deliveryProtocol.js +21 -0
- package/dist/client/usage/deliveryProtocol.js.map +1 -0
- package/dist/client/usage/deliveryProvider.d.ts +32 -0
- package/dist/client/usage/deliveryProvider.d.ts.map +1 -0
- package/dist/client/usage/deliveryProvider.js +127 -0
- package/dist/client/usage/deliveryProvider.js.map +1 -0
- package/dist/client/usage/index.d.ts +3 -0
- package/dist/client/usage/index.d.ts.map +1 -1
- package/dist/client/usage/index.js +1 -0
- package/dist/client/usage/index.js.map +1 -1
- package/dist/client/webhooks/handler.d.ts.map +1 -1
- package/dist/client/webhooks/handler.js +2 -1
- package/dist/client/webhooks/handler.js.map +1 -1
- package/dist/client/webhooks/meterErrors.d.ts +5 -0
- package/dist/client/webhooks/meterErrors.d.ts.map +1 -0
- package/dist/client/webhooks/meterErrors.js +98 -0
- package/dist/client/webhooks/meterErrors.js.map +1 -0
- package/dist/client/webhooks/processors.d.ts.map +1 -1
- package/dist/client/webhooks/processors.js +3 -0
- package/dist/client/webhooks/processors.js.map +1 -1
- package/dist/client/webhooks/v2.d.ts.map +1 -1
- package/dist/client/webhooks/v2.js +5 -0
- package/dist/client/webhooks/v2.js.map +1 -1
- package/dist/component/_generated/api.d.ts +18 -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 +232 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/earnings/triggers.d.ts +150 -2
- 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/refundHistory/queries.d.ts +1 -1
- package/dist/component/schema.d.ts +80 -1
- package/dist/component/schema.js +5 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/usage/schema.d.ts +96 -1
- package/dist/component/usage/schema.d.ts.map +1 -1
- package/dist/component/usage/schema.js +11 -2
- package/dist/component/usage/schema.js.map +1 -1
- package/dist/component/usageCatalog/mutations.d.ts +25 -0
- package/dist/component/usageCatalog/mutations.d.ts.map +1 -0
- package/dist/component/usageCatalog/mutations.js +75 -0
- package/dist/component/usageCatalog/mutations.js.map +1 -0
- package/dist/component/usageCatalog/queries.d.ts +33 -0
- package/dist/component/usageCatalog/queries.d.ts.map +1 -0
- package/dist/component/usageCatalog/queries.js +26 -0
- package/dist/component/usageCatalog/queries.js.map +1 -0
- package/dist/component/usageCatalog/schema.d.ts +57 -0
- package/dist/component/usageCatalog/schema.d.ts.map +1 -0
- package/dist/component/usageCatalog/schema.js +22 -0
- package/dist/component/usageCatalog/schema.js.map +1 -0
- package/dist/component/usageDelivery/bridge.d.ts +25 -0
- package/dist/component/usageDelivery/bridge.d.ts.map +1 -0
- package/dist/component/usageDelivery/bridge.js +45 -0
- package/dist/component/usageDelivery/bridge.js.map +1 -0
- package/dist/component/usageDelivery/fields.d.ts +78 -0
- package/dist/component/usageDelivery/fields.d.ts.map +1 -0
- package/dist/component/usageDelivery/fields.js +38 -0
- package/dist/component/usageDelivery/fields.js.map +1 -0
- package/dist/component/usageDelivery/helpers.d.ts +83 -0
- package/dist/component/usageDelivery/helpers.d.ts.map +1 -0
- package/dist/component/usageDelivery/helpers.js +63 -0
- package/dist/component/usageDelivery/helpers.js.map +1 -0
- package/dist/component/usageDelivery/mutations.d.ts +97 -0
- package/dist/component/usageDelivery/mutations.d.ts.map +1 -0
- package/dist/component/usageDelivery/mutations.js +153 -0
- package/dist/component/usageDelivery/mutations.js.map +1 -0
- package/dist/component/usageDelivery/queries.d.ts +113 -0
- package/dist/component/usageDelivery/queries.d.ts.map +1 -0
- package/dist/component/usageDelivery/queries.js +22 -0
- package/dist/component/usageDelivery/queries.js.map +1 -0
- package/dist/component/usageDelivery/schema.d.ts +79 -0
- package/dist/component/usageDelivery/schema.d.ts.map +1 -0
- package/dist/component/usageDelivery/schema.js +30 -0
- package/dist/component/usageDelivery/schema.js.map +1 -0
- package/dist/component/usageDelivery/steps.d.ts +85 -0
- package/dist/component/usageDelivery/steps.d.ts.map +1 -0
- package/dist/component/usageDelivery/steps.js +154 -0
- package/dist/component/usageDelivery/steps.js.map +1 -0
- package/dist/component/usageDelivery/workflow.d.ts +15 -0
- package/dist/component/usageDelivery/workflow.d.ts.map +1 -0
- package/dist/component/usageDelivery/workflow.js +28 -0
- package/dist/component/usageDelivery/workflow.js.map +1 -0
- package/docs/README.md +5 -1
- package/docs/metering-setup.md +3 -1
- package/docs/usage-catalog.md +106 -0
- package/docs/usage-delivery.md +143 -0
- package/package.json +4 -2
- package/src/client/index.ts +23 -0
- package/src/client/products/catalogClient.ts +77 -0
- package/src/client/products/fixedPrice.test.ts +57 -0
- package/src/client/products/fixedPrice.ts +13 -0
- package/src/client/products/products.ts +2 -0
- package/src/client/usage/catalog.test.ts +464 -0
- package/src/client/usage/catalog.ts +117 -0
- package/src/client/usage/catalogLifecycle.ts +128 -0
- package/src/client/usage/catalogMath.test.ts +13 -0
- package/src/client/usage/catalogMath.ts +22 -0
- package/src/client/usage/catalogPrices.ts +251 -0
- package/src/client/usage/catalogRead.ts +98 -0
- package/src/client/usage/catalogTypes.ts +170 -0
- package/src/client/usage/client.ts +5 -2
- package/src/client/usage/deliveryClient.ts +130 -0
- package/src/client/usage/deliveryProtocol.ts +26 -0
- package/src/client/usage/deliveryProvider.test.ts +207 -0
- package/src/client/usage/deliveryProvider.ts +155 -0
- package/src/client/usage/index.ts +13 -0
- package/src/client/webhooks/handler.ts +2 -1
- package/src/client/webhooks/meterErrors.test.ts +164 -0
- package/src/client/webhooks/meterErrors.ts +118 -0
- package/src/client/webhooks/processors.ts +2 -0
- package/src/client/webhooks/v2.ts +5 -0
- package/src/component/_generated/api.ts +18 -0
- package/src/component/_generated/component.ts +380 -0
- package/src/component/schema.ts +8 -0
- package/src/component/usage/schema.ts +11 -2
- package/src/component/usageCatalog/mutations.ts +79 -0
- package/src/component/usageCatalog/queries.ts +32 -0
- package/src/component/usageCatalog/schema.ts +21 -0
- package/src/component/usageDelivery/bridge.ts +56 -0
- package/src/component/usageDelivery/concurrency.test.ts +62 -0
- package/src/component/usageDelivery/fields.ts +59 -0
- package/src/component/usageDelivery/helpers.ts +77 -0
- package/src/component/usageDelivery/mutations.ts +185 -0
- package/src/component/usageDelivery/queries.ts +25 -0
- package/src/component/usageDelivery/schema.ts +33 -0
- package/src/component/usageDelivery/steps.ts +171 -0
- package/src/component/usageDelivery/workflow.test.ts +402 -0
- package/src/component/usageDelivery/workflow.ts +36 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import { BetterStripe } from "../../client/index.js";
|
|
2
|
+
import type { Component } from "../../client/helpers.js";
|
|
3
|
+
import type { UsageDeliveryStartOptions } from "../../client/usage/deliveryClient.js";
|
|
4
|
+
import { executionWorkflow } from "../durableExecution/runtime.js";
|
|
5
|
+
import * as steps from "./steps.js";
|
|
6
|
+
import { request } from "./schema.js";
|
|
7
|
+
import { outcome as outcomeValidator } from "./fields.js";
|
|
8
|
+
import { convexTest } from "convex-test";
|
|
9
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
10
|
+
import {
|
|
11
|
+
anyApi,
|
|
12
|
+
componentsGeneric,
|
|
13
|
+
internalQueryGeneric,
|
|
14
|
+
defineSchema,
|
|
15
|
+
internalActionGeneric,
|
|
16
|
+
internalMutationGeneric,
|
|
17
|
+
} from "convex/server";
|
|
18
|
+
import { v } from "convex/values";
|
|
19
|
+
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
|
20
|
+
import { schema, modules } from "../../test.js";
|
|
21
|
+
const nested = componentsGeneric();
|
|
22
|
+
const scope = {
|
|
23
|
+
stripeAccountId: "acct_platform",
|
|
24
|
+
customerAccount: "acct_buyer",
|
|
25
|
+
meterId: "mtr_calls",
|
|
26
|
+
livemode: false,
|
|
27
|
+
};
|
|
28
|
+
let calls = 0;
|
|
29
|
+
let saveFailures = 0;
|
|
30
|
+
let providerThrows = false;
|
|
31
|
+
let outcomes: Array<Record<string, unknown>> = [];
|
|
32
|
+
const host = {
|
|
33
|
+
start: internalMutationGeneric({
|
|
34
|
+
args: { source: v.string() },
|
|
35
|
+
returns: v.any(),
|
|
36
|
+
handler: async (ctx, { source }) => {
|
|
37
|
+
const now = Math.floor(Date.now() / 1000);
|
|
38
|
+
const event = await ctx.runMutation(
|
|
39
|
+
nested.betterStripe.usage.mutations.record,
|
|
40
|
+
{
|
|
41
|
+
inputJson: JSON.stringify({
|
|
42
|
+
scope,
|
|
43
|
+
binding: {
|
|
44
|
+
stripeCustomerId: "cus_buyer",
|
|
45
|
+
stripePriceId: "price_calls",
|
|
46
|
+
eventName: "calls",
|
|
47
|
+
plan: {
|
|
48
|
+
key: "calls",
|
|
49
|
+
version: "v1",
|
|
50
|
+
model: "pay_as_you_go",
|
|
51
|
+
unit: { name: "call", precision: 0 },
|
|
52
|
+
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
sourceEventId: source,
|
|
56
|
+
quantity: "2",
|
|
57
|
+
occurredAt: now,
|
|
58
|
+
}),
|
|
59
|
+
subscriptionJson: JSON.stringify({
|
|
60
|
+
subscriptionId: "sub_calls",
|
|
61
|
+
subscriptionItemId: "si_calls",
|
|
62
|
+
stripePriceId: "price_calls",
|
|
63
|
+
period: { start: now - 3600, end: now + 86400 },
|
|
64
|
+
interval: "month",
|
|
65
|
+
}),
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
return new BetterStripe(
|
|
69
|
+
nested.betterStripe as unknown as Component,
|
|
70
|
+
).usage.startDelivery(ctx, {
|
|
71
|
+
scope,
|
|
72
|
+
eventId: event.eventId,
|
|
73
|
+
provider: anyApi.host.provider as UsageDeliveryStartOptions["provider"],
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
provider: internalActionGeneric({
|
|
78
|
+
args: {
|
|
79
|
+
scope: v.any(),
|
|
80
|
+
eventId: v.string(),
|
|
81
|
+
attempt: v.number(),
|
|
82
|
+
definitionVersion: v.number(),
|
|
83
|
+
apiVersion: v.string(),
|
|
84
|
+
identifier: v.string(),
|
|
85
|
+
},
|
|
86
|
+
returns: v.any(),
|
|
87
|
+
handler: async (_ctx, args) => {
|
|
88
|
+
calls++;
|
|
89
|
+
if (providerThrows)
|
|
90
|
+
throw new Error("sk_test_PRIVATE private provider body");
|
|
91
|
+
const outcome = outcomes.shift() ?? {
|
|
92
|
+
status: "accepted",
|
|
93
|
+
created: Math.floor(Date.now() / 1000),
|
|
94
|
+
};
|
|
95
|
+
return { ...args, outcome };
|
|
96
|
+
},
|
|
97
|
+
}),
|
|
98
|
+
};
|
|
99
|
+
async function fixture() {
|
|
100
|
+
const t = convexTest(defineSchema({}), {
|
|
101
|
+
"./_generated/api.ts": async () => ({}),
|
|
102
|
+
"./host.ts": async () => host,
|
|
103
|
+
});
|
|
104
|
+
t.registerComponent("betterStripe", schema, {
|
|
105
|
+
...modules,
|
|
106
|
+
"./component/usageDelivery/steps.ts": async () => ({
|
|
107
|
+
...steps,
|
|
108
|
+
save: internalMutationGeneric({
|
|
109
|
+
args: { ...request, outcome: outcomeValidator },
|
|
110
|
+
returns: v.any(),
|
|
111
|
+
handler: async (ctx, args) => {
|
|
112
|
+
if (saveFailures-- > 0)
|
|
113
|
+
throw new Error("Usage receipt projection failed");
|
|
114
|
+
return (
|
|
115
|
+
steps.save as unknown as {
|
|
116
|
+
_handler: (ctx: unknown, args: unknown) => Promise<unknown>;
|
|
117
|
+
}
|
|
118
|
+
)._handler(ctx, args);
|
|
119
|
+
},
|
|
120
|
+
}),
|
|
121
|
+
}),
|
|
122
|
+
"./component/fixture.ts": async () => ({
|
|
123
|
+
history: internalQueryGeneric({
|
|
124
|
+
args: {},
|
|
125
|
+
returns: v.any(),
|
|
126
|
+
handler: async (ctx) => {
|
|
127
|
+
const op = (await ctx.db.query("usageOutbox").first())!;
|
|
128
|
+
return executionWorkflow.listSteps(ctx, op.workflowId!, {
|
|
129
|
+
paginationOpts: { cursor: null, numItems: 100 },
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
}),
|
|
134
|
+
});
|
|
135
|
+
workflowTest.register(t, "betterStripe/workflow");
|
|
136
|
+
return t;
|
|
137
|
+
}
|
|
138
|
+
beforeEach(() => {
|
|
139
|
+
vi.useFakeTimers();
|
|
140
|
+
calls = 0;
|
|
141
|
+
outcomes = [];
|
|
142
|
+
saveFailures = 0;
|
|
143
|
+
providerThrows = false;
|
|
144
|
+
});
|
|
145
|
+
afterEach(() => vi.useRealTimers());
|
|
146
|
+
it("dispatches duplicate requests once through the existing nested Workflow", async () => {
|
|
147
|
+
const t = await fixture();
|
|
148
|
+
const first = await t.mutation(anyApi.host.start, { source: "one" });
|
|
149
|
+
const replay = await t.mutation(anyApi.host.start, { source: "one" });
|
|
150
|
+
expect(replay.eventId).toBe(first.eventId);
|
|
151
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
152
|
+
expect(calls).toBe(1);
|
|
153
|
+
expect(
|
|
154
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
155
|
+
scope,
|
|
156
|
+
eventId: first.eventId,
|
|
157
|
+
}),
|
|
158
|
+
).toMatchObject({ state: "provider_accepted", attempts: 1 });
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("backs off known rate limits without losing partial-batch progress", async () => {
|
|
162
|
+
const t = await fixture();
|
|
163
|
+
outcomes = [
|
|
164
|
+
{ status: "rate_limited" },
|
|
165
|
+
{ status: "accepted", created: Math.floor(Date.now() / 1000) },
|
|
166
|
+
];
|
|
167
|
+
const a = await t.mutation(anyApi.host.start, { source: "a" });
|
|
168
|
+
const b = await t.mutation(anyApi.host.start, { source: "b" });
|
|
169
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
170
|
+
expect(calls).toBe(3);
|
|
171
|
+
for (const op of [a, b])
|
|
172
|
+
expect(
|
|
173
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
174
|
+
scope,
|
|
175
|
+
eventId: op.eventId,
|
|
176
|
+
}),
|
|
177
|
+
).toMatchObject({ state: "provider_accepted" });
|
|
178
|
+
});
|
|
179
|
+
it("holds unknown effects and refuses blind resume even inside the dedupe window", async () => {
|
|
180
|
+
const t = await fixture();
|
|
181
|
+
outcomes = [{ status: "unknown" }];
|
|
182
|
+
const op = await t.mutation(anyApi.host.start, { source: "unknown" });
|
|
183
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
184
|
+
expect(
|
|
185
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
186
|
+
scope,
|
|
187
|
+
eventId: op.eventId,
|
|
188
|
+
}),
|
|
189
|
+
).toMatchObject({ state: "unknown", reason: "provider_outcome_unknown" });
|
|
190
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
191
|
+
scope,
|
|
192
|
+
eventId: op.eventId,
|
|
193
|
+
});
|
|
194
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
195
|
+
expect(calls).toBe(1);
|
|
196
|
+
});
|
|
197
|
+
it("rejects foreign scope controls and preserves acceptance when held", async () => {
|
|
198
|
+
const t = await fixture();
|
|
199
|
+
const op = await t.mutation(anyApi.host.start, { source: "held" });
|
|
200
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.hold, {
|
|
201
|
+
scope,
|
|
202
|
+
eventId: op.eventId,
|
|
203
|
+
});
|
|
204
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
205
|
+
expect(calls).toBe(0);
|
|
206
|
+
expect(
|
|
207
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
208
|
+
scope,
|
|
209
|
+
eventId: op.eventId,
|
|
210
|
+
}),
|
|
211
|
+
).toMatchObject({ state: "held", held: true });
|
|
212
|
+
await expect(
|
|
213
|
+
t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
214
|
+
scope: { ...scope, customerAccount: "acct_foreign" },
|
|
215
|
+
eventId: op.eventId,
|
|
216
|
+
}),
|
|
217
|
+
).rejects.toThrow();
|
|
218
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
219
|
+
scope,
|
|
220
|
+
eventId: op.eventId,
|
|
221
|
+
});
|
|
222
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
223
|
+
expect(calls).toBe(1);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("retains transport acceptance alongside scoped asynchronous validation errors", async () => {
|
|
227
|
+
const t = await fixture();
|
|
228
|
+
const op = await t.mutation(anyApi.host.start, { source: "async" });
|
|
229
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
230
|
+
const receipt = await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
231
|
+
scope,
|
|
232
|
+
eventId: op.eventId,
|
|
233
|
+
});
|
|
234
|
+
const report = {
|
|
235
|
+
stripeAccountId: scope.stripeAccountId,
|
|
236
|
+
livemode: false,
|
|
237
|
+
providerEventId: "evt_error",
|
|
238
|
+
meterId: scope.meterId,
|
|
239
|
+
samples: [
|
|
240
|
+
{
|
|
241
|
+
identifier: receipt.providerIdentifier,
|
|
242
|
+
code: "meter_event_customer_not_found",
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
truncated: false,
|
|
246
|
+
};
|
|
247
|
+
await t.mutation(
|
|
248
|
+
nested.betterStripe.usageDelivery.mutations.recordErrorReport,
|
|
249
|
+
report,
|
|
250
|
+
);
|
|
251
|
+
await t.mutation(
|
|
252
|
+
nested.betterStripe.usageDelivery.mutations.recordErrorReport,
|
|
253
|
+
report,
|
|
254
|
+
);
|
|
255
|
+
const final = await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
256
|
+
scope,
|
|
257
|
+
eventId: op.eventId,
|
|
258
|
+
});
|
|
259
|
+
expect(final).toMatchObject({
|
|
260
|
+
state: "provider_accepted",
|
|
261
|
+
validationErrors: [
|
|
262
|
+
{ code: "meter_event_customer_not_found", providerEventId: "evt_error" },
|
|
263
|
+
],
|
|
264
|
+
});
|
|
265
|
+
expect(final.validationErrors).toHaveLength(1);
|
|
266
|
+
expect(
|
|
267
|
+
await t.mutation(
|
|
268
|
+
nested.betterStripe.usageDelivery.mutations.recordErrorReport,
|
|
269
|
+
{
|
|
270
|
+
...report,
|
|
271
|
+
stripeAccountId: "acct_foreign",
|
|
272
|
+
providerEventId: "evt_foreign",
|
|
273
|
+
},
|
|
274
|
+
),
|
|
275
|
+
).toMatchObject({ matched: 0, unattributed: 1 });
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("restarts a failed receipt projection without repeating the provider step", async () => {
|
|
279
|
+
const t = await fixture();
|
|
280
|
+
saveFailures = 1;
|
|
281
|
+
const op = await t.mutation(anyApi.host.start, { source: "projection" });
|
|
282
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
283
|
+
expect(calls).toBe(1);
|
|
284
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
285
|
+
scope,
|
|
286
|
+
eventId: op.eventId,
|
|
287
|
+
});
|
|
288
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
289
|
+
expect(calls).toBe(1);
|
|
290
|
+
expect(
|
|
291
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
292
|
+
scope,
|
|
293
|
+
eventId: op.eventId,
|
|
294
|
+
}),
|
|
295
|
+
).toMatchObject({ state: "provider_accepted" });
|
|
296
|
+
});
|
|
297
|
+
it("holds provider exceptions without journaling private data", async () => {
|
|
298
|
+
const t = await fixture();
|
|
299
|
+
providerThrows = true;
|
|
300
|
+
const op = await t.mutation(anyApi.host.start, { source: "private" });
|
|
301
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
302
|
+
expect(
|
|
303
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
304
|
+
scope,
|
|
305
|
+
eventId: op.eventId,
|
|
306
|
+
}),
|
|
307
|
+
).toMatchObject({ state: "unknown" });
|
|
308
|
+
const journal = JSON.stringify(
|
|
309
|
+
await t.query(nested.betterStripe.fixture.history, {}),
|
|
310
|
+
);
|
|
311
|
+
expect(journal).not.toContain("sk_test_PRIVATE");
|
|
312
|
+
expect(journal).not.toContain("private provider body");
|
|
313
|
+
});
|
|
314
|
+
it("stops retryable failures after three sends and supports explicit credential recovery", async () => {
|
|
315
|
+
const t = await fixture();
|
|
316
|
+
outcomes = [{ status: "unavailable" }];
|
|
317
|
+
const op = await t.mutation(anyApi.host.start, { source: "unavailable" });
|
|
318
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
319
|
+
expect(calls).toBe(1);
|
|
320
|
+
expect(
|
|
321
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
322
|
+
scope,
|
|
323
|
+
eventId: op.eventId,
|
|
324
|
+
}),
|
|
325
|
+
).toMatchObject({ state: "held", reason: "provider_unavailable" });
|
|
326
|
+
outcomes = [
|
|
327
|
+
{ status: "rate_limited" },
|
|
328
|
+
{ status: "rate_limited" },
|
|
329
|
+
{ status: "rate_limited" },
|
|
330
|
+
];
|
|
331
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
332
|
+
scope,
|
|
333
|
+
eventId: op.eventId,
|
|
334
|
+
});
|
|
335
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
336
|
+
expect(calls).toBe(4);
|
|
337
|
+
expect(
|
|
338
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
339
|
+
scope,
|
|
340
|
+
eventId: op.eventId,
|
|
341
|
+
}),
|
|
342
|
+
).toMatchObject({ state: "held", reason: "retry_exhausted" });
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("holds an expired deduplication window without another provider call", async () => {
|
|
346
|
+
const t = await fixture();
|
|
347
|
+
outcomes = [{ status: "unavailable" }];
|
|
348
|
+
const op = await t.mutation(anyApi.host.start, { source: "expired" });
|
|
349
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
350
|
+
vi.setSystemTime(Date.now() + 24 * 3600 * 1000);
|
|
351
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
352
|
+
scope,
|
|
353
|
+
eventId: op.eventId,
|
|
354
|
+
});
|
|
355
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
356
|
+
expect(calls).toBe(1);
|
|
357
|
+
expect(
|
|
358
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
359
|
+
scope,
|
|
360
|
+
eventId: op.eventId,
|
|
361
|
+
}),
|
|
362
|
+
).toMatchObject({ state: "unknown", reason: "delivery_window_expired" });
|
|
363
|
+
});
|
|
364
|
+
it("holds an event whose native timestamp window elapsed before first dispatch", async () => {
|
|
365
|
+
const t = await fixture();
|
|
366
|
+
const op = await t.mutation(anyApi.host.start, { source: "late" });
|
|
367
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.hold, {
|
|
368
|
+
scope,
|
|
369
|
+
eventId: op.eventId,
|
|
370
|
+
});
|
|
371
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
372
|
+
vi.setSystemTime(Date.now() + 36 * 86400 * 1000);
|
|
373
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
374
|
+
scope,
|
|
375
|
+
eventId: op.eventId,
|
|
376
|
+
});
|
|
377
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
378
|
+
expect(calls).toBe(0);
|
|
379
|
+
expect(
|
|
380
|
+
await t.query(nested.betterStripe.usageDelivery.queries.get, {
|
|
381
|
+
scope,
|
|
382
|
+
eventId: op.eventId,
|
|
383
|
+
}),
|
|
384
|
+
).toMatchObject({ state: "held", reason: "delivery_window_expired" });
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("clears an administrative hold on a completed event without another send", async () => {
|
|
388
|
+
const t = await fixture();
|
|
389
|
+
const op = await t.mutation(anyApi.host.start, { source: "completed-hold" });
|
|
390
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
391
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.hold, {
|
|
392
|
+
scope,
|
|
393
|
+
eventId: op.eventId,
|
|
394
|
+
});
|
|
395
|
+
expect(
|
|
396
|
+
await t.mutation(nested.betterStripe.usageDelivery.mutations.resume, {
|
|
397
|
+
scope,
|
|
398
|
+
eventId: op.eventId,
|
|
399
|
+
}),
|
|
400
|
+
).toMatchObject({ state: "provider_accepted", held: false });
|
|
401
|
+
expect(calls).toBe(1);
|
|
402
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { anyApi } from "convex/server";
|
|
2
|
+
import { executionWorkflow } from "../durableExecution/runtime.js";
|
|
3
|
+
import { refFields } from "./schema.js";
|
|
4
|
+
import { v } from "convex/values";
|
|
5
|
+
export const run = executionWorkflow
|
|
6
|
+
.define({ args: refFields, returns: v.null() })
|
|
7
|
+
.handler(async (step, args): Promise<null> => {
|
|
8
|
+
let sends = 0;
|
|
9
|
+
for (let turn = 0; turn < 40; turn++) {
|
|
10
|
+
const claimed = await step.runMutation(
|
|
11
|
+
anyApi.usageDelivery.steps.claim,
|
|
12
|
+
args,
|
|
13
|
+
{ name: `claim-${turn}` },
|
|
14
|
+
);
|
|
15
|
+
if (claimed.kind === "done") return null;
|
|
16
|
+
if (claimed.kind === "wait") {
|
|
17
|
+
await step.sleep(claimed.delayMs);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const outcome = await step.runAction(
|
|
21
|
+
anyApi.usageDelivery.bridge.invoke,
|
|
22
|
+
claimed.request,
|
|
23
|
+
{ name: `provider-${turn}`, retry: false },
|
|
24
|
+
);
|
|
25
|
+
const saved = await step.runMutation(
|
|
26
|
+
anyApi.usageDelivery.steps.save,
|
|
27
|
+
{ ...claimed.request, outcome },
|
|
28
|
+
{ name: "save-receipt" },
|
|
29
|
+
);
|
|
30
|
+
sends++;
|
|
31
|
+
if (saved.state !== "queued") return null;
|
|
32
|
+
if (sends >= 3) break;
|
|
33
|
+
}
|
|
34
|
+
await step.runMutation(anyApi.usageDelivery.steps.exhaust, args);
|
|
35
|
+
return null;
|
|
36
|
+
});
|