hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import type { Exam, LiveRosterEntry } from "@/api/client"; |
| 2 | import { type ExamDisplayStatus, examDisplayStatus, examStatusMessageKey, liveDisplayStatus } from "./exam-status"; |
| 3 | |
| 4 | const exam = { |
| 5 | id: "exam-1", |
| 6 | course: "course-1", |
| 7 | title: "Retake exam", |
| 8 | description: "", |
| 9 | kind: "quiz", |
| 10 | mode: "sync", |
| 11 | starts_at: 1, |
| 12 | ends_at: 10_000, |
| 13 | duration_ms: 10_000, |
| 14 | max_attempts: 2, |
| 15 | allow_rejoin: false, |
| 16 | draft: false, |
| 17 | creator: "teacher-1", |
| 18 | } satisfies Exam; |
| 19 | |
| 20 | 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"); |
| 22 | }); |
| 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 | |
| 72 | it("maps every exam display status to the intended message key", () => { |
| 73 | const expected: Record<ExamDisplayStatus, string> = { |
| 74 | draft: "exams.draft", |
| 75 | unscheduled: "exams.unscheduled", |
| 76 | upcoming: "exams.upcoming", |
| 77 | active: "exams.active", |
| 78 | finished: "exams.finished", |
| 79 | submitted: "attempt.submitted", |
| 80 | expired: "attempt.expired", |
| 81 | no_attempts_left: "attempt.noAttemptsLeft", |
| 82 | }; |
| 83 | |
| 84 | for (const status of Object.keys(expected) as ExamDisplayStatus[]) { |
| 85 | expect(examStatusMessageKey(status)).toBe(expected[status]); |
| 86 | } |
| 87 | }); |