QueuePage.test.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /**
  2. * Tests for the QueuePage component.
  3. */
  4. import { describe, it, expect, beforeEach, 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. // Mock queue data
  12. const mockQueueItems = [
  13. {
  14. id: 1,
  15. printer_id: 1,
  16. archive_id: 1,
  17. position: 1,
  18. status: 'pending',
  19. scheduled_time: null,
  20. require_previous_success: false,
  21. auto_off_after: false,
  22. manual_start: false,
  23. ams_mapping: null,
  24. plate_id: null,
  25. bed_levelling: 'on',
  26. flow_cali: 'off',
  27. vibration_cali: true,
  28. layer_inspect: false,
  29. timelapse: false,
  30. use_ams: true,
  31. started_at: null,
  32. completed_at: null,
  33. error_message: null,
  34. created_at: '2024-01-01T00:00:00Z',
  35. archive_name: 'Test Print 1',
  36. archive_thumbnail: '/thumb1.png',
  37. printer_name: 'Test Printer',
  38. print_time_seconds: 3600,
  39. },
  40. {
  41. id: 2,
  42. printer_id: 1,
  43. archive_id: 2,
  44. position: 2,
  45. status: 'printing',
  46. scheduled_time: null,
  47. require_previous_success: false,
  48. auto_off_after: true,
  49. manual_start: false,
  50. ams_mapping: null,
  51. plate_id: null,
  52. bed_levelling: 'on',
  53. flow_cali: 'off',
  54. vibration_cali: true,
  55. layer_inspect: false,
  56. timelapse: false,
  57. use_ams: true,
  58. started_at: '2024-01-01T10:00:00Z',
  59. completed_at: null,
  60. error_message: null,
  61. created_at: '2024-01-01T00:00:00Z',
  62. archive_name: 'Active Print',
  63. archive_thumbnail: '/thumb2.png',
  64. printer_name: 'Test Printer',
  65. print_time_seconds: 7200,
  66. },
  67. {
  68. id: 3,
  69. printer_id: 1,
  70. archive_id: 3,
  71. position: 3,
  72. status: 'completed',
  73. scheduled_time: null,
  74. require_previous_success: false,
  75. auto_off_after: false,
  76. manual_start: false,
  77. ams_mapping: null,
  78. plate_id: null,
  79. bed_levelling: 'on',
  80. flow_cali: 'off',
  81. vibration_cali: true,
  82. layer_inspect: false,
  83. timelapse: false,
  84. use_ams: true,
  85. started_at: '2024-01-01T08:00:00Z',
  86. completed_at: '2024-01-01T09:00:00Z',
  87. error_message: null,
  88. created_at: '2024-01-01T00:00:00Z',
  89. archive_name: 'Completed Print',
  90. archive_thumbnail: '/thumb3.png',
  91. printer_name: 'Test Printer',
  92. print_time_seconds: 1800,
  93. },
  94. ];
  95. const mockPrinters = [
  96. {
  97. id: 1,
  98. name: 'Test Printer',
  99. ip_address: '192.168.1.100',
  100. serial_number: 'TESTSERIAL0001',
  101. access_code: '12345678',
  102. model: 'X1C',
  103. enabled: true,
  104. created_at: '2024-01-01T00:00:00Z',
  105. },
  106. ];
  107. describe('QueuePage', () => {
  108. beforeEach(() => {
  109. // Mock localStorage.getItem to return expected defaults for queue page
  110. vi.mocked(localStorage.getItem).mockImplementation((key: string) => {
  111. if (key === 'queue.historyCollapsed') return 'false'; // expanded
  112. if (key === 'queue.viewMode') return 'list';
  113. return null;
  114. });
  115. // Setup MSW handlers for this test
  116. server.use(
  117. http.get('/api/v1/queue/', () => {
  118. return HttpResponse.json(mockQueueItems);
  119. }),
  120. http.get('/api/v1/printers/', () => {
  121. return HttpResponse.json(mockPrinters);
  122. }),
  123. http.delete('/api/v1/queue/:id', () => {
  124. return HttpResponse.json({ success: true });
  125. }),
  126. http.post('/api/v1/queue/:id/cancel', () => {
  127. return HttpResponse.json({ success: true });
  128. }),
  129. http.post('/api/v1/queue/:id/start', () => {
  130. return HttpResponse.json({ success: true });
  131. }),
  132. http.post('/api/v1/queue/:id/stop', () => {
  133. return HttpResponse.json({ success: true });
  134. }),
  135. http.post('/api/v1/queue/reorder', () => {
  136. return HttpResponse.json({ success: true });
  137. })
  138. );
  139. });
  140. describe('rendering', () => {
  141. it('renders the page title', async () => {
  142. render(<QueuePage />);
  143. await waitFor(() => {
  144. expect(screen.getByText('Print Queue')).toBeInTheDocument();
  145. });
  146. });
  147. it('renders the page description', async () => {
  148. render(<QueuePage />);
  149. await waitFor(() => {
  150. expect(screen.getByText('Schedule and manage your print jobs')).toBeInTheDocument();
  151. });
  152. });
  153. it('shows summary cards', async () => {
  154. render(<QueuePage />);
  155. await waitFor(() => {
  156. // Check for the page title (Print Queue is the h1)
  157. expect(screen.getByText('Print Queue')).toBeInTheDocument();
  158. });
  159. });
  160. it('shows filter dropdowns', async () => {
  161. render(<QueuePage />);
  162. await waitFor(() => {
  163. expect(screen.getByText('All Printers')).toBeInTheDocument();
  164. expect(screen.getByText('All Status')).toBeInTheDocument();
  165. });
  166. });
  167. });
  168. describe('queue items display', () => {
  169. it('shows pending queue items', async () => {
  170. render(<QueuePage />);
  171. await waitFor(() => {
  172. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  173. });
  174. });
  175. it('shows active printing items', async () => {
  176. render(<QueuePage />);
  177. await waitFor(() => {
  178. expect(screen.getByText('Active Print')).toBeInTheDocument();
  179. expect(screen.getByText('Currently Printing')).toBeInTheDocument();
  180. });
  181. });
  182. it('shows one if-started-now ETA for an eligible pending item', async () => {
  183. // Printer 1 is free: nothing is printing on it and nothing is queued ahead.
  184. server.use(
  185. http.get('/api/v1/queue/', () => {
  186. return HttpResponse.json([mockQueueItems[0]]);
  187. }),
  188. );
  189. render(<QueuePage />);
  190. const name = await screen.findByText('Test Print 1');
  191. const row = name.closest('.group');
  192. expect(row).not.toBeNull();
  193. const etaEl = within(row as HTMLElement).getAllByTestId('queue-item-eta');
  194. expect(etaEl).toHaveLength(1);
  195. // The tooltip must say what the number actually means, not reuse the
  196. // printers-page "Estimated completion time" wording (which this is not).
  197. expect(etaEl[0]).toHaveAttribute(
  198. 'title',
  199. 'Completion time if this job started now',
  200. );
  201. });
  202. // The scheduler only writes waiting_reason on the model-based assignment
  203. // path, so an item pinned to a specific printer carries no marker at all
  204. // while it sits behind a running job. Without the printer-busy check every
  205. // one of these quoted the same wrong "starts now" time.
  206. it('does not show an ETA for an item pinned behind a running print', async () => {
  207. render(<QueuePage />);
  208. // mockQueueItems[1] ("Active Print") is printing on printer 1, and
  209. // "Test Print 1" is pending on the same printer with waiting_reason null.
  210. const name = await screen.findByText('Test Print 1');
  211. const row = name.closest('.group');
  212. expect(row).not.toBeNull();
  213. expect(
  214. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  215. ).not.toBeInTheDocument();
  216. });
  217. it('shows the ETA only on the next item up when several share an idle printer', async () => {
  218. server.use(
  219. http.get('/api/v1/queue/', () => {
  220. return HttpResponse.json([
  221. { ...mockQueueItems[0], id: 10, position: 1, archive_name: 'First up' },
  222. { ...mockQueueItems[0], id: 11, position: 2, archive_name: 'Second up' },
  223. { ...mockQueueItems[0], id: 12, position: 3, archive_name: 'Third up' },
  224. ]);
  225. }),
  226. );
  227. render(<QueuePage />);
  228. await screen.findByText('Third up');
  229. const etaRowNames = screen
  230. .queryAllByTestId('queue-item-eta')
  231. .map((el) => el.closest('.group')?.textContent);
  232. expect(etaRowNames).toHaveLength(1);
  233. expect(etaRowNames[0]).toContain('First up');
  234. });
  235. it('shows an ETA for a staged item queued behind others on an idle printer', async () => {
  236. // The scheduler skips manual-start items without claiming the printer, so
  237. // a staged job is startable whenever its printer is free — queue order
  238. // does not gate it.
  239. server.use(
  240. http.get('/api/v1/queue/', () => {
  241. return HttpResponse.json([
  242. { ...mockQueueItems[0], id: 20, position: 1, archive_name: 'Auto first' },
  243. {
  244. ...mockQueueItems[0],
  245. id: 21,
  246. position: 2,
  247. archive_name: 'Staged second',
  248. manual_start: true,
  249. },
  250. ]);
  251. }),
  252. );
  253. render(<QueuePage />);
  254. const name = await screen.findByText('Staged second');
  255. const row = name.closest('.group');
  256. expect(row).not.toBeNull();
  257. expect(
  258. within(row as HTMLElement).getAllByTestId('queue-item-eta'),
  259. ).toHaveLength(1);
  260. });
  261. it('does not show an ETA for an item conditional on a previous print', async () => {
  262. server.use(
  263. http.get('/api/v1/queue/', () => {
  264. return HttpResponse.json([
  265. {
  266. ...mockQueueItems[0],
  267. archive_name: 'Conditional Print',
  268. require_previous_success: true,
  269. },
  270. ]);
  271. }),
  272. );
  273. render(<QueuePage />);
  274. const name = await screen.findByText('Conditional Print');
  275. const row = name.closest('.group');
  276. expect(row).not.toBeNull();
  277. expect(
  278. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  279. ).not.toBeInTheDocument();
  280. });
  281. it('advances the ETA as time passes', async () => {
  282. vi.useFakeTimers({ shouldAdvanceTime: true });
  283. vi.setSystemTime(new Date('2026-08-02T10:00:00Z'));
  284. try {
  285. server.use(
  286. http.get('/api/v1/queue/', () => {
  287. return HttpResponse.json([mockQueueItems[0]]);
  288. }),
  289. );
  290. render(<QueuePage />);
  291. const name = await screen.findByText('Test Print 1');
  292. const row = name.closest('.group') as HTMLElement;
  293. const before = within(row).getByTestId('queue-item-eta').textContent;
  294. // The queue payload never changes, so react-query hands back the same
  295. // object and nothing here re-renders on its own. Only the page's own
  296. // clock can move this value.
  297. await vi.advanceTimersByTimeAsync(45 * 60 * 1000);
  298. await waitFor(() => {
  299. expect(
  300. within(row).getByTestId('queue-item-eta').textContent,
  301. ).not.toBe(before);
  302. });
  303. } finally {
  304. vi.useRealTimers();
  305. }
  306. });
  307. it('shows one if-started-now ETA for a staged item', async () => {
  308. server.use(
  309. http.get('/api/v1/queue/', () => {
  310. return HttpResponse.json([
  311. {
  312. ...mockQueueItems[0],
  313. archive_name: 'Staged Print',
  314. manual_start: true,
  315. },
  316. ]);
  317. }),
  318. );
  319. render(<QueuePage />);
  320. const name = await screen.findByText('Staged Print');
  321. const row = name.closest('.group');
  322. expect(row).not.toBeNull();
  323. expect(
  324. within(row as HTMLElement).getAllByTestId('queue-item-eta'),
  325. ).toHaveLength(1);
  326. });
  327. it('shows exactly one live ETA for a printing item', async () => {
  328. server.use(
  329. http.get('/api/v1/printers/:id/status', ({ params }) => {
  330. return HttpResponse.json({
  331. id: Number(params.id),
  332. name: 'Test Printer',
  333. connected: true,
  334. state: 'RUNNING',
  335. progress: 50,
  336. remaining_time: 60,
  337. layer_num: 50,
  338. total_layers: 100,
  339. filename: 'active.3mf',
  340. });
  341. }),
  342. );
  343. render(<QueuePage />);
  344. const name = await screen.findByText('Active Print');
  345. const row = name.closest('.group');
  346. expect(row).not.toBeNull();
  347. await waitFor(() => {
  348. expect(
  349. within(row as HTMLElement).getAllByText(/^ETA\s/),
  350. ).toHaveLength(1);
  351. });
  352. expect(
  353. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  354. ).not.toBeInTheDocument();
  355. });
  356. it('does not show an ETA for a waiting item', async () => {
  357. server.use(
  358. http.get('/api/v1/queue/', () => {
  359. return HttpResponse.json([
  360. {
  361. ...mockQueueItems[0],
  362. archive_name: 'Waiting Print',
  363. waiting_reason: 'Waiting for matching printer',
  364. },
  365. ]);
  366. }),
  367. );
  368. render(<QueuePage />);
  369. const name = await screen.findByText('Waiting Print');
  370. const row = name.closest('.group');
  371. expect(row).not.toBeNull();
  372. expect(
  373. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  374. ).not.toBeInTheDocument();
  375. });
  376. it('does not show an if-started-now ETA for a scheduled item', async () => {
  377. server.use(
  378. http.get('/api/v1/queue/', () => {
  379. return HttpResponse.json([
  380. {
  381. ...mockQueueItems[0],
  382. archive_name: 'Scheduled Print',
  383. scheduled_time: new Date(
  384. Date.now() + 5 * 60 * 60 * 1000,
  385. ).toISOString(),
  386. },
  387. ]);
  388. }),
  389. );
  390. render(<QueuePage />);
  391. const name = await screen.findByText('Scheduled Print');
  392. const row = name.closest('.group');
  393. expect(row).not.toBeNull();
  394. expect(
  395. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  396. ).not.toBeInTheDocument();
  397. });
  398. it('tells an unscheduled item when it runs, without naming a dispatch mode', async () => {
  399. // ASAP and Queue differ only in where the item is inserted; neither is
  400. // stored on it, so this column cannot tell them apart. Labelling every
  401. // unscheduled item "ASAP" made a Queue choice look overridden, which is
  402. // what both #2557 and #3018 opened on.
  403. server.use(
  404. http.get('/api/v1/queue/', () => {
  405. return HttpResponse.json([
  406. { ...mockQueueItems[0], archive_name: 'Queued Print', scheduled_time: null },
  407. ]);
  408. }),
  409. );
  410. render(<QueuePage />);
  411. const name = await screen.findByText('Queued Print');
  412. const row = name.closest('.group') as HTMLElement;
  413. expect(row).not.toBeNull();
  414. expect(within(row).getByText('When a printer is free')).toBeInTheDocument();
  415. expect(within(row).queryByText('ASAP')).not.toBeInTheDocument();
  416. });
  417. it('does not render a dangling ETA for an invalid duration', async () => {
  418. server.use(
  419. http.get('/api/v1/queue/', () => {
  420. return HttpResponse.json([
  421. {
  422. ...mockQueueItems[0],
  423. archive_name: 'Invalid Duration Print',
  424. print_time_seconds: -60,
  425. },
  426. ]);
  427. }),
  428. );
  429. render(<QueuePage />);
  430. const name = await screen.findByText('Invalid Duration Print');
  431. const row = name.closest('.group');
  432. expect(row).not.toBeNull();
  433. expect(
  434. within(row as HTMLElement).queryByTestId('queue-item-eta'),
  435. ).not.toBeInTheDocument();
  436. expect(
  437. within(row as HTMLElement).queryByText(/^ETA(?:\s|$)/),
  438. ).not.toBeInTheDocument();
  439. });
  440. it('shows completed items in history', async () => {
  441. const user = userEvent.setup();
  442. render(<QueuePage />);
  443. // The History tab now owns the completed/cancelled/failed list.
  444. await user.click(await screen.findByRole('button', { name: /^History/ }));
  445. await waitFor(() => {
  446. expect(screen.getByText('Completed Print')).toBeInTheDocument();
  447. });
  448. });
  449. it('shows status badges', async () => {
  450. render(<QueuePage />);
  451. await waitFor(() => {
  452. // Queue items should be visible with status indicators
  453. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  454. });
  455. });
  456. it('shows printer names', async () => {
  457. render(<QueuePage />);
  458. await waitFor(() => {
  459. const printerElements = screen.getAllByText('Test Printer');
  460. expect(printerElements.length).toBeGreaterThan(0);
  461. });
  462. });
  463. it('renders queue items with plate_id correctly', async () => {
  464. // Override with queue items that have plate_id set
  465. server.use(
  466. http.get('/api/v1/queue/', () => {
  467. return HttpResponse.json([
  468. {
  469. ...mockQueueItems[0],
  470. plate_id: 2,
  471. archive_name: 'Multi-plate Print',
  472. },
  473. ]);
  474. })
  475. );
  476. render(<QueuePage />);
  477. await waitFor(() => {
  478. expect(screen.getByText('Multi-plate Print')).toBeInTheDocument();
  479. });
  480. });
  481. });
  482. describe('history pagination', () => {
  483. // #2682: History rendered the full count in the header but only ever drew
  484. // the first 50 rows, with no way to reach the rest. It now paginates with
  485. // a "Show more" control.
  486. const manyHistory = Array.from({ length: 60 }, (_, i) => ({
  487. ...mockQueueItems[2],
  488. id: 100 + i,
  489. batch_id: null,
  490. archive_name: `History Item ${String(i).padStart(2, '0')}`,
  491. // Descending completed_at so index 0 is newest and sorts first; the
  492. // default History sort is by date, newest first.
  493. completed_at: new Date(Date.UTC(2024, 0, 1, 0, 0, 0) - i * 60000).toISOString(),
  494. }));
  495. beforeEach(() => {
  496. server.use(
  497. http.get('/api/v1/queue/', () => {
  498. return HttpResponse.json(manyHistory);
  499. })
  500. );
  501. });
  502. it('caps the History list at one page and reveals the rest on Show more', async () => {
  503. const user = userEvent.setup();
  504. render(<QueuePage />);
  505. await user.click(await screen.findByRole('button', { name: /^History/ }));
  506. // First page is drawn; an item past the 50-row cap is not.
  507. await waitFor(() => {
  508. expect(screen.getByText('History Item 00')).toBeInTheDocument();
  509. });
  510. expect(screen.queryByText('History Item 59')).not.toBeInTheDocument();
  511. expect(screen.getByText('Showing 50 of 60')).toBeInTheDocument();
  512. // Show more reveals the remainder and then disappears (nothing left).
  513. await user.click(screen.getByRole('button', { name: /show more/i }));
  514. await waitFor(() => {
  515. expect(screen.getByText('History Item 59')).toBeInTheDocument();
  516. });
  517. expect(screen.queryByRole('button', { name: /show more/i })).not.toBeInTheDocument();
  518. });
  519. });
  520. describe('empty state', () => {
  521. it('shows empty state when no queue items', async () => {
  522. server.use(
  523. http.get('/api/v1/queue/', () => {
  524. return HttpResponse.json([]);
  525. })
  526. );
  527. render(<QueuePage />);
  528. await waitFor(() => {
  529. expect(screen.getByText('No prints scheduled')).toBeInTheDocument();
  530. });
  531. });
  532. });
  533. describe('filtering', () => {
  534. it('has printer filter options', async () => {
  535. const user = userEvent.setup();
  536. render(<QueuePage />);
  537. await waitFor(() => {
  538. expect(screen.getByText('All Printers')).toBeInTheDocument();
  539. });
  540. const printerSelect = screen.getByDisplayValue('All Printers');
  541. await user.click(printerSelect);
  542. expect(screen.getByText('Unassigned')).toBeInTheDocument();
  543. });
  544. it('has status filter options', async () => {
  545. const user = userEvent.setup();
  546. render(<QueuePage />);
  547. await waitFor(() => {
  548. expect(screen.getByText('All Status')).toBeInTheDocument();
  549. });
  550. const statusSelect = screen.getByDisplayValue('All Status');
  551. await user.click(statusSelect);
  552. expect(screen.getByRole('option', { name: 'Pending' })).toBeInTheDocument();
  553. expect(screen.getByRole('option', { name: 'Printing' })).toBeInTheDocument();
  554. expect(screen.getByRole('option', { name: 'Completed' })).toBeInTheDocument();
  555. });
  556. });
  557. describe('queue actions', () => {
  558. it('shows edit button for pending items', async () => {
  559. render(<QueuePage />);
  560. await waitFor(() => {
  561. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  562. });
  563. // Find the edit button (Pencil icon)
  564. const editButtons = screen.getAllByTitle('Edit');
  565. expect(editButtons.length).toBeGreaterThan(0);
  566. });
  567. it('shows cancel button for pending items', async () => {
  568. render(<QueuePage />);
  569. await waitFor(() => {
  570. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  571. });
  572. const cancelButtons = screen.getAllByTitle('Cancel');
  573. expect(cancelButtons.length).toBeGreaterThan(0);
  574. });
  575. it('shows stop button for printing items', async () => {
  576. render(<QueuePage />);
  577. await waitFor(() => {
  578. expect(screen.getByText('Active Print')).toBeInTheDocument();
  579. });
  580. const stopButtons = screen.getAllByTitle('Stop Print');
  581. expect(stopButtons.length).toBeGreaterThan(0);
  582. });
  583. it('shows re-queue button for history items', async () => {
  584. const user = userEvent.setup();
  585. render(<QueuePage />);
  586. await user.click(await screen.findByRole('button', { name: /^History/ }));
  587. await waitFor(() => {
  588. expect(screen.getByText('Completed Print')).toBeInTheDocument();
  589. });
  590. const requeueButtons = screen.getAllByTitle('Re-queue');
  591. expect(requeueButtons.length).toBeGreaterThan(0);
  592. });
  593. });
  594. describe('clear history', () => {
  595. it('shows clear history button when history exists', async () => {
  596. const user = userEvent.setup();
  597. render(<QueuePage />);
  598. // Clear History only renders inside the History tab now.
  599. await user.click(await screen.findByRole('button', { name: /^History/ }));
  600. await waitFor(() => {
  601. expect(screen.getByText('Clear History')).toBeInTheDocument();
  602. });
  603. });
  604. it('opens confirm modal when clicking clear history', async () => {
  605. const user = userEvent.setup();
  606. render(<QueuePage />);
  607. await user.click(await screen.findByRole('button', { name: /^History/ }));
  608. await waitFor(() => {
  609. expect(screen.getByText('Clear History')).toBeInTheDocument();
  610. });
  611. const clearButton = screen.getByRole('button', { name: /clear history/i });
  612. await user.click(clearButton);
  613. await waitFor(() => {
  614. expect(screen.getByText(/Are you sure you want to remove all/i)).toBeInTheDocument();
  615. });
  616. });
  617. });
  618. describe('staged items', () => {
  619. it('shows staged badge for manual_start items', async () => {
  620. server.use(
  621. http.get('/api/v1/queue/', () => {
  622. return HttpResponse.json([
  623. {
  624. ...mockQueueItems[0],
  625. manual_start: true,
  626. },
  627. ]);
  628. })
  629. );
  630. render(<QueuePage />);
  631. await waitFor(() => {
  632. expect(screen.getByText('Staged')).toBeInTheDocument();
  633. });
  634. });
  635. it('shows start button for staged items', async () => {
  636. server.use(
  637. http.get('/api/v1/queue/', () => {
  638. return HttpResponse.json([
  639. {
  640. ...mockQueueItems[0],
  641. manual_start: true,
  642. },
  643. ]);
  644. })
  645. );
  646. render(<QueuePage />);
  647. await waitFor(() => {
  648. expect(screen.getByTitle('Start Print')).toBeInTheDocument();
  649. });
  650. });
  651. });
  652. describe('auto power off badge', () => {
  653. it('shows power off badge when auto_off_after is true', async () => {
  654. render(<QueuePage />);
  655. await waitFor(() => {
  656. expect(screen.getByText('Auto power off')).toBeInTheDocument();
  657. });
  658. });
  659. });
  660. describe('gcode injection badge', () => {
  661. it('shows G-code badge when gcode_injection is true', async () => {
  662. const itemsWithGcode = mockQueueItems.map((item, i) =>
  663. i === 0 ? { ...item, gcode_injection: true } : item
  664. );
  665. server.use(
  666. http.get('/api/v1/queue/', () => HttpResponse.json(itemsWithGcode)),
  667. );
  668. render(<QueuePage />);
  669. await waitFor(() => {
  670. expect(screen.getByText('G-code')).toBeInTheDocument();
  671. });
  672. });
  673. it('does not show G-code badge when gcode_injection is false', async () => {
  674. render(<QueuePage />);
  675. await waitFor(() => {
  676. expect(screen.getByText('Test Print 1')).toBeInTheDocument();
  677. });
  678. expect(screen.queryByText('G-code')).not.toBeInTheDocument();
  679. });
  680. });
  681. describe('filament-short ▶ flow (#1496)', () => {
  682. /**
  683. * The dispatch pre-flight flags a queue item as filament_short. The user
  684. * clicks ▶, the backend re-checks live and either dispatches (no deficit
  685. * anymore — clear flag) or returns 409 with the per-slot deficit so the
  686. * frontend can render the "Print Anyway" confirm modal.
  687. */
  688. const shortItem = {
  689. ...mockQueueItems[0],
  690. manual_start: true,
  691. filament_short: true,
  692. };
  693. it('renders the filament-short badge on a flagged pending row', async () => {
  694. server.use(
  695. http.get('/api/v1/queue/', () => HttpResponse.json([shortItem])),
  696. );
  697. render(<QueuePage />);
  698. await waitFor(() => {
  699. expect(screen.getByText(/Insufficient filament for the assigned spool/i)).toBeInTheDocument();
  700. });
  701. });
  702. it('opens the Print Anyway modal when ▶ returns 409 and retries with skip_filament_check', async () => {
  703. let secondCallSkippedCheck: boolean | null = null;
  704. let attempts = 0;
  705. server.use(
  706. http.get('/api/v1/queue/', () => HttpResponse.json([shortItem])),
  707. http.post('/api/v1/queue/:id/start', ({ request }) => {
  708. attempts += 1;
  709. const url = new URL(request.url);
  710. const skip = url.searchParams.get('skip_filament_check') === 'true';
  711. if (attempts === 1) {
  712. return HttpResponse.json(
  713. {
  714. detail: {
  715. code: 'insufficient_filament',
  716. deficit: [
  717. {
  718. slot_id: 1,
  719. ams_id: 0,
  720. tray_id: 0,
  721. filament_type: 'PLA',
  722. required_grams: 270,
  723. remaining_grams: 200,
  724. },
  725. ],
  726. },
  727. },
  728. { status: 409 },
  729. );
  730. }
  731. secondCallSkippedCheck = skip;
  732. return HttpResponse.json({ ...shortItem, manual_start: false, filament_short: false });
  733. }),
  734. );
  735. render(<QueuePage />);
  736. const playButton = await screen.findByTitle(/Start Print|do not have permission to start prints/i);
  737. await userEvent.click(playButton);
  738. // Wait for the start endpoint to be hit (the 409 path returns to onError).
  739. await waitFor(() => expect(attempts).toBe(1));
  740. // Modal shows the deficit detail
  741. await screen.findByRole('button', { name: /Print Anyway/i });
  742. expect(
  743. screen.getByText(/Slot 1: needs 270 g, 200 g remaining/i),
  744. ).toBeInTheDocument();
  745. await userEvent.click(screen.getByRole('button', { name: /Print Anyway/i }));
  746. await waitFor(() => expect(secondCallSkippedCheck).toBe(true));
  747. expect(attempts).toBe(2);
  748. });
  749. });
  750. // #2667: mobile can't drag-reorder the queue, so pending rows get up/down
  751. // arrows. They persist via the same POST /queue/reorder as drag. The
  752. // buttons live in the DOM at every width (Tailwind `sm:hidden` is CSS-only),
  753. // so they're clickable in jsdom.
  754. describe('mobile reorder arrows (#2667)', () => {
  755. const threePending = [1, 2, 3].map((n) => ({
  756. ...mockQueueItems[0],
  757. id: n,
  758. archive_id: n,
  759. position: n,
  760. status: 'pending',
  761. archive_name: `Pending ${n}`,
  762. }));
  763. it('renders Move Up / Move Down controls for pending items', async () => {
  764. server.use(http.get('/api/v1/queue/', () => HttpResponse.json(threePending)));
  765. render(<QueuePage />);
  766. await waitFor(() => expect(screen.getByText('Pending 1')).toBeInTheDocument());
  767. // One pair per pending row.
  768. expect(screen.getAllByTitle('Move Up')).toHaveLength(3);
  769. expect(screen.getAllByTitle('Move Down')).toHaveLength(3);
  770. });
  771. it('moving the first item down persists the swapped order', async () => {
  772. let reorderBody: { items: { id: number; position: number }[] } | null = null;
  773. server.use(
  774. http.get('/api/v1/queue/', () => HttpResponse.json(threePending)),
  775. http.post('/api/v1/queue/reorder', async ({ request }) => {
  776. reorderBody = (await request.json()) as typeof reorderBody;
  777. return HttpResponse.json({ message: 'ok' });
  778. }),
  779. );
  780. render(<QueuePage />);
  781. await waitFor(() => expect(screen.getByText('Pending 1')).toBeInTheDocument());
  782. // First row's "Move Down": item 1 drops below item 2 → [2, 1, 3].
  783. await userEvent.click(screen.getAllByTitle('Move Down')[0]);
  784. await waitFor(() => expect(reorderBody).not.toBeNull());
  785. expect(reorderBody!.items).toEqual([
  786. { id: 2, position: 1 },
  787. { id: 1, position: 2 },
  788. { id: 3, position: 3 },
  789. ]);
  790. });
  791. it('disables Move Up on the first row and Move Down on the last', async () => {
  792. server.use(http.get('/api/v1/queue/', () => HttpResponse.json(threePending)));
  793. render(<QueuePage />);
  794. await waitFor(() => expect(screen.getByText('Pending 1')).toBeInTheDocument());
  795. // Rows render top-to-bottom in position order.
  796. expect(screen.getAllByTitle('Move Up')[0]).toBeDisabled();
  797. expect(screen.getAllByTitle('Move Down')[2]).toBeDisabled();
  798. });
  799. });
  800. });