hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import type { PersonRef, StudentInsight } from "@/api/client"; |
| 2 | import { cachedStudentSignal, clearStudentSignalCache, insightOverview, loadStudentSignals } from "./insight-students"; |
| 3 | import { toStudentSignal } from "./insight-run-report"; |
| 4 | |
| 5 | const person = (id: string): PersonRef => ({ id, username: id, display_name: id.toUpperCase() }); |
| 6 | const insight = (id: string, average: number, rate: number, attention: number): StudentInsight => ({ |
| 7 | user_id: id, |
| 8 | summary: { |
| 9 | confidence: "high", |
| 10 | computed_at: 1, |
| 11 | marks: { courses: { c1: { n_marks: 2, average } } }, |
| 12 | attendance: { overall: { rate, n_obs: 10 } }, |
| 13 | } as never, |
| 14 | attention: Array.from({ length: attention }, () => ({}) as never), |
| 15 | cards: [], |
| 16 | segments: [], |
| 17 | }); |
| 18 | |
| 19 | it("reads every student, turning a failed read into that row's error", async () => { |
| 20 | const rows: string[] = []; |
| 21 | await loadStudentSignals([person("a"), person("b")], (row) => rows.push(`${row.id}:${row.state}`), async (id) => { |
| 22 | if (id === "b") throw new Error("boom"); |
| 23 | return insight(id, 80, 0.9, 0); |
| 24 | }); |
| 25 | expect(rows.sort()).toEqual(["a:ok", "b:error"]); |
| 26 | }); |
| 27 | |
| 28 | it("summarises only what the summaries measured", () => { |
| 29 | const signals = [ |
| 30 | toStudentSignal({ id: "a", name: "A" }, insight("a", 80, 0.9, 2), null), |
| 31 | toStudentSignal({ id: "b", name: "B" }, insight("b", 60, 0.7, 0), null), |
| 32 | toStudentSignal({ id: "c", name: "C" }, { user_id: "c", summary: null, attention: [], cards: [], segments: [] }, null), |
| 33 | toStudentSignal({ id: "d", name: "D" }, null, null), |
| 34 | ]; |
| 35 | expect(insightOverview(signals, 5)).toEqual({ |
| 36 | total: 5, |
| 37 | loaded: 3, |
| 38 | analysed: 2, |
| 39 | needAttention: 1, |
| 40 | marksAverage: 70, |
| 41 | attendanceRate: 0.8, |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | it("reuses successful reads within a viewer tab and keeps viewers separate", async () => { |
| 46 | const read = vi.fn(async (id: string) => insight(id, 80, 0.9, 0)); |
| 47 | const rows: string[] = []; |
| 48 | await loadStudentSignals([person("a")], (row) => rows.push(row.state), read, "viewer-a"); |
| 49 | expect(cachedStudentSignal(person("a"), "viewer-a")?.state).toBe("ok"); |
| 50 | await loadStudentSignals([person("a")], (row) => rows.push(row.state), read, "viewer-a"); |
| 51 | expect(read).toHaveBeenCalledTimes(1); |
| 52 | expect(cachedStudentSignal(person("a"), "viewer-b")).toBeNull(); |
| 53 | await loadStudentSignals([person("a")], () => {}, read, "viewer-b"); |
| 54 | expect(read).toHaveBeenCalledTimes(2); |
| 55 | expect(rows).toEqual(["ok", "ok"]); |
| 56 | clearStudentSignalCache("viewer-a"); |
| 57 | clearStudentSignalCache("viewer-b"); |
| 58 | }); |
| 59 | |
| 60 | it("limits concurrent reads in a requested row window", async () => { |
| 61 | let active = 0; |
| 62 | let peak = 0; |
| 63 | const pending: Array<() => void> = []; |
| 64 | const read = vi.fn((id: string) => new Promise<StudentInsight>((resolve) => { |
| 65 | active++; |
| 66 | peak = Math.max(peak, active); |
| 67 | pending.push(() => { active--; resolve(insight(id, 80, 0.9, 0)); }); |
| 68 | })); |
| 69 | const done = loadStudentSignals(Array.from({ length: 12 }, (_, index) => person(String(index))), () => {}, read); |
| 70 | expect(read).toHaveBeenCalledTimes(6); |
| 71 | while (pending.length) { |
| 72 | pending.shift()!(); |
| 73 | await Promise.resolve(); |
| 74 | } |
| 75 | await done; |
| 76 | expect(read).toHaveBeenCalledTimes(12); |
| 77 | expect(peak).toBe(6); |
| 78 | }); |
| 79 | |
| 80 | it("does not cache a failed read", async () => { |
| 81 | const read = vi.fn() |
| 82 | .mockRejectedValueOnce(new Error("temporary")) |
| 83 | .mockResolvedValueOnce(insight("retry", 80, 0.9, 0)); |
| 84 | const states: string[] = []; |
| 85 | await loadStudentSignals([person("retry")], (row) => states.push(row.state), read, "retry-viewer"); |
| 86 | await loadStudentSignals([person("retry")], (row) => states.push(row.state), read, "retry-viewer"); |
| 87 | expect(states).toEqual(["error", "ok"]); |
| 88 | expect(read).toHaveBeenCalledTimes(2); |
| 89 | clearStudentSignalCache("retry-viewer"); |
| 90 | }); |