From 6f6ff8e9ed5bcf4aedfd681e7bf8e7092bcaea23 Mon Sep 17 00:00:00 2001 From: Marcel Date: Fri, 17 Apr 2026 00:37:18 +0200 Subject: [PATCH] fix(#248): add console.error to typeahead catch block and expose setActiveIndex Co-Authored-By: Claude Sonnet 4.6 --- .../__tests__/useTypeahead.svelte.test.ts | 18 ++++++++++++++++++ frontend/src/lib/hooks/useTypeahead.svelte.ts | 8 +++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/frontend/src/lib/hooks/__tests__/useTypeahead.svelte.test.ts b/frontend/src/lib/hooks/__tests__/useTypeahead.svelte.test.ts index bb053c01..3de2a66d 100644 --- a/frontend/src/lib/hooks/__tests__/useTypeahead.svelte.test.ts +++ b/frontend/src/lib/hooks/__tests__/useTypeahead.svelte.test.ts @@ -66,4 +66,22 @@ describe('createTypeahead', () => { expect(fetchUrl).toHaveBeenCalledTimes(1); expect(fetchUrl).toHaveBeenCalledWith('foo'); }); + + it('fetch error logs to console.error and sets results to empty', async () => { + const fetchUrl = vi.fn().mockRejectedValue(new Error('network error')); + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + const ta = createTypeahead({ fetchUrl, debounceMs: 0 }); + ta.setQuery('foo'); + await vi.advanceTimersByTimeAsync(0); + expect(errorSpy).toHaveBeenCalled(); + expect(ta.results).toEqual([]); + errorSpy.mockRestore(); + }); + + it('setActiveIndex updates activeIndex', () => { + const ta = createTypeahead({ fetchUrl: vi.fn().mockResolvedValue([]) }); + expect(ta.activeIndex).toBe(-1); + ta.setActiveIndex(2); + expect(ta.activeIndex).toBe(2); + }); }); diff --git a/frontend/src/lib/hooks/useTypeahead.svelte.ts b/frontend/src/lib/hooks/useTypeahead.svelte.ts index a090fcd3..f0ac30d7 100644 --- a/frontend/src/lib/hooks/useTypeahead.svelte.ts +++ b/frontend/src/lib/hooks/useTypeahead.svelte.ts @@ -23,7 +23,8 @@ export function createTypeahead(options: Options) { loading = true; try { results = await fetchUrl(q); - } catch { + } catch (e) { + console.error('typeahead fetch error', e); results = []; } finally { loading = false; @@ -36,6 +37,10 @@ export function createTypeahead(options: Options) { activeIndex = -1; } + function setActiveIndex(idx: number) { + activeIndex = idx; + } + function select(item: T) { onSelect?.(item); close(); @@ -64,6 +69,7 @@ export function createTypeahead(options: Options) { return activeIndex; }, setQuery, + setActiveIndex, close, select, openWith