@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,523 @@
1
+ import { getConvexSize, getDocumentSize } from "convex/values";
2
+ import { afterEach, expect, it, vi } from "vitest";
3
+ import {
4
+ canonicalJournalRecord,
5
+ journalReservationMaterial,
6
+ } from "../../shared/renewalOperationJournal.js";
7
+ import { prepareRenewalPriceReplacementV2 } from "../../client/billing/renewalV2/index.js";
8
+ import {
9
+ canonical,
10
+ codecFixture,
11
+ componentFixture,
12
+ create,
13
+ load,
14
+ VALUE_LIMIT,
15
+ expectInvalidCreate,
16
+ } from "./timingPolicy.fixtures.js";
17
+
18
+ afterEach(() => vi.unstubAllGlobals());
19
+
20
+ it("CODEC01 preserves complete attach policy DATA without a pre-CAS live basis", async () => {
21
+ await codecFixture();
22
+ });
23
+
24
+ it("CODEC00 keeps the exact legacy v1 normalized return shape", () => {
25
+ const legacy = {
26
+ schemaVersion: 1,
27
+ authorization: { scope: "legacy-host", id: "legacy-consent", revision: 1 },
28
+ logicalOperationId: "legacy-operation",
29
+ requestBinding: "legacy-immutable-DATA",
30
+ originalBundle: { unknown: [null, "retained"] },
31
+ basis: { retainedLegacyDATA: true },
32
+ };
33
+ const json = canonical(legacy);
34
+ expect(canonicalJournalRecord(json)).toEqual({
35
+ json,
36
+ authorizationScope: "legacy-host",
37
+ authorizationId: "legacy-consent",
38
+ authorizationRevision: 1,
39
+ logicalOperationId: "legacy-operation",
40
+ requestBinding: "legacy-immutable-DATA",
41
+ });
42
+ });
43
+
44
+ // Deliberately invalid host DATA, not a claim that malformed raw source is C1.
45
+ // This closed set of changes is declared before any fixture constructor runs.
46
+ const invalidPolicies = [
47
+ "configure-stage",
48
+ "legacy-null-basis",
49
+ "missing-policy",
50
+ "null-policy",
51
+ "unknown-policy-field",
52
+ "missing-policy-field",
53
+ "wrong-policy-version",
54
+ "unsafe-first-time",
55
+ "negative-period-start",
56
+ "fractional-wall-end",
57
+ "non-number-wall-end",
58
+ "equal-period-end",
59
+ "wall-end-before-work",
60
+ "dispatch-time-mismatch",
61
+ "source-time-mismatch",
62
+ "expected-period-mismatch",
63
+ ] as const;
64
+
65
+ it.each(invalidPolicies)(
66
+ "CODEC02 refuses intrinsic %s DATA",
67
+ async (scenario) => {
68
+ const h = await codecFixture();
69
+ const invalid: Record<string, unknown> = JSON.parse(h.recordJSON);
70
+ const policy: Record<string, unknown> = { ...h.record.timingPolicy };
71
+ invalid.timingPolicy = policy;
72
+ switch (scenario) {
73
+ case "configure-stage":
74
+ invalid.stage = "configure";
75
+ break;
76
+ case "legacy-null-basis":
77
+ invalid.basis = null;
78
+ break;
79
+ case "missing-policy":
80
+ delete invalid.timingPolicy;
81
+ break;
82
+ case "null-policy":
83
+ invalid.timingPolicy = null;
84
+ break;
85
+ case "unknown-policy-field":
86
+ policy.extra = null;
87
+ break;
88
+ case "missing-policy-field":
89
+ delete policy.periodStart;
90
+ break;
91
+ case "wrong-policy-version":
92
+ policy.version = 2;
93
+ break;
94
+ case "unsafe-first-time":
95
+ policy.firstDispatchedAt = 9007199254740992;
96
+ invalid.dispatch = {
97
+ ...h.record.dispatch,
98
+ firstDispatchedAt: 9007199254740992,
99
+ };
100
+ invalid.attachRequest = {
101
+ ...h.record.attachRequest,
102
+ firstDispatchedAt: 9007199254740992,
103
+ };
104
+ break;
105
+ case "negative-period-start":
106
+ policy.periodStart = -1;
107
+ invalid.attachRequest = {
108
+ ...h.record.attachRequest,
109
+ preparation: {
110
+ ...h.record.attachRequest.preparation,
111
+ expected: {
112
+ ...h.record.attachRequest.preparation.expected,
113
+ periodStart: -1,
114
+ },
115
+ },
116
+ };
117
+ break;
118
+ case "fractional-wall-end":
119
+ policy.endWallMs = 1700000006000.5;
120
+ break;
121
+ case "non-number-wall-end":
122
+ policy.endWallMs = "1700000006000";
123
+ break;
124
+ case "equal-period-end":
125
+ policy.periodEnd = policy.periodStart;
126
+ invalid.attachRequest = {
127
+ ...h.record.attachRequest,
128
+ preparation: {
129
+ ...h.record.attachRequest.preparation,
130
+ expected: {
131
+ ...h.record.attachRequest.preparation.expected,
132
+ periodEnd: h.record.timingPolicy.periodStart,
133
+ },
134
+ },
135
+ };
136
+ break;
137
+ case "wall-end-before-work":
138
+ policy.endWallMs = h.record.timingPolicy.workEndWallMs - 1;
139
+ break;
140
+ case "dispatch-time-mismatch":
141
+ invalid.dispatch = {
142
+ ...h.record.dispatch,
143
+ firstDispatchedAt: 1700000002,
144
+ };
145
+ break;
146
+ case "source-time-mismatch":
147
+ invalid.attachRequest = {
148
+ ...h.record.attachRequest,
149
+ firstDispatchedAt: 1700000002,
150
+ };
151
+ break;
152
+ case "expected-period-mismatch":
153
+ invalid.attachRequest = {
154
+ ...h.record.attachRequest,
155
+ preparation: {
156
+ ...h.record.attachRequest.preparation,
157
+ expected: {
158
+ ...h.record.attachRequest.preparation.expected,
159
+ periodEnd: 1702592001,
160
+ },
161
+ },
162
+ };
163
+ break;
164
+ }
165
+ try {
166
+ const json = canonical(invalid);
167
+ expect(json.length).toBeLessThanOrEqual(500000);
168
+ expect(new TextEncoder().encode(json).length).toBeLessThanOrEqual(500000);
169
+ expect(() => canonicalJournalRecord(json)).toThrow(
170
+ "RENEWAL_JOURNAL_JSON_STOP",
171
+ );
172
+ expect(canonical(h.record)).toBe(h.recordJSON);
173
+ } finally {
174
+ expect(h.fetchCalls()).toBe(0);
175
+ }
176
+ },
177
+ );
178
+
179
+ it("MAT01 returns policy-only V2 reservation DATA without basisJSON", async () => {
180
+ const h = await codecFixture();
181
+ try {
182
+ const full = canonicalJournalRecord(h.recordJSON);
183
+ expect(full).toEqual(h.normalized);
184
+ const material = journalReservationMaterial(full);
185
+ expect(material).toEqual({
186
+ recordVersion: 2,
187
+ stage: "attach",
188
+ key: h.request.key,
189
+ apiVersion: h.request.apiVersion,
190
+ firstDispatchedAt: h.request.firstDispatchedAt,
191
+ accountJSON: canonical(h.record.account),
192
+ timingPolicyJSON: canonical(h.record.timingPolicy),
193
+ });
194
+ expect(Object.prototype.hasOwnProperty.call(material, "basisJSON")).toBe(
195
+ false,
196
+ );
197
+ expect(canonical(h.record)).toBe(h.recordJSON);
198
+ } finally {
199
+ expect(h.fetchCalls()).toBe(0);
200
+ }
201
+ });
202
+
203
+ it("MAT00 keeps the exact legacy v1 material shape", async () => {
204
+ const h = await codecFixture();
205
+ const basis = {
206
+ startWallMs: 1700000001000,
207
+ startNs: "1700000001000000000",
208
+ workEndWallMs: 1700000005000,
209
+ workEndNs: "1700000005000000000",
210
+ endWallMs: 1700000006000,
211
+ endNs: "1700000006000000000",
212
+ };
213
+ const legacy: Record<string, unknown> = {
214
+ ...h.record,
215
+ schemaVersion: 1,
216
+ basis,
217
+ };
218
+ delete legacy.timingPolicy;
219
+ const legacyJSON = canonical(legacy);
220
+ try {
221
+ expect(
222
+ journalReservationMaterial(canonicalJournalRecord(legacyJSON)),
223
+ ).toEqual({
224
+ stage: "attach",
225
+ key: h.request.key,
226
+ apiVersion: h.request.apiVersion,
227
+ firstDispatchedAt: h.request.firstDispatchedAt,
228
+ accountJSON: canonical(h.record.account),
229
+ basisJSON: canonical(basis),
230
+ });
231
+ } finally {
232
+ expect(h.fetchCalls()).toBe(0);
233
+ }
234
+ });
235
+
236
+ const invalidSourcePeriodEnd = 1702592001;
237
+
238
+ it("V2CREATE01 retains V2 owned header and versioned PREPARED load DATA", async () => {
239
+ const h = await codecFixture();
240
+ const { t, physical } = componentFixture();
241
+ const args = {
242
+ authorizationScope: h.record.authorization.scope,
243
+ logicalOperationId: h.record.logicalOperationId,
244
+ authorizationRevision: h.record.authorization.revision,
245
+ recordJSON: h.recordJSON,
246
+ };
247
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
248
+ expect(await physical()).toEqual({
249
+ operations: [],
250
+ attempts: [],
251
+ events: [],
252
+ payloads: [],
253
+ });
254
+ let stable: string | undefined;
255
+ let targetFailed = false;
256
+ try {
257
+ const created = await t.mutation(create, args);
258
+ expect(created.status).toBe("created");
259
+ expect(getConvexSize(created)).toBeLessThanOrEqual(VALUE_LIMIT);
260
+ const stored = await physical();
261
+ stable = JSON.stringify(stored);
262
+ expect(stored.operations).toHaveLength(1);
263
+ expect(stored.payloads).toHaveLength(1);
264
+ expect(stored.attempts).toHaveLength(0);
265
+ expect(stored.events).toHaveLength(0);
266
+ for (const rows of Object.values(stored)) {
267
+ for (const row of rows)
268
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
269
+ }
270
+ const operation = stored.operations[0];
271
+ const payload = stored.payloads[0];
272
+ expect(operation).toMatchObject({
273
+ _id: created.operationId,
274
+ payloadId: payload._id,
275
+ requestBinding: h.record.requestBinding,
276
+ revision: 0,
277
+ state: "PREPARED",
278
+ });
279
+ expect(payload).toMatchObject({
280
+ codecVersion: 1,
281
+ json: h.recordJSON,
282
+ owner: { kind: "operation", operationId: created.operationId },
283
+ });
284
+ // First missing behavior: actual owned operation header must retain V2.
285
+ expect(operation).toMatchObject({ recordVersion: 2 });
286
+ const queryArgs = { operationId: created.operationId };
287
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
288
+ const loaded = await t.query(load, queryArgs);
289
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
290
+ expect(loaded).toEqual({
291
+ recordVersion: 2,
292
+ operationId: created.operationId,
293
+ recordJSON: h.recordJSON,
294
+ revision: 0,
295
+ activeAttempt: null,
296
+ initialBasis: null,
297
+ });
298
+ expect(JSON.parse(loaded.recordJSON)).toEqual(h.record);
299
+ } catch (error) {
300
+ targetFailed = true;
301
+ throw error;
302
+ } finally {
303
+ try {
304
+ const after = await physical();
305
+ console.info(
306
+ "V2CREATE01_SETTLEMENT",
307
+ JSON.stringify({
308
+ physicalEqual:
309
+ stable !== undefined && JSON.stringify(after) === stable,
310
+ rows: Object.fromEntries(
311
+ Object.entries(after).map(([key, rows]) => [key, rows.length]),
312
+ ),
313
+ fetchCalls: h.fetchCalls(),
314
+ }),
315
+ );
316
+ if (stable !== undefined) expect(JSON.stringify(after)).toBe(stable);
317
+ expect(h.fetchCalls()).toBe(0);
318
+ } catch (inspectionError) {
319
+ if (!targetFailed) throw inspectionError;
320
+ console.info("V2CREATE01_READONLY_INSPECTION_FAILED");
321
+ }
322
+ }
323
+ });
324
+
325
+ it("V2CREATE02 refuses invalid complete C1 source before any committed DATA writes", async () => {
326
+ const h = await codecFixture();
327
+ // Declared invalid host DATA: expected period/policy remain unchanged.
328
+ // It is not a qualified C1 record despite passing intrinsic policy checks.
329
+ const invalid = structuredClone(h.record);
330
+ const item = invalid.attachRequest.preparation.subscription.items.data[0];
331
+ expect(item.current_period_end).toBe(1702592000);
332
+ item.current_period_end = invalidSourcePeriodEnd;
333
+ await expect(
334
+ prepareRenewalPriceReplacementV2(invalid.attachRequest.preparation),
335
+ ).rejects.toThrow(
336
+ "Renewal replacement does not match the authorized original and target.",
337
+ );
338
+ const recordJSON = canonical(invalid);
339
+ expect(recordJSON.length).toBeLessThanOrEqual(500000);
340
+ expect(new TextEncoder().encode(recordJSON).length).toBeLessThanOrEqual(
341
+ 500000,
342
+ );
343
+ expect(canonicalJournalRecord(recordJSON)).toMatchObject({
344
+ recordVersion: 2,
345
+ json: recordJSON,
346
+ });
347
+ await expectInvalidCreate(h, recordJSON, "V2CREATE02");
348
+ });
349
+
350
+ // Closed host-DATA mutations; the complete raw C1 input stays untouched.
351
+ const invalidRequests = [
352
+ {
353
+ name: "payload",
354
+ dispatch: { payload: { from_subscription: "sub_other" } },
355
+ },
356
+ { name: "key", dispatch: { key: "attach:other" } },
357
+ { name: "path", dispatch: { path: "/v1/subscription_schedules/other" } },
358
+ { name: "extra-wire-key", dispatch: { extra: null } },
359
+ {
360
+ name: "root-binding",
361
+ root: { requestBinding: "not-the-actual-C2-binding" },
362
+ },
363
+ {
364
+ name: "root-account",
365
+ root: {
366
+ account: { mode: "platform", stripeAccount: null, livemode: true },
367
+ },
368
+ },
369
+ { name: "root-api", root: { apiVersion: "2026-08-27.dahlia" } },
370
+ ] as const;
371
+
372
+ it.each(invalidRequests)(
373
+ "V2CREATE03 refuses full C2/root $name mismatch",
374
+ async (scenario) => {
375
+ const h = await codecFixture();
376
+ const root = "root" in scenario ? scenario.root : {};
377
+ const wire = "dispatch" in scenario ? scenario.dispatch : {};
378
+ const invalid = {
379
+ ...h.record,
380
+ ...root,
381
+ dispatch: { ...h.record.dispatch, ...wire },
382
+ };
383
+ expect(canonical(invalid.attachRequest)).toBe(
384
+ canonical(h.record.attachRequest),
385
+ );
386
+ // Prove the raw C1 input remains valid independently of the component target.
387
+ await expect(
388
+ prepareRenewalPriceReplacementV2(invalid.attachRequest.preparation),
389
+ ).resolves.toMatchObject({ version: 2 });
390
+ const recordJSON = canonical(invalid);
391
+ expect(recordJSON.length).toBeLessThanOrEqual(500000);
392
+ expect(new TextEncoder().encode(recordJSON).length).toBeLessThanOrEqual(
393
+ 500000,
394
+ );
395
+ expect(canonicalJournalRecord(recordJSON)).toMatchObject({
396
+ recordVersion: 2,
397
+ json: recordJSON,
398
+ });
399
+ await expectInvalidCreate(h, recordJSON, `V2CREATE03_${scenario.name}`);
400
+ },
401
+ );
402
+
403
+ // Fixed stored-DATA corruption recipes, declared before fixture constructors.
404
+ const storedIntegrityCases = [
405
+ "invalid-actual-C1",
406
+ "missing-V2-header",
407
+ "legacy-source-V2-header",
408
+ ] as const;
409
+
410
+ it.each(storedIntegrityCases)(
411
+ "V2READ01 refuses stored %s without repairing DATA",
412
+ async (scenario) => {
413
+ const h = await codecFixture();
414
+ const { t, physical } = componentFixture();
415
+ const args = {
416
+ authorizationScope: h.record.authorization.scope,
417
+ logicalOperationId: h.record.logicalOperationId,
418
+ authorizationRevision: h.record.authorization.revision,
419
+ recordJSON: h.recordJSON,
420
+ };
421
+ expect(getConvexSize(args)).toBeLessThanOrEqual(VALUE_LIMIT);
422
+ const created = await t.mutation(create, args);
423
+ expect(created.status).toBe("created");
424
+ const queryArgs = { operationId: created.operationId };
425
+ expect(getConvexSize(queryArgs)).toBeLessThanOrEqual(VALUE_LIMIT);
426
+ const loaded = await t.query(load, queryArgs);
427
+ expect(getConvexSize(loaded)).toBeLessThanOrEqual(VALUE_LIMIT);
428
+ expect(loaded).toEqual({
429
+ recordVersion: 2,
430
+ operationId: created.operationId,
431
+ recordJSON: h.recordJSON,
432
+ revision: 0,
433
+ activeAttempt: null,
434
+ initialBasis: null,
435
+ });
436
+ const valid = await physical();
437
+ expect(valid.operations).toHaveLength(1);
438
+ expect(valid.attempts).toHaveLength(0);
439
+ expect(valid.events).toHaveLength(0);
440
+ expect(valid.payloads).toHaveLength(1);
441
+ const operation = valid.operations[0];
442
+ const payload = valid.payloads[0];
443
+ expect(operation._id).toBe(created.operationId);
444
+ expect(operation.recordVersion).toBe(2);
445
+ expect(operation.payloadId).toBe(payload._id);
446
+ expect(payload.json).toBe(h.recordJSON);
447
+ expect(payload.owner).toEqual({
448
+ kind: "operation",
449
+ operationId: created.operationId,
450
+ });
451
+ if (scenario === "invalid-actual-C1") {
452
+ const invalid = structuredClone(h.record);
453
+ const item = invalid.attachRequest.preparation.subscription.items.data[0];
454
+ expect(item.current_period_end).toBe(1702592000);
455
+ item.current_period_end = invalidSourcePeriodEnd;
456
+ expect(invalid.timingPolicy).toEqual(h.record.timingPolicy);
457
+ expect(invalid.attachRequest.preparation.expected).toEqual(
458
+ h.record.attachRequest.preparation.expected,
459
+ );
460
+ const json = canonical(invalid);
461
+ expect(canonicalJournalRecord(json)).toMatchObject({
462
+ json,
463
+ recordVersion: 2,
464
+ });
465
+ await expect(
466
+ prepareRenewalPriceReplacementV2(invalid.attachRequest.preparation),
467
+ ).rejects.toThrow(
468
+ "Renewal replacement does not match the authorized original and target.",
469
+ );
470
+ await t.run((ctx) =>
471
+ ctx.db.patch("renewalOperationPayloads", payload._id, { json }),
472
+ );
473
+ } else if (scenario === "missing-V2-header") {
474
+ await t.run((ctx) =>
475
+ ctx.db.patch("renewalOperations", operation._id, {
476
+ recordVersion: undefined,
477
+ }),
478
+ );
479
+ } else {
480
+ const legacy: Record<string, unknown> = {
481
+ ...h.record,
482
+ schemaVersion: 1,
483
+ basis: { retainedLegacyDATA: true },
484
+ };
485
+ delete legacy.timingPolicy;
486
+ const json = canonical(legacy);
487
+ expect(canonicalJournalRecord(json)).toEqual({
488
+ json,
489
+ authorizationScope: h.record.authorization.scope,
490
+ authorizationId: h.record.authorization.id,
491
+ authorizationRevision: h.record.authorization.revision,
492
+ logicalOperationId: h.record.logicalOperationId,
493
+ requestBinding: h.record.requestBinding,
494
+ });
495
+ await t.run((ctx) =>
496
+ ctx.db.patch("renewalOperationPayloads", payload._id, { json }),
497
+ );
498
+ }
499
+ const afterPatch = await physical();
500
+ expect(afterPatch).not.toEqual(valid);
501
+ for (const rows of Object.values(afterPatch)) {
502
+ for (const row of rows)
503
+ expect(getDocumentSize(row)).toBeLessThanOrEqual(VALUE_LIMIT);
504
+ }
505
+ let targetFailed = false;
506
+ try {
507
+ await expect(t.query(load, queryArgs)).rejects.toThrow(
508
+ /^RENEWAL_JOURNAL_CONFLICT$/,
509
+ );
510
+ } catch (error) {
511
+ targetFailed = true;
512
+ throw error;
513
+ } finally {
514
+ try {
515
+ expect(await physical()).toEqual(afterPatch);
516
+ expect(h.fetchCalls()).toBe(0);
517
+ } catch (inspectionError) {
518
+ if (!targetFailed) throw inspectionError;
519
+ console.info("V2READ01_READONLY_INSPECTION_FAILED");
520
+ }
521
+ }
522
+ },
523
+ );
@@ -135,7 +135,18 @@ import { accountsTable } from "./core/schema";
135
135
  import { pricesTable, productsTable } from "./products/schema";
136
136
  import { webhookEventsTable } from "./webhooks/schema";
137
137
 
138
+ import {
139
+ eventsTable as renewalOperationEvents,
140
+ attemptsTable as renewalOperationAttempts,
141
+ operationsTable as renewalOperations,
142
+ payloadsTable as renewalOperationPayloads,
143
+ } from "./renewalOperations/schema.js";
144
+
138
145
  export default defineSchema({
146
+ renewalOperations,
147
+ renewalOperationAttempts,
148
+ renewalOperationEvents,
149
+ renewalOperationPayloads,
139
150
  reversalObservations,
140
151
  reversalObservationPayloads,
141
152
  reversalObservationRefs,