@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,578 @@
1
+ import { makeFunctionReference } from "convex/server";
2
+ import { getConvexSize, getDocumentSize } from "convex/values";
3
+ import { afterEach, expect, it, vi } from "vitest";
4
+ import {
5
+ canonicalJournalRecord,
6
+ canonicalJournalEvent,
7
+ } from "../../shared/renewalOperationJournal.js";
8
+ import {
9
+ canonical,
10
+ codecFixture,
11
+ componentFixture,
12
+ create,
13
+ load,
14
+ VALUE_LIMIT,
15
+ } from "./timingPolicy.fixtures.js";
16
+
17
+ const reserve = makeFunctionReference<"mutation">(
18
+ "renewalOperations/mutations:reserveAttempt",
19
+ );
20
+ afterEach(() => vi.unstubAllGlobals());
21
+
22
+ it("RESERVE01 commits policy-only V2 first-CAS DATA without a live basis", async () => {
23
+ // The maintained fixture declares its complete raw source/policy before C1/C2.
24
+ // All preparation and journal values here are unsigned DATA, not capabilities.
25
+ const h = await codecFixture();
26
+ const { t, physical } = componentFixture();
27
+ const createArgs = {
28
+ authorizationScope: h.record.authorization.scope,
29
+ logicalOperationId: h.record.logicalOperationId,
30
+ authorizationRevision: h.record.authorization.revision,
31
+ recordJSON: h.recordJSON,
32
+ };
33
+ expect(getConvexSize(createArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
34
+ expect(await physical()).toEqual({
35
+ operations: [],
36
+ attempts: [],
37
+ events: [],
38
+ payloads: [],
39
+ });
40
+ const created = await t.mutation(create, createArgs);
41
+ expect(created.status).toBe("created");
42
+ expect(getConvexSize(created)).toBeLessThanOrEqual(VALUE_LIMIT);
43
+ const queryArgs = { operationId: created.operationId };
44
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
45
+ const prepared = await t.query(load, queryArgs);
46
+ expect(prepared).toEqual({
47
+ recordVersion: 2,
48
+ operationId: created.operationId,
49
+ recordJSON: h.recordJSON,
50
+ revision: 0,
51
+ activeAttempt: null,
52
+ initialBasis: null,
53
+ });
54
+ expect(getConvexSize(prepared)).toBeLessThanOrEqual(VALUE_LIMIT);
55
+ const before = await physical();
56
+ expect(before.operations).toHaveLength(1);
57
+ expect(before.payloads).toHaveLength(1);
58
+ expect(before.attempts).toHaveLength(0);
59
+ expect(before.events).toHaveLength(0);
60
+ const operation = before.operations[0];
61
+ const payload = before.payloads[0];
62
+ expect(operation).toMatchObject({
63
+ _id: created.operationId,
64
+ recordVersion: 2,
65
+ payloadId: payload._id,
66
+ requestBinding: h.record.requestBinding,
67
+ revision: 0,
68
+ state: "PREPARED",
69
+ });
70
+ expect(payload).toMatchObject({
71
+ codecVersion: 1,
72
+ json: h.recordJSON,
73
+ owner: { kind: "operation", operationId: created.operationId },
74
+ });
75
+ expect(Object.prototype.hasOwnProperty.call(operation, "initialBasis")).toBe(
76
+ false,
77
+ );
78
+ for (const rows of Object.values(before)) {
79
+ for (const row of rows)
80
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
81
+ }
82
+ const args = {
83
+ operationId: created.operationId,
84
+ expectedRevision: 0,
85
+ stage: "attach" as const,
86
+ expectedRequestBinding: h.record.requestBinding,
87
+ recordJSON: h.recordJSON,
88
+ };
89
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
90
+ let stable = JSON.stringify(before);
91
+ let targetFailed = false;
92
+ try {
93
+ // Actual existing route, currently fenced: this is the first desired behavior.
94
+ const acquired = await t.mutation(reserve, args);
95
+ expect(acquired.disposition).toBe("acquired");
96
+ expect(getConvexSize(acquired)).toBeLessThanOrEqual(VALUE_LIMIT);
97
+ const summary = {
98
+ recordVersion: 2,
99
+ operationId: created.operationId,
100
+ payloadId: payload._id,
101
+ authorizationScope: h.record.authorization.scope,
102
+ authorizationId: h.record.authorization.id,
103
+ authorizationRevision: h.record.authorization.revision,
104
+ logicalOperationId: h.record.logicalOperationId,
105
+ requestBinding: h.record.requestBinding,
106
+ stage: "attach",
107
+ key: h.request.key,
108
+ apiVersion: h.request.apiVersion,
109
+ accountJSON: canonical(h.record.account),
110
+ timingPolicyJSON: canonical(h.record.timingPolicy),
111
+ firstDispatchedAt: h.request.firstDispatchedAt,
112
+ ordinal: 1,
113
+ preparedRevision: 0,
114
+ committedRevision: 1,
115
+ attemptId: acquired.reservation.attemptId,
116
+ };
117
+ expect(acquired).toEqual({
118
+ disposition: "acquired",
119
+ previousRevision: 0,
120
+ committedRevision: 1,
121
+ reservation: { ...summary, recordJSON: h.recordJSON },
122
+ });
123
+ expect(
124
+ Object.prototype.hasOwnProperty.call(acquired.reservation, "basisJSON"),
125
+ ).toBe(false);
126
+ const stored = await physical();
127
+ stable = JSON.stringify(stored);
128
+ expect(stored.operations).toHaveLength(1);
129
+ expect(stored.attempts).toHaveLength(1);
130
+ expect(stored.payloads).toEqual(before.payloads);
131
+ expect(stored.events).toHaveLength(0);
132
+ expect(stored.operations[0]).toEqual({
133
+ ...operation,
134
+ revision: 1,
135
+ state: "RESERVED",
136
+ activeAttemptId: summary.attemptId,
137
+ });
138
+ const { attemptId, ...fields } = summary;
139
+ expect(stored.attempts[0]).toEqual({
140
+ ...fields,
141
+ _id: attemptId,
142
+ _creationTime: stored.attempts[0]._creationTime,
143
+ lastSequence: 0,
144
+ });
145
+ for (const rows of Object.values(stored)) {
146
+ for (const row of rows)
147
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
148
+ }
149
+ const loaded = await t.query(load, queryArgs);
150
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
151
+ expect(loaded).toEqual({
152
+ recordVersion: 2,
153
+ operationId: created.operationId,
154
+ recordJSON: h.recordJSON,
155
+ revision: 1,
156
+ activeAttempt: summary,
157
+ initialBasis: null,
158
+ });
159
+ expect(canonical(h.record)).toBe(h.recordJSON);
160
+ } catch (error) {
161
+ targetFailed = true;
162
+ console.info(
163
+ "RESERVE01_TARGET_ERROR",
164
+ error instanceof Error ? error.message : "NON_ERROR_THROW",
165
+ );
166
+ throw error;
167
+ } finally {
168
+ try {
169
+ const after = await physical();
170
+ console.info(
171
+ "RESERVE01_SETTLEMENT",
172
+ JSON.stringify({
173
+ physicalEqual: JSON.stringify(after) === stable,
174
+ rows: Object.fromEntries(
175
+ Object.entries(after).map(([key, rows]) => [key, rows.length]),
176
+ ),
177
+ fetchCalls: h.fetchCalls(),
178
+ }),
179
+ );
180
+ expect(JSON.stringify(after)).toBe(stable);
181
+ expect(h.fetchCalls()).toBe(0);
182
+ } catch (inspectionError) {
183
+ if (!targetFailed) throw inspectionError;
184
+ console.info("RESERVE01_READONLY_INSPECTION_FAILED");
185
+ }
186
+ }
187
+ });
188
+
189
+ const changedWorkEndWallMs = 1700000005001;
190
+
191
+ async function preparedOperation() {
192
+ const h = await codecFixture();
193
+ const { t, physical } = componentFixture();
194
+ const createArgs = {
195
+ authorizationScope: h.record.authorization.scope,
196
+ logicalOperationId: h.record.logicalOperationId,
197
+ authorizationRevision: h.record.authorization.revision,
198
+ recordJSON: h.recordJSON,
199
+ };
200
+ expect(getConvexSize(createArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
201
+ const created = await t.mutation(create, createArgs);
202
+ expect(created.status).toBe("created");
203
+ expect(getConvexSize(created)).toBeLessThanOrEqual(VALUE_LIMIT);
204
+ const queryArgs = { operationId: created.operationId };
205
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
206
+ const loaded = await t.query(load, queryArgs);
207
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
208
+ expect(loaded).toEqual({
209
+ recordVersion: 2,
210
+ operationId: created.operationId,
211
+ recordJSON: h.recordJSON,
212
+ revision: 0,
213
+ activeAttempt: null,
214
+ initialBasis: null,
215
+ });
216
+ const before = await physical();
217
+ expect(before.operations).toHaveLength(1);
218
+ expect(before.payloads).toHaveLength(1);
219
+ expect(before.attempts).toHaveLength(0);
220
+ expect(before.events).toHaveLength(0);
221
+ expect(before.operations[0]).toMatchObject({
222
+ _id: created.operationId,
223
+ recordVersion: 2,
224
+ payloadId: before.payloads[0]._id,
225
+ revision: 0,
226
+ state: "PREPARED",
227
+ });
228
+ expect(before.payloads[0]).toMatchObject({
229
+ json: h.recordJSON,
230
+ codecVersion: 1,
231
+ owner: { kind: "operation", operationId: created.operationId },
232
+ });
233
+ const args = {
234
+ operationId: created.operationId,
235
+ expectedRevision: 0,
236
+ stage: "attach" as const,
237
+ expectedRequestBinding: h.record.requestBinding,
238
+ recordJSON: h.recordJSON,
239
+ };
240
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
241
+ for (const rows of Object.values(before)) {
242
+ for (const row of rows)
243
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
244
+ }
245
+ return { h, t, physical, before, args };
246
+ }
247
+
248
+ it("RESERVE02 concurrently commits one acquired and one identical existing V2 DATA reservation", async () => {
249
+ const { h, t, physical, before, args } = await preparedOperation();
250
+ let stable = JSON.stringify(before);
251
+ let targetFailed = false;
252
+ try {
253
+ const settled = await Promise.allSettled([
254
+ t.mutation(reserve, args),
255
+ t.mutation(reserve, args),
256
+ ]);
257
+ const replies = settled.map((reply) => {
258
+ if (reply.status !== "fulfilled") throw reply.reason;
259
+ return reply.value;
260
+ });
261
+ expect(settled.map((reply) => reply.status)).toEqual([
262
+ "fulfilled",
263
+ "fulfilled",
264
+ ]);
265
+ expect(
266
+ replies.filter((reply) => reply.disposition === "acquired"),
267
+ ).toHaveLength(1);
268
+ expect(
269
+ replies.filter((reply) => reply.disposition === "existing"),
270
+ ).toHaveLength(1);
271
+ const acquired = replies.find((reply) => reply.disposition === "acquired");
272
+ const existing = replies.find((reply) => reply.disposition === "existing");
273
+ expect(acquired).toBeDefined();
274
+ expect(existing).toBeDefined();
275
+ if (!acquired || !existing) throw Error("RESERVE02_MISSING_REPLY");
276
+ const expected = {
277
+ recordVersion: 2,
278
+ operationId: args.operationId,
279
+ payloadId: before.payloads[0]._id,
280
+ authorizationScope: h.record.authorization.scope,
281
+ authorizationId: h.record.authorization.id,
282
+ authorizationRevision: h.record.authorization.revision,
283
+ logicalOperationId: h.record.logicalOperationId,
284
+ requestBinding: h.record.requestBinding,
285
+ stage: "attach",
286
+ key: h.request.key,
287
+ apiVersion: h.request.apiVersion,
288
+ accountJSON: canonical(h.record.account),
289
+ timingPolicyJSON: canonical(h.record.timingPolicy),
290
+ firstDispatchedAt: h.request.firstDispatchedAt,
291
+ ordinal: 1,
292
+ preparedRevision: 0,
293
+ committedRevision: 1,
294
+ attemptId: acquired.reservation.attemptId,
295
+ recordJSON: h.recordJSON,
296
+ };
297
+ expect(acquired).toEqual({
298
+ disposition: "acquired",
299
+ previousRevision: 0,
300
+ committedRevision: 1,
301
+ reservation: expected,
302
+ });
303
+ expect(existing).toEqual({
304
+ disposition: "existing",
305
+ committedRevision: 1,
306
+ reservation: expected,
307
+ });
308
+ for (const reply of replies)
309
+ expect(getConvexSize(reply)).toBeLessThanOrEqual(VALUE_LIMIT);
310
+ const stored = await physical();
311
+ stable = JSON.stringify(stored);
312
+ expect(stored.operations).toHaveLength(1);
313
+ expect(stored.attempts).toHaveLength(1);
314
+ expect(stored.payloads).toEqual(before.payloads);
315
+ expect(stored.events).toHaveLength(0);
316
+ expect(stored.operations[0]).toEqual({
317
+ ...before.operations[0],
318
+ revision: 1,
319
+ state: "RESERVED",
320
+ activeAttemptId: expected.attemptId,
321
+ });
322
+ const { recordJSON: _recordJSON, attemptId, ...fields } = expected;
323
+ void _recordJSON;
324
+ expect(stored.attempts[0]).toEqual({
325
+ ...fields,
326
+ _id: attemptId,
327
+ _creationTime: stored.attempts[0]._creationTime,
328
+ lastSequence: 0,
329
+ });
330
+ for (const rows of Object.values(stored)) {
331
+ for (const row of rows)
332
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
333
+ }
334
+ const replay = await t.mutation(reserve, args);
335
+ expect(replay).toEqual(existing);
336
+ expect(getConvexSize(replay)).toBeLessThanOrEqual(VALUE_LIMIT);
337
+ expect(JSON.stringify(await physical())).toBe(stable);
338
+ // Exact row equality above excludes a persisted acquired/disposition flag.
339
+ expect(canonical(h.record)).toBe(h.recordJSON);
340
+ } catch (error) {
341
+ targetFailed = true;
342
+ throw error;
343
+ } finally {
344
+ try {
345
+ expect(JSON.stringify(await physical())).toBe(stable);
346
+ expect(h.fetchCalls()).toBe(0);
347
+ } catch (inspectionError) {
348
+ if (!targetFailed) throw inspectionError;
349
+ console.info("RESERVE02_READONLY_INSPECTION_FAILED");
350
+ }
351
+ }
352
+ });
353
+
354
+ it("RESERVE03 refuses a changed complete timing policy without changing PREPARED DATA", async () => {
355
+ const { h, t, physical, before, args } = await preparedOperation();
356
+ const changed = {
357
+ ...h.record,
358
+ timingPolicy: {
359
+ ...h.record.timingPolicy,
360
+ workEndWallMs: changedWorkEndWallMs,
361
+ },
362
+ };
363
+ const recordJSON = canonical(changed);
364
+ expect(recordJSON).not.toBe(h.recordJSON);
365
+ expect(recordJSON.length).toBeLessThanOrEqual(500000);
366
+ expect(new TextEncoder().encode(recordJSON).length).toBeLessThanOrEqual(
367
+ 500000,
368
+ );
369
+ expect(canonicalJournalRecord(recordJSON)).toMatchObject({
370
+ recordVersion: 2,
371
+ json: recordJSON,
372
+ });
373
+ const changedArgs = { ...args, recordJSON };
374
+ expect(getConvexSize(changedArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
375
+ let targetFailed = false;
376
+ try {
377
+ const refused = await t.mutation(reserve, changedArgs);
378
+ expect(getConvexSize(refused)).toBeLessThanOrEqual(VALUE_LIMIT);
379
+ expect(refused).toEqual({
380
+ disposition: "conflict",
381
+ reason: "identity_changed",
382
+ });
383
+ } catch (error) {
384
+ targetFailed = true;
385
+ throw error;
386
+ } finally {
387
+ try {
388
+ expect(await physical()).toEqual(before);
389
+ expect(h.fetchCalls()).toBe(0);
390
+ } catch (inspectionError) {
391
+ if (!targetFailed) throw inspectionError;
392
+ console.info("RESERVE03_READONLY_INSPECTION_FAILED");
393
+ }
394
+ }
395
+ });
396
+
397
+ const append = makeFunctionReference<"mutation">(
398
+ "renewalOperations/mutations:appendStageEvent",
399
+ );
400
+
401
+ it("UNBOUND01 refuses an actual complete event1 append before basis binding", async () => {
402
+ const { h, t, physical, args } = await preparedOperation();
403
+ const acquired = await t.mutation(reserve, args);
404
+ expect(acquired.disposition).toBe("acquired");
405
+ expect(acquired.reservation).toMatchObject({
406
+ recordVersion: 2,
407
+ recordJSON: h.recordJSON,
408
+ });
409
+ expect(getConvexSize(acquired)).toBeLessThanOrEqual(VALUE_LIMIT);
410
+ const { recordJSON, ...summary } = acquired.reservation;
411
+ expect(recordJSON).toBe(h.recordJSON);
412
+ const clean = await t.query(load, { operationId: args.operationId });
413
+ expect(getConvexSize(clean)).toBeLessThanOrEqual(VALUE_LIMIT);
414
+ expect(clean).toEqual({
415
+ recordVersion: 2,
416
+ operationId: args.operationId,
417
+ recordJSON: h.recordJSON,
418
+ revision: 1,
419
+ activeAttempt: summary,
420
+ initialBasis: null,
421
+ });
422
+ // Canonical unsigned dispatch DATA; no actual typed C10/Owner claim.
423
+ const datum = { event: "dispatch", stage: "attach", request: h.request };
424
+ const event = await canonicalJournalEvent(1, canonical(datum));
425
+ expect(JSON.parse(event.json)).toEqual(datum);
426
+ const appendArgs = {
427
+ reservation: acquired.reservation,
428
+ expectedSequence: 0,
429
+ event: {
430
+ sequence: event.sequence,
431
+ binding: event.binding,
432
+ dataJSON: event.json,
433
+ },
434
+ };
435
+ expect(getConvexSize(appendArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
436
+ const before = await physical();
437
+ expect(before.operations).toHaveLength(1);
438
+ expect(before.attempts).toHaveLength(1);
439
+ expect(before.payloads).toHaveLength(1);
440
+ expect(before.events).toHaveLength(0);
441
+ expect(before.attempts[0].lastSequence).toBe(0);
442
+ expect(
443
+ Object.prototype.hasOwnProperty.call(before.operations[0], "initialBasis"),
444
+ ).toBe(false);
445
+ for (const rows of Object.values(before)) {
446
+ for (const row of rows)
447
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
448
+ }
449
+ let targetFailed = false;
450
+ try {
451
+ await expect(t.mutation(append, appendArgs)).rejects.toThrow(
452
+ /^RENEWAL_JOURNAL_CONFLICT$/,
453
+ );
454
+ } catch (error) {
455
+ targetFailed = true;
456
+ throw error;
457
+ } finally {
458
+ try {
459
+ expect(await physical()).toEqual(before);
460
+ expect(h.fetchCalls()).toBe(0);
461
+ } catch (inspectionError) {
462
+ if (!targetFailed) throw inspectionError;
463
+ console.info("UNBOUND01_READONLY_INSPECTION_FAILED");
464
+ }
465
+ }
466
+ });
467
+
468
+ it("UNBOUND02 refuses an actual event row hidden behind a zero sequence counter", async () => {
469
+ const { h, t, physical, args } = await preparedOperation();
470
+ const acquired = await t.mutation(reserve, args);
471
+ expect(acquired.disposition).toBe("acquired");
472
+ expect(acquired.reservation).toMatchObject({
473
+ recordVersion: 2,
474
+ recordJSON: h.recordJSON,
475
+ });
476
+ expect(getConvexSize(acquired)).toBeLessThanOrEqual(VALUE_LIMIT);
477
+ const { recordJSON, ...summary } = acquired.reservation;
478
+ expect(recordJSON).toBe(h.recordJSON);
479
+ const clean = await t.query(load, { operationId: args.operationId });
480
+ expect(getConvexSize(clean)).toBeLessThanOrEqual(VALUE_LIMIT);
481
+ expect(clean).toEqual({
482
+ recordVersion: 2,
483
+ operationId: args.operationId,
484
+ recordJSON: h.recordJSON,
485
+ revision: 1,
486
+ activeAttempt: summary,
487
+ initialBasis: null,
488
+ });
489
+ const event = await canonicalJournalEvent(
490
+ 1,
491
+ canonical({ event: "dispatch", stage: "attach", request: h.request }),
492
+ );
493
+ const intact = await physical();
494
+ expect(intact.operations).toHaveLength(1);
495
+ expect(intact.attempts).toHaveLength(1);
496
+ expect(intact.attempts[0].lastSequence).toBe(0);
497
+ expect(intact.events).toHaveLength(0);
498
+ expect(intact.payloads).toHaveLength(1);
499
+ // Deliberate physical corruption using actual returned IDs and canonical DATA.
500
+ // The attempt counter deliberately stays zero; no receipt/basis is fabricated.
501
+ const injected = await t.run(async (ctx) => {
502
+ const payloadId = await ctx.db.insert("renewalOperationPayloads", {
503
+ codecVersion: 1,
504
+ json: event.json,
505
+ owner: {
506
+ kind: "event",
507
+ operationId: args.operationId,
508
+ attemptId: acquired.reservation.attemptId,
509
+ stage: "attach",
510
+ sequence: 1,
511
+ },
512
+ });
513
+ const eventId = await ctx.db.insert("renewalOperationEvents", {
514
+ operationId: args.operationId,
515
+ attemptId: acquired.reservation.attemptId,
516
+ stage: "attach",
517
+ sourcePayloadId: acquired.reservation.payloadId,
518
+ sequence: 1,
519
+ binding: event.binding,
520
+ payloadId,
521
+ });
522
+ return { payloadId, eventId };
523
+ });
524
+ const before = await physical();
525
+ expect(before.operations).toEqual(intact.operations);
526
+ expect(before.attempts).toEqual(intact.attempts);
527
+ expect(before.payloads).toHaveLength(2);
528
+ expect(before.events).toHaveLength(1);
529
+ expect(
530
+ before.payloads.find((row) => row._id === intact.payloads[0]._id),
531
+ ).toEqual(intact.payloads[0]);
532
+ expect(
533
+ before.payloads.find((row) => row._id === injected.payloadId),
534
+ ).toMatchObject({
535
+ json: event.json,
536
+ codecVersion: 1,
537
+ owner: {
538
+ kind: "event",
539
+ operationId: args.operationId,
540
+ attemptId: acquired.reservation.attemptId,
541
+ stage: "attach",
542
+ sequence: 1,
543
+ },
544
+ });
545
+ expect(before.events[0]).toMatchObject({
546
+ _id: injected.eventId,
547
+ operationId: args.operationId,
548
+ attemptId: acquired.reservation.attemptId,
549
+ stage: "attach",
550
+ sourcePayloadId: acquired.reservation.payloadId,
551
+ sequence: 1,
552
+ binding: event.binding,
553
+ payloadId: injected.payloadId,
554
+ });
555
+ for (const rows of Object.values(before)) {
556
+ for (const row of rows)
557
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
558
+ }
559
+ const queryArgs = { operationId: args.operationId };
560
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
561
+ let targetFailed = false;
562
+ try {
563
+ await expect(t.query(load, queryArgs)).rejects.toThrow(
564
+ /^RENEWAL_JOURNAL_CONFLICT$/,
565
+ );
566
+ } catch (error) {
567
+ targetFailed = true;
568
+ throw error;
569
+ } finally {
570
+ try {
571
+ expect(await physical()).toEqual(before);
572
+ expect(h.fetchCalls()).toBe(0);
573
+ } catch (inspectionError) {
574
+ if (!targetFailed) throw inspectionError;
575
+ console.info("UNBOUND02_READONLY_INSPECTION_FAILED");
576
+ }
577
+ }
578
+ });