FileManagerPage.test.tsx 36 KB

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