π nginx API whitelist, answer-sheet join, prefs clear
Browse files at this commit8aa8ff23dc1de04f2c3e552958f0a575b9d43e64Parent: e00544e- fix
- π nginx API whitelist, answer-sheet join, prefs clear
- contract tests
21 changed files +216 -83
- nginx.conf +1 -1
- nginx.conf.test.ts +33 -0
- src/api/patchCourseById.ts +4 -3
- src/api/patchEventById.ts +4 -4
- src/api/patchNoteById.ts +3 -2
- src/api/patchSessionById.ts +4 -3
- src/api/patchUserPreferences.ts +4 -2
- src/api/patchWorkEntryById.ts +3 -2
- src/components/exams/answer-sheet-view.tsx +26 -53
- src/components/users/user-preferences-actions.tsx +3 -2
- src/components/users/user-table.tsx +1 -1
- src/lib/answer-sheet.test.ts +58 -0
- src/lib/answer-sheet.ts +16 -0
- src/lib/exam-labels.test.ts +14 -0
- src/lib/exam-labels.ts +14 -0
- src/lib/exam-weight.test.ts +20 -0
- src/pages/admin-users-page.tsx +1 -1
- src/pages/course-detail-page.tsx +2 -0
- src/pages/live-monitor-page.tsx +1 -7
- tsconfig.app.json +2 -1
- tsconfig.json +2 -1
nginx.confmodified+1 -1
| @@ -3,7 +3,7 @@ server { | |||
| 3 | root /usr/share/nginx/html; | 3 | root /usr/share/nginx/html; |
| 4 | index index.html; | 4 | index index.html; |
| 5 | 5 | ||
| 6 | - location ~ ^/api/(auth|users|notes|events|exams|courses|marks|health|time)(/|$) { | 6 | + location ~ ^/api/(auth|users|notes|events|exams|courses|sessions|marks|attendance|work|pomodoro|settings|subjects|terms|health|time)(/|$) { |
| 7 | rewrite ^/api(/.*)$ $1 break; | 7 | rewrite ^/api(/.*)$ $1 break; |
| 8 | proxy_pass http://hezarfen-backend:8080; | 8 | proxy_pass http://hezarfen-backend:8080; |
| 9 | proxy_http_version 1.1; | 9 | proxy_http_version 1.1; |
nginx.conf.test.tsadded+33 -0
| @@ -0,0 +1,33 @@ | |||
| 1 | +import { expect, test } from "bun:test"; | ||
| 2 | +import { readFileSync } from "node:fs"; | ||
| 3 | + | ||
| 4 | +// Mirror of backend route mounts (hezarfen_backend src/lib.rs .nest calls + /health, /time). | ||
| 5 | +// Backend adds a prefix β add it here AND in nginx.conf's proxy location. | ||
| 6 | +const BACKEND_PREFIXES = [ | ||
| 7 | + "auth", | ||
| 8 | + "users", | ||
| 9 | + "notes", | ||
| 10 | + "events", | ||
| 11 | + "courses", | ||
| 12 | + "sessions", | ||
| 13 | + "exams", | ||
| 14 | + "marks", | ||
| 15 | + "work", | ||
| 16 | + "pomodoro", | ||
| 17 | + "attendance", | ||
| 18 | + "settings", | ||
| 19 | + "subjects", | ||
| 20 | + "terms", | ||
| 21 | + "health", | ||
| 22 | + "time", | ||
| 23 | +]; | ||
| 24 | + | ||
| 25 | +test("nginx proxy whitelist covers every backend route prefix", () => { | ||
| 26 | + const conf = readFileSync(new URL("./nginx.conf", import.meta.url), "utf8"); | ||
| 27 | + const match = conf.match(/\^\/api\/\(([^)]+)\)/); | ||
| 28 | + expect(match).not.toBeNull(); | ||
| 29 | + const listed = match![1].split("|"); | ||
| 30 | + for (const prefix of BACKEND_PREFIXES) { | ||
| 31 | + expect(listed).toContain(prefix); | ||
| 32 | + } | ||
| 33 | +}); | ||
src/api/patchCourseById.tsmodified+4 -3
| @@ -1,10 +1,11 @@ | |||
| 1 | import { client } from "./client"; | 1 | import { client } from "./client"; |
| 2 | import type { Course, CourseKind } from "./types"; | 2 | import type { Course, CourseKind } from "./types"; |
| 3 | 3 | ||
| 4 | +/** Omitted fields keep their value. Only `term_id`/`capacity` clear on explicit null. */ | ||
| 4 | export type PatchCourseBody = { | 5 | export type PatchCourseBody = { |
| 5 | - title?: string | null; | 6 | + title?: string; |
| 6 | - description?: string | null; | 7 | + description?: string; |
| 7 | - kind?: CourseKind | null; | 8 | + kind?: CourseKind; |
| 8 | term_id?: string | null; | 9 | term_id?: string | null; |
| 9 | capacity?: number | null; | 10 | capacity?: number | null; |
| 10 | }; | 11 | }; |
src/api/patchEventById.tsmodified+4 -4
| @@ -1,11 +1,11 @@ | |||
| 1 | import { client } from "./client"; | 1 | import { client } from "./client"; |
| 2 | import type { Event, EventAudience } from "./types"; | 2 | import type { Event, EventAudience } from "./types"; |
| 3 | 3 | ||
| 4 | -/** Omitted time fields keep their value; explicit null clears them. */ | 4 | +/** Omitted fields keep their value. Only the time fields clear on explicit null. */ |
| 5 | export type PatchEventBody = { | 5 | export type PatchEventBody = { |
| 6 | - title?: string | null; | 6 | + title?: string; |
| 7 | - description?: string | null; | 7 | + description?: string; |
| 8 | - audience?: EventAudience | null; | 8 | + audience?: EventAudience; |
| 9 | starts_at?: number | null; | 9 | starts_at?: number | null; |
| 10 | ends_at?: number | null; | 10 | ends_at?: number | null; |
| 11 | }; | 11 | }; |
src/api/patchNoteById.tsmodified+3 -2
| @@ -1,9 +1,10 @@ | |||
| 1 | import { client } from "./client"; | 1 | import { client } from "./client"; |
| 2 | import type { Note } from "./types"; | 2 | import type { Note } from "./types"; |
| 3 | 3 | ||
| 4 | +/** Omitted fields keep their value; null never clears here. */ | ||
| 4 | export type PatchNoteBody = { | 5 | export type PatchNoteBody = { |
| 5 | - title?: string | null; | 6 | + title?: string; |
| 6 | - content?: string | null; | 7 | + content?: string; |
| 7 | }; | 8 | }; |
| 8 | 9 | ||
| 9 | export function patchNoteById(id: string, body: PatchNoteBody): Promise<Note> { | 10 | export function patchNoteById(id: string, body: PatchNoteBody): Promise<Note> { |
src/api/patchSessionById.tsmodified+4 -3
| @@ -1,10 +1,11 @@ | |||
| 1 | import { client } from "./client"; | 1 | import { client } from "./client"; |
| 2 | import type { CourseSession } from "./types"; | 2 | import type { CourseSession } from "./types"; |
| 3 | 3 | ||
| 4 | +/** Omitted fields keep their value. Only `ends_at` clears on explicit null. */ | ||
| 4 | export type PatchSessionBody = { | 5 | export type PatchSessionBody = { |
| 5 | - topic?: string | null; | 6 | + topic?: string; |
| 6 | - teacher_id?: string | null; | 7 | + teacher_id?: string; |
| 7 | - starts_at?: number | null; | 8 | + starts_at?: number; |
| 8 | ends_at?: number | null; | 9 | ends_at?: number | null; |
| 9 | }; | 10 | }; |
| 10 | 11 | ||
src/api/patchUserPreferences.tsmodified+4 -2
| @@ -1,9 +1,11 @@ | |||
| 1 | import { client } from "./client"; | 1 | import { client } from "./client"; |
| 2 | import type { User, UserLanguage, UserTheme } from "./types"; | 2 | import type { User, UserLanguage, UserTheme } from "./types"; |
| 3 | 3 | ||
| 4 | +/** Omit to keep; empty string clears back to the device default. Null also | ||
| 5 | + * means keep on the backend, so it is deliberately not allowed here. */ | ||
| 4 | export type PatchUserPreferencesBody = { | 6 | export type PatchUserPreferencesBody = { |
| 5 | - theme?: UserTheme | null; | 7 | + theme?: UserTheme | ""; |
| 6 | - language?: UserLanguage | null; | 8 | + language?: UserLanguage | ""; |
| 7 | }; | 9 | }; |
| 8 | 10 | ||
| 9 | export function patchUserPreferences(id: string, body: PatchUserPreferencesBody): Promise<User> { | 11 | export function patchUserPreferences(id: string, body: PatchUserPreferencesBody): Promise<User> { |
src/api/patchWorkEntryById.tsmodified+3 -2
| @@ -1,9 +1,10 @@ | |||
| 1 | import { client } from "./client"; | 1 | import { client } from "./client"; |
| 2 | import type { WorkEntry } from "./types"; | 2 | import type { WorkEntry } from "./types"; |
| 3 | 3 | ||
| 4 | +/** Omitted fields keep their value; null never clears here. */ | ||
| 4 | export type PatchWorkEntryBody = { | 5 | export type PatchWorkEntryBody = { |
| 5 | - check_in?: number | null; | 6 | + check_in?: number; |
| 6 | - check_out?: number | null; | 7 | + check_out?: number; |
| 7 | }; | 8 | }; |
| 8 | 9 | ||
| 9 | export function patchWorkEntryById(id: string, body: PatchWorkEntryBody): Promise<WorkEntry> { | 10 | export function patchWorkEntryById(id: string, body: PatchWorkEntryBody): Promise<WorkEntry> { |
src/components/exams/answer-sheet-view.tsxmodified+26 -53
| @@ -1,90 +1,80 @@ | |||
| 1 | import { For, Show, Suspense, createResource } from "solid-js"; | 1 | import { For, Show, Suspense, createResource } from "solid-js"; |
| 2 | import { getExamQuestions } from "@/api/getExamQuestions"; | 2 | import { getExamQuestions } from "@/api/getExamQuestions"; |
| 3 | import { getStudentAnswers } from "@/api/getStudentAnswers"; | 3 | import { getStudentAnswers } from "@/api/getStudentAnswers"; |
| 4 | -import type { ExamQuestion, StudentAnswer } from "@/api/types"; | ||
| 5 | import { Badge } from "@/components/ui/badge"; | 4 | import { Badge } from "@/components/ui/badge"; |
| 6 | import { PageSpinner } from "@/components/ui/page-spinner"; | 5 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 6 | +import { joinAnswerSheet } from "@/lib/answer-sheet"; | ||
| 7 | import { personLabelWithId } from "@/lib/person"; | 7 | import { personLabelWithId } from "@/lib/person"; |
| 8 | import { useT } from "@/stores/preferences-context"; | 8 | import { useT } from "@/stores/preferences-context"; |
| 9 | 9 | ||
| 10 | export function AnswerSheetView(props: { examId: string; userId: string }) { | 10 | export function AnswerSheetView(props: { examId: string; userId: string }) { |
| 11 | const t = useT(); | 11 | const t = useT(); |
| 12 | - const [sheet] = createResource( | 12 | + const [data] = createResource( |
| 13 | () => [props.examId, props.userId] as const, | 13 | () => [props.examId, props.userId] as const, |
| 14 | async ([examId, userId]) => { | 14 | async ([examId, userId]) => { |
| 15 | - const [answers, questions] = await Promise.all([getStudentAnswers(examId, userId), getExamQuestions(examId)]); | 15 | + const [questions, sheet] = await Promise.all([getExamQuestions(examId), getStudentAnswers(examId, userId)]); |
| 16 | - const byId = new Map(questions.items.map((question) => [question.id, question])); | 16 | + return { sheet, rows: joinAnswerSheet(questions.items, sheet.answers) }; |
| 17 | - return { | ||
| 18 | - ...answers, | ||
| 19 | - rows: answers.answers.flatMap((answer) => { | ||
| 20 | - const question = byId.get(answer.question); | ||
| 21 | - return question ? [answerRow(answer, question)] : []; | ||
| 22 | - }), | ||
| 23 | - }; | ||
| 24 | }, | 17 | }, |
| 25 | ); | 18 | ); |
| 26 | 19 | ||
| 27 | return ( | 20 | return ( |
| 28 | <Suspense fallback={<PageSpinner />}> | 21 | <Suspense fallback={<PageSpinner />}> |
| 29 | - <Show when={sheet()}> | 22 | + <Show when={data()}> |
| 30 | - {(s) => ( | 23 | + {(d) => ( |
| 31 | <div class="space-y-4"> | 24 | <div class="space-y-4"> |
| 32 | <div class="flex flex-wrap items-center justify-between gap-2 rounded-md border bg-muted/30 px-4 py-3"> | 25 | <div class="flex flex-wrap items-center justify-between gap-2 rounded-md border bg-muted/30 px-4 py-3"> |
| 33 | - <p class="text-sm font-medium">{personLabelWithId(s().user)}</p> | 26 | + <p class="text-sm font-medium">{personLabelWithId(d().sheet.user)}</p> |
| 34 | <Badge variant="secondary"> | 27 | <Badge variant="secondary"> |
| 35 | - {t("exams.autoScore")}: {s().auto_score.earned}/{s().auto_score.possible} | 28 | + {t("exams.autoScore")}: {d().sheet.auto_score.earned}/{d().sheet.auto_score.possible} |
| 36 | </Badge> | 29 | </Badge> |
| 37 | </div> | 30 | </div> |
| 38 | 31 | ||
| 39 | - <For each={s().rows}> | 32 | + <For each={d().rows}> |
| 40 | - {(answer, idx) => ( | 33 | + {(row, idx) => ( |
| 41 | <div class="rounded-md border p-4"> | 34 | <div class="rounded-md border p-4"> |
| 42 | <div class="mb-2 flex flex-wrap items-center gap-2"> | 35 | <div class="mb-2 flex flex-wrap items-center gap-2"> |
| 43 | <span class="text-xs font-semibold text-muted-foreground">#{idx() + 1}</span> | 36 | <span class="text-xs font-semibold text-muted-foreground">#{idx() + 1}</span> |
| 44 | - <Badge variant="outline">{answer.points} {t("questions.points")}</Badge> | 37 | + <Badge variant="outline">{row.question.points} {t("questions.points")}</Badge> |
| 45 | - <Badge variant={answer.is_correct === true ? "default" : answer.is_correct === false ? "destructive" : "outline"}> | 38 | + <Badge variant={row.answer?.is_correct === true ? "default" : row.answer?.is_correct === false ? "destructive" : "outline"}> |
| 46 | - {answer.is_correct === true ? t("exams.isCorrect") : | 39 | + {row.answer?.is_correct === true ? t("exams.isCorrect") : |
| 47 | - answer.is_correct === false ? "β" : | 40 | + row.answer?.is_correct === false ? "β" : |
| 48 | - answer.kind === "text" ? t("questions.kind.text") : "β"} | 41 | + row.question.kind === "text" ? t("questions.kind.text") : "β"} |
| 49 | </Badge> | 42 | </Badge> |
| 50 | - <Badge variant="outline">{t("exams.autoScore")}: {answer.auto_score.earned}/{answer.auto_score.possible}</Badge> | ||
| 51 | </div> | 43 | </div> |
| 52 | - <p class="mb-3 whitespace-pre-wrap text-sm font-medium">{answer.text}</p> | 44 | + <p class="mb-3 whitespace-pre-wrap text-sm font-medium">{row.question.text}</p> |
| 53 | 45 | ||
| 54 | <Show | 46 | <Show |
| 55 | - when={answer.kind === "choice"} | 47 | + when={row.question.kind === "choice"} |
| 56 | fallback={ | 48 | fallback={ |
| 57 | <div class="rounded-sm bg-muted/40 p-3"> | 49 | <div class="rounded-sm bg-muted/40 p-3"> |
| 58 | <p class="text-xs text-muted-foreground">{t("exams.textAnswer")}</p> | 50 | <p class="text-xs text-muted-foreground">{t("exams.textAnswer")}</p> |
| 59 | - <p class="mt-1 whitespace-pre-wrap text-sm">{answer.text_answer || "β"}</p> | 51 | + <p class="mt-1 whitespace-pre-wrap text-sm">{row.answer?.text || "β"}</p> |
| 60 | </div> | 52 | </div> |
| 61 | } | 53 | } |
| 62 | > | 54 | > |
| 63 | <div class="space-y-1.5"> | 55 | <div class="space-y-1.5"> |
| 64 | - <For each={answer.choices}> | 56 | + <For each={row.question.choices}> |
| 65 | {(choice, ci) => ( | 57 | {(choice, ci) => ( |
| 66 | <div | 58 | <div |
| 67 | class={`flex items-center gap-2 rounded-sm border px-3 py-2 text-sm ${ | 59 | class={`flex items-center gap-2 rounded-sm border px-3 py-2 text-sm ${ |
| 68 | - ci() === answer.correct && ci() === answer.selected | 60 | + ci() === row.question.correct && ci() === row.answer?.selected |
| 69 | ? "border-green-500 bg-green-50 dark:bg-green-950/30" | 61 | ? "border-green-500 bg-green-50 dark:bg-green-950/30" |
| 70 | - : ci() === answer.correct | 62 | + : ci() === row.question.correct |
| 71 | ? "border-green-300 bg-green-50/50 dark:bg-green-950/20" | 63 | ? "border-green-300 bg-green-50/50 dark:bg-green-950/20" |
| 72 | - : ci() === answer.selected | 64 | + : ci() === row.answer?.selected |
| 73 | ? "border-destructive bg-destructive/10" | 65 | ? "border-destructive bg-destructive/10" |
| 74 | : "border-border" | 66 | : "border-border" |
| 75 | }`} | 67 | }`} |
| 76 | > | 68 | > |
| 77 | <span class="flex h-5 w-5 shrink-0 items-center justify-center rounded-[3px] border text-[11px] font-medium"> | 69 | <span class="flex h-5 w-5 shrink-0 items-center justify-center rounded-[3px] border text-[11px] font-medium"> |
| 78 | - {ci() === answer.correct || ci() === answer.selected | 70 | + {ci() === row.question.correct || ci() === row.answer?.selected |
| 79 | - ? ci() === answer.correct && ci() === answer.selected | 71 | + ? ci() === row.question.correct |
| 80 | ? "β" | 72 | ? "β" |
| 81 | - : ci() === answer.correct | 73 | + : "β" |
| 82 | - ? "β" | ||
| 83 | - : "β" | ||
| 84 | : String.fromCharCode(65 + ci())} | 74 | : String.fromCharCode(65 + ci())} |
| 85 | </span> | 75 | </span> |
| 86 | <span>{choice}</span> | 76 | <span>{choice}</span> |
| 87 | - {ci() === answer.correct && <Badge variant="outline" class="ml-auto text-[10px]">{t("questions.correct")}</Badge>} | 77 | + {ci() === row.question.correct && <Badge variant="outline" class="ml-auto text-[10px]">{t("questions.correct")}</Badge>} |
| 88 | </div> | 78 | </div> |
| 89 | )} | 79 | )} |
| 90 | </For> | 80 | </For> |
| @@ -99,20 +89,3 @@ export function AnswerSheetView(props: { examId: string; userId: string }) { | |||
| 99 | </Suspense> | 89 | </Suspense> |
| 100 | ); | 90 | ); |
| 101 | } | 91 | } |
| 102 | - | ||
| 103 | -function answerRow(answer: StudentAnswer, question: ExamQuestion) { | ||
| 104 | - const isChoice = question.kind === "choice"; | ||
| 105 | - const possible = isChoice ? question.points : 0; | ||
| 106 | - return { | ||
| 107 | - question_id: answer.question, | ||
| 108 | - text: question.text, | ||
| 109 | - kind: question.kind, | ||
| 110 | - points: question.points, | ||
| 111 | - choices: question.choices, | ||
| 112 | - correct: question.correct, | ||
| 113 | - selected: answer.selected, | ||
| 114 | - text_answer: answer.text, | ||
| 115 | - is_correct: answer.is_correct, | ||
| 116 | - auto_score: { earned: answer.is_correct ? question.points : 0, possible }, | ||
| 117 | - }; | ||
| 118 | -} | ||
src/components/users/user-preferences-actions.tsxmodified+3 -2
| @@ -7,7 +7,7 @@ import { useT } from "@/stores/preferences-context"; | |||
| 7 | 7 | ||
| 8 | export function UserPreferencesActions(props: { | 8 | export function UserPreferencesActions(props: { |
| 9 | user: User; | 9 | user: User; |
| 10 | - onPreferencesChange: (userId: string, body: { theme?: UserTheme | null; language?: UserLanguage | null }) => Promise<void>; | 10 | + onPreferencesChange: (userId: string, body: { theme?: UserTheme | ""; language?: UserLanguage | "" }) => Promise<void>; |
| 11 | }) { | 11 | }) { |
| 12 | const t = useT(); | 12 | const t = useT(); |
| 13 | const [theme, setTheme] = createSignal<UserTheme | "">(props.user.theme ?? ""); | 13 | const [theme, setTheme] = createSignal<UserTheme | "">(props.user.theme ?? ""); |
| @@ -29,7 +29,8 @@ export function UserPreferencesActions(props: { | |||
| 29 | </Select> | 29 | </Select> |
| 30 | </div> | 30 | </div> |
| 31 | <Show when={dirty()}> | 31 | <Show when={dirty()}> |
| 32 | - <Button type="button" size="sm" class="h-7 rounded-sm px-2" onClick={() => props.onPreferencesChange(props.user.id, { theme: theme() || null, language: language() || null })}> | 32 | + {/* "" clears the preference on the backend; null would silently keep it. */} |
| 33 | + <Button type="button" size="sm" class="h-7 rounded-sm px-2" onClick={() => props.onPreferencesChange(props.user.id, { theme: theme(), language: language() })}> | ||
| 33 | <IconCheck /> | 34 | <IconCheck /> |
| 34 | {t("common.save")} | 35 | {t("common.save")} |
| 35 | </Button> | 36 | </Button> |
src/components/users/user-table.tsxmodified+1 -1
| @@ -75,7 +75,7 @@ export function UserTable(props: { | |||
| 75 | users: User[]; | 75 | users: User[]; |
| 76 | currentUserId: string; | 76 | currentUserId: string; |
| 77 | onRoleChange: (userId: string, role: Role) => Promise<void>; | 77 | onRoleChange: (userId: string, role: Role) => Promise<void>; |
| 78 | - onPreferencesChange?: (userId: string, body: { theme?: UserTheme | null; language?: UserLanguage | null }) => Promise<void>; | 78 | + onPreferencesChange?: (userId: string, body: { theme?: UserTheme | ""; language?: UserLanguage | "" }) => Promise<void>; |
| 79 | }) { | 79 | }) { |
| 80 | const t = useT(); | 80 | const t = useT(); |
| 81 | const searchUser = (user: User, query: string) => | 81 | const searchUser = (user: User, query: string) => |
src/lib/answer-sheet.test.tsadded+58 -0
| @@ -0,0 +1,58 @@ | |||
| 1 | +import { expect, test } from "bun:test"; | ||
| 2 | +import type { ExamQuestion, StudentAnswer } from "@/api/types"; | ||
| 3 | +import { joinAnswerSheet } from "./answer-sheet"; | ||
| 4 | + | ||
| 5 | +function question(id: string, overrides: Partial<ExamQuestion> = {}): ExamQuestion { | ||
| 6 | + return { | ||
| 7 | + id, | ||
| 8 | + exam: "e1", | ||
| 9 | + subject: "s1", | ||
| 10 | + text: `Question ${id}`, | ||
| 11 | + kind: "choice", | ||
| 12 | + points: 10, | ||
| 13 | + choices: ["a", "b"], | ||
| 14 | + correct: 0, | ||
| 15 | + ...overrides, | ||
| 16 | + }; | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +function answer(questionId: string, overrides: Partial<StudentAnswer> = {}): StudentAnswer { | ||
| 20 | + return { | ||
| 21 | + question: questionId, | ||
| 22 | + selected: 0, | ||
| 23 | + text: null, | ||
| 24 | + updated_at: 1, | ||
| 25 | + is_correct: true, | ||
| 26 | + ...overrides, | ||
| 27 | + }; | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +test("pairs each question with its answer by id, keeping question order", () => { | ||
| 31 | + const rows = joinAnswerSheet( | ||
| 32 | + [question("q1"), question("q2")], | ||
| 33 | + [answer("q2", { selected: 1, is_correct: false }), answer("q1")], | ||
| 34 | + ); | ||
| 35 | + expect(rows.map((r) => r.question.id)).toEqual(["q1", "q2"]); | ||
| 36 | + expect(rows[0].answer?.is_correct).toBe(true); | ||
| 37 | + expect(rows[1].answer?.selected).toBe(1); | ||
| 38 | +}); | ||
| 39 | + | ||
| 40 | +test("unanswered question yields a null answer", () => { | ||
| 41 | + const rows = joinAnswerSheet([question("q1")], []); | ||
| 42 | + expect(rows).toHaveLength(1); | ||
| 43 | + expect(rows[0].answer).toBeNull(); | ||
| 44 | +}); | ||
| 45 | + | ||
| 46 | +test("answers for deleted questions are dropped", () => { | ||
| 47 | + const rows = joinAnswerSheet([question("q1")], [answer("gone")]); | ||
| 48 | + expect(rows).toHaveLength(1); | ||
| 49 | + expect(rows[0].answer).toBeNull(); | ||
| 50 | +}); | ||
| 51 | + | ||
| 52 | +test("text answers carry the student's text through", () => { | ||
| 53 | + const rows = joinAnswerSheet( | ||
| 54 | + [question("q1", { kind: "text", choices: null, correct: null })], | ||
| 55 | + [answer("q1", { selected: null, text: "my essay", is_correct: null })], | ||
| 56 | + ); | ||
| 57 | + expect(rows[0].answer?.text).toBe("my essay"); | ||
| 58 | +}); | ||
src/lib/answer-sheet.tsadded+16 -0
| @@ -0,0 +1,16 @@ | |||
| 1 | +import type { ExamQuestion, StudentAnswer } from "@/api/types"; | ||
| 2 | + | ||
| 3 | +export type AnswerSheetRow = { | ||
| 4 | + question: ExamQuestion; | ||
| 5 | + answer: StudentAnswer | null; | ||
| 6 | +}; | ||
| 7 | + | ||
| 8 | +/** Pair the exam's questions with a student's answers (`answer.question` β `question.id`). | ||
| 9 | + * Unanswered questions get `answer: null`; answers to deleted questions are dropped. */ | ||
| 10 | +export function joinAnswerSheet(questions: ExamQuestion[], answers: StudentAnswer[]): AnswerSheetRow[] { | ||
| 11 | + const byQuestion = new Map(answers.map((answer) => [answer.question, answer])); | ||
| 12 | + return questions.map((question) => ({ | ||
| 13 | + question, | ||
| 14 | + answer: byQuestion.get(question.id) ?? null, | ||
| 15 | + })); | ||
| 16 | +} | ||
src/lib/exam-labels.test.tsadded+14 -0
| @@ -0,0 +1,14 @@ | |||
| 1 | +import { expect, test } from "bun:test"; | ||
| 2 | +import { attemptLabel } from "./exam-labels"; | ||
| 3 | + | ||
| 4 | +test("shows used/max when the exam caps attempts", () => { | ||
| 5 | + expect(attemptLabel({ attempts_used: 1, attempt: 1 }, 3)).toBe("1 / 3"); | ||
| 6 | +}); | ||
| 7 | + | ||
| 8 | +test("unlimited attempts (max 0) falls back to the attempt count", () => { | ||
| 9 | + expect(attemptLabel({ attempts_used: 2, attempt: 2 }, 0)).toBe("2"); | ||
| 10 | +}); | ||
| 11 | + | ||
| 12 | +test("no attempt data renders a dash", () => { | ||
| 13 | + expect(attemptLabel({}, 3)).toBe("β"); | ||
| 14 | +}); | ||
src/lib/exam-labels.tsmodified+14 -0
| @@ -15,3 +15,17 @@ const EXAM_KIND_LABELS: Record<KnownExamKind, MessageKey> = { | |||
| 15 | export function examKindLabel(kind: string, t: T): string { | 15 | export function examKindLabel(kind: string, t: T): string { |
| 16 | return kind in EXAM_KIND_LABELS ? t(EXAM_KIND_LABELS[kind as KnownExamKind]) : kind; | 16 | return kind in EXAM_KIND_LABELS ? t(EXAM_KIND_LABELS[kind as KnownExamKind]) : kind; |
| 17 | } | 17 | } |
| 18 | + | ||
| 19 | +/** Attempts cell for the live monitor. `maxAttempts` comes from the monitored | ||
| 20 | + * exam itself (the roster rows don't carry it); `0` means unlimited. */ | ||
| 21 | +export function attemptLabel( | ||
| 22 | + entry: { attempt?: number | null; attempts_used?: number | null }, | ||
| 23 | + maxAttempts: number | null | undefined, | ||
| 24 | +): string { | ||
| 25 | + if (entry.attempts_used != null && maxAttempts != null && maxAttempts > 0) { | ||
| 26 | + return `${entry.attempts_used} / ${maxAttempts}`; | ||
| 27 | + } | ||
| 28 | + if (entry.attempt != null) return String(entry.attempt); | ||
| 29 | + if (entry.attempts_used != null) return String(entry.attempts_used); | ||
| 30 | + return "β"; | ||
| 31 | +} | ||
src/lib/exam-weight.test.tsadded+20 -0
| @@ -0,0 +1,20 @@ | |||
| 1 | +import { expect, test } from "bun:test"; | ||
| 2 | +import { examWeight } from "./exam-weight"; | ||
| 3 | + | ||
| 4 | +const kinds = [ | ||
| 5 | + { name: "quiz", weight: 2 }, | ||
| 6 | + { name: "final", weight: 5 }, | ||
| 7 | +]; | ||
| 8 | + | ||
| 9 | +test("a direct weight (marks report) wins over the settings lookup", () => { | ||
| 10 | + expect(examWeight({ kind: "quiz", weight: 7 }, kinds)).toBe(7); | ||
| 11 | +}); | ||
| 12 | + | ||
| 13 | +test("without a direct weight, the exam kind resolves through settings", () => { | ||
| 14 | + expect(examWeight({ kind: "final" }, kinds)).toBe(5); | ||
| 15 | +}); | ||
| 16 | + | ||
| 17 | +test("unknown kind and no weight yields null", () => { | ||
| 18 | + expect(examWeight({ kind: "custom" }, kinds)).toBeNull(); | ||
| 19 | + expect(examWeight({}, undefined)).toBeNull(); | ||
| 20 | +}); | ||
src/pages/admin-users-page.tsxmodified+1 -1
| @@ -46,7 +46,7 @@ function AdminUsersContent() { | |||
| 46 | } | 46 | } |
| 47 | }; | 47 | }; |
| 48 | 48 | ||
| 49 | - const onPreferencesChange = async (userId: string, body: { theme?: UserTheme | null; language?: UserLanguage | null }) => { | 49 | + const onPreferencesChange = async (userId: string, body: { theme?: UserTheme | ""; language?: UserLanguage | "" }) => { |
| 50 | setError(""); | 50 | setError(""); |
| 51 | try { | 51 | try { |
| 52 | await patchUserPreferences(userId, body); | 52 | await patchUserPreferences(userId, body); |
src/pages/course-detail-page.tsxmodified+2 -0
| @@ -359,6 +359,7 @@ function CourseDetailContent() { | |||
| 359 | id="edit-course-title" | 359 | id="edit-course-title" |
| 360 | value={title()} | 360 | value={title()} |
| 361 | required | 361 | required |
| 362 | + maxlength={200} | ||
| 362 | onInput={(e) => setTitle(e.currentTarget.value)} | 363 | onInput={(e) => setTitle(e.currentTarget.value)} |
| 363 | /> | 364 | /> |
| 364 | </div> | 365 | </div> |
| @@ -368,6 +369,7 @@ function CourseDetailContent() { | |||
| 368 | id="edit-course-desc" | 369 | id="edit-course-desc" |
| 369 | value={description()} | 370 | value={description()} |
| 370 | rows={3} | 371 | rows={3} |
| 372 | + maxlength={2000} | ||
| 371 | onInput={(e) => setDescription(e.currentTarget.value)} | 373 | onInput={(e) => setDescription(e.currentTarget.value)} |
| 372 | /> | 374 | /> |
| 373 | </div> | 375 | </div> |
src/pages/live-monitor-page.tsxmodified+1 -7
| @@ -17,6 +17,7 @@ import { IconChevronLeft } from "@/components/ui/icons"; | |||
| 17 | import { PageSpinner } from "@/components/ui/page-spinner"; | 17 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 18 | import { usePreferences, useT } from "@/stores/preferences-context"; | 18 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 19 | import { createNow } from "@/lib/create-now"; | 19 | import { createNow } from "@/lib/create-now"; |
| 20 | +import { attemptLabel } from "@/lib/exam-labels"; | ||
| 20 | import { formatDateTime } from "@/lib/format"; | 21 | import { formatDateTime } from "@/lib/format"; |
| 21 | 22 | ||
| 22 | const PAGE_SIZE = 10; | 23 | const PAGE_SIZE = 10; |
| @@ -84,13 +85,6 @@ function isLowRemaining(entry: LiveRosterEntry): boolean { | |||
| 84 | return entry.status === "in_progress" && remaining <= 5 * 60 * 1000; | 85 | return entry.status === "in_progress" && remaining <= 5 * 60 * 1000; |
| 85 | } | 86 | } |
| 86 | 87 | ||
| 87 | -function attemptLabel(entry: LiveRosterEntry, maxAttempts: number | null): string { | ||
| 88 | - if (entry.attempts_used != null && maxAttempts != null) return `${entry.attempts_used} / ${maxAttempts}`; | ||
| 89 | - if (entry.attempt != null) return String(entry.attempt); | ||
| 90 | - if (entry.attempts_used != null) return String(entry.attempts_used); | ||
| 91 | - return "β"; | ||
| 92 | -} | ||
| 93 | - | ||
| 94 | function LiveMonitorContent() { | 88 | function LiveMonitorContent() { |
| 95 | const location = useLocation(); | 89 | const location = useLocation(); |
| 96 | const params = useParams({ from: "/exams/$id/live" }); | 90 | const params = useParams({ from: "/exams/$id/live" }); |
tsconfig.app.jsonmodified+2 -1
| @@ -23,5 +23,6 @@ | |||
| 23 | "@/*": ["./src/*"] | 23 | "@/*": ["./src/*"] |
| 24 | } | 24 | } |
| 25 | }, | 25 | }, |
| 26 | - "include": ["src"] | 26 | + "include": ["src"], |
| 27 | + "exclude": ["src/**/*.test.ts"] | ||
| 27 | } | 28 | } |
tsconfig.jsonmodified+2 -1
| @@ -22,5 +22,6 @@ | |||
| 22 | "@/*": ["./src/*"] | 22 | "@/*": ["./src/*"] |
| 23 | } | 23 | } |
| 24 | }, | 24 | }, |
| 25 | - "include": ["src"] | 25 | + "include": ["src"], |
| 26 | + "exclude": ["src/**/*.test.ts"] | ||
| 26 | } | 27 | } |