PrintersPageAiDetection.test.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * Tests for the AI failure detection badge on printer cards (#1546).
  3. *
  4. * The badge reflects /obico/printer-status: always shown for printers in the
  5. * monitored set while detection is enabled — gray "Idle" outside a monitored
  6. * print, class-colored (safe/warning/failure) during one.
  7. */
  8. import { describe, it, expect, beforeEach } from 'vitest';
  9. import { screen, waitFor } from '@testing-library/react';
  10. import userEvent from '@testing-library/user-event';
  11. import { render } from '../utils';
  12. import { PrintersPage } from '../../pages/PrintersPage';
  13. import { http, HttpResponse } from 'msw';
  14. import { server } from '../mocks/server';
  15. const mockPrinters = [
  16. {
  17. id: 1,
  18. name: 'X1 Carbon',
  19. ip_address: '192.168.1.100',
  20. serial_number: '00M09A350100001',
  21. access_code: '12345678',
  22. model: 'X1C',
  23. enabled: true,
  24. is_active: true,
  25. nozzle_diameter: 0.4,
  26. nozzle_type: 'hardened_steel',
  27. location: 'Workshop',
  28. auto_archive: true,
  29. created_at: '2024-01-01T00:00:00Z',
  30. updated_at: '2024-01-01T00:00:00Z',
  31. },
  32. {
  33. id: 2,
  34. name: 'P1S Backup',
  35. ip_address: '192.168.1.101',
  36. serial_number: '00W00A123456789',
  37. access_code: '87654321',
  38. model: 'P1S',
  39. enabled: true,
  40. is_active: true,
  41. nozzle_diameter: 0.4,
  42. nozzle_type: 'stainless_steel',
  43. location: null,
  44. auto_archive: true,
  45. created_at: '2024-01-02T00:00:00Z',
  46. updated_at: '2024-01-02T00:00:00Z',
  47. },
  48. ];
  49. const mockPrinterStatus = {
  50. connected: true,
  51. state: 'RUNNING',
  52. awaiting_plate_clear: false,
  53. progress: 42,
  54. layer_num: 10,
  55. total_layers: 100,
  56. temperatures: { nozzle: 220, bed: 55, chamber: 30 },
  57. remaining_time: 3600,
  58. filename: 'benchy.3mf',
  59. wifi_signal: -50,
  60. vt_tray: [],
  61. };
  62. describe('PrintersPage AI detection badge (#1546)', () => {
  63. beforeEach(() => {
  64. localStorage.removeItem('printerCardSize');
  65. server.use(
  66. http.get('/api/v1/printers/', () => HttpResponse.json(mockPrinters)),
  67. http.get('/api/v1/printers/:id/status', () => HttpResponse.json(mockPrinterStatus)),
  68. http.get('/api/v1/settings/ui-preferences', () =>
  69. HttpResponse.json({
  70. ams_humidity_good: 40,
  71. ams_humidity_fair: 60,
  72. ams_temp_good: 30,
  73. ams_temp_fair: 35,
  74. })
  75. ),
  76. http.get('/api/v1/queue/', () => HttpResponse.json([]))
  77. );
  78. });
  79. it('shows the live class for a monitored print and Idle for other monitored printers', async () => {
  80. server.use(
  81. http.get('/api/v1/obico/printer-status', () =>
  82. HttpResponse.json({
  83. enabled: true,
  84. monitored_printers: null,
  85. per_printer: { '1': { class: 'warning', frame_count: 12, score: 0.31 } },
  86. last_error: null,
  87. })
  88. )
  89. );
  90. render(<PrintersPage />);
  91. const badge = await screen.findByText('Warning');
  92. expect(badge.closest('button')).toHaveAttribute(
  93. 'title',
  94. 'AI Failure Detection: Warning (score 0.310) - click for details'
  95. );
  96. // Printer 2 is monitored (null = all) but has no active print — gray Idle badge
  97. const idleBadge = await screen.findByText('Idle');
  98. expect(idleBadge.closest('button')).toHaveAttribute(
  99. 'title',
  100. 'AI Failure Detection enabled - monitoring starts with the next print - click for details'
  101. );
  102. expect(screen.getAllByText('Warning')).toHaveLength(1);
  103. });
  104. it('shows no badge for printers outside the monitored subset', async () => {
  105. server.use(
  106. http.get('/api/v1/obico/printer-status', () =>
  107. HttpResponse.json({
  108. enabled: true,
  109. monitored_printers: [2],
  110. per_printer: {},
  111. last_error: null,
  112. })
  113. )
  114. );
  115. render(<PrintersPage />);
  116. // Printer 2 gets the Idle badge; printer 1 (not monitored) gets none
  117. expect(await screen.findAllByText('Idle')).toHaveLength(1);
  118. });
  119. it('clicking the badge opens a modal with live status and the last error', async () => {
  120. server.use(
  121. http.get('/api/v1/obico/printer-status', () =>
  122. HttpResponse.json({
  123. enabled: true,
  124. monitored_printers: null,
  125. per_printer: { '1': { class: 'failure', frame_count: 30, score: 0.92 } },
  126. last_error: 'ML API call failed for printer 1: connection refused',
  127. })
  128. )
  129. );
  130. render(<PrintersPage />);
  131. const user = userEvent.setup();
  132. await user.click(await screen.findByText('Failure'));
  133. expect(await screen.findByText('AI Failure Detection - X1 Carbon')).toBeInTheDocument();
  134. expect(screen.getByText('0.920')).toBeInTheDocument();
  135. expect(screen.getByText('30')).toBeInTheDocument();
  136. expect(screen.getByText('Last error')).toBeInTheDocument();
  137. expect(screen.getByText('ML API call failed for printer 1: connection refused')).toBeInTheDocument();
  138. });
  139. it('shows no badge when detection is disabled, even with stale per_printer state', async () => {
  140. server.use(
  141. http.get('/api/v1/obico/printer-status', () =>
  142. HttpResponse.json({
  143. enabled: false,
  144. monitored_printers: null,
  145. per_printer: { '1': { class: 'failure', frame_count: 30, score: 0.92 } },
  146. last_error: null,
  147. })
  148. )
  149. );
  150. render(<PrintersPage />);
  151. await waitFor(() => {
  152. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  153. });
  154. expect(screen.queryByText('Failure')).not.toBeInTheDocument();
  155. expect(screen.queryByText('Idle')).not.toBeInTheDocument();
  156. });
  157. });
  158. describe('PrintersPage AI detection badge — no verdict is not Safe (#2952)', () => {
  159. beforeEach(() => {
  160. localStorage.removeItem('printerCardSize');
  161. server.use(
  162. http.get('/api/v1/printers/', () => HttpResponse.json(mockPrinters)),
  163. http.get('/api/v1/printers/:id/status', () => HttpResponse.json(mockPrinterStatus)),
  164. http.get('/api/v1/settings/ui-preferences', () =>
  165. HttpResponse.json({
  166. ams_humidity_good: 40,
  167. ams_humidity_fair: 60,
  168. ams_temp_good: 30,
  169. ams_temp_fair: 35,
  170. })
  171. ),
  172. http.get('/api/v1/queue/', () => HttpResponse.json([]))
  173. );
  174. });
  175. const withPerPrinter = (entry: Record<string, unknown>) =>
  176. server.use(
  177. http.get('/api/v1/obico/printer-status', () =>
  178. HttpResponse.json({
  179. enabled: true,
  180. monitored_printers: [1],
  181. per_printer: { '1': entry },
  182. last_error: null,
  183. })
  184. )
  185. );
  186. it('a printer whose detection is failing reads "Not checking", never Safe', async () => {
  187. // The reporter's case: the loop is calling the ML API every 10s and being
  188. // turned away with a 401. This used to render as a green "Safe" pill at
  189. // score 0.000, indistinguishable from a healthy print.
  190. withPerPrinter({
  191. class: 'error',
  192. frame_count: 0,
  193. score: 0,
  194. error: 'Obico ML API rejected the token (401).',
  195. });
  196. render(<PrintersPage />);
  197. const badge = await screen.findByText('Not checking');
  198. expect(badge.closest('button')).toHaveAttribute(
  199. 'title',
  200. 'AI Failure Detection is not checking this print: Obico ML API rejected the token (401). - click for details'
  201. );
  202. expect(screen.queryByText('Safe')).not.toBeInTheDocument();
  203. });
  204. it('does not quote a score the model never produced', async () => {
  205. withPerPrinter({ class: 'error', frame_count: 0, score: 0, error: 'Failed to capture snapshot' });
  206. render(<PrintersPage />);
  207. const user = userEvent.setup();
  208. await user.click(await screen.findByText('Not checking'));
  209. expect(await screen.findByText('AI Failure Detection - X1 Carbon')).toBeInTheDocument();
  210. expect(screen.getByText('Failed to capture snapshot')).toBeInTheDocument();
  211. // "Score 0.000" next to "Not checking" reads as a measurement rather than
  212. // the absence of one, so neither figure is shown.
  213. expect(screen.queryByText('0.000')).not.toBeInTheDocument();
  214. expect(screen.queryByText('Frames analyzed')).not.toBeInTheDocument();
  215. });
  216. it('the window before the first result reads "Starting", never Safe', async () => {
  217. withPerPrinter({ class: 'unknown', frame_count: 0, score: 0, error: null });
  218. render(<PrintersPage />);
  219. expect(await screen.findByText('Starting')).toBeInTheDocument();
  220. expect(screen.queryByText('Safe')).not.toBeInTheDocument();
  221. });
  222. it('an unrecognised class falls back to Starting, not Safe', async () => {
  223. // A newer backend class must never be silently absorbed into a green badge.
  224. withPerPrinter({ class: 'something-new', frame_count: 5, score: 0.1, error: null });
  225. render(<PrintersPage />);
  226. expect(await screen.findByText('Starting')).toBeInTheDocument();
  227. expect(screen.queryByText('Safe')).not.toBeInTheDocument();
  228. });
  229. it('still shows Safe when an inference actually said so', async () => {
  230. withPerPrinter({ class: 'safe', frame_count: 216, score: 0, error: null });
  231. render(<PrintersPage />);
  232. const badge = await screen.findByText('Safe');
  233. expect(badge.closest('button')).toHaveAttribute(
  234. 'title',
  235. 'AI Failure Detection: Safe (score 0.000) - click for details'
  236. );
  237. });
  238. it("prefers this printer's own reason over the service-wide last error", async () => {
  239. server.use(
  240. http.get('/api/v1/obico/printer-status', () =>
  241. HttpResponse.json({
  242. enabled: true,
  243. monitored_printers: [1],
  244. per_printer: { '1': { class: 'error', frame_count: 0, score: 0, error: 'This printer: camera timed out' } },
  245. last_error: 'Some other printer: token rejected',
  246. })
  247. )
  248. );
  249. render(<PrintersPage />);
  250. const user = userEvent.setup();
  251. await user.click(await screen.findByText('Not checking'));
  252. expect(await screen.findByText('This printer: camera timed out')).toBeInTheDocument();
  253. expect(screen.queryByText('Some other printer: token rejected')).not.toBeInTheDocument();
  254. });
  255. });