PrintersPageDryingFeedback.test.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * Feedback for the AMS drying start/stop buttons (#2533).
  3. *
  4. * The reporter's P1S accepted `ams_filament_drying` with result=success and
  5. * then never dried. Two gaps: the Start button gave no confirmation at all,
  6. * and Bambuddy treated the MQTT ack as proof the cycle had begun. These tests
  7. * cover the toast and the post-ack watcher that catches a printer which takes
  8. * the command and drops it.
  9. *
  10. * The reporter's own printer is now handled further up: Bambu's P1 manual says
  11. * P1-series AMS drying is screen-only, so the card no longer offers to command
  12. * it (last describe block). The watcher stays for any other firmware that acks
  13. * and declines — on the models we *can* command, the ack is still all we have,
  14. * because the `dry_sf_reason` refusal array only exists on some of them.
  15. */
  16. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  17. import { screen, waitFor } from '@testing-library/react';
  18. import userEvent from '@testing-library/user-event';
  19. import { render } from '../utils';
  20. import { PrintersPage } from '../../pages/PrintersPage';
  21. import { http, HttpResponse } from 'msw';
  22. import { server } from '../mocks/server';
  23. const mockShowToast = vi.fn();
  24. vi.mock('../../contexts/ToastContext', async (importOriginal) => {
  25. const actual = await importOriginal<typeof import('../../contexts/ToastContext')>();
  26. return { ...actual, useToast: () => ({ showToast: mockShowToast }) };
  27. });
  28. const mockPrinter = {
  29. id: 1,
  30. name: 'X1C',
  31. ip_address: '192.168.1.100',
  32. serial_number: '01P00A000000001',
  33. access_code: '12345678',
  34. model: 'X1C',
  35. enabled: true,
  36. nozzle_diameter: 0.4,
  37. nozzle_type: 'stainless_steel',
  38. location: 'Workshop',
  39. auto_archive: true,
  40. created_at: '2024-01-01T00:00:00Z',
  41. updated_at: '2024-01-01T00:00:00Z',
  42. };
  43. const baseTray = {
  44. tray_color: 'FF0000FF',
  45. tray_type: 'PLA',
  46. tray_sub_brands: 'PLA Basic',
  47. tray_id_name: 'A00-R0',
  48. tray_info_idx: 'GFA00',
  49. remain: 80,
  50. k: 0.02,
  51. cali_idx: null,
  52. tag_uid: null,
  53. tray_uuid: null,
  54. nozzle_temp_min: 190,
  55. nozzle_temp_max: 230,
  56. drying_temp: null,
  57. drying_time: null,
  58. state: 3,
  59. };
  60. /** AMS 2 Pro (n3f) on an idle printer that accepts remote drying commands. */
  61. function makeStatus(
  62. dry: { dry_time: number; dry_status: number; dry_countdown_stalled?: boolean },
  63. caps: { supports_drying?: boolean; drying_screen_only?: boolean } = {},
  64. ) {
  65. return {
  66. connected: true,
  67. state: 'IDLE',
  68. progress: 0,
  69. layer_num: 0,
  70. total_layers: 0,
  71. temperatures: { nozzle: 25, bed: 25, chamber: 25 },
  72. remaining_time: 0,
  73. filename: null,
  74. wifi_signal: -29,
  75. speed_level: 2,
  76. supports_drying: caps.supports_drying ?? true,
  77. drying_screen_only: caps.drying_screen_only ?? false,
  78. vt_tray: [],
  79. ams: [
  80. {
  81. id: 0,
  82. humidity: 30,
  83. temp: 33,
  84. is_ams_ht: false,
  85. serial_number: 'AMS00',
  86. sw_ver: '03.00.21.29',
  87. dry_sub_status: 0,
  88. dry_sf_reason: [],
  89. module_type: 'n3f',
  90. ...dry,
  91. tray: [
  92. { id: 0, ...baseTray },
  93. { id: 1, ...baseTray },
  94. { id: 2, ...baseTray },
  95. { id: 3, ...baseTray },
  96. ],
  97. },
  98. ],
  99. };
  100. }
  101. const IDLE = makeStatus({ dry_time: 0, dry_status: 0 });
  102. const DRYING = makeStatus({ dry_time: 720, dry_status: 2 });
  103. /** Timer set by an accepted command, but the countdown never started ticking. */
  104. const STALLED = makeStatus({ dry_time: 720, dry_status: 0, dry_countdown_stalled: true });
  105. /** A P1: the AMS dries, but only from the printer's own screen. */
  106. const SCREEN_ONLY = makeStatus(
  107. { dry_time: 0, dry_status: 0 },
  108. { supports_drying: false, drying_screen_only: true },
  109. );
  110. const SCREEN_ONLY_DRYING = makeStatus(
  111. { dry_time: 720, dry_status: 2 },
  112. { supports_drying: false, drying_screen_only: true },
  113. );
  114. const SCREEN_ONLY_TITLE =
  115. "AMS drying on this printer can only be controlled from the printer's own screen (Bambu limitation)";
  116. /**
  117. * Open the drying popover and press Start. The card renders the AMS in two
  118. * layouts, so the flame icon appears more than once — either opens the same
  119. * popover.
  120. */
  121. async function startDrying(user: ReturnType<typeof userEvent.setup>) {
  122. await waitFor(() => {
  123. expect(screen.getAllByTitle('Start Drying').length).toBeGreaterThan(0);
  124. });
  125. await user.click(screen.getAllByTitle('Start Drying')[0]);
  126. await user.click(await screen.findByTestId('drying-start-confirm'));
  127. }
  128. describe('PrintersPage - AMS drying feedback (#2533)', () => {
  129. beforeEach(() => {
  130. mockShowToast.mockClear();
  131. server.use(
  132. http.get('/api/v1/printers/', () => HttpResponse.json([mockPrinter])),
  133. http.get('/api/v1/queue/', () => HttpResponse.json([])),
  134. http.post('/api/v1/printers/:id/drying/start', () =>
  135. HttpResponse.json({ status: 'drying_started', ams_id: 0, temp: 45, duration: 12 }),
  136. ),
  137. http.post('/api/v1/printers/:id/drying/stop', () =>
  138. HttpResponse.json({ status: 'drying_stopped', ams_id: 0 }),
  139. ),
  140. );
  141. });
  142. afterEach(() => {
  143. vi.useRealTimers();
  144. });
  145. it('acknowledges the start command with a toast that claims only that it was sent', async () => {
  146. const user = userEvent.setup();
  147. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(IDLE)));
  148. render(<PrintersPage />);
  149. await startDrying(user);
  150. await waitFor(() => {
  151. expect(mockShowToast).toHaveBeenCalledWith('Drying command sent', 'success');
  152. });
  153. });
  154. it('confirms the stop command with a toast', async () => {
  155. const user = userEvent.setup();
  156. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(DRYING)));
  157. render(<PrintersPage />);
  158. // While a cycle is live the same button becomes Stop Drying.
  159. await waitFor(() => {
  160. expect(screen.getAllByTitle('Stop Drying').length).toBeGreaterThan(0);
  161. });
  162. await user.click(screen.getAllByTitle('Stop Drying')[0]);
  163. // Sent, not "stopped": the AMS reports the end of the cycle later.
  164. await waitFor(() => {
  165. expect(mockShowToast).toHaveBeenCalledWith('Stop command sent', 'success');
  166. });
  167. });
  168. it('warns when the printer acks the command but the AMS never starts drying', async () => {
  169. vi.useFakeTimers({ shouldAdvanceTime: true });
  170. const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
  171. // Status keeps reporting dry_status 0 — the reporter's exact symptom.
  172. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(IDLE)));
  173. render(<PrintersPage />);
  174. await startDrying(user);
  175. await waitFor(() => {
  176. expect(mockShowToast).toHaveBeenCalledWith('Drying command sent', 'success');
  177. });
  178. await vi.advanceTimersByTimeAsync(31_000);
  179. expect(mockShowToast).toHaveBeenCalledWith(
  180. 'The printer accepted the command but the AMS never started drying. Check that the AMS power adapter is connected and that the printer is idle.',
  181. 'error',
  182. );
  183. });
  184. it('stays quiet when the AMS does enter a drying cycle', async () => {
  185. vi.useFakeTimers({ shouldAdvanceTime: true });
  186. const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
  187. // Idle until the command lands, then the AMS reports a live cycle — which is
  188. // what the mutation's cache invalidation refetches.
  189. let started = false;
  190. server.use(
  191. http.get('/api/v1/printers/:id/status', () => HttpResponse.json(started ? DRYING : IDLE)),
  192. http.post('/api/v1/printers/:id/drying/start', () => {
  193. started = true;
  194. return HttpResponse.json({ status: 'drying_started', ams_id: 0, temp: 45, duration: 12 });
  195. }),
  196. );
  197. render(<PrintersPage />);
  198. await startDrying(user);
  199. await waitFor(() => {
  200. expect(mockShowToast).toHaveBeenCalledWith('Drying command sent', 'success');
  201. });
  202. await vi.advanceTimersByTimeAsync(31_000);
  203. expect(mockShowToast).not.toHaveBeenCalledWith(expect.stringContaining('never started drying'), 'error');
  204. });
  205. });
  206. describe('PrintersPage - screen-only AMS drying (#2533)', () => {
  207. beforeEach(() => {
  208. mockShowToast.mockClear();
  209. server.use(
  210. http.get('/api/v1/printers/', () => HttpResponse.json([mockPrinter])),
  211. http.get('/api/v1/queue/', () => HttpResponse.json([])),
  212. );
  213. });
  214. it('keeps the drying control visible but disabled, and says why', async () => {
  215. const user = userEvent.setup();
  216. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(SCREEN_ONLY)));
  217. render(<PrintersPage />);
  218. // Present, so the user learns the AMS *can* dry and where to do it — silently
  219. // dropping the button would just look like the feature vanished.
  220. const buttons = await screen.findAllByTitle(SCREEN_ONLY_TITLE);
  221. expect(buttons[0]).toBeDisabled();
  222. await user.click(buttons[0]);
  223. expect(screen.queryByTestId('drying-start-confirm')).not.toBeInTheDocument();
  224. });
  225. it('shows a cycle started at the printer, without offering to stop it', async () => {
  226. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(SCREEN_ONLY_DRYING)));
  227. render(<PrintersPage />);
  228. // The countdown is pure observation and still works.
  229. expect((await screen.findAllByText(/12h 0m/)).length).toBeGreaterThan(0);
  230. // Stop is a command, and a P1 ignores it exactly as it ignores start.
  231. expect(screen.queryByTitle('Stop Drying')).not.toBeInTheDocument();
  232. });
  233. });
  234. describe('PrintersPage - parked drying command is not shown as an active cycle', () => {
  235. beforeEach(() => {
  236. mockShowToast.mockClear();
  237. server.use(
  238. http.get('/api/v1/printers/', () => HttpResponse.json([mockPrinter])),
  239. http.get('/api/v1/queue/', () => HttpResponse.json([])),
  240. );
  241. });
  242. it('shows "Drying not running" instead of the active badge when the countdown is stalled', async () => {
  243. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(STALLED)));
  244. render(<PrintersPage />);
  245. expect((await screen.findAllByText('Drying not running')).length).toBeGreaterThan(0);
  246. // The active-cycle claims are gone: no "Drying" badge text, no countdown.
  247. expect(screen.queryByText('12h 0m left')).not.toBeInTheDocument();
  248. });
  249. it('keeps the amber active badge for a countdown that is really ticking', async () => {
  250. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(DRYING)));
  251. render(<PrintersPage />);
  252. expect((await screen.findAllByText(/12h 0m/)).length).toBeGreaterThan(0);
  253. expect(screen.queryByText('Drying not running')).not.toBeInTheDocument();
  254. });
  255. });