QueuePage.test.tsx 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  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[], extra: Record<string, unknown> = {}) => ({
  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. ...extra,
  694. });
  695. const useMappedPrinter = (
  696. amsMapping: number[],
  697. status: Record<string, unknown>,
  698. item: Record<string, unknown> = {},
  699. ) => {
  700. server.use(
  701. http.get('/api/v1/queue/', () => HttpResponse.json([mappedItem(amsMapping, item)])),
  702. http.get('/api/v1/library/files/19/plates', () =>
  703. HttpResponse.json({
  704. file_id: 19,
  705. filename: 'mapped-slot.3mf',
  706. plates: [
  707. {
  708. index: 1,
  709. name: 'Plate 1',
  710. objects: ['Part'],
  711. has_thumbnail: false,
  712. thumbnail_url: null,
  713. print_time_seconds: 3600,
  714. filament_used_grams: 20,
  715. filaments: [
  716. { slot_id: 1, type: 'PLA', color: '#7C4B00', used_grams: 20, used_meters: 6.7 },
  717. ],
  718. },
  719. ],
  720. is_multi_plate: false,
  721. }),
  722. ),
  723. http.get('/api/v1/printers/:id/status', () =>
  724. HttpResponse.json({
  725. id: 1,
  726. name: 'Test Printer',
  727. state: 'IDLE',
  728. nozzles: [],
  729. ams_extruder_map: {},
  730. ...status,
  731. }),
  732. ),
  733. http.get('/api/v1/printers/:id/inventory-remain', () =>
  734. HttpResponse.json({ inventory_remain_g: {}, slot_materials: [] }),
  735. ),
  736. );
  737. };
  738. // Regular AMS units expose four tray records; slot A3 (global 2) is empty.
  739. const amsWithEmptyA3 = [
  740. {
  741. id: 0,
  742. tray: [
  743. { id: 0, tray_type: 'PLA', tray_color: 'FFFFFFFF', remain: 50 },
  744. { id: 1, tray_type: 'PLA', tray_color: 'C2BAA7FF', remain: 80 },
  745. // Firmware confirms A3 has no spool (tray_exist_bits bit clear).
  746. { id: 2, tray_type: null, exists: false, state: 9 },
  747. { id: 3, tray_type: null },
  748. ],
  749. },
  750. ];
  751. const mappedRow = async () => {
  752. render(<QueuePage />);
  753. const name = await screen.findByText('Mapped slot test');
  754. const row = name.closest('.group');
  755. expect(row).not.toBeNull();
  756. return row as HTMLElement;
  757. };
  758. it('resolves a mapping to the external spool', async () => {
  759. useMappedPrinter([254], {
  760. connected: true,
  761. ams: [],
  762. vt_tray: [{ id: 254, tray_type: 'PETG', tray_color: 'C2BAA7FF', remain: 60 }],
  763. });
  764. const row = await mappedRow();
  765. await waitFor(() => {
  766. expect(within(row).getByText(/^External · PETG · /)).toBeInTheDocument();
  767. });
  768. expect(within(row).queryByText('Caramel')).not.toBeInTheDocument();
  769. });
  770. it('says the mapped slot is empty, keeping the intended colour', async () => {
  771. useMappedPrinter([2], { connected: true, ams: amsWithEmptyA3, vt_tray: [] });
  772. const row = await mappedRow();
  773. await waitFor(() => {
  774. expect(within(row).getByText('A3 · Empty · Caramel')).toBeInTheDocument();
  775. });
  776. expect(within(row).getByTestId('filament-swatch')).toHaveAttribute('title', '#8E351B');
  777. });
  778. describe('AMS Filament Backup during the print', () => {
  779. const printing = { status: 'printing', started_at: '2024-01-01T10:00:00Z' };
  780. it('shows the backup spool the printer switched to', async () => {
  781. // A3 ran out at layer 350 and the printer carried on from A2.
  782. useMappedPrinter(
  783. [2],
  784. { connected: true, state: 'RUNNING', ams: amsWithEmptyA3, vt_tray: [], tray_change_log: [[2, 0], [1, 350]] },
  785. printing,
  786. );
  787. const row = await mappedRow();
  788. await waitFor(() => {
  789. expect(within(row).getByText(/^A2 · PLA · /)).toBeInTheDocument();
  790. });
  791. expect(within(row).queryByText(/Empty/)).not.toBeInTheDocument();
  792. });
  793. it('follows a backup that ran out in turn', async () => {
  794. useMappedPrinter(
  795. [2],
  796. {
  797. connected: true,
  798. state: 'RUNNING',
  799. ams: amsWithEmptyA3,
  800. vt_tray: [],
  801. tray_change_log: [[2, 0], [1, 120], [0, 400]],
  802. },
  803. printing,
  804. );
  805. const row = await mappedRow();
  806. await waitFor(() => {
  807. expect(within(row).getByText(/^A1 · PLA · /)).toBeInTheDocument();
  808. });
  809. expect(within(row).queryByText(/^A2 · /)).not.toBeInTheDocument();
  810. });
  811. it('does not take a change to another mapped slot for a backup', async () => {
  812. // Slot 2 of the job feeds from A2: switching there is a colour change.
  813. useMappedPrinter(
  814. [2, 1],
  815. { connected: true, state: 'RUNNING', ams: amsWithEmptyA3, vt_tray: [], tray_change_log: [[2, 0], [1, 30]] },
  816. printing,
  817. );
  818. const row = await mappedRow();
  819. await waitFor(() => {
  820. expect(within(row).getByText('A3 · Empty · Caramel')).toBeInTheDocument();
  821. });
  822. });
  823. it('leaves a queued job pointed at its mapped slot', async () => {
  824. // The log belongs to whatever the printer prints now, not this job.
  825. useMappedPrinter([2], {
  826. connected: true,
  827. ams: amsWithEmptyA3,
  828. vt_tray: [],
  829. tray_change_log: [[2, 0], [1, 350]],
  830. });
  831. const row = await mappedRow();
  832. await waitFor(() => {
  833. expect(within(row).getByText('A3 · Empty · Caramel')).toBeInTheDocument();
  834. });
  835. });
  836. });
  837. it('never calls the external spool empty, having no presence signal', async () => {
  838. // Shape the status route really sends: an external tray carries no
  839. // state / exists, so an unconfigured spool and no spool look the same.
  840. let statusServed = false;
  841. useMappedPrinter([254], { connected: true, ams: [], vt_tray: [] });
  842. server.use(
  843. http.get('/api/v1/printers/:id/status', () => {
  844. statusServed = true;
  845. return HttpResponse.json({ id: 1, name: 'Test Printer', state: 'IDLE', connected: true, ams: [], vt_tray: [{ id: 254, tray_type: '' }], nozzles: [], ams_extruder_map: {} });
  846. }),
  847. );
  848. const row = await mappedRow();
  849. await waitFor(() => expect(statusServed).toBe(true));
  850. expect(await within(row).findByText('Caramel')).toBeInTheDocument();
  851. expect(within(row).queryByText(/Empty/)).not.toBeInTheDocument();
  852. });
  853. it('does not call a loaded but unconfigured spool empty (#2527)', async () => {
  854. // A non-RFID spool the firmware can't identify has no tray_type but is
  855. // physically present; the Printers page draws it as "?", not "Empty".
  856. const ams = [
  857. {
  858. id: 0,
  859. tray: [
  860. { id: 0, tray_type: 'PLA', tray_color: 'FFFFFFFF', remain: 50 },
  861. { id: 1, tray_type: 'PLA', tray_color: 'C2BAA7FF', remain: 80 },
  862. { id: 2, tray_type: '', exists: true, state: 3 },
  863. { id: 3, tray_type: null },
  864. ],
  865. },
  866. ];
  867. let statusServed = false;
  868. useMappedPrinter([2], { connected: true, ams, vt_tray: [] });
  869. server.use(
  870. http.get('/api/v1/printers/:id/status', () => {
  871. statusServed = true;
  872. return HttpResponse.json({ id: 1, name: 'Test Printer', state: 'IDLE', connected: true, ams, vt_tray: [], nozzles: [], ams_extruder_map: {} });
  873. }),
  874. );
  875. const row = await mappedRow();
  876. await waitFor(() => expect(statusServed).toBe(true));
  877. expect(await within(row).findByText('Caramel')).toBeInTheDocument();
  878. expect(within(row).queryByText(/Empty/)).not.toBeInTheDocument();
  879. });
  880. it('does not call a slot empty while the printer is offline', async () => {
  881. useMappedPrinter([2], { connected: false, ams: amsWithEmptyA3, vt_tray: [] });
  882. const row = await mappedRow();
  883. expect(await within(row).findByText('Caramel')).toBeInTheDocument();
  884. expect(within(row).queryByText(/Empty/)).not.toBeInTheDocument();
  885. });
  886. });
  887. it('shows one if-started-now ETA for an eligible pending item', async () => {
  888. // Printer 1 is free: nothing is printing on it and nothing is queued ahead.
  889. server.use(
  890. http.get('/api/v1/queue/', () => {
  891. return HttpResponse.json([mockQueueItems[0]]);
  892. }),
  893. );
  894. render(<QueuePage />);
  895. const name = await screen.findByText('Test Print 1');
  896. const row = name.closest('.group');
  897. expect(row).not.toBeNull();
  898. const etaEl = within(row as HTMLElement).getAllByTestId('queue-item-eta');
  899. expect(etaEl).toHaveLength(1);
  900. // The tooltip must say what the number actually means, not reuse the
  901. // printers-page "Estimated completion time" wording (which this is not).
  902. expect(etaEl[0]).toHaveAttribute(
  903. 'title',
  904. 'Completion time if this job started now',
  905. );
  906. });
  907. // The scheduler only writes waiting_reason on the model-based assignment
  908. // path, so an item pinned to a specific printer carries no marker at all
  909. // while it sits behind a running job. Without the printer-busy check every
  910. // one of these quoted the same wrong "starts now" time.
  911. it('does not show an ETA for an item pinned behind a running print', async () => {
  912. render(<QueuePage />);
  913. // mockQueueItems[1] ("Active Print") is printing on printer 1, and
  914. // "Test Print 1" is pending on the same printer with waiting_reason null.
  915. const name = await screen.findByText('Test Print 1');
  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('shows the ETA only on the next item up when several share an idle printer', async () => {
  923. server.use(
  924. http.get('/api/v1/queue/', () => {
  925. return HttpResponse.json([
  926. { ...mockQueueItems[0], id: 10, position: 1, archive_name: 'First up' },
  927. { ...mockQueueItems[0], id: 11, position: 2, archive_name: 'Second up' },
  928. { ...mockQueueItems[0], id: 12, position: 3, archive_name: 'Third up' },
  929. ]);
  930. }),
  931. );
  932. render(<QueuePage />);
  933. await screen.findByText('Third up');
  934. const etaRowNames = screen
  935. .queryAllByTestId('queue-item-eta')
  936. .map((el) => el.closest('.group')?.textContent);
  937. expect(etaRowNames).toHaveLength(1);
  938. expect(etaRowNames[0]).toContain('First up');
  939. });
  940. it('shows an ETA for a staged item queued behind others on an idle printer', async () => {
  941. // The scheduler skips manual-start items without claiming the printer, so
  942. // a staged job is startable whenever its printer is free — queue order
  943. // does not gate it.
  944. server.use(
  945. http.get('/api/v1/queue/', () => {
  946. return HttpResponse.json([
  947. { ...mockQueueItems[0], id: 20, position: 1, archive_name: 'Auto first' },
  948. {
  949. ...mockQueueItems[0],
  950. id: 21,
  951. position: 2,
  952. archive_name: 'Staged second',
  953. manual_start: true,
  954. },
  955. ]);
  956. }),
  957. );
  958. render(<QueuePage />);
  959. const name = await screen.findByText('Staged second');
  960. const row = name.closest('.group');
  961. expect(row).not.toBeNull();
  962. expect(
  963. within(row as HTMLElement).getAllByTestId('queue-item-eta'),
  964. ).toHaveLength(1);
  965. });
  966. it('does not show an ETA for an item conditional on a previous print', async () => {
  967. server.use(
  968. http.get('/api/v1/queue/', () => {
  969. return HttpResponse.json([
  970. {
  971. ...mockQueueItems[0],
  972. archive_name: 'Conditional Print',
  973. require_previous_success: true,
  974. },
  975. ]);
  976. }),
  977. );
  978. render(<QueuePage />);
  979. const name = await screen.findByText('Conditional Print');
  980. const row = name.closest('.group');
  981. expect(row).not.toBeNull();
  982. expect(
  983. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  984. ).not.toBeInTheDocument();
  985. });
  986. it('advances the ETA as time passes', async () => {
  987. vi.useFakeTimers({ shouldAdvanceTime: true });
  988. vi.setSystemTime(new Date('2026-08-02T10:00:00Z'));
  989. try {
  990. server.use(
  991. http.get('/api/v1/queue/', () => {
  992. return HttpResponse.json([mockQueueItems[0]]);
  993. }),
  994. );
  995. render(<QueuePage />);
  996. const name = await screen.findByText('Test Print 1');
  997. const row = name.closest('.group') as HTMLElement;
  998. const before = within(row).getByTestId('queue-item-eta').textContent;
  999. // The queue payload never changes, so react-query hands back the same
  1000. // object and nothing here re-renders on its own. Only the page's own
  1001. // clock can move this value.
  1002. await vi.advanceTimersByTimeAsync(45 * 60 * 1000);
  1003. await waitFor(() => {
  1004. expect(
  1005. within(row).getByTestId('queue-item-eta').textContent,
  1006. ).not.toBe(before);
  1007. });
  1008. } finally {
  1009. vi.useRealTimers();
  1010. }
  1011. });
  1012. it('shows one if-started-now ETA for a staged item', async () => {
  1013. server.use(
  1014. http.get('/api/v1/queue/', () => {
  1015. return HttpResponse.json([
  1016. {
  1017. ...mockQueueItems[0],
  1018. archive_name: 'Staged Print',
  1019. manual_start: true,
  1020. },
  1021. ]);
  1022. }),
  1023. );
  1024. render(<QueuePage />);
  1025. const name = await screen.findByText('Staged Print');
  1026. const row = name.closest('.group');
  1027. expect(row).not.toBeNull();
  1028. expect(
  1029. within(row as HTMLElement).getAllByTestId('queue-item-eta'),
  1030. ).toHaveLength(1);
  1031. });
  1032. it('shows exactly one live ETA for a printing item', async () => {
  1033. server.use(
  1034. http.get('/api/v1/printers/:id/status', ({ params }) => {
  1035. return HttpResponse.json({
  1036. id: Number(params.id),
  1037. name: 'Test Printer',
  1038. connected: true,
  1039. state: 'RUNNING',
  1040. progress: 50,
  1041. remaining_time: 60,
  1042. layer_num: 50,
  1043. total_layers: 100,
  1044. filename: 'active.3mf',
  1045. });
  1046. }),
  1047. );
  1048. render(<QueuePage />);
  1049. const name = await screen.findByText('Active Print');
  1050. const row = name.closest('.group');
  1051. expect(row).not.toBeNull();
  1052. await waitFor(() => {
  1053. expect(
  1054. within(row as HTMLElement).getAllByText(/^ETA\s/),
  1055. ).toHaveLength(1);
  1056. });
  1057. expect(
  1058. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  1059. ).not.toBeInTheDocument();
  1060. });
  1061. it('does not show an ETA for a waiting item', async () => {
  1062. server.use(
  1063. http.get('/api/v1/queue/', () => {
  1064. return HttpResponse.json([
  1065. {
  1066. ...mockQueueItems[0],
  1067. archive_name: 'Waiting Print',
  1068. waiting_reason: 'Waiting for matching printer',
  1069. },
  1070. ]);
  1071. }),
  1072. );
  1073. render(<QueuePage />);
  1074. const name = await screen.findByText('Waiting Print');
  1075. const row = name.closest('.group');
  1076. expect(row).not.toBeNull();
  1077. expect(
  1078. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  1079. ).not.toBeInTheDocument();
  1080. });
  1081. it('does not show an if-started-now ETA for a scheduled item', async () => {
  1082. server.use(
  1083. http.get('/api/v1/queue/', () => {
  1084. return HttpResponse.json([
  1085. {
  1086. ...mockQueueItems[0],
  1087. archive_name: 'Scheduled Print',
  1088. scheduled_time: new Date(
  1089. Date.now() + 5 * 60 * 60 * 1000,
  1090. ).toISOString(),
  1091. },
  1092. ]);
  1093. }),
  1094. );
  1095. render(<QueuePage />);
  1096. const name = await screen.findByText('Scheduled Print');
  1097. const row = name.closest('.group');
  1098. expect(row).not.toBeNull();
  1099. expect(
  1100. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  1101. ).not.toBeInTheDocument();
  1102. });
  1103. it('tells an unscheduled item when it runs, without naming a dispatch mode', async () => {
  1104. // ASAP and Queue differ only in where the item is inserted; neither is
  1105. // stored on it, so this column cannot tell them apart. Labelling every
  1106. // unscheduled item "ASAP" made a Queue choice look overridden, which is
  1107. // what both #2557 and #3018 opened on.
  1108. server.use(
  1109. http.get('/api/v1/queue/', () => {
  1110. return HttpResponse.json([
  1111. { ...mockQueueItems[0], archive_name: 'Queued Print', scheduled_time: null },
  1112. ]);
  1113. }),
  1114. );
  1115. render(<QueuePage />);
  1116. const name = await screen.findByText('Queued Print');
  1117. const row = name.closest('.group') as HTMLElement;
  1118. expect(row).not.toBeNull();
  1119. expect(within(row).getByText('When a printer is free')).toBeInTheDocument();
  1120. expect(within(row).queryByText('ASAP')).not.toBeInTheDocument();
  1121. });
  1122. it('does not render a dangling ETA for an invalid duration', async () => {
  1123. server.use(
  1124. http.get('/api/v1/queue/', () => {
  1125. return HttpResponse.json([
  1126. {
  1127. ...mockQueueItems[0],
  1128. archive_name: 'Invalid Duration Print',
  1129. print_time_seconds: -60,
  1130. },
  1131. ]);
  1132. }),
  1133. );
  1134. render(<QueuePage />);
  1135. const name = await screen.findByText('Invalid Duration Print');
  1136. const row = name.closest('.group');
  1137. expect(row).not.toBeNull();
  1138. expect(
  1139. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  1140. ).not.toBeInTheDocument();
  1141. expect(
  1142. within(row as HTMLElement).queryByText(/^ETA(?:\s|$)/),
  1143. ).not.toBeInTheDocument();
  1144. });
  1145. it('shows completed items in history', async () => {
  1146. const user = userEvent.setup();
  1147. render(<QueuePage />);
  1148. // The History tab now owns the completed/cancelled/failed list.
  1149. await user.click(await screen.findByRole('button', { name: /^History/ }));
  1150. await waitFor(() => {
  1151. expect(screen.getByText('Completed Print')).toBeInTheDocument();
  1152. });
  1153. });
  1154. it('shows status badges', async () => {
  1155. render(<QueuePage />);
  1156. await waitFor(() => {
  1157. // Queue items should be visible with status indicators
  1158. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  1159. });
  1160. });
  1161. it('shows printer names', async () => {
  1162. render(<QueuePage />);
  1163. await waitFor(() => {
  1164. const printerElements = screen.getAllByText('Test Printer');
  1165. expect(printerElements.length).toBeGreaterThan(0);
  1166. });
  1167. });
  1168. it('renders queue items with plate_id correctly', async () => {
  1169. // Override with queue items that have plate_id set
  1170. server.use(
  1171. http.get('/api/v1/queue/', () => {
  1172. return HttpResponse.json([
  1173. {
  1174. ...mockQueueItems[0],
  1175. plate_id: 2,
  1176. archive_name: 'Multi-plate Print',
  1177. },
  1178. ]);
  1179. })
  1180. );
  1181. render(<QueuePage />);
  1182. await waitFor(() => {
  1183. expect(screen.getByText('Multi-plate Print')).toBeInTheDocument();
  1184. });
  1185. });
  1186. });
  1187. describe('history pagination', () => {
  1188. // #2682: History rendered the full count in the header but only ever drew
  1189. // the first 50 rows, with no way to reach the rest. It now paginates with
  1190. // a "Show more" control.
  1191. const manyHistory = Array.from({ length: 60 }, (_, i) => ({
  1192. ...mockQueueItems[2],
  1193. id: 100 + i,
  1194. batch_id: null,
  1195. archive_name: `History Item ${String(i).padStart(2, '0')}`,
  1196. // Descending completed_at so index 0 is newest and sorts first; the
  1197. // default History sort is by date, newest first.
  1198. completed_at: new Date(Date.UTC(2024, 0, 1, 0, 0, 0) - i * 60000).toISOString(),
  1199. }));
  1200. beforeEach(() => {
  1201. server.use(
  1202. http.get('/api/v1/queue/', () => {
  1203. return HttpResponse.json(manyHistory);
  1204. })
  1205. );
  1206. });
  1207. it('caps the History list at one page and reveals the rest on Show more', async () => {
  1208. const user = userEvent.setup();
  1209. render(<QueuePage />);
  1210. await user.click(await screen.findByRole('button', { name: /^History/ }));
  1211. // First page is drawn; an item past the 50-row cap is not.
  1212. await waitFor(() => {
  1213. expect(screen.getByText('History Item 00')).toBeInTheDocument();
  1214. });
  1215. expect(screen.queryByText('History Item 59')).not.toBeInTheDocument();
  1216. expect(screen.getByText('Showing 50 of 60')).toBeInTheDocument();
  1217. // Show more reveals the remainder and then disappears (nothing left).
  1218. await user.click(screen.getByRole('button', { name: /show more/i }));
  1219. await waitFor(() => {
  1220. expect(screen.getByText('History Item 59')).toBeInTheDocument();
  1221. });
  1222. expect(screen.queryByRole('button', { name: /show more/i })).not.toBeInTheDocument();
  1223. });
  1224. });
  1225. describe('empty state', () => {
  1226. it('shows empty state when no queue items', async () => {
  1227. server.use(
  1228. http.get('/api/v1/queue/', () => {
  1229. return HttpResponse.json([]);
  1230. })
  1231. );
  1232. render(<QueuePage />);
  1233. await waitFor(() => {
  1234. expect(screen.getByText('No prints scheduled')).toBeInTheDocument();
  1235. });
  1236. });
  1237. });
  1238. describe('filtering', () => {
  1239. it('has printer filter options', async () => {
  1240. const user = userEvent.setup();
  1241. render(<QueuePage />);
  1242. await waitFor(() => {
  1243. expect(screen.getByText('All Printers')).toBeInTheDocument();
  1244. });
  1245. const printerSelect = screen.getByDisplayValue('All Printers');
  1246. await user.click(printerSelect);
  1247. expect(screen.getByText('Unassigned')).toBeInTheDocument();
  1248. });
  1249. it('has status filter options', async () => {
  1250. const user = userEvent.setup();
  1251. render(<QueuePage />);
  1252. await waitFor(() => {
  1253. expect(screen.getByText('All Status')).toBeInTheDocument();
  1254. });
  1255. const statusSelect = screen.getByDisplayValue('All Status');
  1256. await user.click(statusSelect);
  1257. expect(screen.getByRole('option', { name: 'Pending' })).toBeInTheDocument();
  1258. expect(screen.getByRole('option', { name: 'Printing' })).toBeInTheDocument();
  1259. expect(screen.getByRole('option', { name: 'Completed' })).toBeInTheDocument();
  1260. });
  1261. });
  1262. describe('queue actions', () => {
  1263. it('shows edit button for pending items', async () => {
  1264. render(<QueuePage />);
  1265. await waitFor(() => {
  1266. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  1267. });
  1268. // Find the edit button (Pencil icon)
  1269. const editButtons = screen.getAllByTitle('Edit');
  1270. expect(editButtons.length).toBeGreaterThan(0);
  1271. });
  1272. it('shows cancel button for pending items', async () => {
  1273. render(<QueuePage />);
  1274. await waitFor(() => {
  1275. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  1276. });
  1277. const cancelButtons = screen.getAllByTitle('Cancel');
  1278. expect(cancelButtons.length).toBeGreaterThan(0);
  1279. });
  1280. it('shows stop button for printing items', async () => {
  1281. render(<QueuePage />);
  1282. await waitFor(() => {
  1283. expect(screen.getByText('Active Print')).toBeInTheDocument();
  1284. });
  1285. const stopButtons = screen.getAllByTitle('Stop Print');
  1286. expect(stopButtons.length).toBeGreaterThan(0);
  1287. });
  1288. it('shows re-queue button for history items', async () => {
  1289. const user = userEvent.setup();
  1290. render(<QueuePage />);
  1291. await user.click(await screen.findByRole('button', { name: /^History/ }));
  1292. await waitFor(() => {
  1293. expect(screen.getByText('Completed Print')).toBeInTheDocument();
  1294. });
  1295. const requeueButtons = screen.getAllByTitle('Re-queue');
  1296. expect(requeueButtons.length).toBeGreaterThan(0);
  1297. });
  1298. });
  1299. describe('clear history', () => {
  1300. it('shows clear history button when history exists', async () => {
  1301. const user = userEvent.setup();
  1302. render(<QueuePage />);
  1303. // Clear History only renders inside the History tab now.
  1304. await user.click(await screen.findByRole('button', { name: /^History/ }));
  1305. await waitFor(() => {
  1306. expect(screen.getByText('Clear History')).toBeInTheDocument();
  1307. });
  1308. });
  1309. it('opens confirm modal when clicking clear history', async () => {
  1310. const user = userEvent.setup();
  1311. render(<QueuePage />);
  1312. await user.click(await screen.findByRole('button', { name: /^History/ }));
  1313. await waitFor(() => {
  1314. expect(screen.getByText('Clear History')).toBeInTheDocument();
  1315. });
  1316. const clearButton = screen.getByRole('button', { name: /clear history/i });
  1317. await user.click(clearButton);
  1318. await waitFor(() => {
  1319. expect(screen.getByText(/Are you sure you want to remove all/i)).toBeInTheDocument();
  1320. });
  1321. });
  1322. });
  1323. describe('staged items', () => {
  1324. it('shows staged badge for manual_start items', async () => {
  1325. server.use(
  1326. http.get('/api/v1/queue/', () => {
  1327. return HttpResponse.json([
  1328. {
  1329. ...mockQueueItems[0],
  1330. manual_start: true,
  1331. },
  1332. ]);
  1333. })
  1334. );
  1335. render(<QueuePage />);
  1336. await waitFor(() => {
  1337. expect(screen.getByText('Staged')).toBeInTheDocument();
  1338. });
  1339. });
  1340. it('shows start button for staged items', async () => {
  1341. server.use(
  1342. http.get('/api/v1/queue/', () => {
  1343. return HttpResponse.json([
  1344. {
  1345. ...mockQueueItems[0],
  1346. manual_start: true,
  1347. },
  1348. ]);
  1349. })
  1350. );
  1351. render(<QueuePage />);
  1352. await waitFor(() => {
  1353. expect(screen.getByTitle('Start Print')).toBeInTheDocument();
  1354. });
  1355. });
  1356. // Without queue:start_unreviewed their jobs wait for staff to start them (#1620)
  1357. describe('waiting for review', () => {
  1358. const signInWith = (permissions: string[]) => {
  1359. setAuthToken('test-token', 'session');
  1360. server.use(
  1361. http.get('*/api/v1/auth/status', () => HttpResponse.json({ auth_enabled: true, requires_setup: false })),
  1362. http.get('*/api/v1/auth/me', () =>
  1363. HttpResponse.json({ id: 7, username: 'student', is_admin: false, permissions }),
  1364. ),
  1365. http.get('/api/v1/queue/', () =>
  1366. HttpResponse.json([{ ...mockQueueItems[0], manual_start: true, created_by_id: 7 }]),
  1367. ),
  1368. );
  1369. };
  1370. afterEach(() => {
  1371. setAuthToken(null);
  1372. });
  1373. it('tells a student their job waits for review and offers no start', async () => {
  1374. signInWith(['queue:read_own', 'queue:update_own', 'queue:delete_own']);
  1375. render(<QueuePage />);
  1376. expect(await screen.findByText('Waiting for review')).toBeInTheDocument();
  1377. const start = screen.getByTitle('Waiting for review: someone who manages the queue starts this job');
  1378. expect(start).toBeDisabled();
  1379. });
  1380. it('keeps the start button for users who may print without review', async () => {
  1381. signInWith(['queue:read_own', 'queue:update_own', 'queue:start_unreviewed']);
  1382. render(<QueuePage />);
  1383. expect(await screen.findByText('Staged')).toBeInTheDocument();
  1384. expect(screen.getByTitle('Start Print')).not.toBeDisabled();
  1385. });
  1386. });
  1387. });
  1388. describe('auto power off badge', () => {
  1389. it('shows power off badge when auto_off_after is true', async () => {
  1390. render(<QueuePage />);
  1391. await waitFor(() => {
  1392. expect(screen.getByText('Auto power off')).toBeInTheDocument();
  1393. });
  1394. });
  1395. });
  1396. describe('gcode injection badge', () => {
  1397. it('shows G-code badge when gcode_injection is true', async () => {
  1398. const itemsWithGcode = mockQueueItems.map((item, i) =>
  1399. i === 0 ? { ...item, gcode_injection: true } : item
  1400. );
  1401. server.use(
  1402. http.get('/api/v1/queue/', () => HttpResponse.json(itemsWithGcode)),
  1403. );
  1404. render(<QueuePage />);
  1405. await waitFor(() => {
  1406. expect(screen.getByText('G-code')).toBeInTheDocument();
  1407. });
  1408. });
  1409. it('does not show G-code badge when gcode_injection is false', async () => {
  1410. render(<QueuePage />);
  1411. await waitFor(() => {
  1412. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  1413. });
  1414. expect(screen.queryByText('G-code')).not.toBeInTheDocument();
  1415. });
  1416. });
  1417. describe('outcome prompt badge (#1898)', () => {
  1418. it('shows the badge only on items with confirm_outcome set', async () => {
  1419. const items = mockQueueItems.map((item, i) =>
  1420. i === 0 ? { ...item, confirm_outcome: true } : { ...item, confirm_outcome: false }
  1421. );
  1422. server.use(http.get('/api/v1/queue/', () => HttpResponse.json(items)));
  1423. render(<QueuePage />);
  1424. await waitFor(() => expect(screen.getByText('Test Print 1')).toBeInTheDocument());
  1425. const badges = screen.getAllByText('Outcome prompt');
  1426. expect(badges).toHaveLength(1);
  1427. expect(badges[0].closest('span')).toHaveAttribute('title', 'Asks how the print came out after it completes');
  1428. });
  1429. it('renders nothing when no item asks for an outcome', async () => {
  1430. render(<QueuePage />);
  1431. await waitFor(() => expect(screen.getByText('Test Print 1')).toBeInTheDocument());
  1432. expect(screen.queryByText('Outcome prompt')).not.toBeInTheDocument();
  1433. });
  1434. });
  1435. describe('filament-short ▶ flow (#1496)', () => {
  1436. /**
  1437. * The dispatch pre-flight flags a queue item as filament_short. The user
  1438. * clicks ▶, the backend re-checks live and either dispatches (no deficit
  1439. * anymore — clear flag) or returns 409 with the per-slot deficit so the
  1440. * frontend can render the "Print Anyway" confirm modal.
  1441. */
  1442. const shortItem = {
  1443. ...mockQueueItems[0],
  1444. manual_start: true,
  1445. filament_short: true,
  1446. };
  1447. it('renders the filament-short badge on a flagged pending row', async () => {
  1448. server.use(
  1449. http.get('/api/v1/queue/', () => HttpResponse.json([shortItem])),
  1450. );
  1451. render(<QueuePage />);
  1452. await waitFor(() => {
  1453. expect(screen.getByText(/Insufficient filament for the assigned spool/i)).toBeInTheDocument();
  1454. });
  1455. });
  1456. it('opens the Print Anyway modal when ▶ returns 409 and retries with skip_filament_check', async () => {
  1457. let secondCallSkippedCheck: boolean | null = null;
  1458. let attempts = 0;
  1459. server.use(
  1460. http.get('/api/v1/queue/', () => HttpResponse.json([shortItem])),
  1461. http.post('/api/v1/queue/:id/start', ({ request }) => {
  1462. attempts += 1;
  1463. const url = new URL(request.url);
  1464. const skip = url.searchParams.get('skip_filament_check') === 'true';
  1465. if (attempts === 1) {
  1466. return HttpResponse.json(
  1467. {
  1468. detail: {
  1469. code: 'insufficient_filament',
  1470. deficit: [
  1471. {
  1472. slot_id: 1,
  1473. ams_id: 0,
  1474. tray_id: 0,
  1475. filament_type: 'PLA',
  1476. required_grams: 270,
  1477. remaining_grams: 200,
  1478. },
  1479. ],
  1480. },
  1481. },
  1482. { status: 409 },
  1483. );
  1484. }
  1485. secondCallSkippedCheck = skip;
  1486. return HttpResponse.json({ ...shortItem, manual_start: false, filament_short: false });
  1487. }),
  1488. );
  1489. render(<QueuePage />);
  1490. const playButton = await screen.findByTitle(/Start Print|do not have permission to start prints/i);
  1491. await userEvent.click(playButton);
  1492. // Wait for the start endpoint to be hit (the 409 path returns to onError).
  1493. await waitFor(() => expect(attempts).toBe(1));
  1494. // Modal shows the deficit detail
  1495. await screen.findByRole('button', { name: /Print Anyway/i });
  1496. expect(
  1497. screen.getByText(/Slot 1: needs 270 g, 200 g remaining/i),
  1498. ).toBeInTheDocument();
  1499. await userEvent.click(screen.getByRole('button', { name: /Print Anyway/i }));
  1500. await waitFor(() => expect(secondCallSkippedCheck).toBe(true));
  1501. expect(attempts).toBe(2);
  1502. });
  1503. });
  1504. describe('filament-not-loaded ▶ flow (#2799)', () => {
  1505. /**
  1506. * The scheduler held the item because a filament it prints has no tray on
  1507. * the printer. ▶ asks the backend first: still missing is a 409 with the
  1508. * missing filaments, and "Print Anyway" retries with skip_filament_check.
  1509. */
  1510. const heldItem = {
  1511. ...mockQueueItems[0],
  1512. manual_start: true,
  1513. waiting_reason: 'Needs PETG #2850E0',
  1514. };
  1515. it('shows the missing filament and retries with skip_filament_check', async () => {
  1516. let secondCallSkippedCheck: boolean | null = null;
  1517. let attempts = 0;
  1518. server.use(
  1519. http.get('/api/v1/queue/', () => HttpResponse.json([heldItem])),
  1520. http.post('/api/v1/queue/:id/start', ({ request }) => {
  1521. attempts += 1;
  1522. if (attempts === 1) {
  1523. return HttpResponse.json(
  1524. { detail: { code: 'unmatched_filament', missing: ['PETG #2850E0'] } },
  1525. { status: 409 },
  1526. );
  1527. }
  1528. secondCallSkippedCheck = new URL(request.url).searchParams.get('skip_filament_check') === 'true';
  1529. return HttpResponse.json({ ...heldItem, manual_start: false });
  1530. }),
  1531. );
  1532. render(<QueuePage />);
  1533. const playButton = await screen.findByTitle(/Start Print|do not have permission to start prints/i);
  1534. await userEvent.click(playButton);
  1535. await waitFor(() => expect(attempts).toBe(1));
  1536. await screen.findByText('Filament not loaded');
  1537. // The dialog's message names what to load; the row says it too.
  1538. expect(screen.getByText(/no tray with this filament[\s\S]*PETG #2850E0/)).toBeInTheDocument();
  1539. await userEvent.click(screen.getByRole('button', { name: /Print Anyway/i }));
  1540. await waitFor(() => expect(secondCallSkippedCheck).toBe(true));
  1541. expect(attempts).toBe(2);
  1542. });
  1543. });
  1544. // #2667: mobile can't drag-reorder the queue, so pending rows get up/down
  1545. // arrows. They persist via the same POST /queue/reorder as drag. The
  1546. // buttons live in the DOM at every width (Tailwind `sm:hidden` is CSS-only),
  1547. // so they're clickable in jsdom.
  1548. describe('mobile reorder arrows (#2667)', () => {
  1549. const threePending = [1, 2, 3].map((n) => ({
  1550. ...mockQueueItems[0],
  1551. id: n,
  1552. archive_id: n,
  1553. position: n,
  1554. status: 'pending',
  1555. archive_name: `Pending ${n}`,
  1556. }));
  1557. it('renders Move Up / Move Down controls for pending items', async () => {
  1558. server.use(http.get('/api/v1/queue/', () => HttpResponse.json(threePending)));
  1559. render(<QueuePage />);
  1560. await waitFor(() => expect(screen.getByText('Pending 1')).toBeInTheDocument());
  1561. // One pair per pending row.
  1562. expect(screen.getAllByTitle('Move Up')).toHaveLength(3);
  1563. expect(screen.getAllByTitle('Move Down')).toHaveLength(3);
  1564. });
  1565. it('moving the first item down persists the swapped order', async () => {
  1566. let reorderBody: { items: { id: number; position: number }[] } | null = null;
  1567. server.use(
  1568. http.get('/api/v1/queue/', () => HttpResponse.json(threePending)),
  1569. http.post('/api/v1/queue/reorder', async ({ request }) => {
  1570. reorderBody = (await request.json()) as typeof reorderBody;
  1571. return HttpResponse.json({ message: 'ok' });
  1572. }),
  1573. );
  1574. render(<QueuePage />);
  1575. await waitFor(() => expect(screen.getByText('Pending 1')).toBeInTheDocument());
  1576. // First row's "Move Down": item 1 drops below item 2 → [2, 1, 3].
  1577. await userEvent.click(screen.getAllByTitle('Move Down')[0]);
  1578. await waitFor(() => expect(reorderBody).not.toBeNull());
  1579. expect(reorderBody!.items).toEqual([
  1580. { id: 2, position: 1 },
  1581. { id: 1, position: 2 },
  1582. { id: 3, position: 3 },
  1583. ]);
  1584. });
  1585. it('disables Move Up on the first row and Move Down on the last', async () => {
  1586. server.use(http.get('/api/v1/queue/', () => HttpResponse.json(threePending)));
  1587. render(<QueuePage />);
  1588. await waitFor(() => expect(screen.getByText('Pending 1')).toBeInTheDocument());
  1589. // Rows render top-to-bottom in position order.
  1590. expect(screen.getAllByTitle('Move Up')[0]).toBeDisabled();
  1591. expect(screen.getAllByTitle('Move Down')[2]).toBeDisabled();
  1592. });
  1593. });
  1594. describe('bulk edit G-code injection (#3058)', () => {
  1595. /**
  1596. * gcode_injection is a per-item column the single-item edit modal has
  1597. * always exposed, and PATCH /queue/bulk has always accepted — only the
  1598. * bulk dialog left it out, so turning injection on for a whole queue
  1599. * meant opening every item.
  1600. */
  1601. const settingsWithSnippets = {
  1602. auto_archive: true,
  1603. gcode_snippets: '{"X1C":{"start_gcode":"","end_gcode":"M400"}}',
  1604. };
  1605. /** Select the one pending row and open the bulk edit dialog. */
  1606. const openBulkEdit = async () => {
  1607. render(<QueuePage />);
  1608. await waitFor(() => expect(screen.getByText('Test Print 1')).toBeInTheDocument());
  1609. await userEvent.click(screen.getByText('Select All'));
  1610. await userEvent.click(await screen.findByTitle('Edit Selected'));
  1611. return screen.getByText('Edit 1 Item').closest('div')!.parentElement!;
  1612. };
  1613. it('offers the toggle when a G-code snippet is configured', async () => {
  1614. server.use(http.get('/api/v1/settings/', () => HttpResponse.json(settingsWithSnippets)));
  1615. await openBulkEdit();
  1616. expect(await screen.findByText('Inject G-code')).toBeInTheDocument();
  1617. });
  1618. it('hides the toggle when no snippet is configured', async () => {
  1619. await openBulkEdit();
  1620. // The dialog is open — other queue options are there, injection is not.
  1621. expect(screen.getByText('Staged (manual start)')).toBeInTheDocument();
  1622. expect(screen.queryByText('Inject G-code')).not.toBeInTheDocument();
  1623. });
  1624. it('sends gcode_injection with the bulk PATCH once toggled on', async () => {
  1625. let patchBody: Record<string, unknown> | null = null;
  1626. server.use(
  1627. http.get('/api/v1/settings/', () => HttpResponse.json(settingsWithSnippets)),
  1628. http.patch('/api/v1/queue/bulk', async ({ request }) => {
  1629. patchBody = (await request.json()) as Record<string, unknown>;
  1630. return HttpResponse.json({ updated_count: 1, skipped_count: 0, message: 'Updated 1 items' });
  1631. }),
  1632. );
  1633. const dialog = await openBulkEdit();
  1634. const row = (await screen.findByText('Inject G-code')).closest('div')!;
  1635. await userEvent.click(within(row).getByText('On'));
  1636. await userEvent.click(within(dialog).getByText('Apply Changes'));
  1637. await waitFor(() => expect(patchBody).not.toBeNull());
  1638. expect(patchBody!.item_ids).toEqual([1]);
  1639. expect(patchBody!.gcode_injection).toBe(true);
  1640. });
  1641. it('leaves gcode_injection out of the PATCH while it stays on "no change"', async () => {
  1642. let patchBody: Record<string, unknown> | null = null;
  1643. server.use(
  1644. http.get('/api/v1/settings/', () => HttpResponse.json(settingsWithSnippets)),
  1645. http.patch('/api/v1/queue/bulk', async ({ request }) => {
  1646. patchBody = (await request.json()) as Record<string, unknown>;
  1647. return HttpResponse.json({ updated_count: 1, skipped_count: 0, message: 'Updated 1 items' });
  1648. }),
  1649. );
  1650. const dialog = await openBulkEdit();
  1651. // Change something else entirely; injection must not ride along as false.
  1652. const stagedRow = screen.getByText('Staged (manual start)').closest('div')!;
  1653. await userEvent.click(within(stagedRow).getByText('On'));
  1654. await userEvent.click(within(dialog).getByText('Apply Changes'));
  1655. await waitFor(() => expect(patchBody).not.toBeNull());
  1656. expect(patchBody!.manual_start).toBe(true);
  1657. expect('gcode_injection' in patchBody!).toBe(false);
  1658. });
  1659. });
  1660. describe('bulk edit ask-for-outcome (#1898)', () => {
  1661. const openBulkEdit = async () => {
  1662. render(<QueuePage />);
  1663. await waitFor(() => expect(screen.getByText('Test Print 1')).toBeInTheDocument());
  1664. await userEvent.click(screen.getByText('Select All'));
  1665. await userEvent.click(await screen.findByTitle('Edit Selected'));
  1666. return screen.getByText('Edit 1 Item').closest('div')!.parentElement!;
  1667. };
  1668. const captureBulkPatch = () => {
  1669. const captured: { body: Record<string, unknown> | null } = { body: null };
  1670. server.use(
  1671. http.patch('/api/v1/queue/bulk', async ({ request }) => {
  1672. captured.body = (await request.json()) as Record<string, unknown>;
  1673. return HttpResponse.json({ updated_count: 1, skipped_count: 0, message: 'Updated 1 items' });
  1674. }),
  1675. );
  1676. return captured;
  1677. };
  1678. it('sends confirm_outcome with the bulk PATCH once toggled', async () => {
  1679. const captured = captureBulkPatch();
  1680. const dialog = await openBulkEdit();
  1681. const row = within(dialog).getByText('Ask for outcome').closest('div')!;
  1682. await userEvent.click(within(row).getByText('On'));
  1683. await userEvent.click(within(dialog).getByText('Apply Changes'));
  1684. await waitFor(() => expect(captured.body).not.toBeNull());
  1685. expect(captured.body!.item_ids).toEqual([1]);
  1686. expect(captured.body!.confirm_outcome).toBe(true);
  1687. });
  1688. it('leaves confirm_outcome out of the PATCH while it stays on "no change"', async () => {
  1689. const captured = captureBulkPatch();
  1690. const dialog = await openBulkEdit();
  1691. const timelapseRow = within(dialog).getByText('Timelapse').closest('div')!;
  1692. await userEvent.click(within(timelapseRow).getByText('On'));
  1693. await userEvent.click(within(dialog).getByText('Apply Changes'));
  1694. await waitFor(() => expect(captured.body).not.toBeNull());
  1695. expect(captured.body!.timelapse).toBe(true);
  1696. expect('confirm_outcome' in captured.body!).toBe(false);
  1697. });
  1698. });
  1699. });