FileManagerPage.test.tsx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /**
  2. * Tests for the FileManagerPage component.
  3. */
  4. import { describe, it, expect, beforeEach, vi } 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. id: 3,
  79. filename: 'cube.gcode.3mf',
  80. file_path: '/library/cube.gcode.3mf',
  81. file_size: 2048576,
  82. file_type: '3mf',
  83. folder_id: null,
  84. thumbnail_path: '/thumbnails/3.png',
  85. print_name: 'Cube',
  86. print_time_seconds: 1800,
  87. print_count: 2,
  88. duplicate_count: 0,
  89. created_at: '2024-01-03T00:00:00Z',
  90. },
  91. ];
  92. const mockStats = {
  93. total_files: 10,
  94. total_folders: 3,
  95. total_size_bytes: 104857600,
  96. disk_free_bytes: 10737418240,
  97. disk_total_bytes: 107374182400,
  98. };
  99. describe('FileManagerPage', () => {
  100. beforeEach(() => {
  101. // Clear localStorage to ensure consistent view mode
  102. localStorage.clear();
  103. server.use(
  104. http.get('/api/v1/library/folders', () => {
  105. return HttpResponse.json(mockFolders);
  106. }),
  107. http.get('/api/v1/library/files', () => {
  108. return HttpResponse.json(mockFiles);
  109. }),
  110. http.get('/api/v1/library/stats', () => {
  111. return HttpResponse.json(mockStats);
  112. }),
  113. http.get('/api/v1/settings/', () => {
  114. return HttpResponse.json({
  115. check_updates: false,
  116. check_printer_firmware: false,
  117. library_disk_warning_gb: 5,
  118. });
  119. }),
  120. http.post('/api/v1/library/folders', async ({ request }) => {
  121. const body = await request.json() as { name: string };
  122. return HttpResponse.json({ id: 4, name: body.name, parent_id: null, children: [] });
  123. }),
  124. http.delete('/api/v1/library/folders/:id', () => {
  125. return HttpResponse.json({ success: true });
  126. }),
  127. http.delete('/api/v1/library/files/:id', () => {
  128. return HttpResponse.json({ success: true });
  129. }),
  130. http.post('/api/v1/library/files/move', () => {
  131. return HttpResponse.json({ success: true });
  132. }),
  133. http.post('/api/v1/library/files/add-to-queue', () => {
  134. return HttpResponse.json({ added: [{ file_id: 1, queue_id: 1 }], errors: [] });
  135. }),
  136. http.get('/api/v1/projects/', () => {
  137. return HttpResponse.json([{ id: 1, name: 'Test Project', color: '#00ae42' }]);
  138. }),
  139. http.get('/api/v1/archives/', () => {
  140. return HttpResponse.json([{ id: 1, print_name: 'Test Archive', filename: 'test.3mf' }]);
  141. })
  142. );
  143. });
  144. describe('rendering', () => {
  145. it('renders the page title', async () => {
  146. render(<FileManagerPage />);
  147. await waitFor(() => {
  148. expect(screen.getByText('File Manager')).toBeInTheDocument();
  149. });
  150. });
  151. it('renders the page description', async () => {
  152. render(<FileManagerPage />);
  153. await waitFor(() => {
  154. expect(screen.getByText('Organize and manage your print files')).toBeInTheDocument();
  155. });
  156. });
  157. it('shows New Folder button', async () => {
  158. render(<FileManagerPage />);
  159. await waitFor(() => {
  160. expect(screen.getByText('New Folder')).toBeInTheDocument();
  161. });
  162. });
  163. it('shows Upload button', async () => {
  164. render(<FileManagerPage />);
  165. await waitFor(() => {
  166. expect(screen.getByText('Upload')).toBeInTheDocument();
  167. });
  168. });
  169. });
  170. describe('stats display', () => {
  171. it('shows file count', async () => {
  172. render(<FileManagerPage />);
  173. await waitFor(() => {
  174. expect(screen.getByText('Files:')).toBeInTheDocument();
  175. expect(screen.getByText('10')).toBeInTheDocument();
  176. });
  177. });
  178. it('shows folder count', async () => {
  179. render(<FileManagerPage />);
  180. await waitFor(() => {
  181. expect(screen.getByText('Folders:')).toBeInTheDocument();
  182. // Folder count appears multiple places, just verify the label is present
  183. const foldersLabel = screen.getByText('Folders:');
  184. expect(foldersLabel.nextElementSibling?.textContent).toBe('3');
  185. });
  186. });
  187. it('shows total size', async () => {
  188. render(<FileManagerPage />);
  189. await waitFor(() => {
  190. expect(screen.getByText('Size:')).toBeInTheDocument();
  191. expect(screen.getByText('100.0 MB')).toBeInTheDocument();
  192. });
  193. });
  194. it('shows free space', async () => {
  195. render(<FileManagerPage />);
  196. await waitFor(() => {
  197. expect(screen.getByText('Free:')).toBeInTheDocument();
  198. });
  199. });
  200. });
  201. describe('folder sidebar', () => {
  202. it('shows All Files option', async () => {
  203. render(<FileManagerPage />);
  204. await waitFor(() => {
  205. expect(screen.getByText('All Files')).toBeInTheDocument();
  206. });
  207. });
  208. it('shows folder tree', async () => {
  209. render(<FileManagerPage />);
  210. await waitFor(() => {
  211. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  212. expect(screen.getByText('Art Projects')).toBeInTheDocument();
  213. });
  214. });
  215. it('shows nested folders', async () => {
  216. render(<FileManagerPage />);
  217. await waitFor(() => {
  218. expect(screen.getByText('Brackets')).toBeInTheDocument();
  219. });
  220. });
  221. it('shows linked folder indicator', async () => {
  222. render(<FileManagerPage />);
  223. await waitFor(() => {
  224. // Art Projects has a project_id
  225. expect(screen.getByText('Art Projects')).toBeInTheDocument();
  226. });
  227. });
  228. });
  229. describe('file display', () => {
  230. it('shows files in grid', async () => {
  231. render(<FileManagerPage />);
  232. await waitFor(() => {
  233. expect(screen.getByText('Benchy')).toBeInTheDocument();
  234. });
  235. });
  236. it('shows file type badges', async () => {
  237. render(<FileManagerPage />);
  238. await waitFor(() => {
  239. // File type badges show uppercase type
  240. expect(screen.getAllByText('3MF').length).toBeGreaterThan(0);
  241. expect(screen.getAllByText('STL').length).toBeGreaterThan(0);
  242. });
  243. });
  244. it('shows print count', async () => {
  245. render(<FileManagerPage />);
  246. await waitFor(() => {
  247. expect(screen.getByText('Printed 5x')).toBeInTheDocument();
  248. });
  249. });
  250. it('shows duplicate badge', async () => {
  251. render(<FileManagerPage />);
  252. await waitFor(() => {
  253. // Duplicate badge shows count, there may be multiple "2"s on the page
  254. // so we check that at least one element with "2" exists
  255. const elements = screen.getAllByText('2');
  256. expect(elements.length).toBeGreaterThan(0);
  257. });
  258. });
  259. });
  260. describe('view modes', () => {
  261. it('has grid view button', async () => {
  262. render(<FileManagerPage />);
  263. await waitFor(() => {
  264. expect(screen.getByTitle('Grid view')).toBeInTheDocument();
  265. });
  266. });
  267. it('has list view button', async () => {
  268. render(<FileManagerPage />);
  269. await waitFor(() => {
  270. expect(screen.getByTitle('List view')).toBeInTheDocument();
  271. });
  272. });
  273. it('can switch to list view', async () => {
  274. const user = userEvent.setup();
  275. render(<FileManagerPage />);
  276. // Wait for files to load first
  277. await waitFor(() => {
  278. expect(screen.getByText('Benchy')).toBeInTheDocument();
  279. });
  280. // Both view mode buttons should be present and clickable
  281. const gridButton = screen.getByTitle('Grid view');
  282. const listButton = screen.getByTitle('List view');
  283. expect(gridButton).toBeInTheDocument();
  284. expect(listButton).toBeInTheDocument();
  285. // Click list view button - verify no errors occur
  286. await user.click(listButton);
  287. // Clicking grid button should also work
  288. await user.click(gridButton);
  289. // Verify files are still displayed after toggling
  290. expect(screen.getByText('Benchy')).toBeInTheDocument();
  291. });
  292. });
  293. describe('search and filter', () => {
  294. it('has search input', async () => {
  295. render(<FileManagerPage />);
  296. await waitFor(() => {
  297. expect(screen.getByPlaceholderText('Search files...')).toBeInTheDocument();
  298. });
  299. });
  300. it('has type filter', async () => {
  301. render(<FileManagerPage />);
  302. await waitFor(() => {
  303. expect(screen.getByText('All types')).toBeInTheDocument();
  304. });
  305. });
  306. it('has sort options', async () => {
  307. render(<FileManagerPage />);
  308. await waitFor(() => {
  309. // Sort dropdown should show Name as default option (persisted to localStorage)
  310. expect(screen.getByDisplayValue('Name')).toBeInTheDocument();
  311. });
  312. });
  313. });
  314. describe('selection', () => {
  315. it('shows select all button', async () => {
  316. render(<FileManagerPage />);
  317. await waitFor(() => {
  318. expect(screen.getByText('Select All')).toBeInTheDocument();
  319. });
  320. });
  321. it('can select files', async () => {
  322. const user = userEvent.setup();
  323. render(<FileManagerPage />);
  324. await waitFor(() => {
  325. expect(screen.getByText('Benchy')).toBeInTheDocument();
  326. });
  327. // Click on the file card to select it
  328. const fileCard = screen.getByText('Benchy').closest('div[class*="cursor-pointer"]');
  329. if (fileCard) {
  330. await user.click(fileCard);
  331. }
  332. await waitFor(() => {
  333. expect(screen.getByText('1 selected')).toBeInTheDocument();
  334. });
  335. });
  336. it('shows bulk actions when files selected', async () => {
  337. const user = userEvent.setup();
  338. render(<FileManagerPage />);
  339. await waitFor(() => {
  340. expect(screen.getByText('Select All')).toBeInTheDocument();
  341. });
  342. await user.click(screen.getByText('Select All'));
  343. await waitFor(() => {
  344. expect(screen.getByText('Move')).toBeInTheDocument();
  345. expect(screen.getByText('Delete')).toBeInTheDocument();
  346. });
  347. });
  348. });
  349. describe('new folder modal', () => {
  350. it('opens new folder modal', async () => {
  351. const user = userEvent.setup();
  352. render(<FileManagerPage />);
  353. await waitFor(() => {
  354. expect(screen.getByText('New Folder')).toBeInTheDocument();
  355. });
  356. await user.click(screen.getByText('New Folder'));
  357. await waitFor(() => {
  358. expect(screen.getByText('Folder Name')).toBeInTheDocument();
  359. expect(screen.getByPlaceholderText('e.g., Functional Parts')).toBeInTheDocument();
  360. });
  361. });
  362. it('can create a folder', async () => {
  363. const user = userEvent.setup();
  364. render(<FileManagerPage />);
  365. await waitFor(() => {
  366. expect(screen.getByText('New Folder')).toBeInTheDocument();
  367. });
  368. await user.click(screen.getByText('New Folder'));
  369. await waitFor(() => {
  370. expect(screen.getByPlaceholderText('e.g., Functional Parts')).toBeInTheDocument();
  371. });
  372. const input = screen.getByPlaceholderText('e.g., Functional Parts');
  373. await user.type(input, 'My New Folder');
  374. const createButton = screen.getByRole('button', { name: 'Create' });
  375. await user.click(createButton);
  376. // Modal should close after creation
  377. await waitFor(() => {
  378. expect(screen.queryByText('Folder Name')).not.toBeInTheDocument();
  379. });
  380. });
  381. });
  382. describe('empty state', () => {
  383. it('shows empty state when no files', async () => {
  384. server.use(
  385. http.get('/api/v1/library/files', () => {
  386. return HttpResponse.json([]);
  387. })
  388. );
  389. render(<FileManagerPage />);
  390. await waitFor(() => {
  391. expect(screen.getByText('No files yet')).toBeInTheDocument();
  392. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  393. });
  394. });
  395. });
  396. describe('schedule print', () => {
  397. it('shows schedule print button when one sliced file is selected', async () => {
  398. const user = userEvent.setup();
  399. render(<FileManagerPage />);
  400. await waitFor(() => {
  401. expect(screen.getByText('Benchy')).toBeInTheDocument();
  402. });
  403. // Select a sliced file (benchy.gcode.3mf) by clicking on its card
  404. const fileCard = screen.getByText('Benchy').closest('div[class*="cursor-pointer"]');
  405. if (fileCard) {
  406. await user.click(fileCard);
  407. }
  408. await waitFor(() => {
  409. expect(screen.getByText(/Schedule/)).toBeInTheDocument();
  410. });
  411. });
  412. it('hides schedule print button when multiple files are selected', async () => {
  413. const user = userEvent.setup();
  414. render(<FileManagerPage />);
  415. await waitFor(() => {
  416. expect(screen.getByText('Select All')).toBeInTheDocument();
  417. });
  418. // Select all files
  419. await user.click(screen.getByText('Select All'));
  420. await waitFor(() => {
  421. // Schedule button should not be present when multiple files are selected
  422. expect(screen.queryByText(/Schedule/)).not.toBeInTheDocument();
  423. });
  424. });
  425. });
  426. describe('STL thumbnail generation', () => {
  427. it('shows Generate Thumbnails button', async () => {
  428. render(<FileManagerPage />);
  429. await waitFor(() => {
  430. expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
  431. });
  432. });
  433. it('Generate Thumbnails button has correct title', async () => {
  434. render(<FileManagerPage />);
  435. await waitFor(() => {
  436. const button = screen.getByTitle('Generate thumbnails for STL files missing them');
  437. expect(button).toBeInTheDocument();
  438. });
  439. });
  440. it('can click Generate Thumbnails button', async () => {
  441. const user = userEvent.setup();
  442. server.use(
  443. http.post('/api/v1/library/generate-stl-thumbnails', () => {
  444. return HttpResponse.json({
  445. processed: 1,
  446. succeeded: 1,
  447. failed: 0,
  448. results: [{ file_id: 2, success: true }],
  449. });
  450. })
  451. );
  452. render(<FileManagerPage />);
  453. await waitFor(() => {
  454. expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
  455. });
  456. const button = screen.getByText('Generate Thumbnails');
  457. await user.click(button);
  458. // Button should work without error
  459. await waitFor(() => {
  460. expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
  461. });
  462. });
  463. it('shows STL file without thumbnail in file list', async () => {
  464. render(<FileManagerPage />);
  465. await waitFor(() => {
  466. // bracket.stl has no thumbnail_path
  467. expect(screen.getByText('bracket.stl')).toBeInTheDocument();
  468. expect(screen.getAllByText('STL').length).toBeGreaterThan(0);
  469. });
  470. });
  471. });
  472. describe('upload modal (FileUploadModal)', () => {
  473. it('opens upload modal when Upload button is clicked', async () => {
  474. const user = userEvent.setup();
  475. render(<FileManagerPage />);
  476. await waitFor(() => {
  477. expect(screen.getByText('Upload')).toBeInTheDocument();
  478. });
  479. await user.click(screen.getByText('Upload'));
  480. await waitFor(() => {
  481. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  482. expect(screen.getByText(/Drag & drop/)).toBeInTheDocument();
  483. });
  484. });
  485. it('closes upload modal when Cancel is clicked', async () => {
  486. const user = userEvent.setup();
  487. render(<FileManagerPage />);
  488. await waitFor(() => {
  489. expect(screen.getByText('Upload')).toBeInTheDocument();
  490. });
  491. await user.click(screen.getByText('Upload'));
  492. await waitFor(() => {
  493. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  494. });
  495. await user.click(screen.getByRole('button', { name: 'Cancel' }));
  496. await waitFor(() => {
  497. expect(screen.queryByText('Upload Files')).not.toBeInTheDocument();
  498. });
  499. });
  500. it('shows 3MF extraction info when 3MF file is added', async () => {
  501. const user = userEvent.setup();
  502. render(<FileManagerPage />);
  503. await waitFor(() => {
  504. expect(screen.getByText('Upload')).toBeInTheDocument();
  505. });
  506. await user.click(screen.getByText('Upload'));
  507. await waitFor(() => {
  508. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  509. });
  510. const threemfFile = new File(['content'], 'model.gcode.3mf', { type: 'application/octet-stream' });
  511. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  512. expect(fileInput).toBeInTheDocument();
  513. await user.upload(fileInput, threemfFile);
  514. await waitFor(() => {
  515. expect(screen.getByText('3MF files detected')).toBeInTheDocument();
  516. expect(screen.getByText(/Printer model.*will be automatically extracted/i)).toBeInTheDocument();
  517. });
  518. });
  519. it('shows STL thumbnail option when STL file is added', async () => {
  520. const user = userEvent.setup();
  521. render(<FileManagerPage />);
  522. await waitFor(() => {
  523. expect(screen.getByText('Upload')).toBeInTheDocument();
  524. });
  525. await user.click(screen.getByText('Upload'));
  526. await waitFor(() => {
  527. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  528. });
  529. const stlFile = new File(['solid test'], 'model.stl', { type: 'application/sla' });
  530. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  531. expect(fileInput).toBeInTheDocument();
  532. await user.upload(fileInput, stlFile);
  533. await waitFor(() => {
  534. expect(screen.getByText('STL thumbnail generation')).toBeInTheDocument();
  535. expect(screen.getByText(/Thumbnails can be generated/i)).toBeInTheDocument();
  536. });
  537. });
  538. it('shows ZIP options when ZIP file is added', async () => {
  539. const user = userEvent.setup();
  540. render(<FileManagerPage />);
  541. await waitFor(() => {
  542. expect(screen.getByText('Upload')).toBeInTheDocument();
  543. });
  544. await user.click(screen.getByText('Upload'));
  545. await waitFor(() => {
  546. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  547. });
  548. const zipFile = new File(['pk'], 'models.zip', { type: 'application/zip' });
  549. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  550. await user.upload(fileInput, zipFile);
  551. await waitFor(() => {
  552. expect(screen.getByText('ZIP files detected')).toBeInTheDocument();
  553. expect(screen.getByText(/Preserve folder structure/)).toBeInTheDocument();
  554. });
  555. });
  556. it('can add a file via the file input', async () => {
  557. const user = userEvent.setup();
  558. render(<FileManagerPage />);
  559. await waitFor(() => {
  560. expect(screen.getByText('Upload')).toBeInTheDocument();
  561. });
  562. await user.click(screen.getByText('Upload'));
  563. await waitFor(() => {
  564. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  565. });
  566. const file = new File(['content'], 'model.3mf', { type: 'application/octet-stream' });
  567. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  568. await user.upload(fileInput, file);
  569. await waitFor(() => {
  570. expect(screen.getByText('model.3mf')).toBeInTheDocument();
  571. expect(screen.getByRole('button', { name: /Upload \(1\)/i })).toBeInTheDocument();
  572. });
  573. });
  574. it('uploads file and refreshes file list', async () => {
  575. server.use(
  576. http.post('/api/v1/library/files', () => {
  577. return HttpResponse.json({
  578. id: 10,
  579. filename: 'uploaded.3mf',
  580. file_type: '3mf',
  581. file_size: 1024,
  582. thumbnail_path: null,
  583. duplicate_of: null,
  584. metadata: null,
  585. });
  586. })
  587. );
  588. const user = userEvent.setup();
  589. render(<FileManagerPage />);
  590. await waitFor(() => {
  591. expect(screen.getByText('Upload')).toBeInTheDocument();
  592. });
  593. await user.click(screen.getByText('Upload'));
  594. await waitFor(() => {
  595. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  596. });
  597. const file = new File(['content'], 'uploaded.3mf', { type: 'application/octet-stream' });
  598. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  599. await user.upload(fileInput, file);
  600. const uploadButton = screen.getByRole('button', { name: /Upload \(1\)/i });
  601. await user.click(uploadButton);
  602. // Modal should auto-close after upload completes
  603. await waitFor(() => {
  604. expect(screen.queryByText('Upload Files')).not.toBeInTheDocument();
  605. });
  606. });
  607. });
  608. describe('authentication-based UI changes', () => {
  609. it('hides "Uploaded By" column and user filter when auth is disabled', async () => {
  610. // Mock auth disabled (default)
  611. server.use(
  612. http.get('*/api/v1/auth/status', () => {
  613. return HttpResponse.json({
  614. auth_enabled: false,
  615. requires_setup: false,
  616. });
  617. }),
  618. http.get('/api/v1/library/files', () => {
  619. return HttpResponse.json([
  620. {
  621. id: 1,
  622. filename: 'test.3mf',
  623. file_path: '/library/test.3mf',
  624. file_size: 1048576,
  625. file_type: '3mf',
  626. folder_id: null,
  627. thumbnail_path: null,
  628. print_name: 'Test File',
  629. print_time_seconds: 3600,
  630. print_count: 0,
  631. duplicate_count: 0,
  632. created_at: '2024-01-01T00:00:00Z',
  633. created_by_username: 'testuser',
  634. },
  635. ]);
  636. })
  637. );
  638. render(<FileManagerPage />);
  639. // Switch to list view to see the column headers
  640. await waitFor(() => {
  641. expect(screen.getByText('Test File')).toBeInTheDocument();
  642. });
  643. const user = userEvent.setup();
  644. const listViewButton = screen.getByRole('button', { name: /list/i });
  645. await user.click(listViewButton);
  646. // "Uploaded By" column header should not be present
  647. await waitFor(() => {
  648. expect(screen.queryByText('Uploaded By')).not.toBeInTheDocument();
  649. });
  650. // User filter dropdown should not be present
  651. expect(screen.queryByPlaceholderText('Filter by user')).not.toBeInTheDocument();
  652. });
  653. it('shows "Uploaded By" column and user filter when auth is enabled', async () => {
  654. // Mock auth enabled
  655. server.use(
  656. http.get('*/api/v1/auth/status', () => {
  657. return HttpResponse.json({
  658. auth_enabled: true,
  659. requires_setup: false,
  660. });
  661. }),
  662. http.get('/api/v1/library/files', () => {
  663. return HttpResponse.json([
  664. {
  665. id: 1,
  666. filename: 'test.3mf',
  667. file_path: '/library/test.3mf',
  668. file_size: 1048576,
  669. file_type: '3mf',
  670. folder_id: null,
  671. thumbnail_path: null,
  672. print_name: 'Test File',
  673. print_time_seconds: 3600,
  674. print_count: 0,
  675. duplicate_count: 0,
  676. created_at: '2024-01-01T00:00:00Z',
  677. created_by_username: 'testuser',
  678. },
  679. ]);
  680. }),
  681. http.get('/api/v1/users/', () => {
  682. return HttpResponse.json([
  683. { id: 1, username: 'testuser' },
  684. { id: 2, username: 'admin' },
  685. ]);
  686. })
  687. );
  688. render(<FileManagerPage />);
  689. // Switch to list view to see the column headers
  690. await waitFor(() => {
  691. expect(screen.getByText('Test File')).toBeInTheDocument();
  692. });
  693. const user = userEvent.setup();
  694. const listViewButton = screen.getByRole('button', { name: /list/i });
  695. await user.click(listViewButton);
  696. // "Uploaded By" column header should be present
  697. await waitFor(() => {
  698. expect(screen.getByText('Uploaded By')).toBeInTheDocument();
  699. });
  700. // User filter dropdown should be present
  701. expect(screen.getByPlaceholderText('Filter by user')).toBeInTheDocument();
  702. // Username should be displayed in the column
  703. expect(screen.getByText('testuser')).toBeInTheDocument();
  704. });
  705. });
  706. describe('folder tree collapse preference (#996)', () => {
  707. // localStorage is globally mocked in setup.ts (returns undefined by default),
  708. // so we program each test's getItem return value explicitly.
  709. const getItemMock = localStorage.getItem as ReturnType<typeof vi.fn>;
  710. const setItemMock = localStorage.setItem as ReturnType<typeof vi.fn>;
  711. beforeEach(() => {
  712. getItemMock.mockReset();
  713. setItemMock.mockReset();
  714. });
  715. it('defaults to expanded (nested folders visible) when library-collapse-folders is unset', async () => {
  716. getItemMock.mockReturnValue(null);
  717. render(<FileManagerPage />);
  718. await waitFor(() => {
  719. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  720. });
  721. expect(screen.getByText('Brackets')).toBeInTheDocument();
  722. });
  723. it('honors library-collapse-folders=true on load (nested folders hidden)', async () => {
  724. getItemMock.mockImplementation((key: string) =>
  725. key === 'library-collapse-folders' ? 'true' : null
  726. );
  727. render(<FileManagerPage />);
  728. await waitFor(() => {
  729. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  730. });
  731. expect(screen.queryByText('Brackets')).not.toBeInTheDocument();
  732. });
  733. it('collapses nested folders and persists preference when Collapse is clicked', async () => {
  734. getItemMock.mockReturnValue(null);
  735. const user = userEvent.setup();
  736. render(<FileManagerPage />);
  737. await waitFor(() => {
  738. expect(screen.getByText('Brackets')).toBeInTheDocument();
  739. });
  740. // The Collapse button sits next to Wrap in the sidebar header.
  741. // Its text content is "Collapse" (from fileManager.collapse).
  742. await user.click(screen.getByRole('button', { name: 'Collapse' }));
  743. await waitFor(() => {
  744. expect(screen.queryByText('Brackets')).not.toBeInTheDocument();
  745. });
  746. expect(setItemMock).toHaveBeenCalledWith('library-collapse-folders', 'true');
  747. });
  748. it('re-expands nested folders and persists preference when Collapse is toggled off', async () => {
  749. getItemMock.mockImplementation((key: string) =>
  750. key === 'library-collapse-folders' ? 'true' : null
  751. );
  752. const user = userEvent.setup();
  753. render(<FileManagerPage />);
  754. await waitFor(() => {
  755. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  756. });
  757. expect(screen.queryByText('Brackets')).not.toBeInTheDocument();
  758. await user.click(screen.getByRole('button', { name: 'Collapse' }));
  759. await waitFor(() => {
  760. expect(screen.getByText('Brackets')).toBeInTheDocument();
  761. });
  762. expect(setItemMock).toHaveBeenCalledWith('library-collapse-folders', 'false');
  763. });
  764. });
  765. describe('Internal / External top-level views (#1621)', () => {
  766. const externalMockFolders = [
  767. ...mockFolders,
  768. {
  769. id: 99,
  770. name: 'NAS Library',
  771. parent_id: null,
  772. file_count: 200,
  773. project_id: null,
  774. archive_id: null,
  775. project_name: null,
  776. archive_name: null,
  777. is_external: true,
  778. external_readonly: false,
  779. external_path: '/mnt/nas',
  780. children: [],
  781. },
  782. ];
  783. it('shows the External sidebar entry only when at least one external folder is linked', async () => {
  784. // Default mockFolders have no is_external entries → no External row.
  785. const { unmount } = render(<FileManagerPage />);
  786. await waitFor(() => {
  787. expect(screen.getByText('All Files')).toBeInTheDocument();
  788. });
  789. expect(screen.queryByText('External')).not.toBeInTheDocument();
  790. unmount();
  791. // With an external folder linked, the row appears.
  792. server.use(
  793. http.get('/api/v1/library/folders', () => HttpResponse.json(externalMockFolders)),
  794. );
  795. render(<FileManagerPage />);
  796. await waitFor(() => {
  797. expect(screen.getByText('External')).toBeInTheDocument();
  798. });
  799. });
  800. it('sends internal_only=true by default ("All Files" = managed storage only)', async () => {
  801. const scopes: string[] = [];
  802. server.use(
  803. http.get('/api/v1/library/folders', () => HttpResponse.json(externalMockFolders)),
  804. http.get('/api/v1/library/files', ({ request }) => {
  805. const url = new URL(request.url);
  806. scopes.push(
  807. url.searchParams.get('internal_only') === 'true'
  808. ? 'internal'
  809. : url.searchParams.get('external_only') === 'true'
  810. ? 'external'
  811. : 'all',
  812. );
  813. return HttpResponse.json(mockFiles);
  814. }),
  815. );
  816. render(<FileManagerPage />);
  817. await waitFor(() => {
  818. expect(scopes).toContain('internal');
  819. });
  820. });
  821. it('switches to external_only=true when the External sidebar entry is clicked', async () => {
  822. const scopes: string[] = [];
  823. server.use(
  824. http.get('/api/v1/library/folders', () => HttpResponse.json(externalMockFolders)),
  825. http.get('/api/v1/library/files', ({ request }) => {
  826. const url = new URL(request.url);
  827. scopes.push(
  828. url.searchParams.get('internal_only') === 'true'
  829. ? 'internal'
  830. : url.searchParams.get('external_only') === 'true'
  831. ? 'external'
  832. : 'all',
  833. );
  834. return HttpResponse.json([]);
  835. }),
  836. );
  837. const { default: userEvent } = await import('@testing-library/user-event');
  838. const user = userEvent.setup();
  839. render(<FileManagerPage />);
  840. await waitFor(() => expect(screen.getByText('External')).toBeInTheDocument());
  841. await user.click(screen.getByText('External'));
  842. await waitFor(() => {
  843. expect(scopes).toContain('external');
  844. });
  845. });
  846. });
  847. describe('"All Files" view (#1499)', () => {
  848. it('requests every file (include_root=false) so subfolder contents are visible', async () => {
  849. const rootFile = {
  850. id: 10,
  851. filename: 'root-file.3mf',
  852. file_path: '/library/root-file.3mf',
  853. file_size: 1024,
  854. file_type: '3mf',
  855. folder_id: null,
  856. thumbnail_path: null,
  857. print_name: 'Root File',
  858. print_time_seconds: 0,
  859. print_count: 0,
  860. duplicate_count: 0,
  861. created_at: '2024-01-01T00:00:00Z',
  862. };
  863. const nestedFile = {
  864. ...rootFile,
  865. id: 11,
  866. filename: 'nested-file.3mf',
  867. file_path: '/library/Functional Parts/nested-file.3mf',
  868. folder_id: 1,
  869. print_name: 'Nested File',
  870. };
  871. const includeRootValues: string[] = [];
  872. server.use(
  873. http.get('/api/v1/library/files', ({ request }) => {
  874. const url = new URL(request.url);
  875. const includeRoot = url.searchParams.get('include_root');
  876. includeRootValues.push(includeRoot ?? '');
  877. // Mirror the backend: include_root=false returns everything; true
  878. // returns only files with folder_id IS NULL.
  879. if (includeRoot === 'false') {
  880. return HttpResponse.json([rootFile, nestedFile]);
  881. }
  882. return HttpResponse.json([rootFile]);
  883. }),
  884. );
  885. render(<FileManagerPage />);
  886. await waitFor(() => {
  887. expect(screen.getByText('Root File')).toBeInTheDocument();
  888. expect(screen.getByText('Nested File')).toBeInTheDocument();
  889. });
  890. // Sanity-check: the buggy call would have sent include_root=true here.
  891. expect(includeRootValues).toContain('false');
  892. });
  893. });
  894. });