@fantastic.dev/better-stripe 0.111.0 → 0.113.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 (87) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/observation/freshActor.d.ts +564 -0
  4. package/dist/client/billing/renewalAttach/internal/observation/freshActor.d.ts.map +1 -1
  5. package/dist/client/billing/renewalAttach/internal/observation/freshActor.js +103 -21
  6. package/dist/client/billing/renewalAttach/internal/observation/freshActor.js.map +1 -1
  7. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.d.ts +18 -1
  8. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.d.ts.map +1 -1
  9. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.js +54 -1
  10. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.js.map +1 -1
  11. package/dist/client/billing/renewalAttach/internal/observation/invocation.d.ts +4 -0
  12. package/dist/client/billing/renewalAttach/internal/observation/invocation.d.ts.map +1 -1
  13. package/dist/client/billing/renewalAttach/internal/observation/invocation.js +1 -0
  14. package/dist/client/billing/renewalAttach/internal/observation/invocation.js.map +1 -1
  15. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts +4 -0
  16. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts.map +1 -1
  17. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js +6 -1
  18. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js.map +1 -1
  19. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.d.ts +38 -0
  20. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.d.ts.map +1 -0
  21. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.js +172 -0
  22. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.js.map +1 -0
  23. package/dist/client/billing/renewalAttach/internal/observation/witnessData.d.ts +135 -0
  24. package/dist/client/billing/renewalAttach/internal/observation/witnessData.d.ts.map +1 -0
  25. package/dist/client/billing/renewalAttach/internal/observation/witnessData.js +143 -0
  26. package/dist/client/billing/renewalAttach/internal/observation/witnessData.js.map +1 -0
  27. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.d.ts +85 -0
  28. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.d.ts.map +1 -0
  29. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.js +121 -0
  30. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.js.map +1 -0
  31. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.d.ts +44 -0
  32. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.d.ts.map +1 -0
  33. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.js +4 -0
  34. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.js.map +1 -0
  35. package/dist/client/billing/renewalAttach/internal/producer/configureJournalWork.d.ts +1 -1
  36. package/dist/client/billing/renewalAttach/internal/producer/configureLineage.d.ts +21 -21
  37. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.d.ts +3 -3
  38. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.d.ts +1 -1
  39. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
  40. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  41. package/dist/component/_generated/api.d.ts +24 -0
  42. package/dist/component/_generated/api.d.ts.map +1 -1
  43. package/dist/component/_generated/api.js.map +1 -1
  44. package/dist/component/_generated/component.d.ts +94 -10
  45. package/dist/component/_generated/component.d.ts.map +1 -1
  46. package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
  47. package/dist/component/balanceFunding/queries.d.ts +1 -1
  48. package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
  49. package/dist/component/renewalWitnesses/mutations.d.ts +45 -0
  50. package/dist/component/renewalWitnesses/mutations.d.ts.map +1 -0
  51. package/dist/component/renewalWitnesses/mutations.js +14 -0
  52. package/dist/component/renewalWitnesses/mutations.js.map +1 -0
  53. package/dist/component/renewalWitnesses/queries.d.ts +39 -0
  54. package/dist/component/renewalWitnesses/queries.d.ts.map +1 -0
  55. package/dist/component/renewalWitnesses/queries.js +11 -0
  56. package/dist/component/renewalWitnesses/queries.js.map +1 -0
  57. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  58. package/docs/renewal-example-authorization.md +74 -0
  59. package/docs/renewal-witness-protocol.md +61 -10
  60. package/package.json +1 -1
  61. package/src/client/billing/renewalAttach/internal/observation/freshActor.ts +142 -19
  62. package/src/client/billing/renewalAttach/internal/observation/freshTransport.ts +90 -1
  63. package/src/client/billing/renewalAttach/internal/observation/invocation.ts +2 -0
  64. package/src/client/billing/renewalAttach/internal/observation/readerAuthorization.ts +6 -1
  65. package/src/client/billing/renewalAttach/internal/observation/witnessAcquisition.integration.test.ts +348 -0
  66. package/src/client/billing/renewalAttach/internal/observation/witnessActor.integration.test.ts +204 -0
  67. package/src/client/billing/renewalAttach/internal/observation/witnessAuthorization.ts +239 -0
  68. package/src/client/billing/renewalAttach/internal/observation/witnessCapabilities.integration.test.ts +231 -0
  69. package/src/client/billing/renewalAttach/internal/observation/witnessData.ts +189 -0
  70. package/src/client/billing/renewalAttach/internal/observation/witnessHistorical.integration.test.ts +98 -0
  71. package/src/client/billing/renewalAttach/internal/observation/witnessImmutable.integration.test.ts +78 -0
  72. package/src/client/billing/renewalAttach/internal/observation/witnessInvoice.integration.test.ts +85 -0
  73. package/src/client/billing/renewalAttach/internal/observation/witnessLifecycle.integration.test.ts +264 -0
  74. package/src/client/billing/renewalAttach/internal/observation/witnessOperation.ts +205 -0
  75. package/src/client/billing/renewalAttach/internal/observation/witnessProfile.integration.test.ts +93 -0
  76. package/src/client/billing/renewalAttach/internal/observation/witnessRPCControls.integration.test.ts +256 -0
  77. package/src/client/billing/renewalAttach/internal/operations/witnessProfiles.ts +58 -0
  78. package/src/client/billing/renewalAttach/internal/test-fixtures/invoiceObservation.ts +3 -1
  79. package/src/client/billing/renewalAttach/internal/test-fixtures/producerConfigureAdmission.ts +8 -0
  80. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +28 -11
  81. package/src/client/billing/renewalAttach/internal/test-fixtures/witnessHistory.ts +70 -0
  82. package/src/client/billing/renewalAttach/internal/test-fixtures/witnessObservation.ts +353 -0
  83. package/src/component/_generated/api.ts +24 -0
  84. package/src/component/_generated/component.ts +98 -10
  85. package/src/component/renewalWitnesses/mutations.ts +17 -0
  86. package/src/component/renewalWitnesses/queries.ts +11 -0
  87. package/src/component/renewalWitnesses/routes.test.ts +154 -0
