ConfigureAmsSlotModal.test.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * Tests for the ConfigureAmsSlotModal component.
  3. */
  4. import { describe, it, expect, vi, beforeEach } from 'vitest';
  5. import { screen, fireEvent, waitFor } from '@testing-library/react';
  6. import { render } from '../utils';
  7. import { ConfigureAmsSlotModal } from '../../components/ConfigureAmsSlotModal';
  8. import { api } from '../../api/client';
  9. // Mock the API client
  10. vi.mock('../../api/client', () => ({
  11. api: {
  12. getCloudSettings: vi.fn(),
  13. getKProfiles: vi.fn(),
  14. configureAmsSlot: vi.fn(),
  15. getCloudSettingDetail: vi.fn(),
  16. saveSlotPreset: vi.fn(),
  17. getSettings: vi.fn().mockResolvedValue({}),
  18. updateSettings: vi.fn().mockResolvedValue({}),
  19. getLocalPresets: vi.fn(),
  20. getBuiltinFilaments: vi.fn(),
  21. searchColors: vi.fn(),
  22. getColorCatalog: vi.fn(),
  23. resetAmsSlot: vi.fn(),
  24. },
  25. }));
  26. const mockCloudSettings = {
  27. filament: [
  28. {
  29. setting_id: 'GFSL05_09',
  30. name: 'Bambu PLA Basic @BBL X1C',
  31. filament_id: 'GFL05',
  32. },
  33. {
  34. setting_id: 'PFUScd84f663d2c2ef',
  35. name: '# Overture Matte PLA @BBL H2D',
  36. filament_id: null,
  37. },
  38. ],
  39. };
  40. const mockKProfiles = {
  41. profiles: [
  42. {
  43. id: 1,
  44. name: 'PLA Basic',
  45. k_value: '0.020',
  46. filament_id: 'GFL05',
  47. setting_id: '',
  48. extruder_id: 1,
  49. cali_idx: 1,
  50. },
  51. ],
  52. };
  53. const defaultProps = {
  54. isOpen: true,
  55. onClose: vi.fn(),
  56. printerId: 1,
  57. slotInfo: {
  58. amsId: 0,
  59. trayId: 0,
  60. trayCount: 4,
  61. trayType: 'PLA',
  62. trayColor: 'FFFFFF',
  63. traySubBrands: 'PLA Basic',
  64. },
  65. nozzleDiameter: '0.4',
  66. onSuccess: vi.fn(),
  67. };
  68. describe('ConfigureAmsSlotModal', () => {
  69. beforeEach(() => {
  70. vi.clearAllMocks();
  71. // Mock scrollIntoView which is not available in jsdom
  72. Element.prototype.scrollIntoView = vi.fn();
  73. (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue(mockCloudSettings);
  74. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue(mockKProfiles);
  75. (api.configureAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  76. (api.saveSlotPreset as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  77. (api.getLocalPresets as ReturnType<typeof vi.fn>).mockResolvedValue({ filament: [] });
  78. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  79. (api.searchColors as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  80. (api.getColorCatalog as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  81. (api.resetAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true, message: 'ok' });
  82. });
  83. it('renders nothing visible when closed', () => {
  84. render(<ConfigureAmsSlotModal {...defaultProps} isOpen={false} />);
  85. expect(screen.queryByText('Configure AMS Slot')).not.toBeInTheDocument();
  86. });
  87. it('renders modal when open', async () => {
  88. render(<ConfigureAmsSlotModal {...defaultProps} />);
  89. await waitFor(() => {
  90. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  91. });
  92. });
  93. it('displays basic color buttons', async () => {
  94. render(<ConfigureAmsSlotModal {...defaultProps} />);
  95. await waitFor(() => {
  96. // Check for basic color buttons by their title attribute
  97. expect(screen.getByTitle('White')).toBeInTheDocument();
  98. expect(screen.getByTitle('Black')).toBeInTheDocument();
  99. expect(screen.getByTitle('Red')).toBeInTheDocument();
  100. expect(screen.getByTitle('Blue')).toBeInTheDocument();
  101. expect(screen.getByTitle('Green')).toBeInTheDocument();
  102. expect(screen.getByTitle('Yellow')).toBeInTheDocument();
  103. expect(screen.getByTitle('Orange')).toBeInTheDocument();
  104. expect(screen.getByTitle('Gray')).toBeInTheDocument();
  105. });
  106. });
  107. it('does not show extended colors by default', async () => {
  108. render(<ConfigureAmsSlotModal {...defaultProps} />);
  109. await waitFor(() => {
  110. expect(screen.getByTitle('White')).toBeInTheDocument();
  111. });
  112. // Extended colors should not be visible initially
  113. expect(screen.queryByTitle('Cyan')).not.toBeInTheDocument();
  114. expect(screen.queryByTitle('Purple')).not.toBeInTheDocument();
  115. expect(screen.queryByTitle('Coral')).not.toBeInTheDocument();
  116. });
  117. it('shows extended colors when expand button is clicked', async () => {
  118. render(<ConfigureAmsSlotModal {...defaultProps} />);
  119. await waitFor(() => {
  120. expect(screen.getByTitle('White')).toBeInTheDocument();
  121. });
  122. // Click the expand button (+ button)
  123. const expandButton = screen.getByTitle('Show more colors');
  124. fireEvent.click(expandButton);
  125. // Extended colors should now be visible
  126. await waitFor(() => {
  127. expect(screen.getByTitle('Cyan')).toBeInTheDocument();
  128. expect(screen.getByTitle('Purple')).toBeInTheDocument();
  129. expect(screen.getByTitle('Pink')).toBeInTheDocument();
  130. expect(screen.getByTitle('Brown')).toBeInTheDocument();
  131. expect(screen.getByTitle('Coral')).toBeInTheDocument();
  132. });
  133. });
  134. it('hides extended colors when collapse button is clicked', async () => {
  135. render(<ConfigureAmsSlotModal {...defaultProps} />);
  136. await waitFor(() => {
  137. expect(screen.getByTitle('White')).toBeInTheDocument();
  138. });
  139. // Click the expand button
  140. const expandButton = screen.getByTitle('Show more colors');
  141. fireEvent.click(expandButton);
  142. // Wait for extended colors to appear
  143. await waitFor(() => {
  144. expect(screen.getByTitle('Cyan')).toBeInTheDocument();
  145. });
  146. // Click the collapse button
  147. const collapseButton = screen.getByTitle('Show less colors');
  148. fireEvent.click(collapseButton);
  149. // Extended colors should be hidden again
  150. await waitFor(() => {
  151. expect(screen.queryByTitle('Cyan')).not.toBeInTheDocument();
  152. });
  153. });
  154. it('selects a color when color button is clicked', async () => {
  155. render(<ConfigureAmsSlotModal {...defaultProps} />);
  156. await waitFor(() => {
  157. expect(screen.getByTitle('Red')).toBeInTheDocument();
  158. });
  159. // Click the red color button
  160. const redButton = screen.getByTitle('Red');
  161. fireEvent.click(redButton);
  162. // The color input should now show "Red"
  163. const colorInput = screen.getByPlaceholderText(/Color name or hex/);
  164. expect(colorInput).toHaveValue('Red');
  165. });
  166. it('sends PFUS setting_id as tray_info_idx when cloud detail has filament_id: null (#1053)', async () => {
  167. // Cloud returns a user preset that inherits from a generic Bambu base and
  168. // has no distinct filament_id of its own — this is how Bambu Cloud responds
  169. // for custom presets built on top of "Generic ABS @BBL H2D" etc.
  170. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockResolvedValue({
  171. filament_id: null,
  172. base_id: 'GFSB99_07',
  173. name: '# Overture Matte PLA @BBL H2D',
  174. });
  175. const slotInfo = {
  176. ...defaultProps.slotInfo,
  177. savedPresetId: 'PFUScd84f663d2c2ef',
  178. };
  179. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  180. await waitFor(() => {
  181. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  182. });
  183. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  184. await waitFor(() => {
  185. expect(api.configureAmsSlot).toHaveBeenCalled();
  186. });
  187. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  188. // Before the fix, this collapsed to 'GFB99' (Generic ABS's filament_id),
  189. // which made OrcaSlicer/BambuStudio Sync Filaments resolve to "Generic ABS".
  190. expect(payload.tray_info_idx).toBe('PFUScd84f663d2c2ef');
  191. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  192. });
  193. it('uses cloud detail filament_id when present', async () => {
  194. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockResolvedValue({
  195. filament_id: 'P285e239',
  196. base_id: 'GFSB99_07',
  197. name: '# Overture Matte PLA @BBL H2D',
  198. });
  199. const slotInfo = {
  200. ...defaultProps.slotInfo,
  201. savedPresetId: 'PFUScd84f663d2c2ef',
  202. };
  203. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  204. await waitFor(() => {
  205. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  206. });
  207. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  208. await waitFor(() => {
  209. expect(api.configureAmsSlot).toHaveBeenCalled();
  210. });
  211. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  212. expect(payload.tray_info_idx).toBe('P285e239');
  213. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  214. });
  215. it('sends short GF filament_id for Bambu GFS* presets (cloud detail not consulted)', async () => {
  216. // Bambu-provided presets (GFS*) convert the setting_id → filament_id locally.
  217. // The cloud detail endpoint must NOT be consulted for them; the rewrite that
  218. // fixed #1053 preserves this pre-existing shortcut.
  219. const slotInfo = {
  220. ...defaultProps.slotInfo,
  221. savedPresetId: 'GFSL05_09',
  222. };
  223. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  224. await waitFor(() => {
  225. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  226. });
  227. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  228. await waitFor(() => {
  229. expect(api.configureAmsSlot).toHaveBeenCalled();
  230. });
  231. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  232. expect(payload.tray_info_idx).toBe('GFL05');
  233. expect(payload.setting_id).toBe('GFSL05_09');
  234. expect(api.getCloudSettingDetail).not.toHaveBeenCalled();
  235. });
  236. it('keeps default PFUS tray_info_idx when cloud detail fetch fails', async () => {
  237. // Network/5xx from /cloud/settings/{id} must not abort the configure flow
  238. // nor leave tray_info_idx empty — we fall back to the setting_id default.
  239. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockRejectedValue(
  240. new Error('cloud unreachable')
  241. );
  242. const slotInfo = {
  243. ...defaultProps.slotInfo,
  244. savedPresetId: 'PFUScd84f663d2c2ef',
  245. };
  246. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  247. await waitFor(() => {
  248. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  249. });
  250. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  251. await waitFor(() => {
  252. expect(api.configureAmsSlot).toHaveBeenCalled();
  253. });
  254. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  255. expect(payload.tray_info_idx).toBe('PFUScd84f663d2c2ef');
  256. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  257. });
  258. it('renders configure slot button', async () => {
  259. render(<ConfigureAmsSlotModal {...defaultProps} />);
  260. await waitFor(() => {
  261. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  262. });
  263. // Find the Configure Slot button
  264. const configureButton = screen.getByRole('button', { name: /Configure Slot/i });
  265. expect(configureButton).toBeInTheDocument();
  266. });
  267. it('filters presets by printer model', async () => {
  268. // Render with printerModel="H2D"
  269. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="H2D" />);
  270. // Wait for presets to load - the H2D preset should be visible
  271. await waitFor(() => {
  272. expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
  273. });
  274. // The X1C preset should NOT be visible (filtered out by model)
  275. expect(screen.queryByText(/Bambu PLA Basic @BBL X1C/)).not.toBeInTheDocument();
  276. });
  277. it('shows current preset even when it does not match model filter', async () => {
  278. // Render with printerModel="H2D" but savedPresetId pointing to the X1C preset
  279. const slotInfo = {
  280. ...defaultProps.slotInfo,
  281. savedPresetId: 'GFSL05_09', // X1C preset
  282. };
  283. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} printerModel="H2D" />);
  284. await waitFor(() => {
  285. // Both should be visible - H2D matches model, X1C is saved preset
  286. // Use the full preset name to match the list item (not the "Filtering for" label)
  287. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  288. expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
  289. });
  290. });
  291. it('preset row expands inline on hover so the full name is readable (#1237)', async () => {
  292. // Long preset names (e.g. "SUNLU PETG GLOW IN THE DARK GEN2 @Bambu Lab H2C 0.4 nozzle")
  293. // get visually truncated; the row un-truncates on hover via group-hover so the
  294. // nozzle suffix is readable without waiting on the browser's title-tooltip delay,
  295. // and the title attribute remains as a fallback for assistive tech / touch.
  296. render(<ConfigureAmsSlotModal {...defaultProps} />);
  297. await waitFor(() => {
  298. const fullName = 'Bambu PLA Basic @BBL X1C';
  299. const span = screen.getByText(fullName);
  300. expect(span).toHaveAttribute('title', fullName);
  301. expect(span).toHaveClass('truncate');
  302. expect(span).toHaveClass('group-hover:whitespace-normal');
  303. expect(span).toHaveClass('group-hover:break-all');
  304. expect(span.closest('button')).toHaveClass('group');
  305. });
  306. });
  307. it('pre-selects saved preset when opening configured slot', async () => {
  308. const slotInfo = {
  309. ...defaultProps.slotInfo,
  310. savedPresetId: 'GFSL05_09',
  311. };
  312. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  313. await waitFor(() => {
  314. // The saved preset should have the selected style (green border)
  315. // Use the full preset name to avoid matching the "Filtering for" label
  316. const presetButton = screen.getByText('Bambu PLA Basic @BBL X1C').closest('button');
  317. expect(presetButton).toHaveClass('bg-bambu-green/20');
  318. });
  319. });
  320. it('pre-populates color from trayColor', async () => {
  321. const slotInfo = {
  322. ...defaultProps.slotInfo,
  323. trayColor: 'FF0000FF', // Red with alpha
  324. };
  325. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  326. await waitFor(() => {
  327. expect(screen.getByTitle('White')).toBeInTheDocument();
  328. });
  329. // The hex display should show the pre-populated color
  330. expect(screen.getByText('Hex: #FF0000', { exact: false })).toBeInTheDocument();
  331. });
  332. it('uses translated text for modal elements', async () => {
  333. render(<ConfigureAmsSlotModal {...defaultProps} />);
  334. await waitFor(() => {
  335. expect(screen.getByText('Configure AMS Slot')).toBeInTheDocument();
  336. expect(screen.getByText('Filament Profile')).toBeInTheDocument();
  337. });
  338. // Check footer buttons
  339. expect(screen.getByRole('button', { name: /Configure Slot/i })).toBeInTheDocument();
  340. expect(screen.getByRole('button', { name: /Cancel/i })).toBeInTheDocument();
  341. expect(screen.getByRole('button', { name: /Reset Slot/i })).toBeInTheDocument();
  342. });
  343. });