hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | |
| 3 | const api = vi.hoisted(() => ({ |
| 4 | getCourses: vi.fn(), |
| 5 | getExams: vi.fn(), |
| 6 | getHomework: vi.fn(), |
| 7 | getEvents: vi.fn(), |
| 8 | getMyInstances: vi.fn(), |
| 9 | loadInstanceLabels: vi.fn(), |
| 10 | })); |
| 11 | |
| 12 | vi.mock("@/api/courses", () => ({ getCourses: api.getCourses })); |
| 13 | vi.mock("@/api/exams", () => ({ getExams: api.getExams })); |
| 14 | vi.mock("@/api/homework", () => ({ getHomework: api.getHomework })); |
| 15 | vi.mock("@/api/events", () => ({ getEvents: api.getEvents })); |
| 16 | vi.mock("@/api/instances", () => ({ getMyInstances: api.getMyInstances })); |
| 17 | vi.mock("@/lib/instance-labels", () => ({ loadInstanceLabels: api.loadInstanceLabels })); |
| 18 | |
| 19 | import { |
| 20 | filterCommandRecords, |
| 21 | loadCommandRecords, |
| 22 | resetCommandRecordCache, |
| 23 | type CommandRecord, |
| 24 | } from "@/lib/command-search"; |
| 25 | |
| 26 | const page = <T,>(items: T[]) => ({ items, total: items.length, limit: 100, offset: 0 }); |
| 27 | const record = (kind: CommandRecord["kind"], id: string, title: string, context: string | null = null): CommandRecord => ({ |
| 28 | kind, |
| 29 | id, |
| 30 | title, |
| 31 | context, |
| 32 | description: null, |
| 33 | at: null, |
| 34 | }); |
| 35 | |
| 36 | describe("filterCommandRecords", () => { |
| 37 | const records = [ |
| 38 | record("event", "ev1", "Biyoloji gezisi"), |
| 39 | record("course", "c1", "Biyoloji"), |
| 40 | record("course", "c2", "Kimya"), |
| 41 | record("exam", "e1", "1. Yazılı", "Biyoloji — 10-B"), |
| 42 | record("homework", "h1", "Hücre ödevi", "Biyoloji — 9-A"), |
| 43 | ]; |
| 44 | |
| 45 | test("matches titles and section labels, Turkish-folded, grouped in kind order", () => { |
| 46 | expect(filterCommandRecords(records, "biyoloji").map((r) => r.id)).toEqual(["c1", "e1", "h1", "ev1"]); |
| 47 | expect(filterCommandRecords(records, "BİYOLOJİ").map((r) => r.id)).toEqual(["c1", "e1", "h1", "ev1"]); |
| 48 | expect(filterCommandRecords(records, "yazili").map((r) => r.id)).toEqual(["e1"]); |
| 49 | expect(filterCommandRecords(records, "hucre 9a").map((r) => r.id)).toEqual(["h1"]); |
| 50 | }); |
| 51 | |
| 52 | test("a one-letter query matches nothing", () => { |
| 53 | expect(filterCommandRecords(records, "b")).toEqual([]); |
| 54 | expect(filterCommandRecords(records, " ")).toEqual([]); |
| 55 | }); |
| 56 | |
| 57 | test("caps each kind", () => { |
| 58 | const many = Array.from({ length: 8 }, (_, i) => record("course", `c${i}`, `Matematik ${i}`)); |
| 59 | expect(filterCommandRecords(many, "matematik", 3).map((r) => r.id)).toEqual(["c0", "c1", "c2"]); |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | describe("loadCommandRecords", () => { |
| 64 | beforeEach(() => { |
| 65 | resetCommandRecordCache(); |
| 66 | api.getCourses.mockResolvedValue(page([{ id: "c1", title: "Biyoloji", description: "", kind: "course" }])); |
| 67 | api.getExams.mockResolvedValue( |
| 68 | page([ |
| 69 | { id: "e1", title: "Yazılı", description: "", class_course: "i1", starts_at: 5 }, |
| 70 | { id: "e2", title: "Quiz", description: "", class_course: "i2", starts_at: null }, |
| 71 | ]), |
| 72 | ); |
| 73 | api.getHomework.mockResolvedValue(page([{ id: "h1", title: "Ödev", description: null, class_course: "i1", due_at: 7 }])); |
| 74 | api.getEvents.mockResolvedValue(page([{ id: "ev1", title: "Gezi", description: "", starts_at: 9 }])); |
| 75 | api.getMyInstances.mockResolvedValue(page([{ id: "i1" }])); |
| 76 | api.loadInstanceLabels.mockResolvedValue(new Map([["i1", { label: "Biyoloji — 10-B" }]])); |
| 77 | }); |
| 78 | afterEach(() => vi.clearAllMocks()); |
| 79 | |
| 80 | test("a student reads their own courses and only exams in their own sections", async () => { |
| 81 | // GET /courses is already scoped to the student's own courses. |
| 82 | const out = await loadCommandRecords("student", ["course", "exam"]); |
| 83 | expect(api.getCourses).toHaveBeenCalled(); |
| 84 | expect(out.map((r) => r.id)).toEqual(["c1", "e1"]); |
| 85 | expect(out[1].context).toBe("Biyoloji — 10-B"); |
| 86 | }); |
| 87 | |
| 88 | test("staff read the full catalog and every exam the backend returns", async () => { |
| 89 | const out = await loadCommandRecords("teacher", ["course", "exam"]); |
| 90 | expect(api.getCourses).toHaveBeenCalled(); |
| 91 | expect(api.getMyInstances).not.toHaveBeenCalled(); |
| 92 | expect(out.map((r) => r.id)).toEqual(["c1", "e1", "e2"]); |
| 93 | }); |
| 94 | |
| 95 | test("only the requested kinds are read, and a failing kind leaves the others", async () => { |
| 96 | api.getEvents.mockRejectedValue(new Error("boom")); |
| 97 | const out = await loadCommandRecords("parent", ["event", "homework"]); |
| 98 | expect(api.getCourses).not.toHaveBeenCalled(); |
| 99 | expect(api.getExams).not.toHaveBeenCalled(); |
| 100 | expect(out.map((r) => r.id)).toEqual(["h1"]); |
| 101 | }); |
| 102 | |
| 103 | test("reopening within the cache window reuses the last read", async () => { |
| 104 | await loadCommandRecords("teacher", ["course"], 1_000); |
| 105 | await loadCommandRecords("teacher", ["course"], 30_000); |
| 106 | expect(api.getCourses).toHaveBeenCalledTimes(1); |
| 107 | await loadCommandRecords("teacher", ["course"], 120_000); |
| 108 | expect(api.getCourses).toHaveBeenCalledTimes(2); |
| 109 | }); |
| 110 | }); |