mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-11-20 07:52:52 +08:00
c17de413e2
The usage page could only show converted amounts with a hand-entered exchange rate. Add an opt-in "fetch automatically" rate source. - Usage settings > Display gains a rate source selector: manual (default) or automatic. Nothing is fetched until the user picks automatic. - Automatic rates are stored separately from manual ones, so switching back to manual keeps the user's own values. - New fetch_exchange_rates command tries open.er-api.com, then floatrates.com (8 s timeout each, app-wide proxy honored). - In automatic mode the usage page refreshes rates older than 12 hours; failures keep the last cached or manual rate. Costs are still stored in USD. - The hint links to the actual data source (ExchangeRate-API or FloatRates), as open.er-api.com's free terms require. - Async saves merge into the latest settings and are dropped after the page unmounts, so concurrent setting changes are not overwritten.
375 lines
12 KiB
TypeScript
375 lines
12 KiB
TypeScript
import { useState } from "react";
|
|
import { act, render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { UsageDisplaySection } from "@/components/usage/UsageDisplaySection";
|
|
import type { FetchedExchangeRates, UsageDisplaySettings } from "@/types";
|
|
|
|
const fetchExchangeRatesMock = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock("@/lib/api", () => ({
|
|
usageApi: { fetchExchangeRates: fetchExchangeRatesMock },
|
|
}));
|
|
|
|
const pickCurrency = async (
|
|
user: ReturnType<typeof userEvent.setup>,
|
|
code: string,
|
|
) => {
|
|
await user.click(
|
|
screen.getByRole("combobox", { name: "usage.display.currency" }),
|
|
);
|
|
await user.click(
|
|
await screen.findByRole("option", { name: new RegExp(`^${code}`) }),
|
|
);
|
|
};
|
|
|
|
describe("UsageDisplaySection", () => {
|
|
// jsdom 没有 scrollIntoView,下拉打开时会调
|
|
beforeAll(() => {
|
|
Element.prototype.scrollIntoView = vi.fn();
|
|
});
|
|
|
|
beforeEach(() => {
|
|
// 默认让取汇率的请求一直挂着:不相干的用例不需要异步保存
|
|
fetchExchangeRatesMock.mockReset();
|
|
fetchExchangeRatesMock.mockReturnValue(new Promise(() => {}));
|
|
});
|
|
|
|
it("offers every display currency and hides the rate while showing USD", async () => {
|
|
const user = userEvent.setup();
|
|
const onChange = vi.fn();
|
|
render(<UsageDisplaySection value={undefined} onChange={onChange} />);
|
|
|
|
expect(screen.queryByRole("textbox")).not.toBeInTheDocument();
|
|
await user.click(
|
|
screen.getByRole("combobox", { name: "usage.display.currency" }),
|
|
);
|
|
const options = await screen.findAllByRole("option");
|
|
expect(options.map((o) => o.textContent?.slice(0, 3))).toEqual([
|
|
"USD",
|
|
"CNY",
|
|
"EUR",
|
|
"JPY",
|
|
"TWD",
|
|
"TRY",
|
|
]);
|
|
await user.keyboard("{Escape}");
|
|
|
|
await pickCurrency(user, "JPY");
|
|
expect(onChange).toHaveBeenCalledWith({ currency: "JPY" });
|
|
// 预置汇率:用户没填过时从这里起步
|
|
expect(screen.getByRole("textbox")).toHaveValue("158");
|
|
});
|
|
|
|
it("rejects a bad rate and keeps the rates of other currencies", async () => {
|
|
const user = userEvent.setup();
|
|
const onChange = vi.fn();
|
|
render(
|
|
<UsageDisplaySection
|
|
value={{
|
|
currency: "CNY",
|
|
exchangeRates: { CNY: 7, EUR: 0.9 },
|
|
rateSource: "manual",
|
|
}}
|
|
onChange={onChange}
|
|
/>,
|
|
);
|
|
const input = screen.getByRole("textbox");
|
|
expect(input).toHaveValue("7");
|
|
|
|
await user.clear(input);
|
|
await user.type(input, "0{Enter}");
|
|
expect(screen.getByText("usage.display.rateInvalid")).toBeInTheDocument();
|
|
expect(input).toHaveAttribute("aria-invalid", "true");
|
|
expect(onChange).not.toHaveBeenCalled();
|
|
|
|
await user.clear(input);
|
|
await user.type(input, "7.25{Enter}");
|
|
expect(onChange).toHaveBeenCalledWith({
|
|
currency: "CNY",
|
|
rateSource: "manual",
|
|
exchangeRates: { CNY: 7.25, EUR: 0.9 },
|
|
});
|
|
});
|
|
|
|
it("switches the rate box to the rate saved for the picked currency", async () => {
|
|
const user = userEvent.setup();
|
|
const onChange = vi.fn();
|
|
render(
|
|
<UsageDisplaySection
|
|
value={{
|
|
currency: "CNY",
|
|
exchangeRates: { CNY: 7, EUR: 0.9 },
|
|
rateSource: "manual",
|
|
}}
|
|
onChange={onChange}
|
|
/>,
|
|
);
|
|
await pickCurrency(user, "EUR");
|
|
expect(screen.getByRole("textbox")).toHaveValue("0.9");
|
|
});
|
|
|
|
it("shows the fetched rate read-only while the source is automatic", () => {
|
|
render(
|
|
<UsageDisplaySection
|
|
value={{
|
|
currency: "CNY",
|
|
rateSource: "auto",
|
|
autoExchangeRates: { CNY: 6.9 },
|
|
exchangeRates: { CNY: 7.1 },
|
|
}}
|
|
onChange={vi.fn()}
|
|
/>,
|
|
);
|
|
const input = screen.getByRole("textbox");
|
|
expect(input).toHaveValue("6.9");
|
|
expect(input).toHaveAttribute("readonly");
|
|
expect(
|
|
screen.getByRole("combobox", { name: "usage.display.rateSource" }),
|
|
).toHaveTextContent("usage.display.rateSources.auto");
|
|
});
|
|
|
|
it("keeps the currency picked while a rate refresh is in flight", async () => {
|
|
const user = userEvent.setup();
|
|
let resolveFetch!: (value: FetchedExchangeRates) => void;
|
|
fetchExchangeRatesMock.mockReturnValueOnce(
|
|
new Promise<FetchedExchangeRates>((resolve) => {
|
|
resolveFetch = resolve;
|
|
}),
|
|
);
|
|
|
|
const saved: Array<UsageDisplaySettings | undefined> = [];
|
|
const Harness = () => {
|
|
const [value, setValue] = useState<UsageDisplaySettings | undefined>({
|
|
rateSource: "auto",
|
|
});
|
|
return (
|
|
<UsageDisplaySection
|
|
value={value}
|
|
onChange={async (next) => {
|
|
saved.push(next);
|
|
setValue(next);
|
|
return true;
|
|
}}
|
|
/>
|
|
);
|
|
};
|
|
render(<Harness />);
|
|
|
|
await pickCurrency(user, "CNY");
|
|
await waitFor(() =>
|
|
expect(fetchExchangeRatesMock).toHaveBeenCalledTimes(1),
|
|
);
|
|
// 请求在切币种之后才回来:保存时不能把币种写回切换前的值
|
|
await act(async () => {
|
|
resolveFetch({
|
|
rates: { CNY: 6.9 },
|
|
fetchedAt: 1_700_000_000_000,
|
|
source: "open.er-api.com",
|
|
});
|
|
});
|
|
|
|
const last = saved.at(-1);
|
|
expect(last?.currency).toBe("CNY");
|
|
expect(last?.autoExchangeRates?.CNY).toBe(6.9);
|
|
});
|
|
|
|
it("keeps settings changed while a rate refresh is in flight", async () => {
|
|
const user = userEvent.setup();
|
|
let resolveFetch!: (value: FetchedExchangeRates) => void;
|
|
fetchExchangeRatesMock.mockReturnValueOnce(
|
|
new Promise<FetchedExchangeRates>((resolve) => {
|
|
resolveFetch = resolve;
|
|
}),
|
|
);
|
|
|
|
// 模拟 UsagePage 的 autoSaveSettings:用它闭包里的整份设置合并保存。
|
|
// 请求在途时改了刷新间隔,汇率回来后不能把间隔改回去。
|
|
const saved: Array<{ usage: UsageDisplaySettings; refreshMs: number }> = [];
|
|
const Harness = () => {
|
|
const [usage, setUsage] = useState<UsageDisplaySettings>({
|
|
currency: "CNY",
|
|
rateSource: "auto",
|
|
});
|
|
const [refreshMs, setRefreshMs] = useState(60_000);
|
|
const captured = { usage, refreshMs };
|
|
return (
|
|
<>
|
|
<UsageDisplaySection
|
|
value={usage}
|
|
onChange={async (next) => {
|
|
saved.push({ usage: next, refreshMs: captured.refreshMs });
|
|
setUsage(next);
|
|
setRefreshMs(captured.refreshMs);
|
|
return true;
|
|
}}
|
|
/>
|
|
<button type="button" onClick={() => setRefreshMs(120_000)}>
|
|
change refresh
|
|
</button>
|
|
</>
|
|
);
|
|
};
|
|
render(<Harness />);
|
|
|
|
await user.click(
|
|
screen.getByRole("button", { name: "usage.display.rateRefresh" }),
|
|
);
|
|
await waitFor(() =>
|
|
expect(fetchExchangeRatesMock).toHaveBeenCalledTimes(1),
|
|
);
|
|
await user.click(screen.getByRole("button", { name: "change refresh" }));
|
|
await act(async () => {
|
|
resolveFetch({
|
|
rates: { CNY: 6.9 },
|
|
fetchedAt: 1_700_000_000_000,
|
|
source: "open.er-api.com",
|
|
});
|
|
});
|
|
|
|
const last = saved.at(-1);
|
|
expect(last?.refreshMs).toBe(120_000);
|
|
expect(last?.usage.autoExchangeRates?.CNY).toBe(6.9);
|
|
expect(last?.usage.rateProvider).toBe("open.er-api.com");
|
|
});
|
|
|
|
it("drops a fetched rate when the page was left meanwhile", async () => {
|
|
const user = userEvent.setup();
|
|
let resolveFetch!: (value: FetchedExchangeRates) => void;
|
|
fetchExchangeRatesMock.mockReturnValueOnce(
|
|
new Promise<FetchedExchangeRates>((resolve) => {
|
|
resolveFetch = resolve;
|
|
}),
|
|
);
|
|
const onChange = vi.fn();
|
|
const { unmount } = render(
|
|
<UsageDisplaySection
|
|
value={{ currency: "CNY", rateSource: "auto" }}
|
|
onChange={onChange}
|
|
/>,
|
|
);
|
|
|
|
await user.click(
|
|
screen.getByRole("button", { name: "usage.display.rateRefresh" }),
|
|
);
|
|
await waitFor(() =>
|
|
expect(fetchExchangeRatesMock).toHaveBeenCalledTimes(1),
|
|
);
|
|
unmount();
|
|
await act(async () => {
|
|
resolveFetch({
|
|
rates: { CNY: 6.9 },
|
|
fetchedAt: 1_700_000_000_000,
|
|
source: "open.er-api.com",
|
|
});
|
|
});
|
|
|
|
expect(onChange).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("credits the provider of the automatic rates", () => {
|
|
render(
|
|
<UsageDisplaySection
|
|
value={{
|
|
currency: "CNY",
|
|
rateSource: "auto",
|
|
autoExchangeRates: { CNY: 6.9 },
|
|
rateProvider: "floatrates.com",
|
|
}}
|
|
onChange={vi.fn()}
|
|
/>,
|
|
);
|
|
// 备用源署它自己的名字;主源(open.er-api.com)的署名由条款要求
|
|
expect(
|
|
screen.getByRole("link", { name: "usage.display.rateAttribution" }),
|
|
).toHaveAttribute("href", "https://www.floatrates.com/");
|
|
});
|
|
|
|
it("does not write the preset rate when it was left unchanged", async () => {
|
|
const user = userEvent.setup();
|
|
const onChange = vi.fn();
|
|
render(
|
|
<UsageDisplaySection value={{ currency: "TWD" }} onChange={onChange} />,
|
|
);
|
|
await user.type(screen.getByRole("textbox"), "{Enter}");
|
|
expect(onChange).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("picks up to two items for the sidebar Today label", async () => {
|
|
const user = userEvent.setup();
|
|
const onChange = vi.fn();
|
|
render(
|
|
<UsageDisplaySection value={{ currency: "CNY" }} onChange={onChange} />,
|
|
);
|
|
const trigger = screen.getByRole("button", {
|
|
name: /^usage\.display\.sidebarToday:/,
|
|
});
|
|
expect(trigger).toHaveTextContent("usage.display.sidebarTodayItems.cost");
|
|
|
|
const item = (key: string) =>
|
|
screen.getByRole("menuitemcheckbox", {
|
|
name: `usage.display.sidebarTodayItems.${key}`,
|
|
});
|
|
await user.click(trigger);
|
|
expect(
|
|
await screen.findByText("usage.display.sidebarTodayLimit"),
|
|
).toBeInTheDocument();
|
|
expect(item("cost")).toHaveAttribute("aria-checked", "true");
|
|
|
|
// 勾的先后不影响顺序:按菜单顺序存
|
|
await user.click(item("hitRate"));
|
|
expect(onChange).toHaveBeenLastCalledWith({
|
|
currency: "CNY",
|
|
sidebarTodayItems: ["cost", "hitRate"],
|
|
});
|
|
expect(trigger).toHaveTextContent(
|
|
"usage.display.sidebarTodayItems.cost + usage.display.sidebarTodayItems.hitRate",
|
|
);
|
|
|
|
// 勾满两项:没勾的灰掉,点了也不变
|
|
expect(item("requests")).toHaveAttribute("aria-disabled", "true");
|
|
expect(item("tokens")).toHaveAttribute("aria-disabled", "true");
|
|
await user.click(item("requests"));
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
|
|
|
// 去掉一项后其余的又能勾;全去掉=不显示,存空列表
|
|
await user.click(item("cost"));
|
|
expect(onChange).toHaveBeenLastCalledWith({
|
|
currency: "CNY",
|
|
sidebarTodayItems: ["hitRate"],
|
|
});
|
|
expect(item("requests")).not.toHaveAttribute("aria-disabled");
|
|
await user.click(item("hitRate"));
|
|
expect(onChange).toHaveBeenLastCalledWith({
|
|
currency: "CNY",
|
|
sidebarTodayItems: [],
|
|
});
|
|
expect(trigger).toHaveTextContent("usage.display.sidebarTodayItems.none");
|
|
});
|
|
|
|
it("picks the trend chart style and keeps the other display settings", async () => {
|
|
const user = userEvent.setup();
|
|
const onChange = vi.fn();
|
|
render(
|
|
<UsageDisplaySection value={{ currency: "CNY" }} onChange={onChange} />,
|
|
);
|
|
const trigger = screen.getByRole("combobox", {
|
|
name: "usage.display.trendChartType",
|
|
});
|
|
// 没设置过按柱形图
|
|
expect(trigger).toHaveTextContent("usage.display.trendChartTypes.bar");
|
|
|
|
await user.click(trigger);
|
|
await user.click(
|
|
await screen.findByRole("option", {
|
|
name: "usage.display.trendChartTypes.area",
|
|
}),
|
|
);
|
|
expect(onChange).toHaveBeenCalledWith({
|
|
currency: "CNY",
|
|
trendChartType: "area",
|
|
});
|
|
expect(trigger).toHaveTextContent("usage.display.trendChartTypes.area");
|
|
});
|
|
});
|