@fantastic.dev/better-stripe 0.104.0 → 0.106.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 (70) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/c10/configureEvents.d.ts +23 -0
  4. package/dist/client/billing/renewalAttach/internal/c10/configureEvents.d.ts.map +1 -1
  5. package/dist/client/billing/renewalAttach/internal/c10/configureEvents.js +28 -0
  6. package/dist/client/billing/renewalAttach/internal/c10/configureEvents.js.map +1 -1
  7. package/dist/client/billing/renewalAttach/internal/configure/transport.d.ts +300 -0
  8. package/dist/client/billing/renewalAttach/internal/configure/transport.d.ts.map +1 -1
  9. package/dist/client/billing/renewalAttach/internal/configure/transport.js +44 -2
  10. package/dist/client/billing/renewalAttach/internal/configure/transport.js.map +1 -1
  11. package/dist/client/billing/renewalAttach/internal/producer/configureAcknowledgement.d.ts +19 -0
  12. package/dist/client/billing/renewalAttach/internal/producer/configureAcknowledgement.d.ts.map +1 -0
  13. package/dist/client/billing/renewalAttach/internal/producer/configureAcknowledgement.js +11 -0
  14. package/dist/client/billing/renewalAttach/internal/producer/configureAcknowledgement.js.map +1 -0
  15. package/dist/client/billing/renewalAttach/internal/producer/configureAttemptAuthority.d.ts +56 -0
  16. package/dist/client/billing/renewalAttach/internal/producer/configureAttemptAuthority.d.ts.map +1 -0
  17. package/dist/client/billing/renewalAttach/internal/producer/configureAttemptAuthority.js +79 -0
  18. package/dist/client/billing/renewalAttach/internal/producer/configureAttemptAuthority.js.map +1 -0
  19. package/dist/client/billing/renewalAttach/internal/producer/configureFrontier.d.ts +47 -0
  20. package/dist/client/billing/renewalAttach/internal/producer/configureFrontier.d.ts.map +1 -1
  21. package/dist/client/billing/renewalAttach/internal/producer/configureFrontier.js +79 -0
  22. package/dist/client/billing/renewalAttach/internal/producer/configureFrontier.js.map +1 -1
  23. package/dist/client/billing/renewalAttach/internal/producer/configureJournalData.d.ts +25 -0
  24. package/dist/client/billing/renewalAttach/internal/producer/configureJournalData.d.ts.map +1 -1
  25. package/dist/client/billing/renewalAttach/internal/producer/configureJournalData.js +62 -0
  26. package/dist/client/billing/renewalAttach/internal/producer/configureJournalData.js.map +1 -1
  27. package/dist/client/billing/renewalAttach/internal/producer/configureJournalWork.d.ts +14 -0
  28. package/dist/client/billing/renewalAttach/internal/producer/configureJournalWork.d.ts.map +1 -1
  29. package/dist/client/billing/renewalAttach/internal/producer/configureJournalWork.js +17 -0
  30. package/dist/client/billing/renewalAttach/internal/producer/configureJournalWork.js.map +1 -1
  31. package/dist/client/billing/renewalAttach/internal/producer/configureRecovery.d.ts +5 -0
  32. package/dist/client/billing/renewalAttach/internal/producer/configureRecovery.d.ts.map +1 -0
  33. package/dist/client/billing/renewalAttach/internal/producer/configureRecovery.js +259 -0
  34. package/dist/client/billing/renewalAttach/internal/producer/configureRecovery.js.map +1 -0
  35. package/dist/client/billing/renewalAttach/internal/producer/configureWrite.d.ts +7 -0
  36. package/dist/client/billing/renewalAttach/internal/producer/configureWrite.d.ts.map +1 -0
  37. package/dist/client/billing/renewalAttach/internal/producer/configureWrite.js +209 -0
  38. package/dist/client/billing/renewalAttach/internal/producer/configureWrite.js.map +1 -0
  39. package/dist/client/billing/renewalAttach/internal/producer/configureWriteFactory.d.ts +61 -0
  40. package/dist/client/billing/renewalAttach/internal/producer/configureWriteFactory.d.ts.map +1 -0
  41. package/dist/client/billing/renewalAttach/internal/producer/configureWriteFactory.js +73 -0
  42. package/dist/client/billing/renewalAttach/internal/producer/configureWriteFactory.js.map +1 -0
  43. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
  44. package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
  45. package/dist/component/balanceFunding/mutations.d.ts +27 -27
  46. package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
  47. package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
  48. package/dist/component/earnings/triggers.d.ts +16 -16
  49. package/dist/component/payoutRelease/queries.d.ts +2 -2
  50. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  51. package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
  52. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  53. package/dist/component/usageCredits/accounting.fixtures.d.ts +16 -16
  54. package/docs/renewal-context-reads.md +99 -4
  55. package/package.json +1 -1
  56. package/src/client/billing/renewalAttach/internal/c10/configureEvents.ts +37 -0
  57. package/src/client/billing/renewalAttach/internal/configure/transport.ts +64 -2
  58. package/src/client/billing/renewalAttach/internal/producer/configureAcknowledgement.ts +11 -0
  59. package/src/client/billing/renewalAttach/internal/producer/configureAttemptAuthority.ts +121 -0
  60. package/src/client/billing/renewalAttach/internal/producer/configureFrontier.ts +155 -0
  61. package/src/client/billing/renewalAttach/internal/producer/configureJournalData.ts +84 -0
  62. package/src/client/billing/renewalAttach/internal/producer/configureJournalWork.ts +16 -0
  63. package/src/client/billing/renewalAttach/internal/producer/configureRecovery.ts +361 -0
  64. package/src/client/billing/renewalAttach/internal/producer/configureWrite.ts +288 -0
  65. package/src/client/billing/renewalAttach/internal/producer/configureWriteFactory.ts +88 -0
  66. package/src/client/billing/renewalAttach/internal/producer/producerConfigureRecovery.integration.test.ts +99 -0
  67. package/src/client/billing/renewalAttach/internal/producer/producerConfigureRecoveryControls.integration.test.ts +420 -0
  68. package/src/client/billing/renewalAttach/internal/producer/producerConfigureWrite.integration.test.ts +158 -0
  69. package/src/client/billing/renewalAttach/internal/producer/producerConfigureWriteControls.integration.test.ts +537 -0
  70. package/src/client/billing/renewalAttach/internal/test-fixtures/producerConfigureAdmission.ts +85 -2
