Repository navigation
fix(dashboard): logs auto-refresh reads live visibility, not a stale mount ref (#3972) #3997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 1 commit into
release/v3.8.27
from
fix/3972-logs-autorefresh-visibility
Jun 16, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
tests/unit/request-logger-autorefresh-visibility-3972.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // @vitest-environment jsdom | ||
| /** | ||
| * TDD regression for #3972: Logs page auto-refresh is broken — it never polls | ||
| * until the manual Refresh button is clicked. | ||
| * | ||
| * Root cause: the auto-refresh interval gated each tick on `visibleRef.current`, | ||
| * a ref seeded once at mount from `document.visibilityState` and only updated by | ||
| * a `visibilitychange` event. When the logs tab mounts while the document is | ||
| * reported "hidden" (background load, bfcache restore, embedded/proxied webviews) | ||
| * and no `visibilitychange` ever fires, the ref stays `false` forever — the | ||
| * interval ticks but never calls `fetchLogs`, so auto-refresh produces zero | ||
| * requests. The manual button (no gate) still works, matching the report. | ||
| * | ||
| * Fix: the tick reads the live `document.visibilityState` instead of the stale | ||
| * ref, so polling self-heals as soon as the tab is visible. | ||
| * | ||
| * This test mounts hidden, then flips visibility to "visible" WITHOUT dispatching | ||
| * a `visibilitychange` event, and asserts the 10s tick still polls. | ||
| */ | ||
| import React from "react"; | ||
| import { act } from "react"; | ||
| import { createRoot, type Root } from "react-dom/client"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| vi.mock("next-intl", () => ({ | ||
| useTranslations: () => (key: string) => key, | ||
| })); | ||
|
|
||
| vi.mock("next/navigation", () => ({ | ||
| useRouter: () => ({ push: vi.fn(), replace: vi.fn(), prefetch: vi.fn(), refresh: vi.fn() }), | ||
| useSearchParams: () => new URLSearchParams(), | ||
| usePathname: () => "/dashboard/logs", | ||
| })); | ||
|
|
||
| vi.mock("@/store/emailPrivacyStore", () => ({ | ||
| default: () => ({ emailsVisible: true }), | ||
| })); | ||
|
|
||
| const RequestLoggerV2 = (await import("../../src/shared/components/RequestLoggerV2.tsx")).default; | ||
| const { DEFAULT_REFRESH_INTERVAL_SEC } = await import( | ||
| "../../src/shared/components/requestLoggerPreferences.ts" | ||
| ); | ||
|
|
||
| function setVisibility(state: "visible" | "hidden") { | ||
| Object.defineProperty(document, "visibilityState", { configurable: true, get: () => state }); | ||
| } | ||
|
|
||
| class FakeIntersectionObserver { | ||
| observe() {} | ||
| unobserve() {} | ||
| disconnect() {} | ||
| takeRecords() { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| let callLogsRequests = 0; | ||
| let container: HTMLElement; | ||
| let root: Root; | ||
|
|
||
| beforeEach(() => { | ||
| callLogsRequests = 0; | ||
| localStorage.clear(); | ||
| vi.stubGlobal("IntersectionObserver", FakeIntersectionObserver); | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn(async (input: RequestInfo | URL) => { | ||
| const url = String(input); | ||
| if (url.startsWith("/api/usage/call-logs")) { | ||
| callLogsRequests += 1; | ||
| return new Response("[]", { status: 200, headers: { "Content-Type": "application/json" } }); | ||
| } | ||
| if (url.startsWith("/api/provider-nodes")) { | ||
| return Response.json({ nodes: [] }); | ||
| } | ||
| if (url.startsWith("/api/logs/detail")) { | ||
| return Response.json({ enabled: false }); | ||
| } | ||
| return Response.json({}); | ||
| }) | ||
| ); | ||
| vi.useFakeTimers(); | ||
| container = document.createElement("div"); | ||
| document.body.appendChild(container); | ||
| root = createRoot(container); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await act(async () => { | ||
| root.unmount(); | ||
| }); | ||
| container.remove(); | ||
| vi.useRealTimers(); | ||
| vi.unstubAllGlobals(); | ||
| setVisibility("visible"); | ||
| }); | ||
|
|
||
| describe("RequestLoggerV2 auto-refresh (#3972)", () => { | ||
| it("keeps polling on the interval when the tab becomes visible without a visibilitychange event", async () => { | ||
| // Mounts while the document reports "hidden" → resolveInitialVisibility() = false. | ||
| setVisibility("hidden"); | ||
|
|
||
| await act(async () => { | ||
| root.render(<RequestLoggerV2 />); | ||
| }); | ||
| // Settle the mount fetches (logs + provider-nodes + detail). | ||
| await act(async () => { | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| }); | ||
|
|
||
| const afterMount = callLogsRequests; | ||
| expect(afterMount).toBeGreaterThanOrEqual(1); // initial load fired | ||
|
|
||
| // Tab becomes visible, but NO `visibilitychange` event is dispatched — this is | ||
| // the trap: the old code's visibleRef would stay false forever. | ||
| setVisibility("visible"); | ||
|
|
||
| // One auto-refresh interval tick (10s). | ||
| await act(async () => { | ||
| await vi.advanceTimersByTimeAsync(DEFAULT_REFRESH_INTERVAL_SEC * 1000); | ||
| }); | ||
|
|
||
| expect(callLogsRequests).toBeGreaterThan(afterMount); | ||
| }); | ||
|
|
||
| it("does not poll while the tab stays hidden (preserves the hidden-tab optimization)", async () => { | ||
| setVisibility("hidden"); | ||
|
|
||
| await act(async () => { | ||
| root.render(<RequestLoggerV2 />); | ||
| }); | ||
| await act(async () => { | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| }); | ||
| const afterMount = callLogsRequests; | ||
|
|
||
| // Stays hidden across two ticks → must not poll. | ||
| await act(async () => { | ||
| await vi.advanceTimersByTimeAsync(DEFAULT_REFRESH_INTERVAL_SEC * 2000); | ||
| }); | ||
|
|
||
| expect(callLogsRequests).toBe(afterMount); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this
setIntervalis registered inside auseEffecthook, it is guaranteed to execute only on the client side (browser environment) wheredocumentis always defined. Checkingtypeof document === "undefined"is redundant and can be safely removed to simplify the condition.