| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077 |
- /**
- * Tests for the FileManagerPage component.
- */
- import { describe, it, expect, beforeEach, vi } from 'vitest';
- import { screen, waitFor } from '@testing-library/react';
- import userEvent from '@testing-library/user-event';
- import { render } from '../utils';
- import { FileManagerPage } from '../../pages/FileManagerPage';
- import { http, HttpResponse } from 'msw';
- import { server } from '../mocks/server';
- // Mock data
- const mockFolders = [
- {
- id: 1,
- name: 'Functional Parts',
- parent_id: null,
- file_count: 5,
- project_id: null,
- archive_id: null,
- project_name: null,
- archive_name: null,
- children: [
- {
- id: 2,
- name: 'Brackets',
- parent_id: 1,
- file_count: 3,
- project_id: null,
- archive_id: null,
- project_name: null,
- archive_name: null,
- children: [],
- },
- ],
- },
- {
- id: 3,
- name: 'Art Projects',
- parent_id: null,
- file_count: 2,
- project_id: 1,
- archive_id: null,
- project_name: 'My Art Project',
- archive_name: null,
- children: [],
- },
- ];
- const mockFiles = [
- {
- id: 1,
- filename: 'benchy.gcode.3mf',
- file_path: '/library/benchy.gcode.3mf',
- file_size: 1048576,
- file_type: '3mf',
- folder_id: null,
- thumbnail_path: '/thumbnails/1.png',
- print_name: 'Benchy',
- print_time_seconds: 3600,
- print_count: 5,
- duplicate_count: 0,
- created_at: '2024-01-01T00:00:00Z',
- },
- {
- id: 2,
- filename: 'bracket.stl',
- file_path: '/library/bracket.stl',
- file_size: 524288,
- file_type: 'stl',
- folder_id: null,
- thumbnail_path: null,
- print_name: null,
- print_time_seconds: null,
- print_count: 0,
- duplicate_count: 2,
- created_at: '2024-01-02T00:00:00Z',
- },
- {
- id: 3,
- filename: 'cube.gcode.3mf',
- file_path: '/library/cube.gcode.3mf',
- file_size: 2048576,
- file_type: '3mf',
- folder_id: null,
- thumbnail_path: '/thumbnails/3.png',
- print_name: 'Cube',
- print_time_seconds: 1800,
- print_count: 2,
- duplicate_count: 0,
- created_at: '2024-01-03T00:00:00Z',
- },
- ];
- const mockStats = {
- total_files: 10,
- total_folders: 3,
- total_size_bytes: 104857600,
- disk_free_bytes: 10737418240,
- disk_total_bytes: 107374182400,
- };
- describe('FileManagerPage', () => {
- beforeEach(() => {
- // Clear localStorage to ensure consistent view mode
- localStorage.clear();
- server.use(
- http.get('/api/v1/library/folders', () => {
- return HttpResponse.json(mockFolders);
- }),
- http.get('/api/v1/library/files', () => {
- return HttpResponse.json(mockFiles);
- }),
- http.get('/api/v1/library/stats', () => {
- return HttpResponse.json(mockStats);
- }),
- http.get('/api/v1/settings/', () => {
- return HttpResponse.json({
- check_updates: false,
- check_printer_firmware: false,
- library_disk_warning_gb: 5,
- });
- }),
- http.post('/api/v1/library/folders', async ({ request }) => {
- const body = await request.json() as { name: string };
- return HttpResponse.json({ id: 4, name: body.name, parent_id: null, children: [] });
- }),
- http.delete('/api/v1/library/folders/:id', () => {
- return HttpResponse.json({ success: true });
- }),
- http.delete('/api/v1/library/files/:id', () => {
- return HttpResponse.json({ success: true });
- }),
- http.post('/api/v1/library/files/move', () => {
- return HttpResponse.json({ success: true });
- }),
- http.post('/api/v1/library/files/add-to-queue', () => {
- return HttpResponse.json({ added: [{ file_id: 1, queue_id: 1 }], errors: [] });
- }),
- http.get('/api/v1/projects/', () => {
- return HttpResponse.json([{ id: 1, name: 'Test Project', color: '#00ae42' }]);
- }),
- http.get('/api/v1/archives/', () => {
- return HttpResponse.json([{ id: 1, print_name: 'Test Archive', filename: 'test.3mf' }]);
- })
- );
- });
- describe('rendering', () => {
- it('renders the page title', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('File Manager')).toBeInTheDocument();
- });
- });
- it('renders the page description', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Organize and manage your print files')).toBeInTheDocument();
- });
- });
- it('shows New Folder button', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('New Folder')).toBeInTheDocument();
- });
- });
- it('shows Upload button', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Upload')).toBeInTheDocument();
- });
- });
- });
- describe('stats display', () => {
- it('shows file count', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Files:')).toBeInTheDocument();
- expect(screen.getByText('10')).toBeInTheDocument();
- });
- });
- it('shows folder count', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Folders:')).toBeInTheDocument();
- // Folder count appears multiple places, just verify the label is present
- const foldersLabel = screen.getByText('Folders:');
- expect(foldersLabel.nextElementSibling?.textContent).toBe('3');
- });
- });
- it('shows total size', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Size:')).toBeInTheDocument();
- expect(screen.getByText('100.0 MB')).toBeInTheDocument();
- });
- });
- it('shows free space', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Free:')).toBeInTheDocument();
- });
- });
- });
- describe('folder sidebar', () => {
- it('shows All Files option', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('All Files')).toBeInTheDocument();
- });
- });
- it('shows folder tree', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Functional Parts')).toBeInTheDocument();
- expect(screen.getByText('Art Projects')).toBeInTheDocument();
- });
- });
- it('shows nested folders', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Brackets')).toBeInTheDocument();
- });
- });
- it('shows linked folder indicator', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- // Art Projects has a project_id
- expect(screen.getByText('Art Projects')).toBeInTheDocument();
- });
- });
- });
- describe('file display', () => {
- it('shows files in grid', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Benchy')).toBeInTheDocument();
- });
- });
- it('shows file type badges', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- // File type badges show uppercase type
- expect(screen.getAllByText('3MF').length).toBeGreaterThan(0);
- expect(screen.getAllByText('STL').length).toBeGreaterThan(0);
- });
- });
- it('shows print count', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Printed 5x')).toBeInTheDocument();
- });
- });
- it('shows duplicate badge', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- // Duplicate badge shows count, there may be multiple "2"s on the page
- // so we check that at least one element with "2" exists
- const elements = screen.getAllByText('2');
- expect(elements.length).toBeGreaterThan(0);
- });
- });
- });
- describe('view modes', () => {
- it('has grid view button', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByTitle('Grid view')).toBeInTheDocument();
- });
- });
- it('has list view button', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByTitle('List view')).toBeInTheDocument();
- });
- });
- it('can switch to list view', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- // Wait for files to load first
- await waitFor(() => {
- expect(screen.getByText('Benchy')).toBeInTheDocument();
- });
- // Both view mode buttons should be present and clickable
- const gridButton = screen.getByTitle('Grid view');
- const listButton = screen.getByTitle('List view');
- expect(gridButton).toBeInTheDocument();
- expect(listButton).toBeInTheDocument();
- // Click list view button - verify no errors occur
- await user.click(listButton);
- // Clicking grid button should also work
- await user.click(gridButton);
- // Verify files are still displayed after toggling
- expect(screen.getByText('Benchy')).toBeInTheDocument();
- });
- });
- describe('search and filter', () => {
- it('has search input', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByPlaceholderText('Search files...')).toBeInTheDocument();
- });
- });
- it('has type filter', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('All types')).toBeInTheDocument();
- });
- });
- it('has sort options', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- // Sort dropdown should show Name as default option (persisted to localStorage)
- expect(screen.getByDisplayValue('Name')).toBeInTheDocument();
- });
- });
- });
- describe('selection', () => {
- it('shows select all button', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Select All')).toBeInTheDocument();
- });
- });
- it('can select files', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Benchy')).toBeInTheDocument();
- });
- // Click on the file card to select it
- const fileCard = screen.getByText('Benchy').closest('div[class*="cursor-pointer"]');
- if (fileCard) {
- await user.click(fileCard);
- }
- await waitFor(() => {
- expect(screen.getByText('1 selected')).toBeInTheDocument();
- });
- });
- it('shows bulk actions when files selected', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Select All')).toBeInTheDocument();
- });
- await user.click(screen.getByText('Select All'));
- await waitFor(() => {
- expect(screen.getByText('Move')).toBeInTheDocument();
- expect(screen.getByText('Delete')).toBeInTheDocument();
- });
- });
- });
- describe('new folder modal', () => {
- it('opens new folder modal', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('New Folder')).toBeInTheDocument();
- });
- await user.click(screen.getByText('New Folder'));
- await waitFor(() => {
- expect(screen.getByText('Folder Name')).toBeInTheDocument();
- expect(screen.getByPlaceholderText('e.g., Functional Parts')).toBeInTheDocument();
- });
- });
- it('can create a folder', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('New Folder')).toBeInTheDocument();
- });
- await user.click(screen.getByText('New Folder'));
- await waitFor(() => {
- expect(screen.getByPlaceholderText('e.g., Functional Parts')).toBeInTheDocument();
- });
- const input = screen.getByPlaceholderText('e.g., Functional Parts');
- await user.type(input, 'My New Folder');
- const createButton = screen.getByRole('button', { name: 'Create' });
- await user.click(createButton);
- // Modal should close after creation
- await waitFor(() => {
- expect(screen.queryByText('Folder Name')).not.toBeInTheDocument();
- });
- });
- });
- describe('empty state', () => {
- it('shows empty state when no files', async () => {
- server.use(
- http.get('/api/v1/library/files', () => {
- return HttpResponse.json([]);
- })
- );
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('No files yet')).toBeInTheDocument();
- expect(screen.getByText('Upload Files')).toBeInTheDocument();
- });
- });
- });
- describe('schedule print', () => {
- it('shows schedule print button when one sliced file is selected', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Benchy')).toBeInTheDocument();
- });
- // Select a sliced file (benchy.gcode.3mf) by clicking on its card
- const fileCard = screen.getByText('Benchy').closest('div[class*="cursor-pointer"]');
- if (fileCard) {
- await user.click(fileCard);
- }
- await waitFor(() => {
- expect(screen.getByText(/Schedule/)).toBeInTheDocument();
- });
- });
- it('hides schedule print button when multiple files are selected', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Select All')).toBeInTheDocument();
- });
- // Select all files
- await user.click(screen.getByText('Select All'));
- await waitFor(() => {
- // Schedule button should not be present when multiple files are selected
- expect(screen.queryByText(/Schedule/)).not.toBeInTheDocument();
- });
- });
- });
- describe('STL thumbnail generation', () => {
- it('shows Generate Thumbnails button', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
- });
- });
- it('Generate Thumbnails button has correct title', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- const button = screen.getByTitle('Generate thumbnails for STL files missing them');
- expect(button).toBeInTheDocument();
- });
- });
- it('can click Generate Thumbnails button', async () => {
- const user = userEvent.setup();
- server.use(
- http.post('/api/v1/library/generate-stl-thumbnails', () => {
- return HttpResponse.json({
- processed: 1,
- succeeded: 1,
- failed: 0,
- results: [{ file_id: 2, success: true }],
- });
- })
- );
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
- });
- const button = screen.getByText('Generate Thumbnails');
- await user.click(button);
- // Button should work without error
- await waitFor(() => {
- expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
- });
- });
- it('shows STL file without thumbnail in file list', async () => {
- render(<FileManagerPage />);
- await waitFor(() => {
- // bracket.stl has no thumbnail_path
- expect(screen.getByText('bracket.stl')).toBeInTheDocument();
- expect(screen.getAllByText('STL').length).toBeGreaterThan(0);
- });
- });
- });
- describe('upload modal (FileUploadModal)', () => {
- it('opens upload modal when Upload button is clicked', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Upload')).toBeInTheDocument();
- });
- await user.click(screen.getByText('Upload'));
- await waitFor(() => {
- expect(screen.getByText('Upload Files')).toBeInTheDocument();
- expect(screen.getByText(/Drag & drop/)).toBeInTheDocument();
- });
- });
- it('closes upload modal when Cancel is clicked', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Upload')).toBeInTheDocument();
- });
- await user.click(screen.getByText('Upload'));
- await waitFor(() => {
- expect(screen.getByText('Upload Files')).toBeInTheDocument();
- });
- await user.click(screen.getByRole('button', { name: 'Cancel' }));
- await waitFor(() => {
- expect(screen.queryByText('Upload Files')).not.toBeInTheDocument();
- });
- });
- it('shows 3MF extraction info when 3MF file is added', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Upload')).toBeInTheDocument();
- });
- await user.click(screen.getByText('Upload'));
- await waitFor(() => {
- expect(screen.getByText('Upload Files')).toBeInTheDocument();
- });
- const threemfFile = new File(['content'], 'model.gcode.3mf', { type: 'application/octet-stream' });
- const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
- expect(fileInput).toBeInTheDocument();
- await user.upload(fileInput, threemfFile);
- await waitFor(() => {
- expect(screen.getByText('3MF files detected')).toBeInTheDocument();
- expect(screen.getByText(/Printer model.*will be automatically extracted/i)).toBeInTheDocument();
- });
- });
- it('shows STL thumbnail option when STL file is added', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Upload')).toBeInTheDocument();
- });
- await user.click(screen.getByText('Upload'));
- await waitFor(() => {
- expect(screen.getByText('Upload Files')).toBeInTheDocument();
- });
- const stlFile = new File(['solid test'], 'model.stl', { type: 'application/sla' });
- const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
- expect(fileInput).toBeInTheDocument();
- await user.upload(fileInput, stlFile);
- await waitFor(() => {
- expect(screen.getByText('STL thumbnail generation')).toBeInTheDocument();
- expect(screen.getByText(/Thumbnails can be generated/i)).toBeInTheDocument();
- });
- });
- it('shows ZIP options when ZIP file is added', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Upload')).toBeInTheDocument();
- });
- await user.click(screen.getByText('Upload'));
- await waitFor(() => {
- expect(screen.getByText('Upload Files')).toBeInTheDocument();
- });
- const zipFile = new File(['pk'], 'models.zip', { type: 'application/zip' });
- const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
- await user.upload(fileInput, zipFile);
- await waitFor(() => {
- expect(screen.getByText('ZIP files detected')).toBeInTheDocument();
- expect(screen.getByText(/Preserve folder structure/)).toBeInTheDocument();
- });
- });
- it('can add a file via the file input', async () => {
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Upload')).toBeInTheDocument();
- });
- await user.click(screen.getByText('Upload'));
- await waitFor(() => {
- expect(screen.getByText('Upload Files')).toBeInTheDocument();
- });
- const file = new File(['content'], 'model.3mf', { type: 'application/octet-stream' });
- const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
- await user.upload(fileInput, file);
- await waitFor(() => {
- expect(screen.getByText('model.3mf')).toBeInTheDocument();
- expect(screen.getByRole('button', { name: /Upload \(1\)/i })).toBeInTheDocument();
- });
- });
- it('uploads file and refreshes file list', async () => {
- server.use(
- http.post('/api/v1/library/files', () => {
- return HttpResponse.json({
- id: 10,
- filename: 'uploaded.3mf',
- file_type: '3mf',
- file_size: 1024,
- thumbnail_path: null,
- duplicate_of: null,
- metadata: null,
- });
- })
- );
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Upload')).toBeInTheDocument();
- });
- await user.click(screen.getByText('Upload'));
- await waitFor(() => {
- expect(screen.getByText('Upload Files')).toBeInTheDocument();
- });
- const file = new File(['content'], 'uploaded.3mf', { type: 'application/octet-stream' });
- const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
- await user.upload(fileInput, file);
- const uploadButton = screen.getByRole('button', { name: /Upload \(1\)/i });
- await user.click(uploadButton);
- // Modal should auto-close after upload completes
- await waitFor(() => {
- expect(screen.queryByText('Upload Files')).not.toBeInTheDocument();
- });
- });
- });
- describe('authentication-based UI changes', () => {
- it('hides "Uploaded By" column and user filter when auth is disabled', async () => {
- // Mock auth disabled (default)
- server.use(
- http.get('*/api/v1/auth/status', () => {
- return HttpResponse.json({
- auth_enabled: false,
- requires_setup: false,
- });
- }),
- http.get('/api/v1/library/files', () => {
- return HttpResponse.json([
- {
- id: 1,
- filename: 'test.3mf',
- file_path: '/library/test.3mf',
- file_size: 1048576,
- file_type: '3mf',
- folder_id: null,
- thumbnail_path: null,
- print_name: 'Test File',
- print_time_seconds: 3600,
- print_count: 0,
- duplicate_count: 0,
- created_at: '2024-01-01T00:00:00Z',
- created_by_username: 'testuser',
- },
- ]);
- })
- );
- render(<FileManagerPage />);
- // Switch to list view to see the column headers
- await waitFor(() => {
- expect(screen.getByText('Test File')).toBeInTheDocument();
- });
- const user = userEvent.setup();
- const listViewButton = screen.getByRole('button', { name: /list/i });
- await user.click(listViewButton);
- // "Uploaded By" column header should not be present
- await waitFor(() => {
- expect(screen.queryByText('Uploaded By')).not.toBeInTheDocument();
- });
- // User filter dropdown should not be present
- expect(screen.queryByPlaceholderText('Filter by user')).not.toBeInTheDocument();
- });
- it('shows "Uploaded By" column and user filter when auth is enabled', async () => {
- // Mock auth enabled
- server.use(
- http.get('*/api/v1/auth/status', () => {
- return HttpResponse.json({
- auth_enabled: true,
- requires_setup: false,
- });
- }),
- http.get('/api/v1/library/files', () => {
- return HttpResponse.json([
- {
- id: 1,
- filename: 'test.3mf',
- file_path: '/library/test.3mf',
- file_size: 1048576,
- file_type: '3mf',
- folder_id: null,
- thumbnail_path: null,
- print_name: 'Test File',
- print_time_seconds: 3600,
- print_count: 0,
- duplicate_count: 0,
- created_at: '2024-01-01T00:00:00Z',
- created_by_username: 'testuser',
- },
- ]);
- }),
- http.get('/api/v1/users/', () => {
- return HttpResponse.json([
- { id: 1, username: 'testuser' },
- { id: 2, username: 'admin' },
- ]);
- })
- );
- render(<FileManagerPage />);
- // Switch to list view to see the column headers
- await waitFor(() => {
- expect(screen.getByText('Test File')).toBeInTheDocument();
- });
- const user = userEvent.setup();
- const listViewButton = screen.getByRole('button', { name: /list/i });
- await user.click(listViewButton);
- // "Uploaded By" column header should be present
- await waitFor(() => {
- expect(screen.getByText('Uploaded By')).toBeInTheDocument();
- });
- // User filter dropdown should be present
- expect(screen.getByPlaceholderText('Filter by user')).toBeInTheDocument();
- // Username should be displayed in the column
- expect(screen.getByText('testuser')).toBeInTheDocument();
- });
- });
- describe('folder tree collapse preference (#996)', () => {
- // localStorage is globally mocked in setup.ts (returns undefined by default),
- // so we program each test's getItem return value explicitly.
- const getItemMock = localStorage.getItem as ReturnType<typeof vi.fn>;
- const setItemMock = localStorage.setItem as ReturnType<typeof vi.fn>;
- beforeEach(() => {
- getItemMock.mockReset();
- setItemMock.mockReset();
- });
- it('defaults to expanded (nested folders visible) when library-collapse-folders is unset', async () => {
- getItemMock.mockReturnValue(null);
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Functional Parts')).toBeInTheDocument();
- });
- expect(screen.getByText('Brackets')).toBeInTheDocument();
- });
- it('honors library-collapse-folders=true on load (nested folders hidden)', async () => {
- getItemMock.mockImplementation((key: string) =>
- key === 'library-collapse-folders' ? 'true' : null
- );
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Functional Parts')).toBeInTheDocument();
- });
- expect(screen.queryByText('Brackets')).not.toBeInTheDocument();
- });
- it('collapses nested folders and persists preference when Collapse is clicked', async () => {
- getItemMock.mockReturnValue(null);
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Brackets')).toBeInTheDocument();
- });
- // The Collapse button sits next to Wrap in the sidebar header.
- // Its text content is "Collapse" (from fileManager.collapse).
- await user.click(screen.getByRole('button', { name: 'Collapse' }));
- await waitFor(() => {
- expect(screen.queryByText('Brackets')).not.toBeInTheDocument();
- });
- expect(setItemMock).toHaveBeenCalledWith('library-collapse-folders', 'true');
- });
- it('re-expands nested folders and persists preference when Collapse is toggled off', async () => {
- getItemMock.mockImplementation((key: string) =>
- key === 'library-collapse-folders' ? 'true' : null
- );
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Functional Parts')).toBeInTheDocument();
- });
- expect(screen.queryByText('Brackets')).not.toBeInTheDocument();
- await user.click(screen.getByRole('button', { name: 'Collapse' }));
- await waitFor(() => {
- expect(screen.getByText('Brackets')).toBeInTheDocument();
- });
- expect(setItemMock).toHaveBeenCalledWith('library-collapse-folders', 'false');
- });
- });
- describe('Internal / External top-level views (#1621)', () => {
- const externalMockFolders = [
- ...mockFolders,
- {
- id: 99,
- name: 'NAS Library',
- parent_id: null,
- file_count: 200,
- project_id: null,
- archive_id: null,
- project_name: null,
- archive_name: null,
- is_external: true,
- external_readonly: false,
- external_path: '/mnt/nas',
- children: [],
- },
- ];
- it('shows the External sidebar entry only when at least one external folder is linked', async () => {
- // Default mockFolders have no is_external entries → no External row.
- const { unmount } = render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('All Files')).toBeInTheDocument();
- });
- expect(screen.queryByText('External')).not.toBeInTheDocument();
- unmount();
- // With an external folder linked, the row appears.
- server.use(
- http.get('/api/v1/library/folders', () => HttpResponse.json(externalMockFolders)),
- );
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('External')).toBeInTheDocument();
- });
- });
- it('sends internal_only=true by default ("All Files" = managed storage only)', async () => {
- const scopes: string[] = [];
- server.use(
- http.get('/api/v1/library/folders', () => HttpResponse.json(externalMockFolders)),
- http.get('/api/v1/library/files', ({ request }) => {
- const url = new URL(request.url);
- scopes.push(
- url.searchParams.get('internal_only') === 'true'
- ? 'internal'
- : url.searchParams.get('external_only') === 'true'
- ? 'external'
- : 'all',
- );
- return HttpResponse.json(mockFiles);
- }),
- );
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(scopes).toContain('internal');
- });
- });
- it('switches to external_only=true when the External sidebar entry is clicked', async () => {
- const scopes: string[] = [];
- server.use(
- http.get('/api/v1/library/folders', () => HttpResponse.json(externalMockFolders)),
- http.get('/api/v1/library/files', ({ request }) => {
- const url = new URL(request.url);
- scopes.push(
- url.searchParams.get('internal_only') === 'true'
- ? 'internal'
- : url.searchParams.get('external_only') === 'true'
- ? 'external'
- : 'all',
- );
- return HttpResponse.json([]);
- }),
- );
- const { default: userEvent } = await import('@testing-library/user-event');
- const user = userEvent.setup();
- render(<FileManagerPage />);
- await waitFor(() => expect(screen.getByText('External')).toBeInTheDocument());
- await user.click(screen.getByText('External'));
- await waitFor(() => {
- expect(scopes).toContain('external');
- });
- });
- });
- describe('"All Files" view (#1499)', () => {
- it('requests every file (include_root=false) so subfolder contents are visible', async () => {
- const rootFile = {
- id: 10,
- filename: 'root-file.3mf',
- file_path: '/library/root-file.3mf',
- file_size: 1024,
- file_type: '3mf',
- folder_id: null,
- thumbnail_path: null,
- print_name: 'Root File',
- print_time_seconds: 0,
- print_count: 0,
- duplicate_count: 0,
- created_at: '2024-01-01T00:00:00Z',
- };
- const nestedFile = {
- ...rootFile,
- id: 11,
- filename: 'nested-file.3mf',
- file_path: '/library/Functional Parts/nested-file.3mf',
- folder_id: 1,
- print_name: 'Nested File',
- };
- const includeRootValues: string[] = [];
- server.use(
- http.get('/api/v1/library/files', ({ request }) => {
- const url = new URL(request.url);
- const includeRoot = url.searchParams.get('include_root');
- includeRootValues.push(includeRoot ?? '');
- // Mirror the backend: include_root=false returns everything; true
- // returns only files with folder_id IS NULL.
- if (includeRoot === 'false') {
- return HttpResponse.json([rootFile, nestedFile]);
- }
- return HttpResponse.json([rootFile]);
- }),
- );
- render(<FileManagerPage />);
- await waitFor(() => {
- expect(screen.getByText('Root File')).toBeInTheDocument();
- expect(screen.getByText('Nested File')).toBeInTheDocument();
- });
- // Sanity-check: the buggy call would have sent include_root=true here.
- expect(includeRootValues).toContain('false');
- });
- });
- });
|