ConfigureAmsSlotModal.test.tsx 51 KB

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