@@ -0,0 +1,205 @@
1
+ import { closed, freeze } from "../c10/ownData.js";
2
+ import { matchConfigureDATA } from "../producer/configureJournalData.js";
3
+ import { readCheckedJournalActionRPC } from "../producer/classOwnedActionRoot.js";
4
+ import { readObservationInvocation } from "./invocation.js";
5
+ import {
6
+ readFreshObservationTransport,
7
+ consumeFreshObservationAcquisition,
8
+ } from "./freshTransport.js";
9
+ import {
10
+ captureReaderQueryDATA,
11
+ readerText as text,
12
+ } from "./readerAuthorization.js";
13
+ import {
14
+ createWitnessAuthorization,
15
+ witnessRPC,
16
+ witnessAddress,
17
+ witnessProfileGuard,
18
+ type WitnessMode,
19
+ type WitnessProfile,
20
+ } from "./witnessAuthorization.js";
21
+ import {
22
+ captureWitnessPayload,
23
+ inspectWitnessPayload,
24
+ checkedWitnessDescriptor,
25
+ checkWitnessC7,
26
+ type InspectedWitness,
27
+ } from "./witnessData.js";
28
+ import { inspectHistoricalMaterialEvidenceV1 } from "../../../renewalObservation/evidence-material.js";
29
+ import {
30
+ witnessNeed as need,
31
+ type WitnessIdentity,
32
+ } from "../../../../../shared/renewalWitness.js";
33
+
34
+ /** Actor-private protocol. Caller DATA cannot register a token, grant or returned ACK. */
35
+ export function createWitnessOperation(
36
+ token: object,
37
+ profile: WitnessProfile,
38
+ mode: WitnessMode,
39
+ identity: WitnessIdentity,
40
+ ) {
41
+ need(arguments.length === 4);
42
+ const state = readObservationInvocation(token),
43
+ rpc = readCheckedJournalActionRPC(state.binding);
44
+ const transport = readFreshObservationTransport(
45
+ token,
46
+ profile.freshProfile,
47
+ profile.invoiceProfile,
48
+ );
49
+ const refs = rpc.component.renewalWitnesses;
50
+ const retain = refs.mutations.retainAuthorizedWitness,
51
+ read = refs.queries.readAuthorizedWitness;
52
+ const addresses = [witnessAddress(retain), witnessAddress(read)];
53
+ let authorization: ReturnType<typeof createWitnessAuthorization> | null =
54
+ null;
55
+ let stored: InspectedWitness | null = null;
56
+ let descriptor: ReturnType<typeof checkedWitnessDescriptor> | null = null;
57
+ let used = false;
58
+ function guard() {
59
+ state.guard();
60
+ transport.guard();
61
+ witnessProfileGuard(profile, mode);
62
+ need(
63
+ readCheckedJournalActionRPC(state.binding) === rpc &&
64
+ profile.freshProfile.historyEntry === state.historyEntry,
65
+ );
66
+ const current = rpc.component.renewalWitnesses;
67
+ need(
68
+ witnessAddress(current.mutations.retainAuthorizedWitness) ===
69
+ addresses[0] &&
70
+ witnessAddress(current.queries.readAuthorizedWitness) === addresses[1],
71
+ );
72
+ authorization?.guard();
73
+ }
74
+ async function fullRead() {
75
+ const raw: unknown = await witnessRPC(
76
+ state,
77
+ () => rpc.runQuery.call(rpc.receiver, read, { identity }),
78
+ guard,
79
+ );
80
+ const reply = closed(captureReaderQueryDATA(raw), [
81
+ "descriptor",
82
+ "payloadJSON",
83
+ ]);
84
+ return {
85
+ descriptor: reply.descriptor,
86
+ payloadJSON: text(reply.payloadJSON, 500000),
87
+ };
88
+ }
89
+ return Object.freeze({
90
+ guard,
91
+ async retain() {
92
+ need(arguments.length === 0 && mode === "retain" && !used);
93
+ used = true;
94
+ guard();
95
+ const { reads, invoiceContext } = consumeFreshObservationAcquisition(
96
+ token,
97
+ profile.freshProfile,
98
+ profile.invoiceProfile,
99
+ );
100
+ const payloadJSON = captureWitnessPayload(
101
+ state,
102
+ identity,
103
+ reads,
104
+ invoiceContext,
105
+ );
106
+ const admitted = await state.owned(
107
+ () => inspectWitnessPayload(payloadJSON),
108
+ guard,
109
+ );
110
+ checkWitnessC7(admitted.payload, state);
111
+ authorization = createWitnessAuthorization(
112
+ state,
113
+ profile,
114
+ mode,
115
+ identity,
116
+ admitted.scope,
117
+ );
118
+ await authorization.authorize();
119
+ guard();
120
+ const raw: unknown = await witnessRPC(
121
+ state,
122
+ () =>
123
+ rpc.runMutation.call(rpc.receiver, retain, { identity, payloadJSON }),
124
+ guard,
125
+ );
126
+ const reply = captureReaderQueryDATA(raw);
127
+ if (
128
+ reply &&
129
+ typeof reply === "object" &&
130
+ Reflect.get(reply, "status") === "conflict"
131
+ ) {
132
+ matchConfigureDATA(reply, {
133
+ status: "conflict",
134
+ reason: "identity_changed",
135
+ });
136
+ return freeze({
137
+ status: "conflict" as const,
138
+ reason: "identity_changed" as const,
139
+ });
140
+ }
141
+ const result = closed(reply, ["status", "descriptor"]);
142
+ need(result.status === "retained");
143
+ const retained = checkedWitnessDescriptor(result.descriptor, admitted);
144
+ const readback = await fullRead();
145
+ need(readback.payloadJSON === payloadJSON);
146
+ matchConfigureDATA(
147
+ checkedWitnessDescriptor(readback.descriptor, admitted),
148
+ retained,
149
+ );
150
+ guard();
151
+ return freeze({ status: "retained" as const, descriptor: retained });
152
+ },
153
+ async load() {
154
+ need(mode === "read" && !used);
155
+ used = true;
156
+ guard();
157
+ authorization = createWitnessAuthorization(
158
+ state,
159
+ profile,
160
+ mode,
161
+ identity,
162
+ null,
163
+ );
164
+ const grant = await authorization.authorize();
165
+ guard();
166
+ const reply = await fullRead();
167
+ const admitted = await state.owned(
168
+ () => inspectWitnessPayload(reply.payloadJSON),
169
+ guard,
170
+ );
171
+ matchConfigureDATA(admitted.payload.identity, identity);
172
+ checkWitnessC7(admitted.payload, state);
173
+ matchConfigureDATA(admitted.scope, grant.scope);
174
+ const checked = checkedWitnessDescriptor(reply.descriptor, admitted);
175
+ need(
176
+ checked.witnessRowId === grant.witnessRowId &&
177
+ checked.payloadBinding === grant.payloadBinding,
178
+ );
179
+ stored = admitted;
180
+ descriptor = checked;
181
+ guard();
182
+ },
183
+ async historicalEvidence() {
184
+ need(mode === "read" && stored && descriptor);
185
+ guard();
186
+ const input = stored.historicalInput;
187
+ const material = await state.owned(
188
+ () => inspectHistoricalMaterialEvidenceV1(input),
189
+ guard,
190
+ );
191
+ return { identity: stored.identity, material };
192
+ },
193
+ descriptor() {
194
+ need(mode === "read" && descriptor);
195
+ guard();
196
+ return descriptor;
197
+ },
198
+ async finalize() {
199
+ need(authorization);
200
+ guard();
201
+ await authorization.authorize();
202
+ guard();
203
+ },
204
+ });
205
+ }
@@ -0,0 +1,93 @@
1
+ import {
2
+ witnessControls,
3
+ prepareWitnessObservation,
4
+ invokeWitnessCapture,
5
+ readStoredWitness,
6
+ witnessFixtureOptions,
7
+ } from "../test-fixtures/witnessObservation.js";
8
+ import {
9
+ configureAdmissionCase,
10
+ admissionControls,
11
+ } from "../test-fixtures/producerConfigureAdmission.js";
12
+ import { INITIAL_NS } from "../test-fixtures/producerInitialDeclarations.js";
13
+ import { expect, vi } from "vitest";
14
+ import assert from "node:assert/strict";
15
+
16
+ for (const mode of ["duplicate", "foreign-fresh", "foreign-invoice"] as const)
17
+ configureAdmissionCase(
18
+ "witness Source refuses " + mode + " profile before provider I/O",
19
+ prepareWitnessObservation,
20
+ async (h, state) => {
21
+ const before = h.calls.length,
22
+ selectors = admissionControls.selectors;
23
+ if (mode === "duplicate")
24
+ witnessControls.captures.push(state.captureProfile);
25
+ else
26
+ witnessControls.captures.splice(
27
+ 0,
28
+ witnessControls.captures.length,
29
+ Object.freeze({
30
+ ...state.captureProfile,
31
+ ...(mode === "foreign-fresh"
32
+ ? { freshProfile: Object.freeze({ ...state.profile }) }
33
+ : {
34
+ invoiceProfile: Object.freeze({
35
+ freshProfile: state.profile,
36
+ readProfile: "foreign",
37
+ authorizationQuery: state.captureProfile.authorizationQuery,
38
+ }),
39
+ }),
40
+ }),
41
+ );
42
+ await expect(invokeWitnessCapture(h, state)).rejects.toThrow();
43
+ expect(h.calls).toHaveLength(before);
44
+ expect(admissionControls.selectors).toBe(selectors);
45
+ expect(await readStoredWitness(h, state)).toBeNull();
46
+ },
47
+ witnessFixtureOptions,
48
+ );
49
+
50
+ let track = false,
51
+ ticks = 0;
52
+ configureAdmissionCase(
53
+ "captured acquisition metadata measures the original fresh wall and monotonic lifetime",
54
+ async (h) => {
55
+ track = false;
56
+ ticks = 0;
57
+ return prepareWitnessObservation(h);
58
+ },
59
+ async (h, state) => {
60
+ const start = Date.now();
61
+ track = true;
62
+ try {
63
+ await invokeWitnessCapture(h, state);
64
+ } finally {
65
+ track = false;
66
+ }
67
+ const stored = await readStoredWitness(h, state);
68
+ assert(stored);
69
+ const payload = JSON.parse(stored.payloadJSON);
70
+ expect(ticks).toBe(6);
71
+ expect(payload.acquisition).toEqual({
72
+ observedAtMs: start + 12,
73
+ elapsedWallMs: 12,
74
+ elapsedNsDecimal: "12000000",
75
+ apiVersion: "2026-09-30.endive",
76
+ baseReadProfile: "endive-schedule-first-44-v1",
77
+ invoiceReadProfile: null,
78
+ });
79
+ expect(stored.descriptor.observedAtMs).toBe(start + 12);
80
+ },
81
+ {
82
+ ...witnessFixtureOptions,
83
+ beforeFetch: async (_path, method) => {
84
+ if (track && method === "GET") {
85
+ ticks++;
86
+ vi.setSystemTime(Date.now() + 2);
87
+ vi.mocked(process.hrtime.bigint).mockReturnValue(
88
+ INITIAL_NS + BigInt(ticks * 2000000),
89
+ );
90
+ }
91
+ },
92
+ },
93
+ );
@@ -0,0 +1,256 @@
1
+ import {
2
+ prepareWitnessObservation,
3
+ invokeWitnessCapture,
4
+ invokeWitnessRead,
5
+ readStoredWitness,
6
+ witnessFixtureOptions,
7
+ } from "../test-fixtures/witnessObservation.js";
8
+ import { configureAdmissionCase } from "../test-fixtures/producerConfigureAdmission.js";
9
+ import { captureFreshWitnessRenewalObservation } from "./freshActor.js";
10
+ import {
11
+ getFunctionAddress,
12
+ type GenericActionCtx,
13
+ type GenericDataModel,
14
+ } from "convex/server";
15
+ import { expect } from "vitest";
16
+ import assert from "node:assert/strict";
17
+
18
+ const isRetain = (ref: Parameters<typeof getFunctionAddress>[0]) =>
19
+ getFunctionAddress(ref).reference?.endsWith("/retainAuthorizedWitness");
20
+ const isRead = (ref: Parameters<typeof getFunctionAddress>[0]) =>
21
+ getFunctionAddress(ref).reference?.endsWith("/readAuthorizedWitness");
22
+ function data(raw: unknown) {
23
+ assert(raw !== null && typeof raw === "object");
24
+ return raw;
25
+ }
26
+
27
+ configureAdmissionCase(
28
+ "retention and later read settle original native RPC Promises despite poisoned public then",
29
+ prepareWitnessObservation,
30
+ async (h, state) => {
31
+ let poisons = 0,
32
+ selected = 0;
33
+ const before = (ctx: GenericActionCtx<GenericDataModel>) => {
34
+ const mutate = ctx.runMutation,
35
+ query = ctx.runQuery;
36
+ Object.defineProperty(ctx, "runMutation", {
37
+ value: (...args: Parameters<typeof mutate>) => {
38
+ const pending = mutate.apply(ctx, args);
39
+ if (isRetain(args[0])) {
40
+ selected++;
41
+ void Object.defineProperty(pending, "then", {
42
+ value() {
43
+ poisons++;
44
+ throw Error("PUBLIC_RETENTION_THEN_POISON");
45
+ },
46
+ });
47
+ }
48
+ return pending;
49
+ },
50
+ });
51
+ Object.defineProperty(ctx, "runQuery", {
52
+ value: (...args: Parameters<typeof query>) => {
53
+ const pending = query.apply(ctx, args);
54
+ if (isRead(args[0])) {
55
+ selected++;
56
+ void Object.defineProperty(pending, "then", {
57
+ value() {
58
+ poisons++;
59
+ throw Error("PUBLIC_READBACK_THEN_POISON");
60
+ },
61
+ });
62
+ }
63
+ return pending;
64
+ },
65
+ });
66
+ };
67
+ const retained = await invokeWitnessCapture(h, state, { before });
68
+ assert("witnessRetention" in retained);
69
+ expect(retained.witnessRetention?.status).toBe("retained");
70
+ const later = await invokeWitnessRead(h, state, { before });
71
+ expect(later.evidence?.historicalMaterial.predicate).toBe(
72
+ "snapshot_target_material_match",
73
+ );
74
+ expect(selected).toBe(3);
75
+ expect(poisons).toBe(0);
76
+ },
77
+ witnessFixtureOptions,
78
+ );
79
+
80
+ for (const shape of ["copied-reply", "thenable"] as const)
81
+ configureAdmissionCase(
82
+ "a " + shape + " cannot substitute for the actual mutation Promise",
83
+ prepareWitnessObservation,
84
+ async (h, state) => {
85
+ const first = await invokeWitnessCapture(h, state);
86
+ assert(
87
+ "witnessRetention" in first &&
88
+ first.witnessRetention?.status === "retained",
89
+ );
90
+ const stored = await readStoredWitness(h, state);
91
+ let intercepted = 0,
92
+ thenCalls = 0;
93
+ await expect(
94
+ invokeWitnessCapture(h, state, {
95
+ before(ctx) {
96
+ const mutate = ctx.runMutation;
97
+ Object.defineProperty(ctx, "runMutation", {
98
+ value: (...args: Parameters<typeof mutate>) => {
99
+ if (!isRetain(args[0])) return mutate.apply(ctx, args);
100
+ intercepted++;
101
+ const copy = structuredClone(first.witnessRetention);
102
+ return shape === "copied-reply"
103
+ ? copy
104
+ : {
105
+ then(resolve: (value: unknown) => void) {
106
+ thenCalls++;
107
+ resolve(copy);
108
+ },
109
+ };
110
+ },
111
+ });
112
+ },
113
+ }),
114
+ ).rejects.toThrow("RENEWAL_WITNESS_INVALID");
115
+ expect(intercepted).toBe(1);
116
+ expect(thenCalls).toBe(0);
117
+ expect(await readStoredWitness(h, state)).toEqual(stored);
118
+ },
119
+ witnessFixtureOptions,
120
+ );
121
+
122
+ configureAdmissionCase(
123
+ "a copied thenable readback cannot grant later historical DATA",
124
+ prepareWitnessObservation,
125
+ async (h, state) => {
126
+ await invokeWitnessCapture(h, state);
127
+ const stored = await readStoredWitness(h, state);
128
+ let thenCalls = 0;
129
+ const before = h.calls.length;
130
+ await expect(
131
+ invokeWitnessRead(h, state, {
132
+ before(ctx) {
133
+ const query = ctx.runQuery;
134
+ Object.defineProperty(ctx, "runQuery", {
135
+ value: (...args: Parameters<typeof query>) => {
136
+ if (!isRead(args[0])) return query.apply(ctx, args);
137
+ return {
138
+ then(resolve: (value: unknown) => void) {
139
+ thenCalls++;
140
+ resolve(structuredClone(stored));
141
+ },
142
+ };
143
+ },
144
+ });
145
+ },
146
+ }),
147
+ ).rejects.toThrow("RENEWAL_WITNESS_INVALID");
148
+ expect(thenCalls).toBe(0);
149
+ expect(h.calls).toHaveLength(before);
150
+ expect(await readStoredWitness(h, state)).toEqual(stored);
151
+ },
152
+ witnessFixtureOptions,
153
+ );
154
+
155
+ for (const phase of ["retention-readback", "historical-read"] as const)
156
+ for (const change of ["row-id", "payload", "rehashed-payload"] as const)
157
+ configureAdmissionCase(
158
+ phase + " refuses a changed " + change,
159
+ prepareWitnessObservation,
160
+ async (h, state) => {
161
+ if (phase === "historical-read") await invokeWitnessCapture(h, state);
162
+ let changed = 0;
163
+ const invoke =
164
+ phase === "historical-read"
165
+ ? invokeWitnessRead
166
+ : invokeWitnessCapture;
167
+ await expect(
168
+ invoke(h, state, {
169
+ before(ctx) {
170
+ const query = ctx.runQuery;
171
+ Object.defineProperty(ctx, "runQuery", {
172
+ value: async (...args: Parameters<typeof query>) => {
173
+ const actual = await query.apply(ctx, args);
174
+ if (!isRead(args[0])) return actual;
175
+ const reply = data(structuredClone(actual)),
176
+ descriptor = data(Reflect.get(reply, "descriptor"));
177
+ changed++;
178
+ if (change === "row-id")
179
+ Reflect.set(
180
+ descriptor,
181
+ "witnessRowId",
182
+ "controlled-foreign-row",
183
+ );
184
+ else {
185
+ const raw = Reflect.get(reply, "payloadJSON");
186
+ assert(typeof raw === "string");
187
+ const payload = JSON.parse(raw);
188
+ payload.capturedSubject =
189
+ "https://host.test|changed-captured-subject";
190
+ const json = JSON.stringify(payload);
191
+ Reflect.set(reply, "payloadJSON", json);
192
+ if (change === "rehashed-payload") {
193
+ const hash = await crypto.subtle.digest(
194
+ "SHA-256",
195
+ new TextEncoder().encode(
196
+ "renewal-witness:v1\u0000" + json,
197
+ ),
198
+ );
199
+ const hex = [...new Uint8Array(hash)]
200
+ .map((n) => n.toString(16).padStart(2, "0"))
201
+ .join("");
202
+ Reflect.set(
203
+ descriptor,
204
+ "payloadBinding",
205
+ "renewal-witness:v1:sha256:" + hex,
206
+ );
207
+ Reflect.set(
208
+ descriptor,
209
+ "capturedSubject",
210
+ payload.capturedSubject,
211
+ );
212
+ }
213
+ }
214
+ return reply;
215
+ },
216
+ });
217
+ },
218
+ }),
219
+ ).rejects.toThrow();
220
+ expect(changed).toBe(1);
221
+ const stored = await readStoredWitness(h, state);
222
+ assert(stored);
223
+ expect(JSON.parse(stored.payloadJSON).capturedSubject).toBe(
224
+ witnessFixtureOptions.identity.tokenIdentifier,
225
+ );
226
+ },
227
+ witnessFixtureOptions,
228
+ );
229
+
230
+ configureAdmissionCase(
231
+ "returned retention DATA cannot register another capture invocation",
232
+ prepareWitnessObservation,
233
+ async (h, state) => {
234
+ const output = await invokeWitnessCapture(h, state);
235
+ assert("witnessRetention" in output);
236
+ const before = h.calls.length,
237
+ stored = await readStoredWitness(h, state);
238
+ await h.inAnotherAction(async (ctx) => {
239
+ await expect(
240
+ captureFreshWitnessRenewalObservation(
241
+ h.sdk,
242
+ structuredClone(output.witnessRetention),
243
+ ),
244
+ ).rejects.toThrow();
245
+ await expect(
246
+ captureFreshWitnessRenewalObservation(h.sdk, {
247
+ ...output.witnessRetention,
248
+ actualActionCtx: ctx,
249
+ }),
250
+ ).rejects.toThrow();
251
+ });
252
+ expect(h.calls).toHaveLength(before);
253
+ expect(await readStoredWitness(h, state)).toEqual(stored);
254
+ },
255
+ witnessFixtureOptions,
256
+ );
@@ -0,0 +1,58 @@
1
+ import type { FunctionReference } from "convex/server";
2
+ import type { WitnessIdentity } from "../../../../../shared/renewalWitness.js";
3
+ import type {
4
+ FreshObservationProfile,
5
+ InvoiceObservationProfile,
6
+ } from "./sourceOperations.js";
7
+
8
+ export type WitnessScope = Readonly<{
9
+ identity: WitnessIdentity;
10
+ providerScope: Readonly<{
11
+ mode: "platform";
12
+ stripeAccount: null;
13
+ livemode: boolean;
14
+ customerAccountId: string;
15
+ customerId: string;
16
+ subscriptionId: string;
17
+ scheduleId: string;
18
+ }>;
19
+ configureRequestBinding: string;
20
+ observedItemId: string;
21
+ nativePeriod: Readonly<{ start: number; end: number }>;
22
+ }>;
23
+ export type WitnessRetentionArgs = {
24
+ capability: "renewal_witness_retain_v1";
25
+ scope: WitnessScope;
26
+ };
27
+ export type WitnessReadArgs = {
28
+ capability: "renewal_witness_read_v1";
29
+ identity: WitnessIdentity;
30
+ };
31
+ type BaseProfile = Readonly<{
32
+ freshProfile: FreshObservationProfile;
33
+ invoiceProfile: InvoiceObservationProfile | null;
34
+ }>;
35
+ export type WitnessCaptureProfile = BaseProfile &
36
+ Readonly<{
37
+ authorizationQuery: FunctionReference<
38
+ "query",
39
+ "public" | "internal",
40
+ WitnessRetentionArgs,
41
+ unknown
42
+ >;
43
+ }>;
44
+ export type WitnessReadProfile = BaseProfile &
45
+ Readonly<{
46
+ authorizationQuery: FunctionReference<
47
+ "query",
48
+ "public" | "internal",
49
+ WitnessReadArgs,
50
+ unknown
51
+ >;
52
+ }>;
53
+
54
+ /** Fixed Source associations only. Returned DATA cannot populate these profiles. */
55
+ export const COMPILED_WITNESS_CAPTURE_ENTRIES: readonly WitnessCaptureProfile[] =
56
+ Object.freeze([]);
57
+ export const COMPILED_WITNESS_READ_ENTRIES: readonly WitnessReadProfile[] =
58
+ Object.freeze([]);
@@ -147,7 +147,9 @@ export async function prepareInvoiceObservation(
147
147
  const profile = Object.freeze({
148
148
  freshProfile: state.profile,
149
149
  readProfile: "endive-pinned-invoices-fence-7-v1",
150
- authorizationQuery: makeFunctionReference("initialOwner:authorizeInvoices"),
150
+ authorizationQuery: makeFunctionReference<"query">(
151
+ "initialOwner:authorizeInvoices",
152
+ ),
151
153
  });
152
154
  admissionControls.invoices.splice(
153
155
  0,
@@ -413,6 +413,12 @@ export async function configureAdmissionFixture(
413
413
  hostInvoiceQuery?: NonNullable<
414
414
  Parameters<typeof initialOperations>[2]
415
415
  >["hostQuery"];
416
+ hostWitnessRetainQuery?: NonNullable<
417
+ Parameters<typeof initialOperations>[2]
418
+ >["hostQuery"];
419
+ hostWitnessReadQuery?: NonNullable<
420
+ Parameters<typeof initialOperations>[2]
421
+ >["hostQuery"];
416
422
  count?: number;
417
423
  keys?: readonly string[];
418
424
  afterBind?: (reply: unknown) => Promise<void>;
@@ -444,6 +450,8 @@ export async function configureAdmissionFixture(
444
450
  identity: options.identity,
445
451
  hostQuery: options.hostQuery,
446
452
  hostInvoiceQuery: options.hostInvoiceQuery,
453
+ hostWitnessRetainQuery: options.hostWitnessRetainQuery,
454
+ hostWitnessReadQuery: options.hostWitnessReadQuery,
447
455
  source: captureRetainedInput(baseline.original),
448
456
  afterBind: options.afterBind,
449
457
  afterQuery: options.afterQuery,