@fantastic.dev/better-stripe 0.92.0 → 0.94.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 (96) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/operationJournal.d.ts +41 -0
  4. package/dist/client/billing/renewalAttach/internal/operationJournal.d.ts.map +1 -0
  5. package/dist/client/billing/renewalAttach/internal/operationJournal.js +378 -0
  6. package/dist/client/billing/renewalAttach/internal/operationJournal.js.map +1 -0
  7. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +44 -0
  8. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -0
  9. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +251 -0
  10. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -0
  11. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +562 -0
  12. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  13. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +281 -0
  14. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  15. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +562 -0
  16. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  17. package/dist/client/connect/payoutSyncTestHelpers.d.ts +281 -0
  18. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  19. package/dist/client/index.d.ts +3 -2
  20. package/dist/client/index.d.ts.map +1 -1
  21. package/dist/client/index.js +16 -3
  22. package/dist/client/index.js.map +1 -1
  23. package/dist/component/_generated/api.d.ts +6 -0
  24. package/dist/component/_generated/api.d.ts.map +1 -1
  25. package/dist/component/_generated/api.js.map +1 -1
  26. package/dist/component/_generated/component.d.ts +406 -0
  27. package/dist/component/_generated/component.d.ts.map +1 -1
  28. package/dist/component/balanceFunding/mutations.d.ts +27 -27
  29. package/dist/component/balanceFunding/queries.d.ts +1 -1
  30. package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
  31. package/dist/component/earnings/triggers.d.ts +294 -0
  32. package/dist/component/earnings/triggers.d.ts.map +1 -1
  33. package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
  34. package/dist/component/renewalOperations/helpers.d.ts +236 -0
  35. package/dist/component/renewalOperations/helpers.d.ts.map +1 -0
  36. package/dist/component/renewalOperations/helpers.js +457 -0
  37. package/dist/component/renewalOperations/helpers.js.map +1 -0
  38. package/dist/component/renewalOperations/mutations.d.ts +252 -0
  39. package/dist/component/renewalOperations/mutations.d.ts.map +1 -0
  40. package/dist/component/renewalOperations/mutations.js +340 -0
  41. package/dist/component/renewalOperations/mutations.js.map +1 -0
  42. package/dist/component/renewalOperations/queries.d.ts +163 -0
  43. package/dist/component/renewalOperations/queries.d.ts.map +1 -0
  44. package/dist/component/renewalOperations/queries.js +167 -0
  45. package/dist/component/renewalOperations/queries.js.map +1 -0
  46. package/dist/component/renewalOperations/schema.d.ts +1988 -0
  47. package/dist/component/renewalOperations/schema.d.ts.map +1 -0
  48. package/dist/component/renewalOperations/schema.js +227 -0
  49. package/dist/component/renewalOperations/schema.js.map +1 -0
  50. package/dist/component/schema.d.ts +281 -0
  51. package/dist/component/schema.js +5 -0
  52. package/dist/component/schema.js.map +1 -1
  53. package/dist/component/usage/allowance.fixtures.d.ts +281 -0
  54. package/dist/component/usage/allowance.fixtures.d.ts.map +1 -1
  55. package/dist/component/usageCredits/accounting.fixtures.d.ts +297 -16
  56. package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -1
  57. package/dist/shared/renewalOperationJournal.d.ts +43 -0
  58. package/dist/shared/renewalOperationJournal.d.ts.map +1 -0
  59. package/dist/shared/renewalOperationJournal.js +294 -0
  60. package/dist/shared/renewalOperationJournal.js.map +1 -0
  61. package/docs/renewal-context-reads.md +180 -0
  62. package/package.json +1 -1
  63. package/src/client/billing/renewalAttach/internal/operationJournal.acknowledgement.integration.test.ts +500 -0
  64. package/src/client/billing/renewalAttach/internal/operationJournal.c10.integration.test.ts +560 -0
  65. package/src/client/billing/renewalAttach/internal/operationJournal.controls.integration.test.ts +356 -0
  66. package/src/client/billing/renewalAttach/internal/operationJournal.integration.test.ts +560 -0
  67. package/src/client/billing/renewalAttach/internal/operationJournal.methods.integration.test.ts +534 -0
  68. package/src/client/billing/renewalAttach/internal/operationJournal.ts +561 -0
  69. package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +334 -0
  70. package/src/client/billing/renewalAttach/internal/producer/producerGuards.integration.test.ts +323 -0
  71. package/src/client/billing/renewalAttach/internal/producer/producerInitial.integration.test.ts +301 -0
  72. package/src/client/billing/renewalAttach/internal/producer/producerIsolation.integration.test.ts +531 -0
  73. package/src/client/index.ts +24 -3
  74. package/src/component/_generated/api.ts +6 -0
  75. package/src/component/_generated/component.ts +450 -0
  76. package/src/component/renewalOperations/acknowledgement.fixtures.ts +552 -0
  77. package/src/component/renewalOperations/acknowledgement.test.ts +573 -0
  78. package/src/component/renewalOperations/acknowledgementReads.test.ts +442 -0
  79. package/src/component/renewalOperations/acknowledgementV2.test.ts +493 -0
  80. package/src/component/renewalOperations/acknowledgementV2Publication.test.ts +373 -0
  81. package/src/component/renewalOperations/basisBinding.test.ts +567 -0
  82. package/src/component/renewalOperations/basisLifecycle.test.ts +569 -0
  83. package/src/component/renewalOperations/boundHistory.test.ts +474 -0
  84. package/src/component/renewalOperations/helpers.ts +592 -0
  85. package/src/component/renewalOperations/historyReads.test.ts +595 -0
  86. package/src/component/renewalOperations/initialBasis.test.ts +578 -0
  87. package/src/component/renewalOperations/journal.test.ts +567 -0
  88. package/src/component/renewalOperations/mutations.ts +456 -0
  89. package/src/component/renewalOperations/payload.test.ts +589 -0
  90. package/src/component/renewalOperations/payloadEncoding.test.ts +592 -0
  91. package/src/component/renewalOperations/queries.ts +204 -0
  92. package/src/component/renewalOperations/schema.ts +268 -0
  93. package/src/component/renewalOperations/timingPolicy.fixtures.ts +193 -0
  94. package/src/component/renewalOperations/timingPolicy.test.ts +523 -0
  95. package/src/component/schema.ts +11 -0
  96. package/src/shared/renewalOperationJournal.ts +333 -0
