@fantastic.dev/better-stripe 0.100.0 → 0.102.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 (119) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +2 -0
  4. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -1
  5. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +16 -0
  6. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -1
  7. package/dist/client/billing/renewalAttach/internal/producer/configureAdmission.d.ts +7 -0
  8. package/dist/client/billing/renewalAttach/internal/producer/configureAdmission.d.ts.map +1 -0
  9. package/dist/client/billing/renewalAttach/internal/producer/configureAdmission.js +117 -0
  10. package/dist/client/billing/renewalAttach/internal/producer/configureAdmission.js.map +1 -0
  11. package/dist/client/billing/renewalAttach/internal/producer/configureJournalData.d.ts +53 -0
  12. package/dist/client/billing/renewalAttach/internal/producer/configureJournalData.d.ts.map +1 -0
  13. package/dist/client/billing/renewalAttach/internal/producer/configureJournalData.js +198 -0
  14. package/dist/client/billing/renewalAttach/internal/producer/configureJournalData.js.map +1 -0
  15. package/dist/client/billing/renewalAttach/internal/producer/configureLineage.d.ts +959 -0
  16. package/dist/client/billing/renewalAttach/internal/producer/configureLineage.d.ts.map +1 -0
  17. package/dist/client/billing/renewalAttach/internal/producer/configureLineage.js +115 -0
  18. package/dist/client/billing/renewalAttach/internal/producer/configureLineage.js.map +1 -0
  19. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts +2 -0
  20. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts.map +1 -1
  21. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js +4 -0
  22. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js.map +1 -1
  23. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts +1 -0
  24. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts.map +1 -1
  25. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js +1 -0
  26. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js.map +1 -1
  27. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +18 -18
  28. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +9 -9
  29. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +50 -50
  30. package/dist/client/connect/payoutSyncTestHelpers.d.ts +25 -25
  31. package/dist/component/balanceFunding/mutations.d.ts +27 -27
  32. package/dist/component/balanceFunding/queries.d.ts +1 -1
  33. package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
  34. package/dist/component/earnings/triggers.d.ts +22 -22
  35. package/dist/component/payoutRelease/queries.d.ts +2 -2
  36. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  37. package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
  38. package/dist/component/renewalOperations/attachValidation.d.ts +19 -0
  39. package/dist/component/renewalOperations/attachValidation.d.ts.map +1 -0
  40. package/dist/component/renewalOperations/attachValidation.js +167 -0
  41. package/dist/component/renewalOperations/attachValidation.js.map +1 -0
  42. package/dist/component/renewalOperations/configureAcknowledgement.d.ts +13 -0
  43. package/dist/component/renewalOperations/configureAcknowledgement.d.ts.map +1 -0
  44. package/dist/component/renewalOperations/configureAcknowledgement.js +108 -0
  45. package/dist/component/renewalOperations/configureAcknowledgement.js.map +1 -0
  46. package/dist/component/renewalOperations/configureParent.d.ts +10 -0
  47. package/dist/component/renewalOperations/configureParent.d.ts.map +1 -0
  48. package/dist/component/renewalOperations/configureParent.js +62 -0
  49. package/dist/component/renewalOperations/configureParent.js.map +1 -0
  50. package/dist/component/renewalOperations/configureValidation.d.ts +12 -0
  51. package/dist/component/renewalOperations/configureValidation.d.ts.map +1 -0
  52. package/dist/component/renewalOperations/configureValidation.js +64 -0
  53. package/dist/component/renewalOperations/configureValidation.js.map +1 -0
  54. package/dist/component/renewalOperations/helpers.d.ts +16 -21
  55. package/dist/component/renewalOperations/helpers.d.ts.map +1 -1
  56. package/dist/component/renewalOperations/helpers.js +13 -169
  57. package/dist/component/renewalOperations/helpers.js.map +1 -1
  58. package/dist/component/renewalOperations/mutations.d.ts +6 -6
  59. package/dist/component/renewalOperations/mutations.d.ts.map +1 -1
  60. package/dist/component/renewalOperations/mutations.js +14 -7
  61. package/dist/component/renewalOperations/mutations.js.map +1 -1
  62. package/dist/component/renewalOperations/operationContext.d.ts +12 -0
  63. package/dist/component/renewalOperations/operationContext.d.ts.map +1 -0
  64. package/dist/component/renewalOperations/operationContext.js +14 -0
  65. package/dist/component/renewalOperations/operationContext.js.map +1 -0
  66. package/dist/component/renewalOperations/queries.d.ts +4 -4
  67. package/dist/component/renewalOperations/queries.js +2 -1
  68. package/dist/component/renewalOperations/queries.js.map +1 -1
  69. package/dist/component/renewalOperations/schema.d.ts +59 -59
  70. package/dist/component/renewalOperations/schema.js +3 -3
  71. package/dist/component/renewalOperations/schema.js.map +1 -1
  72. package/dist/component/renewalOperations/stageAcknowledgement.d.ts +15 -0
  73. package/dist/component/renewalOperations/stageAcknowledgement.d.ts.map +1 -0
  74. package/dist/component/renewalOperations/stageAcknowledgement.js +20 -0
  75. package/dist/component/renewalOperations/stageAcknowledgement.js.map +1 -0
  76. package/dist/component/renewalOperations/validationTypes.d.ts +9 -0
  77. package/dist/component/renewalOperations/validationTypes.d.ts.map +1 -0
  78. package/dist/component/renewalOperations/validationTypes.js +2 -0
  79. package/dist/component/renewalOperations/validationTypes.js.map +1 -0
  80. package/dist/component/schema.d.ts +9 -9
  81. package/dist/component/usage/allowance.fixtures.d.ts +25 -25
  82. package/dist/component/usageCredits/accounting.fixtures.d.ts +9 -9
  83. package/dist/shared/renewalOperationJournal.d.ts +1 -1
  84. package/dist/shared/renewalOperationJournal.d.ts.map +1 -1
  85. package/dist/shared/renewalOperationJournal.js +41 -9
  86. package/dist/shared/renewalOperationJournal.js.map +1 -1
  87. package/docs/renewal-context-reads.md +81 -11
  88. package/package.json +1 -1
  89. package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +21 -0
  90. package/src/client/billing/renewalAttach/internal/producer/configureAdmission.ts +179 -0
  91. package/src/client/billing/renewalAttach/internal/producer/configureJournalData.ts +248 -0
  92. package/src/client/billing/renewalAttach/internal/producer/configureLineage.ts +166 -0
  93. package/src/client/billing/renewalAttach/internal/producer/invocationProducer.ts +13 -0
  94. package/src/client/billing/renewalAttach/internal/producer/lineageAuthority.ts +2 -0
  95. package/src/client/billing/renewalAttach/internal/producer/producerConfigureAdmission.integration.test.ts +203 -0
  96. package/src/client/billing/renewalAttach/internal/producer/producerConfigureControls.integration.test.ts +566 -0
  97. package/src/client/billing/renewalAttach/internal/test-fixtures/producerConfigureAdmission.ts +316 -0
  98. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +61 -3
  99. package/src/component/renewalOperations/attachValidation.ts +203 -0
  100. package/src/component/renewalOperations/configureAcknowledgement.test.ts +30 -0
  101. package/src/component/renewalOperations/configureAcknowledgement.ts +182 -0
  102. package/src/component/renewalOperations/configureAcknowledgementControls.test.ts +96 -0
  103. package/src/component/renewalOperations/configureBasis.test.ts +77 -0
  104. package/src/component/renewalOperations/configureCreation.test.ts +75 -0
  105. package/src/component/renewalOperations/configureJournal.fixtures.ts +385 -0
  106. package/src/component/renewalOperations/configureParent.ts +71 -0
  107. package/src/component/renewalOperations/configurePolicy.test.ts +105 -0
  108. package/src/component/renewalOperations/configurePublication.test.ts +85 -0
  109. package/src/component/renewalOperations/configureReceiptBounds.test.ts +39 -0
  110. package/src/component/renewalOperations/configureSourceControls.test.ts +65 -0
  111. package/src/component/renewalOperations/configureValidation.ts +70 -0
  112. package/src/component/renewalOperations/helpers.ts +21 -204
  113. package/src/component/renewalOperations/mutations.ts +16 -7
  114. package/src/component/renewalOperations/operationContext.ts +21 -0
  115. package/src/component/renewalOperations/queries.ts +1 -1
  116. package/src/component/renewalOperations/schema.ts +3 -3
  117. package/src/component/renewalOperations/stageAcknowledgement.ts +45 -0
  118. package/src/component/renewalOperations/validationTypes.ts +9 -0
  119. package/src/shared/renewalOperationJournal.ts +49 -8
