ConfigureAmsSlotModal.test.tsx 58 KB

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