forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

rag.test.ts129 lines, 4.4 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import {
3 deleteRagThreadById,
4 getRagMessageById,
5 getRagThreadMessages,
6 getRagThreads,
7 patchRagThreadById,
8 postRagMessage,
9 postRagQuestions,
10 postRagSummarize,
11 postRagThread,
12 ragStreamUrl,
13} from "../../rag";
14import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
15
16describe("rag API", () => {
17 afterEach(() => {
18 vi.restoreAllMocks();
19 });
20
21 it("getRagThreads GETs /rag/threads with pagination", async () => {
22 mockFetchSuccess({ items: [{ id: "r1" }], total: 1 });
23
24 const result = await getRagThreads({ limit: 10 });
25 expect(result.items).toEqual([{ id: "r1" }]);
26
27 const [url, init] = lastFetchCall();
28 expect(url).toBe("/api/rag/threads?limit=10");
29 expect(init?.method).toBe("GET");
30 });
31
32 it("postRagThread POSTs /rag/threads with the title", async () => {
33 mockFetchSuccess({ id: "r1" });
34
35 await postRagThread("Fizik ödevi");
36
37 const [url, init] = lastFetchCall();
38 expect(url).toBe("/api/rag/threads");
39 expect(init?.method).toBe("POST");
40 expect(init?.body).toBe(JSON.stringify({ title: "Fizik ödevi" }));
41 });
42
43 it("postRagThread sends a null title when none is given", async () => {
44 mockFetchSuccess({ id: "r1" });
45
46 await postRagThread();
47
48 const [, init] = lastFetchCall();
49 expect(init?.body).toBe(JSON.stringify({ title: null }));
50 });
51
52 it("patchRagThreadById PATCHes /rag/threads/:id", async () => {
53 mockFetchSuccess({ id: "r1" });
54
55 await patchRagThreadById("r1", null);
56
57 const [url, init] = lastFetchCall();
58 expect(url).toBe("/api/rag/threads/r1");
59 expect(init?.method).toBe("PATCH");
60 expect(init?.body).toBe(JSON.stringify({ title: null }));
61 });
62
63 it("deleteRagThreadById DELETEs /rag/threads/:id", async () => {
64 mockFetch204();
65
66 await deleteRagThreadById("r1");
67
68 const [url, init] = lastFetchCall();
69 expect(url).toBe("/api/rag/threads/r1");
70 expect(init?.method).toBe("DELETE");
71 });
72
73 it("getRagThreadMessages GETs /rag/threads/:id/messages", async () => {
74 mockFetchSuccess({ items: [], total: 0 });
75
76 await getRagThreadMessages("r1", { limit: 50 });
77
78 const [url] = lastFetchCall();
79 expect(url).toBe("/api/rag/threads/r1/messages?limit=50");
80 });
81
82 it("getRagMessageById GETs one turn", async () => {
83 mockFetchSuccess({ id: "m1", status: "pending" });
84
85 await getRagMessageById("r1", "m1");
86
87 const [url] = lastFetchCall();
88 expect(url).toBe("/api/rag/threads/r1/messages/m1");
89 });
90
91 it("postRagMessage POSTs /rag/threads/:id/messages with the content", async () => {
92 mockFetchSuccess({ message_id: "m1", status: "pending" }, 202);
93
94 const result = await postRagMessage("r1", "Newton'un ikinci yasası?");
95 expect(result.message_id).toBe("m1");
96
97 const [url, init] = lastFetchCall();
98 expect(url).toBe("/api/rag/threads/r1/messages");
99 expect(init?.method).toBe("POST");
100 expect(init?.body).toBe(JSON.stringify({ content: "Newton'un ikinci yasası?" }));
101 });
102
103 it("ragStreamUrl builds the SSE path for an EventSource", () => {
104 expect(ragStreamUrl("r 1", "m1")).toBe("/api/rag/threads/r%201/messages/m1/stream");
105 });
106 it("postRagSummarize POSTs /rag/summarize with the study scope", async () => {
107 mockFetchSuccess({ text: "Özet", abstained: false, reason: "", citations: [], scope_pages: [3, 4], hierarchical: false });
108
109 const result = await postRagSummarize({ ders: "Fizik", sinif: "9", span_ids: ["s1", "s2"], scope_label: "Kuvvet" });
110 expect(result.scope_pages).toEqual([3, 4]);
111
112 const [url, init] = lastFetchCall();
113 expect(url).toBe("/api/rag/summarize");
114 expect(init?.method).toBe("POST");
115 expect(init?.body).toBe(JSON.stringify({ ders: "Fizik", sinif: "9", span_ids: ["s1", "s2"], scope_label: "Kuvvet" }));
116 });
117
118 it("postRagQuestions POSTs /rag/questions with the scope and set shape", async () => {
119 mockFetchSuccess({ items: [{ question: "Q?", answer: "A", difficulty: "orta" }], abstained: false, reason: "", span_ids: ["s1"], pages: [3] });
120
121 const result = await postRagQuestions({ ders: "Fizik", pages: [3], n: 3, difficulty: "kolay" });
122 expect(result.items[0]?.answer).toBe("A");
123
124 const [url, init] = lastFetchCall();
125 expect(url).toBe("/api/rag/questions");
126 expect(init?.method).toBe("POST");
127 expect(init?.body).toBe(JSON.stringify({ ders: "Fizik", pages: [3], n: 3, difficulty: "kolay" }));
128 });
129});