EditArchiveModal.test.tsx 27 KB

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