SliceModal.test.tsx 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. /**
  2. * Tests for SliceModal.
  3. *
  4. * The modal handles preset selection across three tiers (cloud / local /
  5. * standard) + enqueueing a slice job. After enqueue success it hands the
  6. * job_id off to SliceJobTrackerProvider (which lives at app level) and
  7. * calls onClose. Polling, toasts, and query invalidation all happen in
  8. * the tracker — not here.
  9. */
  10. import { describe, it, expect, vi, beforeEach } from 'vitest';
  11. import { screen, waitFor, within } from '@testing-library/react';
  12. import userEvent from '@testing-library/user-event';
  13. import { render } from '../utils';
  14. import { SliceModal } from '../../components/SliceModal';
  15. import { SliceJobTrackerProvider } from '../../contexts/SliceJobTrackerContext';
  16. import { api, type UnifiedPresetsResponse } from '../../api/client';
  17. vi.mock('../../api/client', () => ({
  18. api: {
  19. getSlicerPresets: vi.fn(),
  20. sliceLibraryFile: vi.fn(),
  21. sliceArchive: vi.fn(),
  22. getSliceJob: vi.fn(),
  23. getLibraryFilePlates: vi.fn(),
  24. getArchivePlates: vi.fn(),
  25. getLibraryFileFilamentRequirements: vi.fn(),
  26. getArchiveFilamentRequirements: vi.fn(),
  27. listSlicerBundles: vi.fn(),
  28. getSettings: vi.fn().mockResolvedValue({}),
  29. updateSettings: vi.fn().mockResolvedValue({}),
  30. },
  31. }));
  32. const mockApi = api as unknown as {
  33. getSlicerPresets: ReturnType<typeof vi.fn>;
  34. sliceLibraryFile: ReturnType<typeof vi.fn>;
  35. sliceArchive: ReturnType<typeof vi.fn>;
  36. getSliceJob: ReturnType<typeof vi.fn>;
  37. getLibraryFilePlates: ReturnType<typeof vi.fn>;
  38. getArchivePlates: ReturnType<typeof vi.fn>;
  39. getLibraryFileFilamentRequirements: ReturnType<typeof vi.fn>;
  40. getArchiveFilamentRequirements: ReturnType<typeof vi.fn>;
  41. listSlicerBundles: ReturnType<typeof vi.fn>;
  42. };
  43. function makeUnified(overrides: Partial<UnifiedPresetsResponse> = {}): UnifiedPresetsResponse {
  44. return {
  45. cloud: { printer: [], process: [], filament: [] },
  46. local: { printer: [], process: [], filament: [] },
  47. standard: { printer: [], process: [], filament: [] },
  48. cloud_status: 'ok',
  49. ...overrides,
  50. };
  51. }
  52. const fullThreeTier: UnifiedPresetsResponse = makeUnified({
  53. cloud: {
  54. printer: [{ id: 'PFUcloud-printer', name: 'My Custom X1C', source: 'cloud' }],
  55. process: [{ id: 'PFUcloud-process', name: 'My 0.16mm Tweaked', source: 'cloud' }],
  56. filament: [{ id: 'PFUcloud-filament', name: 'My PLA Black', source: 'cloud' }],
  57. },
  58. local: {
  59. printer: [{ id: '1', name: 'Imported X1C 0.4', source: 'local' }],
  60. process: [{ id: '2', name: 'Imported 0.20mm', source: 'local' }],
  61. filament: [{ id: '3', name: 'Imported PLA Basic', source: 'local' }],
  62. },
  63. standard: {
  64. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  65. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  66. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  67. },
  68. });
  69. function renderWithTracker(props: Parameters<typeof SliceModal>[0]) {
  70. return render(
  71. <SliceJobTrackerProvider>
  72. <SliceModal {...props} />
  73. </SliceJobTrackerProvider>,
  74. );
  75. }
  76. describe('SliceModal', () => {
  77. beforeEach(() => {
  78. vi.clearAllMocks();
  79. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  80. mockApi.getSliceJob.mockResolvedValue({
  81. job_id: 42,
  82. status: 'running',
  83. kind: 'library_file',
  84. source_id: 100,
  85. source_name: 'Cube.stl',
  86. created_at: new Date().toISOString(),
  87. started_at: null,
  88. completed_at: null,
  89. });
  90. // Default: single-plate (or non-3MF). Multi-plate tests override this.
  91. mockApi.getLibraryFilePlates.mockResolvedValue({
  92. file_id: 100,
  93. filename: 'Cube.stl',
  94. plates: [],
  95. is_multi_plate: false,
  96. });
  97. mockApi.getArchivePlates.mockResolvedValue({
  98. archive_id: 100,
  99. filename: 'Cube.3mf',
  100. plates: [],
  101. is_multi_plate: false,
  102. });
  103. // Default: no per-plate filament metadata available (mirrors STL or
  104. // unsliced source). Multi-color tests override this.
  105. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  106. file_id: 100,
  107. filename: 'Cube.stl',
  108. plate_id: 1,
  109. filaments: [],
  110. });
  111. mockApi.getArchiveFilamentRequirements.mockResolvedValue({
  112. archive_id: 100,
  113. filename: 'Cube.3mf',
  114. plate_id: 1,
  115. filaments: [],
  116. });
  117. // Default: no bundles imported. Bundle-tier tests override this with a
  118. // populated array; everything else inherits the empty default so the
  119. // modal renders the original (preset-only) layout.
  120. mockApi.listSlicerBundles.mockResolvedValue([]);
  121. });
  122. it('auto-selects the highest-priority tier per slot on first load', async () => {
  123. renderWithTracker({
  124. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  125. onClose: vi.fn(),
  126. });
  127. // SliceModal-specific tier priority: imported (local) wins over cloud
  128. // and standard so the user's curated picks come first.
  129. await waitFor(() => {
  130. expect(screen.getByText('My Custom X1C')).toBeDefined();
  131. });
  132. // 4 selects: printer, process, bed-type (#1337), filament. bed-type sits
  133. // between process and filament — it overrides curr_bed_type on the
  134. // process preset so the related controls cluster — and defaults to "".
  135. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  136. expect(selects).toHaveLength(4);
  137. expect(selects[0].value).toBe('local:1');
  138. expect(selects[1].value).toBe('local:2');
  139. expect(selects[2].value).toBe('');
  140. expect(selects[3].value).toBe('local:3');
  141. // Slice button is enabled because all three slots auto-defaulted and
  142. // the preview-slice query has resolved (mock returns immediately).
  143. const sliceBtn = screen.getByRole('button', { name: /^Slice$/ });
  144. expect((sliceBtn as HTMLButtonElement).disabled).toBe(false);
  145. });
  146. it('renders Imported / Cloud / Standard sections via <optgroup>', async () => {
  147. renderWithTracker({
  148. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  149. onClose: vi.fn(),
  150. });
  151. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  152. const printerSelect = screen.getAllByRole('combobox')[0];
  153. const groups = printerSelect.querySelectorAll('optgroup');
  154. expect(Array.from(groups).map((g) => g.label)).toEqual([
  155. 'Imported',
  156. 'Cloud',
  157. 'Standard',
  158. ]);
  159. // Each entry sits inside its own tier's group — pin the assignment so
  160. // a future render-shape change can't quietly mix them. Order matches
  161. // SLICE_MODAL_TIER_ORDER (local → cloud → standard).
  162. const localGroup = groups[0];
  163. expect(within(localGroup as HTMLElement).getByText('Imported X1C 0.4')).toBeDefined();
  164. const cloudGroup = groups[1];
  165. expect(within(cloudGroup as HTMLElement).getByText('My Custom X1C')).toBeDefined();
  166. const standardGroup = groups[2];
  167. expect(within(standardGroup as HTMLElement).getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined();
  168. });
  169. it('falls back to local when cloud is empty (auto-pick respects priority)', async () => {
  170. mockApi.getSlicerPresets.mockResolvedValue(
  171. makeUnified({
  172. local: fullThreeTier.local,
  173. standard: fullThreeTier.standard,
  174. }),
  175. );
  176. renderWithTracker({
  177. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  178. onClose: vi.fn(),
  179. });
  180. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  181. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  182. expect(selects[0].value).toBe('local:1');
  183. });
  184. it('falls back to standard when both cloud and local are empty', async () => {
  185. mockApi.getSlicerPresets.mockResolvedValue(
  186. makeUnified({ standard: fullThreeTier.standard }),
  187. );
  188. renderWithTracker({
  189. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  190. onClose: vi.fn(),
  191. });
  192. await waitFor(() => expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined());
  193. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  194. expect(selects[0].value).toBe('standard:Bambu Lab X1 Carbon 0.4 nozzle');
  195. });
  196. it('sends source-aware refs (not legacy bare ints) on submit', async () => {
  197. const onClose = vi.fn();
  198. mockApi.sliceLibraryFile.mockResolvedValue({
  199. job_id: 42,
  200. status: 'pending',
  201. status_url: '/api/v1/slice-jobs/42',
  202. });
  203. renderWithTracker({
  204. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  205. onClose,
  206. });
  207. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  208. const user = userEvent.setup();
  209. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  210. await waitFor(() => {
  211. // SliceModal-specific tier priority puts imported (local) above cloud,
  212. // so the auto-pick lands on the local entries even when a cloud entry
  213. // with the same slot is also available in the listing.
  214. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(100, {
  215. printer_preset: { source: 'local', id: '1' },
  216. process_preset: { source: 'local', id: '2' },
  217. filament_preset: { source: 'local', id: '3' },
  218. filament_presets: [{ source: 'local', id: '3' }],
  219. });
  220. });
  221. await waitFor(() => expect(onClose).toHaveBeenCalled());
  222. });
  223. it('includes bed_type in the request when the user picks a non-auto plate (#1337)', async () => {
  224. const onClose = vi.fn();
  225. mockApi.sliceLibraryFile.mockResolvedValue({
  226. job_id: 42,
  227. status: 'pending',
  228. status_url: '/api/v1/slice-jobs/42',
  229. });
  230. renderWithTracker({
  231. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  232. onClose,
  233. });
  234. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  235. const user = userEvent.setup();
  236. // Order with the dropdown now sits between Process and Filament:
  237. // printer (0), process (1), bed-type (2), filament (3+). Find the
  238. // bed-type select by name rather than positional index so this stays
  239. // green if the layout adds another control around it.
  240. const bedSelect = screen.getAllByRole('combobox').find((el) =>
  241. (el as HTMLSelectElement).options[0]?.textContent?.toLowerCase().includes('auto'),
  242. ) as HTMLSelectElement;
  243. expect(bedSelect).toBeDefined();
  244. await user.selectOptions(bedSelect, 'Textured PEI Plate');
  245. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  246. await waitFor(() => {
  247. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  248. 100,
  249. expect.objectContaining({ bed_type: 'Textured PEI Plate' }),
  250. );
  251. });
  252. });
  253. it('omits bed_type when the user leaves it on Auto (no override)', async () => {
  254. const onClose = vi.fn();
  255. mockApi.sliceLibraryFile.mockResolvedValue({
  256. job_id: 42,
  257. status: 'pending',
  258. status_url: '/api/v1/slice-jobs/42',
  259. });
  260. renderWithTracker({
  261. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  262. onClose,
  263. });
  264. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  265. const user = userEvent.setup();
  266. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  267. await waitFor(() => {
  268. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  269. expect(body).not.toHaveProperty('bed_type');
  270. });
  271. });
  272. it('lets the user override the default and pick a Standard preset', async () => {
  273. const onClose = vi.fn();
  274. mockApi.sliceLibraryFile.mockResolvedValue({
  275. job_id: 42,
  276. status: 'pending',
  277. status_url: '/api/v1/slice-jobs/42',
  278. });
  279. renderWithTracker({
  280. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  281. onClose,
  282. });
  283. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  284. const user = userEvent.setup();
  285. const selects = screen.getAllByRole('combobox');
  286. await user.selectOptions(selects[0], 'standard:Bambu Lab X1 Carbon 0.4 nozzle');
  287. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  288. await waitFor(() => {
  289. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  290. 100,
  291. expect.objectContaining({
  292. printer_preset: { source: 'standard', id: 'Bambu Lab X1 Carbon 0.4 nozzle' },
  293. }),
  294. );
  295. });
  296. });
  297. it('routes archive sources to sliceArchive instead of sliceLibraryFile', async () => {
  298. const onClose = vi.fn();
  299. mockApi.sliceArchive.mockResolvedValue({
  300. job_id: 7,
  301. status: 'pending',
  302. status_url: '/api/v1/slice-jobs/7',
  303. });
  304. renderWithTracker({
  305. source: { kind: 'archive', id: 86, filename: 'orca.3mf' },
  306. onClose,
  307. });
  308. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  309. const user = userEvent.setup();
  310. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  311. await waitFor(() => {
  312. expect(mockApi.sliceArchive).toHaveBeenCalledWith(86, expect.any(Object));
  313. expect(mockApi.sliceLibraryFile).not.toHaveBeenCalled();
  314. });
  315. });
  316. it('surfaces enqueue errors inline and keeps the modal open', async () => {
  317. const onClose = vi.fn();
  318. mockApi.sliceLibraryFile.mockRejectedValue(new Error('Server says no'));
  319. renderWithTracker({
  320. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  321. onClose,
  322. });
  323. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  324. const user = userEvent.setup();
  325. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  326. await waitFor(() => {
  327. expect(screen.getByRole('alert')).toHaveTextContent('Server says no');
  328. });
  329. expect(onClose).not.toHaveBeenCalled();
  330. });
  331. it('shows a friendly notice when getSlicerPresets fails', async () => {
  332. mockApi.getSlicerPresets.mockRejectedValue(new Error('500'));
  333. renderWithTracker({
  334. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  335. onClose: vi.fn(),
  336. });
  337. await waitFor(() => {
  338. expect(screen.getByRole('alert')).toHaveTextContent(/Failed to load presets/i);
  339. });
  340. });
  341. it('renders a "sign in" banner when cloud_status is not_authenticated', async () => {
  342. mockApi.getSlicerPresets.mockResolvedValue(
  343. makeUnified({
  344. cloud_status: 'not_authenticated',
  345. local: fullThreeTier.local,
  346. standard: fullThreeTier.standard,
  347. }),
  348. );
  349. renderWithTracker({
  350. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  351. onClose: vi.fn(),
  352. });
  353. await waitFor(() => {
  354. expect(screen.getByRole('status')).toHaveTextContent(/Sign in to Bambu Cloud/i);
  355. });
  356. });
  357. it('renders an "expired" banner when cloud_status is expired', async () => {
  358. mockApi.getSlicerPresets.mockResolvedValue(
  359. makeUnified({
  360. cloud_status: 'expired',
  361. local: fullThreeTier.local,
  362. }),
  363. );
  364. renderWithTracker({
  365. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  366. onClose: vi.fn(),
  367. });
  368. await waitFor(() => {
  369. expect(screen.getByRole('status')).toHaveTextContent(/expired/i);
  370. });
  371. });
  372. it('omits the banner entirely when cloud_status is ok', async () => {
  373. renderWithTracker({
  374. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  375. onClose: vi.fn(),
  376. });
  377. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  378. // No status-role banner should be rendered on the happy path.
  379. expect(screen.queryByRole('status')).toBeNull();
  380. });
  381. // ----- Multi-plate flow -----------------------------------------------
  382. function makeMultiPlateLibraryResponse() {
  383. return {
  384. file_id: 100,
  385. filename: 'Multi.3mf',
  386. is_multi_plate: true,
  387. plates: [
  388. {
  389. index: 1,
  390. name: 'Plate 1',
  391. objects: ['Cube'],
  392. object_count: 1,
  393. has_thumbnail: false,
  394. thumbnail_url: null,
  395. print_time_seconds: 600,
  396. filament_used_grams: 10,
  397. filaments: [],
  398. },
  399. {
  400. index: 2,
  401. name: 'Plate 2',
  402. objects: ['Pyramid'],
  403. object_count: 1,
  404. has_thumbnail: false,
  405. thumbnail_url: null,
  406. print_time_seconds: 800,
  407. filament_used_grams: 12,
  408. filaments: [],
  409. },
  410. ],
  411. };
  412. }
  413. it('shows the plate picker first for multi-plate library files', async () => {
  414. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  415. renderWithTracker({
  416. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  417. onClose: vi.fn(),
  418. });
  419. // Plate picker renders one button per plate — the accessible name
  420. // joins the heading ("Plate N — name") with the object summary line.
  421. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  422. expect(screen.getByRole('button', { name: /Plate 2.*Pyramid/ })).toBeDefined();
  423. // Profile dropdowns must NOT be visible yet — the user has to pick a
  424. // plate first.
  425. expect(screen.queryByRole('combobox')).toBeNull();
  426. });
  427. it('skips the plate picker for single-plate sources', async () => {
  428. mockApi.getLibraryFilePlates.mockResolvedValue({
  429. file_id: 100,
  430. filename: 'Single.3mf',
  431. is_multi_plate: false,
  432. plates: [
  433. {
  434. index: 1,
  435. name: 'Plate 1',
  436. objects: [],
  437. has_thumbnail: false,
  438. thumbnail_url: null,
  439. print_time_seconds: null,
  440. filament_used_grams: null,
  441. filaments: [],
  442. },
  443. ],
  444. });
  445. renderWithTracker({
  446. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  447. onClose: vi.fn(),
  448. });
  449. // Should jump straight to the profile dropdowns.
  450. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  451. });
  452. it('passes the picked plate to the slice request', async () => {
  453. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  454. mockApi.sliceLibraryFile.mockResolvedValue({
  455. job_id: 42,
  456. status: 'pending',
  457. status_url: '/api/v1/slice-jobs/42',
  458. });
  459. renderWithTracker({
  460. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  461. onClose: vi.fn(),
  462. });
  463. const user = userEvent.setup();
  464. // Step 1: pick Plate 2.
  465. const plate2Button = await screen.findByRole('button', { name: /Plate 2.*Pyramid/ });
  466. await user.click(plate2Button);
  467. // Step 2: profile dropdowns are now visible.
  468. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  469. // Step 3: submit and verify the plate index made it into the body.
  470. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  471. await waitFor(() => {
  472. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  473. 100,
  474. expect.objectContaining({ plate: 2 }),
  475. );
  476. });
  477. });
  478. it('"Slice all plates" toggle sends plate=0 sentinel to the backend (#1493)', async () => {
  479. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  480. mockApi.sliceLibraryFile.mockResolvedValue({
  481. job_id: 42,
  482. status: 'pending',
  483. status_url: '/api/v1/slice-jobs/42',
  484. });
  485. renderWithTracker({
  486. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  487. onClose: vi.fn(),
  488. });
  489. const user = userEvent.setup();
  490. const plate1Button = await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  491. await user.click(plate1Button);
  492. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  493. // The "Slice all plates" checkbox only appears for multi-plate sources.
  494. const toggle = await screen.findByRole('checkbox', { name: /Slice all 2 plates/i });
  495. await user.click(toggle);
  496. // The action button's label flips to the "Slice all" form. Click it.
  497. await user.click(screen.getByRole('button', { name: /Slice all 2 plates/i }));
  498. await waitFor(() => {
  499. expect(mockApi.sliceLibraryFile).toHaveBeenCalledTimes(1);
  500. });
  501. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  502. // ``plate=0`` is the BS CLI's all-plates sentinel — one slice call,
  503. // one output 3MF with every plate's gcode inside, one archive.
  504. expect((body as { plate?: number }).plate).toBe(0);
  505. });
  506. it('"Slice all plates" toggle is hidden for single-plate sources', async () => {
  507. mockApi.getLibraryFilePlates.mockResolvedValue({
  508. file_id: 100,
  509. filename: 'Single.3mf',
  510. is_multi_plate: false,
  511. plates: [
  512. {
  513. index: 1,
  514. name: 'Plate 1',
  515. objects: [],
  516. has_thumbnail: false,
  517. thumbnail_url: null,
  518. print_time_seconds: null,
  519. filament_used_grams: null,
  520. filaments: [],
  521. },
  522. ],
  523. });
  524. renderWithTracker({
  525. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  526. onClose: vi.fn(),
  527. });
  528. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  529. expect(screen.queryByRole('checkbox', { name: /Slice all/i })).toBeNull();
  530. });
  531. it('routes the plate fetch through getArchivePlates for archive sources', async () => {
  532. mockApi.getArchivePlates.mockResolvedValue({
  533. ...makeMultiPlateLibraryResponse(),
  534. archive_id: 100,
  535. filename: 'Multi.3mf',
  536. });
  537. renderWithTracker({
  538. source: { kind: 'archive', id: 100, filename: 'Multi.3mf' },
  539. onClose: vi.fn(),
  540. });
  541. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  542. expect(mockApi.getArchivePlates).toHaveBeenCalledWith(100);
  543. expect(mockApi.getLibraryFilePlates).not.toHaveBeenCalled();
  544. });
  545. it('cancelling the plate picker closes the entire slice flow', async () => {
  546. const onClose = vi.fn();
  547. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  548. renderWithTracker({
  549. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  550. onClose,
  551. });
  552. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  553. const user = userEvent.setup();
  554. await user.click(screen.getByRole('button', { name: /^Close$/i }));
  555. expect(onClose).toHaveBeenCalled();
  556. });
  557. it('omits the plate field when the source is single-plate', async () => {
  558. mockApi.sliceLibraryFile.mockResolvedValue({
  559. job_id: 42,
  560. status: 'pending',
  561. status_url: '/api/v1/slice-jobs/42',
  562. });
  563. renderWithTracker({
  564. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  565. onClose: vi.fn(),
  566. });
  567. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  568. const user = userEvent.setup();
  569. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  570. await waitFor(() => {
  571. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  572. expect(body).not.toHaveProperty('plate');
  573. });
  574. });
  575. // ----- Multi-color flow ------------------------------------------------
  576. function makeMultiColorPlateResponse() {
  577. // Single-plate 3MF that uses two filament slots — mirrors the realistic
  578. // "I have a multi-color file with one plate" case. Multi-plate is a
  579. // separate axis that's already covered above.
  580. return {
  581. file_id: 100,
  582. filename: 'TwoColor.3mf',
  583. is_multi_plate: false,
  584. plates: [
  585. {
  586. index: 1,
  587. name: 'Plate 1',
  588. objects: ['Logo'],
  589. object_count: 1,
  590. has_thumbnail: false,
  591. thumbnail_url: null,
  592. print_time_seconds: 600,
  593. filament_used_grams: 20,
  594. filaments: [],
  595. },
  596. ],
  597. };
  598. }
  599. function makeMultiColorRequirementsResponse() {
  600. return {
  601. file_id: 100,
  602. filename: 'TwoColor.3mf',
  603. plate_id: 1,
  604. filaments: [
  605. { slot_id: 1, type: 'PLA', color: '#000000', used_grams: 10, used_meters: 3 },
  606. { slot_id: 2, type: 'PLA', color: '#FFFFFF', used_grams: 10, used_meters: 3 },
  607. ],
  608. };
  609. }
  610. function makeColorAwarePresets(): UnifiedPresetsResponse {
  611. // Two filament presets in cloud: one black PLA, one white PLA. Pre-pick
  612. // should match each plate slot to the same-colour preset so the user
  613. // doesn't have to manually align them.
  614. return {
  615. cloud: {
  616. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  617. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  618. filament: [
  619. { id: 'F-BLACK', name: 'Cloud PLA Black', source: 'cloud', filament_type: 'PLA', filament_colour: '#000000' },
  620. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  621. ],
  622. },
  623. local: { printer: [], process: [], filament: [] },
  624. standard: { printer: [], process: [], filament: [] },
  625. cloud_status: 'ok',
  626. };
  627. }
  628. it('renders one filament dropdown per plate slot when the source is multi-color', async () => {
  629. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  630. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  631. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  632. renderWithTracker({
  633. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  634. onClose: vi.fn(),
  635. });
  636. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  637. // 1 printer + 1 process + 2 filament + 1 bed-type (#1337) = 5 dropdowns.
  638. expect(screen.getAllByRole('combobox')).toHaveLength(5);
  639. });
  640. it('pre-picks each filament slot by matching colour metadata', async () => {
  641. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  642. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  643. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  644. mockApi.sliceLibraryFile.mockResolvedValue({
  645. job_id: 42,
  646. status: 'pending',
  647. status_url: '/api/v1/slice-jobs/42',
  648. });
  649. renderWithTracker({
  650. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  651. onClose: vi.fn(),
  652. });
  653. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  654. const user = userEvent.setup();
  655. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  656. await waitFor(() => {
  657. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  658. // Slot 1 was black plate → cloud black preset; slot 2 was white →
  659. // cloud white preset. Pre-pick aligns them by metadata so the user
  660. // doesn't have to swap them manually.
  661. expect(body.filament_presets).toEqual([
  662. { source: 'cloud', id: 'F-BLACK' },
  663. { source: 'cloud', id: 'F-WHITE' },
  664. ]);
  665. });
  666. });
  667. it('still sends the legacy filament_preset for single-color flows', async () => {
  668. // Backwards-compat with backends / proxies that read the singular field.
  669. mockApi.sliceLibraryFile.mockResolvedValue({
  670. job_id: 42,
  671. status: 'pending',
  672. status_url: '/api/v1/slice-jobs/42',
  673. });
  674. renderWithTracker({
  675. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  676. onClose: vi.fn(),
  677. });
  678. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  679. const user = userEvent.setup();
  680. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  681. await waitFor(() => {
  682. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  683. // Single-color path mirrors the array's first entry into the legacy
  684. // singular so older backend clients that only know about
  685. // `filament_preset` still work.
  686. expect(body.filament_preset).toEqual(body.filament_presets[0]);
  687. expect(body.filament_presets).toHaveLength(1);
  688. });
  689. });
  690. it('lets the user override a pre-picked filament slot', async () => {
  691. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  692. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  693. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  694. mockApi.sliceLibraryFile.mockResolvedValue({
  695. job_id: 42,
  696. status: 'pending',
  697. status_url: '/api/v1/slice-jobs/42',
  698. });
  699. renderWithTracker({
  700. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  701. onClose: vi.fn(),
  702. });
  703. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  704. const user = userEvent.setup();
  705. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  706. // Order: 0 printer, 1 process, 2 bed-type, 3 filament-1, 4 filament-2
  707. // (#1337). Auto-picks land on printer/process/filaments; bed-type
  708. // defaults to "". Swap filament-1 (index 3) from the auto-picked black
  709. // to white.
  710. await user.selectOptions(selects[3], 'cloud:F-WHITE');
  711. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  712. await waitFor(() => {
  713. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  714. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  715. // Slot 1 stayed at the auto-picked white.
  716. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  717. });
  718. });
  719. // Cross-printer re-slicing is a normal, supported operation as of
  720. // 2026-05-20 (Step 0 empirical test: sidecar overrides printer / process
  721. // / bed / kinematics from the picked bundle, producing valid target-
  722. // printer G-code). No banner, no warning — the picker UI already shows
  723. // which printer the user picked, and that's enough.
  724. it('does not surface any cross-printer banner and keeps Slice enabled when models differ', async () => {
  725. mockApi.getLibraryFilePlates.mockResolvedValue({
  726. file_id: 100,
  727. filename: 'A1Original.3mf',
  728. is_multi_plate: false,
  729. plates: [
  730. {
  731. index: 1,
  732. name: 'Plate 1',
  733. objects: [],
  734. has_thumbnail: false,
  735. thumbnail_url: null,
  736. print_time_seconds: null,
  737. filament_used_grams: null,
  738. filaments: [],
  739. },
  740. ],
  741. });
  742. // Standard tier offers an X1C profile — the user picks (auto-picks) it.
  743. mockApi.getSlicerPresets.mockResolvedValue(makeUnified({
  744. standard: {
  745. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  746. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  747. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  748. },
  749. }));
  750. renderWithTracker({
  751. source: { kind: 'libraryFile', id: 100, filename: 'A1Original.3mf' },
  752. onClose: vi.fn(),
  753. });
  754. await waitFor(() =>
  755. expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined(),
  756. );
  757. // No banner, no alert — re-slicing across printers is just a normal slice now.
  758. expect(screen.queryByRole('alert')).toBeNull();
  759. const sliceButton = screen.getByRole('button', { name: /^Slice$/ }) as HTMLButtonElement;
  760. expect(sliceButton.disabled).toBe(false);
  761. });
  762. // The `used_in_plate` flag tells the modal which AMS slots are
  763. // actually consumed by the picked plate. Slots flagged as unused
  764. // are still rendered (the slicer CLI needs a profile per project
  765. // slot, otherwise it silently fills the gap from embedded defaults
  766. // and unwanted colours leak into the output) but disabled in the UI
  767. // so the user only interacts with the dropdowns that matter.
  768. it('disables filament dropdowns for slots not used by the picked plate', async () => {
  769. mockApi.getLibraryFilePlates.mockResolvedValue({
  770. file_id: 100,
  771. filename: 'Helmet.3mf',
  772. is_multi_plate: false,
  773. plates: [
  774. {
  775. index: 1,
  776. name: 'Plate 1',
  777. objects: ['Helmet'],
  778. has_thumbnail: false,
  779. thumbnail_url: null,
  780. print_time_seconds: 1200,
  781. filament_used_grams: 80,
  782. filaments: [],
  783. },
  784. ],
  785. });
  786. // Project has 2 AMS slots configured (white + grey support), but
  787. // plate 1 only paints with white (slot 1). The backend now returns
  788. // BOTH slots with used_in_plate flagging the difference.
  789. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  790. file_id: 100,
  791. filename: 'Helmet.3mf',
  792. plate_id: 1,
  793. filaments: [
  794. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  795. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  796. ],
  797. });
  798. mockApi.getSlicerPresets.mockResolvedValue({
  799. cloud: {
  800. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  801. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  802. filament: [
  803. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  804. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  805. ],
  806. },
  807. local: { printer: [], process: [], filament: [] },
  808. standard: { printer: [], process: [], filament: [] },
  809. cloud_status: 'ok',
  810. });
  811. renderWithTracker({
  812. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  813. onClose: vi.fn(),
  814. });
  815. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  816. // Both filament rows render — 1 printer + 1 process + 1 bed-type +
  817. // 2 filament (#1337) = 5. bed-type sits at index 2, filament slots
  818. // follow at 3 and 4.
  819. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  820. expect(selects).toHaveLength(5);
  821. // Slot 1 (used) is editable, slot 2 (not used) is disabled.
  822. expect(selects[3].disabled).toBe(false);
  823. expect(selects[4].disabled).toBe(true);
  824. // The disabled row's label calls out why it's disabled.
  825. expect(screen.getByText(/not used by this plate/i)).toBeDefined();
  826. });
  827. it('still sends both filaments to the backend even when one slot is disabled', async () => {
  828. // The auto-pick scoring fills the disabled slot from project
  829. // metadata — the slicer CLI requires a profile for every project
  830. // slot, otherwise it silently fills the gap. The disabled UI is
  831. // purely cosmetic; the wire format must include the full list.
  832. mockApi.getLibraryFilePlates.mockResolvedValue({
  833. file_id: 100,
  834. filename: 'Helmet.3mf',
  835. is_multi_plate: false,
  836. plates: [
  837. {
  838. index: 1,
  839. name: 'Plate 1',
  840. objects: ['Helmet'],
  841. has_thumbnail: false,
  842. thumbnail_url: null,
  843. print_time_seconds: 1200,
  844. filament_used_grams: 80,
  845. filaments: [],
  846. },
  847. ],
  848. });
  849. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  850. file_id: 100,
  851. filename: 'Helmet.3mf',
  852. plate_id: 1,
  853. filaments: [
  854. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  855. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  856. ],
  857. });
  858. mockApi.getSlicerPresets.mockResolvedValue({
  859. cloud: {
  860. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  861. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  862. filament: [
  863. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  864. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  865. ],
  866. },
  867. local: { printer: [], process: [], filament: [] },
  868. standard: { printer: [], process: [], filament: [] },
  869. cloud_status: 'ok',
  870. });
  871. mockApi.sliceLibraryFile.mockResolvedValue({
  872. job_id: 50,
  873. status: 'pending',
  874. status_url: '/api/v1/slice-jobs/50',
  875. });
  876. renderWithTracker({
  877. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  878. onClose: vi.fn(),
  879. });
  880. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  881. const user = userEvent.setup();
  882. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  883. await waitFor(() => {
  884. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  885. // Both slots populated: slot 1 with the user's white pick, slot
  886. // 2 auto-picked with grey from the colour-match scoring.
  887. expect(body.filament_presets).toHaveLength(2);
  888. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  889. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-GREY' });
  890. });
  891. });
  892. // -------------------------------------------------------------------------
  893. // Bundle tier — picking an imported .bbscfg replaces the cloud/local/standard
  894. // dropdown set with bundle-scoped pickers and routes the slice through the
  895. // backend's bundle dispatch shape (no PresetRefs in the body).
  896. // -------------------------------------------------------------------------
  897. describe('Bundle tier', () => {
  898. const sampleBundle = {
  899. id: 'abc123def456abcd',
  900. printer_preset_name: '# Bambu Lab H2D 0.4 nozzle',
  901. printer: ['# Bambu Lab H2D 0.4 nozzle'],
  902. process: [
  903. '# 0.20mm Standard @BBL H2D',
  904. '# 0.16mm Standard @BBL H2D',
  905. ],
  906. filament: [
  907. '# Bambu PLA Basic @BBL H2D',
  908. '# Bambu PETG HF @BBL H2D 0.4 nozzle',
  909. ],
  910. version: '02.06.00.50',
  911. };
  912. it('hides the bundle picker when no bundles are imported', async () => {
  913. // Default beforeEach already returns []; assert the picker isn't
  914. // rendered so users without bundles see the original layout.
  915. renderWithTracker({
  916. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  917. onClose: vi.fn(),
  918. });
  919. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  920. expect(screen.queryByText(/slicer bundle/i)).toBeNull();
  921. });
  922. it('renders the bundle picker when at least one bundle is imported', async () => {
  923. mockApi.listSlicerBundles.mockResolvedValue([sampleBundle]);
  924. renderWithTracker({
  925. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  926. onClose: vi.fn(),
  927. });
  928. await waitFor(() =>
  929. expect(screen.getByText(/slicer bundle/i)).toBeDefined(),
  930. );
  931. // The bundle option is in the dropdown.
  932. const bundleSelect = screen.getAllByRole('combobox')[0] as HTMLSelectElement;
  933. expect(
  934. Array.from(bundleSelect.options).map((o) => o.textContent),
  935. ).toContain('# Bambu Lab H2D 0.4 nozzle');
  936. });
  937. it('replaces preset dropdowns with bundle-scoped pickers when a bundle is selected', async () => {
  938. mockApi.listSlicerBundles.mockResolvedValue([sampleBundle]);
  939. renderWithTracker({
  940. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  941. onClose: vi.fn(),
  942. });
  943. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  944. const user = userEvent.setup();
  945. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  946. // First select is the bundle picker (new top-of-modal dropdown).
  947. await user.selectOptions(selects[0], sampleBundle.id);
  948. // Wait for the bundle-mode UI to take over: process options should
  949. // now reflect the bundle's process names.
  950. await waitFor(() => {
  951. expect(
  952. screen.getByText('# 0.20mm Standard @BBL H2D'),
  953. ).toBeDefined();
  954. });
  955. // The static printer label shows the bundle's printer. Both the
  956. // <option> in the bundle picker and the read-only <div> below
  957. // contain this text, so use getAllByText.
  958. const printerNameMatches = screen.getAllByText('# Bambu Lab H2D 0.4 nozzle');
  959. expect(printerNameMatches.length).toBeGreaterThanOrEqual(2);
  960. // Cloud/local/standard preset names from the original tier no longer
  961. // appear in the visible dropdowns (the bundle replaced them).
  962. const visibleSelects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  963. const allOptionTexts = visibleSelects.flatMap((sel) =>
  964. Array.from(sel.options).map((o) => o.textContent ?? ''),
  965. );
  966. // Cloud printer name shouldn't be in any visible dropdown anymore.
  967. expect(allOptionTexts).not.toContain('My Custom X1C');
  968. });
  969. it('submits bundle dispatch shape (no PresetRefs) when a bundle is selected', async () => {
  970. mockApi.listSlicerBundles.mockResolvedValue([sampleBundle]);
  971. mockApi.sliceLibraryFile.mockResolvedValue({
  972. job_id: 99,
  973. status: 'pending',
  974. status_url: '/api/v1/slice-jobs/99',
  975. });
  976. renderWithTracker({
  977. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  978. onClose: vi.fn(),
  979. });
  980. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  981. const user = userEvent.setup();
  982. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  983. await user.selectOptions(selects[0], sampleBundle.id);
  984. // Wait for bundle-mode dropdowns to render.
  985. await waitFor(() =>
  986. expect(screen.getByText('# 0.20mm Standard @BBL H2D')).toBeDefined(),
  987. );
  988. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  989. await waitFor(() => {
  990. const [fileId, body] = mockApi.sliceLibraryFile.mock.calls[0];
  991. expect(fileId).toBe(100);
  992. expect(body.bundle).toEqual({
  993. bundle_id: sampleBundle.id,
  994. printer_name: '# Bambu Lab H2D 0.4 nozzle',
  995. process_name: '# 0.20mm Standard @BBL H2D',
  996. filament_names: ['# Bambu PLA Basic @BBL H2D'],
  997. });
  998. // The preset triplet must NOT be in the body — bundle dispatch
  999. // skips PresetRef resolution entirely on the backend.
  1000. expect(body.printer_preset).toBeUndefined();
  1001. expect(body.process_preset).toBeUndefined();
  1002. expect(body.filament_presets).toBeUndefined();
  1003. });
  1004. });
  1005. it('switching back to "None" restores the preset triplet path', async () => {
  1006. mockApi.listSlicerBundles.mockResolvedValue([sampleBundle]);
  1007. mockApi.sliceLibraryFile.mockResolvedValue({
  1008. job_id: 100,
  1009. status: 'pending',
  1010. status_url: '/api/v1/slice-jobs/100',
  1011. });
  1012. renderWithTracker({
  1013. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1014. onClose: vi.fn(),
  1015. });
  1016. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1017. const user = userEvent.setup();
  1018. const bundleSelect = screen.getAllByRole('combobox')[0] as HTMLSelectElement;
  1019. await user.selectOptions(bundleSelect, sampleBundle.id);
  1020. await waitFor(() =>
  1021. expect(screen.getByText('# 0.20mm Standard @BBL H2D')).toBeDefined(),
  1022. );
  1023. // Flip back to None.
  1024. await user.selectOptions(bundleSelect, '');
  1025. await waitFor(() => {
  1026. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  1027. // After de-selecting bundle, the printer dropdown's first option
  1028. // should be one of the original cloud/local/standard names.
  1029. const printerOptions = Array.from(selects[1].options).map((o) => o.textContent);
  1030. expect(printerOptions).toContain('My Custom X1C');
  1031. });
  1032. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1033. await waitFor(() => {
  1034. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1035. expect(body.bundle).toBeUndefined();
  1036. expect(body.printer_preset).toBeDefined();
  1037. });
  1038. });
  1039. });
  1040. });