@fantastic.dev/better-stripe 0.100.0 → 0.101.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 (80) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +1 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +18 -18
  4. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +9 -9
  5. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +18 -18
  6. package/dist/client/connect/payoutSyncTestHelpers.d.ts +9 -9
  7. package/dist/component/balanceFunding/mutations.d.ts +43 -43
  8. package/dist/component/earnings/triggers.d.ts +6 -6
  9. package/dist/component/renewalOperations/attachValidation.d.ts +19 -0
  10. package/dist/component/renewalOperations/attachValidation.d.ts.map +1 -0
  11. package/dist/component/renewalOperations/attachValidation.js +167 -0
  12. package/dist/component/renewalOperations/attachValidation.js.map +1 -0
  13. package/dist/component/renewalOperations/configureAcknowledgement.d.ts +13 -0
  14. package/dist/component/renewalOperations/configureAcknowledgement.d.ts.map +1 -0
  15. package/dist/component/renewalOperations/configureAcknowledgement.js +108 -0
  16. package/dist/component/renewalOperations/configureAcknowledgement.js.map +1 -0
  17. package/dist/component/renewalOperations/configureParent.d.ts +10 -0
  18. package/dist/component/renewalOperations/configureParent.d.ts.map +1 -0
  19. package/dist/component/renewalOperations/configureParent.js +62 -0
  20. package/dist/component/renewalOperations/configureParent.js.map +1 -0
  21. package/dist/component/renewalOperations/configureValidation.d.ts +12 -0
  22. package/dist/component/renewalOperations/configureValidation.d.ts.map +1 -0
  23. package/dist/component/renewalOperations/configureValidation.js +64 -0
  24. package/dist/component/renewalOperations/configureValidation.js.map +1 -0
  25. package/dist/component/renewalOperations/helpers.d.ts +16 -21
  26. package/dist/component/renewalOperations/helpers.d.ts.map +1 -1
  27. package/dist/component/renewalOperations/helpers.js +13 -169
  28. package/dist/component/renewalOperations/helpers.js.map +1 -1
  29. package/dist/component/renewalOperations/mutations.d.ts +6 -6
  30. package/dist/component/renewalOperations/mutations.d.ts.map +1 -1
  31. package/dist/component/renewalOperations/mutations.js +14 -7
  32. package/dist/component/renewalOperations/mutations.js.map +1 -1
  33. package/dist/component/renewalOperations/operationContext.d.ts +12 -0
  34. package/dist/component/renewalOperations/operationContext.d.ts.map +1 -0
  35. package/dist/component/renewalOperations/operationContext.js +14 -0
  36. package/dist/component/renewalOperations/operationContext.js.map +1 -0
  37. package/dist/component/renewalOperations/queries.d.ts +4 -4
  38. package/dist/component/renewalOperations/queries.js +2 -1
  39. package/dist/component/renewalOperations/queries.js.map +1 -1
  40. package/dist/component/renewalOperations/schema.d.ts +59 -59
  41. package/dist/component/renewalOperations/schema.js +3 -3
  42. package/dist/component/renewalOperations/schema.js.map +1 -1
  43. package/dist/component/renewalOperations/stageAcknowledgement.d.ts +15 -0
  44. package/dist/component/renewalOperations/stageAcknowledgement.d.ts.map +1 -0
  45. package/dist/component/renewalOperations/stageAcknowledgement.js +20 -0
  46. package/dist/component/renewalOperations/stageAcknowledgement.js.map +1 -0
  47. package/dist/component/renewalOperations/validationTypes.d.ts +9 -0
  48. package/dist/component/renewalOperations/validationTypes.d.ts.map +1 -0
  49. package/dist/component/renewalOperations/validationTypes.js +2 -0
  50. package/dist/component/renewalOperations/validationTypes.js.map +1 -0
  51. package/dist/component/schema.d.ts +9 -9
  52. package/dist/component/usage/allowance.fixtures.d.ts +9 -9
  53. package/dist/component/usageCredits/accounting.fixtures.d.ts +9 -9
  54. package/dist/shared/renewalOperationJournal.d.ts +1 -1
  55. package/dist/shared/renewalOperationJournal.d.ts.map +1 -1
  56. package/dist/shared/renewalOperationJournal.js +41 -9
  57. package/dist/shared/renewalOperationJournal.js.map +1 -1
  58. package/docs/renewal-context-reads.md +36 -8
  59. package/package.json +1 -1
  60. package/src/component/renewalOperations/attachValidation.ts +203 -0
  61. package/src/component/renewalOperations/configureAcknowledgement.test.ts +30 -0
  62. package/src/component/renewalOperations/configureAcknowledgement.ts +182 -0
  63. package/src/component/renewalOperations/configureAcknowledgementControls.test.ts +96 -0
  64. package/src/component/renewalOperations/configureBasis.test.ts +77 -0
  65. package/src/component/renewalOperations/configureCreation.test.ts +75 -0
  66. package/src/component/renewalOperations/configureJournal.fixtures.ts +385 -0
  67. package/src/component/renewalOperations/configureParent.ts +71 -0
  68. package/src/component/renewalOperations/configurePolicy.test.ts +105 -0
  69. package/src/component/renewalOperations/configurePublication.test.ts +85 -0
  70. package/src/component/renewalOperations/configureReceiptBounds.test.ts +39 -0
  71. package/src/component/renewalOperations/configureSourceControls.test.ts +65 -0
  72. package/src/component/renewalOperations/configureValidation.ts +70 -0
  73. package/src/component/renewalOperations/helpers.ts +21 -204
  74. package/src/component/renewalOperations/mutations.ts +16 -7
  75. package/src/component/renewalOperations/operationContext.ts +21 -0
  76. package/src/component/renewalOperations/queries.ts +1 -1
  77. package/src/component/renewalOperations/schema.ts +3 -3
  78. package/src/component/renewalOperations/stageAcknowledgement.ts +45 -0
  79. package/src/component/renewalOperations/validationTypes.ts +9 -0
  80. package/src/shared/renewalOperationJournal.ts +49 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fantastic.dev/better-stripe",
