@fantastic.dev/better-stripe 0.92.0 → 0.93.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 (84) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +30 -0
  4. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -0
  5. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +113 -0
  6. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -0
  7. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +594 -32
  8. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +281 -0
  10. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  11. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +594 -32
  12. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  13. package/dist/client/connect/payoutSyncTestHelpers.d.ts +281 -0
  14. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  15. package/dist/client/index.d.ts +3 -2
  16. package/dist/client/index.d.ts.map +1 -1
  17. package/dist/client/index.js +16 -3
  18. package/dist/client/index.js.map +1 -1
  19. package/dist/component/_generated/api.d.ts +6 -0
  20. package/dist/component/_generated/api.d.ts.map +1 -1
  21. package/dist/component/_generated/api.js.map +1 -1
  22. package/dist/component/_generated/component.d.ts +406 -0
  23. package/dist/component/_generated/component.d.ts.map +1 -1
  24. package/dist/component/balanceFunding/mutations.d.ts +16 -16
  25. package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
  26. package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
  27. package/dist/component/earnings/triggers.d.ts +294 -0
  28. package/dist/component/earnings/triggers.d.ts.map +1 -1
  29. package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
  30. package/dist/component/renewalOperations/helpers.d.ts +236 -0
  31. package/dist/component/renewalOperations/helpers.d.ts.map +1 -0
  32. package/dist/component/renewalOperations/helpers.js +457 -0
  33. package/dist/component/renewalOperations/helpers.js.map +1 -0
  34. package/dist/component/renewalOperations/mutations.d.ts +252 -0
  35. package/dist/component/renewalOperations/mutations.d.ts.map +1 -0
  36. package/dist/component/renewalOperations/mutations.js +340 -0
  37. package/dist/component/renewalOperations/mutations.js.map +1 -0
  38. package/dist/component/renewalOperations/queries.d.ts +163 -0
  39. package/dist/component/renewalOperations/queries.d.ts.map +1 -0
  40. package/dist/component/renewalOperations/queries.js +167 -0
  41. package/dist/component/renewalOperations/queries.js.map +1 -0
  42. package/dist/component/renewalOperations/schema.d.ts +1988 -0
  43. package/dist/component/renewalOperations/schema.d.ts.map +1 -0
  44. package/dist/component/renewalOperations/schema.js +227 -0
  45. package/dist/component/renewalOperations/schema.js.map +1 -0
  46. package/dist/component/schema.d.ts +281 -0
  47. package/dist/component/schema.js +5 -0
  48. package/dist/component/schema.js.map +1 -1
  49. package/dist/component/usage/allowance.fixtures.d.ts +281 -0
  50. package/dist/component/usage/allowance.fixtures.d.ts.map +1 -1
  51. package/dist/component/usageCredits/accounting.fixtures.d.ts +297 -16
  52. package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -1
  53. package/dist/shared/renewalOperationJournal.d.ts +43 -0
  54. package/dist/shared/renewalOperationJournal.d.ts.map +1 -0
  55. package/dist/shared/renewalOperationJournal.js +294 -0
  56. package/dist/shared/renewalOperationJournal.js.map +1 -0
  57. package/docs/renewal-context-reads.md +152 -0
  58. package/package.json +1 -1
  59. package/src/client/billing/renewalAttach/internal/operationJournal.integration.test.ts +380 -0
  60. package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +155 -0
  61. package/src/client/index.ts +24 -3
  62. package/src/component/_generated/api.ts +6 -0
  63. package/src/component/_generated/component.ts +450 -0
  64. package/src/component/renewalOperations/acknowledgement.fixtures.ts +552 -0
  65. package/src/component/renewalOperations/acknowledgement.test.ts +573 -0
  66. package/src/component/renewalOperations/acknowledgementReads.test.ts +442 -0
  67. package/src/component/renewalOperations/acknowledgementV2.test.ts +493 -0
  68. package/src/component/renewalOperations/acknowledgementV2Publication.test.ts +373 -0
  69. package/src/component/renewalOperations/basisBinding.test.ts +567 -0
  70. package/src/component/renewalOperations/basisLifecycle.test.ts +569 -0
  71. package/src/component/renewalOperations/boundHistory.test.ts +474 -0
  72. package/src/component/renewalOperations/helpers.ts +592 -0
  73. package/src/component/renewalOperations/historyReads.test.ts +595 -0
  74. package/src/component/renewalOperations/initialBasis.test.ts +578 -0
  75. package/src/component/renewalOperations/journal.test.ts +567 -0
  76. package/src/component/renewalOperations/mutations.ts +456 -0
  77. package/src/component/renewalOperations/payload.test.ts +589 -0
  78. package/src/component/renewalOperations/payloadEncoding.test.ts +592 -0
  79. package/src/component/renewalOperations/queries.ts +204 -0
  80. package/src/component/renewalOperations/schema.ts +268 -0
  81. package/src/component/renewalOperations/timingPolicy.fixtures.ts +193 -0
  82. package/src/component/renewalOperations/timingPolicy.test.ts +523 -0
  83. package/src/component/schema.ts +11 -0
  84. package/src/shared/renewalOperationJournal.ts +333 -0
