hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; |
| 2 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | import { RagOutputsPanel } from "@/components/notes/rag-outputs-panel"; |
| 4 | import type { NoteFileSource } from "@/lib/note-source"; |
| 5 | import { PreferencesProvider } from "@/stores/preferences-context"; |
| 6 | |
| 7 | const aiApi = vi.hoisted(() => ({ getAiCapabilities: vi.fn() })); |
| 8 | // The panel reports success through a toast; capture the text without a portal. |
| 9 | const toastApi = vi.hoisted(() => ({ showToast: vi.fn() })); |
| 10 | |
| 11 | vi.mock("@/api/ai", () => aiApi); |
| 12 | vi.mock("@/components/ui/toast", () => ({ showToast: toastApi.showToast, Toaster: () => null })); |
| 13 | |
| 14 | const CAPABILITIES_WITH_RAG = { |
| 15 | enabled: true, |
| 16 | protocol: "hab/2", |
| 17 | capabilities: [{ capability: "rag.index", workers: 1, inflight: 0 }], |
| 18 | }; |
| 19 | |
| 20 | /** A source whose list stays empty — the state a refused reindex leaves behind. */ |
| 21 | function stubSource(options: { rag?: boolean } = {}): NoteFileSource { |
| 22 | return { |
| 23 | maxFiles: 10, |
| 24 | listFiles: async () => ({ items: [], total: 0, limit: 10, offset: 0 }), |
| 25 | uploadFile: async () => { |
| 26 | throw new Error("not used by this panel"); |
| 27 | }, |
| 28 | deleteFile: async () => {}, |
| 29 | fileUrl: () => "", |
| 30 | fileBlob: async () => new Blob(), |
| 31 | listRagOutputs: async () => ({ items: [], total: 0, limit: 10, offset: 0 }), |
| 32 | deleteRagOutput: async () => {}, |
| 33 | reindexRag: async () => { |
| 34 | if (!options.rag) throw new Error("reindex should not be called without rag.index"); |
| 35 | }, |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | function renderPanel(source: NoteFileSource) { |
| 40 | render(() => ( |
| 41 | <PreferencesProvider> |
| 42 | <RagOutputsPanel noteId="note-1" active source={source} /> |
| 43 | </PreferencesProvider> |
| 44 | )); |
| 45 | } |
| 46 | |
| 47 | const generateButton = () => screen.getByRole("button", { name: "Create summary" }) as HTMLButtonElement; |
| 48 | |
| 49 | describe("RagOutputsPanel", () => { |
| 50 | afterEach(() => { |
| 51 | cleanup(); |
| 52 | vi.useRealTimers(); |
| 53 | vi.clearAllMocks(); |
| 54 | }); |
| 55 | |
| 56 | it("reports that nothing was indexed when the poll ends without an output", async () => { |
| 57 | vi.useFakeTimers(); |
| 58 | aiApi.getAiCapabilities.mockResolvedValue(CAPABILITIES_WITH_RAG); |
| 59 | |
| 60 | renderPanel(stubSource({ rag: true })); |
| 61 | fireEvent.click(generateButton()); |
| 62 | |
| 63 | // 24 polls after the initial 1.5s delay is the panel's own ceiling. |
| 64 | for (let i = 0; i < 24; i += 1) await vi.advanceTimersByTimeAsync(2500); |
| 65 | |
| 66 | // The truthful message is on screen, twice: the notice and the empty list. |
| 67 | expect( |
| 68 | screen.getByText("The summary was not created. Nothing is indexed for this note yet — press Create summary to try again."), |
| 69 | ).toBeTruthy(); |
| 70 | expect(screen.getByText("Nothing is indexed for this note yet. Press Create summary to try again.")).toBeTruthy(); |
| 71 | // The false claim must be gone: nothing is running in the background. |
| 72 | expect(toastApi.showToast).not.toHaveBeenCalledWith({ title: "Summary generation continues in the background." }); |
| 73 | // The button can be pressed again. |
| 74 | expect(generateButton().disabled).toBe(false); |
| 75 | }); |
| 76 | |
| 77 | it("disables the button and names the reason when no worker serves rag.index", async () => { |
| 78 | aiApi.getAiCapabilities.mockResolvedValue({ |
| 79 | enabled: true, |
| 80 | protocol: "hab/2", |
| 81 | capabilities: [{ capability: "chat.reply", workers: 1, inflight: 0 }], |
| 82 | }); |
| 83 | |
| 84 | renderPanel(stubSource()); |
| 85 | |
| 86 | await waitFor(() => { |
| 87 | expect( |
| 88 | screen.getByText("The AI summary service is not connected right now. Try again when a worker is available."), |
| 89 | ).toBeTruthy(); |
| 90 | }); |
| 91 | expect(generateButton().disabled).toBe(true); |
| 92 | }); |
| 93 | |
| 94 | it("keeps the button live when rag.index is advertised", async () => { |
| 95 | aiApi.getAiCapabilities.mockResolvedValue(CAPABILITIES_WITH_RAG); |
| 96 | |
| 97 | renderPanel(stubSource({ rag: true })); |
| 98 | |
| 99 | await waitFor(() => { |
| 100 | expect(generateButton().disabled).toBe(false); |
| 101 | }); |
| 102 | expect( |
| 103 | screen.queryByText("The AI summary service is not connected right now. Try again when a worker is available."), |
| 104 | ).toBeNull(); |
| 105 | }); |
| 106 | }); |