|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | | -import { isNavigable } from './entries.js' |
| 2 | +import type { FileEntry } from '@/lib/api' |
| 3 | +import { DEFAULT_SORT, isNavigable, nextSort, visibleEntries, type Sort } from './entries.js' |
| 4 | + |
| 5 | +function entry(name: string, overrides: Partial<FileEntry> = {}): FileEntry { |
| 6 | + return { |
| 7 | + name, |
| 8 | + path: `/x/${name}`, |
| 9 | + type: 'file', |
| 10 | + size: 0, |
| 11 | + modifiedAt: '2026-01-01T00:00:00.000Z', |
| 12 | + mode: 0o644, |
| 13 | + ...overrides, |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +const directory = (name: string, overrides: Partial<FileEntry> = {}) => |
| 18 | + entry(name, { type: 'directory', size: 4096, ...overrides }) |
| 19 | + |
| 20 | +function order(entries: FileEntry[], sort: Sort, options: { filter?: string; showHidden?: boolean } = {}) { |
| 21 | + return visibleEntries(entries, { |
| 22 | + filter: options.filter ?? '', |
| 23 | + showHidden: options.showHidden ?? true, |
| 24 | + sort, |
| 25 | + }).map((item) => item.name) |
| 26 | +} |
3 | 27 |
|
4 | 28 | describe('isNavigable', () => { |
5 | 29 | /** |
@@ -30,3 +54,87 @@ describe('isNavigable', () => { |
30 | 54 | expect(isNavigable({ type: 'file', targetType: 'directory' })).toBe(false) |
31 | 55 | }) |
32 | 56 | }) |
| 57 | + |
| 58 | +describe('nextSort', () => { |
| 59 | + it('opens size and modified on the answer you clicked for', () => { |
| 60 | + // Nobody clicks Size to find the smallest file. |
| 61 | + expect(nextSort(DEFAULT_SORT, 'size')).toEqual({ key: 'size', direction: 'desc' }) |
| 62 | + expect(nextSort(DEFAULT_SORT, 'modified')).toEqual({ key: 'modified', direction: 'desc' }) |
| 63 | + }) |
| 64 | + |
| 65 | + it('opens name forwards', () => { |
| 66 | + expect(nextSort({ key: 'size', direction: 'desc' }, 'name')).toEqual({ key: 'name', direction: 'asc' }) |
| 67 | + }) |
| 68 | + |
| 69 | + it('flips the column already being sorted on', () => { |
| 70 | + expect(nextSort({ key: 'name', direction: 'asc' }, 'name')).toEqual({ key: 'name', direction: 'desc' }) |
| 71 | + expect(nextSort({ key: 'name', direction: 'desc' }, 'name')).toEqual({ key: 'name', direction: 'asc' }) |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe('visibleEntries', () => { |
| 76 | + it('sorts names the way a person reads them, not by code point', () => { |
| 77 | + // Plain localeCompare puts file10 before file2, and a capitalised name |
| 78 | + // into a block of its own above every lowercase one. |
| 79 | + const names = order([entry('file10'), entry('file2'), entry('Photos.txt'), entry('apps.txt')], DEFAULT_SORT) |
| 80 | + expect(names).toEqual(['apps.txt', 'file2', 'file10', 'Photos.txt']) |
| 81 | + }) |
| 82 | + |
| 83 | + it('keeps directories above files in both directions', () => { |
| 84 | + const listing = [entry('a.txt'), directory('zoo'), entry('z.txt'), directory('apps')] |
| 85 | + expect(order(listing, { key: 'name', direction: 'asc' })).toEqual(['apps', 'zoo', 'a.txt', 'z.txt']) |
| 86 | + expect(order(listing, { key: 'name', direction: 'desc' })).toEqual(['zoo', 'apps', 'z.txt', 'a.txt']) |
| 87 | + }) |
| 88 | + |
| 89 | + it('groups a link to a directory with the directories', () => { |
| 90 | + const listing = [entry('a.txt'), entry('data', { type: 'symlink', targetType: 'directory' })] |
| 91 | + expect(order(listing, DEFAULT_SORT)).toEqual(['data', 'a.txt']) |
| 92 | + }) |
| 93 | + |
| 94 | + it('sorts by size, largest first, without reordering the folders', () => { |
| 95 | + // Directories report their own inode size, which the pane draws as an em |
| 96 | + // dash: ordering identical-looking rows by an invisible number reads as a bug. |
| 97 | + const listing = [ |
| 98 | + directory('zoo', { size: 4096 }), |
| 99 | + directory('apps', { size: 40960 }), |
| 100 | + entry('small.txt', { size: 10 }), |
| 101 | + entry('big.bin', { size: 9_000_000 }), |
| 102 | + ] |
| 103 | + expect(order(listing, { key: 'size', direction: 'desc' })).toEqual(['apps', 'zoo', 'big.bin', 'small.txt']) |
| 104 | + expect(order(listing, { key: 'size', direction: 'asc' })).toEqual(['apps', 'zoo', 'small.txt', 'big.bin']) |
| 105 | + }) |
| 106 | + |
| 107 | + it('sorts by modified time, newest first', () => { |
| 108 | + const listing = [ |
| 109 | + entry('old.txt', { modifiedAt: '2020-06-01T00:00:00.000Z' }), |
| 110 | + entry('new.txt', { modifiedAt: '2026-09-01T00:00:00.000Z' }), |
| 111 | + entry('middle.txt', { modifiedAt: '2024-01-01T00:00:00.000Z' }), |
| 112 | + ] |
| 113 | + expect(order(listing, { key: 'modified', direction: 'desc' })).toEqual(['new.txt', 'middle.txt', 'old.txt']) |
| 114 | + }) |
| 115 | + |
| 116 | + it('sorts an unreadable mtime as the epoch instead of poisoning the comparison', () => { |
| 117 | + // A NaN comparison returns NaN for every pair, which leaves the whole |
| 118 | + // listing in arrival order and looks like sorting silently stopped working. |
| 119 | + const listing = [entry('b.txt', { modifiedAt: '' }), entry('a.txt', { modifiedAt: '2026-01-01T00:00:00.000Z' })] |
| 120 | + expect(order(listing, { key: 'modified', direction: 'desc' })).toEqual(['a.txt', 'b.txt']) |
| 121 | + }) |
| 122 | + |
| 123 | + it('breaks ties by name, so both panes draw an equal pair the same way', () => { |
| 124 | + const listing = [entry('b.txt', { size: 10 }), entry('a.txt', { size: 10 })] |
| 125 | + expect(order(listing, { key: 'size', direction: 'desc' })).toEqual(['a.txt', 'b.txt']) |
| 126 | + expect(order(listing, { key: 'modified', direction: 'desc' })).toEqual(['a.txt', 'b.txt']) |
| 127 | + }) |
| 128 | + |
| 129 | + it('still hides dotfiles and honours the filter', () => { |
| 130 | + const listing = [entry('.hidden'), entry('notes.txt'), entry('other.md')] |
| 131 | + expect(order(listing, DEFAULT_SORT, { showHidden: false })).toEqual(['notes.txt', 'other.md']) |
| 132 | + expect(order(listing, DEFAULT_SORT, { showHidden: true, filter: 'HID' })).toEqual(['.hidden']) |
| 133 | + }) |
| 134 | + |
| 135 | + it('leaves the array it was given alone', () => { |
| 136 | + const listing = [entry('b.txt'), entry('a.txt')] |
| 137 | + order(listing, DEFAULT_SORT) |
| 138 | + expect(listing.map((item) => item.name)).toEqual(['b.txt', 'a.txt']) |
| 139 | + }) |
| 140 | +}) |
0 commit comments