ConfigureAmsSlotModal.test.tsx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. searchColors: vi.fn(),
  26. getColorCatalog: vi.fn(),
  27. resetAmsSlot: vi.fn(),
  28. },
  29. }));
  30. const mockCloudSettings = {
  31. filament: [
  32. {
  33. setting_id: 'GFSL05_09',
  34. name: 'Bambu PLA Basic @BBL X1C',
  35. filament_id: 'GFL05',
  36. },
  37. {
  38. setting_id: 'PFUScd84f663d2c2ef',
  39. name: '# Overture Matte PLA @BBL H2D',
  40. filament_id: null,
  41. },
  42. ],
  43. };
  44. const mockKProfiles = {
  45. profiles: [
  46. {
  47. id: 1,
  48. name: 'PLA Basic',
  49. k_value: '0.020',
  50. filament_id: 'GFL05',
  51. setting_id: '',
  52. extruder_id: 1,
  53. cali_idx: 1,
  54. },
  55. ],
  56. };
  57. const defaultProps = {
  58. isOpen: true,
  59. onClose: vi.fn(),
  60. printerId: 1,
  61. slotInfo: {
  62. amsId: 0,
  63. trayId: 0,
  64. trayCount: 4,
  65. trayType: 'PLA',
  66. trayColor: 'FFFFFF',
  67. traySubBrands: 'PLA Basic',
  68. },
  69. nozzleDiameter: '0.4',
  70. onSuccess: vi.fn(),
  71. };
  72. describe('ConfigureAmsSlotModal', () => {
  73. beforeEach(() => {
  74. vi.clearAllMocks();
  75. // Mock scrollIntoView which is not available in jsdom
  76. Element.prototype.scrollIntoView = vi.fn();
  77. (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue(mockCloudSettings);
  78. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue(mockKProfiles);
  79. (api.configureAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  80. (api.saveSlotPreset as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  81. (api.getLocalPresets as ReturnType<typeof vi.fn>).mockResolvedValue({ filament: [] });
  82. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  83. (api.searchColors as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  84. (api.getColorCatalog as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  85. (api.resetAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true, message: 'ok' });
  86. });
  87. it('renders nothing visible when closed', () => {
  88. render(<ConfigureAmsSlotModal {...defaultProps} isOpen={false} />);
  89. expect(screen.queryByText('Configure AMS Slot')).not.toBeInTheDocument();
  90. });
  91. it('renders a clear tray as the transparency checkerboard rather than solid black (#2912)', () => {
  92. render(
  93. <ConfigureAmsSlotModal
  94. {...defaultProps}
  95. slotInfo={{ ...defaultProps.slotInfo, trayType: 'PETG', trayColor: '00000000' }}
  96. />,
  97. );
  98. const styled = Array.from(document.querySelectorAll<HTMLElement>('[style]'));
  99. expect(
  100. styled.some((el) => el.style.backgroundImage.includes('repeating-conic-gradient')),
  101. ).toBe(true);
  102. // The regression this pins: the alpha byte was sliced off before painting,
  103. // so a clear tray rendered as opaque black — the reported symptom, in the UI.
  104. expect(styled.some((el) => el.style.backgroundColor === 'rgb(0, 0, 0)')).toBe(false);
  105. });
  106. it('renders modal when open', async () => {
  107. render(<ConfigureAmsSlotModal {...defaultProps} />);
  108. await waitFor(() => {
  109. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  110. });
  111. });
  112. it('displays basic color buttons', async () => {
  113. render(<ConfigureAmsSlotModal {...defaultProps} />);
  114. await waitFor(() => {
  115. // Check for basic color buttons by their title attribute
  116. expect(screen.getByTitle('White')).toBeInTheDocument();
  117. expect(screen.getByTitle('Black')).toBeInTheDocument();
  118. expect(screen.getByTitle('Red')).toBeInTheDocument();
  119. expect(screen.getByTitle('Blue')).toBeInTheDocument();
  120. expect(screen.getByTitle('Green')).toBeInTheDocument();
  121. expect(screen.getByTitle('Yellow')).toBeInTheDocument();
  122. expect(screen.getByTitle('Orange')).toBeInTheDocument();
  123. expect(screen.getByTitle('Gray')).toBeInTheDocument();
  124. });
  125. });
  126. it('does not show extended colors by default', async () => {
  127. render(<ConfigureAmsSlotModal {...defaultProps} />);
  128. await waitFor(() => {
  129. expect(screen.getByTitle('White')).toBeInTheDocument();
  130. });
  131. // Extended colors should not be visible initially
  132. expect(screen.queryByTitle('Cyan')).not.toBeInTheDocument();
  133. expect(screen.queryByTitle('Purple')).not.toBeInTheDocument();
  134. expect(screen.queryByTitle('Coral')).not.toBeInTheDocument();
  135. });
  136. it('shows extended colors when expand button is clicked', async () => {
  137. render(<ConfigureAmsSlotModal {...defaultProps} />);
  138. await waitFor(() => {
  139. expect(screen.getByTitle('White')).toBeInTheDocument();
  140. });
  141. // Click the expand button (+ button)
  142. const expandButton = screen.getByTitle('Show more colors');
  143. fireEvent.click(expandButton);
  144. // Extended colors should now be visible
  145. await waitFor(() => {
  146. expect(screen.getByTitle('Cyan')).toBeInTheDocument();
  147. expect(screen.getByTitle('Purple')).toBeInTheDocument();
  148. expect(screen.getByTitle('Pink')).toBeInTheDocument();
  149. expect(screen.getByTitle('Brown')).toBeInTheDocument();
  150. expect(screen.getByTitle('Coral')).toBeInTheDocument();
  151. });
  152. });
  153. it('hides extended colors when collapse button is clicked', async () => {
  154. render(<ConfigureAmsSlotModal {...defaultProps} />);
  155. await waitFor(() => {
  156. expect(screen.getByTitle('White')).toBeInTheDocument();
  157. });
  158. // Click the expand button
  159. const expandButton = screen.getByTitle('Show more colors');
  160. fireEvent.click(expandButton);
  161. // Wait for extended colors to appear
  162. await waitFor(() => {
  163. expect(screen.getByTitle('Cyan')).toBeInTheDocument();
  164. });
  165. // Click the collapse button
  166. const collapseButton = screen.getByTitle('Show less colors');
  167. fireEvent.click(collapseButton);
  168. // Extended colors should be hidden again
  169. await waitFor(() => {
  170. expect(screen.queryByTitle('Cyan')).not.toBeInTheDocument();
  171. });
  172. });
  173. it('selects a color when color button is clicked', async () => {
  174. render(<ConfigureAmsSlotModal {...defaultProps} />);
  175. await waitFor(() => {
  176. expect(screen.getByTitle('Red')).toBeInTheDocument();
  177. });
  178. // Click the red color button
  179. const redButton = screen.getByTitle('Red');
  180. fireEvent.click(redButton);
  181. // The color input should now show "Red"
  182. const colorInput = screen.getByPlaceholderText(/Color name or hex/);
  183. expect(colorInput).toHaveValue('Red');
  184. });
  185. it('sends PFUS setting_id as tray_info_idx when cloud detail has filament_id: null (#1053)', async () => {
  186. // Cloud returns a user preset that inherits from a generic Bambu base and
  187. // has no distinct filament_id of its own — this is how Bambu Cloud responds
  188. // for custom presets built on top of "Generic ABS @BBL H2D" etc.
  189. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockResolvedValue({
  190. filament_id: null,
  191. base_id: 'GFSB99_07',
  192. name: '# Overture Matte PLA @BBL H2D',
  193. });
  194. const slotInfo = {
  195. ...defaultProps.slotInfo,
  196. savedPresetId: 'PFUScd84f663d2c2ef',
  197. };
  198. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  199. await waitFor(() => {
  200. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  201. });
  202. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  203. await waitFor(() => {
  204. expect(api.configureAmsSlot).toHaveBeenCalled();
  205. });
  206. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  207. // Before the fix, this collapsed to 'GFB99' (Generic ABS's filament_id),
  208. // which made OrcaSlicer/BambuStudio Sync Filaments resolve to "Generic ABS".
  209. expect(payload.tray_info_idx).toBe('PFUScd84f663d2c2ef');
  210. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  211. });
  212. it('uses cloud detail filament_id when present', async () => {
  213. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockResolvedValue({
  214. filament_id: 'P285e239',
  215. base_id: 'GFSB99_07',
  216. name: '# Overture Matte PLA @BBL H2D',
  217. });
  218. const slotInfo = {
  219. ...defaultProps.slotInfo,
  220. savedPresetId: 'PFUScd84f663d2c2ef',
  221. };
  222. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  223. await waitFor(() => {
  224. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  225. });
  226. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  227. await waitFor(() => {
  228. expect(api.configureAmsSlot).toHaveBeenCalled();
  229. });
  230. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  231. expect(payload.tray_info_idx).toBe('P285e239');
  232. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  233. });
  234. it('sends short GF filament_id for Bambu GFS* presets (cloud detail not consulted)', async () => {
  235. // Bambu-provided presets (GFS*) convert the setting_id → filament_id locally.
  236. // The cloud detail endpoint must NOT be consulted for them; the rewrite that
  237. // fixed #1053 preserves this pre-existing shortcut.
  238. const slotInfo = {
  239. ...defaultProps.slotInfo,
  240. savedPresetId: 'GFSL05_09',
  241. };
  242. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  243. await waitFor(() => {
  244. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  245. });
  246. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  247. await waitFor(() => {
  248. expect(api.configureAmsSlot).toHaveBeenCalled();
  249. });
  250. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  251. expect(payload.tray_info_idx).toBe('GFL05');
  252. expect(payload.setting_id).toBe('GFSL05_09');
  253. expect(api.getCloudSettingDetail).not.toHaveBeenCalled();
  254. });
  255. it('keeps default PFUS tray_info_idx when cloud detail fetch fails', async () => {
  256. // Network/5xx from /cloud/settings/{id} must not abort the configure flow
  257. // nor leave tray_info_idx empty — we fall back to the setting_id default.
  258. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockRejectedValue(
  259. new Error('cloud unreachable')
  260. );
  261. const slotInfo = {
  262. ...defaultProps.slotInfo,
  263. savedPresetId: 'PFUScd84f663d2c2ef',
  264. };
  265. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  266. await waitFor(() => {
  267. expect(screen.getByText('# Overture Matte PLA @BBL H2D')).toBeInTheDocument();
  268. });
  269. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  270. await waitFor(() => {
  271. expect(api.configureAmsSlot).toHaveBeenCalled();
  272. });
  273. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  274. expect(payload.tray_info_idx).toBe('PFUScd84f663d2c2ef');
  275. expect(payload.setting_id).toBe('PFUScd84f663d2c2ef');
  276. });
  277. it('renders configure slot button', async () => {
  278. render(<ConfigureAmsSlotModal {...defaultProps} />);
  279. await waitFor(() => {
  280. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  281. });
  282. // Find the Configure Slot button
  283. const configureButton = screen.getByRole('button', { name: /Configure Slot/i });
  284. expect(configureButton).toBeInTheDocument();
  285. });
  286. it('filters presets by printer model', async () => {
  287. // Render with printerModel="H2D"
  288. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="H2D" />);
  289. // Wait for presets to load - the H2D preset should be visible
  290. await waitFor(() => {
  291. expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
  292. });
  293. // The X1C preset should NOT be visible (filtered out by model)
  294. expect(screen.queryByText(/Bambu PLA Basic @BBL X1C/)).not.toBeInTheDocument();
  295. });
  296. it('treats Bambu cloud rename @BBL A1M as a match for A1 Mini (#1649)', async () => {
  297. // Bambu cloud shifted A1 Mini filament profiles from
  298. // "Bambu PLA Basic @BBL A1 Mini ..." to the terse "@BBL A1M" mid-2026.
  299. // Without an alias-aware compare, the model filter strips every cloud
  300. // profile from the picker when the user selects an A1 Mini printer.
  301. (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue({
  302. filament: [
  303. { setting_id: 'GFA00_A1M', name: 'Bambu PLA Basic @BBL A1M', filament_id: 'GFA00' },
  304. { setting_id: 'GFA00_A1', name: 'Bambu PLA Basic @BBL A1', filament_id: 'GFA00' },
  305. ],
  306. });
  307. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="A1 Mini" />);
  308. await waitFor(() => {
  309. expect(screen.getByText('Bambu PLA Basic @BBL A1M')).toBeInTheDocument();
  310. });
  311. // The A1 (non-mini) preset must still be filtered out — the alias
  312. // table must not collapse two physically distinct printers.
  313. expect(screen.queryByText('Bambu PLA Basic @BBL A1')).not.toBeInTheDocument();
  314. });
  315. it('still filters cross-model cloud profiles when the printer is A1 Mini', async () => {
  316. // Sanity check that the alias addition didn't accidentally widen the
  317. // matcher: an X1C cloud preset stays hidden when the picker is for an
  318. // A1 Mini printer.
  319. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="A1 Mini" />);
  320. await waitFor(() => {
  321. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  322. });
  323. expect(screen.queryByText('Bambu PLA Basic @BBL X1C')).not.toBeInTheDocument();
  324. });
  325. it('shows current preset even when it does not match model filter', async () => {
  326. // Render with printerModel="H2D" but savedPresetId pointing to the X1C preset
  327. const slotInfo = {
  328. ...defaultProps.slotInfo,
  329. savedPresetId: 'GFSL05_09', // X1C preset
  330. };
  331. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} printerModel="H2D" />);
  332. await waitFor(() => {
  333. // Both should be visible - H2D matches model, X1C is saved preset
  334. // Use the full preset name to match the list item (not the "Filtering for" label)
  335. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  336. expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
  337. });
  338. });
  339. it('preset row expands inline on hover so the full name is readable (#1237)', async () => {
  340. // Long preset names (e.g. "SUNLU PETG GLOW IN THE DARK GEN2 @Bambu Lab H2C 0.4 nozzle")
  341. // get visually truncated; the row un-truncates on hover via group-hover so the
  342. // nozzle suffix is readable without waiting on the browser's title-tooltip delay,
  343. // and the title attribute remains as a fallback for assistive tech / touch.
  344. render(<ConfigureAmsSlotModal {...defaultProps} />);
  345. await waitFor(() => {
  346. const fullName = 'Bambu PLA Basic @BBL X1C';
  347. const span = screen.getByText(fullName);
  348. expect(span).toHaveAttribute('title', fullName);
  349. expect(span).toHaveClass('truncate');
  350. expect(span).toHaveClass('group-hover:whitespace-normal');
  351. expect(span).toHaveClass('group-hover:break-all');
  352. expect(span.closest('button')).toHaveClass('group');
  353. });
  354. });
  355. it('pre-selects saved preset when opening configured slot', async () => {
  356. const slotInfo = {
  357. ...defaultProps.slotInfo,
  358. savedPresetId: 'GFSL05_09',
  359. };
  360. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  361. await waitFor(() => {
  362. // The saved preset should have the selected style (green border)
  363. // Use the full preset name to avoid matching the "Filtering for" label
  364. const presetButton = screen.getByText('Bambu PLA Basic @BBL X1C').closest('button');
  365. expect(presetButton).toHaveClass('bg-bambu-green/20');
  366. });
  367. });
  368. it('pre-populates color from trayColor', async () => {
  369. const slotInfo = {
  370. ...defaultProps.slotInfo,
  371. trayColor: 'FF0000FF', // Red with alpha
  372. };
  373. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  374. await waitFor(() => {
  375. expect(screen.getByTitle('White')).toBeInTheDocument();
  376. });
  377. // The hex display should show the pre-populated color
  378. expect(screen.getByText('Hex: #FF0000', { exact: false })).toBeInTheDocument();
  379. });
  380. it('uses translated text for modal elements', async () => {
  381. render(<ConfigureAmsSlotModal {...defaultProps} />);
  382. await waitFor(() => {
  383. expect(screen.getByText('Configure AMS Slot')).toBeInTheDocument();
  384. expect(screen.getByText('Filament Profile')).toBeInTheDocument();
  385. });
  386. // Check footer buttons
  387. expect(screen.getByRole('button', { name: /Configure Slot/i })).toBeInTheDocument();
  388. expect(screen.getByRole('button', { name: /Cancel/i })).toBeInTheDocument();
  389. expect(screen.getByRole('button', { name: /Reset Slot/i })).toBeInTheDocument();
  390. });
  391. it('surfaces a K-profile whose name does not match the preset when filament_id agrees (#1688)', async () => {
  392. // Spool was edited with slicer_filament = "GFSL05_09" (the setting_id form
  393. // for Bambu PLA Basic). The printer has a *custom* K-profile saved on the
  394. // same filament_id, but the user named it something that doesn't include
  395. // "PLA". Pre-fix, the name-only filter dropped it; the id-match path now
  396. // surfaces it because both sides normalise to "GFL05".
  397. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  398. profiles: [
  399. {
  400. slot_id: 3,
  401. extruder_id: 0,
  402. nozzle_id: 'HH00-0.4',
  403. nozzle_diameter: '0.4',
  404. filament_id: 'GFL05',
  405. name: 'my-custom-tune',
  406. k_value: '0.025',
  407. n_coef: '0',
  408. ams_id: 0,
  409. tray_id: 0,
  410. setting_id: '',
  411. },
  412. ],
  413. });
  414. const slotInfo = {
  415. ...defaultProps.slotInfo,
  416. savedPresetId: 'GFSL05_09', // setting_id form for Bambu PLA Basic
  417. };
  418. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  419. await waitFor(() => {
  420. // Renders as an <option> on the K-profile select even though
  421. // "my-custom-tune" doesn't contain "PLA" anywhere.
  422. expect(screen.getByRole('option', { name: /my-custom-tune/ })).toBeInTheDocument();
  423. });
  424. });
  425. it("always includes the slot's currently-active K-profile when name and id don't match (#1689)", async () => {
  426. // Reporter scenario: spool assigned under "Generic PLA" but the slot has
  427. // a custom K-profile (filament_id "GFG98" = PETG-something) actively
  428. // selected via cali_idx. Pre-fix the modal showed "default 0.020"; the
  429. // safety net now surfaces the active profile so Configure Slot reflects
  430. // what the printer is actually using.
  431. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  432. profiles: [
  433. {
  434. slot_id: 7, // matches caliIdx below
  435. extruder_id: 0,
  436. nozzle_id: 'HH00-0.4',
  437. nozzle_diameter: '0.4',
  438. filament_id: 'GFG98', // unrelated to "Generic PLA"
  439. name: 'unrelated-petg-tune',
  440. k_value: '0.030',
  441. n_coef: '0',
  442. ams_id: 0,
  443. tray_id: 0,
  444. setting_id: '',
  445. },
  446. ],
  447. });
  448. const slotInfo = {
  449. ...defaultProps.slotInfo,
  450. savedPresetId: 'GFSL05_09', // Generic PLA preset
  451. caliIdx: 7,
  452. extruderId: 0,
  453. };
  454. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  455. await waitFor(() => {
  456. expect(screen.getByRole('option', { name: /unrelated-petg-tune/ })).toBeInTheDocument();
  457. });
  458. });
  459. it("surfaces the slot's active K-profile when no preset is resolvable (#1689 follow-up)", async () => {
  460. // Repro from Spionkiller01: slot is physically loaded but unconfigured —
  461. // tray_type='', tray_info_idx='', no slot_preset_mappings row — so
  462. // selectedPresetInfo resolves to null. Before the patch the main matcher's
  463. // early return on !selectedPresetInfo skipped past the cali_idx safety net
  464. // entirely; on reopen the dropdown went back to default 0.020 even though
  465. // the printer still holds the active profile at cali_idx=6.
  466. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  467. profiles: [
  468. {
  469. slot_id: 6,
  470. extruder_id: 0,
  471. nozzle_id: 'HH00-0.4',
  472. nozzle_diameter: '0.4',
  473. filament_id: 'GFG98',
  474. name: 'active-on-unconfigured-slot',
  475. k_value: '0.030',
  476. n_coef: '0',
  477. ams_id: 0,
  478. tray_id: 0,
  479. setting_id: '',
  480. },
  481. ],
  482. });
  483. const slotInfo = {
  484. ...defaultProps.slotInfo,
  485. trayType: '',
  486. traySubBrands: '',
  487. caliIdx: 6,
  488. extruderId: 0,
  489. // savedPresetId intentionally omitted — no preset bound yet
  490. };
  491. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  492. await waitFor(() => {
  493. expect(screen.getByRole('option', { name: /active-on-unconfigured-slot/ })).toBeInTheDocument();
  494. });
  495. });
  496. describe('Generic presets and the K-profile picker (#2710)', () => {
  497. // Reporter's printer: nine Flow-Dynamics entries, every one of them
  498. // calibrated against Generic PLA (filament_id GFL99) and named after the
  499. // spool's colour rather than its material. Bambu Studio lists all nine for
  500. // a Generic PLA slot; Bambuddy showed only the one already bound to the
  501. // slot via cali_idx.
  502. const genericPlaProfiles = [
  503. 'Black PLA+', 'Dark Brown', 'Glow', 'Gray', 'Lt Brown',
  504. 'Marble', 'Orange PLA', 'Sunlu White PLA+', 'White PLA+ Duramic',
  505. ].map((name, i) => ({
  506. slot_id: i + 1,
  507. extruder_id: 0,
  508. nozzle_id: 'HH00-0.4',
  509. nozzle_diameter: '0.4',
  510. filament_id: 'GFL99',
  511. name,
  512. k_value: `0.0${30 + i}`,
  513. n_coef: '0',
  514. ams_id: 0,
  515. tray_id: 0,
  516. setting_id: '',
  517. }));
  518. const genericPlaSlot = {
  519. ...defaultProps.slotInfo,
  520. savedPresetId: 'builtin_GFL99',
  521. extruderId: 0,
  522. };
  523. beforeEach(() => {
  524. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  525. { filament_id: 'GFL99', name: 'Generic PLA', filament_type: 'PLA' },
  526. ]);
  527. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  528. profiles: genericPlaProfiles,
  529. });
  530. });
  531. it('offers every Generic PLA profile when the slot preset is Generic PLA', async () => {
  532. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={genericPlaSlot} />);
  533. await waitFor(() => {
  534. expect(screen.getByRole('option', { name: /Dark Brown/ })).toBeInTheDocument();
  535. });
  536. // All nine, not just the one bound via cali_idx — matching the printer's
  537. // own calibration table for GFL99.
  538. for (const profile of genericPlaProfiles) {
  539. expect(
  540. screen.getByRole('option', { name: `${profile.name} (K=${profile.k_value})` }),
  541. ).toBeInTheDocument();
  542. }
  543. });
  544. it('offers them on a freshly reset slot with no active cali_idx', async () => {
  545. // "When I reset the AMS slot, the generic PLA shows no k-values at all."
  546. // With no cali_idx the #1689 safety net has nothing to surface, so the
  547. // list came back empty; the id match has to stand on its own.
  548. render(
  549. <ConfigureAmsSlotModal
  550. {...defaultProps}
  551. slotInfo={{ ...genericPlaSlot, caliIdx: 0 }}
  552. />
  553. );
  554. await waitFor(() => {
  555. expect(screen.getByRole('option', { name: /Marble/ })).toBeInTheDocument();
  556. });
  557. expect(screen.getByRole('option', { name: /Glow/ })).toBeInTheDocument();
  558. });
  559. it('matches on name when the profile carries no filament_id at all', async () => {
  560. // Not every firmware fills filament_id in extrusion_cali_get. "Generic"
  561. // is not a brand, so the name path must fall back to the material
  562. // instead of demanding "GENERIC" in the profile name.
  563. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  564. profiles: [
  565. { ...genericPlaProfiles[0], filament_id: '', name: 'Orange PLA' },
  566. { ...genericPlaProfiles[1], filament_id: '', name: 'Dark Brown' },
  567. ],
  568. });
  569. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={genericPlaSlot} />);
  570. await waitFor(() => {
  571. expect(screen.getByRole('option', { name: /Orange PLA/ })).toBeInTheDocument();
  572. });
  573. });
  574. it('does not sweep generic profiles into a brand preset', async () => {
  575. // Guard against the id path widening: "Bambu PLA Basic" is GFL05, so
  576. // GFL99 profiles must still be filtered out of the matching group and
  577. // only reachable through the explicit "other profiles" group.
  578. render(
  579. <ConfigureAmsSlotModal
  580. {...defaultProps}
  581. slotInfo={{ ...defaultProps.slotInfo, savedPresetId: 'GFSL05_09', extruderId: 0 }}
  582. />
  583. );
  584. await waitFor(() => {
  585. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  586. });
  587. // Every GFL99 profile is demoted to the other group: the brand gate on
  588. // "Bambu" still applies, and none of these names carry it.
  589. for (const profile of genericPlaProfiles) {
  590. const option = screen.getByRole('option', { name: `${profile.name} (K=${profile.k_value})` });
  591. expect(option.closest('optgroup')).toHaveAttribute(
  592. 'label',
  593. 'Other K profiles on this printer',
  594. );
  595. }
  596. });
  597. it("offers the printer's other profiles even when nothing matches the preset", async () => {
  598. // The escape hatch: a PETG preset matches none of the PLA profiles, but
  599. // the user can still reach every profile the printer holds instead of
  600. // being sent to the slicer.
  601. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  602. { filament_id: 'GFG99', name: 'Generic PETG', filament_type: 'PETG' },
  603. ]);
  604. render(
  605. <ConfigureAmsSlotModal
  606. {...defaultProps}
  607. slotInfo={{ ...genericPlaSlot, savedPresetId: 'builtin_GFG99', caliIdx: 0 }}
  608. />
  609. );
  610. await waitFor(() => {
  611. expect(screen.getByRole('option', { name: /Dark Brown/ })).toBeInTheDocument();
  612. });
  613. expect(screen.getByRole('option', { name: /Dark Brown/ }).closest('optgroup')).toHaveAttribute(
  614. 'label',
  615. 'Other K profiles on this printer',
  616. );
  617. });
  618. it('sends the cali_idx of a profile picked from the other-profiles group', async () => {
  619. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  620. { filament_id: 'GFG99', name: 'Generic PETG', filament_type: 'PETG' },
  621. ]);
  622. render(
  623. <ConfigureAmsSlotModal
  624. {...defaultProps}
  625. slotInfo={{ ...genericPlaSlot, savedPresetId: 'builtin_GFG99', caliIdx: 0 }}
  626. />
  627. );
  628. await waitFor(() => {
  629. expect(screen.getByRole('option', { name: /Marble/ })).toBeInTheDocument();
  630. });
  631. const marble = genericPlaProfiles.find(p => p.name === 'Marble')!;
  632. // Option identity carries the extruder, so a filament calibrated on both
  633. // hotends yields two distinguishable entries rather than one.
  634. fireEvent.change(screen.getByRole('combobox'), {
  635. target: { value: `${marble.extruder_id}|${marble.name}|${marble.k_value}` },
  636. });
  637. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  638. await waitFor(() => {
  639. expect(api.configureAmsSlot).toHaveBeenCalled();
  640. });
  641. const payload = (api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3];
  642. expect(payload.cali_idx).toBe(marble.slot_id);
  643. expect(payload.kprofile_filament_id).toBe('GFL99');
  644. });
  645. it('distinguishes two profiles that share a name but differ in K', async () => {
  646. // The picker used to key options by name alone, so same-named profiles
  647. // were indistinguishable and the first always won. The key now also
  648. // carries the extruder, for the same reason one step further out: the
  649. // printer numbers its calibration table per hotend.
  650. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  651. profiles: [
  652. { ...genericPlaProfiles[0], slot_id: 1, name: 'PLA', k_value: '0.020' },
  653. { ...genericPlaProfiles[1], slot_id: 2, name: 'PLA', k_value: '0.045' },
  654. ],
  655. });
  656. render(
  657. <ConfigureAmsSlotModal
  658. {...defaultProps}
  659. slotInfo={{ ...genericPlaSlot, caliIdx: 0 }}
  660. />
  661. );
  662. await waitFor(() => {
  663. expect(screen.getByRole('option', { name: /K=0.045/ })).toBeInTheDocument();
  664. });
  665. fireEvent.change(screen.getByRole('combobox'), { target: { value: '0|PLA|0.045' } });
  666. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  667. await waitFor(() => {
  668. expect(api.configureAmsSlot).toHaveBeenCalled();
  669. });
  670. expect((api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3].cali_idx).toBe(2);
  671. });
  672. });
  673. it('does not include the active K-profile when caliIdx is 0 or null (#1689 guard)', async () => {
  674. // cali_idx == 0 / null means no profile is active (printer default 0.020).
  675. // The safety net only triggers for activeIdx > 0 — otherwise unrelated
  676. // profiles whose slot_id happens to equal 0 would leak in.
  677. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  678. profiles: [
  679. {
  680. slot_id: 0,
  681. extruder_id: 0,
  682. nozzle_id: 'HH00-0.4',
  683. nozzle_diameter: '0.4',
  684. filament_id: 'GFG98',
  685. name: 'should-not-appear',
  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',
  697. caliIdx: 0,
  698. extruderId: 0,
  699. };
  700. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  701. await waitFor(() => {
  702. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  703. });
  704. expect(screen.queryByRole('option', { name: /should-not-appear/ })).not.toBeInTheDocument();
  705. });
  706. });
  707. describe('ConfigureAmsSlotModal — per-nozzle K-profiles', () => {
  708. /**
  709. * K-profiles are per-nozzle and the printer numbers its calibration table per
  710. * nozzle too, so one filament calibrated on both hotends gives two profiles
  711. * with the same name and the same index space. Measured on the maintainer's
  712. * H2C: "HF Bambu PLA Matte Black" is K=0.018 at index 16 on the left hotend
  713. * and K=0.020 at index 15 on the right.
  714. *
  715. * A tray holds exactly one cali_idx, so the picker has to resolve it against
  716. * the slot's own nozzle. It used to match on the index alone and, failing
  717. * that, take the first profile in the list — which on a Filament Track Switch
  718. * machine (where no AMS reports an extruder at all) was arbitrary.
  719. */
  720. const matteBlack = (extruder: number, caliIdx: number, k: string) => ({
  721. slot_id: caliIdx,
  722. extruder_id: extruder,
  723. nozzle_id: 'HH00-0.4',
  724. nozzle_diameter: '0.4',
  725. filament_id: 'GFL99',
  726. name: 'HF Bambu PLA Matte Black',
  727. k_value: k,
  728. n_coef: '0',
  729. ams_id: 0,
  730. tray_id: 0,
  731. setting_id: '',
  732. });
  733. const bothNozzles = [matteBlack(1, 16, '0.018'), matteBlack(0, 15, '0.020')];
  734. beforeEach(() => {
  735. vi.clearAllMocks();
  736. Element.prototype.scrollIntoView = vi.fn();
  737. (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue(mockCloudSettings);
  738. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([
  739. { filament_id: 'GFL99', name: 'Generic PLA', filament_type: 'PLA' },
  740. ]);
  741. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({ profiles: bothNozzles });
  742. (api.configureAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  743. });
  744. const slot = (extruderId: number | undefined, caliIdx: number) => ({
  745. ...defaultProps.slotInfo,
  746. savedPresetId: 'builtin_GFL99',
  747. extruderId,
  748. caliIdx,
  749. });
  750. it('preselects the right hotend profile for a slot on the right hotend', async () => {
  751. // The reported bug, from the other side: the slot is bound to cali_idx 16,
  752. // which is the LEFT hotend's entry. On a right-hotend slot the picker must
  753. // land on 15, not follow the index into the wrong table.
  754. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 16)} />);
  755. await waitFor(() => {
  756. expect(screen.getByRole('combobox')).toBeInTheDocument();
  757. });
  758. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe('0|HF Bambu PLA Matte Black|0.020');
  759. });
  760. it('preselects the left hotend profile for a slot on the left hotend', async () => {
  761. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(1, 16)} />);
  762. await waitFor(() => {
  763. expect(screen.getByRole('combobox')).toBeInTheDocument();
  764. });
  765. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe('1|HF Bambu PLA Matte Black|0.018');
  766. });
  767. it('does not offer the other hotend profile as a match', async () => {
  768. // Both are still reachable — the other lands in the "Other K profiles"
  769. // group — but only one is a match for this slot.
  770. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 15)} />);
  771. await waitFor(() => {
  772. expect(screen.getByRole('option', { name: /K=0.020/ })).toBeInTheDocument();
  773. });
  774. const matchGroup = screen.getByRole('combobox').querySelectorAll(':scope > option');
  775. const matchValues = Array.from(matchGroup).map(o => (o as HTMLOptionElement).value).filter(Boolean);
  776. expect(matchValues).toEqual(['0|HF Bambu PLA Matte Black|0.020']);
  777. });
  778. it('names the hotend on each option when the printer has two', async () => {
  779. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 15)} />);
  780. await waitFor(() => {
  781. expect(screen.getByRole('option', { name: /K=0.020/ })).toBeInTheDocument();
  782. });
  783. expect(screen.getByRole('option', { name: /K=0\.020.*Right/ })).toBeInTheDocument();
  784. expect(screen.getByRole('option', { name: /K=0\.018.*Left/ })).toBeInTheDocument();
  785. });
  786. it('leaves single-nozzle printers unlabelled', async () => {
  787. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue({
  788. profiles: [matteBlack(0, 15, '0.020')],
  789. });
  790. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 15)} />);
  791. await waitFor(() => {
  792. expect(screen.getByRole('option', { name: /K=0.020/ })).toBeInTheDocument();
  793. });
  794. expect(screen.queryByRole('option', { name: /Right/ })).not.toBeInTheDocument();
  795. });
  796. it('sends the cali_idx of the nozzle-correct profile', async () => {
  797. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slot(0, 16)} />);
  798. // Wait for the preselect to settle, not merely for the control to mount —
  799. // the submit reads the selected profile, which lands an effect later.
  800. await waitFor(() => {
  801. expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe(
  802. '0|HF Bambu PLA Matte Black|0.020'
  803. );
  804. });
  805. fireEvent.click(screen.getByRole('button', { name: /Configure Slot/i }));
  806. await waitFor(() => {
  807. expect(api.configureAmsSlot).toHaveBeenCalled();
  808. });
  809. expect((api.configureAmsSlot as ReturnType<typeof vi.fn>).mock.calls[0][3].cali_idx).toBe(15);
  810. });
  811. });