|
| 1 | +import {describe, it, expect} from 'vitest'; |
| 2 | +import { |
| 3 | + filterWorktreesByQuery, |
| 4 | + filterSessionItemsByQuery, |
| 5 | +} from './filterByQuery.js'; |
| 6 | +import {Worktree} from '../types/index.js'; |
| 7 | +import {SessionItem} from './worktreeUtils.js'; |
| 8 | + |
| 9 | +const makeItem = (searchableName: string, path: string): SessionItem => ({ |
| 10 | + worktree: { |
| 11 | + path, |
| 12 | + isMainWorktree: false, |
| 13 | + hasSession: false, |
| 14 | + } as Worktree, |
| 15 | + baseLabel: searchableName, |
| 16 | + searchableName, |
| 17 | + fileChanges: '', |
| 18 | + aheadBehind: '', |
| 19 | + parentBranch: '', |
| 20 | + lastCommitDate: '', |
| 21 | + lengths: { |
| 22 | + base: 0, |
| 23 | + fileChanges: 0, |
| 24 | + aheadBehind: 0, |
| 25 | + parentBranch: 0, |
| 26 | + lastCommitDate: 0, |
| 27 | + }, |
| 28 | +}); |
| 29 | + |
| 30 | +describe('filterWorktreesByQuery', () => { |
| 31 | + const worktrees: Worktree[] = [ |
| 32 | + { |
| 33 | + path: '/repo/feature-a', |
| 34 | + branch: 'feature/a', |
| 35 | + isMainWorktree: false, |
| 36 | + hasSession: false, |
| 37 | + }, |
| 38 | + { |
| 39 | + path: '/repo/main', |
| 40 | + branch: 'main', |
| 41 | + isMainWorktree: true, |
| 42 | + hasSession: false, |
| 43 | + }, |
| 44 | + ]; |
| 45 | + |
| 46 | + it('returns all worktrees when query is empty', () => { |
| 47 | + expect(filterWorktreesByQuery(worktrees, '')).toEqual(worktrees); |
| 48 | + }); |
| 49 | + |
| 50 | + it('matches branch name case-insensitively', () => { |
| 51 | + const result = filterWorktreesByQuery(worktrees, 'FEATURE'); |
| 52 | + expect(result).toHaveLength(1); |
| 53 | + expect(result[0]?.branch).toBe('feature/a'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('matches path', () => { |
| 57 | + const result = filterWorktreesByQuery(worktrees, '/repo/main'); |
| 58 | + expect(result).toHaveLength(1); |
| 59 | + expect(result[0]?.path).toBe('/repo/main'); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('filterSessionItemsByQuery', () => { |
| 64 | + const items: SessionItem[] = [ |
| 65 | + makeItem('feature/a', '/repo/feature-a'), |
| 66 | + makeItem('main (main)', '/repo/main'), |
| 67 | + makeItem('feature/b: my-session', '/repo/feature-b'), |
| 68 | + makeItem('feature/b: other', '/repo/feature-b'), |
| 69 | + ]; |
| 70 | + |
| 71 | + it('returns all items when query is empty', () => { |
| 72 | + expect(filterSessionItemsByQuery(items, '')).toEqual(items); |
| 73 | + }); |
| 74 | + |
| 75 | + it('matches the session name within a worktree', () => { |
| 76 | + const result = filterSessionItemsByQuery(items, 'my-session'); |
| 77 | + expect(result).toHaveLength(1); |
| 78 | + expect(result[0]?.searchableName).toBe('feature/b: my-session'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('matches the (main) indicator', () => { |
| 82 | + const result = filterSessionItemsByQuery(items, '(main)'); |
| 83 | + expect(result).toHaveLength(1); |
| 84 | + expect(result[0]?.searchableName).toBe('main (main)'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('matches branch name case-insensitively', () => { |
| 88 | + const result = filterSessionItemsByQuery(items, 'FEATURE/A'); |
| 89 | + expect(result).toHaveLength(1); |
| 90 | + expect(result[0]?.searchableName).toBe('feature/a'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('matches path', () => { |
| 94 | + const result = filterSessionItemsByQuery(items, '/repo/feature-b'); |
| 95 | + expect(result).toHaveLength(2); |
| 96 | + }); |
| 97 | +}); |
0 commit comments