@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,356 @@
1
+ import assert from "node:assert/strict";
2
+ import { components } from "../../../../../example/convex/_generated/api.js";
3
+ import { createActionOperationJournal } from "./operationJournal.js";
4
+ import { convexTest } from "convex-test";
5
+ import {
6
+ actionGeneric,
7
+ mutationGeneric,
8
+ componentsGeneric,
9
+ defineSchema,
10
+ makeFunctionReference,
11
+ type FunctionReturnType,
12
+ } from "convex/server";
13
+ import { getConvexSize, getDocumentSize, v } from "convex/values";
14
+ import { afterEach, expect, it, vi } from "vitest";
15
+ import { modules, schema } from "../../../../test.js";
16
+ import { query } from "../../../../component/_generated/server.js";
17
+ import type { Doc } from "../../../../component/_generated/dataModel.js";
18
+ import {
19
+ canonical,
20
+ codecFixture,
21
+ VALUE_LIMIT,
22
+ } from "../../../../component/renewalOperations/timingPolicy.fixtures.js";
23
+
24
+ const refs = components.betterStripe.renewalOperations;
25
+ const nested = componentsGeneric();
26
+ type Loaded = FunctionReturnType<typeof refs.queries.loadAuthorizedOperation>;
27
+ type Reserved = FunctionReturnType<typeof refs.mutations.reserveAttempt>;
28
+ type Physical = {
29
+ operations: Doc<"renewalOperations">[];
30
+ attempts: Doc<"renewalOperationAttempts">[];
31
+ events: Doc<"renewalOperationEvents">[];
32
+ payloads: Doc<"renewalOperationPayloads">[];
33
+ };
34
+ const inspection = {
35
+ physical: query({
36
+ args: {},
37
+ handler: async (ctx): Promise<Physical> => ({
38
+ operations: await ctx.db.query("renewalOperations").take(4),
39
+ attempts: await ctx.db.query("renewalOperationAttempts").take(4),
40
+ events: await ctx.db.query("renewalOperationEvents").take(4),
41
+ payloads: await ctx.db.query("renewalOperationPayloads").take(6),
42
+ }),
43
+ }),
44
+ };
45
+ const mutationTarget = makeFunctionReference<
46
+ "mutation",
47
+ { operationId: string },
48
+ null
49
+ >("adapterControls:mutationContext");
50
+ const concurrentTarget = makeFunctionReference<
51
+ "action",
52
+ { operationId: string },
53
+ { before: Loaded; acquired: Reserved; after: Loaded }
54
+ >("adapterControls:concurrent");
55
+
56
+ afterEach(() => vi.unstubAllGlobals());
57
+
58
+ it("ADAPTER-CONTEXT refuses a genuine registered Mutation context", async () => {
59
+ const h = await codecFixture();
60
+ const progress = { entered: 0, direct: 0, refused: 0 };
61
+ const host = {
62
+ mutationContext: mutationGeneric({
63
+ args: { operationId: v.string() },
64
+ handler: async (ctx, args): Promise<null> => {
65
+ progress.entered++;
66
+ expect(await ctx.meta.getFunctionMetadata()).toEqual({
67
+ name: "adapterControls:mutationContext",
68
+ componentPath: "",
69
+ type: "mutation",
70
+ visibility: "public",
71
+ });
72
+ expect(
73
+ await ctx.runQuery(refs.queries.loadAuthorizedOperation, args),
74
+ ).toEqual({
75
+ recordVersion: 2,
76
+ ...args,
77
+ recordJSON: h.recordJSON,
78
+ revision: 0,
79
+ activeAttempt: null,
80
+ initialBasis: null,
81
+ });
82
+ progress.direct++;
83
+ // Deliberate runtime bad-context call using the actual registered ctx.
84
+ // No context/metadata/reference cast or replacement is introduced.
85
+ await expect(
86
+ Reflect.apply(createActionOperationJournal, undefined, [
87
+ ctx,
88
+ components.betterStripe,
89
+ args.operationId,
90
+ ]),
91
+ ).rejects.toThrow(/^OPERATION_JOURNAL_ACTION_UNBOUND$/);
92
+ progress.refused++;
93
+ return null;
94
+ },
95
+ }),
96
+ };
97
+ const t = convexTest(defineSchema({}), {
98
+ "./_generated/api.ts": async () => ({}),
99
+ "./adapterControls.ts": async () => host,
100
+ });
101
+ t.registerComponent("betterStripe", schema, {
102
+ ...modules,
103
+ "./component/adapterControlsInspection.ts": async () => inspection,
104
+ });
105
+ const physical = async (): Promise<Physical> =>
106
+ t.query(nested.betterStripe.adapterControlsInspection.physical, {});
107
+ expect(await physical()).toEqual({
108
+ operations: [],
109
+ attempts: [],
110
+ events: [],
111
+ payloads: [],
112
+ });
113
+ const created = await t.mutation(refs.mutations.createAuthorizedOperation, {
114
+ authorizationScope: h.record.authorization.scope,
115
+ authorizationRevision: h.record.authorization.revision,
116
+ logicalOperationId: h.record.logicalOperationId,
117
+ recordJSON: h.recordJSON,
118
+ });
119
+ assert(created.status === "created");
120
+ const before = await physical();
121
+ expect(before.operations).toHaveLength(1);
122
+ expect(before.attempts).toEqual([]);
123
+ expect(before.events).toEqual([]);
124
+ expect(before.payloads).toHaveLength(1);
125
+ expect(before.operations[0]).toMatchObject({
126
+ _id: created.operationId,
127
+ recordVersion: 2,
128
+ state: "PREPARED",
129
+ revision: 0,
130
+ payloadId: before.payloads[0]._id,
131
+ });
132
+ expect(before.payloads[0]).toMatchObject({
133
+ codecVersion: 1,
134
+ json: h.recordJSON,
135
+ owner: { kind: "operation", operationId: created.operationId },
136
+ });
137
+ for (const rows of Object.values(before))
138
+ for (const row of rows)
139
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
140
+ let failed = false;
141
+ try {
142
+ expect(
143
+ await t.mutation(mutationTarget, { operationId: created.operationId }),
144
+ ).toBeNull();
145
+ expect(progress).toEqual({ entered: 1, direct: 1, refused: 1 });
146
+ } catch (error) {
147
+ failed = true;
148
+ throw error;
149
+ } finally {
150
+ try {
151
+ expect(await physical()).toEqual(before);
152
+ expect(h.fetchCalls()).toBe(0);
153
+ console.info(
154
+ "ADAPTER_CONTEXT_SETTLEMENT",
155
+ JSON.stringify({ ...progress, physicalEqual: true, fetchCalls: 0 }),
156
+ );
157
+ } catch (error) {
158
+ if (!failed) throw error;
159
+ console.info("ADAPTER_CONTEXT_INSPECTION_FAILED");
160
+ }
161
+ }
162
+ });
163
+
164
+ it("ADAPTER-CONCURRENT keeps two genuine Action facades operation-bound", async () => {
165
+ const h = await codecFixture();
166
+ const records = [
167
+ h.recordJSON,
168
+ canonical({
169
+ ...h.record,
170
+ logicalOperationId: h.record.logicalOperationId + "-concurrent",
171
+ }),
172
+ ];
173
+ const logicalOperationIds = [
174
+ h.record.logicalOperationId,
175
+ h.record.logicalOperationId + "-concurrent",
176
+ ];
177
+ const expectedRecords = new Map<string, string>();
178
+ let captured = 0;
179
+ let releaseBoth = () => {};
180
+ const bothCaptured = new Promise<void>((resolve) => {
181
+ releaseBoth = resolve;
182
+ });
183
+ let deadline: ReturnType<typeof setTimeout> | undefined;
184
+ const host = {
185
+ concurrent: actionGeneric({
186
+ args: { operationId: v.string() },
187
+ handler: async (
188
+ ctx,
189
+ args,
190
+ ): Promise<FunctionReturnType<typeof concurrentTarget>> => {
191
+ expect(await ctx.meta.getFunctionMetadata()).toEqual({
192
+ name: "adapterControls:concurrent",
193
+ componentPath: "",
194
+ type: "action",
195
+ visibility: "public",
196
+ });
197
+ const recordJSON = expectedRecords.get(args.operationId);
198
+ assert(recordJSON);
199
+ const journal = await createActionOperationJournal(
200
+ ctx,
201
+ components.betterStripe,
202
+ args.operationId,
203
+ );
204
+ captured++;
205
+ if (captured === 2) releaseBoth();
206
+ await Promise.race([bothCaptured, barrierTimeout]);
207
+ const before = await journal.loadAuthorizedOperation();
208
+ expect(before).toEqual({
209
+ recordVersion: 2,
210
+ ...args,
211
+ recordJSON,
212
+ revision: 0,
213
+ activeAttempt: null,
214
+ initialBasis: null,
215
+ });
216
+ const acquired = await journal.reserveAttempt({
217
+ expectedRevision: 0,
218
+ stage: "attach",
219
+ expectedRequestBinding: h.record.requestBinding,
220
+ recordJSON,
221
+ });
222
+ assert(acquired.disposition === "acquired");
223
+ expect(acquired.reservation.operationId).toBe(args.operationId);
224
+ expect(acquired.reservation.recordJSON).toBe(recordJSON);
225
+ const { recordJSON: _recordJSON, ...summary } = acquired.reservation;
226
+ const after = await journal.loadAuthorizedOperation();
227
+ expect(after).toEqual({
228
+ recordVersion: 2,
229
+ ...args,
230
+ recordJSON,
231
+ revision: 1,
232
+ activeAttempt: summary,
233
+ initialBasis: null,
234
+ });
235
+ expect(getConvexSize({ before, acquired, after })).toBeLessThanOrEqual(
236
+ VALUE_LIMIT,
237
+ );
238
+ return { before, acquired, after };
239
+ },
240
+ }),
241
+ };
242
+ const t = convexTest(defineSchema({}), {
243
+ "./_generated/api.ts": async () => ({}),
244
+ "./adapterControls.ts": async () => host,
245
+ });
246
+ t.registerComponent("betterStripe", schema, {
247
+ ...modules,
248
+ "./component/adapterControlsInspection.ts": async () => inspection,
249
+ });
250
+ const physical = async (): Promise<Physical> =>
251
+ t.query(nested.betterStripe.adapterControlsInspection.physical, {});
252
+ expect(await physical()).toEqual({
253
+ operations: [],
254
+ attempts: [],
255
+ events: [],
256
+ payloads: [],
257
+ });
258
+ const operationIds: string[] = [];
259
+ for (const [index, recordJSON] of records.entries()) {
260
+ const created = await t.mutation(refs.mutations.createAuthorizedOperation, {
261
+ authorizationScope: h.record.authorization.scope,
262
+ authorizationRevision: h.record.authorization.revision,
263
+ logicalOperationId: logicalOperationIds[index],
264
+ recordJSON,
265
+ });
266
+ assert(created.status === "created");
267
+ operationIds.push(created.operationId);
268
+ expectedRecords.set(created.operationId, recordJSON);
269
+ }
270
+ expect(new Set(operationIds).size).toBe(2);
271
+ const before = await physical();
272
+ expect(before.operations).toHaveLength(2);
273
+ expect(before.attempts).toEqual([]);
274
+ expect(before.events).toEqual([]);
275
+ expect(before.payloads).toHaveLength(2);
276
+ let stable = before;
277
+ let failed = false;
278
+ const barrierTimeout = new Promise<never>((_, reject) => {
279
+ deadline = setTimeout(
280
+ () => reject(Error("ADAPTER_CONCURRENT_CAPTURE_PREREQUISITE_STOP")),
281
+ 1000,
282
+ );
283
+ });
284
+ try {
285
+ const settled = await Promise.allSettled(
286
+ operationIds.map((operationId) =>
287
+ t.action(concurrentTarget, { operationId }),
288
+ ),
289
+ );
290
+ const results = settled.map((result) => {
291
+ if (result.status === "rejected") throw result.reason;
292
+ return result.value;
293
+ });
294
+ expect(settled.map((result) => result.status)).toEqual([
295
+ "fulfilled",
296
+ "fulfilled",
297
+ ]);
298
+ expect(captured).toBe(2);
299
+ const attempts = new Set<string>();
300
+ for (let index = 0; index < results.length; index++) {
301
+ const result = results[index];
302
+ assert(result.acquired.disposition === "acquired");
303
+ expect(result.before?.operationId).toBe(operationIds[index]);
304
+ expect(result.before?.recordJSON).toBe(records[index]);
305
+ expect(result.after?.operationId).toBe(operationIds[index]);
306
+ expect(result.after?.recordJSON).toBe(records[index]);
307
+ attempts.add(result.acquired.reservation.attemptId);
308
+ }
309
+ expect(attempts.size).toBe(2);
310
+ const after = await physical();
311
+ expect(after.operations).toHaveLength(2);
312
+ expect(after.attempts).toHaveLength(2);
313
+ expect(after.events).toEqual([]);
314
+ expect(after.payloads).toEqual(before.payloads);
315
+ for (const original of before.operations) {
316
+ const result = results.find(
317
+ (value) => value.after?.operationId === original._id,
318
+ );
319
+ assert(result && result.acquired.disposition === "acquired");
320
+ const reservation = result.acquired.reservation;
321
+ expect(after.operations.find((row) => row._id === original._id)).toEqual({
322
+ ...original,
323
+ revision: 1,
324
+ state: "RESERVED",
325
+ activeAttemptId: reservation.attemptId,
326
+ });
327
+ expect(
328
+ after.attempts.find((row) => row._id === reservation.attemptId),
329
+ ).toMatchObject({
330
+ operationId: original._id,
331
+ payloadId: reservation.payloadId,
332
+ lastSequence: 0,
333
+ });
334
+ }
335
+ for (const rows of Object.values(after))
336
+ for (const row of rows)
337
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
338
+ stable = after;
339
+ } catch (error) {
340
+ failed = true;
341
+ throw error;
342
+ } finally {
343
+ if (deadline !== undefined) clearTimeout(deadline);
344
+ try {
345
+ expect(await physical()).toEqual(stable);
346
+ expect(h.fetchCalls()).toBe(0);
347
+ console.info(
348
+ "ADAPTER_CONCURRENT_SETTLEMENT",
349
+ JSON.stringify({ captured, physicalEqual: true, fetchCalls: 0 }),
350
+ );
351
+ } catch (error) {
352
+ if (!failed) throw error;
353
+ console.info("ADAPTER_CONCURRENT_INSPECTION_FAILED");
354
+ }
355
+ }
356
+ });