@fantastic.dev/better-stripe 0.111.0 → 0.113.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/observation/freshActor.d.ts +564 -0
  4. package/dist/client/billing/renewalAttach/internal/observation/freshActor.d.ts.map +1 -1
  5. package/dist/client/billing/renewalAttach/internal/observation/freshActor.js +103 -21
  6. package/dist/client/billing/renewalAttach/internal/observation/freshActor.js.map +1 -1
  7. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.d.ts +18 -1
  8. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.d.ts.map +1 -1
  9. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.js +54 -1
  10. package/dist/client/billing/renewalAttach/internal/observation/freshTransport.js.map +1 -1
  11. package/dist/client/billing/renewalAttach/internal/observation/invocation.d.ts +4 -0
  12. package/dist/client/billing/renewalAttach/internal/observation/invocation.d.ts.map +1 -1
  13. package/dist/client/billing/renewalAttach/internal/observation/invocation.js +1 -0
  14. package/dist/client/billing/renewalAttach/internal/observation/invocation.js.map +1 -1
  15. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts +4 -0
  16. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts.map +1 -1
  17. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js +6 -1
  18. package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js.map +1 -1
  19. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.d.ts +38 -0
  20. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.d.ts.map +1 -0
  21. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.js +172 -0
  22. package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.js.map +1 -0
  23. package/dist/client/billing/renewalAttach/internal/observation/witnessData.d.ts +135 -0
  24. package/dist/client/billing/renewalAttach/internal/observation/witnessData.d.ts.map +1 -0
  25. package/dist/client/billing/renewalAttach/internal/observation/witnessData.js +143 -0
  26. package/dist/client/billing/renewalAttach/internal/observation/witnessData.js.map +1 -0
  27. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.d.ts +85 -0
  28. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.d.ts.map +1 -0
  29. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.js +121 -0
  30. package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.js.map +1 -0
  31. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.d.ts +44 -0
  32. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.d.ts.map +1 -0
  33. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.js +4 -0
  34. package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.js.map +1 -0
  35. package/dist/client/billing/renewalAttach/internal/producer/configureJournalWork.d.ts +1 -1
  36. package/dist/client/billing/renewalAttach/internal/producer/configureLineage.d.ts +21 -21
  37. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.d.ts +3 -3
  38. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.d.ts +1 -1
  39. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
  40. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  41. package/dist/component/_generated/api.d.ts +24 -0
  42. package/dist/component/_generated/api.d.ts.map +1 -1
  43. package/dist/component/_generated/api.js.map +1 -1
  44. package/dist/component/_generated/component.d.ts +94 -10
  45. package/dist/component/_generated/component.d.ts.map +1 -1
  46. package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
  47. package/dist/component/balanceFunding/queries.d.ts +1 -1
  48. package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
  49. package/dist/component/renewalWitnesses/mutations.d.ts +45 -0
  50. package/dist/component/renewalWitnesses/mutations.d.ts.map +1 -0
  51. package/dist/component/renewalWitnesses/mutations.js +14 -0
  52. package/dist/component/renewalWitnesses/mutations.js.map +1 -0
  53. package/dist/component/renewalWitnesses/queries.d.ts +39 -0
  54. package/dist/component/renewalWitnesses/queries.d.ts.map +1 -0
  55. package/dist/component/renewalWitnesses/queries.js +11 -0
  56. package/dist/component/renewalWitnesses/queries.js.map +1 -0
  57. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  58. package/docs/renewal-example-authorization.md +74 -0
  59. package/docs/renewal-witness-protocol.md +61 -10
  60. package/package.json +1 -1
  61. package/src/client/billing/renewalAttach/internal/observation/freshActor.ts +142 -19
  62. package/src/client/billing/renewalAttach/internal/observation/freshTransport.ts +90 -1
  63. package/src/client/billing/renewalAttach/internal/observation/invocation.ts +2 -0
  64. package/src/client/billing/renewalAttach/internal/observation/readerAuthorization.ts +6 -1
  65. package/src/client/billing/renewalAttach/internal/observation/witnessAcquisition.integration.test.ts +348 -0
  66. package/src/client/billing/renewalAttach/internal/observation/witnessActor.integration.test.ts +204 -0
  67. package/src/client/billing/renewalAttach/internal/observation/witnessAuthorization.ts +239 -0
  68. package/src/client/billing/renewalAttach/internal/observation/witnessCapabilities.integration.test.ts +231 -0
  69. package/src/client/billing/renewalAttach/internal/observation/witnessData.ts +189 -0
  70. package/src/client/billing/renewalAttach/internal/observation/witnessHistorical.integration.test.ts +98 -0
  71. package/src/client/billing/renewalAttach/internal/observation/witnessImmutable.integration.test.ts +78 -0
  72. package/src/client/billing/renewalAttach/internal/observation/witnessInvoice.integration.test.ts +85 -0
  73. package/src/client/billing/renewalAttach/internal/observation/witnessLifecycle.integration.test.ts +264 -0
  74. package/src/client/billing/renewalAttach/internal/observation/witnessOperation.ts +205 -0
  75. package/src/client/billing/renewalAttach/internal/observation/witnessProfile.integration.test.ts +93 -0
  76. package/src/client/billing/renewalAttach/internal/observation/witnessRPCControls.integration.test.ts +256 -0
  77. package/src/client/billing/renewalAttach/internal/operations/witnessProfiles.ts +58 -0
  78. package/src/client/billing/renewalAttach/internal/test-fixtures/invoiceObservation.ts +3 -1
  79. package/src/client/billing/renewalAttach/internal/test-fixtures/producerConfigureAdmission.ts +8 -0
  80. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +28 -11
  81. package/src/client/billing/renewalAttach/internal/test-fixtures/witnessHistory.ts +70 -0
  82. package/src/client/billing/renewalAttach/internal/test-fixtures/witnessObservation.ts +353 -0
  83. package/src/component/_generated/api.ts +24 -0
  84. package/src/component/_generated/component.ts +98 -10
  85. package/src/component/renewalWitnesses/mutations.ts +17 -0
  86. package/src/component/renewalWitnesses/queries.ts +11 -0
  87. package/src/component/renewalWitnesses/routes.test.ts +154 -0
