forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

chatbot.test.ts44 lines, 2.5 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { chatbotStreamUrl, deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread } from "../../chatbot";
3import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
4
5describe("chatbot API", () => {
6 afterEach(() => vi.restoreAllMocks());
7
8 it("creates an untitled chatbot thread", async () => {
9 mockFetchSuccess({ id: "thread-1" });
10 await postChatbotThread();
11 const [url, init] = lastFetchCall();
12 expect(url).toBe("/api/chatbot/threads");
13 expect(init?.method).toBe("POST");
14 expect(init?.body).toBe("{}");
15 });
16 it("lists threads and a selected thread's messages", async () => {
17 mockFetchSuccess({ items: [], total: 0 }); await getChatbotThreads({ limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/chatbot/threads?limit=20");
18 mockFetchSuccess({ items: [], total: 0 }); await getChatbotThreadMessages("thread 1", { limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/chatbot/threads/thread%201/messages?limit=20");
19 });
20 it("renames and deletes a thread", async () => {
21 mockFetchSuccess({}); await patchChatbotThreadById("thread 1", { title: "Physics" }); expect(lastFetchCall()).toMatchObject(["/api/chatbot/threads/thread%201", { method: "PATCH", body: JSON.stringify({ title: "Physics" }) }]);
22 mockFetch204(); await deleteChatbotThreadById("thread 1"); expect(lastFetchCall()).toMatchObject(["/api/chatbot/threads/thread%201", { method: "DELETE" }]);
23 });
24
25 it("sends a message to its thread", async () => {
26 mockFetchSuccess({ message_id: "answer-1", status: "pending" });
27 await postChatbotMessage("thread 1", "Merhaba");
28 const [url, init] = lastFetchCall();
29 expect(url).toBe("/api/chatbot/threads/thread%201/messages");
30 expect(init?.method).toBe("POST");
31 expect(init?.body).toBe(JSON.stringify({ content: "Merhaba" }));
32 });
33
34 it("reads a pending or completed assistant message", async () => {
35 mockFetchSuccess({ id: "answer-1", status: "complete" });
36 await getChatbotMessageById("thread-1", "answer-1");
37 const [url, init] = lastFetchCall();
38 expect(url).toBe("/api/chatbot/threads/thread-1/messages/answer-1");
39 expect(init?.method).toBe("GET");
40 });
41 it("chatbotStreamUrl builds the SSE path for an EventSource", () => {
42 expect(chatbotStreamUrl("t 1", "m1")).toBe("/api/chatbot/threads/t%201/messages/m1/stream");
43 });
44});