import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { createTestApp, sampleQuestions, type TestApp } from './helpers.ts'; let t: TestApp; beforeEach(async () => { t = await createTestApp(); }); afterEach(() => { vi.restoreAllMocks(); t.close(); }); async function makeClass(name = 'Pharmacology') { return (await t.api('POST', '/api/classes', { name })).body.class as { id: string }; } describe('manual ordering', () => { it('stores a custom order per kind and exposes it on the class', async () => { const cls = await makeClass(); const ids: string[] = []; for (const title of ['A', 'B', 'C']) ids.push((await t.api('POST', '/api/quizzes', { classId: cls.id, title, questions: sampleQuestions })).body.quiz.id); const other = await makeClass('Other'); const foreign = (await t.api('POST', '/api/quizzes', { classId: other.id, title: 'Foreign', questions: sampleQuestions })).body.quiz.id; const res = await t.api('POST', `/api/classes/${cls.id}/items/reorder`, { kind: 'quiz', ids: [ids[2], foreign, ids[0], ids[1]] }); expect(res.status).toBe(200); const items = (await t.api('GET', `/api/classes/${cls.id}`)).body.items as { id: string; kind: string; title: string; sortOrder: number }[]; const order = items.filter((i) => i.kind === 'quiz').sort((a, b) => a.sortOrder - b.sortOrder).map((i) => i.title); expect(order).toEqual(['C', 'A', 'B']); // An id from another class is ignored, not moved. const otherItems = (await t.api('GET', `/api/classes/${other.id}`)).body.items as { id: string; sortOrder: number }[]; expect(otherItems.find((i) => i.id === foreign)?.sortOrder).toBe(0); }); it('rejects unknown kinds', async () => { const cls = await makeClass(); expect((await t.api('POST', `/api/classes/${cls.id}/items/reorder`, { kind: 'class', ids: [] })).status).toBe(422); }); }); describe('timed exam deadlines', () => { async function startExam(minutes: number) { const cls = await makeClass(); const quiz = (await t.api('POST', '/api/quizzes', { classId: cls.id, title: 'Timed', questions: sampleQuestions })).body.quiz; const res = await t.api('POST', '/api/attempts', { mode: 'exam', source: { type: 'quiz', quizId: quiz.id }, timeLimitMin: minutes, shuffleQuestions: false, shuffleOptions: false }); expect(res.status).toBe(201); return { attempt: res.body.attempt, quiz }; } it('fixes a wall-clock deadline when the exam starts', async () => { const before = Date.now(); const { attempt } = await startExam(10); expect(attempt.settings.deadlineAt).toBeGreaterThanOrEqual(before + 600_000); expect(attempt.settings.deadlineAt).toBeLessThanOrEqual(Date.now() + 600_000); expect(typeof attempt.serverNow).toBe('number'); }); it('accepts answers before the deadline and refuses them after, submitting what was saved', async () => { const { attempt } = await startExam(1); const q = attempt.questions[0]; const right = q.options.find((o: { correct: boolean }) => o.correct).id; expect((await t.api('PATCH', `/api/attempts/${attempt.id}`, { answers: { [q.id]: { selected: [right] } } })).status).toBe(200); const start = attempt.settings.deadlineAt - 60_000; vi.spyOn(Date, 'now').mockReturnValue(attempt.settings.deadlineAt + 30_000); const q2 = attempt.questions[1]; const late = await t.api('PATCH', `/api/attempts/${attempt.id}`, { answers: { [q2.id]: { selected: [q2.options[0].id] } } }); expect(late.status).toBe(409); expect(late.body.error.code).toBe('time_up'); const after = (await t.api('GET', `/api/attempts/${attempt.id}`)).body.attempt; expect(after.status).toBe('completed'); expect(after.answers[q2.id]).toBeUndefined(); expect(after.score).toBe(1); // Time used is capped at the limit even though the clock ran on. expect(after.elapsedSec).toBe(60); expect(after.startedAt).toBe(start); }); it('allows a short grace period for saves in flight at the deadline', async () => { const { attempt } = await startExam(1); const q = attempt.questions[0]; vi.spyOn(Date, 'now').mockReturnValue(attempt.settings.deadlineAt + 2_000); expect((await t.api('PATCH', `/api/attempts/${attempt.id}`, { answers: { [q.id]: { selected: [q.options[0].id] } } })).status).toBe(200); }); it('submits overdue exams before lists are read, so they are not offered for resuming', async () => { const { attempt, quiz } = await startExam(1); vi.spyOn(Date, 'now').mockReturnValue(attempt.settings.deadlineAt + 60_000); const active = (await t.api('GET', '/api/attempts?status=active')).body.attempts; expect(active).toHaveLength(0); const cls = (await t.api('GET', `/api/classes/${quiz.classId}`)).body; expect(cls.items.find((i: { id: string }) => i.id === quiz.id).activeAttemptId).toBeNull(); }); it('leaves untimed sessions alone', async () => { const cls = await makeClass(); const quiz = (await t.api('POST', '/api/quizzes', { classId: cls.id, title: 'Untimed', questions: sampleQuestions })).body.quiz; const attempt = (await t.api('POST', '/api/attempts', { mode: 'exam', source: { type: 'quiz', quizId: quiz.id } })).body.attempt; expect(attempt.settings.deadlineAt).toBeNull(); vi.spyOn(Date, 'now').mockReturnValue(Date.now() + 86_400_000); expect((await t.api('GET', `/api/attempts/${attempt.id}`)).body.attempt.status).toBe('active'); }); });