@@ -0,0 +1,385 @@
1
+ import { expect, vi } from "vitest";
2
+ import assert from "node:assert/strict";
3
+ import { makeConfigureReadNativeBaseline } from "../../client/billing/renewalAttach/internal/test-fixtures/configureReadNativeBaseline.js";
4
+ import {
5
+ prepareRenewalPriceReplacementV2 as c1,
6
+ prepareRenewalScheduleAttachmentV2 as c2,
7
+ constructRenewalScheduleAttachmentReceiptV2 as c3,
8
+ projectRenewalConfigurationV2 as c6,
9
+ } from "../../client/billing/renewalV2/index.js";
10
+ import { renewalSnapshot } from "../../client/billing/renewalReplacementSnapshot.js";
11
+
12
+ export const canonicalConfigure = (value: unknown) =>
13
+ JSON.stringify(renewalSnapshot(value));
14
+ export function sevenConfigure(request: {
15
+ scope: "platform";
16
+ apiVersion: string;
17
+ method: "POST";
18
+ path: string;
19
+ payload: unknown;
20
+ key: string;
21
+ firstDispatchedAt: number;
22
+ }) {
23
+ const { scope, apiVersion, method, path, payload, key, firstDispatchedAt } =
24
+ request;
25
+ return { scope, apiVersion, method, path, payload, key, firstDispatchedAt };
26
+ }
27
+ /** Complete synthetic constructor DATA only, without an Owner or provider call. */
28
+ export async function configureJournalFixture() {
29
+ let fetches = 0;
30
+ vi.stubGlobal("fetch", () => {
31
+ fetches++;
32
+ throw Error("UNEXPECTED_PROVIDER_CALL");
33
+ });
34
+ const baseline = makeConfigureReadNativeBaseline();
35
+ const attachRequest = structuredClone(baseline.original.attachRequest);
36
+ const prepared = await c1(attachRequest.preparation);
37
+ const attach = await c2({
38
+ prepared,
39
+ apiVersion: attachRequest.apiVersion,
40
+ attachKey: attachRequest.attachKey,
41
+ firstDispatchedAt: attachRequest.firstDispatchedAt,
42
+ });
43
+ const rawReceipt: Parameters<typeof c3>[0] = {
44
+ attachRequest,
45
+ actualDispatch: {
46
+ scope: attach.scope,
47
+ apiVersion: attach.apiVersion,
48
+ method: attach.method,
49
+ path: attach.path,
50
+ payload: attach.payload,
51
+ key: attach.key,
52
+ firstDispatchedAt: attach.firstDispatchedAt,
53
+ },
54
+ providerSnapshot: structuredClone(baseline.schedule),
55
+ };
56
+ const receipt = await c3(rawReceipt);
57
+ const configuration = {
58
+ preparation: attachRequest.preparation,
59
+ attachReceipt: rawReceipt,
60
+ freshSubscription: {
61
+ ...structuredClone(attachRequest.preparation.subscription),
62
+ schedule: receipt.scheduleId,
63
+ },
64
+ targetPrice: structuredClone(attachRequest.preparation.targetPrice),
65
+ billingCustomer: structuredClone(attachRequest.preparation.billingCustomer),
66
+ schedule: structuredClone(baseline.schedule),
67
+ apiVersion: attachRequest.apiVersion,
68
+ observedAt: baseline.clockPlan.observationSeconds,
69
+ firstDispatchedAt: baseline.clockPlan.observationSeconds,
70
+ configureKey: "configure:journal:retained-key",
71
+ };
72
+ const request = await c6(configuration);
73
+ assert("requestIdentity" in request);
74
+ const parentAttachment = {
75
+ operationId: "synthetic-parent-operation-DATA",
76
+ attemptId: "synthetic-parent-attempt-DATA",
77
+ sourcePayloadId: "synthetic-parent-source-DATA",
78
+ receiptBinding: receipt.binding,
79
+ };
80
+ const record = {
81
+ schemaVersion: 2,
82
+ authorization: {
83
+ scope: "configure-host",
84
+ id: "configure-consent",
85
+ revision: 1,
86
+ },
87
+ logicalOperationId: "configure-stage-operation",
88
+ requestBinding: request.requestIdentity.binding,
89
+ stage: "configure",
90
+ account: {
91
+ mode: "platform",
92
+ stripeAccount: null,
93
+ livemode: prepared.expected.livemode,
94
+ },
95
+ apiVersion: request.apiVersion,
96
+ dispatch: sevenConfigure(request),
97
+ parentAttachment,
98
+ configureRecord: {
99
+ configuration,
100
+ request,
101
+ attachReceipt: receipt,
102
+ originalBundle: structuredClone(baseline.original.originalBundle),
103
+ },
104
+ timingPolicy: {
105
+ version: 1,
106
+ firstDispatchedAt: request.firstDispatchedAt,
107
+ periodStart: prepared.expected.periodStart,
108
+ periodEnd: prepared.expected.periodEnd,
109
+ workEndWallMs:
110
+ (baseline.clockPlan.attachSeconds + baseline.clockPlan.workSeconds) *
111
+ 1000,
112
+ endWallMs:
113
+ (baseline.clockPlan.attachSeconds + baseline.clockPlan.endSeconds) *
114
+ 1000,
115
+ },
116
+ };
117
+ expect(request.key).not.toBe(attach.key);
118
+ expect(fetches).toBe(0);
119
+ return {
120
+ record,
121
+ request,
122
+ receipt,
123
+ attachRequest,
124
+ rawReceipt,
125
+ fetches: () => fetches,
126
+ };
127
+ }
128
+
129
+ export async function configureComponentFixture(publishParent = true) {
130
+ const h = await configureJournalFixture();
131
+ const { convexTest } = await import("convex-test");
132
+ const { schema, modules } = await import("../../test.js");
133
+ const { makeFunctionReference } = await import("convex/server");
134
+ const { canonicalJournalEvent } =
135
+ await import("../../shared/renewalOperationJournal.js");
136
+ const t = convexTest(schema, modules);
137
+ const create = makeFunctionReference<"mutation">(
138
+ "renewalOperations/mutations:createAuthorizedOperation",
139
+ );
140
+ const reserve = makeFunctionReference<"mutation">(
141
+ "renewalOperations/mutations:reserveAttempt",
142
+ );
143
+ const bind = makeFunctionReference<"mutation">(
144
+ "renewalOperations/mutations:bindInitialBasis",
145
+ );
146
+ const append = makeFunctionReference<"mutation">(
147
+ "renewalOperations/mutations:appendStageEvent",
148
+ );
149
+ const retain = makeFunctionReference<"mutation">(
150
+ "renewalOperations/mutations:retainProviderAcknowledgement",
151
+ );
152
+ const load = makeFunctionReference<"query">(
153
+ "renewalOperations/queries:loadAuthorizedOperation",
154
+ );
155
+ const parentPolicy = {
156
+ ...h.record.timingPolicy,
157
+ firstDispatchedAt: h.attachRequest.firstDispatchedAt,
158
+ };
159
+ const parentRecord = {
160
+ schemaVersion: 2,
161
+ authorization: { ...h.record.authorization, id: "attach-parent-consent" },
162
+ logicalOperationId: "attach-stage-parent",
163
+ stage: "attach",
164
+ account: h.record.account,
165
+ apiVersion: h.record.apiVersion,
166
+ requestBinding: h.receipt.request.requestIdentity.binding,
167
+ attachRequest: h.attachRequest,
168
+ originalBundle: h.record.configureRecord.originalBundle,
169
+ dispatch: h.rawReceipt.actualDispatch,
170
+ timingPolicy: parentPolicy,
171
+ };
172
+ const parentJSON = canonicalConfigure(parentRecord);
173
+ const parentCreated = await t.mutation(create, {
174
+ authorizationScope: parentRecord.authorization.scope,
175
+ authorizationRevision: 1,
176
+ logicalOperationId: parentRecord.logicalOperationId,
177
+ recordJSON: parentJSON,
178
+ });
179
+ expect(parentCreated.status).toBe("created");
180
+ const acquired = await t.mutation(reserve, {
181
+ operationId: parentCreated.operationId,
182
+ expectedRevision: 0,
183
+ stage: "attach",
184
+ expectedRequestBinding: parentRecord.requestBinding,
185
+ recordJSON: parentJSON,
186
+ });
187
+ expect(acquired.disposition).toBe("acquired");
188
+ const basis = {
189
+ startWallMs: parentPolicy.firstDispatchedAt * 1000,
190
+ startNsDecimal: "1000000000",
191
+ workEndWallMs: parentPolicy.workEndWallMs,
192
+ workEndNsDecimal: String(
193
+ 1000000000n +
194
+ BigInt(
195
+ parentPolicy.workEndWallMs - parentPolicy.firstDispatchedAt * 1000,
196
+ ) *
197
+ 1000000n,
198
+ ),
199
+ endWallMs: parentPolicy.endWallMs,
200
+ endNsDecimal: String(
201
+ 1000000000n +
202
+ BigInt(parentPolicy.endWallMs - parentPolicy.firstDispatchedAt * 1000) *
203
+ 1000000n,
204
+ ),
205
+ };
206
+ const bound = await t.mutation(bind, {
207
+ operationId: parentCreated.operationId,
208
+ attemptId: acquired.reservation.attemptId,
209
+ policyJSON: canonicalConfigure(parentPolicy),
210
+ basisJSON: canonicalConfigure(basis),
211
+ });
212
+ expect(bound.status).toBe("bound");
213
+ const parentEvents = [
214
+ { event: "dispatch", stage: "attach", request: h.receipt.request },
215
+ {
216
+ event: "response",
217
+ stage: "attach",
218
+ request: h.receipt.request,
219
+ body: h.rawReceipt.providerSnapshot,
220
+ status: 200,
221
+ },
222
+ {
223
+ event: "receipt",
224
+ stage: "attach",
225
+ request: h.receipt.request,
226
+ receipt: h.receipt,
227
+ },
228
+ ];
229
+ for (let index = 0; index < (publishParent ? 3 : 2); index++) {
230
+ if (index === 2)
231
+ await t.mutation(retain, {
232
+ operationId: parentCreated.operationId,
233
+ attemptId: acquired.reservation.attemptId,
234
+ expectedSequence: 2,
235
+ receiptJSON: canonicalConfigure(h.receipt),
236
+ });
237
+ const event = await canonicalJournalEvent(
238
+ index + 1,
239
+ canonicalConfigure(parentEvents[index]),
240
+ );
241
+ await t.mutation(append, {
242
+ reservation: acquired.reservation,
243
+ expectedSequence: index,
244
+ event: {
245
+ sequence: index + 1,
246
+ binding: event.binding,
247
+ dataJSON: event.json,
248
+ },
249
+ });
250
+ }
251
+ if (!publishParent)
252
+ await t.mutation(retain, {
253
+ operationId: parentCreated.operationId,
254
+ attemptId: acquired.reservation.attemptId,
255
+ expectedSequence: 2,
256
+ receiptJSON: canonicalConfigure(h.receipt),
257
+ });
258
+ const record = {
259
+ ...h.record,
260
+ parentAttachment: {
261
+ operationId: parentCreated.operationId,
262
+ attemptId: acquired.reservation.attemptId,
263
+ sourcePayloadId: acquired.reservation.payloadId,
264
+ receiptBinding: h.receipt.binding,
265
+ },
266
+ };
267
+ const createArgs = {
268
+ authorizationScope: record.authorization.scope,
269
+ authorizationRevision: record.authorization.revision,
270
+ logicalOperationId: record.logicalOperationId,
271
+ recordJSON: canonicalConfigure(record),
272
+ };
273
+ return {
274
+ ...h,
275
+ record,
276
+ t,
277
+ create,
278
+ reserve,
279
+ bind,
280
+ append,
281
+ retain,
282
+ load,
283
+ parentCreated,
284
+ acquired,
285
+ basis,
286
+ createArgs,
287
+ };
288
+ }
289
+
290
+ export async function configureAcknowledgementFixture(
291
+ defaults: "absent" | "null" | "object" = "absent",
292
+ responseExtras: Record<string, unknown> = {},
293
+ ) {
294
+ const h = await configureComponentFixture();
295
+ const { constructRenewalConfigurationReceiptV2 } =
296
+ await import("../../client/billing/renewalV2/index.js");
297
+ const { canonicalJournalEvent } =
298
+ await import("../../shared/renewalOperationJournal.js");
299
+ const { makeFunctionReference } = await import("convex/server");
300
+ const created = await h.t.mutation(h.create, h.createArgs);
301
+ expect(created.status).toBe("created");
302
+ const acquired = await h.t.mutation(h.reserve, {
303
+ operationId: created.operationId,
304
+ expectedRevision: 0,
305
+ stage: "configure",
306
+ expectedRequestBinding: h.request.requestIdentity.binding,
307
+ recordJSON: h.createArgs.recordJSON,
308
+ });
309
+ expect(acquired.disposition).toBe("acquired");
310
+ await h.t.mutation(h.bind, {
311
+ operationId: created.operationId,
312
+ attemptId: acquired.reservation.attemptId,
313
+ policyJSON: canonicalConfigure(h.record.timingPolicy),
314
+ basisJSON: canonicalConfigure(h.basis),
315
+ });
316
+ const response = structuredClone(h.rawReceipt.providerSnapshot);
317
+ if (defaults === "absent") delete response.default_settings;
318
+ if (defaults === "null") response.default_settings = null;
319
+ response.futureResponse = { retained: [null, "complete", 1.25] };
320
+ Object.assign(response, responseExtras);
321
+ const transport = { status: 200, requestId: "req_configure_journal_DATA" };
322
+ const request = h.request;
323
+ const receipt = await constructRenewalConfigurationReceiptV2({
324
+ configuration: h.record.configureRecord.configuration,
325
+ actualDispatch: {
326
+ scope: request.scope,
327
+ apiVersion: request.apiVersion,
328
+ method: request.method,
329
+ path: request.path,
330
+ payload: request.payload,
331
+ key: request.key,
332
+ firstDispatchedAt: request.firstDispatchedAt,
333
+ },
334
+ providerSnapshot: response,
335
+ transport,
336
+ });
337
+ const events = [
338
+ { event: "dispatch", stage: "configure", request },
339
+ {
340
+ event: "response",
341
+ stage: "configure",
342
+ request,
343
+ body: response,
344
+ transport,
345
+ },
346
+ ];
347
+ for (const [index, data] of events.entries()) {
348
+ const event = await canonicalJournalEvent(
349
+ index + 1,
350
+ canonicalConfigure(data),
351
+ );
352
+ await h.t.mutation(h.append, {
353
+ reservation: acquired.reservation,
354
+ expectedSequence: index,
355
+ event: {
356
+ sequence: index + 1,
357
+ binding: event.binding,
358
+ dataJSON: event.json,
359
+ },
360
+ });
361
+ }
362
+ const retainArgs = {
363
+ operationId: created.operationId,
364
+ attemptId: acquired.reservation.attemptId,
365
+ expectedSequence: 2,
366
+ receiptJSON: canonicalConfigure(receipt),
367
+ };
368
+ const readAcknowledgement = makeFunctionReference<"query">(
369
+ "renewalOperations/queries:readProviderAcknowledgement",
370
+ );
371
+ const readAttempt = makeFunctionReference<"query">(
372
+ "renewalOperations/queries:readAttempt",
373
+ );
374
+ return {
375
+ ...h,
376
+ created,
377
+ acquired,
378
+ response,
379
+ transport,
380
+ receipt,
381
+ retainArgs,
382
+ readAcknowledgement,
383
+ readAttempt,
384
+ };
385
+ }
@@ -0,0 +1,71 @@
1
+ import type { QueryCtx } from "../_generated/server.js";
2
+ import { canonicalJournalRecord } from "../../shared/renewalOperationJournal.js";
3
+ import { equal } from "../../client/billing/renewalAggregate/shared.js";
4
+ import {
5
+ originalRecord,
6
+ currentReservation,
7
+ eventChain,
8
+ checkedInitialBasis,
9
+ validateV2KnownAcknowledgement,
10
+ } from "./helpers.js";
11
+ import { configureConflict, jsonObject } from "./configureValidation.js";
12
+
13
+ type Reader = Pick<QueryCtx, "db">;
14
+ /** Parent linkage is checked DATA; host authorization and live custody remain separate. */
15
+ export async function validateConfigureParent(
16
+ ctx: Reader,
17
+ full: ReturnType<typeof canonicalJournalRecord>,
18
+ ) {
19
+ if (!("recordVersion" in full)) return null;
20
+ const child = jsonObject(JSON.parse(full.json));
21
+ if (child.stage !== "configure") return null;
22
+ try {
23
+ const link = jsonObject(child.parentAttachment);
24
+ if (typeof link.operationId !== "string") configureConflict();
25
+ const parentId = ctx.db.normalizeId("renewalOperations", link.operationId);
26
+ if (!parentId) configureConflict();
27
+ const row = await ctx.db.get("renewalOperations", parentId);
28
+ if (!row || row.recordVersion !== 2) configureConflict();
29
+ const parentFull = await originalRecord(ctx, row);
30
+ const parent = jsonObject(JSON.parse(parentFull.json));
31
+ if (parent.stage !== "attach") configureConflict();
32
+ const reservation = await currentReservation(ctx, row, parentFull);
33
+ if (!reservation || reservation.stage !== "attach") configureConflict();
34
+ const basis = checkedInitialBasis(row, reservation.attemptId, reservation);
35
+ if (!basis) configureConflict();
36
+ const events = await eventChain(ctx, reservation);
37
+ if (events.length !== 3) configureConflict();
38
+ const known = await validateV2KnownAcknowledgement(
39
+ ctx,
40
+ row,
41
+ parentFull,
42
+ reservation,
43
+ events,
44
+ );
45
+ if (!known) configureConflict();
46
+ const source = jsonObject(child.configureRecord);
47
+ const configuration = jsonObject(source.configuration);
48
+ const rawReceipt = jsonObject(configuration.attachReceipt);
49
+ const policy = jsonObject(child.timingPolicy);
50
+ const parentPolicy = jsonObject(parent.timingPolicy);
51
+ if (
52
+ link.operationId !== row._id ||
53
+ link.attemptId !== reservation.attemptId ||
54
+ link.sourcePayloadId !== reservation.payloadId ||
55
+ link.receiptBinding !== known.admitted.receiptBinding ||
56
+ !equal(source.attachReceipt, JSON.parse(known.admitted.receiptJSON)) ||
57
+ !equal(rawReceipt.attachRequest, parent.attachRequest) ||
58
+ !equal(source.originalBundle, parent.originalBundle) ||
59
+ !equal(child.account, parent.account) ||
60
+ child.apiVersion !== parent.apiVersion ||
61
+ policy.workEndWallMs !== parentPolicy.workEndWallMs ||
62
+ policy.endWallMs !== parentPolicy.endWallMs ||
63
+ policy.periodStart !== parentPolicy.periodStart ||
64
+ policy.periodEnd !== parentPolicy.periodEnd
65
+ )
66
+ configureConflict();
67
+ return { basisJSON: basis.basisJSON, parentOperationId: row._id };
68
+ } catch {
69
+ return configureConflict();
70
+ }
71
+ }
@@ -0,0 +1,105 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import {
3
+ canonicalJournalRecord,
4
+ journalReservationMaterial,
5
+ } from "../../shared/renewalOperationJournal.js";
6
+ import {
7
+ canonicalConfigure,
8
+ configureJournalFixture,
9
+ } from "./configureJournal.fixtures.js";
10
+
11
+ afterEach(() => vi.unstubAllGlobals());
12
+
13
+ it("preserves complete V2 configure policy and parent-link DATA without creating a basis", async () => {
14
+ const h = await configureJournalFixture();
15
+ const json = canonicalConfigure(h.record);
16
+ const full = canonicalJournalRecord(json);
17
+ expect(full).toEqual({
18
+ json,
19
+ authorizationScope: "configure-host",
20
+ authorizationId: "configure-consent",
21
+ authorizationRevision: 1,
22
+ logicalOperationId: "configure-stage-operation",
23
+ requestBinding: h.request.requestIdentity.binding,
24
+ recordVersion: 2,
25
+ });
26
+ expect(JSON.parse(full.json)).toEqual(h.record);
27
+ expect(journalReservationMaterial(full)).toEqual({
28
+ stage: "configure",
29
+ key: h.request.key,
30
+ apiVersion: h.request.apiVersion,
31
+ firstDispatchedAt: h.request.firstDispatchedAt,
32
+ accountJSON: canonicalConfigure(h.record.account),
33
+ recordVersion: 2,
34
+ timingPolicyJSON: canonicalConfigure(h.record.timingPolicy),
35
+ });
36
+ expect(
37
+ Object.prototype.hasOwnProperty.call(JSON.parse(full.json), "basis"),
38
+ ).toBe(false);
39
+ expect(h.fetches()).toBe(0);
40
+ });
41
+
42
+ const malformed = [
43
+ "missing-parent",
44
+ "foreign-parent-binding",
45
+ "missing-parent-id",
46
+ "extra-parent-field",
47
+ "empty-parent-id",
48
+ "wrong-request-stage",
49
+ "wrong-request-binding",
50
+ "wrong-request-time",
51
+ "wrong-policy-time",
52
+ "wrong-period",
53
+ "legacy-basis",
54
+ "unknown-policy-field",
55
+ ] as const;
56
+ it.each(malformed)(
57
+ "refuses malformed intrinsic configure %s DATA",
58
+ async (scenario) => {
59
+ const h = await configureJournalFixture();
60
+ const raw = JSON.parse(canonicalConfigure(h.record));
61
+ switch (scenario) {
62
+ case "missing-parent":
63
+ delete raw.parentAttachment;
64
+ break;
65
+ case "foreign-parent-binding":
66
+ raw.parentAttachment.receiptBinding =
67
+ "renewal-attach-receipt:v2:sha256:" + "0".repeat(64);
68
+ break;
69
+ case "missing-parent-id":
70
+ delete raw.parentAttachment.attemptId;
71
+ break;
72
+ case "extra-parent-field":
73
+ raw.parentAttachment.owner = { copied: true };
74
+ break;
75
+ case "empty-parent-id":
76
+ raw.parentAttachment.operationId = "";
77
+ break;
78
+ case "wrong-request-stage":
79
+ raw.configureRecord.request.stage = "attach";
80
+ break;
81
+ case "wrong-request-binding":
82
+ raw.requestBinding = "forged-configure-binding";
83
+ break;
84
+ case "wrong-request-time":
85
+ raw.configureRecord.request.firstDispatchedAt++;
86
+ break;
87
+ case "wrong-policy-time":
88
+ raw.timingPolicy.firstDispatchedAt++;
89
+ break;
90
+ case "wrong-period":
91
+ raw.timingPolicy.periodEnd++;
92
+ break;
93
+ case "legacy-basis":
94
+ raw.basis = { copiedLegacy: true };
95
+ break;
96
+ case "unknown-policy-field":
97
+ raw.timingPolicy.clock = "caller";
98
+ break;
99
+ }
100
+ expect(() => canonicalJournalRecord(canonicalConfigure(raw))).toThrow(
101
+ "RENEWAL_JOURNAL_JSON_STOP",
102
+ );
103
+ expect(h.fetches()).toBe(0);
104
+ },
105
+ );
@@ -0,0 +1,85 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import { canonicalJournalEvent } from "../../shared/renewalOperationJournal.js";
3
+ import {
4
+ canonicalConfigure,
5
+ configureAcknowledgementFixture,
6
+ } from "./configureJournal.fixtures.js";
7
+
8
+ afterEach(() => vi.unstubAllGlobals());
9
+ it("publishes only matching C9 event3 and preserves identical retention/readback", async () => {
10
+ const h = await configureAcknowledgementFixture("object");
11
+ const retained = await h.t.mutation(h.retain, h.retainArgs);
12
+ const data = {
13
+ event: "receipt",
14
+ stage: "configure",
15
+ request: h.request,
16
+ receipt: h.receipt,
17
+ };
18
+ const event = await canonicalJournalEvent(3, canonicalConfigure(data));
19
+ const args = {
20
+ reservation: h.acquired.reservation,
21
+ expectedSequence: 2,
22
+ event: { sequence: 3, binding: event.binding, dataJSON: event.json },
23
+ };
24
+ const appended = await h.t.mutation(h.append, args);
25
+ expect(appended.stage).toBe("configure");
26
+ expect(await h.t.mutation(h.append, args)).toEqual(appended);
27
+ expect(await h.t.mutation(h.retain, h.retainArgs)).toEqual(retained);
28
+ const read = await h.t.query(h.readAcknowledgement, {
29
+ operationId: h.created.operationId,
30
+ });
31
+ expect(read).toEqual({
32
+ retention: retained,
33
+ receiptJSON: h.retainArgs.receiptJSON,
34
+ });
35
+ expect(
36
+ (
37
+ await h.t.query(h.readAttempt, {
38
+ operationId: h.created.operationId,
39
+ attemptId: h.acquired.reservation.attemptId,
40
+ })
41
+ ).events,
42
+ ).toHaveLength(3);
43
+ expect(h.fetches()).toBe(0);
44
+ });
45
+ it.each(["body", "request", "stage", "extra-field"])(
46
+ "rejects contradictory %s event3 without altering known C9 DATA",
47
+ async (change) => {
48
+ const h = await configureAcknowledgementFixture();
49
+ const retained = await h.t.mutation(h.retain, h.retainArgs);
50
+ const data = JSON.parse(
51
+ canonicalConfigure({
52
+ event: "receipt",
53
+ stage: "configure",
54
+ request: h.request,
55
+ receipt: h.receipt,
56
+ }),
57
+ );
58
+ if (change === "body")
59
+ data.receipt.providerSnapshot.futureResponse = { changed: true };
60
+ if (change === "request") data.request.key += ":foreign";
61
+ if (change === "stage") data.stage = "attach";
62
+ if (change === "extra-field") data.receipt.future = "unowned";
63
+ const event = await canonicalJournalEvent(3, canonicalConfigure(data));
64
+ await expect(
65
+ h.t.mutation(h.append, {
66
+ reservation: h.acquired.reservation,
67
+ expectedSequence: 2,
68
+ event: { sequence: 3, binding: event.binding, dataJSON: event.json },
69
+ }),
70
+ ).rejects.toThrow("RENEWAL_JOURNAL_CONFLICT");
71
+ expect(
72
+ await h.t.query(h.readAcknowledgement, {
73
+ operationId: h.created.operationId,
74
+ }),
75
+ ).toEqual({ retention: retained, receiptJSON: h.retainArgs.receiptJSON });
76
+ expect(
77
+ (
78
+ await h.t.query(h.readAttempt, {
79
+ operationId: h.created.operationId,
80
+ attemptId: h.acquired.reservation.attemptId,
81
+ })
82
+ ).events,
83
+ ).toHaveLength(2);
84
+ },
85
+ );
@@ -0,0 +1,39 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import { canonicalJournalEvent } from "../../shared/renewalOperationJournal.js";
3
+ import { configureAcknowledgementFixture } from "./configureJournal.fixtures.js";
4
+
5
+ afterEach(() => vi.unstubAllGlobals());
6
+ it("retains a large complete C9 at head2 and validates its actual receipt event independently", async () => {
7
+ const h = await configureAcknowledgementFixture("absent", {
8
+ padding: "x".repeat(450000),
9
+ });
10
+ expect(h.retainArgs.receiptJSON.length).toBeGreaterThan(450000);
11
+ const prospective = JSON.stringify({
12
+ event: "receipt",
13
+ stage: "configure",
14
+ request: h.request,
15
+ receipt: h.receipt,
16
+ });
17
+ expect(prospective.length).toBeLessThan(500000);
18
+ const retained = await h.t.mutation(h.retain, h.retainArgs);
19
+ expect(retained.stage).toBe("configure");
20
+ const event = await canonicalJournalEvent(3, prospective);
21
+ await h.t.mutation(h.append, {
22
+ reservation: h.acquired.reservation,
23
+ expectedSequence: 2,
24
+ event: { sequence: 3, binding: event.binding, dataJSON: event.json },
25
+ });
26
+ const read = await h.t.query(h.readAcknowledgement, {
27
+ operationId: h.created.operationId,
28
+ });
29
+ expect(read.receiptJSON).toBe(h.retainArgs.receiptJSON);
30
+ expect(
31
+ (
32
+ await h.t.query(h.readAttempt, {
33
+ operationId: h.created.operationId,
34
+ attemptId: h.acquired.reservation.attemptId,
35
+ })
36
+ ).events,
37
+ ).toHaveLength(3);
38
+ expect(h.fetches()).toBe(0);
39
+ });