@fantastic.dev/better-stripe 0.96.0 → 0.97.1

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 (37) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/c10/appendAck.d.ts +7 -0
  4. package/dist/client/billing/renewalAttach/internal/c10/appendAck.d.ts.map +1 -1
  5. package/dist/client/billing/renewalAttach/internal/c10/appendAck.js +11 -3
  6. package/dist/client/billing/renewalAttach/internal/c10/appendAck.js.map +1 -1
  7. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.d.ts +23 -0
  8. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.d.ts.map +1 -1
  9. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.js +32 -2
  10. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.js.map +1 -1
  11. package/dist/client/billing/renewalAttach/internal/material/classifiers.d.ts +11 -0
  12. package/dist/client/billing/renewalAttach/internal/material/classifiers.d.ts.map +1 -1
  13. package/dist/client/billing/renewalAttach/internal/material/classifiers.js +57 -22
  14. package/dist/client/billing/renewalAttach/internal/material/classifiers.js.map +1 -1
  15. package/dist/client/billing/renewalAttach/internal/operationJournal.d.ts +1 -0
  16. package/dist/client/billing/renewalAttach/internal/operationJournal.d.ts.map +1 -1
  17. package/dist/client/billing/renewalAttach/internal/operationJournal.js +5 -1
  18. package/dist/client/billing/renewalAttach/internal/operationJournal.js.map +1 -1
  19. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts +4 -0
  20. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts.map +1 -1
  21. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js +13 -0
  22. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js.map +1 -1
  23. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts +1 -0
  24. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts.map +1 -1
  25. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js +2 -1
  26. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js.map +1 -1
  27. package/docs/renewal-context-reads.md +25 -0
  28. package/package.json +1 -1
  29. package/src/client/billing/renewalAttach/internal/c10/appendAck.ts +14 -3
  30. package/src/client/billing/renewalAttach/internal/c10/attachEvents.ts +39 -2
  31. package/src/client/billing/renewalAttach/internal/material/classifiers.ts +64 -22
  32. package/src/client/billing/renewalAttach/internal/operationJournal.ts +6 -2
  33. package/src/client/billing/renewalAttach/internal/producer/invocationProducer.ts +13 -0
  34. package/src/client/billing/renewalAttach/internal/producer/lineageAuthority.ts +7 -4
  35. package/src/client/billing/renewalAttach/internal/producer/producerChannels.integration.test.ts +350 -0
  36. package/src/client/billing/renewalAttach/internal/producer/producerMaterial.integration.test.ts +251 -0
  37. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +45 -6
