mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-10-25 17:22:27 +08:00
The usage page's auto-refresh interval only re-read the database, so session usage still waited for the fixed 60-second background scan, and routed usage already updated in real time through usage-log-recorded. The interval now controls how often session logs are scanned: - The background scan drops to every 10 minutes as a safety net against CLIs pruning old session files. - While the usage page is visible it scans right away, again whenever the window becomes visible, and then at the selected interval (5/10/30/60s, default 60s). The first scan waits until settings load, so it never runs when automatic scanning is off. - The "Off" option is removed (a saved 0 falls back to 60s), and the interval menu is hidden when automatic scanning is off. - Statistics no longer poll. Imports refresh them through the usage-log-recorded event, which is always on again, and time-relative ranges re-run once a minute so "Today" rolls over at midnight and rolling windows drop old records without new usage. Copy is updated in zh, zh-TW, en, ja and tr, and in the zh/en/ja manual.
84 lines
2.8 KiB
TypeScript
84 lines
2.8 KiB
TypeScript
import { render } from "@testing-library/react";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { UsagePage } from "@/components/usage/UsagePage";
|
|
import { UsageDashboard } from "@/components/usage/UsageDashboard";
|
|
|
|
const autoSaveSettings = vi.hoisted(() => vi.fn());
|
|
const settingsState = vi.hoisted(() => ({
|
|
current: null as null | {
|
|
usageDashboardRefreshIntervalMs: number;
|
|
sessionAutoSyncEnabled: boolean;
|
|
},
|
|
}));
|
|
vi.mock("@/hooks/useSettings", () => ({
|
|
useSettings: () => ({ settings: settingsState.current, autoSaveSettings }),
|
|
}));
|
|
vi.mock("@/components/usage/UsageDashboard", () => ({
|
|
UsageDashboard: vi.fn(() => null),
|
|
}));
|
|
|
|
describe("UsagePage settings persistence", () => {
|
|
beforeEach(() => {
|
|
autoSaveSettings.mockReset();
|
|
vi.mocked(UsageDashboard).mockClear();
|
|
settingsState.current = {
|
|
usageDashboardRefreshIntervalMs: 30000,
|
|
sessionAutoSyncEnabled: true,
|
|
};
|
|
});
|
|
|
|
it("tells the dashboard settings are not ready until they load", () => {
|
|
settingsState.current = null;
|
|
const { rerender } = render(<UsagePage />);
|
|
expect(vi.mocked(UsageDashboard).mock.lastCall![0]).toMatchObject({
|
|
settingsReady: false,
|
|
});
|
|
settingsState.current = {
|
|
usageDashboardRefreshIntervalMs: 5000,
|
|
sessionAutoSyncEnabled: false,
|
|
};
|
|
rerender(<UsagePage />);
|
|
expect(vi.mocked(UsageDashboard).mock.lastCall![0]).toMatchObject({
|
|
settingsReady: true,
|
|
sessionAutoSyncEnabled: false,
|
|
});
|
|
});
|
|
|
|
it("waits for saving before reporting success to the dashboard", async () => {
|
|
let finish!: (value: { requiresRestart: boolean }) => void;
|
|
autoSaveSettings.mockReturnValue(
|
|
new Promise((resolve) => {
|
|
finish = resolve;
|
|
}),
|
|
);
|
|
render(<UsagePage />);
|
|
const onChange =
|
|
vi.mocked(UsageDashboard).mock.calls[0][0]!.onRefreshIntervalChange!;
|
|
const settled = vi.fn();
|
|
const save = Promise.resolve(onChange(5000)).then(settled);
|
|
await Promise.resolve();
|
|
expect(settled).not.toHaveBeenCalled();
|
|
expect(autoSaveSettings).toHaveBeenCalledWith({
|
|
usageDashboardRefreshIntervalMs: 5000,
|
|
});
|
|
finish({ requiresRestart: false });
|
|
await save;
|
|
expect(settled).toHaveBeenCalledWith(true);
|
|
});
|
|
|
|
it.each(["rejected", "not loaded"])(
|
|
"reports a failed save when settings are %s",
|
|
async (failure) => {
|
|
if (failure === "rejected")
|
|
autoSaveSettings.mockRejectedValue(new Error("save failed"));
|
|
else autoSaveSettings.mockResolvedValue(null);
|
|
render(<UsagePage />);
|
|
const props = vi.mocked(UsageDashboard).mock.calls[0][0]!;
|
|
await expect(props.onRefreshIntervalChange!(5000)).resolves.toBe(false);
|
|
await expect(props.onSessionAutoSyncEnabledChange!(false)).resolves.toBe(
|
|
false,
|
|
);
|
|
},
|
|
);
|
|
});
|