diff --git a/src/components/exams/exam-room-ws.test.tsx b/src/components/exams/exam-room-ws.test.tsx index df8a138..6df2fc6 100644 --- a/src/components/exams/exam-room-ws.test.tsx +++ b/src/components/exams/exam-room-ws.test.tsx @@ -397,3 +397,31 @@ test("an expired frame zeroes the clock and a later state frame does not reopen expect(closedBanners().length).toBeGreaterThan(0); expect(choice("Paris").disabled).toBe(true); }); + +// The other direction of the rejoin hazard: a live-looking `state` frame must +// not un-finish a sitting that is already over. +test("a state frame claiming in_progress cannot reopen a finished sheet", async () => { + await openWritableRoom(); + socket().deliver({ type: "finished", finished_at: Date.now() }); + await tick(); + + socket().deliver(liveState); // status: "in_progress", a full hour left + await tick(); + expect(closedBanners().length).toBeGreaterThan(0); + expect(choice("Paris").disabled).toBe(true); + expect(badge("Submitted")).toBe(true); + expect(badge("00:00:00")).toBe(true); // the countdown does not come back either +}); + +test("a state frame claiming in_progress cannot reopen an expired sheet", async () => { + await openWritableRoom(); + socket().deliver({ type: "expired" }); + await tick(); + + socket().deliver(liveState); + await tick(); + expect(closedBanners().length).toBeGreaterThan(0); + expect(choice("Paris").disabled).toBe(true); + expect(badge("Expired")).toBe(true); + expect(badge("00:00:00")).toBe(true); +}); diff --git a/src/components/exams/exam-room-ws.tsx b/src/components/exams/exam-room-ws.tsx index e50a75c..234e67a 100644 --- a/src/components/exams/exam-room-ws.tsx +++ b/src/components/exams/exam-room-ws.tsx @@ -190,12 +190,17 @@ export function ExamRoomWS(props: { exam: Exam }) { // stamp server-side. Keeping the stale value would leave a student who // merely reopened the room reading as "left" — every answer disabled // on a sitting the server would still take. + // Terminal is terminal, the other direction of the same hazard: once + // this sitting is submitted or expired, a frame claiming `in_progress` + // must not reopen it — status and clock stay put, only the harmless + // counters catch up. + const over = attempt()?.status === "submitted" || attempt()?.status === "expired"; setAttempt((prev) => prev - ? { ...prev, status: msg.status as any, left_at: null, deadline: msg.deadline, remaining_ms: msg.remaining_ms, answered: msg.answered, question_count: msg.question_count, now: msg.now } + ? { ...prev, status: over ? prev.status : (msg.status as any), left_at: null, deadline: msg.deadline, remaining_ms: over ? 0 : msg.remaining_ms, answered: msg.answered, question_count: msg.question_count, now: msg.now } : prev, ); - setRemainingMs(msg.remaining_ms); + setRemainingMs(over ? 0 : msg.remaining_ms); break; } case "saved": {