ConfigureAmsSlotModal.test.tsx 29 KB

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