SpoolmanSettings.test.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. disconnectSpoolman: vi.fn(),
  25. syncAllPrintersAms: vi.fn(),
  26. syncPrinterAms: vi.fn(),
  27. syncSpoolmanAmsWeights: 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.disconnectSpoolman).mockResolvedValue({ success: true, message: 'Disconnected' });
  60. vi.mocked(api.syncAllPrintersAms).mockResolvedValue({
  61. success: true,
  62. synced_count: 3,
  63. skipped_count: 1,
  64. skipped: [],
  65. errors: [],
  66. });
  67. });
  68. describe('rendering', () => {
  69. it('renders loading state initially', () => {
  70. vi.mocked(api.getSpoolmanSettings).mockImplementation(() => new Promise(() => {}));
  71. render(<SpoolmanSettings />);
  72. expect(document.querySelector('.animate-spin')).toBeInTheDocument();
  73. });
  74. it('renders filament tracking title', async () => {
  75. render(<SpoolmanSettings />);
  76. await waitFor(() => {
  77. expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
  78. });
  79. });
  80. it('renders mode selector cards', async () => {
  81. render(<SpoolmanSettings />);
  82. await waitFor(() => {
  83. expect(screen.getByText('Built-in Inventory')).toBeInTheDocument();
  84. expect(screen.getByText('Spoolman')).toBeInTheDocument();
  85. });
  86. });
  87. });
  88. describe('built-in inventory mode (default)', () => {
  89. it('shows built-in inventory as selected by default', async () => {
  90. render(<SpoolmanSettings />);
  91. await waitFor(() => {
  92. // Built-in Inventory card should have the active border
  93. const builtInBtn = screen.getByText('Built-in Inventory').closest('button');
  94. expect(builtInBtn).toHaveClass('border-bambu-green');
  95. });
  96. });
  97. it('shows built-in info panel when selected', async () => {
  98. render(<SpoolmanSettings />);
  99. await waitFor(() => {
  100. expect(screen.getByText(/Automatically detects Bambu Lab RFID spools/)).toBeInTheDocument();
  101. });
  102. });
  103. it('does not show Spoolman URL input', async () => {
  104. render(<SpoolmanSettings />);
  105. await waitFor(() => {
  106. expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
  107. });
  108. expect(screen.queryByPlaceholderText('http://192.168.1.100:7912')).not.toBeInTheDocument();
  109. });
  110. });
  111. describe('spoolman mode', () => {
  112. beforeEach(() => {
  113. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  114. spoolman_enabled: 'true',
  115. spoolman_url: 'http://localhost:7912',
  116. spoolman_sync_mode: 'auto',
  117. spoolman_disable_weight_sync: 'false',
  118. spoolman_report_partial_usage: 'true',
  119. });
  120. vi.mocked(api.updateSpoolmanSettings).mockResolvedValue({
  121. spoolman_enabled: 'true',
  122. spoolman_url: 'http://localhost:7912',
  123. spoolman_sync_mode: 'auto',
  124. spoolman_disable_weight_sync: 'false',
  125. spoolman_report_partial_usage: 'true',
  126. });
  127. });
  128. it('shows Spoolman card as selected', async () => {
  129. render(<SpoolmanSettings />);
  130. await waitFor(() => {
  131. const spoolmanBtn = screen.getByText('Spoolman').closest('button');
  132. expect(spoolmanBtn).toHaveClass('border-bambu-green');
  133. });
  134. });
  135. it('shows URL input when Spoolman is selected', async () => {
  136. render(<SpoolmanSettings />);
  137. await waitFor(() => {
  138. expect(screen.getByPlaceholderText('http://192.168.1.100:7912')).toBeInTheDocument();
  139. });
  140. });
  141. it('shows sync mode selector', async () => {
  142. render(<SpoolmanSettings />);
  143. await waitFor(() => {
  144. expect(screen.getByText('Sync Mode')).toBeInTheDocument();
  145. });
  146. });
  147. it('shows how sync works info', async () => {
  148. render(<SpoolmanSettings />);
  149. await waitFor(() => {
  150. expect(screen.getByText('How Sync Works')).toBeInTheDocument();
  151. });
  152. });
  153. it('shows connection status section', async () => {
  154. render(<SpoolmanSettings />);
  155. await waitFor(() => {
  156. expect(screen.getByText('Status:')).toBeInTheDocument();
  157. });
  158. });
  159. it('shows Disconnected when not connected', async () => {
  160. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  161. enabled: true,
  162. connected: false,
  163. url: 'http://localhost:7912',
  164. });
  165. render(<SpoolmanSettings />);
  166. await waitFor(() => {
  167. expect(screen.getByText('Disconnected')).toBeInTheDocument();
  168. });
  169. });
  170. it('shows Connected and offers nothing to press when connected', async () => {
  171. // Spoolman is a stateless HTTP API with no session to close, so there is
  172. // nothing for a Disconnect button to disconnect: it dropped this
  173. // process's client object, which the next request rebuilt lazily, and the
  174. // status flipped back on its own (#2903). Turning the integration off is
  175. // the enable toggle's job.
  176. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  177. enabled: true,
  178. connected: true,
  179. url: 'http://localhost:7912',
  180. });
  181. render(<SpoolmanSettings />);
  182. await waitFor(() => {
  183. expect(screen.getByText('Connected')).toBeInTheDocument();
  184. });
  185. expect(screen.queryByText('Disconnect')).not.toBeInTheDocument();
  186. expect(screen.queryByText('Connect')).not.toBeInTheDocument();
  187. });
  188. it('offers Connect as a retry only while Spoolman is unreachable', async () => {
  189. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  190. enabled: true,
  191. connected: false,
  192. url: 'http://localhost:7912',
  193. });
  194. render(<SpoolmanSettings />);
  195. await waitFor(() => {
  196. expect(screen.getByText('Connect')).toBeInTheDocument();
  197. });
  198. expect(screen.queryByText('Disconnect')).not.toBeInTheDocument();
  199. });
  200. it('shows sync section when connected', async () => {
  201. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  202. enabled: true,
  203. connected: true,
  204. url: 'http://localhost:7912',
  205. });
  206. render(<SpoolmanSettings />);
  207. await waitFor(() => {
  208. expect(screen.getByText('Sync AMS Data')).toBeInTheDocument();
  209. });
  210. });
  211. });
  212. describe('weight sync toggle', () => {
  213. it('shows weight sync toggle when Spoolman enabled and sync mode is auto', async () => {
  214. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  215. spoolman_enabled: 'true',
  216. spoolman_url: 'http://localhost:7912',
  217. spoolman_sync_mode: 'auto',
  218. spoolman_disable_weight_sync: 'false',
  219. spoolman_report_partial_usage: 'true',
  220. });
  221. render(<SpoolmanSettings />);
  222. await waitFor(() => {
  223. expect(screen.getByText('Disable AMS Estimated Weight Sync')).toBeInTheDocument();
  224. });
  225. });
  226. it('does not show weight sync toggle when sync mode is manual', async () => {
  227. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  228. spoolman_enabled: 'true',
  229. spoolman_url: 'http://localhost:7912',
  230. spoolman_sync_mode: 'manual',
  231. spoolman_disable_weight_sync: 'false',
  232. spoolman_report_partial_usage: 'true',
  233. });
  234. render(<SpoolmanSettings />);
  235. await waitFor(() => {
  236. expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
  237. });
  238. expect(screen.queryByText('Disable AMS Estimated Weight Sync')).not.toBeInTheDocument();
  239. });
  240. });
  241. describe('partial usage toggle', () => {
  242. it('shows partial usage toggle when weight sync is disabled', async () => {
  243. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  244. spoolman_enabled: 'true',
  245. spoolman_url: 'http://localhost:7912',
  246. spoolman_sync_mode: 'auto',
  247. spoolman_disable_weight_sync: 'true',
  248. spoolman_report_partial_usage: 'true',
  249. });
  250. render(<SpoolmanSettings />);
  251. await waitFor(() => {
  252. expect(screen.getByText('Report Partial Usage for Failed Prints')).toBeInTheDocument();
  253. });
  254. });
  255. it('does not show partial usage toggle when weight sync is enabled', async () => {
  256. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  257. spoolman_enabled: 'true',
  258. spoolman_url: 'http://localhost:7912',
  259. spoolman_sync_mode: 'auto',
  260. spoolman_disable_weight_sync: 'false',
  261. spoolman_report_partial_usage: 'true',
  262. });
  263. render(<SpoolmanSettings />);
  264. await waitFor(() => {
  265. expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
  266. });
  267. expect(screen.queryByText('Report Partial Usage for Failed Prints')).not.toBeInTheDocument();
  268. });
  269. });
  270. describe('mode switching', () => {
  271. it('can switch to Spoolman mode', async () => {
  272. const user = userEvent.setup();
  273. render(<SpoolmanSettings />);
  274. await waitFor(() => {
  275. expect(screen.getByText('Built-in Inventory')).toBeInTheDocument();
  276. });
  277. // Click Spoolman card
  278. await user.click(screen.getByText('Spoolman').closest('button')!);
  279. // Spoolman settings should now be visible
  280. await waitFor(() => {
  281. expect(screen.getByPlaceholderText('http://192.168.1.100:7912')).toBeInTheDocument();
  282. });
  283. });
  284. });
  285. describe('Spoolman AMS weight sync button', () => {
  286. beforeEach(() => {
  287. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  288. spoolman_enabled: 'true',
  289. spoolman_url: 'http://localhost:7912',
  290. spoolman_sync_mode: 'auto',
  291. spoolman_disable_weight_sync: 'false',
  292. spoolman_report_partial_usage: 'true',
  293. });
  294. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  295. enabled: true,
  296. connected: true,
  297. url: 'http://localhost:7912',
  298. });
  299. vi.mocked(api.syncSpoolmanAmsWeights).mockResolvedValue({ synced: 2, skipped: 1 });
  300. });
  301. it('shows Spoolman AMS sync button when connected', async () => {
  302. render(<SpoolmanSettings />);
  303. // Text appears in both the label <p> and the button — use getAllByText
  304. await waitFor(() => {
  305. const elements = screen.getAllByText('Sync Spoolman Weights from AMS');
  306. expect(elements.length).toBeGreaterThanOrEqual(1);
  307. });
  308. });
  309. it('opens confirm modal when Spoolman AMS sync button clicked', async () => {
  310. const user = userEvent.setup();
  311. render(<SpoolmanSettings />);
  312. // Wait for the button section to appear
  313. await waitFor(() => {
  314. const elements = screen.getAllByText('Sync Spoolman Weights from AMS');
  315. expect(elements.length).toBeGreaterThanOrEqual(1);
  316. });
  317. // Find the button by role (not the label <p>) and click it
  318. const syncButtons = screen.getAllByRole('button', { name: /Sync Spoolman Weights from AMS/i });
  319. await user.click(syncButtons[0]);
  320. await waitFor(() => {
  321. expect(screen.getByText('Sync Spoolman Spool Weights from AMS')).toBeInTheDocument();
  322. });
  323. });
  324. });
  325. });