forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

instance-labels.test.ts100 lines, 4.4 KiBHistoryRawDownload
1import { classSections, formatInstanceLabel, invalidateSchoolWalk, loadInstanceLabels, loadSchoolSections, resetInstanceLabelCache } from "./instance-labels";
2
3const calls: string[] = [];
4let failClassOnce = false;
5
6vi.mock("@/api/instances", () => ({
7 getInstanceById: async (id: string) => {
8 calls.push(`instance:${id}`);
9 if (id === "missing") throw new Error("404");
10 // The instance carries its resolved display title, so no course read.
11 return { id, course: "c-math", class: id === "i2" ? "k-9a" : "k-10b", title: "Matematik" };
12 },
13}));
14vi.mock("@/api/classes", () => ({
15 getClasses: async () => {
16 calls.push("classes:list");
17 return { items: [{ id: "k-9a", name: "9-A" }, { id: "k-10b", name: "10-B" }], total: 2, limit: 200, offset: 0 };
18 },
19 getClassInstances: async (classId: string) => {
20 calls.push(`class-instances:${classId}`);
21 if (classId === "k-9a" && failClassOnce) {
22 failClassOnce = false;
23 throw new Error("offline");
24 }
25 const items = classId === "k-9a"
26 ? [{ id: "i2", course: "c-math", class: "k-9a", title: "Matematik" }]
27 : [{ id: "i1", course: "c-math", class: "k-10b", title: "Matematik" }, { id: "i3", course: "c-fiz", class: "k-10b", title: "Fizik" }];
28 return { items, total: items.length, limit: 200, offset: 0 };
29 },
30 getClassById: async (id: string) => {
31 calls.push(`class:${id}`);
32 return { id, name: id === "k-9a" ? "9-A" : "10-B" };
33 },
34 getMyClasses: async () => {
35 calls.push("classes:me");
36 return { items: [{ id: "k-10b", name: "10-B" }], total: 1, limit: null, offset: 0 };
37 },
38}));
39
40describe("instance labels", () => {
41 beforeEach(() => {
42 calls.length = 0;
43 failClassOnce = false;
44 resetInstanceLabelCache();
45 });
46
47 it("names the class next to the course so two sections of one ders differ", async () => {
48 const labels = await loadInstanceLabels(["i1", "i2", "i1"], "admin");
49 expect(labels.get("i1")?.label).toBe("Matematik — 10-B");
50 expect(labels.get("i2")?.label).toBe("Matematik — 9-A");
51 // No per-course or per-class reads: one class list covers every section.
52 expect(calls.some((call) => call.startsWith("course:") || call.startsWith("class:"))).toBe(false);
53 expect(calls.filter((call) => call === "classes:list")).toHaveLength(1);
54 });
55
56 it("reads a student's class names from /classes/me, never /classes/{id}", async () => {
57 const labels = await loadInstanceLabels(["i1", "i2"], "student");
58 expect(labels.get("i1")?.label).toBe("Matematik — 10-B");
59 // 9-A is not the student's class: the label falls back to the course title.
60 expect(labels.get("i2")?.label).toBe("Matematik");
61 expect(calls.some((call) => call.startsWith("class:"))).toBe(false);
62 expect(calls.filter((call) => call === "classes:me")).toHaveLength(1);
63 });
64
65 it("drops an unreadable instance instead of inventing a label", async () => {
66 const labels = await loadInstanceLabels(["missing"], "admin");
67 expect(labels.has("missing")).toBe(false);
68 });
69
70 it("formats partial labels", () => {
71 expect(formatInstanceLabel("Fizik", null)).toBe("Fizik");
72 expect(formatInstanceLabel(null, "9-A")).toBe("9-A");
73 expect(formatInstanceLabel(null, null)).toBe("");
74 });
75
76 it("reads the school's sections class by class when labels outnumber classes", async () => {
77 const labels = await loadInstanceLabels(["i1", "i2", "i3"], "admin");
78 expect(labels.get("i3")?.label).toBe("Fizik — 10-B");
79 expect(labels.get("i2")?.label).toBe("Matematik — 9-A");
80 // Two classes, three sections: two list reads instead of three instance reads.
81 expect(calls.filter((call) => call.startsWith("instance:"))).toHaveLength(0);
82 expect(calls.filter((call) => call.startsWith("class-instances:"))).toHaveLength(2);
83 });
84
85 it("shares the school walk across callers and refreshes after invalidation", async () => {
86 await loadSchoolSections();
87 await loadSchoolSections();
88 expect(calls.filter((call) => call.startsWith("class-instances:"))).toHaveLength(2);
89 invalidateSchoolWalk();
90 await loadSchoolSections();
91 expect(calls.filter((call) => call.startsWith("class-instances:"))).toHaveLength(4);
92 });
93
94 it("retries only the class whose section read failed", async () => {
95 failClassOnce = true;
96 expect(await classSections("k-9a").catch(() => [])).toEqual([]);
97 expect(await classSections("k-9a")).toHaveLength(1);
98 expect(calls.filter((call) => call === "class-instances:k-9a")).toHaveLength(2);
99 });
100});