@@ -0,0 +1,552 @@
1
+ import assert from "node:assert/strict";
2
+ import { convexTest } from "convex-test";
3
+ import { makeFunctionReference } from "convex/server";
4
+ import {
5
+ getConvexSize,
6
+ getDocumentSize,
7
+ type Infer,
8
+ type ObjectType,
9
+ } from "convex/values";
10
+ import type {
11
+ appendArgs,
12
+ eventReceipt,
13
+ retainAcknowledgementArgs,
14
+ retentionReceipt,
15
+ } from "./schema.js";
16
+ import { expect, vi } from "vitest";
17
+ import { modules, schema } from "../../test.js";
18
+ import { renewalSnapshot } from "../../client/billing/renewalReplacementSnapshot.js";
19
+ import {
20
+ prepareRenewalPriceReplacementV2,
21
+ prepareRenewalScheduleAttachmentV2,
22
+ constructRenewalScheduleAttachmentReceiptV2,
23
+ type ConstructRenewalScheduleAttachmentReceiptV2Options,
24
+ } from "../../client/billing/renewalV2/index.js";
25
+ import { receiptInput } from "../../client/billing/renewalAttach/internal/acquisition/test-fixtures/fixture.mjs";
26
+ import { admitInitialOutput } from "../../client/billing/renewalAttach/internal/recovery/material.js";
27
+ import {
28
+ captureAttachSource,
29
+ captureAttachDispatch,
30
+ captureAttachResponse,
31
+ captureAttachReceipt,
32
+ } from "../../client/billing/renewalAttach/internal/c10/eventCapture.js";
33
+ import {
34
+ eventDigest,
35
+ objectNode,
36
+ scalarNode,
37
+ materialize,
38
+ } from "../../client/billing/renewalAttach/internal/c10/eventCodec.js";
39
+ import { canonicalJournalEvent } from "../../shared/renewalOperationJournal.js";
40
+
41
+ export type EventReceipt = Infer<typeof eventReceipt>;
42
+ export type RetainArgs = ObjectType<typeof retainAcknowledgementArgs>;
43
+ // Closed storage DATA only; inferred types cannot assert actual custody.
44
+ export type RetentionReceipt = Infer<typeof retentionReceipt>;
45
+
46
+ export const VALUE_LIMIT = 1048576;
47
+ const create = makeFunctionReference<"mutation">(
48
+ "renewalOperations/mutations:createAuthorizedOperation",
49
+ );
50
+ const load = makeFunctionReference<"query">(
51
+ "renewalOperations/queries:loadAuthorizedOperation",
52
+ );
53
+ const reserve = makeFunctionReference<"mutation">(
54
+ "renewalOperations/mutations:reserveAttempt",
55
+ );
56
+ const bind = makeFunctionReference<"mutation">(
57
+ "renewalOperations/mutations:bindInitialBasis",
58
+ );
59
+ export const append = makeFunctionReference<
60
+ "mutation",
61
+ ObjectType<typeof appendArgs>,
62
+ EventReceipt
63
+ >("renewalOperations/mutations:appendStageEvent");
64
+ export const retain = makeFunctionReference<
65
+ "mutation",
66
+ RetainArgs,
67
+ RetentionReceipt | null
68
+ >("renewalOperations/mutations:retainProviderAcknowledgement");
69
+ export const canonical = (value: unknown) =>
70
+ JSON.stringify(renewalSnapshot(value));
71
+
72
+ export const negativeScenarios = [
73
+ "privacy-text",
74
+ "privacy-key",
75
+ "unsafe-integer",
76
+ "ordinary-binding",
77
+ ] as const;
78
+ type NegativeScenario = (typeof negativeScenarios)[number];
79
+ export type Scenario = "clean" | "nullable-key" | NegativeScenario;
80
+ const negativeMetadata: Record<
81
+ NegativeScenario,
82
+ Record<string, string | number>
83
+ > = {
84
+ "privacy-text": { opaque: "Bearer synthetic-fixture" },
85
+ "privacy-key": { client_secret: "synthetic-value" },
86
+ "unsafe-integer": { unsafe: 9007199254740992 },
87
+ "ordinary-binding": {
88
+ ordinary:
89
+ "renewal:v2:sha256:" + "a".repeat(25) + "1234567890123" + "a".repeat(26),
90
+ },
91
+ };
92
+
93
+ export async function prepareAcknowledgementScenario(scenario: Scenario) {
94
+ return prepareAcknowledgement(scenario, 1);
95
+ }
96
+ export async function prepareV2AcknowledgementScenario() {
97
+ return prepareAcknowledgement("clean", 2);
98
+ }
99
+ async function prepareAcknowledgement(scenario: Scenario, mode: 1 | 2) {
100
+ const shouldRetain = scenario === "clean" || scenario === "nullable-key";
101
+ let fetchCalls = 0;
102
+ vi.stubGlobal("fetch", () => {
103
+ fetchCalls++;
104
+ throw Error("UNEXPECTED_EXTERNAL_FETCH");
105
+ });
106
+ // Offline source only. This is not an actual provider/Owner/C10 ACK fixture.
107
+ // Complete raw recipe precedes every constructor and DB operation.
108
+ const recipe = receiptInput();
109
+ const declared =
110
+ scenario === "clean"
111
+ ? recipe
112
+ : {
113
+ ...recipe,
114
+ providerSnapshot: {
115
+ ...recipe.providerSnapshot,
116
+ metadata: {
117
+ ...recipe.providerSnapshot.metadata,
118
+ ...(scenario === "nullable-key"
119
+ ? { client_secret: null }
120
+ : negativeMetadata[scenario]),
121
+ },
122
+ },
123
+ };
124
+ const declaredJSON = canonical(declared);
125
+ const sourceJSON = canonical(declared.attachRequest);
126
+ const bodyJSON = canonical(declared.providerSnapshot);
127
+ const wire = declared.actualDispatch;
128
+ assert(wire.scope === "platform");
129
+ assert(wire.method === "POST");
130
+ assert(wire.path === "/v1/subscription_schedules");
131
+ const raw: ConstructRenewalScheduleAttachmentReceiptV2Options = {
132
+ attachRequest: declared.attachRequest,
133
+ actualDispatch: {
134
+ scope: wire.scope,
135
+ apiVersion: wire.apiVersion,
136
+ method: wire.method,
137
+ path: wire.path,
138
+ payload: wire.payload,
139
+ key: wire.key,
140
+ firstDispatchedAt: wire.firstDispatchedAt,
141
+ },
142
+ providerSnapshot: declared.providerSnapshot,
143
+ };
144
+ const authorization = Object.freeze({
145
+ scope: "ack01-offline-host",
146
+ id: "ack01-original-consent",
147
+ revision: 1,
148
+ });
149
+ const basis = Object.freeze({
150
+ startWallMs: 1700000001000,
151
+ startNs: "1700000001000000000",
152
+ workEndWallMs: 1700000005000,
153
+ workEndNs: "1700000005000000000",
154
+ endWallMs: 1700000006000,
155
+ endNs: "1700000006000000000",
156
+ });
157
+ // Independent V2 unsigned DATA; never relabel the legacy ns as an origin.
158
+ const timingPolicy = Object.freeze({
159
+ version: 1,
160
+ firstDispatchedAt: 1700000001,
161
+ periodStart: 1700000000,
162
+ periodEnd: 1702592000,
163
+ workEndWallMs: 1700000005000,
164
+ endWallMs: 1700000006000,
165
+ });
166
+ const operationalBasis = Object.freeze({
167
+ startWallMs: 1700000001000,
168
+ startNsDecimal: "1000000000",
169
+ workEndWallMs: 1700000005000,
170
+ workEndNsDecimal: "5000000000",
171
+ endWallMs: 1700000006000,
172
+ endNsDecimal: "6000000000",
173
+ });
174
+ const responseStatus = 200;
175
+ expect(canonical(raw)).toBe(declaredJSON);
176
+ expect(Object.keys(declared.actualDispatch)).toHaveLength(7);
177
+ // Existing runtime-neutral capture, then actual public pure constructors.
178
+ captureAttachSource(raw.attachRequest);
179
+ const prepared = await prepareRenewalPriceReplacementV2(
180
+ raw.attachRequest.preparation,
181
+ );
182
+ const request = await prepareRenewalScheduleAttachmentV2({
183
+ prepared,
184
+ apiVersion: raw.attachRequest.apiVersion,
185
+ attachKey: raw.attachRequest.attachKey,
186
+ firstDispatchedAt: raw.attachRequest.firstDispatchedAt,
187
+ });
188
+ const seven = {
189
+ scope: request.scope,
190
+ apiVersion: request.apiVersion,
191
+ method: request.method,
192
+ path: request.path,
193
+ payload: request.payload,
194
+ key: request.key,
195
+ firstDispatchedAt: request.firstDispatchedAt,
196
+ };
197
+ if (mode === 2) {
198
+ expect(prepared.expected.periodStart).toBe(timingPolicy.periodStart);
199
+ expect(prepared.expected.periodEnd).toBe(timingPolicy.periodEnd);
200
+ expect(request.firstDispatchedAt).toBe(timingPolicy.firstDispatchedAt);
201
+ }
202
+ // Compare ENTIRE declared dispatch; no extra field is projected away.
203
+ expect(declared.actualDispatch).toEqual(seven);
204
+ expect(raw.actualDispatch).toEqual(declared.actualDispatch);
205
+ const receipt = await constructRenewalScheduleAttachmentReceiptV2(raw);
206
+ expect(receipt.stage).toBe("attached_unconfigured");
207
+ expect(receipt.request).toEqual(request);
208
+ expect(receipt.providerSnapshot).toEqual(declared.providerSnapshot);
209
+ expect(receipt.binding).toMatch(
210
+ /^renewal-attach-receipt:v2:sha256:[0-9a-f]{64}$/,
211
+ );
212
+ // TEST-only Node guard: no component/helper imports it in this proposal.
213
+ // This is actual whole initial-output DATA admission, not a receipt-only cap.
214
+ if (shouldRetain) {
215
+ const output = admitInitialOutput(receipt, prepared, request);
216
+ expect(output).toEqual({
217
+ status: "attach_acknowledged",
218
+ operationalAuthority: false,
219
+ receipt,
220
+ });
221
+ } else {
222
+ expect(() => admitInitialOutput(receipt, prepared, request)).toThrow(
223
+ "REQUIRED_JSON_PRIVACY_STOP",
224
+ );
225
+ }
226
+ expect(canonical(raw)).toBe(declaredJSON);
227
+ if (scenario === "nullable-key") {
228
+ expect(
229
+ Object.prototype.hasOwnProperty.call(
230
+ recipe.providerSnapshot.metadata,
231
+ "client_secret",
232
+ ),
233
+ ).toBe(false);
234
+ const field = Object.getOwnPropertyDescriptor(
235
+ receipt.providerSnapshot.metadata,
236
+ "client_secret",
237
+ );
238
+ assert(field && "value" in field);
239
+ expect(field.value).toBeNull();
240
+ }
241
+ const receiptJSON = canonical(receipt);
242
+ expect(receiptJSON.length).toBeLessThanOrEqual(500000);
243
+ expect(new TextEncoder().encode(receiptJSON).length).toBeLessThanOrEqual(
244
+ 500000,
245
+ );
246
+ const commonRecord = {
247
+ requestBinding: request.requestIdentity.binding,
248
+ authorization,
249
+ logicalOperationId: "ack01-original-operation",
250
+ stage: "attach",
251
+ account: { mode: "platform", stripeAccount: null, livemode: false },
252
+ apiVersion: raw.attachRequest.apiVersion,
253
+ profile: "unqualified-offline-C3-storage-DATA",
254
+ attachRequest: raw.attachRequest,
255
+ dispatch: declared.actualDispatch,
256
+ originalBundle: raw.attachRequest.preparation,
257
+ };
258
+ const record =
259
+ mode === 1
260
+ ? { ...commonRecord, schemaVersion: 1, basis }
261
+ : { ...commonRecord, schemaVersion: 2, timingPolicy };
262
+ const recordJSON = canonical(record);
263
+ expect(JSON.parse(recordJSON).attachRequest).toEqual(declared.attachRequest);
264
+ const data = [
265
+ { event: "dispatch", stage: "attach", request },
266
+ {
267
+ event: "response",
268
+ stage: "attach",
269
+ request,
270
+ body: raw.providerSnapshot,
271
+ status: responseStatus,
272
+ },
273
+ ];
274
+ const captures = [captureAttachDispatch(data[0])];
275
+ if (shouldRetain) captures.push(captureAttachResponse(data[1]));
276
+ else expect(() => captureAttachResponse(data[1])).toThrow();
277
+ const events: Awaited<ReturnType<typeof canonicalJournalEvent>>[] = [];
278
+ for (const [index, datum] of data.entries()) {
279
+ const node = captures[index];
280
+ if (node) expect(materialize(node)).toEqual(datum);
281
+ const event = await canonicalJournalEvent(
282
+ index + 1,
283
+ canonical(data[index]),
284
+ );
285
+ if (node) {
286
+ expect(event.binding).toBe(
287
+ await eventDigest(
288
+ objectNode([
289
+ ["sequence", scalarNode(index + 1)],
290
+ ["data", node],
291
+ ]),
292
+ ),
293
+ );
294
+ } else {
295
+ // Invalid host response DATA is only generic journal JSON, not a valid
296
+ // typed C10 capture. Actual append rechecks the complete canonical hash.
297
+ expect(event.json).toBe(canonical(datum));
298
+ expect(event.binding).toMatch(
299
+ /^unbound-qualification-event:v2:sha256:[0-9a-f]{64}$/,
300
+ );
301
+ }
302
+ events.push(event);
303
+ }
304
+ // The clean small fixture admits event3; invalid host DATA must fail capture.
305
+ const receiptEvent = { event: "receipt", stage: "attach", request, receipt };
306
+ if (shouldRetain) captureAttachReceipt(receiptEvent);
307
+ else expect(() => captureAttachReceipt(receiptEvent)).toThrow();
308
+ const t = convexTest(schema, modules);
309
+ const physical = async () => ({
310
+ operations: await t.run((ctx) => ctx.db.query("renewalOperations").take(3)),
311
+ attempts: await t.run((ctx) =>
312
+ ctx.db.query("renewalOperationAttempts").take(3),
313
+ ),
314
+ events: await t.run((ctx) =>
315
+ ctx.db.query("renewalOperationEvents").take(4),
316
+ ),
317
+ payloads: await t.run((ctx) =>
318
+ ctx.db.query("renewalOperationPayloads").take(6),
319
+ ),
320
+ });
321
+ const creationArgs = {
322
+ authorizationScope: authorization.scope,
323
+ authorizationRevision: authorization.revision,
324
+ logicalOperationId: record.logicalOperationId,
325
+ recordJSON,
326
+ };
327
+ expect(getConvexSize(creationArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
328
+ const created = await t.mutation(create, creationArgs);
329
+ expect(created.status).toBe("created");
330
+ expect(getConvexSize(created)).toBeLessThanOrEqual(VALUE_LIMIT);
331
+ const queryArgs = { operationId: created.operationId };
332
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
333
+ const original = await t.query(load, queryArgs);
334
+ expect(original).toEqual({
335
+ ...queryArgs,
336
+ recordJSON,
337
+ revision: 0,
338
+ activeAttempt: null,
339
+ ...(mode === 2 ? { recordVersion: 2, initialBasis: null } : {}),
340
+ });
341
+ expect(getConvexSize(original)).toBeLessThanOrEqual(VALUE_LIMIT);
342
+ const reserveArgs = {
343
+ ...queryArgs,
344
+ expectedRevision: 0,
345
+ stage: "attach",
346
+ expectedRequestBinding: record.requestBinding,
347
+ recordJSON,
348
+ };
349
+ expect(getConvexSize(reserveArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
350
+ const acquired = await t.mutation(reserve, reserveArgs);
351
+ expect(acquired.disposition).toBe("acquired");
352
+ expect(acquired.reservation.recordJSON).toBe(recordJSON);
353
+ if (mode === 1) expect(acquired.reservation.basisJSON).toBe(canonical(basis));
354
+ else {
355
+ const summary = {
356
+ recordVersion: 2,
357
+ operationId: created.operationId,
358
+ payloadId: acquired.reservation.payloadId,
359
+ authorizationScope: authorization.scope,
360
+ authorizationId: authorization.id,
361
+ authorizationRevision: authorization.revision,
362
+ logicalOperationId: record.logicalOperationId,
363
+ requestBinding: record.requestBinding,
364
+ stage: "attach",
365
+ key: request.key,
366
+ apiVersion: request.apiVersion,
367
+ accountJSON: canonical(record.account),
368
+ timingPolicyJSON: canonical(timingPolicy),
369
+ firstDispatchedAt: request.firstDispatchedAt,
370
+ ordinal: 1,
371
+ preparedRevision: 0,
372
+ committedRevision: 1,
373
+ attemptId: acquired.reservation.attemptId,
374
+ };
375
+ expect(acquired).toEqual({
376
+ disposition: "acquired",
377
+ previousRevision: 0,
378
+ committedRevision: 1,
379
+ reservation: { ...summary, recordJSON },
380
+ });
381
+ expect(
382
+ Object.prototype.hasOwnProperty.call(acquired.reservation, "basisJSON"),
383
+ ).toBe(false);
384
+ const bindArgs = {
385
+ operationId: created.operationId,
386
+ attemptId: acquired.reservation.attemptId,
387
+ policyJSON: canonical(timingPolicy),
388
+ basisJSON: canonical(operationalBasis),
389
+ };
390
+ expect(getConvexSize(bindArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
391
+ const initialBasis = {
392
+ version: 1,
393
+ attemptId: acquired.reservation.attemptId,
394
+ sourcePayloadId: acquired.reservation.payloadId,
395
+ stage: "attach",
396
+ policyJSON: bindArgs.policyJSON,
397
+ basisJSON: bindArgs.basisJSON,
398
+ };
399
+ const bound = await t.mutation(bind, bindArgs);
400
+ expect(bound).toEqual({ status: "bound", ...queryArgs, initialBasis });
401
+ expect(getConvexSize(bound)).toBeLessThanOrEqual(VALUE_LIMIT);
402
+ const loaded = await t.query(load, queryArgs);
403
+ expect(loaded).toEqual({
404
+ recordVersion: 2,
405
+ ...queryArgs,
406
+ recordJSON,
407
+ revision: 1,
408
+ activeAttempt: summary,
409
+ initialBasis,
410
+ });
411
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
412
+ }
413
+ expect(getConvexSize(acquired)).toBeLessThanOrEqual(VALUE_LIMIT);
414
+ const ownerScope = {
415
+ operationId: created.operationId,
416
+ attemptId: acquired.reservation.attemptId,
417
+ stage: acquired.reservation.stage,
418
+ };
419
+ const commits: EventReceipt[] = [];
420
+ for (const [index, event] of events.entries()) {
421
+ const args: ObjectType<typeof appendArgs> = {
422
+ reservation: acquired.reservation,
423
+ expectedSequence: index,
424
+ event: {
425
+ sequence: event.sequence,
426
+ binding: event.binding,
427
+ dataJSON: event.json,
428
+ },
429
+ };
430
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
431
+ const committed = await t.mutation(append, args);
432
+ expect(committed.sequence).toBe(index + 1);
433
+ expect(committed.binding).toBe(event.binding);
434
+ expect(committed.reservation).toEqual(acquired.reservation);
435
+ expect(committed.operationId).toBe(created.operationId);
436
+ expect(committed.attemptId).toBe(acquired.reservation.attemptId);
437
+ expect(committed.sourcePayloadId).toBe(acquired.reservation.payloadId);
438
+ expect(committed.stage).toBe("attach");
439
+ expect(getConvexSize(committed)).toBeLessThanOrEqual(VALUE_LIMIT);
440
+ commits.push(committed);
441
+ }
442
+ const before = await physical();
443
+ expect(Object.values(before).map((rows) => rows.length)).toEqual([
444
+ 1, 1, 2, 3,
445
+ ]);
446
+ expect(before.attempts[0].lastSequence).toBe(2);
447
+ expect(before.operations[0].revision).toBe(1);
448
+ for (const [index, committed] of commits.entries()) {
449
+ const eventRow = before.events.find((row) => row._id === committed.eventId);
450
+ const payloadRow = before.payloads.find(
451
+ (row) => row._id === committed.payloadId,
452
+ );
453
+ assert(eventRow && payloadRow);
454
+ expect(Number.isFinite(eventRow._creationTime)).toBe(true);
455
+ expect(eventRow).toEqual({
456
+ _id: committed.eventId,
457
+ _creationTime: eventRow._creationTime,
458
+ operationId: committed.operationId,
459
+ attemptId: committed.attemptId,
460
+ stage: committed.stage,
461
+ sourcePayloadId: committed.sourcePayloadId,
462
+ sequence: committed.sequence,
463
+ binding: committed.binding,
464
+ payloadId: committed.payloadId,
465
+ });
466
+ expect(Number.isFinite(payloadRow._creationTime)).toBe(true);
467
+ expect(payloadRow).toEqual({
468
+ _id: committed.payloadId,
469
+ _creationTime: payloadRow._creationTime,
470
+ codecVersion: 1,
471
+ json: events[index].json,
472
+ owner: {
473
+ kind: "event",
474
+ ...ownerScope,
475
+ sequence: committed.sequence,
476
+ },
477
+ });
478
+ expect(JSON.parse(payloadRow.json)).toEqual(data[index]);
479
+ }
480
+ expect(
481
+ Object.prototype.hasOwnProperty.call(
482
+ before.operations[0],
483
+ "knownAcknowledgement",
484
+ ),
485
+ ).toBe(false);
486
+ const sourcePayload = before.payloads.find(
487
+ (row) => row._id === acquired.reservation.payloadId,
488
+ );
489
+ const responsePayload = before.payloads.find(
490
+ (row) => row._id === commits[1].payloadId,
491
+ );
492
+ assert(sourcePayload && responsePayload);
493
+ expect(sourcePayload.json).toBe(recordJSON);
494
+ expect(sourcePayload.owner).toEqual({
495
+ kind: "operation",
496
+ operationId: created.operationId,
497
+ });
498
+ expect(canonical(JSON.parse(sourcePayload.json).attachRequest)).toBe(
499
+ sourceJSON,
500
+ );
501
+ expect(canonical(JSON.parse(responsePayload.json).body)).toBe(bodyJSON);
502
+ expect(
503
+ Object.getOwnPropertyDescriptor(receipt.providerSnapshot, "__proto__")
504
+ ?.value,
505
+ ).toEqual({ retained: true, nested: ["SYNTHETIC", null] });
506
+ expect(receipt.providerSnapshot.future_native_field).toEqual({
507
+ retained: [null, "safe", 1.25],
508
+ });
509
+ for (const rows of Object.values(before))
510
+ for (const doc of rows)
511
+ expect(getDocumentSize(doc)).toBeLessThanOrEqual(VALUE_LIMIT);
512
+ const args: RetainArgs = {
513
+ ...queryArgs,
514
+ attemptId: acquired.reservation.attemptId,
515
+ expectedSequence: 2,
516
+ receiptJSON,
517
+ };
518
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
519
+ expect(fetchCalls).toBe(0);
520
+ console.info(
521
+ "BTS317_ACK01_PREREQUISITES",
522
+ JSON.stringify({
523
+ recordBytes: new TextEncoder().encode(recordJSON).length,
524
+ receiptBytes: new TextEncoder().encode(receiptJSON).length,
525
+ wholeRPCBytes: getConvexSize(args),
526
+ counts: [1, 1, 2, 3],
527
+ lastSequence: 2,
528
+ fullNodeOutputAdmitted: shouldRetain,
529
+ scenario,
530
+ fetchCalls,
531
+ }),
532
+ );
533
+ return {
534
+ t,
535
+ physical,
536
+ before,
537
+ args,
538
+ receipt,
539
+ receiptJSON,
540
+ acquired,
541
+ commits,
542
+ ownerScope,
543
+ queryArgs,
544
+ shouldRetain,
545
+ scenario,
546
+ raw,
547
+ prepared,
548
+ request,
549
+ receiptEvent,
550
+ fetchCalls: () => fetchCalls,
551
+ };
552
+ }