@fantastic.dev/better-stripe 0.106.0 → 0.108.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 (42) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/observation/history.d.ts +41 -0
  4. package/dist/client/billing/renewalAttach/internal/observation/history.d.ts.map +1 -0
  5. package/dist/client/billing/renewalAttach/internal/observation/history.js +161 -0
  6. package/dist/client/billing/renewalAttach/internal/observation/history.js.map +1 -0
  7. package/dist/client/billing/renewalAttach/internal/observation/historyEvents.d.ts +55 -0
  8. package/dist/client/billing/renewalAttach/internal/observation/historyEvents.d.ts.map +1 -0
  9. package/dist/client/billing/renewalAttach/internal/observation/historyEvents.js +83 -0
  10. package/dist/client/billing/renewalAttach/internal/observation/historyEvents.js.map +1 -0
  11. package/dist/client/billing/renewalAttach/internal/observation/reader.d.ts +64 -0
  12. package/dist/client/billing/renewalAttach/internal/observation/reader.d.ts.map +1 -0
  13. package/dist/client/billing/renewalAttach/internal/observation/reader.js +170 -0
  14. package/dist/client/billing/renewalAttach/internal/observation/reader.js.map +1 -0
  15. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts +44 -0
  16. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts.map +1 -0
  17. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js +145 -0
  18. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js.map +1 -0
  19. package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.d.ts +2 -0
  20. package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.d.ts.map +1 -1
  21. package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.js +1 -0
  22. package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.js.map +1 -1
  23. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +2 -0
  24. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -1
  25. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +9 -0
  26. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -1
  27. package/docs/renewal-context-reads.md +87 -0
  28. package/package.json +1 -1
  29. package/src/client/billing/renewalAttach/internal/observation/history.integration.test.ts +79 -0
  30. package/src/client/billing/renewalAttach/internal/observation/history.ts +200 -0
  31. package/src/client/billing/renewalAttach/internal/observation/historyControls.integration.test.ts +238 -0
  32. package/src/client/billing/renewalAttach/internal/observation/historyEvents.ts +114 -0
  33. package/src/client/billing/renewalAttach/internal/observation/reader.integration.test.ts +93 -0
  34. package/src/client/billing/renewalAttach/internal/observation/reader.ts +241 -0
  35. package/src/client/billing/renewalAttach/internal/observation/readerAuthorization.ts +179 -0
  36. package/src/client/billing/renewalAttach/internal/observation/readerControls.integration.test.ts +553 -0
  37. package/src/client/billing/renewalAttach/internal/operations/sourceOperations.ts +3 -0
  38. package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +12 -0
  39. package/src/client/billing/renewalAttach/internal/test-fixtures/historySnapshot.ts +47 -0
  40. package/src/client/billing/renewalAttach/internal/test-fixtures/observationReader.ts +100 -0
  41. package/src/client/billing/renewalAttach/internal/test-fixtures/producerConfigureAdmission.ts +9 -0
  42. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +5 -1