@@ -0,0 +1,98 @@
1
+ import {
2
+ prepareWitnessObservation,
3
+ invokeWitnessCapture,
4
+ invokeWitnessRead,
5
+ readStoredWitness,
6
+ witnessFixtureOptions,
7
+ } from "../test-fixtures/witnessObservation.js";
8
+ import { retainLaterWitnessC9 } from "../test-fixtures/witnessHistory.js";
9
+ import { configureAdmissionCase } from "../test-fixtures/producerConfigureAdmission.js";
10
+ import { inspectHistoricalMaterialEvidenceV1 } from "../../../renewalObservation/evidence-material.js";
11
+ import { expect } from "vitest";
12
+ import assert from "node:assert/strict";
13
+
14
+ configureAdmissionCase(
15
+ "a captured-null witness does not inherit a later real C9 context",
16
+ prepareWitnessObservation,
17
+ async (h, state) => {
18
+ await invokeWitnessCapture(h, state);
19
+ const stored = await readStoredWitness(h, state);
20
+ assert(stored);
21
+ const payload = JSON.parse(stored.payloadJSON);
22
+ expect(payload.source.configure.acknowledgement).toBeNull();
23
+ const later = await retainLaterWitnessC9(h, state, 1);
24
+ const conflated = await inspectHistoricalMaterialEvidenceV1({
25
+ configuration: later.rawReceipt.configuration,
26
+ configureReceipt: later.rawReceipt,
27
+ historicalSnapshot: {
28
+ reads: payload.reads,
29
+ invoiceContext: payload.invoiceContext,
30
+ },
31
+ });
32
+ expect(conflated.predicate).toBe("unknown");
33
+ const output = await invokeWitnessRead(h, state);
34
+ expect(output.history.configureReceipt).not.toBeNull();
35
+ expect(output.evidence?.currentMaterial.predicate).toBe("unknown");
36
+ expect(output.evidence?.historicalMaterial.predicate).toBe(
37
+ "snapshot_target_material_match",
38
+ );
39
+ expect(await readStoredWitness(h, state)).toEqual(stored);
40
+ },
41
+ witnessFixtureOptions,
42
+ );
43
+
44
+ let rejectCurrentRead = false;
45
+ configureAdmissionCase(
46
+ "a first-GET provider rejection preserves only validated historical intent and material",
47
+ async (h) => {
48
+ rejectCurrentRead = false;
49
+ return prepareWitnessObservation(h);
50
+ },
51
+ async (h, state) => {
52
+ const captured = await invokeWitnessCapture(h, state);
53
+ assert(captured.evidence?.identity);
54
+ const stored = await readStoredWitness(h, state);
55
+ assert(stored);
56
+ const before = h.calls.length;
57
+ rejectCurrentRead = true;
58
+ const output = await invokeWitnessRead(h, state);
59
+ expect(output.status).toBe("observation_provider_rejected");
60
+ expect(output.reads).toBeNull();
61
+ expect(output.evidence?.currentMaterial.predicate).toBe("unknown");
62
+ expect(output.evidence?.lifecycle.predicate).toBe("unknown");
63
+ expect(output.evidence?.historicalMaterial.predicate).toBe(
64
+ "snapshot_target_material_match",
65
+ );
66
+ expect(output.evidence?.identity).toEqual(captured.evidence.identity);
67
+ expect(h.calls.slice(before)).toHaveLength(1);
68
+ expect(await readStoredWitness(h, state)).toEqual(stored);
69
+ },
70
+ {
71
+ ...witnessFixtureOptions,
72
+ beforeFetch: async () => {
73
+ if (rejectCurrentRead) throw Error("CONTROLLED_FRESH_PROVIDER_FAILURE");
74
+ },
75
+ },
76
+ );
77
+
78
+ configureAdmissionCase(
79
+ "later terminal and settings drift cannot rewrite the retained active native snapshot",
80
+ prepareWitnessObservation,
81
+ async (h, state) => {
82
+ await invokeWitnessCapture(h, state);
83
+ const stored = await readStoredWitness(h, state);
84
+ assert(stored);
85
+ state.reads.schedule.status = "completed";
86
+ state.reads.schedule.current_phase = null;
87
+ state.reads.schedule.completed_at = state.scope.nativePeriod.end;
88
+ state.reads.subscription.cancel_at_period_end = true;
89
+ const output = await invokeWitnessRead(h, state);
90
+ expect(output.evidence?.lifecycle.providerStatus).toBe("completed");
91
+ expect(output.evidence?.currentMaterial.predicate).toBe("unknown");
92
+ expect(output.evidence?.historicalMaterial.predicate).toBe(
93
+ "snapshot_target_material_match",
94
+ );
95
+ expect(await readStoredWitness(h, state)).toEqual(stored);
96
+ },
97
+ witnessFixtureOptions,
98
+ );
@@ -0,0 +1,78 @@
1
+ import {
2
+ prepareWitnessObservation,
3
+ invokeWitnessCapture,
4
+ readStoredWitness,
5
+ witnessFixtureOptions,
6
+ } from "../test-fixtures/witnessObservation.js";
7
+ import { configureAdmissionCase } from "../test-fixtures/producerConfigureAdmission.js";
8
+ import { getFunctionAddress } from "convex/server";
9
+ import { expect, vi } from "vitest";
10
+ import assert from "node:assert/strict";
11
+
12
+ configureAdmissionCase(
13
+ "concurrent fresh invocations retain one immutable witness through two original RPCs",
14
+ prepareWitnessObservation,
15
+ async (h, state) => {
16
+ let mutations = 0;
17
+ const options: NonNullable<Parameters<typeof invokeWitnessCapture>[2]> = {
18
+ before(ctx) {
19
+ const original = ctx.runMutation;
20
+ Object.defineProperty(ctx, "runMutation", {
21
+ value: (...args: Parameters<typeof original>) => {
22
+ if (
23
+ getFunctionAddress(args[0]).reference?.endsWith(
24
+ "/retainAuthorizedWitness",
25
+ )
26
+ )
27
+ mutations++;
28
+ return original.apply(ctx, args);
29
+ },
30
+ });
31
+ },
32
+ };
33
+ const before = h.calls.length;
34
+ const [first, second] = await Promise.all([
35
+ invokeWitnessCapture(h, state, options),
36
+ invokeWitnessCapture(h, state, options),
37
+ ]);
38
+ assert(
39
+ "witnessRetention" in first &&
40
+ first.witnessRetention?.status === "retained",
41
+ );
42
+ assert(
43
+ "witnessRetention" in second &&
44
+ second.witnessRetention?.status === "retained",
45
+ );
46
+ expect(second.witnessRetention).toEqual(first.witnessRetention);
47
+ expect((await readStoredWitness(h, state))?.descriptor).toEqual(
48
+ first.witnessRetention.descriptor,
49
+ );
50
+ expect(mutations).toBe(2);
51
+ expect(h.calls.slice(before).map((c) => c.method)).toEqual(
52
+ Array(12).fill("GET"),
53
+ );
54
+ },
55
+ witnessFixtureOptions,
56
+ );
57
+
58
+ configureAdmissionCase(
59
+ "a later valid capture conflicts under its original witness identity without overwrite or rotation",
60
+ prepareWitnessObservation,
61
+ async (h, state) => {
62
+ await invokeWitnessCapture(h, state);
63
+ const original = await readStoredWitness(h, state);
64
+ assert(original);
65
+ vi.setSystemTime(Date.now() + 1);
66
+ const output = await invokeWitnessCapture(h, state);
67
+ assert("witnessRetention" in output);
68
+ expect(output.witnessRetention).toEqual({
69
+ status: "conflict",
70
+ reason: "identity_changed",
71
+ });
72
+ expect(output.evidence?.currentMaterial.predicate).toBe(
73
+ "current_target_material_match",
74
+ );
75
+ expect(await readStoredWitness(h, state)).toEqual(original);
76
+ },
77
+ witnessFixtureOptions,
78
+ );
@@ -0,0 +1,85 @@
1
+ import {
2
+ witnessControls,
3
+ prepareWitnessObservation,
4
+ invokeWitnessCapture,
5
+ invokeWitnessRead,
6
+ readStoredWitness,
7
+ witnessFixtureOptions,
8
+ } from "../test-fixtures/witnessObservation.js";
9
+ import { invoiceControls } from "../test-fixtures/invoiceObservation.js";
10
+ import { configureAdmissionCase } from "../test-fixtures/producerConfigureAdmission.js";
11
+ import { expect } from "vitest";
12
+ import assert from "node:assert/strict";
13
+
14
+ for (const retained of [false, true])
15
+ configureAdmissionCase(
16
+ "retains complete acquired invoice context with actual C9 " + retained,
17
+ (h) => prepareWitnessObservation(h, retained, true),
18
+ async (h, state) => {
19
+ assert(
20
+ state.invoiceInventories && Array.isArray(state.invoiceInventories),
21
+ );
22
+ const before = h.calls.length;
23
+ const output = await invokeWitnessCapture(h, state);
24
+ assert(
25
+ "witnessRetention" in output &&
26
+ output.witnessRetention?.status === "retained",
27
+ );
28
+ expect(output.evidence?.invoiceEvidence.predicate).toBe(
29
+ "invoice_lines_correlated",
30
+ );
31
+ expect(h.calls.slice(before).map((c) => c.method)).toEqual(
32
+ Array(11).fill("GET"),
33
+ );
34
+ const stored = await readStoredWitness(h, state);
35
+ assert(stored);
36
+ const payload = JSON.parse(stored.payloadJSON);
37
+ const [original, current] = state.invoiceInventories;
38
+ expect(payload.invoiceContext).toEqual({
39
+ original: { invoice: original.invoice, linePages: [original.page] },
40
+ current: { invoice: current.invoice, linePages: [current.page] },
41
+ });
42
+ expect(payload.acquisition.invoiceReadProfile).toBe(
43
+ "endive-pinned-invoices-fence-7-v1",
44
+ );
45
+ expect(
46
+ payload.invoiceContext.current.linePages[0].data[0].pricing
47
+ .unit_amount_decimal,
48
+ ).toBe(current.page.data[0].pricing.unit_amount_decimal);
49
+ expect(payload.source.configure.acknowledgement === null).toBe(!retained);
50
+ const baseOnlyRead = Object.freeze({
51
+ ...state.readProfile,
52
+ invoiceProfile: null,
53
+ });
54
+ witnessControls.readers.splice(
55
+ 0,
56
+ witnessControls.readers.length,
57
+ baseOnlyRead,
58
+ );
59
+ state.reads.subscription.cancel_at_period_end = true;
60
+ const later = await invokeWitnessRead(h, state);
61
+ expect(later.evidence?.currentMaterial.predicate).toBe("unknown");
62
+ expect(later.evidence?.invoiceEvidence.reason).toBe("not_supplied");
63
+ expect(later.evidence?.historicalMaterial.predicate).toBe(
64
+ "snapshot_target_material_match",
65
+ );
66
+ expect(await readStoredWitness(h, state)).toEqual(stored);
67
+ expect(h.calls.slice(before)).toHaveLength(17);
68
+ },
69
+ witnessFixtureOptions,
70
+ );
71
+
72
+ configureAdmissionCase(
73
+ "invoice evidence permission alone cannot retain a witness",
74
+ (h) => prepareWitnessObservation(h, false, true),
75
+ async (h, state) => {
76
+ witnessControls.denyRetain = true;
77
+ const before = h.calls.length;
78
+ await expect(invokeWitnessCapture(h, state)).rejects.toThrow();
79
+ expect(invoiceControls.count).toBeGreaterThan(0);
80
+ expect(h.calls.slice(before)).toHaveLength(11);
81
+ expect(witnessControls.retainQueries).toBe(1);
82
+ expect(await readStoredWitness(h, state)).toBeNull();
83
+ },
84
+ witnessFixtureOptions,
85
+ );
@@ -0,0 +1,264 @@
1
+ import {
2
+ witnessControls,
3
+ prepareWitnessObservation,
4
+ invokeWitnessCapture,
5
+ invokeWitnessRead,
6
+ readStoredWitness,
7
+ witnessFixtureOptions,
8
+ witnessActorInput,
9
+ } from "../test-fixtures/witnessObservation.js";
10
+ import {
11
+ configureAdmissionCase,
12
+ admissionControls,
13
+ } from "../test-fixtures/producerConfigureAdmission.js";
14
+ import { captureFreshWitnessRenewalObservation } from "./freshActor.js";
15
+ import { getFunctionAddress } from "convex/server";
16
+ import { expect, vi } from "vitest";
17
+ import assert from "node:assert/strict";
18
+
19
+ function latch() {
20
+ let release!: () => void;
21
+ const promise = new Promise<void>((resolve) => {
22
+ release = resolve;
23
+ });
24
+ return { promise, release };
25
+ }
26
+ const isRetain = (ref: Parameters<typeof getFunctionAddress>[0]) =>
27
+ getFunctionAddress(ref).reference?.endsWith("/retainAuthorizedWitness");
28
+ const isRead = (ref: Parameters<typeof getFunctionAddress>[0]) =>
29
+ getFunctionAddress(ref).reference?.endsWith("/readAuthorizedWitness");
30
+
31
+ for (const phase of ["reply", "readback"] as const)
32
+ configureAdmissionCase(
33
+ "an unresolved original " + phase + " cannot release retention DATA",
34
+ prepareWitnessObservation,
35
+ async (h, state) => {
36
+ const entered = latch(),
37
+ resume = latch();
38
+ let settled = false,
39
+ readbacks = 0;
40
+ const pending = invokeWitnessCapture(h, state, {
41
+ before(ctx) {
42
+ const mutate = ctx.runMutation,
43
+ query = ctx.runQuery;
44
+ Object.defineProperty(ctx, "runMutation", {
45
+ value: async (...args: Parameters<typeof mutate>) => {
46
+ const result = await mutate.apply(ctx, args);
47
+ if (phase === "reply" && isRetain(args[0])) {
48
+ entered.release();
49
+ await resume.promise;
50
+ }
51
+ return result;
52
+ },
53
+ });
54
+ Object.defineProperty(ctx, "runQuery", {
55
+ value: async (...args: Parameters<typeof query>) => {
56
+ const result = await query.apply(ctx, args);
57
+ if (isRead(args[0])) {
58
+ readbacks++;
59
+ if (phase === "readback") {
60
+ entered.release();
61
+ await resume.promise;
62
+ }
63
+ }
64
+ return result;
65
+ },
66
+ });
67
+ },
68
+ });
69
+ const observed = pending.then((value) => {
70
+ settled = true;
71
+ return value;
72
+ });
73
+ try {
74
+ await entered.promise;
75
+ expect(await readStoredWitness(h, state)).not.toBeNull();
76
+ await vi.advanceTimersByTimeAsync(0);
77
+ expect(settled).toBe(false);
78
+ expect(readbacks).toBe(phase === "reply" ? 0 : 1);
79
+ expect(witnessControls.retainQueries).toBe(1);
80
+ } finally {
81
+ resume.release();
82
+ }
83
+ const output = await observed;
84
+ assert("witnessRetention" in output);
85
+ expect(output.witnessRetention?.status).toBe("retained");
86
+ expect(witnessControls.retainQueries).toBe(2);
87
+ },
88
+ witnessFixtureOptions,
89
+ );
90
+
91
+ for (const phase of ["reply", "readback"] as const)
92
+ configureAdmissionCase(
93
+ "expiry during original " +
94
+ phase +
95
+ " leaves durable storage without returned ACK",
96
+ prepareWitnessObservation,
97
+ async (h, state) => {
98
+ const entered = latch(),
99
+ resume = latch();
100
+ let returned = false;
101
+ const pending = invokeWitnessCapture(h, state, {
102
+ before(ctx) {
103
+ if (phase === "reply") {
104
+ const original = ctx.runMutation;
105
+ Object.defineProperty(ctx, "runMutation", {
106
+ value: async (...args: Parameters<typeof original>) => {
107
+ const result = await original.apply(ctx, args);
108
+ if (isRetain(args[0])) {
109
+ entered.release();
110
+ await resume.promise;
111
+ }
112
+ return result;
113
+ },
114
+ });
115
+ } else {
116
+ const original = ctx.runQuery;
117
+ Object.defineProperty(ctx, "runQuery", {
118
+ value: async (...args: Parameters<typeof original>) => {
119
+ const result = await original.apply(ctx, args);
120
+ if (isRead(args[0])) {
121
+ entered.release();
122
+ await resume.promise;
123
+ }
124
+ return result;
125
+ },
126
+ });
127
+ }
128
+ },
129
+ });
130
+ const outcome = pending.then(
131
+ () => {
132
+ returned = true;
133
+ return null;
134
+ },
135
+ (error: unknown) => error,
136
+ );
137
+ try {
138
+ await entered.promise;
139
+ expect(await readStoredWitness(h, state)).not.toBeNull();
140
+ await vi.advanceTimersByTimeAsync(30000);
141
+ expect(await outcome).toEqual(
142
+ Error("OBSERVATION_HISTORY_READER_EXPIRED"),
143
+ );
144
+ } finally {
145
+ resume.release();
146
+ }
147
+ await vi.advanceTimersByTimeAsync(0);
148
+ expect(returned).toBe(false);
149
+ expect(witnessControls.retainQueries).toBe(1);
150
+ expect(await readStoredWitness(h, state)).not.toBeNull();
151
+ },
152
+ witnessFixtureOptions,
153
+ );
154
+
155
+ for (const change of [
156
+ "lost-reply",
157
+ "retention-revoked",
158
+ "profile-removed",
159
+ "query-address",
160
+ "selected-sdk-drift",
161
+ ] as const)
162
+ configureAdmissionCase(
163
+ "committed retention produces no ACK after " + change,
164
+ prepareWitnessObservation,
165
+ async (h, state) => {
166
+ let writes = 0;
167
+ const selected: { client: object | null } = { client: null };
168
+ const oldAddress = Reflect.get(
169
+ state.captureProfile.authorizationQuery,
170
+ Symbol.for("functionName"),
171
+ );
172
+ admissionControls.onSelect = (client) => {
173
+ selected.client = client;
174
+ };
175
+ try {
176
+ await expect(
177
+ invokeWitnessCapture(h, state, {
178
+ before(ctx) {
179
+ const original = ctx.runMutation;
180
+ Object.defineProperty(ctx, "runMutation", {
181
+ value: async (...args: Parameters<typeof original>) => {
182
+ const result = await original.apply(ctx, args);
183
+ if (isRetain(args[0])) {
184
+ writes++;
185
+ if (change === "lost-reply")
186
+ throw Error("CONTROLLED_LOST_RETENTION_REPLY");
187
+ if (change === "retention-revoked")
188
+ witnessControls.denyRetain = true;
189
+ if (change === "profile-removed")
190
+ witnessControls.captures.splice(0);
191
+ if (change === "query-address")
192
+ Reflect.set(
193
+ state.captureProfile.authorizationQuery,
194
+ Symbol.for("functionName"),
195
+ "initialOwner:authorizeWitnessRead",
196
+ );
197
+ if (change === "selected-sdk-drift") {
198
+ assert(selected.client);
199
+ Object.defineProperty(selected.client, "rawRequest", {
200
+ configurable: true,
201
+ value() {
202
+ throw Error("UNEXPECTED_DRIFTED_SDK_CALL");
203
+ },
204
+ });
205
+ }
206
+ }
207
+ return result;
208
+ },
209
+ });
210
+ },
211
+ }),
212
+ ).rejects.toThrow();
213
+ } finally {
214
+ if (selected.client && change === "selected-sdk-drift")
215
+ Reflect.deleteProperty(selected.client, "rawRequest");
216
+ Reflect.set(
217
+ state.captureProfile.authorizationQuery,
218
+ Symbol.for("functionName"),
219
+ oldAddress,
220
+ );
221
+ }
222
+ expect(writes).toBe(1);
223
+ expect(await readStoredWitness(h, state)).not.toBeNull();
224
+ },
225
+ witnessFixtureOptions,
226
+ );
227
+
228
+ configureAdmissionCase(
229
+ "an idle captured factory expires within its original 30 seconds",
230
+ prepareWitnessObservation,
231
+ async (h, state) => {
232
+ const before = h.calls.length;
233
+ await h.inAnotherAction(async (ctx) => {
234
+ const actor = await captureFreshWitnessRenewalObservation(
235
+ h.sdk,
236
+ witnessActorInput(state, ctx),
237
+ );
238
+ await vi.advanceTimersByTimeAsync(30000);
239
+ await expect(actor.invoke()).rejects.toThrow(
240
+ "OBSERVATION_HISTORY_READER_EXPIRED",
241
+ );
242
+ await expect(actor.invoke()).rejects.toThrow();
243
+ });
244
+ expect(h.calls).toHaveLength(before);
245
+ expect(await readStoredWitness(h, state)).toBeNull();
246
+ },
247
+ witnessFixtureOptions,
248
+ );
249
+
250
+ configureAdmissionCase(
251
+ "revocation of the distinct read capability before release cannot return historical DATA",
252
+ prepareWitnessObservation,
253
+ async (h, state) => {
254
+ await invokeWitnessCapture(h, state);
255
+ const stored = await readStoredWitness(h, state);
256
+ witnessControls.onReadGrant = (_grant, count) => {
257
+ if (count === 1) witnessControls.denyRead = true;
258
+ };
259
+ await expect(invokeWitnessRead(h, state)).rejects.toThrow();
260
+ expect(witnessControls.readQueries).toBe(2);
261
+ expect(await readStoredWitness(h, state)).toEqual(stored);
262
+ },
263
+ witnessFixtureOptions,
264
+ );