3
- "version": "0.100.0",
3
+ "version": "0.101.0",
4
4
  "description": "Convex component for Stripe Accounts v2, subscriptions, metered billing and Connect marketplaces, with React workflows.",
5
5
  "keywords": [
6
6
  "stripe",
@@ -0,0 +1,203 @@
1
+ import { renewalSnapshot } from "../../client/billing/renewalReplacementSnapshot.js";
2
+ import { constructRenewalScheduleAttachmentReceiptV2 } from "../../client/billing/renewalV2/index.js";
3
+ import { captureAttachmentReceipt } from "../../client/billing/renewalV2/receipt.js";
4
+ import {
5
+ captureAttachSource,
6
+ captureAttachDispatch,
7
+ captureAttachResponse,
8
+ captureAttachReceipt,
9
+ } from "../../client/billing/renewalAttach/internal/c10/eventCapture.js";
10
+ import { requiredJSONSnapshot } from "../../client/billing/renewalAttach/internal/material/requiredJson.js";
11
+ import type { canonicalJournalRecord } from "../../shared/renewalOperationJournal.js";
12
+ import type { Reservation, CheckedEvents } from "./validationTypes.js";
13
+
14
+ export function acknowledgementConflict(): never {
15
+ throw Error("RENEWAL_JOURNAL_CONFLICT");
16
+ }
17
+ function isJSONObject(value: unknown): value is Record<string, unknown> {
18
+ return value !== null && typeof value === "object" && !Array.isArray(value);
19
+ }
20
+ export function jsonObject(value: unknown) {
21
+ if (!isJSONObject(value)) acknowledgementConflict();
22
+ return value;
23
+ }
24
+ export function sameJSON(left: unknown, right: unknown) {
25
+ return (
26
+ JSON.stringify(renewalSnapshot(left)) ===
27
+ JSON.stringify(renewalSnapshot(right))
28
+ );
29
+ }
30
+ type AttachReceipt = Awaited<
31
+ ReturnType<typeof constructRenewalScheduleAttachmentReceiptV2>
32
+ >;
33
+
34
+ /** Parsed JSON DATA admission only; no caller-object or provider provenance. */
35
+ export function admitAttachReceiptOutputDATA(
36
+ receiptJSON: string,
37
+ rebuilt: AttachReceipt,
38
+ ) {
39
+ if (
40
+ typeof receiptJSON !== "string" ||
41
+ receiptJSON.length > 500000 ||
42
+ new TextEncoder().encode(receiptJSON).length > 500000
43
+ )
44
+ acknowledgementConflict();
45
+ const parsed: unknown = JSON.parse(receiptJSON);
46
+ if (!sameJSON(parsed, rebuilt)) acknowledgementConflict();
47
+ const output = renewalSnapshot({
48
+ status: "attach_acknowledged",
49
+ operationalAuthority: false,
50
+ receipt: parsed,
51
+ });
52
+ const slots = new Map<string, { expected: string; family: string }>([
53
+ [
54
+ JSON.stringify(["receipt", "logicalBinding"]),
55
+ { expected: rebuilt.logicalBinding, family: "renewal" },
56
+ ],
57
+ [
58
+ JSON.stringify(["receipt", "request", "logicalBinding"]),
59
+ { expected: rebuilt.request.logicalBinding, family: "renewal" },
60
+ ],
61
+ [
62
+ JSON.stringify(["receipt", "request", "requestIdentity", "binding"]),
63
+ {
64
+ expected: rebuilt.request.requestIdentity.binding,
65
+ family: "renewal-attach",
66
+ },
67
+ ],
68
+ [
69
+ JSON.stringify(["receipt", "binding"]),
70
+ { expected: rebuilt.binding, family: "renewal-attach-receipt" },
71
+ ],
72
+ ]);
73
+ const found = new Set<string>();
74
+ function walk(value: unknown, path: string[]) {
75
+ const key = JSON.stringify(path);
76
+ const slot = slots.get(key);
77
+ if (slot) {
78
+ if (
79
+ value !== slot.expected ||
80
+ typeof value !== "string" ||
81
+ !new RegExp("^" + slot.family + ":v2:sha256:[0-9a-f]{64}$").test(value)
82
+ )
83
+ acknowledgementConflict();
84
+ found.add(key);
85
+ return;
86
+ }
87
+ if (value === null || typeof value !== "object") {
88
+ requiredJSONSnapshot(value);
89
+ return;
90
+ }
91
+ for (const [name, child] of Object.entries(value)) {
92
+ requiredJSONSnapshot(name);
93
+ requiredJSONSnapshot({ [name]: child === null ? null : true });
94
+ walk(child, [...path, name]);
95
+ }
96
+ }
97
+ walk(output, []);
98
+ if (
99
+ found.size !== 4 ||
100
+ new TextEncoder().encode(JSON.stringify(output)).length > 500000
101
+ )
102
+ acknowledgementConflict();
103
+ return JSON.stringify(output.receipt);
104
+ }
105
+
106
+ /** Full stored-source/event/C3 consistency; these are unsigned DATA checks. */
107
+ export async function validateAttachAcknowledgement(
108
+ full: ReturnType<typeof canonicalJournalRecord>,
109
+ reservation: Reservation,
110
+ events: CheckedEvents,
111
+ receiptJSON: string,
112
+ ) {
113
+ if (
114
+ reservation.stage !== "attach" ||
115
+ (events.length !== 2 && events.length !== 3) ||
116
+ events[0].sequence !== 1 ||
117
+ events[1].sequence !== 2
118
+ )
119
+ acknowledgementConflict();
120
+ const parsedOriginal: unknown = JSON.parse(full.json);
121
+ const original = jsonObject(parsedOriginal);
122
+ const account = jsonObject(original.account);
123
+ const source = captureAttachSource(original.attachRequest);
124
+ if (!sameJSON(original.attachRequest, source)) acknowledgementConflict();
125
+ const parsedDispatch: unknown = JSON.parse(events[0].dataJSON);
126
+ const parsedResponse: unknown = JSON.parse(events[1].dataJSON);
127
+ captureAttachDispatch(parsedDispatch);
128
+ captureAttachResponse(parsedResponse);
129
+ const dispatch = jsonObject(parsedDispatch);
130
+ const response = jsonObject(parsedResponse);
131
+ const raw = captureAttachmentReceipt({
132
+ attachRequest: source,
133
+ actualDispatch: original.dispatch,
134
+ providerSnapshot: response.body,
135
+ });
136
+ // C3 internally rebuilds C1/C2 and compares the entire closed seven-wire input.
137
+ const rebuilt = await constructRenewalScheduleAttachmentReceiptV2({
138
+ attachRequest: source,
139
+ actualDispatch: raw.actualDispatch,
140
+ providerSnapshot: raw.providerSnapshot,
141
+ });
142
+ const request = rebuilt.request;
143
+ const seven = {
144
+ scope: request.scope,
145
+ apiVersion: request.apiVersion,
146
+ method: request.method,
147
+ path: request.path,
148
+ payload: request.payload,
149
+ key: request.key,
150
+ firstDispatchedAt: request.firstDispatchedAt,
151
+ };
152
+ if (
153
+ !sameJSON(original.dispatch, seven) ||
154
+ !sameJSON(dispatch.request, request) ||
155
+ !sameJSON(response.request, request) ||
156
+ original.stage !== "attach" ||
157
+ original.apiVersion !== request.apiVersion ||
158
+ full.requestBinding !== request.requestIdentity.binding ||
159
+ reservation.key !== request.key ||
160
+ reservation.apiVersion !== request.apiVersion ||
161
+ reservation.firstDispatchedAt !== request.firstDispatchedAt ||
162
+ account.mode !== "platform" ||
163
+ account.stripeAccount !== null ||
164
+ account.livemode !== rebuilt.livemode ||
165
+ !sameJSON(JSON.parse(reservation.accountJSON), account)
166
+ )
167
+ acknowledgementConflict();
168
+ if ("recordVersion" in reservation) {
169
+ if (
170
+ !sameJSON(JSON.parse(reservation.timingPolicyJSON), original.timingPolicy)
171
+ )
172
+ acknowledgementConflict();
173
+ } else if (!sameJSON(JSON.parse(reservation.basisJSON), original.basis))
174
+ acknowledgementConflict();
175
+ const admittedJSON = admitAttachReceiptOutputDATA(receiptJSON, rebuilt);
176
+ // Only ACTUAL published event3 is checked. A head2 retention does not model
177
+ // or require a prospective receipt-event envelope to pass another cap.
178
+ if (events.length === 3)
179
+ validateAttachReceiptEventDATA(events[2].dataJSON, rebuilt);
180
+ return {
181
+ receiptJSON: admittedJSON,
182
+ receiptBinding: rebuilt.binding,
183
+ receipt: rebuilt,
184
+ };
185
+ }
186
+
187
+ /** Whole typed C10 DATA and full request/receipt equality, never ACK authority. */
188
+ export function validateAttachReceiptEventDATA(
189
+ raw: string,
190
+ receipt: AttachReceipt,
191
+ ) {
192
+ const parsed: unknown = JSON.parse(raw);
193
+ captureAttachReceipt(parsed);
194
+ if (
195
+ !sameJSON(parsed, {
196
+ event: "receipt",
197
+ stage: "attach",
198
+ request: receipt.request,
199
+ receipt,
200
+ })
201
+ )
202
+ acknowledgementConflict();
203
+ }
@@ -0,0 +1,30 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import { configureAcknowledgementFixture } from "./configureJournal.fixtures.js";
3
+
4
+ afterEach(() => vi.unstubAllGlobals());
5
+ it.each(["absent", "null", "object"] as const)(
6
+ "retains complete C9 DATA at head2 with %s defaults without claiming receipt3",
7
+ async (defaults) => {
8
+ const h = await configureAcknowledgementFixture(defaults);
9
+ const retained = await h.t.mutation(h.retain, h.retainArgs);
10
+ expect(retained.status).toBe("retained");
11
+ expect(retained.stage).toBe("configure");
12
+ expect(retained.receiptBinding).toBe(h.receipt.binding);
13
+ const read = await h.t.query(h.readAcknowledgement, {
14
+ operationId: h.created.operationId,
15
+ });
16
+ expect(read.retention).toEqual(retained);
17
+ expect(read.receiptJSON).toBe(h.retainArgs.receiptJSON);
18
+ expect(JSON.parse(read.receiptJSON).providerSnapshot).toEqual(h.response);
19
+ expect(
20
+ (
21
+ await h.t.query(h.readAttempt, {
22
+ operationId: h.created.operationId,
23
+ attemptId: h.acquired.reservation.attemptId,
24
+ })
25
+ ).events,
26
+ ).toHaveLength(2);
27
+ expect(await h.t.mutation(h.retain, h.retainArgs)).toEqual(retained);
28
+ expect(h.fetches()).toBe(0);
29
+ },
30
+ );
@@ -0,0 +1,182 @@
1
+ import { constructRenewalConfigurationReceiptV2 } from "../../client/billing/renewalV2/index.js";
2
+ import {
3
+ captureConfigureDispatch,
4
+ captureConfigureResponse,
5
+ captureConfigureReceipt,
6
+ } from "../../client/billing/renewalAttach/internal/c10/eventCapture.js";
7
+ import { requiredJSONSnapshot } from "../../client/billing/renewalAttach/internal/material/requiredJson.js";
8
+ import { renewalSnapshot } from "../../client/billing/renewalReplacementSnapshot.js";
9
+ import { equal } from "../../client/billing/renewalAggregate/shared.js";
10
+ import type { canonicalJournalRecord } from "../../shared/renewalOperationJournal.js";
11
+ import type { Reservation, CheckedEvents } from "./validationTypes.js";
12
+ import {
13
+ validateOperationalConfigureSource,
14
+ configureConflict,
15
+ jsonObject,
16
+ } from "./configureValidation.js";
17
+
18
+ type Receipt = Awaited<
19
+ ReturnType<typeof constructRenewalConfigurationReceiptV2>
20
+ >;
21
+ function admitReceipt(receiptJSON: string, rebuilt: Receipt) {
22
+ if (
23
+ receiptJSON.length > 500000 ||
24
+ new TextEncoder().encode(receiptJSON).length > 500000
25
+ )
26
+ configureConflict();
27
+ const parsed: unknown = JSON.parse(receiptJSON);
28
+ if (!equal(parsed, rebuilt)) configureConflict();
29
+ const output = renewalSnapshot({
30
+ status: "configure_acknowledged",
31
+ operationalAuthority: false,
32
+ receipt: parsed,
33
+ });
34
+ const slots = new Map<string, { expected: string; family: string }>();
35
+ const add = (path: string[], expected: string, family: string) =>
36
+ slots.set(JSON.stringify(["receipt", ...path]), { expected, family });
37
+ add(["logicalBinding"], rebuilt.logicalBinding, "renewal");
38
+ add(
39
+ ["attachReceiptBinding"],
40
+ rebuilt.attachReceiptBinding,
41
+ "renewal-attach-receipt",
42
+ );
43
+ add(
44
+ ["providerSnapshotBinding"],
45
+ rebuilt.providerSnapshotBinding,
46
+ "renewal-configured-baseline",
47
+ );
48
+ add(["binding"], rebuilt.binding, "renewal-configure-receipt");
49
+ if (rebuilt.defaultSettingsBinding !== undefined)
50
+ add(
51
+ ["defaultSettingsBinding"],
52
+ rebuilt.defaultSettingsBinding,
53
+ "renewal-configured-defaults",
54
+ );
55
+ add(["request", "logicalBinding"], rebuilt.request.logicalBinding, "renewal");
56
+ add(
57
+ ["request", "attachReceiptBinding"],
58
+ rebuilt.request.attachReceiptBinding,
59
+ "renewal-attach-receipt",
60
+ );
61
+ add(
62
+ ["request", "baselineBinding"],
63
+ rebuilt.request.baselineBinding,
64
+ "renewal-baseline",
65
+ );
66
+ add(
67
+ ["request", "defaultSettingsBinding"],
68
+ rebuilt.request.defaultSettingsBinding,
69
+ "renewal-defaults",
70
+ );
71
+ add(
72
+ ["request", "requestIdentity", "binding"],
73
+ rebuilt.request.requestIdentity.binding,
74
+ "renewal-configure",
75
+ );
76
+ const found = new Set<string>();
77
+ function walk(value: unknown, path: string[]) {
78
+ const key = JSON.stringify(path),
79
+ slot = slots.get(key);
80
+ if (slot) {
81
+ if (
82
+ value !== slot.expected ||
83
+ typeof value !== "string" ||
84
+ !new RegExp("^" + slot.family + ":v2:sha256:[0-9a-f]{64}$").test(value)
85
+ )
86
+ configureConflict();
87
+ found.add(key);
88
+ return;
89
+ }
90
+ if (value === null || typeof value !== "object") {
91
+ requiredJSONSnapshot(value);
92
+ return;
93
+ }
94
+ for (const [name, child] of Object.entries(value)) {
95
+ requiredJSONSnapshot(name);
96
+ requiredJSONSnapshot({ [name]: child === null ? null : true });
97
+ walk(child, [...path, name]);
98
+ }
99
+ }
100
+ walk(output, []);
101
+ if (
102
+ found.size !== slots.size ||
103
+ new TextEncoder().encode(JSON.stringify(output)).length > 500000
104
+ )
105
+ configureConflict();
106
+ return JSON.stringify(output.receipt);
107
+ }
108
+
109
+ /** Complete C9 DATA against checked C3/C6/C7 source and actual stored events. */
110
+ export async function validateConfigureAcknowledgement(
111
+ full: ReturnType<typeof canonicalJournalRecord>,
112
+ reservation: Reservation,
113
+ events: CheckedEvents,
114
+ receiptJSON: string,
115
+ ) {
116
+ if (
117
+ !("recordVersion" in reservation) ||
118
+ reservation.stage !== "configure" ||
119
+ (events.length !== 2 && events.length !== 3) ||
120
+ events[0].sequence !== 1 ||
121
+ events[1].sequence !== 2
122
+ )
123
+ configureConflict();
124
+ const checked = await validateOperationalConfigureSource(full);
125
+ const dispatch: unknown = JSON.parse(events[0].dataJSON),
126
+ response: unknown = JSON.parse(events[1].dataJSON);
127
+ captureConfigureDispatch(dispatch);
128
+ captureConfigureResponse(response);
129
+ const sent = jsonObject(dispatch),
130
+ received = jsonObject(response);
131
+ if (
132
+ !equal(sent.request, checked.request) ||
133
+ !equal(received.request, checked.request) ||
134
+ reservation.requestBinding !== checked.request.requestIdentity.binding ||
135
+ reservation.key !== checked.request.key ||
136
+ reservation.apiVersion !== checked.request.apiVersion ||
137
+ reservation.firstDispatchedAt !== checked.request.firstDispatchedAt ||
138
+ !equal(JSON.parse(reservation.accountJSON), checked.original.account) ||
139
+ !equal(
140
+ JSON.parse(reservation.timingPolicyJSON),
141
+ checked.original.timingPolicy,
142
+ )
143
+ )
144
+ configureConflict();
145
+ const rebuilt = await constructRenewalConfigurationReceiptV2({
146
+ configuration: checked.configuration as Parameters<
147
+ typeof constructRenewalConfigurationReceiptV2
148
+ >[0]["configuration"],
149
+ actualDispatch: checked.original.dispatch as Parameters<
150
+ typeof constructRenewalConfigurationReceiptV2
151
+ >[0]["actualDispatch"],
152
+ providerSnapshot: received.body as Record<string, unknown>,
153
+ transport: received.transport as Parameters<
154
+ typeof constructRenewalConfigurationReceiptV2
155
+ >[0]["transport"],
156
+ });
157
+ const admittedJSON = admitReceipt(receiptJSON, rebuilt);
158
+ // Retention at head2 never models a prospective, potentially larger event3.
159
+ if (events.length === 3)
160
+ validateConfigureReceiptEventDATA(events[2].dataJSON, rebuilt);
161
+ return {
162
+ receiptJSON: admittedJSON,
163
+ receiptBinding: rebuilt.binding,
164
+ receipt: rebuilt,
165
+ };
166
+ }
167
+ export function validateConfigureReceiptEventDATA(
168
+ raw: string,
169
+ receipt: Receipt,
170
+ ) {
171
+ const parsed: unknown = JSON.parse(raw);
172
+ captureConfigureReceipt(parsed);
173
+ if (
174
+ !equal(parsed, {
175
+ event: "receipt",
176
+ stage: "configure",
177
+ request: receipt.request,
178
+ receipt,
179
+ })
180
+ )
181
+ configureConflict();
182
+ }
@@ -0,0 +1,96 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import {
3
+ canonicalConfigure,
4
+ configureAcknowledgementFixture,
5
+ } from "./configureJournal.fixtures.js";
6
+
7
+ afterEach(() => vi.unstubAllGlobals());
8
+ it.each([
9
+ "body",
10
+ "binding",
11
+ "transport",
12
+ "request",
13
+ "foreign-C3",
14
+ "extra-field",
15
+ ])("refuses changed %s receipt DATA before first retention", async (change) => {
16
+ const h = await configureAcknowledgementFixture();
17
+ const receipt = JSON.parse(h.retainArgs.receiptJSON);
18
+ if (change === "body")
19
+ receipt.providerSnapshot.futureResponse = { changed: true };
20
+ if (change === "binding")
21
+ receipt.binding = "renewal-configure-receipt:v2:sha256:" + "0".repeat(64);
22
+ if (change === "transport") receipt.transport.requestId = "req_foreign";
23
+ if (change === "request") receipt.request.key += ":foreign";
24
+ if (change === "extra-field") receipt.future = "unowned";
25
+ const receiptJSON =
26
+ change === "foreign-C3"
27
+ ? canonicalConfigure(h.record.configureRecord.attachReceipt)
28
+ : canonicalConfigure(receipt);
29
+ await expect(
30
+ h.t.mutation(h.retain, { ...h.retainArgs, receiptJSON }),
31
+ ).rejects.toThrow("RENEWAL_JOURNAL_CONFLICT");
32
+ expect(
33
+ await h.t.query(h.readAcknowledgement, {
34
+ operationId: h.created.operationId,
35
+ }),
36
+ ).toBeNull();
37
+ expect(
38
+ (
39
+ await h.t.query(h.readAttempt, {
40
+ operationId: h.created.operationId,
41
+ attemptId: h.acquired.reservation.attemptId,
42
+ })
43
+ ).events,
44
+ ).toHaveLength(2);
45
+ expect(h.fetches()).toBe(0);
46
+ });
47
+ it.each(["receipt-payload", "basis-origin", "parent-reference"])(
48
+ "corrupt %s makes bounded configure reads refuse without repair",
49
+ async (change) => {
50
+ const h = await configureAcknowledgementFixture();
51
+ const retained = await h.t.mutation(h.retain, h.retainArgs);
52
+ await h.t.run(async (ctx) => {
53
+ if (change === "receipt-payload")
54
+ await ctx.db.patch(
55
+ "renewalOperationPayloads",
56
+ retained.receiptPayloadId,
57
+ { json: "{}" },
58
+ );
59
+ if (change === "basis-origin") {
60
+ const row = await ctx.db.get(
61
+ "renewalOperations",
62
+ h.created.operationId,
63
+ );
64
+ if (!row?.initialBasis) throw Error("MISSING_FIXTURE_BASIS");
65
+ const basis = JSON.parse(row.initialBasis.basisJSON);
66
+ basis.startWallMs++;
67
+ basis.startNsDecimal = String(BigInt(basis.startNsDecimal) + 1000000n);
68
+ await ctx.db.patch("renewalOperations", row._id, {
69
+ initialBasis: {
70
+ ...row.initialBasis,
71
+ basisJSON: canonicalConfigure(basis),
72
+ },
73
+ });
74
+ }
75
+ if (change === "parent-reference")
76
+ await ctx.db.patch("renewalOperations", h.parentCreated.operationId, {
77
+ activeAttemptId: h.acquired.reservation.attemptId,
78
+ });
79
+ });
80
+ const before = await h.t.run((ctx) =>
81
+ ctx.db.query("renewalOperations").take(3),
82
+ );
83
+ await expect(
84
+ h.t.query(h.load, { operationId: h.created.operationId }),
85
+ ).rejects.toThrow();
86
+ await expect(
87
+ h.t.query(h.readAcknowledgement, { operationId: h.created.operationId }),
88
+ ).rejects.toThrow("RENEWAL_JOURNAL_CONFLICT");
89
+ await expect(h.t.mutation(h.retain, h.retainArgs)).rejects.toThrow(
90
+ "RENEWAL_JOURNAL_CONFLICT",
91
+ );
92
+ expect(
93
+ await h.t.run((ctx) => ctx.db.query("renewalOperations").take(3)),
94
+ ).toEqual(before);
95
+ },
96
+ );
@@ -0,0 +1,77 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import {
3
+ canonicalConfigure,
4
+ configureComponentFixture,
5
+ } from "./configureJournal.fixtures.js";
6
+
7
+ afterEach(() => vi.unstubAllGlobals());
8
+ async function reserveConfigure() {
9
+ const h = await configureComponentFixture();
10
+ const created = await h.t.mutation(h.create, h.createArgs);
11
+ expect(created.status).toBe("created");
12
+ const reserveArgs = {
13
+ operationId: created.operationId,
14
+ expectedRevision: 0,
15
+ stage: "configure",
16
+ expectedRequestBinding: h.request.requestIdentity.binding,
17
+ recordJSON: h.createArgs.recordJSON,
18
+ };
19
+ const acquired = await h.t.mutation(h.reserve, reserveArgs);
20
+ expect(acquired.disposition).toBe("acquired");
21
+ return {
22
+ ...h,
23
+ created,
24
+ acquired,
25
+ reserveArgs,
26
+ bindArgs: {
27
+ operationId: created.operationId,
28
+ attemptId: acquired.reservation.attemptId,
29
+ policyJSON: canonicalConfigure(h.record.timingPolicy),
30
+ basisJSON: canonicalConfigure(h.basis),
31
+ },
32
+ };
33
+ }
34
+ it("reserves a distinct configure attempt and binds exactly the parent basis with configure stage", async () => {
35
+ const h = await reserveConfigure();
36
+ expect(h.acquired.reservation.stage).toBe("configure");
37
+ expect(h.acquired.reservation.attemptId).not.toBe(
38
+ h.record.parentAttachment.attemptId,
39
+ );
40
+ expect((await h.t.mutation(h.reserve, h.reserveArgs)).disposition).toBe(
41
+ "existing",
42
+ );
43
+ const bound = await h.t.mutation(h.bind, h.bindArgs);
44
+ expect(bound.initialBasis).toEqual({
45
+ version: 1,
46
+ stage: "configure",
47
+ attemptId: h.acquired.reservation.attemptId,
48
+ sourcePayloadId: h.acquired.reservation.payloadId,
49
+ policyJSON: h.bindArgs.policyJSON,
50
+ basisJSON: h.bindArgs.basisJSON,
51
+ });
52
+ expect(await h.t.mutation(h.bind, h.bindArgs)).toEqual(bound);
53
+ expect(
54
+ (await h.t.query(h.load, { operationId: h.created.operationId }))
55
+ .initialBasis,
56
+ ).toEqual(bound.initialBasis);
57
+ expect(h.fetches()).toBe(0);
58
+ });
59
+ it("refuses a mathematically valid shifted basis instead of renewing the parent origin", async () => {
60
+ const h = await reserveConfigure();
61
+ const shifted = {
62
+ ...h.basis,
63
+ startWallMs: h.basis.startWallMs + 1,
64
+ startNsDecimal: String(BigInt(h.basis.startNsDecimal) + 1000000n),
65
+ };
66
+ await expect(
67
+ h.t.mutation(h.bind, {
68
+ ...h.bindArgs,
69
+ basisJSON: canonicalConfigure(shifted),
70
+ }),
71
+ ).rejects.toThrow("RENEWAL_JOURNAL_CONFLICT");
72
+ expect(
73
+ (await h.t.query(h.load, { operationId: h.created.operationId }))
74
+ .initialBasis,
75
+ ).toBeNull();
76
+ expect(h.fetches()).toBe(0);
77
+ });
@@ -0,0 +1,75 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import {
3
+ canonicalConfigure,
4
+ configureComponentFixture,
5
+ } from "./configureJournal.fixtures.js";
6
+
7
+ afterEach(() => vi.unstubAllGlobals());
8
+ it("creates a separately authorized V2 configure operation only after its complete parent attachment DATA", async () => {
9
+ const h = await configureComponentFixture();
10
+ const created = await h.t.mutation(h.create, h.createArgs);
11
+ expect(created.status).toBe("created");
12
+ expect(created.operationId).not.toBe(h.parentCreated.operationId);
13
+ expect(await h.t.query(h.load, { operationId: created.operationId })).toEqual(
14
+ {
15
+ recordVersion: 2,
16
+ operationId: created.operationId,
17
+ recordJSON: h.createArgs.recordJSON,
18
+ revision: 0,
19
+ activeAttempt: null,
20
+ initialBasis: null,
21
+ },
22
+ );
23
+ expect(h.fetches()).toBe(0);
24
+ });
25
+
26
+ it.each([
27
+ "missing-parent",
28
+ "wrong-attempt",
29
+ "wrong-source",
30
+ "changed-bundle",
31
+ "extended-cutoff",
32
+ "shortened-cutoff",
33
+ "unpublished-parent",
34
+ ])(
35
+ "rejects configure parent-link %s before inserting child DATA",
36
+ async (scenario) => {
37
+ const h = await configureComponentFixture(
38
+ scenario !== "unpublished-parent",
39
+ );
40
+ const raw = JSON.parse(h.createArgs.recordJSON);
41
+ switch (scenario) {
42
+ case "missing-parent":
43
+ raw.parentAttachment.operationId = "missing-parent-operation";
44
+ break;
45
+ case "wrong-attempt":
46
+ raw.parentAttachment.attemptId = h.parentCreated.operationId;
47
+ break;
48
+ case "wrong-source":
49
+ raw.parentAttachment.sourcePayloadId = h.parentCreated.operationId;
50
+ break;
51
+ case "changed-bundle":
52
+ raw.configureRecord.originalBundle.future = ["changed"];
53
+ break;
54
+ case "extended-cutoff":
55
+ raw.timingPolicy.workEndWallMs++;
56
+ break;
57
+ case "shortened-cutoff":
58
+ raw.timingPolicy.workEndWallMs--;
59
+ break;
60
+ }
61
+ const before = await h.t.run((ctx) =>
62
+ ctx.db.query("renewalOperations").take(3),
63
+ );
64
+ await expect(
65
+ h.t.mutation(h.create, {
66
+ ...h.createArgs,
67
+ recordJSON: canonicalConfigure(raw),
68
+ }),
69
+ ).rejects.toThrow("RENEWAL_JOURNAL_CONFLICT");
70
+ expect(
71
+ await h.t.run((ctx) => ctx.db.query("renewalOperations").take(3)),
72
+ ).toEqual(before);
73
+ expect(h.fetches()).toBe(0);
74
+ },
75
+ );