@@ -0,0 +1,200 @@
1
+ import { rejectProxyJSON } from "../acquisition/rawWireSource.js";
2
+ import { renewalSnapshot } from "../../../renewalReplacementSnapshot.js";
3
+ import { closed, freeze, sevenWire } from "../c10/ownData.js";
4
+ import { canonicalInitialBasis } from "../../../../../shared/renewalOperationJournal.js";
5
+ import {
6
+ reconstructConfigurationV2,
7
+ constructRenewalConfigurationReceiptV2,
8
+ } from "../../../renewalV2/index.js";
9
+ import { captureObservationRecord, verifyObservationRecord } from "./record.js";
10
+ import { admitConfigureACK } from "../configure/output.js";
11
+ import {
12
+ checkOperationLoad,
13
+ matchConfigureDATA,
14
+ object,
15
+ text,
16
+ requireConfigure as need,
17
+ } from "../producer/configureJournalData.js";
18
+ import {
19
+ captureHistoryStage,
20
+ verifyHistoryEvents,
21
+ verifyHistoryRetention,
22
+ } from "./historyEvents.js";
23
+
24
+ /** Pure retained DATA. No Source, actual returned ACK, authorization or execution grant. */
25
+ export async function captureRetainedObservationHistory(raw: unknown) {
26
+ need(arguments.length === 1);
27
+ // Capture the complete graph before the first await, including opaque JSON strings.
28
+ rejectProxyJSON(raw);
29
+ const copy = renewalSnapshot(raw);
30
+ need(new TextEncoder().encode(JSON.stringify(copy)).length <= 500000);
31
+ matchConfigureDATA(raw, copy);
32
+ freeze(copy);
33
+ const snapshot = closed(copy, ["parent", "configure"]);
34
+ const parent = captureHistoryStage(snapshot.parent, "attach");
35
+ const child = captureHistoryStage(snapshot.configure, "configure");
36
+ need(
37
+ parent.reservation.operationId !== child.reservation.operationId &&
38
+ parent.reservation.attemptId !== child.reservation.attemptId,
39
+ );
40
+ const original = object(JSON.parse(child.reservation.recordJSON));
41
+ const parentOriginal = object(JSON.parse(parent.reservation.recordJSON));
42
+ const captured = captureObservationRecord(original.configureRecord);
43
+ const record = captured.record;
44
+ const rebuilt = await reconstructConfigurationV2(record.configuration);
45
+ need(rebuilt.material && "requestIdentity" in rebuilt.result);
46
+ verifyObservationRecord(captured, rebuilt.result, rebuilt.material.receipt);
47
+ const receipt = rebuilt.material.receipt,
48
+ request = rebuilt.result;
49
+ matchConfigureDATA(original.parentAttachment, {
50
+ operationId: parent.reservation.operationId,
51
+ attemptId: parent.reservation.attemptId,
52
+ sourcePayloadId: parent.reservation.payloadId,
53
+ receiptBinding: receipt.binding,
54
+ });
55
+ matchConfigureDATA(
56
+ record.configuration.attachReceipt.attachRequest,
57
+ parentOriginal.attachRequest,
58
+ );
59
+ matchConfigureDATA(record.originalBundle, parentOriginal.originalBundle);
60
+ matchConfigureDATA(original.account, parentOriginal.account);
61
+ matchConfigureDATA(original.account, {
62
+ mode: "platform",
63
+ stripeAccount: null,
64
+ livemode: receipt.livemode,
65
+ });
66
+ need(
67
+ original.apiVersion === parentOriginal.apiVersion &&
68
+ original.apiVersion === request.apiVersion,
69
+ );
70
+ matchConfigureDATA(original.dispatch, sevenWire(request));
71
+ matchConfigureDATA(parentOriginal.dispatch, sevenWire(receipt.request));
72
+ need(
73
+ child.reservation.requestBinding === request.requestIdentity.binding &&
74
+ parent.reservation.requestBinding ===
75
+ receipt.request.requestIdentity.binding,
76
+ );
77
+ const policy = object(original.timingPolicy),
78
+ parentPolicy = object(parentOriginal.timingPolicy);
79
+ for (const key of ["workEndWallMs", "endWallMs", "periodStart", "periodEnd"])
80
+ need(policy[key] === parentPolicy[key]);
81
+ const parentLoad = object(parent.operation),
82
+ basis = object(parentLoad.initialBasis);
83
+ const basisJSON = text(basis.basisJSON);
84
+ need(
85
+ canonicalInitialBasis(parent.reservation.timingPolicyJSON, basisJSON)
86
+ .basisJSON === basisJSON,
87
+ );
88
+ need(
89
+ canonicalInitialBasis(child.reservation.timingPolicyJSON, basisJSON)
90
+ .basisJSON === basisJSON,
91
+ );
92
+ checkOperationLoad(parent.operation, parent.reservation, basisJSON);
93
+ checkOperationLoad(
94
+ child.operation,
95
+ child.reservation,
96
+ basisJSON,
97
+ child.events.length === 0,
98
+ );
99
+ await verifyHistoryEvents(parent, [
100
+ { event: "dispatch", stage: "attach", request: receipt.request },
101
+ {
102
+ event: "response",
103
+ stage: "attach",
104
+ request: receipt.request,
105
+ body: receipt.providerSnapshot,
106
+ status: parent.events[1].data.status,
107
+ },
108
+ { event: "receipt", stage: "attach", request: receipt.request, receipt },
109
+ ]);
110
+ const attachmentRetention = verifyHistoryRetention(parent, receipt);
111
+ const expected: unknown[] = [];
112
+ if (child.events.length >= 1)
113
+ expected.push({ event: "dispatch", stage: "configure", request });
114
+ const response = child.events[1]?.data;
115
+ if (response)
116
+ expected.push({
117
+ event: "response",
118
+ stage: "configure",
119
+ request,
120
+ body: response.body,
121
+ transport: response.transport,
122
+ });
123
+ let configureReceipt = null,
124
+ rawConfigureReceipt = null,
125
+ configureRetention = null;
126
+ if (response) {
127
+ const t = closed(response.transport, ["status"], ["requestId"]);
128
+ need(typeof t.status === "number");
129
+ need(t.requestId === undefined || typeof t.requestId === "string");
130
+ const rawResponse = {
131
+ configuration: record.configuration,
132
+ actualDispatch: sevenWire(request),
133
+ providerSnapshot: object(response.body),
134
+ transport: {
135
+ status: t.status,
136
+ ...(typeof t.requestId === "string" ? { requestId: t.requestId } : {}),
137
+ },
138
+ };
139
+ const reconstructed =
140
+ await constructRenewalConfigurationReceiptV2(rawResponse);
141
+ admitConfigureACK(record, rawResponse, reconstructed);
142
+ // Response2 may be valid without durable C9. Reconstruction does not retain it.
143
+ if (child.acknowledgement !== null) {
144
+ rawConfigureReceipt = rawResponse;
145
+ configureReceipt = reconstructed;
146
+ configureRetention = verifyHistoryRetention(child, configureReceipt);
147
+ }
148
+ } else need(child.acknowledgement === null);
149
+ if (child.events.length === 3) {
150
+ need(configureReceipt);
151
+ expected.push({
152
+ event: "receipt",
153
+ stage: "configure",
154
+ request,
155
+ receipt: configureReceipt,
156
+ });
157
+ }
158
+ await verifyHistoryEvents(child, expected);
159
+ // IDs remain opaque DATA. Even equal complete records never become runtime tokens.
160
+ const payloads = [
161
+ parent.reservation.payloadId,
162
+ child.reservation.payloadId,
163
+ ...parent.events.map((e) => e.descriptor.payloadId),
164
+ ...child.events.map((e) => e.descriptor.payloadId),
165
+ object(attachmentRetention.retention).receiptPayloadId,
166
+ ...(configureRetention
167
+ ? [object(configureRetention.retention).receiptPayloadId]
168
+ : []),
169
+ ];
170
+ const eventIds = [...parent.events, ...child.events].map(
171
+ (e) => e.descriptor.eventId,
172
+ );
173
+ need(
174
+ new Set(payloads).size === payloads.length &&
175
+ new Set(eventIds).size === eventIds.length,
176
+ );
177
+ const ids = [
178
+ parent.reservation.operationId,
179
+ child.reservation.operationId,
180
+ parent.reservation.attemptId,
181
+ child.reservation.attemptId,
182
+ ...payloads,
183
+ ...eventIds,
184
+ ];
185
+ need(new Set(ids).size === ids.length);
186
+ return freeze({
187
+ status: "retained_observation_history_data" as const,
188
+ record,
189
+ configureHead: child.events.length,
190
+ configureReceipt,
191
+ rawConfigureReceipt,
192
+ attachmentReceipt: receipt,
193
+ attachmentRetention,
194
+ configureRetention,
195
+ snapshot: { parent: snapshot.parent, configure: snapshot.configure },
196
+ operationalAuthority: false as const,
197
+ authenticationEstablished: false as const,
198
+ publicDeliveryEstablished: false as const,
199
+ });
200
+ }
@@ -0,0 +1,238 @@
1
+ import { expect } from "vitest";
2
+ import { configureAdmissionCase } from "../test-fixtures/producerConfigureAdmission.js";
3
+ import {
4
+ historySnapshot,
5
+ historyPrefix,
6
+ type HistorySnapshot,
7
+ } from "../test-fixtures/historySnapshot.js";
8
+ import { captureRetainedObservationHistory } from "./history.js";
9
+ import { canonicalJournalEvent } from "../../../../../shared/renewalOperationJournal.js";
10
+ import { renewalSnapshot } from "../../../renewalReplacementSnapshot.js";
11
+
12
+ // Mutable controlled fixture DATA only, never the detached production reader.
13
+ function object(value: unknown): Record<string, unknown> {
14
+ if (!value || typeof value !== "object" || Array.isArray(value))
15
+ throw Error("FIXTURE_OBJECT");
16
+ return value as Record<string, unknown>;
17
+ }
18
+
19
+ function rewriteRecord(
20
+ s: HistorySnapshot,
21
+ change: (r: Record<string, unknown>) => void,
22
+ ) {
23
+ const record = object(JSON.parse(s.configure.operation.recordJSON));
24
+ change(record);
25
+ const json = JSON.stringify(renewalSnapshot(record));
26
+ s.configure.operation.recordJSON = json;
27
+ s.configure.attempt.reservation.recordJSON = json;
28
+ }
29
+ async function rewriteEvent(
30
+ s: HistorySnapshot,
31
+ change: (r: Record<string, unknown>) => void,
32
+ ) {
33
+ const event = s.configure.events[1]!;
34
+ const data = object(JSON.parse(event!.dataJSON));
35
+ change(data);
36
+ const canonical = await canonicalJournalEvent(2, JSON.stringify(data));
37
+ Object.assign(event!, {
38
+ binding: canonical.binding,
39
+ dataJSON: canonical.json,
40
+ });
41
+ s.configure.attempt.events[1].binding = canonical.binding;
42
+ }
43
+
44
+ configureAdmissionCase(
45
+ "rejects inconsistent full history, response, C9 and immutable parent relations",
46
+ historySnapshot,
47
+ async (_h, full) => {
48
+ const controls: [string, (s: HistorySnapshot) => void | Promise<void>][] = [
49
+ [
50
+ "foreign parent",
51
+ (s) =>
52
+ rewriteRecord(s, (r) => {
53
+ object(r.parentAttachment).operationId = "foreign";
54
+ }),
55
+ ],
56
+ [
57
+ "raw original material",
58
+ (s) =>
59
+ rewriteRecord(s, (r) => {
60
+ object(
61
+ object(object(r.configureRecord).originalBundle).wireTargetPrice,
62
+ ).metadata = { changed: true };
63
+ }),
64
+ ],
65
+ [
66
+ "account",
67
+ (s) =>
68
+ rewriteRecord(s, (r) => {
69
+ object(r.account).livemode = true;
70
+ }),
71
+ ],
72
+ [
73
+ "deadline",
74
+ (s) =>
75
+ rewriteRecord(s, (r) => {
76
+ const p = object(r.timingPolicy);
77
+ p.endWallMs = Number(p.endWallMs) + 1;
78
+ }),
79
+ ],
80
+ [
81
+ "basis",
82
+ (s) => {
83
+ object(object(s.configure.operation).initialBasis).basisJSON = "{}";
84
+ },
85
+ ],
86
+ [
87
+ "missing bound basis",
88
+ (s) => {
89
+ Object.assign(s.configure.operation, { initialBasis: null });
90
+ },
91
+ ],
92
+ [
93
+ "descriptor mismatch",
94
+ (s) => {
95
+ s.configure.attempt.events[0].eventId = "other-event";
96
+ },
97
+ ],
98
+ [
99
+ "binding mismatch",
100
+ (s) => {
101
+ s.configure.events[0]!.binding = "wrong";
102
+ s.configure.attempt.events[0].binding = "wrong";
103
+ },
104
+ ],
105
+ [
106
+ "response request",
107
+ async (s) =>
108
+ rewriteEvent(s, (r) => {
109
+ object(r.request).key = "different-key";
110
+ }),
111
+ ],
112
+ [
113
+ "response body",
114
+ async (s) =>
115
+ rewriteEvent(s, (r) => {
116
+ object(r.body).id = "sub_sched_foreign";
117
+ }),
118
+ ],
119
+ [
120
+ "contradictory C9",
121
+ (s) => {
122
+ const ack = s.configure.acknowledgement!;
123
+ const r = JSON.parse(ack.receiptJSON);
124
+ r.providerSnapshot.metadata = { changed: true };
125
+ ack.receiptJSON = JSON.stringify(renewalSnapshot(r));
126
+ },
127
+ ],
128
+ [
129
+ "missing head3 retention",
130
+ (s) => {
131
+ s.configure.acknowledgement = null;
132
+ },
133
+ ],
134
+ [
135
+ "retention source alias",
136
+ (s) => {
137
+ s.configure.acknowledgement!.retention.receiptPayloadId =
138
+ s.configure.attempt.reservation.payloadId;
139
+ },
140
+ ],
141
+ [
142
+ "retention event alias",
143
+ (s) => {
144
+ s.configure.acknowledgement!.retention.receiptPayloadId =
145
+ s.configure.attempt.events[2].payloadId;
146
+ },
147
+ ],
148
+ [
149
+ "retention event identity alias",
150
+ (s) => {
151
+ s.configure.acknowledgement!.retention.receiptPayloadId =
152
+ s.configure.attempt.events[1].eventId;
153
+ },
154
+ ],
155
+ [
156
+ "parent receipt alias",
157
+ (s) => {
158
+ s.parent.acknowledgement!.retention.receiptPayloadId =
159
+ s.parent.attempt.reservation.payloadId;
160
+ },
161
+ ],
162
+ [
163
+ "cross stage payload alias",
164
+ (s) => {
165
+ s.configure.acknowledgement!.retention.receiptPayloadId =
166
+ s.parent.acknowledgement!.retention.receiptPayloadId;
167
+ },
168
+ ],
169
+ [
170
+ "parent event missing",
171
+ (s) => {
172
+ s.parent.events.pop();
173
+ s.parent.attempt.events.pop();
174
+ },
175
+ ],
176
+ [
177
+ "extra stage key",
178
+ (s) => {
179
+ Object.assign(s.configure, { callback: null });
180
+ },
181
+ ],
182
+ ];
183
+ for (const [name, change] of controls) {
184
+ const s = structuredClone(full);
185
+ await change(s);
186
+ let refused = false;
187
+ try {
188
+ await captureRetainedObservationHistory(s);
189
+ } catch {
190
+ refused = true;
191
+ }
192
+ expect(refused, name).toBe(true);
193
+ }
194
+ const absent = historyPrefix(full, 2);
195
+ await rewriteEvent(absent, (r) => {
196
+ object(r.body).id = "sub_sched_foreign";
197
+ });
198
+ await expect(
199
+ captureRetainedObservationHistory(absent),
200
+ "foreign response without retained C9",
201
+ ).rejects.toThrow();
202
+ },
203
+ );
204
+
205
+ configureAdmissionCase(
206
+ "rejects proxy traps, getters, hidden keys, sparse arrays and oversized graphs before awaits",
207
+ historySnapshot,
208
+ async (_h, full) => {
209
+ let touched = 0;
210
+ const proxy = new Proxy(full, {
211
+ ownKeys() {
212
+ touched++;
213
+ throw Error("TRAP");
214
+ },
215
+ });
216
+ await expect(captureRetainedObservationHistory(proxy)).rejects.toThrow();
217
+ expect(touched).toBe(0);
218
+ const getter = { ...full };
219
+ Object.defineProperty(getter, "parent", {
220
+ enumerable: true,
221
+ get() {
222
+ touched++;
223
+ return full.parent;
224
+ },
225
+ });
226
+ await expect(captureRetainedObservationHistory(getter)).rejects.toThrow();
227
+ expect(touched).toBe(0);
228
+ const hidden = structuredClone(full);
229
+ Object.defineProperty(hidden.configure, "secret", { value: true });
230
+ await expect(captureRetainedObservationHistory(hidden)).rejects.toThrow();
231
+ const sparse = structuredClone(full);
232
+ delete sparse.configure.events[0];
233
+ await expect(captureRetainedObservationHistory(sparse)).rejects.toThrow();
234
+ const huge = structuredClone(full);
235
+ huge.configure.operation.recordJSON = "x".repeat(500001);
236
+ await expect(captureRetainedObservationHistory(huge)).rejects.toThrow();
237
+ },
238
+ );
@@ -0,0 +1,114 @@
1
+ import { canonicalJournalEvent } from "../../../../../shared/renewalOperationJournal.js";
2
+ import { renewalSnapshot } from "../../../renewalReplacementSnapshot.js";
3
+ import { closed, freeze } from "../c10/ownData.js";
4
+ import {
5
+ captureAttachDispatch,
6
+ captureAttachResponse,
7
+ captureAttachReceipt,
8
+ captureConfigureDispatch,
9
+ captureConfigureResponse,
10
+ captureConfigureReceipt,
11
+ } from "../c10/eventCapture.js";
12
+ import {
13
+ captureOperationalReservation,
14
+ captureParentHistory,
15
+ captureConfigureHistory,
16
+ matchConfigureDATA,
17
+ object,
18
+ text,
19
+ requireConfigure as need,
20
+ } from "../producer/configureJournalData.js";
21
+
22
+ /** Complete bounded query DATA, never a class journal or actual ACK. */
23
+ export function captureHistoryStage(
24
+ raw: unknown,
25
+ stage: "attach" | "configure",
26
+ ) {
27
+ const x = closed(raw, ["operation", "attempt", "events", "acknowledgement"]);
28
+ const attempt = closed(x.attempt, ["reservation", "events"]);
29
+ const reservation = captureOperationalReservation(attempt.reservation, stage);
30
+ need(Array.isArray(attempt.events) && Array.isArray(x.events));
31
+ const count = attempt.events.length;
32
+ need(count <= 3 && x.events.length === count);
33
+ const descriptors =
34
+ stage === "attach"
35
+ ? captureParentHistory(x.attempt, reservation)
36
+ : count === 0
37
+ ? (matchConfigureDATA(x.attempt, { reservation, events: [] }), [])
38
+ : captureConfigureHistory(x.attempt, reservation, count);
39
+ const allPayloads = [
40
+ reservation.payloadId,
41
+ ...descriptors.map((e) => e.payloadId),
42
+ ];
43
+ need(new Set(allPayloads).size === allPayloads.length);
44
+ const events = x.events.map((raw, index) => {
45
+ const event = closed(raw, [...Object.keys(descriptors[index]), "dataJSON"]);
46
+ const dataJSON = text(event.dataJSON);
47
+ need(dataJSON.length <= 500000);
48
+ const parsed: unknown = JSON.parse(dataJSON);
49
+ const captures =
50
+ stage === "attach"
51
+ ? [captureAttachDispatch, captureAttachResponse, captureAttachReceipt]
52
+ : [
53
+ captureConfigureDispatch,
54
+ captureConfigureResponse,
55
+ captureConfigureReceipt,
56
+ ];
57
+ captures[index](parsed);
58
+ matchConfigureDATA(raw, { ...descriptors[index], dataJSON });
59
+ return { descriptor: descriptors[index], dataJSON, data: object(parsed) };
60
+ });
61
+ return {
62
+ operation: x.operation,
63
+ reservation,
64
+ events,
65
+ acknowledgement: x.acknowledgement,
66
+ };
67
+ }
68
+ export async function verifyHistoryEvents(
69
+ stage: ReturnType<typeof captureHistoryStage>,
70
+ expected: readonly unknown[],
71
+ ) {
72
+ need(stage.events.length === expected.length);
73
+ for (let index = 0; index < expected.length; index++) {
74
+ const event = stage.events[index];
75
+ matchConfigureDATA(event.data, expected[index]);
76
+ const canonical = await canonicalJournalEvent(index + 1, event.dataJSON);
77
+ need(
78
+ canonical.json === event.dataJSON &&
79
+ canonical.binding === event.descriptor.binding,
80
+ );
81
+ }
82
+ }
83
+ export function verifyHistoryRetention(
84
+ stage: ReturnType<typeof captureHistoryStage>,
85
+ receipt: unknown,
86
+ ) {
87
+ need(stage.events.length >= 2);
88
+ const ack = closed(stage.acknowledgement, ["retention", "receiptJSON"]);
89
+ const retention = object(ack.retention),
90
+ r = object(receipt);
91
+ const receiptPayloadId = text(retention.receiptPayloadId);
92
+ need(
93
+ receiptPayloadId !== stage.reservation.payloadId &&
94
+ stage.events.every((e) => e.descriptor.payloadId !== receiptPayloadId),
95
+ );
96
+ const [dispatch, response] = stage.events;
97
+ matchConfigureDATA(JSON.parse(text(ack.receiptJSON)), receipt);
98
+ need(JSON.stringify(renewalSnapshot(receipt)) === ack.receiptJSON);
99
+ matchConfigureDATA(ack.retention, {
100
+ status: "retained",
101
+ operationId: stage.reservation.operationId,
102
+ schemaVersion: 1,
103
+ stage: stage.reservation.stage,
104
+ attemptId: stage.reservation.attemptId,
105
+ sourcePayloadId: stage.reservation.payloadId,
106
+ dispatchEventId: dispatch.descriptor.eventId,
107
+ dispatchPayloadId: dispatch.descriptor.payloadId,
108
+ responseEventId: response.descriptor.eventId,
109
+ responsePayloadId: response.descriptor.payloadId,
110
+ receiptPayloadId,
111
+ receiptBinding: r.binding,
112
+ });
113
+ return freeze({ retention: ack.retention, receiptJSON: ack.receiptJSON });
114
+ }
@@ -0,0 +1,93 @@
1
+ import { expect, it, vi } from "vitest";
2
+ import {
3
+ configureAdmissionCase,
4
+ admissionControls,
5
+ } from "../test-fixtures/producerConfigureAdmission.js";
6
+ import {
7
+ prepareObservationReader,
8
+ observerIdentity,
9
+ authorizeObservation,
10
+ readerControls,
11
+ } from "../test-fixtures/observationReader.js";
12
+ import { captureAuthorizedObservationHistoryReader } from "./reader.js";
13
+ configureAdmissionCase(
14
+ "fresh authenticated query-only reader survives original write retirement and period boundary",
15
+ prepareObservationReader,
16
+ async (h, state) => {
17
+ const calls = h.calls.length,
18
+ selectors = admissionControls.selectors;
19
+ await h.inAnotherAction(async (ctx) => {
20
+ const reader = await captureAuthorizedObservationHistoryReader(h.sdk, {
21
+ actualActionCtx: ctx,
22
+ operationId: state.operationId,
23
+ parentOperationId: state.parentOperationId,
24
+ });
25
+ expect(Object.keys(reader)).toEqual(["read"]);
26
+ const output = await reader.read();
27
+ expect(output.status).toBe("authorized_observation_history_data");
28
+ expect(output.history.snapshot).toEqual(state.full);
29
+ expect(output.authorization.authorizationRevision).toBe(3);
30
+ expect(output.operationalAuthority).toBe(false);
31
+ expect(Object.isFrozen(output.history.record)).toBe(true);
32
+ await expect(reader.read()).rejects.toThrow();
33
+ });
34
+ expect(readerControls.authorizations).toBe(2);
35
+ expect(h.calls).toHaveLength(calls);
36
+ expect(admissionControls.selectors).toBe(selectors);
37
+ expect(admissionControls.legacy).toBe(0);
38
+ },
39
+ { identity: observerIdentity, hostQuery: authorizeObservation },
40
+ );
41
+ it("EMPTY observation history profile refuses before root/input/clock reflection", async () => {
42
+ admissionControls.history.splice(0);
43
+ let traps = 0;
44
+ const root = new Proxy(
45
+ {},
46
+ {
47
+ getPrototypeOf() {
48
+ traps++;
49
+ throw Error("TRAP");
50
+ },
51
+ },
52
+ );
53
+ const input = new Proxy(
54
+ {},
55
+ {
56
+ ownKeys() {
57
+ traps++;
58
+ throw Error("TRAP");
59
+ },
60
+ },
61
+ );
62
+ const wall = vi.spyOn(Date, "now"),
63
+ ns = vi.spyOn(process.hrtime, "bigint");
64
+ wall.mockClear();
65
+ ns.mockClear();
66
+ await expect(
67
+ captureAuthorizedObservationHistoryReader(root, input),
68
+ ).rejects.toThrow("OBSERVATION_HISTORY_READER_UNBOUND");
69
+ expect(traps).toBe(0);
70
+ expect(wall).not.toHaveBeenCalled();
71
+ expect(ns).not.toHaveBeenCalled();
72
+ });
73
+ configureAdmissionCase(
74
+ "reader consumes before concurrent calls and exposes only four fixed query routes",
75
+ prepareObservationReader,
76
+ async (h, state) => {
77
+ await h.inAnotherAction(async (ctx) => {
78
+ const queries = vi.spyOn(ctx, "runQuery"),
79
+ mutations = vi.spyOn(ctx, "runMutation");
80
+ const reader = await captureAuthorizedObservationHistoryReader(h.sdk, {
81
+ actualActionCtx: ctx,
82
+ operationId: state.operationId,
83
+ parentOperationId: state.parentOperationId,
84
+ });
85
+ const first = reader.read();
86
+ await expect(reader.read()).rejects.toThrow();
87
+ expect((await first).status).toBe("authorized_observation_history_data");
88
+ expect(mutations).not.toHaveBeenCalled();
89
+ expect(queries).toHaveBeenCalledTimes(14);
90
+ });
91
+ },
92
+ { identity: observerIdentity, hostQuery: authorizeObservation },
93
+ );