@@ -0,0 +1,350 @@
1
+ import assert from "node:assert/strict";
2
+ import { afterEach, beforeEach, expect, it, vi } from "vitest";
3
+ import { initialOperations } from "../test-fixtures/producerInitialOperation.js";
4
+ import {
5
+ INITIAL_NS,
6
+ INITIAL_WALL,
7
+ } from "../test-fixtures/producerInitialDeclarations.js";
8
+ import {
9
+ captureInitialInvocationCore,
10
+ type InitialInvocationCore,
11
+ } from "./invocationProducer.js";
12
+ import {
13
+ prepareRenewalPriceReplacementV2,
14
+ prepareRenewalScheduleAttachmentV2,
15
+ constructRenewalScheduleAttachmentReceiptV2,
16
+ } from "../../../renewalV2/index.js";
17
+ import { receiptInput } from "../acquisition/test-fixtures/fixture.mjs";
18
+ import { createInitialAttachEventCompiler } from "../c10/attachEvents.js";
19
+
20
+ async function rebuiltRequest(recordJSON: string) {
21
+ const source = JSON.parse(recordJSON).attachRequest;
22
+ const prepared = await prepareRenewalPriceReplacementV2(source.preparation);
23
+ return prepareRenewalScheduleAttachmentV2({
24
+ prepared,
25
+ apiVersion: source.apiVersion,
26
+ attachKey: source.attachKey,
27
+ firstDispatchedAt: source.firstDispatchedAt,
28
+ });
29
+ }
30
+
31
+ const legacy = vi.hoisted(() => ({ touched: 0 }));
32
+ vi.mock("../originalOwner.js", () => ({
33
+ OWNER: Object.freeze({
34
+ get basis() {
35
+ legacy.touched++;
36
+ throw Error("LEGACY_OWNER_POISON");
37
+ },
38
+ check() {
39
+ legacy.touched++;
40
+ throw Error("LEGACY_OWNER_POISON");
41
+ },
42
+ sample() {
43
+ legacy.touched++;
44
+ throw Error("LEGACY_OWNER_POISON");
45
+ },
46
+ subscribe() {
47
+ legacy.touched++;
48
+ throw Error("LEGACY_OWNER_POISON");
49
+ },
50
+ retire() {
51
+ legacy.touched++;
52
+ throw Error("LEGACY_OWNER_POISON");
53
+ },
54
+ }),
55
+ }));
56
+ beforeEach(() => {
57
+ legacy.touched = 0;
58
+ vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"] });
59
+ vi.setSystemTime(INITIAL_WALL);
60
+ vi.spyOn(process.hrtime, "bigint").mockReturnValue(INITIAL_NS);
61
+ });
62
+ afterEach(() => {
63
+ vi.useRealTimers();
64
+ vi.unstubAllGlobals();
65
+ vi.restoreAllMocks();
66
+ });
67
+
68
+ it("compiles initial dispatch through its actual Owner and commits before C10 ACK with legacy Owner poisoned", async () => {
69
+ await initialOperations(async ({ fresh, journal }) => {
70
+ assert(fresh.result.disposition === "acquired");
71
+ const request = await rebuiltRequest(fresh.result.reservation.recordJSON);
72
+ const core = await captureInitialInvocationCore(fresh.call, journal);
73
+ try {
74
+ expect(typeof core.attachEvents).toBe("function");
75
+ const compiler = core.attachEvents();
76
+ const result = await compiler.dispatch({
77
+ event: "dispatch",
78
+ stage: "attach",
79
+ request,
80
+ });
81
+ expect(result.kind).toBe("C10_EVENT_DATA_APPENDED");
82
+ const committed = await journal.readStageEvent({
83
+ attemptId: fresh.result.reservation.attemptId,
84
+ sequence: 1,
85
+ });
86
+ expect(committed).not.toBeNull();
87
+ expect(legacy.touched).toBe(0);
88
+ expect(() => core.attachEvents()).toThrow();
89
+ } finally {
90
+ core.owner.retire();
91
+ }
92
+ });
93
+ });
94
+
95
+ it("two pending registered-Action channels keep retirement and ACK independent", async () => {
96
+ const cores: InitialInvocationCore[] = [];
97
+ let ready = () => {};
98
+ let appended = () => {};
99
+ let retired = () => {};
100
+ let release = () => {};
101
+ let appends = 0;
102
+ const bothReady = new Promise<void>((resolve) => {
103
+ ready = resolve;
104
+ });
105
+ const bothAppended = new Promise<void>((resolve) => {
106
+ appended = resolve;
107
+ });
108
+ const firstRetired = new Promise<void>((resolve) => {
109
+ retired = resolve;
110
+ });
111
+ const gate = new Promise<void>((resolve) => {
112
+ release = resolve;
113
+ });
114
+ await initialOperations(
115
+ async ({ fresh, journal, index }) => {
116
+ assert(fresh.result.disposition === "acquired");
117
+ const request = await rebuiltRequest(fresh.result.reservation.recordJSON);
118
+ const core = await captureInitialInvocationCore(fresh.call, journal);
119
+ cores[index] = core;
120
+ try {
121
+ const compiler = core.attachEvents();
122
+ if (cores.filter(Boolean).length === 2) ready();
123
+ await bothReady;
124
+ const pending = compiler.dispatch({
125
+ event: "dispatch",
126
+ stage: "attach",
127
+ request,
128
+ });
129
+ const observed = pending.then(
130
+ () => true,
131
+ () => false,
132
+ );
133
+ await bothAppended;
134
+ expect(cores[0].owner === cores[1].owner).toBe(false);
135
+ if (index === 0) {
136
+ core.owner.retire();
137
+ expect(await observed).toBe(false);
138
+ retired();
139
+ } else {
140
+ await firstRetired;
141
+ release();
142
+ expect(await observed).toBe(true);
143
+ expect(core.owner.check().retired).toBe(false);
144
+ }
145
+ expect(
146
+ await journal.readStageEvent({
147
+ attemptId: fresh.result.reservation.attemptId,
148
+ sequence: 1,
149
+ }),
150
+ ).not.toBeNull();
151
+ expect(await journal.readProviderAcknowledgement()).toBeNull();
152
+ expect(legacy.touched).toBe(0);
153
+ } finally {
154
+ release();
155
+ core.owner.retire();
156
+ }
157
+ },
158
+ 2,
159
+ {
160
+ afterAppend: async () => {
161
+ if (++appends === 2) appended();
162
+ await gate;
163
+ },
164
+ },
165
+ );
166
+ });
167
+
168
+ it("a retired Owner refuses channel capture without appending or touching legacy state", async () => {
169
+ await initialOperations(async ({ fresh, journal }) => {
170
+ assert(fresh.result.disposition === "acquired");
171
+ const core = await captureInitialInvocationCore(fresh.call, journal);
172
+ expect(typeof core.attachEvents).toBe("function");
173
+ core.owner.retire();
174
+ expect(() => core.attachEvents()).toThrow("INITIAL_OWNER_STOP");
175
+ expect(
176
+ await journal.readStageEvent({
177
+ attemptId: fresh.result.reservation.attemptId,
178
+ sequence: 1,
179
+ }),
180
+ ).toBeNull();
181
+ expect(legacy.touched).toBe(0);
182
+ });
183
+ });
184
+
185
+ it("legacy EMPTY initial capture refuses before the poisoned Owner", async () => {
186
+ const { captureOwnedInitialAttachV2 } =
187
+ await import("../operations/initialAttach.js");
188
+ const actor = captureOwnedInitialAttachV2();
189
+ expect(await actor.invoke()).toMatchObject({
190
+ status: "not_dispatched",
191
+ reason: "no_qualified_native_class",
192
+ });
193
+ expect(legacy.touched).toBe(0);
194
+ });
195
+
196
+ it("rejects a replacement sink before reading it and spends channel capture", async () => {
197
+ await initialOperations(async ({ fresh, journal }) => {
198
+ const core = await captureInitialInvocationCore(fresh.call, journal);
199
+ let touched = 0;
200
+ const replacement = {
201
+ get send() {
202
+ touched++;
203
+ throw Error("FOREIGN_SINK");
204
+ },
205
+ };
206
+ try {
207
+ expect(() =>
208
+ Reflect.apply(core.attachEvents, undefined, [replacement]),
209
+ ).toThrow("INITIAL_CHANNEL_UNBOUND");
210
+ expect(() => core.attachEvents()).toThrow("INITIAL_CHANNEL_CONSUMED");
211
+ expect(touched).toBe(0);
212
+ expect(legacy.touched).toBe(0);
213
+ } finally {
214
+ core.owner.retire();
215
+ }
216
+ });
217
+ });
218
+
219
+ it("refuses a copied lineage token before clock or legacy access", () => {
220
+ const now = vi.spyOn(Date, "now");
221
+ expect(() => createInitialAttachEventCompiler(Object.freeze({}))).toThrow(
222
+ "INITIAL_LINEAGE_UNBOUND",
223
+ );
224
+ expect(now).not.toHaveBeenCalled();
225
+ expect(legacy.touched).toBe(0);
226
+ });
227
+
228
+ it("compiles the complete initial three-event history through actual component mutations", async () => {
229
+ const declared = receiptInput();
230
+ const receipt = await constructRenewalScheduleAttachmentReceiptV2({
231
+ attachRequest: declared.attachRequest,
232
+ actualDispatch: {
233
+ ...declared.actualDispatch,
234
+ scope: "platform",
235
+ method: "POST",
236
+ path: "/v1/subscription_schedules",
237
+ },
238
+ providerSnapshot: declared.providerSnapshot,
239
+ });
240
+ await initialOperations(async ({ fresh, journal }) => {
241
+ assert(fresh.result.disposition === "acquired");
242
+ const reservation = fresh.result.reservation;
243
+ const request = await rebuiltRequest(reservation.recordJSON);
244
+ const core = await captureInitialInvocationCore(fresh.call, journal);
245
+ try {
246
+ const compiler = core.attachEvents();
247
+ await compiler.dispatch({ event: "dispatch", stage: "attach", request });
248
+ await compiler.response({
249
+ event: "response",
250
+ stage: "attach",
251
+ request,
252
+ body: declared.providerSnapshot,
253
+ status: 200,
254
+ });
255
+ await compiler.receipt({
256
+ event: "receipt",
257
+ stage: "attach",
258
+ request,
259
+ receipt,
260
+ });
261
+ const events = [];
262
+ for (const sequence of [1, 2, 3] as const) {
263
+ const saved = await journal.readStageEvent({
264
+ attemptId: reservation.attemptId,
265
+ sequence,
266
+ });
267
+ assert(saved);
268
+ events.push(saved);
269
+ }
270
+ expect(events.map((event) => event.sequence)).toEqual([1, 2, 3]);
271
+ expect(await journal.readProviderAcknowledgement()).toBeNull();
272
+ expect(legacy.touched).toBe(0);
273
+ expect(core.operationalAuthority).toBe(false);
274
+ } finally {
275
+ core.owner.retire();
276
+ }
277
+ });
278
+ });
279
+
280
+ it.each([false, true])(
281
+ "a committed append delayed before its reply cannot ACK early, retire=%s",
282
+ async (retire) => {
283
+ let entered = () => {};
284
+ let release = () => {};
285
+ const reached = new Promise<void>((resolve) => {
286
+ entered = resolve;
287
+ });
288
+ const gate = new Promise<void>((resolve) => {
289
+ release = resolve;
290
+ });
291
+ await initialOperations(
292
+ async ({ fresh, journal }) => {
293
+ assert(fresh.result.disposition === "acquired");
294
+ const reservation = fresh.result.reservation;
295
+ const request = await rebuiltRequest(reservation.recordJSON);
296
+ const core = await captureInitialInvocationCore(fresh.call, journal);
297
+ try {
298
+ const compiler = core.attachEvents();
299
+ let settled = false;
300
+ const pending = compiler.dispatch({
301
+ event: "dispatch",
302
+ stage: "attach",
303
+ request,
304
+ });
305
+ const observed = pending.then(
306
+ (value) => {
307
+ settled = true;
308
+ return { ok: true, value };
309
+ },
310
+ (error: unknown) => {
311
+ settled = true;
312
+ return { ok: false, error };
313
+ },
314
+ );
315
+ await reached;
316
+ const committed = await journal.readStageEvent({
317
+ attemptId: reservation.attemptId,
318
+ sequence: 1,
319
+ });
320
+ expect(committed).not.toBeNull();
321
+ expect(settled).toBe(false);
322
+ if (retire) {
323
+ core.owner.retire();
324
+ expect((await observed).ok).toBe(false);
325
+ }
326
+ release();
327
+ const result = await observed;
328
+ expect(result.ok).toBe(!retire);
329
+ expect(
330
+ await journal.readStageEvent({
331
+ attemptId: reservation.attemptId,
332
+ sequence: 1,
333
+ }),
334
+ ).toEqual(committed);
335
+ expect(legacy.touched).toBe(0);
336
+ } finally {
337
+ release();
338
+ core.owner.retire();
339
+ }
340
+ },
341
+ 1,
342
+ {
343
+ afterAppend: async () => {
344
+ entered();
345
+ await gate;
346
+ },
347
+ },
348
+ );
349
+ },
350
+ );
@@ -0,0 +1,251 @@
1
+ import assert from "node:assert/strict";
2
+ import { afterEach, beforeEach, expect, it, vi } from "vitest";
3
+ import { initialOperations } from "../test-fixtures/producerInitialOperation.js";
4
+ import { declaredInputs } from "../test-fixtures/attachEntries.js";
5
+ import { INITIAL_NS } from "../test-fixtures/producerInitialDeclarations.js";
6
+ import {
7
+ captureInitialInvocationCore,
8
+ type InitialInvocationCore,
9
+ } from "./invocationProducer.js";
10
+ import {
11
+ prepareRenewalPriceReplacementV2,
12
+ prepareRenewalScheduleAttachmentV2,
13
+ } from "../../../renewalV2/index.js";
14
+ import { createInitialMaterialClassifiers } from "../material/classifiers.js";
15
+
16
+ const legacy = vi.hoisted(() => ({ touched: 0 }));
17
+ vi.mock("../originalOwner.js", () => ({
18
+ OWNER: Object.freeze({
19
+ get basis() {
20
+ legacy.touched++;
21
+ throw Error("LEGACY_OWNER_POISON");
22
+ },
23
+ check() {
24
+ legacy.touched++;
25
+ throw Error("LEGACY_OWNER_POISON");
26
+ },
27
+ sample() {
28
+ legacy.touched++;
29
+ throw Error("LEGACY_OWNER_POISON");
30
+ },
31
+ subscribe() {
32
+ legacy.touched++;
33
+ throw Error("LEGACY_OWNER_POISON");
34
+ },
35
+ retire() {
36
+ legacy.touched++;
37
+ throw Error("LEGACY_OWNER_POISON");
38
+ },
39
+ }),
40
+ }));
41
+ beforeEach(() => {
42
+ legacy.touched = 0;
43
+ vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"] });
44
+ vi.setSystemTime(
45
+ (declaredInputs("INITIAL-ACK").original.attachRequest.firstDispatchedAt +
46
+ 1) *
47
+ 1000,
48
+ );
49
+ vi.spyOn(process.hrtime, "bigint").mockReturnValue(INITIAL_NS);
50
+ });
51
+ afterEach(() => {
52
+ vi.useRealTimers();
53
+ vi.unstubAllGlobals();
54
+ vi.restoreAllMocks();
55
+ });
56
+
57
+ it("classifies original retained and fresh Coupon material with its actual invocation Owner", async () => {
58
+ const source = declaredInputs("INITIAL-ACK").original;
59
+ const prepared = await prepareRenewalPriceReplacementV2(
60
+ source.attachRequest.preparation,
61
+ );
62
+ const request = await prepareRenewalScheduleAttachmentV2({
63
+ prepared,
64
+ apiVersion: source.attachRequest.apiVersion,
65
+ attachKey: source.attachRequest.attachKey,
66
+ firstDispatchedAt: source.attachRequest.firstDispatchedAt,
67
+ });
68
+ await initialOperations(
69
+ async ({ fresh, journal }) => {
70
+ const core = await captureInitialInvocationCore(fresh.call, journal);
71
+ try {
72
+ expect(core.material).toBeDefined();
73
+ const retained = await core.material.retained();
74
+ expect(retained.status).toBe("classified");
75
+ assert(retained.status === "classified");
76
+ expect(retained.kind).toBe("RENEWAL_MATERIAL_V2_DATA_CONSISTENCY_ONLY");
77
+ expect(retained.material.originalPrepared).toEqual(prepared);
78
+ expect(retained.material.completeRequest).toEqual(request);
79
+ expect(retained.material.rawSources.attachRequest).toEqual(
80
+ source.attachRequest,
81
+ );
82
+ const current = structuredClone(source.originalBundle);
83
+ const live = await core.material.fresh(
84
+ current,
85
+ source.attachRequest.firstDispatchedAt,
86
+ );
87
+ expect(live.status).toBe("classified");
88
+ assert(live.status === "classified");
89
+ expect(live.material.freshness).toBe("fresh");
90
+ expect(live.material.completeRequest).toEqual(request);
91
+ expect(Object.isFrozen(live.material)).toBe(true);
92
+ expect(legacy.touched).toBe(0);
93
+ expect(core.operationalAuthority).toBe(false);
94
+ } finally {
95
+ core.owner.retire();
96
+ }
97
+ },
98
+ 1,
99
+ {
100
+ source,
101
+ workEndWallMs: (source.attachRequest.firstDispatchedAt + 4) * 1000,
102
+ endWallMs: (source.attachRequest.firstDispatchedAt + 5) * 1000,
103
+ },
104
+ );
105
+ });
106
+
107
+ it("refuses replacement retained source and incompatible fresh material without legacy access", async () => {
108
+ const source = declaredInputs("INITIAL-ACK").original;
109
+ await initialOperations(
110
+ async ({ fresh, journal }) => {
111
+ const core = await captureInitialInvocationCore(fresh.call, journal);
112
+ try {
113
+ expect(core.material).toBeDefined();
114
+ let read = 0;
115
+ const foreign = {
116
+ get attachRequest() {
117
+ read++;
118
+ throw Error("FOREIGN_SOURCE");
119
+ },
120
+ };
121
+ expect(() =>
122
+ Reflect.apply(core.material.retained, undefined, [foreign]),
123
+ ).toThrow("INITIAL_MATERIAL_UNBOUND");
124
+ expect(read).toBe(0);
125
+ const current = structuredClone(source.originalBundle);
126
+ current.wireSubscription.schedule = "sub_sched_foreign";
127
+ expect(
128
+ (
129
+ await core.material.fresh(
130
+ current,
131
+ source.attachRequest.firstDispatchedAt,
132
+ )
133
+ ).status,
134
+ ).toBe("unsupported");
135
+ expect((await core.material.retained()).status).toBe("classified");
136
+ core.owner.retire();
137
+ expect(() => core.material.retained()).toThrow("INITIAL_OWNER_STOP");
138
+ expect(legacy.touched).toBe(0);
139
+ } finally {
140
+ core.owner.retire();
141
+ }
142
+ },
143
+ 1,
144
+ {
145
+ source,
146
+ workEndWallMs: (source.attachRequest.firstDispatchedAt + 4) * 1000,
147
+ endWallMs: (source.attachRequest.firstDispatchedAt + 5) * 1000,
148
+ },
149
+ );
150
+ });
151
+
152
+ it("retiring one Owner rejects its pending material reconstruction while another finishes", async () => {
153
+ const source = declaredInputs("INITIAL-ACK").original;
154
+ const cores: InitialInvocationCore[] = [];
155
+ let ready = () => {},
156
+ entered = () => {},
157
+ retired = () => {},
158
+ release = () => {};
159
+ const bothReady = new Promise<void>((resolve) => {
160
+ ready = resolve;
161
+ });
162
+ const bothPending = new Promise<void>((resolve) => {
163
+ entered = resolve;
164
+ });
165
+ const firstRetired = new Promise<void>((resolve) => {
166
+ retired = resolve;
167
+ });
168
+ const gate = new Promise<void>((resolve) => {
169
+ release = resolve;
170
+ });
171
+ let holding = false,
172
+ pending = 0;
173
+ const nativeDigest = crypto.subtle.digest.bind(crypto.subtle);
174
+ vi.spyOn(crypto.subtle, "digest").mockImplementation(
175
+ async (algorithm, data) => {
176
+ const value = await nativeDigest(algorithm, data);
177
+ if (holding) {
178
+ if (++pending === 2) entered();
179
+ await gate;
180
+ }
181
+ return value;
182
+ },
183
+ );
184
+ await initialOperations(
185
+ async ({ fresh, journal, index }) => {
186
+ const core = await captureInitialInvocationCore(fresh.call, journal);
187
+ cores[index] = core;
188
+ try {
189
+ expect(core.material).toBeDefined();
190
+ if (cores.filter(Boolean).length === 2) ready();
191
+ await bothReady;
192
+ holding = true;
193
+ const observed = core.material.retained().then(
194
+ (value) => ({ ok: true, value }),
195
+ (error: unknown) => ({ ok: false, error }),
196
+ );
197
+ await bothPending;
198
+ if (index === 0) {
199
+ core.owner.retire();
200
+ expect((await observed).ok).toBe(false);
201
+ retired();
202
+ } else {
203
+ await firstRetired;
204
+ release();
205
+ const result = await observed;
206
+ expect(result.ok).toBe(true);
207
+ assert("value" in result);
208
+ expect(result.value.status).toBe("classified");
209
+ expect(core.owner.check().retired).toBe(false);
210
+ }
211
+ expect(legacy.touched).toBe(0);
212
+ } finally {
213
+ release();
214
+ core.owner.retire();
215
+ }
216
+ },
217
+ 2,
218
+ {
219
+ source,
220
+ workEndWallMs: (source.attachRequest.firstDispatchedAt + 4) * 1000,
221
+ endWallMs: (source.attachRequest.firstDispatchedAt + 5) * 1000,
222
+ },
223
+ );
224
+ });
225
+
226
+ it("opaque stored material remains unsupported rather than gaining source qualification", async () => {
227
+ // The original codec fixture has an opaque bundle instead of raw Coupon joins.
228
+ vi.setSystemTime(1700000002000);
229
+ await initialOperations(async ({ fresh, journal }) => {
230
+ const core = await captureInitialInvocationCore(fresh.call, journal);
231
+ try {
232
+ expect((await core.material.retained()).status).toBe("unsupported");
233
+ expect((await core.material.fresh({}, 1700000002)).status).toBe(
234
+ "unsupported",
235
+ );
236
+ expect(core.operationalAuthority).toBe(false);
237
+ expect(legacy.touched).toBe(0);
238
+ } finally {
239
+ core.owner.retire();
240
+ }
241
+ });
242
+ });
243
+
244
+ it("forged lineage cannot select a classifier Owner or sample clocks", () => {
245
+ const now = vi.spyOn(Date, "now");
246
+ expect(() => createInitialMaterialClassifiers(Object.freeze({}))).toThrow(
247
+ "INITIAL_LINEAGE_UNBOUND",
248
+ );
249
+ expect(now).not.toHaveBeenCalled();
250
+ expect(legacy.touched).toBe(0);
251
+ });
@@ -14,6 +14,7 @@ import {
14
14
  codecFixture,
15
15
  } from "../../../../../component/renewalOperations/timingPolicy.fixtures.js";
