forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

note-studio-panel.test.tsx50 lines, 1.9 KiBHistoryRawDownload
1import { beforeEach, describe, expect, it, vi } from "vitest";
2import { getCachedStudioNotes, loadStudioNotes } from "@/components/ai/note-studio-panel";
3
4const coursesApi = vi.hoisted(() => ({ getCourses: vi.fn() }));
5const notesApi = vi.hoisted(() => ({ getCourseNotes: vi.fn() }));
6
7vi.mock("@/api/courses", () => coursesApi);
8vi.mock("@/api/course-notes", async (importOriginal) => ({
9 ...await importOriginal<typeof import("@/api/course-notes")>(),
10 ...notesApi,
11}));
12
13describe("loadStudioNotes", () => {
14 beforeEach(() => vi.clearAllMocks());
15 it("limits concurrent course note requests and preserves course order", async () => {
16 const courses = Array.from({ length: 10 }, (_, index) => ({
17 id: `course-${index}`,
18 title: `Course ${index}`,
19 creator: { id: "teacher" },
20 }));
21 coursesApi.getCourses.mockResolvedValue({ items: courses });
22 let active = 0;
23 let peak = 0;
24 notesApi.getCourseNotes.mockImplementation(async (courseId: string) => {
25 active++;
26 peak = Math.max(peak, active);
27 await new Promise((resolve) => setTimeout(resolve, courseId === "course-0" ? 5 : 0));
28 active--;
29 return { items: [{ id: `note-${courseId}`, title: courseId }] };
30 });
31
32 const notes = await loadStudioNotes();
33
34 expect(coursesApi.getCourses).toHaveBeenCalledWith({ limit: 100 });
35 expect(notesApi.getCourseNotes).toHaveBeenCalledTimes(10);
36 expect(peak).toBe(4);
37 expect(notes.map((note) => note.courseTitle)).toEqual(courses.map((course) => course.title));
38 });
39
40 it("reuses the completed course note list within a tab", async () => {
41 coursesApi.getCourses.mockResolvedValue({ items: [] });
42 const first = getCachedStudioNotes("cache-test-user");
43 const second = getCachedStudioNotes("cache-test-user");
44
45 expect(first).toBe(second);
46 await first;
47 await getCachedStudioNotes("cache-test-user");
48 expect(coursesApi.getCourses).toHaveBeenCalledTimes(1);
49 });
50});