@fantastic.dev/better-stripe 0.94.0 → 0.95.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 (89) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/operationJournal.d.ts +6 -0
  4. package/dist/client/billing/renewalAttach/internal/operationJournal.d.ts.map +1 -1
  5. package/dist/client/billing/renewalAttach/internal/operationJournal.js +10 -0
  6. package/dist/client/billing/renewalAttach/internal/operationJournal.js.map +1 -1
  7. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +5 -0
  8. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -1
  9. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +40 -0
  10. package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -1
  11. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts +13 -0
  12. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts.map +1 -0
  13. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js +43 -0
  14. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js.map +1 -0
  15. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts +32 -0
  16. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts.map +1 -0
  17. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js +102 -0
  18. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js.map +1 -0
  19. package/dist/client/billing/renewalAttach/internal/producer/ownedAwaitFactory.d.ts +3 -0
  20. package/dist/client/billing/renewalAttach/internal/producer/ownedAwaitFactory.d.ts.map +1 -0
  21. package/dist/client/billing/renewalAttach/internal/producer/ownedAwaitFactory.js +59 -0
  22. package/dist/client/billing/renewalAttach/internal/producer/ownedAwaitFactory.js.map +1 -0
  23. package/dist/client/billing/renewalAttach/internal/producer/ownerFactory.d.ts +11 -0
  24. package/dist/client/billing/renewalAttach/internal/producer/ownerFactory.d.ts.map +1 -0
  25. package/dist/client/billing/renewalAttach/internal/producer/ownerFactory.js +132 -0
  26. package/dist/client/billing/renewalAttach/internal/producer/ownerFactory.js.map +1 -0
  27. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.d.ts +6 -0
  28. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.d.ts.map +1 -0
  29. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.js +16 -0
  30. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.js.map +1 -0
  31. package/dist/client/billing/renewalObservation/evidence-invoice-lines.d.ts +27 -0
  32. package/dist/client/billing/renewalObservation/evidence-invoice-lines.d.ts.map +1 -0
  33. package/dist/client/billing/renewalObservation/evidence-invoice-lines.js +164 -0
  34. package/dist/client/billing/renewalObservation/evidence-invoice-lines.js.map +1 -0
  35. package/dist/client/billing/renewalObservation/evidence-invoice.d.ts +4 -0
  36. package/dist/client/billing/renewalObservation/evidence-invoice.d.ts.map +1 -0
  37. package/dist/client/billing/renewalObservation/evidence-invoice.js +190 -0
  38. package/dist/client/billing/renewalObservation/evidence-invoice.js.map +1 -0
  39. package/dist/client/billing/renewalObservation/evidence-material.d.ts +26 -0
  40. package/dist/client/billing/renewalObservation/evidence-material.d.ts.map +1 -0
  41. package/dist/client/billing/renewalObservation/evidence-material.js +253 -0
  42. package/dist/client/billing/renewalObservation/evidence-material.js.map +1 -0
  43. package/dist/client/billing/renewalObservation/evidence-v1-types.d.ts +156 -0
  44. package/dist/client/billing/renewalObservation/evidence-v1-types.d.ts.map +1 -0
  45. package/dist/client/billing/renewalObservation/evidence-v1-types.js +2 -0
  46. package/dist/client/billing/renewalObservation/evidence-v1-types.js.map +1 -0
  47. package/dist/client/billing/renewalObservation/evidence-v1.d.ts +4 -0
  48. package/dist/client/billing/renewalObservation/evidence-v1.d.ts.map +1 -0
  49. package/dist/client/billing/renewalObservation/evidence-v1.js +263 -0
  50. package/dist/client/billing/renewalObservation/evidence-v1.js.map +1 -0
  51. package/dist/client/billing/renewalObservation/terminal-evidence.d.ts +17 -0
  52. package/dist/client/billing/renewalObservation/terminal-evidence.d.ts.map +1 -0
  53. package/dist/client/billing/renewalObservation/terminal-evidence.js +289 -0
  54. package/dist/client/billing/renewalObservation/terminal-evidence.js.map +1 -0
  55. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
  56. package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
  57. package/dist/component/balanceFunding/mutations.d.ts +43 -43
  58. package/dist/component/balanceFunding/queries.d.ts +1 -1
  59. package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
  60. package/dist/component/earnings/triggers.d.ts +16 -16
  61. package/dist/component/payoutRelease/queries.d.ts +2 -2
  62. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  63. package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
  64. package/docs/renewal-context-reads.md +28 -0
  65. package/docs/renewal-material-observation.md +50 -0
  66. package/package.json +1 -1
  67. package/src/client/billing/renewalAttach/internal/operationJournal.ts +12 -0
  68. package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +59 -0
  69. package/src/client/billing/renewalAttach/internal/producer/invocationProducer.ts +58 -0
  70. package/src/client/billing/renewalAttach/internal/producer/lineageAuthority.ts +158 -0
  71. package/src/client/billing/renewalAttach/internal/producer/ownedAwaitFactory.ts +54 -0
  72. package/src/client/billing/renewalAttach/internal/producer/ownerFactory.ts +136 -0
  73. package/src/client/billing/renewalAttach/internal/producer/producerClock.integration.test.ts +175 -0
  74. package/src/client/billing/renewalAttach/internal/producer/producerOwner.integration.test.ts +449 -0
  75. package/src/client/billing/renewalAttach/internal/producer/stockDelegateFactory.ts +23 -0
  76. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialDeclarations.ts +5 -0
  77. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +142 -0
  78. package/src/client/billing/renewalObservation/evidence-admission.test.ts +219 -0
  79. package/src/client/billing/renewalObservation/evidence-history.test.ts +187 -0
  80. package/src/client/billing/renewalObservation/evidence-invoice-controls.test.ts +293 -0
  81. package/src/client/billing/renewalObservation/evidence-invoice-lines.ts +238 -0
  82. package/src/client/billing/renewalObservation/evidence-invoice.test.ts +177 -0
  83. package/src/client/billing/renewalObservation/evidence-invoice.ts +271 -0
  84. package/src/client/billing/renewalObservation/evidence-material.test.ts +289 -0
  85. package/src/client/billing/renewalObservation/evidence-material.ts +313 -0
  86. package/src/client/billing/renewalObservation/evidence-v1-types.ts +186 -0
  87. package/src/client/billing/renewalObservation/evidence-v1.test.ts +371 -0
  88. package/src/client/billing/renewalObservation/evidence-v1.ts +286 -0
  89. package/src/client/billing/renewalObservation/terminal-evidence.ts +379 -0
