2026-02-24 23:43:58 +01:00
|
|
|
import { describe, it, expect, vi } from 'vitest'
|
|
|
|
|
import { renderHook } from '@testing-library/react'
|
2026-03-05 18:18:52 +01:00
|
|
|
import { useCommandRegistry, buildTypeCommands, extractVaultTypes, pluralizeType, groupSortKey } from './useCommandRegistry'
|
|
|
|
|
import type { CommandAction } from './useCommandRegistry'
|
2026-02-24 23:43:58 +01:00
|
|
|
|
|
|
|
|
function makeConfig(overrides: Record<string, unknown> = {}) {
|
|
|
|
|
return {
|
2026-03-05 18:18:52 +01:00
|
|
|
activeTabPath: '/vault/test.md',
|
|
|
|
|
entries: [],
|
2026-02-24 23:43:58 +01:00
|
|
|
modifiedCount: 0,
|
|
|
|
|
onQuickOpen: vi.fn(),
|
|
|
|
|
onCreateNote: vi.fn(),
|
2026-02-28 19:13:29 +01:00
|
|
|
onCreateNoteOfType: vi.fn(),
|
2026-02-24 23:43:58 +01:00
|
|
|
onSave: vi.fn(),
|
|
|
|
|
onOpenSettings: vi.fn(),
|
|
|
|
|
onTrashNote: vi.fn(),
|
2026-03-02 11:55:51 +01:00
|
|
|
onRestoreNote: vi.fn(),
|
2026-02-24 23:43:58 +01:00
|
|
|
onArchiveNote: vi.fn(),
|
|
|
|
|
onUnarchiveNote: vi.fn(),
|
|
|
|
|
onCommitPush: vi.fn(),
|
2026-03-05 18:18:52 +01:00
|
|
|
onResolveConflicts: vi.fn(),
|
2026-02-24 23:43:58 +01:00
|
|
|
onSetViewMode: vi.fn(),
|
|
|
|
|
onToggleInspector: vi.fn(),
|
2026-03-05 18:18:52 +01:00
|
|
|
onToggleDiff: vi.fn(),
|
|
|
|
|
onToggleRawEditor: vi.fn(),
|
|
|
|
|
onToggleAIChat: vi.fn(),
|
|
|
|
|
onOpenVault: vi.fn(),
|
|
|
|
|
activeNoteModified: false,
|
2026-02-28 12:41:57 +01:00
|
|
|
onZoomIn: vi.fn(),
|
|
|
|
|
onZoomOut: vi.fn(),
|
|
|
|
|
onZoomReset: vi.fn(),
|
|
|
|
|
zoomLevel: 100,
|
2026-02-24 23:43:58 +01:00
|
|
|
onSelect: vi.fn(),
|
2026-03-02 05:00:29 +01:00
|
|
|
onOpenDailyNote: vi.fn(),
|
2026-03-05 18:18:52 +01:00
|
|
|
onCloseTab: vi.fn(),
|
|
|
|
|
onGoBack: vi.fn(),
|
|
|
|
|
onGoForward: vi.fn(),
|
|
|
|
|
canGoBack: false,
|
|
|
|
|
canGoForward: false,
|
|
|
|
|
onCheckForUpdates: vi.fn(),
|
|
|
|
|
onCreateType: vi.fn(),
|
2026-02-24 23:43:58 +01:00
|
|
|
...overrides,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
function findCommand(commands: CommandAction[], id: string): CommandAction | undefined {
|
|
|
|
|
return commands.find(c => c.id === id)
|
|
|
|
|
}
|
2026-03-02 11:55:51 +01:00
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
describe('useCommandRegistry', () => {
|
|
|
|
|
it('includes resolve-conflicts command in Git group', () => {
|
|
|
|
|
const config = makeConfig()
|
|
|
|
|
const { result } = renderHook(() => useCommandRegistry(config))
|
|
|
|
|
const cmd = findCommand(result.current, 'resolve-conflicts')
|
2026-03-02 18:38:25 +01:00
|
|
|
expect(cmd).toBeDefined()
|
2026-03-05 18:18:52 +01:00
|
|
|
expect(cmd!.group).toBe('Git')
|
|
|
|
|
expect(cmd!.label).toBe('Resolve Conflicts')
|
2026-03-02 18:38:25 +01:00
|
|
|
})
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
it('resolve-conflicts is always enabled', () => {
|
|
|
|
|
const config = makeConfig()
|
|
|
|
|
const { result } = renderHook(() => useCommandRegistry(config))
|
|
|
|
|
const cmd = findCommand(result.current, 'resolve-conflicts')
|
2026-03-02 18:38:25 +01:00
|
|
|
expect(cmd!.enabled).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
it('resolve-conflicts executes onResolveConflicts callback', () => {
|
|
|
|
|
const onResolveConflicts = vi.fn()
|
|
|
|
|
const config = makeConfig({ onResolveConflicts })
|
|
|
|
|
const { result } = renderHook(() => useCommandRegistry(config))
|
|
|
|
|
const cmd = findCommand(result.current, 'resolve-conflicts')
|
|
|
|
|
cmd!.execute()
|
|
|
|
|
expect(onResolveConflicts).toHaveBeenCalled()
|
2026-02-28 12:41:57 +01:00
|
|
|
})
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
it('resolve-conflicts has searchable keywords', () => {
|
|
|
|
|
const config = makeConfig()
|
|
|
|
|
const { result } = renderHook(() => useCommandRegistry(config))
|
|
|
|
|
const cmd = findCommand(result.current, 'resolve-conflicts')
|
|
|
|
|
expect(cmd!.keywords).toContain('conflict')
|
|
|
|
|
expect(cmd!.keywords).toContain('merge')
|
2026-02-28 12:41:57 +01:00
|
|
|
})
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
it('commit-push is enabled when modifiedCount > 0', () => {
|
|
|
|
|
const config = makeConfig({ modifiedCount: 5 })
|
|
|
|
|
const { result } = renderHook(() => useCommandRegistry(config))
|
|
|
|
|
const cmd = findCommand(result.current, 'commit-push')
|
2026-03-02 05:00:29 +01:00
|
|
|
expect(cmd!.enabled).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
it('commit-push is disabled when modifiedCount is 0', () => {
|
|
|
|
|
const config = makeConfig({ modifiedCount: 0 })
|
|
|
|
|
const { result } = renderHook(() => useCommandRegistry(config))
|
|
|
|
|
const cmd = findCommand(result.current, 'commit-push')
|
2026-03-03 02:00:48 +01:00
|
|
|
expect(cmd!.enabled).toBe(false)
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
it('resolve-conflicts stays enabled across rerenders', () => {
|
|
|
|
|
const config = makeConfig()
|
|
|
|
|
const { result, rerender } = renderHook(
|
|
|
|
|
(props) => useCommandRegistry(props),
|
|
|
|
|
{ initialProps: config },
|
2026-03-03 02:00:48 +01:00
|
|
|
)
|
2026-03-05 18:18:52 +01:00
|
|
|
expect(findCommand(result.current, 'resolve-conflicts')!.enabled).toBe(true)
|
2026-03-03 02:00:48 +01:00
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
rerender(makeConfig())
|
|
|
|
|
expect(findCommand(result.current, 'resolve-conflicts')!.enabled).toBe(true)
|
2026-03-05 11:18:53 +01:00
|
|
|
})
|
2026-02-28 19:13:29 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('pluralizeType', () => {
|
2026-03-05 18:18:52 +01:00
|
|
|
it('pluralizes regular types', () => {
|
2026-02-28 19:13:29 +01:00
|
|
|
expect(pluralizeType('Project')).toBe('Projects')
|
|
|
|
|
expect(pluralizeType('Note')).toBe('Notes')
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
it('uses overrides for irregular plurals', () => {
|
2026-02-28 19:13:29 +01:00
|
|
|
expect(pluralizeType('Person')).toBe('People')
|
|
|
|
|
expect(pluralizeType('Responsibility')).toBe('Responsibilities')
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
it('handles sibilant endings', () => {
|
|
|
|
|
expect(pluralizeType('Address')).toBe('Addresses')
|
2026-02-28 19:13:29 +01:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('extractVaultTypes', () => {
|
|
|
|
|
it('returns default types when no entries', () => {
|
2026-03-05 18:18:52 +01:00
|
|
|
expect(extractVaultTypes([])).toEqual(['Event', 'Person', 'Project', 'Note'])
|
2026-02-28 19:13:29 +01:00
|
|
|
})
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
it('extracts unique types from entries', () => {
|
2026-02-28 19:13:29 +01:00
|
|
|
const entries = [
|
2026-03-05 18:18:52 +01:00
|
|
|
{ path: '/a', title: 'A', isA: 'Project' },
|
|
|
|
|
{ path: '/b', title: 'B', isA: 'Project' },
|
|
|
|
|
{ path: '/c', title: 'C', isA: 'Event' },
|
|
|
|
|
] as never[]
|
2026-02-28 19:13:29 +01:00
|
|
|
const types = extractVaultTypes(entries)
|
2026-03-05 18:18:52 +01:00
|
|
|
expect(types).toContain('Project')
|
2026-02-28 19:13:29 +01:00
|
|
|
expect(types).toContain('Event')
|
2026-03-05 18:18:52 +01:00
|
|
|
expect(types).toHaveLength(2)
|
2026-02-28 19:13:29 +01:00
|
|
|
})
|
|
|
|
|
|
2026-03-05 18:18:52 +01:00
|
|
|
it('excludes trashed entries', () => {
|
2026-02-28 19:13:29 +01:00
|
|
|
const entries = [
|
2026-03-05 18:18:52 +01:00
|
|
|
{ path: '/a', title: 'A', isA: 'Project', trashed: true },
|
|
|
|
|
] as never[]
|
|
|
|
|
expect(extractVaultTypes(entries)).toEqual(['Event', 'Person', 'Project', 'Note'])
|
2026-02-28 19:13:29 +01:00
|
|
|
})
|
2026-02-24 23:43:58 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('groupSortKey', () => {
|
2026-03-05 18:18:52 +01:00
|
|
|
it('returns correct order for groups', () => {
|
2026-02-24 23:43:58 +01:00
|
|
|
expect(groupSortKey('Navigation')).toBeLessThan(groupSortKey('Note'))
|
|
|
|
|
expect(groupSortKey('Note')).toBeLessThan(groupSortKey('Git'))
|
|
|
|
|
expect(groupSortKey('Git')).toBeLessThan(groupSortKey('View'))
|
2026-03-05 18:18:52 +01:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('buildTypeCommands', () => {
|
|
|
|
|
it('creates new and list commands for each type', () => {
|
|
|
|
|
const onCreateNoteOfType = vi.fn()
|
|
|
|
|
const onSelect = vi.fn()
|
|
|
|
|
const commands = buildTypeCommands(['Project', 'Event'], onCreateNoteOfType, onSelect)
|
|
|
|
|
expect(commands).toHaveLength(4)
|
|
|
|
|
expect(commands[0].id).toBe('new-project')
|
|
|
|
|
expect(commands[1].id).toBe('list-project')
|
|
|
|
|
expect(commands[2].id).toBe('new-event')
|
|
|
|
|
expect(commands[3].id).toBe('list-event')
|
2026-02-24 23:43:58 +01:00
|
|
|
})
|
|
|
|
|
})
|