|
| 1 | +import {signal, type WritableSignal} from '@angular/core'; |
| 2 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 3 | +import {ConfirmationService, MessageService} from '@openng/optimus-ui/api'; |
| 4 | +import {beforeEach, describe, expect, it, vi} from 'vitest'; |
| 5 | + |
| 6 | +import {createQueryClientHarness, flushQueryAsync} from '../../../core/testing/query-testing'; |
| 7 | +import {getTranslocoModule} from '../../../core/testing/transloco-testing'; |
| 8 | +import {type BrowseSelection, type BrowseSelectionState} from '../../../shared/browse/selection'; |
| 9 | +import {AppSettingsService} from '../../../shared/service/app-settings.service'; |
| 10 | +import {UserService} from '../../settings/user-management/user.service'; |
| 11 | +import {type BookSummary} from '../data/book-response.models'; |
| 12 | +import {ShelfDefinitionQueryService} from '../data/shelf-definition-query.service'; |
| 13 | +import {BookDialogHelperService} from '../components/book-browser/book-dialog-helper.service'; |
| 14 | +import {BookBrowseBulkBarComponent} from './book-browse-bulk-bar.component'; |
| 15 | + |
| 16 | +function selectionOf(ids: readonly number[]): BrowseSelection { |
| 17 | + const selected = new Set(ids); |
| 18 | + return { |
| 19 | + state: signal<BrowseSelectionState>({mode: 'explicit', ids: selected}), |
| 20 | + count: signal(ids.length), |
| 21 | + active: signal(true), |
| 22 | + allMatchingSelected: signal(false), |
| 23 | + isSelected: id => selected.has(id), |
| 24 | + toggle: vi.fn(), |
| 25 | + selectAll: vi.fn(), |
| 26 | + clear: vi.fn(), |
| 27 | + pruneDeleted: vi.fn(), |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +function book(id: number, libraryId = 1): BookSummary { |
| 32 | + return {id, libraryId, libraryName: `Library ${libraryId}`}; |
| 33 | +} |
| 34 | + |
| 35 | +function user(permissions: Record<string, boolean>, id = 1) { |
| 36 | + return {id, permissions}; |
| 37 | +} |
| 38 | + |
| 39 | +describe('BookBrowseBulkBarComponent', () => { |
| 40 | + let fixture: ComponentFixture<BookBrowseBulkBarComponent>; |
| 41 | + let currentUser: WritableSignal<ReturnType<typeof user> | null>; |
| 42 | + let appSettings: WritableSignal<{diskType: string}>; |
| 43 | + let shelfDefinitions: {id: number; userId: number; name: string}[]; |
| 44 | + |
| 45 | + function mount(books: BookSummary[], selectedIds: number[]): BookBrowseBulkBarComponent { |
| 46 | + fixture = TestBed.createComponent(BookBrowseBulkBarComponent); |
| 47 | + fixture.componentRef.setInput('selection', selectionOf(selectedIds)); |
| 48 | + fixture.componentRef.setInput('books', books); |
| 49 | + fixture.componentRef.setInput('total', books.length); |
| 50 | + fixture.componentRef.setInput('fetchIds', () => Promise.resolve([])); |
| 51 | + fixture.detectChanges(); |
| 52 | + return fixture.componentInstance; |
| 53 | + } |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + const harness = createQueryClientHarness(); |
| 57 | + currentUser = signal(null); |
| 58 | + appSettings = signal({diskType: 'LOCAL'}); |
| 59 | + shelfDefinitions = []; |
| 60 | + |
| 61 | + TestBed.configureTestingModule({ |
| 62 | + imports: [BookBrowseBulkBarComponent, getTranslocoModule()], |
| 63 | + providers: [ |
| 64 | + ...harness.providers, |
| 65 | + {provide: UserService, useValue: {currentUser}}, |
| 66 | + {provide: AppSettingsService, useValue: {appSettings}}, |
| 67 | + { |
| 68 | + provide: ShelfDefinitionQueryService, |
| 69 | + useValue: { |
| 70 | + definitions: () => ({ |
| 71 | + queryKey: ['shelves', 'query', 'definitions'] as const, |
| 72 | + queryFn: () => Promise.resolve(shelfDefinitions), |
| 73 | + }), |
| 74 | + }, |
| 75 | + }, |
| 76 | + {provide: BookDialogHelperService, useValue: {}}, |
| 77 | + {provide: ConfirmationService, useValue: {confirm: vi.fn()}}, |
| 78 | + {provide: MessageService, useValue: {add: vi.fn()}}, |
| 79 | + ], |
| 80 | + }).overrideComponent(BookBrowseBulkBarComponent, { |
| 81 | + set: {template: '', imports: []}, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('allows only the metadata actions the user is permitted', () => { |
| 86 | + currentUser.set(user({canBulkLockUnlockMetadata: true})); |
| 87 | + const bar = mount([book(1)], [1]); |
| 88 | + |
| 89 | + expect(bar['bulkMetadataAvailable']()).toBe(true); |
| 90 | + expect(bar['bulkPermissions']()).toMatchObject({ |
| 91 | + canLockUnlockMetadata: true, |
| 92 | + canAutoFetchMetadata: false, |
| 93 | + canCustomFetchMetadata: false, |
| 94 | + canRegenerateCover: false, |
| 95 | + }); |
| 96 | + |
| 97 | + currentUser.set(user({ |
| 98 | + canBulkAutoFetchMetadata: true, |
| 99 | + canBulkCustomFetchMetadata: true, |
| 100 | + canBulkRegenerateCover: true, |
| 101 | + })); |
| 102 | + |
| 103 | + expect(bar['bulkPermissions']()).toMatchObject({ |
| 104 | + canLockUnlockMetadata: false, |
| 105 | + canAutoFetchMetadata: true, |
| 106 | + canCustomFetchMetadata: true, |
| 107 | + canRegenerateCover: true, |
| 108 | + }); |
| 109 | + |
| 110 | + currentUser.set(user({})); |
| 111 | + expect(bar['bulkMetadataAvailable']()).toBe(false); |
| 112 | + }); |
| 113 | + |
| 114 | + it('requires permission and one library to attach files, and local disk to organize files', () => { |
| 115 | + currentUser.set(user({canManageLibrary: true})); |
| 116 | + const bar = mount([book(1, 1), book(2, 2)], [1, 2]); |
| 117 | + expect(bar['bulkPermissions']().canAttachFiles).toBe(true); |
| 118 | + expect(bar['bulkAttachEligible']()).toBe(false); |
| 119 | + |
| 120 | + fixture.componentRef.setInput('books', [book(1), book(2)]); |
| 121 | + expect(bar['bulkAttachEligible']()).toBe(true); |
| 122 | + |
| 123 | + fixture.componentRef.setInput('books', [book(1)]); |
| 124 | + expect(bar['bulkAttachEligible']()).toBe(false); |
| 125 | + |
| 126 | + currentUser.set(user({canMoveOrganizeFiles: true})); |
| 127 | + expect(bar['bulkPermissions']().canAttachFiles).toBe(false); |
| 128 | + appSettings.set({diskType: 'S3'}); |
| 129 | + expect(bar['bulkPermissions']().canOrganizeFiles).toBe(false); |
| 130 | + |
| 131 | + appSettings.set({diskType: 'LOCAL'}); |
| 132 | + expect(bar['bulkPermissions']().canOrganizeFiles).toBe(true); |
| 133 | + |
| 134 | + currentUser.set(user({})); |
| 135 | + expect(bar['bulkPermissions']().canOrganizeFiles).toBe(false); |
| 136 | + }); |
| 137 | + |
| 138 | + it('offers only the current user shelves for bulk assignment', async () => { |
| 139 | + currentUser.set(user({}, 7)); |
| 140 | + shelfDefinitions = [ |
| 141 | + {id: 5, userId: 7, name: 'Mine'}, |
| 142 | + {id: 6, userId: 9, name: 'Shared by someone else'}, |
| 143 | + ]; |
| 144 | + const bar = mount([book(1)], [1]); |
| 145 | + await flushQueryAsync(); |
| 146 | + |
| 147 | + expect(bar['bulkShelves']().map(shelf => shelf.id)).toEqual([5]); |
| 148 | + |
| 149 | + currentUser.set(user({}, 9)); |
| 150 | + expect(bar['bulkShelves']().map(shelf => shelf.id)).toEqual([6]); |
| 151 | + }); |
| 152 | +}); |
0 commit comments