SpoolmanSettings.test.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 Disconnect button when connected', async () => {
  171. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  172. enabled: true,
  173. connected: true,
  174. url: 'http://localhost:7912',
  175. });
  176. render(<SpoolmanSettings />);
  177. await waitFor(() => {
  178. expect(screen.getByText('Connected')).toBeInTheDocument();
  179. expect(screen.getByText('Disconnect')).toBeInTheDocument();
  180. });
  181. });
  182. it('shows sync section when connected', async () => {
  183. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  184. enabled: true,
  185. connected: true,
  186. url: 'http://localhost:7912',
  187. });
  188. render(<SpoolmanSettings />);
  189. await waitFor(() => {
  190. expect(screen.getByText('Sync AMS Data')).toBeInTheDocument();
  191. });
  192. });
  193. });
  194. describe('weight sync toggle', () => {
  195. it('shows weight sync toggle when Spoolman enabled and sync mode is auto', async () => {
  196. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  197. spoolman_enabled: 'true',
  198. spoolman_url: 'http://localhost:7912',
  199. spoolman_sync_mode: 'auto',
  200. spoolman_disable_weight_sync: 'false',
  201. spoolman_report_partial_usage: 'true',
  202. });
  203. render(<SpoolmanSettings />);
  204. await waitFor(() => {
  205. expect(screen.getByText('Disable AMS Estimated Weight Sync')).toBeInTheDocument();
  206. });
  207. });
  208. it('does not show weight sync toggle when sync mode is manual', async () => {
  209. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  210. spoolman_enabled: 'true',
  211. spoolman_url: 'http://localhost:7912',
  212. spoolman_sync_mode: 'manual',
  213. spoolman_disable_weight_sync: 'false',
  214. spoolman_report_partial_usage: 'true',
  215. });
  216. render(<SpoolmanSettings />);
  217. await waitFor(() => {
  218. expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
  219. });
  220. expect(screen.queryByText('Disable AMS Estimated Weight Sync')).not.toBeInTheDocument();
  221. });
  222. });
  223. describe('partial usage toggle', () => {
  224. it('shows partial usage toggle when weight sync is disabled', async () => {
  225. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  226. spoolman_enabled: 'true',
  227. spoolman_url: 'http://localhost:7912',
  228. spoolman_sync_mode: 'auto',
  229. spoolman_disable_weight_sync: 'true',
  230. spoolman_report_partial_usage: 'true',
  231. });
  232. render(<SpoolmanSettings />);
  233. await waitFor(() => {
  234. expect(screen.getByText('Report Partial Usage for Failed Prints')).toBeInTheDocument();
  235. });
  236. });
  237. it('does not show partial usage toggle when weight sync is enabled', async () => {
  238. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  239. spoolman_enabled: 'true',
  240. spoolman_url: 'http://localhost:7912',
  241. spoolman_sync_mode: 'auto',
  242. spoolman_disable_weight_sync: 'false',
  243. spoolman_report_partial_usage: 'true',
  244. });
  245. render(<SpoolmanSettings />);
  246. await waitFor(() => {
  247. expect(screen.getByText('Filament Tracking')).toBeInTheDocument();
  248. });
  249. expect(screen.queryByText('Report Partial Usage for Failed Prints')).not.toBeInTheDocument();
  250. });
  251. });
  252. describe('mode switching', () => {
  253. it('can switch to Spoolman mode', async () => {
  254. const user = userEvent.setup();
  255. render(<SpoolmanSettings />);
  256. await waitFor(() => {
  257. expect(screen.getByText('Built-in Inventory')).toBeInTheDocument();
  258. });
  259. // Click Spoolman card
  260. await user.click(screen.getByText('Spoolman').closest('button')!);
  261. // Spoolman settings should now be visible
  262. await waitFor(() => {
  263. expect(screen.getByPlaceholderText('http://192.168.1.100:7912')).toBeInTheDocument();
  264. });
  265. });
  266. });
  267. describe('Spoolman AMS weight sync button', () => {
  268. beforeEach(() => {
  269. vi.mocked(api.getSpoolmanSettings).mockResolvedValue({
  270. spoolman_enabled: 'true',
  271. spoolman_url: 'http://localhost:7912',
  272. spoolman_sync_mode: 'auto',
  273. spoolman_disable_weight_sync: 'false',
  274. spoolman_report_partial_usage: 'true',
  275. });
  276. vi.mocked(api.getSpoolmanStatus).mockResolvedValue({
  277. enabled: true,
  278. connected: true,
  279. url: 'http://localhost:7912',
  280. });
  281. vi.mocked(api.syncSpoolmanAmsWeights).mockResolvedValue({ synced: 2, skipped: 1 });
  282. });
  283. it('shows Spoolman AMS sync button when connected', async () => {
  284. render(<SpoolmanSettings />);
  285. // Text appears in both the label <p> and the button — use getAllByText
  286. await waitFor(() => {
  287. const elements = screen.getAllByText('Sync Spoolman Weights from AMS');
  288. expect(elements.length).toBeGreaterThanOrEqual(1);
  289. });
  290. });
  291. it('opens confirm modal when Spoolman AMS sync button clicked', async () => {
  292. const user = userEvent.setup();
  293. render(<SpoolmanSettings />);
  294. // Wait for the button section to appear
  295. await waitFor(() => {
  296. const elements = screen.getAllByText('Sync Spoolman Weights from AMS');
  297. expect(elements.length).toBeGreaterThanOrEqual(1);
  298. });
  299. // Find the button by role (not the label <p>) and click it
  300. const syncButtons = screen.getAllByRole('button', { name: /Sync Spoolman Weights from AMS/i });
  301. await user.click(syncButtons[0]);
  302. await waitFor(() => {
  303. expect(screen.getByText('Sync Spoolman Spool Weights from AMS')).toBeInTheDocument();
  304. });
  305. });
  306. });
  307. });