Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion apps/frontend/src/screens/LiveQuiz.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FakeSocket {
}

function renderLive(path: string, isOrganizer: boolean) {
render(
return render(
<MemoryRouter initialEntries={[path]}>
<Routes>
<Route
Expand Down Expand Up @@ -69,6 +69,7 @@ beforeEach(() => {
});

afterEach(() => {
vi.useRealTimers();
vi.unstubAllGlobals();
useAuthStore.setState({ user: null, token: null });
});
Expand Down Expand Up @@ -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);
});
});
12 changes: 9 additions & 3 deletions apps/frontend/src/screens/LiveQuiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@
const [sendFailed, setSendFailed] = useState(false);
const isConnected = connectionState === 'connected';
const [gameState, setGameState] = useState<GameState>('WAITING');
const [participants, setParticipants] = useState<any[]>([]);

Check failure on line 60 in apps/frontend/src/screens/LiveQuiz.tsx

View workflow job for this annotation

GitHub Actions / Lint & type-check

Unexpected any. Specify a different type
const [currentQuestion, setCurrentQuestion] = useState<any>(null);

Check failure on line 61 in apps/frontend/src/screens/LiveQuiz.tsx

View workflow job for this annotation

GitHub Actions / Lint & type-check

Unexpected any. Specify a different type
const [timeLeft, setTimeLeft] = useState(0);
const [correctAnswers, setCorrectAnswers] = useState<string[]>([]);
const [leaderboard, setLeaderboard] = useState<any[]>([]);

Check failure on line 64 in apps/frontend/src/screens/LiveQuiz.tsx

View workflow job for this annotation

GitHub Actions / Lint & type-check

Unexpected any. Specify a different type

const [joinCode, setJoinCode] = useState('');
// Set by the server on join: the host opened this lobby with live video.
Expand Down Expand Up @@ -133,7 +133,7 @@
{ rank: 2, player: sortedLeaderboard[1], order: 'order-2 sm:order-1' },
{ rank: 1, player: sortedLeaderboard[0], order: 'order-1 sm:order-2' },
{ rank: 3, player: sortedLeaderboard[2], order: 'order-3 sm:order-3' },
].filter((entry): entry is { rank: number; player: any; order: string } => Boolean(entry.player));

Check failure on line 136 in apps/frontend/src/screens/LiveQuiz.tsx

View workflow job for this annotation

GitHub Actions / Lint & type-check

Unexpected any. Specify a different type

// Everyone past the podium, capped: a 500-player session rendered the whole
// board, so the end of a quiz was a page of names nobody reads.
Expand Down Expand Up @@ -174,6 +174,7 @@
let isMounted = true;
let reconnectAttempts = 0;
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
let startTimer: ReturnType<typeof setTimeout> | null = null;
// Lobby-only reconciliation for the host; cleared once the quiz starts.
let refreshTimer: ReturnType<typeof setInterval> | null = null;
// Set when we close a socket ourselves (unmount, or replacing a stale
Expand Down Expand Up @@ -284,8 +285,12 @@
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':
Expand Down Expand Up @@ -403,6 +408,7 @@
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
Expand All @@ -416,7 +422,7 @@
}
};

}, [sessionId, isOrganizer]);

Check warning on line 425 in apps/frontend/src/screens/LiveQuiz.tsx

View workflow job for this annotation

GitHub Actions / Lint & type-check

React Hook useEffect has missing dependencies: 'navigate', 'participantId', 'token', and 'user?.id'. Either include them or remove the dependency array


// `ws?.send` on a closed socket does nothing and throws nothing, so every
Expand Down Expand Up @@ -1130,4 +1136,4 @@

}

export default LiveQuiz
export default LiveQuiz
Loading