diff --git a/apps/frontend/src/screens/LiveQuiz.tsx b/apps/frontend/src/screens/LiveQuiz.tsx index cbdd828..e9ef52a 100644 --- a/apps/frontend/src/screens/LiveQuiz.tsx +++ b/apps/frontend/src/screens/LiveQuiz.tsx @@ -66,6 +66,7 @@ function LiveQuiz({ isOrganizer = false }: LiveQuizProps) { const [joinCode, setJoinCode] = useState(''); // Set by the server on join: the host opened this lobby with live video. const [liveVideo, setLiveVideo] = useState(false); + const [startError, setStartError] = useState(''); @@ -267,6 +268,7 @@ function LiveQuiz({ isOrganizer = false }: LiveQuizProps) { // The server refused a message, for example when this login is not // an organizer of the quiz. console.error('[ws]', payload?.code, payload?.message); + if (payload?.code === 'NO_QUESTIONS') setStartError(payload.message); break; case 'participants:sync': setParticipants(payload.participants); @@ -561,6 +563,9 @@ function LiveQuiz({ isOrganizer = false }: LiveQuizProps) { > Start Game + {startError && ( +

{startError}

+ )} {notSentNotice('the game was not started.', 'the game can be started once the connection is back.')} diff --git a/apps/ws-server/src/utils/handleMessages.auth.test.ts b/apps/ws-server/src/utils/handleMessages.auth.test.ts index 2b99053..653b114 100644 --- a/apps/ws-server/src/utils/handleMessages.auth.test.ts +++ b/apps/ws-server/src/utils/handleMessages.auth.test.ts @@ -136,6 +136,7 @@ beforeEach(() => { } mocks.getCachedParticipants.mockResolvedValue([]); mocks.getCachedSession.mockResolvedValue({ status: "WAITING", quiz: { joinCode: "ABC123" } }); + mocks.getCachedQuestions.mockResolvedValue([{ id: "q-1", answers: [] }]); mocks.prisma.quizSession.updateMany.mockResolvedValue({ count: 1 }); }); @@ -325,6 +326,17 @@ describe("a verified organizer can run their quiz", () => { expect(errorCodes(host)).toEqual([]); }); + it("refuses to start a quiz that has no questions", async () => { + const host = await joinedHost(); + mocks.getCachedQuestions.mockResolvedValue([]); + + await handleMessage(host, { type: "quiz:start", payload: { sessionId } }); + + expect(errorCodes(host)).toEqual(["NO_QUESTIONS"]); + expect(mocks.prisma.quizSession.updateMany).not.toHaveBeenCalled(); + expect(mocks.broadcastToSession).not.toHaveBeenCalled(); + }); + it("uses the joined session when the message leaves it out", async () => { const host = await joinedHost(); diff --git a/apps/ws-server/src/utils/handleMessages.ts b/apps/ws-server/src/utils/handleMessages.ts index 2377da7..0b21b3c 100644 --- a/apps/ws-server/src/utils/handleMessages.ts +++ b/apps/ws-server/src/utils/handleMessages.ts @@ -353,6 +353,13 @@ export const handleMessage = async (client: Client, data: any) => { case 'quiz:start': { const sessionId = authorizeOrganizerAction(client, payload); if (!sessionId) break; + // Starting a quiz with nothing to ask only walks the room through + // "get ready" and then ends the session as COMPLETED. + const startQuestions = await getCachedQuestions(sessionId); + if (!startQuestions || startQuestions.length === 0) { + sendError(client, 'NO_QUESTIONS', 'Add at least one question before starting the quiz.'); + break; + } // A session the super-admin cancelled (or one that already // ended) must not be revived by a host coming back to the lobby. const { count } = await prisma.quizSession.updateMany({