diff --git a/src/components/ServerDetailOnlineHistory.tsx b/src/components/ServerDetailOnlineHistory.tsx index af93f8c..48fbde4 100644 --- a/src/components/ServerDetailOnlineHistory.tsx +++ b/src/components/ServerDetailOnlineHistory.tsx @@ -245,7 +245,7 @@ export default function ServerDetailOnlineHistory({ }) { const { t } = useTranslation(); const { lastData } = useWebSocketContext(); - const [days, setDays] = useState(DAY_OPTIONS[1]); + const [days, setDays] = useState(DAY_OPTIONS[0]); const [guestMaxDays, setGuestMaxDays] = useState(null); const [periodMode, setPeriodMode] = useState("online"); const [visibleDays, setVisibleDays] = useState(PERIODS_INITIAL_DAYS); diff --git a/src/components/loading/OnlineHistorySkeleton.tsx b/src/components/loading/OnlineHistorySkeleton.tsx index c927d68..051d42c 100644 --- a/src/components/loading/OnlineHistorySkeleton.tsx +++ b/src/components/loading/OnlineHistorySkeleton.tsx @@ -30,7 +30,7 @@ function Metric({ footer = true }: { footer?: boolean }) { ); } -export default function OnlineHistorySkeleton({ days = 7 }: { days?: number }) { +export default function OnlineHistorySkeleton({ days = 1 }: { days?: number }) { const { t } = useTranslation(); const dayCount = Math.min(31, Math.max(1, days) + 1); const cardClassName = cn({ diff --git a/src/test/components/server-detail-online-history.test.tsx b/src/test/components/server-detail-online-history.test.tsx index 207d4a8..2eef023 100644 --- a/src/test/components/server-detail-online-history.test.tsx +++ b/src/test/components/server-detail-online-history.test.tsx @@ -82,7 +82,7 @@ describe("ServerDetailOnlineHistory", () => { screen.getByRole("status", { name: "serverOnlineHistory.loading" }), ).toHaveAttribute("aria-busy", "true"); expect(screen.getByTestId("online-history-loading")).toBeInTheDocument(); - expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledWith(7, 7); + expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledWith(7, 1); }); it("keeps existing content instead of flashing the skeleton while changing days", async () => { @@ -299,7 +299,7 @@ describe("ServerDetailOnlineHistory", () => { await user.click(screen.getByText("serverOnlineHistory.days30")); expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledTimes(1); - expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledWith(7, 7); + expect(apiMocks.fetchServerOnlineHistory).toHaveBeenCalledWith(7, 1); }); it("shows the login hint when the API rejects anonymous guests", async () => { @@ -314,30 +314,34 @@ describe("ServerDetailOnlineHistory", () => { ).toBeInTheDocument(); }); - it("falls back to one day and retries automatically for guests", async () => { + it("falls back to one day when guests request a longer window", async () => { const user = userEvent.setup(); apiMocks.fetchServerOnlineHistory + .mockResolvedValueOnce(onlineHistoryResponse()) .mockRejectedValueOnce( new Error("unauthorized: only 1d data available for guests"), ) - .mockResolvedValue(onlineHistoryResponse()); + .mockResolvedValue(onlineHistoryResponse(1)); renderWithQuery(); - // 首次 7d 被拒后自动以 1d 重试并渲染数据 + // 首屏直接请求 1d;手动选择 7d 被拒后自动回退到 1d。 await screen.findByText("serverOnlineHistory.dailyOnline"); + expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith(1, 7, 1); + await user.click(screen.getByText("serverOnlineHistory.days7")); await waitFor(() => { expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith( - 2, + 3, 7, 1, ); }); + expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith(2, 7, 7); // 更长窗口被锁定(游客上限 1 天),点击不再发起新请求 const calls = apiMocks.fetchServerOnlineHistory.mock.calls.length; await user.click(screen.getByText("serverOnlineHistory.days30")); - expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith(2, 7, 1); + expect(apiMocks.fetchServerOnlineHistory).toHaveBeenNthCalledWith(3, 7, 1); expect(apiMocks.fetchServerOnlineHistory.mock.calls.length).toBe(calls); });