File size: 9,332 Bytes
5710dd0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | import { act, renderHook, waitFor } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { ReactNode } from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useFilePicker } from '@/components/inputarea/hooks/useFilePicker';
import { useChatStore } from '@/stores';
const getProjectFiles = vi.fn();
vi.mock('@/services', () => ({
bridge: {
getProjectFiles: (...args: unknown[]) => getProjectFiles(...args),
},
}));
const noop = () => {};
const at = (query: string) => ({ trigger: '@' as const, start: 0, query });
type Token = ReturnType<typeof at> | null;
function createWrapper() {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
return ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
}
beforeEach(() => {
getProjectFiles.mockReset();
getProjectFiles.mockResolvedValue([]);
useChatStore.setState({ isStreaming: false, draftMedia: [] });
});
describe('enabled gating', () => {
it('does not search without an active token', async () => {
renderHook(() => useFilePicker(null, noop, noop, noop), { wrapper: createWrapper() });
await new Promise((r) => setTimeout(r, 150));
expect(getProjectFiles).not.toHaveBeenCalled();
});
});
describe('search', () => {
it('searches with the current query and returns results', async () => {
getProjectFiles.mockResolvedValue([{ name: 'app.ts', path: 'src/app.ts', isDirectory: false }]);
const { result } = renderHook(() => useFilePicker(at('app'), noop, noop, noop), { wrapper: createWrapper() });
await waitFor(() => { expect(result.current.fileItems).toHaveLength(1); });
expect(result.current.fileItems[0]).toMatchObject({ name: 'app.ts', path: 'src/app.ts' });
});
it('caps the displayed items at 50', async () => {
getProjectFiles.mockResolvedValue(
Array.from({ length: 60 }, (_, i) => ({ name: `f${i}.ts`, path: `f${i}.ts`, isDirectory: false })),
);
const { result } = renderHook(() => useFilePicker(at('f'), noop, noop, noop), { wrapper: createWrapper() });
await waitFor(() => { expect(result.current.fileItems).toHaveLength(50); });
});
it('debounces rapid query changes into a single request after 100ms', async () => {
const { rerender } = renderHook(({ token }) => useFilePicker(token, noop, noop, noop), {
initialProps: { token: at('') as Token },
wrapper: createWrapper(),
});
await waitFor(() => { expect(getProjectFiles).toHaveBeenCalledTimes(1); });
getProjectFiles.mockClear();
rerender({ token: at('a') });
rerender({ token: at('ap') });
rerender({ token: at('app') });
expect(getProjectFiles).not.toHaveBeenCalled();
await waitFor(() => { expect(getProjectFiles).toHaveBeenCalledTimes(1); });
expect(getProjectFiles).toHaveBeenCalledWith({ query: 'app' });
});
});
describe('media option', () => {
it('shows the media option only for an empty query', async () => {
const { result, rerender } = renderHook(({ token }) => useFilePicker(token, noop, noop, noop), {
initialProps: { token: at('') as Token },
wrapper: createWrapper(),
});
expect(result.current.showMediaOption).toBe(true);
expect(result.current.fileMenuHeaderCount).toBe(1);
rerender({ token: at('a') });
expect(result.current.showMediaOption).toBe(false);
expect(result.current.fileMenuHeaderCount).toBe(0);
});
it('hides the media option when media cannot be added', () => {
useChatStore.setState({ isStreaming: true });
const { result } = renderHook(() => useFilePicker(at(''), noop, noop, noop), { wrapper: createWrapper() });
expect(result.current.showMediaOption).toBe(false);
expect(result.current.fileMenuHeaderCount).toBe(0);
});
});
describe('keyboard navigation', () => {
const key = (result: { current: ReturnType<typeof useFilePicker> }, k: string) => {
act(() => {
result.current.handleFileMenuKey({ key: k, preventDefault: vi.fn() } as unknown as React.KeyboardEvent);
});
};
it('moves selectedIndex within the list bounds with ArrowDown and ArrowUp', async () => {
getProjectFiles.mockResolvedValue([
{ name: 'a.ts', path: 'a.ts', isDirectory: false },
{ name: 'b.ts', path: 'b.ts', isDirectory: false },
]);
const { result } = renderHook(() => useFilePicker(at('a'), noop, noop, noop), { wrapper: createWrapper() });
await waitFor(() => { expect(result.current.fileItems).toHaveLength(2); });
const maxIndex = result.current.fileMenuHeaderCount + result.current.fileItems.length - 1;
expect(result.current.selectedIndex).toBe(0);
key(result, 'ArrowUp');
expect(result.current.selectedIndex).toBe(0);
key(result, 'ArrowDown');
key(result, 'ArrowDown');
key(result, 'ArrowDown');
expect(result.current.selectedIndex).toBe(maxIndex);
key(result, 'ArrowUp');
expect(result.current.selectedIndex).toBe(maxIndex - 1);
});
it('lets Enter fall through when there is no selectable entry', async () => {
getProjectFiles.mockResolvedValue([]);
const { result } = renderHook(() => useFilePicker(at('zzz'), noop, noop, noop), { wrapper: createWrapper() });
await waitFor(() => { expect(result.current.isLoading).toBe(false); });
let handled = true;
act(() => {
handled = result.current.handleFileMenuKey({ key: 'Enter', preventDefault: vi.fn() } as unknown as React.KeyboardEvent);
});
expect(handled).toBe(false);
});
it('clamps the selection when fresh results shrink the list', async () => {
getProjectFiles.mockResolvedValue([
{ name: 'a.ts', path: 'a.ts', isDirectory: false },
{ name: 'b.ts', path: 'b.ts', isDirectory: false },
]);
const { result, rerender } = renderHook(({ token }) => useFilePicker(token, noop, noop, noop), {
initialProps: { token: at('a') as Token },
wrapper: createWrapper(),
});
await waitFor(() => { expect(result.current.fileItems).toHaveLength(2); });
getProjectFiles.mockResolvedValue([{ name: 'app.ts', path: 'app.ts', isDirectory: false }]);
rerender({ token: at('ap') });
act(() => { result.current.setSelectedIndex(1); });
await waitFor(() => { expect(result.current.fileItems).toHaveLength(1); });
expect(result.current.selectedIndex).toBe(0);
});
it('calls onPickMedia when Enter selects the media option', async () => {
const onPickMedia = vi.fn();
const { result } = renderHook(() => useFilePicker(at(''), noop, onPickMedia, noop), { wrapper: createWrapper() });
expect(result.current.selectedIndex).toBe(0);
key(result, 'Enter');
expect(onPickMedia).toHaveBeenCalledTimes(1);
});
it('ignores confirmation while results are stale for the current query', async () => {
getProjectFiles.mockResolvedValue([{ name: 'a.ts', path: 'src/a.ts', isDirectory: false }]);
const onInsertFile = vi.fn();
const { result, rerender } = renderHook(({ token }) => useFilePicker(token, onInsertFile, noop, noop), {
initialProps: { token: at('a') as Token },
wrapper: createWrapper(),
});
await waitFor(() => { expect(result.current.fileItems).toHaveLength(1); });
let resolveNext: (value: unknown) => void = noop;
getProjectFiles.mockImplementation(() => new Promise((resolve) => { resolveNext = resolve; }));
rerender({ token: at('ap') });
expect(result.current.isStale).toBe(true);
key(result, 'Enter');
act(() => { result.current.handleSelectItem(result.current.fileItems[0]!); });
expect(onInsertFile).not.toHaveBeenCalled();
await waitFor(() => { expect(getProjectFiles).toHaveBeenCalledWith({ query: 'ap' }); });
act(() => { resolveNext([{ name: 'app.ts', path: 'src/app.ts', isDirectory: false }]); });
await waitFor(() => { expect(result.current.fileItems[0]?.name).toBe('app.ts'); });
expect(result.current.isStale).toBe(false);
key(result, 'Enter');
expect(onInsertFile).toHaveBeenCalledWith('src/app.ts');
});
it('calls onInsertFile when Enter selects a file', async () => {
getProjectFiles.mockResolvedValue([{ name: 'a.ts', path: 'src/a.ts', isDirectory: false }]);
const onInsertFile = vi.fn();
const { result } = renderHook(() => useFilePicker(at('a'), onInsertFile, noop, noop), { wrapper: createWrapper() });
await waitFor(() => { expect(result.current.fileItems).toHaveLength(1); });
act(() => {
result.current.setSelectedIndex(result.current.fileMenuHeaderCount);
});
key(result, 'Enter');
expect(onInsertFile).toHaveBeenCalledWith('src/a.ts');
});
it('calls onInsertFile with the directory path when Enter selects a directory', async () => {
getProjectFiles.mockResolvedValue([{ name: 'src', path: 'src', isDirectory: true }]);
const onInsertFile = vi.fn();
const { result } = renderHook(() => useFilePicker(at('sr'), onInsertFile, noop, noop), { wrapper: createWrapper() });
await waitFor(() => { expect(result.current.fileItems).toHaveLength(1); });
act(() => {
result.current.setSelectedIndex(result.current.fileMenuHeaderCount);
});
key(result, 'Enter');
expect(onInsertFile).toHaveBeenCalledWith('src');
});
});
|