SpoolmanSettings.test.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /**
  2. * Tests for the SpoolmanSettings component.
  3. *
  4. * Tests the filament tracking mode selector and Spoolman integration UI:
  5. * - Mode selector (Built-in Inventory vs Spoolman)
  6. * - Built-in Inventory info panel
  7. * - Spoolman URL, sync mode, connection status
  8. * - Weight sync and partial usage toggles
  9. */
  10. import { describe, it, expect, vi, beforeEach } from 'vitest';
  11. import { screen, waitFor } from '@testing-library/react';
  12. import userEvent from '@testing-library/user-event';
  13. import { render } from '../utils';
  14. import { SpoolmanSettings } from '../../components/SpoolmanSettings';
  15. // Mock the API client
  16. vi.mock('../../api/client', () => ({
  17. api: {
  18. getSettings: vi.fn().mockResolvedValue({}),
  19. updateSettings: vi.fn().mockResolvedValue({}),
  20. getSpoolmanSettings: vi.fn(),
  21. updateSpoolmanSettings: vi.fn(),
  22. getSpoolmanStatus: vi.fn(),
  23. connectSpoolman: vi.fn(),
  24. syncAllPrintersAms: vi.fn(),
  25. syncPrinterAms: vi.fn(),
  26. syncSpoolmanAmsWeights: vi.fn(),
  27. migrateSpoolmanTags: vi.fn(),
  28. getPrinters: vi.fn(),
  29. getAuthStatus: vi.fn().mockResolvedValue({ auth_enabled: false }),
  30. },
  31. }));
  32. // Import mocked module
  33. import { api } from '../../api/client';
  34. describe('SpoolmanSettings', () => {
  35. beforeEach(() => {
  36. vi.clearAllMocks();
  37. // Default API mocks — Spoolman disabled (Built-in Inventory mode)
  38. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  39. spoolman_enabled: 'false',
  40. spoolman_url: '',
  41. spoolman_sync_mode: 'auto',
  42. spoolman_disable_weight_sync: 'false',
  43. spoolman_report_partial_usage: 'true',
  44. });
  45. vi.mocked(api.updateSpoolmanSettings).mockResolvedValue({
  46. spoolman_enabled: 'false',
  47. spoolman_url: '',
  48. spoolman_sync_mode: 'auto',
  49. spoolman_disable_weight_sync: 'false',
  50. spoolman_report_partial_usage: 'true',
  51. });
  52. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  53. enabled: false,
  54. connected: false,
  55. url: null,
  56. });
  57. vi.mocked(api.getPrinters).mockResolvedValue([]);
  58. vi.mocked(api.connectSpoolman).mockResolvedValue({ success: true, message: 'Connected' });
  59. vi.mocked(api.syncAllPrintersAms).mockResolvedValue({
  60. success: true,
  61. synced_count: 3,
  62. skipped_count: 1,
  63. skipped: [],
  64. errors: [],
  65. });
  66. });
  67. describe('rendering', () => {
  68. it('renders loading state initially', () => {
  69. vi.mocked(api.getSpoolmanSettings).mockImplementation(() => new Promise(() => {}));
  70. render(<SpoolmanSettings />);
  71. expect(document.querySelector('.animate-spin')).toBeInTheDocument();
  72. });
  73. it('renders filament tracking title', async () => {
  74. render(<SpoolmanSettings />);
  75. await waitFor(() => {
  76. expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
  77. });
  78. });
  79. it('renders mode selector cards', async () => {
  80. render(<SpoolmanSettings />);
  81. await waitFor(() => {
  82. expect(screen.getByText('Built-in Inventory')).toBeInTheDocument();
  83. expect(screen.getByText('Spoolman')).toBeInTheDocument();
  84. });
  85. });
  86. });
  87. describe('built-in inventory mode (default)', () => {
  88. it('shows built-in inventory as selected by default', async () => {
  89. render(<SpoolmanSettings />);
  90. await waitFor(() => {
  91. // Built-in Inventory card should have the active border
  92. const builtInBtn = screen.getByText('Built-in Inventory').closest('button');
  93. expect(builtInBtn).toHaveClass('border-bambu-green');
  94. });
  95. });
  96. it('shows built-in info panel when selected', async () => {
  97. render(<SpoolmanSettings />);
  98. await waitFor(() => {
  99. expect(screen.getByText(/Automatically detects Bambu Lab RFID spools/)).toBeInTheDocument();
  100. });
  101. });
  102. it('does not show Spoolman URL input', async () => {
  103. render(<SpoolmanSettings />);
  104. await waitFor(() => {
  105. expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
  106. });
  107. expect(screen.queryByPlaceholderText('http://192.168.1.100:7912')).not.toBeInTheDocument();
  108. });
  109. });
  110. describe('spoolman mode', () => {
  111. beforeEach(() => {
  112. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  113. spoolman_enabled: 'true',
  114. spoolman_url: 'http://localhost:7912',
  115. spoolman_sync_mode: 'auto',
  116. spoolman_disable_weight_sync: 'false',
  117. spoolman_report_partial_usage: 'true',
  118. });
  119. vi.mocked(api.updateSpoolmanSettings).mockResolvedValue({
  120. spoolman_enabled: 'true',
  121. spoolman_url: 'http://localhost:7912',
  122. spoolman_sync_mode: 'auto',
  123. spoolman_disable_weight_sync: 'false',
  124. spoolman_report_partial_usage: 'true',
  125. });
  126. });
  127. it('shows Spoolman card as selected', async () => {
  128. render(<SpoolmanSettings />);
  129. await waitFor(() => {
  130. const spoolmanBtn = screen.getByText('Spoolman').closest('button');
  131. expect(spoolmanBtn).toHaveClass('border-bambu-green');
  132. });
  133. });
  134. it('shows URL input when Spoolman is selected', async () => {
  135. render(<SpoolmanSettings />);
  136. await waitFor(() => {
  137. expect(screen.getByPlaceholderText('http://192.168.1.100:7912')).toBeInTheDocument();
  138. });
  139. });
  140. it('shows sync mode selector', async () => {
  141. render(<SpoolmanSettings />);
  142. await waitFor(() => {
  143. expect(screen.getByText('Sync Mode')).toBeInTheDocument();
  144. });
  145. });
  146. it('shows how sync works info', async () => {
  147. render(<SpoolmanSettings />);
  148. await waitFor(() => {
  149. expect(screen.getByText('How Sync Works')).toBeInTheDocument();
  150. });
  151. });
  152. it('shows connection status section', async () => {
  153. render(<SpoolmanSettings />);
  154. await waitFor(() => {
  155. expect(screen.getByText('Status:')).toBeInTheDocument();
  156. });
  157. });
  158. it('shows Disconnected when not connected', async () => {
  159. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  160. enabled: true,
  161. connected: false,
  162. url: 'http://localhost:7912',
  163. });
  164. render(<SpoolmanSettings />);
  165. await waitFor(() => {
  166. expect(screen.getByText('Disconnected')).toBeInTheDocument();
  167. });
  168. });
  169. it('shows Connected and offers nothing to press when connected', async () => {
  170. // Spoolman is a stateless HTTP API with no session to close, so there is
  171. // nothing for a Disconnect button to disconnect: it dropped this
  172. // process's client object, which the next request rebuilt lazily, and the
  173. // status flipped back on its own (#2903). Turning the integration off is
  174. // the enable toggle's job.
  175. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  176. enabled: true,
  177. connected: true,
  178. url: 'http://localhost:7912',
  179. });
  180. render(<SpoolmanSettings />);
  181. await waitFor(() => {
  182. expect(screen.getByText('Connected')).toBeInTheDocument();
  183. });
  184. expect(screen.queryByText('Disconnect')).not.toBeInTheDocument();
  185. expect(screen.queryByText('Connect')).not.toBeInTheDocument();
  186. });
  187. it('offers Connect as a retry only while Spoolman is unreachable', async () => {
  188. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  189. enabled: true,
  190. connected: false,
  191. url: 'http://localhost:7912',
  192. });
  193. render(<SpoolmanSettings />);
  194. await waitFor(() => {
  195. expect(screen.getByText('Connect')).toBeInTheDocument();
  196. });
  197. expect(screen.queryByText('Disconnect')).not.toBeInTheDocument();
  198. });
  199. it('shows sync section when connected', async () => {
  200. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  201. enabled: true,
  202. connected: true,
  203. url: 'http://localhost:7912',
  204. });
  205. render(<SpoolmanSettings />);
  206. await waitFor(() => {
  207. expect(screen.getByText('Sync AMS Data')).toBeInTheDocument();
  208. });
  209. });
  210. });
  211. describe('weight sync toggle', () => {
  212. it('shows weight sync toggle when Spoolman enabled and sync mode is auto', async () => {
  213. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  214. spoolman_enabled: 'true',
  215. spoolman_url: 'http://localhost:7912',
  216. spoolman_sync_mode: 'auto',
  217. spoolman_disable_weight_sync: 'false',
  218. spoolman_report_partial_usage: 'true',
  219. });
  220. render(<SpoolmanSettings />);
  221. await waitFor(() => {
  222. expect(screen.getByText('Disable AMS Estimated Weight Sync')).toBeInTheDocument();
  223. });
  224. });
  225. it('does not show weight sync toggle when sync mode is manual', async () => {
  226. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  227. spoolman_enabled: 'true',
  228. spoolman_url: 'http://localhost:7912',
  229. spoolman_sync_mode: 'manual',
  230. spoolman_disable_weight_sync: 'false',
  231. spoolman_report_partial_usage: 'true',
  232. });
  233. render(<SpoolmanSettings />);
  234. await waitFor(() => {
  235. expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
  236. });
  237. expect(screen.queryByText('Disable AMS Estimated Weight Sync')).not.toBeInTheDocument();
  238. });
  239. });
  240. describe('partial usage toggle', () => {
  241. it('shows partial usage toggle when weight sync is disabled', async () => {
  242. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  243. spoolman_enabled: 'true',
  244. spoolman_url: 'http://localhost:7912',
  245. spoolman_sync_mode: 'auto',
  246. spoolman_disable_weight_sync: 'true',
  247. spoolman_report_partial_usage: 'true',
  248. });
  249. render(<SpoolmanSettings />);
  250. await waitFor(() => {
  251. expect(screen.getByText('Report Partial Usage for Failed Prints')).toBeInTheDocument();
  252. });
  253. });
  254. it('does not show partial usage toggle when weight sync is enabled', async () => {
  255. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  256. spoolman_enabled: 'true',
  257. spoolman_url: 'http://localhost:7912',
  258. spoolman_sync_mode: 'auto',
  259. spoolman_disable_weight_sync: 'false',
  260. spoolman_report_partial_usage: 'true',
  261. });
  262. render(<SpoolmanSettings />);
  263. await waitFor(() => {
  264. expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
  265. });
  266. expect(screen.queryByText('Report Partial Usage for Failed Prints')).not.toBeInTheDocument();
  267. });
  268. });
  269. describe('mode switching', () => {
  270. it('can switch to Spoolman mode', async () => {
  271. const user = userEvent.setup();
  272. render(<SpoolmanSettings />);
  273. await waitFor(() => {
  274. expect(screen.getByText('Built-in Inventory')).toBeInTheDocument();
  275. });
  276. // Click Spoolman card
  277. await user.click(screen.getByText('Spoolman').closest('button')!);
  278. // Spoolman settings should now be visible
  279. await waitFor(() => {
  280. expect(screen.getByPlaceholderText('http://192.168.1.100:7912')).toBeInTheDocument();
  281. });
  282. });
  283. });
  284. describe('Spoolman AMS weight sync button', () => {
  285. beforeEach(() => {
  286. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  287. spoolman_enabled: 'true',
  288. spoolman_url: 'http://localhost:7912',
  289. spoolman_sync_mode: 'auto',
  290. spoolman_disable_weight_sync: 'false',
  291. spoolman_report_partial_usage: 'true',
  292. });
  293. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  294. enabled: true,
  295. connected: true,
  296. url: 'http://localhost:7912',
  297. });
  298. vi.mocked(api.syncSpoolmanAmsWeights).mockResolvedValue({ synced: 2, skipped: 1 });
  299. });
  300. it('shows Spoolman AMS sync button when connected', async () => {
  301. render(<SpoolmanSettings />);
  302. // Text appears in both the label <p> and the button — use getAllByText
  303. await waitFor(() => {
  304. const elements = screen.getAllByText('Sync Spoolman Weights from AMS');
  305. expect(elements.length).toBeGreaterThanOrEqual(1);
  306. });
  307. });
  308. it('opens confirm modal when Spoolman AMS sync button clicked', async () => {
  309. const user = userEvent.setup();
  310. render(<SpoolmanSettings />);
  311. // Wait for the button section to appear
  312. await waitFor(() => {
  313. const elements = screen.getAllByText('Sync Spoolman Weights from AMS');
  314. expect(elements.length).toBeGreaterThanOrEqual(1);
  315. });
  316. // Find the button by role (not the label <p>) and click it
  317. const syncButtons = screen.getAllByRole('button', { name: /Sync Spoolman Weights from AMS/i });
  318. await user.click(syncButtons[0]);
  319. await waitFor(() => {
  320. expect(screen.getByText('Sync Spoolman Spool Weights from AMS')).toBeInTheDocument();
  321. });
  322. });
  323. });
  324. describe('AMS weight sync with a printer printing', () => {
  325. beforeEach(() => {
  326. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  327. spoolman_enabled: 'true',
  328. spoolman_url: 'http://localhost:7912',
  329. spoolman_sync_mode: 'auto',
  330. spoolman_disable_weight_sync: 'false',
  331. spoolman_report_partial_usage: 'true',
  332. });
  333. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  334. enabled: true,
  335. connected: true,
  336. url: 'http://localhost:7912',
  337. });
  338. vi.mocked(api.syncSpoolmanAmsWeights).mockResolvedValue({ synced: 1, skipped: 2, printing: 2 });
  339. });
  340. it('says which spools were left alone because their printer is printing', async () => {
  341. const user = userEvent.setup();
  342. render(<SpoolmanSettings />);
  343. const syncButtons = await screen.findAllByRole('button', { name: /Sync Spoolman Weights from AMS/i });
  344. await user.click(syncButtons[0]);
  345. const dialogTitle = await screen.findByText('Sync Spoolman Spool Weights from AMS');
  346. const dialog = dialogTitle.closest('div[class*="fixed"]') ?? document.body;
  347. const confirm = Array.from(dialog.querySelectorAll('button')).filter((b) =>
  348. /Sync Spoolman Weights from AMS/i.test(b.textContent ?? ''),
  349. );
  350. await user.click(confirm[confirm.length - 1]);
  351. expect(
  352. await screen.findByText('2 spool(s) on a printer that is printing left alone. Sync again once the print has finished.'),
  353. ).toBeInTheDocument();
  354. });
  355. });
  356. describe('native tag migration (Spoolman 0.27+, #3168)', () => {
  357. const report = {
  358. dry_run: true,
  359. moved: [1, 6],
  360. already: 38,
  361. slot_ids: 1,
  362. conflicts: [
  363. { spool_id: 4, tag: 'A1B2C3D4', holder: 5 },
  364. { spool_id: 9, tag: '11223344', holder: -1 },
  365. ],
  366. };
  367. beforeEach(() => {
  368. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  369. spoolman_enabled: 'true',
  370. spoolman_url: 'http://localhost:7912',
  371. spoolman_sync_mode: 'auto',
  372. spoolman_disable_weight_sync: 'false',
  373. spoolman_report_partial_usage: 'true',
  374. });
  375. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  376. enabled: true,
  377. connected: true,
  378. url: 'http://localhost:7912',
  379. native_tags: true,
  380. });
  381. vi.mocked(api.migrateSpoolmanTags).mockImplementation(async (dryRun: boolean) =>
  382. dryRun ? report : { ...report, dry_run: false },
  383. );
  384. });
  385. it('is not offered by a Spoolman without native tags', async () => {
  386. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  387. enabled: true,
  388. connected: true,
  389. url: 'http://localhost:7912',
  390. native_tags: false,
  391. });
  392. render(<SpoolmanSettings />);
  393. await waitFor(() => {
  394. expect(screen.getByText('Sync AMS Data')).toBeInTheDocument();
  395. });
  396. expect(screen.queryByText('Spoolman native tags')).not.toBeInTheDocument();
  397. });
  398. it('checks first and copies nothing until asked to', async () => {
  399. const user = userEvent.setup();
  400. render(<SpoolmanSettings />);
  401. await user.click(await screen.findByRole('button', { name: /Check tags/i }));
  402. expect(await screen.findByText('To copy')).toBeInTheDocument();
  403. expect(api.migrateSpoolmanTags).toHaveBeenCalledTimes(1);
  404. expect(api.migrateSpoolmanTags).toHaveBeenCalledWith(true);
  405. expect(screen.getByText('Spool #4: tag A1B2C3D4 already belongs to spool #5')).toBeInTheDocument();
  406. expect(screen.getByText('Spool #9: tag 11223344 already belongs to a filament or location')).toBeInTheDocument();
  407. expect(screen.getByRole('button', { name: /Copy tags/i })).toBeInTheDocument();
  408. });
  409. it('copies the tags the check named', async () => {
  410. const user = userEvent.setup();
  411. render(<SpoolmanSettings />);
  412. await user.click(await screen.findByRole('button', { name: /Check tags/i }));
  413. await user.click(await screen.findByRole('button', { name: /Copy tags/i }));
  414. await waitFor(() => {
  415. expect(api.migrateSpoolmanTags).toHaveBeenLastCalledWith(false);
  416. });
  417. expect(await screen.findByText('Tags copied to Spoolman: 2')).toBeInTheDocument();
  418. // Nothing is left to copy, so the button to do it is gone.
  419. expect(screen.queryByRole('button', { name: /Copy tags/i })).not.toBeInTheDocument();
  420. });
  421. it('says so when every tag is already in Spoolman', async () => {
  422. vi.mocked(api.migrateSpoolmanTags).mockResolvedValue({
  423. dry_run: true,
  424. moved: [],
  425. already: 39,
  426. slot_ids: 0,
  427. conflicts: [],
  428. });
  429. const user = userEvent.setup();
  430. render(<SpoolmanSettings />);
  431. await user.click(await screen.findByRole('button', { name: /Check tags/i }));
  432. expect(await screen.findByText('Every tag is in Spoolman.')).toBeInTheDocument();
  433. expect(screen.queryByRole('button', { name: /Copy tags/i })).not.toBeInTheDocument();
  434. });
  435. });
  436. });