forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

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