🩹 stop claiming summary work happens in the background
Browse files at this commit2c2da014d9da61207fa9624a705df2ff9ca89852Parent: a3421b2- fix
- 🩹 stop claiming summary work happens in the background
3 changed files +144 -7
- src/components/notes/rag-outputs-panel.test.tsx +106 -0
- src/components/notes/rag-outputs-panel.tsx +32 -4
- src/i18n/messages.ts +6 -3
src/components/notes/rag-outputs-panel.test.tsxadded+106 -0
| @@ -0,0 +1,106 @@ | ||
| 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 | +}); |
src/components/notes/rag-outputs-panel.tsxmodified+32 -4
| @@ -17,6 +17,13 @@ import { RagOutputContent } from "@/components/notes/rag-output-content"; | ||
| 17 | 17 | |
| 18 | 18 | const RAG_PAGE_SIZE = 10; |
| 19 | 19 | |
| 20 | +/** | |
| 21 | + * The capability a worker must have declared for a reindex call to have any | |
| 22 | + * chance of being answered. `/ai/capabilities` lists what connected services | |
| 23 | + * actually serve, so the button stays disabled until some worker serves this. | |
| 24 | + */ | |
| 25 | +const RAG_INDEX_CAPABILITY = "rag.index"; | |
| 26 | + | |
| 20 | 27 | export function RagOutputsPanel(props: { |
| 21 | 28 | noteId: string; |
| 22 | 29 | active: boolean; |
| @@ -30,6 +37,8 @@ export function RagOutputsPanel(props: { | ||
| 30 | 37 | const [flash, setFlash] = createFlash(); |
| 31 | 38 | const [deleteTarget, setDeleteTarget] = createSignal<RagOutput | null>(null); |
| 32 | 39 | const [generating, setGenerating] = createSignal(false); |
| 40 | + /** Set when a reindex attempt ended without a new output — the truthful state. */ | |
| 41 | + const [missed, setMissed] = createSignal(""); | |
| 33 | 42 | let pollTimer: ReturnType<typeof setTimeout> | undefined; |
| 34 | 43 | |
| 35 | 44 | onCleanup(() => pollTimer && clearTimeout(pollTimer)); |
| @@ -44,11 +53,20 @@ export function RagOutputsPanel(props: { | ||
| 44 | 53 | ); |
| 45 | 54 | |
| 46 | 55 | const outputs = () => list()?.items ?? []; |
| 47 | - const available = () => capabilities.error || capabilities()?.enabled !== false; | |
| 56 | + // The button is live only when a worker actually serves `rag.index`. A | |
| 57 | + // discovery failure is not evidence of absence, so leave it live and let the | |
| 58 | + // call itself report; while the list is still loading we do not know either. | |
| 59 | + const available = () => { | |
| 60 | + if (capabilities.error || capabilities.loading) return true; | |
| 61 | + const caps = capabilities(); | |
| 62 | + if (!caps || caps.enabled === false) return false; | |
| 63 | + return caps.capabilities.some((c) => c.capability === RAG_INDEX_CAPABILITY); | |
| 64 | + }; | |
| 48 | 65 | |
| 49 | 66 | const generate = async () => { |
| 50 | 67 | if (!props.source.reindexRag || generating()) return; |
| 51 | 68 | setError(""); |
| 69 | + setMissed(""); | |
| 52 | 70 | setGenerating(true); |
| 53 | 71 | const previousId = outputs()[0]?.id; |
| 54 | 72 | try { |
| @@ -62,9 +80,16 @@ export function RagOutputsPanel(props: { | ||
| 62 | 80 | attempts += 1; |
| 63 | 81 | const next = await refetch(); |
| 64 | 82 | const firstId = next?.items?.[0]?.id; |
| 65 | - if ((firstId && firstId !== previousId) || attempts >= 24) { | |
| 83 | + if (firstId && firstId !== previousId) { | |
| 84 | + setGenerating(false); | |
| 85 | + setFlash(t("courseNotes.ragReady")); | |
| 86 | + return; | |
| 87 | + } | |
| 88 | + if (attempts >= 24) { | |
| 89 | + // Nothing was produced. Say that plainly: no worker indexed this | |
| 90 | + // note, and the button runs the whole call again. | |
| 66 | 91 | setGenerating(false); |
| 67 | - setFlash(firstId && firstId !== previousId ? t("courseNotes.ragReady") : t("courseNotes.ragQueued")); | |
| 92 | + setMissed(t("courseNotes.ragNotIndexed")); | |
| 68 | 93 | return; |
| 69 | 94 | } |
| 70 | 95 | pollTimer = setTimeout(() => void poll().catch(fail), 2500); |
| @@ -115,6 +140,9 @@ export function RagOutputsPanel(props: { | ||
| 115 | 140 | <Show when={error()}> |
| 116 | 141 | <Alert variant="destructive">{error()}</Alert> |
| 117 | 142 | </Show> |
| 143 | + <Show when={missed()}> | |
| 144 | + <Alert variant="warning">{missed()}</Alert> | |
| 145 | + </Show> | |
| 118 | 146 | <Show when={!available()}> |
| 119 | 147 | <Alert variant="warning">{t("courseNotes.ragUnavailable")}</Alert> |
| 120 | 148 | </Show> |
| @@ -122,7 +150,7 @@ export function RagOutputsPanel(props: { | ||
| 122 | 150 | <Show when={list.error}> |
| 123 | 151 | <Alert variant="destructive">{formatApiError(list.error)}</Alert> |
| 124 | 152 | </Show> |
| 125 | - <Show when={outputs().length > 0} fallback={<p class="text-sm text-muted-foreground">{t("courseNotes.ragEmpty")}</p>}> | |
| 153 | + <Show when={outputs().length > 0} fallback={<p class="text-sm text-muted-foreground">{missed() ? t("courseNotes.ragEmptyFailed") : t("courseNotes.ragEmpty")}</p>}> | |
| 126 | 154 | <ul class="space-y-3"> |
| 127 | 155 | <For each={outputs()}> |
| 128 | 156 | {(output) => ( |
src/i18n/messages.tsmodified+6 -3
| @@ -875,7 +875,8 @@ export type MessageKey = | ||
| 875 | 875 | | "courseNotes.ragGenerate" |
| 876 | 876 | | "courseNotes.ragGenerating" |
| 877 | 877 | | "courseNotes.ragReady" |
| 878 | - | "courseNotes.ragQueued" | |
| 878 | + | "courseNotes.ragNotIndexed" | |
| 879 | + | "courseNotes.ragEmptyFailed" | |
| 879 | 880 | | "courseNotes.ragUnavailable" |
| 880 | 881 | | "podcast.title" |
| 881 | 882 | | "podcast.description" |
| @@ -2984,7 +2985,8 @@ const en: Dict = { | ||
| 2984 | 2985 | "courseNotes.ragGenerate": "Create summary", |
| 2985 | 2986 | "courseNotes.ragGenerating": "Creating summary…", |
| 2986 | 2987 | "courseNotes.ragReady": "The new summary is ready.", |
| 2987 | - "courseNotes.ragQueued": "Summary generation continues in the background.", | |
| 2988 | + "courseNotes.ragNotIndexed": "The summary was not created. Nothing is indexed for this note yet — press Create summary to try again.", | |
| 2989 | + "courseNotes.ragEmptyFailed": "Nothing is indexed for this note yet. Press Create summary to try again.", | |
| 2988 | 2990 | "courseNotes.ragUnavailable": "The AI summary service is not connected right now. Try again when a worker is available.", |
| 2989 | 2991 | "podcast.title": "Audio workshop", |
| 2990 | 2992 | "podcast.description": "Turn a lesson note into a listenable episode.", |
| @@ -5095,7 +5097,8 @@ const tr: Dict = { | ||
| 5095 | 5097 | "courseNotes.ragGenerate": "Özet oluştur", |
| 5096 | 5098 | "courseNotes.ragGenerating": "Özet oluşturuluyor…", |
| 5097 | 5099 | "courseNotes.ragReady": "Yeni özet hazır.", |
| 5098 | - "courseNotes.ragQueued": "Özet oluşturma arka planda devam ediyor.", | |
| 5100 | + "courseNotes.ragNotIndexed": "Özet oluşmadı. Bu not için henüz bir dizin oluşturulmadı — yeniden denemek için Özet oluştur'a basın.", | |
| 5101 | + "courseNotes.ragEmptyFailed": "Bu not için henüz bir dizin oluşturulmadı. Yeniden denemek için Özet oluştur'a basın.", | |
| 5099 | 5102 | "courseNotes.ragUnavailable": "YZ özet servisi şu anda bağlı değil. Bir worker hazır olduğunda tekrar deneyin.", |
| 5100 | 5103 | "podcast.title": "Ses Atölyesi", |
| 5101 | 5104 | "podcast.description": "Bir ders notunu dinlenebilir bir bölüme dönüştürün.", |