PrintersPage.test.tsx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /**
  2. * Tests for the PrintersPage component.
  3. */
  4. import { describe, it, expect, beforeEach } from 'vitest';
  5. import { screen, waitFor, fireEvent } from '@testing-library/react';
  6. import userEvent from '@testing-library/user-event';
  7. import { render } from '../utils';
  8. import { PrintersPage } from '../../pages/PrintersPage';
  9. import { http, HttpResponse } from 'msw';
  10. import { server } from '../mocks/server';
  11. const mockPrinters = [
  12. {
  13. id: 1,
  14. name: 'X1 Carbon',
  15. ip_address: '192.168.1.100',
  16. serial_number: '00M09A350100001',
  17. access_code: '12345678',
  18. model: 'X1C',
  19. enabled: true,
  20. nozzle_diameter: 0.4,
  21. nozzle_type: 'hardened_steel',
  22. location: 'Workshop',
  23. auto_archive: true,
  24. created_at: '2024-01-01T00:00:00Z',
  25. updated_at: '2024-01-01T00:00:00Z',
  26. },
  27. {
  28. id: 2,
  29. name: 'P1S Backup',
  30. ip_address: '192.168.1.101',
  31. serial_number: '00W00A123456789',
  32. access_code: '87654321',
  33. model: 'P1S',
  34. enabled: false,
  35. nozzle_diameter: 0.4,
  36. nozzle_type: 'stainless_steel',
  37. location: null,
  38. auto_archive: true,
  39. created_at: '2024-01-02T00:00:00Z',
  40. updated_at: '2024-01-02T00:00:00Z',
  41. },
  42. ];
  43. const mockPrinterStatus = {
  44. connected: true,
  45. state: 'IDLE',
  46. awaiting_plate_clear: false,
  47. progress: 0,
  48. layer_num: 0,
  49. total_layers: 0,
  50. temperatures: {
  51. nozzle: 25,
  52. bed: 25,
  53. chamber: 25,
  54. },
  55. remaining_time: 0,
  56. filename: null,
  57. wifi_signal: -50,
  58. vt_tray: [],
  59. };
  60. const selectToolbarDropdownOption = async (triggerName: RegExp, optionName: RegExp) => {
  61. const user = userEvent.setup();
  62. await user.click(screen.getByRole('button', { name: triggerName }));
  63. await user.click(await screen.findByRole('button', { name: optionName }));
  64. };
  65. describe('PrintersPage', () => {
  66. beforeEach(() => {
  67. localStorage.removeItem('printerCardSize');
  68. server.use(
  69. http.get('/api/v1/printers/', () => {
  70. return HttpResponse.json(mockPrinters);
  71. }),
  72. http.get('/api/v1/printers/:id/status', () => {
  73. return HttpResponse.json(mockPrinterStatus);
  74. }),
  75. http.post('/api/v1/printers/:id/clear-plate', () => {
  76. return HttpResponse.json({ success: true, message: 'Plate cleared' });
  77. }),
  78. http.get('/api/v1/settings/', () => {
  79. return HttpResponse.json({
  80. auto_archive: true,
  81. save_thumbnails: true,
  82. capture_finish_photo: true,
  83. default_filament_cost: 25.0,
  84. currency: 'USD',
  85. ams_humidity_good: 40,
  86. ams_humidity_fair: 60,
  87. ams_temp_good: 30,
  88. ams_temp_fair: 35,
  89. require_plate_clear: true,
  90. });
  91. }),
  92. http.get('/api/v1/queue/', () => {
  93. return HttpResponse.json([]);
  94. })
  95. );
  96. });
  97. describe('rendering', () => {
  98. it('renders the page title', async () => {
  99. render(<PrintersPage />);
  100. await waitFor(() => {
  101. expect(screen.getByText('Printers')).toBeInTheDocument();
  102. });
  103. });
  104. it('shows printer cards', async () => {
  105. render(<PrintersPage />);
  106. await waitFor(() => {
  107. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  108. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  109. });
  110. });
  111. it('shows printer models', async () => {
  112. render(<PrintersPage />);
  113. await waitFor(() => {
  114. expect(screen.getByText('X1C')).toBeInTheDocument();
  115. expect(screen.getByText('P1S')).toBeInTheDocument();
  116. });
  117. });
  118. it('shows printer status', async () => {
  119. render(<PrintersPage />);
  120. await waitFor(() => {
  121. // Status should be shown - may vary based on state
  122. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  123. });
  124. });
  125. });
  126. describe('printer info', () => {
  127. it('shows IP address in printer info modal', async () => {
  128. render(<PrintersPage />);
  129. await waitFor(() => {
  130. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  131. });
  132. // IP address is shown in the PrinterInfoModal (accessed via 3-dot menu),
  133. // not directly on the card. Verify the printer data loaded correctly.
  134. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  135. });
  136. it('shows location when set', async () => {
  137. render(<PrintersPage />);
  138. await waitFor(() => {
  139. // Printers should render - location display may vary
  140. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  141. });
  142. });
  143. });
  144. describe('temperature display', () => {
  145. it('shows nozzle temperature', async () => {
  146. render(<PrintersPage />);
  147. await waitFor(() => {
  148. // Temperatures are shown in the UI
  149. expect(screen.getAllByText(/25/)).toBeTruthy();
  150. });
  151. });
  152. });
  153. describe('empty state', () => {
  154. it('shows empty state when no printers', async () => {
  155. server.use(
  156. http.get('/api/v1/printers/', () => {
  157. return HttpResponse.json([]);
  158. })
  159. );
  160. render(<PrintersPage />);
  161. await waitFor(() => {
  162. expect(screen.getByText(/no printers/i)).toBeInTheDocument();
  163. });
  164. });
  165. });
  166. describe('printer actions', () => {
  167. it('has action buttons', async () => {
  168. render(<PrintersPage />);
  169. await waitFor(() => {
  170. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  171. });
  172. // There should be some interactive elements for printer actions
  173. const buttons = screen.getAllByRole('button');
  174. expect(buttons.length).toBeGreaterThan(0);
  175. });
  176. it('shows plate clear status and action on finished printers when not cleared', async () => {
  177. server.use(
  178. http.get('/api/v1/printers/:id/status', () => {
  179. return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: true });
  180. })
  181. );
  182. render(<PrintersPage />);
  183. await waitFor(() => {
  184. expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
  185. });
  186. expect(screen.getAllByRole('button', { name: 'Mark plate as cleared' }).length).toBeGreaterThan(0);
  187. });
  188. it('shows plate clear status and action on failed printers when not cleared', async () => {
  189. server.use(
  190. http.get('/api/v1/printers/:id/status', () => {
  191. return HttpResponse.json({ ...mockPrinterStatus, state: 'FAILED', awaiting_plate_clear: true });
  192. })
  193. );
  194. render(<PrintersPage />);
  195. await waitFor(() => {
  196. expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
  197. });
  198. expect(screen.getAllByRole('button', { name: 'Mark plate as cleared' }).length).toBeGreaterThan(0);
  199. });
  200. it('keeps the clear action available when an idle printer is still awaiting acknowledgment', async () => {
  201. server.use(
  202. http.get('/api/v1/printers/:id/status', () => {
  203. return HttpResponse.json({ ...mockPrinterStatus, state: 'IDLE', awaiting_plate_clear: true });
  204. })
  205. );
  206. render(<PrintersPage />);
  207. await waitFor(() => {
  208. expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
  209. });
  210. expect(screen.getAllByRole('button', { name: 'Mark plate as cleared' }).length).toBeGreaterThan(0);
  211. });
  212. it('updates the plate clear status after using the printer card action', async () => {
  213. let awaitingPlateClear = true;
  214. server.use(
  215. http.get('/api/v1/printers/', () => {
  216. return HttpResponse.json([mockPrinters[0]]);
  217. }),
  218. http.get('/api/v1/printers/:id/status', () => {
  219. return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: awaitingPlateClear });
  220. }),
  221. http.post('/api/v1/printers/:id/clear-plate', () => {
  222. awaitingPlateClear = false;
  223. return HttpResponse.json({ success: true, message: 'Plate cleared' });
  224. })
  225. );
  226. render(<PrintersPage />);
  227. await waitFor(() => {
  228. expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
  229. });
  230. fireEvent.click(screen.getAllByRole('button', { name: 'Mark plate as cleared' })[0]);
  231. await waitFor(() => {
  232. expect(screen.queryByText('Plate not Clear')).not.toBeInTheDocument();
  233. });
  234. expect(screen.getAllByText('Plate Clear').length).toBeGreaterThan(0);
  235. });
  236. it('shows an icon-only plate clear action in small card view', async () => {
  237. let awaitingPlateClear = true;
  238. server.use(
  239. http.get('/api/v1/printers/', () => {
  240. return HttpResponse.json([mockPrinters[0]]);
  241. }),
  242. http.get('/api/v1/printers/:id/status', () => {
  243. return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: awaitingPlateClear });
  244. }),
  245. http.post('/api/v1/printers/:id/clear-plate', () => {
  246. awaitingPlateClear = false;
  247. return HttpResponse.json({ success: true, message: 'Plate cleared' });
  248. })
  249. );
  250. render(<PrintersPage />);
  251. await waitFor(() => {
  252. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  253. });
  254. fireEvent.click(screen.getByRole('button', { name: 'S' }));
  255. await waitFor(() => {
  256. expect(screen.queryByText('Mark plate as cleared')).not.toBeInTheDocument();
  257. });
  258. const clearButton = screen.getByRole('button', { name: 'Mark plate as cleared' });
  259. fireEvent.click(clearButton);
  260. await waitFor(() => {
  261. expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
  262. });
  263. });
  264. it('shows plate clear status but no action while idle', async () => {
  265. render(<PrintersPage />);
  266. await waitFor(() => {
  267. expect(screen.getAllByText('Plate Clear').length).toBeGreaterThan(0);
  268. });
  269. expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
  270. });
  271. it('shows plate in use status while printing and hides the clear action', async () => {
  272. server.use(
  273. http.get('/api/v1/printers/:id/status', () => {
  274. return HttpResponse.json({ ...mockPrinterStatus, state: 'RUNNING', awaiting_plate_clear: false });
  275. })
  276. );
  277. render(<PrintersPage />);
  278. await waitFor(() => {
  279. expect(screen.getAllByText('Plate in Use').length).toBeGreaterThan(0);
  280. });
  281. expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
  282. });
  283. it('hides plate status and action when plate-clear confirmation is disabled', async () => {
  284. server.use(
  285. http.get('/api/v1/settings/', () => {
  286. return HttpResponse.json({
  287. auto_archive: true,
  288. save_thumbnails: true,
  289. capture_finish_photo: true,
  290. default_filament_cost: 25.0,
  291. currency: 'USD',
  292. ams_humidity_good: 40,
  293. ams_humidity_fair: 60,
  294. ams_temp_good: 30,
  295. ams_temp_fair: 35,
  296. require_plate_clear: false,
  297. });
  298. }),
  299. http.get('/api/v1/printers/:id/status', () => {
  300. return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: true });
  301. })
  302. );
  303. render(<PrintersPage />);
  304. await waitFor(() => {
  305. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  306. });
  307. expect(screen.queryByText('Plate not Clear')).not.toBeInTheDocument();
  308. expect(screen.queryByText('Plate Clear')).not.toBeInTheDocument();
  309. expect(screen.queryByText('Plate in Use')).not.toBeInTheDocument();
  310. expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
  311. });
  312. });
  313. describe('disabled printer', () => {
  314. it('shows disabled state for disabled printers', async () => {
  315. render(<PrintersPage />);
  316. await waitFor(() => {
  317. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  318. });
  319. // Disabled printers have visual indication
  320. const disabledPrinter = screen.getByText('P1S Backup').closest('div');
  321. expect(disabledPrinter).toBeInTheDocument();
  322. });
  323. });
  324. describe('nozzle rack card', () => {
  325. const h2cStatus = {
  326. ...mockPrinterStatus,
  327. nozzle_rack: [
  328. { id: 0, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 5, stat: 1, max_temp: 300, serial_number: 'SN-L', filament_color: '', filament_id: '', filament_type: '' },
  329. { id: 1, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 3, stat: 0, max_temp: 300, serial_number: 'SN-R', filament_color: '', filament_id: '', filament_type: '' },
  330. { id: 16, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 10, stat: 0, max_temp: 300, serial_number: 'SN-16', filament_color: '', filament_id: '', filament_type: '' },
  331. { id: 17, nozzle_type: 'HH01', nozzle_diameter: '0.6', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-17', filament_color: '', filament_id: '', filament_type: '' },
  332. { id: 18, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 2, stat: 0, max_temp: 300, serial_number: 'SN-18', filament_color: '', filament_id: '', filament_type: '' },
  333. { id: 19, nozzle_type: '', nozzle_diameter: '', wear: null, stat: null, max_temp: 0, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  334. { id: 20, nozzle_type: '', nozzle_diameter: '', wear: null, stat: null, max_temp: 0, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  335. { id: 21, nozzle_type: '', nozzle_diameter: '', wear: null, stat: null, max_temp: 0, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  336. ],
  337. };
  338. it('shows nozzle rack when H2C rack slots present', async () => {
  339. server.use(
  340. http.get('/api/v1/printers/:id/status', () => {
  341. return HttpResponse.json(h2cStatus);
  342. })
  343. );
  344. render(<PrintersPage />);
  345. await waitFor(() => {
  346. expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
  347. });
  348. });
  349. it('shows 6 rack slot elements for H2C', async () => {
  350. server.use(
  351. http.get('/api/v1/printers/:id/status', () => {
  352. return HttpResponse.json(h2cStatus);
  353. })
  354. );
  355. render(<PrintersPage />);
  356. await waitFor(() => {
  357. expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
  358. });
  359. // Rack shows diameters for occupied slots and dashes for empty ones
  360. const dashes = screen.getAllByText('—');
  361. expect(dashes.length).toBeGreaterThanOrEqual(3); // 3 empty rack positions (IDs 19,20,21)
  362. });
  363. it('keeps empty slot anchored to physical position when its nozzle is mounted (#943)', async () => {
  364. // H2C with rack slot 16 picked up into the hotend — firmware omits ID 16
  365. // entirely from nozzle.info. Each rack diameter is unique so we can assert
  366. // the ordering by tooltip lookup.
  367. const h2cSlot16Mounted = {
  368. ...mockPrinterStatus,
  369. nozzle_rack: [
  370. { id: 0, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 5, stat: 1, max_temp: 300, serial_number: 'SN-L', filament_color: '', filament_id: '', filament_type: '' },
  371. { id: 1, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 3, stat: 0, max_temp: 300, serial_number: 'SN-R', filament_color: '', filament_id: '', filament_type: '' },
  372. // ID 16 missing — currently in hotend
  373. { id: 17, nozzle_type: 'HS', nozzle_diameter: '0.2', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-17', filament_color: '', filament_id: '', filament_type: '' },
  374. { id: 18, nozzle_type: 'HS', nozzle_diameter: '0.6', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-18', filament_color: '', filament_id: '', filament_type: '' },
  375. { id: 19, nozzle_type: 'HS', nozzle_diameter: '0.8', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-19', filament_color: '', filament_id: '', filament_type: '' },
  376. { id: 20, nozzle_type: 'HH01', nozzle_diameter: '1.0', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-20', filament_color: '', filament_id: '', filament_type: '' },
  377. { id: 21, nozzle_type: 'HH01', nozzle_diameter: '1.2', wear: 0, stat: 0, max_temp: 300, serial_number: 'SN-21', filament_color: '', filament_id: '', filament_type: '' },
  378. ],
  379. };
  380. server.use(
  381. http.get('/api/v1/printers/:id/status', () => {
  382. return HttpResponse.json(h2cSlot16Mounted);
  383. })
  384. );
  385. render(<PrintersPage />);
  386. await waitFor(() => {
  387. expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
  388. });
  389. // Slot 1 (leftmost, ID 16) should be the empty dash; slots 2..6 should
  390. // hold the 5 remaining nozzles in order 17, 18, 19, 20, 21.
  391. const rackLabel = screen.getAllByText('Nozzle Rack')[0];
  392. const rackCard = rackLabel.parentElement!;
  393. const slotRow = rackCard.querySelectorAll('div.flex')[0];
  394. const slotTexts = Array.from(slotRow.querySelectorAll('span')).map(s => s.textContent);
  395. expect(slotTexts).toEqual(['—', '0.2', '0.6', '0.8', '1.0', '1.2']);
  396. });
  397. it('hides nozzle rack when only L/R nozzles present (H2D)', async () => {
  398. const h2dStatus = {
  399. ...mockPrinterStatus,
  400. nozzle_rack: [
  401. { id: 0, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 5, stat: 1, max_temp: 300, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  402. { id: 1, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 3, stat: 1, max_temp: 300, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  403. ],
  404. };
  405. server.use(
  406. http.get('/api/v1/printers/:id/status', () => {
  407. return HttpResponse.json(h2dStatus);
  408. })
  409. );
  410. render(<PrintersPage />);
  411. await waitFor(() => {
  412. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  413. });
  414. expect(screen.queryByText('Nozzle Rack')).not.toBeInTheDocument();
  415. });
  416. });
  417. describe('firmware version badge', () => {
  418. const firmwareUpToDate = {
  419. printer_id: 1,
  420. current_version: '01.09.00.00',
  421. latest_version: '01.09.00.00',
  422. update_available: false,
  423. download_url: null,
  424. release_notes: 'Bug fixes and improvements.',
  425. };
  426. const firmwareUpdateAvailable = {
  427. printer_id: 1,
  428. current_version: '01.08.00.00',
  429. latest_version: '01.09.00.00',
  430. update_available: true,
  431. download_url: 'https://example.com/firmware.bin',
  432. release_notes: 'New features added.',
  433. };
  434. it('shows green badge when firmware is up to date', async () => {
  435. server.use(
  436. http.get('/api/v1/firmware/updates/:id', () => {
  437. return HttpResponse.json(firmwareUpToDate);
  438. }),
  439. http.get('/api/v1/settings/', () => {
  440. return HttpResponse.json({
  441. check_printer_firmware: true,
  442. auto_archive: true,
  443. save_thumbnails: true,
  444. });
  445. })
  446. );
  447. render(<PrintersPage />);
  448. await waitFor(() => {
  449. expect(screen.getAllByText('01.09.00.00').length).toBeGreaterThan(0);
  450. });
  451. const badge = screen.getAllByText('01.09.00.00')[0].closest('button');
  452. expect(badge).toBeInTheDocument();
  453. expect(badge?.className).toContain('text-status-ok');
  454. });
  455. it('shows orange badge when firmware update is available', async () => {
  456. server.use(
  457. http.get('/api/v1/firmware/updates/:id', () => {
  458. return HttpResponse.json(firmwareUpdateAvailable);
  459. }),
  460. http.get('/api/v1/settings/', () => {
  461. return HttpResponse.json({
  462. check_printer_firmware: true,
  463. auto_archive: true,
  464. save_thumbnails: true,
  465. });
  466. })
  467. );
  468. render(<PrintersPage />);
  469. await waitFor(() => {
  470. expect(screen.getAllByText('01.08.00.00').length).toBeGreaterThan(0);
  471. });
  472. const badge = screen.getAllByText('01.08.00.00')[0].closest('button');
  473. expect(badge).toBeInTheDocument();
  474. expect(badge?.className).toContain('text-orange-400');
  475. });
  476. it('hides badge when firmware check is disabled', async () => {
  477. server.use(
  478. http.get('/api/v1/settings/', () => {
  479. return HttpResponse.json({
  480. check_printer_firmware: false,
  481. auto_archive: true,
  482. save_thumbnails: true,
  483. });
  484. })
  485. );
  486. render(<PrintersPage />);
  487. await waitFor(() => {
  488. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  489. });
  490. // Version should not appear when firmware check is disabled
  491. expect(screen.queryByText('01.09.00.00')).not.toBeInTheDocument();
  492. expect(screen.queryByText('01.08.00.00')).not.toBeInTheDocument();
  493. });
  494. it('hides badge when API has no firmware data for the model', async () => {
  495. const firmwareNoData = {
  496. printer_id: 1,
  497. current_version: '01.01.03.00',
  498. latest_version: null,
  499. update_available: false,
  500. download_url: null,
  501. release_notes: null,
  502. };
  503. server.use(
  504. http.get('/api/v1/firmware/updates/:id', () => {
  505. return HttpResponse.json(firmwareNoData);
  506. }),
  507. http.get('/api/v1/settings/', () => {
  508. return HttpResponse.json({
  509. check_printer_firmware: true,
  510. auto_archive: true,
  511. save_thumbnails: true,
  512. });
  513. })
  514. );
  515. render(<PrintersPage />);
  516. await waitFor(() => {
  517. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  518. });
  519. // Badge should not appear when API returns no latest_version
  520. expect(screen.queryByText('01.01.03.00')).not.toBeInTheDocument();
  521. });
  522. });
  523. describe('bulk selection', () => {
  524. it('shows select button in toolbar', async () => {
  525. render(<PrintersPage />);
  526. await waitFor(() => {
  527. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  528. });
  529. // The Select button should be in the toolbar (title attribute)
  530. const selectButton = screen.getByTitle('Select');
  531. expect(selectButton).toBeInTheDocument();
  532. });
  533. it('shows selection toolbar after clicking select button', async () => {
  534. render(<PrintersPage />);
  535. await waitFor(() => {
  536. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  537. });
  538. // Click the Select button to enter selection mode
  539. fireEvent.click(screen.getByTitle('Select'));
  540. // The floating toolbar should appear with Select All
  541. await waitFor(() => {
  542. expect(screen.getByText('Select All')).toBeInTheDocument();
  543. });
  544. });
  545. it('shows selection count when printers are selected', async () => {
  546. render(<PrintersPage />);
  547. await waitFor(() => {
  548. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  549. });
  550. // Enter selection mode
  551. fireEvent.click(screen.getByTitle('Select'));
  552. await waitFor(() => {
  553. expect(screen.getByText('Select All')).toBeInTheDocument();
  554. });
  555. // Click Select All to select both printers
  556. fireEvent.click(screen.getByText('Select All'));
  557. // Should show "2 selected"
  558. await waitFor(() => {
  559. expect(screen.getByText('2 selected')).toBeInTheDocument();
  560. });
  561. });
  562. it('shows select by state dropdown', async () => {
  563. render(<PrintersPage />);
  564. await waitFor(() => {
  565. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  566. });
  567. // Enter selection mode
  568. fireEvent.click(screen.getByTitle('Select'));
  569. await waitFor(() => {
  570. expect(screen.getByText('Select by State')).toBeInTheDocument();
  571. });
  572. });
  573. it('exits selection mode on close button', async () => {
  574. render(<PrintersPage />);
  575. await waitFor(() => {
  576. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  577. });
  578. // Enter selection mode
  579. fireEvent.click(screen.getByTitle('Select'));
  580. await waitFor(() => {
  581. expect(screen.getByText('Select All')).toBeInTheDocument();
  582. });
  583. // Click the Select button again to exit (it toggles)
  584. fireEvent.click(screen.getByTitle('Select'));
  585. // Floating toolbar should disappear
  586. await waitFor(() => {
  587. expect(screen.queryByText('Select All')).not.toBeInTheDocument();
  588. });
  589. });
  590. });
  591. describe('search and filter', () => {
  592. beforeEach(() => {
  593. server.use(
  594. http.get('/api/v1/printers/', () => HttpResponse.json(mockPrinters)),
  595. http.get('/api/v1/printers/:id/status', () => HttpResponse.json(mockPrinterStatus)),
  596. http.get('/api/v1/queue/', () => HttpResponse.json([]))
  597. );
  598. });
  599. it('filters by name (case-insensitive)', async () => {
  600. render(<PrintersPage />);
  601. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  602. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'x1 carbon' } });
  603. await waitFor(() => {
  604. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  605. expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
  606. });
  607. });
  608. it('trims leading and trailing whitespace from search', async () => {
  609. render(<PrintersPage />);
  610. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  611. // " X1 Carbon " with surrounding spaces must still match
  612. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: ' X1 Carbon ' } });
  613. await waitFor(() => {
  614. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  615. expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
  616. });
  617. });
  618. it('filters by model', async () => {
  619. render(<PrintersPage />);
  620. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  621. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'P1S' } });
  622. await waitFor(() => {
  623. expect(screen.queryByText('X1 Carbon')).not.toBeInTheDocument();
  624. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  625. });
  626. });
  627. it('filters by serial number', async () => {
  628. render(<PrintersPage />);
  629. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  630. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: '00M09A' } });
  631. await waitFor(() => {
  632. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  633. expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
  634. });
  635. });
  636. it('shows empty state when no printers match search', async () => {
  637. render(<PrintersPage />);
  638. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  639. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'ZZZ_NO_MATCH' } });
  640. await waitFor(() => {
  641. expect(screen.getByText('No printers match your search or filters')).toBeInTheDocument();
  642. });
  643. });
  644. it('clear button resets search and shows all printers', async () => {
  645. render(<PrintersPage />);
  646. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  647. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'X1 Carbon' } });
  648. await waitFor(() => expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument());
  649. // Click the accessible clear button
  650. fireEvent.click(screen.getByRole('button', { name: 'Clear' }));
  651. await waitFor(() => {
  652. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  653. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  654. });
  655. });
  656. it('filters by status (offline) via dropdown', async () => {
  657. // Override: printer 1 online, printer 2 offline
  658. server.use(
  659. http.get('/api/v1/printers/:id/status', ({ params }) => {
  660. if (Number(params.id) === 2) {
  661. return HttpResponse.json({ ...mockPrinterStatus, connected: false });
  662. }
  663. return HttpResponse.json(mockPrinterStatus);
  664. })
  665. );
  666. render(<PrintersPage />);
  667. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  668. await selectToolbarDropdownOption(/all statuses/i, /^offline$/i);
  669. await waitFor(() => {
  670. expect(screen.queryByText('X1 Carbon')).not.toBeInTheDocument();
  671. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  672. });
  673. });
  674. it('shows empty state when status filter matches nothing', async () => {
  675. render(<PrintersPage />);
  676. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  677. // Both printers are IDLE; filtering by "printing" should yield no results
  678. await selectToolbarDropdownOption(/all statuses/i, /^printing$/i);
  679. await waitFor(() => {
  680. expect(screen.getByText('No printers match your search or filters')).toBeInTheDocument();
  681. });
  682. });
  683. it('combines search and status filter', async () => {
  684. // Printer 1 = RUNNING (printing), printer 2 = IDLE
  685. server.use(
  686. http.get('/api/v1/printers/:id/status', ({ params }) => {
  687. if (Number(params.id) === 1) {
  688. return HttpResponse.json({ ...mockPrinterStatus, state: 'RUNNING' });
  689. }
  690. return HttpResponse.json(mockPrinterStatus);
  691. })
  692. );
  693. render(<PrintersPage />);
  694. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  695. // Filter to only "printing" printers
  696. await selectToolbarDropdownOption(/all statuses/i, /^printing$/i);
  697. // Then also search for a term that only matches printer 1
  698. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'X1' } });
  699. await waitFor(() => {
  700. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  701. expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
  702. });
  703. });
  704. it('filters by location via dropdown', async () => {
  705. // Override: give printer 2 its own location so the dropdown has two options
  706. // and we can verify the filter picks the right one. Printer 1 stays at 'Workshop'.
  707. server.use(
  708. http.get('/api/v1/printers/', () =>
  709. HttpResponse.json([
  710. mockPrinters[0],
  711. { ...mockPrinters[1], location: 'Office' },
  712. ])
  713. )
  714. );
  715. render(<PrintersPage />);
  716. await waitFor(() => {
  717. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  718. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  719. });
  720. await selectToolbarDropdownOption(/all locations/i, /^workshop$/i);
  721. await waitFor(() => {
  722. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  723. expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
  724. });
  725. await selectToolbarDropdownOption(/^workshop$/i, /^office$/i);
  726. await waitFor(() => {
  727. expect(screen.queryByText('X1 Carbon')).not.toBeInTheDocument();
  728. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  729. });
  730. });
  731. it('hides location filter when no printers have a location', async () => {
  732. // Both printers have null location — dropdown should not render at all
  733. server.use(
  734. http.get('/api/v1/printers/', () =>
  735. HttpResponse.json([
  736. { ...mockPrinters[0], location: null },
  737. { ...mockPrinters[1], location: null },
  738. ])
  739. )
  740. );
  741. render(<PrintersPage />);
  742. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  743. // Status filter is still there, but the location filter should be absent.
  744. expect(screen.getByRole('button', { name: /all statuses/i })).toBeInTheDocument();
  745. expect(screen.queryByRole('button', { name: /all locations/i })).not.toBeInTheDocument();
  746. });
  747. });
  748. });