hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { getCachedStudioNotes, loadStudioNotes } from "@/components/ai/note-studio-panel"; |
| 3 | |
| 4 | const coursesApi = vi.hoisted(() => ({ getCourses: vi.fn() })); |
| 5 | const notesApi = vi.hoisted(() => ({ getCourseNotes: vi.fn() })); |
| 6 | |
| 7 | vi.mock("@/api/courses", () => coursesApi); |
| 8 | vi.mock("@/api/course-notes", async (importOriginal) => ({ |
| 9 | ...await importOriginal<typeof import("@/api/course-notes")>(), |
| 10 | ...notesApi, |
| 11 | })); |
| 12 | |
| 13 | describe("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 | }); |