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
5 changes: 5 additions & 0 deletions apps/frontend/src/screens/LiveQuiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('');



Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -561,6 +563,9 @@ function LiveQuiz({ isOrganizer = false }: LiveQuizProps) {
>
Start Game
</button>
{startError && (
<p role="alert" className="mt-3 text-sm text-red-400">{startError}</p>
)}
{notSentNotice('the game was not started.',
'the game can be started once the connection is back.')}
</>
Expand Down
12 changes: 12 additions & 0 deletions apps/ws-server/src/utils/handleMessages.auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});

Expand Down Expand Up @@ -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();

Expand Down
7 changes: 7 additions & 0 deletions apps/ws-server/src/utils/handleMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down