@fantastic.dev/better-stripe 0.91.0 → 0.93.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAttach/internal/configure/actor.d.ts +6 -0
- package/dist/client/billing/renewalAttach/internal/configure/actor.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/configure/actor.js +46 -56
- package/dist/client/billing/renewalAttach/internal/configure/actor.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.d.ts +3 -0
- package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.js +61 -0
- package/dist/client/billing/renewalAttach/internal/configure/ownedContinuation.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/actor.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/actor.js +25 -4
- package/dist/client/billing/renewalAttach/internal/observation/actor.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/capture.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/capture.js +3 -2
- package/dist/client/billing/renewalAttach/internal/observation/capture.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.d.ts +5 -0
- package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.js +11 -0
- package/dist/client/billing/renewalAttach/internal/observation/configureAcknowledgement.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/types.d.ts +5 -0
- package/dist/client/billing/renewalAttach/internal/observation/types.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/types.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +30 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +113 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +562 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +281 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +562 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +281 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/index.d.ts +3 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +16 -3
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/api.d.ts +6 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +406 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
- package/dist/component/earnings/triggers.d.ts +294 -0
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/dist/component/renewalOperations/helpers.d.ts +236 -0
- package/dist/component/renewalOperations/helpers.d.ts.map +1 -0
- package/dist/component/renewalOperations/helpers.js +457 -0
- package/dist/component/renewalOperations/helpers.js.map +1 -0
- package/dist/component/renewalOperations/mutations.d.ts +252 -0
- package/dist/component/renewalOperations/mutations.d.ts.map +1 -0
- package/dist/component/renewalOperations/mutations.js +340 -0
- package/dist/component/renewalOperations/mutations.js.map +1 -0
- package/dist/component/renewalOperations/queries.d.ts +163 -0
- package/dist/component/renewalOperations/queries.d.ts.map +1 -0
- package/dist/component/renewalOperations/queries.js +167 -0
- package/dist/component/renewalOperations/queries.js.map +1 -0
- package/dist/component/renewalOperations/schema.d.ts +1988 -0
- package/dist/component/renewalOperations/schema.d.ts.map +1 -0
- package/dist/component/renewalOperations/schema.js +227 -0
- package/dist/component/renewalOperations/schema.js.map +1 -0
- package/dist/component/schema.d.ts +281 -0
- package/dist/component/schema.js +5 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +281 -0
- package/dist/component/usage/allowance.fixtures.d.ts.map +1 -1
- package/dist/component/usageCredits/accounting.fixtures.d.ts +281 -0
- package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -1
- package/dist/shared/renewalOperationJournal.d.ts +43 -0
- package/dist/shared/renewalOperationJournal.d.ts.map +1 -0
- package/dist/shared/renewalOperationJournal.js +294 -0
- package/dist/shared/renewalOperationJournal.js.map +1 -0
- package/docs/renewal-context-reads.md +152 -0
- package/docs/renewal-material-observation.md +32 -11
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/internal/configure/actor.ts +63 -52
- package/src/client/billing/renewalAttach/internal/configure/ownedContinuation.ts +56 -0
- package/src/client/billing/renewalAttach/internal/observation/actor.ts +46 -3
- package/src/client/billing/renewalAttach/internal/observation/capture.ts +3 -2
- package/src/client/billing/renewalAttach/internal/observation/configureAcknowledgement.ts +20 -0
- package/src/client/billing/renewalAttach/internal/observation/observationConfigureACK.integration.test.ts +36 -0
- package/src/client/billing/renewalAttach/internal/observation/observationConfigureACKControls.integration.test.ts +48 -0
- package/src/client/billing/renewalAttach/internal/observation/observationConfigureACKLifecycle.integration.test.ts +44 -0
- package/src/client/billing/renewalAttach/internal/observation/observationConfiguredHistory.integration.test.ts +34 -0
- package/src/client/billing/renewalAttach/internal/observation/types.ts +5 -0
- package/src/client/billing/renewalAttach/internal/operationJournal.integration.test.ts +380 -0
- package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +155 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteAssertions.ts +5 -1
- package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteDeclarations.ts +12 -1
- package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteDelegate.ts +38 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteHistory.ts +8 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/configureWriteOperation.ts +10 -23
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKAssertions.ts +115 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKDeclarations.ts +141 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleAssertions.ts +74 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleDeclarations.ts +79 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleInstrumentation.ts +207 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKLifecycleOperation.ts +494 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfigureACKOperation.ts +452 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryAssertions.ts +88 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryDeclarations.ts +170 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryNative.ts +99 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationConfiguredHistoryOperation.ts +411 -0
- package/src/client/index.ts +24 -3
- package/src/component/_generated/api.ts +6 -0
- package/src/component/_generated/component.ts +450 -0
- package/src/component/renewalOperations/acknowledgement.fixtures.ts +552 -0
- package/src/component/renewalOperations/acknowledgement.test.ts +573 -0
- package/src/component/renewalOperations/acknowledgementReads.test.ts +442 -0
- package/src/component/renewalOperations/acknowledgementV2.test.ts +493 -0
- package/src/component/renewalOperations/acknowledgementV2Publication.test.ts +373 -0
- package/src/component/renewalOperations/basisBinding.test.ts +567 -0
- package/src/component/renewalOperations/basisLifecycle.test.ts +569 -0
- package/src/component/renewalOperations/boundHistory.test.ts +474 -0
- package/src/component/renewalOperations/helpers.ts +592 -0
- package/src/component/renewalOperations/historyReads.test.ts +595 -0
- package/src/component/renewalOperations/initialBasis.test.ts +578 -0
- package/src/component/renewalOperations/journal.test.ts +567 -0
- package/src/component/renewalOperations/mutations.ts +456 -0
- package/src/component/renewalOperations/payload.test.ts +589 -0
- package/src/component/renewalOperations/payloadEncoding.test.ts +592 -0
- package/src/component/renewalOperations/queries.ts +204 -0
- package/src/component/renewalOperations/schema.ts +268 -0
- package/src/component/renewalOperations/timingPolicy.fixtures.ts +193 -0
- package/src/component/renewalOperations/timingPolicy.test.ts +523 -0
- package/src/component/schema.ts +11 -0
- package/src/shared/renewalOperationJournal.ts +333 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import { makeFunctionReference } from "convex/server";
|
|
3
|
+
import { getConvexSize, getDocumentSize } from "convex/values";
|
|
4
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
5
|
+
import { schema, modules } from "../../test.js";
|
|
6
|
+
import { renewalSnapshot } from "../../client/billing/renewalReplacementSnapshot.js";
|
|
7
|
+
import {
|
|
8
|
+
eventDigest,
|
|
9
|
+
materialize,
|
|
10
|
+
objectNode,
|
|
11
|
+
scalarNode,
|
|
12
|
+
} from "../../client/billing/renewalAttach/internal/c10/eventCodec.js";
|
|
13
|
+
|
|
14
|
+
const create = makeFunctionReference<"mutation">(
|
|
15
|
+
"renewalOperations/mutations:createAuthorizedOperation",
|
|
16
|
+
);
|
|
17
|
+
const reserve = makeFunctionReference<"mutation">(
|
|
18
|
+
"renewalOperations/mutations:reserveAttempt",
|
|
19
|
+
);
|
|
20
|
+
const load = makeFunctionReference<"query">(
|
|
21
|
+
"renewalOperations/queries:loadAuthorizedOperation",
|
|
22
|
+
);
|
|
23
|
+
const append = makeFunctionReference<"mutation">(
|
|
24
|
+
"renewalOperations/mutations:appendStageEvent",
|
|
25
|
+
);
|
|
26
|
+
const readEvent = makeFunctionReference<"query">(
|
|
27
|
+
"renewalOperations/queries:readStageEvent",
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Independent immutable storage DATA, never a qualified producer or Owner.
|
|
31
|
+
const record = Object.freeze({
|
|
32
|
+
schemaVersion: 1,
|
|
33
|
+
requestBinding: "scoped-history-storage-only",
|
|
34
|
+
authorization: Object.freeze({
|
|
35
|
+
scope: "history-scope",
|
|
36
|
+
id: "history-consent",
|
|
37
|
+
revision: 1,
|
|
38
|
+
}),
|
|
39
|
+
logicalOperationId: "history-read-small",
|
|
40
|
+
profile: "unqualified-storage-DATA",
|
|
41
|
+
stage: "attach",
|
|
42
|
+
apiVersion: "2026-09-30.endive",
|
|
43
|
+
account: Object.freeze({
|
|
44
|
+
mode: "platform",
|
|
45
|
+
stripeAccount: null,
|
|
46
|
+
livemode: false,
|
|
47
|
+
}),
|
|
48
|
+
dispatch: Object.freeze({
|
|
49
|
+
scope: "platform",
|
|
50
|
+
apiVersion: "2026-09-30.endive",
|
|
51
|
+
method: "POST",
|
|
52
|
+
path: "/v1/subscription_schedules",
|
|
53
|
+
payload: Object.freeze({ from_subscription: "sub_history" }),
|
|
54
|
+
key: "history-storage-key",
|
|
55
|
+
firstDispatchedAt: 2000000000,
|
|
56
|
+
}),
|
|
57
|
+
basis: Object.freeze({
|
|
58
|
+
startWallMs: 2000000000000,
|
|
59
|
+
startNs: "1000000000",
|
|
60
|
+
workEndWallMs: 2000000004000,
|
|
61
|
+
workEndNs: "5000000000",
|
|
62
|
+
endWallMs: 2000000005000,
|
|
63
|
+
endNs: "6000000000",
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
const recordJSON = JSON.stringify(renewalSnapshot(record));
|
|
67
|
+
const eventData = Object.freeze({
|
|
68
|
+
event: "unqualified-storage-history",
|
|
69
|
+
padding: "complete-small-event",
|
|
70
|
+
});
|
|
71
|
+
const dataJSON = JSON.stringify(renewalSnapshot(eventData));
|
|
72
|
+
const limit = 1048576;
|
|
73
|
+
afterEach(() => vi.unstubAllGlobals());
|
|
74
|
+
|
|
75
|
+
it("R01 scoped event query returns the complete committed event and descriptor", async () => {
|
|
76
|
+
let fetchCalls = 0;
|
|
77
|
+
vi.stubGlobal("fetch", () => {
|
|
78
|
+
fetchCalls++;
|
|
79
|
+
throw Error("UNEXPECTED_EXTERNAL_FETCH");
|
|
80
|
+
});
|
|
81
|
+
const t = convexTest(schema, modules);
|
|
82
|
+
function fits(value: Parameters<typeof getConvexSize>[0]) {
|
|
83
|
+
expect(getConvexSize(value) <= limit).toBe(true);
|
|
84
|
+
}
|
|
85
|
+
const creation = {
|
|
86
|
+
authorizationScope: record.authorization.scope,
|
|
87
|
+
logicalOperationId: record.logicalOperationId,
|
|
88
|
+
authorizationRevision: record.authorization.revision,
|
|
89
|
+
recordJSON,
|
|
90
|
+
};
|
|
91
|
+
fits(creation);
|
|
92
|
+
const op = await t.mutation(create, creation);
|
|
93
|
+
expect(op.status).toBe("created");
|
|
94
|
+
fits(op);
|
|
95
|
+
const loadArgs = { operationId: op.operationId };
|
|
96
|
+
fits(loadArgs);
|
|
97
|
+
const original = await t.query(load, loadArgs);
|
|
98
|
+
fits(original);
|
|
99
|
+
expect(original.recordJSON).toBe(recordJSON);
|
|
100
|
+
expect(original.revision).toBe(0);
|
|
101
|
+
expect(original.activeAttempt).toBeNull();
|
|
102
|
+
const reserveArgs = {
|
|
103
|
+
operationId: op.operationId,
|
|
104
|
+
expectedRevision: 0,
|
|
105
|
+
stage: "attach",
|
|
106
|
+
expectedRequestBinding: record.requestBinding,
|
|
107
|
+
recordJSON,
|
|
108
|
+
};
|
|
109
|
+
fits(reserveArgs);
|
|
110
|
+
const acquired = await t.mutation(reserve, reserveArgs);
|
|
111
|
+
fits(acquired);
|
|
112
|
+
expect(acquired.disposition).toBe("acquired");
|
|
113
|
+
expect(acquired.reservation.recordJSON).toBe(recordJSON);
|
|
114
|
+
expect(acquired.reservation.basisJSON).toBe(
|
|
115
|
+
JSON.stringify(renewalSnapshot(record.basis)),
|
|
116
|
+
);
|
|
117
|
+
const dataNode = objectNode([
|
|
118
|
+
["event", scalarNode(eventData.event)],
|
|
119
|
+
["padding", scalarNode(eventData.padding)],
|
|
120
|
+
]);
|
|
121
|
+
expect(materialize(dataNode)).toEqual(eventData);
|
|
122
|
+
const binding = await eventDigest(
|
|
123
|
+
objectNode([
|
|
124
|
+
["sequence", scalarNode(1)],
|
|
125
|
+
["data", dataNode],
|
|
126
|
+
]),
|
|
127
|
+
);
|
|
128
|
+
expect(binding).toMatch(
|
|
129
|
+
/^unbound-qualification-event:v2:sha256:[0-9a-f]{64}$/,
|
|
130
|
+
);
|
|
131
|
+
const appendArgs = {
|
|
132
|
+
reservation: acquired.reservation,
|
|
133
|
+
expectedSequence: 0,
|
|
134
|
+
event: { sequence: 1, binding, dataJSON },
|
|
135
|
+
};
|
|
136
|
+
fits(appendArgs);
|
|
137
|
+
const receipt = await t.mutation(append, appendArgs);
|
|
138
|
+
fits(receipt);
|
|
139
|
+
expect(receipt.reservation).toEqual(acquired.reservation);
|
|
140
|
+
expect(receipt.sequence).toBe(1);
|
|
141
|
+
expect(receipt.binding).toBe(binding);
|
|
142
|
+
const { reservation: unusedReservation, ...descriptor } = receipt;
|
|
143
|
+
void unusedReservation;
|
|
144
|
+
const expected = { ...descriptor, dataJSON };
|
|
145
|
+
fits(expected);
|
|
146
|
+
const args = {
|
|
147
|
+
operationId: op.operationId,
|
|
148
|
+
attemptId: acquired.reservation.attemptId,
|
|
149
|
+
sequence: 1,
|
|
150
|
+
};
|
|
151
|
+
fits(args);
|
|
152
|
+
// This first case has only small complete documents. Never reuse an aggregate
|
|
153
|
+
// payload snapshot for the later large case: read each large payload by ID.
|
|
154
|
+
async function physical() {
|
|
155
|
+
const rows = await t.run(async (ctx) => ({
|
|
156
|
+
operations: await ctx.db.query("renewalOperations").take(2),
|
|
157
|
+
attempts: await ctx.db.query("renewalOperationAttempts").take(2),
|
|
158
|
+
events: await ctx.db.query("renewalOperationEvents").take(2),
|
|
159
|
+
payloads: await ctx.db.query("renewalOperationPayloads").take(3),
|
|
160
|
+
}));
|
|
161
|
+
fits(rows);
|
|
162
|
+
for (const group of Object.values(rows))
|
|
163
|
+
for (const row of group) expect(getDocumentSize(row) <= limit).toBe(true);
|
|
164
|
+
expect(rows.operations).toHaveLength(1);
|
|
165
|
+
expect(rows.attempts).toHaveLength(1);
|
|
166
|
+
expect(rows.events).toHaveLength(1);
|
|
167
|
+
expect(rows.payloads).toHaveLength(2);
|
|
168
|
+
expect(rows.operations[0]._id).toBe(op.operationId);
|
|
169
|
+
expect(rows.operations[0].state).toBe("RESERVED");
|
|
170
|
+
expect(rows.operations[0].revision).toBe(1);
|
|
171
|
+
expect(rows.operations[0].activeAttemptId).toBe(args.attemptId);
|
|
172
|
+
expect(rows.attempts[0]._id).toBe(args.attemptId);
|
|
173
|
+
expect(rows.attempts[0].lastSequence).toBe(1);
|
|
174
|
+
const { _id, _creationTime, ...fields } = rows.events[0];
|
|
175
|
+
void _creationTime;
|
|
176
|
+
expect({ ...fields, eventId: _id }).toEqual(descriptor);
|
|
177
|
+
const payload = rows.payloads.find((row) => row._id === receipt.payloadId);
|
|
178
|
+
const originalPayload = rows.payloads.find(
|
|
179
|
+
(row) => row._id === acquired.reservation.payloadId,
|
|
180
|
+
);
|
|
181
|
+
expect(payload?.codecVersion).toBe(1);
|
|
182
|
+
expect(payload?.json).toBe(dataJSON);
|
|
183
|
+
expect(originalPayload?.json).toBe(recordJSON);
|
|
184
|
+
return rows;
|
|
185
|
+
}
|
|
186
|
+
const before = await physical();
|
|
187
|
+
let received = false;
|
|
188
|
+
let returnedNull = false;
|
|
189
|
+
try {
|
|
190
|
+
const actual = await t.query(readEvent, args);
|
|
191
|
+
received = true;
|
|
192
|
+
returnedNull = actual === null;
|
|
193
|
+
fits(actual);
|
|
194
|
+
expect(actual).toEqual(expected);
|
|
195
|
+
} finally {
|
|
196
|
+
const after = await physical();
|
|
197
|
+
const unchanged = JSON.stringify(after) === JSON.stringify(before);
|
|
198
|
+
console.info(
|
|
199
|
+
"SCOPED_EVENT_R01",
|
|
200
|
+
JSON.stringify({
|
|
201
|
+
received,
|
|
202
|
+
returnedNull,
|
|
203
|
+
unchanged,
|
|
204
|
+
fetchCalls,
|
|
205
|
+
}),
|
|
206
|
+
);
|
|
207
|
+
expect(unchanged).toBe(true);
|
|
208
|
+
expect(fetchCalls).toBe(0);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// Follow-on actual storage tests. R01 above remains byte-for-byte unchanged.
|
|
213
|
+
const readManifest = makeFunctionReference<"query">(
|
|
214
|
+
"renewalOperations/queries:readAttempt",
|
|
215
|
+
);
|
|
216
|
+
function within(value: Parameters<typeof getConvexSize>[0]) {
|
|
217
|
+
expect(getConvexSize(value) <= limit).toBe(true);
|
|
218
|
+
}
|
|
219
|
+
function fixture() {
|
|
220
|
+
let fetches = 0;
|
|
221
|
+
vi.stubGlobal("fetch", () => {
|
|
222
|
+
fetches++;
|
|
223
|
+
throw Error("UNEXPECTED_EXTERNAL_FETCH");
|
|
224
|
+
});
|
|
225
|
+
return { t: convexTest(schema, modules), fetches: () => fetches };
|
|
226
|
+
}
|
|
227
|
+
type TestClient = ReturnType<typeof fixture>["t"];
|
|
228
|
+
async function prepare(t: TestClient, name: string) {
|
|
229
|
+
const data = Object.freeze({
|
|
230
|
+
...record,
|
|
231
|
+
logicalOperationId: name,
|
|
232
|
+
requestBinding: `storage:${name}`,
|
|
233
|
+
});
|
|
234
|
+
const json = JSON.stringify(renewalSnapshot(data));
|
|
235
|
+
const args = {
|
|
236
|
+
authorizationScope: data.authorization.scope,
|
|
237
|
+
logicalOperationId: name,
|
|
238
|
+
authorizationRevision: data.authorization.revision,
|
|
239
|
+
recordJSON: json,
|
|
240
|
+
};
|
|
241
|
+
within(args);
|
|
242
|
+
const created = await t.mutation(create, args);
|
|
243
|
+
within(created);
|
|
244
|
+
expect(created.status).toBe("created");
|
|
245
|
+
const loadArgs = { operationId: created.operationId };
|
|
246
|
+
within(loadArgs);
|
|
247
|
+
const retained = await t.query(load, loadArgs);
|
|
248
|
+
within(retained);
|
|
249
|
+
expect(retained.recordJSON).toBe(json);
|
|
250
|
+
expect(retained.revision).toBe(0);
|
|
251
|
+
expect(retained.activeAttempt).toBeNull();
|
|
252
|
+
return { operationId: created.operationId, data, json };
|
|
253
|
+
}
|
|
254
|
+
async function acquire(t: TestClient, name: string) {
|
|
255
|
+
const prepared = await prepare(t, name);
|
|
256
|
+
const args = {
|
|
257
|
+
operationId: prepared.operationId,
|
|
258
|
+
expectedRevision: 0,
|
|
259
|
+
stage: "attach",
|
|
260
|
+
expectedRequestBinding: prepared.data.requestBinding,
|
|
261
|
+
recordJSON: prepared.json,
|
|
262
|
+
};
|
|
263
|
+
within(args);
|
|
264
|
+
const result = await t.mutation(reserve, args);
|
|
265
|
+
within(result);
|
|
266
|
+
expect(result.disposition).toBe("acquired");
|
|
267
|
+
expect(result.reservation.recordJSON).toBe(prepared.json);
|
|
268
|
+
expect(result.reservation.basisJSON).toBe(
|
|
269
|
+
JSON.stringify(renewalSnapshot(record.basis)),
|
|
270
|
+
);
|
|
271
|
+
return { ...prepared, reservation: result.reservation };
|
|
272
|
+
}
|
|
273
|
+
async function addEvent(
|
|
274
|
+
t: TestClient,
|
|
275
|
+
owned: Awaited<ReturnType<typeof acquire>>,
|
|
276
|
+
sequence: 1 | 2 | 3,
|
|
277
|
+
padding: string,
|
|
278
|
+
) {
|
|
279
|
+
const data = Object.freeze({ event: "unqualified-storage-history", padding });
|
|
280
|
+
const json = JSON.stringify(renewalSnapshot(data));
|
|
281
|
+
expect(json.length <= 500000).toBe(true);
|
|
282
|
+
expect(new TextEncoder().encode(json).length <= 500000).toBe(true);
|
|
283
|
+
const node = objectNode([
|
|
284
|
+
["event", scalarNode(data.event)],
|
|
285
|
+
["padding", scalarNode(padding)],
|
|
286
|
+
]);
|
|
287
|
+
expect(materialize(node)).toEqual(data);
|
|
288
|
+
const binding = await eventDigest(
|
|
289
|
+
objectNode([
|
|
290
|
+
["sequence", scalarNode(sequence)],
|
|
291
|
+
["data", node],
|
|
292
|
+
]),
|
|
293
|
+
);
|
|
294
|
+
expect(binding).toMatch(
|
|
295
|
+
/^unbound-qualification-event:v2:sha256:[0-9a-f]{64}$/,
|
|
296
|
+
);
|
|
297
|
+
const args = {
|
|
298
|
+
reservation: owned.reservation,
|
|
299
|
+
expectedSequence: sequence - 1,
|
|
300
|
+
event: { sequence, binding, dataJSON: json },
|
|
301
|
+
};
|
|
302
|
+
within(args);
|
|
303
|
+
const receipt = await t.mutation(append, args);
|
|
304
|
+
within(receipt);
|
|
305
|
+
expect(receipt.reservation).toEqual(owned.reservation);
|
|
306
|
+
expect(receipt.sequence).toBe(sequence);
|
|
307
|
+
expect(receipt.binding).toBe(binding);
|
|
308
|
+
const { reservation: omitted, ...descriptor } = receipt;
|
|
309
|
+
void omitted;
|
|
310
|
+
return { descriptor, full: { ...descriptor, dataJSON: json } };
|
|
311
|
+
}
|
|
312
|
+
function eventArgs(
|
|
313
|
+
owned: Awaited<ReturnType<typeof acquire>>,
|
|
314
|
+
sequence: number,
|
|
315
|
+
) {
|
|
316
|
+
const args = {
|
|
317
|
+
operationId: owned.operationId,
|
|
318
|
+
attemptId: owned.reservation.attemptId,
|
|
319
|
+
sequence,
|
|
320
|
+
};
|
|
321
|
+
within(args);
|
|
322
|
+
return args;
|
|
323
|
+
}
|
|
324
|
+
async function snapshot(t: TestClient) {
|
|
325
|
+
const operations = await t.run((ctx) =>
|
|
326
|
+
ctx.db.query("renewalOperations").take(4),
|
|
327
|
+
);
|
|
328
|
+
const attempts = await t.run((ctx) =>
|
|
329
|
+
ctx.db.query("renewalOperationAttempts").take(4),
|
|
330
|
+
);
|
|
331
|
+
const events = await t.run((ctx) =>
|
|
332
|
+
ctx.db.query("renewalOperationEvents").take(5),
|
|
333
|
+
);
|
|
334
|
+
const ids = await t.run(async (ctx) =>
|
|
335
|
+
(await ctx.db.query("renewalOperationPayloads").take(7)).map(
|
|
336
|
+
(row) => row._id,
|
|
337
|
+
),
|
|
338
|
+
);
|
|
339
|
+
for (const reply of [operations, attempts, events, ids]) within(reply);
|
|
340
|
+
const payloads = [];
|
|
341
|
+
for (const id of ids) {
|
|
342
|
+
const row = await t.run((ctx) =>
|
|
343
|
+
ctx.db.get("renewalOperationPayloads", id),
|
|
344
|
+
);
|
|
345
|
+
expect(row).not.toBeNull();
|
|
346
|
+
if (!row) throw Error("SCOPED_HISTORY_PAYLOAD_MISSING");
|
|
347
|
+
within(row);
|
|
348
|
+
payloads.push(row);
|
|
349
|
+
}
|
|
350
|
+
for (const row of [...operations, ...attempts, ...events, ...payloads])
|
|
351
|
+
expect(getDocumentSize(row) <= limit).toBe(true);
|
|
352
|
+
return { operations, attempts, events, payloads };
|
|
353
|
+
}
|
|
354
|
+
async function readOnly(
|
|
355
|
+
h: ReturnType<typeof fixture>,
|
|
356
|
+
task: (before: Awaited<ReturnType<typeof snapshot>>) => Promise<void>,
|
|
357
|
+
) {
|
|
358
|
+
const before = await snapshot(h.t);
|
|
359
|
+
try {
|
|
360
|
+
await task(before);
|
|
361
|
+
} finally {
|
|
362
|
+
const after = await snapshot(h.t);
|
|
363
|
+
expect(JSON.stringify(after) === JSON.stringify(before)).toBe(true);
|
|
364
|
+
expect(h.fetches()).toBe(0);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
it("R02 three large events are fully readable through scoped bounded replies", async () => {
|
|
369
|
+
const pads = Object.freeze(
|
|
370
|
+
["a", "b", "c"].map((char) => char.repeat(380000)),
|
|
371
|
+
);
|
|
372
|
+
const h = fixture();
|
|
373
|
+
const owned = await acquire(h.t, "large-three");
|
|
374
|
+
const events: Awaited<ReturnType<typeof addEvent>>[] = [];
|
|
375
|
+
for (const sequence of [1, 2, 3] as const)
|
|
376
|
+
events.push(await addEvent(h.t, owned, sequence, pads[sequence - 1]));
|
|
377
|
+
const manifestArgs = {
|
|
378
|
+
operationId: owned.operationId,
|
|
379
|
+
attemptId: owned.reservation.attemptId,
|
|
380
|
+
};
|
|
381
|
+
within(manifestArgs);
|
|
382
|
+
expect(
|
|
383
|
+
getConvexSize({
|
|
384
|
+
reservation: owned.reservation,
|
|
385
|
+
events: events.map((event) => event.full),
|
|
386
|
+
}) > limit,
|
|
387
|
+
).toBe(true);
|
|
388
|
+
await readOnly(h, async (before) => {
|
|
389
|
+
expect(before.operations).toHaveLength(1);
|
|
390
|
+
expect(before.attempts).toHaveLength(1);
|
|
391
|
+
expect(before.events).toHaveLength(3);
|
|
392
|
+
expect(before.payloads).toHaveLength(4);
|
|
393
|
+
expect(before.operations[0].state).toBe("RESERVED");
|
|
394
|
+
expect(before.operations[0].revision).toBe(1);
|
|
395
|
+
expect(before.attempts[0].lastSequence).toBe(3);
|
|
396
|
+
for (const expected of events) {
|
|
397
|
+
const row = before.events.find(
|
|
398
|
+
(r) => r._id === expected.descriptor.eventId,
|
|
399
|
+
);
|
|
400
|
+
expect(row).toBeDefined();
|
|
401
|
+
if (!row) throw Error("SCOPED_HISTORY_EVENT_MISSING");
|
|
402
|
+
const { _id, _creationTime, ...fields } = row;
|
|
403
|
+
void _creationTime;
|
|
404
|
+
expect({ ...fields, eventId: _id }).toEqual(expected.descriptor);
|
|
405
|
+
expect(
|
|
406
|
+
before.payloads.find((p) => p._id === expected.descriptor.payloadId)
|
|
407
|
+
?.json === expected.full.dataJSON,
|
|
408
|
+
).toBe(true);
|
|
409
|
+
}
|
|
410
|
+
const manifest = await h.t.query(readManifest, manifestArgs);
|
|
411
|
+
within(manifest);
|
|
412
|
+
expect(manifest.reservation).toEqual(owned.reservation);
|
|
413
|
+
expect(manifest.events).toEqual(events.map((event) => event.descriptor));
|
|
414
|
+
for (const [index, expected] of events.entries()) {
|
|
415
|
+
const actual = await h.t.query(readEvent, eventArgs(owned, index + 1));
|
|
416
|
+
within(actual);
|
|
417
|
+
expect(actual !== null).toBe(true);
|
|
418
|
+
if (!actual) throw Error("SCOPED_EVENT_UNEXPECTED_NULL");
|
|
419
|
+
const { dataJSON: completeJSON, ...descriptor } = actual;
|
|
420
|
+
expect(descriptor).toEqual(expected.descriptor);
|
|
421
|
+
expect(completeJSON === expected.full.dataJSON).toBe(true);
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it("R03 foreign operation and attempt pairs cannot retrieve another event", async () => {
|
|
427
|
+
const h = fixture();
|
|
428
|
+
const a = await acquire(h.t, "scope-A");
|
|
429
|
+
const b = await acquire(h.t, "scope-B");
|
|
430
|
+
await addEvent(h.t, a, 1, "event-A");
|
|
431
|
+
await addEvent(h.t, b, 1, "event-B");
|
|
432
|
+
await readOnly(h, async () => {
|
|
433
|
+
for (const [op, attempt] of [
|
|
434
|
+
[a, b],
|
|
435
|
+
[b, a],
|
|
436
|
+
]) {
|
|
437
|
+
const args = {
|
|
438
|
+
...eventArgs(op, 1),
|
|
439
|
+
attemptId: attempt.reservation.attemptId,
|
|
440
|
+
};
|
|
441
|
+
within(args);
|
|
442
|
+
await expect(h.t.query(readEvent, args)).rejects.toThrow(
|
|
443
|
+
"RENEWAL_JOURNAL_CONFLICT",
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
it("R04 PREPARED operation has no active attempt to read", async () => {
|
|
449
|
+
const h = fixture();
|
|
450
|
+
const a = await acquire(h.t, "active");
|
|
451
|
+
await addEvent(h.t, a, 1, "active-event");
|
|
452
|
+
const p = await prepare(h.t, "prepared-only");
|
|
453
|
+
await readOnly(h, async () => {
|
|
454
|
+
const args = { ...eventArgs(a, 1), operationId: p.operationId };
|
|
455
|
+
within(args);
|
|
456
|
+
await expect(h.t.query(readEvent, args)).rejects.toThrow(
|
|
457
|
+
"RENEWAL_JOURNAL_CONFLICT",
|
|
458
|
+
);
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
it("R05 valid uncommitted sequences return null without losing the event", async () => {
|
|
462
|
+
const h = fixture();
|
|
463
|
+
const a = await acquire(h.t, "missing-sequence");
|
|
464
|
+
const event = await addEvent(h.t, a, 1, "retained-event");
|
|
465
|
+
await readOnly(h, async () => {
|
|
466
|
+
for (const sequence of [2, 3]) {
|
|
467
|
+
const actual = await h.t.query(readEvent, eventArgs(a, sequence));
|
|
468
|
+
within(actual);
|
|
469
|
+
expect(actual).toBeNull();
|
|
470
|
+
}
|
|
471
|
+
const actual = await h.t.query(readEvent, eventArgs(a, 1));
|
|
472
|
+
within(actual);
|
|
473
|
+
expect(actual).toEqual(event.full);
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
it.each([0, 4])(
|
|
477
|
+
"R06 sequence %s is rejected by the registered args validator",
|
|
478
|
+
async (sequence) => {
|
|
479
|
+
const h = fixture();
|
|
480
|
+
const a = await acquire(h.t, "invalid-sequence");
|
|
481
|
+
await addEvent(h.t, a, 1, "validator-prerequisite");
|
|
482
|
+
await readOnly(h, async () => {
|
|
483
|
+
await expect(
|
|
484
|
+
h.t.query(readEvent, eventArgs(a, sequence)),
|
|
485
|
+
).rejects.toThrow(/^Validator error:/);
|
|
486
|
+
});
|
|
487
|
+
},
|
|
488
|
+
);
|
|
489
|
+
it("R07 an actual created then deleted operation returns null", async () => {
|
|
490
|
+
const h = fixture();
|
|
491
|
+
const a = await acquire(h.t, "retained-attempt");
|
|
492
|
+
await addEvent(h.t, a, 1, "retained-event");
|
|
493
|
+
const removed = await prepare(h.t, "removed-operation");
|
|
494
|
+
// Explicit damaged-fixture setup, not normal API behavior or a fabricated ID.
|
|
495
|
+
await h.t.run((ctx) =>
|
|
496
|
+
ctx.db.delete("renewalOperations", removed.operationId),
|
|
497
|
+
);
|
|
498
|
+
await readOnly(h, async () => {
|
|
499
|
+
const args = { ...eventArgs(a, 1), operationId: removed.operationId };
|
|
500
|
+
within(args);
|
|
501
|
+
const actual = await h.t.query(readEvent, args);
|
|
502
|
+
within(actual);
|
|
503
|
+
expect(actual).toBeNull();
|
|
504
|
+
});
|
|
505
|
+
});
|
|
506
|
+
it.each(["gap", "binding", "source"] as const)(
|
|
507
|
+
"R08 %s damage refuses even a missing sequence",
|
|
508
|
+
async (damage) => {
|
|
509
|
+
const h = fixture();
|
|
510
|
+
const a = await acquire(h.t, "corrupt-chain");
|
|
511
|
+
const first = await addEvent(h.t, a, 1, "first");
|
|
512
|
+
await addEvent(h.t, a, 2, "second");
|
|
513
|
+
// Freeze actual damage before readonly oracles. Queries do not cause damage.
|
|
514
|
+
if (damage === "gap") {
|
|
515
|
+
await h.t.run((ctx) =>
|
|
516
|
+
ctx.db.delete("renewalOperationEvents", first.descriptor.eventId),
|
|
517
|
+
);
|
|
518
|
+
} else if (damage === "binding") {
|
|
519
|
+
await h.t.run((ctx) =>
|
|
520
|
+
ctx.db.patch("renewalOperationEvents", first.descriptor.eventId, {
|
|
521
|
+
binding: "changed-storage-hash",
|
|
522
|
+
}),
|
|
523
|
+
);
|
|
524
|
+
} else {
|
|
525
|
+
const b = await acquire(h.t, "foreign-source");
|
|
526
|
+
await h.t.run((ctx) =>
|
|
527
|
+
ctx.db.patch("renewalOperationEvents", first.descriptor.eventId, {
|
|
528
|
+
sourcePayloadId: b.reservation.payloadId,
|
|
529
|
+
}),
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
await readOnly(h, async (before) => {
|
|
533
|
+
const damaged = before.events.find(
|
|
534
|
+
(row) => row._id === first.descriptor.eventId,
|
|
535
|
+
);
|
|
536
|
+
if (damage === "gap") expect(damaged).toBeUndefined();
|
|
537
|
+
else expect(damaged).toBeDefined();
|
|
538
|
+
if (damage === "binding")
|
|
539
|
+
expect(damaged?.binding).toBe("changed-storage-hash");
|
|
540
|
+
if (damage === "source")
|
|
541
|
+
expect(damaged?.sourcePayloadId !== a.reservation.payloadId).toBe(true);
|
|
542
|
+
for (const sequence of [2, 3])
|
|
543
|
+
await expect(
|
|
544
|
+
h.t.query(readEvent, eventArgs(a, sequence)),
|
|
545
|
+
).rejects.toThrow("RENEWAL_JOURNAL_CONFLICT");
|
|
546
|
+
});
|
|
547
|
+
},
|
|
548
|
+
);
|
|
549
|
+
|
|
550
|
+
it("R06b closed query args reject an arbitrary payload ID selector", async () => {
|
|
551
|
+
const h = fixture();
|
|
552
|
+
const a = await acquire(h.t, "no-payload-selector");
|
|
553
|
+
const event = await addEvent(h.t, a, 1, "retained-event");
|
|
554
|
+
await readOnly(h, async () => {
|
|
555
|
+
const args = { ...eventArgs(a, 1), payloadId: event.descriptor.payloadId };
|
|
556
|
+
within(args);
|
|
557
|
+
await expect(h.t.query(readEvent, args)).rejects.toThrow(
|
|
558
|
+
/^Validator error:/,
|
|
559
|
+
);
|
|
560
|
+
});
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
it("R09 identical foreign payload cannot replace the scoped event payload", async () => {
|
|
564
|
+
const h = fixture();
|
|
565
|
+
const a = await acquire(h.t, "payload-owner-A");
|
|
566
|
+
const b = await acquire(h.t, "payload-owner-B");
|
|
567
|
+
const first = await addEvent(h.t, a, 1, "same-complete-DATA");
|
|
568
|
+
const second = await addEvent(h.t, b, 1, "same-complete-DATA");
|
|
569
|
+
expect(a.operationId).not.toBe(b.operationId);
|
|
570
|
+
expect(a.reservation.attemptId).not.toBe(b.reservation.attemptId);
|
|
571
|
+
expect(first.descriptor.payloadId).not.toBe(second.descriptor.payloadId);
|
|
572
|
+
expect(first.full.dataJSON).toBe(second.full.dataJSON);
|
|
573
|
+
expect(first.descriptor.binding).toBe(second.descriptor.binding);
|
|
574
|
+
// Explicit fixture corruption changes only the A event's payload pointer.
|
|
575
|
+
await h.t.run((ctx) =>
|
|
576
|
+
ctx.db.patch("renewalOperationEvents", first.descriptor.eventId, {
|
|
577
|
+
payloadId: second.descriptor.payloadId,
|
|
578
|
+
}),
|
|
579
|
+
);
|
|
580
|
+
await readOnly(h, async (before) => {
|
|
581
|
+
const { eventId, ...originalFields } = first.descriptor;
|
|
582
|
+
expect(before.events.find((row) => row._id === eventId)).toMatchObject({
|
|
583
|
+
...originalFields,
|
|
584
|
+
_id: eventId,
|
|
585
|
+
payloadId: second.descriptor.payloadId,
|
|
586
|
+
});
|
|
587
|
+
expect(
|
|
588
|
+
before.payloads.find((row) => row._id === second.descriptor.payloadId)
|
|
589
|
+
?.json,
|
|
590
|
+
).toBe(second.full.dataJSON);
|
|
591
|
+
await expect(h.t.query(readEvent, eventArgs(a, 1))).rejects.toThrow(
|
|
592
|
+
"RENEWAL_JOURNAL_CONFLICT",
|
|
593
|
+
);
|
|
594
|
+
});
|
|
595
|
+
});
|