Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions server/webSearchService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ describe("WebSearchService", () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
});

it("returns false when health endpoint hangs and the request times out", async () => {
vi.useFakeTimers();
try {
(global.fetch as MockedFunction<typeof fetch>).mockImplementation(
(_input: string | Request | URL, init?: RequestInit) =>
new Promise((_resolve, reject) => {
init?.signal?.addEventListener("abort", () =>
reject(new Error("Aborted")),
);
}),
);

const promise = getWebSearchStatus();
await vi.advanceTimersByTimeAsync(2000);
const status = await promise;
expect(status).toBe(false);
} finally {
vi.useRealTimers();
}
});

it("should return empty array on fetchSearXNG error", async () => {
(global.fetch as MockedFunction<typeof fetch>).mockRejectedValue(
new Error("Network failure"),
Expand Down
4 changes: 3 additions & 1 deletion server/webSearchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export async function startWebSearchService() {

export async function getWebSearchStatus() {
try {
const response = await fetch(`${SERVICE_BASE_URL}/healthz`);
const response = await fetch(`${SERVICE_BASE_URL}/healthz`, {
signal: AbortSignal.timeout(2000),
});
const responseText = await response.text();
return responseText.trim() === "OK";
} catch {
Expand Down