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-study-actions.test.tsx68 lines, 2.9 KiBHistoryRawDownload
1import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library";
2import { afterEach, expect, test, vi } from "vitest";
3import { ApiError } from "@/api/client";
4import { ragCopy } from "@/components/rag/rag-copy";
5import { RagStudyActions } from "@/components/rag/rag-study-actions";
6import { PreferencesProvider } from "@/stores/preferences-context";
7
8const { postRagSummarize, postRagQuestions } = vi.hoisted(() => ({
9 postRagSummarize: vi.fn(),
10 postRagQuestions: vi.fn(),
11}));
12vi.mock("@/api/rag", () => ({ postRagSummarize, postRagQuestions }));
13
14afterEach(() => {
15 cleanup();
16 vi.clearAllMocks();
17});
18
19const scope = { ders: "Biyoloji", span_ids: ["s1"], pages: [1], scope_label: "Hücre" };
20
21function mount(unavailable = false, onUnavailable = vi.fn()) {
22 render(() => (
23 <PreferencesProvider>
24 <RagStudyActions scope={scope} copy={ragCopy("en")} unavailable={unavailable} onUnavailable={onUnavailable} />
25 </PreferencesProvider>
26 ));
27 return onUnavailable;
28}
29
30test("summarize sends exactly the cited range and shows the summary", async () => {
31 postRagSummarize.mockResolvedValue({ text: "Cells share metabolism.", abstained: false, reason: "", citations: [], scope_pages: [1], hierarchical: false });
32 mount();
33
34 fireEvent.click(screen.getByRole("button", { name: "Summarize" }));
35
36 expect(await screen.findByText("Cells share metabolism.")).toBeTruthy();
37 expect(postRagSummarize).toHaveBeenCalledWith(scope);
38 expect(screen.getByText("Pages covered: 1")).toBeTruthy();
39});
40
41test("practice questions carry the chosen difficulty and keep answers folded", async () => {
42 postRagQuestions.mockResolvedValue({ items: [{ question: "What do all cells do?", answer: "Metabolize.", difficulty: "zor" }], abstained: false, reason: "", span_ids: ["s1"], pages: [1] });
43 mount();
44
45 // The app Select keeps a hidden native <select> as its value carrier.
46 fireEvent.change(document.querySelector("select")!, { target: { value: "zor" } });
47 fireEvent.click(screen.getByRole("button", { name: "Practice questions" }));
48
49 expect(await screen.findByText("What do all cells do?")).toBeTruthy();
50 expect(postRagQuestions).toHaveBeenCalledWith({ ...scope, n: 5, difficulty: "zor" });
51 expect(screen.getByText("Show answer")).toBeTruthy();
52});
53
54test("a 503 tells the board the study service is down instead of showing an error", async () => {
55 postRagSummarize.mockRejectedValue(new ApiError(503, "no AI service is connected right now"));
56 const onUnavailable = mount();
57
58 fireEvent.click(screen.getByRole("button", { name: "Summarize" }));
59
60 await waitFor(() => expect(onUnavailable).toHaveBeenCalledOnce());
61 expect(screen.queryByRole("alert")).toBeNull();
62});
63
64test("when the service is down the buttons stand down with a plain note", () => {
65 mount(true);
66 expect(screen.queryByRole("button", { name: "Summarize" })).toBeNull();
67 expect(screen.getByText(/not available right now/)).toBeTruthy();
68});