forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB4e0a329Burak Boduroğlu2 months ago537 commits

course-notes.test.ts167 lines, 6.1 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { getCourseNotes } from "../../course-notes";
3import { getCourseNoteById } from "../../course-notes";
4import { postCourseNote } from "../../course-notes";
5import { patchCourseNoteById } from "../../course-notes";
6import { deleteCourseNoteById } from "../../course-notes";
7import { getCourseNoteFiles } from "../../course-notes";
8import { postCourseNoteFile } from "../../course-notes";
9import { deleteCourseNoteFileById } from "../../course-notes";
10import { getCourseNoteFileUrl } from "../../course-notes";
11import { getCourseNoteFileBlob } from "../../course-notes";
12import { getCourseNoteRag } from "../../course-notes";
13import { deleteCourseNoteRagOutput } from "../../course-notes";
14import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch";
15describe("course notes API", () => {
16 afterEach(() => {
17 vi.restoreAllMocks();
18 });
19
20 it("getCourseNotes calls /course-notes with the required course filter", async () => {
21 const mockPage = { items: [{ id: "cn1", course: "c1", title: "Note" }], total: 1 };
22 mockFetchSuccess(mockPage);
23
24 const result = await getCourseNotes("c1");
25 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
26
27 const [url, init] = lastFetchCall();
28 expect(url).toBe("/api/course-notes?course=c1");
29 expect(init?.method).toBe("GET");
30 });
31
32 it("getCourseNotes keeps the course filter alongside pagination", async () => {
33 mockFetchSuccess({ items: [], total: 0 });
34
35 await getCourseNotes("c1", { limit: 10, offset: 20 });
36
37 const [url] = lastFetchCall();
38 expect(url).toBe("/api/course-notes?course=c1&limit=10&offset=20");
39 });
40
41 it("postCourseNote calls /course-notes with the course in the body", async () => {
42 const mockNote = { id: "cn1", course: "c1", title: "Note", content: "Content" };
43 mockFetchSuccess(mockNote);
44
45 const data = { course: "c1", title: "Note", content: "Content" };
46 const result = await postCourseNote(data);
47 expect(result).toEqual(mockNote);
48
49 const [url, init] = lastFetchCall();
50 expect(url).toBe("/api/course-notes");
51 expect(init?.method).toBe("POST");
52 expect(init?.body).toBe(JSON.stringify(data));
53 });
54
55 it("getCourseNoteById calls /course-notes/:id", async () => {
56 const mockNote = { id: "cn1", course: "c1", title: "Note", content: "Content" };
57 mockFetchSuccess(mockNote);
58
59 const result = await getCourseNoteById("cn1");
60 expect(result).toEqual(mockNote);
61
62 const [url, init] = lastFetchCall();
63 expect(url).toBe("/api/course-notes/cn1");
64 expect(init?.method).toBe("GET");
65 });
66
67 it("patchCourseNoteById calls /course-notes/:id with updates", async () => {
68 const mockNote = { id: "cn1", course: "c1", title: "Updated", content: "Content" };
69 mockFetchSuccess(mockNote);
70
71 const updates = { title: "Updated" };
72 const result = await patchCourseNoteById("cn1", updates);
73 expect(result).toEqual(mockNote);
74
75 const [url, init] = lastFetchCall();
76 expect(url).toBe("/api/course-notes/cn1");
77 expect(init?.method).toBe("PATCH");
78 expect(init?.body).toBe(JSON.stringify(updates));
79 });
80
81 it("deleteCourseNoteById calls /course-notes/:id", async () => {
82 mockFetch204();
83
84 await deleteCourseNoteById("cn1");
85
86 const [url, init] = lastFetchCall();
87 expect(url).toBe("/api/course-notes/cn1");
88 expect(init?.method).toBe("DELETE");
89 });
90
91 it("getCourseNoteFiles calls /course-notes/:id/files with pagination", async () => {
92 const mockPage = { items: [{ id: "f1", name: "test.pdf" }], total: 1 };
93 mockFetchSuccess(mockPage);
94
95 const result = await getCourseNoteFiles("cn1", { limit: 10 });
96 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
97
98 const [url, init] = lastFetchCall();
99 expect(url).toBe("/api/course-notes/cn1/files?limit=10");
100 expect(init?.method).toBe("GET");
101 });
102
103 it("postCourseNoteFile calls /course-notes/:id/files with FormData", async () => {
104 const mockFile = { id: "f1", name: "test.pdf", content_type: "application/pdf", size: 100 };
105 mockFetchSuccess(mockFile);
106
107 const file = new File(["test"], "test.pdf", { type: "application/pdf" });
108 const result = await postCourseNoteFile("cn1", file);
109 expect(result).toEqual(mockFile);
110
111 const [url, init] = lastFetchCall();
112 expect(url).toBe("/api/course-notes/cn1/files");
113 expect(init?.method).toBe("POST");
114 expect(init?.body).toBeInstanceOf(FormData);
115 expect((init?.body as FormData).get("file")).toBe(file);
116 });
117
118 it("deleteCourseNoteFileById calls /course-notes/:id/files/:fileId", async () => {
119 mockFetch204();
120
121 await deleteCourseNoteFileById("cn1", "f1");
122
123 const [url, init] = lastFetchCall();
124 expect(url).toBe("/api/course-notes/cn1/files/f1");
125 expect(init?.method).toBe("DELETE");
126 });
127
128 it("getCourseNoteFileUrl returns correct URL path", () => {
129 const url = getCourseNoteFileUrl("cn1", "f1");
130 expect(url).toBe("/api/course-notes/cn1/files/f1");
131 });
132
133 it("getCourseNoteFileBlob fetches file blob", async () => {
134 const blob = new Blob(["test"], { type: "application/pdf" });
135 mockFetchBlob(blob);
136
137 const result = await getCourseNoteFileBlob("cn1", "f1");
138 expect(result).toBeInstanceOf(Blob);
139 expect(result.type).toBe("application/pdf");
140
141 const [url, init] = lastFetchCall();
142 expect(url).toBe("/api/course-notes/cn1/files/f1");
143 expect(init?.credentials).toBe("same-origin");
144 });
145
146 it("getCourseNoteRag calls /course-notes/:id/rag with pagination", async () => {
147 const mockPage = { items: [{ id: "r1", course_note: "cn1" }], total: 1 };
148 mockFetchSuccess(mockPage);
149
150 const result = await getCourseNoteRag("cn1", { limit: 10 });
151 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
152
153 const [url, init] = lastFetchCall();
154 expect(url).toBe("/api/course-notes/cn1/rag?limit=10");
155 expect(init?.method).toBe("GET");
156 });
157
158 it("deleteCourseNoteRagOutput calls /course-notes/:id/rag/:outputId", async () => {
159 mockFetch204();
160
161 await deleteCourseNoteRagOutput("cn1", "r1");
162
163 const [url, init] = lastFetchCall();
164 expect(url).toBe("/api/course-notes/cn1/rag/r1");
165 expect(init?.method).toBe("DELETE");
166 });
167});