SliceModal.test.tsx 46 KB

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