FileManagerPage.test.tsx 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. /**
  2. * Tests for the FileManagerPage component.
  3. */
  4. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  5. import { screen, waitFor, within } 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 { openInSlicer } from '../../utils/slicer';
  10. import { setAuthToken } from '../../api/client';
  11. import { http, HttpResponse } from 'msw';
  12. import { server } from '../mocks/server';
  13. // Only the protocol-handler launch is stubbed — it would navigate the jsdom
  14. // window. Everything else in the module is a pure predicate, so keep the real
  15. // implementations: isSliceableFilename decides which rows even offer the
  16. // action these tests click.
  17. vi.mock('../../utils/slicer', async (importOriginal) => ({
  18. ...(await importOriginal<typeof import('../../utils/slicer')>()),
  19. openInSlicer: vi.fn(),
  20. }));
  21. vi.mock('../../components/SliceModal', () => ({
  22. SliceModal: ({ source }: { source: { filename: string } }) => (
  23. <div data-testid="slice-modal">{source.filename}</div>
  24. ),
  25. }));
  26. // Mock data
  27. const mockFolders = [
  28. {
  29. id: 1,
  30. name: 'Functional Parts',
  31. parent_id: null,
  32. file_count: 5,
  33. project_id: null,
  34. archive_id: null,
  35. project_name: null,
  36. archive_name: null,
  37. // #2680: distinctive year so the folder-pane display test can assert on it
  38. // without colliding with the file mtimes below.
  39. latest_activity_at: '2031-04-05T10:00:00Z',
  40. children: [
  41. {
  42. id: 2,
  43. name: 'Brackets',
  44. parent_id: 1,
  45. file_count: 3,
  46. project_id: null,
  47. archive_id: null,
  48. project_name: null,
  49. archive_name: null,
  50. latest_activity_at: '2032-06-07T10:00:00Z',
  51. children: [],
  52. },
  53. ],
  54. },
  55. {
  56. id: 3,
  57. name: 'Art Projects',
  58. parent_id: null,
  59. file_count: 2,
  60. project_id: 1,
  61. archive_id: null,
  62. project_name: 'My Art Project',
  63. archive_name: null,
  64. // No activity timestamp — must render no date line rather than an
  65. // "Invalid Date" placeholder.
  66. latest_activity_at: null,
  67. children: [],
  68. },
  69. ];
  70. const mockFiles = [
  71. {
  72. id: 1,
  73. filename: 'benchy.gcode.3mf',
  74. file_path: '/library/benchy.gcode.3mf',
  75. file_size: 1048576,
  76. file_type: '3mf',
  77. folder_id: null,
  78. thumbnail_path: '/thumbnails/1.png',
  79. print_name: 'Benchy',
  80. print_time_seconds: 3600,
  81. print_count: 5,
  82. duplicate_count: 0,
  83. created_at: '2024-01-01T00:00:00Z',
  84. // #2680: real on-disk mtime in a distinctive year so the display test can
  85. // prove fs_modified_at is preferred over created_at (2024).
  86. fs_modified_at: '2030-06-15T12:00:00Z',
  87. },
  88. {
  89. id: 2,
  90. filename: 'bracket.stl',
  91. file_path: '/library/bracket.stl',
  92. file_size: 524288,
  93. file_type: 'stl',
  94. folder_id: null,
  95. thumbnail_path: null,
  96. print_name: null,
  97. print_time_seconds: null,
  98. print_count: 0,
  99. duplicate_count: 2,
  100. created_at: '2024-01-02T00:00:00Z',
  101. },
  102. {
  103. id: 3,
  104. filename: 'cube.gcode.3mf',
  105. file_path: '/library/cube.gcode.3mf',
  106. file_size: 2048576,
  107. file_type: '3mf',
  108. folder_id: null,
  109. thumbnail_path: '/thumbnails/3.png',
  110. print_name: 'Cube',
  111. print_time_seconds: 1800,
  112. print_count: 2,
  113. duplicate_count: 0,
  114. created_at: '2024-01-03T00:00:00Z',
  115. },
  116. ];
  117. const mockStats = {
  118. total_files: 10,
  119. total_folders: 3,
  120. total_size_bytes: 104857600,
  121. disk_free_bytes: 10737418240,
  122. disk_total_bytes: 107374182400,
  123. };
  124. describe('FileManagerPage', () => {
  125. beforeEach(() => {
  126. // Clear localStorage to ensure consistent view mode
  127. localStorage.clear();
  128. server.use(
  129. http.get('/api/v1/library/folders', () => {
  130. return HttpResponse.json(mockFolders);
  131. }),
  132. http.get('/api/v1/library/files', () => {
  133. return HttpResponse.json(mockFiles);
  134. }),
  135. http.get('/api/v1/library/stats', () => {
  136. return HttpResponse.json(mockStats);
  137. }),
  138. http.get('/api/v1/settings/', () => {
  139. return HttpResponse.json({
  140. check_updates: false,
  141. check_printer_firmware: false,
  142. library_disk_warning_gb: 5,
  143. });
  144. }),
  145. http.post('/api/v1/library/folders', async ({ request }) => {
  146. const body = await request.json() as { name: string };
  147. return HttpResponse.json({ id: 4, name: body.name, parent_id: null, children: [] });
  148. }),
  149. http.delete('/api/v1/library/folders/:id', () => {
  150. return HttpResponse.json({ success: true });
  151. }),
  152. http.delete('/api/v1/library/files/:id', () => {
  153. return HttpResponse.json({ success: true });
  154. }),
  155. http.post('/api/v1/library/files/move', () => {
  156. return HttpResponse.json({ success: true });
  157. }),
  158. http.post('/api/v1/library/files/add-to-queue', () => {
  159. return HttpResponse.json({ added: [{ file_id: 1, queue_id: 1 }], errors: [] });
  160. }),
  161. http.get('/api/v1/projects/', () => {
  162. return HttpResponse.json([{ id: 1, name: 'Test Project', color: '#00ae42' }]);
  163. }),
  164. http.get('/api/v1/archives/', () => {
  165. return HttpResponse.json([{ id: 1, print_name: 'Test Archive', filename: 'test.3mf' }]);
  166. })
  167. );
  168. });
  169. describe('rendering', () => {
  170. it('renders the page title', async () => {
  171. render(<FileManagerPage />);
  172. await waitFor(() => {
  173. expect(screen.getByText('File Manager')).toBeInTheDocument();
  174. });
  175. });
  176. it('renders the page description', async () => {
  177. render(<FileManagerPage />);
  178. await waitFor(() => {
  179. expect(screen.getByText('Organize and manage your print files')).toBeInTheDocument();
  180. });
  181. });
  182. it('shows New Folder button', async () => {
  183. render(<FileManagerPage />);
  184. await waitFor(() => {
  185. expect(screen.getByText('New Folder')).toBeInTheDocument();
  186. });
  187. });
  188. it('shows Upload button', async () => {
  189. render(<FileManagerPage />);
  190. await waitFor(() => {
  191. expect(screen.getByText('Upload')).toBeInTheDocument();
  192. });
  193. });
  194. });
  195. describe('stats display', () => {
  196. it('shows file count', async () => {
  197. render(<FileManagerPage />);
  198. await waitFor(() => {
  199. expect(screen.getByText('Files:')).toBeInTheDocument();
  200. expect(screen.getByText('10')).toBeInTheDocument();
  201. });
  202. });
  203. it('shows folder count', async () => {
  204. render(<FileManagerPage />);
  205. await waitFor(() => {
  206. expect(screen.getByText('Folders:')).toBeInTheDocument();
  207. // Folder count appears multiple places, just verify the label is present
  208. const foldersLabel = screen.getByText('Folders:');
  209. expect(foldersLabel.nextElementSibling?.textContent).toBe('3');
  210. });
  211. });
  212. it('shows total size', async () => {
  213. render(<FileManagerPage />);
  214. await waitFor(() => {
  215. expect(screen.getByText('Size:')).toBeInTheDocument();
  216. expect(screen.getByText('100.0 MB')).toBeInTheDocument();
  217. });
  218. });
  219. it('shows free space', async () => {
  220. render(<FileManagerPage />);
  221. await waitFor(() => {
  222. expect(screen.getByText('Free:')).toBeInTheDocument();
  223. });
  224. });
  225. });
  226. describe('folder sidebar', () => {
  227. it('shows All Files option', async () => {
  228. render(<FileManagerPage />);
  229. await waitFor(() => {
  230. expect(screen.getByText('All Files')).toBeInTheDocument();
  231. });
  232. });
  233. it('shows folder tree', async () => {
  234. render(<FileManagerPage />);
  235. await waitFor(() => {
  236. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  237. expect(screen.getByText('Art Projects')).toBeInTheDocument();
  238. });
  239. });
  240. it('shows nested folders', async () => {
  241. render(<FileManagerPage />);
  242. await waitFor(() => {
  243. expect(screen.getByText('Brackets')).toBeInTheDocument();
  244. });
  245. });
  246. it('shows linked folder indicator', async () => {
  247. render(<FileManagerPage />);
  248. await waitFor(() => {
  249. // Art Projects has a project_id
  250. expect(screen.getByText('Art Projects')).toBeInTheDocument();
  251. });
  252. });
  253. });
  254. describe('file display', () => {
  255. it('shows files in grid', async () => {
  256. render(<FileManagerPage />);
  257. await waitFor(() => {
  258. expect(screen.getByText('Benchy')).toBeInTheDocument();
  259. });
  260. });
  261. it('shows file type badges', async () => {
  262. render(<FileManagerPage />);
  263. await waitFor(() => {
  264. // File type badges show uppercase type
  265. expect(screen.getAllByText('3MF').length).toBeGreaterThan(0);
  266. expect(screen.getAllByText('STL').length).toBeGreaterThan(0);
  267. });
  268. });
  269. it('shows print count', async () => {
  270. render(<FileManagerPage />);
  271. await waitFor(() => {
  272. expect(screen.getByText('Printed 5x')).toBeInTheDocument();
  273. });
  274. });
  275. it('shows duplicate badge', async () => {
  276. render(<FileManagerPage />);
  277. await waitFor(() => {
  278. // Duplicate badge shows count, there may be multiple "2"s on the page
  279. // so we check that at least one element with "2" exists
  280. const elements = screen.getAllByText('2');
  281. expect(elements.length).toBeGreaterThan(0);
  282. });
  283. });
  284. });
  285. describe('view modes', () => {
  286. it('has grid view button', async () => {
  287. render(<FileManagerPage />);
  288. await waitFor(() => {
  289. expect(screen.getByTitle('Grid view')).toBeInTheDocument();
  290. });
  291. });
  292. it('has list view button', async () => {
  293. render(<FileManagerPage />);
  294. await waitFor(() => {
  295. expect(screen.getByTitle('List view')).toBeInTheDocument();
  296. });
  297. });
  298. it('can switch to list view', async () => {
  299. const user = userEvent.setup();
  300. render(<FileManagerPage />);
  301. // Wait for files to load first
  302. await waitFor(() => {
  303. expect(screen.getByText('Benchy')).toBeInTheDocument();
  304. });
  305. // Both view mode buttons should be present and clickable
  306. const gridButton = screen.getByTitle('Grid view');
  307. const listButton = screen.getByTitle('List view');
  308. expect(gridButton).toBeInTheDocument();
  309. expect(listButton).toBeInTheDocument();
  310. // Click list view button - verify no errors occur
  311. await user.click(listButton);
  312. // Clicking grid button should also work
  313. await user.click(gridButton);
  314. // Verify files are still displayed after toggling
  315. expect(screen.getByText('Benchy')).toBeInTheDocument();
  316. });
  317. });
  318. describe('search and filter', () => {
  319. it('has search input', async () => {
  320. render(<FileManagerPage />);
  321. await waitFor(() => {
  322. expect(screen.getByPlaceholderText('Search files...')).toBeInTheDocument();
  323. });
  324. });
  325. it('has type filter', async () => {
  326. render(<FileManagerPage />);
  327. await waitFor(() => {
  328. expect(screen.getByText('All types')).toBeInTheDocument();
  329. });
  330. });
  331. it('has sort options', async () => {
  332. render(<FileManagerPage />);
  333. await waitFor(() => {
  334. // Sort dropdown should show Name as default option (persisted to localStorage)
  335. expect(screen.getByDisplayValue('Name')).toBeInTheDocument();
  336. });
  337. });
  338. });
  339. describe('selection', () => {
  340. it('shows select all button', async () => {
  341. render(<FileManagerPage />);
  342. await waitFor(() => {
  343. expect(screen.getByText('Select All')).toBeInTheDocument();
  344. });
  345. });
  346. it('can select files', async () => {
  347. const user = userEvent.setup();
  348. render(<FileManagerPage />);
  349. await waitFor(() => {
  350. expect(screen.getByText('Benchy')).toBeInTheDocument();
  351. });
  352. // Click on the file card to select it
  353. const fileCard = screen.getByText('Benchy').closest('div[class*="cursor-pointer"]');
  354. if (fileCard) {
  355. await user.click(fileCard);
  356. }
  357. await waitFor(() => {
  358. expect(screen.getByText('1 selected')).toBeInTheDocument();
  359. });
  360. });
  361. it('shows bulk actions when files selected', async () => {
  362. const user = userEvent.setup();
  363. render(<FileManagerPage />);
  364. await waitFor(() => {
  365. expect(screen.getByText('Select All')).toBeInTheDocument();
  366. });
  367. await user.click(screen.getByText('Select All'));
  368. await waitFor(() => {
  369. expect(screen.getByText('Move')).toBeInTheDocument();
  370. expect(screen.getByText('Delete')).toBeInTheDocument();
  371. });
  372. });
  373. });
  374. describe('new folder modal', () => {
  375. it('opens new folder modal', async () => {
  376. const user = userEvent.setup();
  377. render(<FileManagerPage />);
  378. await waitFor(() => {
  379. expect(screen.getByText('New Folder')).toBeInTheDocument();
  380. });
  381. await user.click(screen.getByText('New Folder'));
  382. await waitFor(() => {
  383. expect(screen.getByText('Folder Name')).toBeInTheDocument();
  384. expect(screen.getByPlaceholderText('e.g., Functional Parts')).toBeInTheDocument();
  385. });
  386. });
  387. it('can create a folder', async () => {
  388. const user = userEvent.setup();
  389. render(<FileManagerPage />);
  390. await waitFor(() => {
  391. expect(screen.getByText('New Folder')).toBeInTheDocument();
  392. });
  393. await user.click(screen.getByText('New Folder'));
  394. await waitFor(() => {
  395. expect(screen.getByPlaceholderText('e.g., Functional Parts')).toBeInTheDocument();
  396. });
  397. const input = screen.getByPlaceholderText('e.g., Functional Parts');
  398. await user.type(input, 'My New Folder');
  399. const createButton = screen.getByRole('button', { name: 'Create' });
  400. await user.click(createButton);
  401. // Modal should close after creation
  402. await waitFor(() => {
  403. expect(screen.queryByText('Folder Name')).not.toBeInTheDocument();
  404. });
  405. });
  406. });
  407. describe('empty state', () => {
  408. it('shows empty state when no files', async () => {
  409. server.use(
  410. http.get('/api/v1/library/files', () => {
  411. return HttpResponse.json([]);
  412. })
  413. );
  414. render(<FileManagerPage />);
  415. await waitFor(() => {
  416. expect(screen.getByText('No files yet')).toBeInTheDocument();
  417. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  418. });
  419. });
  420. });
  421. describe('bulk-action print button', () => {
  422. // PR #1625 consolidated print actions: the old single-file-selected
  423. // "Schedule" button now opens the unified PrintModal (which carries
  424. // schedule options inside). The bulk-action toolbar shows a single
  425. // "Print" button only when exactly one sliced file is selected, and
  426. // hides it for multi-selection. The button is targeted by its accessible
  427. // name ("Print") + role to disambiguate from the file-card dropdown's
  428. // own Print entry, which stays collapsed unless its kebab is opened.
  429. it('shows a Print button in the bulk toolbar when one sliced file is selected', async () => {
  430. const user = userEvent.setup();
  431. render(<FileManagerPage />);
  432. await waitFor(() => {
  433. expect(screen.getByText('Benchy')).toBeInTheDocument();
  434. });
  435. // Select a sliced file (benchy.gcode.3mf) by clicking on its card
  436. const fileCard = screen.getByText('Benchy').closest('div[class*="cursor-pointer"]');
  437. if (fileCard) {
  438. await user.click(fileCard);
  439. }
  440. await waitFor(() => {
  441. expect(screen.getByRole('button', { name: /^Print$/ })).toBeInTheDocument();
  442. });
  443. });
  444. it('hides the bulk Print button when multiple files are selected', async () => {
  445. const user = userEvent.setup();
  446. render(<FileManagerPage />);
  447. await waitFor(() => {
  448. expect(screen.getByText('Select All')).toBeInTheDocument();
  449. });
  450. // Select all files
  451. await user.click(screen.getByText('Select All'));
  452. await waitFor(() => {
  453. expect(screen.queryByRole('button', { name: /^Print$/ })).not.toBeInTheDocument();
  454. });
  455. });
  456. });
  457. describe('STL thumbnail generation', () => {
  458. it('shows Generate Thumbnails button', async () => {
  459. render(<FileManagerPage />);
  460. await waitFor(() => {
  461. expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
  462. });
  463. });
  464. it('Generate Thumbnails button has correct title', async () => {
  465. render(<FileManagerPage />);
  466. await waitFor(() => {
  467. const button = screen.getByTitle('Generate thumbnails for STL and PDF files missing them');
  468. expect(button).toBeInTheDocument();
  469. });
  470. });
  471. it('can click Generate Thumbnails button', async () => {
  472. const user = userEvent.setup();
  473. server.use(
  474. http.post('/api/v1/library/generate-stl-thumbnails', () => {
  475. return HttpResponse.json({
  476. processed: 1,
  477. succeeded: 1,
  478. failed: 0,
  479. results: [{ file_id: 2, success: true }],
  480. });
  481. })
  482. );
  483. render(<FileManagerPage />);
  484. await waitFor(() => {
  485. expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
  486. });
  487. const button = screen.getByText('Generate Thumbnails');
  488. await user.click(button);
  489. // Button should work without error
  490. await waitFor(() => {
  491. expect(screen.getByText('Generate Thumbnails')).toBeInTheDocument();
  492. });
  493. });
  494. it('shows STL file without thumbnail in file list', async () => {
  495. render(<FileManagerPage />);
  496. await waitFor(() => {
  497. // bracket.stl has no thumbnail_path
  498. expect(screen.getByText('bracket.stl')).toBeInTheDocument();
  499. expect(screen.getAllByText('STL').length).toBeGreaterThan(0);
  500. });
  501. });
  502. // Since #2976 the server renders PDF thumbnails too, so the per-file
  503. // action is offered for PDFs and stays hidden for types it cannot render.
  504. describe('per-file action for PDFs', () => {
  505. const pdfFile = {
  506. id: 40,
  507. filename: 'drawing.pdf',
  508. file_path: '/library/drawing.pdf',
  509. file_size: 4096,
  510. file_type: 'pdf',
  511. folder_id: null,
  512. thumbnail_path: null,
  513. print_name: null,
  514. print_time_seconds: null,
  515. print_count: 0,
  516. duplicate_count: 0,
  517. created_at: '2024-01-04T00:00:00Z',
  518. };
  519. const stepFile = { ...pdfFile, id: 41, filename: 'part.step', file_path: '/library/part.step', file_type: 'step' };
  520. beforeEach(() => {
  521. server.use(
  522. http.get('/api/v1/library/files', () => HttpResponse.json([...mockFiles, pdfFile, stepFile])),
  523. );
  524. });
  525. const openMenu = async (user: ReturnType<typeof userEvent.setup>, filename: string) => {
  526. const card = screen.getByText(filename).closest('.group') as HTMLElement;
  527. const kebab = card.querySelector('.lucide-ellipsis-vertical')?.closest('button') as HTMLButtonElement;
  528. await user.click(kebab);
  529. return card;
  530. };
  531. it('offers Generate Thumbnail in the card menu of a PDF', async () => {
  532. const user = userEvent.setup();
  533. server.use(
  534. http.post('/api/v1/library/generate-stl-thumbnails', async ({ request }) => {
  535. const body = (await request.json()) as { file_ids?: number[] };
  536. return HttpResponse.json({
  537. processed: 1,
  538. succeeded: 1,
  539. failed: 0,
  540. results: [{ file_id: body.file_ids?.[0], success: true }],
  541. });
  542. })
  543. );
  544. render(<FileManagerPage />);
  545. await waitFor(() => expect(screen.getByText('drawing.pdf')).toBeInTheDocument());
  546. const card = await openMenu(user, 'drawing.pdf');
  547. await user.click(within(card).getByText('Generate Thumbnail'));
  548. expect(await screen.findByText('Thumbnail generated')).toBeInTheDocument();
  549. });
  550. it('does not offer it for STEP, which only the browser can render', async () => {
  551. const user = userEvent.setup();
  552. render(<FileManagerPage />);
  553. await waitFor(() => expect(screen.getByText('part.step')).toBeInTheDocument());
  554. const card = await openMenu(user, 'part.step');
  555. expect(within(card).queryByText('Generate Thumbnail')).not.toBeInTheDocument();
  556. });
  557. it('offers the action in the list view strip of a PDF', async () => {
  558. const user = userEvent.setup();
  559. render(<FileManagerPage />);
  560. await waitFor(() => expect(screen.getByText('drawing.pdf')).toBeInTheDocument());
  561. await user.click(screen.getByRole('button', { name: /list/i }));
  562. // List rows are CSS grids; the row is the nearest grid ancestor.
  563. await waitFor(() => {
  564. const row = screen.getByText('drawing.pdf').closest('.grid') as HTMLElement;
  565. expect(within(row).getByTitle('Generate Thumbnail')).toBeInTheDocument();
  566. });
  567. const stepRow = screen.getByText('part.step').closest('.grid') as HTMLElement;
  568. expect(within(stepRow).queryByTitle('Generate Thumbnail')).not.toBeInTheDocument();
  569. });
  570. });
  571. });
  572. describe('upload modal (FileUploadModal)', () => {
  573. it('opens upload modal when Upload button is clicked', async () => {
  574. const user = userEvent.setup();
  575. render(<FileManagerPage />);
  576. await waitFor(() => {
  577. expect(screen.getByText('Upload')).toBeInTheDocument();
  578. });
  579. await user.click(screen.getByText('Upload'));
  580. await waitFor(() => {
  581. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  582. expect(screen.getByText(/Drag & drop/)).toBeInTheDocument();
  583. });
  584. });
  585. it('closes upload modal when Cancel is clicked', async () => {
  586. const user = userEvent.setup();
  587. render(<FileManagerPage />);
  588. await waitFor(() => {
  589. expect(screen.getByText('Upload')).toBeInTheDocument();
  590. });
  591. await user.click(screen.getByText('Upload'));
  592. await waitFor(() => {
  593. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  594. });
  595. await user.click(screen.getByRole('button', { name: 'Cancel' }));
  596. await waitFor(() => {
  597. expect(screen.queryByText('Upload Files')).not.toBeInTheDocument();
  598. });
  599. });
  600. it('shows 3MF extraction info when 3MF file is added', async () => {
  601. const user = userEvent.setup();
  602. render(<FileManagerPage />);
  603. await waitFor(() => {
  604. expect(screen.getByText('Upload')).toBeInTheDocument();
  605. });
  606. await user.click(screen.getByText('Upload'));
  607. await waitFor(() => {
  608. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  609. });
  610. const threemfFile = new File(['content'], 'model.gcode.3mf', { type: 'application/octet-stream' });
  611. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  612. expect(fileInput).toBeInTheDocument();
  613. await user.upload(fileInput, threemfFile);
  614. await waitFor(() => {
  615. expect(screen.getByText('3MF files detected')).toBeInTheDocument();
  616. expect(screen.getByText(/Printer model.*will be automatically extracted/i)).toBeInTheDocument();
  617. });
  618. });
  619. it('shows STL thumbnail option when STL file is added', async () => {
  620. const user = userEvent.setup();
  621. render(<FileManagerPage />);
  622. await waitFor(() => {
  623. expect(screen.getByText('Upload')).toBeInTheDocument();
  624. });
  625. await user.click(screen.getByText('Upload'));
  626. await waitFor(() => {
  627. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  628. });
  629. const stlFile = new File(['solid test'], 'model.stl', { type: 'application/sla' });
  630. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  631. expect(fileInput).toBeInTheDocument();
  632. await user.upload(fileInput, stlFile);
  633. await waitFor(() => {
  634. expect(screen.getByText('STL thumbnail generation')).toBeInTheDocument();
  635. expect(screen.getByText(/Thumbnails can be generated/i)).toBeInTheDocument();
  636. });
  637. });
  638. it('shows ZIP options when ZIP file is added', async () => {
  639. const user = userEvent.setup();
  640. render(<FileManagerPage />);
  641. await waitFor(() => {
  642. expect(screen.getByText('Upload')).toBeInTheDocument();
  643. });
  644. await user.click(screen.getByText('Upload'));
  645. await waitFor(() => {
  646. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  647. });
  648. const zipFile = new File(['pk'], 'models.zip', { type: 'application/zip' });
  649. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  650. await user.upload(fileInput, zipFile);
  651. await waitFor(() => {
  652. expect(screen.getByText('ZIP files detected')).toBeInTheDocument();
  653. expect(screen.getByText(/Preserve folder structure/)).toBeInTheDocument();
  654. });
  655. });
  656. it('can add a file via the file input', async () => {
  657. const user = userEvent.setup();
  658. render(<FileManagerPage />);
  659. await waitFor(() => {
  660. expect(screen.getByText('Upload')).toBeInTheDocument();
  661. });
  662. await user.click(screen.getByText('Upload'));
  663. await waitFor(() => {
  664. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  665. });
  666. const file = new File(['content'], 'model.3mf', { type: 'application/octet-stream' });
  667. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  668. await user.upload(fileInput, file);
  669. await waitFor(() => {
  670. expect(screen.getByText('model.3mf')).toBeInTheDocument();
  671. expect(screen.getByRole('button', { name: /Upload \(1\)/i })).toBeInTheDocument();
  672. });
  673. });
  674. it('uploads file and refreshes file list', async () => {
  675. server.use(
  676. http.post('/api/v1/library/files', () => {
  677. return HttpResponse.json({
  678. id: 10,
  679. filename: 'uploaded.3mf',
  680. file_type: '3mf',
  681. file_size: 1024,
  682. thumbnail_path: null,
  683. duplicate_of: null,
  684. metadata: null,
  685. });
  686. })
  687. );
  688. const user = userEvent.setup();
  689. render(<FileManagerPage />);
  690. await waitFor(() => {
  691. expect(screen.getByText('Upload')).toBeInTheDocument();
  692. });
  693. await user.click(screen.getByText('Upload'));
  694. await waitFor(() => {
  695. expect(screen.getByText('Upload Files')).toBeInTheDocument();
  696. });
  697. const file = new File(['content'], 'uploaded.3mf', { type: 'application/octet-stream' });
  698. const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
  699. await user.upload(fileInput, file);
  700. const uploadButton = screen.getByRole('button', { name: /Upload \(1\)/i });
  701. await user.click(uploadButton);
  702. // Modal should auto-close after upload completes
  703. await waitFor(() => {
  704. expect(screen.queryByText('Upload Files')).not.toBeInTheDocument();
  705. });
  706. });
  707. });
  708. describe('authentication-based UI changes', () => {
  709. it('hides "Uploaded By" column and user filter when auth is disabled', async () => {
  710. // Mock auth disabled (default)
  711. server.use(
  712. http.get('*/api/v1/auth/status', () => {
  713. return HttpResponse.json({
  714. auth_enabled: false,
  715. requires_setup: false,
  716. });
  717. }),
  718. http.get('/api/v1/library/files', () => {
  719. return HttpResponse.json([
  720. {
  721. id: 1,
  722. filename: 'test.3mf',
  723. file_path: '/library/test.3mf',
  724. file_size: 1048576,
  725. file_type: '3mf',
  726. folder_id: null,
  727. thumbnail_path: null,
  728. print_name: 'Test File',
  729. print_time_seconds: 3600,
  730. print_count: 0,
  731. duplicate_count: 0,
  732. created_at: '2024-01-01T00:00:00Z',
  733. created_by_username: 'testuser',
  734. },
  735. ]);
  736. })
  737. );
  738. render(<FileManagerPage />);
  739. // Switch to list view to see the column headers
  740. await waitFor(() => {
  741. expect(screen.getByText('Test File')).toBeInTheDocument();
  742. });
  743. const user = userEvent.setup();
  744. const listViewButton = screen.getByRole('button', { name: /list/i });
  745. await user.click(listViewButton);
  746. // "Uploaded By" column header should not be present
  747. await waitFor(() => {
  748. expect(screen.queryByText('Uploaded By')).not.toBeInTheDocument();
  749. });
  750. // User filter dropdown should not be present
  751. expect(screen.queryByPlaceholderText('Filter by user')).not.toBeInTheDocument();
  752. });
  753. it('shows "Uploaded By" column and user filter when auth is enabled', async () => {
  754. // Mock auth enabled
  755. server.use(
  756. http.get('*/api/v1/auth/status', () => {
  757. return HttpResponse.json({
  758. auth_enabled: true,
  759. requires_setup: false,
  760. });
  761. }),
  762. http.get('/api/v1/library/files', () => {
  763. return HttpResponse.json([
  764. {
  765. id: 1,
  766. filename: 'test.3mf',
  767. file_path: '/library/test.3mf',
  768. file_size: 1048576,
  769. file_type: '3mf',
  770. folder_id: null,
  771. thumbnail_path: null,
  772. print_name: 'Test File',
  773. print_time_seconds: 3600,
  774. print_count: 0,
  775. duplicate_count: 0,
  776. created_at: '2024-01-01T00:00:00Z',
  777. created_by_username: 'testuser',
  778. },
  779. ]);
  780. }),
  781. http.get('/api/v1/users/', () => {
  782. return HttpResponse.json([
  783. { id: 1, username: 'testuser' },
  784. { id: 2, username: 'admin' },
  785. ]);
  786. })
  787. );
  788. render(<FileManagerPage />);
  789. // Switch to list view to see the column headers
  790. await waitFor(() => {
  791. expect(screen.getByText('Test File')).toBeInTheDocument();
  792. });
  793. const user = userEvent.setup();
  794. const listViewButton = screen.getByRole('button', { name: /list/i });
  795. await user.click(listViewButton);
  796. // "Uploaded By" column header should be present
  797. await waitFor(() => {
  798. expect(screen.getByText('Uploaded By')).toBeInTheDocument();
  799. });
  800. // User filter dropdown should be present
  801. expect(screen.getByPlaceholderText('Filter by user')).toBeInTheDocument();
  802. // Username should be displayed in the column
  803. expect(screen.getByText('testuser')).toBeInTheDocument();
  804. });
  805. });
  806. describe('folder tree collapse preference (#996)', () => {
  807. // localStorage is globally mocked in setup.ts (returns undefined by default),
  808. // so we program each test's getItem return value explicitly.
  809. const getItemMock = localStorage.getItem as ReturnType<typeof vi.fn>;
  810. const setItemMock = localStorage.setItem as ReturnType<typeof vi.fn>;
  811. beforeEach(() => {
  812. getItemMock.mockReset();
  813. setItemMock.mockReset();
  814. });
  815. // The mock is module-global, so an implementation left behind here would
  816. // silently change every later describe (e.g. collapsing the folder tree).
  817. afterEach(() => {
  818. getItemMock.mockReset();
  819. setItemMock.mockReset();
  820. });
  821. it('defaults to expanded (nested folders visible) when library-collapse-folders is unset', async () => {
  822. getItemMock.mockReturnValue(null);
  823. render(<FileManagerPage />);
  824. await waitFor(() => {
  825. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  826. });
  827. expect(screen.getByText('Brackets')).toBeInTheDocument();
  828. });
  829. it('honors library-collapse-folders=true on load (nested folders hidden)', async () => {
  830. getItemMock.mockImplementation((key: string) =>
  831. key === 'library-collapse-folders' ? 'true' : null
  832. );
  833. render(<FileManagerPage />);
  834. await waitFor(() => {
  835. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  836. });
  837. expect(screen.queryByText('Brackets')).not.toBeInTheDocument();
  838. });
  839. it('collapses nested folders and persists preference when Collapse is clicked', async () => {
  840. getItemMock.mockReturnValue(null);
  841. const user = userEvent.setup();
  842. render(<FileManagerPage />);
  843. await waitFor(() => {
  844. expect(screen.getByText('Brackets')).toBeInTheDocument();
  845. });
  846. // The Collapse button sits next to Wrap in the sidebar header.
  847. // Its text content is "Collapse" (from fileManager.collapse).
  848. await user.click(screen.getByRole('button', { name: 'Collapse' }));
  849. await waitFor(() => {
  850. expect(screen.queryByText('Brackets')).not.toBeInTheDocument();
  851. });
  852. expect(setItemMock).toHaveBeenCalledWith('library-collapse-folders', 'true');
  853. });
  854. it('re-expands nested folders and persists preference when Collapse is toggled off', async () => {
  855. getItemMock.mockImplementation((key: string) =>
  856. key === 'library-collapse-folders' ? 'true' : null
  857. );
  858. const user = userEvent.setup();
  859. render(<FileManagerPage />);
  860. await waitFor(() => {
  861. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  862. });
  863. expect(screen.queryByText('Brackets')).not.toBeInTheDocument();
  864. await user.click(screen.getByRole('button', { name: 'Collapse' }));
  865. await waitFor(() => {
  866. expect(screen.getByText('Brackets')).toBeInTheDocument();
  867. });
  868. expect(setItemMock).toHaveBeenCalledWith('library-collapse-folders', 'false');
  869. });
  870. });
  871. describe('Internal / External top-level views (#1621)', () => {
  872. const externalMockFolders = [
  873. ...mockFolders,
  874. {
  875. id: 99,
  876. name: 'NAS Library',
  877. parent_id: null,
  878. file_count: 200,
  879. project_id: null,
  880. archive_id: null,
  881. project_name: null,
  882. archive_name: null,
  883. is_external: true,
  884. external_readonly: false,
  885. external_path: '/mnt/nas',
  886. children: [],
  887. },
  888. ];
  889. it('shows the External sidebar entry only when at least one external folder is linked', async () => {
  890. // Default mockFolders have no is_external entries → no External row.
  891. const { unmount } = render(<FileManagerPage />);
  892. await waitFor(() => {
  893. expect(screen.getByText('All Files')).toBeInTheDocument();
  894. });
  895. expect(screen.queryByText('External')).not.toBeInTheDocument();
  896. unmount();
  897. // With an external folder linked, the row appears.
  898. server.use(
  899. http.get('/api/v1/library/folders', () => HttpResponse.json(externalMockFolders)),
  900. );
  901. render(<FileManagerPage />);
  902. await waitFor(() => {
  903. expect(screen.getByText('External')).toBeInTheDocument();
  904. });
  905. });
  906. it('sends internal_only=true by default ("All Files" = managed storage only)', async () => {
  907. const scopes: string[] = [];
  908. server.use(
  909. http.get('/api/v1/library/folders', () => HttpResponse.json(externalMockFolders)),
  910. http.get('/api/v1/library/files', ({ request }) => {
  911. const url = new URL(request.url);
  912. scopes.push(
  913. url.searchParams.get('internal_only') === 'true'
  914. ? 'internal'
  915. : url.searchParams.get('external_only') === 'true'
  916. ? 'external'
  917. : 'all',
  918. );
  919. return HttpResponse.json(mockFiles);
  920. }),
  921. );
  922. render(<FileManagerPage />);
  923. await waitFor(() => {
  924. expect(scopes).toContain('internal');
  925. });
  926. });
  927. it('switches to external_only=true when the External sidebar entry is clicked', async () => {
  928. const scopes: string[] = [];
  929. server.use(
  930. http.get('/api/v1/library/folders', () => HttpResponse.json(externalMockFolders)),
  931. http.get('/api/v1/library/files', ({ request }) => {
  932. const url = new URL(request.url);
  933. scopes.push(
  934. url.searchParams.get('internal_only') === 'true'
  935. ? 'internal'
  936. : url.searchParams.get('external_only') === 'true'
  937. ? 'external'
  938. : 'all',
  939. );
  940. return HttpResponse.json([]);
  941. }),
  942. );
  943. const { default: userEvent } = await import('@testing-library/user-event');
  944. const user = userEvent.setup();
  945. render(<FileManagerPage />);
  946. await waitFor(() => expect(screen.getByText('External')).toBeInTheDocument());
  947. await user.click(screen.getByText('External'));
  948. await waitFor(() => {
  949. expect(scopes).toContain('external');
  950. });
  951. });
  952. });
  953. describe('"All Files" view (#1499)', () => {
  954. it('requests every file (include_root=false) so subfolder contents are visible', async () => {
  955. const rootFile = {
  956. id: 10,
  957. filename: 'root-file.3mf',
  958. file_path: '/library/root-file.3mf',
  959. file_size: 1024,
  960. file_type: '3mf',
  961. folder_id: null,
  962. thumbnail_path: null,
  963. print_name: 'Root File',
  964. print_time_seconds: 0,
  965. print_count: 0,
  966. duplicate_count: 0,
  967. created_at: '2024-01-01T00:00:00Z',
  968. };
  969. const nestedFile = {
  970. ...rootFile,
  971. id: 11,
  972. filename: 'nested-file.3mf',
  973. file_path: '/library/Functional Parts/nested-file.3mf',
  974. folder_id: 1,
  975. print_name: 'Nested File',
  976. };
  977. const includeRootValues: string[] = [];
  978. server.use(
  979. http.get('/api/v1/library/files', ({ request }) => {
  980. const url = new URL(request.url);
  981. const includeRoot = url.searchParams.get('include_root');
  982. includeRootValues.push(includeRoot ?? '');
  983. // Mirror the backend: include_root=false returns everything; true
  984. // returns only files with folder_id IS NULL.
  985. if (includeRoot === 'false') {
  986. return HttpResponse.json([rootFile, nestedFile]);
  987. }
  988. return HttpResponse.json([rootFile]);
  989. }),
  990. );
  991. render(<FileManagerPage />);
  992. await waitFor(() => {
  993. expect(screen.getByText('Root File')).toBeInTheDocument();
  994. expect(screen.getByText('Nested File')).toBeInTheDocument();
  995. });
  996. // Sanity-check: the buggy call would have sent include_root=true here.
  997. expect(includeRootValues).toContain('false');
  998. });
  999. });
  1000. describe('last-modified date display (#2680)', () => {
  1001. it('is hidden by default and revealed by the toolbar toggle', async () => {
  1002. const user = userEvent.setup();
  1003. render(<FileManagerPage />);
  1004. await waitFor(() => {
  1005. expect(screen.getByText('Benchy')).toBeInTheDocument();
  1006. });
  1007. // Hidden by default.
  1008. expect(screen.queryByText(/2030/)).not.toBeInTheDocument();
  1009. // Toggle on via the toolbar button.
  1010. await user.click(screen.getByTitle('Show modified dates'));
  1011. // benchy carries fs_modified_at in 2030, which must be preferred over its
  1012. // created_at (2024) — proving the real on-disk mtime drives the display.
  1013. await waitFor(() => {
  1014. expect(screen.getByText(/2030/)).toBeInTheDocument();
  1015. });
  1016. // Toggling off hides it again.
  1017. await user.click(screen.getByTitle('Hide modified dates'));
  1018. await waitFor(() => {
  1019. expect(screen.queryByText(/2030/)).not.toBeInTheDocument();
  1020. });
  1021. });
  1022. it('the same toggle reveals latest activity on folder rows, including nested ones', async () => {
  1023. const user = userEvent.setup();
  1024. render(<FileManagerPage />);
  1025. await waitFor(() => {
  1026. expect(screen.getByText('Functional Parts')).toBeInTheDocument();
  1027. });
  1028. expect(screen.queryByText(/2031/)).not.toBeInTheDocument();
  1029. await user.click(screen.getByTitle('Show modified dates'));
  1030. await waitFor(() => {
  1031. expect(screen.getByText(/2031/)).toBeInTheDocument();
  1032. });
  1033. // Nested folders get it too — the prop must survive the recursion.
  1034. expect(screen.getByText(/2032/)).toBeInTheDocument();
  1035. // A folder with no activity timestamp renders nothing rather than an
  1036. // "Invalid Date" string.
  1037. const artRow = screen.getByText('Art Projects').closest('div.group')!;
  1038. expect(artRow.textContent).not.toMatch(/Invalid/);
  1039. await user.click(screen.getByTitle('Hide modified dates'));
  1040. await waitFor(() => {
  1041. expect(screen.queryByText(/2031/)).not.toBeInTheDocument();
  1042. });
  1043. });
  1044. });
  1045. describe('slice action', () => {
  1046. beforeEach(() => {
  1047. vi.mocked(openInSlicer).mockClear();
  1048. server.use(
  1049. http.post('/api/v1/library/files/:id/slicer-token', () => HttpResponse.json({ token: 'test-token' })),
  1050. // The only sliceable fixture is an STL, and since #3029 the desktop
  1051. // handoff is only offered to a slicer whose protocol handler will
  1052. // actually load one -- Bambu Studio's takes 3MF only. These tests are
  1053. // about the handoff mechanics and the permission gate, not about which
  1054. // slicer, so they run against OrcaSlicer. The Bambu Studio side is
  1055. // covered by its own tests below.
  1056. http.get('/api/v1/settings/', () => HttpResponse.json({ preferred_slicer: 'orcaslicer' })),
  1057. );
  1058. });
  1059. afterEach(() => {
  1060. // Permission tests set a token; clear it so it can't leak into the
  1061. // list-view tests that follow (mirrors FileManagerFolderDelete.test.tsx).
  1062. setAuthToken(null);
  1063. });
  1064. const openMenu = async (user: ReturnType<typeof userEvent.setup>, filename: string) => {
  1065. const card = screen.getByText(filename).closest('.group') as HTMLElement;
  1066. // Target the kebab (ellipsis) toggle specifically rather than the card's
  1067. // first button — a button added ahead of the kebab would otherwise
  1068. // break the menu-opening assumption.
  1069. const kebab = card.querySelector('.lucide-ellipsis-vertical')?.closest('button') as HTMLButtonElement;
  1070. await user.click(kebab);
  1071. return card;
  1072. };
  1073. it('opens the desktop slicer when the slicer API is disabled', async () => {
  1074. const user = userEvent.setup();
  1075. render(<FileManagerPage />);
  1076. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1077. const card = await openMenu(user, 'bracket.stl');
  1078. await user.click(within(card).getByText('Slice'));
  1079. await waitFor(() => {
  1080. expect(openInSlicer).toHaveBeenCalledWith(
  1081. expect.stringContaining('/library/files/2/dl/test-token/'),
  1082. 'orcaslicer',
  1083. );
  1084. });
  1085. });
  1086. it('opens the in-app SliceModal when the slicer API is enabled', async () => {
  1087. server.use(
  1088. http.get('/api/v1/settings/', () => HttpResponse.json({ use_slicer_api: true })),
  1089. );
  1090. const user = userEvent.setup();
  1091. render(<FileManagerPage />);
  1092. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1093. const card = await openMenu(user, 'bracket.stl');
  1094. await user.click(within(card).getByText('Slice'));
  1095. expect(await screen.findByTestId('slice-modal')).toBeInTheDocument();
  1096. expect(openInSlicer).not.toHaveBeenCalled();
  1097. });
  1098. // #2846: the menu used to be an absolutely-positioned child of the card,
  1099. // and the card clipped its own overflow. A bare STL card is only about
  1100. // 270px tall -- thumbnail plus name and size -- which is shorter than the
  1101. // seven-entry menu, so the top entry was cut off. That entry is Slice,
  1102. // because Print is suppressed for an unsliced file. A 3MF card carries two
  1103. // more metadata rows and was tall enough, which is why the report said 3MF
  1104. // worked. Nothing about STL was special; the card was just the shortest.
  1105. it('keeps the first menu entry out of the card so it cannot be clipped (#2846)', async () => {
  1106. server.use(
  1107. http.get('/api/v1/settings/', () => HttpResponse.json({ use_slicer_api: true })),
  1108. );
  1109. const user = userEvent.setup();
  1110. render(<FileManagerPage />);
  1111. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1112. const card = await openMenu(user, 'bracket.stl');
  1113. const menu = within(card).getByText('Slice').closest('.fixed');
  1114. // Viewport-positioned, so no ancestor's overflow can cut it down.
  1115. expect(menu).not.toBeNull();
  1116. expect(within(menu as HTMLElement).getAllByRole('button')[0]).toHaveTextContent('Slice');
  1117. // And the card itself no longer clips what its children draw.
  1118. expect(card.className).not.toContain('overflow-hidden');
  1119. });
  1120. // #3029: Bambu Studio's protocol handler refuses anything that is not a
  1121. // 3MF before it even fetches the URL -- "Download failed, unknown file
  1122. // format." Offering the handoff anyway put an action on an STL card that
  1123. // could only fail, with an error that blamed the file.
  1124. it('hides the desktop handoff for an STL when the target is Bambu Studio', async () => {
  1125. server.use(
  1126. http.get('/api/v1/settings/', () => HttpResponse.json({ preferred_slicer: 'bambu_studio' })),
  1127. );
  1128. const user = userEvent.setup();
  1129. render(<FileManagerPage />);
  1130. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1131. const card = await openMenu(user, 'bracket.stl');
  1132. expect(within(card).queryByText('Slice')).not.toBeInTheDocument();
  1133. });
  1134. it('honours the open_in_slicer override over the preferred slicer', async () => {
  1135. // The desktop target is its own setting (#1329), so it -- not
  1136. // preferred_slicer, which drives the sidecar -- decides whether an STL
  1137. // can be handed over at all.
  1138. server.use(
  1139. http.get('/api/v1/settings/', () =>
  1140. HttpResponse.json({ preferred_slicer: 'bambu_studio', open_in_slicer: 'orcaslicer' }),
  1141. ),
  1142. );
  1143. const user = userEvent.setup();
  1144. render(<FileManagerPage />);
  1145. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1146. const card = await openMenu(user, 'bracket.stl');
  1147. await user.click(within(card).getByText('Slice'));
  1148. await waitFor(() => {
  1149. expect(openInSlicer).toHaveBeenCalledWith(expect.any(String), 'orcaslicer');
  1150. });
  1151. });
  1152. it('still offers an STL to the in-app slicer with Bambu Studio as the desktop target', async () => {
  1153. // The restriction is on the URL handoff, not on the file: the sidecar
  1154. // slices an STL regardless of which desktop slicer is configured.
  1155. server.use(
  1156. http.get('/api/v1/settings/', () =>
  1157. HttpResponse.json({ use_slicer_api: true, preferred_slicer: 'bambu_studio' }),
  1158. ),
  1159. );
  1160. const user = userEvent.setup();
  1161. render(<FileManagerPage />);
  1162. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1163. const card = await openMenu(user, 'bracket.stl');
  1164. await user.click(within(card).getByText('Slice'));
  1165. expect(await screen.findByTestId('slice-modal')).toBeInTheDocument();
  1166. });
  1167. it('hides the slice item for already-sliced files', async () => {
  1168. const user = userEvent.setup();
  1169. render(<FileManagerPage />);
  1170. await waitFor(() => expect(screen.getByText('Benchy')).toBeInTheDocument());
  1171. const card = await openMenu(user, 'Benchy');
  1172. expect(within(card).queryByText('Slice')).not.toBeInTheDocument();
  1173. });
  1174. // Permission gating is the security-relevant half of the slice action: the
  1175. // in-app API path needs library:upload, and the desktop handoff mirrors the
  1176. // ownership check the slicer-token endpoint runs — library:read_all or
  1177. // library:read_own. The legacy library:read is deliberately not accepted;
  1178. // it satisfies neither the token endpoint nor the folder listing that gets
  1179. // a user to this page at all.
  1180. const mockAuthUser = (permissions: string[]) => {
  1181. setAuthToken('test-token', 'session');
  1182. server.use(
  1183. http.get('*/api/v1/auth/status', () =>
  1184. HttpResponse.json({ auth_enabled: true, requires_setup: false }),
  1185. ),
  1186. http.get('*/api/v1/auth/me', () =>
  1187. HttpResponse.json({
  1188. id: 7,
  1189. username: 'operator1',
  1190. is_admin: false,
  1191. permissions,
  1192. }),
  1193. ),
  1194. http.get('/api/v1/users/', () => HttpResponse.json([])),
  1195. );
  1196. };
  1197. it('disables the Slice menu item without library:upload when the slicer API is enabled', async () => {
  1198. mockAuthUser([]);
  1199. server.use(
  1200. http.get('/api/v1/settings/', () => HttpResponse.json({ use_slicer_api: true })),
  1201. );
  1202. const user = userEvent.setup();
  1203. render(<FileManagerPage />);
  1204. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1205. const card = await openMenu(user, 'bracket.stl');
  1206. const sliceItem = within(card).getByText('Slice').closest('button');
  1207. expect(sliceItem).toBeDisabled();
  1208. await user.click(sliceItem!);
  1209. expect(openInSlicer).not.toHaveBeenCalled();
  1210. });
  1211. it('enables the Slice menu item with library:upload when the slicer API is enabled', async () => {
  1212. mockAuthUser(['library:upload']);
  1213. server.use(
  1214. http.get('/api/v1/settings/', () => HttpResponse.json({ use_slicer_api: true })),
  1215. );
  1216. const user = userEvent.setup();
  1217. render(<FileManagerPage />);
  1218. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1219. const card = await openMenu(user, 'bracket.stl');
  1220. const sliceItem = within(card).getByText('Slice').closest('button');
  1221. expect(sliceItem).not.toBeDisabled();
  1222. });
  1223. it('disables the Slice menu item without any library read permission for the desktop handoff', async () => {
  1224. mockAuthUser(['library:upload']);
  1225. const user = userEvent.setup();
  1226. render(<FileManagerPage />);
  1227. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1228. const card = await openMenu(user, 'bracket.stl');
  1229. const sliceItem = within(card).getByText('Slice').closest('button');
  1230. expect(sliceItem).toBeDisabled();
  1231. await user.click(sliceItem!);
  1232. expect(openInSlicer).not.toHaveBeenCalled();
  1233. });
  1234. it('enables the Slice menu item with library:read_own for the desktop handoff', async () => {
  1235. mockAuthUser(['library:read_own']);
  1236. const user = userEvent.setup();
  1237. render(<FileManagerPage />);
  1238. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1239. const card = await openMenu(user, 'bracket.stl');
  1240. const sliceItem = within(card).getByText('Slice').closest('button');
  1241. expect(sliceItem).not.toBeDisabled();
  1242. });
  1243. it('does not accept the legacy library:read for the desktop handoff', async () => {
  1244. // require_ownership_permission(LIBRARY_READ_ALL, LIBRARY_READ_OWN) does no
  1245. // legacy expansion, so this group 403s on the slicer-token endpoint.
  1246. // Enabling the item would offer an action the server refuses, and the
  1247. // failure would look like "no slicer installed" once the fallback URL is
  1248. // handed over.
  1249. mockAuthUser(['library:read']);
  1250. const user = userEvent.setup();
  1251. render(<FileManagerPage />);
  1252. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1253. const card = await openMenu(user, 'bracket.stl');
  1254. const sliceItem = within(card).getByText('Slice').closest('button');
  1255. expect(sliceItem).toBeDisabled();
  1256. await user.click(sliceItem!);
  1257. expect(openInSlicer).not.toHaveBeenCalled();
  1258. });
  1259. it('slices from the list-view button when the slicer API is disabled', async () => {
  1260. const user = userEvent.setup();
  1261. render(<FileManagerPage />);
  1262. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1263. await user.click(screen.getByTitle('List view'));
  1264. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1265. const row = screen.getByText('bracket.stl').closest('div[class*="cursor-pointer"]') as HTMLElement;
  1266. await user.click(within(row).getByTitle('Slice'));
  1267. await waitFor(() => {
  1268. expect(openInSlicer).toHaveBeenCalledWith(
  1269. expect.stringContaining('/library/files/2/dl/test-token/'),
  1270. 'orcaslicer',
  1271. );
  1272. });
  1273. });
  1274. it('slices from the list-view button into the in-app modal when the slicer API is enabled', async () => {
  1275. server.use(
  1276. http.get('/api/v1/settings/', () => HttpResponse.json({ use_slicer_api: true })),
  1277. );
  1278. const user = userEvent.setup();
  1279. render(<FileManagerPage />);
  1280. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1281. await user.click(screen.getByTitle('List view'));
  1282. await waitFor(() => expect(screen.getByText('bracket.stl')).toBeInTheDocument());
  1283. const row = screen.getByText('bracket.stl').closest('div[class*="cursor-pointer"]') as HTMLElement;
  1284. await user.click(within(row).getByTitle('Slice'));
  1285. expect(await screen.findByTestId('slice-modal')).toBeInTheDocument();
  1286. expect(openInSlicer).not.toHaveBeenCalled();
  1287. });
  1288. });
  1289. });