|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { buildFlyoutEntries } from '@/app/workspace/[workspaceId]/components/folders/flyout-entries' |
| 6 | + |
| 7 | +function folder(id: string, name: string, parentId: string | null, updatedAt: string) { |
| 8 | + return { id, name, parentId, updatedAt: new Date(updatedAt) } |
| 9 | +} |
| 10 | + |
| 11 | +function item(id: string, name: string, folderId: string | null, updatedAt: string) { |
| 12 | + return { id, name, folderId, updatedAt: new Date(updatedAt) } |
| 13 | +} |
| 14 | + |
| 15 | +const NONE: ReadonlySet<string> = new Set() |
| 16 | + |
| 17 | +function build( |
| 18 | + folders: ReturnType<typeof folder>[], |
| 19 | + items: ReturnType<typeof item>[], |
| 20 | + pinned?: { folders?: ReadonlySet<string>; items?: ReadonlySet<string> } |
| 21 | +) { |
| 22 | + return buildFlyoutEntries({ |
| 23 | + folders, |
| 24 | + items, |
| 25 | + pinnedFolderIds: pinned?.folders ?? NONE, |
| 26 | + pinnedItemIds: pinned?.items ?? NONE, |
| 27 | + hrefForItem: (row) => `/x/${row.id}`, |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +describe('buildFlyoutEntries', () => { |
| 32 | + it('orders folders and items together, most-recently-updated first', () => { |
| 33 | + const entries = build( |
| 34 | + [ |
| 35 | + folder('f1', 'Older folder', null, '2026-01-01'), |
| 36 | + folder('f2', 'Newest', null, '2026-03-01'), |
| 37 | + ], |
| 38 | + [item('i1', 'Middle', null, '2026-02-01')] |
| 39 | + ) |
| 40 | + |
| 41 | + expect(entries.map((entry) => entry.id)).toEqual(['f2', 'i1', 'f1']) |
| 42 | + }) |
| 43 | + |
| 44 | + it('floats pinned rows above newer unpinned ones, matching the list pages', () => { |
| 45 | + const entries = build( |
| 46 | + [folder('f1', 'Folder', null, '2026-03-01')], |
| 47 | + [item('i1', 'Pinned', null, '2026-01-01'), item('i2', 'Newest', null, '2026-04-01')], |
| 48 | + { items: new Set(['i1']) } |
| 49 | + ) |
| 50 | + |
| 51 | + expect(entries.map((entry) => entry.id)).toEqual(['i1', 'i2', 'f1']) |
| 52 | + }) |
| 53 | + |
| 54 | + it('breaks ties on name', () => { |
| 55 | + const entries = build( |
| 56 | + [], |
| 57 | + [ |
| 58 | + item('b', 'Beta', null, '2026-01-01'), |
| 59 | + item('c', 'Alpha', null, '2026-01-01'), |
| 60 | + item('a', 'Gamma', null, '2026-01-01'), |
| 61 | + ] |
| 62 | + ) |
| 63 | + |
| 64 | + expect(entries.map((entry) => entry.id)).toEqual(['c', 'b', 'a']) |
| 65 | + }) |
| 66 | + |
| 67 | + it('nests items under their folder and links each one', () => { |
| 68 | + const entries = build( |
| 69 | + [folder('f1', 'Reports', null, '2026-01-01'), folder('f2', 'Q1', 'f1', '2026-01-02')], |
| 70 | + [item('i1', 'Revenue', 'f2', '2026-01-03')] |
| 71 | + ) |
| 72 | + |
| 73 | + expect(entries).toEqual([ |
| 74 | + { |
| 75 | + kind: 'folder', |
| 76 | + id: 'f1', |
| 77 | + name: 'Reports', |
| 78 | + children: [ |
| 79 | + { |
| 80 | + kind: 'folder', |
| 81 | + id: 'f2', |
| 82 | + name: 'Q1', |
| 83 | + children: [{ kind: 'item', id: 'i1', name: 'Revenue', href: '/x/i1' }], |
| 84 | + }, |
| 85 | + ], |
| 86 | + }, |
| 87 | + ]) |
| 88 | + }) |
| 89 | + |
| 90 | + it('hoists a folder and an item whose parent folder is gone to the root', () => { |
| 91 | + const entries = build( |
| 92 | + [folder('f1', 'Orphan', 'archived-folder', '2026-01-02')], |
| 93 | + [item('i1', 'Loose', 'archived-folder', '2026-01-01')] |
| 94 | + ) |
| 95 | + |
| 96 | + expect(entries.map((entry) => entry.id)).toEqual(['f1', 'i1']) |
| 97 | + expect(entries[0]).toMatchObject({ kind: 'folder', children: [] }) |
| 98 | + }) |
| 99 | + |
| 100 | + it('drops folders reachable only through a parent cycle instead of descending it', () => { |
| 101 | + const entries = build( |
| 102 | + [ |
| 103 | + folder('a', 'A', 'b', '2026-01-01'), |
| 104 | + folder('b', 'B', 'a', '2026-01-01'), |
| 105 | + folder('root', 'Root', null, '2026-01-01'), |
| 106 | + ], |
| 107 | + [] |
| 108 | + ) |
| 109 | + |
| 110 | + expect(entries.map((entry) => entry.id)).toEqual(['root']) |
| 111 | + }) |
| 112 | + |
| 113 | + it('accepts serialized date strings and sorts undated rows last', () => { |
| 114 | + const entries = buildFlyoutEntries({ |
| 115 | + folders: [], |
| 116 | + items: [ |
| 117 | + { id: 'i1', name: 'Undated', folderId: null, updatedAt: 'not-a-date' }, |
| 118 | + { id: 'i2', name: 'Dated', folderId: null, updatedAt: '2026-01-01T00:00:00.000Z' }, |
| 119 | + ], |
| 120 | + pinnedFolderIds: NONE, |
| 121 | + pinnedItemIds: NONE, |
| 122 | + hrefForItem: (row) => `/x/${row.id}`, |
| 123 | + }) |
| 124 | + |
| 125 | + expect(entries.map((entry) => entry.id)).toEqual(['i2', 'i1']) |
| 126 | + }) |
| 127 | + |
| 128 | + it('treats a missing folderId as the root', () => { |
| 129 | + const entries = buildFlyoutEntries({ |
| 130 | + folders: [], |
| 131 | + items: [{ id: 'i1', name: 'Rootless', updatedAt: new Date('2026-01-01') }], |
| 132 | + pinnedFolderIds: NONE, |
| 133 | + pinnedItemIds: NONE, |
| 134 | + hrefForItem: (row) => `/x/${row.id}`, |
| 135 | + }) |
| 136 | + |
| 137 | + expect(entries).toEqual([{ kind: 'item', id: 'i1', name: 'Rootless', href: '/x/i1' }]) |
| 138 | + }) |
| 139 | +}) |
0 commit comments