Fix unhandled rejection from session ping when network is unavailable
All checks were successful
🚀 Deploy / 🧪 Test (pull_request) Successful in 1m36s
🚀 Deploy / ʦ TypeScript (pull_request) Successful in 1m21s
🚀 Deploy / 🔍 Lint (pull_request) Successful in 50s
🚀 Deploy / 🐳 Build (pull_request) Has been skipped
🚀 Deploy / 🚀 Deploy (pull_request) Has been skipped
All checks were successful
🚀 Deploy / 🧪 Test (pull_request) Successful in 1m36s
🚀 Deploy / ʦ TypeScript (pull_request) Successful in 1m21s
🚀 Deploy / 🔍 Lint (pull_request) Successful in 50s
🚀 Deploy / 🐳 Build (pull_request) Has been skipped
🚀 Deploy / 🚀 Deploy (pull_request) Has been skipped
The 15-minute keep-alive interval in useDraftAuthRecovery called
authClient.getSession() with no try-catch. When the network is
unavailable (e.g. computer waking from sleep), better-fetch throws a
TypeError rather than returning an in-band error, producing an
unhandled promise rejection that was surfacing in Sentry.
Adds a try-catch that silently swallows network TypeErrors — real
session expiry (401) is still caught by the existing !session check
because better-fetch returns HTTP errors in-band as { data: null }.
Adds tests covering the three ping outcomes: network throw, null
session (expired), and valid session.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a9d4954242
commit
43660d16e9
2 changed files with 128 additions and 2 deletions
118
app/hooks/__tests__/useDraftAuthRecovery.session-ping.test.ts
Normal file
118
app/hooks/__tests__/useDraftAuthRecovery.session-ping.test.ts
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/**
|
||||
* Tests for the 15-minute session keep-alive ping in useDraftAuthRecovery.
|
||||
*
|
||||
* Verifies that:
|
||||
* - A network TypeError (fetch throws) is silently swallowed and does NOT
|
||||
* set authDegraded, because a transient network drop should not trigger the
|
||||
* "Session Expired" overlay mid-draft.
|
||||
* - A null session response (in-band 401 / expired) DOES set authDegraded.
|
||||
* - A valid session response leaves authDegraded false.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { renderHook, act } from "@testing-library/react";
|
||||
import { useDraftAuthRecovery } from "../useDraftAuthRecovery";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mock auth-client
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const { mockGetSession } = vi.hoisted(() => ({
|
||||
mockGetSession: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/lib/auth-client", () => ({
|
||||
authClient: { getSession: mockGetSession },
|
||||
}));
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Minimal hook params — only what the interval ping needs
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function makeParams() {
|
||||
return {
|
||||
reconnectCount: 0,
|
||||
revalidate: vi.fn(),
|
||||
revalidatorState: "idle" as const,
|
||||
currentUserId: "user-1",
|
||||
userAutodraftSettings: null,
|
||||
draftPicks: [],
|
||||
season: {} as any,
|
||||
userQueue: [],
|
||||
timers: [],
|
||||
autodraftSettings: [],
|
||||
setPicks: vi.fn(),
|
||||
setCurrentPick: vi.fn(),
|
||||
setIsPaused: vi.fn(),
|
||||
setIsDraftComplete: vi.fn(),
|
||||
setQueue: vi.fn(),
|
||||
setTeamTimers: vi.fn(),
|
||||
setAutodraftStatus: vi.fn(),
|
||||
setIsSyncing: vi.fn(),
|
||||
setUserAutodraft: vi.fn(),
|
||||
};
|
||||
}
|
||||
|
||||
const FIFTEEN_MINUTES = 15 * 60 * 1000;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe("useDraftAuthRecovery — session keep-alive ping", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
mockGetSession.mockReset();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("does NOT set authDegraded when getSession() throws a network TypeError", async () => {
|
||||
mockGetSession.mockRejectedValue(new TypeError("Failed to fetch"));
|
||||
|
||||
const { result } = renderHook(() => useDraftAuthRecovery(makeParams()));
|
||||
|
||||
expect(result.current.authDegraded).toBe(false);
|
||||
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(FIFTEEN_MINUTES);
|
||||
// flush the async callback
|
||||
await Promise.resolve();
|
||||
});
|
||||
|
||||
expect(result.current.authDegraded).toBe(false);
|
||||
});
|
||||
|
||||
it("sets authDegraded when getSession() returns null data (session expired)", async () => {
|
||||
mockGetSession.mockResolvedValue({ data: null, error: { status: 401 } });
|
||||
|
||||
const { result } = renderHook(() => useDraftAuthRecovery(makeParams()));
|
||||
|
||||
expect(result.current.authDegraded).toBe(false);
|
||||
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(FIFTEEN_MINUTES);
|
||||
await Promise.resolve();
|
||||
});
|
||||
|
||||
expect(result.current.authDegraded).toBe(true);
|
||||
});
|
||||
|
||||
it("leaves authDegraded false when getSession() returns a valid session", async () => {
|
||||
mockGetSession.mockResolvedValue({
|
||||
data: { userId: "user-1" },
|
||||
error: null,
|
||||
});
|
||||
|
||||
const { result } = renderHook(() => useDraftAuthRecovery(makeParams()));
|
||||
|
||||
await act(async () => {
|
||||
vi.advanceTimersByTime(FIFTEEN_MINUTES);
|
||||
await Promise.resolve();
|
||||
});
|
||||
|
||||
expect(result.current.authDegraded).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
@ -120,8 +120,16 @@ export function useDraftAuthRecovery({
|
|||
if (authDegraded) return;
|
||||
const FIFTEEN_MINUTES = 15 * 60 * 1000;
|
||||
const id = setInterval(async () => {
|
||||
const { data: session } = await authClient.getSession();
|
||||
if (!session) setAuthDegraded(true);
|
||||
try {
|
||||
const { data: session } = await authClient.getSession();
|
||||
if (!session) setAuthDegraded(true);
|
||||
} catch {
|
||||
// Network failures throw a TypeError from fetch(), but real session
|
||||
// expiry (401) is returned in-band as { data: null } by better-fetch
|
||||
// and caught by the !session check above. Swallowing the throw here
|
||||
// means a transient network drop doesn't trigger the auth overlay
|
||||
// mid-draft.
|
||||
}
|
||||
}, FIFTEEN_MINUTES);
|
||||
return () => clearInterval(id);
|
||||
}, [authDegraded]);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue