ConfigureAmsSlotModal.test.tsx 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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('has the backend resolve an Orca Cloud profile\'s own filament_id (#3003, #3216)', async () => {
  224. // OrcaSlicer's "Sync filaments" matches a slot by filament_id alone, so a
  225. // generic here turns the profile into "Generic PLA" in the slicer. The
  226. // lookup used to run here and fall back silently; it now runs in configure,
  227. // which logs the outcome and reports a fallback.
  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.configureAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({
  235. success: true, tray_info_idx: 'P56e1be0', orca_fallback_reason: '',
  236. });
  237. const slotInfo = { ...defaultProps.slotInfo, savedPresetId: ORCA_ID };
  238. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  239. await waitFor(() => {
  240. expect(screen.getByText('Overture Matte PLA @Orca')).toBeInTheDocument();
  241. });
  242. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  243. await waitFor(() => {
  244. expect(api.saveSlotPreset).toHaveBeenCalled();
  245. });
  246. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  247. expect(payload.orca_profile_id).toBe(ORCA_ID);
  248. expect(payload.tray_info_idx).toBe('');
  249. // The UUID is what the slicer cannot resolve; it goes in neither field.
  250. expect(payload.setting_id).toBe('');
  251. expect(api.orcaCloudGetProfile).not.toHaveBeenCalled();
  252. // The slot preset row records the id the slot was actually given, so the
  253. // slot card notices when the Device tab re-configures the slot.
  254. const saveArgs = (api.saveSlotPreset as ReturnType<typeof vi.fn>).mock.calls[0];
  255. expect(saveArgs[5]).toBe('orca_cloud');
  256. expect(saveArgs[6]).toBe('P56e1be0');
  257. expect(screen.queryByText(/OrcaSlicer will see this slot as Generic/)).not.toBeInTheDocument();
  258. });
  259. it('warns when an Orca profile went into the slot as a generic (#3216)', async () => {
  260. const ORCA_ID = '3f2a9c1e-4b7d-4a02-9f61-8c5e2d1a7b30';
  261. (api.orcaCloudListProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  262. filament: [{ setting_id: ORCA_ID, name: 'Homebrew PLA @Orca', type: 'filament', is_custom: true }],
  263. printer: [],
  264. process: [],
  265. });
  266. (api.configureAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({
  267. success: true, tray_info_idx: 'GFL99', orca_fallback_reason: 'no_filament_id',
  268. });
  269. const slotInfo = { ...defaultProps.slotInfo, savedPresetId: ORCA_ID };
  270. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  271. await waitFor(() => {
  272. expect(screen.getByText('Homebrew PLA @Orca')).toBeInTheDocument();
  273. });
  274. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  275. expect(
  276. await screen.findByText(
  277. 'This Orca profile has no filament ID of its own, so OrcaSlicer will see this slot as Generic PLA.',
  278. ),
  279. ).toBeInTheDocument();
  280. });
  281. it('uses the filament_id nested under setting when the envelope has none (#3003)', async () => {
  282. // Bambu Cloud returns a custom preset's own filament_id on the envelope for
  283. // some presets and inside the preset JSON for others. Only the envelope was
  284. // read, so presets of the second shape reached the slot as their inherited
  285. // base — the "Bambu profile instead of my profile" half of #3003.
  286. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockResolvedValue({
  287. filament_id: null,
  288. base_id: 'GFSB99_07',
  289. name: '# Overture Matte PLA @BBL H2D',
  290. setting: { filament_id: 'P4d64437' },
  291. });
  292. const slotInfo = {
  293. ...defaultProps.slotInfo,
  294. savedPresetId: 'PFUScd84f663d2c2ef',
  295. };
  296. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  297. await waitFor(() => {
  298. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  299. });
  300. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  301. await waitFor(() => {
  302. expect(api.configureAmsSlot).toHaveBeenCalled();
  303. });
  304. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  305. // 8 characters, so the printer stores it whole and the slicer matches it.
  306. expect(payload.tray_info_idx).toBe('P4d64437');
  307. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  308. });
  309. it('uses cloud detail filament_id when present', async () => {
  310. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockResolvedValue({
  311. filament_id: 'P285e239',
  312. base_id: 'GFSB99_07',
  313. name: '# Overture Matte PLA @BBL H2D',
  314. });
  315. const slotInfo = {
  316. ...defaultProps.slotInfo,
  317. savedPresetId: 'PFUScd84f663d2c2ef',
  318. };
  319. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  320. await waitFor(() => {
  321. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  322. });
  323. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  324. await waitFor(() => {
  325. expect(api.configureAmsSlot).toHaveBeenCalled();
  326. });
  327. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  328. expect(payload.tray_info_idx).toBe('P285e239');
  329. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  330. });
  331. it('sends short GF filament_id for Bambu GFS* presets (cloud detail not consulted)', async () => {
  332. // Bambu-provided presets (GFS*) convert the setting_id → filament_id locally.
  333. // The cloud detail endpoint must NOT be consulted for them; the rewrite that
  334. // fixed #1053 preserves this pre-existing shortcut.
  335. const slotInfo = {
  336. ...defaultProps.slotInfo,
  337. savedPresetId: 'GFSL05_09',
  338. };
  339. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  340. await waitFor(() => {
  341. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  342. });
  343. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  344. await waitFor(() => {
  345. expect(api.configureAmsSlot).toHaveBeenCalled();
  346. });
  347. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  348. expect(payload.tray_info_idx).toBe('GFL05');
  349. expect(payload.setting_id).toBe('GFSL05_09');
  350. expect(api.getCloudSettingDetail).not.toHaveBeenCalled();
  351. });
  352. it('sends no tray_info_idx when the cloud detail fetch fails (#3003)', async () => {
  353. // Network/5xx from /cloud/settings/{id} must not abort the configure flow.
  354. // It leaves no filament_id to send, and the PFUS default is not a usable
  355. // stand-in (see the filament_id: null case above), so the field goes out
  356. // empty and the backend resolves it.
  357. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockRejectedValue(
  358. new Error('cloud unreachable')
  359. );
  360. const slotInfo = {
  361. ...defaultProps.slotInfo,
  362. savedPresetId: 'PFUScd84f663d2c2ef',
  363. };
  364. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  365. await waitFor(() => {
  366. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  367. });
  368. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  369. await waitFor(() => {
  370. expect(api.configureAmsSlot).toHaveBeenCalled();
  371. });
  372. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  373. expect(payload.tray_info_idx).toBe('');
  374. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  375. });
  376. it('renders configure slot button', async () => {
  377. render(<ConfigureAmsSlotModal {...defaultProps} />);
  378. await waitFor(() => {
  379. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  380. });
  381. // Find the Configure Slot button
  382. const configureButton = screen.getByRole('button', { name: /Configure Slot/i });
  383. expect(configureButton).toBeInTheDocument();
  384. });
  385. it('filters presets by printer model', async () => {
  386. // Render with printerModel="H2D"
  387. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="H2D" />);
  388. // Wait for presets to load - the H2D preset should be visible
  389. await waitFor(() => {
  390. expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
  391. });
  392. // The X1C preset should NOT be visible (filtered out by model)
  393. expect(screen.queryByText(/Bambu PLA Basic @BBL X1C/)).not.toBeInTheDocument();
  394. });
  395. it('treats Bambu cloud rename @BBL A1M as a match for A1 Mini (#1649)', async () => {
  396. // Bambu cloud shifted A1 Mini filament profiles from
  397. // "Bambu PLA Basic @BBL A1 Mini ..." to the terse "@BBL A1M" mid-2026.
  398. // Without an alias-aware compare, the model filter strips every cloud
  399. // profile from the picker when the user selects an A1 Mini printer.
  400. (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue({
  401. filament: [
  402. { setting_id: 'GFA00_A1M', name: 'Bambu PLA Basic @BBL A1M', filament_id: 'GFA00' },
  403. { setting_id: 'GFA00_A1', name: 'Bambu PLA Basic @BBL A1', filament_id: 'GFA00' },
  404. ],
  405. });
  406. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="A1 Mini" />);
  407. await waitFor(() => {
  408. expect(screen.getByText('Bambu PLA Basic @BBL A1M')).toBeInTheDocument();
  409. });
  410. // The A1 (non-mini) preset must still be filtered out — the alias
  411. // table must not collapse two physically distinct printers.
  412. expect(screen.queryByText('Bambu PLA Basic @BBL A1')).not.toBeInTheDocument();
  413. });
  414. it('still filters cross-model cloud profiles when the printer is A1 Mini', async () => {
  415. // Sanity check that the alias addition didn't accidentally widen the
  416. // matcher: an X1C cloud preset stays hidden when the picker is for an
  417. // A1 Mini printer.
  418. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="A1 Mini" />);
  419. await waitFor(() => {
  420. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  421. });
  422. expect(screen.queryByText('Bambu PLA Basic @BBL X1C')).not.toBeInTheDocument();
  423. });
  424. it('shows current preset even when it does not match model filter', async () => {
  425. // Render with printerModel="H2D" but savedPresetId pointing to the X1C preset
  426. const slotInfo = {
  427. ...defaultProps.slotInfo,
  428. savedPresetId: 'GFSL05_09', // X1C preset
  429. };
  430. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} printerModel="H2D" />);
  431. await waitFor(() => {
  432. // Both should be visible - H2D matches model, X1C is saved preset
  433. // Use the full preset name to match the list item (not the "Filtering for" label)
  434. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  435. expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
  436. });
  437. });
  438. it('preset row expands inline on hover so the full name is readable (#1237)', async () => {
  439. // Long preset names (e.g. "SUNLU PETG GLOW IN THE DARK GEN2 @Bambu Lab H2C 0.4 nozzle")
  440. // get visually truncated; the row un-truncates on hover via group-hover so the
  441. // nozzle suffix is readable without waiting on the browser's title-tooltip delay,
  442. // and the title attribute remains as a fallback for assistive tech / touch.
  443. render(<ConfigureAmsSlotModal {...defaultProps} />);
  444. await waitFor(() => {
  445. const fullName = 'Bambu PLA Basic @BBL X1C';
  446. const span = screen.getByText(fullName);
  447. expect(span).toHaveAttribute('title', fullName);
  448. expect(span).toHaveClass('truncate');
  449. expect(span).toHaveClass('group-hover:whitespace-normal');
  450. expect(span).toHaveClass('group-hover:break-all');
  451. expect(span.closest('button')).toHaveClass('group');
  452. });
  453. });
  454. it('pre-selects saved preset when opening configured slot', async () => {
  455. const slotInfo = {
  456. ...defaultProps.slotInfo,
  457. savedPresetId: 'GFSL05_09',
  458. };
  459. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  460. await waitFor(() => {
  461. // The saved preset should have the selected style (green border)
  462. // Use the full preset name to avoid matching the "Filtering for" label
  463. const presetButton = screen.getByText('Bambu PLA Basic @BBL X1C').closest('button');
  464. expect(presetButton).toHaveClass('bg-bambu-green/20');
  465. });
  466. });
  467. it('pre-populates color from trayColor', async () => {
  468. const slotInfo = {
  469. ...defaultProps.slotInfo,
  470. trayColor: 'FF0000FF', // Red with alpha
  471. };
  472. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  473. await waitFor(() => {
  474. expect(screen.getByTitle('White')).toBeInTheDocument();
  475. });
  476. // The hex display should show the pre-populated color
  477. expect(screen.getByText('Hex: #FF0000', { exact: false })).toBeInTheDocument();
  478. });
  479. it('uses translated text for modal elements', async () => {
  480. render(<ConfigureAmsSlotModal {...defaultProps} />);
  481. await waitFor(() => {
  482. expect(screen.getByText('Configure AMS Slot')).toBeInTheDocument();
  483. expect(screen.getByText('Filament Profile')).toBeInTheDocument();
  484. });
  485. // Check footer buttons
  486. expect(screen.getByRole('button', { name: /Configure Slot/i })).toBeInTheDocument();
  487. expect(screen.getByRole('button', { name: /Cancel/i })).toBeInTheDocument();
  488. expect(screen.getByRole('button', { name: /Reset Slot/i })).toBeInTheDocument();
  489. });
  490. it('surfaces a K-profile whose name does not match the preset when filament_id agrees (#1688)', async () => {
  491. // Spool was edited with slicer_filament = "GFSL05_09" (the setting_id form
  492. // for Bambu PLA Basic). The printer has a *custom* K-profile saved on the
  493. // same filament_id, but the user named it something that doesn't include
  494. // "PLA". Pre-fix, the name-only filter dropped it; the id-match path now
  495. // surfaces it because both sides normalise to "GFL05".
  496. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  497. profiles: [
  498. {
  499. slot_id: 3,
  500. extruder_id: 0,
  501. nozzle_id: 'HH00-0.4',
  502. nozzle_diameter: '0.4',
  503. filament_id: 'GFL05',
  504. name: 'my-custom-tune',
  505. k_value: '0.025',
  506. n_coef: '0',
  507. ams_id: 0,
  508. tray_id: 0,
  509. setting_id: '',
  510. },
  511. ],
  512. });
  513. const slotInfo = {
  514. ...defaultProps.slotInfo,
  515. savedPresetId: 'GFSL05_09', // setting_id form for Bambu PLA Basic
  516. };
  517. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  518. await waitFor(() => {
  519. // Renders as an <option> on the K-profile select even though
  520. // "my-custom-tune" doesn't contain "PLA" anywhere.
  521. expect(screen.getByRole('option', { name: /my-custom-tune/ })).toBeInTheDocument();
  522. });
  523. });
  524. it("always includes the slot's currently-active K-profile when name and id don't match (#1689)", async () => {
  525. // Reporter scenario: spool assigned under "Generic PLA" but the slot has
  526. // a custom K-profile (filament_id "GFG98" = PETG-something) actively
  527. // selected via cali_idx. Pre-fix the modal showed "default 0.020"; the
  528. // safety net now surfaces the active profile so Configure Slot reflects
  529. // what the printer is actually using.
  530. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  531. profiles: [
  532. {
  533. slot_id: 7, // matches caliIdx below
  534. extruder_id: 0,
  535. nozzle_id: 'HH00-0.4',
  536. nozzle_diameter: '0.4',
  537. filament_id: 'GFG98', // unrelated to "Generic PLA"
  538. name: 'unrelated-petg-tune',
  539. k_value: '0.030',
  540. n_coef: '0',
  541. ams_id: 0,
  542. tray_id: 0,
  543. setting_id: '',
  544. },
  545. ],
  546. });
  547. const slotInfo = {
  548. ...defaultProps.slotInfo,
  549. savedPresetId: 'GFSL05_09', // Generic PLA preset
  550. caliIdx: 7,
  551. extruderId: 0,
  552. };
  553. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  554. await waitFor(() => {
  555. expect(screen.getByRole('option', { name: /unrelated-petg-tune/ })).toBeInTheDocument();
  556. });
  557. });
  558. it("does not carry the slot's K-profile over to a different filament (#3216)", async () => {
  559. // X1C slot on Devil Design PLA with its K-profile active, re-configured for
  560. // Azurefilm PLA Wood. The active profile used to join Azurefilm's list and
  561. // win the auto-select; the backend then realigned the slot to that
  562. // profile's filament, so the printer -- and OrcaSlicer -- stayed on Devil
  563. // Design. Only a slot reset first made it work.
  564. const DEVIL = '0ed901cd-6700-5772-80b2-25ed659df0d1';
  565. const AZURE = '2161df52-3d0e-579a-b4b6-dfbc62b93b76';
  566. (api.orcaCloudListProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  567. filament: [
  568. { setting_id: DEVIL, name: 'Devil Design PLA @Orca', type: 'filament', is_custom: true },
  569. { setting_id: AZURE, name: 'Azurefilm PLA Wood @Orca', type: 'filament', is_custom: true },
  570. ],
  571. printer: [],
  572. process: [],
  573. });
  574. const kp = (slot_id: number, name: string, filament_id: string, setting_id: string) => ({
  575. slot_id, extruder_id: 0, nozzle_id: 'HS00-0.4', nozzle_diameter: '0.4', filament_id, name,
  576. k_value: '0.020', n_coef: '0', ams_id: 0, tray_id: 0, setting_id,
  577. });
  578. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  579. profiles: [
  580. kp(7, 'Devil Design PLA', 'P4d64437', 'PFUSedbf16b803ff3e'),
  581. kp(9, 'Azurefilm PLA Wood', 'P285e239', 'PFUS220b72bdc39f90'),
  582. ],
  583. });
  584. const slotInfo = {
  585. ...defaultProps.slotInfo,
  586. savedPresetId: `orca_${DEVIL}`,
  587. trayInfoIdx: 'P4d64437',
  588. caliIdx: 7,
  589. extruderId: 0,
  590. };
  591. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  592. // Reopening on the slot's own filament still shows its active profile.
  593. await waitFor(() => {
  594. expect(screen.getByRole('option', { name: /Devil Design PLA/ })).toBeInTheDocument();
  595. });
  596. fireEvent.click(screen.getByText('Azurefilm PLA Wood @Orca'));
  597. // Azurefilm's own profile is offered and picked; Devil Design's is not.
  598. await waitFor(() => {
  599. expect(screen.getByRole('option', { name: /Azurefilm PLA Wood/ })).toBeInTheDocument();
  600. });
  601. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  602. await waitFor(() => {
  603. expect(api.configureAmsSlot).toHaveBeenCalled();
  604. });
  605. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  606. expect(payload.orca_profile_id).toBe(AZURE);
  607. expect(payload.kprofile_filament_id).toBe('P285e239');
  608. expect(payload.cali_idx).toBe(9);
  609. });
  610. it("surfaces the slot's active K-profile when no preset is resolvable (#1689 follow-up)", async () => {
  611. // Repro from Spionkiller01: slot is physically loaded but unconfigured —
  612. // tray_type='', tray_info_idx='', no slot_preset_mappings row — so
  613. // selectedPresetInfo resolves to null. Before the patch the main matcher's
  614. // early return on !selectedPresetInfo skipped past the cali_idx safety net
  615. // entirely; on reopen the dropdown went back to default 0.020 even though
  616. // the printer still holds the active profile at cali_idx=6.
  617. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  618. profiles: [
  619. {
  620. slot_id: 6,
  621. extruder_id: 0,
  622. nozzle_id: 'HH00-0.4',
  623. nozzle_diameter: '0.4',
  624. filament_id: 'GFG98',
  625. name: 'active-on-unconfigured-slot',
  626. k_value: '0.030',
  627. n_coef: '0',
  628. ams_id: 0,
  629. tray_id: 0,
  630. setting_id: '',
  631. },
  632. ],
  633. });
  634. const slotInfo = {
  635. ...defaultProps.slotInfo,
  636. trayType: '',
  637. traySubBrands: '',
  638. caliIdx: 6,
  639. extruderId: 0,
  640. // savedPresetId intentionally omitted — no preset bound yet
  641. };
  642. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  643. await waitFor(() => {
  644. expect(screen.getByRole('option', { name: /active-on-unconfigured-slot/ })).toBeInTheDocument();
  645. });
  646. });
  647. describe('Generic presets and the K-profile picker (#2710)', () => {
  648. // Reporter's printer: nine Flow-Dynamics entries, every one of them
  649. // calibrated against Generic PLA (filament_id GFL99) and named after the
  650. // spool's colour rather than its material. Bambu Studio lists all nine for
  651. // a Generic PLA slot; Bambuddy showed only the one already bound to the
  652. // slot via cali_idx.
  653. const genericPlaProfiles = [
  654. 'Black PLA+', 'Dark Brown', 'Glow', 'Gray', 'Lt Brown',
  655. 'Marble', 'Orange PLA', 'Sunlu White PLA+', 'White PLA+ Duramic',
  656. ].map((name, i) => ({
  657. slot_id: i + 1,
  658. extruder_id: 0,
  659. nozzle_id: 'HH00-0.4',
  660. nozzle_diameter: '0.4',
  661. filament_id: 'GFL99',
  662. name,
  663. k_value: `0.0${30 + i}`,
  664. n_coef: '0',
  665. ams_id: 0,
  666. tray_id: 0,
  667. setting_id: '',
  668. }));
  669. const genericPlaSlot = {
  670. ...defaultProps.slotInfo,
  671. savedPresetId: 'builtin_GFL99',
  672. extruderId: 0,
  673. };
  674. beforeEach(() => {
  675. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  676. { filament_id: 'GFL99', name: 'Generic PLA', filament_type: 'PLA' },
  677. ]);
  678. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  679. profiles: genericPlaProfiles,
  680. });
  681. });
  682. it('offers every Generic PLA profile when the slot preset is Generic PLA', async () => {
  683. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={genericPlaSlot} />);
  684. await waitFor(() => {
  685. expect(screen.getByRole('option', { name: /Dark Brown/ })).toBeInTheDocument();
  686. });
  687. // All nine, not just the one bound via cali_idx — matching the printer's
  688. // own calibration table for GFL99.
  689. for (const profile of genericPlaProfiles) {
  690. expect(
  691. screen.getByRole('option', { name: `${profile.name} (K=${profile.k_value})` }),
  692. ).toBeInTheDocument();
  693. }
  694. });
  695. it('offers them on a freshly reset slot with no active cali_idx', async () => {
  696. // "When I reset the AMS slot, the generic PLA shows no k-values at all."
  697. // With no cali_idx the #1689 safety net has nothing to surface, so the
  698. // list came back empty; the id match has to stand on its own.
  699. render(
  700. <ConfigureAmsSlotModal
  701. {...defaultProps}
  702. slotInfo={{ ...genericPlaSlot, caliIdx: 0 }}
  703. />
  704. );
  705. await waitFor(() => {
  706. expect(screen.getByRole('option', { name: /Marble/ })).toBeInTheDocument();
  707. });
  708. expect(screen.getByRole('option', { name: /Glow/ })).toBeInTheDocument();
  709. });
  710. it('matches on name when the profile carries no filament_id at all', async () => {
  711. // Not every firmware fills filament_id in extrusion_cali_get. "Generic"
  712. // is not a brand, so the name path must fall back to the material
  713. // instead of demanding "GENERIC" in the profile name.
  714. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  715. profiles: [
  716. { ...genericPlaProfiles[0], filament_id: '', name: 'Orange PLA' },
  717. { ...genericPlaProfiles[1], filament_id: '', name: 'Dark Brown' },
  718. ],
  719. });
  720. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={genericPlaSlot} />);
  721. await waitFor(() => {
  722. expect(screen.getByRole('option', { name: /Orange PLA/ })).toBeInTheDocument();
  723. });
  724. });
  725. it('does not sweep generic profiles into a brand preset', async () => {
  726. // Guard against the id path widening: "Bambu PLA Basic" is GFL05, so
  727. // GFL99 profiles must still be filtered out of the matching group and
  728. // only reachable through the explicit "other profiles" group.
  729. render(
  730. <ConfigureAmsSlotModal
  731. {...defaultProps}
  732. slotInfo={{ ...defaultProps.slotInfo, savedPresetId: 'GFSL05_09', extruderId: 0 }}
  733. />
  734. );
  735. await waitFor(() => {
  736. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  737. });
  738. // Every GFL99 profile is demoted to the other group: the brand gate on
  739. // "Bambu" still applies, and none of these names carry it.
  740. for (const profile of genericPlaProfiles) {
  741. const option = screen.getByRole('option', { name: `${profile.name} (K=${profile.k_value})` });
  742. expect(option.closest('optgroup')).toHaveAttribute(
  743. 'label',
  744. 'Other K profiles on this printer',
  745. );
  746. }
  747. });
  748. it("offers the printer's other profiles even when nothing matches the preset", async () => {
  749. // The escape hatch: a PETG preset matches none of the PLA profiles, but
  750. // the user can still reach every profile the printer holds instead of
  751. // being sent to the slicer.
  752. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  753. { filament_id: 'GFG99', name: 'Generic PETG', filament_type: 'PETG' },
  754. ]);
  755. render(
  756. <ConfigureAmsSlotModal
  757. {...defaultProps}
  758. slotInfo={{ ...genericPlaSlot, savedPresetId: 'builtin_GFG99', caliIdx: 0 }}
  759. />
  760. );
  761. await waitFor(() => {
  762. expect(screen.getByRole('option', { name: /Dark Brown/ })).toBeInTheDocument();
  763. });
  764. expect(screen.getByRole('option', { name: /Dark Brown/ }).closest('optgroup')).toHaveAttribute(
  765. 'label',
  766. 'Other K profiles on this printer',
  767. );
  768. });
  769. it('sends the cali_idx of a profile picked from the other-profiles group', async () => {
  770. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  771. { filament_id: 'GFG99', name: 'Generic PETG', filament_type: 'PETG' },
  772. ]);
  773. render(
  774. <ConfigureAmsSlotModal
  775. {...defaultProps}
  776. slotInfo={{ ...genericPlaSlot, savedPresetId: 'builtin_GFG99', caliIdx: 0 }}
  777. />
  778. );
  779. await waitFor(() => {
  780. expect(screen.getByRole('option', { name: /Marble/ })).toBeInTheDocument();
  781. });
  782. const marble = genericPlaProfiles.find(p => p.name === 'Marble')!;
  783. // Option identity carries the extruder, so a filament calibrated on both
  784. // hotends yields two distinguishable entries rather than one.
  785. fireEvent.change(screen.getByRole('combobox'), {
  786. target: { value: `${marble.extruder_id}|${marble.name}|${marble.k_value}` },
  787. });
  788. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  789. await waitFor(() => {
  790. expect(api.configureAmsSlot).toHaveBeenCalled();
  791. });
  792. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  793. expect(payload.cali_idx).toBe(marble.slot_id);
  794. expect(payload.kprofile_filament_id).toBe('GFL99');
  795. });
  796. it('distinguishes two profiles that share a name but differ in K', async () => {
  797. // The picker used to key options by name alone, so same-named profiles
  798. // were indistinguishable and the first always won. The key now also
  799. // carries the extruder, for the same reason one step further out: the
  800. // printer numbers its calibration table per hotend.
  801. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  802. profiles: [
  803. { ...genericPlaProfiles[0], slot_id: 1, name: 'PLA', k_value: '0.020' },
  804. { ...genericPlaProfiles[1], slot_id: 2, name: 'PLA', k_value: '0.045' },
  805. ],
  806. });
  807. render(
  808. <ConfigureAmsSlotModal
  809. {...defaultProps}
  810. slotInfo={{ ...genericPlaSlot, caliIdx: 0 }}
  811. />
  812. );
  813. await waitFor(() => {
  814. expect(screen.getByRole('option', { name: /K=0.045/ })).toBeInTheDocument();
  815. });
  816. fireEvent.change(screen.getByRole('combobox'), { target: { value: '0|PLA|0.045' } });
  817. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  818. await waitFor(() => {
  819. expect(api.configureAmsSlot).toHaveBeenCalled();
  820. });
  821. expect((api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3].cali_idx).toBe(2);
  822. });
  823. });
  824. describe('A slot whose extruder claims no profile (#3044)', () => {
  825. // Reporter's X2D: two AMS 2 Pro, one per hotend, the same filaments in
  826. // both. The printer files one profile per filament rather than one per
  827. // hotend, so the second AMS's slots point at entries tagged extruder 0.
  828. // Scoping the picker to the slot's own extruder left it with nothing:
  829. // the slot read as unconfigured and choosing a profile changed nothing
  830. // the user could see.
  831. const sharedProfiles = [
  832. { name: 'PLA', k_value: '0.021', slot_id: 1 },
  833. { name: 'PETG', k_value: '0.026', slot_id: 4 },
  834. ].map(p => ({
  835. ...p,
  836. extruder_id: 0,
  837. nozzle_id: 'HH00-0.4',
  838. nozzle_diameter: '0.4',
  839. filament_id: 'GFL99',
  840. n_coef: '0',
  841. ams_id: 0,
  842. tray_id: 0,
  843. setting_id: '',
  844. }));
  845. // AMS-B, right-hand hotend, already bound to the PLA profile the A-side
  846. // slots use.
  847. const rightHandSlot = {
  848. ...defaultProps.slotInfo,
  849. amsId: 1,
  850. savedPresetId: 'builtin_GFL99',
  851. extruderId: 1,
  852. caliIdx: 1,
  853. };
  854. beforeEach(() => {
  855. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  856. { filament_id: 'GFL99', name: 'Generic PLA', filament_type: 'PLA' },
  857. ]);
  858. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({ profiles: sharedProfiles });
  859. });
  860. it('offers the profiles as matches rather than an empty picker', async () => {
  861. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={rightHandSlot} />);
  862. await waitFor(() => {
  863. expect(screen.getByRole('option', { name: /PLA \(K=0.021\)/ })).toBeInTheDocument();
  864. });
  865. // Matches are the select's direct children; anything demoted to the
  866. // "Other K profiles" group sits inside an optgroup instead. Being merely
  867. // present is what the slot already had, and it read as unconfigured.
  868. const matches = Array.from(screen.getByRole('combobox').querySelectorAll(':scope > option'))
  869. .map(o => (o as HTMLOptionElement).value)
  870. .filter(Boolean);
  871. expect(matches).toEqual(['0|PLA|0.021', '0|PETG|0.026']);
  872. });
  873. it('shows the slot as already bound to its active profile', async () => {
  874. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={rightHandSlot} />);
  875. await waitFor(() => {
  876. expect(screen.getByRole('option', { name: /PLA \(K=0.021\)/ })).toBeInTheDocument();
  877. });
  878. // Not the "no K profile" placeholder, which is what the slot showed while
  879. // the active profile could not be found on this extruder.
  880. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe('0|PLA|0.021');
  881. });
  882. it('still scopes to the slot own hotend when that hotend has its own profiles', async () => {
  883. // The H2C case behind the scoping: one filament calibrated on both
  884. // hotends, two profiles, one right answer per slot. The fallback is a
  885. // last resort and must not reopen this.
  886. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  887. profiles: [
  888. { ...sharedProfiles[0], extruder_id: 0, k_value: '0.020', slot_id: 1 },
  889. { ...sharedProfiles[0], extruder_id: 1, k_value: '0.018', slot_id: 2 },
  890. ],
  891. });
  892. render(
  893. <ConfigureAmsSlotModal
  894. {...defaultProps}
  895. slotInfo={{ ...rightHandSlot, caliIdx: 2 }}
  896. />
  897. );
  898. await waitFor(() => {
  899. expect(screen.getByRole('option', { name: /K=0.018/ })).toBeInTheDocument();
  900. });
  901. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe('1|PLA|0.018');
  902. // The left hotend's copy is still reachable, but under "Other".
  903. const other = screen.getByRole('option', { name: /K=0.020/ });
  904. expect(other.parentElement?.tagName).toBe('OPTGROUP');
  905. });
  906. it('names each hotend once, not two or three times', async () => {
  907. // The label rendered kProfileNozzleSuffix twice in the matching group and
  908. // three times under "Other", so every option on a dual-nozzle printer
  909. // read "... . Left . Left".
  910. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  911. profiles: [
  912. { ...sharedProfiles[0], extruder_id: 0, k_value: '0.020', slot_id: 1 },
  913. { ...sharedProfiles[0], extruder_id: 1, k_value: '0.018', slot_id: 2 },
  914. ],
  915. });
  916. render(
  917. <ConfigureAmsSlotModal
  918. {...defaultProps}
  919. slotInfo={{ ...rightHandSlot, caliIdx: 2 }}
  920. />
  921. );
  922. await waitFor(() => {
  923. expect(screen.getByRole('option', { name: /K=0.018/ })).toBeInTheDocument();
  924. });
  925. for (const option of screen.getAllByRole('option')) {
  926. // The suffix is the only thing that puts a middot in an option label,
  927. // so counting separators counts how many times the hotend was named.
  928. const separators = (option.textContent ?? '').match(/\u00b7/g) ?? [];
  929. expect(separators.length).toBeLessThanOrEqual(1);
  930. }
  931. });
  932. });
  933. it('does not include the active K-profile when caliIdx is 0 or null (#1689 guard)', async () => {
  934. // cali_idx == 0 / null means no profile is active (printer default 0.020).
  935. // The safety net only triggers for activeIdx > 0 — otherwise unrelated
  936. // profiles whose slot_id happens to equal 0 would leak in.
  937. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  938. profiles: [
  939. {
  940. slot_id: 0,
  941. extruder_id: 0,
  942. nozzle_id: 'HH00-0.4',
  943. nozzle_diameter: '0.4',
  944. filament_id: 'GFG98',
  945. name: 'should-not-appear',
  946. k_value: '0.030',
  947. n_coef: '0',
  948. ams_id: 0,
  949. tray_id: 0,
  950. setting_id: '',
  951. },
  952. ],
  953. });
  954. const slotInfo = {
  955. ...defaultProps.slotInfo,
  956. savedPresetId: 'GFSL05_09',
  957. caliIdx: 0,
  958. extruderId: 0,
  959. };
  960. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  961. await waitFor(() => {
  962. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  963. });
  964. expect(screen.queryByRole('option', { name: /should-not-appear/ })).not.toBeInTheDocument();
  965. });
  966. });
  967. describe('ConfigureAmsSlotModal — per-nozzle K-profiles', () => {
  968. /**
  969. * K-profiles are per-nozzle and the printer numbers its calibration table per
  970. * nozzle too, so one filament calibrated on both hotends gives two profiles
  971. * with the same name and the same index space. Measured on the maintainer's
  972. * H2C: "HF Bambu PLA Matte Black" is K=0.018 at index 16 on the left hotend
  973. * and K=0.020 at index 15 on the right.
  974. *
  975. * A tray holds exactly one cali_idx, so the picker has to resolve it against
  976. * the slot's own nozzle. It used to match on the index alone and, failing
  977. * that, take the first profile in the list — which on a Filament Track Switch
  978. * machine (where no AMS reports an extruder at all) was arbitrary.
  979. */
  980. const matteBlack = (extruder: number, caliIdx: number, k: string) => ({
  981. slot_id: caliIdx,
  982. extruder_id: extruder,
  983. nozzle_id: 'HH00-0.4',
  984. nozzle_diameter: '0.4',
  985. filament_id: 'GFL99',
  986. name: 'HF Bambu PLA Matte Black',
  987. k_value: k,
  988. n_coef: '0',
  989. ams_id: 0,
  990. tray_id: 0,
  991. setting_id: '',
  992. });
  993. const bothNozzles = [matteBlack(1, 16, '0.018'), matteBlack(0, 15, '0.020')];
  994. beforeEach(() => {
  995. vi.clearAllMocks();
  996. Element.prototype.scrollIntoView = vi.fn();
  997. (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue(mockCloudSettings);
  998. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  999. { filament_id: 'GFL99', name: 'Generic PLA', filament_type: 'PLA' },
  1000. ]);
  1001. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({ profiles: bothNozzles });
  1002. (api.configureAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  1003. });
  1004. const slot = (extruderId: number | undefined, caliIdx: number) => ({
  1005. ...defaultProps.slotInfo,
  1006. savedPresetId: 'builtin_GFL99',
  1007. extruderId,
  1008. caliIdx,
  1009. });
  1010. it('preselects the right hotend profile for a slot on the right hotend', async () => {
  1011. // The reported bug, from the other side: the slot is bound to cali_idx 16,
  1012. // which is the LEFT hotend's entry. On a right-hotend slot the picker must
  1013. // land on 15, not follow the index into the wrong table.
  1014. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 16)} />);
  1015. await waitFor(() => {
  1016. expect(screen.getByRole('combobox')).toBeInTheDocument();
  1017. });
  1018. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe('0|HF Bambu PLA Matte Black|0.020');
  1019. });
  1020. it('preselects the left hotend profile for a slot on the left hotend', async () => {
  1021. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(1, 16)} />);
  1022. await waitFor(() => {
  1023. expect(screen.getByRole('combobox')).toBeInTheDocument();
  1024. });
  1025. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe('1|HF Bambu PLA Matte Black|0.018');
  1026. });
  1027. it('does not offer the other hotend profile as a match', async () => {
  1028. // Both are still reachable — the other lands in the "Other K profiles"
  1029. // group — but only one is a match for this slot.
  1030. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 15)} />);
  1031. await waitFor(() => {
  1032. expect(screen.getByRole('option', { name: /K=0.020/ })).toBeInTheDocument();
  1033. });
  1034. const matchGroup = screen.getByRole('combobox').querySelectorAll(':scope > option');
  1035. const matchValues = Array.from(matchGroup).map(o => (o as HTMLOptionElement).value).filter(Boolean);
  1036. expect(matchValues).toEqual(['0|HF Bambu PLA Matte Black|0.020']);
  1037. });
  1038. it('names the hotend on each option when the printer has two', async () => {
  1039. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 15)} />);
  1040. await waitFor(() => {
  1041. expect(screen.getByRole('option', { name: /K=0.020/ })).toBeInTheDocument();
  1042. });
  1043. expect(screen.getByRole('option', { name: /K=0\.020.*Right/ })).toBeInTheDocument();
  1044. expect(screen.getByRole('option', { name: /K=0\.018.*Left/ })).toBeInTheDocument();
  1045. });
  1046. it('leaves single-nozzle printers unlabelled', async () => {
  1047. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  1048. profiles: [matteBlack(0, 15, '0.020')],
  1049. });
  1050. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 15)} />);
  1051. await waitFor(() => {
  1052. expect(screen.getByRole('option', { name: /K=0.020/ })).toBeInTheDocument();
  1053. });
  1054. expect(screen.queryByRole('option', { name: /Right/ })).not.toBeInTheDocument();
  1055. });
  1056. it('sends the cali_idx of the nozzle-correct profile', async () => {
  1057. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 16)} />);
  1058. // Wait for the preselect to settle, not merely for the control to mount —
  1059. // the submit reads the selected profile, which lands an effect later.
  1060. await waitFor(() => {
  1061. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe(
  1062. '0|HF Bambu PLA Matte Black|0.020'
  1063. );
  1064. });
  1065. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  1066. await waitFor(() => {
  1067. expect(api.configureAmsSlot).toHaveBeenCalled();
  1068. });
  1069. expect((api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3].cali_idx).toBe(15);
  1070. });
  1071. });