PrintersPageAiDetection.test.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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('shows OctoEverywhere quality in the badge and detail modal for monitored printers', async () => {
  120. server.use(
  121. http.get('/api/v1/octoeverywhere/printer-status', () =>
  122. HttpResponse.json({
  123. enabled: true,
  124. monitored_printers: [1],
  125. per_printer: { '1': { class: 'warning', frame_count: 12, print_quality: 5, error: null } },
  126. last_error: null,
  127. })
  128. )
  129. );
  130. render(<PrintersPage />);
  131. const badge = await screen.findByText('Warning');
  132. expect(badge.closest('button')).toHaveAttribute(
  133. 'title',
  134. 'AI Failure Detection: Warning (print quality 5/10) - click for details'
  135. );
  136. expect(screen.queryByText('Idle')).not.toBeInTheDocument();
  137. await userEvent.click(badge);
  138. expect(await screen.findByText('Print quality')).toBeInTheDocument();
  139. expect(screen.getByText('5/10')).toBeInTheDocument();
  140. expect(screen.queryByText('Score')).not.toBeInTheDocument();
  141. });
  142. it('shows an OctoEverywhere camera error without a made-up quality value', async () => {
  143. server.use(
  144. http.get('/api/v1/octoeverywhere/printer-status', () =>
  145. HttpResponse.json({
  146. enabled: true,
  147. monitored_printers: [1],
  148. per_printer: { '1': { class: 'error', frame_count: 0, print_quality: null, error: 'Camera unavailable' } },
  149. last_error: null,
  150. })
  151. )
  152. );
  153. render(<PrintersPage />);
  154. await userEvent.click(await screen.findByText('Not checking'));
  155. expect(await screen.findByText('Camera unavailable')).toBeInTheDocument();
  156. expect(screen.queryByText('Print quality')).not.toBeInTheDocument();
  157. expect(screen.queryByText('Safe')).not.toBeInTheDocument();
  158. });
  159. it('localizes an OctoEverywhere usage-limit code in the badge and opens its billing setup link', async () => {
  160. server.use(
  161. http.get('/api/v1/octoeverywhere/printer-status', () => HttpResponse.json({
  162. enabled: true,
  163. monitored_printers: [1],
  164. per_printer: {
  165. '1': { class: 'error', frame_count: 0, print_quality: null, error: 'Remote quota text', error_code: 'OE_FREE_USAGE_LIMIT_REACHED' },
  166. },
  167. last_error: 'Remote quota text',
  168. last_error_code: 'OE_FREE_USAGE_LIMIT_REACHED',
  169. })),
  170. );
  171. render(<PrintersPage />);
  172. const badge = await screen.findByText('Not checking');
  173. expect(badge.closest('button')?.getAttribute('title')).toContain('Usage limit reached.');
  174. expect(badge.closest('button')?.getAttribute('title')).not.toContain('Remote quota text');
  175. await userEvent.click(badge);
  176. expect(await screen.findByRole('link', { name: 'Set up billing to continue' })).toHaveAttribute('href', 'https://octoeverywhere.com/gadgetapi');
  177. expect(screen.queryByText('Remote quota text')).not.toBeInTheDocument();
  178. });
  179. it('keeps a redacted OctoEverywhere error generic without exposing a quota link', async () => {
  180. server.use(
  181. http.get('/api/v1/octoeverywhere/printer-status', () => HttpResponse.json({
  182. enabled: true,
  183. monitored_printers: [1],
  184. per_printer: {
  185. '1': { class: 'error', frame_count: 0, print_quality: null, error: null, error_code: null },
  186. },
  187. last_error: null,
  188. last_error_code: null,
  189. })),
  190. );
  191. render(<PrintersPage />);
  192. await userEvent.click(await screen.findByText('Not checking'));
  193. expect(screen.queryByText('Usage limit reached.')).not.toBeInTheDocument();
  194. expect(screen.queryByRole('link', { name: /set up billing/i })).not.toBeInTheDocument();
  195. });
  196. it('clicking the badge opens a modal with live status and the last error', async () => {
  197. server.use(
  198. http.get('/api/v1/obico/printer-status', () =>
  199. HttpResponse.json({
  200. enabled: true,
  201. monitored_printers: null,
  202. per_printer: { '1': { class: 'failure', frame_count: 30, score: 0.92 } },
  203. last_error: 'ML API call failed for printer 1: connection refused',
  204. })
  205. )
  206. );
  207. render(<PrintersPage />);
  208. const user = userEvent.setup();
  209. await user.click(await screen.findByText('Failure'));
  210. expect(await screen.findByText('AI Failure Detection - X1 Carbon')).toBeInTheDocument();
  211. expect(screen.getByText('0.920')).toBeInTheDocument();
  212. expect(screen.getByText('30')).toBeInTheDocument();
  213. expect(screen.getByText('Last error')).toBeInTheDocument();
  214. expect(screen.getByText('ML API call failed for printer 1: connection refused')).toBeInTheDocument();
  215. });
  216. it('shows no badge when detection is disabled, even with stale per_printer state', async () => {
  217. server.use(
  218. http.get('/api/v1/obico/printer-status', () =>
  219. HttpResponse.json({
  220. enabled: false,
  221. monitored_printers: null,
  222. per_printer: { '1': { class: 'failure', frame_count: 30, score: 0.92 } },
  223. last_error: null,
  224. })
  225. )
  226. );
  227. render(<PrintersPage />);
  228. await waitFor(() => {
  229. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  230. });
  231. expect(screen.queryByText('Failure')).not.toBeInTheDocument();
  232. expect(screen.queryByText('Idle')).not.toBeInTheDocument();
  233. });
  234. });
  235. describe('PrintersPage AI detection badge — no verdict is not Safe (#2952)', () => {
  236. beforeEach(() => {
  237. localStorage.removeItem('printerCardSize');
  238. server.use(
  239. http.get('/api/v1/printers/', () => HttpResponse.json(mockPrinters)),
  240. http.get('/api/v1/printers/:id/status', () => HttpResponse.json(mockPrinterStatus)),
  241. http.get('/api/v1/settings/ui-preferences', () =>
  242. HttpResponse.json({
  243. ams_humidity_good: 40,
  244. ams_humidity_fair: 60,
  245. ams_temp_good: 30,
  246. ams_temp_fair: 35,
  247. })
  248. ),
  249. http.get('/api/v1/queue/', () => HttpResponse.json([]))
  250. );
  251. });
  252. const withPerPrinter = (entry: Record<string, unknown>) =>
  253. server.use(
  254. http.get('/api/v1/obico/printer-status', () =>
  255. HttpResponse.json({
  256. enabled: true,
  257. monitored_printers: [1],
  258. per_printer: { '1': entry },
  259. last_error: null,
  260. })
  261. )
  262. );
  263. it('a printer whose detection is failing reads "Not checking", never Safe', async () => {
  264. // The reporter's case: the loop is calling the ML API every 10s and being
  265. // turned away with a 401. This used to render as a green "Safe" pill at
  266. // score 0.000, indistinguishable from a healthy print.
  267. withPerPrinter({
  268. class: 'error',
  269. frame_count: 0,
  270. score: 0,
  271. error: 'Obico ML API rejected the token (401).',
  272. });
  273. render(<PrintersPage />);
  274. const badge = await screen.findByText('Not checking');
  275. expect(badge.closest('button')).toHaveAttribute(
  276. 'title',
  277. 'AI Failure Detection is not checking this print: Obico ML API rejected the token (401). - click for details'
  278. );
  279. expect(screen.queryByText('Safe')).not.toBeInTheDocument();
  280. });
  281. it('does not quote a score the model never produced', async () => {
  282. withPerPrinter({ class: 'error', frame_count: 0, score: 0, error: 'Failed to capture snapshot' });
  283. render(<PrintersPage />);
  284. const user = userEvent.setup();
  285. await user.click(await screen.findByText('Not checking'));
  286. expect(await screen.findByText('AI Failure Detection - X1 Carbon')).toBeInTheDocument();
  287. expect(screen.getByText('Failed to capture snapshot')).toBeInTheDocument();
  288. // "Score 0.000" next to "Not checking" reads as a measurement rather than
  289. // the absence of one, so neither figure is shown.
  290. expect(screen.queryByText('0.000')).not.toBeInTheDocument();
  291. expect(screen.queryByText('Frames analyzed')).not.toBeInTheDocument();
  292. });
  293. it('the window before the first result reads "Starting", never Safe', async () => {
  294. withPerPrinter({ class: 'unknown', frame_count: 0, score: 0, error: null });
  295. render(<PrintersPage />);
  296. expect(await screen.findByText('Starting')).toBeInTheDocument();
  297. expect(screen.queryByText('Safe')).not.toBeInTheDocument();
  298. });
  299. it('an unrecognised class falls back to Starting, not Safe', async () => {
  300. // A newer backend class must never be silently absorbed into a green badge.
  301. withPerPrinter({ class: 'something-new', frame_count: 5, score: 0.1, error: null });
  302. render(<PrintersPage />);
  303. expect(await screen.findByText('Starting')).toBeInTheDocument();
  304. expect(screen.queryByText('Safe')).not.toBeInTheDocument();
  305. });
  306. it('still shows Safe when an inference actually said so', async () => {
  307. withPerPrinter({ class: 'safe', frame_count: 216, score: 0, error: null });
  308. render(<PrintersPage />);
  309. const badge = await screen.findByText('Safe');
  310. expect(badge.closest('button')).toHaveAttribute(
  311. 'title',
  312. 'AI Failure Detection: Safe (score 0.000) - click for details'
  313. );
  314. });
  315. it("prefers this printer's own reason over the service-wide last error", async () => {
  316. server.use(
  317. http.get('/api/v1/obico/printer-status', () =>
  318. HttpResponse.json({
  319. enabled: true,
  320. monitored_printers: [1],
  321. per_printer: { '1': { class: 'error', frame_count: 0, score: 0, error: 'This printer: camera timed out' } },
  322. last_error: 'Some other printer: token rejected',
  323. })
  324. )
  325. );
  326. render(<PrintersPage />);
  327. const user = userEvent.setup();
  328. await user.click(await screen.findByText('Not checking'));
  329. expect(await screen.findByText('This printer: camera timed out')).toBeInTheDocument();
  330. expect(screen.queryByText('Some other printer: token rejected')).not.toBeInTheDocument();
  331. });
  332. });