ConfigureAmsSlotModal.test.tsx 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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. // Queried by the modal for the @BBL short-code matcher. Omitted, it is
  19. // undefined here, so the query runs with no queryFn and rejects -- a
  20. // stray render at an unpredictable moment in every one of these tests.
  21. getSlicerPrinterModels: vi.fn().mockResolvedValue({}),
  22. updateSettings: vi.fn().mockResolvedValue({}),
  23. getLocalPresets: vi.fn(),
  24. getBuiltinFilaments: vi.fn(),
  25. orcaCloudListProfiles: vi.fn(),
  26. orcaCloudGetProfile: vi.fn(),
  27. searchColors: vi.fn(),
  28. getColorCatalog: vi.fn(),
  29. resetAmsSlot: vi.fn(),
  30. },
  31. }));
  32. const mockCloudSettings = {
  33. filament: [
  34. {
  35. setting_id: 'GFSL05_09',
  36. name: 'Bambu PLA Basic @BBL X1C',
  37. filament_id: 'GFL05',
  38. },
  39. {
  40. setting_id: 'PFUScd84f663d2c2ef',
  41. name: '# Overture Matte PLA @BBL H2D',
  42. filament_id: null,
  43. },
  44. ],
  45. };
  46. const mockKProfiles = {
  47. profiles: [
  48. {
  49. id: 1,
  50. name: 'PLA Basic',
  51. k_value: '0.020',
  52. filament_id: 'GFL05',
  53. setting_id: '',
  54. extruder_id: 1,
  55. cali_idx: 1,
  56. },
  57. ],
  58. };
  59. const defaultProps = {
  60. isOpen: true,
  61. onClose: vi.fn(),
  62. printerId: 1,
  63. slotInfo: {
  64. amsId: 0,
  65. trayId: 0,
  66. trayCount: 4,
  67. trayType: 'PLA',
  68. trayColor: 'FFFFFF',
  69. traySubBrands: 'PLA Basic',
  70. },
  71. nozzleDiameter: '0.4',
  72. onSuccess: vi.fn(),
  73. };
  74. describe('ConfigureAmsSlotModal', () => {
  75. beforeEach(() => {
  76. vi.clearAllMocks();
  77. // Mock scrollIntoView which is not available in jsdom
  78. Element.prototype.scrollIntoView = vi.fn();
  79. (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue(mockCloudSettings);
  80. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue(mockKProfiles);
  81. (api.configureAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  82. (api.saveSlotPreset as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  83. (api.getLocalPresets as ReturnType<typeof vi.fn>).mockResolvedValue({ filament: [] });
  84. (api.orcaCloudListProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  85. filament: [], printer: [], process: [],
  86. });
  87. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  88. (api.searchColors as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  89. (api.getColorCatalog as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  90. (api.resetAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true, message: 'ok' });
  91. });
  92. it('renders nothing visible when closed', () => {
  93. render(<ConfigureAmsSlotModal {...defaultProps} isOpen={false} />);
  94. expect(screen.queryByText('Configure AMS Slot')).not.toBeInTheDocument();
  95. });
  96. it('renders a clear tray as the transparency checkerboard rather than solid black (#2912)', () => {
  97. render(
  98. <ConfigureAmsSlotModal
  99. {...defaultProps}
  100. slotInfo={{ ...defaultProps.slotInfo, trayType: 'PETG', trayColor: '00000000' }}
  101. />,
  102. );
  103. const styled = Array.from(document.querySelectorAll<HTMLElement>('[style]'));
  104. expect(
  105. styled.some((el) => el.style.backgroundImage.includes('repeating-conic-gradient')),
  106. ).toBe(true);
  107. // The regression this pins: the alpha byte was sliced off before painting,
  108. // so a clear tray rendered as opaque black — the reported symptom, in the UI.
  109. expect(styled.some((el) => el.style.backgroundColor === 'rgb(0, 0, 0)')).toBe(false);
  110. });
  111. it('renders modal when open', async () => {
  112. render(<ConfigureAmsSlotModal {...defaultProps} />);
  113. await waitFor(() => {
  114. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  115. });
  116. });
  117. it('displays basic color buttons', async () => {
  118. render(<ConfigureAmsSlotModal {...defaultProps} />);
  119. await waitFor(() => {
  120. // Check for basic color buttons by their title attribute
  121. expect(screen.getByTitle('White')).toBeInTheDocument();
  122. expect(screen.getByTitle('Black')).toBeInTheDocument();
  123. expect(screen.getByTitle('Red')).toBeInTheDocument();
  124. expect(screen.getByTitle('Blue')).toBeInTheDocument();
  125. expect(screen.getByTitle('Green')).toBeInTheDocument();
  126. expect(screen.getByTitle('Yellow')).toBeInTheDocument();
  127. expect(screen.getByTitle('Orange')).toBeInTheDocument();
  128. expect(screen.getByTitle('Gray')).toBeInTheDocument();
  129. });
  130. });
  131. it('does not show extended colors by default', async () => {
  132. render(<ConfigureAmsSlotModal {...defaultProps} />);
  133. await waitFor(() => {
  134. expect(screen.getByTitle('White')).toBeInTheDocument();
  135. });
  136. // Extended colors should not be visible initially
  137. expect(screen.queryByTitle('Cyan')).not.toBeInTheDocument();
  138. expect(screen.queryByTitle('Purple')).not.toBeInTheDocument();
  139. expect(screen.queryByTitle('Coral')).not.toBeInTheDocument();
  140. });
  141. it('shows extended colors when expand button is clicked', async () => {
  142. render(<ConfigureAmsSlotModal {...defaultProps} />);
  143. await waitFor(() => {
  144. expect(screen.getByTitle('White')).toBeInTheDocument();
  145. });
  146. // Click the expand button (+ button)
  147. const expandButton = screen.getByTitle('Show more colors');
  148. fireEvent.click(expandButton);
  149. // Extended colors should now be visible
  150. await waitFor(() => {
  151. expect(screen.getByTitle('Cyan')).toBeInTheDocument();
  152. expect(screen.getByTitle('Purple')).toBeInTheDocument();
  153. expect(screen.getByTitle('Pink')).toBeInTheDocument();
  154. expect(screen.getByTitle('Brown')).toBeInTheDocument();
  155. expect(screen.getByTitle('Coral')).toBeInTheDocument();
  156. });
  157. });
  158. it('hides extended colors when collapse button is clicked', async () => {
  159. render(<ConfigureAmsSlotModal {...defaultProps} />);
  160. await waitFor(() => {
  161. expect(screen.getByTitle('White')).toBeInTheDocument();
  162. });
  163. // Click the expand button
  164. const expandButton = screen.getByTitle('Show more colors');
  165. fireEvent.click(expandButton);
  166. // Wait for extended colors to appear
  167. await waitFor(() => {
  168. expect(screen.getByTitle('Cyan')).toBeInTheDocument();
  169. });
  170. // Click the collapse button
  171. const collapseButton = screen.getByTitle('Show less colors');
  172. fireEvent.click(collapseButton);
  173. // Extended colors should be hidden again
  174. await waitFor(() => {
  175. expect(screen.queryByTitle('Cyan')).not.toBeInTheDocument();
  176. });
  177. });
  178. it('selects a color when color button is clicked', async () => {
  179. render(<ConfigureAmsSlotModal {...defaultProps} />);
  180. await waitFor(() => {
  181. expect(screen.getByTitle('Red')).toBeInTheDocument();
  182. });
  183. // Click the red color button
  184. const redButton = screen.getByTitle('Red');
  185. fireEvent.click(redButton);
  186. // The color input should now show "Red"
  187. const colorInput = screen.getByPlaceholderText(/Color name or hex/);
  188. expect(colorInput).toHaveValue('Red');
  189. });
  190. it('sends no tray_info_idx when cloud detail has filament_id: null (#3003)', async () => {
  191. // Cloud returns a user preset that inherits from a generic Bambu base and
  192. // has no distinct filament_id of its own — this is how Bambu Cloud responds
  193. // for custom presets built on top of "Generic ABS @BBL H2D" etc.
  194. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockResolvedValue({
  195. filament_id: null,
  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. // #1053 sent the PFUS here instead, to stop the slot collapsing to
  213. // "Generic ABS" in the slicer. The A1 capture in #3003 showed that does not
  214. // work: tray_info_idx is 8 characters on the printer, so an 18-character
  215. // PFUS is stored truncated (PFUScd84 here) and acknowledged as a success.
  216. // The slot then resolves to nothing — "Generic" anyway, plus a calibration
  217. // table keyed by an id that does not exist. Sending nothing lets the
  218. // backend pick a filament id the printer can actually hold; setting_id
  219. // still carries the preset.
  220. expect(payload.tray_info_idx).toBe('');
  221. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  222. });
  223. it('uses an Orca Cloud profile\'s own filament_id instead of a generic (#3003)', async () => {
  224. // Orca profile ids are UUIDs the printer cannot hold, so this branch used
  225. // to skip the lookup entirely and send a generic — every Orca custom
  226. // filament reached the slicer as "Generic PLA". The profile's slicer JSON
  227. // carries a filament_id of exactly the storable shape; use it.
  228. const ORCA_ID = '3f2a9c1e-4b7d-4a02-9f61-8c5e2d1a7b30';
  229. (api.orcaCloudListProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  230. filament: [{ setting_id: ORCA_ID, name: 'Overture Matte PLA @Orca', type: 'filament', is_custom: true }],
  231. printer: [],
  232. process: [],
  233. });
  234. (api.orcaCloudGetProfile as ReturnType<typeof vi.fn>).mockResolvedValue({
  235. setting_id: ORCA_ID,
  236. name: 'Overture Matte PLA @Orca',
  237. type: 'filament',
  238. setting: { filament_id: 'P56e1be0', filament_type: ['PLA'] },
  239. });
  240. const slotInfo = { ...defaultProps.slotInfo, savedPresetId: ORCA_ID };
  241. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  242. await waitFor(() => {
  243. expect(screen.getByText('Overture Matte PLA @Orca')).toBeInTheDocument();
  244. });
  245. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  246. await waitFor(() => {
  247. expect(api.configureAmsSlot).toHaveBeenCalled();
  248. });
  249. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  250. expect(payload.tray_info_idx).toBe('P56e1be0');
  251. // The UUID is what the slicer cannot resolve; it goes in neither field.
  252. expect(payload.setting_id).toBe('');
  253. });
  254. it('falls back to a generic when an Orca profile has no filament_id (#3003)', async () => {
  255. const ORCA_ID = '3f2a9c1e-4b7d-4a02-9f61-8c5e2d1a7b30';
  256. (api.orcaCloudListProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  257. filament: [{ setting_id: ORCA_ID, name: 'Homebrew PLA @Orca', type: 'filament', is_custom: true }],
  258. printer: [],
  259. process: [],
  260. });
  261. (api.orcaCloudGetProfile as ReturnType<typeof vi.fn>).mockResolvedValue({
  262. setting_id: ORCA_ID,
  263. name: 'Homebrew PLA @Orca',
  264. type: 'filament',
  265. setting: {},
  266. });
  267. const slotInfo = { ...defaultProps.slotInfo, savedPresetId: ORCA_ID };
  268. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  269. await waitFor(() => {
  270. expect(screen.getByText('Homebrew PLA @Orca')).toBeInTheDocument();
  271. });
  272. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  273. await waitFor(() => {
  274. expect(api.configureAmsSlot).toHaveBeenCalled();
  275. });
  276. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  277. expect(payload.tray_info_idx).toBe('GFL99');
  278. expect(payload.setting_id).toBe('');
  279. });
  280. it('uses the filament_id nested under setting when the envelope has none (#3003)', async () => {
  281. // Bambu Cloud returns a custom preset's own filament_id on the envelope for
  282. // some presets and inside the preset JSON for others. Only the envelope was
  283. // read, so presets of the second shape reached the slot as their inherited
  284. // base — the "Bambu profile instead of my profile" half of #3003.
  285. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockResolvedValue({
  286. filament_id: null,
  287. base_id: 'GFSB99_07',
  288. name: '# Overture Matte PLA @BBL H2D',
  289. setting: { filament_id: 'P4d64437' },
  290. });
  291. const slotInfo = {
  292. ...defaultProps.slotInfo,
  293. savedPresetId: 'PFUScd84f663d2c2ef',
  294. };
  295. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  296. await waitFor(() => {
  297. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  298. });
  299. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  300. await waitFor(() => {
  301. expect(api.configureAmsSlot).toHaveBeenCalled();
  302. });
  303. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  304. // 8 characters, so the printer stores it whole and the slicer matches it.
  305. expect(payload.tray_info_idx).toBe('P4d64437');
  306. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  307. });
  308. it('uses cloud detail filament_id when present', async () => {
  309. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockResolvedValue({
  310. filament_id: 'P285e239',
  311. base_id: 'GFSB99_07',
  312. name: '# Overture Matte PLA @BBL H2D',
  313. });
  314. const slotInfo = {
  315. ...defaultProps.slotInfo,
  316. savedPresetId: 'PFUScd84f663d2c2ef',
  317. };
  318. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  319. await waitFor(() => {
  320. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  321. });
  322. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  323. await waitFor(() => {
  324. expect(api.configureAmsSlot).toHaveBeenCalled();
  325. });
  326. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  327. expect(payload.tray_info_idx).toBe('P285e239');
  328. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  329. });
  330. it('sends short GF filament_id for Bambu GFS* presets (cloud detail not consulted)', async () => {
  331. // Bambu-provided presets (GFS*) convert the setting_id → filament_id locally.
  332. // The cloud detail endpoint must NOT be consulted for them; the rewrite that
  333. // fixed #1053 preserves this pre-existing shortcut.
  334. const slotInfo = {
  335. ...defaultProps.slotInfo,
  336. savedPresetId: 'GFSL05_09',
  337. };
  338. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  339. await waitFor(() => {
  340. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  341. });
  342. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  343. await waitFor(() => {
  344. expect(api.configureAmsSlot).toHaveBeenCalled();
  345. });
  346. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  347. expect(payload.tray_info_idx).toBe('GFL05');
  348. expect(payload.setting_id).toBe('GFSL05_09');
  349. expect(api.getCloudSettingDetail).not.toHaveBeenCalled();
  350. });
  351. it('sends no tray_info_idx when the cloud detail fetch fails (#3003)', async () => {
  352. // Network/5xx from /cloud/settings/{id} must not abort the configure flow.
  353. // It leaves no filament_id to send, and the PFUS default is not a usable
  354. // stand-in (see the filament_id: null case above), so the field goes out
  355. // empty and the backend resolves it.
  356. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockRejectedValue(
  357. new Error('cloud unreachable')
  358. );
  359. const slotInfo = {
  360. ...defaultProps.slotInfo,
  361. savedPresetId: 'PFUScd84f663d2c2ef',
  362. };
  363. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  364. await waitFor(() => {
  365. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  366. });
  367. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  368. await waitFor(() => {
  369. expect(api.configureAmsSlot).toHaveBeenCalled();
  370. });
  371. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  372. expect(payload.tray_info_idx).toBe('');
  373. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  374. });
  375. it('renders configure slot button', async () => {
  376. render(<ConfigureAmsSlotModal {...defaultProps} />);
  377. await waitFor(() => {
  378. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  379. });
  380. // Find the Configure Slot button
  381. const configureButton = screen.getByRole('button', { name: /Configure Slot/i });
  382. expect(configureButton).toBeInTheDocument();
  383. });
  384. it('filters presets by printer model', async () => {
  385. // Render with printerModel="H2D"
  386. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="H2D" />);
  387. // Wait for presets to load - the H2D preset should be visible
  388. await waitFor(() => {
  389. expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
  390. });
  391. // The X1C preset should NOT be visible (filtered out by model)
  392. expect(screen.queryByText(/Bambu PLA Basic @BBL X1C/)).not.toBeInTheDocument();
  393. });
  394. it('treats Bambu cloud rename @BBL A1M as a match for A1 Mini (#1649)', async () => {
  395. // Bambu cloud shifted A1 Mini filament profiles from
  396. // "Bambu PLA Basic @BBL A1 Mini ..." to the terse "@BBL A1M" mid-2026.
  397. // Without an alias-aware compare, the model filter strips every cloud
  398. // profile from the picker when the user selects an A1 Mini printer.
  399. (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue({
  400. filament: [
  401. { setting_id: 'GFA00_A1M', name: 'Bambu PLA Basic @BBL A1M', filament_id: 'GFA00' },
  402. { setting_id: 'GFA00_A1', name: 'Bambu PLA Basic @BBL A1', filament_id: 'GFA00' },
  403. ],
  404. });
  405. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="A1 Mini" />);
  406. await waitFor(() => {
  407. expect(screen.getByText('Bambu PLA Basic @BBL A1M')).toBeInTheDocument();
  408. });
  409. // The A1 (non-mini) preset must still be filtered out — the alias
  410. // table must not collapse two physically distinct printers.
  411. expect(screen.queryByText('Bambu PLA Basic @BBL A1')).not.toBeInTheDocument();
  412. });
  413. it('still filters cross-model cloud profiles when the printer is A1 Mini', async () => {
  414. // Sanity check that the alias addition didn't accidentally widen the
  415. // matcher: an X1C cloud preset stays hidden when the picker is for an
  416. // A1 Mini printer.
  417. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="A1 Mini" />);
  418. await waitFor(() => {
  419. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  420. });
  421. expect(screen.queryByText('Bambu PLA Basic @BBL X1C')).not.toBeInTheDocument();
  422. });
  423. it('shows current preset even when it does not match model filter', async () => {
  424. // Render with printerModel="H2D" but savedPresetId pointing to the X1C preset
  425. const slotInfo = {
  426. ...defaultProps.slotInfo,
  427. savedPresetId: 'GFSL05_09', // X1C preset
  428. };
  429. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} printerModel="H2D" />);
  430. await waitFor(() => {
  431. // Both should be visible - H2D matches model, X1C is saved preset
  432. // Use the full preset name to match the list item (not the "Filtering for" label)
  433. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  434. expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
  435. });
  436. });
  437. it('preset row expands inline on hover so the full name is readable (#1237)', async () => {
  438. // Long preset names (e.g. "SUNLU PETG GLOW IN THE DARK GEN2 @Bambu Lab H2C 0.4 nozzle")
  439. // get visually truncated; the row un-truncates on hover via group-hover so the
  440. // nozzle suffix is readable without waiting on the browser's title-tooltip delay,
  441. // and the title attribute remains as a fallback for assistive tech / touch.
  442. render(<ConfigureAmsSlotModal {...defaultProps} />);
  443. await waitFor(() => {
  444. const fullName = 'Bambu PLA Basic @BBL X1C';
  445. const span = screen.getByText(fullName);
  446. expect(span).toHaveAttribute('title', fullName);
  447. expect(span).toHaveClass('truncate');
  448. expect(span).toHaveClass('group-hover:whitespace-normal');
  449. expect(span).toHaveClass('group-hover:break-all');
  450. expect(span.closest('button')).toHaveClass('group');
  451. });
  452. });
  453. it('pre-selects saved preset when opening configured slot', async () => {
  454. const slotInfo = {
  455. ...defaultProps.slotInfo,
  456. savedPresetId: 'GFSL05_09',
  457. };
  458. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  459. await waitFor(() => {
  460. // The saved preset should have the selected style (green border)
  461. // Use the full preset name to avoid matching the "Filtering for" label
  462. const presetButton = screen.getByText('Bambu PLA Basic @BBL X1C').closest('button');
  463. expect(presetButton).toHaveClass('bg-bambu-green/20');
  464. });
  465. });
  466. it('pre-populates color from trayColor', async () => {
  467. const slotInfo = {
  468. ...defaultProps.slotInfo,
  469. trayColor: 'FF0000FF', // Red with alpha
  470. };
  471. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  472. await waitFor(() => {
  473. expect(screen.getByTitle('White')).toBeInTheDocument();
  474. });
  475. // The hex display should show the pre-populated color
  476. expect(screen.getByText('Hex: #FF0000', { exact: false })).toBeInTheDocument();
  477. });
  478. it('uses translated text for modal elements', async () => {
  479. render(<ConfigureAmsSlotModal {...defaultProps} />);
  480. await waitFor(() => {
  481. expect(screen.getByText('Configure AMS Slot')).toBeInTheDocument();
  482. expect(screen.getByText('Filament Profile')).toBeInTheDocument();
  483. });
  484. // Check footer buttons
  485. expect(screen.getByRole('button', { name: /Configure Slot/i })).toBeInTheDocument();
  486. expect(screen.getByRole('button', { name: /Cancel/i })).toBeInTheDocument();
  487. expect(screen.getByRole('button', { name: /Reset Slot/i })).toBeInTheDocument();
  488. });
  489. it('surfaces a K-profile whose name does not match the preset when filament_id agrees (#1688)', async () => {
  490. // Spool was edited with slicer_filament = "GFSL05_09" (the setting_id form
  491. // for Bambu PLA Basic). The printer has a *custom* K-profile saved on the
  492. // same filament_id, but the user named it something that doesn't include
  493. // "PLA". Pre-fix, the name-only filter dropped it; the id-match path now
  494. // surfaces it because both sides normalise to "GFL05".
  495. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  496. profiles: [
  497. {
  498. slot_id: 3,
  499. extruder_id: 0,
  500. nozzle_id: 'HH00-0.4',
  501. nozzle_diameter: '0.4',
  502. filament_id: 'GFL05',
  503. name: 'my-custom-tune',
  504. k_value: '0.025',
  505. n_coef: '0',
  506. ams_id: 0,
  507. tray_id: 0,
  508. setting_id: '',
  509. },
  510. ],
  511. });
  512. const slotInfo = {
  513. ...defaultProps.slotInfo,
  514. savedPresetId: 'GFSL05_09', // setting_id form for Bambu PLA Basic
  515. };
  516. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  517. await waitFor(() => {
  518. // Renders as an <option> on the K-profile select even though
  519. // "my-custom-tune" doesn't contain "PLA" anywhere.
  520. expect(screen.getByRole('option', { name: /my-custom-tune/ })).toBeInTheDocument();
  521. });
  522. });
  523. it("always includes the slot's currently-active K-profile when name and id don't match (#1689)", async () => {
  524. // Reporter scenario: spool assigned under "Generic PLA" but the slot has
  525. // a custom K-profile (filament_id "GFG98" = PETG-something) actively
  526. // selected via cali_idx. Pre-fix the modal showed "default 0.020"; the
  527. // safety net now surfaces the active profile so Configure Slot reflects
  528. // what the printer is actually using.
  529. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  530. profiles: [
  531. {
  532. slot_id: 7, // matches caliIdx below
  533. extruder_id: 0,
  534. nozzle_id: 'HH00-0.4',
  535. nozzle_diameter: '0.4',
  536. filament_id: 'GFG98', // unrelated to "Generic PLA"
  537. name: 'unrelated-petg-tune',
  538. k_value: '0.030',
  539. n_coef: '0',
  540. ams_id: 0,
  541. tray_id: 0,
  542. setting_id: '',
  543. },
  544. ],
  545. });
  546. const slotInfo = {
  547. ...defaultProps.slotInfo,
  548. savedPresetId: 'GFSL05_09', // Generic PLA preset
  549. caliIdx: 7,
  550. extruderId: 0,
  551. };
  552. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  553. await waitFor(() => {
  554. expect(screen.getByRole('option', { name: /unrelated-petg-tune/ })).toBeInTheDocument();
  555. });
  556. });
  557. it("surfaces the slot's active K-profile when no preset is resolvable (#1689 follow-up)", async () => {
  558. // Repro from Spionkiller01: slot is physically loaded but unconfigured —
  559. // tray_type='', tray_info_idx='', no slot_preset_mappings row — so
  560. // selectedPresetInfo resolves to null. Before the patch the main matcher's
  561. // early return on !selectedPresetInfo skipped past the cali_idx safety net
  562. // entirely; on reopen the dropdown went back to default 0.020 even though
  563. // the printer still holds the active profile at cali_idx=6.
  564. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  565. profiles: [
  566. {
  567. slot_id: 6,
  568. extruder_id: 0,
  569. nozzle_id: 'HH00-0.4',
  570. nozzle_diameter: '0.4',
  571. filament_id: 'GFG98',
  572. name: 'active-on-unconfigured-slot',
  573. k_value: '0.030',
  574. n_coef: '0',
  575. ams_id: 0,
  576. tray_id: 0,
  577. setting_id: '',
  578. },
  579. ],
  580. });
  581. const slotInfo = {
  582. ...defaultProps.slotInfo,
  583. trayType: '',
  584. traySubBrands: '',
  585. caliIdx: 6,
  586. extruderId: 0,
  587. // savedPresetId intentionally omitted — no preset bound yet
  588. };
  589. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  590. await waitFor(() => {
  591. expect(screen.getByRole('option', { name: /active-on-unconfigured-slot/ })).toBeInTheDocument();
  592. });
  593. });
  594. describe('Generic presets and the K-profile picker (#2710)', () => {
  595. // Reporter's printer: nine Flow-Dynamics entries, every one of them
  596. // calibrated against Generic PLA (filament_id GFL99) and named after the
  597. // spool's colour rather than its material. Bambu Studio lists all nine for
  598. // a Generic PLA slot; Bambuddy showed only the one already bound to the
  599. // slot via cali_idx.
  600. const genericPlaProfiles = [
  601. 'Black PLA+', 'Dark Brown', 'Glow', 'Gray', 'Lt Brown',
  602. 'Marble', 'Orange PLA', 'Sunlu White PLA+', 'White PLA+ Duramic',
  603. ].map((name, i) => ({
  604. slot_id: i + 1,
  605. extruder_id: 0,
  606. nozzle_id: 'HH00-0.4',
  607. nozzle_diameter: '0.4',
  608. filament_id: 'GFL99',
  609. name,
  610. k_value: `0.0${30 + i}`,
  611. n_coef: '0',
  612. ams_id: 0,
  613. tray_id: 0,
  614. setting_id: '',
  615. }));
  616. const genericPlaSlot = {
  617. ...defaultProps.slotInfo,
  618. savedPresetId: 'builtin_GFL99',
  619. extruderId: 0,
  620. };
  621. beforeEach(() => {
  622. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  623. { filament_id: 'GFL99', name: 'Generic PLA', filament_type: 'PLA' },
  624. ]);
  625. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  626. profiles: genericPlaProfiles,
  627. });
  628. });
  629. it('offers every Generic PLA profile when the slot preset is Generic PLA', async () => {
  630. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={genericPlaSlot} />);
  631. await waitFor(() => {
  632. expect(screen.getByRole('option', { name: /Dark Brown/ })).toBeInTheDocument();
  633. });
  634. // All nine, not just the one bound via cali_idx — matching the printer's
  635. // own calibration table for GFL99.
  636. for (const profile of genericPlaProfiles) {
  637. expect(
  638. screen.getByRole('option', { name: `${profile.name} (K=${profile.k_value})` }),
  639. ).toBeInTheDocument();
  640. }
  641. });
  642. it('offers them on a freshly reset slot with no active cali_idx', async () => {
  643. // "When I reset the AMS slot, the generic PLA shows no k-values at all."
  644. // With no cali_idx the #1689 safety net has nothing to surface, so the
  645. // list came back empty; the id match has to stand on its own.
  646. render(
  647. <ConfigureAmsSlotModal
  648. {...defaultProps}
  649. slotInfo={{ ...genericPlaSlot, caliIdx: 0 }}
  650. />
  651. );
  652. await waitFor(() => {
  653. expect(screen.getByRole('option', { name: /Marble/ })).toBeInTheDocument();
  654. });
  655. expect(screen.getByRole('option', { name: /Glow/ })).toBeInTheDocument();
  656. });
  657. it('matches on name when the profile carries no filament_id at all', async () => {
  658. // Not every firmware fills filament_id in extrusion_cali_get. "Generic"
  659. // is not a brand, so the name path must fall back to the material
  660. // instead of demanding "GENERIC" in the profile name.
  661. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  662. profiles: [
  663. { ...genericPlaProfiles[0], filament_id: '', name: 'Orange PLA' },
  664. { ...genericPlaProfiles[1], filament_id: '', name: 'Dark Brown' },
  665. ],
  666. });
  667. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={genericPlaSlot} />);
  668. await waitFor(() => {
  669. expect(screen.getByRole('option', { name: /Orange PLA/ })).toBeInTheDocument();
  670. });
  671. });
  672. it('does not sweep generic profiles into a brand preset', async () => {
  673. // Guard against the id path widening: "Bambu PLA Basic" is GFL05, so
  674. // GFL99 profiles must still be filtered out of the matching group and
  675. // only reachable through the explicit "other profiles" group.
  676. render(
  677. <ConfigureAmsSlotModal
  678. {...defaultProps}
  679. slotInfo={{ ...defaultProps.slotInfo, savedPresetId: 'GFSL05_09', extruderId: 0 }}
  680. />
  681. );
  682. await waitFor(() => {
  683. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  684. });
  685. // Every GFL99 profile is demoted to the other group: the brand gate on
  686. // "Bambu" still applies, and none of these names carry it.
  687. for (const profile of genericPlaProfiles) {
  688. const option = screen.getByRole('option', { name: `${profile.name} (K=${profile.k_value})` });
  689. expect(option.closest('optgroup')).toHaveAttribute(
  690. 'label',
  691. 'Other K profiles on this printer',
  692. );
  693. }
  694. });
  695. it("offers the printer's other profiles even when nothing matches the preset", async () => {
  696. // The escape hatch: a PETG preset matches none of the PLA profiles, but
  697. // the user can still reach every profile the printer holds instead of
  698. // being sent to the slicer.
  699. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  700. { filament_id: 'GFG99', name: 'Generic PETG', filament_type: 'PETG' },
  701. ]);
  702. render(
  703. <ConfigureAmsSlotModal
  704. {...defaultProps}
  705. slotInfo={{ ...genericPlaSlot, savedPresetId: 'builtin_GFG99', caliIdx: 0 }}
  706. />
  707. );
  708. await waitFor(() => {
  709. expect(screen.getByRole('option', { name: /Dark Brown/ })).toBeInTheDocument();
  710. });
  711. expect(screen.getByRole('option', { name: /Dark Brown/ }).closest('optgroup')).toHaveAttribute(
  712. 'label',
  713. 'Other K profiles on this printer',
  714. );
  715. });
  716. it('sends the cali_idx of a profile picked from the other-profiles group', async () => {
  717. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  718. { filament_id: 'GFG99', name: 'Generic PETG', filament_type: 'PETG' },
  719. ]);
  720. render(
  721. <ConfigureAmsSlotModal
  722. {...defaultProps}
  723. slotInfo={{ ...genericPlaSlot, savedPresetId: 'builtin_GFG99', caliIdx: 0 }}
  724. />
  725. );
  726. await waitFor(() => {
  727. expect(screen.getByRole('option', { name: /Marble/ })).toBeInTheDocument();
  728. });
  729. const marble = genericPlaProfiles.find(p => p.name === 'Marble')!;
  730. // Option identity carries the extruder, so a filament calibrated on both
  731. // hotends yields two distinguishable entries rather than one.
  732. fireEvent.change(screen.getByRole('combobox'), {
  733. target: { value: `${marble.extruder_id}|${marble.name}|${marble.k_value}` },
  734. });
  735. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  736. await waitFor(() => {
  737. expect(api.configureAmsSlot).toHaveBeenCalled();
  738. });
  739. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  740. expect(payload.cali_idx).toBe(marble.slot_id);
  741. expect(payload.kprofile_filament_id).toBe('GFL99');
  742. });
  743. it('distinguishes two profiles that share a name but differ in K', async () => {
  744. // The picker used to key options by name alone, so same-named profiles
  745. // were indistinguishable and the first always won. The key now also
  746. // carries the extruder, for the same reason one step further out: the
  747. // printer numbers its calibration table per hotend.
  748. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  749. profiles: [
  750. { ...genericPlaProfiles[0], slot_id: 1, name: 'PLA', k_value: '0.020' },
  751. { ...genericPlaProfiles[1], slot_id: 2, name: 'PLA', k_value: '0.045' },
  752. ],
  753. });
  754. render(
  755. <ConfigureAmsSlotModal
  756. {...defaultProps}
  757. slotInfo={{ ...genericPlaSlot, caliIdx: 0 }}
  758. />
  759. );
  760. await waitFor(() => {
  761. expect(screen.getByRole('option', { name: /K=0.045/ })).toBeInTheDocument();
  762. });
  763. fireEvent.change(screen.getByRole('combobox'), { target: { value: '0|PLA|0.045' } });
  764. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  765. await waitFor(() => {
  766. expect(api.configureAmsSlot).toHaveBeenCalled();
  767. });
  768. expect((api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3].cali_idx).toBe(2);
  769. });
  770. });
  771. describe('A slot whose extruder claims no profile (#3044)', () => {
  772. // Reporter's X2D: two AMS 2 Pro, one per hotend, the same filaments in
  773. // both. The printer files one profile per filament rather than one per
  774. // hotend, so the second AMS's slots point at entries tagged extruder 0.
  775. // Scoping the picker to the slot's own extruder left it with nothing:
  776. // the slot read as unconfigured and choosing a profile changed nothing
  777. // the user could see.
  778. const sharedProfiles = [
  779. { name: 'PLA', k_value: '0.021', slot_id: 1 },
  780. { name: 'PETG', k_value: '0.026', slot_id: 4 },
  781. ].map(p => ({
  782. ...p,
  783. extruder_id: 0,
  784. nozzle_id: 'HH00-0.4',
  785. nozzle_diameter: '0.4',
  786. filament_id: 'GFL99',
  787. n_coef: '0',
  788. ams_id: 0,
  789. tray_id: 0,
  790. setting_id: '',
  791. }));
  792. // AMS-B, right-hand hotend, already bound to the PLA profile the A-side
  793. // slots use.
  794. const rightHandSlot = {
  795. ...defaultProps.slotInfo,
  796. amsId: 1,
  797. savedPresetId: 'builtin_GFL99',
  798. extruderId: 1,
  799. caliIdx: 1,
  800. };
  801. beforeEach(() => {
  802. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  803. { filament_id: 'GFL99', name: 'Generic PLA', filament_type: 'PLA' },
  804. ]);
  805. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({ profiles: sharedProfiles });
  806. });
  807. it('offers the profiles as matches rather than an empty picker', async () => {
  808. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={rightHandSlot} />);
  809. await waitFor(() => {
  810. expect(screen.getByRole('option', { name: /PLA \(K=0.021\)/ })).toBeInTheDocument();
  811. });
  812. // Matches are the select's direct children; anything demoted to the
  813. // "Other K profiles" group sits inside an optgroup instead. Being merely
  814. // present is what the slot already had, and it read as unconfigured.
  815. const matches = Array.from(screen.getByRole('combobox').querySelectorAll(':scope > option'))
  816. .map(o => (o as HTMLOptionElement).value)
  817. .filter(Boolean);
  818. expect(matches).toEqual(['0|PLA|0.021', '0|PETG|0.026']);
  819. });
  820. it('shows the slot as already bound to its active profile', async () => {
  821. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={rightHandSlot} />);
  822. await waitFor(() => {
  823. expect(screen.getByRole('option', { name: /PLA \(K=0.021\)/ })).toBeInTheDocument();
  824. });
  825. // Not the "no K profile" placeholder, which is what the slot showed while
  826. // the active profile could not be found on this extruder.
  827. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe('0|PLA|0.021');
  828. });
  829. it('still scopes to the slot own hotend when that hotend has its own profiles', async () => {
  830. // The H2C case behind the scoping: one filament calibrated on both
  831. // hotends, two profiles, one right answer per slot. The fallback is a
  832. // last resort and must not reopen this.
  833. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  834. profiles: [
  835. { ...sharedProfiles[0], extruder_id: 0, k_value: '0.020', slot_id: 1 },
  836. { ...sharedProfiles[0], extruder_id: 1, k_value: '0.018', slot_id: 2 },
  837. ],
  838. });
  839. render(
  840. <ConfigureAmsSlotModal
  841. {...defaultProps}
  842. slotInfo={{ ...rightHandSlot, caliIdx: 2 }}
  843. />
  844. );
  845. await waitFor(() => {
  846. expect(screen.getByRole('option', { name: /K=0.018/ })).toBeInTheDocument();
  847. });
  848. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe('1|PLA|0.018');
  849. // The left hotend's copy is still reachable, but under "Other".
  850. const other = screen.getByRole('option', { name: /K=0.020/ });
  851. expect(other.parentElement?.tagName).toBe('OPTGROUP');
  852. });
  853. it('names each hotend once, not two or three times', async () => {
  854. // The label rendered kProfileNozzleSuffix twice in the matching group and
  855. // three times under "Other", so every option on a dual-nozzle printer
  856. // read "... . Left . Left".
  857. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  858. profiles: [
  859. { ...sharedProfiles[0], extruder_id: 0, k_value: '0.020', slot_id: 1 },
  860. { ...sharedProfiles[0], extruder_id: 1, k_value: '0.018', slot_id: 2 },
  861. ],
  862. });
  863. render(
  864. <ConfigureAmsSlotModal
  865. {...defaultProps}
  866. slotInfo={{ ...rightHandSlot, caliIdx: 2 }}
  867. />
  868. );
  869. await waitFor(() => {
  870. expect(screen.getByRole('option', { name: /K=0.018/ })).toBeInTheDocument();
  871. });
  872. for (const option of screen.getAllByRole('option')) {
  873. // The suffix is the only thing that puts a middot in an option label,
  874. // so counting separators counts how many times the hotend was named.
  875. const separators = (option.textContent ?? '').match(/\u00b7/g) ?? [];
  876. expect(separators.length).toBeLessThanOrEqual(1);
  877. }
  878. });
  879. });
  880. it('does not include the active K-profile when caliIdx is 0 or null (#1689 guard)', async () => {
  881. // cali_idx == 0 / null means no profile is active (printer default 0.020).
  882. // The safety net only triggers for activeIdx > 0 — otherwise unrelated
  883. // profiles whose slot_id happens to equal 0 would leak in.
  884. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  885. profiles: [
  886. {
  887. slot_id: 0,
  888. extruder_id: 0,
  889. nozzle_id: 'HH00-0.4',
  890. nozzle_diameter: '0.4',
  891. filament_id: 'GFG98',
  892. name: 'should-not-appear',
  893. k_value: '0.030',
  894. n_coef: '0',
  895. ams_id: 0,
  896. tray_id: 0,
  897. setting_id: '',
  898. },
  899. ],
  900. });
  901. const slotInfo = {
  902. ...defaultProps.slotInfo,
  903. savedPresetId: 'GFSL05_09',
  904. caliIdx: 0,
  905. extruderId: 0,
  906. };
  907. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  908. await waitFor(() => {
  909. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  910. });
  911. expect(screen.queryByRole('option', { name: /should-not-appear/ })).not.toBeInTheDocument();
  912. });
  913. });
  914. describe('ConfigureAmsSlotModal — per-nozzle K-profiles', () => {
  915. /**
  916. * K-profiles are per-nozzle and the printer numbers its calibration table per
  917. * nozzle too, so one filament calibrated on both hotends gives two profiles
  918. * with the same name and the same index space. Measured on the maintainer's
  919. * H2C: "HF Bambu PLA Matte Black" is K=0.018 at index 16 on the left hotend
  920. * and K=0.020 at index 15 on the right.
  921. *
  922. * A tray holds exactly one cali_idx, so the picker has to resolve it against
  923. * the slot's own nozzle. It used to match on the index alone and, failing
  924. * that, take the first profile in the list — which on a Filament Track Switch
  925. * machine (where no AMS reports an extruder at all) was arbitrary.
  926. */
  927. const matteBlack = (extruder: number, caliIdx: number, k: string) => ({
  928. slot_id: caliIdx,
  929. extruder_id: extruder,
  930. nozzle_id: 'HH00-0.4',
  931. nozzle_diameter: '0.4',
  932. filament_id: 'GFL99',
  933. name: 'HF Bambu PLA Matte Black',
  934. k_value: k,
  935. n_coef: '0',
  936. ams_id: 0,
  937. tray_id: 0,
  938. setting_id: '',
  939. });
  940. const bothNozzles = [matteBlack(1, 16, '0.018'), matteBlack(0, 15, '0.020')];
  941. beforeEach(() => {
  942. vi.clearAllMocks();
  943. Element.prototype.scrollIntoView = vi.fn();
  944. (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue(mockCloudSettings);
  945. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  946. { filament_id: 'GFL99', name: 'Generic PLA', filament_type: 'PLA' },
  947. ]);
  948. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({ profiles: bothNozzles });
  949. (api.configureAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  950. });
  951. const slot = (extruderId: number | undefined, caliIdx: number) => ({
  952. ...defaultProps.slotInfo,
  953. savedPresetId: 'builtin_GFL99',
  954. extruderId,
  955. caliIdx,
  956. });
  957. it('preselects the right hotend profile for a slot on the right hotend', async () => {
  958. // The reported bug, from the other side: the slot is bound to cali_idx 16,
  959. // which is the LEFT hotend's entry. On a right-hotend slot the picker must
  960. // land on 15, not follow the index into the wrong table.
  961. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 16)} />);
  962. await waitFor(() => {
  963. expect(screen.getByRole('combobox')).toBeInTheDocument();
  964. });
  965. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe('0|HF Bambu PLA Matte Black|0.020');
  966. });
  967. it('preselects the left hotend profile for a slot on the left hotend', async () => {
  968. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(1, 16)} />);
  969. await waitFor(() => {
  970. expect(screen.getByRole('combobox')).toBeInTheDocument();
  971. });
  972. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe('1|HF Bambu PLA Matte Black|0.018');
  973. });
  974. it('does not offer the other hotend profile as a match', async () => {
  975. // Both are still reachable — the other lands in the "Other K profiles"
  976. // group — but only one is a match for this slot.
  977. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 15)} />);
  978. await waitFor(() => {
  979. expect(screen.getByRole('option', { name: /K=0.020/ })).toBeInTheDocument();
  980. });
  981. const matchGroup = screen.getByRole('combobox').querySelectorAll(':scope > option');
  982. const matchValues = Array.from(matchGroup).map(o => (o as HTMLOptionElement).value).filter(Boolean);
  983. expect(matchValues).toEqual(['0|HF Bambu PLA Matte Black|0.020']);
  984. });
  985. it('names the hotend on each option when the printer has two', async () => {
  986. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 15)} />);
  987. await waitFor(() => {
  988. expect(screen.getByRole('option', { name: /K=0.020/ })).toBeInTheDocument();
  989. });
  990. expect(screen.getByRole('option', { name: /K=0\.020.*Right/ })).toBeInTheDocument();
  991. expect(screen.getByRole('option', { name: /K=0\.018.*Left/ })).toBeInTheDocument();
  992. });
  993. it('leaves single-nozzle printers unlabelled', async () => {
  994. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  995. profiles: [matteBlack(0, 15, '0.020')],
  996. });
  997. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 15)} />);
  998. await waitFor(() => {
  999. expect(screen.getByRole('option', { name: /K=0.020/ })).toBeInTheDocument();
  1000. });
  1001. expect(screen.queryByRole('option', { name: /Right/ })).not.toBeInTheDocument();
  1002. });
  1003. it('sends the cali_idx of the nozzle-correct profile', async () => {
  1004. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 16)} />);
  1005. // Wait for the preselect to settle, not merely for the control to mount —
  1006. // the submit reads the selected profile, which lands an effect later.
  1007. await waitFor(() => {
  1008. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe(
  1009. '0|HF Bambu PLA Matte Black|0.020'
  1010. );
  1011. });
  1012. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  1013. await waitFor(() => {
  1014. expect(api.configureAmsSlot).toHaveBeenCalled();
  1015. });
  1016. expect((api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3].cali_idx).toBe(15);
  1017. });
  1018. });