From 885f0a91d98c2bbc15ef205906ff5468bca8b9d1 Mon Sep 17 00:00:00 2001 From: XZH Date: Thu, 1 Oct 2026 07:52:30 -0700 Subject: [PATCH] test(history): cover stale search cancellation --- web/tests/chat-history-search.spec.tsx | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/web/tests/chat-history-search.spec.tsx b/web/tests/chat-history-search.spec.tsx index 169da7bc6..1b8255ac7 100644 --- a/web/tests/chat-history-search.spec.tsx +++ b/web/tests/chat-history-search.spec.tsx @@ -1,6 +1,9 @@ import { act, fireEvent, render, screen } from "@testing-library/react"; import { afterEach, expect, it, vi } from "vitest"; import ChatHistorySection from "@/components/space/ChatHistorySection"; +import { searchAllSessions } from "@/lib/session-api"; + +const searchMock = vi.mocked(searchAllSessions); const fixture = vi.hoisted(() => ({ calls: [] as Array<{ query: string; signal?: AbortSignal }>, @@ -128,3 +131,45 @@ it("searches the full account history, then restores the unfiltered index", asyn expect(screen.getByText("Recent chat")).toBeInTheDocument(); expect(screen.queryByText("Older conversation")).toBeNull(); }); + +it("cancels a stale search when the query changes", async () => { + render(); + + await act(async () => { + await Promise.resolve(); + }); + + vi.useFakeTimers(); + searchMock.mockImplementationOnce( + (query: string, signal?: AbortSignal) => { + fixture.calls.push({ query, signal }); + return new Promise((_resolve, reject) => { + signal?.addEventListener( + "abort", + () => reject(new DOMException("Aborted", "AbortError")), + { once: true }, + ); + }); + }, + ); + + const input = screen.getByPlaceholderText("Search chat history..."); + await act(async () => { + fireEvent.change(input, { target: { value: "Bay" } }); + }); + await act(async () => { + vi.advanceTimersByTime(300); + }); + + await act(async () => { + fireEvent.change(input, { target: { value: "Bayes" } }); + }); + await act(async () => { + vi.advanceTimersByTime(300); + }); + + expect(fixture.calls.map((call) => call.query)).toEqual(["Bay", "Bayes"]); + expect(fixture.calls[0].signal?.aborted).toBe(true); + expect(fixture.calls[1].signal?.aborted).toBe(false); + expect(screen.getByText("Older conversation")).toBeInTheDocument(); +});