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
451 changes: 75 additions & 376 deletions cardinal/src/App.tsx

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions cardinal/src/components/FilesTabContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ type FilesTabContentProps = {
currentQuery: string;
virtualListRef: React.Ref<VirtualListHandle>;
results: SlabIndex[];
// Bumps when backend search results change; invalidates row metadata cache.
dataResultsVersion: number;
// Bumps when visible ordering changes; invalidates viewport-dependent state.
displayedResultsVersion: number;
rowHeight: number;
overscan: number;
Expand Down
2 changes: 2 additions & 0 deletions cardinal/src/components/VirtualList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export type VirtualListHandle = {

type VirtualListProps = {
results: SlabIndex[];
// Raw data version from search responses; drives row-cache resets in useDataLoader.
dataResultsVersion: number;
// Visible ordering/version token; drives viewport/icon hydration refreshes.
displayedResultsVersion: number;
rowHeight: number;
overscan: number;
Expand Down
201 changes: 201 additions & 0 deletions cardinal/src/hooks/__tests__/useAppHotkeys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import { act, renderHook, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { listen } from '@tauri-apps/api/event';
import { invoke } from '@tauri-apps/api/core';
import { openResultPath } from '../../utils/openResultPath';
import { useAppHotkeys } from '../useAppHotkeys';

vi.mock('@tauri-apps/api/event', () => ({
listen: vi.fn(),
}));

vi.mock('@tauri-apps/api/core', () => ({
invoke: vi.fn(),
}));

vi.mock('../../utils/openResultPath', () => ({
openResultPath: vi.fn(),
}));

const mockedListen = vi.mocked(listen);
const mockedInvoke = vi.mocked(invoke);
const mockedOpenResultPath = vi.mocked(openResultPath);

type HookProps = {
activeTab: 'files' | 'events';
selectedPaths: string[];
selectedIndicesRef: { current: number[] };
focusSearchInput: () => void;
navigateSelection: (delta: 1 | -1, options?: { extend?: boolean }) => void;
triggerQuickLook: () => void;
};

describe('useAppHotkeys', () => {
const quickLookUnlisten = vi.fn();
const focusSearchInput = vi.fn();
const navigateSelection = vi.fn();
const triggerQuickLook = vi.fn();

let quickLookListener: ((event: any) => void) | null;

const renderHotkeys = (overrides: Partial<HookProps> = {}) =>
renderHook((props: HookProps) => useAppHotkeys(props), {
initialProps: {
activeTab: 'files',
selectedPaths: ['/tmp/a', '/tmp/b'],
selectedIndicesRef: { current: [0] },
focusSearchInput,
navigateSelection,
triggerQuickLook,
...overrides,
},
});

beforeEach(() => {
vi.clearAllMocks();
quickLookListener = null;
mockedInvoke.mockResolvedValue(undefined);

mockedListen.mockImplementation(async (eventName: string, callback: (event: any) => void) => {
if (eventName === 'quicklook-keydown') {
quickLookListener = callback;
return quickLookUnlisten;
}
return vi.fn();
});
});

it('handles Meta+F, Meta+R, Meta+O, and Meta+C shortcuts on files tab', async () => {
renderHotkeys();

const findEvent = new KeyboardEvent('keydown', {
key: 'f',
metaKey: true,
cancelable: true,
});
act(() => {
window.dispatchEvent(findEvent);
});
expect(focusSearchInput).toHaveBeenCalledTimes(1);
expect(findEvent.defaultPrevented).toBe(true);

const openEvent = new KeyboardEvent('keydown', {
key: 'o',
metaKey: true,
cancelable: true,
});
act(() => {
window.dispatchEvent(openEvent);
});
expect(mockedOpenResultPath).toHaveBeenCalledWith('/tmp/a');
expect(mockedOpenResultPath).toHaveBeenCalledWith('/tmp/b');

const revealEvent = new KeyboardEvent('keydown', {
key: 'r',
metaKey: true,
cancelable: true,
});
act(() => {
window.dispatchEvent(revealEvent);
});
expect(mockedInvoke).toHaveBeenCalledWith('open_in_finder', { path: '/tmp/a' });
expect(mockedInvoke).toHaveBeenCalledWith('open_in_finder', { path: '/tmp/b' });

const copyEvent = new KeyboardEvent('keydown', {
key: 'c',
metaKey: true,
cancelable: true,
});
act(() => {
window.dispatchEvent(copyEvent);
});
expect(mockedInvoke).toHaveBeenCalledWith('copy_files_to_clipboard', {
paths: ['/tmp/a', '/tmp/b'],
});
});

it('handles space and arrow navigation on files tab', () => {
renderHotkeys();

const spaceEvent = new KeyboardEvent('keydown', {
key: ' ',
code: 'Space',
cancelable: true,
});
act(() => {
window.dispatchEvent(spaceEvent);
});
expect(triggerQuickLook).toHaveBeenCalledTimes(1);
expect(spaceEvent.defaultPrevented).toBe(true);

const downEvent = new KeyboardEvent('keydown', {
key: 'ArrowDown',
shiftKey: true,
cancelable: true,
});
act(() => {
window.dispatchEvent(downEvent);
});
expect(navigateSelection).toHaveBeenCalledWith(1, { extend: true });

const upEvent = new KeyboardEvent('keydown', {
key: 'ArrowUp',
cancelable: true,
});
act(() => {
window.dispatchEvent(upEvent);
});
expect(navigateSelection).toHaveBeenCalledWith(-1, { extend: false });
});

it('handles Quick Look native keydown events and cleanup', async () => {
const { rerender, unmount } = renderHotkeys();

await waitFor(() => {
expect(quickLookListener).not.toBeNull();
});

act(() => {
quickLookListener?.({
payload: {
keyCode: 125,
modifiers: {
shift: true,
control: false,
option: false,
command: false,
},
},
});
});
expect(navigateSelection).toHaveBeenCalledWith(1, { extend: true });

rerender({
activeTab: 'events',
selectedPaths: ['/tmp/a', '/tmp/b'],
selectedIndicesRef: { current: [0] },
focusSearchInput,
navigateSelection,
triggerQuickLook,
});

navigateSelection.mockClear();
act(() => {
quickLookListener?.({
payload: {
keyCode: 126,
modifiers: {
shift: false,
control: false,
option: false,
command: false,
},
},
});
});
expect(navigateSelection).not.toHaveBeenCalled();

unmount();
expect(quickLookUnlisten).toHaveBeenCalledTimes(1);
});
});
Loading