FileManagerPage.test.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /**
  2. * Tests for the FileManagerPage component.
  3. */
  4. import { describe, it, expect, beforeEach } from 'vitest';
  5. import { screen, waitFor } from '@testing-library/react';
  6. import userEvent from '@testing-library/user-event';
  7. import { render } from '../utils';
  8. import { FileManagerPage } from '../../pages/FileManagerPage';
  9. import { http, HttpResponse } from 'msw';
  10. import { server } from '../mocks/server';
  11. // Mock data
  12. const mockFolders = [
  13. {
  14. id: 1,
  15. name: 'Functional Parts',
  16. parent_id: null,
  17. file_count: 5,
  18. project_id: null,
  19. archive_id: null,
  20. project_name: null,
  21. archive_name: null,
  22. children: [
  23. {
  24. id: 2,
  25. name: 'Brackets',
  26. parent_id: 1,
  27. file_count: 3,
  28. project_id: null,
  29. archive_id: null,
  30. project_name: null,
  31. archive_name: null,
  32. children: [],
  33. },
  34. ],
  35. },
  36. {
  37. id: 3,
  38. name: 'Art Projects',
  39. parent_id: null,
  40. file_count: 2,
  41. project_id: 1,
  42. archive_id: null,
  43. project_name: 'My Art Project',
  44. archive_name: null,
  45. children: [],
  46. },
  47. ];
  48. const mockFiles = [
  49. {
  50. id: 1,
  51. filename: 'benchy.gcode.3mf',
  52. file_path: '/library/benchy.gcode.3mf',
  53. file_size: 1048576,
  54. file_type: '3mf',
  55. folder_id: null,
  56. thumbnail_path: '/thumbnails/1.png',
  57. print_name: 'Benchy',
  58. print_time_seconds: 3600,
  59. print_count: 5,
  60. duplicate_count: 0,
  61. created_at: '2024-01-01T00:00:00Z',
  62. },
  63. {
  64. id: 2,
  65. filename: 'bracket.stl',
  66. file_path: '/library/bracket.stl',
  67. file_size: 524288,
  68. file_type: 'stl',
  69. folder_id: null,
  70. thumbnail_path: null,
  71. print_name: null,
  72. print_time_seconds: null,
  73. print_count: 0,
  74. duplicate_count: 2,
  75. created_at: '2024-01-02T00:00:00Z',
  76. },
  77. ];
  78. const mockStats = {
  79. total_files: 10,
  80. total_folders: 3,
  81. total_size_bytes: 104857600,
  82. disk_free_bytes: 10737418240,
  83. disk_total_bytes: 107374182400,
  84. };
  85. describe('FileManagerPage', () => {
  86. beforeEach(() => {
  87. // Clear localStorage to ensure consistent view mode
  88. localStorage.clear();
  89. server.use(
  90. http.get('/api/v1/library/folders', () => {
  91. return HttpResponse.json(mockFolders);
  92. }),
  93. http.get('/api/v1/library/files', () => {
  94. return HttpResponse.json(mockFiles);
  95. }),
  96. http.get('/api/v1/library/stats', () => {
  97. return HttpResponse.json(mockStats);
  98. }),
  99. http.get('/api/v1/settings/', () => {
  100. return HttpResponse.json({
  101. check_updates: false,
  102. check_printer_firmware: false,
  103. library_disk_warning_gb: 5,
  104. });
  105. }),
  106. http.post('/api/v1/library/folders', async ({ request }) => {
  107. const body = await request.json() as { name: string };
  108. return HttpResponse.json({ id: 4, name: body.name, parent_id: null, children: [] });
  109. }),
  110. http.delete('/api/v1/library/folders/:id', () => {
  111. return HttpResponse.json({ success: true });
  112. }),
  113. http.delete('/api/v1/library/files/:id', () => {
  114. return HttpResponse.json({ success: true });
  115. }),
  116. http.post('/api/v1/library/files/move', () => {
  117. return HttpResponse.json({ success: true });
  118. }),
  119. http.post('/api/v1/library/files/add-to-queue', () => {
  120. return HttpResponse.json({ added: [{ file_id: 1, queue_id: 1 }], errors: [] });
  121. }),
  122. http.get('/api/v1/projects/', () => {
  123. return HttpResponse.json([{ id: 1, name: 'Test Project', color: '#00ae42' }]);
  124. }),
  125. http.get('/api/v1/archives/', () => {
  126. return HttpResponse.json([{ id: 1, print_name: 'Test Archive', filename: 'test.3mf' }]);
  127. })
  128. );
  129. });
  130. describe('rendering', () => {
  131. it('renders the page title', async () => {
  132. render(<FileManagerPage />);
  133. await waitFor(() => {
  134. expect(screen.getByText('File Manager')).toBeInTheDocument();
  135. });
  136. });
  137. it('renders the page description', async () => {
  138. render(<FileManagerPage />);
  139. await waitFor(() => {
  140. expect(screen.getByText('Organize and manage your print files')).toBeInTheDocument();
  141. });
  142. });
  143. it('shows New Folder button', async () => {
  144. render(<FileManagerPage />);
  145. await waitFor(() => {
  146. expect(screen.getByText('New Folder')).toBeInTheDocument();
  147. });
  148. });
  149. it('shows Upload button', async () => {
  150. render(<FileManagerPage />);
  151. await waitFor(() => {
  152. expect(screen.getByText('Upload')).toBeInTheDocument();
  153. });
  154. });
  155. });
  156. describe('stats display', () => {
  157. it('shows file count', async () => {
  158. render(<FileManagerPage />);
  159. await waitFor(() => {
  160. expect(screen.getByText('Files:')).toBeInTheDocument();
  161. expect(screen.getByText('10')).toBeInTheDocument();
  162. });
  163. });
  164. it('shows folder count', async () => {
  165. render(<FileManagerPage />);
  166. await waitFor(() => {
  167. expect(screen.getByText('Folders:')).toBeInTheDocument();
  168. // Folder count appears multiple places, just verify the label is present
  169. const foldersLabel = screen.getByText('Folders:');
  170. expect(foldersLabel.nextElementSibling?.textContent).toBe('3');
  171. });
  172. });
  173. it('shows total size', async () => {
  174. render(<FileManagerPage />);
  175. await waitFor(() => {
  176. expect(screen.getByText('Size:')).toBeInTheDocument();
  177. expect(screen.getByText('100.0 MB')).toBeInTheDocument();
  178. });
  179. });
  180. it('shows free space', async () => {
  181. render(<FileManagerPage />);
  182. await waitFor(() => {
  183. expect(screen.getByText('Free:')).toBeInTheDocument();
  184. });
  185. });
  186. });
  187. describe('folder sidebar', () => {
  188. it('shows All Files option', async () => {
  189. render(<FileManagerPage />);
  190. await waitFor(() => {
  191. expect(screen.getByText('All Files')).toBeInTheDocument();
  192. });
  193. });
  194. it('shows folder tree', async () => {
  195. render(<FileManagerPage />);
  196. await waitFor(() => {
  197. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  198. expect(screen.getByText('Art Projects')).toBeInTheDocument();
  199. });
  200. });
  201. it('shows nested folders', async () => {
  202. render(<FileManagerPage />);
  203. await waitFor(() => {
  204. expect(screen.getByText('Brackets')).toBeInTheDocument();
  205. });
  206. });
  207. it('shows linked folder indicator', async () => {
  208. render(<FileManagerPage />);
  209. await waitFor(() => {
  210. // Art Projects has a project_id
  211. expect(screen.getByText('Art Projects')).toBeInTheDocument();
  212. });
  213. });
  214. });
  215. describe('file display', () => {
  216. it('shows files in grid', async () => {
  217. render(<FileManagerPage />);
  218. await waitFor(() => {
  219. expect(screen.getByText('Benchy')).toBeInTheDocument();
  220. });
  221. });
  222. it('shows file type badges', async () => {
  223. render(<FileManagerPage />);
  224. await waitFor(() => {
  225. // File type badges show uppercase type
  226. expect(screen.getAllByText('3MF').length).toBeGreaterThan(0);
  227. expect(screen.getAllByText('STL').length).toBeGreaterThan(0);
  228. });
  229. });
  230. it('shows print count', async () => {
  231. render(<FileManagerPage />);
  232. await waitFor(() => {
  233. expect(screen.getByText('Printed 5x')).toBeInTheDocument();
  234. });
  235. });
  236. it('shows duplicate badge', async () => {
  237. render(<FileManagerPage />);
  238. await waitFor(() => {
  239. // Duplicate badge shows count, there may be multiple "2"s on the page
  240. // so we check that at least one element with "2" exists
  241. const elements = screen.getAllByText('2');
  242. expect(elements.length).toBeGreaterThan(0);
  243. });
  244. });
  245. });
  246. describe('view modes', () => {
  247. it('has grid view button', async () => {
  248. render(<FileManagerPage />);
  249. await waitFor(() => {
  250. expect(screen.getByTitle('Grid view')).toBeInTheDocument();
  251. });
  252. });
  253. it('has list view button', async () => {
  254. render(<FileManagerPage />);
  255. await waitFor(() => {
  256. expect(screen.getByTitle('List view')).toBeInTheDocument();
  257. });
  258. });
  259. it('can switch to list view', async () => {
  260. const user = userEvent.setup();
  261. render(<FileManagerPage />);
  262. // Wait for files to load first
  263. await waitFor(() => {
  264. expect(screen.getByText('Benchy')).toBeInTheDocument();
  265. });
  266. // Both view mode buttons should be present and clickable
  267. const gridButton = screen.getByTitle('Grid view');
  268. const listButton = screen.getByTitle('List view');
  269. expect(gridButton).toBeInTheDocument();
  270. expect(listButton).toBeInTheDocument();
  271. // Click list view button - verify no errors occur
  272. await user.click(listButton);
  273. // Clicking grid button should also work
  274. await user.click(gridButton);
  275. // Verify files are still displayed after toggling
  276. expect(screen.getByText('Benchy')).toBeInTheDocument();
  277. });
  278. });
  279. describe('search and filter', () => {
  280. it('has search input', async () => {
  281. render(<FileManagerPage />);
  282. await waitFor(() => {
  283. expect(screen.getByPlaceholderText('Search files...')).toBeInTheDocument();
  284. });
  285. });
  286. it('has type filter', async () => {
  287. render(<FileManagerPage />);
  288. await waitFor(() => {
  289. expect(screen.getByText('All types')).toBeInTheDocument();
  290. });
  291. });
  292. it('has sort options', async () => {
  293. render(<FileManagerPage />);
  294. await waitFor(() => {
  295. // Sort dropdown should show Name as default option (persisted to localStorage)
  296. expect(screen.getByDisplayValue('Name')).toBeInTheDocument();
  297. });
  298. });
  299. });
  300. describe('selection', () => {
  301. it('shows select all button', async () => {
  302. render(<FileManagerPage />);
  303. await waitFor(() => {
  304. expect(screen.getByText('Select All')).toBeInTheDocument();
  305. });
  306. });
  307. it('can select files', async () => {
  308. const user = userEvent.setup();
  309. render(<FileManagerPage />);
  310. await waitFor(() => {
  311. expect(screen.getByText('Benchy')).toBeInTheDocument();
  312. });
  313. // Click on the file card to select it
  314. const fileCard = screen.getByText('Benchy').closest('div[class*="cursor-pointer"]');
  315. if (fileCard) {
  316. await user.click(fileCard);
  317. }
  318. await waitFor(() => {
  319. expect(screen.getByText('1 selected')).toBeInTheDocument();
  320. });
  321. });
  322. it('shows bulk actions when files selected', async () => {
  323. const user = userEvent.setup();
  324. render(<FileManagerPage />);
  325. await waitFor(() => {
  326. expect(screen.getByText('Select All')).toBeInTheDocument();
  327. });
  328. await user.click(screen.getByText('Select All'));
  329. await waitFor(() => {
  330. expect(screen.getByText('Move')).toBeInTheDocument();
  331. expect(screen.getByText('Delete')).toBeInTheDocument();
  332. });
  333. });
  334. });
  335. describe('new folder modal', () => {
  336. it('opens new folder modal', async () => {
  337. const user = userEvent.setup();
  338. render(<FileManagerPage />);
  339. await waitFor(() => {
  340. expect(screen.getByText('New Folder')).toBeInTheDocument();
  341. });
  342. await user.click(screen.getByText('New Folder'));
  343. await waitFor(() => {
  344. expect(screen.getByText('Folder Name')).toBeInTheDocument();
  345. expect(screen.getByPlaceholderText('e.g., Functional Parts')).toBeInTheDocument();
  346. });
  347. });
  348. it('can create a folder', async () => {
  349. const user = userEvent.setup();
  350. render(<FileManagerPage />);
  351. await waitFor(() => {
  352. expect(screen.getByText('New Folder')).toBeInTheDocument();
  353. });
  354. await user.click(screen.getByText('New Folder'));
  355. await waitFor(() => {
  356. expect(screen.getByPlaceholderText('e.g., Functional Parts')).toBeInTheDocument();
  357. });
  358. const input = screen.getByPlaceholderText('e.g., Functional Parts');
  359. await user.type(input, 'My New Folder');
  360. const createButton = screen.getByRole('button', { name: 'Create' });
  361. await user.click(createButton);
  362. // Modal should close after creation
  363. await waitFor(() => {
  364. expect(screen.queryByText('Folder Name')).not.toBeInTheDocument();
  365. });
  366. });
  367. });
  368. describe('empty state', () => {
  369. it('shows empty state when no files', async () => {
  370. server.use(
  371. http.get('/api/v1/library/files', () => {
  372. return HttpResponse.json([]);
  373. })
  374. );
  375. render(<FileManagerPage />);
  376. await waitFor(() => {
  377. expect(screen.getByText('No files yet')).toBeInTheDocument();
  378. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  379. });
  380. });
  381. });
  382. describe('schedule print', () => {
  383. it('shows schedule print button for sliced files', async () => {
  384. const user = userEvent.setup();
  385. render(<FileManagerPage />);
  386. await waitFor(() => {
  387. expect(screen.getByText('Select All')).toBeInTheDocument();
  388. });
  389. // Select a sliced file (benchy.gcode.3mf)
  390. await user.click(screen.getByText('Select All'));
  391. await waitFor(() => {
  392. expect(screen.getByText(/Schedule/)).toBeInTheDocument();
  393. });
  394. });
  395. });
  396. describe('STL thumbnail generation', () => {
  397. it('shows Generate Thumbnails button', async () => {
  398. render(<FileManagerPage />);
  399. await waitFor(() => {
  400. expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
  401. });
  402. });
  403. it('Generate Thumbnails button has correct title', async () => {
  404. render(<FileManagerPage />);
  405. await waitFor(() => {
  406. const button = screen.getByTitle('Generate thumbnails for STL files missing them');
  407. expect(button).toBeInTheDocument();
  408. });
  409. });
  410. it('can click Generate Thumbnails button', async () => {
  411. const user = userEvent.setup();
  412. server.use(
  413. http.post('/api/v1/library/generate-stl-thumbnails', () => {
  414. return HttpResponse.json({
  415. processed: 1,
  416. succeeded: 1,
  417. failed: 0,
  418. results: [{ file_id: 2, success: true }],
  419. });
  420. })
  421. );
  422. render(<FileManagerPage />);
  423. await waitFor(() => {
  424. expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
  425. });
  426. const button = screen.getByText('Generate Thumbnails');
  427. await user.click(button);
  428. // Button should work without error
  429. await waitFor(() => {
  430. expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
  431. });
  432. });
  433. it('shows STL file without thumbnail in file list', async () => {
  434. render(<FileManagerPage />);
  435. await waitFor(() => {
  436. // bracket.stl has no thumbnail_path
  437. expect(screen.getByText('bracket.stl')).toBeInTheDocument();
  438. expect(screen.getAllByText('STL').length).toBeGreaterThan(0);
  439. });
  440. });
  441. });
  442. describe('upload modal with advanced 3MF support', () => {
  443. it('opens upload modal', async () => {
  444. const user = userEvent.setup();
  445. render(<FileManagerPage />);
  446. await waitFor(() => {
  447. expect(screen.getByText('Upload')).toBeInTheDocument();
  448. });
  449. await user.click(screen.getByText('Upload'));
  450. await waitFor(() => {
  451. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  452. expect(screen.getByText(/Drag & drop/)).toBeInTheDocument();
  453. });
  454. });
  455. it('shows 3MF extraction info when 3MF file is added', async () => {
  456. const user = userEvent.setup();
  457. render(<FileManagerPage />);
  458. await waitFor(() => {
  459. expect(screen.getByText('Upload')).toBeInTheDocument();
  460. });
  461. await user.click(screen.getByText('Upload'));
  462. await waitFor(() => {
  463. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  464. });
  465. // Create a mock 3MF file
  466. const threemfFile = new File(['content'], 'model.gcode.3mf', { type: 'application/octet-stream' });
  467. // Get the hidden file input
  468. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  469. expect(fileInput).toBeInTheDocument();
  470. // Simulate file selection
  471. await user.upload(fileInput, threemfFile);
  472. // 3MF extraction info should appear
  473. await waitFor(() => {
  474. expect(screen.getByText('3MF files detected')).toBeInTheDocument();
  475. expect(screen.getByText(/Printer model.*will be automatically extracted/i)).toBeInTheDocument();
  476. });
  477. });
  478. it('shows STL thumbnail option when STL file is added', async () => {
  479. const user = userEvent.setup();
  480. render(<FileManagerPage />);
  481. await waitFor(() => {
  482. expect(screen.getByText('Upload')).toBeInTheDocument();
  483. });
  484. await user.click(screen.getByText('Upload'));
  485. await waitFor(() => {
  486. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  487. });
  488. // Create a mock STL file
  489. const stlFile = new File(['solid test'], 'model.stl', { type: 'application/sla' });
  490. // Get the hidden file input
  491. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  492. expect(fileInput).toBeInTheDocument();
  493. // Simulate file selection
  494. await user.upload(fileInput, stlFile);
  495. // STL thumbnail option should appear
  496. await waitFor(() => {
  497. expect(screen.getByText('STL thumbnail generation')).toBeInTheDocument();
  498. expect(screen.getByText(/Thumbnails can be generated/i)).toBeInTheDocument();
  499. });
  500. });
  501. });
  502. describe('authentication-based UI changes', () => {
  503. it('hides "Uploaded By" column and user filter when auth is disabled', async () => {
  504. // Mock auth disabled (default)
  505. server.use(
  506. http.get('*/api/v1/auth/status', () => {
  507. return HttpResponse.json({
  508. auth_enabled: false,
  509. requires_setup: false,
  510. });
  511. }),
  512. http.get('/api/v1/library/files', () => {
  513. return HttpResponse.json([
  514. {
  515. id: 1,
  516. filename: 'test.3mf',
  517. file_path: '/library/test.3mf',
  518. file_size: 1048576,
  519. file_type: '3mf',
  520. folder_id: null,
  521. thumbnail_path: null,
  522. print_name: 'Test File',
  523. print_time_seconds: 3600,
  524. print_count: 0,
  525. duplicate_count: 0,
  526. created_at: '2024-01-01T00:00:00Z',
  527. created_by_username: 'testuser',
  528. },
  529. ]);
  530. })
  531. );
  532. render(<FileManagerPage />);
  533. // Switch to list view to see the column headers
  534. await waitFor(() => {
  535. expect(screen.getByText('Test File')).toBeInTheDocument();
  536. });
  537. const user = userEvent.setup();
  538. const listViewButton = screen.getByRole('button', { name: /list/i });
  539. await user.click(listViewButton);
  540. // "Uploaded By" column header should not be present
  541. await waitFor(() => {
  542. expect(screen.queryByText('Uploaded By')).not.toBeInTheDocument();
  543. });
  544. // User filter dropdown should not be present
  545. expect(screen.queryByPlaceholderText('Filter by user')).not.toBeInTheDocument();
  546. });
  547. it('shows "Uploaded By" column and user filter when auth is enabled', async () => {
  548. // Mock auth enabled
  549. server.use(
  550. http.get('*/api/v1/auth/status', () => {
  551. return HttpResponse.json({
  552. auth_enabled: true,
  553. requires_setup: false,
  554. });
  555. }),
  556. http.get('/api/v1/library/files', () => {
  557. return HttpResponse.json([
  558. {
  559. id: 1,
  560. filename: 'test.3mf',
  561. file_path: '/library/test.3mf',
  562. file_size: 1048576,
  563. file_type: '3mf',
  564. folder_id: null,
  565. thumbnail_path: null,
  566. print_name: 'Test File',
  567. print_time_seconds: 3600,
  568. print_count: 0,
  569. duplicate_count: 0,
  570. created_at: '2024-01-01T00:00:00Z',
  571. created_by_username: 'testuser',
  572. },
  573. ]);
  574. }),
  575. http.get('/api/v1/users/', () => {
  576. return HttpResponse.json([
  577. { id: 1, username: 'testuser' },
  578. { id: 2, username: 'admin' },
  579. ]);
  580. })
  581. );
  582. render(<FileManagerPage />);
  583. // Switch to list view to see the column headers
  584. await waitFor(() => {
  585. expect(screen.getByText('Test File')).toBeInTheDocument();
  586. });
  587. const user = userEvent.setup();
  588. const listViewButton = screen.getByRole('button', { name: /list/i });
  589. await user.click(listViewButton);
  590. // "Uploaded By" column header should be present
  591. await waitFor(() => {
  592. expect(screen.getByText('Uploaded By')).toBeInTheDocument();
  593. });
  594. // User filter dropdown should be present
  595. expect(screen.getByPlaceholderText('Filter by user')).toBeInTheDocument();
  596. // Username should be displayed in the column
  597. expect(screen.getByText('testuser')).toBeInTheDocument();
  598. });
  599. });
  600. });