diff --git a/apps/frontend/src/screens/LiveQuiz.test.tsx b/apps/frontend/src/screens/LiveQuiz.test.tsx index 8cdd955..bcab9f8 100644 --- a/apps/frontend/src/screens/LiveQuiz.test.tsx +++ b/apps/frontend/src/screens/LiveQuiz.test.tsx @@ -33,7 +33,7 @@ class FakeSocket { } function renderLive(path: string, isOrganizer: boolean) { - render( + return render( { }); afterEach(() => { + vi.useRealTimers(); vi.unstubAllGlobals(); useAuthStore.setState({ user: null, token: null }); }); @@ -210,3 +211,29 @@ describe('LiveQuiz host controls on a dropped connection', () => { expect(screen.queryByRole('alert')).toBeNull(); }); }); + +describe('LiveQuiz host start countdown', () => { + it('cancels the first-question send when the host leaves', async () => { + const view = renderLive('/live/session-1', true); + await firstJoinMessage(); + vi.useFakeTimers(); + deliver('quiz:start', {}); + + view.unmount(); + act(() => vi.advanceTimersByTime(3000)); + + expect(sentOfType('quiz:next-question')).toHaveLength(0); + }); + + it('does not send the first question to a closed socket', async () => { + renderLive('/live/session-1', true); + await firstJoinMessage(); + vi.useFakeTimers(); + deliver('quiz:start', {}); + sockets[0]!.readyState = 3; + + act(() => vi.advanceTimersByTime(3000)); + + expect(sentOfType('quiz:next-question')).toHaveLength(0); + }); +}); diff --git a/apps/frontend/src/screens/LiveQuiz.tsx b/apps/frontend/src/screens/LiveQuiz.tsx index 4de5c29..cbdd828 100644 --- a/apps/frontend/src/screens/LiveQuiz.tsx +++ b/apps/frontend/src/screens/LiveQuiz.tsx @@ -174,6 +174,7 @@ function LiveQuiz({ isOrganizer = false }: LiveQuizProps) { let isMounted = true; let reconnectAttempts = 0; let reconnectTimer: ReturnType | null = null; + let startTimer: ReturnType | null = null; // Lobby-only reconciliation for the host; cleared once the quiz starts. let refreshTimer: ReturnType | null = null; // Set when we close a socket ourselves (unmount, or replacing a stale @@ -284,8 +285,12 @@ function LiveQuiz({ isOrganizer = false }: LiveQuizProps) { refreshTimer = null; } setGameState(GameState.STARTING); - setTimeout(() => { - if (isOrganizer) socket?.send(JSON.stringify({ type: 'quiz:next-question', payload: { sessionId, questionIndex: 0 } })); + if (startTimer) clearTimeout(startTimer); + startTimer = setTimeout(() => { + startTimer = null; + if (isMounted && isOrganizer && socket?.readyState === WebSocket.OPEN) { + socket.send(JSON.stringify({ type: 'quiz:next-question', payload: { sessionId, questionIndex: 0 } })); + } }, 3000); break; case 'quiz:question': @@ -403,6 +408,7 @@ function LiveQuiz({ isOrganizer = false }: LiveQuizProps) { deliberateClose = true; clearTimeout(timeoutId); if (reconnectTimer) clearTimeout(reconnectTimer); + if (startTimer) clearTimeout(startTimer); if (refreshTimer) clearInterval(refreshTimer); if (socket) { // Drop the handler first: closing a socket fires onclose, which would @@ -1130,4 +1136,4 @@ function LiveQuiz({ isOrganizer = false }: LiveQuizProps) { } -export default LiveQuiz \ No newline at end of file +export default LiveQuiz