diff --git a/src/features/search/__tests__/findWindow.test.ts b/src/features/search/__tests__/findWindow.test.ts index cb7cd33314..6100ed491e 100644 --- a/src/features/search/__tests__/findWindow.test.ts +++ b/src/features/search/__tests__/findWindow.test.ts @@ -78,6 +78,71 @@ describe("Find tool window tabs", () => { }); }); +describe("Find tool window tab limit", () => { + const titles = () => useFindWindow.getState().tabs.map((t) => t.title); + const running = () => { + const cancelled: number[] = []; + let next = 0; + const tracked: FindDeps = { + validate: async () => {}, + start: () => new Promise(() => {}), + cancel: (request) => void cancelled.push(request), + nextRequestId: () => ++next, + }; + return { tracked, cancelled }; + }; + + it("drops the oldest unpinned tab, not a pinned one", async () => { + const { tracked, cancelled } = running(); + const started = () => new Promise((r) => setTimeout(r, 0)); + pinFindTab(openInFindWindow(q("p"), "pinned", { deps: tracked }), true); + await started(); + for (let i = 1; i <= 8; i++) { + openInFindWindow(q(`o${i}`), `other-${i}`, { deps: tracked, newTab: true }); + // oxlint-disable-next-line no-await-in-loop -- each search gets its request before the next tab opens + await started(); + } + expect(titles()).toEqual(["pinned", ...[2, 3, 4, 5, 6, 7, 8].map((i) => `other-${i}`)]); + expect(useFindWindow.getState().tabs[0]?.pinned).toBe(true); + expect(cancelled).toEqual([2]); + }); + + it("keeps the selected tab too when opening in a new tab", () => { + const tabs = Array.from({ length: 8 }, (_, i) => openInFindWindow(q(`t${i}`), `t${i}`, { deps, newTab: true })); + pinFindTab(tabs[0]!, true); + useFindWindow.setState({ active: tabs[1]! }); + openInFindWindow(q("n"), "n", { deps, newTab: true }); + expect(titles()).toEqual(["t0", "t1", "t3", "t4", "t5", "t6", "t7", "n"]); + }); + + it("drops the earliest unpinned search, not one that reused an earlier tab's place", () => { + const tabs = Array.from({ length: 8 }, (_, i) => openInFindWindow(q(`t${i}`), `t${i}`, { deps, newTab: true })); + pinFindTab(tabs[0]!, true); + useFindWindow.setState({ active: tabs[1]! }); + openInFindWindow(q("r"), "reused", { deps }); + useFindWindow.setState({ active: tabs[0]! }); + openInFindWindow(q("n"), "n", { deps }); + expect(titles()).toEqual(["t0", "reused", "t3", "t4", "t5", "t6", "t7", "n"]); + }); + + it("drops the selected unpinned tab before a pinned one when it is the last unpinned", () => { + const tabs = Array.from({ length: 8 }, (_, i) => openInFindWindow(q(`t${i}`), `t${i}`, { deps, newTab: true })); + for (const t of tabs) if (t !== tabs[3]) pinFindTab(t, true); + useFindWindow.setState({ active: tabs[3]! }); + openInFindWindow(q("n"), "n", { deps, newTab: true }); + expect(titles()).toEqual(["t0", "t1", "t2", "t4", "t5", "t6", "t7", "n"]); + }); + + it("drops the oldest tab when none is pinned, or all of them are", async () => { + for (let i = 1; i <= 9; i++) openInFindWindow(q(`a${i}`), `a${i}`, { deps, newTab: true }); + expect(titles()).toEqual([2, 3, 4, 5, 6, 7, 8, 9].map((i) => `a${i}`)); + + for (const t of useFindWindow.getState().tabs) pinFindTab(t.id, true); + openInFindWindow(q("b"), "b", { deps, newTab: true }); + expect(titles()).toEqual([3, 4, 5, 6, 7, 8, 9].map((i) => `a${i}`).concat("b")); + }); +}); + describe("buildFindTree", () => { const files = [file("src/b.ts"), file("a.ts"), file("src/a.ts")]; diff --git a/src/features/search/find/windowStore.ts b/src/features/search/find/windowStore.ts index 3ff98ba2bb..5446a20a55 100644 --- a/src/features/search/find/windowStore.ts +++ b/src/features/search/find/windowStore.ts @@ -74,10 +74,16 @@ export function openInFindWindow( } let tabs = reused ? s.tabs.map((t) => (t === reused ? tab : t)) : [...s.tabs, tab]; while (tabs.length > MAX_TABS) { - const old = tabs[0]!; + // A pinned first tab stays while an unpinned one can go: the earliest search (ids grow; a + // reused tab keeps its place), the selected one only when it is the last unpinned. + const earliest = (list: FindTab[]) => + list.reduce((a, t) => (a && a.id < t.id ? a : t), undefined); + const unpinned = tabs[0]!.pinned ? tabs.filter((t) => !t.pinned && t.id !== id) : []; + const spare = earliest(unpinned.filter((t) => t.id !== s.active)) ?? earliest(unpinned); + const old = spare ?? tabs[0]!; sessions.get(old.id)?.dispose(); sessions.delete(old.id); - tabs = tabs.slice(1); + tabs = tabs.filter((t) => t !== old); } return { tabs, active: id }; });