@@ -0,0 +1,219 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import { inspectRenewalEvidenceV1 } from "./evidence-v1.js";
3
+ import { object } from "./common.js";
4
+ import {
5
+ recipe,
6
+ set,
7
+ model,
8
+ unknownResult,
9
+ } from "../../../../tests/renewalObservation/evidenceFixtures.js";
10
+
11
+ afterEach(() => vi.unstubAllGlobals());
12
+ /** Reflection only: do not invoke getters or serialize malformed/cyclic DATA. */
13
+ function descriptors(input: unknown) {
14
+ const seen = new Set<object>();
15
+ const rows: {
16
+ object: object;
17
+ key: PropertyKey;
18
+ descriptor: PropertyDescriptor;
19
+ }[] = [];
20
+ function walk(value: unknown) {
21
+ if (!value || typeof value !== "object" || seen.has(value)) return;
22
+ seen.add(value);
23
+ for (const key of Reflect.ownKeys(value)) {
24
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
25
+ if (!descriptor) throw Error("MISSING_TEST_DESCRIPTOR");
26
+ rows.push({ object: value, key, descriptor });
27
+ if ("value" in descriptor) walk(descriptor.value);
28
+ }
29
+ }
30
+ walk(input);
31
+ return rows;
32
+ }
33
+ const malformed = [
34
+ "missing-root",
35
+ "extra-root",
36
+ "undefined",
37
+ "symbol",
38
+ "hidden",
39
+ "getter",
40
+ "cycle",
41
+ "nonfinite",
42
+ "sparse",
43
+ "prototype",
44
+ ] as const;
45
+ it.each(malformed)(
46
+ "evidence admission rejects %s without reading getters",
47
+ async (control) => {
48
+ const input = recipe().input;
49
+ const schedule = object(input.reads.schedule);
50
+ let getterCalls = 0,
51
+ fetchCalls = 0;
52
+ if (control === "missing-root") Reflect.deleteProperty(input, "reads");
53
+ if (control === "extra-root") set(input, "extra", null);
54
+ if (control === "undefined") set(schedule, "unknown", undefined);
55
+ if (control === "symbol")
56
+ set(schedule, "unknown", { [Symbol("TEST")]: true });
57
+ if (control === "hidden")
58
+ Object.defineProperty(schedule, "unknown", { value: null });
59
+ if (control === "getter")
60
+ Object.defineProperty(schedule, "unknown", {
61
+ enumerable: true,
62
+ get() {
63
+ getterCalls++;
64
+ return null;
65
+ },
66
+ });
67
+ if (control === "cycle") set(schedule, "unknown", schedule);
68
+ if (control === "nonfinite") set(schedule, "unknown", Number.NaN);
69
+ if (control === "sparse") set(schedule, "unknown", new Array(1));
70
+ if (control === "prototype") set(schedule, "unknown", new Date(0));
71
+ const before = descriptors(input);
72
+ vi.stubGlobal("fetch", () => {
73
+ fetchCalls++;
74
+ throw Error("UNEXPECTED_FETCH");
75
+ });
76
+ let targetFailed = false;
77
+ try {
78
+ await expect(inspectRenewalEvidenceV1(input)).rejects.toThrow(
79
+ /^Invalid raw renewal evidence\.$/,
80
+ );
81
+ } catch (error) {
82
+ targetFailed = true;
83
+ throw error;
84
+ } finally {
85
+ try {
86
+ expect(descriptors(input)).toEqual(before);
87
+ expect(getterCalls).toBe(0);
88
+ expect(fetchCalls).toBe(0);
89
+ } catch (error) {
90
+ if (!targetFailed) throw error;
91
+ console.info("EVIDENCE_ADMISSION_INSPECTION_FAILED");
92
+ }
93
+ }
94
+ },
95
+ );
96
+ it.each(["version", "api"] as const)(
97
+ "evidence unsupported %s returns the entire closed DTO",
98
+ async (control) => {
99
+ const input = recipe().input;
100
+ set(
101
+ input,
102
+ control === "version" ? "version" : "apiVersion",
103
+ control === "version" ? 2 : "2026-01-28.clover",
104
+ );
105
+ const before = structuredClone(input);
106
+ const expected = unknownResult(
107
+ control === "version" ? "unsupported_profile" : "unsupported_api_version",
108
+ );
109
+ let fetchCalls = 0;
110
+ vi.stubGlobal("fetch", () => {
111
+ fetchCalls++;
112
+ throw Error("UNEXPECTED_FETCH");
113
+ });
114
+ let targetFailed = false;
115
+ try {
116
+ const actual = await inspectRenewalEvidenceV1(input);
117
+ expect(actual).toEqual(expected);
118
+ expect(Reflect.ownKeys(actual)).toHaveLength(12);
119
+ } catch (error) {
120
+ targetFailed = true;
121
+ throw error;
122
+ } finally {
123
+ try {
124
+ expect(input).toEqual(before);
125
+ expect(fetchCalls).toBe(0);
126
+ } catch (error) {
127
+ if (!targetFailed) throw error;
128
+ console.info("EVIDENCE_ADMISSION_INSPECTION_FAILED");
129
+ }
130
+ }
131
+ },
132
+ );
133
+ const boundaries = [
134
+ "characters-under",
135
+ "characters-over",
136
+ "utf8-over",
137
+ "nodes-under",
138
+ "nodes-over",
139
+ "depth-under",
140
+ "depth-over",
141
+ ] as const;
142
+ it.each(boundaries)("evidence whole-model boundary %s", async (control) => {
143
+ const input = recipe().input;
144
+ // Unsupported version isolates whole-input admission from material predicates.
145
+ set(input, "version", 2);
146
+ const schedule = object(input.reads.schedule);
147
+ set(schedule, "unknown", "");
148
+ if (control.startsWith("characters")) {
149
+ const remaining = 500000 - model(input).characters;
150
+ expect(remaining).toBeGreaterThan(0);
151
+ set(
152
+ schedule,
153
+ "unknown",
154
+ "x".repeat(remaining + (control.endsWith("over") ? 1 : 0)),
155
+ );
156
+ } else if (control === "utf8-over") {
157
+ set(schedule, "unknown", "é".repeat(260000));
158
+ } else if (control.startsWith("nodes")) {
159
+ set(schedule, "unknown", []);
160
+ const remaining = 10000 - model(input).nodes;
161
+ expect(remaining).toBeGreaterThan(0);
162
+ set(
163
+ schedule,
164
+ "unknown",
165
+ Array.from(
166
+ { length: remaining + (control.endsWith("over") ? 1 : 0) },
167
+ () => null,
168
+ ),
169
+ );
170
+ } else {
171
+ let nested: unknown = null;
172
+ for (let i = 0; i < (control.endsWith("over") ? 38 : 37); i++)
173
+ nested = { next: nested };
174
+ set(schedule, "unknown", nested);
175
+ }
176
+ const metrics = model(input);
177
+ const over = control.endsWith("over");
178
+ if (control.startsWith("characters"))
179
+ expect(metrics.characters).toBe(over ? 500001 : 500000);
180
+ if (control === "utf8-over") {
181
+ expect(metrics.characters).toBeLessThan(500000);
182
+ expect(metrics.bytes).toBeGreaterThan(500000);
183
+ }
184
+ if (control.startsWith("nodes"))
185
+ expect(metrics.nodes).toBe(over ? 10001 : 10000);
186
+ if (control.startsWith("depth")) expect(metrics.depth).toBe(over ? 41 : 40);
187
+ const before = descriptors(input);
188
+ let fetchCalls = 0;
189
+ vi.stubGlobal("fetch", () => {
190
+ fetchCalls++;
191
+ throw Error("UNEXPECTED_FETCH");
192
+ });
193
+ let targetFailed = false;
194
+ try {
195
+ if (over)
196
+ await expect(inspectRenewalEvidenceV1(input)).rejects.toThrow(
197
+ /^Invalid raw renewal evidence\.$/,
198
+ );
199
+ else
200
+ expect(await inspectRenewalEvidenceV1(input)).toEqual(
201
+ unknownResult("unsupported_profile"),
202
+ );
203
+ } catch (error) {
204
+ targetFailed = true;
205
+ throw error;
206
+ } finally {
207
+ try {
208
+ expect(descriptors(input)).toEqual(before);
209
+ expect(fetchCalls).toBe(0);
210
+ console.info(
211
+ "EVIDENCE_ADMISSION_BOUNDARY",
212
+ JSON.stringify({ control, ...metrics, fetchCalls }),
213
+ );
214
+ } catch (error) {
215
+ if (!targetFailed) throw error;
216
+ console.info("EVIDENCE_ADMISSION_INSPECTION_FAILED");
217
+ }
218
+ }
219
+ });
@@ -0,0 +1,187 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import { inspectRenewalEvidenceV1 } from "./evidence-v1.js";
3
+ import { inspectRenewalScheduleMaterialV2 } from "../renewalV2/observation.js";
4
+ import { object, array, get } from "./common.js";
5
+ import type { HistoricalMaterialV1, LifecycleV1 } from "./evidence-v1-types.js";
6
+ import {
7
+ recipe,
8
+ evidence,
9
+ facts,
10
+ complete,
11
+ completionLifecycle,
12
+ currentTarget,
13
+ prerequisites,
14
+ bounded,
15
+ set,
16
+ } from "../../../../tests/renewalObservation/evidenceFixtures.js";
17
+
18
+ afterEach(() => vi.unstubAllGlobals());
19
+ const controls = [
20
+ "full-snapshot",
21
+ "live-default-drift",
22
+ "foreign-snapshot",
23
+ "snapshot-settings",
24
+ "snapshot-native",
25
+ "duplicate-items",
26
+ "terminal-snapshot",
27
+ "absent",
28
+ ] as const;
29
+ it.each(controls)("unsigned historical full snapshot %s", async (control) => {
30
+ const h = recipe("active");
31
+ const snapshot = structuredClone(h.baseline.reads);
32
+ const item = object(
33
+ array(get(object(snapshot.subscription), "items").data)[0],
34
+ );
35
+ complete(h.input);
36
+ if (control === "live-default-drift")
37
+ set(
38
+ get(object(h.input.reads.schedule), "default_settings"),
39
+ "description",
40
+ "TEST_LIVE_DRIFT_ONLY",
41
+ );
42
+ if (control === "foreign-snapshot")
43
+ set(snapshot.subscription, "id", "sub_foreign");
44
+ if (control === "snapshot-settings")
45
+ set(snapshot.subscription, "description", "TEST_SNAPSHOT_DRIFT");
46
+ if (control === "snapshot-native")
47
+ set(item, "current_period_end", Number(item.current_period_end) + 1);
48
+ if (control === "duplicate-items") {
49
+ const duplicate = structuredClone(item);
50
+ set(duplicate, "id", "si_second");
51
+ set(get(object(snapshot.subscription), "items"), "data", [item, duplicate]);
52
+ }
53
+ if (control === "terminal-snapshot")
54
+ complete({ ...h.input, reads: snapshot });
55
+ if (control !== "absent")
56
+ set(h.input, "historicalSnapshot", {
57
+ reads: snapshot,
58
+ invoiceContext: null,
59
+ });
60
+ const lifecycle: LifecycleV1 =
61
+ control === "live-default-drift"
62
+ ? {
63
+ predicate: "conflict",
64
+ providerStatus: "completed",
65
+ facts: facts(h.input),
66
+ completion: null,
67
+ reason: "material_mismatch",
68
+ }
69
+ : completionLifecycle(h.input);
70
+ let historical: HistoricalMaterialV1;
71
+ if (control === "full-snapshot" || control === "live-default-drift") {
72
+ const match = currentTarget({ ...h.input, reads: snapshot });
73
+ historical = {
74
+ predicate: "snapshot_target_material_match",
75
+ observedItemId: match.observedItemId,
76
+ nativePeriod: match.nativePeriod,
77
+ reason: null,
78
+ };
79
+ } else if (control === "foreign-snapshot")
80
+ historical = {
81
+ predicate: "conflict",
82
+ observedItemId: null,
83
+ nativePeriod: null,
84
+ reason: "snapshot_relation_mismatch",
85
+ };
86
+ else if (control === "snapshot-settings")
87
+ historical = {
88
+ predicate: "conflict",
89
+ observedItemId: null,
90
+ nativePeriod: null,
91
+ reason: "material_mismatch",
92
+ };
93
+ else
94
+ historical = {
95
+ predicate: "unknown",
96
+ observedItemId: null,
97
+ nativePeriod: null,
98
+ reason:
99
+ control === "absent"
100
+ ? "not_supplied"
101
+ : control === "duplicate-items"
102
+ ? "ambiguous_items"
103
+ : "incomplete_material",
104
+ };
105
+ const expected = {
106
+ ...evidence(h.input, lifecycle, {
107
+ predicate: "unknown",
108
+ observedItemId: null,
109
+ nativePeriod: null,
110
+ reason: "terminal_completion_unproven",
111
+ }),
112
+ historicalMaterial: historical,
113
+ };
114
+ let fetchCalls = 0;
115
+ vi.stubGlobal("fetch", () => {
116
+ fetchCalls++;
117
+ throw Error("UNEXPECTED_FETCH");
118
+ });
119
+ await prerequisites(h);
120
+ const old = await inspectRenewalScheduleMaterialV2({
121
+ configuration: h.input.configuration,
122
+ reads: snapshot,
123
+ });
124
+ const base = {
125
+ acknowledgementEstablished: false,
126
+ authenticationEstablished: false,
127
+ providerQualificationRequired: true,
128
+ } as const;
129
+ if (["full-snapshot", "live-default-drift", "absent"].includes(control)) {
130
+ expect(old).toEqual({
131
+ ...base,
132
+ predicate: "current_target_material_match",
133
+ observedItemId: "si_successor",
134
+ nativePeriod: currentTarget({ ...h.input, reads: snapshot }).nativePeriod,
135
+ });
136
+ } else if (control === "terminal-snapshot") {
137
+ expect(old).toEqual({
138
+ ...base,
139
+ predicate: "terminal_facts_only",
140
+ providerStatus: "completed",
141
+ effectiveness: "unknown",
142
+ });
143
+ } else {
144
+ expect(old).toEqual({
145
+ ...base,
146
+ predicate:
147
+ control === "foreign-snapshot" || control === "snapshot-settings"
148
+ ? "conflict"
149
+ : "unknown",
150
+ reason:
151
+ control === "foreign-snapshot"
152
+ ? "original_subscription_identity_mismatch"
153
+ : control === "snapshot-settings"
154
+ ? "effective_full_material_drift"
155
+ : control === "snapshot-native"
156
+ ? "native_target_period_not_exact"
157
+ : "incomplete_or_ambiguous_current_items",
158
+ });
159
+ }
160
+ const before = bounded(h.input);
161
+ let targetFailed = false;
162
+ try {
163
+ const actual = await inspectRenewalEvidenceV1(h.input);
164
+ expect(actual).toEqual(expected);
165
+ expect(Reflect.ownKeys(actual)).toHaveLength(12);
166
+ } catch (error) {
167
+ targetFailed = true;
168
+ throw error;
169
+ } finally {
170
+ try {
171
+ expect(h.input).toEqual(before);
172
+ expect(fetchCalls).toBe(0);
173
+ console.info(
174
+ "EVIDENCE_HISTORY_SETTLEMENT",
175
+ JSON.stringify({
176
+ control,
177
+ targetReached: true,
178
+ inputUnchanged: true,
179
+ fetchCalls,
180
+ }),
181
+ );
182
+ } catch (error) {
183
+ if (!targetFailed) throw error;
184
+ console.info("EVIDENCE_HISTORY_INSPECTION_FAILED");
185
+ }
186
+ }
187
+ });
@@ -0,0 +1,293 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import { inspectRenewalEvidenceV1 } from "./evidence-v1.js";
3
+ import { object } from "./common.js";
4
+ import type { InvoiceEvidenceV1, Reason } from "./evidence-v1-types.js";
5
+ import {
6
+ invoiceRecipe,
7
+ invoiceMatch,
8
+ currentTarget,
9
+ evidence,
10
+ activeLifecycle,
11
+ prerequisites,
12
+ bounded,
13
+ set,
14
+ } from "../../../../tests/renewalObservation/evidenceFixtures.js";
15
+
16
+ afterEach(() => vi.unstubAllGlobals());
17
+ const controls = [
18
+ "I0-manual",
19
+ "I1-create",
20
+ "invoice-parent",
21
+ "line-sub",
22
+ "line-item",
23
+ "invoice-id",
24
+ "customer",
25
+ "account",
26
+ "mode",
27
+ "price",
28
+ "product",
29
+ "currency",
30
+ "native-end",
31
+ "fractional-quantity",
32
+ "decimal-price-drift",
33
+ "large-decimal-one-unit",
34
+ "quantity-only",
35
+ "legacy-line-only",
36
+ "null-parent",
37
+ "numeric-decimal",
38
+ "too-precise-decimal",
39
+ "duplicate-eligible",
40
+ "duplicate-page",
41
+ "nonprogress",
42
+ "incomplete-page-two",
43
+ "wrong-page-url",
44
+ "too-many-pages",
45
+ "too-many-lines",
46
+ "full-settings-drift",
47
+ "unrelated-supplied-invoice",
48
+ ] as const;
49
+ it.each(controls)("unsigned invoice refusal %s", async (control) => {
50
+ const f = invoiceRecipe(
51
+ "subscription_create",
52
+ "open",
53
+ control === "large-decimal-one-unit" ? "large-decimal" : "standard",
54
+ );
55
+ const addon = (id: string) => {
56
+ const row = structuredClone(f.currentLine);
57
+ set(row, "id", id);
58
+ set(row, "subscription", null);
59
+ set(row, "parent", {
60
+ type: "invoice_item_details",
61
+ subscription_item_details: null,
62
+ invoice_item_details: {
63
+ invoice_item: "ii_declared_extra",
64
+ proration: false,
65
+ },
66
+ });
67
+ return row;
68
+ };
69
+ let reason: Reason = "invoice_relation_mismatch";
70
+ let predicate: "unknown" | "conflict" = "conflict";
71
+ if (control === "I0-manual" || control === "I1-create") {
72
+ set(
73
+ control === "I0-manual" ? f.originalInvoice : f.currentInvoice,
74
+ "billing_reason",
75
+ control === "I0-manual" ? "manual" : "subscription_create",
76
+ );
77
+ reason = "unsupported_invoice_reason";
78
+ predicate = "unknown";
79
+ }
80
+ if (control === "invoice-parent")
81
+ set(
82
+ f.currentInvoice.parent.subscription_details,
83
+ "subscription",
84
+ "sub_foreign",
85
+ );
86
+ if (control === "line-sub")
87
+ set(
88
+ f.currentLine.parent.subscription_item_details,
89
+ "subscription",
90
+ "sub_foreign",
91
+ );
92
+ if (control === "line-item")
93
+ set(
94
+ f.currentLine.parent.subscription_item_details,
95
+ "subscription_item",
96
+ "si_foreign",
97
+ );
98
+ if (control === "invoice-id") set(f.currentInvoice, "id", "in_foreign");
99
+ if (control === "customer") set(f.currentInvoice, "customer", "cus_foreign");
100
+ if (control === "account")
101
+ set(f.currentInvoice, "customer_account", "acct_foreign");
102
+ if (control === "mode") set(f.currentInvoice, "livemode", true);
103
+ if (control === "price" || control === "product") {
104
+ set(
105
+ f.currentLine.pricing.price_details,
106
+ control,
107
+ control === "price" ? "price_foreign" : "prod_foreign",
108
+ );
109
+ reason = "invoice_material_mismatch";
110
+ }
111
+ if (control === "currency") {
112
+ set(f.currentLine, "currency", "eur");
113
+ reason = "invoice_material_mismatch";
114
+ }
115
+ if (control === "native-end") {
116
+ set(f.currentLine.period, "end", f.currentLine.period.end - 1);
117
+ reason = "invoice_material_mismatch";
118
+ }
119
+ if (control === "fractional-quantity") {
120
+ set(f.currentLine, "quantity_decimal", "3.5");
121
+ reason = "invoice_material_mismatch";
122
+ }
123
+ if (control === "decimal-price-drift") {
124
+ set(f.currentLine.pricing, "unit_amount_decimal", "2000.000000000001");
125
+ reason = "invoice_material_mismatch";
126
+ }
127
+ if (control === "large-decimal-one-unit") {
128
+ const original = f.currentLine.pricing.unit_amount_decimal;
129
+ const changed = "9007199254740991.000000000002";
130
+ expect(
131
+ BigInt(changed.replace(".", "")) - BigInt(original.replace(".", "")),
132
+ ).toBe(1n);
133
+ // This offline illusion is why the production comparison may not use Number.
134
+ expect(Number(changed)).toBe(Number(original));
135
+ set(f.currentLine.pricing, "unit_amount_decimal", changed);
136
+ reason = "invoice_material_mismatch";
137
+ }
138
+ if (control === "quantity-only") {
139
+ set(f.currentLine, "quantity_decimal", null);
140
+ reason = "incomplete_invoice_lines";
141
+ predicate = "unknown";
142
+ }
143
+ if (control === "legacy-line-only") {
144
+ set(f.currentLine, "parent", null);
145
+ set(f.currentLine, "pricing", null);
146
+ set(f.currentLine, "price", {
147
+ id: "price_target",
148
+ product: "custom-target",
149
+ });
150
+ set(f.currentLine, "subscription_item", "si_successor");
151
+ reason = "incomplete_invoice_lines";
152
+ predicate = "unknown";
153
+ }
154
+ if (control === "null-parent") {
155
+ set(f.currentInvoice, "parent", null);
156
+ reason = "incomplete_invoice_lines";
157
+ predicate = "unknown";
158
+ }
159
+ if (control === "numeric-decimal" || control === "too-precise-decimal") {
160
+ set(
161
+ f.currentLine.pricing,
162
+ "unit_amount_decimal",
163
+ control === "numeric-decimal" ? 2000 : "2000.0000000000001",
164
+ );
165
+ reason = "unsupported_profile";
166
+ predicate = "unknown";
167
+ }
168
+ if (control === "duplicate-eligible") {
169
+ const duplicate = structuredClone(f.currentLine);
170
+ set(duplicate, "id", "il_duplicate_eligible");
171
+ set(f.currentPages[0], "data", [f.currentLine, duplicate]);
172
+ reason = "incomplete_invoice_lines";
173
+ predicate = "unknown";
174
+ }
175
+ if (control === "duplicate-page") {
176
+ set(f.currentPages[0], "has_more", true);
177
+ f.currentPages.push({
178
+ ...structuredClone(f.currentPages[0]),
179
+ has_more: false,
180
+ });
181
+ reason = "incomplete_invoice_lines";
182
+ predicate = "unknown";
183
+ }
184
+ if (control === "incomplete-page-two" || control === "too-many-pages") {
185
+ set(f.currentPages[0], "data", [addon("il_page_one_extra")]);
186
+ set(f.currentPages[0], "has_more", true);
187
+ f.currentPages.push({
188
+ object: "list",
189
+ url: f.currentPages[0].url,
190
+ has_more: true,
191
+ data:
192
+ control === "too-many-pages"
193
+ ? [addon("il_page_two_extra")]
194
+ : [f.currentLine],
195
+ });
196
+ if (control === "too-many-pages")
197
+ f.currentPages.push({
198
+ object: "list",
199
+ url: f.currentPages[0].url,
200
+ has_more: false,
201
+ data: [f.currentLine],
202
+ });
203
+ reason = "incomplete_invoice_lines";
204
+ predicate = "unknown";
205
+ }
206
+ if (control === "nonprogress") {
207
+ set(f.currentPages[0], "data", []);
208
+ set(f.currentPages[0], "has_more", true);
209
+ f.currentPages.push({
210
+ object: "list",
211
+ url: f.currentPages[0].url,
212
+ has_more: false,
213
+ data: [f.currentLine],
214
+ });
215
+ reason = "incomplete_invoice_lines";
216
+ predicate = "unknown";
217
+ }
218
+ if (control === "wrong-page-url")
219
+ set(f.currentPages[0], "url", "/v1/invoices/in_foreign/lines");
220
+ if (control === "too-many-lines") {
221
+ set(f.currentPages[0], "data", [
222
+ f.currentLine,
223
+ ...Array.from({ length: 100 }, (_, index) => addon(`il_bound_${index}`)),
224
+ ]);
225
+ reason = "incomplete_invoice_lines";
226
+ predicate = "unknown";
227
+ }
228
+ if (control === "full-settings-drift")
229
+ set(
230
+ object(f.h.input.reads.subscription),
231
+ "description",
232
+ "TEST_FULL_SUBSCRIPTION_DRIFT",
233
+ );
234
+ if (control === "unrelated-supplied-invoice")
235
+ set(object(f.h.input.reads.subscription), "latest_invoice", "in_original");
236
+ const invoiceEvidence: InvoiceEvidenceV1 =
237
+ control === "full-settings-drift"
238
+ ? invoiceMatch(f)
239
+ : { predicate, original: null, current: null, reason };
240
+ const currentMaterial =
241
+ control === "unrelated-supplied-invoice"
242
+ ? currentTarget(f.h.input)
243
+ : control === "full-settings-drift"
244
+ ? {
245
+ predicate: "conflict" as const,
246
+ observedItemId: null,
247
+ nativePeriod: null,
248
+ reason: "material_mismatch" as const,
249
+ }
250
+ : {
251
+ predicate: "unknown" as const,
252
+ observedItemId: null,
253
+ nativePeriod: null,
254
+ reason: "incomplete_material" as const,
255
+ };
256
+ const expected = {
257
+ ...evidence(f.h.input, activeLifecycle, currentMaterial),
258
+ invoiceEvidence,
259
+ };
260
+ let fetchCalls = 0;
261
+ vi.stubGlobal("fetch", () => {
262
+ fetchCalls++;
263
+ throw Error("UNEXPECTED_FETCH");
264
+ });
265
+ await prerequisites(f.h);
266
+ const before = bounded(f.h.input);
267
+ let targetFailed = false;
268
+ try {
269
+ const actual = await inspectRenewalEvidenceV1(f.h.input);
270
+ expect(actual).toEqual(expected);
271
+ expect(Reflect.ownKeys(actual)).toHaveLength(12);
272
+ } catch (error) {
273
+ targetFailed = true;
274
+ throw error;
275
+ } finally {
276
+ try {
277
+ expect(f.h.input).toEqual(before);
278
+ expect(fetchCalls).toBe(0);
279
+ console.info(
280
+ "EVIDENCE_INVOICE_CONTROL_SETTLEMENT",
281
+ JSON.stringify({
282
+ control,
283
+ targetReached: true,
284
+ inputUnchanged: true,
285
+ fetchCalls,
286
+ }),
287
+ );
288
+ } catch (error) {
289
+ if (!targetFailed) throw error;
290
+ console.info("EVIDENCE_INVOICE_CONTROL_INSPECTION_FAILED");
291
+ }
292
+ }
293
+ });