@@ -0,0 +1,373 @@
1
+ import { canonicalJournalEvent } from "../../shared/renewalOperationJournal.js";
2
+ import { captureAttachReceipt } from "../../client/billing/renewalAttach/internal/c10/eventCapture.js";
3
+ import {
4
+ materialize,
5
+ eventDigest,
6
+ objectNode,
7
+ scalarNode,
8
+ } from "../../client/billing/renewalAttach/internal/c10/eventCodec.js";
9
+ import assert from "node:assert/strict";
10
+ import { makeFunctionReference } from "convex/server";
11
+ import { getConvexSize, getDocumentSize, type Infer } from "convex/values";
12
+ import { afterEach, expect, it, vi } from "vitest";
13
+ import type {
14
+ providerAcknowledgementReadResult,
15
+ retentionReceipt,
16
+ } from "./schema.js";
17
+ import {
18
+ prepareV2AcknowledgementScenario,
19
+ append,
20
+ retain,
21
+ canonical,
22
+ VALUE_LIMIT,
23
+ } from "./acknowledgement.fixtures.js";
24
+
25
+ const readAcknowledgement = makeFunctionReference<
26
+ "query",
27
+ { operationId: Infer<typeof retentionReceipt>["operationId"] },
28
+ Infer<typeof providerAcknowledgementReadResult>
29
+ >("renewalOperations/queries:readProviderAcknowledgement");
30
+ afterEach(() => vi.unstubAllGlobals());
31
+
32
+ const bind = makeFunctionReference<"mutation">(
33
+ "renewalOperations/mutations:bindInitialBasis",
34
+ );
35
+ const load = makeFunctionReference<"query">(
36
+ "renewalOperations/queries:loadAuthorizedOperation",
37
+ );
38
+ const manifest = makeFunctionReference<"query">(
39
+ "renewalOperations/queries:readAttempt",
40
+ );
41
+ const readEvent = makeFunctionReference<"query">(
42
+ "renewalOperations/queries:readStageEvent",
43
+ );
44
+ const reserve = makeFunctionReference<"mutation">(
45
+ "renewalOperations/mutations:reserveAttempt",
46
+ );
47
+ const create = makeFunctionReference<"mutation">(
48
+ "renewalOperations/mutations:createAuthorizedOperation",
49
+ );
50
+ const contradictoryValue = "different-retained-full-body";
51
+
52
+ async function retainedScenario() {
53
+ const h = await prepareV2AcknowledgementScenario();
54
+ expect(h.scenario).toBe("clean");
55
+ expect(h.shouldRetain).toBe(true);
56
+ expect(h.before.operations[0].recordVersion).toBe(2);
57
+ const initialBasis = h.before.operations[0].initialBasis;
58
+ assert(initialBasis);
59
+ expect(h.before.attempts[0].lastSequence).toBe(2);
60
+ expect(h.commits.map((row) => row.sequence)).toEqual([1, 2]);
61
+ const retained = await h.t.mutation(retain, h.args);
62
+ assert(retained !== null);
63
+ const known = {
64
+ schemaVersion: 1 as const,
65
+ stage: "attach" as const,
66
+ attemptId: h.acquired.reservation.attemptId,
67
+ sourcePayloadId: h.acquired.reservation.payloadId,
68
+ dispatchEventId: h.commits[0].eventId,
69
+ dispatchPayloadId: h.commits[0].payloadId,
70
+ responseEventId: h.commits[1].eventId,
71
+ responsePayloadId: h.commits[1].payloadId,
72
+ receiptPayloadId: retained.receiptPayloadId,
73
+ receiptBinding: h.receipt.binding,
74
+ };
75
+ expect(retained).toEqual({ status: "retained", ...h.queryArgs, ...known });
76
+ expect(getConvexSize(retained)).toBeLessThanOrEqual(VALUE_LIMIT);
77
+ const after = await h.physical();
78
+ expect(Object.values(after).map((rows) => rows.length)).toEqual([1, 1, 2, 4]);
79
+ expect(after.operations).toEqual([
80
+ { ...h.before.operations[0], knownAcknowledgement: known },
81
+ ]);
82
+ expect(after.attempts).toEqual(h.before.attempts);
83
+ expect(after.events).toEqual(h.before.events);
84
+ for (const old of h.before.payloads)
85
+ expect(after.payloads.find((row) => row._id === old._id)).toEqual(old);
86
+ const payload = after.payloads.find(
87
+ (row) => row._id === retained.receiptPayloadId,
88
+ );
89
+ assert(payload);
90
+ expect(payload).toEqual({
91
+ _id: retained.receiptPayloadId,
92
+ _creationTime: payload._creationTime,
93
+ codecVersion: 1,
94
+ json: h.receiptJSON,
95
+ owner: { kind: "receipt", ...h.ownerScope },
96
+ });
97
+ for (const group of Object.values(after))
98
+ for (const row of group)
99
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
100
+ const expectedRead = { retention: retained, receiptJSON: h.receiptJSON };
101
+ expect(await h.t.query(readAcknowledgement, h.queryArgs)).toEqual(
102
+ expectedRead,
103
+ );
104
+ const { recordJSON, ...summary } = h.acquired.reservation;
105
+ const loaded = await h.t.query(load, h.queryArgs);
106
+ expect(loaded).toEqual({
107
+ recordVersion: 2,
108
+ ...h.queryArgs,
109
+ recordJSON,
110
+ revision: 1,
111
+ activeAttempt: summary,
112
+ initialBasis,
113
+ });
114
+ const bindArgs = {
115
+ ...h.queryArgs,
116
+ attemptId: known.attemptId,
117
+ policyJSON: initialBasis.policyJSON,
118
+ basisJSON: initialBasis.basisJSON,
119
+ };
120
+ const historyArgs = { ...h.queryArgs, attemptId: known.attemptId };
121
+ const reserveArgs = {
122
+ ...h.queryArgs,
123
+ expectedRevision: 0,
124
+ stage: "attach" as const,
125
+ expectedRequestBinding: h.acquired.reservation.requestBinding,
126
+ recordJSON,
127
+ };
128
+ const createArgs = {
129
+ authorizationScope: h.acquired.reservation.authorizationScope,
130
+ logicalOperationId: h.acquired.reservation.logicalOperationId,
131
+ authorizationRevision: h.acquired.reservation.authorizationRevision,
132
+ recordJSON,
133
+ };
134
+ const first = h.before.payloads.find(
135
+ (row) => row._id === known.dispatchPayloadId,
136
+ );
137
+ assert(first);
138
+ const duplicateArgs = {
139
+ reservation: h.acquired.reservation,
140
+ expectedSequence: 0,
141
+ event: {
142
+ sequence: 1 as const,
143
+ binding: h.commits[0].binding,
144
+ dataJSON: first.json,
145
+ },
146
+ };
147
+ for (const args of [
148
+ bindArgs,
149
+ historyArgs,
150
+ reserveArgs,
151
+ createArgs,
152
+ duplicateArgs,
153
+ { ...historyArgs, sequence: 3 },
154
+ ])
155
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
156
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
157
+ expect(getConvexSize(expectedRead)).toBeLessThanOrEqual(VALUE_LIMIT);
158
+ expect(await h.physical()).toEqual(after);
159
+ expect(h.fetchCalls()).toBe(0);
160
+ return {
161
+ ...h,
162
+ retained,
163
+ known,
164
+ after,
165
+ expectedRead,
166
+ loaded,
167
+ bindArgs,
168
+ historyArgs,
169
+ reserveArgs,
170
+ createArgs,
171
+ duplicateArgs,
172
+ };
173
+ }
174
+
175
+ type Retained = Awaited<ReturnType<typeof retainedScenario>>;
176
+ function contradictoryReceipt(h: Retained) {
177
+ const metadata = h.receipt.providerSnapshot.metadata;
178
+ assert(metadata && typeof metadata === "object" && !Array.isArray(metadata));
179
+ return {
180
+ ...h.receipt,
181
+ providerSnapshot: {
182
+ ...h.receipt.providerSnapshot,
183
+ metadata: { ...metadata, opaque: contradictoryValue },
184
+ },
185
+ };
186
+ }
187
+ async function thirdArgs(h: Retained, receipt: Retained["receipt"]) {
188
+ const datum = {
189
+ event: "receipt",
190
+ stage: "attach",
191
+ request: h.request,
192
+ receipt,
193
+ };
194
+ const node = captureAttachReceipt(datum);
195
+ expect(materialize(node)).toEqual(datum);
196
+ const full = await canonicalJournalEvent(3, canonical(datum));
197
+ expect(full.binding).toBe(
198
+ await eventDigest(
199
+ objectNode([
200
+ ["sequence", scalarNode(3)],
201
+ ["data", node],
202
+ ]),
203
+ ),
204
+ );
205
+ const args = {
206
+ reservation: h.acquired.reservation,
207
+ expectedSequence: 2,
208
+ event: { sequence: 3 as const, binding: full.binding, dataJSON: full.json },
209
+ };
210
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
211
+ return args;
212
+ }
213
+
214
+ async function inspectStable(
215
+ h: Retained,
216
+ rows: Retained["after"],
217
+ failed: boolean,
218
+ label: string,
219
+ ) {
220
+ try {
221
+ expect(await h.physical()).toEqual(rows);
222
+ expect(h.fetchCalls()).toBe(0);
223
+ } catch (error) {
224
+ if (!failed) throw error;
225
+ console.info(label);
226
+ }
227
+ }
228
+ async function checkStableKnown(h: Retained, expectedEvent: unknown = null) {
229
+ const events = h.commits.map(({ reservation, ...event }) => {
230
+ expect(reservation).toEqual(h.acquired.reservation);
231
+ return event;
232
+ });
233
+ const settled = await Promise.allSettled([
234
+ h.t.mutation(retain, h.args),
235
+ h.t.mutation(bind, h.bindArgs),
236
+ h.t.query(readAcknowledgement, h.queryArgs),
237
+ h.t.query(load, h.queryArgs),
238
+ h.t.query(manifest, h.historyArgs),
239
+ h.t.query(readEvent, { ...h.historyArgs, sequence: 3 }),
240
+ h.t.mutation(reserve, h.reserveArgs),
241
+ h.t.mutation(create, h.createArgs),
242
+ h.t.mutation(append, h.duplicateArgs),
243
+ ]);
244
+ const values = settled.map((item) => {
245
+ if (item.status !== "fulfilled") throw item.reason;
246
+ expect(getConvexSize(item.value)).toBeLessThanOrEqual(VALUE_LIMIT);
247
+ return item.value;
248
+ });
249
+ expect(values).toEqual([
250
+ h.retained,
251
+ { status: "bound", ...h.queryArgs, initialBasis: h.loaded.initialBasis },
252
+ h.expectedRead,
253
+ h.loaded,
254
+ { reservation: h.acquired.reservation, events },
255
+ expectedEvent,
256
+ {
257
+ disposition: "existing",
258
+ committedRevision: 1,
259
+ reservation: h.acquired.reservation,
260
+ },
261
+ { status: "existing", ...h.queryArgs },
262
+ h.commits[0],
263
+ ]);
264
+ expect(await h.physical()).toEqual(h.after);
265
+ }
266
+
267
+ it("ACKV203 publishes full matching typed event3 then keeps stale2 retention stable", async () => {
268
+ const h = await retainedScenario();
269
+ const args = await thirdArgs(h, h.receipt);
270
+ let stable = h.after;
271
+ let targetFailed = false;
272
+ try {
273
+ const committed = await h.t.mutation(append, args);
274
+ const descriptor = {
275
+ ...h.ownerScope,
276
+ sourcePayloadId: h.known.sourcePayloadId,
277
+ sequence: 3,
278
+ binding: args.event.binding,
279
+ payloadId: committed.payloadId,
280
+ eventId: committed.eventId,
281
+ };
282
+ expect(committed).toEqual({
283
+ ...descriptor,
284
+ reservation: h.acquired.reservation,
285
+ });
286
+ expect(getConvexSize(committed)).toBeLessThanOrEqual(VALUE_LIMIT);
287
+ const after = await h.physical();
288
+ stable = after;
289
+ expect(Object.values(after).map((rows) => rows.length)).toEqual([
290
+ 1, 1, 3, 5,
291
+ ]);
292
+ expect(after.operations).toEqual(h.after.operations);
293
+ expect(after.attempts).toEqual([
294
+ { ...h.after.attempts[0], lastSequence: 3 },
295
+ ]);
296
+ for (const row of h.after.payloads)
297
+ expect(after.payloads.find((x) => x._id === row._id)).toEqual(row);
298
+ for (const row of h.after.events)
299
+ expect(after.events.find((x) => x._id === row._id)).toEqual(row);
300
+ const payload = after.payloads.find(
301
+ (row) => row._id === committed.payloadId,
302
+ );
303
+ assert(payload);
304
+ expect(payload).toEqual({
305
+ _id: committed.payloadId,
306
+ _creationTime: payload._creationTime,
307
+ codecVersion: 1,
308
+ json: args.event.dataJSON,
309
+ owner: { kind: "event", ...h.ownerScope, sequence: 3 },
310
+ });
311
+ const event = after.events.find((row) => row._id === committed.eventId);
312
+ assert(event);
313
+ const { eventId, ...fields } = descriptor;
314
+ expect(event).toEqual({
315
+ ...fields,
316
+ _id: eventId,
317
+ _creationTime: event._creationTime,
318
+ });
319
+ for (const group of Object.values(after))
320
+ for (const row of group)
321
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
322
+ await checkStableKnown(
323
+ { ...h, after, commits: [...h.commits, committed] },
324
+ { ...descriptor, dataJSON: args.event.dataJSON },
325
+ );
326
+ } catch (error) {
327
+ targetFailed = true;
328
+ throw error;
329
+ } finally {
330
+ await inspectStable(h, stable, targetFailed, "ACKV203_INSPECTION_FAILED");
331
+ }
332
+ });
333
+
334
+ it.each(["contradictory", "malformed"] as const)(
335
+ "ACKV204 refuses %s incoming event3 without writes",
336
+ async (mode) => {
337
+ const h = await retainedScenario();
338
+ // Copied binding is invalid host C3 DATA; {} is not a typed C10 event.
339
+ const candidate = contradictoryReceipt(h);
340
+ expect(candidate.binding).toBe(h.receipt.binding);
341
+ expect(canonical(candidate)).not.toBe(h.receiptJSON);
342
+ const malformed = await canonicalJournalEvent(3, "{}");
343
+ const args =
344
+ mode === "contradictory"
345
+ ? await thirdArgs(h, candidate)
346
+ : {
347
+ reservation: h.acquired.reservation,
348
+ expectedSequence: 2,
349
+ event: {
350
+ sequence: 3 as const,
351
+ binding: malformed.binding,
352
+ dataJSON: malformed.json,
353
+ },
354
+ };
355
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
356
+ let targetFailed = false;
357
+ try {
358
+ await expect(h.t.mutation(append, args)).rejects.toThrow(
359
+ /^RENEWAL_JOURNAL_CONFLICT$/,
360
+ );
361
+ } catch (error) {
362
+ targetFailed = true;
363
+ throw error;
364
+ } finally {
365
+ await inspectStable(
366
+ h,
367
+ h.after,
368
+ targetFailed,
369
+ "ACKV204_INSPECTION_FAILED",
370
+ );
371
+ }
372
+ },
373
+ );