@fantastic.dev/better-stripe 0.91.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 (132) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/configure/actor.d.ts +6 -0
  4. package/dist/client/billing/renewalAttach/internal/configure/actor.d.ts.map +1 -1
  5. package/dist/client/billing/renewalAttach/internal/configure/actor.js +46 -56
  6. package/dist/client/billing/renewalAttach/internal/configure/actor.js.map +1 -1
  7. package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.d.ts +3 -0
  8. package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.d.ts.map +1 -0
  9. package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.js +61 -0
  10. package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.js.map +1 -0
  11. package/dist/client/billing/renewalAttach/internal/observation/actor.d.ts.map +1 -1
  12. package/dist/client/billing/renewalAttach/internal/observation/actor.js +25 -4
  13. package/dist/client/billing/renewalAttach/internal/observation/actor.js.map +1 -1
  14. package/dist/client/billing/renewalAttach/internal/observation/capture.d.ts.map +1 -1
  15. package/dist/client/billing/renewalAttach/internal/observation/capture.js +3 -2
  16. package/dist/client/billing/renewalAttach/internal/observation/capture.js.map +1 -1
  17. package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.d.ts +5 -0
  18. package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.d.ts.map +1 -0
  19. package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.js +11 -0
  20. package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.js.map +1 -0
  21. package/dist/client/billing/renewalAttach/internal/observation/types.d.ts +5 -0
  22. package/dist/client/billing/renewalAttach/internal/observation/types.d.ts.map +1 -1
  23. package/dist/client/billing/renewalAttach/internal/observation/types.js.map +1 -1
  24. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +30 -0
  25. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -0
  26. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +113 -0
  27. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -0
  28. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +562 -0
  29. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  30. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +281 -0
  31. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  32. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +562 -0
  33. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  34. package/dist/client/connect/payoutSyncTestHelpers.d.ts +281 -0
  35. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  36. package/dist/client/index.d.ts +3 -2
  37. package/dist/client/index.d.ts.map +1 -1
  38. package/dist/client/index.js +16 -3
  39. package/dist/client/index.js.map +1 -1
  40. package/dist/component/_generated/api.d.ts +6 -0
  41. package/dist/component/_generated/api.d.ts.map +1 -1
  42. package/dist/component/_generated/api.js.map +1 -1
  43. package/dist/component/_generated/component.d.ts +406 -0
  44. package/dist/component/_generated/component.d.ts.map +1 -1
  45. package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
  46. package/dist/component/earnings/triggers.d.ts +294 -0
  47. package/dist/component/earnings/triggers.d.ts.map +1 -1
  48. package/dist/component/payoutRelease/queries.d.ts +2 -2
  49. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  50. package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
  51. package/dist/component/renewalOperations/helpers.d.ts +236 -0
  52. package/dist/component/renewalOperations/helpers.d.ts.map +1 -0
  53. package/dist/component/renewalOperations/helpers.js +457 -0
  54. package/dist/component/renewalOperations/helpers.js.map +1 -0
  55. package/dist/component/renewalOperations/mutations.d.ts +252 -0
  56. package/dist/component/renewalOperations/mutations.d.ts.map +1 -0
  57. package/dist/component/renewalOperations/mutations.js +340 -0
  58. package/dist/component/renewalOperations/mutations.js.map +1 -0
  59. package/dist/component/renewalOperations/queries.d.ts +163 -0
  60. package/dist/component/renewalOperations/queries.d.ts.map +1 -0
  61. package/dist/component/renewalOperations/queries.js +167 -0
  62. package/dist/component/renewalOperations/queries.js.map +1 -0
  63. package/dist/component/renewalOperations/schema.d.ts +1988 -0
  64. package/dist/component/renewalOperations/schema.d.ts.map +1 -0
  65. package/dist/component/renewalOperations/schema.js +227 -0
  66. package/dist/component/renewalOperations/schema.js.map +1 -0
  67. package/dist/component/schema.d.ts +281 -0
  68. package/dist/component/schema.js +5 -0
  69. package/dist/component/schema.js.map +1 -1
  70. package/dist/component/usage/allowance.fixtures.d.ts +281 -0
  71. package/dist/component/usage/allowance.fixtures.d.ts.map +1 -1
  72. package/dist/component/usageCredits/accounting.fixtures.d.ts +281 -0
  73. package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -1
  74. package/dist/shared/renewalOperationJournal.d.ts +43 -0
  75. package/dist/shared/renewalOperationJournal.d.ts.map +1 -0
  76. package/dist/shared/renewalOperationJournal.js +294 -0
  77. package/dist/shared/renewalOperationJournal.js.map +1 -0
  78. package/docs/renewal-context-reads.md +152 -0
  79. package/docs/renewal-material-observation.md +32 -11
  80. package/package.json +1 -1
  81. package/src/client/billing/renewalAttach/internal/configure/actor.ts +63 -52
  82. package/src/client/billing/renewalAttach/internal/configure/ownedContinuation.ts +56 -0
  83. package/src/client/billing/renewalAttach/internal/observation/actor.ts +46 -3
  84. package/src/client/billing/renewalAttach/internal/observation/capture.ts +3 -2
  85. package/src/client/billing/renewalAttach/internal/observation/configureAcknowledgement.ts +20 -0
  86. package/src/client/billing/renewalAttach/internal/observation/observationConfigureACK.integration.test.ts +36 -0
  87. package/src/client/billing/renewalAttach/internal/observation/observationConfigureACKControls.integration.test.ts +48 -0
  88. package/src/client/billing/renewalAttach/internal/observation/observationConfigureACKLifecycle.integration.test.ts +44 -0
  89. package/src/client/billing/renewalAttach/internal/observation/observationConfiguredHistory.integration.test.ts +34 -0
  90. package/src/client/billing/renewalAttach/internal/observation/types.ts +5 -0
  91. package/src/client/billing/renewalAttach/internal/operationJournal.integration.test.ts +380 -0
  92. package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +155 -0
  93. package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteAssertions.ts +5 -1
  94. package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteDeclarations.ts +12 -1
  95. package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteDelegate.ts +38 -0
  96. package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteHistory.ts +8 -0
  97. package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteOperation.ts +10 -23
  98. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKAssertions.ts +115 -0
  99. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKDeclarations.ts +141 -0
  100. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleAssertions.ts +74 -0
  101. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleDeclarations.ts +79 -0
  102. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleInstrumentation.ts +207 -0
  103. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleOperation.ts +494 -0
  104. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKOperation.ts +452 -0
  105. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryAssertions.ts +88 -0
  106. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryDeclarations.ts +170 -0
  107. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryNative.ts +99 -0
  108. package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryOperation.ts +411 -0
  109. package/src/client/index.ts +24 -3
  110. package/src/component/_generated/api.ts +6 -0
  111. package/src/component/_generated/component.ts +450 -0
  112. package/src/component/renewalOperations/acknowledgement.fixtures.ts +552 -0
  113. package/src/component/renewalOperations/acknowledgement.test.ts +573 -0
  114. package/src/component/renewalOperations/acknowledgementReads.test.ts +442 -0
  115. package/src/component/renewalOperations/acknowledgementV2.test.ts +493 -0
  116. package/src/component/renewalOperations/acknowledgementV2Publication.test.ts +373 -0
  117. package/src/component/renewalOperations/basisBinding.test.ts +567 -0
  118. package/src/component/renewalOperations/basisLifecycle.test.ts +569 -0
  119. package/src/component/renewalOperations/boundHistory.test.ts +474 -0
  120. package/src/component/renewalOperations/helpers.ts +592 -0
  121. package/src/component/renewalOperations/historyReads.test.ts +595 -0
  122. package/src/component/renewalOperations/initialBasis.test.ts +578 -0
  123. package/src/component/renewalOperations/journal.test.ts +567 -0
  124. package/src/component/renewalOperations/mutations.ts +456 -0
  125. package/src/component/renewalOperations/payload.test.ts +589 -0
  126. package/src/component/renewalOperations/payloadEncoding.test.ts +592 -0
  127. package/src/component/renewalOperations/queries.ts +204 -0
  128. package/src/component/renewalOperations/schema.ts +268 -0
  129. package/src/component/renewalOperations/timingPolicy.fixtures.ts +193 -0
  130. package/src/component/renewalOperations/timingPolicy.test.ts +523 -0
  131. package/src/component/schema.ts +11 -0
  132. package/src/shared/renewalOperationJournal.ts +333 -0
