QueuePage.test.tsx 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  1. /**
  2. * Tests for the QueuePage component.
  3. */
  4. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  5. import { screen, waitFor, within } from '@testing-library/react';
  6. import userEvent from '@testing-library/user-event';
  7. import { render } from '../utils';
  8. import { QueuePage } from '../../pages/QueuePage';
  9. import { http, HttpResponse } from 'msw';
  10. import { server } from '../mocks/server';
  11. import { setAuthToken } from '../../api/client';
  12. // Mock queue data
  13. const mockQueueItems = [
  14. {
  15. id: 1,
  16. printer_id: 1,
  17. archive_id: 1,
  18. position: 1,
  19. status: 'pending',
  20. scheduled_time: null,
  21. require_previous_success: false,
  22. auto_off_after: false,
  23. manual_start: false,
  24. ams_mapping: null,
  25. plate_id: null,
  26. bed_levelling: 'on',
  27. flow_cali: 'off',
  28. vibration_cali: true,
  29. layer_inspect: false,
  30. timelapse: false,
  31. use_ams: true,
  32. started_at: null,
  33. completed_at: null,
  34. error_message: null,
  35. created_at: '2024-01-01T00:00:00Z',
  36. archive_name: 'Test Print 1',
  37. archive_thumbnail: '/thumb1.png',
  38. printer_name: 'Test Printer',
  39. print_time_seconds: 3600,
  40. },
  41. {
  42. id: 2,
  43. printer_id: 1,
  44. archive_id: 2,
  45. position: 2,
  46. status: 'printing',
  47. scheduled_time: null,
  48. require_previous_success: false,
  49. auto_off_after: true,
  50. manual_start: false,
  51. ams_mapping: null,
  52. plate_id: null,
  53. bed_levelling: 'on',
  54. flow_cali: 'off',
  55. vibration_cali: true,
  56. layer_inspect: false,
  57. timelapse: false,
  58. use_ams: true,
  59. started_at: '2024-01-01T10:00:00Z',
  60. completed_at: null,
  61. error_message: null,
  62. created_at: '2024-01-01T00:00:00Z',
  63. archive_name: 'Active Print',
  64. archive_thumbnail: '/thumb2.png',
  65. printer_name: 'Test Printer',
  66. print_time_seconds: 7200,
  67. },
  68. {
  69. id: 3,
  70. printer_id: 1,
  71. archive_id: 3,
  72. position: 3,
  73. status: 'completed',
  74. scheduled_time: null,
  75. require_previous_success: false,
  76. auto_off_after: false,
  77. manual_start: false,
  78. ams_mapping: null,
  79. plate_id: null,
  80. bed_levelling: 'on',
  81. flow_cali: 'off',
  82. vibration_cali: true,
  83. layer_inspect: false,
  84. timelapse: false,
  85. use_ams: true,
  86. started_at: '2024-01-01T08:00:00Z',
  87. completed_at: '2024-01-01T09:00:00Z',
  88. error_message: null,
  89. created_at: '2024-01-01T00:00:00Z',
  90. archive_name: 'Completed Print',
  91. archive_thumbnail: '/thumb3.png',
  92. printer_name: 'Test Printer',
  93. print_time_seconds: 1800,
  94. },
  95. ];
  96. const mockPrinters = [
  97. {
  98. id: 1,
  99. name: 'Test Printer',
  100. ip_address: '192.168.1.100',
  101. serial_number: 'TESTSERIAL0001',
  102. access_code: '12345678',
  103. model: 'X1C',
  104. enabled: true,
  105. created_at: '2024-01-01T00:00:00Z',
  106. },
  107. ];
  108. describe('QueuePage', () => {
  109. beforeEach(() => {
  110. // Mock localStorage.getItem to return expected defaults for queue page
  111. vi.mocked(localStorage.getItem).mockImplementation((key: string) => {
  112. if (key === 'queue.historyCollapsed') return 'false'; // expanded
  113. if (key === 'queue.viewMode') return 'list';
  114. return null;
  115. });
  116. // Setup MSW handlers for this test
  117. server.use(
  118. http.get('/api/v1/queue/', () => {
  119. return HttpResponse.json(mockQueueItems);
  120. }),
  121. http.get('/api/v1/printers/', () => {
  122. return HttpResponse.json(mockPrinters);
  123. }),
  124. http.delete('/api/v1/queue/:id', () => {
  125. return HttpResponse.json({ success: true });
  126. }),
  127. http.post('/api/v1/queue/:id/cancel', () => {
  128. return HttpResponse.json({ success: true });
  129. }),
  130. http.post('/api/v1/queue/:id/start', () => {
  131. return HttpResponse.json({ success: true });
  132. }),
  133. http.post('/api/v1/queue/:id/stop', () => {
  134. return HttpResponse.json({ success: true });
  135. }),
  136. http.post('/api/v1/queue/reorder', () => {
  137. return HttpResponse.json({ success: true });
  138. })
  139. );
  140. });
  141. describe('rendering', () => {
  142. it('renders the page title', async () => {
  143. render(<QueuePage />);
  144. await waitFor(() => {
  145. expect(screen.getByText('Print Queue')).toBeInTheDocument();
  146. });
  147. });
  148. it('renders the page description', async () => {
  149. render(<QueuePage />);
  150. await waitFor(() => {
  151. expect(screen.getByText('Schedule and manage your print jobs')).toBeInTheDocument();
  152. });
  153. });
  154. it('shows summary cards', async () => {
  155. render(<QueuePage />);
  156. await waitFor(() => {
  157. // Check for the page title (Print Queue is the h1)
  158. expect(screen.getByText('Print Queue')).toBeInTheDocument();
  159. });
  160. });
  161. it('shows filter dropdowns', async () => {
  162. render(<QueuePage />);
  163. await waitFor(() => {
  164. expect(screen.getByText('All Printers')).toBeInTheDocument();
  165. expect(screen.getByText('All Status')).toBeInTheDocument();
  166. });
  167. });
  168. });
  169. describe('queue items display', () => {
  170. it('shows pending queue items', async () => {
  171. render(<QueuePage />);
  172. await waitFor(() => {
  173. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  174. });
  175. });
  176. it('shows active printing items', async () => {
  177. render(<QueuePage />);
  178. await waitFor(() => {
  179. expect(screen.getByText('Active Print')).toBeInTheDocument();
  180. expect(screen.getByText('Currently Printing')).toBeInTheDocument();
  181. });
  182. });
  183. it('shows the queue override colour instead of the original 3MF colour (#3132)', async () => {
  184. const item = {
  185. ...mockQueueItems[0],
  186. id: 82,
  187. printer_id: null,
  188. target_model: 'P2S',
  189. archive_id: null,
  190. library_file_id: 13,
  191. archive_name: null,
  192. library_file_name: 'Override colour test',
  193. plate_id: 1,
  194. filament_used_grams: 23.66,
  195. filament_type: 'PLA',
  196. filament_color: '#7C4B00',
  197. filament_overrides: [
  198. {
  199. slot_id: 1,
  200. type: 'PLA',
  201. color: '#C2BAA7FF',
  202. color_name: 'Bone White',
  203. force_color_match: false,
  204. },
  205. ],
  206. };
  207. server.use(
  208. http.get('/api/v1/queue/', () => HttpResponse.json([item])),
  209. http.get('/api/v1/library/files/13/plates', () =>
  210. HttpResponse.json({
  211. file_id: 13,
  212. filename: 'override-colour.3mf',
  213. plates: [
  214. {
  215. index: 1,
  216. name: 'Plate 1',
  217. objects: ['Part'],
  218. has_thumbnail: false,
  219. thumbnail_url: null,
  220. print_time_seconds: 3095,
  221. filament_used_grams: 23.66,
  222. filaments: [
  223. {
  224. slot_id: 1,
  225. type: 'PLA',
  226. color: '#7C4B00',
  227. used_grams: 23.7,
  228. used_meters: 7.93,
  229. },
  230. ],
  231. },
  232. ],
  233. is_multi_plate: false,
  234. }),
  235. ),
  236. );
  237. render(<QueuePage />);
  238. const colourName = await screen.findByText('Bone White');
  239. const row = colourName.closest('.group');
  240. expect(row).not.toBeNull();
  241. expect(within(row as HTMLElement).getByTestId('filament-swatch')).toHaveAttribute(
  242. 'title',
  243. '#C2BAA7',
  244. );
  245. });
  246. it('shows the resolved AMS slot and bound spool when a physical mapping is stored (#3132)', async () => {
  247. const item = {
  248. ...mockQueueItems[0],
  249. id: 85,
  250. printer_id: 1,
  251. printer_name: 'Test Printer',
  252. archive_id: null,
  253. library_file_id: 16,
  254. archive_name: null,
  255. library_file_name: 'Resolved AMS mapping test',
  256. plate_id: 1,
  257. ams_mapping: [1],
  258. filament_overrides: [
  259. {
  260. slot_id: 1,
  261. type: 'PLA',
  262. color: '#8E351BFF',
  263. color_name: 'Caramel',
  264. force_color_match: false,
  265. },
  266. ],
  267. };
  268. server.use(
  269. http.get('/api/v1/queue/', () => HttpResponse.json([item])),
  270. http.get('/api/v1/library/files/16/plates', () =>
  271. HttpResponse.json({
  272. file_id: 16,
  273. filename: 'resolved-mapping.3mf',
  274. plates: [
  275. {
  276. index: 1,
  277. name: 'Plate 1',
  278. objects: ['Part'],
  279. has_thumbnail: false,
  280. thumbnail_url: null,
  281. print_time_seconds: 3600,
  282. filament_used_grams: 20,
  283. filaments: [
  284. {
  285. slot_id: 1,
  286. type: 'PLA',
  287. color: '#7C4B00',
  288. used_grams: 20,
  289. used_meters: 6.7,
  290. },
  291. ],
  292. },
  293. ],
  294. is_multi_plate: false,
  295. }),
  296. ),
  297. http.get('/api/v1/printers/:id/status', () =>
  298. HttpResponse.json({
  299. id: 1,
  300. name: 'Test Printer',
  301. connected: true,
  302. state: 'IDLE',
  303. ams: [
  304. {
  305. id: 0,
  306. // Regular AMS units expose four tray records. Keeping all four
  307. // here matters because buildLoadedFilaments distinguishes a
  308. // regular AMS from a single-slot AMS-HT by tray count.
  309. tray: [
  310. { id: 0, tray_type: null },
  311. {
  312. id: 1,
  313. tray_type: 'PLA',
  314. tray_color: 'C2BAA7FF',
  315. tray_sub_brands: 'PLA Basic',
  316. tray_info_idx: 'GFA00',
  317. remain: 80,
  318. },
  319. { id: 2, tray_type: null },
  320. { id: 3, tray_type: null },
  321. ],
  322. },
  323. ],
  324. vt_tray: [],
  325. nozzles: [],
  326. ams_extruder_map: {},
  327. }),
  328. ),
  329. http.get('/api/v1/printers/:id/inventory-remain', () =>
  330. HttpResponse.json({
  331. inventory_remain_g: { '1': 800 },
  332. slot_materials: [
  333. {
  334. ams_id: 0,
  335. tray_id: 1,
  336. global_tray_id: 1,
  337. material_key: 'PLA|basic|bone-white',
  338. remaining_g: 800,
  339. extruder: 0,
  340. spool: {
  341. brand: 'eSUN',
  342. material: 'PLA',
  343. subtype: 'Basic',
  344. color_name: 'Bone White',
  345. rgba: 'C2BAA7FF',
  346. },
  347. },
  348. ],
  349. }),
  350. ),
  351. );
  352. render(<QueuePage />);
  353. const name = await screen.findByText('Resolved AMS mapping test');
  354. const row = name.closest('.group');
  355. expect(row).not.toBeNull();
  356. await waitFor(() => {
  357. expect(
  358. within(row as HTMLElement).getByText('A2 · eSUN PLA Basic · Bone White'),
  359. ).toBeInTheDocument();
  360. });
  361. expect(within(row as HTMLElement).queryByText('Caramel')).not.toBeInTheDocument();
  362. });
  363. it('resolves a mapped tray on a second regular AMS unit (#3132)', async () => {
  364. const item = {
  365. ...mockQueueItems[0],
  366. id: 86,
  367. printer_id: 1,
  368. printer_name: 'Test Printer',
  369. archive_id: null,
  370. library_file_id: 17,
  371. archive_name: null,
  372. library_file_name: 'Second AMS mapping test',
  373. plate_id: 1,
  374. ams_mapping: [5],
  375. filament_overrides: null,
  376. };
  377. server.use(
  378. http.get('/api/v1/queue/', () => HttpResponse.json([item])),
  379. http.get('/api/v1/library/files/17/plates', () =>
  380. HttpResponse.json({
  381. file_id: 17,
  382. filename: 'second-ams.3mf',
  383. plates: [
  384. {
  385. index: 1,
  386. name: 'Plate 1',
  387. objects: ['Part'],
  388. has_thumbnail: false,
  389. thumbnail_url: null,
  390. print_time_seconds: 3600,
  391. filament_used_grams: 20,
  392. filaments: [
  393. {
  394. slot_id: 1,
  395. type: 'PLA',
  396. color: '#111111',
  397. used_grams: 20,
  398. used_meters: 6.7,
  399. },
  400. ],
  401. },
  402. ],
  403. is_multi_plate: false,
  404. }),
  405. ),
  406. http.get('/api/v1/printers/:id/status', () =>
  407. HttpResponse.json({
  408. id: 1,
  409. name: 'Test Printer',
  410. connected: true,
  411. state: 'IDLE',
  412. ams: [
  413. {
  414. id: 1,
  415. tray: [
  416. { id: 0, tray_type: null },
  417. {
  418. id: 1,
  419. tray_type: 'PLA',
  420. tray_color: 'C2BAA7FF',
  421. tray_sub_brands: 'PLA Basic',
  422. tray_info_idx: 'GFA00',
  423. remain: 80,
  424. },
  425. { id: 2, tray_type: null },
  426. { id: 3, tray_type: null },
  427. ],
  428. },
  429. ],
  430. vt_tray: [],
  431. nozzles: [],
  432. ams_extruder_map: {},
  433. }),
  434. ),
  435. http.get('/api/v1/printers/:id/inventory-remain', () =>
  436. HttpResponse.json({
  437. inventory_remain_g: { '5': 800 },
  438. slot_materials: [
  439. {
  440. ams_id: 1,
  441. tray_id: 1,
  442. global_tray_id: 5,
  443. material_key: 'PLA|basic|bone-white',
  444. remaining_g: 800,
  445. extruder: 0,
  446. spool: {
  447. brand: 'eSUN',
  448. material: 'PLA',
  449. subtype: 'Basic',
  450. color_name: 'Bone White',
  451. rgba: 'C2BAA7FF',
  452. },
  453. },
  454. ],
  455. }),
  456. ),
  457. );
  458. render(<QueuePage />);
  459. const name = await screen.findByText('Second AMS mapping test');
  460. const row = name.closest('.group');
  461. expect(row).not.toBeNull();
  462. await waitFor(() => {
  463. expect(
  464. within(row as HTMLElement).getByText('B2 · eSUN PLA Basic · Bone White'),
  465. ).toBeInTheDocument();
  466. });
  467. });
  468. it('falls back to the selected plate 3MF colour when there is no override (#3132)', async () => {
  469. const item = {
  470. ...mockQueueItems[0],
  471. id: 83,
  472. printer_id: null,
  473. target_model: 'P2S',
  474. archive_id: null,
  475. library_file_id: 14,
  476. archive_name: null,
  477. library_file_name: 'Original colour test',
  478. plate_id: 1,
  479. filament_color: '#FFFFFF',
  480. filament_overrides: null,
  481. };
  482. server.use(
  483. http.get('/api/v1/queue/', () => HttpResponse.json([item])),
  484. http.get('/api/v1/library/files/14/plates', () =>
  485. HttpResponse.json({
  486. file_id: 14,
  487. filename: 'original-colour.3mf',
  488. plates: [
  489. {
  490. index: 1,
  491. name: 'Plate 1',
  492. objects: ['Part'],
  493. has_thumbnail: false,
  494. thumbnail_url: null,
  495. print_time_seconds: 3600,
  496. filament_used_grams: 20,
  497. filaments: [
  498. {
  499. slot_id: 1,
  500. type: 'PLA',
  501. color: '#7C4B00',
  502. used_grams: 20,
  503. used_meters: 6.7,
  504. },
  505. ],
  506. },
  507. ],
  508. is_multi_plate: false,
  509. }),
  510. ),
  511. );
  512. render(<QueuePage />);
  513. await screen.findByText('Original colour test');
  514. const row = screen.getByText('Original colour test').closest('.group');
  515. expect(row).not.toBeNull();
  516. await waitFor(() => {
  517. expect(within(row as HTMLElement).getByTestId('filament-swatch')).toHaveAttribute(
  518. 'title',
  519. '#7C4B00',
  520. );
  521. });
  522. });
  523. it('shows all used plate colours and only overrides the matching slot (#3132)', async () => {
  524. const item = {
  525. ...mockQueueItems[0],
  526. id: 84,
  527. printer_id: null,
  528. target_model: 'P2S',
  529. archive_id: null,
  530. library_file_id: 15,
  531. archive_name: null,
  532. library_file_name: 'Multi colour test',
  533. plate_id: 1,
  534. filament_overrides: [
  535. {
  536. slot_id: 2,
  537. type: 'PLA',
  538. color: '#C2BAA7FF',
  539. color_name: 'Bone White',
  540. force_color_match: false,
  541. },
  542. ],
  543. };
  544. server.use(
  545. http.get('/api/v1/queue/', () => HttpResponse.json([item])),
  546. http.get('/api/v1/library/files/15/plates', () =>
  547. HttpResponse.json({
  548. file_id: 15,
  549. filename: 'multi-colour.3mf',
  550. plates: [
  551. {
  552. index: 1,
  553. name: 'Plate 1',
  554. objects: ['Part'],
  555. has_thumbnail: false,
  556. thumbnail_url: null,
  557. print_time_seconds: 3600,
  558. filament_used_grams: 30,
  559. filaments: [
  560. {
  561. slot_id: 1,
  562. type: 'PLA',
  563. color: '#000000',
  564. used_grams: 20,
  565. used_meters: 6.7,
  566. },
  567. {
  568. slot_id: 2,
  569. type: 'PLA',
  570. color: '#7C4B00',
  571. used_grams: 10,
  572. used_meters: 3.35,
  573. },
  574. ],
  575. },
  576. ],
  577. is_multi_plate: false,
  578. }),
  579. ),
  580. );
  581. render(<QueuePage />);
  582. const overrideName = await screen.findByText('Bone White');
  583. const row = overrideName.closest('.group');
  584. expect(row).not.toBeNull();
  585. await waitFor(() => {
  586. const swatches = within(row as HTMLElement).getAllByTestId('filament-swatch');
  587. expect(swatches).toHaveLength(2);
  588. expect(swatches.map((swatch) => swatch.getAttribute('title'))).toEqual([
  589. '#000000',
  590. '#C2BAA7',
  591. ]);
  592. });
  593. });
  594. it('keeps an eight-colour job compact and exposes details in the tooltip (#3132)', async () => {
  595. const colours = [
  596. '#FF0000',
  597. '#00FF00',
  598. '#0000FF',
  599. '#FFFF00',
  600. '#FF00FF',
  601. '#00FFFF',
  602. '#FFFFFF',
  603. '#000000',
  604. ];
  605. const item = {
  606. ...mockQueueItems[0],
  607. id: 87,
  608. printer_id: null,
  609. target_model: 'P2S',
  610. archive_id: null,
  611. library_file_id: 18,
  612. archive_name: null,
  613. library_file_name: 'Eight colour test',
  614. plate_id: 1,
  615. filament_overrides: colours.map((color, index) => ({
  616. slot_id: index + 1,
  617. type: 'PLA',
  618. color,
  619. color_name: `Colour ${index + 1}`,
  620. force_color_match: false,
  621. })),
  622. };
  623. server.use(
  624. http.get('/api/v1/queue/', () => HttpResponse.json([item])),
  625. http.get('/api/v1/library/files/18/plates', () =>
  626. HttpResponse.json({
  627. file_id: 18,
  628. filename: 'eight-colour.3mf',
  629. plates: [
  630. {
  631. index: 1,
  632. name: 'Plate 1',
  633. objects: ['Part'],
  634. has_thumbnail: false,
  635. thumbnail_url: null,
  636. print_time_seconds: 3600,
  637. filament_used_grams: 40,
  638. filaments: colours.map((color, index) => ({
  639. slot_id: index + 1,
  640. type: 'PLA',
  641. color,
  642. used_grams: 5,
  643. used_meters: 1.7,
  644. })),
  645. },
  646. ],
  647. is_multi_plate: false,
  648. }),
  649. ),
  650. );
  651. render(<QueuePage />);
  652. const name = await screen.findByText('Eight colour test');
  653. const row = name.closest('.group');
  654. expect(row).not.toBeNull();
  655. await waitFor(() => {
  656. const compact = within(row as HTMLElement).getByTestId('queue-filament-compact');
  657. expect(compact).toHaveAttribute(
  658. 'title',
  659. colours.map((_, index) => `Colour ${index + 1}`).join('\n'),
  660. );
  661. expect(within(compact).getAllByTestId('filament-swatch')).toHaveLength(8);
  662. });
  663. // Touch screens never show the tooltip; the same lines are the group's
  664. // accessible name.
  665. expect(screen.getByRole('img', { name: /^Colour 1, Colour 2, .*Colour 8$/ })).toBe(
  666. within(row as HTMLElement).getByTestId('queue-filament-compact'),
  667. );
  668. // Compact mode keeps the long labels out of the metadata row itself.
  669. expect(within(row as HTMLElement).queryByText('Colour 1')).not.toBeInTheDocument();
  670. expect(within(row as HTMLElement).queryByText('Colour 8')).not.toBeInTheDocument();
  671. });
  672. describe('stored mapping on the live printer (#3132)', () => {
  673. const mappedItem = (amsMapping: number[]) => ({
  674. ...mockQueueItems[0],
  675. id: 88,
  676. printer_id: 1,
  677. printer_name: 'Test Printer',
  678. archive_id: null,
  679. library_file_id: 19,
  680. archive_name: null,
  681. library_file_name: 'Mapped slot test',
  682. plate_id: 1,
  683. ams_mapping: amsMapping,
  684. filament_overrides: [
  685. {
  686. slot_id: 1,
  687. type: 'PLA',
  688. color: '#8E351BFF',
  689. color_name: 'Caramel',
  690. force_color_match: false,
  691. },
  692. ],
  693. });
  694. const useMappedPrinter = (amsMapping: number[], status: Record<string, unknown>) => {
  695. server.use(
  696. http.get('/api/v1/queue/', () => HttpResponse.json([mappedItem(amsMapping)])),
  697. http.get('/api/v1/library/files/19/plates', () =>
  698. HttpResponse.json({
  699. file_id: 19,
  700. filename: 'mapped-slot.3mf',
  701. plates: [
  702. {
  703. index: 1,
  704. name: 'Plate 1',
  705. objects: ['Part'],
  706. has_thumbnail: false,
  707. thumbnail_url: null,
  708. print_time_seconds: 3600,
  709. filament_used_grams: 20,
  710. filaments: [
  711. { slot_id: 1, type: 'PLA', color: '#7C4B00', used_grams: 20, used_meters: 6.7 },
  712. ],
  713. },
  714. ],
  715. is_multi_plate: false,
  716. }),
  717. ),
  718. http.get('/api/v1/printers/:id/status', () =>
  719. HttpResponse.json({
  720. id: 1,
  721. name: 'Test Printer',
  722. state: 'IDLE',
  723. nozzles: [],
  724. ams_extruder_map: {},
  725. ...status,
  726. }),
  727. ),
  728. http.get('/api/v1/printers/:id/inventory-remain', () =>
  729. HttpResponse.json({ inventory_remain_g: {}, slot_materials: [] }),
  730. ),
  731. );
  732. };
  733. // Regular AMS units expose four tray records; slot A3 (global 2) is empty.
  734. const amsWithEmptyA3 = [
  735. {
  736. id: 0,
  737. tray: [
  738. { id: 0, tray_type: 'PLA', tray_color: 'FFFFFFFF', remain: 50 },
  739. { id: 1, tray_type: 'PLA', tray_color: 'C2BAA7FF', remain: 80 },
  740. // Firmware confirms A3 has no spool (tray_exist_bits bit clear).
  741. { id: 2, tray_type: null, exists: false, state: 9 },
  742. { id: 3, tray_type: null },
  743. ],
  744. },
  745. ];
  746. const mappedRow = async () => {
  747. render(<QueuePage />);
  748. const name = await screen.findByText('Mapped slot test');
  749. const row = name.closest('.group');
  750. expect(row).not.toBeNull();
  751. return row as HTMLElement;
  752. };
  753. it('resolves a mapping to the external spool', async () => {
  754. useMappedPrinter([254], {
  755. connected: true,
  756. ams: [],
  757. vt_tray: [{ id: 254, tray_type: 'PETG', tray_color: 'C2BAA7FF', remain: 60 }],
  758. });
  759. const row = await mappedRow();
  760. await waitFor(() => {
  761. expect(within(row).getByText(/^External · PETG · /)).toBeInTheDocument();
  762. });
  763. expect(within(row).queryByText('Caramel')).not.toBeInTheDocument();
  764. });
  765. it('says the mapped slot is empty, keeping the intended colour', async () => {
  766. useMappedPrinter([2], { connected: true, ams: amsWithEmptyA3, vt_tray: [] });
  767. const row = await mappedRow();
  768. await waitFor(() => {
  769. expect(within(row).getByText('A3 · Empty · Caramel')).toBeInTheDocument();
  770. });
  771. expect(within(row).getByTestId('filament-swatch')).toHaveAttribute('title', '#8E351B');
  772. });
  773. it('never calls the external spool empty, having no presence signal', async () => {
  774. // Shape the status route really sends: an external tray carries no
  775. // state / exists, so an unconfigured spool and no spool look the same.
  776. let statusServed = false;
  777. useMappedPrinter([254], { connected: true, ams: [], vt_tray: [] });
  778. server.use(
  779. http.get('/api/v1/printers/:id/status', () => {
  780. statusServed = true;
  781. return HttpResponse.json({ id: 1, name: 'Test Printer', state: 'IDLE', connected: true, ams: [], vt_tray: [{ id: 254, tray_type: '' }], nozzles: [], ams_extruder_map: {} });
  782. }),
  783. );
  784. const row = await mappedRow();
  785. await waitFor(() => expect(statusServed).toBe(true));
  786. expect(await within(row).findByText('Caramel')).toBeInTheDocument();
  787. expect(within(row).queryByText(/Empty/)).not.toBeInTheDocument();
  788. });
  789. it('does not call a loaded but unconfigured spool empty (#2527)', async () => {
  790. // A non-RFID spool the firmware can't identify has no tray_type but is
  791. // physically present; the Printers page draws it as "?", not "Empty".
  792. const ams = [
  793. {
  794. id: 0,
  795. tray: [
  796. { id: 0, tray_type: 'PLA', tray_color: 'FFFFFFFF', remain: 50 },
  797. { id: 1, tray_type: 'PLA', tray_color: 'C2BAA7FF', remain: 80 },
  798. { id: 2, tray_type: '', exists: true, state: 3 },
  799. { id: 3, tray_type: null },
  800. ],
  801. },
  802. ];
  803. let statusServed = false;
  804. useMappedPrinter([2], { connected: true, ams, vt_tray: [] });
  805. server.use(
  806. http.get('/api/v1/printers/:id/status', () => {
  807. statusServed = true;
  808. return HttpResponse.json({ id: 1, name: 'Test Printer', state: 'IDLE', connected: true, ams, vt_tray: [], nozzles: [], ams_extruder_map: {} });
  809. }),
  810. );
  811. const row = await mappedRow();
  812. await waitFor(() => expect(statusServed).toBe(true));
  813. expect(await within(row).findByText('Caramel')).toBeInTheDocument();
  814. expect(within(row).queryByText(/Empty/)).not.toBeInTheDocument();
  815. });
  816. it('does not call a slot empty while the printer is offline', async () => {
  817. useMappedPrinter([2], { connected: false, ams: amsWithEmptyA3, vt_tray: [] });
  818. const row = await mappedRow();
  819. expect(await within(row).findByText('Caramel')).toBeInTheDocument();
  820. expect(within(row).queryByText(/Empty/)).not.toBeInTheDocument();
  821. });
  822. });
  823. it('shows one if-started-now ETA for an eligible pending item', async () => {
  824. // Printer 1 is free: nothing is printing on it and nothing is queued ahead.
  825. server.use(
  826. http.get('/api/v1/queue/', () => {
  827. return HttpResponse.json([mockQueueItems[0]]);
  828. }),
  829. );
  830. render(<QueuePage />);
  831. const name = await screen.findByText('Test Print 1');
  832. const row = name.closest('.group');
  833. expect(row).not.toBeNull();
  834. const etaEl = within(row as HTMLElement).getAllByTestId('queue-item-eta');
  835. expect(etaEl).toHaveLength(1);
  836. // The tooltip must say what the number actually means, not reuse the
  837. // printers-page "Estimated completion time" wording (which this is not).
  838. expect(etaEl[0]).toHaveAttribute(
  839. 'title',
  840. 'Completion time if this job started now',
  841. );
  842. });
  843. // The scheduler only writes waiting_reason on the model-based assignment
  844. // path, so an item pinned to a specific printer carries no marker at all
  845. // while it sits behind a running job. Without the printer-busy check every
  846. // one of these quoted the same wrong "starts now" time.
  847. it('does not show an ETA for an item pinned behind a running print', async () => {
  848. render(<QueuePage />);
  849. // mockQueueItems[1] ("Active Print") is printing on printer 1, and
  850. // "Test Print 1" is pending on the same printer with waiting_reason null.
  851. const name = await screen.findByText('Test Print 1');
  852. const row = name.closest('.group');
  853. expect(row).not.toBeNull();
  854. expect(
  855. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  856. ).not.toBeInTheDocument();
  857. });
  858. it('shows the ETA only on the next item up when several share an idle printer', async () => {
  859. server.use(
  860. http.get('/api/v1/queue/', () => {
  861. return HttpResponse.json([
  862. { ...mockQueueItems[0], id: 10, position: 1, archive_name: 'First up' },
  863. { ...mockQueueItems[0], id: 11, position: 2, archive_name: 'Second up' },
  864. { ...mockQueueItems[0], id: 12, position: 3, archive_name: 'Third up' },
  865. ]);
  866. }),
  867. );
  868. render(<QueuePage />);
  869. await screen.findByText('Third up');
  870. const etaRowNames = screen
  871. .queryAllByTestId('queue-item-eta')
  872. .map((el) => el.closest('.group')?.textContent);
  873. expect(etaRowNames).toHaveLength(1);
  874. expect(etaRowNames[0]).toContain('First up');
  875. });
  876. it('shows an ETA for a staged item queued behind others on an idle printer', async () => {
  877. // The scheduler skips manual-start items without claiming the printer, so
  878. // a staged job is startable whenever its printer is free — queue order
  879. // does not gate it.
  880. server.use(
  881. http.get('/api/v1/queue/', () => {
  882. return HttpResponse.json([
  883. { ...mockQueueItems[0], id: 20, position: 1, archive_name: 'Auto first' },
  884. {
  885. ...mockQueueItems[0],
  886. id: 21,
  887. position: 2,
  888. archive_name: 'Staged second',
  889. manual_start: true,
  890. },
  891. ]);
  892. }),
  893. );
  894. render(<QueuePage />);
  895. const name = await screen.findByText('Staged second');
  896. const row = name.closest('.group');
  897. expect(row).not.toBeNull();
  898. expect(
  899. within(row as HTMLElement).getAllByTestId('queue-item-eta'),
  900. ).toHaveLength(1);
  901. });
  902. it('does not show an ETA for an item conditional on a previous print', async () => {
  903. server.use(
  904. http.get('/api/v1/queue/', () => {
  905. return HttpResponse.json([
  906. {
  907. ...mockQueueItems[0],
  908. archive_name: 'Conditional Print',
  909. require_previous_success: true,
  910. },
  911. ]);
  912. }),
  913. );
  914. render(<QueuePage />);
  915. const name = await screen.findByText('Conditional Print');
  916. const row = name.closest('.group');
  917. expect(row).not.toBeNull();
  918. expect(
  919. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  920. ).not.toBeInTheDocument();
  921. });
  922. it('advances the ETA as time passes', async () => {
  923. vi.useFakeTimers({ shouldAdvanceTime: true });
  924. vi.setSystemTime(new Date('2026-08-02T10:00:00Z'));
  925. try {
  926. server.use(
  927. http.get('/api/v1/queue/', () => {
  928. return HttpResponse.json([mockQueueItems[0]]);
  929. }),
  930. );
  931. render(<QueuePage />);
  932. const name = await screen.findByText('Test Print 1');
  933. const row = name.closest('.group') as HTMLElement;
  934. const before = within(row).getByTestId('queue-item-eta').textContent;
  935. // The queue payload never changes, so react-query hands back the same
  936. // object and nothing here re-renders on its own. Only the page's own
  937. // clock can move this value.
  938. await vi.advanceTimersByTimeAsync(45 * 60 * 1000);
  939. await waitFor(() => {
  940. expect(
  941. within(row).getByTestId('queue-item-eta').textContent,
  942. ).not.toBe(before);
  943. });
  944. } finally {
  945. vi.useRealTimers();
  946. }
  947. });
  948. it('shows one if-started-now ETA for a staged item', async () => {
  949. server.use(
  950. http.get('/api/v1/queue/', () => {
  951. return HttpResponse.json([
  952. {
  953. ...mockQueueItems[0],
  954. archive_name: 'Staged Print',
  955. manual_start: true,
  956. },
  957. ]);
  958. }),
  959. );
  960. render(<QueuePage />);
  961. const name = await screen.findByText('Staged Print');
  962. const row = name.closest('.group');
  963. expect(row).not.toBeNull();
  964. expect(
  965. within(row as HTMLElement).getAllByTestId('queue-item-eta'),
  966. ).toHaveLength(1);
  967. });
  968. it('shows exactly one live ETA for a printing item', async () => {
  969. server.use(
  970. http.get('/api/v1/printers/:id/status', ({ params }) => {
  971. return HttpResponse.json({
  972. id: Number(params.id),
  973. name: 'Test Printer',
  974. connected: true,
  975. state: 'RUNNING',
  976. progress: 50,
  977. remaining_time: 60,
  978. layer_num: 50,
  979. total_layers: 100,
  980. filename: 'active.3mf',
  981. });
  982. }),
  983. );
  984. render(<QueuePage />);
  985. const name = await screen.findByText('Active Print');
  986. const row = name.closest('.group');
  987. expect(row).not.toBeNull();
  988. await waitFor(() => {
  989. expect(
  990. within(row as HTMLElement).getAllByText(/^ETA\s/),
  991. ).toHaveLength(1);
  992. });
  993. expect(
  994. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  995. ).not.toBeInTheDocument();
  996. });
  997. it('does not show an ETA for a waiting item', async () => {
  998. server.use(
  999. http.get('/api/v1/queue/', () => {
  1000. return HttpResponse.json([
  1001. {
  1002. ...mockQueueItems[0],
  1003. archive_name: 'Waiting Print',
  1004. waiting_reason: 'Waiting for matching printer',
  1005. },
  1006. ]);
  1007. }),
  1008. );
  1009. render(<QueuePage />);
  1010. const name = await screen.findByText('Waiting Print');
  1011. const row = name.closest('.group');
  1012. expect(row).not.toBeNull();
  1013. expect(
  1014. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  1015. ).not.toBeInTheDocument();
  1016. });
  1017. it('does not show an if-started-now ETA for a scheduled item', async () => {
  1018. server.use(
  1019. http.get('/api/v1/queue/', () => {
  1020. return HttpResponse.json([
  1021. {
  1022. ...mockQueueItems[0],
  1023. archive_name: 'Scheduled Print',
  1024. scheduled_time: new Date(
  1025. Date.now() + 5 * 60 * 60 * 1000,
  1026. ).toISOString(),
  1027. },
  1028. ]);
  1029. }),
  1030. );
  1031. render(<QueuePage />);
  1032. const name = await screen.findByText('Scheduled Print');
  1033. const row = name.closest('.group');
  1034. expect(row).not.toBeNull();
  1035. expect(
  1036. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  1037. ).not.toBeInTheDocument();
  1038. });
  1039. it('tells an unscheduled item when it runs, without naming a dispatch mode', async () => {
  1040. // ASAP and Queue differ only in where the item is inserted; neither is
  1041. // stored on it, so this column cannot tell them apart. Labelling every
  1042. // unscheduled item "ASAP" made a Queue choice look overridden, which is
  1043. // what both #2557 and #3018 opened on.
  1044. server.use(
  1045. http.get('/api/v1/queue/', () => {
  1046. return HttpResponse.json([
  1047. { ...mockQueueItems[0], archive_name: 'Queued Print', scheduled_time: null },
  1048. ]);
  1049. }),
  1050. );
  1051. render(<QueuePage />);
  1052. const name = await screen.findByText('Queued Print');
  1053. const row = name.closest('.group') as HTMLElement;
  1054. expect(row).not.toBeNull();
  1055. expect(within(row).getByText('When a printer is free')).toBeInTheDocument();
  1056. expect(within(row).queryByText('ASAP')).not.toBeInTheDocument();
  1057. });
  1058. it('does not render a dangling ETA for an invalid duration', async () => {
  1059. server.use(
  1060. http.get('/api/v1/queue/', () => {
  1061. return HttpResponse.json([
  1062. {
  1063. ...mockQueueItems[0],
  1064. archive_name: 'Invalid Duration Print',
  1065. print_time_seconds: -60,
  1066. },
  1067. ]);
  1068. }),
  1069. );
  1070. render(<QueuePage />);
  1071. const name = await screen.findByText('Invalid Duration Print');
  1072. const row = name.closest('.group');
  1073. expect(row).not.toBeNull();
  1074. expect(
  1075. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  1076. ).not.toBeInTheDocument();
  1077. expect(
  1078. within(row as HTMLElement).queryByText(/^ETA(?:\s|$)/),
  1079. ).not.toBeInTheDocument();
  1080. });
  1081. it('shows completed items in history', async () => {
  1082. const user = userEvent.setup();
  1083. render(<QueuePage />);
  1084. // The History tab now owns the completed/cancelled/failed list.
  1085. await user.click(await screen.findByRole('button', { name: /^History/ }));
  1086. await waitFor(() => {
  1087. expect(screen.getByText('Completed Print')).toBeInTheDocument();
  1088. });
  1089. });
  1090. it('shows status badges', async () => {
  1091. render(<QueuePage />);
  1092. await waitFor(() => {
  1093. // Queue items should be visible with status indicators
  1094. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  1095. });
  1096. });
  1097. it('shows printer names', async () => {
  1098. render(<QueuePage />);
  1099. await waitFor(() => {
  1100. const printerElements = screen.getAllByText('Test Printer');
  1101. expect(printerElements.length).toBeGreaterThan(0);
  1102. });
  1103. });
  1104. it('renders queue items with plate_id correctly', async () => {
  1105. // Override with queue items that have plate_id set
  1106. server.use(
  1107. http.get('/api/v1/queue/', () => {
  1108. return HttpResponse.json([
  1109. {
  1110. ...mockQueueItems[0],
  1111. plate_id: 2,
  1112. archive_name: 'Multi-plate Print',
  1113. },
  1114. ]);
  1115. })
  1116. );
  1117. render(<QueuePage />);
  1118. await waitFor(() => {
  1119. expect(screen.getByText('Multi-plate Print')).toBeInTheDocument();
  1120. });
  1121. });
  1122. });
  1123. describe('history pagination', () => {
  1124. // #2682: History rendered the full count in the header but only ever drew
  1125. // the first 50 rows, with no way to reach the rest. It now paginates with
  1126. // a "Show more" control.
  1127. const manyHistory = Array.from({ length: 60 }, (_, i) => ({
  1128. ...mockQueueItems[2],
  1129. id: 100 + i,
  1130. batch_id: null,
  1131. archive_name: `History Item ${String(i).padStart(2, '0')}`,
  1132. // Descending completed_at so index 0 is newest and sorts first; the
  1133. // default History sort is by date, newest first.
  1134. completed_at: new Date(Date.UTC(2024, 0, 1, 0, 0, 0) - i * 60000).toISOString(),
  1135. }));
  1136. beforeEach(() => {
  1137. server.use(
  1138. http.get('/api/v1/queue/', () => {
  1139. return HttpResponse.json(manyHistory);
  1140. })
  1141. );
  1142. });
  1143. it('caps the History list at one page and reveals the rest on Show more', async () => {
  1144. const user = userEvent.setup();
  1145. render(<QueuePage />);
  1146. await user.click(await screen.findByRole('button', { name: /^History/ }));
  1147. // First page is drawn; an item past the 50-row cap is not.
  1148. await waitFor(() => {
  1149. expect(screen.getByText('History Item 00')).toBeInTheDocument();
  1150. });
  1151. expect(screen.queryByText('History Item 59')).not.toBeInTheDocument();
  1152. expect(screen.getByText('Showing 50 of 60')).toBeInTheDocument();
  1153. // Show more reveals the remainder and then disappears (nothing left).
  1154. await user.click(screen.getByRole('button', { name: /show more/i }));
  1155. await waitFor(() => {
  1156. expect(screen.getByText('History Item 59')).toBeInTheDocument();
  1157. });
  1158. expect(screen.queryByRole('button', { name: /show more/i })).not.toBeInTheDocument();
  1159. });
  1160. });
  1161. describe('empty state', () => {
  1162. it('shows empty state when no queue items', async () => {
  1163. server.use(
  1164. http.get('/api/v1/queue/', () => {
  1165. return HttpResponse.json([]);
  1166. })
  1167. );
  1168. render(<QueuePage />);
  1169. await waitFor(() => {
  1170. expect(screen.getByText('No prints scheduled')).toBeInTheDocument();
  1171. });
  1172. });
  1173. });
  1174. describe('filtering', () => {
  1175. it('has printer filter options', async () => {
  1176. const user = userEvent.setup();
  1177. render(<QueuePage />);
  1178. await waitFor(() => {
  1179. expect(screen.getByText('All Printers')).toBeInTheDocument();
  1180. });
  1181. const printerSelect = screen.getByDisplayValue('All Printers');
  1182. await user.click(printerSelect);
  1183. expect(screen.getByText('Unassigned')).toBeInTheDocument();
  1184. });
  1185. it('has status filter options', async () => {
  1186. const user = userEvent.setup();
  1187. render(<QueuePage />);
  1188. await waitFor(() => {
  1189. expect(screen.getByText('All Status')).toBeInTheDocument();
  1190. });
  1191. const statusSelect = screen.getByDisplayValue('All Status');
  1192. await user.click(statusSelect);
  1193. expect(screen.getByRole('option', { name: 'Pending' })).toBeInTheDocument();
  1194. expect(screen.getByRole('option', { name: 'Printing' })).toBeInTheDocument();
  1195. expect(screen.getByRole('option', { name: 'Completed' })).toBeInTheDocument();
  1196. });
  1197. });
  1198. describe('queue actions', () => {
  1199. it('shows edit button for pending items', async () => {
  1200. render(<QueuePage />);
  1201. await waitFor(() => {
  1202. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  1203. });
  1204. // Find the edit button (Pencil icon)
  1205. const editButtons = screen.getAllByTitle('Edit');
  1206. expect(editButtons.length).toBeGreaterThan(0);
  1207. });
  1208. it('shows cancel button for pending items', async () => {
  1209. render(<QueuePage />);
  1210. await waitFor(() => {
  1211. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  1212. });
  1213. const cancelButtons = screen.getAllByTitle('Cancel');
  1214. expect(cancelButtons.length).toBeGreaterThan(0);
  1215. });
  1216. it('shows stop button for printing items', async () => {
  1217. render(<QueuePage />);
  1218. await waitFor(() => {
  1219. expect(screen.getByText('Active Print')).toBeInTheDocument();
  1220. });
  1221. const stopButtons = screen.getAllByTitle('Stop Print');
  1222. expect(stopButtons.length).toBeGreaterThan(0);
  1223. });
  1224. it('shows re-queue button for history items', async () => {
  1225. const user = userEvent.setup();
  1226. render(<QueuePage />);
  1227. await user.click(await screen.findByRole('button', { name: /^History/ }));
  1228. await waitFor(() => {
  1229. expect(screen.getByText('Completed Print')).toBeInTheDocument();
  1230. });
  1231. const requeueButtons = screen.getAllByTitle('Re-queue');
  1232. expect(requeueButtons.length).toBeGreaterThan(0);
  1233. });
  1234. });
  1235. describe('clear history', () => {
  1236. it('shows clear history button when history exists', async () => {
  1237. const user = userEvent.setup();
  1238. render(<QueuePage />);
  1239. // Clear History only renders inside the History tab now.
  1240. await user.click(await screen.findByRole('button', { name: /^History/ }));
  1241. await waitFor(() => {
  1242. expect(screen.getByText('Clear History')).toBeInTheDocument();
  1243. });
  1244. });
  1245. it('opens confirm modal when clicking clear history', async () => {
  1246. const user = userEvent.setup();
  1247. render(<QueuePage />);
  1248. await user.click(await screen.findByRole('button', { name: /^History/ }));
  1249. await waitFor(() => {
  1250. expect(screen.getByText('Clear History')).toBeInTheDocument();
  1251. });
  1252. const clearButton = screen.getByRole('button', { name: /clear history/i });
  1253. await user.click(clearButton);
  1254. await waitFor(() => {
  1255. expect(screen.getByText(/Are you sure you want to remove all/i)).toBeInTheDocument();
  1256. });
  1257. });
  1258. });
  1259. describe('staged items', () => {
  1260. it('shows staged badge for manual_start items', async () => {
  1261. server.use(
  1262. http.get('/api/v1/queue/', () => {
  1263. return HttpResponse.json([
  1264. {
  1265. ...mockQueueItems[0],
  1266. manual_start: true,
  1267. },
  1268. ]);
  1269. })
  1270. );
  1271. render(<QueuePage />);
  1272. await waitFor(() => {
  1273. expect(screen.getByText('Staged')).toBeInTheDocument();
  1274. });
  1275. });
  1276. it('shows start button for staged items', async () => {
  1277. server.use(
  1278. http.get('/api/v1/queue/', () => {
  1279. return HttpResponse.json([
  1280. {
  1281. ...mockQueueItems[0],
  1282. manual_start: true,
  1283. },
  1284. ]);
  1285. })
  1286. );
  1287. render(<QueuePage />);
  1288. await waitFor(() => {
  1289. expect(screen.getByTitle('Start Print')).toBeInTheDocument();
  1290. });
  1291. });
  1292. // Without queue:start_unreviewed their jobs wait for staff to start them (#1620)
  1293. describe('waiting for review', () => {
  1294. const signInWith = (permissions: string[]) => {
  1295. setAuthToken('test-token', 'session');
  1296. server.use(
  1297. http.get('*/api/v1/auth/status', () => HttpResponse.json({ auth_enabled: true, requires_setup: false })),
  1298. http.get('*/api/v1/auth/me', () =>
  1299. HttpResponse.json({ id: 7, username: 'student', is_admin: false, permissions }),
  1300. ),
  1301. http.get('/api/v1/queue/', () =>
  1302. HttpResponse.json([{ ...mockQueueItems[0], manual_start: true, created_by_id: 7 }]),
  1303. ),
  1304. );
  1305. };
  1306. afterEach(() => {
  1307. setAuthToken(null);
  1308. });
  1309. it('tells a student their job waits for review and offers no start', async () => {
  1310. signInWith(['queue:read_own', 'queue:update_own', 'queue:delete_own']);
  1311. render(<QueuePage />);
  1312. expect(await screen.findByText('Waiting for review')).toBeInTheDocument();
  1313. const start = screen.getByTitle('Waiting for review: someone who manages the queue starts this job');
  1314. expect(start).toBeDisabled();
  1315. });
  1316. it('keeps the start button for users who may print without review', async () => {
  1317. signInWith(['queue:read_own', 'queue:update_own', 'queue:start_unreviewed']);
  1318. render(<QueuePage />);
  1319. expect(await screen.findByText('Staged')).toBeInTheDocument();
  1320. expect(screen.getByTitle('Start Print')).not.toBeDisabled();
  1321. });
  1322. });
  1323. });
  1324. describe('auto power off badge', () => {
  1325. it('shows power off badge when auto_off_after is true', async () => {
  1326. render(<QueuePage />);
  1327. await waitFor(() => {
  1328. expect(screen.getByText('Auto power off')).toBeInTheDocument();
  1329. });
  1330. });
  1331. });
  1332. describe('gcode injection badge', () => {
  1333. it('shows G-code badge when gcode_injection is true', async () => {
  1334. const itemsWithGcode = mockQueueItems.map((item, i) =>
  1335. i === 0 ? { ...item, gcode_injection: true } : item
  1336. );
  1337. server.use(
  1338. http.get('/api/v1/queue/', () => HttpResponse.json(itemsWithGcode)),
  1339. );
  1340. render(<QueuePage />);
  1341. await waitFor(() => {
  1342. expect(screen.getByText('G-code')).toBeInTheDocument();
  1343. });
  1344. });
  1345. it('does not show G-code badge when gcode_injection is false', async () => {
  1346. render(<QueuePage />);
  1347. await waitFor(() => {
  1348. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  1349. });
  1350. expect(screen.queryByText('G-code')).not.toBeInTheDocument();
  1351. });
  1352. });
  1353. describe('outcome prompt badge (#1898)', () => {
  1354. it('shows the badge only on items with confirm_outcome set', async () => {
  1355. const items = mockQueueItems.map((item, i) =>
  1356. i === 0 ? { ...item, confirm_outcome: true } : { ...item, confirm_outcome: false }
  1357. );
  1358. server.use(http.get('/api/v1/queue/', () => HttpResponse.json(items)));
  1359. render(<QueuePage />);
  1360. await waitFor(() => expect(screen.getByText('Test Print 1')).toBeInTheDocument());
  1361. const badges = screen.getAllByText('Outcome prompt');
  1362. expect(badges).toHaveLength(1);
  1363. expect(badges[0].closest('span')).toHaveAttribute('title', 'Asks how the print came out after it completes');
  1364. });
  1365. it('renders nothing when no item asks for an outcome', async () => {
  1366. render(<QueuePage />);
  1367. await waitFor(() => expect(screen.getByText('Test Print 1')).toBeInTheDocument());
  1368. expect(screen.queryByText('Outcome prompt')).not.toBeInTheDocument();
  1369. });
  1370. });
  1371. describe('filament-short ▶ flow (#1496)', () => {
  1372. /**
  1373. * The dispatch pre-flight flags a queue item as filament_short. The user
  1374. * clicks ▶, the backend re-checks live and either dispatches (no deficit
  1375. * anymore — clear flag) or returns 409 with the per-slot deficit so the
  1376. * frontend can render the "Print Anyway" confirm modal.
  1377. */
  1378. const shortItem = {
  1379. ...mockQueueItems[0],
  1380. manual_start: true,
  1381. filament_short: true,
  1382. };
  1383. it('renders the filament-short badge on a flagged pending row', async () => {
  1384. server.use(
  1385. http.get('/api/v1/queue/', () => HttpResponse.json([shortItem])),
  1386. );
  1387. render(<QueuePage />);
  1388. await waitFor(() => {
  1389. expect(screen.getByText(/Insufficient filament for the assigned spool/i)).toBeInTheDocument();
  1390. });
  1391. });
  1392. it('opens the Print Anyway modal when ▶ returns 409 and retries with skip_filament_check', async () => {
  1393. let secondCallSkippedCheck: boolean | null = null;
  1394. let attempts = 0;
  1395. server.use(
  1396. http.get('/api/v1/queue/', () => HttpResponse.json([shortItem])),
  1397. http.post('/api/v1/queue/:id/start', ({ request }) => {
  1398. attempts += 1;
  1399. const url = new URL(request.url);
  1400. const skip = url.searchParams.get('skip_filament_check') === 'true';
  1401. if (attempts === 1) {
  1402. return HttpResponse.json(
  1403. {
  1404. detail: {
  1405. code: 'insufficient_filament',
  1406. deficit: [
  1407. {
  1408. slot_id: 1,
  1409. ams_id: 0,
  1410. tray_id: 0,
  1411. filament_type: 'PLA',
  1412. required_grams: 270,
  1413. remaining_grams: 200,
  1414. },
  1415. ],
  1416. },
  1417. },
  1418. { status: 409 },
  1419. );
  1420. }
  1421. secondCallSkippedCheck = skip;
  1422. return HttpResponse.json({ ...shortItem, manual_start: false, filament_short: false });
  1423. }),
  1424. );
  1425. render(<QueuePage />);
  1426. const playButton = await screen.findByTitle(/Start Print|do not have permission to start prints/i);
  1427. await userEvent.click(playButton);
  1428. // Wait for the start endpoint to be hit (the 409 path returns to onError).
  1429. await waitFor(() => expect(attempts).toBe(1));
  1430. // Modal shows the deficit detail
  1431. await screen.findByRole('button', { name: /Print Anyway/i });
  1432. expect(
  1433. screen.getByText(/Slot 1: needs 270 g, 200 g remaining/i),
  1434. ).toBeInTheDocument();
  1435. await userEvent.click(screen.getByRole('button', { name: /Print Anyway/i }));
  1436. await waitFor(() => expect(secondCallSkippedCheck).toBe(true));
  1437. expect(attempts).toBe(2);
  1438. });
  1439. });
  1440. // #2667: mobile can't drag-reorder the queue, so pending rows get up/down
  1441. // arrows. They persist via the same POST /queue/reorder as drag. The
  1442. // buttons live in the DOM at every width (Tailwind `sm:hidden` is CSS-only),
  1443. // so they're clickable in jsdom.
  1444. describe('mobile reorder arrows (#2667)', () => {
  1445. const threePending = [1, 2, 3].map((n) => ({
  1446. ...mockQueueItems[0],
  1447. id: n,
  1448. archive_id: n,
  1449. position: n,
  1450. status: 'pending',
  1451. archive_name: `Pending ${n}`,
  1452. }));
  1453. it('renders Move Up / Move Down controls for pending items', async () => {
  1454. server.use(http.get('/api/v1/queue/', () => HttpResponse.json(threePending)));
  1455. render(<QueuePage />);
  1456. await waitFor(() => expect(screen.getByText('Pending 1')).toBeInTheDocument());
  1457. // One pair per pending row.
  1458. expect(screen.getAllByTitle('Move Up')).toHaveLength(3);
  1459. expect(screen.getAllByTitle('Move Down')).toHaveLength(3);
  1460. });
  1461. it('moving the first item down persists the swapped order', async () => {
  1462. let reorderBody: { items: { id: number; position: number }[] } | null = null;
  1463. server.use(
  1464. http.get('/api/v1/queue/', () => HttpResponse.json(threePending)),
  1465. http.post('/api/v1/queue/reorder', async ({ request }) => {
  1466. reorderBody = (await request.json()) as typeof reorderBody;
  1467. return HttpResponse.json({ message: 'ok' });
  1468. }),
  1469. );
  1470. render(<QueuePage />);
  1471. await waitFor(() => expect(screen.getByText('Pending 1')).toBeInTheDocument());
  1472. // First row's "Move Down": item 1 drops below item 2 → [2, 1, 3].
  1473. await userEvent.click(screen.getAllByTitle('Move Down')[0]);
  1474. await waitFor(() => expect(reorderBody).not.toBeNull());
  1475. expect(reorderBody!.items).toEqual([
  1476. { id: 2, position: 1 },
  1477. { id: 1, position: 2 },
  1478. { id: 3, position: 3 },
  1479. ]);
  1480. });
  1481. it('disables Move Up on the first row and Move Down on the last', async () => {
  1482. server.use(http.get('/api/v1/queue/', () => HttpResponse.json(threePending)));
  1483. render(<QueuePage />);
  1484. await waitFor(() => expect(screen.getByText('Pending 1')).toBeInTheDocument());
  1485. // Rows render top-to-bottom in position order.
  1486. expect(screen.getAllByTitle('Move Up')[0]).toBeDisabled();
  1487. expect(screen.getAllByTitle('Move Down')[2]).toBeDisabled();
  1488. });
  1489. });
  1490. describe('bulk edit G-code injection (#3058)', () => {
  1491. /**
  1492. * gcode_injection is a per-item column the single-item edit modal has
  1493. * always exposed, and PATCH /queue/bulk has always accepted — only the
  1494. * bulk dialog left it out, so turning injection on for a whole queue
  1495. * meant opening every item.
  1496. */
  1497. const settingsWithSnippets = {
  1498. auto_archive: true,
  1499. gcode_snippets: '{"X1C":{"start_gcode":"","end_gcode":"M400"}}',
  1500. };
  1501. /** Select the one pending row and open the bulk edit dialog. */
  1502. const openBulkEdit = async () => {
  1503. render(<QueuePage />);
  1504. await waitFor(() => expect(screen.getByText('Test Print 1')).toBeInTheDocument());
  1505. await userEvent.click(screen.getByText('Select All'));
  1506. await userEvent.click(await screen.findByTitle('Edit Selected'));
  1507. return screen.getByText('Edit 1 Item').closest('div')!.parentElement!;
  1508. };
  1509. it('offers the toggle when a G-code snippet is configured', async () => {
  1510. server.use(http.get('/api/v1/settings/', () => HttpResponse.json(settingsWithSnippets)));
  1511. await openBulkEdit();
  1512. expect(await screen.findByText('Inject G-code')).toBeInTheDocument();
  1513. });
  1514. it('hides the toggle when no snippet is configured', async () => {
  1515. await openBulkEdit();
  1516. // The dialog is open — other queue options are there, injection is not.
  1517. expect(screen.getByText('Staged (manual start)')).toBeInTheDocument();
  1518. expect(screen.queryByText('Inject G-code')).not.toBeInTheDocument();
  1519. });
  1520. it('sends gcode_injection with the bulk PATCH once toggled on', async () => {
  1521. let patchBody: Record<string, unknown> | null = null;
  1522. server.use(
  1523. http.get('/api/v1/settings/', () => HttpResponse.json(settingsWithSnippets)),
  1524. http.patch('/api/v1/queue/bulk', async ({ request }) => {
  1525. patchBody = (await request.json()) as Record<string, unknown>;
  1526. return HttpResponse.json({ updated_count: 1, skipped_count: 0, message: 'Updated 1 items' });
  1527. }),
  1528. );
  1529. const dialog = await openBulkEdit();
  1530. const row = (await screen.findByText('Inject G-code')).closest('div')!;
  1531. await userEvent.click(within(row).getByText('On'));
  1532. await userEvent.click(within(dialog).getByText('Apply Changes'));
  1533. await waitFor(() => expect(patchBody).not.toBeNull());
  1534. expect(patchBody!.item_ids).toEqual([1]);
  1535. expect(patchBody!.gcode_injection).toBe(true);
  1536. });
  1537. it('leaves gcode_injection out of the PATCH while it stays on "no change"', async () => {
  1538. let patchBody: Record<string, unknown> | null = null;
  1539. server.use(
  1540. http.get('/api/v1/settings/', () => HttpResponse.json(settingsWithSnippets)),
  1541. http.patch('/api/v1/queue/bulk', async ({ request }) => {
  1542. patchBody = (await request.json()) as Record<string, unknown>;
  1543. return HttpResponse.json({ updated_count: 1, skipped_count: 0, message: 'Updated 1 items' });
  1544. }),
  1545. );
  1546. const dialog = await openBulkEdit();
  1547. // Change something else entirely; injection must not ride along as false.
  1548. const stagedRow = screen.getByText('Staged (manual start)').closest('div')!;
  1549. await userEvent.click(within(stagedRow).getByText('On'));
  1550. await userEvent.click(within(dialog).getByText('Apply Changes'));
  1551. await waitFor(() => expect(patchBody).not.toBeNull());
  1552. expect(patchBody!.manual_start).toBe(true);
  1553. expect('gcode_injection' in patchBody!).toBe(false);
  1554. });
  1555. });
  1556. describe('bulk edit ask-for-outcome (#1898)', () => {
  1557. const openBulkEdit = async () => {
  1558. render(<QueuePage />);
  1559. await waitFor(() => expect(screen.getByText('Test Print 1')).toBeInTheDocument());
  1560. await userEvent.click(screen.getByText('Select All'));
  1561. await userEvent.click(await screen.findByTitle('Edit Selected'));
  1562. return screen.getByText('Edit 1 Item').closest('div')!.parentElement!;
  1563. };
  1564. const captureBulkPatch = () => {
  1565. const captured: { body: Record<string, unknown> | null } = { body: null };
  1566. server.use(
  1567. http.patch('/api/v1/queue/bulk', async ({ request }) => {
  1568. captured.body = (await request.json()) as Record<string, unknown>;
  1569. return HttpResponse.json({ updated_count: 1, skipped_count: 0, message: 'Updated 1 items' });
  1570. }),
  1571. );
  1572. return captured;
  1573. };
  1574. it('sends confirm_outcome with the bulk PATCH once toggled', async () => {
  1575. const captured = captureBulkPatch();
  1576. const dialog = await openBulkEdit();
  1577. const row = within(dialog).getByText('Ask for outcome').closest('div')!;
  1578. await userEvent.click(within(row).getByText('On'));
  1579. await userEvent.click(within(dialog).getByText('Apply Changes'));
  1580. await waitFor(() => expect(captured.body).not.toBeNull());
  1581. expect(captured.body!.item_ids).toEqual([1]);
  1582. expect(captured.body!.confirm_outcome).toBe(true);
  1583. });
  1584. it('leaves confirm_outcome out of the PATCH while it stays on "no change"', async () => {
  1585. const captured = captureBulkPatch();
  1586. const dialog = await openBulkEdit();
  1587. const timelapseRow = within(dialog).getByText('Timelapse').closest('div')!;
  1588. await userEvent.click(within(timelapseRow).getByText('On'));
  1589. await userEvent.click(within(dialog).getByText('Apply Changes'));
  1590. await waitFor(() => expect(captured.body).not.toBeNull());
  1591. expect(captured.body!.timelapse).toBe(true);
  1592. expect('confirm_outcome' in captured.body!).toBe(false);
  1593. });
  1594. });
  1595. });