@@ -0,0 +1,158 @@
1
+ import { object } from "./configureJournalData.js";
2
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
3
+ import {
4
+ configureAdmissionFixture,
5
+ admissionControls,
6
+ configureAdmissionCase,
7
+ prepareAdmittedConfigure,
8
+ } from "../test-fixtures/producerConfigureAdmission.js";
9
+ import { captureAdmittedConfigurePreflight } from "./configurePreflight.js";
10
+ import {
11
+ captureAdmittedConfigureWrite,
12
+ readAdmittedConfigureAcknowledgement,
13
+ readAdmittedConfigureWriteAttempt,
14
+ } from "./configureWrite.js";
15
+
16
+ configureAdmissionCase(
17
+ "commits dispatch, one exact configure POST, retained C9 and receipt publication before returning ACK custody",
18
+ prepareAdmittedConfigure,
19
+ async (h, { child, token }) => {
20
+ const before = h.calls.length;
21
+ const actor = captureAdmittedConfigureWrite(token);
22
+ expect(actor).not.toBeNull();
23
+ const output = await actor!.invoke();
24
+ expect(object(output).status).toBe("configure_acknowledged");
25
+ const view = readAdmittedConfigureAcknowledgement(token);
26
+ expect(view!.output === output).toBe(true);
27
+ expect(view!.receipt === object(output).receipt).toBe(true);
28
+ expect(view!.rawReceipt === object(output).rawReceipt).toBe(true);
29
+ expect(view!.acknowledgement === h.core.attachAcknowledgement()).toBe(true);
30
+ const calls = h.calls.slice(before);
31
+ expect(calls.filter((call) => call.method === "GET")).toHaveLength(6);
32
+ expect(calls.filter((call) => call.method === "POST")).toEqual([
33
+ expect.objectContaining({
34
+ path: child.record.configureRecord.request.path,
35
+ idempotencyKey: child.record.configureRecord.request.key,
36
+ apiVersion: "2026-09-30.endive",
37
+ }),
38
+ ]);
39
+ if (child.fresh.result.disposition !== "acquired")
40
+ throw Error("INVALID_FIXTURE_CAS");
41
+ const history = await child.journal.readAttempt({
42
+ attemptId: child.fresh.result.reservation.attemptId,
43
+ });
44
+ expect(history!.events.map((event) => event.sequence)).toEqual([1, 2, 3]);
45
+ const retained = await child.journal.readProviderAcknowledgement();
46
+ expect(retained).not.toBeNull();
47
+ expect(JSON.parse(retained!.receiptJSON)).toEqual(object(output).receipt);
48
+ expect(admissionControls.legacy).toBe(0);
49
+ },
50
+ );
51
+
52
+ function deferred() {
53
+ let resolve!: () => void;
54
+ const promise = new Promise<void>((done) => {
55
+ resolve = done;
56
+ });
57
+ return { promise, resolve };
58
+ }
59
+ describe("known C9 with oversized whole output", () => {
60
+ let pending: Promise<void>,
61
+ start: ReturnType<typeof deferred>,
62
+ postReady: ReturnType<typeof deferred>,
63
+ releasePost: ReturnType<typeof deferred>,
64
+ run = false,
65
+ armed = false;
66
+ beforeEach(async () => {
67
+ run = false;
68
+ armed = false;
69
+ start = deferred();
70
+ postReady = deferred();
71
+ releasePost = deferred();
72
+ let ready!: () => void, failed!: (error: unknown) => void;
73
+ const prepared = new Promise<void>((resolve, reject) => {
74
+ ready = resolve;
75
+ failed = reject;
76
+ });
77
+ pending = configureAdmissionFixture(
78
+ async (h) => {
79
+ await h.complete();
80
+ const child = await h.child();
81
+ const token = await h.core.admitConfigure(
82
+ child.fresh.call,
83
+ child.journal,
84
+ );
85
+ h.configureReads(child);
86
+ admissionControls.preflight.push({
87
+ originalEntry: admissionControls.entries[0],
88
+ coversPreflight: () => true,
89
+ });
90
+ admissionControls.write.push({
91
+ originalEntry: admissionControls.entries[0],
92
+ coversConfigure: () => true,
93
+ });
94
+ await captureAdmittedConfigurePreflight(token).invoke();
95
+ Reflect.set(h.baseline.schedule, "padding", "x".repeat(300000));
96
+ ready();
97
+ await start.promise;
98
+ if (!run) return;
99
+ armed = true;
100
+ const actor = captureAdmittedConfigureWrite(token);
101
+ expect(object(await actor.invoke()).status).toBe("configure_unknown");
102
+ expect(
103
+ await child.journal.readProviderAcknowledgement(),
104
+ ).not.toBeNull();
105
+ const attempt = readAdmittedConfigureWriteAttempt(token);
106
+ expect(attempt).not.toBeNull();
107
+ expect(attempt!.receiptKnown).toBe(true);
108
+ expect(Object.isFrozen(attempt!.receipt)).toBe(true);
109
+ expect(Object.isFrozen(attempt!.rawReceipt)).toBe(true);
110
+ expect(attempt!.receipt).not.toBeNull();
111
+ expect(attempt!.rawReceipt).not.toBeNull();
112
+ expect(attempt!.returned).toBe(false);
113
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
114
+ await expect(actor.invoke()).rejects.toThrow();
115
+ expect(
116
+ h.calls.filter(
117
+ (call) =>
118
+ call.method === "POST" &&
119
+ call.path === child.record.configureRecord.request.path,
120
+ ),
121
+ ).toHaveLength(1);
122
+ },
123
+ {
124
+ beforeFetch: async (path, method) => {
125
+ if (
126
+ armed &&
127
+ method === "POST" &&
128
+ path.startsWith("/v1/subscription_schedules/")
129
+ ) {
130
+ postReady.resolve();
131
+ await releasePost.promise;
132
+ }
133
+ },
134
+ },
135
+ );
136
+ void pending.catch(failed);
137
+ await prepared;
138
+ });
139
+ beforeEach(async () => {
140
+ run = true;
141
+ start.resolve();
142
+ await Promise.race([
143
+ postReady.promise,
144
+ pending.then(() => {
145
+ throw Error("FIXTURE_SETTLED_BEFORE_POST");
146
+ }),
147
+ ]);
148
+ });
149
+ afterEach(async () => {
150
+ start?.resolve();
151
+ releasePost?.resolve();
152
+ await pending?.catch(() => {});
153
+ });
154
+ it("durably retains actual C9 even when whole-output admission fails", async () => {
155
+ releasePost.resolve();
156
+ await pending;
157
+ });
158
+ });
@@ -0,0 +1,537 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+ import {
3
+ configureAdmissionFixture,
4
+ admissionControls,
5
+ configureAdmissionCase,
6
+ prepareAdmittedConfigure,
7
+ } from "../test-fixtures/producerConfigureAdmission.js";
8
+ import { captureAdmittedConfigurePreflight } from "./configurePreflight.js";
9
+ import {
10
+ captureAdmittedConfigureWrite,
11
+ readAdmittedConfigureAcknowledgement,
12
+ } from "./configureWrite.js";
13
+ import { object } from "./configureJournalData.js";
14
+
15
+ type Harness = Parameters<Parameters<typeof configureAdmissionFixture>[0]>[0];
16
+ async function prepare(h: Harness, preflight = true) {
17
+ await h.complete();
18
+ const child = await h.child();
19
+ const token = await h.core.admitConfigure(child.fresh.call, child.journal);
20
+ const current = h.configureReads(child);
21
+ if (admissionControls.preflight.length === 0)
22
+ admissionControls.preflight.push({
23
+ originalEntry: admissionControls.entries[0],
24
+ coversPreflight: () => true,
25
+ });
26
+ if (admissionControls.write.length === 0)
27
+ admissionControls.write.push({
28
+ originalEntry: admissionControls.entries[0],
29
+ coversConfigure: () => true,
30
+ });
31
+ if (preflight) await captureAdmittedConfigurePreflight(token).invoke();
32
+ return { child, token, current };
33
+ }
34
+ function deferred() {
35
+ let resolve!: () => void;
36
+ const promise = new Promise<void>((done) => {
37
+ resolve = done;
38
+ });
39
+ return { promise, resolve };
40
+ }
41
+ function posts(h: Harness) {
42
+ return h.calls.filter(
43
+ (call) =>
44
+ call.method === "POST" &&
45
+ call.path.startsWith("/v1/subscription_schedules/"),
46
+ );
47
+ }
48
+
49
+ it.each([
50
+ "no-preflight",
51
+ "empty",
52
+ "foreign",
53
+ "ambiguous",
54
+ "copy",
55
+ "proxy",
56
+ "extra",
57
+ ])("refuses %s write custody without provider reads", async (scenario) => {
58
+ await configureAdmissionFixture(async (h) => {
59
+ const { token } = await prepare(h, scenario !== "no-preflight");
60
+ if (scenario === "empty") admissionControls.write.splice(0);
61
+ if (scenario === "foreign") admissionControls.write[0].originalEntry = {};
62
+ if (scenario === "ambiguous")
63
+ admissionControls.write.push({ ...admissionControls.write[0] });
64
+ let traps = 0;
65
+ const copied =
66
+ scenario === "copy"
67
+ ? { ...token }
68
+ : scenario === "proxy"
69
+ ? new Proxy(token, {
70
+ get() {
71
+ traps++;
72
+ throw Error("PROXY_TRAP");
73
+ },
74
+ })
75
+ : token;
76
+ const before = h.calls.length;
77
+ expect(() =>
78
+ scenario === "extra"
79
+ ? Reflect.apply(captureAdmittedConfigureWrite, undefined, [token, {}])
80
+ : captureAdmittedConfigureWrite(copied),
81
+ ).toThrow();
82
+ expect(h.calls).toHaveLength(before);
83
+ expect(posts(h)).toHaveLength(0);
84
+ expect(traps).toBe(0);
85
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
86
+ });
87
+ });
88
+ it("invalid invoke arguments spend the actor without a POST or second frame", async () => {
89
+ await configureAdmissionFixture(async (h) => {
90
+ const { token } = await prepare(h),
91
+ actor = captureAdmittedConfigureWrite(token),
92
+ before = h.calls.length;
93
+ await expect(Reflect.apply(actor.invoke, actor, [{}])).rejects.toThrow();
94
+ await expect(actor.invoke()).rejects.toThrow();
95
+ expect(() => captureAdmittedConfigureWrite(token)).toThrow();
96
+ expect(h.calls).toHaveLength(before);
97
+ });
98
+ });
99
+ it.each([
100
+ "subscription",
101
+ "schedule",
102
+ "predicate",
103
+ "entry-pointer",
104
+ "preflight-pointer",
105
+ ])("changed %s refuses before POST", async (scenario) => {
106
+ await configureAdmissionFixture(async (h) => {
107
+ const { token, current } = await prepare(h),
108
+ actor = captureAdmittedConfigureWrite(token);
109
+ if (scenario === "subscription")
110
+ Reflect.set(current.subscription, "metadata", { changed: true });
111
+ if (scenario === "schedule") current.schedule.metadata = { changed: true };
112
+ if (scenario === "predicate")
113
+ admissionControls.write[0].coversConfigure = () => false;
114
+ if (scenario === "entry-pointer") admissionControls.write.splice(0);
115
+ if (scenario === "preflight-pointer")
116
+ admissionControls.preflight[0].coversPreflight = () => true;
117
+ try {
118
+ await actor.invoke();
119
+ } catch {
120
+ /* Closed source metadata may reject. */
121
+ }
122
+ expect(posts(h)).toHaveLength(0);
123
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
124
+ await expect(actor.invoke()).rejects.toThrow();
125
+ });
126
+ });
127
+ for (const phase of ["dispatch", "retention", "receipt"] as const) {
128
+ const entered = deferred(),
129
+ released = deferred();
130
+ let armed = false,
131
+ appended = 0;
132
+ configureAdmissionCase(
133
+ "pending " + phase + " reply cannot return actual configure ACK",
134
+ prepareAdmittedConfigure,
135
+ async (h, { token, child }) => {
136
+ armed = true;
137
+ const actor = captureAdmittedConfigureWrite(token),
138
+ pending = actor.invoke();
139
+ try {
140
+ expect(
141
+ await Promise.race([
142
+ entered.promise.then(() => true),
143
+ pending.then(
144
+ () => false,
145
+ () => false,
146
+ ),
147
+ ]),
148
+ ).toBe(true);
149
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
150
+ await expect(actor.invoke()).rejects.toThrow();
151
+ expect(posts(h)).toHaveLength(phase === "dispatch" ? 0 : 1);
152
+ if (phase !== "dispatch")
153
+ expect(
154
+ await child.journal.readProviderAcknowledgement(),
155
+ ).not.toBeNull();
156
+ } finally {
157
+ released.resolve();
158
+ }
159
+ const output = await pending;
160
+ expect(object(output).status).toBe("configure_acknowledged");
161
+ expect(
162
+ readAdmittedConfigureAcknowledgement(token).output === output,
163
+ ).toBe(true);
164
+ },
165
+ {
166
+ afterAppend: async () => {
167
+ if (
168
+ armed &&
169
+ ++appended === (phase === "dispatch" ? 1 : 3) &&
170
+ phase !== "retention"
171
+ ) {
172
+ entered.resolve();
173
+ await released.promise;
174
+ }
175
+ },
176
+ afterRetain: async () => {
177
+ if (armed && phase === "retention") {
178
+ entered.resolve();
179
+ await released.promise;
180
+ }
181
+ },
182
+ },
183
+ );
184
+ }
185
+ it("retention rejection after commit keeps C9 but returns no ACK or second POST", async () => {
186
+ let armed = false;
187
+ await configureAdmissionFixture(
188
+ async (h) => {
189
+ const { token, child } = await prepare(h);
190
+ armed = true;
191
+ const actor = captureAdmittedConfigureWrite(token);
192
+ expect(object(await actor.invoke()).status).toBe("configure_unknown");
193
+ expect(await child.journal.readProviderAcknowledgement()).not.toBeNull();
194
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
195
+ await expect(actor.invoke()).rejects.toThrow();
196
+ expect(posts(h)).toHaveLength(1);
197
+ },
198
+ {
199
+ afterRetain: async () => {
200
+ if (armed) throw Error("CONTROLLED_RETENTION_REPLY_REJECTION");
201
+ },
202
+ },
203
+ );
204
+ });
205
+ it("parent retirement during POST returns unknown and late fulfillment cannot publish ACK", async () => {
206
+ const entered = deferred(),
207
+ released = deferred();
208
+ let armed = false;
209
+ await configureAdmissionFixture(
210
+ async (h) => {
211
+ const { token, child } = await prepare(h);
212
+ armed = true;
213
+ const actor = captureAdmittedConfigureWrite(token),
214
+ pending = actor.invoke();
215
+ try {
216
+ expect(
217
+ await Promise.race([
218
+ entered.promise.then(() => true),
219
+ pending.then(
220
+ () => false,
221
+ () => false,
222
+ ),
223
+ ]),
224
+ ).toBe(true);
225
+ h.core.owner.retire();
226
+ expect(object(await pending).status).toBe("configure_unknown");
227
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
228
+ } finally {
229
+ released.resolve();
230
+ }
231
+ await vi.advanceTimersByTimeAsync(0);
232
+ expect(await child.journal.readProviderAcknowledgement()).toBeNull();
233
+ expect(posts(h)).toHaveLength(1);
234
+ await expect(actor.invoke()).rejects.toThrow();
235
+ },
236
+ {
237
+ beforeFetch: async (_path, method) => {
238
+ if (armed && method === "POST") {
239
+ armed = false;
240
+ entered.resolve();
241
+ await released.promise;
242
+ }
243
+ },
244
+ },
245
+ );
246
+ });
247
+ it("unsafe fulfilled POST stays unknown without response event or returned ACK", async () => {
248
+ await configureAdmissionFixture(async (h) => {
249
+ const { token, child } = await prepare(h);
250
+ Reflect.set(h.baseline.schedule, "metadata", {
251
+ unsafe: "Bearer synthetic-configure-write-control",
252
+ });
253
+ const output = await captureAdmittedConfigureWrite(token).invoke();
254
+ expect(object(output).status).toBe("configure_unknown");
255
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
256
+ expect(await child.journal.readProviderAcknowledgement()).toBeNull();
257
+ expect(posts(h)).toHaveLength(1);
258
+ });
259
+ });
260
+
261
+ it.each(["GET", "POST"])(
262
+ "an actual %s rejection carries bounded failure and never retries",
263
+ async (verb) => {
264
+ let armed = false,
265
+ attempts = 0;
266
+ await configureAdmissionFixture(
267
+ async (h) => {
268
+ const { token, child } = await prepare(h);
269
+ armed = true;
270
+ const actor = captureAdmittedConfigureWrite(token);
271
+ let actual: unknown;
272
+ try {
273
+ await actor.invoke();
274
+ } catch (error) {
275
+ actual = error;
276
+ }
277
+ expect(actual).toBeInstanceOf(Error);
278
+ expect(object(Reflect.get(actual as Error, "failure")).phase).toBe(
279
+ verb === "GET" ? "read" : "post",
280
+ );
281
+ expect(attempts).toBe(1);
282
+ expect(await child.journal.readProviderAcknowledgement()).toBeNull();
283
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
284
+ await expect(actor.invoke()).rejects.toThrow();
285
+ },
286
+ {
287
+ beforeFetch: async (_path, method) => {
288
+ if (armed && method === verb) {
289
+ attempts++;
290
+ throw Error("CONTROLLED_PROVIDER_REJECTION");
291
+ }
292
+ },
293
+ },
294
+ );
295
+ },
296
+ );
297
+ it("a returned ACK reader preserves output identity and closes after observed Source pointer drift", async () => {
298
+ await configureAdmissionFixture(async (h) => {
299
+ const { token } = await prepare(h);
300
+ const output = await captureAdmittedConfigureWrite(token).invoke();
301
+ const view = readAdmittedConfigureAcknowledgement(token);
302
+ expect(view.output === output).toBe(true);
303
+ expect(view.receipt === object(output).receipt).toBe(true);
304
+ expect(() => readAdmittedConfigureAcknowledgement({ ...token })).toThrow();
305
+ expect(() =>
306
+ Reflect.apply(readAdmittedConfigureAcknowledgement, undefined, [
307
+ token,
308
+ structuredClone(output),
309
+ ]),
310
+ ).toThrow();
311
+ const covers = admissionControls.write[0].coversConfigure;
312
+ admissionControls.write[0].coversConfigure = () => true;
313
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
314
+ admissionControls.write[0].coversConfigure = covers;
315
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
316
+ expect(posts(h)).toHaveLength(1);
317
+ });
318
+ });
319
+ it("a genuine second Action context retains the original configure attempt custody", async () => {
320
+ await configureAdmissionFixture(async (h) => {
321
+ const { token } = await prepare(h),
322
+ actor = captureAdmittedConfigureWrite(token);
323
+ await h.inAnotherAction(async () => {
324
+ const output = await actor.invoke();
325
+ expect(object(output).status).toBe("configure_acknowledged");
326
+ expect(
327
+ readAdmittedConfigureAcknowledgement(token).output === output,
328
+ ).toBe(true);
329
+ });
330
+ expect(posts(h)).toHaveLength(1);
331
+ expect(admissionControls.legacy).toBe(0);
332
+ });
333
+ });
334
+
335
+ it.each(["event-payload-alias", "changed-id", "extra-field"])(
336
+ "refuses %s in the final retention readback",
337
+ async (scenario) => {
338
+ let childId = "",
339
+ eventPayload = "",
340
+ forged = false;
341
+ await configureAdmissionFixture(
342
+ async (h) => {
343
+ const { token, child } = await prepare(h);
344
+ childId = child.created.operationId;
345
+ const output = await captureAdmittedConfigureWrite(token).invoke();
346
+ expect(forged).toBe(true);
347
+ expect(object(output).status).toBe("configure_unknown");
348
+ expect(() => readAdmittedConfigureAcknowledgement(token)).toThrow();
349
+ expect(posts(h)).toHaveLength(1);
350
+ },
351
+ {
352
+ afterQuery: async (reply, reference, args) => {
353
+ if (!reply || object(args).operationId !== childId) return;
354
+ const own = object(reply);
355
+ if (
356
+ reference.endsWith("/readAttempt") &&
357
+ Array.isArray(own.events) &&
358
+ own.events.length === 3
359
+ ) {
360
+ const payload = object(own.events[2]).payloadId;
361
+ if (typeof payload === "string") eventPayload = payload;
362
+ }
363
+ if (
364
+ reference.endsWith("/readProviderAcknowledgement") &&
365
+ eventPayload &&
366
+ own.retention &&
367
+ typeof own.retention === "object"
368
+ ) {
369
+ Reflect.set(
370
+ own.retention,
371
+ scenario === "extra-field" ? "extra" : "receiptPayloadId",
372
+ scenario === "event-payload-alias"
373
+ ? eventPayload
374
+ : "controlled-changed-payload",
375
+ );
376
+ forged = true;
377
+ }
378
+ },
379
+ },
380
+ );
381
+ },
382
+ );
383
+
384
+ describe("concurrent admitted children", () => {
385
+ let pending: Promise<void>,
386
+ start: ReturnType<typeof deferred>,
387
+ postsReady: ReturnType<typeof deferred>,
388
+ releasePosts: ReturnType<typeof deferred>,
389
+ retainedReady: ReturnType<typeof deferred>,
390
+ releaseRetained: ReturnType<typeof deferred>,
391
+ run = false,
392
+ armed = false,
393
+ postCount = 0,
394
+ retainedCount = 0;
395
+ const stage = (ready: ReturnType<typeof deferred>) =>
396
+ Promise.race([
397
+ ready.promise,
398
+ pending.then(() => {
399
+ throw Error("FIXTURE_SETTLED_BEFORE_STAGE");
400
+ }),
401
+ ]);
402
+ beforeEach(async () => {
403
+ run = false;
404
+ armed = false;
405
+ postCount = 0;
406
+ retainedCount = 0;
407
+ start = deferred();
408
+ postsReady = deferred();
409
+ releasePosts = deferred();
410
+ retainedReady = deferred();
411
+ releaseRetained = deferred();
412
+ let ready!: () => void, failed!: (error: unknown) => void;
413
+ const prepared = new Promise<void>((resolve, reject) => {
414
+ ready = resolve;
415
+ failed = reject;
416
+ });
417
+ pending = configureAdmissionFixture(
418
+ async (h) => {
419
+ const first = await prepare(h);
420
+ const child = await h.child();
421
+ const token = await h.core.admitConfigure(
422
+ child.fresh.call,
423
+ child.journal,
424
+ );
425
+ h.configureReads(child);
426
+ await captureAdmittedConfigurePreflight(token).invoke();
427
+ ready();
428
+ await start.promise;
429
+ if (!run) return;
430
+ armed = true;
431
+ const actors = [
432
+ captureAdmittedConfigureWrite(first.token),
433
+ captureAdmittedConfigureWrite(token),
434
+ ];
435
+ const outputs = await Promise.all(
436
+ actors.map((actor) => actor.invoke()),
437
+ );
438
+ expect(outputs.map((output) => object(output).status)).toEqual([
439
+ "configure_acknowledged",
440
+ "configure_acknowledged",
441
+ ]);
442
+ expect(outputs[0] === outputs[1]).toBe(false);
443
+ expect(
444
+ readAdmittedConfigureAcknowledgement(first.token).output ===
445
+ outputs[0],
446
+ ).toBe(true);
447
+ expect(
448
+ readAdmittedConfigureAcknowledgement(token).output === outputs[1],
449
+ ).toBe(true);
450
+ expect(
451
+ posts(h)
452
+ .map((call) => call.idempotencyKey)
453
+ .sort(),
454
+ ).toEqual(
455
+ [
456
+ first.child.record.configureRecord.request.key,
457
+ child.record.configureRecord.request.key,
458
+ ].sort(),
459
+ );
460
+ const retained = await Promise.all([
461
+ first.child.journal.readProviderAcknowledgement(),
462
+ child.journal.readProviderAcknowledgement(),
463
+ ]);
464
+ expect(retained.every((value) => value !== null)).toBe(true);
465
+ expect(retained[0]!.receiptJSON === retained[1]!.receiptJSON).toBe(
466
+ false,
467
+ );
468
+ },
469
+ {
470
+ beforeFetch: async (path, method) => {
471
+ if (
472
+ armed &&
473
+ method === "POST" &&
474
+ path.startsWith("/v1/subscription_schedules/")
475
+ ) {
476
+ if (++postCount === 2) postsReady.resolve();
477
+ await releasePosts.promise;
478
+ }
479
+ },
480
+ afterRetain: async () => {
481
+ if (armed) {
482
+ if (++retainedCount === 2) retainedReady.resolve();
483
+ await releaseRetained.promise;
484
+ }
485
+ },
486
+ },
487
+ );
488
+ void pending.catch(failed);
489
+ await prepared;
490
+ });
491
+ beforeEach(async () => {
492
+ run = true;
493
+ start.resolve();
494
+ await stage(postsReady);
495
+ });
496
+ beforeEach(async () => {
497
+ releasePosts.resolve();
498
+ await stage(retainedReady);
499
+ });
500
+ afterEach(async () => {
501
+ start?.resolve();
502
+ releasePosts?.resolve();
503
+ releaseRetained?.resolve();
504
+ await pending?.catch(() => {});
505
+ });
506
+ it("concurrent children retain separate POST keys, journals and returned outputs", async () => {
507
+ expect(postCount).toBe(2);
508
+ expect(retainedCount).toBe(2);
509
+ releaseRetained.resolve();
510
+ await pending;
511
+ });
512
+ });
513
+
514
+ describe("independent SDK root lifetime", () => {
515
+ let prior: object;
516
+ beforeEach(async () => {
517
+ await configureAdmissionFixture(async (h) => {
518
+ const { token } = await prepare(h);
519
+ prior = token;
520
+ expect(
521
+ object(await captureAdmittedConfigureWrite(token).invoke()).status,
522
+ ).toBe("configure_acknowledged");
523
+ });
524
+ });
525
+ it("retiring one SDK root cannot close a later independent root", async () => {
526
+ await configureAdmissionFixture(async (h) => {
527
+ const { token } = await prepare(h);
528
+ expect(() => readAdmittedConfigureAcknowledgement(prior)).toThrow();
529
+ const output = await captureAdmittedConfigureWrite(token).invoke();
530
+ expect(object(output).status).toBe("configure_acknowledged");
531
+ expect(
532
+ readAdmittedConfigureAcknowledgement(token).output === output,
533
+ ).toBe(true);
534
+ expect(() => readAdmittedConfigureAcknowledgement(prior)).toThrow();
535
+ });
536
+ });
537
+ });