@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,442 @@
1
+ import assert from "node:assert/strict";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import {
4
+ getConvexSize,
5
+ getDocumentSize,
6
+ type Infer,
7
+ type ObjectType,
8
+ } from "convex/values";
9
+ import { afterEach, expect, it, vi } from "vitest";
10
+ import type {
11
+ retentionReceipt,
12
+ providerAcknowledgementReadResult,
13
+ knownAcknowledgement,
14
+ appendArgs,
15
+ } from "./schema.js";
16
+ import {
17
+ prepareAcknowledgementScenario,
18
+ retain,
19
+ append,
20
+ canonical,
21
+ VALUE_LIMIT,
22
+ } from "./acknowledgement.fixtures.js";
23
+
24
+ import { canonicalJournalEvent } from "../../shared/renewalOperationJournal.js";
25
+ import { captureAttachReceipt } from "../../client/billing/renewalAttach/internal/c10/eventCapture.js";
26
+ import { materialize } from "../../client/billing/renewalAttach/internal/c10/eventCodec.js";
27
+
28
+ const readAcknowledgement = makeFunctionReference<
29
+ "query",
30
+ { operationId: Infer<typeof retentionReceipt>["operationId"] },
31
+ Infer<typeof providerAcknowledgementReadResult>
32
+ >("renewalOperations/queries:readProviderAcknowledgement");
33
+ afterEach(() => vi.unstubAllGlobals());
34
+
35
+ it("READACK01 returns the complete owned known receipt and stable retention DATA", async () => {
36
+ const h = await prepareAcknowledgementScenario("clean");
37
+ const retained = await h.t.mutation(retain, h.args);
38
+ assert(retained);
39
+ expect(getConvexSize(retained)).toBeLessThanOrEqual(VALUE_LIMIT);
40
+ const expectedKnown = {
41
+ schemaVersion: 1,
42
+ stage: "attach",
43
+ attemptId: h.acquired.reservation.attemptId,
44
+ sourcePayloadId: h.acquired.reservation.payloadId,
45
+ dispatchEventId: h.commits[0].eventId,
46
+ dispatchPayloadId: h.commits[0].payloadId,
47
+ responseEventId: h.commits[1].eventId,
48
+ responsePayloadId: h.commits[1].payloadId,
49
+ receiptPayloadId: retained.receiptPayloadId,
50
+ receiptBinding: h.receipt.binding,
51
+ };
52
+ expect(retained).toEqual({
53
+ status: "retained",
54
+ ...h.queryArgs,
55
+ ...expectedKnown,
56
+ });
57
+ const before = await h.physical();
58
+ expect(Object.values(before).map((rows) => rows.length)).toEqual([
59
+ 1, 1, 2, 4,
60
+ ]);
61
+ expect(before.operations[0].knownAcknowledgement).toEqual(expectedKnown);
62
+ expect(before.operations[0].revision).toBe(1);
63
+ expect(before.attempts[0].lastSequence).toBe(2);
64
+ const payload = before.payloads.find(
65
+ (row) => row._id === retained.receiptPayloadId,
66
+ );
67
+ assert(payload);
68
+ expect(payload.codecVersion).toBe(1);
69
+ expect(payload.owner).toEqual({ kind: "receipt", ...h.ownerScope });
70
+ expect(payload.json).toBe(h.receiptJSON);
71
+ expect(JSON.parse(payload.json)).toEqual(h.receipt);
72
+ for (const row of h.before.payloads)
73
+ expect(
74
+ before.payloads.find((candidate) => candidate._id === row._id),
75
+ ).toEqual(row);
76
+ expect(before.attempts).toEqual(h.before.attempts);
77
+ expect(before.events).toEqual(h.before.events);
78
+ expect(
79
+ Object.fromEntries(
80
+ Object.entries(before.operations[0]).filter(
81
+ ([key]) => key !== "knownAcknowledgement",
82
+ ),
83
+ ),
84
+ ).toEqual(h.before.operations[0]);
85
+ for (const rows of Object.values(before))
86
+ for (const row of rows)
87
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
88
+ const args = { operationId: retained.operationId };
89
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
90
+ const expected = { retention: retained, receiptJSON: h.receiptJSON };
91
+ expect(getConvexSize(expected)).toBeLessThanOrEqual(VALUE_LIMIT);
92
+ expect(h.fetchCalls()).toBe(0);
93
+ let received = false;
94
+ let returnedNull = false;
95
+ let valueBytes = -1;
96
+ let targetError: unknown;
97
+ try {
98
+ const actual = await h.t.query(readAcknowledgement, args);
99
+ received = true;
100
+ returnedNull = actual === null;
101
+ valueBytes = getConvexSize(actual);
102
+ expect(valueBytes).toBeLessThanOrEqual(VALUE_LIMIT);
103
+ // The intended null-vs-complete-known-result RED occurs only HERE.
104
+ expect(actual).not.toBeNull();
105
+ expect(actual).toEqual(expected);
106
+ } catch (error) {
107
+ targetError = error;
108
+ throw error;
109
+ } finally {
110
+ try {
111
+ const after = await h.physical();
112
+ const physicalEqual = JSON.stringify(after) === JSON.stringify(before);
113
+ console.info(
114
+ "READACK01_SETTLEMENT",
115
+ JSON.stringify({
116
+ received,
117
+ returnedNull,
118
+ valueBytes,
119
+ physicalEqual,
120
+ counts: Object.values(after).map((rows) => rows.length),
121
+ lastSequence: after.attempts[0]?.lastSequence,
122
+ fetchCalls: h.fetchCalls(),
123
+ }),
124
+ );
125
+ expect(physicalEqual).toBe(true);
126
+ expect(h.fetchCalls()).toBe(0);
127
+ } catch (inspectionError) {
128
+ if (targetError !== undefined) throw targetError;
129
+ throw inspectionError;
130
+ }
131
+ }
132
+ });
133
+
134
+ const create = makeFunctionReference<"mutation">(
135
+ "renewalOperations/mutations:createAuthorizedOperation",
136
+ );
137
+ const reserve = makeFunctionReference<"mutation">(
138
+ "renewalOperations/mutations:reserveAttempt",
139
+ );
140
+ type ReaderFixture = Awaited<ReturnType<typeof prepareAcknowledgementScenario>>;
141
+ async function unchanged(h: ReaderFixture, target: () => Promise<void>) {
142
+ const before = await h.physical();
143
+ expect(getConvexSize(before)).toBeLessThanOrEqual(VALUE_LIMIT);
144
+ for (const rows of Object.values(before))
145
+ for (const row of rows)
146
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
147
+ expect(h.fetchCalls()).toBe(0);
148
+ let failed = false;
149
+ let originalError: unknown;
150
+ try {
151
+ await target();
152
+ } catch (error) {
153
+ failed = true;
154
+ originalError = error;
155
+ throw error;
156
+ } finally {
157
+ try {
158
+ const after = await h.physical();
159
+ expect(after).toEqual(before);
160
+ expect(getConvexSize(after)).toBeLessThanOrEqual(VALUE_LIMIT);
161
+ for (const rows of Object.values(after))
162
+ for (const row of rows)
163
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
164
+ expect(h.fetchCalls()).toBe(0);
165
+ } catch (inspectionError) {
166
+ if (failed) throw originalError;
167
+ throw inspectionError;
168
+ }
169
+ }
170
+ }
171
+ async function cleanKnown() {
172
+ const h = await prepareAcknowledgementScenario("clean");
173
+ expect(getConvexSize(h.args)).toBeLessThanOrEqual(VALUE_LIMIT);
174
+ const retained = await h.t.mutation(retain, h.args);
175
+ assert(retained);
176
+ const known: Infer<typeof knownAcknowledgement> = {
177
+ schemaVersion: 1,
178
+ stage: "attach",
179
+ attemptId: h.acquired.reservation.attemptId,
180
+ sourcePayloadId: h.acquired.reservation.payloadId,
181
+ dispatchEventId: h.commits[0].eventId,
182
+ dispatchPayloadId: h.commits[0].payloadId,
183
+ responseEventId: h.commits[1].eventId,
184
+ responsePayloadId: h.commits[1].payloadId,
185
+ receiptPayloadId: retained.receiptPayloadId,
186
+ receiptBinding: h.receipt.binding,
187
+ };
188
+ expect(retained).toEqual({ status: "retained", ...h.queryArgs, ...known });
189
+ expect(getConvexSize(retained)).toBeLessThanOrEqual(VALUE_LIMIT);
190
+ const rows = await h.physical();
191
+ expect(rows.operations).toHaveLength(1);
192
+ expect(rows.attempts).toHaveLength(1);
193
+ expect(rows.events).toHaveLength(2);
194
+ expect(rows.payloads).toHaveLength(4);
195
+ expect(rows.operations[0].knownAcknowledgement).toEqual(known);
196
+ const payload = rows.payloads.find(
197
+ (row) => row._id === retained.receiptPayloadId,
198
+ );
199
+ assert(payload);
200
+ expect(payload.json).toBe(h.receiptJSON);
201
+ expect(JSON.parse(payload.json)).toEqual(h.receipt);
202
+ expect(payload.owner).toEqual({ kind: "receipt", ...h.ownerScope });
203
+ return { ...h, retained, known };
204
+ }
205
+ async function createPreparedB(h: ReaderFixture) {
206
+ const parsed: unknown = JSON.parse(h.acquired.reservation.recordJSON);
207
+ assert(
208
+ parsed !== null && typeof parsed === "object" && !Array.isArray(parsed),
209
+ );
210
+ // New host operation DATA only; original source/dispatch/body stay unchanged.
211
+ const recordJSON = canonical({
212
+ ...parsed,
213
+ logicalOperationId: "read-ack-operation-B",
214
+ });
215
+ const args = {
216
+ authorizationScope: h.acquired.reservation.authorizationScope,
217
+ authorizationRevision: h.acquired.reservation.authorizationRevision,
218
+ logicalOperationId: "read-ack-operation-B",
219
+ recordJSON,
220
+ };
221
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
222
+ const created = await h.t.mutation(create, args);
223
+ expect(created.status).toBe("created");
224
+ expect(created.operationId).not.toBe(h.queryArgs.operationId);
225
+ expect(getConvexSize(created)).toBeLessThanOrEqual(VALUE_LIMIT);
226
+ const row = await h.t.run((ctx) =>
227
+ ctx.db.get("renewalOperations", created.operationId),
228
+ );
229
+ assert(row);
230
+ expect(row.state).toBe("PREPARED");
231
+ expect(row.revision).toBe(0);
232
+ expect(row.activeAttemptId).toBeUndefined();
233
+ expect(row.knownAcknowledgement).toBeUndefined();
234
+ const payload = await h.t.run((ctx) =>
235
+ ctx.db.get("renewalOperationPayloads", row.payloadId),
236
+ );
237
+ assert(payload);
238
+ expect(payload.codecVersion).toBe(1);
239
+ expect(payload.json).toBe(recordJSON);
240
+ expect(payload.owner).toEqual({
241
+ kind: "operation",
242
+ operationId: created.operationId,
243
+ });
244
+ return { operationId: created.operationId, recordJSON };
245
+ }
246
+
247
+ it.each(["missing", "prepared", "reserved"] as const)(
248
+ "READACK-null %s actual operation has no known acknowledgement",
249
+ async (mode) => {
250
+ const h = await cleanKnown();
251
+ const b = await createPreparedB(h);
252
+ if (mode === "missing") {
253
+ await h.t.run((ctx) => ctx.db.delete("renewalOperations", b.operationId));
254
+ expect(
255
+ await h.t.run((ctx) => ctx.db.get("renewalOperations", b.operationId)),
256
+ ).toBeNull();
257
+ } else if (mode === "reserved") {
258
+ const args = {
259
+ operationId: b.operationId,
260
+ expectedRevision: 0,
261
+ stage: "attach",
262
+ expectedRequestBinding: h.acquired.reservation.requestBinding,
263
+ recordJSON: b.recordJSON,
264
+ };
265
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
266
+ const acquired = await h.t.mutation(reserve, args);
267
+ expect(acquired.disposition).toBe("acquired");
268
+ expect(acquired.reservation.operationId).toBe(b.operationId);
269
+ expect(acquired.reservation.attemptId).not.toBe(
270
+ h.acquired.reservation.attemptId,
271
+ );
272
+ expect(getConvexSize(acquired)).toBeLessThanOrEqual(VALUE_LIMIT);
273
+ const stored = await h.physical();
274
+ const operation = stored.operations.find(
275
+ (row) => row._id === b.operationId,
276
+ );
277
+ const attempt = stored.attempts.find(
278
+ (row) => row._id === acquired.reservation.attemptId,
279
+ );
280
+ assert(operation && attempt);
281
+ expect(operation.state).toBe("RESERVED");
282
+ expect(operation.knownAcknowledgement).toBeUndefined();
283
+ expect(attempt.operationId).toBe(b.operationId);
284
+ expect(attempt.lastSequence).toBe(0);
285
+ }
286
+ const args = { operationId: b.operationId };
287
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
288
+ await unchanged(h, async () => {
289
+ const result = await h.t.query(readAcknowledgement, args);
290
+ expect(result).toBeNull();
291
+ expect(getConvexSize(result)).toBeLessThanOrEqual(VALUE_LIMIT);
292
+ });
293
+ },
294
+ );
295
+
296
+ it("READACK-foreign PREPARED B with actual known A reference conflicts before null", async () => {
297
+ const h = await cleanKnown();
298
+ const b = await createPreparedB(h);
299
+ await h.t.run((ctx) =>
300
+ ctx.db.patch("renewalOperations", b.operationId, {
301
+ knownAcknowledgement: h.known,
302
+ }),
303
+ );
304
+ const rows = await h.physical();
305
+ const operationB = rows.operations.find((row) => row._id === b.operationId);
306
+ assert(operationB);
307
+ expect(operationB.state).toBe("PREPARED");
308
+ expect(operationB.activeAttemptId).toBeUndefined();
309
+ expect(rows.attempts.some((row) => row.operationId === b.operationId)).toBe(
310
+ false,
311
+ );
312
+ expect(operationB.knownAcknowledgement).toEqual(h.known);
313
+ const args = { operationId: b.operationId };
314
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
315
+ await unchanged(h, async () => {
316
+ await expect(h.t.query(readAcknowledgement, args)).rejects.toThrow(
317
+ /^RENEWAL_JOURNAL_CONFLICT$/,
318
+ );
319
+ });
320
+ });
321
+
322
+ const readerCorruptions = [
323
+ "receipt-owner",
324
+ "response-reference",
325
+ "chain-head",
326
+ "event-hash",
327
+ "receipt-json",
328
+ "missing-receipt",
329
+ ] as const;
330
+ it.each(readerCorruptions)(
331
+ "READACK-corrupt %s refuses without repair",
332
+ async (mode) => {
333
+ const h = await cleanKnown();
334
+ if (mode === "receipt-owner") {
335
+ await h.t.run((ctx) =>
336
+ ctx.db.patch("renewalOperationPayloads", h.retained.receiptPayloadId, {
337
+ owner: { kind: "operation", operationId: h.queryArgs.operationId },
338
+ }),
339
+ );
340
+ } else if (mode === "response-reference") {
341
+ expect(h.commits[0].payloadId).not.toBe(h.commits[1].payloadId);
342
+ await h.t.run((ctx) =>
343
+ ctx.db.patch("renewalOperations", h.queryArgs.operationId, {
344
+ knownAcknowledgement: {
345
+ ...h.known,
346
+ responsePayloadId: h.commits[0].payloadId,
347
+ },
348
+ }),
349
+ );
350
+ } else if (mode === "chain-head") {
351
+ await h.t.run((ctx) =>
352
+ ctx.db.patch(
353
+ "renewalOperationAttempts",
354
+ h.acquired.reservation.attemptId,
355
+ { lastSequence: 1 },
356
+ ),
357
+ );
358
+ } else if (mode === "event-hash") {
359
+ expect(h.commits[0].binding).not.toBe(h.commits[1].binding);
360
+ await h.t.run((ctx) =>
361
+ ctx.db.patch("renewalOperationEvents", h.commits[1].eventId, {
362
+ binding: h.commits[0].binding,
363
+ }),
364
+ );
365
+ } else if (mode === "receipt-json") {
366
+ const changedJSON = canonical({
367
+ ...h.receipt,
368
+ providerSnapshot: {
369
+ ...h.receipt.providerSnapshot,
370
+ reader_drift: { preserve: "conflict" },
371
+ },
372
+ });
373
+ expect(changedJSON).not.toBe(h.receiptJSON);
374
+ expect(JSON.parse(changedJSON).binding).toBe(h.receipt.binding);
375
+ await h.t.run((ctx) =>
376
+ ctx.db.patch("renewalOperationPayloads", h.retained.receiptPayloadId, {
377
+ json: changedJSON,
378
+ }),
379
+ );
380
+ } else {
381
+ await h.t.run((ctx) =>
382
+ ctx.db.delete("renewalOperationPayloads", h.retained.receiptPayloadId),
383
+ );
384
+ }
385
+ expect(getConvexSize(h.queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
386
+ await unchanged(h, async () => {
387
+ await expect(h.t.query(readAcknowledgement, h.queryArgs)).rejects.toThrow(
388
+ /^RENEWAL_JOURNAL_CONFLICT$/,
389
+ );
390
+ });
391
+ },
392
+ );
393
+
394
+ it("READACK-after3 getter preserves the exact full receipt and original descriptor", async () => {
395
+ const h = await cleanKnown();
396
+ expect(getConvexSize(h.queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
397
+ const expected = { retention: h.retained, receiptJSON: h.receiptJSON };
398
+ expect(getConvexSize(expected)).toBeLessThanOrEqual(VALUE_LIMIT);
399
+ await unchanged(h, async () => {
400
+ const actual = await h.t.query(readAcknowledgement, h.queryArgs);
401
+ expect(actual).toEqual(expected);
402
+ expect(getConvexSize(actual)).toBeLessThanOrEqual(VALUE_LIMIT);
403
+ });
404
+ const captured = captureAttachReceipt(h.receiptEvent);
405
+ expect(materialize(captured)).toEqual(h.receiptEvent);
406
+ const event = await canonicalJournalEvent(3, canonical(h.receiptEvent));
407
+ const args: ObjectType<typeof appendArgs> = {
408
+ reservation: h.acquired.reservation,
409
+ expectedSequence: 2,
410
+ event: {
411
+ sequence: 3,
412
+ binding: event.binding,
413
+ dataJSON: event.json,
414
+ },
415
+ };
416
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
417
+ const committed = await h.t.mutation(append, args);
418
+ expect(committed.sequence).toBe(3);
419
+ expect(committed.binding).toBe(event.binding);
420
+ expect(committed.reservation).toEqual(h.acquired.reservation);
421
+ expect(getConvexSize(committed)).toBeLessThanOrEqual(VALUE_LIMIT);
422
+ const rows = await h.physical();
423
+ expect(rows.events).toHaveLength(3);
424
+ expect(rows.attempts[0].lastSequence).toBe(3);
425
+ expect(rows.operations[0].knownAcknowledgement).toEqual(h.known);
426
+ await unchanged(h, async () => {
427
+ const actual = await h.t.query(readAcknowledgement, h.queryArgs);
428
+ expect(actual).toEqual(expected);
429
+ expect(getConvexSize(actual)).toBeLessThanOrEqual(VALUE_LIMIT);
430
+ });
431
+ });
432
+
433
+ it("READACK-closed args reject an extra actual receipt payload selector", async () => {
434
+ const h = await cleanKnown();
435
+ const args = { ...h.queryArgs, payloadId: h.retained.receiptPayloadId };
436
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
437
+ await unchanged(h, async () => {
438
+ await expect(h.t.query(readAcknowledgement, args)).rejects.toThrow(
439
+ /^Validator error:/,
440
+ );
441
+ });
442
+ });