@fantastic.dev/better-stripe 0.60.1 → 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +5 -1
- package/dist/client/usage/index.d.ts +2 -0
- package/dist/client/usage/index.d.ts.map +1 -1
- package/dist/react/index.d.ts +17 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +10 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/workflows/Pagination.d.ts +1 -0
- package/dist/react/workflows/Pagination.d.ts.map +1 -1
- package/dist/react/workflows/Pagination.js +1 -0
- package/dist/react/workflows/Pagination.js.map +1 -1
- package/dist/react/workflows/Usage.fixtures.d.ts +17 -0
- package/dist/react/workflows/Usage.fixtures.d.ts.map +1 -0
- package/dist/react/workflows/Usage.fixtures.js +79 -0
- package/dist/react/workflows/Usage.fixtures.js.map +1 -0
- package/dist/react/workflows/Usage.types.d.ts +25 -0
- package/dist/react/workflows/Usage.types.d.ts.map +1 -0
- package/dist/react/workflows/Usage.types.js +11 -0
- package/dist/react/workflows/Usage.types.js.map +1 -0
- package/dist/react/workflows/UsageAllowance.d.ts +14 -0
- package/dist/react/workflows/UsageAllowance.d.ts.map +1 -0
- package/dist/react/workflows/UsageAllowance.js +21 -0
- package/dist/react/workflows/UsageAllowance.js.map +1 -0
- package/dist/react/workflows/UsageBilling.d.ts +20 -0
- package/dist/react/workflows/UsageBilling.d.ts.map +1 -0
- package/dist/react/workflows/UsageBilling.js +40 -0
- package/dist/react/workflows/UsageBilling.js.map +1 -0
- package/dist/react/workflows/UsageConfiguration.d.ts +4 -0
- package/dist/react/workflows/UsageConfiguration.d.ts.map +1 -0
- package/dist/react/workflows/UsageConfiguration.js +19 -0
- package/dist/react/workflows/UsageConfiguration.js.map +1 -0
- package/dist/react/workflows/UsageConfiguration.types.d.ts +65 -0
- package/dist/react/workflows/UsageConfiguration.types.d.ts.map +1 -0
- package/dist/react/workflows/UsageConfiguration.types.js +2 -0
- package/dist/react/workflows/UsageConfiguration.types.js.map +1 -0
- package/dist/react/workflows/UsageCredits.d.ts +14 -0
- package/dist/react/workflows/UsageCredits.d.ts.map +1 -0
- package/dist/react/workflows/UsageCredits.js +25 -0
- package/dist/react/workflows/UsageCredits.js.map +1 -0
- package/dist/react/workflows/UsageHistory.d.ts +40 -0
- package/dist/react/workflows/UsageHistory.d.ts.map +1 -0
- package/dist/react/workflows/UsageHistory.js +74 -0
- package/dist/react/workflows/UsageHistory.js.map +1 -0
- package/dist/react/workflows/UsagePriceRow.d.ts +26 -0
- package/dist/react/workflows/UsagePriceRow.d.ts.map +1 -0
- package/dist/react/workflows/UsagePriceRow.js +40 -0
- package/dist/react/workflows/UsagePriceRow.js.map +1 -0
- package/dist/react/workflows/UsageSummary.d.ts +19 -0
- package/dist/react/workflows/UsageSummary.d.ts.map +1 -0
- package/dist/react/workflows/UsageSummary.js +54 -0
- package/dist/react/workflows/UsageSummary.js.map +1 -0
- package/dist/react/workflows/usage-configuration-validation.d.ts +4 -0
- package/dist/react/workflows/usage-configuration-validation.d.ts.map +1 -0
- package/dist/react/workflows/usage-configuration-validation.js +116 -0
- package/dist/react/workflows/usage-configuration-validation.js.map +1 -0
- package/dist/react/workflows/usage-format.d.ts +11 -0
- package/dist/react/workflows/usage-format.d.ts.map +1 -0
- package/dist/react/workflows/usage-format.js +43 -0
- package/dist/react/workflows/usage-format.js.map +1 -0
- package/dist/react/workflows/usage-scope.d.ts +5 -0
- package/dist/react/workflows/usage-scope.d.ts.map +1 -0
- package/dist/react/workflows/usage-scope.js +26 -0
- package/dist/react/workflows/usage-scope.js.map +1 -0
- package/dist/react/workflows/useUsageConfiguration.d.ts +4 -0
- package/dist/react/workflows/useUsageConfiguration.d.ts.map +1 -0
- package/dist/react/workflows/useUsageConfiguration.js +84 -0
- package/dist/react/workflows/useUsageConfiguration.js.map +1 -0
- package/docs/README.md +4 -0
- package/docs/react-api.md +6 -0
- package/docs/usage-configuration.md +150 -0
- package/docs/usage-react.md +182 -0
- package/package.json +4 -2
- package/src/client/usage/index.ts +3 -0
- package/src/react/index.ts +36 -0
- package/src/react/workflows/Pagination.tsx +2 -0
- package/src/react/workflows/Usage.fixtures.ts +88 -0
- package/src/react/workflows/Usage.test.tsx +373 -0
- package/src/react/workflows/Usage.types.ts +39 -0
- package/src/react/workflows/UsageAllowance.tsx +114 -0
- package/src/react/workflows/UsageBilling.tsx +90 -0
- package/src/react/workflows/UsageConfiguration.test.tsx +393 -0
- package/src/react/workflows/UsageConfiguration.tsx +135 -0
- package/src/react/workflows/UsageConfiguration.types.ts +67 -0
- package/src/react/workflows/UsageCredits.tsx +112 -0
- package/src/react/workflows/UsageHistory.test.tsx +206 -0
- package/src/react/workflows/UsageHistory.tsx +154 -0
- package/src/react/workflows/UsagePriceRow.test.tsx +140 -0
- package/src/react/workflows/UsagePriceRow.tsx +109 -0
- package/src/react/workflows/UsageSummary.tsx +204 -0
- package/src/react/workflows/usage-configuration-validation.test.ts +107 -0
- package/src/react/workflows/usage-configuration-validation.ts +152 -0
- package/src/react/workflows/usage-format.ts +48 -0
- package/src/react/workflows/usage-scope.ts +42 -0
- package/src/react/workflows/useUsageConfiguration.ts +99 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import {
|
|
3
|
+
act,
|
|
4
|
+
cleanup,
|
|
5
|
+
fireEvent,
|
|
6
|
+
render,
|
|
7
|
+
screen,
|
|
8
|
+
waitFor,
|
|
9
|
+
} from "@testing-library/react";
|
|
10
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
11
|
+
import * as ui from "../index.js";
|
|
12
|
+
import { creditBalance, usageScope, usageSnapshot } from "./Usage.fixtures.js";
|
|
13
|
+
import type { UsageBillingSnapshot, UsageViewScope } from "./Usage.types.js";
|
|
14
|
+
afterEach(cleanup);
|
|
15
|
+
it("renders exact local quantities and keeps unknown delivery separate from native aggregation and final billing", () => {
|
|
16
|
+
expect(ui.UsageSummary).toBeTypeOf("function");
|
|
17
|
+
const snapshot = usageSnapshot("900719925474.099301");
|
|
18
|
+
render(<ui.UsageSummary scope={usageScope} {...snapshot} />);
|
|
19
|
+
expect(
|
|
20
|
+
screen.getAllByText("900719925474.099301 call").length,
|
|
21
|
+
).toBeGreaterThan(0);
|
|
22
|
+
expect(screen.getByText(/Native aggregation unavailable/)).toBeTruthy();
|
|
23
|
+
expect(screen.getByText(/Unknown delivery/)).toBeTruthy();
|
|
24
|
+
expect(screen.getByText(/Estimate only/)).toBeTruthy();
|
|
25
|
+
expect(screen.queryByText(/Paid invoice/)).toBeNull();
|
|
26
|
+
});
|
|
27
|
+
it("does not turn loading or unavailable usage into an empty or zero bill", () => {
|
|
28
|
+
expect(ui.UsageSummary).toBeTypeOf("function");
|
|
29
|
+
const { rerender } = render(
|
|
30
|
+
<ui.UsageSummary scope={usageScope} summary={undefined} />,
|
|
31
|
+
);
|
|
32
|
+
expect(screen.getByText("Loading usage…")).toBeTruthy();
|
|
33
|
+
rerender(
|
|
34
|
+
<ui.UsageSummary
|
|
35
|
+
scope={usageScope}
|
|
36
|
+
summary={{
|
|
37
|
+
status: "unsupported",
|
|
38
|
+
reason: "unsupported_scope",
|
|
39
|
+
retryable: false,
|
|
40
|
+
}}
|
|
41
|
+
/>,
|
|
42
|
+
);
|
|
43
|
+
expect(screen.getByText(/Usage unsupported/)).toBeTruthy();
|
|
44
|
+
expect(screen.queryByText(/No usage/)).toBeNull();
|
|
45
|
+
expect(screen.queryByText(/0\.00/)).toBeNull();
|
|
46
|
+
});
|
|
47
|
+
it("labels incomplete local and stale partial native data without summing or finalizing it", () => {
|
|
48
|
+
expect(ui.UsageSummary).toBeTypeOf("function");
|
|
49
|
+
const snapshot = usageSnapshot();
|
|
50
|
+
if (snapshot.summary.status === "available")
|
|
51
|
+
snapshot.summary.value.complete = false;
|
|
52
|
+
render(
|
|
53
|
+
<ui.UsageSummary
|
|
54
|
+
scope={usageScope}
|
|
55
|
+
{...snapshot}
|
|
56
|
+
provider={{
|
|
57
|
+
status: "available",
|
|
58
|
+
observedAt: 650,
|
|
59
|
+
value: {
|
|
60
|
+
period: usageScope.period,
|
|
61
|
+
quantity: "3",
|
|
62
|
+
scaledQuantity: "3000000",
|
|
63
|
+
complete: false,
|
|
64
|
+
nextCursor: "next",
|
|
65
|
+
aggregationLagPossible: true,
|
|
66
|
+
stale: true,
|
|
67
|
+
entries: [],
|
|
68
|
+
},
|
|
69
|
+
}}
|
|
70
|
+
/>,
|
|
71
|
+
);
|
|
72
|
+
expect(screen.getByText(/Local history incomplete/)).toBeTruthy();
|
|
73
|
+
expect(screen.getByText(/Native aggregation stale/)).toBeTruthy();
|
|
74
|
+
expect(screen.getByText(/Native aggregation partial/)).toBeTruthy();
|
|
75
|
+
expect(screen.getByText(/Aggregation may be delayed/)).toBeTruthy();
|
|
76
|
+
});
|
|
77
|
+
it("shows pending purchases, exhausted credit and held invoice applications as separate observations", () => {
|
|
78
|
+
expect(ui.UsageCredits).toBeTypeOf("function");
|
|
79
|
+
render(
|
|
80
|
+
<ui.UsageCredits
|
|
81
|
+
balance={{ status: "available", observedAt: 700, value: creditBalance }}
|
|
82
|
+
purchase={{
|
|
83
|
+
purchaseId: "topup1",
|
|
84
|
+
state: "pending",
|
|
85
|
+
paymentIntentId: "pi_one",
|
|
86
|
+
creditGrantId: null,
|
|
87
|
+
quantity: "10",
|
|
88
|
+
amount: 20,
|
|
89
|
+
currency: "usd",
|
|
90
|
+
}}
|
|
91
|
+
application={{
|
|
92
|
+
status: "unavailable",
|
|
93
|
+
reason: "invoice_provenance_unavailable",
|
|
94
|
+
retryable: true,
|
|
95
|
+
}}
|
|
96
|
+
/>,
|
|
97
|
+
);
|
|
98
|
+
expect(screen.getByText(/Payment pending/)).toBeTruthy();
|
|
99
|
+
expect(screen.getByText(/Insufficient credits/)).toBeTruthy();
|
|
100
|
+
expect(screen.getByText(/Credit application unavailable/)).toBeTruthy();
|
|
101
|
+
expect(
|
|
102
|
+
screen.getByText(/No cash is collected by consuming credits/),
|
|
103
|
+
).toBeTruthy();
|
|
104
|
+
});
|
|
105
|
+
it("provides exact decimal currency formatting without floating-point rounding", () => {
|
|
106
|
+
expect(ui.formatUsageEstimate).toBeTypeOf("function");
|
|
107
|
+
expect(ui.formatUsageEstimate("9007199254740993.123", "usd")).toBe(
|
|
108
|
+
"90071992547409.93123 USD",
|
|
109
|
+
);
|
|
110
|
+
expect(ui.formatUsageEstimate("25", "jpy")).toBe("25 JPY");
|
|
111
|
+
expect(ui.formatUsageEstimate("1.5", "isk")).toBeNull();
|
|
112
|
+
expect(ui.formatUsageEstimate("NaN", "usd")).toBeNull();
|
|
113
|
+
});
|
|
114
|
+
for (const change of [
|
|
115
|
+
"identityKey",
|
|
116
|
+
"accountKey",
|
|
117
|
+
"meterKey",
|
|
118
|
+
"period",
|
|
119
|
+
"currency",
|
|
120
|
+
] as const)
|
|
121
|
+
it(`ignores old asynchronous usage after ${change} changes and shares its state with render props`, async () => {
|
|
122
|
+
expect(ui.UsageBilling).toBeTypeOf("function");
|
|
123
|
+
let finish!: (snapshot: UsageBillingSnapshot) => void;
|
|
124
|
+
const load = vi
|
|
125
|
+
.fn()
|
|
126
|
+
.mockImplementationOnce(
|
|
127
|
+
() =>
|
|
128
|
+
new Promise<UsageBillingSnapshot>((resolve) => {
|
|
129
|
+
finish = resolve;
|
|
130
|
+
}),
|
|
131
|
+
)
|
|
132
|
+
.mockImplementationOnce(async (scope: UsageViewScope) => {
|
|
133
|
+
const next = usageSnapshot("7");
|
|
134
|
+
if (next.summary.status === "available") {
|
|
135
|
+
next.summary.value.period = scope.period;
|
|
136
|
+
next.summary.value.groups[0].binding.plan.rate.currency =
|
|
137
|
+
scope.currency;
|
|
138
|
+
const estimate = next.summary.value.groups[0].estimate;
|
|
139
|
+
if (estimate.status === "available")
|
|
140
|
+
estimate.value.currency = scope.currency;
|
|
141
|
+
}
|
|
142
|
+
return next;
|
|
143
|
+
});
|
|
144
|
+
const { rerender } = render(
|
|
145
|
+
<ui.UsageBilling scope={usageScope} load={load} />,
|
|
146
|
+
);
|
|
147
|
+
await waitFor(() => expect(load).toHaveBeenCalledTimes(1));
|
|
148
|
+
const scope: UsageViewScope = {
|
|
149
|
+
...usageScope,
|
|
150
|
+
[change]:
|
|
151
|
+
change === "period"
|
|
152
|
+
? { start: 1200, end: 1800 }
|
|
153
|
+
: change === "currency"
|
|
154
|
+
? "jpy"
|
|
155
|
+
: "other",
|
|
156
|
+
};
|
|
157
|
+
rerender(
|
|
158
|
+
<ui.UsageBilling scope={scope} load={load}>
|
|
159
|
+
{(state) => (
|
|
160
|
+
<p>
|
|
161
|
+
{state.data?.summary.status === "available"
|
|
162
|
+
? state.data.summary.value.groups[0].acceptedQuantity
|
|
163
|
+
: "waiting"}
|
|
164
|
+
</p>
|
|
165
|
+
)}
|
|
166
|
+
</ui.UsageBilling>,
|
|
167
|
+
);
|
|
168
|
+
await screen.findByText("7");
|
|
169
|
+
await act(async () => finish(usageSnapshot("99")));
|
|
170
|
+
expect(screen.queryByText("99")).toBeNull();
|
|
171
|
+
expect(load).toHaveBeenLastCalledWith(scope);
|
|
172
|
+
});
|
|
173
|
+
it("sanitizes loader failures and provides an explicit keyboard-accessible retry", async () => {
|
|
174
|
+
expect(ui.UsageBilling).toBeTypeOf("function");
|
|
175
|
+
const load = vi
|
|
176
|
+
.fn()
|
|
177
|
+
.mockRejectedValueOnce(new Error("sk_test_private failure"))
|
|
178
|
+
.mockResolvedValueOnce(usageSnapshot());
|
|
179
|
+
render(<ui.UsageBilling scope={usageScope} load={load} />);
|
|
180
|
+
expect((await screen.findByRole("alert")).textContent).toBe(
|
|
181
|
+
"Usage unavailable. Try again.",
|
|
182
|
+
);
|
|
183
|
+
expect(screen.queryByText(/sk_test_private/)).toBeNull();
|
|
184
|
+
expect(screen.queryByText("Loading usage…")).toBeNull();
|
|
185
|
+
fireEvent.click(screen.getByRole("button", { name: "Refresh usage" }));
|
|
186
|
+
await screen.findByText("Usage by meter");
|
|
187
|
+
expect(load).toHaveBeenCalledTimes(2);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("exposes exact allowance progress, soft warnings and hard enforcement as server observations", () => {
|
|
191
|
+
expect(ui.UsageAllowance).toBeTypeOf("function");
|
|
192
|
+
render(
|
|
193
|
+
<ui.UsageAllowance
|
|
194
|
+
unit="call"
|
|
195
|
+
allowance={{
|
|
196
|
+
status: "available",
|
|
197
|
+
observedAt: 700,
|
|
198
|
+
value: {
|
|
199
|
+
revision: 2,
|
|
200
|
+
observedAt: 700,
|
|
201
|
+
period: usageScope.period,
|
|
202
|
+
accepting: true,
|
|
203
|
+
policy: {
|
|
204
|
+
reset: "billing_period",
|
|
205
|
+
rollover: "none",
|
|
206
|
+
entitled: true,
|
|
207
|
+
overage: "allow",
|
|
208
|
+
limit: { mode: "soft", quantity: "10" },
|
|
209
|
+
},
|
|
210
|
+
consumedQuantity: "12",
|
|
211
|
+
reservedQuantity: "2",
|
|
212
|
+
includedQuantity: "10",
|
|
213
|
+
remainingIncludedQuantity: "0",
|
|
214
|
+
billableOverageQuantity: "2",
|
|
215
|
+
softLimitReached: true,
|
|
216
|
+
hardLimitRemainingQuantity: null,
|
|
217
|
+
committedEstimate: { amountDecimal: "4", currency: "usd" },
|
|
218
|
+
projectedEstimate: { amountDecimal: "8", currency: "usd" },
|
|
219
|
+
cashCollection: "none",
|
|
220
|
+
},
|
|
221
|
+
}}
|
|
222
|
+
/>,
|
|
223
|
+
);
|
|
224
|
+
expect(screen.getByText(/Soft threshold reached/)).toBeTruthy();
|
|
225
|
+
expect(screen.getByText(/Display only/)).toBeTruthy();
|
|
226
|
+
expect(
|
|
227
|
+
screen.getByRole("progressbar").getAttribute("aria-valuetext"),
|
|
228
|
+
).toMatch(/12 consumed and 2 reserved of 10 call/);
|
|
229
|
+
expect(screen.getByText("0.04 USD", { exact: false })).toBeTruthy();
|
|
230
|
+
expect(
|
|
231
|
+
screen.getByText(/Resets at the verified billing-period boundary/),
|
|
232
|
+
).toBeTruthy();
|
|
233
|
+
});
|
|
234
|
+
it("holds a response with another period or currency instead of rendering it under the selected scope", async () => {
|
|
235
|
+
expect(ui.UsageBilling).toBeTypeOf("function");
|
|
236
|
+
render(
|
|
237
|
+
<ui.UsageBilling
|
|
238
|
+
scope={{ ...usageScope, currency: "jpy" }}
|
|
239
|
+
load={async () => usageSnapshot("99")}
|
|
240
|
+
/>,
|
|
241
|
+
);
|
|
242
|
+
expect((await screen.findByRole("alert")).textContent).toBe(
|
|
243
|
+
"Usage unavailable. Try again.",
|
|
244
|
+
);
|
|
245
|
+
expect(screen.queryByText("99 call")).toBeNull();
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
for (const model of ["pay_as_you_go", "included_overage", "prepaid"] as const)
|
|
249
|
+
it(`renders the package's original ${model} summary without converting estimates into paid cash`, () => {
|
|
250
|
+
const data = usageSnapshot("14", model);
|
|
251
|
+
render(<ui.UsageSummary scope={usageScope} {...data} />);
|
|
252
|
+
expect(screen.getByText(/Estimate only/)).toBeTruthy();
|
|
253
|
+
if (model === "included_overage") {
|
|
254
|
+
expect(screen.getByText("Overage units")).toBeTruthy();
|
|
255
|
+
expect(screen.getByText(/Estimated usage charge: 0.08 USD/)).toBeTruthy();
|
|
256
|
+
} else if (model === "pay_as_you_go")
|
|
257
|
+
expect(screen.getByText(/Estimated usage charge: 0.28 USD/)).toBeTruthy();
|
|
258
|
+
else expect(screen.getByText(/Prepaid credits/)).toBeTruthy();
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("keeps raw host failure reasons out of the default billing displays", () => {
|
|
262
|
+
const unavailable = {
|
|
263
|
+
status: "unavailable" as const,
|
|
264
|
+
reason: "sk_test_private_raw_failure",
|
|
265
|
+
retryable: true,
|
|
266
|
+
};
|
|
267
|
+
render(
|
|
268
|
+
<>
|
|
269
|
+
<ui.UsageSummary
|
|
270
|
+
scope={usageScope}
|
|
271
|
+
summary={unavailable}
|
|
272
|
+
provider={unavailable}
|
|
273
|
+
/>
|
|
274
|
+
<ui.UsageAllowance unit="call" allowance={unavailable} />
|
|
275
|
+
<ui.UsageCredits balance={unavailable} application={unavailable} />
|
|
276
|
+
</>,
|
|
277
|
+
);
|
|
278
|
+
expect(screen.queryByText(/sk_test_private_raw_failure/)).toBeNull();
|
|
279
|
+
expect(screen.getByText(/Usage unavailable/)).toBeTruthy();
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("does not display a previous period's native aggregate in a directly supplied summary view", () => {
|
|
283
|
+
const snapshot = usageSnapshot();
|
|
284
|
+
render(
|
|
285
|
+
<ui.UsageSummary
|
|
286
|
+
scope={{ ...usageScope, period: { start: 1200, end: 1800 } }}
|
|
287
|
+
summary={snapshot.summary}
|
|
288
|
+
provider={{
|
|
289
|
+
status: "available",
|
|
290
|
+
observedAt: 700,
|
|
291
|
+
value: {
|
|
292
|
+
period: usageScope.period,
|
|
293
|
+
quantity: "99",
|
|
294
|
+
scaledQuantity: "99000000",
|
|
295
|
+
complete: true,
|
|
296
|
+
nextCursor: null,
|
|
297
|
+
aggregationLagPossible: true,
|
|
298
|
+
stale: false,
|
|
299
|
+
entries: [],
|
|
300
|
+
},
|
|
301
|
+
}}
|
|
302
|
+
/>,
|
|
303
|
+
);
|
|
304
|
+
expect(screen.queryByText("Native aggregate: 99")).toBeNull();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("keeps a valid numeric period outside JavaScript's Date range from crashing the usage view", () => {
|
|
308
|
+
const period = {
|
|
309
|
+
start: Number.MAX_SAFE_INTEGER - 1,
|
|
310
|
+
end: Number.MAX_SAFE_INTEGER,
|
|
311
|
+
};
|
|
312
|
+
const data = usageSnapshot();
|
|
313
|
+
if (data.summary.status === "available") data.summary.value.period = period;
|
|
314
|
+
expect(() =>
|
|
315
|
+
render(<ui.UsageSummary scope={{ ...usageScope, period }} {...data} />),
|
|
316
|
+
).not.toThrow();
|
|
317
|
+
expect(screen.getByText(/Time unavailable/)).toBeTruthy();
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
for (const mode of ["direct", "async"])
|
|
321
|
+
it(`rejects an estimate in another currency through the ${mode} view`, async () => {
|
|
322
|
+
const data = usageSnapshot();
|
|
323
|
+
if (data.summary.status === "available")
|
|
324
|
+
data.summary.value.groups[0].estimate = {
|
|
325
|
+
status: "available",
|
|
326
|
+
observedAt: 700,
|
|
327
|
+
value: { amountDecimal: "28", currency: "eur" },
|
|
328
|
+
};
|
|
329
|
+
if (mode === "direct") {
|
|
330
|
+
render(<ui.UsageSummary scope={usageScope} {...data} />);
|
|
331
|
+
expect(screen.getByText(/Usage unavailable/)).toBeTruthy();
|
|
332
|
+
} else {
|
|
333
|
+
render(<ui.UsageBilling scope={usageScope} load={async () => data} />);
|
|
334
|
+
await screen.findByRole("alert");
|
|
335
|
+
}
|
|
336
|
+
expect(screen.queryByText(/0.28 EUR/)).toBeNull();
|
|
337
|
+
});
|
|
338
|
+
for (const model of ["pay_as_you_go", "included_overage", "prepaid"] as const)
|
|
339
|
+
it(`shares the ${model} summary layout between component observations and serialized API adapters`, async () => {
|
|
340
|
+
const data = usageSnapshot("14", model);
|
|
341
|
+
const direct = render(<ui.UsageSummary scope={usageScope} {...data} />);
|
|
342
|
+
const expected = direct.container.querySelector(
|
|
343
|
+
'[data-better-stripe="usage-summary"]',
|
|
344
|
+
)!.innerHTML;
|
|
345
|
+
cleanup();
|
|
346
|
+
for (const load of [
|
|
347
|
+
async () => data,
|
|
348
|
+
async () => JSON.parse(JSON.stringify(data)),
|
|
349
|
+
]) {
|
|
350
|
+
const view = render(<ui.UsageBilling scope={usageScope} load={load} />);
|
|
351
|
+
await waitFor(() =>
|
|
352
|
+
expect(
|
|
353
|
+
view.container.querySelector('[data-better-stripe="usage-summary"]')!
|
|
354
|
+
.innerHTML,
|
|
355
|
+
).toBe(expected),
|
|
356
|
+
);
|
|
357
|
+
cleanup();
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("labels a complete empty local period without inferring zero from partial history", () => {
|
|
362
|
+
const data = usageSnapshot();
|
|
363
|
+
if (data.summary.status === "available") {
|
|
364
|
+
data.summary.value.groups = [];
|
|
365
|
+
data.summary.value.eventCount = 0;
|
|
366
|
+
data.summary.value.complete = false;
|
|
367
|
+
}
|
|
368
|
+
const { rerender } = render(<ui.UsageSummary scope={usageScope} {...data} />);
|
|
369
|
+
expect(screen.queryByText("No usage in this period.")).toBeNull();
|
|
370
|
+
if (data.summary.status === "available") data.summary.value.complete = true;
|
|
371
|
+
rerender(<ui.UsageSummary scope={usageScope} {...data} />);
|
|
372
|
+
expect(screen.getByText("No usage in this period.")).toBeTruthy();
|
|
373
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
UsageAvailability,
|
|
3
|
+
UsagePeriod,
|
|
4
|
+
} from "../../client/usage/boundaries.js";
|
|
5
|
+
import type { UsageLocalSummary } from "../../client/usage/summary.js";
|
|
6
|
+
import type { UsageAggregateObservation } from "../../client/usage/observationTypes.js";
|
|
7
|
+
import type { UsageAllowanceStatus } from "../../client/usage/allowanceTerms.js";
|
|
8
|
+
import type {
|
|
9
|
+
UsageCreditBalance,
|
|
10
|
+
UsageCreditPurchase,
|
|
11
|
+
} from "../../client/usage/creditTerms.js";
|
|
12
|
+
import type { UsageCreditReconciliation } from "../../client/usage/creditObservation.js";
|
|
13
|
+
/** Cache identity only. The authenticated host must resolve and authorize original native bindings. */
|
|
14
|
+
export type UsageViewScope = {
|
|
15
|
+
identityKey: string;
|
|
16
|
+
accountKey: string;
|
|
17
|
+
/** Include original plan/price version in this opaque host meter key. */
|
|
18
|
+
meterKey: string;
|
|
19
|
+
period: UsagePeriod;
|
|
20
|
+
currency: string;
|
|
21
|
+
};
|
|
22
|
+
export type UsageBillingSnapshot = {
|
|
23
|
+
summary: UsageAvailability<UsageLocalSummary>;
|
|
24
|
+
provider?: UsageAggregateObservation;
|
|
25
|
+
allowance?: UsageAvailability<UsageAllowanceStatus | null>;
|
|
26
|
+
credits?: UsageAvailability<UsageCreditBalance>;
|
|
27
|
+
purchase?: UsageCreditPurchase | null;
|
|
28
|
+
creditApplication?: UsageAvailability<UsageCreditReconciliation>;
|
|
29
|
+
};
|
|
30
|
+
export function usageViewKey(scope: UsageViewScope) {
|
|
31
|
+
return JSON.stringify([
|
|
32
|
+
scope.identityKey,
|
|
33
|
+
scope.accountKey,
|
|
34
|
+
scope.meterKey,
|
|
35
|
+
scope.period.start,
|
|
36
|
+
scope.period.end,
|
|
37
|
+
scope.currency.toLowerCase(),
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import type { UsageAvailability } from "../../client/usage/boundaries.js";
|
|
4
|
+
import type { UsageAllowanceStatus } from "../../client/usage/allowanceTerms.js";
|
|
5
|
+
import { allowanceProgress } from "./usage-format.js";
|
|
6
|
+
import { formatUsageEstimate } from "./usage-format.js";
|
|
7
|
+
export type UsageAllowanceProps = {
|
|
8
|
+
allowance: UsageAvailability<UsageAllowanceStatus | null> | undefined;
|
|
9
|
+
unit: string;
|
|
10
|
+
className?: string;
|
|
11
|
+
children?: (state: {
|
|
12
|
+
allowance: UsageAllowanceProps["allowance"];
|
|
13
|
+
unit: string;
|
|
14
|
+
}) => ReactNode;
|
|
15
|
+
};
|
|
16
|
+
export function UsageAllowance({
|
|
17
|
+
allowance,
|
|
18
|
+
unit,
|
|
19
|
+
className,
|
|
20
|
+
children,
|
|
21
|
+
}: UsageAllowanceProps) {
|
|
22
|
+
if (children) return <>{children({ allowance, unit })}</>;
|
|
23
|
+
return (
|
|
24
|
+
<section
|
|
25
|
+
data-better-stripe="usage-allowance"
|
|
26
|
+
className={className}
|
|
27
|
+
aria-label="Usage allowance"
|
|
28
|
+
aria-busy={allowance === undefined}
|
|
29
|
+
>
|
|
30
|
+
<h2>Usage allowance and limits</h2>
|
|
31
|
+
{!allowance ? (
|
|
32
|
+
<p role="status">Loading allowance…</p>
|
|
33
|
+
) : allowance.status !== "available" ? (
|
|
34
|
+
<p role="status">
|
|
35
|
+
Allowance {allowance.status}. Verified policy evidence is required.
|
|
36
|
+
</p>
|
|
37
|
+
) : !allowance.value ? (
|
|
38
|
+
<p>No allowance policy configured.</p>
|
|
39
|
+
) : (
|
|
40
|
+
<>
|
|
41
|
+
{allowanceProgress(allowance.value) !== null && (
|
|
42
|
+
<progress
|
|
43
|
+
aria-label="Included usage"
|
|
44
|
+
max={10000}
|
|
45
|
+
value={allowanceProgress(allowance.value)!}
|
|
46
|
+
aria-valuetext={`${allowance.value.consumedQuantity} consumed and ${allowance.value.reservedQuantity} reserved of ${allowance.value.includedQuantity} ${unit} included`}
|
|
47
|
+
/>
|
|
48
|
+
)}
|
|
49
|
+
<dl>
|
|
50
|
+
{(
|
|
51
|
+
[
|
|
52
|
+
["Included", allowance.value.includedQuantity],
|
|
53
|
+
["Consumed", allowance.value.consumedQuantity],
|
|
54
|
+
["Reserved", allowance.value.reservedQuantity],
|
|
55
|
+
[
|
|
56
|
+
"Remaining included",
|
|
57
|
+
allowance.value.remainingIncludedQuantity,
|
|
58
|
+
],
|
|
59
|
+
["Billable overage", allowance.value.billableOverageQuantity],
|
|
60
|
+
] as const
|
|
61
|
+
).map(([label, quantity]) => (
|
|
62
|
+
<div key={label}>
|
|
63
|
+
<dt>{label}</dt>
|
|
64
|
+
<dd>
|
|
65
|
+
{quantity} {unit}
|
|
66
|
+
</dd>
|
|
67
|
+
</div>
|
|
68
|
+
))}
|
|
69
|
+
</dl>
|
|
70
|
+
{allowance.value.policy.limit.mode !== "none" && (
|
|
71
|
+
<p>
|
|
72
|
+
{allowance.value.policy.limit.mode === "hard"
|
|
73
|
+
? "Hard limit"
|
|
74
|
+
: "Soft threshold"}
|
|
75
|
+
: {allowance.value.policy.limit.quantity} {unit}
|
|
76
|
+
</p>
|
|
77
|
+
)}
|
|
78
|
+
{allowance.value.hardLimitRemainingQuantity !== null && (
|
|
79
|
+
<p>
|
|
80
|
+
Hard limit remaining: {allowance.value.hardLimitRemainingQuantity}{" "}
|
|
81
|
+
{unit}
|
|
82
|
+
</p>
|
|
83
|
+
)}
|
|
84
|
+
{allowance.value.softLimitReached && (
|
|
85
|
+
<p role="status">Soft threshold reached. Usage may continue.</p>
|
|
86
|
+
)}
|
|
87
|
+
{!allowance.value.accepting && (
|
|
88
|
+
<p role="status">
|
|
89
|
+
New usage is not currently accepting reservations.
|
|
90
|
+
</p>
|
|
91
|
+
)}
|
|
92
|
+
<p>
|
|
93
|
+
Committed estimate:{" "}
|
|
94
|
+
{formatUsageEstimate(
|
|
95
|
+
allowance.value.committedEstimate.amountDecimal,
|
|
96
|
+
allowance.value.committedEstimate.currency,
|
|
97
|
+
) ?? "Unavailable"}
|
|
98
|
+
</p>
|
|
99
|
+
<p>
|
|
100
|
+
Including reservations:{" "}
|
|
101
|
+
{formatUsageEstimate(
|
|
102
|
+
allowance.value.projectedEstimate.amountDecimal,
|
|
103
|
+
allowance.value.projectedEstimate.currency,
|
|
104
|
+
) ?? "Unavailable"}
|
|
105
|
+
</p>
|
|
106
|
+
<p>Resets at the verified billing-period boundary. No rollover.</p>
|
|
107
|
+
</>
|
|
108
|
+
)}
|
|
109
|
+
<p>
|
|
110
|
+
Display only. The server reservation enforces entitlement and limits.
|
|
111
|
+
</p>
|
|
112
|
+
</section>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usageSnapshotMatches } from "./usage-scope.js";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { useWorkflowResource } from "./useWorkflowResource.js";
|
|
5
|
+
import { UsageSummary } from "./UsageSummary.js";
|
|
6
|
+
import { UsageAllowance } from "./UsageAllowance.js";
|
|
7
|
+
import { UsageCredits } from "./UsageCredits.js";
|
|
8
|
+
import {
|
|
9
|
+
usageViewKey,
|
|
10
|
+
type UsageBillingSnapshot,
|
|
11
|
+
type UsageViewScope,
|
|
12
|
+
} from "./Usage.types.js";
|
|
13
|
+
export type UsageBillingProps = {
|
|
14
|
+
scope: UsageViewScope;
|
|
15
|
+
/** Authenticate and derive the original native binding in the host, including every refresh. */
|
|
16
|
+
load: (scope: UsageViewScope) => Promise<UsageBillingSnapshot>;
|
|
17
|
+
unit?: string;
|
|
18
|
+
className?: string;
|
|
19
|
+
children?: (state: ReturnType<typeof useUsageBilling>) => ReactNode;
|
|
20
|
+
};
|
|
21
|
+
export function useUsageBilling({
|
|
22
|
+
scope,
|
|
23
|
+
load,
|
|
24
|
+
}: Pick<UsageBillingProps, "scope" | "load">) {
|
|
25
|
+
const resource = useWorkflowResource(async () => {
|
|
26
|
+
try {
|
|
27
|
+
const data = await load(scope);
|
|
28
|
+
if (!usageSnapshotMatches(scope, data))
|
|
29
|
+
throw new Error("Observation scope mismatch");
|
|
30
|
+
return data;
|
|
31
|
+
} catch {
|
|
32
|
+
throw new Error("Usage unavailable. Try again.");
|
|
33
|
+
}
|
|
34
|
+
}, usageViewKey(scope));
|
|
35
|
+
return resource;
|
|
36
|
+
}
|
|
37
|
+
/** Shared workflow for component-backed and via-API host adapters. No browser provider credentials. */
|
|
38
|
+
export function UsageBilling(props: UsageBillingProps) {
|
|
39
|
+
return <UsageBillingBody key={usageViewKey(props.scope)} {...props} />;
|
|
40
|
+
}
|
|
41
|
+
function UsageBillingBody(props: UsageBillingProps) {
|
|
42
|
+
const state = useUsageBilling(props);
|
|
43
|
+
if (props.children) return <>{props.children(state)}</>;
|
|
44
|
+
return (
|
|
45
|
+
<section
|
|
46
|
+
data-better-stripe="usage-billing"
|
|
47
|
+
className={props.className}
|
|
48
|
+
aria-label="Metered billing"
|
|
49
|
+
aria-busy={state.isLoading}
|
|
50
|
+
>
|
|
51
|
+
<button type="button" onClick={state.refresh} disabled={state.isLoading}>
|
|
52
|
+
Refresh usage
|
|
53
|
+
</button>
|
|
54
|
+
{state.error && <p role="alert">{state.error}</p>}
|
|
55
|
+
{(state.isLoading || state.error) && state.data && (
|
|
56
|
+
<p role="status">
|
|
57
|
+
Refreshing usage. Displayed observations may be stale.
|
|
58
|
+
</p>
|
|
59
|
+
)}
|
|
60
|
+
<UsageSummary
|
|
61
|
+
scope={props.scope}
|
|
62
|
+
summary={
|
|
63
|
+
state.data?.summary ??
|
|
64
|
+
(state.error
|
|
65
|
+
? {
|
|
66
|
+
status: "unavailable",
|
|
67
|
+
reason: "read_unavailable",
|
|
68
|
+
retryable: true,
|
|
69
|
+
}
|
|
70
|
+
: undefined)
|
|
71
|
+
}
|
|
72
|
+
provider={state.data?.provider}
|
|
73
|
+
/>
|
|
74
|
+
{state.data?.allowance && (
|
|
75
|
+
<UsageAllowance
|
|
76
|
+
allowance={state.data.allowance}
|
|
77
|
+
unit={props.unit ?? "units"}
|
|
78
|
+
/>
|
|
79
|
+
)}
|
|
80
|
+
{state.data?.credits && (
|
|
81
|
+
<UsageCredits
|
|
82
|
+
currency={props.scope.currency}
|
|
83
|
+
balance={state.data.credits}
|
|
84
|
+
purchase={state.data.purchase}
|
|
85
|
+
application={state.data.creditApplication}
|
|
86
|
+
/>
|
|
87
|
+
)}
|
|
88
|
+
</section>
|
|
89
|
+
);
|
|
90
|
+
}
|