@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,567 @@
1
+ import { makeFunctionReference } from "convex/server";
2
+ import { getConvexSize, getDocumentSize } from "convex/values";
3
+ import { afterEach, expect, it, vi } from "vitest";
4
+ import {
5
+ canonical,
6
+ codecFixture,
7
+ componentFixture,
8
+ create,
9
+ load,
10
+ VALUE_LIMIT,
11
+ } from "./timingPolicy.fixtures.js";
12
+
13
+ const reserve = makeFunctionReference<"mutation">(
14
+ "renewalOperations/mutations:reserveAttempt",
15
+ );
16
+ const bind = makeFunctionReference<"mutation">(
17
+ "renewalOperations/mutations:bindInitialBasis",
18
+ );
19
+ // Fixed synthetic DATA, not a sampled runtime clock or an eligibility claim.
20
+ const basis = Object.freeze({
21
+ startWallMs: 1700000001000,
22
+ startNsDecimal: "1000000000",
23
+ workEndWallMs: 1700000005000,
24
+ workEndNsDecimal: "5000000000",
25
+ endWallMs: 1700000006000,
26
+ endNsDecimal: "6000000000",
27
+ });
28
+ afterEach(() => vi.unstubAllGlobals());
29
+
30
+ it("BIND01 retains exact first basis DATA on the actual reserved V2 operation", async () => {
31
+ const h = await codecFixture();
32
+ const { t, physical } = componentFixture();
33
+ expect(basis.workEndWallMs).toBe(h.record.timingPolicy.workEndWallMs);
34
+ expect(basis.endWallMs).toBe(h.record.timingPolicy.endWallMs);
35
+ expect(BigInt(basis.workEndNsDecimal)).toBe(
36
+ BigInt(basis.startNsDecimal) +
37
+ BigInt(basis.workEndWallMs - basis.startWallMs) * 1000000n,
38
+ );
39
+ expect(BigInt(basis.endNsDecimal)).toBe(
40
+ BigInt(basis.startNsDecimal) +
41
+ BigInt(basis.endWallMs - basis.startWallMs) * 1000000n,
42
+ );
43
+ expect(await physical()).toEqual({
44
+ operations: [],
45
+ attempts: [],
46
+ events: [],
47
+ payloads: [],
48
+ });
49
+ const createArgs = {
50
+ authorizationScope: h.record.authorization.scope,
51
+ logicalOperationId: h.record.logicalOperationId,
52
+ authorizationRevision: h.record.authorization.revision,
53
+ recordJSON: h.recordJSON,
54
+ };
55
+ expect(getConvexSize(createArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
56
+ const created = await t.mutation(create, createArgs);
57
+ expect(created.status).toBe("created");
58
+ expect(getConvexSize(created)).toBeLessThanOrEqual(VALUE_LIMIT);
59
+ const queryArgs = { operationId: created.operationId };
60
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
61
+ expect(await t.query(load, queryArgs)).toEqual({
62
+ recordVersion: 2,
63
+ operationId: created.operationId,
64
+ recordJSON: h.recordJSON,
65
+ revision: 0,
66
+ activeAttempt: null,
67
+ initialBasis: null,
68
+ });
69
+ const reserveArgs = {
70
+ operationId: created.operationId,
71
+ expectedRevision: 0,
72
+ stage: "attach" as const,
73
+ expectedRequestBinding: h.record.requestBinding,
74
+ recordJSON: h.recordJSON,
75
+ };
76
+ expect(getConvexSize(reserveArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
77
+ const acquired = await t.mutation(reserve, reserveArgs);
78
+ expect(acquired.disposition).toBe("acquired");
79
+ expect(getConvexSize(acquired)).toBeLessThanOrEqual(VALUE_LIMIT);
80
+ const before = await physical();
81
+ expect(before.operations).toHaveLength(1);
82
+ expect(before.attempts).toHaveLength(1);
83
+ expect(before.payloads).toHaveLength(1);
84
+ expect(before.events).toHaveLength(0);
85
+ const operation = before.operations[0];
86
+ const attempt = before.attempts[0];
87
+ const payload = before.payloads[0];
88
+ expect(operation).toMatchObject({
89
+ _id: created.operationId,
90
+ recordVersion: 2,
91
+ payloadId: payload._id,
92
+ requestBinding: h.record.requestBinding,
93
+ revision: 1,
94
+ state: "RESERVED",
95
+ activeAttemptId: attempt._id,
96
+ });
97
+ expect(Object.prototype.hasOwnProperty.call(operation, "initialBasis")).toBe(
98
+ false,
99
+ );
100
+ expect(payload).toMatchObject({
101
+ codecVersion: 1,
102
+ json: h.recordJSON,
103
+ owner: { kind: "operation", operationId: created.operationId },
104
+ });
105
+ const summary = {
106
+ recordVersion: 2,
107
+ operationId: created.operationId,
108
+ payloadId: payload._id,
109
+ authorizationScope: h.record.authorization.scope,
110
+ authorizationId: h.record.authorization.id,
111
+ authorizationRevision: h.record.authorization.revision,
112
+ logicalOperationId: h.record.logicalOperationId,
113
+ requestBinding: h.record.requestBinding,
114
+ stage: "attach",
115
+ key: h.request.key,
116
+ apiVersion: h.request.apiVersion,
117
+ accountJSON: canonical(h.record.account),
118
+ timingPolicyJSON: canonical(h.record.timingPolicy),
119
+ firstDispatchedAt: h.request.firstDispatchedAt,
120
+ ordinal: 1,
121
+ preparedRevision: 0,
122
+ committedRevision: 1,
123
+ attemptId: attempt._id,
124
+ };
125
+ expect(acquired).toEqual({
126
+ disposition: "acquired",
127
+ previousRevision: 0,
128
+ committedRevision: 1,
129
+ reservation: { ...summary, recordJSON: h.recordJSON },
130
+ });
131
+ const { attemptId, ...fields } = summary;
132
+ expect(attempt).toEqual({
133
+ ...fields,
134
+ _id: attemptId,
135
+ _creationTime: attempt._creationTime,
136
+ lastSequence: 0,
137
+ });
138
+ const reserved = await t.query(load, queryArgs);
139
+ expect(reserved).toEqual({
140
+ recordVersion: 2,
141
+ operationId: created.operationId,
142
+ recordJSON: h.recordJSON,
143
+ revision: 1,
144
+ activeAttempt: summary,
145
+ initialBasis: null,
146
+ });
147
+ expect(getConvexSize(reserved)).toBeLessThanOrEqual(VALUE_LIMIT);
148
+ for (const rows of Object.values(before)) {
149
+ for (const row of rows)
150
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
151
+ }
152
+ const args = {
153
+ operationId: created.operationId,
154
+ attemptId: attempt._id,
155
+ policyJSON: canonical(h.record.timingPolicy),
156
+ basisJSON: canonical(basis),
157
+ };
158
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
159
+ const initialBasis = {
160
+ version: 1,
161
+ attemptId: attempt._id,
162
+ sourcePayloadId: payload._id,
163
+ stage: "attach",
164
+ policyJSON: args.policyJSON,
165
+ basisJSON: args.basisJSON,
166
+ };
167
+ let stable = before;
168
+ let targetFailed = false;
169
+ try {
170
+ // The registered route exists; only its honest null behavior is missing.
171
+ const result = await t.mutation(bind, args);
172
+ expect(result).toEqual({
173
+ status: "bound",
174
+ operationId: created.operationId,
175
+ initialBasis,
176
+ });
177
+ expect(getConvexSize(result)).toBeLessThanOrEqual(VALUE_LIMIT);
178
+ const after = await physical();
179
+ stable = after;
180
+ expect(after).toEqual({
181
+ ...before,
182
+ operations: [{ ...operation, initialBasis }],
183
+ });
184
+ for (const rows of Object.values(after)) {
185
+ for (const row of rows)
186
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
187
+ }
188
+ const loaded = await t.query(load, queryArgs);
189
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
190
+ expect(loaded).toEqual({ ...reserved, initialBasis });
191
+ expect(canonical(h.record)).toBe(h.recordJSON);
192
+ } catch (error) {
193
+ targetFailed = true;
194
+ throw error;
195
+ } finally {
196
+ try {
197
+ const after = await physical();
198
+ console.info(
199
+ "BIND01_SETTLEMENT",
200
+ JSON.stringify({
201
+ physicalEqual: JSON.stringify(after) === JSON.stringify(stable),
202
+ rows: Object.fromEntries(
203
+ Object.entries(after).map(([key, rows]) => [key, rows.length]),
204
+ ),
205
+ fetchCalls: h.fetchCalls(),
206
+ }),
207
+ );
208
+ expect(after).toEqual(stable);
209
+ expect(h.fetchCalls()).toBe(0);
210
+ } catch (inspectionError) {
211
+ if (!targetFailed) throw inspectionError;
212
+ console.info("BIND01_READONLY_INSPECTION_FAILED");
213
+ }
214
+ }
215
+ });
216
+
217
+ // Every recipe is predeclared unsigned host DATA, not a live clock/basis claim.
218
+ const missingField: Record<string, unknown> = { ...basis };
219
+ delete missingField.endNsDecimal;
220
+ const highPrecisionBasis = Object.freeze({
221
+ ...basis,
222
+ startNsDecimal: "9223372036854775809",
223
+ workEndNsDecimal: "9223372040854775809",
224
+ endNsDecimal: "9223372041854775809",
225
+ });
226
+ const invalidBases = [
227
+ { name: "missing-field", data: missingField },
228
+ { name: "extra-field", data: { ...basis, extra: null } },
229
+ { name: "null-basis", data: null },
230
+ { name: "null-field", data: { ...basis, startNsDecimal: null } },
231
+ { name: "leading-zero", data: { ...basis, startNsDecimal: "01000000000" } },
232
+ { name: "negative-decimal", data: { ...basis, startNsDecimal: "-1" } },
233
+ { name: "fraction-decimal", data: { ...basis, startNsDecimal: "1.5" } },
234
+ { name: "nonstring-decimal", data: { ...basis, startNsDecimal: 1000000000 } },
235
+ {
236
+ name: "u64-overflow",
237
+ data: {
238
+ ...basis,
239
+ startNsDecimal: "18446744073709551616",
240
+ workEndNsDecimal: "18446744077709551616",
241
+ endNsDecimal: "18446744078709551616",
242
+ },
243
+ },
244
+ { name: "unsafe-wall", data: { ...basis, startWallMs: 9007199254740992 } },
245
+ {
246
+ name: "fractional-wall",
247
+ data: {
248
+ ...basis,
249
+ startWallMs: 1700000001000.5,
250
+ workEndNsDecimal: "4999500000",
251
+ endNsDecimal: "5999500000",
252
+ },
253
+ },
254
+ { name: "negative-wall", data: { ...basis, startWallMs: -1 } },
255
+ {
256
+ name: "start-equals-work",
257
+ data: {
258
+ ...basis,
259
+ startWallMs: 1700000005000,
260
+ workEndNsDecimal: "1000000000",
261
+ endNsDecimal: "2000000000",
262
+ },
263
+ },
264
+ {
265
+ name: "different-policy-work",
266
+ data: {
267
+ ...basis,
268
+ workEndWallMs: 1700000005001,
269
+ workEndNsDecimal: "5001000000",
270
+ },
271
+ },
272
+ {
273
+ name: "different-policy-end",
274
+ data: { ...basis, endWallMs: 1700000006001, endNsDecimal: "6001000000" },
275
+ },
276
+ {
277
+ name: "wrong-ns-projection",
278
+ data: { ...basis, workEndNsDecimal: "5000000001" },
279
+ },
280
+ {
281
+ name: "high-precision-projection",
282
+ data: { ...highPrecisionBasis, workEndNsDecimal: "9223372040854775810" },
283
+ },
284
+ ] as const;
285
+
286
+ async function reservedForBasis() {
287
+ const h = await codecFixture();
288
+ const { t, physical } = componentFixture();
289
+ expect(basis.workEndWallMs).toBe(h.record.timingPolicy.workEndWallMs);
290
+ expect(basis.endWallMs).toBe(h.record.timingPolicy.endWallMs);
291
+ expect(BigInt(basis.workEndNsDecimal)).toBe(
292
+ BigInt(basis.startNsDecimal) +
293
+ BigInt(basis.workEndWallMs - basis.startWallMs) * 1000000n,
294
+ );
295
+ expect(BigInt(basis.endNsDecimal)).toBe(
296
+ BigInt(basis.startNsDecimal) +
297
+ BigInt(basis.endWallMs - basis.startWallMs) * 1000000n,
298
+ );
299
+ expect(await physical()).toEqual({
300
+ operations: [],
301
+ attempts: [],
302
+ events: [],
303
+ payloads: [],
304
+ });
305
+ const createArgs = {
306
+ authorizationScope: h.record.authorization.scope,
307
+ logicalOperationId: h.record.logicalOperationId,
308
+ authorizationRevision: h.record.authorization.revision,
309
+ recordJSON: h.recordJSON,
310
+ };
311
+ expect(getConvexSize(createArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
312
+ const created = await t.mutation(create, createArgs);
313
+ expect(created.status).toBe("created");
314
+ expect(getConvexSize(created)).toBeLessThanOrEqual(VALUE_LIMIT);
315
+ const queryArgs = { operationId: created.operationId };
316
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
317
+ expect(await t.query(load, queryArgs)).toEqual({
318
+ recordVersion: 2,
319
+ operationId: created.operationId,
320
+ recordJSON: h.recordJSON,
321
+ revision: 0,
322
+ activeAttempt: null,
323
+ initialBasis: null,
324
+ });
325
+ const reserveArgs = {
326
+ operationId: created.operationId,
327
+ expectedRevision: 0,
328
+ stage: "attach" as const,
329
+ expectedRequestBinding: h.record.requestBinding,
330
+ recordJSON: h.recordJSON,
331
+ };
332
+ expect(getConvexSize(reserveArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
333
+ const acquired = await t.mutation(reserve, reserveArgs);
334
+ expect(acquired.disposition).toBe("acquired");
335
+ expect(getConvexSize(acquired)).toBeLessThanOrEqual(VALUE_LIMIT);
336
+ const before = await physical();
337
+ expect(before.operations).toHaveLength(1);
338
+ expect(before.attempts).toHaveLength(1);
339
+ expect(before.payloads).toHaveLength(1);
340
+ expect(before.events).toHaveLength(0);
341
+ const operation = before.operations[0];
342
+ const attempt = before.attempts[0];
343
+ const payload = before.payloads[0];
344
+ expect(operation).toMatchObject({
345
+ _id: created.operationId,
346
+ recordVersion: 2,
347
+ payloadId: payload._id,
348
+ requestBinding: h.record.requestBinding,
349
+ revision: 1,
350
+ state: "RESERVED",
351
+ activeAttemptId: attempt._id,
352
+ });
353
+ expect(Object.prototype.hasOwnProperty.call(operation, "initialBasis")).toBe(
354
+ false,
355
+ );
356
+ expect(payload).toMatchObject({
357
+ codecVersion: 1,
358
+ json: h.recordJSON,
359
+ owner: { kind: "operation", operationId: created.operationId },
360
+ });
361
+ const summary = {
362
+ recordVersion: 2,
363
+ operationId: created.operationId,
364
+ payloadId: payload._id,
365
+ authorizationScope: h.record.authorization.scope,
366
+ authorizationId: h.record.authorization.id,
367
+ authorizationRevision: h.record.authorization.revision,
368
+ logicalOperationId: h.record.logicalOperationId,
369
+ requestBinding: h.record.requestBinding,
370
+ stage: "attach",
371
+ key: h.request.key,
372
+ apiVersion: h.request.apiVersion,
373
+ accountJSON: canonical(h.record.account),
374
+ timingPolicyJSON: canonical(h.record.timingPolicy),
375
+ firstDispatchedAt: h.request.firstDispatchedAt,
376
+ ordinal: 1,
377
+ preparedRevision: 0,
378
+ committedRevision: 1,
379
+ attemptId: attempt._id,
380
+ };
381
+ expect(acquired).toEqual({
382
+ disposition: "acquired",
383
+ previousRevision: 0,
384
+ committedRevision: 1,
385
+ reservation: { ...summary, recordJSON: h.recordJSON },
386
+ });
387
+ const { attemptId, ...fields } = summary;
388
+ expect(attempt).toEqual({
389
+ ...fields,
390
+ _id: attemptId,
391
+ _creationTime: attempt._creationTime,
392
+ lastSequence: 0,
393
+ });
394
+ const reserved = await t.query(load, queryArgs);
395
+ expect(reserved).toEqual({
396
+ recordVersion: 2,
397
+ operationId: created.operationId,
398
+ recordJSON: h.recordJSON,
399
+ revision: 1,
400
+ activeAttempt: summary,
401
+ initialBasis: null,
402
+ });
403
+ expect(getConvexSize(reserved)).toBeLessThanOrEqual(VALUE_LIMIT);
404
+ for (const rows of Object.values(before)) {
405
+ for (const row of rows)
406
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
407
+ }
408
+ const args = {
409
+ operationId: created.operationId,
410
+ attemptId: attempt._id,
411
+ policyJSON: canonical(h.record.timingPolicy),
412
+ basisJSON: canonical(basis),
413
+ };
414
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
415
+ return { h, t, physical, before, args };
416
+ }
417
+
418
+ it.each(invalidBases)(
419
+ "BIND02 refuses malformed six-field $name basis DATA",
420
+ async (scenario) => {
421
+ if (scenario.name === "high-precision-projection") {
422
+ expect(BigInt(highPrecisionBasis.workEndNsDecimal)).toBe(
423
+ BigInt(highPrecisionBasis.startNsDecimal) +
424
+ BigInt(basis.workEndWallMs - basis.startWallMs) * 1000000n,
425
+ );
426
+ expect(BigInt(highPrecisionBasis.endNsDecimal)).toBe(
427
+ BigInt(highPrecisionBasis.startNsDecimal) +
428
+ BigInt(basis.endWallMs - basis.startWallMs) * 1000000n,
429
+ );
430
+ expect(BigInt(scenario.data.workEndNsDecimal)).toBe(
431
+ BigInt(highPrecisionBasis.workEndNsDecimal) + 1n,
432
+ );
433
+ expect(BigInt(scenario.data.endNsDecimal)).toBe(
434
+ BigInt(highPrecisionBasis.endNsDecimal),
435
+ );
436
+ }
437
+ if (
438
+ scenario.name === "leading-zero" ||
439
+ scenario.name === "u64-overflow" ||
440
+ scenario.name === "start-equals-work" ||
441
+ scenario.name === "different-policy-work" ||
442
+ scenario.name === "different-policy-end"
443
+ ) {
444
+ // These cases deliberately preserve the complete numerical projection.
445
+ const data = scenario.data;
446
+ expect(BigInt(data.workEndNsDecimal)).toBe(
447
+ BigInt(data.startNsDecimal) +
448
+ BigInt(data.workEndWallMs - data.startWallMs) * 1000000n,
449
+ );
450
+ expect(BigInt(data.endNsDecimal)).toBe(
451
+ BigInt(data.startNsDecimal) +
452
+ BigInt(data.endWallMs - data.startWallMs) * 1000000n,
453
+ );
454
+ }
455
+ const { h, t, physical, before, args } = await reservedForBasis();
456
+ const basisJSON = canonical(scenario.data);
457
+ expect(basisJSON.length).toBeLessThanOrEqual(500000);
458
+ expect(new TextEncoder().encode(basisJSON).length).toBeLessThanOrEqual(
459
+ 500000,
460
+ );
461
+ const invalidArgs = { ...args, basisJSON };
462
+ expect(getConvexSize(invalidArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
463
+ let targetFailed = false;
464
+ try {
465
+ await expect(t.mutation(bind, invalidArgs)).rejects.toThrow(
466
+ /^RENEWAL_JOURNAL_CONFLICT$/,
467
+ );
468
+ } catch (error) {
469
+ targetFailed = true;
470
+ throw error;
471
+ } finally {
472
+ try {
473
+ const after = await physical();
474
+ console.info(
475
+ "BIND02_SETTLEMENT",
476
+ JSON.stringify({
477
+ scenario: scenario.name,
478
+ physicalEqual: JSON.stringify(after) === JSON.stringify(before),
479
+ rows: Object.fromEntries(
480
+ Object.entries(after).map(([key, rows]) => [key, rows.length]),
481
+ ),
482
+ fetchCalls: h.fetchCalls(),
483
+ }),
484
+ );
485
+ expect(after).toEqual(before);
486
+ expect(h.fetchCalls()).toBe(0);
487
+ } catch (inspectionError) {
488
+ if (!targetFailed) throw inspectionError;
489
+ console.info("BIND02_READONLY_INSPECTION_FAILED");
490
+ }
491
+ }
492
+ },
493
+ );
494
+
495
+ it("BIND03 retains exact valid u64 projection above Number precision", async () => {
496
+ // Pure unsigned DATA arithmetic before actual constructors; no clock sample.
497
+ expect(BigInt(highPrecisionBasis.startNsDecimal)).toBeGreaterThan(
498
+ 9007199254740991n,
499
+ );
500
+ expect(BigInt(highPrecisionBasis.workEndNsDecimal)).toBe(
501
+ BigInt(highPrecisionBasis.startNsDecimal) +
502
+ BigInt(
503
+ highPrecisionBasis.workEndWallMs - highPrecisionBasis.startWallMs,
504
+ ) *
505
+ 1000000n,
506
+ );
507
+ expect(BigInt(highPrecisionBasis.endNsDecimal)).toBe(
508
+ BigInt(highPrecisionBasis.startNsDecimal) +
509
+ BigInt(highPrecisionBasis.endWallMs - highPrecisionBasis.startWallMs) *
510
+ 1000000n,
511
+ );
512
+ const { h, t, physical, before, args } = await reservedForBasis();
513
+ const basisJSON = canonical(highPrecisionBasis);
514
+ const highArgs = { ...args, basisJSON };
515
+ expect(getConvexSize(highArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
516
+ const queryArgs = { operationId: args.operationId };
517
+ const reserved = await t.query(load, queryArgs);
518
+ expect(reserved.initialBasis).toBeNull();
519
+ expect(getConvexSize(reserved)).toBeLessThanOrEqual(VALUE_LIMIT);
520
+ const initialBasis = {
521
+ version: 1,
522
+ attemptId: args.attemptId,
523
+ sourcePayloadId: before.payloads[0]._id,
524
+ stage: "attach",
525
+ policyJSON: args.policyJSON,
526
+ basisJSON,
527
+ };
528
+ let stable = before;
529
+ let targetFailed = false;
530
+ try {
531
+ const result = await t.mutation(bind, highArgs);
532
+ expect(result).toEqual({
533
+ status: "bound",
534
+ operationId: args.operationId,
535
+ initialBasis,
536
+ });
537
+ expect(getConvexSize(result)).toBeLessThanOrEqual(VALUE_LIMIT);
538
+ const after = await physical();
539
+ stable = after;
540
+ expect(after).toEqual({
541
+ ...before,
542
+ operations: [{ ...before.operations[0], initialBasis }],
543
+ });
544
+ for (const rows of Object.values(after)) {
545
+ for (const row of rows)
546
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
547
+ }
548
+ const loaded = await t.query(load, queryArgs);
549
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
550
+ expect(loaded).toEqual({ ...reserved, initialBasis });
551
+ expect(JSON.parse(loaded.initialBasis.basisJSON)).toEqual(
552
+ highPrecisionBasis,
553
+ );
554
+ expect(canonical(h.record)).toBe(h.recordJSON);
555
+ } catch (error) {
556
+ targetFailed = true;
557
+ throw error;
558
+ } finally {
559
+ try {
560
+ expect(await physical()).toEqual(stable);
561
+ expect(h.fetchCalls()).toBe(0);
562
+ } catch (inspectionError) {
563
+ if (!targetFailed) throw inspectionError;
564
+ console.info("BIND03_READONLY_INSPECTION_FAILED");
565
+ }
566
+ }
567
+ });