@@ -0,0 +1,569 @@
1
+ import { makeFunctionReference } from "convex/server";
2
+ import { getConvexSize, getDocumentSize } from "convex/values";
3
+ import { afterEach, expect, it, vi } from "vitest";
4
+ import { canonicalJournalEvent } from "../../shared/renewalOperationJournal.js";
5
+ import {
6
+ canonical,
7
+ codecFixture,
8
+ componentFixture,
9
+ create,
10
+ load,
11
+ VALUE_LIMIT,
12
+ } from "./timingPolicy.fixtures.js";
13
+
14
+ const reserve = makeFunctionReference<"mutation">(
15
+ "renewalOperations/mutations:reserveAttempt",
16
+ );
17
+ const bind = makeFunctionReference<"mutation">(
18
+ "renewalOperations/mutations:bindInitialBasis",
19
+ );
20
+ // Fixed unsigned DATA, never an actual sampled clock/Owner or eligibility claim.
21
+ const basis = Object.freeze({
22
+ startWallMs: 1700000001000,
23
+ startNsDecimal: "1000000000",
24
+ workEndWallMs: 1700000005000,
25
+ workEndNsDecimal: "5000000000",
26
+ endWallMs: 1700000006000,
27
+ endNsDecimal: "6000000000",
28
+ });
29
+ const changedAnchor = Object.freeze({
30
+ ...basis,
31
+ startNsDecimal: "1000000001",
32
+ workEndNsDecimal: "5000000001",
33
+ endNsDecimal: "6000000001",
34
+ });
35
+ const corruptBasis = Object.freeze({
36
+ ...basis,
37
+ workEndNsDecimal: "5000000001",
38
+ });
39
+ const legacyBasis = Object.freeze({
40
+ startWallMs: basis.startWallMs,
41
+ startNs: basis.startNsDecimal,
42
+ workEndWallMs: basis.workEndWallMs,
43
+ workEndNs: basis.workEndNsDecimal,
44
+ endWallMs: basis.endWallMs,
45
+ endNs: basis.endNsDecimal,
46
+ });
47
+ afterEach(() => vi.unstubAllGlobals());
48
+
49
+ async function boundOperation() {
50
+ const h = await codecFixture();
51
+ const { t, physical } = componentFixture();
52
+ expect(BigInt(basis.workEndNsDecimal)).toBe(
53
+ BigInt(basis.startNsDecimal) + 4000000000n,
54
+ );
55
+ expect(BigInt(basis.endNsDecimal)).toBe(
56
+ BigInt(basis.startNsDecimal) + 5000000000n,
57
+ );
58
+ const createArgs = {
59
+ authorizationScope: h.record.authorization.scope,
60
+ logicalOperationId: h.record.logicalOperationId,
61
+ authorizationRevision: h.record.authorization.revision,
62
+ recordJSON: h.recordJSON,
63
+ };
64
+ expect(getConvexSize(createArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
65
+ expect(await physical()).toEqual({
66
+ operations: [],
67
+ attempts: [],
68
+ events: [],
69
+ payloads: [],
70
+ });
71
+ const created = await t.mutation(create, createArgs);
72
+ expect(created.status).toBe("created");
73
+ expect(getConvexSize(created)).toBeLessThanOrEqual(VALUE_LIMIT);
74
+ const queryArgs = { operationId: created.operationId };
75
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
76
+ expect(await t.query(load, queryArgs)).toEqual({
77
+ recordVersion: 2,
78
+ operationId: created.operationId,
79
+ recordJSON: h.recordJSON,
80
+ revision: 0,
81
+ activeAttempt: null,
82
+ initialBasis: null,
83
+ });
84
+ const reserveArgs = {
85
+ operationId: created.operationId,
86
+ expectedRevision: 0,
87
+ stage: "attach" as const,
88
+ expectedRequestBinding: h.record.requestBinding,
89
+ recordJSON: h.recordJSON,
90
+ };
91
+ expect(getConvexSize(reserveArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
92
+ const acquired = await t.mutation(reserve, reserveArgs);
93
+ expect(acquired.disposition).toBe("acquired");
94
+ expect(getConvexSize(acquired)).toBeLessThanOrEqual(VALUE_LIMIT);
95
+ const before = await physical();
96
+ expect(before.operations).toHaveLength(1);
97
+ expect(before.attempts).toHaveLength(1);
98
+ expect(before.payloads).toHaveLength(1);
99
+ expect(before.events).toHaveLength(0);
100
+ const operation = before.operations[0];
101
+ const attempt = before.attempts[0];
102
+ const payload = before.payloads[0];
103
+ expect(operation).toMatchObject({
104
+ _id: created.operationId,
105
+ recordVersion: 2,
106
+ payloadId: payload._id,
107
+ requestBinding: h.record.requestBinding,
108
+ revision: 1,
109
+ state: "RESERVED",
110
+ activeAttemptId: attempt._id,
111
+ });
112
+ expect(payload).toMatchObject({
113
+ codecVersion: 1,
114
+ json: h.recordJSON,
115
+ owner: { kind: "operation", operationId: created.operationId },
116
+ });
117
+ expect(Object.prototype.hasOwnProperty.call(operation, "initialBasis")).toBe(
118
+ false,
119
+ );
120
+ const summary = {
121
+ recordVersion: 2,
122
+ operationId: created.operationId,
123
+ payloadId: payload._id,
124
+ authorizationScope: h.record.authorization.scope,
125
+ authorizationId: h.record.authorization.id,
126
+ authorizationRevision: h.record.authorization.revision,
127
+ logicalOperationId: h.record.logicalOperationId,
128
+ requestBinding: h.record.requestBinding,
129
+ stage: "attach",
130
+ key: h.request.key,
131
+ apiVersion: h.request.apiVersion,
132
+ accountJSON: canonical(h.record.account),
133
+ timingPolicyJSON: canonical(h.record.timingPolicy),
134
+ firstDispatchedAt: h.request.firstDispatchedAt,
135
+ ordinal: 1,
136
+ preparedRevision: 0,
137
+ committedRevision: 1,
138
+ attemptId: attempt._id,
139
+ };
140
+ expect(acquired).toEqual({
141
+ disposition: "acquired",
142
+ previousRevision: 0,
143
+ committedRevision: 1,
144
+ reservation: { ...summary, recordJSON: h.recordJSON },
145
+ });
146
+ const { attemptId, ...fields } = summary;
147
+ expect(attempt).toEqual({
148
+ ...fields,
149
+ _id: attemptId,
150
+ _creationTime: attempt._creationTime,
151
+ lastSequence: 0,
152
+ });
153
+ const reserved = await t.query(load, queryArgs);
154
+ expect(reserved).toEqual({
155
+ recordVersion: 2,
156
+ operationId: created.operationId,
157
+ recordJSON: h.recordJSON,
158
+ revision: 1,
159
+ activeAttempt: summary,
160
+ initialBasis: null,
161
+ });
162
+ expect(getConvexSize(reserved)).toBeLessThanOrEqual(VALUE_LIMIT);
163
+ const args = {
164
+ operationId: created.operationId,
165
+ attemptId: attempt._id,
166
+ policyJSON: canonical(h.record.timingPolicy),
167
+ basisJSON: canonical(basis),
168
+ };
169
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
170
+ const initialBasis = {
171
+ version: 1 as const,
172
+ attemptId: attempt._id,
173
+ sourcePayloadId: payload._id,
174
+ stage: "attach" as const,
175
+ policyJSON: args.policyJSON,
176
+ basisJSON: args.basisJSON,
177
+ };
178
+ const receipt = {
179
+ status: "bound",
180
+ operationId: created.operationId,
181
+ initialBasis,
182
+ };
183
+ const result = await t.mutation(bind, args);
184
+ expect(result).toEqual(receipt);
185
+ expect(getConvexSize(result)).toBeLessThanOrEqual(VALUE_LIMIT);
186
+ const boundRows = await physical();
187
+ expect(boundRows).toEqual({
188
+ ...before,
189
+ operations: [{ ...operation, initialBasis }],
190
+ });
191
+ for (const rows of Object.values(boundRows)) {
192
+ for (const row of rows)
193
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
194
+ }
195
+ const loaded = await t.query(load, queryArgs);
196
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
197
+ expect(loaded).toEqual({ ...reserved, initialBasis });
198
+ expect(h.fetchCalls()).toBe(0);
199
+ return {
200
+ fixture: h,
201
+ t,
202
+ physical,
203
+ args,
204
+ queryArgs,
205
+ initialBasis,
206
+ receipt,
207
+ boundRows,
208
+ reservation: acquired.reservation,
209
+ loaded,
210
+ };
211
+ }
212
+
213
+ it("BASISL01 returns the original canonical receipt for reordered duplicate DATA", async () => {
214
+ const h = await boundOperation();
215
+ const policyJSON = JSON.stringify(
216
+ Object.fromEntries(Object.entries(JSON.parse(h.args.policyJSON)).reverse()),
217
+ null,
218
+ 2,
219
+ );
220
+ const basisJSON = JSON.stringify(
221
+ Object.fromEntries(Object.entries(JSON.parse(h.args.basisJSON)).reverse()),
222
+ null,
223
+ 2,
224
+ );
225
+ expect(policyJSON).not.toBe(h.args.policyJSON);
226
+ expect(basisJSON).not.toBe(h.args.basisJSON);
227
+ expect(canonical(JSON.parse(policyJSON))).toBe(h.args.policyJSON);
228
+ expect(canonical(JSON.parse(basisJSON))).toBe(h.args.basisJSON);
229
+ const args = { ...h.args, policyJSON, basisJSON };
230
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
231
+ let targetFailed = false;
232
+ try {
233
+ const duplicate = await h.t.mutation(bind, args);
234
+ expect(getConvexSize(duplicate)).toBeLessThanOrEqual(VALUE_LIMIT);
235
+ expect(duplicate).toEqual(h.receipt);
236
+ } catch (error) {
237
+ targetFailed = true;
238
+ throw error;
239
+ } finally {
240
+ try {
241
+ expect(await h.physical()).toEqual(h.boundRows);
242
+ expect(h.fixture.fetchCalls()).toBe(0);
243
+ } catch (inspectionError) {
244
+ if (!targetFailed) throw inspectionError;
245
+ console.info("BASISL01_READONLY_INSPECTION_FAILED");
246
+ }
247
+ }
248
+ });
249
+
250
+ it("BASISL02 refuses a different mathematically valid anchor without rebinding", async () => {
251
+ expect(BigInt(changedAnchor.workEndNsDecimal)).toBe(
252
+ BigInt(changedAnchor.startNsDecimal) +
253
+ BigInt(changedAnchor.workEndWallMs - changedAnchor.startWallMs) *
254
+ 1000000n,
255
+ );
256
+ expect(BigInt(changedAnchor.endNsDecimal)).toBe(
257
+ BigInt(changedAnchor.startNsDecimal) +
258
+ BigInt(changedAnchor.endWallMs - changedAnchor.startWallMs) * 1000000n,
259
+ );
260
+ const h = await boundOperation();
261
+ const args = { ...h.args, basisJSON: canonical(changedAnchor) };
262
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
263
+ let targetFailed = false;
264
+ try {
265
+ await expect(h.t.mutation(bind, args)).rejects.toThrow(
266
+ /^RENEWAL_JOURNAL_CONFLICT$/,
267
+ );
268
+ } catch (error) {
269
+ targetFailed = true;
270
+ throw error;
271
+ } finally {
272
+ try {
273
+ expect(await h.physical()).toEqual(h.boundRows);
274
+ expect(h.fixture.fetchCalls()).toBe(0);
275
+ } catch (inspectionError) {
276
+ if (!targetFailed) throw inspectionError;
277
+ console.info("BASISL02_READONLY_INSPECTION_FAILED");
278
+ }
279
+ }
280
+ });
281
+
282
+ it("BASISL03 refuses corrupted stored projection through load and duplicate bind", async () => {
283
+ const h = await boundOperation();
284
+ const corrupt = { ...h.initialBasis, basisJSON: canonical(corruptBasis) };
285
+ await h.t.run((ctx) =>
286
+ ctx.db.patch("renewalOperations", h.args.operationId, {
287
+ initialBasis: corrupt,
288
+ }),
289
+ );
290
+ const damaged = await h.physical();
291
+ expect(damaged).toEqual({
292
+ ...h.boundRows,
293
+ operations: [{ ...h.boundRows.operations[0], initialBasis: corrupt }],
294
+ });
295
+ let targetFailed = false;
296
+ try {
297
+ const settled = await Promise.allSettled([
298
+ h.t.query(load, h.queryArgs),
299
+ h.t.mutation(bind, h.args),
300
+ ]);
301
+ expect(settled).toEqual([
302
+ {
303
+ status: "rejected",
304
+ reason: expect.objectContaining({
305
+ message: "RENEWAL_JOURNAL_CONFLICT",
306
+ }),
307
+ },
308
+ {
309
+ status: "rejected",
310
+ reason: expect.objectContaining({
311
+ message: "RENEWAL_JOURNAL_CONFLICT",
312
+ }),
313
+ },
314
+ ]);
315
+ } catch (error) {
316
+ targetFailed = true;
317
+ throw error;
318
+ } finally {
319
+ try {
320
+ expect(await h.physical()).toEqual(damaged);
321
+ expect(h.fixture.fetchCalls()).toBe(0);
322
+ } catch (inspectionError) {
323
+ if (!targetFailed) throw inspectionError;
324
+ console.info("BASISL03_READONLY_INSPECTION_FAILED");
325
+ }
326
+ }
327
+ });
328
+
329
+ it.each(["v2-prepared", "v1-prepared"] as const)(
330
+ "BASISL04 refuses a foreign descriptor on %s DATA",
331
+ async (kind) => {
332
+ const h = await boundOperation();
333
+ const candidate: Record<string, unknown> = {
334
+ ...h.fixture.record,
335
+ logicalOperationId: "basis-lifecycle-B",
336
+ authorization: {
337
+ ...h.fixture.record.authorization,
338
+ id: "basis-lifecycle-consent-B",
339
+ },
340
+ };
341
+ if (kind === "v1-prepared") {
342
+ candidate.schemaVersion = 1;
343
+ candidate.basis = legacyBasis;
344
+ delete candidate.timingPolicy;
345
+ }
346
+ const recordJSON = canonical(candidate);
347
+ const args = {
348
+ authorizationScope: h.fixture.record.authorization.scope,
349
+ logicalOperationId: "basis-lifecycle-B",
350
+ authorizationRevision: 1,
351
+ recordJSON,
352
+ };
353
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
354
+ const created = await h.t.mutation(create, args);
355
+ expect(created.status).toBe("created");
356
+ expect(getConvexSize(created)).toBeLessThanOrEqual(VALUE_LIMIT);
357
+ const queryArgs = { operationId: created.operationId };
358
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
359
+ const clean = await h.t.query(load, queryArgs);
360
+ expect(getConvexSize(clean)).toBeLessThanOrEqual(VALUE_LIMIT);
361
+ expect(clean).toEqual({
362
+ ...(kind === "v2-prepared"
363
+ ? { recordVersion: 2, initialBasis: null }
364
+ : {}),
365
+ operationId: created.operationId,
366
+ recordJSON,
367
+ revision: 0,
368
+ activeAttempt: null,
369
+ });
370
+ const before = await h.physical();
371
+ expect(before.operations).toHaveLength(2);
372
+ expect(before.attempts).toHaveLength(1);
373
+ expect(before.payloads).toHaveLength(2);
374
+ expect(before.events).toHaveLength(0);
375
+ const operationB = before.operations.find(
376
+ (row) => row._id === created.operationId,
377
+ );
378
+ expect(operationB).toMatchObject({
379
+ _id: created.operationId,
380
+ revision: 0,
381
+ state: "PREPARED",
382
+ });
383
+ if (!operationB) throw Error("BASISL04_MISSING_REAL_OPERATION");
384
+ expect(
385
+ before.payloads.find((row) => row._id === operationB.payloadId),
386
+ ).toMatchObject({
387
+ codecVersion: 1,
388
+ json: recordJSON,
389
+ owner: { kind: "operation", operationId: created.operationId },
390
+ });
391
+ const foreign = h.initialBasis;
392
+ expect(foreign.sourcePayloadId).not.toBe(operationB.payloadId);
393
+ await h.t.run((ctx) =>
394
+ ctx.db.patch("renewalOperations", created.operationId, {
395
+ initialBasis: foreign,
396
+ }),
397
+ );
398
+ const damaged = await h.physical();
399
+ expect(damaged).toEqual({
400
+ ...before,
401
+ operations: before.operations.map((row) =>
402
+ row._id === created.operationId
403
+ ? { ...row, initialBasis: foreign }
404
+ : row,
405
+ ),
406
+ });
407
+ for (const rows of Object.values(damaged)) {
408
+ for (const row of rows)
409
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
410
+ }
411
+ const foreignArgs = { ...h.args, operationId: created.operationId };
412
+ expect(getConvexSize(foreignArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
413
+ let targetFailed = false;
414
+ try {
415
+ const settled = await Promise.allSettled([
416
+ h.t.query(load, queryArgs),
417
+ h.t.mutation(bind, foreignArgs),
418
+ ]);
419
+ expect(settled).toEqual([
420
+ {
421
+ status: "rejected",
422
+ reason: expect.objectContaining({
423
+ message: "RENEWAL_JOURNAL_CONFLICT",
424
+ }),
425
+ },
426
+ {
427
+ status: "rejected",
428
+ reason: expect.objectContaining({
429
+ message: "RENEWAL_JOURNAL_CONFLICT",
430
+ }),
431
+ },
432
+ ]);
433
+ } catch (error) {
434
+ targetFailed = true;
435
+ throw error;
436
+ } finally {
437
+ try {
438
+ expect(await h.physical()).toEqual(damaged);
439
+ expect(h.fixture.fetchCalls()).toBe(0);
440
+ } catch (inspectionError) {
441
+ if (!targetFailed) throw inspectionError;
442
+ console.info("BASISL04_READONLY_INSPECTION_FAILED");
443
+ }
444
+ }
445
+ },
446
+ );
447
+
448
+ const append = makeFunctionReference<"mutation">(
449
+ "renewalOperations/mutations:appendStageEvent",
450
+ );
451
+
452
+ it("APPENDB01 appends canonical event1 after actual basis binding and preserves its receipt", async () => {
453
+ const h = await boundOperation();
454
+ // Complete generic unsigned event DATA; no typed C10/provider/Owner claim.
455
+ const datum = {
456
+ event: "dispatch",
457
+ stage: "attach",
458
+ request: h.fixture.request,
459
+ };
460
+ const event = await canonicalJournalEvent(1, canonical(datum));
461
+ expect(JSON.parse(event.json)).toEqual(datum);
462
+ const args = {
463
+ reservation: h.reservation,
464
+ expectedSequence: 0,
465
+ event: {
466
+ sequence: event.sequence,
467
+ binding: event.binding,
468
+ dataJSON: event.json,
469
+ },
470
+ };
471
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
472
+ expect(await h.physical()).toEqual(h.boundRows);
473
+ let stable = h.boundRows;
474
+ let targetFailed = false;
475
+ try {
476
+ // Actual existing registered append is currently fenced for V2.
477
+ const appended = await h.t.mutation(append, args);
478
+ expect(getConvexSize(appended)).toBeLessThanOrEqual(VALUE_LIMIT);
479
+ const descriptor = {
480
+ operationId: h.args.operationId,
481
+ attemptId: h.args.attemptId,
482
+ stage: "attach",
483
+ sourcePayloadId: h.reservation.payloadId,
484
+ sequence: 1,
485
+ binding: event.binding,
486
+ payloadId: appended.payloadId,
487
+ eventId: appended.eventId,
488
+ };
489
+ expect(appended).toEqual({ ...descriptor, reservation: h.reservation });
490
+ const after = await h.physical();
491
+ stable = after;
492
+ expect(after.operations).toEqual(h.boundRows.operations);
493
+ expect(after.attempts).toEqual([
494
+ { ...h.boundRows.attempts[0], lastSequence: 1 },
495
+ ]);
496
+ expect(after.payloads).toHaveLength(2);
497
+ expect(after.events).toHaveLength(1);
498
+ expect(
499
+ after.payloads.find((row) => row._id === h.reservation.payloadId),
500
+ ).toEqual(h.boundRows.payloads[0]);
501
+ const storedPayload = after.payloads.find(
502
+ (row) => row._id === appended.payloadId,
503
+ );
504
+ expect(storedPayload).toBeDefined();
505
+ if (!storedPayload) throw Error("APPENDB01_MISSING_REAL_PAYLOAD");
506
+ expect(storedPayload).toEqual({
507
+ _id: appended.payloadId,
508
+ _creationTime: storedPayload._creationTime,
509
+ codecVersion: 1,
510
+ json: event.json,
511
+ owner: {
512
+ kind: "event",
513
+ operationId: h.args.operationId,
514
+ attemptId: h.args.attemptId,
515
+ stage: "attach",
516
+ sequence: 1,
517
+ },
518
+ });
519
+ expect(after.events[0]).toEqual({
520
+ _id: appended.eventId,
521
+ _creationTime: after.events[0]._creationTime,
522
+ operationId: descriptor.operationId,
523
+ attemptId: descriptor.attemptId,
524
+ stage: descriptor.stage,
525
+ sourcePayloadId: descriptor.sourcePayloadId,
526
+ sequence: 1,
527
+ binding: descriptor.binding,
528
+ payloadId: descriptor.payloadId,
529
+ });
530
+ for (const rows of Object.values(after)) {
531
+ for (const row of rows)
532
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
533
+ }
534
+ const loaded = await h.t.query(load, h.queryArgs);
535
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
536
+ expect(loaded).toEqual(h.loaded);
537
+ const duplicateBasis = await h.t.mutation(bind, h.args);
538
+ expect(getConvexSize(duplicateBasis)).toBeLessThanOrEqual(VALUE_LIMIT);
539
+ expect(duplicateBasis).toEqual(h.receipt);
540
+ expect(await h.physical()).toEqual(after);
541
+ expect(canonical(h.fixture.record)).toBe(h.fixture.recordJSON);
542
+ } catch (error) {
543
+ targetFailed = true;
544
+ console.info(
545
+ "APPENDB01_TARGET_ERROR",
546
+ error instanceof Error ? error.message : "NON_ERROR_THROW",
547
+ );
548
+ throw error;
549
+ } finally {
550
+ try {
551
+ const after = await h.physical();
552
+ console.info(
553
+ "APPENDB01_SETTLEMENT",
554
+ JSON.stringify({
555
+ physicalEqual: JSON.stringify(after) === JSON.stringify(stable),
556
+ rows: Object.fromEntries(
557
+ Object.entries(after).map(([key, rows]) => [key, rows.length]),
558
+ ),
559
+ fetchCalls: h.fixture.fetchCalls(),
560
+ }),
561
+ );
562
+ expect(after).toEqual(stable);
563
+ expect(h.fixture.fetchCalls()).toBe(0);
564
+ } catch (inspectionError) {
565
+ if (!targetFailed) throw inspectionError;
566
+ console.info("APPENDB01_READONLY_INSPECTION_FAILED");
567
+ }
568
+ }
569
+ });