From bc3b5ce216bfb30534210037e594258470f99a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 24 Jul 2026 01:06:55 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20keep=20active=20students?= =?UTF-8?q?=20in=5Fprogress=20on=20the=20live=20monitor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The teacher live monitor mislabeled a student actively sitting the exam as `no_attempts_left` whenever `attempts_used >= max_attempts`. Because `attempts_used` counts the running sitting, every in-progress student on a single-attempt exam (the common case) rendered as `no_attempts_left` instead of `in_progress` — the roster looked empty of live activity. Only `in_progress` entries ever reached that branch (absent/not_started return earlier, submitted/expired are guarded), so it was always wrong. The student's own view already guarded `status !== "in_progress"`; the monitor helper did not. Extract `liveDisplayStatus` into lib/exam-status.ts (testable, matches examDisplayStatus), drop the bad branch, and cover it with regression tests: active-final-attempt stays in_progress, walkout still resolves to left / no_attempts_left, terminal statuses pass through. --- src/lib/exam-status.test.ts | 52 +++++++++++++++++++++++++++++++-- src/lib/exam-status.ts | 28 +++++++++++++++++- src/pages/live-monitor-page.tsx | 14 +-------- 3 files changed, 78 insertions(+), 16 deletions(-) diff --git a/src/lib/exam-status.test.ts b/src/lib/exam-status.test.ts index dbc34e4..00fc4f3 100644 --- a/src/lib/exam-status.test.ts +++ b/src/lib/exam-status.test.ts @@ -1,5 +1,5 @@ -import type { Exam } from "@/api/client"; -import { type ExamDisplayStatus, examDisplayStatus, examStatusMessageKey } from "./exam-status"; +import type { Exam, LiveRosterEntry } from "@/api/client"; +import { type ExamDisplayStatus, examDisplayStatus, examStatusMessageKey, liveDisplayStatus } from "./exam-status"; const exam = { id: "exam-1", @@ -21,6 +21,54 @@ it("shows submitted after a completed attempt even when retakes remain", () => { expect(examDisplayStatus(exam, 5_000, { status: "submitted", attempts_used: 1, max_attempts: 2 })).toBe("submitted"); }); +const rosterEntry = (over: Partial): LiveRosterEntry => ({ + user: { id: "student-1", username: "veli", name: null, surname: null }, + status: "in_progress", + attempt: 1, + attempts_used: 1, + deadline: 10_000, + remaining_ms: 5_000, + left_at: null, + mark: null, + answered: 0, + started_at: 1, + finished_at: null, + last_activity: null, + ...over, +}); + +describe("liveDisplayStatus", () => { + // Regression: attempts_used counts the running sitting, so a student mid-exam + // on a single-attempt exam must still read as in_progress, never no_attempts_left. + it("keeps an active student on their final attempt in_progress", () => { + const single = { ...exam, max_attempts: 1 } satisfies Exam; + const entry = rosterEntry({ status: "in_progress", attempts_used: 1, left_at: null }); + expect(liveDisplayStatus(entry, single, 5_000)).toBe("in_progress"); + }); + + it("keeps an active student on a multi-attempt exam in_progress", () => { + const entry = rosterEntry({ status: "in_progress", attempts_used: 2, left_at: null }); + expect(liveDisplayStatus(entry, { ...exam, max_attempts: 2 }, 5_000)).toBe("in_progress"); + }); + + it("shows left when a student walks out with retakes remaining", () => { + const entry = rosterEntry({ status: "in_progress", attempts_used: 1, left_at: 4_000 }); + expect(liveDisplayStatus(entry, { ...exam, max_attempts: 2 }, 5_000)).toBe("left"); + }); + + it("shows no_attempts_left when a student walks out of their final attempt", () => { + const entry = rosterEntry({ status: "in_progress", attempts_used: 1, left_at: 4_000 }); + expect(liveDisplayStatus(entry, { ...exam, max_attempts: 1 }, 5_000)).toBe("no_attempts_left"); + }); + + it("passes submitted, expired, absent, and not_started straight through", () => { + expect(liveDisplayStatus(rosterEntry({ status: "submitted" }), exam, 5_000)).toBe("submitted"); + expect(liveDisplayStatus(rosterEntry({ status: "expired" }), exam, 5_000)).toBe("expired"); + expect(liveDisplayStatus(rosterEntry({ status: "absent" }), exam, 5_000)).toBe("absent"); + expect(liveDisplayStatus(rosterEntry({ status: "not_started" }), exam, 5_000)).toBe("not_started"); + }); +}); + it("maps every exam display status to the intended message key", () => { const expected: Record = { draft: "exams.draft", diff --git a/src/lib/exam-status.ts b/src/lib/exam-status.ts index a7b0b26..4abc939 100644 --- a/src/lib/exam-status.ts +++ b/src/lib/exam-status.ts @@ -1,4 +1,4 @@ -import type { AttemptStatus, Exam } from "@/api/client"; +import type { AttemptStatus, Exam, LiveMonitor, LiveRosterEntry } from "@/api/client"; import type { MessageKey } from "@/i18n/messages"; export type ExamAttemptSummary = { @@ -30,6 +30,32 @@ export function examDisplayStatus(exam: Exam, now: number, attempt?: ExamAttempt return "active"; } +/** + * A roster entry's status as the teacher's live monitor renders it. `left` is a + * student who walked out of the room mid-attempt (still `in_progress` in the DB, + * but with `left_at` stamped). + */ +export type LiveDisplayStatus = LiveRosterEntry["status"] | ExamDisplayStatus | "left"; + +/** + * The live-monitor display status for one roster entry. A student actively + * sitting the exam (`in_progress`, still in the room) is always `in_progress`, + * even on their final allowed attempt — `attempts_used` counts the running + * sitting, so a `max_attempts >= attempts_used` test would mislabel every + * live single-attempt student as `no_attempts_left`. Attempt exhaustion only + * shows once the sitting is no longer running (submitted, expired, or walked + * out), which `examDisplayStatus` and the `left` branch below handle. + */ +export function liveDisplayStatus(entry: LiveRosterEntry, exam: LiveMonitor["exam"], now: number): LiveDisplayStatus { + if (entry.status === "absent") return "absent"; + if (entry.status === "not_started") return "not_started"; + if (entry.status === "in_progress" && entry.left_at != null) { + return exam.max_attempts > 0 && entry.attempts_used >= exam.max_attempts ? "no_attempts_left" : "left"; + } + const status = examDisplayStatus(exam, now, { status: entry.status, attempts_used: entry.attempts_used, max_attempts: exam.max_attempts }); + return status === "active" ? "in_progress" : status; +} + export function examStatusTone(status: ExamDisplayStatus): string { if (status === "submitted") return "submitted"; if (status === "expired" || status === "no_attempts_left") return "finished"; diff --git a/src/pages/live-monitor-page.tsx b/src/pages/live-monitor-page.tsx index 1c0a4f6..1e827e6 100644 --- a/src/pages/live-monitor-page.tsx +++ b/src/pages/live-monitor-page.tsx @@ -19,7 +19,7 @@ import { usePreferences, useT } from "@/stores/preferences-context"; import { createNow } from "@/lib/create-now"; import { cn } from "@/lib/cn"; import { attemptLabel } from "@/lib/exam-labels"; -import { examDisplayStatus, examStatusTone, type ExamDisplayStatus } from "@/lib/exam-status"; +import { examStatusTone, liveDisplayStatus, type LiveDisplayStatus } from "@/lib/exam-status"; import { formatDateTime } from "@/lib/format"; import { scheduleStatusClass, scheduleStatusDotClass } from "@/lib/schedule-status"; @@ -43,7 +43,6 @@ const STATUS_KEY: Record = { no_attempts_left: "attempt.noAttemptsLeft", }; -type LiveDisplayStatus = LiveRosterEntry["status"] | ExamDisplayStatus | "left"; type LiveRosterRow = LiveRosterEntry & { displayStatus: LiveDisplayStatus }; function labelFromStatus(status: string, t: (key: MessageKey) => string): string { @@ -65,17 +64,6 @@ function liveTone(status: LiveDisplayStatus): string { return examStatusTone(status); } -function liveDisplayStatus(entry: LiveRosterEntry, exam: LiveMonitor["exam"], now: number): LiveDisplayStatus { - if (entry.status === "absent") return "absent"; - if (entry.status === "not_started") return "not_started"; - if (entry.status === "in_progress" && entry.left_at != null) { - return exam.max_attempts > 0 && entry.attempts_used >= exam.max_attempts ? "no_attempts_left" : "left"; - } - if (entry.status !== "submitted" && entry.status !== "expired" && exam.max_attempts > 0 && entry.attempts_used >= exam.max_attempts) return "no_attempts_left"; - const status = examDisplayStatus(exam, now, { status: entry.status, attempts_used: entry.attempts_used, max_attempts: exam.max_attempts }); - return status === "active" ? "in_progress" : status; -} - function progressPercent(entry: LiveRosterEntry, questionCount: number): number { if (questionCount <= 0) return 0; return Math.round(((entry.answered ?? 0) / questionCount) * 100);