16
16
  import { createClassActionOperationJournal } from "../operationJournal.js";
17
+ import type { RetainedInput } from "../material/capture.js";
17
18
  import {
18
19
  prepareRenewalPriceReplacementV2,
19
20
  prepareRenewalScheduleAttachmentV2,
@@ -34,6 +35,8 @@ export async function initialOperations(
34
35
  workEndWallMs?: number;
35
36
  endWallMs?: number;
36
37
  afterBind?: () => Promise<void>;
38
+ afterAppend?: () => Promise<void>;
39
+ source?: RetainedInput;
37
40
  } = {},
38
41
  ) {
39
42
  const h = await codecFixture();
@@ -75,6 +78,35 @@ export async function initialOperations(
75
78
  h.record.timingPolicy.workEndWallMs = options.workEndWallMs;
76
79
  if (options.endWallMs !== undefined)
77
80
  h.record.timingPolicy.endWallMs = options.endWallMs;
81
+ const source = options.source?.attachRequest ?? h.record.attachRequest;
82
+ const prepared = await prepareRenewalPriceReplacementV2(source.preparation);
83
+ const request = await prepareRenewalScheduleAttachmentV2({
84
+ prepared,
85
+ apiVersion: source.apiVersion,
86
+ attachKey: source.attachKey,
87
+ firstDispatchedAt: source.firstDispatchedAt,
88
+ });
89
+ const record = {
90
+ ...h.record,
91
+ attachRequest: source,
92
+ originalBundle: options.source?.originalBundle ?? h.record.originalBundle,
93
+ requestBinding: request.requestIdentity.binding,
94
+ dispatch: {
95
+ scope: request.scope,
96
+ apiVersion: request.apiVersion,
97
+ method: request.method,
98
+ path: request.path,
99
+ payload: request.payload,
100
+ key: request.key,
101
+ firstDispatchedAt: request.firstDispatchedAt,
102
+ },
103
+ timingPolicy: {
104
+ ...h.record.timingPolicy,
105
+ firstDispatchedAt: source.firstDispatchedAt,
106
+ periodStart: prepared.expected.periodStart,
107
+ periodEnd: prepared.expected.periodEnd,
108
+ },
109
+ };
78
110
  const sdk = new BetterStripe(components.betterStripe, {
79
111
  STRIPE_SECRET_KEY: "sk_test_invalid_owner_core",
80
112
  });
@@ -84,7 +116,7 @@ export async function initialOperations(
84
116
  args: {},
85
117
  handler: async (ctx): Promise<null> => {
86
118
  const runMutation = ctx.runMutation;
87
- if (options.afterBind)
119
+ if (options.afterBind || options.afterAppend)
88
120
  ctx.runMutation = async (ref, ...args) => {
89
121
  const result = await runMutation(ref, ...args);
90
122
  if (
@@ -94,18 +126,25 @@ export async function initialOperations(
94
126
  options.afterBind
95
127
  )
96
128
  await options.afterBind();
129
+ if (
130
+ getFunctionAddress(ref).reference?.endsWith(
131
+ "/appendStageEvent",
132
+ ) &&
133
+ options.afterAppend
134
+ )
135
+ await options.afterAppend();
97
136
  return result;
98
137
  };
99
138
  await Promise.all(
100
139
  Array.from({ length: count }, async (_, index) => {
101
140
  const logicalOperationId =
102
- h.record.logicalOperationId + "-owner-" + index;
103
- const recordJSON = canonical({ ...h.record, logicalOperationId });
141
+ record.logicalOperationId + "-owner-" + index;
142
+ const recordJSON = canonical({ ...record, logicalOperationId });
104
143
  const created = await ctx.runMutation(
105
144
  refs.mutations.createAuthorizedOperation,
106
145
  {
107
- authorizationScope: h.record.authorization.scope,
108
- authorizationRevision: h.record.authorization.revision,
146
+ authorizationScope: record.authorization.scope,
147
+ authorizationRevision: record.authorization.revision,
109
148
  logicalOperationId,
110
149
  recordJSON,
111
150
  },
@@ -118,7 +157,7 @@ export async function initialOperations(
118
157
  const fresh = await journal.reserveFreshAttempt({
119
158
  expectedRevision: 0,
120
159
  stage: "attach",
121
- expectedRequestBinding: h.record.requestBinding,
160
+ expectedRequestBinding: record.requestBinding,
122
161
  recordJSON,
123
162
  });
124
163
  assert(fresh.result.disposition === "acquired" && fresh.call);