🐛 keep active students in_progress on the live monitor
Browse files at this commitbc3b5ce216bfb30534210037e594258470f99a48Parent: 9997fe8- fix
- 🐛 keep active students in_progress on the live monitor
3 changed files +78 -16
- src/lib/exam-status.test.ts +50 -2
- src/lib/exam-status.ts +27 -1
- src/pages/live-monitor-page.tsx +1 -13
src/lib/exam-status.test.tsmodified+50 -2
| @@ -1,5 +1,5 @@ | |||
| 1 | -import type { Exam } from "@/api/client"; | 1 | +import type { Exam, LiveRosterEntry } from "@/api/client"; |
| 2 | -import { type ExamDisplayStatus, examDisplayStatus, examStatusMessageKey } from "./exam-status"; | 2 | +import { type ExamDisplayStatus, examDisplayStatus, examStatusMessageKey, liveDisplayStatus } from "./exam-status"; |
| 3 | 3 | ||
| 4 | const exam = { | 4 | const exam = { |
| 5 | id: "exam-1", | 5 | id: "exam-1", |
| @@ -21,6 +21,54 @@ it("shows submitted after a completed attempt even when retakes remain", () => { | |||
| 21 | expect(examDisplayStatus(exam, 5_000, { status: "submitted", attempts_used: 1, max_attempts: 2 })).toBe("submitted"); | 21 | expect(examDisplayStatus(exam, 5_000, { status: "submitted", attempts_used: 1, max_attempts: 2 })).toBe("submitted"); |
| 22 | }); | 22 | }); |
| 23 | 23 | ||
| 24 | +const rosterEntry = (over: Partial<LiveRosterEntry>): LiveRosterEntry => ({ | ||
| 25 | + user: { id: "student-1", username: "veli", name: null, surname: null }, | ||
| 26 | + status: "in_progress", | ||
| 27 | + attempt: 1, | ||
| 28 | + attempts_used: 1, | ||
| 29 | + deadline: 10_000, | ||
| 30 | + remaining_ms: 5_000, | ||
| 31 | + left_at: null, | ||
| 32 | + mark: null, | ||
| 33 | + answered: 0, | ||
| 34 | + started_at: 1, | ||
| 35 | + finished_at: null, | ||
| 36 | + last_activity: null, | ||
| 37 | + ...over, | ||
| 38 | +}); | ||
| 39 | + | ||
| 40 | +describe("liveDisplayStatus", () => { | ||
| 41 | + // Regression: attempts_used counts the running sitting, so a student mid-exam | ||
| 42 | + // on a single-attempt exam must still read as in_progress, never no_attempts_left. | ||
| 43 | + it("keeps an active student on their final attempt in_progress", () => { | ||
| 44 | + const single = { ...exam, max_attempts: 1 } satisfies Exam; | ||
| 45 | + const entry = rosterEntry({ status: "in_progress", attempts_used: 1, left_at: null }); | ||
| 46 | + expect(liveDisplayStatus(entry, single, 5_000)).toBe("in_progress"); | ||
| 47 | + }); | ||
| 48 | + | ||
| 49 | + it("keeps an active student on a multi-attempt exam in_progress", () => { | ||
| 50 | + const entry = rosterEntry({ status: "in_progress", attempts_used: 2, left_at: null }); | ||
| 51 | + expect(liveDisplayStatus(entry, { ...exam, max_attempts: 2 }, 5_000)).toBe("in_progress"); | ||
| 52 | + }); | ||
| 53 | + | ||
| 54 | + it("shows left when a student walks out with retakes remaining", () => { | ||
| 55 | + const entry = rosterEntry({ status: "in_progress", attempts_used: 1, left_at: 4_000 }); | ||
| 56 | + expect(liveDisplayStatus(entry, { ...exam, max_attempts: 2 }, 5_000)).toBe("left"); | ||
| 57 | + }); | ||
| 58 | + | ||
| 59 | + it("shows no_attempts_left when a student walks out of their final attempt", () => { | ||
| 60 | + const entry = rosterEntry({ status: "in_progress", attempts_used: 1, left_at: 4_000 }); | ||
| 61 | + expect(liveDisplayStatus(entry, { ...exam, max_attempts: 1 }, 5_000)).toBe("no_attempts_left"); | ||
| 62 | + }); | ||
| 63 | + | ||
| 64 | + it("passes submitted, expired, absent, and not_started straight through", () => { | ||
| 65 | + expect(liveDisplayStatus(rosterEntry({ status: "submitted" }), exam, 5_000)).toBe("submitted"); | ||
| 66 | + expect(liveDisplayStatus(rosterEntry({ status: "expired" }), exam, 5_000)).toBe("expired"); | ||
| 67 | + expect(liveDisplayStatus(rosterEntry({ status: "absent" }), exam, 5_000)).toBe("absent"); | ||
| 68 | + expect(liveDisplayStatus(rosterEntry({ status: "not_started" }), exam, 5_000)).toBe("not_started"); | ||
| 69 | + }); | ||
| 70 | +}); | ||
| 71 | + | ||
| 24 | it("maps every exam display status to the intended message key", () => { | 72 | it("maps every exam display status to the intended message key", () => { |
| 25 | const expected: Record<ExamDisplayStatus, string> = { | 73 | const expected: Record<ExamDisplayStatus, string> = { |
| 26 | draft: "exams.draft", | 74 | draft: "exams.draft", |
src/lib/exam-status.tsmodified+27 -1
| @@ -1,4 +1,4 @@ | |||
| 1 | -import type { AttemptStatus, Exam } from "@/api/client"; | 1 | +import type { AttemptStatus, Exam, LiveMonitor, LiveRosterEntry } from "@/api/client"; |
| 2 | import type { MessageKey } from "@/i18n/messages"; | 2 | import type { MessageKey } from "@/i18n/messages"; |
| 3 | 3 | ||
| 4 | export type ExamAttemptSummary = { | 4 | export type ExamAttemptSummary = { |
| @@ -30,6 +30,32 @@ export function examDisplayStatus(exam: Exam, now: number, attempt?: ExamAttempt | |||
| 30 | return "active"; | 30 | return "active"; |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | +/** | ||
| 34 | + * A roster entry's status as the teacher's live monitor renders it. `left` is a | ||
| 35 | + * student who walked out of the room mid-attempt (still `in_progress` in the DB, | ||
| 36 | + * but with `left_at` stamped). | ||
| 37 | + */ | ||
| 38 | +export type LiveDisplayStatus = LiveRosterEntry["status"] | ExamDisplayStatus | "left"; | ||
| 39 | + | ||
| 40 | +/** | ||
| 41 | + * The live-monitor display status for one roster entry. A student actively | ||
| 42 | + * sitting the exam (`in_progress`, still in the room) is always `in_progress`, | ||
| 43 | + * even on their final allowed attempt — `attempts_used` counts the running | ||
| 44 | + * sitting, so a `max_attempts >= attempts_used` test would mislabel every | ||
| 45 | + * live single-attempt student as `no_attempts_left`. Attempt exhaustion only | ||
| 46 | + * shows once the sitting is no longer running (submitted, expired, or walked | ||
| 47 | + * out), which `examDisplayStatus` and the `left` branch below handle. | ||
| 48 | + */ | ||
| 49 | +export function liveDisplayStatus(entry: LiveRosterEntry, exam: LiveMonitor["exam"], now: number): LiveDisplayStatus { | ||
| 50 | + if (entry.status === "absent") return "absent"; | ||
| 51 | + if (entry.status === "not_started") return "not_started"; | ||
| 52 | + if (entry.status === "in_progress" && entry.left_at != null) { | ||
| 53 | + return exam.max_attempts > 0 && entry.attempts_used >= exam.max_attempts ? "no_attempts_left" : "left"; | ||
| 54 | + } | ||
| 55 | + const status = examDisplayStatus(exam, now, { status: entry.status, attempts_used: entry.attempts_used, max_attempts: exam.max_attempts }); | ||
| 56 | + return status === "active" ? "in_progress" : status; | ||
| 57 | +} | ||
| 58 | + | ||
| 33 | export function examStatusTone(status: ExamDisplayStatus): string { | 59 | export function examStatusTone(status: ExamDisplayStatus): string { |
| 34 | if (status === "submitted") return "submitted"; | 60 | if (status === "submitted") return "submitted"; |
| 35 | if (status === "expired" || status === "no_attempts_left") return "finished"; | 61 | if (status === "expired" || status === "no_attempts_left") return "finished"; |
src/pages/live-monitor-page.tsxmodified+1 -13
| @@ -19,7 +19,7 @@ import { usePreferences, useT } from "@/stores/preferences-context"; | |||
| 19 | import { createNow } from "@/lib/create-now"; | 19 | import { createNow } from "@/lib/create-now"; |
| 20 | import { cn } from "@/lib/cn"; | 20 | import { cn } from "@/lib/cn"; |
| 21 | import { attemptLabel } from "@/lib/exam-labels"; | 21 | import { attemptLabel } from "@/lib/exam-labels"; |
| 22 | -import { examDisplayStatus, examStatusTone, type ExamDisplayStatus } from "@/lib/exam-status"; | 22 | +import { examStatusTone, liveDisplayStatus, type LiveDisplayStatus } from "@/lib/exam-status"; |
| 23 | import { formatDateTime } from "@/lib/format"; | 23 | import { formatDateTime } from "@/lib/format"; |
| 24 | import { scheduleStatusClass, scheduleStatusDotClass } from "@/lib/schedule-status"; | 24 | import { scheduleStatusClass, scheduleStatusDotClass } from "@/lib/schedule-status"; |
| 25 | 25 | ||
| @@ -43,7 +43,6 @@ const STATUS_KEY: Record<string, MessageKey> = { | |||
| 43 | no_attempts_left: "attempt.noAttemptsLeft", | 43 | no_attempts_left: "attempt.noAttemptsLeft", |
| 44 | }; | 44 | }; |
| 45 | 45 | ||
| 46 | -type LiveDisplayStatus = LiveRosterEntry["status"] | ExamDisplayStatus | "left"; | ||
| 47 | type LiveRosterRow = LiveRosterEntry & { displayStatus: LiveDisplayStatus }; | 46 | type LiveRosterRow = LiveRosterEntry & { displayStatus: LiveDisplayStatus }; |
| 48 | 47 | ||
| 49 | function labelFromStatus(status: string, t: (key: MessageKey) => string): string { | 48 | function labelFromStatus(status: string, t: (key: MessageKey) => string): string { |
| @@ -65,17 +64,6 @@ function liveTone(status: LiveDisplayStatus): string { | |||
| 65 | return examStatusTone(status); | 64 | return examStatusTone(status); |
| 66 | } | 65 | } |
| 67 | 66 | ||
| 68 | -function liveDisplayStatus(entry: LiveRosterEntry, exam: LiveMonitor["exam"], now: number): LiveDisplayStatus { | ||
| 69 | - if (entry.status === "absent") return "absent"; | ||
| 70 | - if (entry.status === "not_started") return "not_started"; | ||
| 71 | - if (entry.status === "in_progress" && entry.left_at != null) { | ||
| 72 | - return exam.max_attempts > 0 && entry.attempts_used >= exam.max_attempts ? "no_attempts_left" : "left"; | ||
| 73 | - } | ||
| 74 | - if (entry.status !== "submitted" && entry.status !== "expired" && exam.max_attempts > 0 && entry.attempts_used >= exam.max_attempts) return "no_attempts_left"; | ||
| 75 | - const status = examDisplayStatus(exam, now, { status: entry.status, attempts_used: entry.attempts_used, max_attempts: exam.max_attempts }); | ||
| 76 | - return status === "active" ? "in_progress" : status; | ||
| 77 | -} | ||
| 78 | - | ||
| 79 | function progressPercent(entry: LiveRosterEntry, questionCount: number): number { | 67 | function progressPercent(entry: LiveRosterEntry, questionCount: number): number { |
| 80 | if (questionCount <= 0) return 0; | 68 | if (questionCount <= 0) return 0; |
| 81 | return Math.round(((entry.answered ?? 0) / questionCount) * 100); | 69 | return Math.round(((entry.answered ?? 0) / questionCount) * 100); |