SliceModal.test.tsx 43 KB

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