@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,493 @@
1
+ import assert from "node:assert/strict";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { getConvexSize, getDocumentSize, type Infer } from "convex/values";
4
+ import { afterEach, expect, it, vi } from "vitest";
5
+ import type {
6
+ providerAcknowledgementReadResult,
7
+ retentionReceipt,
8
+ } from "./schema.js";
9
+ import {
10
+ prepareV2AcknowledgementScenario,
11
+ append,
12
+ retain,
13
+ canonical,
14
+ VALUE_LIMIT,
15
+ } from "./acknowledgement.fixtures.js";
16
+
17
+ const readAcknowledgement = makeFunctionReference<
18
+ "query",
19
+ { operationId: Infer<typeof retentionReceipt>["operationId"] },
20
+ Infer<typeof providerAcknowledgementReadResult>
21
+ >("renewalOperations/queries:readProviderAcknowledgement");
22
+ afterEach(() => vi.unstubAllGlobals());
23
+
24
+ it("ACKV201 retains full V2 attach DATA after actual basis and events1/2", async () => {
25
+ // Independent predeclared DATA expectations; no sampled runtime eligibility.
26
+ const policy = {
27
+ version: 1,
28
+ firstDispatchedAt: 1700000001,
29
+ periodStart: 1700000000,
30
+ periodEnd: 1702592000,
31
+ workEndWallMs: 1700000005000,
32
+ endWallMs: 1700000006000,
33
+ };
34
+ const basis = {
35
+ startWallMs: 1700000001000,
36
+ startNsDecimal: "1000000000",
37
+ workEndWallMs: 1700000005000,
38
+ workEndNsDecimal: "5000000000",
39
+ endWallMs: 1700000006000,
40
+ endNsDecimal: "6000000000",
41
+ };
42
+ const h = await prepareV2AcknowledgementScenario();
43
+ expect(h.scenario).toBe("clean");
44
+ expect(h.shouldRetain).toBe(true);
45
+ const operation = h.before.operations[0];
46
+ const initialBasis = {
47
+ version: 1,
48
+ attemptId: h.acquired.reservation.attemptId,
49
+ sourcePayloadId: h.acquired.reservation.payloadId,
50
+ stage: "attach",
51
+ policyJSON: canonical(policy),
52
+ basisJSON: canonical(basis),
53
+ };
54
+ expect(operation.recordVersion).toBe(2);
55
+ expect(operation.initialBasis).toEqual(initialBasis);
56
+ const source = h.before.payloads.find(
57
+ (row) => row._id === h.acquired.reservation.payloadId,
58
+ );
59
+ assert(source);
60
+ const record = JSON.parse(source.json);
61
+ expect(record.schemaVersion).toBe(2);
62
+ expect(record.timingPolicy).toEqual(policy);
63
+ expect(Object.prototype.hasOwnProperty.call(record, "basis")).toBe(false);
64
+ expect(record.attachRequest).toEqual(h.raw.attachRequest);
65
+ // Whole source equality includes its opaque bundle, without a layout gate.
66
+ expect(source.json).toBe(h.acquired.reservation.recordJSON);
67
+ expect(h.commits.map((event) => event.sequence)).toEqual([1, 2]);
68
+ expect(h.before.attempts[0].lastSequence).toBe(2);
69
+ expect(h.before.events.map((event) => event.sequence)).toEqual([1, 2]);
70
+ expect(Object.values(h.before).map((rows) => rows.length)).toEqual([
71
+ 1, 1, 2, 3,
72
+ ]);
73
+ expect(operation.knownAcknowledgement).toBeUndefined();
74
+ expect(getConvexSize(h.args)).toBeLessThanOrEqual(VALUE_LIMIT);
75
+ let stable = h.before;
76
+ let targetFailed = false;
77
+ try {
78
+ // Real retention route after every owned/basis/history prerequisite above.
79
+ const retained = await h.t.mutation(retain, h.args);
80
+ assert(retained !== null);
81
+ const known = {
82
+ schemaVersion: 1,
83
+ stage: "attach",
84
+ attemptId: h.acquired.reservation.attemptId,
85
+ sourcePayloadId: h.acquired.reservation.payloadId,
86
+ dispatchEventId: h.commits[0].eventId,
87
+ dispatchPayloadId: h.commits[0].payloadId,
88
+ responseEventId: h.commits[1].eventId,
89
+ responsePayloadId: h.commits[1].payloadId,
90
+ receiptPayloadId: retained.receiptPayloadId,
91
+ receiptBinding: h.receipt.binding,
92
+ };
93
+ expect(retained).toEqual({
94
+ status: "retained",
95
+ ...h.queryArgs,
96
+ ...known,
97
+ });
98
+ expect(getConvexSize(retained)).toBeLessThanOrEqual(VALUE_LIMIT);
99
+ const after = await h.physical();
100
+ stable = after;
101
+ expect(Object.values(after).map((rows) => rows.length)).toEqual([
102
+ 1, 1, 2, 4,
103
+ ]);
104
+ expect(after.operations).toEqual([
105
+ { ...operation, knownAcknowledgement: known },
106
+ ]);
107
+ expect(after.operations[0].initialBasis).toEqual(initialBasis);
108
+ expect(after.attempts).toEqual(h.before.attempts);
109
+ expect(after.events).toEqual(h.before.events);
110
+ for (const original of h.before.payloads)
111
+ expect(after.payloads.find((row) => row._id === original._id)).toEqual(
112
+ original,
113
+ );
114
+ const added = after.payloads.filter(
115
+ (row) => !h.before.payloads.some((original) => original._id === row._id),
116
+ );
117
+ expect(added).toHaveLength(1);
118
+ const receiptPayload = added[0];
119
+ expect(Number.isFinite(receiptPayload._creationTime)).toBe(true);
120
+ expect(receiptPayload).toEqual({
121
+ _id: retained.receiptPayloadId,
122
+ _creationTime: receiptPayload._creationTime,
123
+ codecVersion: 1,
124
+ json: h.receiptJSON,
125
+ owner: { kind: "receipt", ...h.ownerScope },
126
+ });
127
+ expect(JSON.parse(receiptPayload.json)).toEqual(h.receipt);
128
+ expect(after.events.some((event) => event.sequence === 3)).toBe(false);
129
+ for (const rows of Object.values(after))
130
+ for (const row of rows)
131
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
132
+ const queryArgs = { operationId: retained.operationId };
133
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
134
+ const loaded = await h.t.query(readAcknowledgement, queryArgs);
135
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
136
+ expect(loaded).toEqual({ retention: retained, receiptJSON: h.receiptJSON });
137
+ expect(await h.physical()).toEqual(after);
138
+ } catch (error) {
139
+ targetFailed = true;
140
+ throw error;
141
+ } finally {
142
+ try {
143
+ const after = await h.physical();
144
+ console.info(
145
+ "ACKV201_SETTLEMENT",
146
+ JSON.stringify({
147
+ physicalEqual: JSON.stringify(after) === JSON.stringify(stable),
148
+ counts: Object.values(after).map((rows) => rows.length),
149
+ lastSequence: after.attempts[0]?.lastSequence,
150
+ fetchCalls: h.fetchCalls(),
151
+ }),
152
+ );
153
+ expect(after).toEqual(stable);
154
+ expect(h.fetchCalls()).toBe(0);
155
+ } catch (inspectionError) {
156
+ if (!targetFailed) throw inspectionError;
157
+ console.info("ACKV201_READONLY_INSPECTION_FAILED");
158
+ }
159
+ }
160
+ });
161
+
162
+ const bind = makeFunctionReference<"mutation">(
163
+ "renewalOperations/mutations:bindInitialBasis",
164
+ );
165
+ const load = makeFunctionReference<"query">(
166
+ "renewalOperations/queries:loadAuthorizedOperation",
167
+ );
168
+ const manifest = makeFunctionReference<"query">(
169
+ "renewalOperations/queries:readAttempt",
170
+ );
171
+ const readEvent = makeFunctionReference<"query">(
172
+ "renewalOperations/queries:readStageEvent",
173
+ );
174
+ const reserve = makeFunctionReference<"mutation">(
175
+ "renewalOperations/mutations:reserveAttempt",
176
+ );
177
+ const create = makeFunctionReference<"mutation">(
178
+ "renewalOperations/mutations:createAuthorizedOperation",
179
+ );
180
+ const contradictoryValue = "different-retained-full-body";
181
+ const corruptions = [
182
+ "known-ref",
183
+ "receipt-owner",
184
+ "receipt-body",
185
+ "missing-payload",
186
+ ] as const;
187
+
188
+ async function retainedScenario() {
189
+ const h = await prepareV2AcknowledgementScenario();
190
+ expect(h.scenario).toBe("clean");
191
+ expect(h.shouldRetain).toBe(true);
192
+ expect(h.before.operations[0].recordVersion).toBe(2);
193
+ const initialBasis = h.before.operations[0].initialBasis;
194
+ assert(initialBasis);
195
+ expect(h.before.attempts[0].lastSequence).toBe(2);
196
+ expect(h.commits.map((row) => row.sequence)).toEqual([1, 2]);
197
+ const retained = await h.t.mutation(retain, h.args);
198
+ assert(retained !== null);
199
+ const known = {
200
+ schemaVersion: 1 as const,
201
+ stage: "attach" as const,
202
+ attemptId: h.acquired.reservation.attemptId,
203
+ sourcePayloadId: h.acquired.reservation.payloadId,
204
+ dispatchEventId: h.commits[0].eventId,
205
+ dispatchPayloadId: h.commits[0].payloadId,
206
+ responseEventId: h.commits[1].eventId,
207
+ responsePayloadId: h.commits[1].payloadId,
208
+ receiptPayloadId: retained.receiptPayloadId,
209
+ receiptBinding: h.receipt.binding,
210
+ };
211
+ expect(retained).toEqual({ status: "retained", ...h.queryArgs, ...known });
212
+ expect(getConvexSize(retained)).toBeLessThanOrEqual(VALUE_LIMIT);
213
+ const after = await h.physical();
214
+ expect(Object.values(after).map((rows) => rows.length)).toEqual([1, 1, 2, 4]);
215
+ expect(after.operations).toEqual([
216
+ { ...h.before.operations[0], knownAcknowledgement: known },
217
+ ]);
218
+ expect(after.attempts).toEqual(h.before.attempts);
219
+ expect(after.events).toEqual(h.before.events);
220
+ for (const old of h.before.payloads)
221
+ expect(after.payloads.find((row) => row._id === old._id)).toEqual(old);
222
+ const payload = after.payloads.find(
223
+ (row) => row._id === retained.receiptPayloadId,
224
+ );
225
+ assert(payload);
226
+ expect(payload).toEqual({
227
+ _id: retained.receiptPayloadId,
228
+ _creationTime: payload._creationTime,
229
+ codecVersion: 1,
230
+ json: h.receiptJSON,
231
+ owner: { kind: "receipt", ...h.ownerScope },
232
+ });
233
+ for (const group of Object.values(after))
234
+ for (const row of group)
235
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
236
+ const expectedRead = { retention: retained, receiptJSON: h.receiptJSON };
237
+ expect(await h.t.query(readAcknowledgement, h.queryArgs)).toEqual(
238
+ expectedRead,
239
+ );
240
+ const { recordJSON, ...summary } = h.acquired.reservation;
241
+ const loaded = await h.t.query(load, h.queryArgs);
242
+ expect(loaded).toEqual({
243
+ recordVersion: 2,
244
+ ...h.queryArgs,
245
+ recordJSON,
246
+ revision: 1,
247
+ activeAttempt: summary,
248
+ initialBasis,
249
+ });
250
+ const bindArgs = {
251
+ ...h.queryArgs,
252
+ attemptId: known.attemptId,
253
+ policyJSON: initialBasis.policyJSON,
254
+ basisJSON: initialBasis.basisJSON,
255
+ };
256
+ const historyArgs = { ...h.queryArgs, attemptId: known.attemptId };
257
+ const reserveArgs = {
258
+ ...h.queryArgs,
259
+ expectedRevision: 0,
260
+ stage: "attach" as const,
261
+ expectedRequestBinding: h.acquired.reservation.requestBinding,
262
+ recordJSON,
263
+ };
264
+ const createArgs = {
265
+ authorizationScope: h.acquired.reservation.authorizationScope,
266
+ logicalOperationId: h.acquired.reservation.logicalOperationId,
267
+ authorizationRevision: h.acquired.reservation.authorizationRevision,
268
+ recordJSON,
269
+ };
270
+ const first = h.before.payloads.find(
271
+ (row) => row._id === known.dispatchPayloadId,
272
+ );
273
+ assert(first);
274
+ const duplicateArgs = {
275
+ reservation: h.acquired.reservation,
276
+ expectedSequence: 0,
277
+ event: {
278
+ sequence: 1 as const,
279
+ binding: h.commits[0].binding,
280
+ dataJSON: first.json,
281
+ },
282
+ };
283
+ for (const args of [
284
+ bindArgs,
285
+ historyArgs,
286
+ reserveArgs,
287
+ createArgs,
288
+ duplicateArgs,
289
+ { ...historyArgs, sequence: 3 },
290
+ ])
291
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
292
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
293
+ expect(getConvexSize(expectedRead)).toBeLessThanOrEqual(VALUE_LIMIT);
294
+ expect(await h.physical()).toEqual(after);
295
+ expect(h.fetchCalls()).toBe(0);
296
+ return {
297
+ ...h,
298
+ retained,
299
+ known,
300
+ after,
301
+ expectedRead,
302
+ loaded,
303
+ bindArgs,
304
+ historyArgs,
305
+ reserveArgs,
306
+ createArgs,
307
+ duplicateArgs,
308
+ };
309
+ }
310
+
311
+ type Retained = Awaited<ReturnType<typeof retainedScenario>>;
312
+ function contradictoryReceipt(h: Retained) {
313
+ const metadata = h.receipt.providerSnapshot.metadata;
314
+ assert(metadata && typeof metadata === "object" && !Array.isArray(metadata));
315
+ return {
316
+ ...h.receipt,
317
+ providerSnapshot: {
318
+ ...h.receipt.providerSnapshot,
319
+ metadata: { ...metadata, opaque: contradictoryValue },
320
+ },
321
+ };
322
+ }
323
+ async function inspectStable(
324
+ h: Retained,
325
+ rows: Retained["after"],
326
+ failed: boolean,
327
+ label: string,
328
+ ) {
329
+ try {
330
+ expect(await h.physical()).toEqual(rows);
331
+ expect(h.fetchCalls()).toBe(0);
332
+ } catch (error) {
333
+ if (!failed) throw error;
334
+ console.info(label);
335
+ }
336
+ }
337
+ async function checkStableKnown(h: Retained) {
338
+ const events = h.commits.map(({ reservation, ...event }) => {
339
+ expect(reservation).toEqual(h.acquired.reservation);
340
+ return event;
341
+ });
342
+ const settled = await Promise.allSettled([
343
+ h.t.mutation(retain, h.args),
344
+ h.t.mutation(bind, h.bindArgs),
345
+ h.t.query(readAcknowledgement, h.queryArgs),
346
+ h.t.query(load, h.queryArgs),
347
+ h.t.query(manifest, h.historyArgs),
348
+ h.t.query(readEvent, { ...h.historyArgs, sequence: 3 }),
349
+ h.t.mutation(reserve, h.reserveArgs),
350
+ h.t.mutation(create, h.createArgs),
351
+ h.t.mutation(append, h.duplicateArgs),
352
+ ]);
353
+ const values = settled.map((item) => {
354
+ if (item.status !== "fulfilled") throw item.reason;
355
+ expect(getConvexSize(item.value)).toBeLessThanOrEqual(VALUE_LIMIT);
356
+ return item.value;
357
+ });
358
+ expect(values).toEqual([
359
+ h.retained,
360
+ { status: "bound", ...h.queryArgs, initialBasis: h.loaded.initialBasis },
361
+ h.expectedRead,
362
+ h.loaded,
363
+ { reservation: h.acquired.reservation, events },
364
+ null,
365
+ {
366
+ disposition: "existing",
367
+ committedRevision: 1,
368
+ reservation: h.acquired.reservation,
369
+ },
370
+ { status: "existing", ...h.queryArgs },
371
+ h.commits[0],
372
+ ]);
373
+ expect(await h.physical()).toEqual(h.after);
374
+ }
375
+
376
+ it("ACKV202 keeps head2 retain, basis and full reads stable before event3", async () => {
377
+ const h = await retainedScenario();
378
+ let targetFailed = false;
379
+ try {
380
+ await checkStableKnown(h);
381
+ } catch (error) {
382
+ targetFailed = true;
383
+ throw error;
384
+ } finally {
385
+ await inspectStable(h, h.after, targetFailed, "ACKV202_INSPECTION_FAILED");
386
+ }
387
+ });
388
+
389
+ it.each(corruptions)(
390
+ "ACKV205 refuses %s known corruption through all eight APIs",
391
+ async (kind) => {
392
+ const h = await retainedScenario();
393
+ await checkStableKnown(h);
394
+ let expected = h.after;
395
+ if (kind === "known-ref") {
396
+ const knownAcknowledgement = {
397
+ ...h.known,
398
+ dispatchEventId: h.known.responseEventId,
399
+ };
400
+ await h.t.run((ctx) =>
401
+ ctx.db.patch("renewalOperations", h.retained.operationId, {
402
+ knownAcknowledgement,
403
+ }),
404
+ );
405
+ expected = {
406
+ ...expected,
407
+ operations: [{ ...expected.operations[0], knownAcknowledgement }],
408
+ };
409
+ } else if (kind === "receipt-owner") {
410
+ const owner = {
411
+ kind: "receipt" as const,
412
+ ...h.ownerScope,
413
+ stage: "configure" as const,
414
+ };
415
+ await h.t.run((ctx) =>
416
+ ctx.db.patch("renewalOperationPayloads", h.retained.receiptPayloadId, {
417
+ owner,
418
+ }),
419
+ );
420
+ expected = {
421
+ ...expected,
422
+ payloads: expected.payloads.map((row) =>
423
+ row._id === h.retained.receiptPayloadId ? { ...row, owner } : row,
424
+ ),
425
+ };
426
+ } else if (kind === "receipt-body") {
427
+ const json = canonical(contradictoryReceipt(h));
428
+ expect(json).not.toBe(h.receiptJSON);
429
+ await h.t.run((ctx) =>
430
+ ctx.db.patch("renewalOperationPayloads", h.retained.receiptPayloadId, {
431
+ json,
432
+ }),
433
+ );
434
+ expected = {
435
+ ...expected,
436
+ payloads: expected.payloads.map((row) =>
437
+ row._id === h.retained.receiptPayloadId ? { ...row, json } : row,
438
+ ),
439
+ };
440
+ } else if (kind === "missing-payload") {
441
+ await h.t.run((ctx) =>
442
+ ctx.db.delete("renewalOperationPayloads", h.retained.receiptPayloadId),
443
+ );
444
+ expected = {
445
+ ...expected,
446
+ payloads: expected.payloads.filter(
447
+ (row) => row._id !== h.retained.receiptPayloadId,
448
+ ),
449
+ };
450
+ }
451
+ const damaged = await h.physical();
452
+ expect(damaged).toEqual(expected);
453
+ for (const group of Object.values(damaged)) {
454
+ for (const row of group)
455
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
456
+ }
457
+ let targetFailed = false;
458
+ try {
459
+ const settled = await Promise.allSettled([
460
+ h.t.query(load, h.queryArgs),
461
+ h.t.query(manifest, h.historyArgs),
462
+ h.t.query(readEvent, { ...h.historyArgs, sequence: 3 }),
463
+ h.t.query(readAcknowledgement, h.queryArgs),
464
+ h.t.mutation(reserve, h.reserveArgs),
465
+ h.t.mutation(bind, h.bindArgs),
466
+ h.t.mutation(create, h.createArgs),
467
+ h.t.mutation(append, h.duplicateArgs),
468
+ ]);
469
+ console.info(
470
+ "ACKV205_SETTLEMENT",
471
+ JSON.stringify({ kind, statuses: settled.map((item) => item.status) }),
472
+ );
473
+ expect(settled).toEqual(
474
+ Array.from({ length: 8 }, () => ({
475
+ status: "rejected",
476
+ reason: expect.objectContaining({
477
+ message: "RENEWAL_JOURNAL_CONFLICT",
478
+ }),
479
+ })),
480
+ );
481
+ } catch (error) {
482
+ targetFailed = true;
483
+ throw error;
484
+ } finally {
485
+ await inspectStable(
486
+ h,
487
+ damaged,
488
+ targetFailed,
489
+ "ACKV205_INSPECTION_FAILED",
490
+ );
491
+ }
492
+ },
493
+ );