EditArchiveModal.test.tsx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /**
  2. * Tests for the EditArchiveModal component.
  3. */
  4. import { describe, it, expect, vi, beforeEach } from 'vitest';
  5. import { screen, waitFor } from '@testing-library/react';
  6. import userEvent from '@testing-library/user-event';
  7. import { render } from '../utils';
  8. import { EditArchiveModal } from '../../components/EditArchiveModal';
  9. import { http, HttpResponse } from 'msw';
  10. import { server } from '../mocks/server';
  11. const mockArchive = {
  12. id: 1,
  13. filename: 'benchy.gcode.3mf',
  14. print_name: 'Benchy',
  15. printer_id: 1,
  16. printer_name: 'X1 Carbon',
  17. notes: 'Test notes',
  18. rating: 4,
  19. project_id: null,
  20. tags: 'test,calibration',
  21. };
  22. const mockProjects = [
  23. { id: 1, name: 'Functional Parts', color: '#00ae42' },
  24. { id: 2, name: 'Art', color: '#ff5500' },
  25. ];
  26. describe('EditArchiveModal', () => {
  27. const mockOnClose = vi.fn();
  28. const mockOnSave = vi.fn();
  29. beforeEach(() => {
  30. vi.clearAllMocks();
  31. server.use(
  32. http.get('/api/v1/projects/', () => {
  33. return HttpResponse.json(mockProjects);
  34. }),
  35. http.get('/api/v1/archives/tags', () => {
  36. return HttpResponse.json([
  37. { name: 'test', count: 2 },
  38. { name: 'calibration', count: 1 },
  39. { name: 'functional', count: 3 },
  40. ]);
  41. }),
  42. http.patch('/api/v1/archives/:id', async ({ request }) => {
  43. const body = await request.json();
  44. return HttpResponse.json({ ...mockArchive, ...body });
  45. })
  46. );
  47. });
  48. describe('rendering', () => {
  49. it('renders the modal title', () => {
  50. render(
  51. <EditArchiveModal
  52. archive={mockArchive}
  53. onClose={mockOnClose}
  54. onSave={mockOnSave}
  55. />
  56. );
  57. expect(screen.getByText(/edit/i)).toBeInTheDocument();
  58. });
  59. it('shows print name field', async () => {
  60. render(
  61. <EditArchiveModal
  62. archive={mockArchive}
  63. onClose={mockOnClose}
  64. onSave={mockOnSave}
  65. />
  66. );
  67. await waitFor(() => {
  68. // Name field should be present
  69. const nameInput = screen.getByDisplayValue('Benchy');
  70. expect(nameInput).toBeInTheDocument();
  71. });
  72. });
  73. it('shows notes field', async () => {
  74. render(
  75. <EditArchiveModal
  76. archive={mockArchive}
  77. onClose={mockOnClose}
  78. onSave={mockOnSave}
  79. />
  80. );
  81. await waitFor(() => {
  82. const notesField = screen.getByDisplayValue('Test notes');
  83. expect(notesField).toBeInTheDocument();
  84. });
  85. });
  86. it('shows rating selector', async () => {
  87. render(
  88. <EditArchiveModal
  89. archive={mockArchive}
  90. onClose={mockOnClose}
  91. onSave={mockOnSave}
  92. />
  93. );
  94. await waitFor(() => {
  95. // Rating may be shown as stars or dropdown
  96. expect(screen.getByText(/edit/i)).toBeInTheDocument();
  97. });
  98. });
  99. it('shows project selector', async () => {
  100. render(
  101. <EditArchiveModal
  102. archive={mockArchive}
  103. onClose={mockOnClose}
  104. onSave={mockOnSave}
  105. />
  106. );
  107. await waitFor(() => {
  108. // Project section should be present
  109. expect(screen.getByText(/edit/i)).toBeInTheDocument();
  110. });
  111. });
  112. it('shows tags input', () => {
  113. render(
  114. <EditArchiveModal
  115. archive={mockArchive}
  116. onClose={mockOnClose}
  117. onSave={mockOnSave}
  118. />
  119. );
  120. expect(screen.getByText(/tags/i)).toBeInTheDocument();
  121. });
  122. });
  123. describe('existing values', () => {
  124. it('shows existing tags', () => {
  125. render(
  126. <EditArchiveModal
  127. archive={mockArchive}
  128. onClose={mockOnClose}
  129. onSave={mockOnSave}
  130. />
  131. );
  132. expect(screen.getByText('test')).toBeInTheDocument();
  133. expect(screen.getByText('calibration')).toBeInTheDocument();
  134. });
  135. });
  136. describe('actions', () => {
  137. it('has save button', () => {
  138. render(
  139. <EditArchiveModal
  140. archive={mockArchive}
  141. onClose={mockOnClose}
  142. onSave={mockOnSave}
  143. />
  144. );
  145. expect(screen.getByRole('button', { name: /save/i })).toBeInTheDocument();
  146. });
  147. it('has cancel button', () => {
  148. render(
  149. <EditArchiveModal
  150. archive={mockArchive}
  151. onClose={mockOnClose}
  152. onSave={mockOnSave}
  153. />
  154. );
  155. expect(screen.getByRole('button', { name: /cancel/i })).toBeInTheDocument();
  156. });
  157. it('calls onClose when cancel is clicked', async () => {
  158. const user = userEvent.setup();
  159. render(
  160. <EditArchiveModal
  161. archive={mockArchive}
  162. onClose={mockOnClose}
  163. onSave={mockOnSave}
  164. />
  165. );
  166. await user.click(screen.getByRole('button', { name: /cancel/i }));
  167. expect(mockOnClose).toHaveBeenCalled();
  168. });
  169. it('can edit print name', async () => {
  170. const user = userEvent.setup();
  171. render(
  172. <EditArchiveModal
  173. archive={mockArchive}
  174. onClose={mockOnClose}
  175. onSave={mockOnSave}
  176. />
  177. );
  178. const nameInput = screen.getByDisplayValue('Benchy');
  179. await user.clear(nameInput);
  180. await user.type(nameInput, 'New Name');
  181. expect(nameInput).toHaveValue('New Name');
  182. });
  183. });
  184. describe('failure_reason vocabulary (#1687 follow-up)', () => {
  185. // The Stats page's Failure Analysis widget groups by the raw column value.
  186. // Before this fix this modal saved the translated label, so a language
  187. // switch fragmented historical buckets and any round-trip through the
  188. // new PATCH /print-log endpoint (which validates against camelCase keys)
  189. // would reject the value. The dropdown now saves the key.
  190. const failedArchive = { ...mockArchive, status: 'failed', failure_reason: 'filamentRunout' };
  191. const legacyArchive = { ...mockArchive, status: 'failed', failure_reason: 'Filament runout' };
  192. it('preselects the option when the stored value is already a camelCase key', () => {
  193. render(<EditArchiveModal archive={failedArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  194. const select = screen.getByLabelText(/failure reason/i) as HTMLSelectElement;
  195. expect(select.value).toBe('filamentRunout');
  196. });
  197. it('reverse-looks-up a legacy translated value back to its key', () => {
  198. render(<EditArchiveModal archive={legacyArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  199. const select = screen.getByLabelText(/failure reason/i) as HTMLSelectElement;
  200. expect(select.value).toBe('filamentRunout');
  201. });
  202. it('sends the camelCase key on save, not the translated label', async () => {
  203. const user = userEvent.setup();
  204. let patched: { failure_reason?: string } | undefined;
  205. server.use(
  206. http.patch('/api/v1/archives/:id', async ({ request }) => {
  207. patched = (await request.json()) as { failure_reason?: string };
  208. return HttpResponse.json({ ...failedArchive, ...patched });
  209. }),
  210. );
  211. render(<EditArchiveModal archive={failedArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  212. const select = screen.getByLabelText(/failure reason/i);
  213. await user.selectOptions(select, 'cloggedNozzle');
  214. await user.click(screen.getByRole('button', { name: /save/i }));
  215. await waitFor(() => {
  216. expect(patched?.failure_reason).toBe('cloggedNozzle');
  217. });
  218. });
  219. // A value outside the vocabulary used to initialise the dropdown to '',
  220. // and saving from that state wrote the empty selection over the stored
  221. // text -- opening the editor and pressing Save destroyed the
  222. // classification. The startup migration folds every known spelling onto a
  223. // key, so what reaches here is genuinely unrecognisable text; it has to
  224. // survive rather than be silently discarded (issue #2974).
  225. const freeTextArchive = {
  226. ...mockArchive,
  227. status: 'failed',
  228. failure_reason: 'Custom legacy reason',
  229. };
  230. it('keeps a stored value it cannot map, as its own option', () => {
  231. render(<EditArchiveModal archive={freeTextArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  232. const select = screen.getByLabelText(/failure reason/i) as HTMLSelectElement;
  233. expect(select.value).toBe('Custom legacy reason');
  234. expect(
  235. screen.getByRole('option', { name: 'Custom legacy reason' }),
  236. ).toBeInTheDocument();
  237. });
  238. it('does not clear an unmappable reason on an untouched save', async () => {
  239. const user = userEvent.setup();
  240. let patched: { failure_reason?: string } | undefined;
  241. server.use(
  242. http.patch('/api/v1/archives/:id', async ({ request }) => {
  243. patched = (await request.json()) as { failure_reason?: string };
  244. return HttpResponse.json({ ...freeTextArchive, ...patched });
  245. }),
  246. );
  247. render(<EditArchiveModal archive={freeTextArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  248. await user.click(screen.getByRole('button', { name: /save/i }));
  249. await waitFor(() => {
  250. expect(patched?.failure_reason).toBe('Custom legacy reason');
  251. });
  252. });
  253. it('offers the stale-path reason the backend now writes', () => {
  254. // Both stale writers in main.py store `noStatusUpdate`. If it were
  255. // missing from the dropdown the editor would treat it as unmappable and
  256. // show the raw key to the user instead of a translated label.
  257. const staleArchive = {
  258. ...mockArchive,
  259. status: 'failed',
  260. failure_reason: 'noStatusUpdate',
  261. };
  262. render(<EditArchiveModal archive={staleArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  263. const select = screen.getByLabelText(/failure reason/i) as HTMLSelectElement;
  264. expect(select.value).toBe('noStatusUpdate');
  265. expect(
  266. screen.getByRole('option', { name: 'No status update received' }),
  267. ).toBeInTheDocument();
  268. });
  269. });
  270. describe('filament grams (#1820)', () => {
  271. // A print archived without its 3MF carries no weight at all, and no rescan
  272. // can supply one — there is no file to read. Typing it here is the only
  273. // route, so the field has to reach the API, and an untouched save must not
  274. // overwrite a figure that came from a real slice.
  275. function patchSpy() {
  276. const seen: { body?: Record<string, unknown> } = {};
  277. server.use(
  278. http.patch('/api/v1/archives/:id', async ({ request }) => {
  279. seen.body = (await request.json()) as Record<string, unknown>;
  280. return HttpResponse.json({ ...mockArchive, ...seen.body });
  281. }),
  282. );
  283. return seen;
  284. }
  285. it('sends a figure typed for an archive that has none', async () => {
  286. const user = userEvent.setup();
  287. const seen = patchSpy();
  288. render(<EditArchiveModal archive={mockArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  289. await user.type(screen.getByLabelText(/filament used/i), '46.16');
  290. await user.click(screen.getByRole('button', { name: /save/i }));
  291. await waitFor(() => {
  292. expect(seen.body?.filament_used_grams).toBe(46.16);
  293. });
  294. });
  295. it('leaves the field out of a save that did not touch it', async () => {
  296. const user = userEvent.setup();
  297. const seen = patchSpy();
  298. const weighed = { ...mockArchive, filament_used_grams: 50 };
  299. render(<EditArchiveModal archive={weighed} onClose={mockOnClose} onSave={mockOnSave} />);
  300. await user.click(screen.getByRole('button', { name: /save/i }));
  301. await waitFor(() => {
  302. expect(seen.body).toBeDefined();
  303. });
  304. expect(seen.body).not.toHaveProperty('filament_used_grams');
  305. });
  306. it('accepts a decimal comma, which a number input would have swallowed', async () => {
  307. const user = userEvent.setup();
  308. const seen = patchSpy();
  309. render(<EditArchiveModal archive={mockArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  310. await user.type(screen.getByLabelText(/filament used/i), '46,16');
  311. await user.click(screen.getByRole('button', { name: /save/i }));
  312. await waitFor(() => {
  313. expect(seen.body?.filament_used_grams).toBe(46.16);
  314. });
  315. });
  316. it('refuses characters that could never reach the API as a number', async () => {
  317. const user = userEvent.setup();
  318. render(<EditArchiveModal archive={mockArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  319. const field = screen.getByLabelText(/filament used/i) as HTMLInputElement;
  320. await user.type(field, '4a6-1..2');
  321. expect(field.value).toBe('461.2');
  322. });
  323. it('clamps to the bound the API enforces, so a save cannot be refused', async () => {
  324. const user = userEvent.setup();
  325. const seen = patchSpy();
  326. render(<EditArchiveModal archive={mockArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  327. await user.type(screen.getByLabelText(/filament used/i), '999999');
  328. await user.click(screen.getByRole('button', { name: /save/i }));
  329. await waitFor(() => {
  330. expect(seen.body?.filament_used_grams).toBe(100000);
  331. });
  332. });
  333. it('does not read a half-typed value as a clear', async () => {
  334. // Enter submits without the field ever losing focus, so the blur-time
  335. // tidy-up has not run and the state still holds what was typed.
  336. const user = userEvent.setup();
  337. const seen = patchSpy();
  338. const weighed = { ...mockArchive, filament_used_grams: 50 };
  339. render(<EditArchiveModal archive={weighed} onClose={mockOnClose} onSave={mockOnSave} />);
  340. const field = screen.getByLabelText(/filament used/i);
  341. await user.clear(field);
  342. await user.type(field, '.{Enter}');
  343. await waitFor(() => {
  344. expect(seen.body).toBeDefined();
  345. });
  346. expect(seen.body).not.toHaveProperty('filament_used_grams');
  347. });
  348. it('refreshes the print log, which the mirrored figure lands in', async () => {
  349. const user = userEvent.setup();
  350. let runFetches = 0;
  351. server.use(
  352. http.get('/api/v1/archives/:id/runs', () => {
  353. runFetches += 1;
  354. return HttpResponse.json({ items: [], total: 0 });
  355. }),
  356. );
  357. render(<EditArchiveModal archive={mockArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  358. await waitFor(() => expect(runFetches).toBe(1));
  359. await user.type(screen.getByLabelText(/filament used/i), '46.16');
  360. await user.click(screen.getByRole('button', { name: /save/i }));
  361. // Without the invalidation the table keeps serving its cached rows, so
  362. // the run the edit just corrected still shows the old figure.
  363. await waitFor(() => expect(runFetches).toBe(2));
  364. });
  365. it('clears the figure when the field is emptied', async () => {
  366. const user = userEvent.setup();
  367. const seen = patchSpy();
  368. const weighed = { ...mockArchive, filament_used_grams: 50 };
  369. render(<EditArchiveModal archive={weighed} onClose={mockOnClose} onSave={mockOnSave} />);
  370. await user.clear(screen.getByLabelText(/filament used/i));
  371. await user.click(screen.getByRole('button', { name: /save/i }));
  372. await waitFor(() => {
  373. expect(seen.body?.filament_used_grams).toBeNull();
  374. });
  375. });
  376. });
  377. describe('project picker (#2888)', () => {
  378. // Statuses matter here, so this describe brings its own list rather than
  379. // the bare one the rest of the file shares.
  380. const withStatuses = (rows: Array<Record<string, unknown>>) =>
  381. server.use(http.get('/api/v1/projects/', () => HttpResponse.json(rows)));
  382. function savedBody() {
  383. const seen: { body?: Record<string, unknown> } = {};
  384. server.use(
  385. http.patch('/api/v1/archives/:id', async ({ request }) => {
  386. seen.body = (await request.json()) as Record<string, unknown>;
  387. return HttpResponse.json({ ...mockArchive, ...seen.body });
  388. }),
  389. );
  390. return seen;
  391. }
  392. it('leaves archived projects out of the list', async () => {
  393. withStatuses([
  394. { id: 1, name: 'Live Work', color: '#00ae42', status: 'active' },
  395. { id: 2, name: 'Last Year', color: '#888888', status: 'archived' },
  396. ]);
  397. render(<EditArchiveModal archive={mockArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  398. await screen.findByRole('option', { name: 'Live Work' });
  399. expect(screen.queryByRole('option', { name: 'Last Year' })).not.toBeInTheDocument();
  400. });
  401. it('keeps completed projects, which are still worth filing a reprint under', async () => {
  402. withStatuses([
  403. { id: 1, name: 'Live Work', color: '#00ae42', status: 'active' },
  404. { id: 3, name: 'Shipped', color: '#888888', status: 'completed' },
  405. ]);
  406. render(<EditArchiveModal archive={mockArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  407. expect(await screen.findByRole('option', { name: 'Shipped' })).toBeInTheDocument();
  408. });
  409. it('still offers the archived project this archive is already in', async () => {
  410. // Filtered out, the select holds a value no option matches, and the
  411. // browser resets it to the first option -- "No project". The archive
  412. // would say it is filed nowhere while sitting in a project.
  413. withStatuses([
  414. { id: 1, name: 'Live Work', color: '#00ae42', status: 'active' },
  415. { id: 2, name: 'Last Year', color: '#888888', status: 'archived' },
  416. ]);
  417. const filed = { ...mockArchive, project_id: 2 };
  418. render(<EditArchiveModal archive={filed} onClose={mockOnClose} onSave={mockOnSave} />);
  419. const option = await screen.findByRole('option', { name: 'Last Year' });
  420. expect((option as HTMLOptionElement).selected).toBe(true);
  421. });
  422. it('saves the project it was already in when nothing else is touched', async () => {
  423. const user = userEvent.setup();
  424. const seen = savedBody();
  425. withStatuses([{ id: 2, name: 'Last Year', color: '#888888', status: 'archived' }]);
  426. render(
  427. <EditArchiveModal
  428. archive={{ ...mockArchive, project_id: 2 }}
  429. onClose={mockOnClose}
  430. onSave={mockOnSave}
  431. />,
  432. );
  433. await screen.findByRole('option', { name: 'Last Year' });
  434. await user.click(screen.getByRole('button', { name: /save/i }));
  435. // The stored id survives the round trip untouched: showing the archived
  436. // project is what makes the field honest, and it must not also change
  437. // what an untouched save writes.
  438. await waitFor(() => expect(seen.body?.project_id).toBe(2));
  439. });
  440. });
  441. describe('items printed (#3051)', () => {
  442. // A plate that jammed and came off ruined produced nothing, even when the
  443. // printer called the job a success. The project's completed-items count
  444. // sums this column, so 0 has to be typeable.
  445. function patchSpy() {
  446. const seen: { body?: Record<string, unknown> } = {};
  447. server.use(
  448. http.patch('/api/v1/archives/:id', async ({ request }) => {
  449. seen.body = (await request.json()) as Record<string, unknown>;
  450. return HttpResponse.json({ ...mockArchive, ...seen.body });
  451. }),
  452. );
  453. return seen;
  454. }
  455. it('sends 0 for a plate that produced nothing', async () => {
  456. const user = userEvent.setup();
  457. const seen = patchSpy();
  458. render(
  459. <EditArchiveModal
  460. archive={{ ...mockArchive, quantity: 4 }}
  461. onClose={mockOnClose}
  462. onSave={mockOnSave}
  463. />,
  464. );
  465. const field = screen.getByLabelText(/items printed/i) as HTMLInputElement;
  466. await user.clear(field);
  467. await user.type(field, '0');
  468. await user.click(screen.getByRole('button', { name: /save/i }));
  469. await waitFor(() => expect(seen.body?.quantity).toBe(0));
  470. });
  471. it('does not floor a cleared field back to 1', async () => {
  472. const user = userEvent.setup();
  473. render(
  474. <EditArchiveModal
  475. archive={{ ...mockArchive, quantity: 4 }}
  476. onClose={mockOnClose}
  477. onSave={mockOnSave}
  478. />,
  479. );
  480. const field = screen.getByLabelText(/items printed/i) as HTMLInputElement;
  481. await user.clear(field);
  482. // Empty while editing (#3182), and 0 rather than 1 once the field is left.
  483. expect(field.value).toBe('');
  484. await user.tab();
  485. expect(field.value).toBe('0');
  486. });
  487. it('still refuses a negative count', async () => {
  488. const user = userEvent.setup();
  489. const seen = patchSpy();
  490. render(
  491. <EditArchiveModal
  492. archive={{ ...mockArchive, quantity: 4 }}
  493. onClose={mockOnClose}
  494. onSave={mockOnSave}
  495. />,
  496. );
  497. const field = screen.getByLabelText(/items printed/i) as HTMLInputElement;
  498. await user.clear(field);
  499. await user.type(field, '-3');
  500. await user.click(screen.getByRole('button', { name: /save/i }));
  501. expect(field.value).toBe('0');
  502. await waitFor(() => expect(seen.body?.quantity).toBe(0));
  503. });
  504. });
  505. // Post-print outcome confirmation (#1898)
  506. describe('outcome verdict source', () => {
  507. const completed = {
  508. ...mockArchive,
  509. status: 'completed',
  510. user_verdict: 'good',
  511. confirm_requested: true,
  512. };
  513. it('explains a verdict the plate-clear default recorded', () => {
  514. render(
  515. <EditArchiveModal
  516. archive={{ ...completed, user_verdict_source: 'plate_clear' }}
  517. onClose={mockOnClose}
  518. onSave={mockOnSave}
  519. />,
  520. );
  521. expect(screen.getByTestId('verdict-source-hint')).toHaveTextContent(
  522. 'Recorded when the plate was cleared.',
  523. );
  524. });
  525. it('shows no hint for a verdict with no recorded source', () => {
  526. render(
  527. <EditArchiveModal
  528. archive={{ ...completed, user_verdict_source: null }}
  529. onClose={mockOnClose}
  530. onSave={mockOnSave}
  531. />,
  532. );
  533. expect(screen.queryByTestId('verdict-source-hint')).not.toBeInTheDocument();
  534. });
  535. it('stamps the dialog as the source when the verdict is changed here', async () => {
  536. const user = userEvent.setup();
  537. let seen: Record<string, unknown> | null = null;
  538. server.use(
  539. http.patch('/api/v1/archives/:id', async ({ request }) => {
  540. seen = (await request.json()) as Record<string, unknown>;
  541. return HttpResponse.json({ ...completed, ...seen });
  542. }),
  543. );
  544. render(
  545. <EditArchiveModal
  546. archive={{ ...completed, user_verdict_source: 'plate_clear' }}
  547. onClose={mockOnClose}
  548. onSave={mockOnSave}
  549. />,
  550. );
  551. await user.selectOptions(screen.getByLabelText(/outcome verdict/i), 'reject');
  552. await user.click(screen.getByRole('button', { name: /save/i }));
  553. await waitFor(() => expect(seen).not.toBeNull());
  554. expect(seen!.user_verdict).toBe('reject');
  555. expect(seen!.user_verdict_source).toBe('dialog');
  556. });
  557. });
  558. describe('failure_reason on a machine status the dropdown cannot show', () => {
  559. // The reject reason (#1898) reuses failure_reason, so the clearing branch
  560. // grew "|| archive.failure_reason" to drop it when the verdict goes away.
  561. // That clause also catches a 'cancelled' archive: print_queue writes
  562. // status 'cancelled' with "Stopped by user (printer was offline)" when a
  563. // stop is issued to an offline printer, the status dropdown has no
  564. // 'cancelled' option, and so opening the editor and pressing Save with
  565. // nothing touched wiped a reason the user could not put back.
  566. const cancelled = {
  567. ...mockArchive,
  568. status: 'cancelled',
  569. failure_reason: 'Stopped by user (printer was offline)',
  570. user_verdict: null,
  571. };
  572. it('keeps the machine-written reason on an untouched save', async () => {
  573. const user = userEvent.setup();
  574. let seen: Record<string, unknown> | null = null;
  575. server.use(
  576. http.patch('/api/v1/archives/:id', async ({ request }) => {
  577. seen = (await request.json()) as Record<string, unknown>;
  578. return HttpResponse.json({ ...cancelled, ...seen });
  579. }),
  580. );
  581. render(<EditArchiveModal archive={cancelled} onClose={mockOnClose} onSave={mockOnSave} />);
  582. await user.click(screen.getByRole('button', { name: /save/i }));
  583. await waitFor(() => expect(seen).not.toBeNull());
  584. expect(seen!).not.toHaveProperty('failure_reason');
  585. });
  586. it('still drops the reason when a reject verdict is taken back', async () => {
  587. // The behaviour the clause was written for, on the archive it was
  588. // written for: a completed print whose reject reason no longer applies.
  589. const user = userEvent.setup();
  590. let seen: Record<string, unknown> | null = null;
  591. const rejected = {
  592. ...mockArchive,
  593. status: 'completed',
  594. user_verdict: 'reject',
  595. failure_reason: 'warping',
  596. };
  597. server.use(
  598. http.patch('/api/v1/archives/:id', async ({ request }) => {
  599. seen = (await request.json()) as Record<string, unknown>;
  600. return HttpResponse.json({ ...rejected, ...seen });
  601. }),
  602. );
  603. render(<EditArchiveModal archive={rejected} onClose={mockOnClose} onSave={mockOnSave} />);
  604. await user.selectOptions(screen.getByLabelText(/outcome verdict/i), 'good');
  605. await user.click(screen.getByRole('button', { name: /save/i }));
  606. await waitFor(() => expect(seen).not.toBeNull());
  607. expect(seen!.failure_reason).toBeNull();
  608. });
  609. });
  610. });