PrintersPage.test.tsx 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  1. /**
  2. * Tests for the PrintersPage component.
  3. */
  4. import { describe, it, expect, beforeEach } from 'vitest';
  5. import { screen, waitFor, fireEvent, within } 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. is_active: true,
  21. nozzle_diameter: 0.4,
  22. nozzle_type: 'hardened_steel',
  23. location: 'Workshop',
  24. auto_archive: true,
  25. created_at: '2024-01-01T00:00:00Z',
  26. updated_at: '2024-01-01T00:00:00Z',
  27. },
  28. {
  29. id: 2,
  30. name: 'P1S Backup',
  31. ip_address: '192.168.1.101',
  32. serial_number: '00W00A123456789',
  33. access_code: '87654321',
  34. model: 'P1S',
  35. enabled: false,
  36. is_active: true,
  37. nozzle_diameter: 0.4,
  38. nozzle_type: 'stainless_steel',
  39. location: null,
  40. auto_archive: true,
  41. created_at: '2024-01-02T00:00:00Z',
  42. updated_at: '2024-01-02T00:00:00Z',
  43. },
  44. ];
  45. const mockPrinterStatus = {
  46. connected: true,
  47. state: 'IDLE',
  48. awaiting_plate_clear: false,
  49. progress: 0,
  50. layer_num: 0,
  51. total_layers: 0,
  52. temperatures: {
  53. nozzle: 25,
  54. bed: 25,
  55. chamber: 25,
  56. },
  57. remaining_time: 0,
  58. filename: null,
  59. wifi_signal: -50,
  60. vt_tray: [],
  61. };
  62. const selectToolbarDropdownOption = async (triggerName: RegExp, optionName: RegExp) => {
  63. const user = userEvent.setup();
  64. await user.click(screen.getByRole('button', { name: triggerName }));
  65. await user.click(await screen.findByRole('button', { name: optionName }));
  66. };
  67. describe('PrintersPage', () => {
  68. beforeEach(() => {
  69. localStorage.removeItem('printerCardSize');
  70. server.use(
  71. http.get('/api/v1/printers/', () => {
  72. return HttpResponse.json(mockPrinters);
  73. }),
  74. http.get('/api/v1/printers/:id/status', () => {
  75. return HttpResponse.json(mockPrinterStatus);
  76. }),
  77. http.post('/api/v1/printers/:id/clear-plate', () => {
  78. return HttpResponse.json({ success: true, message: 'Plate cleared' });
  79. }),
  80. http.get('/api/v1/settings/', () => {
  81. return HttpResponse.json({
  82. auto_archive: true,
  83. save_thumbnails: true,
  84. capture_finish_photo: true,
  85. default_filament_cost: 25.0,
  86. currency: 'USD',
  87. ams_humidity_good: 40,
  88. ams_humidity_fair: 60,
  89. ams_temp_good: 30,
  90. ams_temp_fair: 35,
  91. require_plate_clear: true,
  92. });
  93. }),
  94. // PrintersPage now reads UI rendering fields from the public ui-preferences
  95. // endpoint instead of /settings (#1293) — admin pages still hit /settings.
  96. http.get('/api/v1/settings/ui-preferences', () => {
  97. return HttpResponse.json({
  98. ams_humidity_good: 40,
  99. ams_humidity_fair: 60,
  100. ams_temp_good: 30,
  101. ams_temp_fair: 35,
  102. require_plate_clear: true,
  103. });
  104. }),
  105. http.get('/api/v1/queue/', () => {
  106. return HttpResponse.json([]);
  107. })
  108. );
  109. });
  110. describe('rendering', () => {
  111. it('renders the page title', async () => {
  112. render(<PrintersPage />);
  113. await waitFor(() => {
  114. expect(screen.getByText('Printers')).toBeInTheDocument();
  115. });
  116. });
  117. it('shows printer cards', async () => {
  118. render(<PrintersPage />);
  119. await waitFor(() => {
  120. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  121. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  122. });
  123. });
  124. it('shows printer models', async () => {
  125. render(<PrintersPage />);
  126. await waitFor(() => {
  127. expect(screen.getByText('X1C')).toBeInTheDocument();
  128. expect(screen.getByText('P1S')).toBeInTheDocument();
  129. });
  130. });
  131. it('shows printer status', async () => {
  132. render(<PrintersPage />);
  133. await waitFor(() => {
  134. // Status should be shown - may vary based on state
  135. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  136. });
  137. });
  138. });
  139. describe('printer info', () => {
  140. it('shows IP address in printer info modal', async () => {
  141. render(<PrintersPage />);
  142. await waitFor(() => {
  143. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  144. });
  145. // IP address is shown in the PrinterInfoModal (accessed via 3-dot menu),
  146. // not directly on the card. Verify the printer data loaded correctly.
  147. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  148. });
  149. it('shows location when set', async () => {
  150. render(<PrintersPage />);
  151. await waitFor(() => {
  152. // Printers should render - location display may vary
  153. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  154. });
  155. });
  156. });
  157. describe('temperature display', () => {
  158. it('shows nozzle temperature', async () => {
  159. render(<PrintersPage />);
  160. await waitFor(() => {
  161. // Temperatures are shown in the UI
  162. expect(screen.getAllByText(/25/)).toBeTruthy();
  163. });
  164. });
  165. it('sets left and right nozzle temperatures from the nozzle selector', async () => {
  166. localStorage.setItem('printerCardSize', '2');
  167. const temperatureRequests: Array<{ target: string | null; nozzle: string | null }> = [];
  168. const dualNozzlePrinter = { ...mockPrinters[0], model: 'H2D', nozzle_count: 2 };
  169. const dualNozzleStatus = {
  170. ...mockPrinterStatus,
  171. active_extruder: 0,
  172. temperatures: {
  173. ...mockPrinterStatus.temperatures,
  174. nozzle: 31,
  175. nozzle_target: 0,
  176. nozzle_2: 32,
  177. nozzle_2_target: 0,
  178. },
  179. nozzle_rack: [
  180. { id: 0, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 5, stat: 1, max_temp: 300, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  181. { id: 1, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 3, stat: 1, max_temp: 300, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  182. ],
  183. };
  184. server.use(
  185. http.get('/api/v1/printers/', () => HttpResponse.json([dualNozzlePrinter])),
  186. http.get('/api/v1/printers/:id/status', () => HttpResponse.json(dualNozzleStatus)),
  187. http.post('/api/v1/printers/:id/temperature/nozzle', ({ request }) => {
  188. const url = new URL(request.url);
  189. temperatureRequests.push({
  190. target: url.searchParams.get('target'),
  191. nozzle: url.searchParams.get('nozzle'),
  192. });
  193. return HttpResponse.json({ success: true, message: 'Nozzle temperature set' });
  194. })
  195. );
  196. render(<PrintersPage />);
  197. await waitFor(() => {
  198. expect(screen.getByText('L / R')).toBeInTheDocument();
  199. });
  200. // Dual-nozzle temps live on the L/R temperature card, not the nozzle-select card.
  201. fireEvent.click(screen.getByText('L / R').parentElement!);
  202. const leftTempBox = screen.getByText('Left Temp').parentElement!.parentElement!;
  203. fireEvent.click(within(leftTempBox).getByRole('button', { name: '220 C' }));
  204. await waitFor(() => {
  205. expect(temperatureRequests).toContainEqual({ target: '220', nozzle: '1' });
  206. });
  207. fireEvent.click(screen.getByText('L / R').parentElement!);
  208. const rightTempBox = screen.getByText('Right Temp').parentElement!.parentElement!;
  209. fireEvent.click(within(rightTempBox).getByRole('button', { name: '260 C' }));
  210. await waitFor(() => {
  211. expect(temperatureRequests).toContainEqual({ target: '260', nozzle: '0' });
  212. });
  213. });
  214. });
  215. describe('fan badges', () => {
  216. // Chamber fan only exists on enclosed Bambu models. Open-frame printers
  217. // (A1, A1 Mini, A2L, P1P) have no chamber fan — the firmware reports
  218. // big_fan2_speed as 0 there and the widget would be dead UI.
  219. const statusWithFans = {
  220. ...mockPrinterStatus,
  221. cooling_fan_speed: 53,
  222. big_fan1_speed: 53,
  223. big_fan2_speed: 53,
  224. };
  225. const renderWithPrinter = (printer: typeof mockPrinters[number]) => {
  226. server.use(
  227. http.get('/api/v1/printers/', () => HttpResponse.json([printer])),
  228. http.get('/api/v1/printers/:id/status', () => HttpResponse.json(statusWithFans)),
  229. );
  230. render(<PrintersPage />);
  231. };
  232. it('hides chamber fan badge on A1 Mini (open-frame, no chamber fan)', async () => {
  233. renderWithPrinter({ ...mockPrinters[0], model: 'A1 Mini' });
  234. await waitFor(() => {
  235. // Part-cooling badge confirms the fan row rendered.
  236. expect(screen.getByTitle('Part Cooling Fan')).toBeInTheDocument();
  237. });
  238. expect(screen.getByTitle('Auxiliary Fan')).toBeInTheDocument();
  239. expect(screen.queryByTitle('Chamber Fan')).not.toBeInTheDocument();
  240. });
  241. it('hides chamber fan badge on A1 (open-frame)', async () => {
  242. renderWithPrinter({ ...mockPrinters[0], model: 'A1' });
  243. await waitFor(() => {
  244. expect(screen.getByTitle('Part Cooling Fan')).toBeInTheDocument();
  245. });
  246. expect(screen.queryByTitle('Chamber Fan')).not.toBeInTheDocument();
  247. });
  248. it('hides chamber fan badge on P1P (open-frame)', async () => {
  249. renderWithPrinter({ ...mockPrinters[0], model: 'P1P' });
  250. await waitFor(() => {
  251. expect(screen.getByTitle('Part Cooling Fan')).toBeInTheDocument();
  252. });
  253. expect(screen.queryByTitle('Chamber Fan')).not.toBeInTheDocument();
  254. });
  255. it('shows chamber fan badge on X1C (enclosed)', async () => {
  256. renderWithPrinter({ ...mockPrinters[0], model: 'X1C' });
  257. await waitFor(() => {
  258. expect(screen.getByTitle('Chamber Fan')).toBeInTheDocument();
  259. });
  260. expect(screen.getByTitle('Part Cooling Fan')).toBeInTheDocument();
  261. expect(screen.getByTitle('Auxiliary Fan')).toBeInTheDocument();
  262. });
  263. it('shows chamber fan badge on P1S (enclosed)', async () => {
  264. renderWithPrinter({ ...mockPrinters[0], model: 'P1S' });
  265. await waitFor(() => {
  266. expect(screen.getByTitle('Chamber Fan')).toBeInTheDocument();
  267. });
  268. });
  269. // P2S/X2D left auxiliary part cooling fan (airduct part id 10) — optional
  270. // hardware, so the badge must only appear when the firmware reports it.
  271. const renderWithStatus = (
  272. printer: typeof mockPrinters[number],
  273. status: Record<string, unknown>,
  274. ) => {
  275. server.use(
  276. http.get('/api/v1/printers/', () => HttpResponse.json([printer])),
  277. http.get('/api/v1/printers/:id/status', () => HttpResponse.json(status)),
  278. );
  279. render(<PrintersPage />);
  280. };
  281. it('shows the exhaust tile labeled "Exhaust" on P2S when the kit is present', async () => {
  282. // Exhaust fan is an add-on kit; the tile appears only when the printer
  283. // reports it (airduct part id 3 -> exhaust_fan_present).
  284. renderWithStatus(
  285. { ...mockPrinters[0], model: 'P2S' },
  286. { ...statusWithFans, exhaust_fan_present: true },
  287. );
  288. await waitFor(() => {
  289. expect(screen.getByTitle('Part Cooling Fan')).toBeInTheDocument();
  290. });
  291. expect(screen.getByTitle('Exhaust')).toBeInTheDocument();
  292. expect(screen.queryByTitle('Chamber Fan')).not.toBeInTheDocument();
  293. });
  294. it('hides the exhaust tile on a base P2S without the kit', async () => {
  295. renderWithStatus(
  296. { ...mockPrinters[0], model: 'P2S' },
  297. { ...statusWithFans, exhaust_fan_present: false },
  298. );
  299. await waitFor(() => {
  300. expect(screen.getByTitle('Part Cooling Fan')).toBeInTheDocument();
  301. });
  302. expect(screen.queryByTitle('Exhaust')).not.toBeInTheDocument();
  303. expect(screen.queryByTitle('Chamber Fan')).not.toBeInTheDocument();
  304. });
  305. it('keeps the always-on "Chamber Fan" tile on X1C regardless of exhaust_fan_present', async () => {
  306. renderWithStatus(
  307. { ...mockPrinters[0], model: 'X1C' },
  308. { ...statusWithFans, exhaust_fan_present: false },
  309. );
  310. await waitFor(() => {
  311. expect(screen.getByTitle('Chamber Fan')).toBeInTheDocument();
  312. });
  313. expect(screen.queryByTitle('Exhaust')).not.toBeInTheDocument();
  314. });
  315. it('hides the left aux badge when the accessory is not reported', async () => {
  316. renderWithStatus({ ...mockPrinters[0], model: 'P2S' }, statusWithFans);
  317. await waitFor(() => {
  318. expect(screen.getByTitle('Part Cooling Fan')).toBeInTheDocument();
  319. });
  320. expect(screen.queryByTitle('Left Auxiliary Fan')).not.toBeInTheDocument();
  321. });
  322. it('orders the fan badges left-to-right: part, left aux, aux, exhaust', async () => {
  323. // The two aux badges should read in the same order as the physical
  324. // hardware, so the left fan sits before the right one.
  325. renderWithStatus(
  326. { ...mockPrinters[0], model: 'P2S' },
  327. { ...statusWithFans, left_aux_fan_speed: 80, exhaust_fan_present: true },
  328. );
  329. await waitFor(() => {
  330. expect(screen.getByTitle('Left Auxiliary Fan')).toBeInTheDocument();
  331. });
  332. const order = ['Part Cooling Fan', 'Left Auxiliary Fan', 'Auxiliary Fan', 'Exhaust'].map(
  333. (title) => screen.getByTitle(title),
  334. );
  335. for (let i = 1; i < order.length; i++) {
  336. // Node.compareDocumentPosition returns FOLLOWING (4) when the argument
  337. // comes after the reference node in document order.
  338. expect(order[i - 1].compareDocumentPosition(order[i])).toBe(Node.DOCUMENT_POSITION_FOLLOWING);
  339. }
  340. });
  341. it('shows left aux fan badge when the accessory is installed (P2S)', async () => {
  342. renderWithStatus(
  343. { ...mockPrinters[0], model: 'P2S' },
  344. { ...statusWithFans, left_aux_fan_speed: 80 },
  345. );
  346. await waitFor(() => {
  347. expect(screen.getByTitle('Left Auxiliary Fan')).toBeInTheDocument();
  348. });
  349. });
  350. it('shows left aux fan badge even at 0% while installed', async () => {
  351. renderWithStatus(
  352. { ...mockPrinters[0], model: 'P2S' },
  353. { ...statusWithFans, left_aux_fan_speed: 0 },
  354. );
  355. await waitFor(() => {
  356. expect(screen.getByTitle('Left Auxiliary Fan')).toBeInTheDocument();
  357. });
  358. });
  359. });
  360. describe('empty state', () => {
  361. it('shows empty state when no printers', async () => {
  362. server.use(
  363. http.get('/api/v1/printers/', () => {
  364. return HttpResponse.json([]);
  365. })
  366. );
  367. render(<PrintersPage />);
  368. await waitFor(() => {
  369. expect(screen.getByText(/no printers/i)).toBeInTheDocument();
  370. });
  371. });
  372. });
  373. describe('printer actions', () => {
  374. it('has action buttons', async () => {
  375. render(<PrintersPage />);
  376. await waitFor(() => {
  377. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  378. });
  379. // There should be some interactive elements for printer actions
  380. const buttons = screen.getAllByRole('button');
  381. expect(buttons.length).toBeGreaterThan(0);
  382. });
  383. it('shows plate clear status and action on finished printers when not cleared', async () => {
  384. server.use(
  385. http.get('/api/v1/printers/:id/status', () => {
  386. return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: true });
  387. })
  388. );
  389. render(<PrintersPage />);
  390. await waitFor(() => {
  391. expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
  392. });
  393. expect(screen.getAllByRole('button', { name: 'Mark plate as cleared' }).length).toBeGreaterThan(0);
  394. });
  395. it('shows plate clear status and action on failed printers when not cleared', async () => {
  396. server.use(
  397. http.get('/api/v1/printers/:id/status', () => {
  398. return HttpResponse.json({ ...mockPrinterStatus, state: 'FAILED', awaiting_plate_clear: true });
  399. })
  400. );
  401. render(<PrintersPage />);
  402. await waitFor(() => {
  403. expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
  404. });
  405. expect(screen.getAllByRole('button', { name: 'Mark plate as cleared' }).length).toBeGreaterThan(0);
  406. });
  407. it('keeps the clear action available when an idle printer is still awaiting acknowledgment', async () => {
  408. server.use(
  409. http.get('/api/v1/printers/:id/status', () => {
  410. return HttpResponse.json({ ...mockPrinterStatus, state: 'IDLE', awaiting_plate_clear: true });
  411. })
  412. );
  413. render(<PrintersPage />);
  414. await waitFor(() => {
  415. expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
  416. });
  417. expect(screen.getAllByRole('button', { name: 'Mark plate as cleared' }).length).toBeGreaterThan(0);
  418. });
  419. it('updates the plate clear status after using the printer card action', async () => {
  420. let awaitingPlateClear = true;
  421. server.use(
  422. http.get('/api/v1/printers/', () => {
  423. return HttpResponse.json([mockPrinters[0]]);
  424. }),
  425. http.get('/api/v1/printers/:id/status', () => {
  426. return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: awaitingPlateClear });
  427. }),
  428. http.post('/api/v1/printers/:id/clear-plate', () => {
  429. awaitingPlateClear = false;
  430. return HttpResponse.json({ success: true, message: 'Plate cleared' });
  431. })
  432. );
  433. render(<PrintersPage />);
  434. await waitFor(() => {
  435. expect(screen.getAllByText('Plate not Clear').length).toBeGreaterThan(0);
  436. });
  437. fireEvent.click(screen.getAllByRole('button', { name: 'Mark plate as cleared' })[0]);
  438. await waitFor(() => {
  439. expect(screen.queryByText('Plate not Clear')).not.toBeInTheDocument();
  440. });
  441. expect(screen.getAllByText('Plate Clear').length).toBeGreaterThan(0);
  442. });
  443. it('shows an icon-only plate clear action in small card view', async () => {
  444. let awaitingPlateClear = true;
  445. server.use(
  446. http.get('/api/v1/printers/', () => {
  447. return HttpResponse.json([mockPrinters[0]]);
  448. }),
  449. http.get('/api/v1/printers/:id/status', () => {
  450. return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: awaitingPlateClear });
  451. }),
  452. http.post('/api/v1/printers/:id/clear-plate', () => {
  453. awaitingPlateClear = false;
  454. return HttpResponse.json({ success: true, message: 'Plate cleared' });
  455. })
  456. );
  457. render(<PrintersPage />);
  458. await waitFor(() => {
  459. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  460. });
  461. fireEvent.click(screen.getByRole('button', { name: 'S' }));
  462. await waitFor(() => {
  463. expect(screen.queryByText('Mark plate as cleared')).not.toBeInTheDocument();
  464. });
  465. const clearButton = screen.getByRole('button', { name: 'Mark plate as cleared' });
  466. fireEvent.click(clearButton);
  467. await waitFor(() => {
  468. expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
  469. });
  470. });
  471. it('shows plate clear status but no action while idle', async () => {
  472. render(<PrintersPage />);
  473. await waitFor(() => {
  474. expect(screen.getAllByText('Plate Clear').length).toBeGreaterThan(0);
  475. });
  476. expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
  477. });
  478. it('shows plate in use status while printing and hides the clear action', async () => {
  479. server.use(
  480. http.get('/api/v1/printers/:id/status', () => {
  481. return HttpResponse.json({ ...mockPrinterStatus, state: 'RUNNING', awaiting_plate_clear: false });
  482. })
  483. );
  484. render(<PrintersPage />);
  485. await waitFor(() => {
  486. expect(screen.getAllByText('Plate in Use').length).toBeGreaterThan(0);
  487. });
  488. expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
  489. });
  490. it('hides plate status and action when plate-clear confirmation is disabled', async () => {
  491. server.use(
  492. http.get('/api/v1/settings/', () => {
  493. return HttpResponse.json({
  494. auto_archive: true,
  495. save_thumbnails: true,
  496. capture_finish_photo: true,
  497. default_filament_cost: 25.0,
  498. currency: 'USD',
  499. ams_humidity_good: 40,
  500. ams_humidity_fair: 60,
  501. ams_temp_good: 30,
  502. ams_temp_fair: 35,
  503. require_plate_clear: false,
  504. });
  505. }),
  506. http.get('/api/v1/settings/ui-preferences', () => {
  507. return HttpResponse.json({
  508. ams_humidity_good: 40,
  509. ams_humidity_fair: 60,
  510. ams_temp_good: 30,
  511. ams_temp_fair: 35,
  512. require_plate_clear: false,
  513. });
  514. }),
  515. http.get('/api/v1/printers/:id/status', () => {
  516. return HttpResponse.json({ ...mockPrinterStatus, state: 'FINISH', awaiting_plate_clear: true });
  517. })
  518. );
  519. render(<PrintersPage />);
  520. await waitFor(() => {
  521. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  522. });
  523. expect(screen.queryByText('Plate not Clear')).not.toBeInTheDocument();
  524. expect(screen.queryByText('Plate Clear')).not.toBeInTheDocument();
  525. expect(screen.queryByText('Plate in Use')).not.toBeInTheDocument();
  526. expect(screen.queryByRole('button', { name: 'Mark plate as cleared' })).not.toBeInTheDocument();
  527. });
  528. });
  529. describe('disabled printer', () => {
  530. it('shows disabled state for disabled printers', async () => {
  531. render(<PrintersPage />);
  532. await waitFor(() => {
  533. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  534. });
  535. // Disabled printers have visual indication
  536. const disabledPrinter = screen.getByText('P1S Backup').closest('div');
  537. expect(disabledPrinter).toBeInTheDocument();
  538. });
  539. });
  540. describe('maintenance mode (#1476)', () => {
  541. // Wraps the backend is_active flag — already gates MQTT, queue dispatch,
  542. // scheduler, metrics, picker. These tests pin the UI surface: status
  543. // panel swap, pill swap, and the PATCH on toggle.
  544. const inMaintenancePrinter = { ...mockPrinters[0], is_active: false };
  545. it('shows the maintenance status panel instead of the print container', async () => {
  546. server.use(
  547. http.get('/api/v1/printers/', () => HttpResponse.json([inMaintenancePrinter])),
  548. http.get('/api/v1/printers/:id/status', () =>
  549. HttpResponse.json({ ...mockPrinterStatus, connected: false }),
  550. ),
  551. );
  552. render(<PrintersPage />);
  553. await waitFor(() => {
  554. expect(screen.getByText('In Maintenance')).toBeInTheDocument();
  555. });
  556. // Exit button rendered
  557. expect(screen.getByRole('button', { name: /exit maintenance/i })).toBeInTheDocument();
  558. // The "No active job" / "Ready to print" copy from the normal status
  559. // panel must NOT be present — confirms the swap, not a stacked render.
  560. expect(screen.queryByText(/no active job/i)).not.toBeInTheDocument();
  561. expect(screen.queryByText(/ready to print/i)).not.toBeInTheDocument();
  562. });
  563. it('shows the amber Maintenance pill in the header (no Connected/Offline)', async () => {
  564. server.use(
  565. http.get('/api/v1/printers/', () => HttpResponse.json([inMaintenancePrinter])),
  566. http.get('/api/v1/printers/:id/status', () =>
  567. HttpResponse.json({ ...mockPrinterStatus, connected: false }),
  568. ),
  569. );
  570. render(<PrintersPage />);
  571. // The header pill row contains "Maintenance" exactly once.
  572. await waitFor(() => {
  573. expect(screen.getAllByText('Maintenance').length).toBeGreaterThan(0);
  574. });
  575. // No connection diagnostic CTA (that's reserved for involuntary offline).
  576. expect(screen.queryByRole('button', { name: /run.*diagnostic/i })).not.toBeInTheDocument();
  577. });
  578. it('PATCHes is_active=true when the Exit button is clicked', async () => {
  579. const patchedBodies: unknown[] = [];
  580. server.use(
  581. http.get('/api/v1/printers/', () => HttpResponse.json([inMaintenancePrinter])),
  582. http.get('/api/v1/printers/:id/status', () =>
  583. HttpResponse.json({ ...mockPrinterStatus, connected: false }),
  584. ),
  585. http.patch('/api/v1/printers/:id', async ({ request }) => {
  586. const body = await request.json();
  587. patchedBodies.push(body);
  588. return HttpResponse.json({ ...inMaintenancePrinter, is_active: true });
  589. }),
  590. );
  591. render(<PrintersPage />);
  592. const exit = await screen.findByRole('button', { name: /exit maintenance/i });
  593. fireEvent.click(exit);
  594. await waitFor(() => {
  595. expect(patchedBodies.length).toBeGreaterThan(0);
  596. });
  597. expect(patchedBodies[0]).toEqual(expect.objectContaining({ is_active: true }));
  598. });
  599. it('renders the regular status panel when is_active=true', async () => {
  600. server.use(
  601. http.get('/api/v1/printers/', () => HttpResponse.json([{ ...mockPrinters[0], is_active: true }])),
  602. http.get('/api/v1/printers/:id/status', () => HttpResponse.json(mockPrinterStatus)),
  603. );
  604. render(<PrintersPage />);
  605. await waitFor(() => {
  606. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  607. });
  608. // Active printer never shows the maintenance panel.
  609. expect(screen.queryByText('In Maintenance')).not.toBeInTheDocument();
  610. expect(screen.queryByRole('button', { name: /exit maintenance/i })).not.toBeInTheDocument();
  611. });
  612. });
  613. describe('nozzle rack card', () => {
  614. const h2cStatus = {
  615. ...mockPrinterStatus,
  616. nozzle_rack: [
  617. { 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: '' },
  618. { 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: '' },
  619. { 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: '' },
  620. { 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: '' },
  621. { 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: '' },
  622. { id: 19, nozzle_type: '', nozzle_diameter: '', wear: null, stat: null, max_temp: 0, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  623. { id: 20, nozzle_type: '', nozzle_diameter: '', wear: null, stat: null, max_temp: 0, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  624. { id: 21, nozzle_type: '', nozzle_diameter: '', wear: null, stat: null, max_temp: 0, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  625. ],
  626. };
  627. it('shows nozzle rack when H2C rack slots present', async () => {
  628. server.use(
  629. http.get('/api/v1/printers/:id/status', () => {
  630. return HttpResponse.json(h2cStatus);
  631. })
  632. );
  633. render(<PrintersPage />);
  634. await waitFor(() => {
  635. expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
  636. });
  637. });
  638. it('shows 6 rack slot elements for H2C', async () => {
  639. server.use(
  640. http.get('/api/v1/printers/:id/status', () => {
  641. return HttpResponse.json(h2cStatus);
  642. })
  643. );
  644. render(<PrintersPage />);
  645. await waitFor(() => {
  646. expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
  647. });
  648. // Rack shows diameters for occupied slots and dashes for empty ones
  649. const dashes = screen.getAllByText('—');
  650. expect(dashes.length).toBeGreaterThanOrEqual(3); // 3 empty rack positions (IDs 19,20,21)
  651. });
  652. it('keeps empty slot anchored to physical position when its nozzle is mounted (#943)', async () => {
  653. // H2C with rack slot 16 picked up into the hotend — firmware omits ID 16
  654. // entirely from nozzle.info. Each rack diameter is unique so we can assert
  655. // the ordering by tooltip lookup.
  656. const h2cSlot16Mounted = {
  657. ...mockPrinterStatus,
  658. nozzle_rack: [
  659. { 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: '' },
  660. { 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: '' },
  661. // ID 16 missing — currently in hotend
  662. { 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: '' },
  663. { 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: '' },
  664. { 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: '' },
  665. { 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: '' },
  666. { 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: '' },
  667. ],
  668. };
  669. server.use(
  670. http.get('/api/v1/printers/:id/status', () => {
  671. return HttpResponse.json(h2cSlot16Mounted);
  672. })
  673. );
  674. render(<PrintersPage />);
  675. await waitFor(() => {
  676. expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
  677. });
  678. // Slot 1 (leftmost, ID 16) should be the empty dash; slots 2..6 should
  679. // hold the 5 remaining nozzles in order 17, 18, 19, 20, 21.
  680. const rackLabel = screen.getAllByText('Nozzle Rack')[0];
  681. const rackCard = rackLabel.parentElement!;
  682. const slotRow = rackCard.querySelectorAll('div.flex')[0];
  683. // Match the diameter spans specifically — each chip also carries a slot
  684. // number span, and a bare `span` sweep would interleave the two.
  685. const slotTexts = Array.from(slotRow.querySelectorAll('span[data-rack-diameter]')).map(s => s.textContent);
  686. expect(slotTexts).toEqual(['—', '0.2', '0.6', '0.8', '1.0', '1.2']);
  687. });
  688. it('labels every rack position 1..6 regardless of which nozzles are present', async () => {
  689. // The numbering is positional, not a count of what is loaded: the empty
  690. // position keeps its number so "the nozzle in slot 4" means the same
  691. // thing whether or not slots 1..3 are occupied.
  692. server.use(
  693. http.get('/api/v1/printers/:id/status', () => {
  694. return HttpResponse.json(h2cStatus);
  695. })
  696. );
  697. render(<PrintersPage />);
  698. await waitFor(() => {
  699. expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
  700. });
  701. const rackLabel = screen.getAllByText('Nozzle Rack')[0];
  702. const slotRow = rackLabel.parentElement!.querySelectorAll('div.flex')[0];
  703. const numbers = Array.from(slotRow.querySelectorAll('span:not([data-rack-diameter])')).map(s => s.textContent);
  704. expect(numbers).toEqual(['1', '2', '3', '4', '5', '6']);
  705. });
  706. it('sizes the rack chips from the card scale rather than a fixed 28px', async () => {
  707. // The chips were hard-coded while the type and icons around them grow
  708. // with the card size, so at L and XL the rack read as a shrunken strip
  709. // next to neighbours that had grown by up to 40%.
  710. server.use(
  711. http.get('/api/v1/printers/:id/status', () => {
  712. return HttpResponse.json(h2cStatus);
  713. })
  714. );
  715. render(<PrintersPage />);
  716. await waitFor(() => {
  717. expect(screen.getAllByText('Nozzle Rack').length).toBeGreaterThan(0);
  718. });
  719. const rackLabel = screen.getAllByText('Nozzle Rack')[0];
  720. const slotRow = rackLabel.parentElement!.querySelectorAll('div.flex')[0];
  721. const chip = slotRow.querySelector('span[data-rack-diameter]')!.parentElement!;
  722. expect(chip.className).toContain('w-[var(--pc-i7,28px)]');
  723. expect(chip.className).toContain('h-[var(--pc-i7,28px)]');
  724. });
  725. it('hides nozzle rack when only L/R nozzles present (H2D)', async () => {
  726. const h2dStatus = {
  727. ...mockPrinterStatus,
  728. nozzle_rack: [
  729. { id: 0, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 5, stat: 1, max_temp: 300, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  730. { id: 1, nozzle_type: 'HS', nozzle_diameter: '0.4', wear: 3, stat: 1, max_temp: 300, serial_number: '', filament_color: '', filament_id: '', filament_type: '' },
  731. ],
  732. };
  733. server.use(
  734. http.get('/api/v1/printers/:id/status', () => {
  735. return HttpResponse.json(h2dStatus);
  736. })
  737. );
  738. render(<PrintersPage />);
  739. await waitFor(() => {
  740. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  741. });
  742. expect(screen.queryByText('Nozzle Rack')).not.toBeInTheDocument();
  743. });
  744. });
  745. describe('firmware version badge', () => {
  746. const firmwareUpToDate = {
  747. printer_id: 1,
  748. current_version: '01.09.00.00',
  749. latest_version: '01.09.00.00',
  750. update_available: false,
  751. download_url: null,
  752. release_notes: 'Bug fixes and improvements.',
  753. };
  754. const firmwareUpdateAvailable = {
  755. printer_id: 1,
  756. current_version: '01.08.00.00',
  757. latest_version: '01.09.00.00',
  758. update_available: true,
  759. download_url: 'https://example.com/firmware.bin',
  760. release_notes: 'New features added.',
  761. };
  762. it('shows green badge when firmware is up to date', async () => {
  763. server.use(
  764. http.get('/api/v1/firmware/updates/:id', () => {
  765. return HttpResponse.json(firmwareUpToDate);
  766. }),
  767. http.get('/api/v1/settings/', () => {
  768. return HttpResponse.json({
  769. check_printer_firmware: true,
  770. auto_archive: true,
  771. save_thumbnails: true,
  772. });
  773. })
  774. );
  775. render(<PrintersPage />);
  776. await waitFor(() => {
  777. expect(screen.getAllByText('01.09.00.00').length).toBeGreaterThan(0);
  778. });
  779. const badge = screen.getAllByText('01.09.00.00')[0].closest('button');
  780. expect(badge).toBeInTheDocument();
  781. expect(badge?.className).toContain('text-status-ok');
  782. });
  783. it('shows orange badge when firmware update is available', async () => {
  784. server.use(
  785. http.get('/api/v1/firmware/updates/:id', () => {
  786. return HttpResponse.json(firmwareUpdateAvailable);
  787. }),
  788. http.get('/api/v1/settings/', () => {
  789. return HttpResponse.json({
  790. check_printer_firmware: true,
  791. auto_archive: true,
  792. save_thumbnails: true,
  793. });
  794. })
  795. );
  796. render(<PrintersPage />);
  797. await waitFor(() => {
  798. expect(screen.getAllByText('01.08.00.00').length).toBeGreaterThan(0);
  799. });
  800. const badge = screen.getAllByText('01.08.00.00')[0].closest('button');
  801. expect(badge).toBeInTheDocument();
  802. expect(badge?.className).toContain('text-orange-400');
  803. });
  804. it('hides badge when firmware check is disabled', async () => {
  805. server.use(
  806. http.get('/api/v1/settings/', () => {
  807. return HttpResponse.json({
  808. check_printer_firmware: false,
  809. auto_archive: true,
  810. save_thumbnails: true,
  811. });
  812. })
  813. );
  814. render(<PrintersPage />);
  815. await waitFor(() => {
  816. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  817. });
  818. // Version should not appear when firmware check is disabled
  819. expect(screen.queryByText('01.09.00.00')).not.toBeInTheDocument();
  820. expect(screen.queryByText('01.08.00.00')).not.toBeInTheDocument();
  821. });
  822. it('hides badge when API has no firmware data for the model', async () => {
  823. const firmwareNoData = {
  824. printer_id: 1,
  825. current_version: '01.01.03.00',
  826. latest_version: null,
  827. update_available: false,
  828. download_url: null,
  829. release_notes: null,
  830. };
  831. server.use(
  832. http.get('/api/v1/firmware/updates/:id', () => {
  833. return HttpResponse.json(firmwareNoData);
  834. }),
  835. http.get('/api/v1/settings/', () => {
  836. return HttpResponse.json({
  837. check_printer_firmware: true,
  838. auto_archive: true,
  839. save_thumbnails: true,
  840. });
  841. })
  842. );
  843. render(<PrintersPage />);
  844. await waitFor(() => {
  845. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  846. });
  847. // Badge should not appear when API returns no latest_version
  848. expect(screen.queryByText('01.01.03.00')).not.toBeInTheDocument();
  849. });
  850. });
  851. describe('bulk selection', () => {
  852. it('shows select button in toolbar', async () => {
  853. render(<PrintersPage />);
  854. await waitFor(() => {
  855. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  856. });
  857. // The Select button should be in the toolbar (title attribute)
  858. const selectButton = screen.getByTitle('Select');
  859. expect(selectButton).toBeInTheDocument();
  860. });
  861. it('shows selection toolbar after clicking select button', async () => {
  862. render(<PrintersPage />);
  863. await waitFor(() => {
  864. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  865. });
  866. // Click the Select button to enter selection mode
  867. fireEvent.click(screen.getByTitle('Select'));
  868. // The floating toolbar should appear with Select All
  869. await waitFor(() => {
  870. expect(screen.getByText('Select All')).toBeInTheDocument();
  871. });
  872. });
  873. it('shows selection count when printers are selected', async () => {
  874. render(<PrintersPage />);
  875. await waitFor(() => {
  876. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  877. });
  878. // Enter selection mode
  879. fireEvent.click(screen.getByTitle('Select'));
  880. await waitFor(() => {
  881. expect(screen.getByText('Select All')).toBeInTheDocument();
  882. });
  883. // Click Select All to select both printers
  884. fireEvent.click(screen.getByText('Select All'));
  885. // Should show "2 selected"
  886. await waitFor(() => {
  887. expect(screen.getByText('2 selected')).toBeInTheDocument();
  888. });
  889. });
  890. it('shows select by state dropdown', async () => {
  891. render(<PrintersPage />);
  892. await waitFor(() => {
  893. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  894. });
  895. // Enter selection mode
  896. fireEvent.click(screen.getByTitle('Select'));
  897. await waitFor(() => {
  898. expect(screen.getByText('Select by State')).toBeInTheDocument();
  899. });
  900. });
  901. it('exits selection mode on close button', async () => {
  902. render(<PrintersPage />);
  903. await waitFor(() => {
  904. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  905. });
  906. // Enter selection mode
  907. fireEvent.click(screen.getByTitle('Select'));
  908. await waitFor(() => {
  909. expect(screen.getByText('Select All')).toBeInTheDocument();
  910. });
  911. // Click the Select button again to exit (it toggles)
  912. fireEvent.click(screen.getByTitle('Select'));
  913. // Floating toolbar should disappear
  914. await waitFor(() => {
  915. expect(screen.queryByText('Select All')).not.toBeInTheDocument();
  916. });
  917. });
  918. });
  919. describe('search and filter', () => {
  920. beforeEach(() => {
  921. server.use(
  922. http.get('/api/v1/printers/', () => HttpResponse.json(mockPrinters)),
  923. http.get('/api/v1/printers/:id/status', () => HttpResponse.json(mockPrinterStatus)),
  924. http.get('/api/v1/queue/', () => HttpResponse.json([]))
  925. );
  926. });
  927. it('filters by name (case-insensitive)', async () => {
  928. render(<PrintersPage />);
  929. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  930. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'x1 carbon' } });
  931. await waitFor(() => {
  932. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  933. expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
  934. });
  935. });
  936. it('trims leading and trailing whitespace from search', async () => {
  937. render(<PrintersPage />);
  938. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  939. // " X1 Carbon " with surrounding spaces must still match
  940. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: ' X1 Carbon ' } });
  941. await waitFor(() => {
  942. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  943. expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
  944. });
  945. });
  946. it('filters by model', async () => {
  947. render(<PrintersPage />);
  948. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  949. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'P1S' } });
  950. await waitFor(() => {
  951. expect(screen.queryByText('X1 Carbon')).not.toBeInTheDocument();
  952. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  953. });
  954. });
  955. it('filters by serial number', async () => {
  956. render(<PrintersPage />);
  957. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  958. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: '00M09A' } });
  959. await waitFor(() => {
  960. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  961. expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
  962. });
  963. });
  964. it('shows empty state when no printers match search', async () => {
  965. render(<PrintersPage />);
  966. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  967. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'ZZZ_NO_MATCH' } });
  968. await waitFor(() => {
  969. expect(screen.getByText('No printers match your search or filters')).toBeInTheDocument();
  970. });
  971. });
  972. it('clear button resets search and shows all printers', async () => {
  973. render(<PrintersPage />);
  974. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  975. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'X1 Carbon' } });
  976. await waitFor(() => expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument());
  977. // Click the accessible clear button
  978. fireEvent.click(screen.getByRole('button', { name: 'Clear' }));
  979. await waitFor(() => {
  980. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  981. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  982. });
  983. });
  984. it('filters by status (offline) via dropdown', async () => {
  985. // Override: printer 1 online, printer 2 offline
  986. server.use(
  987. http.get('/api/v1/printers/:id/status', ({ params }) => {
  988. if (Number(params.id) === 2) {
  989. return HttpResponse.json({ ...mockPrinterStatus, connected: false });
  990. }
  991. return HttpResponse.json(mockPrinterStatus);
  992. })
  993. );
  994. render(<PrintersPage />);
  995. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  996. await selectToolbarDropdownOption(/all statuses/i, /^offline$/i);
  997. await waitFor(() => {
  998. expect(screen.queryByText('X1 Carbon')).not.toBeInTheDocument();
  999. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  1000. });
  1001. });
  1002. it('shows empty state when status filter matches nothing', async () => {
  1003. render(<PrintersPage />);
  1004. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  1005. // Both printers are IDLE; filtering by "printing" should yield no results
  1006. await selectToolbarDropdownOption(/all statuses/i, /^printing$/i);
  1007. await waitFor(() => {
  1008. expect(screen.getByText('No printers match your search or filters')).toBeInTheDocument();
  1009. });
  1010. });
  1011. it('combines search and status filter', async () => {
  1012. // Printer 1 = RUNNING (printing), printer 2 = IDLE
  1013. server.use(
  1014. http.get('/api/v1/printers/:id/status', ({ params }) => {
  1015. if (Number(params.id) === 1) {
  1016. return HttpResponse.json({ ...mockPrinterStatus, state: 'RUNNING' });
  1017. }
  1018. return HttpResponse.json(mockPrinterStatus);
  1019. })
  1020. );
  1021. render(<PrintersPage />);
  1022. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  1023. // Filter to only "printing" printers
  1024. await selectToolbarDropdownOption(/all statuses/i, /^printing$/i);
  1025. // Then also search for a term that only matches printer 1
  1026. fireEvent.change(screen.getByPlaceholderText('Search printers...'), { target: { value: 'X1' } });
  1027. await waitFor(() => {
  1028. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  1029. expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
  1030. });
  1031. });
  1032. it('filters by location via dropdown', async () => {
  1033. // Override: give printer 2 its own location so the dropdown has two options
  1034. // and we can verify the filter picks the right one. Printer 1 stays at 'Workshop'.
  1035. server.use(
  1036. http.get('/api/v1/printers/', () =>
  1037. HttpResponse.json([
  1038. mockPrinters[0],
  1039. { ...mockPrinters[1], location: 'Office' },
  1040. ])
  1041. )
  1042. );
  1043. render(<PrintersPage />);
  1044. await waitFor(() => {
  1045. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  1046. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  1047. });
  1048. await selectToolbarDropdownOption(/all locations/i, /^workshop$/i);
  1049. await waitFor(() => {
  1050. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  1051. expect(screen.queryByText('P1S Backup')).not.toBeInTheDocument();
  1052. });
  1053. await selectToolbarDropdownOption(/^workshop$/i, /^office$/i);
  1054. await waitFor(() => {
  1055. expect(screen.queryByText('X1 Carbon')).not.toBeInTheDocument();
  1056. expect(screen.getByText('P1S Backup')).toBeInTheDocument();
  1057. });
  1058. });
  1059. it('hides location filter when no printers have a location', async () => {
  1060. // Both printers have null location — dropdown should not render at all
  1061. server.use(
  1062. http.get('/api/v1/printers/', () =>
  1063. HttpResponse.json([
  1064. { ...mockPrinters[0], location: null },
  1065. { ...mockPrinters[1], location: null },
  1066. ])
  1067. )
  1068. );
  1069. render(<PrintersPage />);
  1070. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  1071. // Status filter is still there, but the location filter should be absent.
  1072. expect(screen.getByRole('button', { name: /all statuses/i })).toBeInTheDocument();
  1073. expect(screen.queryByRole('button', { name: /all locations/i })).not.toBeInTheDocument();
  1074. });
  1075. });
  1076. describe('Spoolman loading guard', () => {
  1077. it('does not show Assign Spool button while Spoolman queries are loading', async () => {
  1078. // Spoolman enabled but inventory and slot-assignment queries never resolve
  1079. server.use(
  1080. http.get('/api/v1/spoolman/status', () =>
  1081. HttpResponse.json({ enabled: true, connected: true })
  1082. ),
  1083. http.get('/api/v1/spoolman/inventory/spools', () =>
  1084. new Promise(() => {}) // never resolves
  1085. ),
  1086. http.get('/api/v1/spoolman/inventory/slot-assignments/all', () =>
  1087. new Promise(() => {}) // never resolves
  1088. )
  1089. );
  1090. render(<PrintersPage />);
  1091. // Wait for the page to render (printers should be visible)
  1092. await waitFor(() => expect(screen.getByText('X1 Carbon')).toBeInTheDocument());
  1093. // While Spoolman queries are still loading, the "Assign Spool" button must
  1094. // not appear (inventory prop is undefined → {inventory && ...} guard fires)
  1095. expect(screen.queryByText('Assign Spool')).not.toBeInTheDocument();
  1096. });
  1097. });
  1098. });
  1099. /**
  1100. * Phase 13 P13-1 (PrintersPage EmptySlotHoverCard onAssignSpool gate removal)
  1101. *
  1102. * Pre-Phase-13 each of the three EmptySlotHoverCard call-sites in PrintersPage
  1103. * gated `onAssignSpool` on `spoolmanEnabled ? (...) : undefined`, so empty
  1104. * slots in local-Inventory mode never showed an Assign action. Maintainer
  1105. * Foto 7 confirmed users expect the button regardless of mode.
  1106. *
  1107. * To assert wiring without going through hover-card animations, we mock the
  1108. * EmptySlotHoverCard component at module level and capture every props
  1109. * payload. The same mock is active in both modes; tests differ only in the
  1110. * spoolman-settings mock. The mock module covers BOTH FilamentHoverCard exports
  1111. * so tests outside this `describe` aren't affected (we re-export the real
  1112. * FilamentHoverCard).
  1113. */
  1114. const phase13EmptySlotProps: Array<Record<string, unknown>> = [];
  1115. const phase14HoverCardProps: Array<Record<string, unknown>> = [];
  1116. vi.mock('../../components/FilamentHoverCard', async (importOriginal) => {
  1117. const actual = await importOriginal<typeof import('../../components/FilamentHoverCard')>();
  1118. return {
  1119. ...actual,
  1120. EmptySlotHoverCard: (props: Record<string, unknown>) => {
  1121. phase13EmptySlotProps.push({ ...props });
  1122. return null;
  1123. },
  1124. FilamentHoverCard: (props: Record<string, unknown>) => {
  1125. phase14HoverCardProps.push({ ...props });
  1126. return null;
  1127. },
  1128. };
  1129. });
  1130. describe('PrintersPage Phase 13 — EmptySlotHoverCard onAssignSpool wiring', () => {
  1131. beforeEach(() => {
  1132. phase13EmptySlotProps.length = 0;
  1133. localStorage.removeItem('printerCardSize');
  1134. server.use(
  1135. http.get('/api/v1/printers/', () => HttpResponse.json(mockPrinters)),
  1136. // Status response includes an empty AMS slot so EmptySlotHoverCard renders.
  1137. http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
  1138. ...mockPrinterStatus,
  1139. ams: [{
  1140. id: 0,
  1141. tray: [{ id: 0, tray_type: '' }],
  1142. }],
  1143. })),
  1144. http.get('/api/v1/settings/', () => HttpResponse.json({
  1145. auto_archive: true, save_thumbnails: true, capture_finish_photo: true,
  1146. default_filament_cost: 25.0, currency: 'USD',
  1147. ams_humidity_good: 40, ams_humidity_fair: 60,
  1148. ams_temp_good: 30, ams_temp_fair: 35,
  1149. })),
  1150. http.get('/api/v1/queue/', () => HttpResponse.json([])),
  1151. );
  1152. });
  1153. it('P13-1 (local mode): EmptySlotHoverCard receives onAssignSpool callback', async () => {
  1154. server.use(
  1155. http.get('/api/v1/spoolman/settings', () => HttpResponse.json({
  1156. spoolman_enabled: 'false', spoolman_url: '',
  1157. })),
  1158. );
  1159. render(<PrintersPage />);
  1160. // Wait for printer status to load and at least one EmptySlotHoverCard
  1161. // to mount with an onAssignSpool callback. Pre-Phase-13 this would have
  1162. // been undefined in local mode (the gate filtered it out).
  1163. await waitFor(() => {
  1164. const withCallback = phase13EmptySlotProps.filter(p => typeof p.onAssignSpool === 'function');
  1165. expect(withCallback.length).toBeGreaterThan(0);
  1166. }, { timeout: 3000 });
  1167. });
  1168. it('#1322: empty slot kind is "physical" when state=9 and "reset" otherwise', async () => {
  1169. // Bambuddy now distinguishes a firmware-confirmed empty slot (state=9
  1170. // via tray_exist_bits) from a slot the user reset but where the
  1171. // firmware still has a spool registered. The kind prop drives both
  1172. // the inline label ("Empty" vs "Reset") and the hover card label.
  1173. server.use(
  1174. http.get('/api/v1/spoolman/settings', () => HttpResponse.json({
  1175. spoolman_enabled: 'false', spoolman_url: '',
  1176. })),
  1177. http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
  1178. ...mockPrinterStatus,
  1179. ams: [{
  1180. id: 0,
  1181. tray: [
  1182. { id: 0, tray_type: '', state: 9 }, // physically empty
  1183. { id: 1, tray_type: '', state: 3 }, // reset / unloading
  1184. { id: 2, tray_type: '', state: null }, // unknown empty
  1185. { id: 3, tray_type: 'PLA', state: 11 }, // loaded — no card here
  1186. ],
  1187. }],
  1188. })),
  1189. );
  1190. render(<PrintersPage />);
  1191. await waitFor(() => {
  1192. expect(phase13EmptySlotProps.filter(p => p.kind === 'physical').length).toBeGreaterThan(0);
  1193. }, { timeout: 3000 });
  1194. const physical = phase13EmptySlotProps.filter(p => p.kind === 'physical');
  1195. const reset = phase13EmptySlotProps.filter(p => p.kind === 'reset');
  1196. expect(physical.length).toBeGreaterThan(0);
  1197. expect(reset.length).toBeGreaterThan(0);
  1198. // state=null falls back to 'reset' too — the helper only returns
  1199. // 'physical' for the canonical 9/10 firmware codes.
  1200. });
  1201. it('P13-1 (spoolman mode): EmptySlotHoverCard still receives onAssignSpool callback', async () => {
  1202. server.use(
  1203. http.get('/api/v1/spoolman/settings', () => HttpResponse.json({
  1204. spoolman_enabled: 'true', spoolman_url: 'http://x:7912',
  1205. })),
  1206. http.get('/api/v1/spoolman/spools/inventory*', () => HttpResponse.json([])),
  1207. http.get('/api/v1/spoolman/inventory/spools', () => HttpResponse.json([])),
  1208. http.get('/api/v1/spoolman/inventory/slot-assignments/all', () => HttpResponse.json([])),
  1209. );
  1210. render(<PrintersPage />);
  1211. await waitFor(() => {
  1212. const withCallback = phase13EmptySlotProps.filter(p => typeof p.onAssignSpool === 'function');
  1213. expect(withCallback.length).toBeGreaterThan(0);
  1214. }, { timeout: 3000 });
  1215. });
  1216. });
  1217. /**
  1218. * Phase 14 — Local-Branch BL-detection symmetry.
  1219. *
  1220. * The Spoolman branch of every IIFE in PrintersPage already passes
  1221. * isAssigned: !!slotAssignment || isBambuLabSpool(tray)
  1222. * onUnassignSpool: (spoolmanSpool && !isBambuLabSpool(tray)) ? ... : undefined
  1223. *
  1224. * The local branch was missing both. As a result a BL-RFID-tagged slot in
  1225. * local-Inventory mode showed an "Assign Spool" button (because no manual
  1226. * SpoolAssignment exists), and a manually-assigned BL-RFID slot showed
  1227. * "Unassign" — which would be overwritten on the next RFID re-read.
  1228. *
  1229. * The same FilamentHoverCard mock from the Phase 13 block above captures
  1230. * inventory props on every render so we can inspect them after setup.
  1231. */
  1232. describe('PrintersPage Phase 14 — Local-Branch BL-detection symmetry', () => {
  1233. beforeEach(() => {
  1234. phase14HoverCardProps.length = 0;
  1235. localStorage.removeItem('printerCardSize');
  1236. server.use(
  1237. http.get('/api/v1/printers/', () => HttpResponse.json(mockPrinters)),
  1238. http.get('/api/v1/settings/', () => HttpResponse.json({
  1239. auto_archive: true, save_thumbnails: true, capture_finish_photo: true,
  1240. default_filament_cost: 25.0, currency: 'USD',
  1241. ams_humidity_good: 40, ams_humidity_fair: 60,
  1242. ams_temp_good: 30, ams_temp_fair: 35,
  1243. })),
  1244. http.get('/api/v1/queue/', () => HttpResponse.json([])),
  1245. http.get('/api/v1/spoolman/settings', () => HttpResponse.json({
  1246. spoolman_enabled: 'false', spoolman_url: '',
  1247. })),
  1248. );
  1249. });
  1250. it('P14-1a (local + BL-RFID + no assignment): inventory.isAssigned=true', async () => {
  1251. server.use(
  1252. http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
  1253. ...mockPrinterStatus,
  1254. ams: [{
  1255. id: 0,
  1256. tray: [{
  1257. id: 0,
  1258. tray_type: 'PLA',
  1259. tray_uuid: '11223344556677880011223344556677',
  1260. tag_uid: '0000000000000000',
  1261. tray_color: 'FF0000FF',
  1262. tray_sub_brands: 'Bambu PLA Basic',
  1263. }],
  1264. }],
  1265. })),
  1266. http.get('/api/v1/inventory/assignments', () => HttpResponse.json([])),
  1267. );
  1268. render(<PrintersPage />);
  1269. await waitFor(() => {
  1270. const matches = phase14HoverCardProps.filter(
  1271. p => (p.inventory as { isAssigned?: boolean } | undefined)?.isAssigned === true
  1272. );
  1273. expect(matches.length).toBeGreaterThan(0);
  1274. }, { timeout: 3000 });
  1275. });
  1276. it('P14-1b (local + non-BL + no assignment): inventory.isAssigned is falsy', async () => {
  1277. server.use(
  1278. http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
  1279. ...mockPrinterStatus,
  1280. ams: [{
  1281. id: 0,
  1282. tray: [{
  1283. id: 0,
  1284. tray_type: 'PLA',
  1285. tray_uuid: '00000000000000000000000000000000',
  1286. tag_uid: '0000000000000000',
  1287. tray_color: 'FF0000FF',
  1288. tray_sub_brands: 'Generic PLA',
  1289. }],
  1290. }],
  1291. })),
  1292. http.get('/api/v1/inventory/assignments', () => HttpResponse.json([])),
  1293. );
  1294. render(<PrintersPage />);
  1295. // Wait for FilamentHoverCard to render at least once.
  1296. await waitFor(() => {
  1297. expect(phase14HoverCardProps.length).toBeGreaterThan(0);
  1298. }, { timeout: 3000 });
  1299. // No render should ever set isAssigned=true for this slot.
  1300. const truthyMatches = phase14HoverCardProps.filter(
  1301. p => (p.inventory as { isAssigned?: boolean } | undefined)?.isAssigned === true
  1302. );
  1303. expect(truthyMatches.length).toBe(0);
  1304. });
  1305. it('P14-1c (local + manual assignment): inventory.isAssigned=true', async () => {
  1306. server.use(
  1307. http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
  1308. ...mockPrinterStatus,
  1309. ams: [{
  1310. id: 0,
  1311. tray: [{
  1312. id: 0,
  1313. tray_type: 'PLA',
  1314. tray_uuid: '00000000000000000000000000000000',
  1315. tag_uid: '0000000000000000',
  1316. tray_color: 'FF0000FF',
  1317. tray_sub_brands: 'Generic PLA',
  1318. }],
  1319. }],
  1320. })),
  1321. http.get('/api/v1/inventory/assignments', () => HttpResponse.json([
  1322. {
  1323. id: 1,
  1324. spool_id: 42,
  1325. printer_id: 1,
  1326. ams_id: 0,
  1327. tray_id: 0,
  1328. printer_name: 'X1 Carbon',
  1329. ams_label: null,
  1330. spool: {
  1331. id: 42,
  1332. material: 'PLA',
  1333. brand: 'Generic',
  1334. color_name: 'Red',
  1335. label_weight: 1000,
  1336. weight_used: 0,
  1337. rgba: 'FF0000FF',
  1338. },
  1339. },
  1340. ])),
  1341. );
  1342. render(<PrintersPage />);
  1343. await waitFor(() => {
  1344. const matches = phase14HoverCardProps.filter(
  1345. p => (p.inventory as { isAssigned?: boolean } | undefined)?.isAssigned === true
  1346. );
  1347. expect(matches.length).toBeGreaterThan(0);
  1348. }, { timeout: 3000 });
  1349. });
  1350. it('P14-2 (local + BL-RFID + manual assignment): onUnassignSpool=undefined', async () => {
  1351. server.use(
  1352. http.get('/api/v1/printers/:id/status', () => HttpResponse.json({
  1353. ...mockPrinterStatus,
  1354. ams: [{
  1355. id: 0,
  1356. tray: [{
  1357. id: 0,
  1358. tray_type: 'PLA',
  1359. tray_uuid: '11223344556677880011223344556677',
  1360. tag_uid: '0000000000000000',
  1361. tray_color: 'FF0000FF',
  1362. tray_sub_brands: 'Bambu PLA Basic',
  1363. }],
  1364. }],
  1365. })),
  1366. http.get('/api/v1/inventory/assignments', () => HttpResponse.json([
  1367. {
  1368. id: 1,
  1369. spool_id: 42,
  1370. printer_id: 1,
  1371. ams_id: 0,
  1372. tray_id: 0,
  1373. printer_name: 'X1 Carbon',
  1374. ams_label: null,
  1375. spool: {
  1376. id: 42,
  1377. material: 'PLA',
  1378. brand: 'Bambu Lab',
  1379. color_name: 'Red',
  1380. label_weight: 1000,
  1381. weight_used: 0,
  1382. rgba: 'FF0000FF',
  1383. },
  1384. },
  1385. ])),
  1386. );
  1387. render(<PrintersPage />);
  1388. // Wait for FilamentHoverCard renders to settle.
  1389. await waitFor(() => {
  1390. expect(phase14HoverCardProps.length).toBeGreaterThan(0);
  1391. }, { timeout: 3000 });
  1392. // For BL-detected slots in local mode, onUnassignSpool must always be
  1393. // undefined — even when a manual assignment exists. Otherwise the user
  1394. // could unassign a BL-RFID slot that the printer would re-assign on the
  1395. // next re-read, surprising them with phantom ghost-assignments.
  1396. const definedUnassign = phase14HoverCardProps.filter(
  1397. p => typeof (p.inventory as { onUnassignSpool?: () => void } | undefined)?.onUnassignSpool === 'function'
  1398. );
  1399. expect(definedUnassign.length).toBe(0);
  1400. });
  1401. });