| import { describe, it, expect } from 'vitest'; |
| import { |
| parseAuthors, |
| createBookGroups, |
| createWithinGroupSorter, |
| createGroupSorter, |
| getGroupSortValue, |
| createBookSorter, |
| ensureLibrarySortByType, |
| ensureLibraryGroupByType, |
| findGroupById, |
| getGroupDisplayName, |
| } from '../../app/library/utils/libraryUtils'; |
| import { Book, BooksGroup } from '../../types/book'; |
| import { LibraryGroupByType, LibrarySortByType } from '../../types/settings'; |
| import { BookMetadata } from '@/libs/document'; |
|
|
| |
| const createMockBook = ( |
| overrides: Partial<Omit<Book, 'metadata'> & { metadata?: Partial<BookMetadata> }> = {}, |
| ): Book => ({ |
| hash: `hash-${Math.random().toString(36).substr(2, 9)}`, |
| format: 'EPUB', |
| title: 'Test Book', |
| author: 'Test Author', |
| createdAt: Date.now(), |
| updatedAt: Date.now(), |
| ...overrides, |
| metadata: { ...overrides.metadata } as BookMetadata, |
| }); |
|
|
| describe('parseAuthors', () => { |
| it('should return single author as array', () => { |
| expect(parseAuthors('John Smith')).toEqual(['John Smith']); |
| }); |
|
|
| it('should split authors by comma', () => { |
| expect(parseAuthors('John Smith, Jane Doe')).toEqual(['John Smith', 'Jane Doe']); |
| }); |
|
|
| it('should split authors by ampersand', () => { |
| expect(parseAuthors('John Smith & Jane Doe')).toEqual(['John Smith', 'Jane Doe']); |
| }); |
|
|
| it('should split authors by "and"', () => { |
| expect(parseAuthors('John Smith and Jane Doe')).toEqual(['John Smith', 'Jane Doe']); |
| }); |
|
|
| it('should handle mixed separators', () => { |
| expect(parseAuthors('John Smith, Jane Doe & Bob Wilson')).toEqual([ |
| 'John Smith', |
| 'Jane Doe', |
| 'Bob Wilson', |
| ]); |
| }); |
|
|
| it('should handle "and" with commas', () => { |
| expect(parseAuthors('John Smith, Jane Doe, and Bob Wilson')).toEqual([ |
| 'John Smith', |
| 'Jane Doe', |
| 'Bob Wilson', |
| ]); |
| }); |
|
|
| it('should trim whitespace from author names', () => { |
| expect(parseAuthors(' John Smith , Jane Doe ')).toEqual(['John Smith', 'Jane Doe']); |
| }); |
|
|
| it('should return empty array for empty string', () => { |
| expect(parseAuthors('')).toEqual([]); |
| }); |
|
|
| it('should return empty array for whitespace only', () => { |
| expect(parseAuthors(' ')).toEqual([]); |
| }); |
|
|
| it('should handle single author with extra spaces', () => { |
| expect(parseAuthors(' John Smith ')).toEqual(['John Smith']); |
| }); |
| }); |
|
|
| describe('createBookGroups', () => { |
| describe('groupBy: none', () => { |
| it('should return all books as flat list', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 1' }), |
| createMockBook({ hash: '2', title: 'Book 2' }), |
| ]; |
|
|
| const result = createBookGroups(books, LibraryGroupByType.None); |
|
|
| expect(result).toHaveLength(2); |
| expect(result.every((item) => 'format' in item)).toBe(true); |
| }); |
|
|
| it('should filter out deleted books', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 1' }), |
| createMockBook({ hash: '2', title: 'Book 2', deletedAt: Date.now() }), |
| ]; |
|
|
| const result = createBookGroups(books, LibraryGroupByType.None); |
|
|
| expect(result).toHaveLength(1); |
| }); |
| }); |
|
|
| describe('groupBy: series', () => { |
| it('should group books by series name', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 1', metadata: { series: 'Series A' } }), |
| createMockBook({ hash: '2', title: 'Book 2', metadata: { series: 'Series A' } }), |
| createMockBook({ hash: '3', title: 'Book 3', metadata: { series: 'Series B' } }), |
| ]; |
|
|
| const result = createBookGroups(books, LibraryGroupByType.Series); |
|
|
| const groups = result.filter((item): item is BooksGroup => 'books' in item); |
| expect(groups).toHaveLength(2); |
|
|
| const seriesA = groups.find((g) => g.name === 'Series A'); |
| expect(seriesA?.books).toHaveLength(2); |
|
|
| const seriesB = groups.find((g) => g.name === 'Series B'); |
| expect(seriesB?.books).toHaveLength(1); |
| }); |
|
|
| it('should leave books without series as ungrouped items', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 1', metadata: { series: 'Series A' } }), |
| createMockBook({ hash: '2', title: 'Book 2' }), |
| createMockBook({ hash: '3', title: 'Book 3', metadata: {} }), |
| ]; |
|
|
| const result = createBookGroups(books, LibraryGroupByType.Series); |
|
|
| const groups = result.filter((item): item is BooksGroup => 'books' in item); |
| const ungrouped = result.filter((item): item is Book => 'format' in item); |
|
|
| expect(groups).toHaveLength(1); |
| expect(ungrouped).toHaveLength(2); |
| }); |
|
|
| it('should handle empty series string as ungrouped', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 1', metadata: { series: '' } }), |
| createMockBook({ hash: '2', title: 'Book 2', metadata: { series: ' ' } }), |
| ]; |
|
|
| const result = createBookGroups(books, LibraryGroupByType.Series); |
|
|
| const groups = result.filter((item): item is BooksGroup => 'books' in item); |
| const ungrouped = result.filter((item): item is Book => 'format' in item); |
|
|
| expect(groups).toHaveLength(0); |
| expect(ungrouped).toHaveLength(2); |
| }); |
|
|
| it('should set group updatedAt to most recent book', () => { |
| const books = [ |
| createMockBook({ |
| hash: '1', |
| title: 'Book 1', |
| metadata: { series: 'Series A' }, |
| updatedAt: 1000, |
| }), |
| createMockBook({ |
| hash: '2', |
| title: 'Book 2', |
| metadata: { series: 'Series A' }, |
| updatedAt: 2000, |
| }), |
| ]; |
|
|
| const result = createBookGroups(books, LibraryGroupByType.Series); |
| const group = result.find((item): item is BooksGroup => 'books' in item); |
|
|
| expect(group?.updatedAt).toBe(2000); |
| }); |
| }); |
|
|
| describe('groupBy: author', () => { |
| it('should group books by author', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 1', author: 'Author A' }), |
| createMockBook({ hash: '2', title: 'Book 2', author: 'Author A' }), |
| createMockBook({ hash: '3', title: 'Book 3', author: 'Author B' }), |
| ]; |
|
|
| const result = createBookGroups(books, LibraryGroupByType.Author); |
|
|
| const groups = result.filter((item): item is BooksGroup => 'books' in item); |
| expect(groups).toHaveLength(2); |
|
|
| const authorA = groups.find((g) => g.name === 'Author A'); |
| expect(authorA?.books).toHaveLength(2); |
| }); |
|
|
| it('should place book in multiple groups for multiple authors', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 1', author: 'John Smith, Jane Doe' }), |
| ]; |
|
|
| const result = createBookGroups(books, LibraryGroupByType.Author); |
|
|
| const groups = result.filter((item): item is BooksGroup => 'books' in item); |
| expect(groups).toHaveLength(2); |
|
|
| const john = groups.find((g) => g.name === 'John Smith'); |
| const jane = groups.find((g) => g.name === 'Jane Doe'); |
|
|
| expect(john?.books).toHaveLength(1); |
| expect(jane?.books).toHaveLength(1); |
| expect(john?.books[0]!.hash).toBe(jane?.books[0]!.hash); |
| }); |
|
|
| it('should leave books without author as ungrouped', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 1', author: 'Author A' }), |
| createMockBook({ hash: '2', title: 'Book 2', author: '' }), |
| createMockBook({ hash: '3', title: 'Book 3', author: ' ' }), |
| ]; |
|
|
| const result = createBookGroups(books, LibraryGroupByType.Author); |
|
|
| const groups = result.filter((item): item is BooksGroup => 'books' in item); |
| const ungrouped = result.filter((item): item is Book => 'format' in item); |
|
|
| expect(groups).toHaveLength(1); |
| expect(ungrouped).toHaveLength(2); |
| }); |
| }); |
|
|
| describe('groupBy: group', () => { |
| it('should return books as-is (group mode handled elsewhere)', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 1' }), |
| createMockBook({ hash: '2', title: 'Book 2' }), |
| ]; |
|
|
| const result = createBookGroups(books, LibraryGroupByType.Group); |
|
|
| |
| expect(result).toHaveLength(2); |
| }); |
| }); |
| }); |
|
|
| describe('createWithinGroupSorter', () => { |
| describe('series grouping', () => { |
| it('should sort by seriesIndex ascending', () => { |
| const books = [ |
| createMockBook({ |
| hash: '1', |
| title: 'Book 3', |
| metadata: { seriesIndex: 3 }, |
| }), |
| createMockBook({ |
| hash: '2', |
| title: 'Book 1', |
| metadata: { seriesIndex: 1 }, |
| }), |
| createMockBook({ |
| hash: '3', |
| title: 'Book 2', |
| metadata: { seriesIndex: 2 }, |
| }), |
| ]; |
|
|
| const sorter = createWithinGroupSorter( |
| LibraryGroupByType.Series, |
| LibrarySortByType.Title, |
| 'en', |
| ); |
| const sorted = [...books].sort(sorter); |
|
|
| expect(sorted[0]!.metadata?.seriesIndex).toBe(1); |
| expect(sorted[1]!.metadata?.seriesIndex).toBe(2); |
| expect(sorted[2]!.metadata?.seriesIndex).toBe(3); |
| }); |
|
|
| it('should place books without seriesIndex after those with index', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book A', metadata: {} }), |
| createMockBook({ hash: '2', title: 'Book B', metadata: { seriesIndex: 1 } }), |
| ]; |
|
|
| const sorter = createWithinGroupSorter( |
| LibraryGroupByType.Series, |
| LibrarySortByType.Title, |
| 'en', |
| ); |
| const sorted = [...books].sort(sorter); |
|
|
| expect(sorted[0]!.hash).toBe('2'); |
| expect(sorted[1]!.hash).toBe('1'); |
| }); |
|
|
| it('should sort books without seriesIndex by global sort criteria', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Zebra', metadata: {} }), |
| createMockBook({ hash: '2', title: 'Apple', metadata: {} }), |
| ]; |
|
|
| const sorter = createWithinGroupSorter( |
| LibraryGroupByType.Series, |
| LibrarySortByType.Title, |
| 'en', |
| ); |
| const sorted = [...books].sort(sorter); |
|
|
| expect(sorted[0]!.title).toBe('Apple'); |
| expect(sorted[1]!.title).toBe('Zebra'); |
| }); |
|
|
| it('should handle decimal seriesIndex values', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 2', metadata: { seriesIndex: 2 } }), |
| createMockBook({ hash: '2', title: 'Book 1.5', metadata: { seriesIndex: 1.5 } }), |
| createMockBook({ hash: '3', title: 'Book 1', metadata: { seriesIndex: 1 } }), |
| ]; |
|
|
| const sorter = createWithinGroupSorter( |
| LibraryGroupByType.Series, |
| LibrarySortByType.Title, |
| 'en', |
| ); |
| const sorted = [...books].sort(sorter); |
|
|
| expect(sorted[0]!.metadata?.seriesIndex).toBe(1); |
| expect(sorted[1]!.metadata?.seriesIndex).toBe(1.5); |
| expect(sorted[2]!.metadata?.seriesIndex).toBe(2); |
| }); |
| }); |
|
|
| describe('author grouping', () => { |
| it('should sort by global sort criteria (title)', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Zebra' }), |
| createMockBook({ hash: '2', title: 'Apple' }), |
| ]; |
|
|
| const sorter = createWithinGroupSorter( |
| LibraryGroupByType.Author, |
| LibrarySortByType.Title, |
| 'en', |
| ); |
| const sorted = [...books].sort(sorter); |
|
|
| expect(sorted[0]!.title).toBe('Apple'); |
| expect(sorted[1]!.title).toBe('Zebra'); |
| }); |
|
|
| it('should sort by global sort criteria (updated)', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Book 1', updatedAt: 2000 }), |
| createMockBook({ hash: '2', title: 'Book 2', updatedAt: 1000 }), |
| ]; |
|
|
| const sorter = createWithinGroupSorter( |
| LibraryGroupByType.Author, |
| LibrarySortByType.Updated, |
| 'en', |
| ); |
| const sorted = [...books].sort(sorter); |
|
|
| expect(sorted[0]!.updatedAt).toBe(1000); |
| expect(sorted[1]!.updatedAt).toBe(2000); |
| }); |
| }); |
| }); |
|
|
| describe('getGroupSortValue', () => { |
| const createMockGroup = (overrides: Partial<BooksGroup> = {}): BooksGroup => ({ |
| id: 'test-group', |
| name: 'Test Group', |
| displayName: 'Test Group', |
| books: [], |
| updatedAt: Date.now(), |
| ...overrides, |
| }); |
|
|
| it('should return group name for title sort', () => { |
| const group = createMockGroup({ name: 'My Series' }); |
| expect(getGroupSortValue(group, LibrarySortByType.Title)).toBe('My Series'); |
| }); |
|
|
| it('should return group name for author sort', () => { |
| const group = createMockGroup({ name: 'John Smith' }); |
| expect(getGroupSortValue(group, LibrarySortByType.Author)).toBe('John Smith'); |
| }); |
|
|
| it('should return group name for format sort', () => { |
| const group = createMockGroup({ name: 'Test Group' }); |
| expect(getGroupSortValue(group, LibrarySortByType.Format)).toBe('Test Group'); |
| }); |
|
|
| it('should return max updatedAt for date read sort', () => { |
| const group = createMockGroup({ |
| books: [ |
| createMockBook({ updatedAt: 1000 }), |
| createMockBook({ updatedAt: 3000 }), |
| createMockBook({ updatedAt: 2000 }), |
| ], |
| }); |
|
|
| expect(getGroupSortValue(group, LibrarySortByType.Updated)).toBe(3000); |
| }); |
|
|
| it('should return max createdAt for date added sort', () => { |
| const group = createMockGroup({ |
| books: [ |
| createMockBook({ createdAt: 1000 }), |
| createMockBook({ createdAt: 3000 }), |
| createMockBook({ createdAt: 2000 }), |
| ], |
| }); |
|
|
| expect(getGroupSortValue(group, LibrarySortByType.Created)).toBe(3000); |
| }); |
|
|
| it('should return max published date for published sort', () => { |
| const group = createMockGroup({ |
| books: [ |
| createMockBook({ metadata: { published: '2020-01-01' } }), |
| createMockBook({ metadata: { published: '2023-06-15' } }), |
| createMockBook({ metadata: { published: '2021-12-31' } }), |
| ], |
| }); |
|
|
| const result = getGroupSortValue(group, LibrarySortByType.Published); |
| expect(result).toBe(new Date('2023-06-15').getTime()); |
| }); |
|
|
| it('should handle missing published dates', () => { |
| const group = createMockGroup({ |
| books: [createMockBook({ metadata: {} }), createMockBook({})], |
| }); |
|
|
| expect(getGroupSortValue(group, LibrarySortByType.Published)).toBe(0); |
| }); |
|
|
| it('should handle empty groups gracefully', () => { |
| const group = createMockGroup({ books: [] }); |
|
|
| |
| expect(getGroupSortValue(group, LibrarySortByType.Title)).toBe('Test Group'); |
| |
| expect(getGroupSortValue(group, LibrarySortByType.Updated)).toBe(0); |
| }); |
| }); |
|
|
| describe('createGroupSorter', () => { |
| const createMockGroup = (overrides: Partial<BooksGroup> = {}): BooksGroup => ({ |
| id: 'test-group', |
| name: 'Test Group', |
| displayName: 'Test Group', |
| books: [], |
| updatedAt: Date.now(), |
| ...overrides, |
| }); |
|
|
| it('should sort groups alphabetically by name for title sort', () => { |
| const groups = [ |
| createMockGroup({ name: 'Zebra Series' }), |
| createMockGroup({ name: 'Apple Series' }), |
| createMockGroup({ name: 'Mango Series' }), |
| ]; |
|
|
| const sorter = createGroupSorter(LibrarySortByType.Title, 'en'); |
| const sorted = [...groups].sort(sorter); |
|
|
| expect(sorted[0]!.name).toBe('Apple Series'); |
| expect(sorted[1]!.name).toBe('Mango Series'); |
| expect(sorted[2]!.name).toBe('Zebra Series'); |
| }); |
|
|
| it('should sort groups by most recent updatedAt for date read sort', () => { |
| const groups = [ |
| createMockGroup({ |
| name: 'Group A', |
| books: [createMockBook({ updatedAt: 1000 })], |
| }), |
| createMockGroup({ |
| name: 'Group B', |
| books: [createMockBook({ updatedAt: 3000 })], |
| }), |
| createMockGroup({ |
| name: 'Group C', |
| books: [createMockBook({ updatedAt: 2000 })], |
| }), |
| ]; |
|
|
| const sorter = createGroupSorter(LibrarySortByType.Updated, 'en'); |
| const sorted = [...groups].sort(sorter); |
|
|
| expect(sorted[0]!.name).toBe('Group A'); |
| expect(sorted[1]!.name).toBe('Group C'); |
| expect(sorted[2]!.name).toBe('Group B'); |
| }); |
|
|
| it('should sort groups by most recent createdAt for date added sort', () => { |
| const groups = [ |
| createMockGroup({ |
| name: 'Group A', |
| books: [createMockBook({ createdAt: 3000 })], |
| }), |
| createMockGroup({ |
| name: 'Group B', |
| books: [createMockBook({ createdAt: 1000 })], |
| }), |
| ]; |
|
|
| const sorter = createGroupSorter(LibrarySortByType.Created, 'en'); |
| const sorted = [...groups].sort(sorter); |
|
|
| expect(sorted[0]!.name).toBe('Group B'); |
| expect(sorted[1]!.name).toBe('Group A'); |
| }); |
|
|
| it('should handle groups with single book', () => { |
| const groups = [ |
| createMockGroup({ |
| name: 'Group A', |
| books: [createMockBook({ updatedAt: 1000 })], |
| }), |
| createMockGroup({ |
| name: 'Group B', |
| books: [createMockBook({ updatedAt: 2000 })], |
| }), |
| ]; |
|
|
| const sorter = createGroupSorter(LibrarySortByType.Updated, 'en'); |
| const sorted = [...groups].sort(sorter); |
|
|
| expect(sorted[0]!.name).toBe('Group A'); |
| expect(sorted[1]!.name).toBe('Group B'); |
| }); |
| }); |
|
|
| describe('createBookSorter', () => { |
| it('should sort by title alphabetically', () => { |
| const books = [ |
| createMockBook({ title: 'Zebra' }), |
| createMockBook({ title: 'Apple' }), |
| createMockBook({ title: 'Mango' }), |
| ]; |
|
|
| const sorter = createBookSorter(LibrarySortByType.Title, 'en'); |
| const sorted = [...books].sort(sorter); |
|
|
| expect(sorted[0]!.title).toBe('Apple'); |
| expect(sorted[1]!.title).toBe('Mango'); |
| expect(sorted[2]!.title).toBe('Zebra'); |
| }); |
|
|
| it('should sort by updatedAt for date read', () => { |
| const books = [ |
| createMockBook({ title: 'Book A', updatedAt: 2000 }), |
| createMockBook({ title: 'Book B', updatedAt: 1000 }), |
| createMockBook({ title: 'Book C', updatedAt: 3000 }), |
| ]; |
|
|
| const sorter = createBookSorter(LibrarySortByType.Updated, 'en'); |
| const sorted = [...books].sort(sorter); |
|
|
| expect(sorted[0]!.title).toBe('Book B'); |
| expect(sorted[1]!.title).toBe('Book A'); |
| expect(sorted[2]!.title).toBe('Book C'); |
| }); |
| }); |
|
|
| describe('grouping and sorting integration', () => { |
| it('should correctly group by series and sort groups by date read', () => { |
| const books = [ |
| createMockBook({ |
| hash: '1', |
| title: 'Old Series Book', |
| metadata: { series: 'Old Series', seriesIndex: 1 }, |
| updatedAt: 1000, |
| }), |
| createMockBook({ |
| hash: '2', |
| title: 'New Series Book 1', |
| metadata: { series: 'New Series', seriesIndex: 1 }, |
| updatedAt: 3000, |
| }), |
| createMockBook({ |
| hash: '3', |
| title: 'New Series Book 2', |
| metadata: { series: 'New Series', seriesIndex: 2 }, |
| updatedAt: 2000, |
| }), |
| ]; |
|
|
| |
| const items = createBookGroups(books, LibraryGroupByType.Series); |
| const groups = items.filter((item): item is BooksGroup => 'books' in item); |
|
|
| |
| const groupSorter = createGroupSorter(LibrarySortByType.Updated, 'en'); |
| groups.sort((a, b) => groupSorter(a, b) * -1); |
|
|
| expect(groups[0]!.name).toBe('New Series'); |
| expect(groups[1]!.name).toBe('Old Series'); |
|
|
| |
| const withinSorter = createWithinGroupSorter( |
| LibraryGroupByType.Series, |
| LibrarySortByType.Updated, |
| 'en', |
| ); |
| groups.forEach((group) => group.books.sort(withinSorter)); |
|
|
| expect(groups[0]!.books[0]!.metadata?.seriesIndex).toBe(1); |
| expect(groups[0]!.books[1]!.metadata?.seriesIndex).toBe(2); |
| }); |
|
|
| it('should correctly group by author and sort within groups by title', () => { |
| const books = [ |
| createMockBook({ hash: '1', title: 'Zebra', author: 'Author A' }), |
| createMockBook({ hash: '2', title: 'Apple', author: 'Author A' }), |
| createMockBook({ hash: '3', title: 'Mango', author: 'Author B' }), |
| ]; |
|
|
| |
| const items = createBookGroups(books, LibraryGroupByType.Author); |
| const groups = items.filter((item): item is BooksGroup => 'books' in item); |
|
|
| |
| const groupSorter = createGroupSorter(LibrarySortByType.Title, 'en'); |
| groups.sort(groupSorter); |
|
|
| expect(groups[0]!.name).toBe('Author A'); |
| expect(groups[1]!.name).toBe('Author B'); |
|
|
| |
| const withinSorter = createWithinGroupSorter( |
| LibraryGroupByType.Author, |
| LibrarySortByType.Title, |
| 'en', |
| ); |
| groups.forEach((group) => group.books.sort(withinSorter)); |
|
|
| expect(groups[0]!.books[0]!.title).toBe('Apple'); |
| expect(groups[0]!.books[1]!.title).toBe('Zebra'); |
| }); |
|
|
| it('should handle ascending/descending sort order', () => { |
| const books = [ |
| createMockBook({ |
| hash: '1', |
| title: 'Series A Book', |
| metadata: { series: 'Series A' }, |
| updatedAt: 1000, |
| }), |
| createMockBook({ |
| hash: '2', |
| title: 'Series B Book', |
| metadata: { series: 'Series B' }, |
| updatedAt: 2000, |
| }), |
| ]; |
|
|
| const items = createBookGroups(books, LibraryGroupByType.Series); |
| const groups = items.filter((item): item is BooksGroup => 'books' in item); |
| const groupSorter = createGroupSorter(LibrarySortByType.Updated, 'en'); |
|
|
| |
| const ascending = [...groups].sort((a, b) => groupSorter(a, b) * 1); |
| expect(ascending[0]!.name).toBe('Series A'); |
|
|
| |
| const descending = [...groups].sort((a, b) => groupSorter(a, b) * -1); |
| expect(descending[0]!.name).toBe('Series B'); |
| }); |
| }); |
|
|
| describe('ensureLibrarySortByType', () => { |
| it('should return valid sort type when value is valid', () => { |
| expect(ensureLibrarySortByType('title', LibrarySortByType.Updated)).toBe( |
| LibrarySortByType.Title, |
| ); |
| expect(ensureLibrarySortByType('author', LibrarySortByType.Updated)).toBe( |
| LibrarySortByType.Author, |
| ); |
| expect(ensureLibrarySortByType('updated', LibrarySortByType.Title)).toBe( |
| LibrarySortByType.Updated, |
| ); |
| expect(ensureLibrarySortByType('created', LibrarySortByType.Updated)).toBe( |
| LibrarySortByType.Created, |
| ); |
| expect(ensureLibrarySortByType('format', LibrarySortByType.Updated)).toBe( |
| LibrarySortByType.Format, |
| ); |
| expect(ensureLibrarySortByType('published', LibrarySortByType.Updated)).toBe( |
| LibrarySortByType.Published, |
| ); |
| }); |
|
|
| it('should return fallback when value is null', () => { |
| expect(ensureLibrarySortByType(null, LibrarySortByType.Updated)).toBe( |
| LibrarySortByType.Updated, |
| ); |
| }); |
|
|
| it('should return fallback when value is undefined', () => { |
| expect(ensureLibrarySortByType(undefined, LibrarySortByType.Title)).toBe( |
| LibrarySortByType.Title, |
| ); |
| }); |
|
|
| it('should return fallback when value is invalid', () => { |
| expect(ensureLibrarySortByType('invalid', LibrarySortByType.Updated)).toBe( |
| LibrarySortByType.Updated, |
| ); |
| expect(ensureLibrarySortByType('random', LibrarySortByType.Title)).toBe( |
| LibrarySortByType.Title, |
| ); |
| }); |
|
|
| it('should return fallback when value is empty string', () => { |
| expect(ensureLibrarySortByType('', LibrarySortByType.Updated)).toBe(LibrarySortByType.Updated); |
| }); |
| }); |
|
|
| describe('ensureLibraryGroupByType', () => { |
| it('should return valid group type when value is valid', () => { |
| expect(ensureLibraryGroupByType('none', LibraryGroupByType.Group)).toBe( |
| LibraryGroupByType.None, |
| ); |
| expect(ensureLibraryGroupByType('group', LibraryGroupByType.None)).toBe( |
| LibraryGroupByType.Group, |
| ); |
| expect(ensureLibraryGroupByType('series', LibraryGroupByType.Group)).toBe( |
| LibraryGroupByType.Series, |
| ); |
| expect(ensureLibraryGroupByType('author', LibraryGroupByType.Group)).toBe( |
| LibraryGroupByType.Author, |
| ); |
| }); |
|
|
| it('should return fallback when value is null', () => { |
| expect(ensureLibraryGroupByType(null, LibraryGroupByType.Group)).toBe(LibraryGroupByType.Group); |
| }); |
|
|
| it('should return fallback when value is undefined', () => { |
| expect(ensureLibraryGroupByType(undefined, LibraryGroupByType.Series)).toBe( |
| LibraryGroupByType.Series, |
| ); |
| }); |
|
|
| it('should return fallback when value is invalid', () => { |
| expect(ensureLibraryGroupByType('invalid', LibraryGroupByType.Group)).toBe( |
| LibraryGroupByType.Group, |
| ); |
| expect(ensureLibraryGroupByType('random', LibraryGroupByType.Author)).toBe( |
| LibraryGroupByType.Author, |
| ); |
| }); |
|
|
| it('should return fallback when value is empty string', () => { |
| expect(ensureLibraryGroupByType('', LibraryGroupByType.Group)).toBe(LibraryGroupByType.Group); |
| }); |
| }); |
|
|
| describe('findGroupById', () => { |
| const createMockGroup = (overrides: Partial<BooksGroup> = {}): BooksGroup => ({ |
| id: 'test-group', |
| name: 'Test Group', |
| displayName: 'Test Group', |
| books: [], |
| updatedAt: Date.now(), |
| ...overrides, |
| }); |
|
|
| it('should find group by id', () => { |
| const items: (Book | BooksGroup)[] = [ |
| createMockBook({ hash: '1' }), |
| createMockGroup({ id: 'group-1', name: 'Group 1' }), |
| createMockGroup({ id: 'group-2', name: 'Group 2' }), |
| ]; |
|
|
| const found = findGroupById(items, 'group-2'); |
| expect(found).toBeDefined(); |
| expect(found!.name).toBe('Group 2'); |
| }); |
|
|
| it('should return undefined when group not found', () => { |
| const items: (Book | BooksGroup)[] = [ |
| createMockBook({ hash: '1' }), |
| createMockGroup({ id: 'group-1', name: 'Group 1' }), |
| ]; |
|
|
| const found = findGroupById(items, 'non-existent'); |
| expect(found).toBeUndefined(); |
| }); |
|
|
| it('should not match books', () => { |
| const items: (Book | BooksGroup)[] = [ |
| createMockBook({ hash: 'group-1' }), |
| createMockGroup({ id: 'group-2', name: 'Group 2' }), |
| ]; |
|
|
| const found = findGroupById(items, 'group-1'); |
| expect(found).toBeUndefined(); |
| }); |
| }); |
|
|
| describe('getGroupDisplayName', () => { |
| const createMockGroup = (overrides: Partial<BooksGroup> = {}): BooksGroup => ({ |
| id: 'test-group', |
| name: 'Test Group', |
| displayName: 'Test Display Name', |
| books: [], |
| updatedAt: Date.now(), |
| ...overrides, |
| }); |
|
|
| it('should return displayName when available', () => { |
| const items: (Book | BooksGroup)[] = [ |
| createMockGroup({ id: 'group-1', name: 'Name', displayName: 'Display Name' }), |
| ]; |
|
|
| expect(getGroupDisplayName(items, 'group-1')).toBe('Display Name'); |
| }); |
|
|
| it('should return name when displayName is empty', () => { |
| const items: (Book | BooksGroup)[] = [ |
| createMockGroup({ id: 'group-1', name: 'Name', displayName: '' }), |
| ]; |
|
|
| expect(getGroupDisplayName(items, 'group-1')).toBe('Name'); |
| }); |
|
|
| it('should return undefined when group not found', () => { |
| const items: (Book | BooksGroup)[] = [createMockGroup({ id: 'group-1', name: 'Name' })]; |
|
|
| expect(getGroupDisplayName(items, 'non-existent')).toBeUndefined(); |
| }); |
| }); |
|
|