|
1 | 1 | /* @vitest-environment jsdom */ |
2 | 2 | import "@testing-library/jest-dom/vitest"; |
3 | | -import { fireEvent, render, screen, waitFor } from "@testing-library/react"; |
4 | | -import { afterEach, describe, expect, it, vi } from "vitest"; |
5 | | -import { StatsTab } from "./StatsTab"; |
| 3 | +import { fireEvent, render, screen, waitFor, act } from "@testing-library/react"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { StatsTab, IndexProgress } from "./StatsTab"; |
| 6 | +import { messages } from "../lib/i18n"; |
6 | 7 |
|
7 | 8 | function mockProjectsFetch(extra?: (url: string, init?: RequestInit) => Response | undefined) { |
8 | 9 | const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { |
@@ -93,3 +94,143 @@ describe("StatsTab index modal", () => { |
93 | 94 | expect(screen.getByRole("button", { name: "Browse D:/" })).toBeInTheDocument(); |
94 | 95 | }); |
95 | 96 | }); |
| 97 | + |
| 98 | +describe("IndexProgress", () => { |
| 99 | + beforeEach(() => { |
| 100 | + vi.useFakeTimers(); |
| 101 | + }); |
| 102 | + |
| 103 | + afterEach(() => { |
| 104 | + vi.useRealTimers(); |
| 105 | + vi.unstubAllGlobals(); |
| 106 | + vi.restoreAllMocks(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("polls and shows indexing in progress when active", async () => { |
| 110 | + const fetchMock = vi.fn().mockImplementation(() => |
| 111 | + Promise.resolve({ |
| 112 | + json: () => Promise.resolve([ |
| 113 | + { slot: 1, status: "indexing", path: "/path/to/project1" } |
| 114 | + ]) |
| 115 | + } as unknown as Response) |
| 116 | + ); |
| 117 | + vi.stubGlobal("fetch", fetchMock); |
| 118 | + |
| 119 | + const onDone = vi.fn(); |
| 120 | + render(<IndexProgress onDone={onDone} />); |
| 121 | + |
| 122 | + // Fast-forward initial poll |
| 123 | + await act(async () => { |
| 124 | + await vi.advanceTimersByTimeAsync(2000); |
| 125 | + }); |
| 126 | + |
| 127 | + expect(fetchMock).toHaveBeenCalledWith("/api/index-status"); |
| 128 | + expect(screen.getByText(messages.en.projects.indexingInProgress)).toBeInTheDocument(); |
| 129 | + expect(screen.getByText("/path/to/project1")).toBeInTheDocument(); |
| 130 | + expect(onDone).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + it("stops polling and calls onDone when indexing finishes successfully", async () => { |
| 134 | + // Backend keeps finished jobs listed with status "done" (src/ui/http_server.c |
| 135 | + // handle_index_status only skips idle slots) — success is a "done" entry, |
| 136 | + // not an empty list. |
| 137 | + let mockData = [ |
| 138 | + { slot: 1, status: "indexing", path: "/path/to/project" } |
| 139 | + ]; |
| 140 | + const fetchMock = vi.fn().mockImplementation(() => |
| 141 | + Promise.resolve({ |
| 142 | + json: () => Promise.resolve(mockData) |
| 143 | + } as unknown as Response) |
| 144 | + ); |
| 145 | + vi.stubGlobal("fetch", fetchMock); |
| 146 | + |
| 147 | + const onDone = vi.fn(); |
| 148 | + render(<IndexProgress onDone={onDone} />); |
| 149 | + |
| 150 | + // First poll returns active |
| 151 | + await act(async () => { |
| 152 | + await vi.advanceTimersByTimeAsync(2000); |
| 153 | + }); |
| 154 | + expect(onDone).not.toHaveBeenCalled(); |
| 155 | + |
| 156 | + // Indexing finishes |
| 157 | + mockData = [ |
| 158 | + { slot: 1, status: "done", path: "/path/to/project" } |
| 159 | + ]; |
| 160 | + |
| 161 | + await act(async () => { |
| 162 | + await vi.advanceTimersByTimeAsync(2000); |
| 163 | + }); |
| 164 | + |
| 165 | + expect(onDone).toHaveBeenCalled(); |
| 166 | + }); |
| 167 | + |
| 168 | + it("keeps waiting and does NOT call onDone while the jobs list is empty", async () => { |
| 169 | + // An empty list mid-index means the job is not visible (e.g. transient |
| 170 | + // backend state loss) — it must NOT be treated as successful completion. |
| 171 | + let mockData: { slot: number; status: string; path: string }[] = []; |
| 172 | + const fetchMock = vi.fn().mockImplementation(() => |
| 173 | + Promise.resolve({ |
| 174 | + json: () => Promise.resolve(mockData) |
| 175 | + } as unknown as Response) |
| 176 | + ); |
| 177 | + vi.stubGlobal("fetch", fetchMock); |
| 178 | + |
| 179 | + const onDone = vi.fn(); |
| 180 | + render(<IndexProgress onDone={onDone} />); |
| 181 | + |
| 182 | + // Two empty polls: still waiting, no premature completion |
| 183 | + await act(async () => { |
| 184 | + await vi.advanceTimersByTimeAsync(2000); |
| 185 | + }); |
| 186 | + await act(async () => { |
| 187 | + await vi.advanceTimersByTimeAsync(2000); |
| 188 | + }); |
| 189 | + expect(onDone).not.toHaveBeenCalled(); |
| 190 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 191 | + |
| 192 | + // Job becomes visible and finishes — now completion fires |
| 193 | + mockData = [{ slot: 1, status: "done", path: "/path/to/project" }]; |
| 194 | + await act(async () => { |
| 195 | + await vi.advanceTimersByTimeAsync(2000); |
| 196 | + }); |
| 197 | + expect(onDone).toHaveBeenCalled(); |
| 198 | + }); |
| 199 | + |
| 200 | + it("renders error banner and does NOT call onDone when indexing fails with error status", async () => { |
| 201 | + const fetchMock = vi.fn().mockImplementation(() => |
| 202 | + Promise.resolve({ |
| 203 | + json: () => Promise.resolve([ |
| 204 | + { slot: 1, status: "error", path: "/path/to/failed-project", error: "OOM Error" } |
| 205 | + ]) |
| 206 | + } as unknown as Response) |
| 207 | + ); |
| 208 | + vi.stubGlobal("fetch", fetchMock); |
| 209 | + |
| 210 | + const onDone = vi.fn(); |
| 211 | + render(<IndexProgress onDone={onDone} />); |
| 212 | + |
| 213 | + await act(async () => { |
| 214 | + await vi.advanceTimersByTimeAsync(2000); |
| 215 | + }); |
| 216 | + |
| 217 | + // Error banner should show up |
| 218 | + expect(screen.getByText(messages.en.projects.indexingFailed)).toBeInTheDocument(); |
| 219 | + expect(screen.getByText("/path/to/failed-project")).toBeInTheDocument(); |
| 220 | + expect(screen.getByText("OOM Error")).toBeInTheDocument(); |
| 221 | + |
| 222 | + // onDone should not be called automatically |
| 223 | + expect(onDone).not.toHaveBeenCalled(); |
| 224 | + |
| 225 | + // Click Dismiss button |
| 226 | + const dismissBtn = screen.getByRole("button", { name: messages.en.common.dismiss }); |
| 227 | + expect(dismissBtn).toBeInTheDocument(); |
| 228 | + |
| 229 | + await act(async () => { |
| 230 | + fireEvent.click(dismissBtn); |
| 231 | + }); |
| 232 | + |
| 233 | + // onDone should be called after manual dismissal |
| 234 | + expect(onDone).toHaveBeenCalled(); |
| 235 | + }); |
| 236 | +}); |
0 commit comments