SliceModal.test.tsx 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  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, afterEach } 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. getSlicerPrinterModels: vi.fn(),
  35. getSlicerPresetValues: vi.fn(),
  36. },
  37. }));
  38. const mockApi = api as unknown as {
  39. getSlicerPresets: ReturnType<typeof vi.fn>;
  40. sliceLibraryFile: ReturnType<typeof vi.fn>;
  41. sliceArchive: ReturnType<typeof vi.fn>;
  42. getSliceJob: ReturnType<typeof vi.fn>;
  43. getLibraryFilePlates: ReturnType<typeof vi.fn>;
  44. getArchivePlates: ReturnType<typeof vi.fn>;
  45. getLibraryFileFilamentRequirements: ReturnType<typeof vi.fn>;
  46. getArchiveFilamentRequirements: ReturnType<typeof vi.fn>;
  47. listSlicerPipelines: ReturnType<typeof vi.fn>;
  48. createSlicerPipeline: ReturnType<typeof vi.fn>;
  49. getSlicerPrinterModels: ReturnType<typeof vi.fn>;
  50. getSlicerPresetValues: ReturnType<typeof vi.fn>;
  51. };
  52. function makeUnified(overrides: Partial<UnifiedPresetsResponse> = {}): UnifiedPresetsResponse {
  53. return {
  54. orca_cloud: { printer: [], process: [], filament: [] },
  55. cloud: { printer: [], process: [], filament: [] },
  56. local: { printer: [], process: [], filament: [] },
  57. standard: { printer: [], process: [], filament: [] },
  58. cloud_status: 'ok',
  59. orca_cloud_status: 'ok',
  60. ...overrides,
  61. };
  62. }
  63. const fullThreeTier: UnifiedPresetsResponse = makeUnified({
  64. cloud: {
  65. printer: [{ id: 'PFUcloud-printer', name: 'My Custom X1C', source: 'cloud' }],
  66. process: [{ id: 'PFUcloud-process', name: 'My 0.16mm Tweaked', source: 'cloud' }],
  67. filament: [{ id: 'PFUcloud-filament', name: 'My PLA Black', source: 'cloud' }],
  68. },
  69. local: {
  70. printer: [{ id: '1', name: 'Imported X1C 0.4', source: 'local' }],
  71. process: [{ id: '2', name: 'Imported 0.20mm', source: 'local' }],
  72. filament: [{ id: '3', name: 'Imported PLA Basic', source: 'local' }],
  73. },
  74. standard: {
  75. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  76. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  77. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  78. },
  79. });
  80. function renderWithTracker(props: Parameters<typeof SliceModal>[0]) {
  81. return render(
  82. <SliceJobTrackerProvider>
  83. <SliceModal {...props} />
  84. </SliceJobTrackerProvider>,
  85. );
  86. }
  87. // SliceModal renders one extra combobox for the Slicer Pipelines (#1425)
  88. // "Apply pipeline" dropdown above the preset slots. Tests written before
  89. // pipelines landed assume selects[0] = printer; this helper drops the
  90. // pipeline combobox so those indices stay stable.
  91. function presetSelects(): HTMLSelectElement[] {
  92. return (screen.getAllByRole('combobox') as HTMLSelectElement[]).filter(
  93. (el) => el.getAttribute('aria-label') !== 'Apply pipeline',
  94. );
  95. }
  96. describe('SliceModal', () => {
  97. beforeEach(() => {
  98. vi.clearAllMocks();
  99. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  100. mockApi.getSlicerPresetValues.mockResolvedValue({ resolved: true, values: {}, reason: 'ok' });
  101. mockApi.getSliceJob.mockResolvedValue({
  102. job_id: 42,
  103. status: 'running',
  104. kind: 'library_file',
  105. source_id: 100,
  106. source_name: 'Cube.stl',
  107. created_at: new Date().toISOString(),
  108. started_at: null,
  109. completed_at: null,
  110. });
  111. // Default: single-plate (or non-3MF). Multi-plate tests override this.
  112. mockApi.getLibraryFilePlates.mockResolvedValue({
  113. file_id: 100,
  114. filename: 'Cube.stl',
  115. plates: [],
  116. is_multi_plate: false,
  117. });
  118. mockApi.getArchivePlates.mockResolvedValue({
  119. archive_id: 100,
  120. filename: 'Cube.3mf',
  121. plates: [],
  122. is_multi_plate: false,
  123. });
  124. // Default: no per-plate filament metadata available (mirrors STL or
  125. // unsliced source). Multi-color tests override this.
  126. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  127. file_id: 100,
  128. filename: 'Cube.stl',
  129. plate_id: 1,
  130. filaments: [],
  131. });
  132. mockApi.getArchiveFilamentRequirements.mockResolvedValue({
  133. archive_id: 100,
  134. filename: 'Cube.3mf',
  135. plate_id: 1,
  136. filaments: [],
  137. });
  138. // Default: no saved pipelines. Tests opt in by overriding this.
  139. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  140. });
  141. it('auto-selects the highest-priority tier per slot on first load', async () => {
  142. renderWithTracker({
  143. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  144. onClose: vi.fn(),
  145. });
  146. // SliceModal-specific tier priority: imported (local) wins over cloud
  147. // and standard so the user's curated picks come first.
  148. await waitFor(() => {
  149. expect(screen.getByText('My Custom X1C')).toBeDefined();
  150. });
  151. // 4 selects: printer, process, bed-type (#1337), filament. bed-type sits
  152. // between process and filament — it overrides curr_bed_type on the
  153. // process preset so the related controls cluster — and defaults to "".
  154. const selects = presetSelects();
  155. expect(selects).toHaveLength(4);
  156. expect(selects[0].value).toBe('local:1');
  157. expect(selects[1].value).toBe('local:2');
  158. expect(selects[2].value).toBe('');
  159. expect(selects[3].value).toBe('local:3');
  160. // Slice button is enabled because all three slots auto-defaulted and
  161. // the preview-slice query has resolved (mock returns immediately).
  162. const sliceBtn = screen.getByRole('button', { name: /^Slice$/ });
  163. expect((sliceBtn as HTMLButtonElement).disabled).toBe(false);
  164. });
  165. it('renders Imported / Cloud / Standard sections via <optgroup>', async () => {
  166. renderWithTracker({
  167. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  168. onClose: vi.fn(),
  169. });
  170. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  171. const printerSelect = presetSelects()[0];
  172. const groups = printerSelect.querySelectorAll('optgroup');
  173. expect(Array.from(groups).map((g) => g.label)).toEqual([
  174. 'Imported',
  175. 'Bambu Cloud',
  176. 'Standard',
  177. ]);
  178. // Each entry sits inside its own tier's group — pin the assignment so
  179. // a future render-shape change can't quietly mix them. Order matches
  180. // SLICE_MODAL_TIER_ORDER (local → cloud → standard).
  181. const localGroup = groups[0];
  182. expect(within(localGroup as HTMLElement).getByText('Imported X1C 0.4')).toBeDefined();
  183. const cloudGroup = groups[1];
  184. expect(within(cloudGroup as HTMLElement).getByText('My Custom X1C')).toBeDefined();
  185. const standardGroup = groups[2];
  186. expect(within(standardGroup as HTMLElement).getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined();
  187. });
  188. it('falls back to local when cloud is empty (auto-pick respects priority)', async () => {
  189. mockApi.getSlicerPresets.mockResolvedValue(
  190. makeUnified({
  191. local: fullThreeTier.local,
  192. standard: fullThreeTier.standard,
  193. }),
  194. );
  195. renderWithTracker({
  196. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  197. onClose: vi.fn(),
  198. });
  199. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  200. const selects = presetSelects();
  201. expect(selects[0].value).toBe('local:1');
  202. });
  203. it('falls back to standard when both cloud and local are empty', async () => {
  204. mockApi.getSlicerPresets.mockResolvedValue(
  205. makeUnified({ standard: fullThreeTier.standard }),
  206. );
  207. renderWithTracker({
  208. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  209. onClose: vi.fn(),
  210. });
  211. await waitFor(() => expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined());
  212. const selects = presetSelects();
  213. expect(selects[0].value).toBe('standard:Bambu Lab X1 Carbon 0.4 nozzle');
  214. });
  215. it('sends source-aware refs (not legacy bare ints) on submit', async () => {
  216. const onClose = vi.fn();
  217. mockApi.sliceLibraryFile.mockResolvedValue({
  218. job_id: 42,
  219. status: 'pending',
  220. status_url: '/api/v1/slice-jobs/42',
  221. });
  222. renderWithTracker({
  223. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  224. onClose,
  225. });
  226. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  227. const user = userEvent.setup();
  228. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  229. await waitFor(() => {
  230. // SliceModal-specific tier priority puts imported (local) above cloud,
  231. // so the auto-pick lands on the local entries even when a cloud entry
  232. // with the same slot is also available in the listing.
  233. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(100, {
  234. printer_preset: { source: 'local', id: '1' },
  235. process_preset: { source: 'local', id: '2' },
  236. filament_preset: { source: 'local', id: '3' },
  237. filament_presets: [{ source: 'local', id: '3' }],
  238. });
  239. });
  240. await waitFor(() => expect(onClose).toHaveBeenCalled());
  241. });
  242. it('offers "use the file\'s built-in settings" when the printer matches the design, and sends the flag (#2611)', async () => {
  243. const onClose = vi.fn();
  244. mockApi.sliceLibraryFile.mockResolvedValue({
  245. job_id: 42,
  246. status: 'pending',
  247. status_url: '/api/v1/slice-jobs/42',
  248. });
  249. // A project 3MF whose embedded printer matches a listed preset — the
  250. // printer pre-pick lands on it, so selectedPrinterName === embedded and
  251. // the "slice as designed" toggle is offered.
  252. mockApi.getLibraryFilePlates.mockResolvedValue({
  253. file_id: 100,
  254. filename: 'Designed.3mf',
  255. plates: [],
  256. is_multi_plate: false,
  257. embedded_printer: 'Bambu Lab X1 Carbon 0.4 nozzle',
  258. embedded_process: '0.20mm Standard',
  259. });
  260. renderWithTracker({
  261. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  262. onClose,
  263. });
  264. const user = userEvent.setup();
  265. const toggle = (await screen.findByLabelText(
  266. /Use the file's built-in settings/,
  267. )) as HTMLInputElement;
  268. expect(toggle.checked).toBe(false);
  269. // All preset dropdowns are live until the toggle is on, then bypassed —
  270. // the printer included, so changing it can't silently drop the mode.
  271. const printerSelect = presetSelects()[0];
  272. const processSelect = presetSelects()[1];
  273. expect(printerSelect.disabled).toBe(false);
  274. expect(processSelect.disabled).toBe(false);
  275. await user.click(toggle);
  276. expect(printerSelect.disabled).toBe(true);
  277. expect(processSelect.disabled).toBe(true);
  278. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  279. await waitFor(() => {
  280. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  281. 100,
  282. expect.objectContaining({ use_embedded_settings: true }),
  283. );
  284. });
  285. await waitFor(() => expect(onClose).toHaveBeenCalled());
  286. });
  287. it('hides the embedded-settings toggle when the picked printer differs from the design (#2611)', async () => {
  288. // Embedded target is a model with no matching preset in the listing, so
  289. // the printer pre-pick falls back to the local default (Imported X1C),
  290. // which does not match — honouring embedded settings would risk the
  291. // wrong bed, so the toggle stays hidden.
  292. mockApi.getLibraryFilePlates.mockResolvedValue({
  293. file_id: 100,
  294. filename: 'Designed.3mf',
  295. plates: [],
  296. is_multi_plate: false,
  297. embedded_printer: 'Bambu Lab P1S 0.4 nozzle',
  298. embedded_process: '0.20mm Standard',
  299. });
  300. renderWithTracker({
  301. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  302. onClose: vi.fn(),
  303. });
  304. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  305. expect(screen.queryByLabelText(/Use the file's built-in settings/)).toBeNull();
  306. });
  307. // #2622: a MakerWorld 3MF designed for another printer carries the author's
  308. // own process tweaks. BambuStudio records which keys deviate from the stock
  309. // preset inside the file, so a cross-printer re-slice can carry them instead
  310. // of losing them to the picked process profile.
  311. const designedFor = {
  312. file_id: 100,
  313. filename: 'Designed.3mf',
  314. plates: [],
  315. is_multi_plate: false,
  316. embedded_printer: 'Bambu Lab A1 0.4 nozzle',
  317. embedded_process: '0.20mm Standard @BBL A1',
  318. design_overrides: [
  319. { key: 'wall_loops', value: '5', printer_coupled: false },
  320. { key: 'sparse_infill_density', value: '100%', printer_coupled: false },
  321. { key: 'outer_wall_speed', value: '200', printer_coupled: true },
  322. ],
  323. };
  324. // The designer's settings are shown inside the process-settings panel now,
  325. // against the options they belong to, rather than in a list of their own.
  326. // The payload contract below is unchanged: their *values* still travel as
  327. // design_overrides keys, read from the file by the backend.
  328. async function openDesignSection() {
  329. const user = userEvent.setup();
  330. await user.click(await screen.findByRole('button', { name: /Process settings/ }));
  331. await screen.findByPlaceholderText('Search settings');
  332. // Every designer key must be reachable, including expert-tier ones.
  333. await user.click(screen.getByRole('button', { name: 'Expert' }));
  334. return user;
  335. }
  336. /** The panel's per-option "use the file's value" checkbox, by option key. */
  337. function sourceCheckbox(key: string): HTMLInputElement {
  338. const boxes = screen.getAllByRole('checkbox') as HTMLInputElement[];
  339. const found = boxes.find((b) => (b.getAttribute('aria-label') ?? '').includes(key));
  340. if (!found) throw new Error(`no source checkbox for ${key}`);
  341. return found;
  342. }
  343. it("carries the design's printer-independent settings by default (#2622)", async () => {
  344. mockApi.sliceLibraryFile.mockResolvedValue({
  345. job_id: 42,
  346. status: 'pending',
  347. status_url: '/api/v1/slice-jobs/42',
  348. });
  349. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  350. renderWithTracker({
  351. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  352. onClose: vi.fn(),
  353. });
  354. // Two of three pre-selected: the speed key is machine-coupled and is
  355. // offered but never pre-ticked.
  356. await waitFor(() => expect(screen.getByRole('button', { name: /^Slice$/ })).toBeEnabled());
  357. const user = userEvent.setup();
  358. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  359. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  360. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as { design_overrides?: string[] };
  361. expect([...(payload.design_overrides ?? [])].sort()).toEqual(['sparse_infill_density', 'wall_loops']);
  362. });
  363. it('lists every changed setting with its value and flags the machine-coupled ones (#2622)', async () => {
  364. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  365. renderWithTracker({
  366. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  367. onClose: vi.fn(),
  368. });
  369. const user = await openDesignSection();
  370. // Carried keys show the designer's value in the option's own control.
  371. await user.type(screen.getByPlaceholderText('Search settings'), 'wall loops');
  372. await waitFor(() => expect(screen.getByLabelText(/^Wall loops/)).toHaveValue(5));
  373. expect(sourceCheckbox('Wall loops').checked).toBe(true);
  374. await user.clear(screen.getByPlaceholderText('Search settings'));
  375. await user.type(screen.getByPlaceholderText('Search settings'), 'outer wall speed');
  376. // The machine-coupled one is present and flagged, just not pre-ticked.
  377. await waitFor(() => expect(screen.getAllByText("designer's printer").length).toBeGreaterThan(0));
  378. expect(sourceCheckbox('Outer wall').checked).toBe(false);
  379. });
  380. it('lets the user opt a machine-coupled setting in and a safe one out (#2622)', async () => {
  381. mockApi.sliceLibraryFile.mockResolvedValue({
  382. job_id: 42,
  383. status: 'pending',
  384. status_url: '/api/v1/slice-jobs/42',
  385. });
  386. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  387. renderWithTracker({
  388. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  389. onClose: vi.fn(),
  390. });
  391. const user = await openDesignSection();
  392. await user.type(screen.getByPlaceholderText('Search settings'), 'outer wall speed');
  393. await waitFor(() => expect(sourceCheckbox('Outer wall')).toBeInTheDocument());
  394. await user.click(sourceCheckbox('Outer wall'));
  395. await user.clear(screen.getByPlaceholderText('Search settings'));
  396. await user.type(screen.getByPlaceholderText('Search settings'), 'wall loops');
  397. await waitFor(() => expect(sourceCheckbox('Wall loops')).toBeInTheDocument());
  398. await user.click(sourceCheckbox('Wall loops'));
  399. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  400. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  401. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as { design_overrides?: string[] };
  402. expect([...(payload.design_overrides ?? [])].sort()).toEqual(['outer_wall_speed', 'sparse_infill_density']);
  403. });
  404. it('omits design_overrides entirely when the user unticks everything (#2622)', async () => {
  405. mockApi.sliceLibraryFile.mockResolvedValue({
  406. job_id: 42,
  407. status: 'pending',
  408. status_url: '/api/v1/slice-jobs/42',
  409. });
  410. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  411. renderWithTracker({
  412. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  413. onClose: vi.fn(),
  414. });
  415. const user = await openDesignSection();
  416. for (const key of ['Wall loops', 'Sparse infill density']) {
  417. await user.clear(screen.getByPlaceholderText('Search settings'));
  418. await user.type(screen.getByPlaceholderText('Search settings'), key.toLowerCase());
  419. await waitFor(() => expect(sourceCheckbox(key)).toBeInTheDocument());
  420. if (sourceCheckbox(key).checked) await user.click(sourceCheckbox(key));
  421. }
  422. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  423. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  424. expect(mockApi.sliceLibraryFile.mock.calls[0][1]).not.toHaveProperty('design_overrides');
  425. });
  426. it('hides the section for a file that changes nothing (#2622)', async () => {
  427. mockApi.getLibraryFilePlates.mockResolvedValue({ ...designedFor, design_overrides: [] });
  428. renderWithTracker({
  429. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  430. onClose: vi.fn(),
  431. });
  432. await waitFor(() => expect(screen.getByRole('button', { name: /^Slice$/ })).toBeEnabled());
  433. // The panel still exists — it is the editor — but nothing is marked as
  434. // coming from the file.
  435. expect(screen.queryByText('from file')).toBeNull();
  436. expect(screen.queryByText("designer's printer")).toBeNull();
  437. });
  438. it('includes bed_type in the request when the user picks a non-auto plate (#1337)', async () => {
  439. const onClose = vi.fn();
  440. mockApi.sliceLibraryFile.mockResolvedValue({
  441. job_id: 42,
  442. status: 'pending',
  443. status_url: '/api/v1/slice-jobs/42',
  444. });
  445. renderWithTracker({
  446. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  447. onClose,
  448. });
  449. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  450. const user = userEvent.setup();
  451. // Order with the dropdown now sits between Process and Filament:
  452. // printer (0), process (1), bed-type (2), filament (3+). Find the
  453. // bed-type select by name rather than positional index so this stays
  454. // green if the layout adds another control around it.
  455. const bedSelect = presetSelects().find((el) =>
  456. (el as HTMLSelectElement).options[0]?.textContent?.toLowerCase().includes('auto'),
  457. ) as HTMLSelectElement;
  458. expect(bedSelect).toBeDefined();
  459. await user.selectOptions(bedSelect, 'Textured PEI Plate');
  460. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  461. await waitFor(() => {
  462. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  463. 100,
  464. expect.objectContaining({ bed_type: 'Textured PEI Plate' }),
  465. );
  466. });
  467. });
  468. it('omits bed_type when the user leaves it on Auto (no override)', async () => {
  469. const onClose = vi.fn();
  470. mockApi.sliceLibraryFile.mockResolvedValue({
  471. job_id: 42,
  472. status: 'pending',
  473. status_url: '/api/v1/slice-jobs/42',
  474. });
  475. renderWithTracker({
  476. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  477. onClose,
  478. });
  479. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  480. const user = userEvent.setup();
  481. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  482. await waitFor(() => {
  483. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  484. expect(body).not.toHaveProperty('bed_type');
  485. });
  486. });
  487. it('sends the layout flags only for the boxes the user ticked (#2548)', async () => {
  488. mockApi.sliceLibraryFile.mockResolvedValue({
  489. job_id: 42,
  490. status: 'pending',
  491. status_url: '/api/v1/slice-jobs/42',
  492. });
  493. renderWithTracker({
  494. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  495. onClose: vi.fn(),
  496. });
  497. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  498. const user = userEvent.setup();
  499. await user.click(screen.getByRole('checkbox', { name: /Auto-orient objects/ }));
  500. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  501. await waitFor(() => {
  502. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  503. expect(body).toHaveProperty('auto_orient', true);
  504. // The untouched box is omitted, not sent as false. The sidecar reads
  505. // any present value as truthy, so a literal false would arrange every
  506. // slice — the flag has to travel by absence.
  507. expect(body).not.toHaveProperty('auto_arrange');
  508. });
  509. });
  510. it('omits both layout flags when neither box is ticked (#2548)', async () => {
  511. mockApi.sliceLibraryFile.mockResolvedValue({
  512. job_id: 42,
  513. status: 'pending',
  514. status_url: '/api/v1/slice-jobs/42',
  515. });
  516. renderWithTracker({
  517. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  518. onClose: vi.fn(),
  519. });
  520. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  521. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  522. await waitFor(() => {
  523. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  524. expect(body).not.toHaveProperty('auto_orient');
  525. expect(body).not.toHaveProperty('auto_arrange');
  526. });
  527. });
  528. it('lets the user override the default and pick a Standard preset', async () => {
  529. const onClose = vi.fn();
  530. mockApi.sliceLibraryFile.mockResolvedValue({
  531. job_id: 42,
  532. status: 'pending',
  533. status_url: '/api/v1/slice-jobs/42',
  534. });
  535. renderWithTracker({
  536. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  537. onClose,
  538. });
  539. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  540. const user = userEvent.setup();
  541. const selects = presetSelects();
  542. await user.selectOptions(selects[0], 'standard:Bambu Lab X1 Carbon 0.4 nozzle');
  543. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  544. await waitFor(() => {
  545. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  546. 100,
  547. expect.objectContaining({
  548. printer_preset: { source: 'standard', id: 'Bambu Lab X1 Carbon 0.4 nozzle' },
  549. }),
  550. );
  551. });
  552. });
  553. it('routes archive sources to sliceArchive instead of sliceLibraryFile', async () => {
  554. const onClose = vi.fn();
  555. mockApi.sliceArchive.mockResolvedValue({
  556. job_id: 7,
  557. status: 'pending',
  558. status_url: '/api/v1/slice-jobs/7',
  559. });
  560. renderWithTracker({
  561. source: { kind: 'archive', id: 86, filename: 'orca.3mf' },
  562. onClose,
  563. });
  564. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  565. const user = userEvent.setup();
  566. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  567. await waitFor(() => {
  568. expect(mockApi.sliceArchive).toHaveBeenCalledWith(86, expect.any(Object));
  569. expect(mockApi.sliceLibraryFile).not.toHaveBeenCalled();
  570. });
  571. });
  572. it('surfaces enqueue errors inline and keeps the modal open', async () => {
  573. const onClose = vi.fn();
  574. mockApi.sliceLibraryFile.mockRejectedValue(new Error('Server says no'));
  575. renderWithTracker({
  576. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  577. onClose,
  578. });
  579. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  580. const user = userEvent.setup();
  581. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  582. await waitFor(() => {
  583. expect(screen.getByRole('alert')).toHaveTextContent('Server says no');
  584. });
  585. expect(onClose).not.toHaveBeenCalled();
  586. });
  587. it('shows a friendly notice when getSlicerPresets fails', async () => {
  588. mockApi.getSlicerPresets.mockRejectedValue(new Error('500'));
  589. renderWithTracker({
  590. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  591. onClose: vi.fn(),
  592. });
  593. await waitFor(() => {
  594. expect(screen.getByRole('alert')).toHaveTextContent(/Failed to load presets/i);
  595. });
  596. });
  597. it('omits the cloud banner when status is not_authenticated (#1712)', async () => {
  598. // A signed-out user (Bambu or Orca) shouldn't get a permanent "sign in"
  599. // nag at the top of every slice. Sign-in lives on the Profiles page; the
  600. // modal stays silent unless a previously-signed-in session actually broke
  601. // (expired / unreachable).
  602. mockApi.getSlicerPresets.mockResolvedValue(
  603. makeUnified({
  604. cloud_status: 'not_authenticated',
  605. orca_cloud_status: 'not_authenticated',
  606. local: fullThreeTier.local,
  607. standard: fullThreeTier.standard,
  608. }),
  609. );
  610. renderWithTracker({
  611. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  612. onClose: vi.fn(),
  613. });
  614. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  615. expect(screen.queryByRole('status')).toBeNull();
  616. });
  617. it('renders an "expired" banner when cloud_status is expired', async () => {
  618. mockApi.getSlicerPresets.mockResolvedValue(
  619. makeUnified({
  620. cloud_status: 'expired',
  621. local: fullThreeTier.local,
  622. }),
  623. );
  624. renderWithTracker({
  625. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  626. onClose: vi.fn(),
  627. });
  628. await waitFor(() => {
  629. expect(screen.getByRole('status')).toHaveTextContent(/expired/i);
  630. });
  631. });
  632. it('omits the banner entirely when cloud_status is ok', async () => {
  633. renderWithTracker({
  634. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  635. onClose: vi.fn(),
  636. });
  637. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  638. // No status-role banner should be rendered on the happy path.
  639. expect(screen.queryByRole('status')).toBeNull();
  640. });
  641. // ----- Multi-plate flow -----------------------------------------------
  642. function makeMultiPlateLibraryResponse() {
  643. return {
  644. file_id: 100,
  645. filename: 'Multi.3mf',
  646. is_multi_plate: true,
  647. plates: [
  648. {
  649. index: 1,
  650. name: 'Plate 1',
  651. objects: ['Cube'],
  652. object_count: 1,
  653. has_thumbnail: false,
  654. thumbnail_url: null,
  655. print_time_seconds: 600,
  656. filament_used_grams: 10,
  657. filaments: [],
  658. },
  659. {
  660. index: 2,
  661. name: 'Plate 2',
  662. objects: ['Pyramid'],
  663. object_count: 1,
  664. has_thumbnail: false,
  665. thumbnail_url: null,
  666. print_time_seconds: 800,
  667. filament_used_grams: 12,
  668. filaments: [],
  669. },
  670. ],
  671. };
  672. }
  673. it('shows the plate picker first for multi-plate library files', async () => {
  674. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  675. renderWithTracker({
  676. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  677. onClose: vi.fn(),
  678. });
  679. // Plate picker renders one button per plate — the accessible name
  680. // joins the heading ("Plate N — name") with the object summary line.
  681. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  682. expect(screen.getByRole('button', { name: /Plate 2.*Pyramid/ })).toBeDefined();
  683. // Profile dropdowns must NOT be visible yet — the user has to pick a
  684. // plate first.
  685. expect(screen.queryByRole('combobox')).toBeNull();
  686. });
  687. it('skips the plate picker for single-plate sources', async () => {
  688. mockApi.getLibraryFilePlates.mockResolvedValue({
  689. file_id: 100,
  690. filename: 'Single.3mf',
  691. is_multi_plate: false,
  692. plates: [
  693. {
  694. index: 1,
  695. name: 'Plate 1',
  696. objects: [],
  697. has_thumbnail: false,
  698. thumbnail_url: null,
  699. print_time_seconds: null,
  700. filament_used_grams: null,
  701. filaments: [],
  702. },
  703. ],
  704. });
  705. renderWithTracker({
  706. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  707. onClose: vi.fn(),
  708. });
  709. // Should jump straight to the profile dropdowns.
  710. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  711. });
  712. it('passes the picked plate to the slice request', async () => {
  713. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  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: 'Multi.3mf' },
  721. onClose: vi.fn(),
  722. });
  723. const user = userEvent.setup();
  724. // Step 1: pick Plate 2.
  725. const plate2Button = await screen.findByRole('button', { name: /Plate 2.*Pyramid/ });
  726. await user.click(plate2Button);
  727. // Step 2: profile dropdowns are now visible.
  728. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  729. // Step 3: submit and verify the plate index made it into the body.
  730. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  731. await waitFor(() => {
  732. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  733. 100,
  734. expect.objectContaining({ plate: 2 }),
  735. );
  736. });
  737. });
  738. it('"Slice all plates" toggle sends plate=0 sentinel to the backend (#1493)', async () => {
  739. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  740. mockApi.sliceLibraryFile.mockResolvedValue({
  741. job_id: 42,
  742. status: 'pending',
  743. status_url: '/api/v1/slice-jobs/42',
  744. });
  745. renderWithTracker({
  746. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  747. onClose: vi.fn(),
  748. });
  749. const user = userEvent.setup();
  750. const plate1Button = await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  751. await user.click(plate1Button);
  752. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  753. // The "Slice all plates" checkbox only appears for multi-plate sources.
  754. const toggle = await screen.findByRole('checkbox', { name: /Slice all 2 plates/i });
  755. await user.click(toggle);
  756. // The action button's label flips to the "Slice all" form. Click it.
  757. await user.click(screen.getByRole('button', { name: /Slice all 2 plates/i }));
  758. await waitFor(() => {
  759. expect(mockApi.sliceLibraryFile).toHaveBeenCalledTimes(1);
  760. });
  761. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  762. // ``plate=0`` is the BS CLI's all-plates sentinel — one slice call,
  763. // one output 3MF with every plate's gcode inside, one archive.
  764. expect((body as { plate?: number }).plate).toBe(0);
  765. });
  766. it('"Slice all plates" toggle is hidden for single-plate sources', async () => {
  767. mockApi.getLibraryFilePlates.mockResolvedValue({
  768. file_id: 100,
  769. filename: 'Single.3mf',
  770. is_multi_plate: false,
  771. plates: [
  772. {
  773. index: 1,
  774. name: 'Plate 1',
  775. objects: [],
  776. has_thumbnail: false,
  777. thumbnail_url: null,
  778. print_time_seconds: null,
  779. filament_used_grams: null,
  780. filaments: [],
  781. },
  782. ],
  783. });
  784. renderWithTracker({
  785. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  786. onClose: vi.fn(),
  787. });
  788. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  789. expect(screen.queryByRole('checkbox', { name: /Slice all/i })).toBeNull();
  790. });
  791. it('routes the plate fetch through getArchivePlates for archive sources', async () => {
  792. mockApi.getArchivePlates.mockResolvedValue({
  793. ...makeMultiPlateLibraryResponse(),
  794. archive_id: 100,
  795. filename: 'Multi.3mf',
  796. });
  797. renderWithTracker({
  798. source: { kind: 'archive', id: 100, filename: 'Multi.3mf' },
  799. onClose: vi.fn(),
  800. });
  801. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  802. expect(mockApi.getArchivePlates).toHaveBeenCalledWith(100);
  803. expect(mockApi.getLibraryFilePlates).not.toHaveBeenCalled();
  804. });
  805. it('cancelling the plate picker closes the entire slice flow', async () => {
  806. const onClose = vi.fn();
  807. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  808. renderWithTracker({
  809. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  810. onClose,
  811. });
  812. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  813. const user = userEvent.setup();
  814. await user.click(screen.getByRole('button', { name: /^Close$/i }));
  815. expect(onClose).toHaveBeenCalled();
  816. });
  817. it('omits the plate field when the source is single-plate', async () => {
  818. mockApi.sliceLibraryFile.mockResolvedValue({
  819. job_id: 42,
  820. status: 'pending',
  821. status_url: '/api/v1/slice-jobs/42',
  822. });
  823. renderWithTracker({
  824. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  825. onClose: vi.fn(),
  826. });
  827. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  828. const user = userEvent.setup();
  829. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  830. await waitFor(() => {
  831. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  832. expect(body).not.toHaveProperty('plate');
  833. });
  834. });
  835. // ----- Multi-color flow ------------------------------------------------
  836. function makeMultiColorPlateResponse() {
  837. // Single-plate 3MF that uses two filament slots — mirrors the realistic
  838. // "I have a multi-color file with one plate" case. Multi-plate is a
  839. // separate axis that's already covered above.
  840. return {
  841. file_id: 100,
  842. filename: 'TwoColor.3mf',
  843. is_multi_plate: false,
  844. plates: [
  845. {
  846. index: 1,
  847. name: 'Plate 1',
  848. objects: ['Logo'],
  849. object_count: 1,
  850. has_thumbnail: false,
  851. thumbnail_url: null,
  852. print_time_seconds: 600,
  853. filament_used_grams: 20,
  854. filaments: [],
  855. },
  856. ],
  857. };
  858. }
  859. function makeMultiColorRequirementsResponse() {
  860. return {
  861. file_id: 100,
  862. filename: 'TwoColor.3mf',
  863. plate_id: 1,
  864. filaments: [
  865. { slot_id: 1, type: 'PLA', color: '#000000', used_grams: 10, used_meters: 3 },
  866. { slot_id: 2, type: 'PLA', color: '#FFFFFF', used_grams: 10, used_meters: 3 },
  867. ],
  868. };
  869. }
  870. function makeColorAwarePresets(): UnifiedPresetsResponse {
  871. // Two filament presets in cloud: one black PLA, one white PLA. Pre-pick
  872. // should match each plate slot to the same-colour preset so the user
  873. // doesn't have to manually align them.
  874. return {
  875. orca_cloud: { printer: [], process: [], filament: [] },
  876. cloud: {
  877. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  878. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  879. filament: [
  880. { id: 'F-BLACK', name: 'Cloud PLA Black', source: 'cloud', filament_type: 'PLA', filament_colour: '#000000' },
  881. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  882. ],
  883. },
  884. local: { printer: [], process: [], filament: [] },
  885. standard: { printer: [], process: [], filament: [] },
  886. cloud_status: 'ok',
  887. orca_cloud_status: 'ok',
  888. };
  889. }
  890. it('renders one filament dropdown per plate slot when the source is multi-color', async () => {
  891. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  892. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  893. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  894. renderWithTracker({
  895. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  896. onClose: vi.fn(),
  897. });
  898. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  899. // 1 printer + 1 process + 2 filament + 1 bed-type (#1337) = 5 dropdowns.
  900. expect(presetSelects()).toHaveLength(5);
  901. });
  902. it('pre-picks each filament slot by matching colour metadata', async () => {
  903. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  904. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  905. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  906. mockApi.sliceLibraryFile.mockResolvedValue({
  907. job_id: 42,
  908. status: 'pending',
  909. status_url: '/api/v1/slice-jobs/42',
  910. });
  911. renderWithTracker({
  912. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  913. onClose: vi.fn(),
  914. });
  915. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  916. const user = userEvent.setup();
  917. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  918. await waitFor(() => {
  919. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  920. // Slot 1 was black plate → cloud black preset; slot 2 was white →
  921. // cloud white preset. Pre-pick aligns them by metadata so the user
  922. // doesn't have to swap them manually.
  923. expect(body.filament_presets).toEqual([
  924. { source: 'cloud', id: 'F-BLACK' },
  925. { source: 'cloud', id: 'F-WHITE' },
  926. ]);
  927. });
  928. });
  929. it('still sends the legacy filament_preset for single-color flows', async () => {
  930. // Backwards-compat with backends / proxies that read the singular field.
  931. mockApi.sliceLibraryFile.mockResolvedValue({
  932. job_id: 42,
  933. status: 'pending',
  934. status_url: '/api/v1/slice-jobs/42',
  935. });
  936. renderWithTracker({
  937. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  938. onClose: vi.fn(),
  939. });
  940. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  941. const user = userEvent.setup();
  942. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  943. await waitFor(() => {
  944. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  945. // Single-color path mirrors the array's first entry into the legacy
  946. // singular so older backend clients that only know about
  947. // `filament_preset` still work.
  948. expect(body.filament_preset).toEqual(body.filament_presets[0]);
  949. expect(body.filament_presets).toHaveLength(1);
  950. });
  951. });
  952. it('lets the user override a pre-picked filament slot', async () => {
  953. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  954. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  955. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  956. mockApi.sliceLibraryFile.mockResolvedValue({
  957. job_id: 42,
  958. status: 'pending',
  959. status_url: '/api/v1/slice-jobs/42',
  960. });
  961. renderWithTracker({
  962. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  963. onClose: vi.fn(),
  964. });
  965. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  966. const user = userEvent.setup();
  967. const selects = presetSelects();
  968. // Order: 0 printer, 1 process, 2 bed-type, 3 filament-1, 4 filament-2
  969. // (#1337). Auto-picks land on printer/process/filaments; bed-type
  970. // defaults to "". Swap filament-1 (index 3) from the auto-picked black
  971. // to white.
  972. await user.selectOptions(selects[3], 'cloud:F-WHITE');
  973. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  974. await waitFor(() => {
  975. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  976. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  977. // Slot 1 stayed at the auto-picked white.
  978. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  979. });
  980. });
  981. // Cross-printer re-slicing is a normal, supported operation as of
  982. // 2026-05-20 (Step 0 empirical test: sidecar overrides printer / process
  983. // / bed / kinematics from the picked profile triplet, producing valid
  984. // target-printer G-code). No banner, no warning — the picker UI already
  985. // shows which printer the user picked, and that's enough.
  986. it('does not surface any cross-printer banner and keeps Slice enabled when models differ', async () => {
  987. mockApi.getLibraryFilePlates.mockResolvedValue({
  988. file_id: 100,
  989. filename: 'A1Original.3mf',
  990. is_multi_plate: false,
  991. plates: [
  992. {
  993. index: 1,
  994. name: 'Plate 1',
  995. objects: [],
  996. has_thumbnail: false,
  997. thumbnail_url: null,
  998. print_time_seconds: null,
  999. filament_used_grams: null,
  1000. filaments: [],
  1001. },
  1002. ],
  1003. });
  1004. // Standard tier offers an X1C profile — the user picks (auto-picks) it.
  1005. mockApi.getSlicerPresets.mockResolvedValue(makeUnified({
  1006. standard: {
  1007. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  1008. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  1009. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  1010. },
  1011. }));
  1012. renderWithTracker({
  1013. source: { kind: 'libraryFile', id: 100, filename: 'A1Original.3mf' },
  1014. onClose: vi.fn(),
  1015. });
  1016. await waitFor(() =>
  1017. expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined(),
  1018. );
  1019. // No banner, no alert — re-slicing across printers is just a normal slice now.
  1020. expect(screen.queryByRole('alert')).toBeNull();
  1021. const sliceButton = screen.getByRole('button', { name: /^Slice$/ }) as HTMLButtonElement;
  1022. expect(sliceButton.disabled).toBe(false);
  1023. });
  1024. // The `used_in_plate` flag tells the modal which AMS slots are
  1025. // actually consumed by the picked plate. Slots flagged as unused
  1026. // are still rendered (the slicer CLI needs a profile per project
  1027. // slot, otherwise it silently fills the gap from embedded defaults
  1028. // and unwanted colours leak into the output) but disabled in the UI
  1029. // so the user only interacts with the dropdowns that matter.
  1030. it('disables filament dropdowns for slots not used by the picked plate', async () => {
  1031. mockApi.getLibraryFilePlates.mockResolvedValue({
  1032. file_id: 100,
  1033. filename: 'Helmet.3mf',
  1034. is_multi_plate: false,
  1035. plates: [
  1036. {
  1037. index: 1,
  1038. name: 'Plate 1',
  1039. objects: ['Helmet'],
  1040. has_thumbnail: false,
  1041. thumbnail_url: null,
  1042. print_time_seconds: 1200,
  1043. filament_used_grams: 80,
  1044. filaments: [],
  1045. },
  1046. ],
  1047. });
  1048. // Project has 2 AMS slots configured (white + grey support), but
  1049. // plate 1 only paints with white (slot 1). The backend now returns
  1050. // BOTH slots with used_in_plate flagging the difference.
  1051. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1052. file_id: 100,
  1053. filename: 'Helmet.3mf',
  1054. plate_id: 1,
  1055. filaments: [
  1056. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  1057. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  1058. ],
  1059. });
  1060. mockApi.getSlicerPresets.mockResolvedValue({
  1061. cloud: {
  1062. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1063. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1064. filament: [
  1065. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  1066. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  1067. ],
  1068. },
  1069. local: { printer: [], process: [], filament: [] },
  1070. standard: { printer: [], process: [], filament: [] },
  1071. cloud_status: 'ok',
  1072. orca_cloud: { printer: [], process: [], filament: [] },
  1073. orca_cloud_status: 'ok',
  1074. });
  1075. renderWithTracker({
  1076. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  1077. onClose: vi.fn(),
  1078. });
  1079. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1080. // Both filament rows render — 1 printer + 1 process + 1 bed-type +
  1081. // 2 filament (#1337) = 5. bed-type sits at index 2, filament slots
  1082. // follow at 3 and 4.
  1083. const selects = presetSelects();
  1084. expect(selects).toHaveLength(5);
  1085. // Slot 1 (used) is editable, slot 2 (not used) is disabled.
  1086. expect(selects[3].disabled).toBe(false);
  1087. expect(selects[4].disabled).toBe(true);
  1088. // The disabled row's label calls out why it's disabled.
  1089. expect(screen.getByText(/not used by this plate/i)).toBeDefined();
  1090. });
  1091. it('still sends both filaments to the backend even when one slot is disabled', async () => {
  1092. // The auto-pick scoring fills the disabled slot from project
  1093. // metadata — the slicer CLI requires a profile for every project
  1094. // slot, otherwise it silently fills the gap. The disabled UI is
  1095. // purely cosmetic; the wire format must include the full list.
  1096. mockApi.getLibraryFilePlates.mockResolvedValue({
  1097. file_id: 100,
  1098. filename: 'Helmet.3mf',
  1099. is_multi_plate: false,
  1100. plates: [
  1101. {
  1102. index: 1,
  1103. name: 'Plate 1',
  1104. objects: ['Helmet'],
  1105. has_thumbnail: false,
  1106. thumbnail_url: null,
  1107. print_time_seconds: 1200,
  1108. filament_used_grams: 80,
  1109. filaments: [],
  1110. },
  1111. ],
  1112. });
  1113. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1114. file_id: 100,
  1115. filename: 'Helmet.3mf',
  1116. plate_id: 1,
  1117. filaments: [
  1118. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  1119. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  1120. ],
  1121. });
  1122. mockApi.getSlicerPresets.mockResolvedValue({
  1123. cloud: {
  1124. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1125. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1126. filament: [
  1127. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  1128. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  1129. ],
  1130. },
  1131. local: { printer: [], process: [], filament: [] },
  1132. standard: { printer: [], process: [], filament: [] },
  1133. cloud_status: 'ok',
  1134. orca_cloud: { printer: [], process: [], filament: [] },
  1135. orca_cloud_status: 'ok',
  1136. });
  1137. mockApi.sliceLibraryFile.mockResolvedValue({
  1138. job_id: 50,
  1139. status: 'pending',
  1140. status_url: '/api/v1/slice-jobs/50',
  1141. });
  1142. renderWithTracker({
  1143. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  1144. onClose: vi.fn(),
  1145. });
  1146. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1147. const user = userEvent.setup();
  1148. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1149. await waitFor(() => {
  1150. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1151. // Both slots populated: slot 1 with the user's white pick, slot
  1152. // 2 auto-picked with grey from the colour-match scoring.
  1153. expect(body.filament_presets).toHaveLength(2);
  1154. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  1155. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-GREY' });
  1156. });
  1157. });
  1158. // #2712: the filament list is positional the whole way down — index 0 is
  1159. // slot 1, and the backend forwards it as filament_1.json..filament_N.json.
  1160. // A MakerWorld source that ships slice_info and paints with slot 4 alone
  1161. // used to yield a one-row list, so the user's only pick was bound to slot 1
  1162. // and slot 4 sliced with the source's embedded default: picking PETG gave a
  1163. // PLA print. The modal now asks for every project slot so the positions
  1164. // line up.
  1165. it('requests every project slot, not just the ones the plate prints with', async () => {
  1166. renderWithTracker({
  1167. source: { kind: 'libraryFile', id: 100, filename: 'Tunnel.3mf' },
  1168. onClose: vi.fn(),
  1169. });
  1170. await waitFor(() => expect(mockApi.getLibraryFileFilamentRequirements).toHaveBeenCalled());
  1171. const [, plateArg, , fullSlots] = mockApi.getLibraryFileFilamentRequirements.mock.calls[0];
  1172. expect(plateArg).toBe(1);
  1173. expect(fullSlots).toBe(true);
  1174. });
  1175. it('sends the pick for a high-numbered slot at its own index', async () => {
  1176. // Four project slots, only slot 4 printed. The pick must arrive as the
  1177. // FOURTH entry; anywhere else and the slicer binds it to the wrong slot.
  1178. mockApi.getLibraryFilePlates.mockResolvedValue({
  1179. file_id: 100,
  1180. filename: 'Tunnel.3mf',
  1181. is_multi_plate: false,
  1182. plates: [
  1183. {
  1184. index: 1,
  1185. name: 'Plate 1',
  1186. objects: ['Tunnel'],
  1187. has_thumbnail: false,
  1188. thumbnail_url: null,
  1189. print_time_seconds: 1200,
  1190. filament_used_grams: 105,
  1191. filaments: [],
  1192. },
  1193. ],
  1194. });
  1195. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1196. file_id: 100,
  1197. filename: 'Tunnel.3mf',
  1198. plate_id: 1,
  1199. filaments: [
  1200. { slot_id: 1, type: 'PLA', color: '#38CC0A', used_grams: 0, used_meters: 0, used_in_plate: false },
  1201. { slot_id: 2, type: 'PLA', color: '#161616', used_grams: 0, used_meters: 0, used_in_plate: false },
  1202. { slot_id: 3, type: 'PLA', color: '#898989', used_grams: 0, used_meters: 0, used_in_plate: false },
  1203. { slot_id: 4, type: 'PLA', color: '#898989', used_grams: 105, used_meters: 35, used_in_plate: true },
  1204. ],
  1205. });
  1206. mockApi.getSlicerPresets.mockResolvedValue({
  1207. cloud: {
  1208. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1209. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1210. filament: [
  1211. { id: 'F-PLA', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#898989' },
  1212. { id: 'F-PETG', name: 'Cloud PETG', source: 'cloud', filament_type: 'PETG', filament_colour: '#00FF00' },
  1213. ],
  1214. },
  1215. local: { printer: [], process: [], filament: [] },
  1216. standard: { printer: [], process: [], filament: [] },
  1217. cloud_status: 'ok',
  1218. orca_cloud: { printer: [], process: [], filament: [] },
  1219. orca_cloud_status: 'ok',
  1220. });
  1221. mockApi.sliceLibraryFile.mockResolvedValue({
  1222. job_id: 51,
  1223. status: 'pending',
  1224. status_url: '/api/v1/slice-jobs/51',
  1225. });
  1226. renderWithTracker({
  1227. source: { kind: 'libraryFile', id: 100, filename: 'Tunnel.3mf' },
  1228. onClose: vi.fn(),
  1229. });
  1230. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1231. // 1 printer + 1 process + 1 bed-type + 4 filament rows.
  1232. const selects = presetSelects();
  1233. expect(selects).toHaveLength(7);
  1234. // Only slot 4 is selectable — the other three are the padding.
  1235. expect([selects[3].disabled, selects[4].disabled, selects[5].disabled]).toEqual([true, true, true]);
  1236. expect(selects[6].disabled).toBe(false);
  1237. const user = userEvent.setup();
  1238. await user.selectOptions(selects[6], 'cloud:F-PETG');
  1239. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1240. await waitFor(() => {
  1241. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1242. expect(body.filament_presets).toHaveLength(4);
  1243. expect(body.filament_presets[3]).toEqual({ source: 'cloud', id: 'F-PETG' });
  1244. });
  1245. });
  1246. // ------------------------------------------------------------------
  1247. // Slicer Pipelines (#1425) — Apply / Save integration in SliceModal
  1248. // ------------------------------------------------------------------
  1249. it('Apply pipeline dropdown is disabled and shows empty hint when no pipelines exist', async () => {
  1250. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1251. renderWithTracker({
  1252. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1253. onClose: vi.fn(),
  1254. });
  1255. await waitFor(() => {
  1256. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  1257. expect(select.disabled).toBe(true);
  1258. expect(select.querySelector('option')?.textContent).toMatch(/No saved pipelines/i);
  1259. });
  1260. });
  1261. it('applies a saved pipeline to printer, process, and bed_type slots on selection', async () => {
  1262. mockApi.listSlicerPipelines.mockResolvedValue({
  1263. pipelines: [
  1264. {
  1265. id: 7,
  1266. name: 'Production Batch',
  1267. description: null,
  1268. printer_preset: { source: 'local', id: '1' },
  1269. process_preset: { source: 'local', id: '2' },
  1270. filament_presets: [{ source: 'local', id: '3' }],
  1271. bed_type: 'Textured PEI Plate',
  1272. target_kind: 'printer_class',
  1273. target_printer_id: null,
  1274. target_model_class: null,
  1275. fanout_strategy: 'max_parallel',
  1276. created_by: null,
  1277. created_at: '2026-06-27T00:00:00Z',
  1278. updated_at: '2026-06-27T00:00:00Z',
  1279. },
  1280. ],
  1281. });
  1282. renderWithTracker({
  1283. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1284. onClose: vi.fn(),
  1285. });
  1286. // Wait for presets + pipelines listing to populate the modal.
  1287. await waitFor(() => {
  1288. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  1289. expect(select.disabled).toBe(false);
  1290. expect(within(select).getByText('Production Batch')).toBeDefined();
  1291. });
  1292. const user = userEvent.setup();
  1293. await user.selectOptions(screen.getByLabelText(/Apply pipeline/i), '7');
  1294. // After applying, submitting the slice request should carry the
  1295. // pipeline's preset refs end-to-end.
  1296. mockApi.sliceLibraryFile.mockResolvedValue({
  1297. job_id: 42,
  1298. status: 'queued',
  1299. status_url: '/api/v1/slice-jobs/42',
  1300. });
  1301. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1302. await waitFor(() => {
  1303. expect(mockApi.sliceLibraryFile).toHaveBeenCalled();
  1304. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1305. expect(body.printer_preset).toEqual({ source: 'local', id: '1' });
  1306. expect(body.process_preset).toEqual({ source: 'local', id: '2' });
  1307. expect(body.filament_presets[0]).toEqual({ source: 'local', id: '3' });
  1308. expect(body.bed_type).toBe('Textured PEI Plate');
  1309. });
  1310. });
  1311. it('saves the current four-slot selection as a new pipeline when the user clicks Save as pipeline', async () => {
  1312. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1313. mockApi.createSlicerPipeline.mockResolvedValue({
  1314. id: 99,
  1315. name: 'My Default',
  1316. description: null,
  1317. printer_preset: { source: 'local', id: '1' },
  1318. process_preset: { source: 'local', id: '2' },
  1319. filament_presets: [{ source: 'local', id: '3' }],
  1320. bed_type: null,
  1321. target_kind: 'printer_class',
  1322. target_printer_id: null,
  1323. target_model_class: null,
  1324. fanout_strategy: 'max_parallel',
  1325. created_by: null,
  1326. created_at: '2026-06-27T00:00:00Z',
  1327. updated_at: '2026-06-27T00:00:00Z',
  1328. });
  1329. renderWithTracker({
  1330. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1331. onClose: vi.fn(),
  1332. });
  1333. // Wait for auto-pick to populate all four slots from the fullThreeTier
  1334. // listing — then Save as pipeline becomes enabled.
  1335. const user = userEvent.setup();
  1336. let saveBtn: HTMLButtonElement;
  1337. await waitFor(() => {
  1338. saveBtn = screen.getByRole('button', { name: /^Save as pipeline$/ }) as HTMLButtonElement;
  1339. expect(saveBtn.disabled).toBe(false);
  1340. });
  1341. await user.click(saveBtn!);
  1342. const nameInput = screen.getByLabelText(/New pipeline name/i);
  1343. await user.type(nameInput, 'My Default');
  1344. await user.click(screen.getByRole('button', { name: /^Save$/ }));
  1345. await waitFor(() => {
  1346. expect(mockApi.createSlicerPipeline).toHaveBeenCalledTimes(1);
  1347. const body = mockApi.createSlicerPipeline.mock.calls[0][0];
  1348. expect(body.name).toBe('My Default');
  1349. // The four slots come from the auto-picked unified-presets listing —
  1350. // local tier wins per SLICE_MODAL_TIER_ORDER.
  1351. expect(body.printer_preset.source).toBe('local');
  1352. expect(body.process_preset.source).toBe('local');
  1353. expect(body.filament_presets[0].source).toBe('local');
  1354. });
  1355. });
  1356. });
  1357. // Pure-function tests for the filament slot picker. Pinned as a separate
  1358. // describe so the contract is visible without needing the modal mount.
  1359. /**
  1360. * The slice dialog switches to a two-column layout once there is room for it,
  1361. * and the process-settings panel then owns the right-hand column. The global
  1362. * test setup pins matchMedia to `matches: false`, so every other test in this
  1363. * file exercises the narrow single-stack path; these override it.
  1364. */
  1365. describe('SliceModal — process settings in "slice as designed" mode', () => {
  1366. beforeEach(() => {
  1367. vi.clearAllMocks();
  1368. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  1369. mockApi.getSlicerPresetValues.mockResolvedValue({ resolved: true, values: {}, reason: 'ok' });
  1370. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1371. mockApi.getSlicerPrinterModels.mockResolvedValue({});
  1372. mockApi.getLibraryFilePlates.mockResolvedValue({
  1373. file_id: 100,
  1374. filename: 'Designed.3mf',
  1375. plates: [],
  1376. is_multi_plate: false,
  1377. embedded_printer: 'Bambu Lab X1 Carbon 0.4 nozzle',
  1378. embedded_process: '0.20mm Standard',
  1379. });
  1380. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1381. file_id: 100, filename: 'Designed.3mf', plate_id: 1, filaments: [],
  1382. });
  1383. });
  1384. it('disables the panel rather than removing it', async () => {
  1385. const user = userEvent.setup();
  1386. renderWithTracker({
  1387. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  1388. onClose: vi.fn(),
  1389. });
  1390. const toggle = (await screen.findByLabelText(/Use the file's built-in settings/)) as HTMLInputElement;
  1391. const header = await screen.findByRole('button', { name: /Process settings/ });
  1392. await user.click(header);
  1393. const search = await screen.findByPlaceholderText('Search settings');
  1394. expect(search).toBeEnabled();
  1395. await user.click(toggle);
  1396. // Still on screen — hiding it made the dialog look like it had lost a
  1397. // feature — but nothing in it can be operated, because nothing in it is
  1398. // sent on this path.
  1399. expect(screen.getByPlaceholderText('Search settings')).toBeDisabled();
  1400. expect(screen.getByRole('button', { name: 'Expert' })).toBeDisabled();
  1401. expect(screen.getByText(/Not used while/)).toBeInTheDocument();
  1402. expect(screen.getByText('Inactive')).toBeInTheDocument();
  1403. });
  1404. it('sends no process overrides once the file drives the slice', async () => {
  1405. mockApi.sliceLibraryFile.mockResolvedValue({ job_id: 42, status: 'pending', status_url: '/x' });
  1406. const user = userEvent.setup();
  1407. renderWithTracker({
  1408. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  1409. onClose: vi.fn(),
  1410. });
  1411. // Edit something, then hand the slice over to the file's own settings.
  1412. await user.click(await screen.findByRole('button', { name: /Process settings/ }));
  1413. const input = await screen.findByLabelText(/^Layer height/);
  1414. await user.clear(input);
  1415. await user.type(input, '0.16');
  1416. await user.click(screen.getByLabelText(/Use the file's built-in settings/));
  1417. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1418. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  1419. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as Record<string, unknown>;
  1420. expect(payload.use_embedded_settings).toBe(true);
  1421. expect(payload).not.toHaveProperty('process_overrides');
  1422. });
  1423. });
  1424. describe('SliceModal — process settings layout', () => {
  1425. const setViewport = (wide: boolean) => {
  1426. Object.defineProperty(window, 'matchMedia', {
  1427. writable: true,
  1428. value: (query: string) => ({
  1429. matches: wide,
  1430. media: query,
  1431. onchange: null,
  1432. addListener: () => {},
  1433. removeListener: () => {},
  1434. addEventListener: () => {},
  1435. removeEventListener: () => {},
  1436. dispatchEvent: () => true,
  1437. }),
  1438. });
  1439. };
  1440. beforeEach(() => {
  1441. vi.clearAllMocks();
  1442. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  1443. mockApi.getSlicerPresetValues.mockResolvedValue({ resolved: true, values: {}, reason: 'ok' });
  1444. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1445. mockApi.getLibraryFilePlates.mockResolvedValue({
  1446. file_id: 100,
  1447. filename: 'Cube.stl',
  1448. plates: [],
  1449. is_multi_plate: false,
  1450. });
  1451. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1452. file_id: 100,
  1453. filename: 'Cube.stl',
  1454. plate_id: 1,
  1455. filaments: [],
  1456. });
  1457. });
  1458. afterEach(() => setViewport(false));
  1459. it('keeps the panel collapsed behind a disclosure in the narrow layout', async () => {
  1460. setViewport(false);
  1461. renderWithTracker({ source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' }, onClose: vi.fn() });
  1462. const header = await screen.findByRole('button', { name: /Process settings/ });
  1463. expect(header).toBeEnabled();
  1464. expect(screen.queryByPlaceholderText('Search settings')).not.toBeInTheDocument();
  1465. await userEvent.setup().click(header);
  1466. await waitFor(() => expect(screen.getByPlaceholderText('Search settings')).toBeInTheDocument());
  1467. });
  1468. it('opens the panel without a click once it has a column of its own', async () => {
  1469. setViewport(true);
  1470. renderWithTracker({ source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' }, onClose: vi.fn() });
  1471. // No disclosure to operate: the panel is the column, so its header is
  1472. // inert rather than offering to collapse something that has room.
  1473. await waitFor(() => expect(screen.getByPlaceholderText('Search settings')).toBeInTheDocument());
  1474. expect(screen.getByRole('button', { name: /Process settings/ })).toBeDisabled();
  1475. });
  1476. });
  1477. /**
  1478. * Process and filament lists hold back presets that resolve to a *different*
  1479. * printer, behind a per-slot "Show all". Two things must never be hidden: a
  1480. * preset whose compatibility is merely unknown, and whatever is currently
  1481. * selected.
  1482. */
  1483. describe('SliceModal — presets filtered by the selected printer', () => {
  1484. const presets: UnifiedPresetsResponse = {
  1485. cloud: { printer: [], process: [], filament: [] },
  1486. orca_cloud: { printer: [], process: [], filament: [] },
  1487. local: { printer: [], process: [], filament: [] },
  1488. standard: {
  1489. printer: [
  1490. { id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' },
  1491. ],
  1492. process: [
  1493. { id: 'p-x1c', name: '0.20mm Standard @BBL X1C', source: 'standard' },
  1494. { id: 'p-h2d', name: '0.20mm Standard @BBL H2D', source: 'standard' },
  1495. { id: 'p-a1m', name: '0.20mm Standard @BBL A1M', source: 'standard' },
  1496. // No printer tag at all — compatibility is unknown, never hidden.
  1497. { id: 'p-custom', name: 'My own profile', source: 'standard' },
  1498. ],
  1499. filament: [{ id: 'f-x1c', name: 'Bambu PLA Basic @BBL X1C', source: 'standard' }],
  1500. },
  1501. cloud_status: 'ok',
  1502. orca_cloud_status: 'ok',
  1503. } as UnifiedPresetsResponse;
  1504. const processOptionNames = () =>
  1505. Array.from(presetSelects()[1].options).map((o) => o.textContent);
  1506. beforeEach(() => {
  1507. vi.clearAllMocks();
  1508. mockApi.getSlicerPresets.mockResolvedValue(presets);
  1509. mockApi.getSlicerPrinterModels.mockResolvedValue({ 'Bambu Lab X1 Carbon': 'X1C' });
  1510. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1511. mockApi.getLibraryFilePlates.mockResolvedValue({
  1512. file_id: 100, filename: 'Cube.stl', plates: [], is_multi_plate: false,
  1513. });
  1514. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1515. file_id: 100, filename: 'Cube.stl', plate_id: 1, filaments: [],
  1516. });
  1517. });
  1518. const open = async () => {
  1519. renderWithTracker({ source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' }, onClose: vi.fn() });
  1520. await waitFor(() => expect(presetSelects().length).toBeGreaterThan(1));
  1521. };
  1522. it('leaves out presets belonging to another printer', async () => {
  1523. await open();
  1524. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL X1C'));
  1525. expect(processOptionNames()).not.toContain('0.20mm Standard @BBL H2D');
  1526. expect(processOptionNames()).not.toContain('0.20mm Standard @BBL A1M');
  1527. });
  1528. it('keeps a preset whose compatibility cannot be determined', async () => {
  1529. await open();
  1530. // An untagged preset carries no evidence either way; hiding it would make
  1531. // a user's own imported profiles vanish.
  1532. await waitFor(() => expect(processOptionNames()).toContain('My own profile'));
  1533. });
  1534. it('says how many it held back and reveals them on request', async () => {
  1535. const user = userEvent.setup();
  1536. await open();
  1537. const hidden = await screen.findByText('2 hidden');
  1538. expect(hidden).toBeInTheDocument();
  1539. await user.click(within(hidden.parentElement as HTMLElement).getByRole('button', { name: 'Show all' }));
  1540. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  1541. expect(processOptionNames()).toContain('0.20mm Standard @BBL A1M');
  1542. });
  1543. it('collapses the list again on Show fewer', async () => {
  1544. const user = userEvent.setup();
  1545. await open();
  1546. await user.click((await screen.findAllByRole('button', { name: 'Show all' }))[0]);
  1547. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  1548. await user.click(screen.getAllByRole('button', { name: 'Show fewer' })[0]);
  1549. await waitFor(() => expect(processOptionNames()).not.toContain('0.20mm Standard @BBL H2D'));
  1550. });
  1551. it('never hides the preset that is currently selected', async () => {
  1552. const user = userEvent.setup();
  1553. await open();
  1554. // Reach a cross-printer preset, pick it, then collapse the list again.
  1555. await user.click((await screen.findAllByRole('button', { name: 'Show all' }))[0]);
  1556. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  1557. await user.selectOptions(presetSelects()[1], 'standard:p-h2d');
  1558. await user.click(screen.getAllByRole('button', { name: 'Show fewer' })[0]);
  1559. // Dropping it from the options would blank the select and silently discard
  1560. // a deliberate cross-printer choice.
  1561. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  1562. expect(presetSelects()[1].value).toBe('standard:p-h2d');
  1563. // The one still-hidden preset is counted; the selected one is not.
  1564. expect(screen.getByText('1 hidden')).toBeInTheDocument();
  1565. });
  1566. });
  1567. describe('pickFilamentForSlot — printer-compat contract (#1851)', () => {
  1568. // Index that recognises @BBL H2C / @BBL A1 tokens via the canonical
  1569. // PRINTER_MODEL_MAP. Real production data comes through
  1570. // ``api.getSlicerPrinterModels`` — the H2C / A1 fragments are the ones
  1571. // the production registry ships.
  1572. const index = buildCompatibilityIndex({
  1573. 'Bambu Lab A1': 'A1',
  1574. 'Bambu Lab H2C': 'H2C',
  1575. });
  1576. it('prefers a printer-compatible preset over a printer-mismatched one even with better colour match', () => {
  1577. // The OP scenario for #1851: a Bambu Lab A1 is selected; the unused-slot
  1578. // requirement carries the original H2C plate's PLA colour. With the
  1579. // legacy soft-penalty scoring an H2C-bound preset whose colour matches
  1580. // exactly could still rise above the A1-compatible PLA Basic whose
  1581. // colour doesn't, and then the unused-slot substitution propagated the
  1582. // H2C-bound preset across every unused slot — the CLI rejected with
  1583. // ``filament preset Generic PLA @BBL H2C (slot 1) is not compatible
  1584. // with printer Bambu Lab A1 0.4 nozzle``. The hard-skip contract makes
  1585. // sure a mismatched preset is never chosen while any compatible
  1586. // alternative exists, irrespective of metadata-score arithmetic.
  1587. const presets = makeUnified({
  1588. standard: {
  1589. printer: [],
  1590. process: [],
  1591. filament: [
  1592. {
  1593. id: 'Generic PLA @BBL H2C',
  1594. name: 'Generic PLA @BBL H2C',
  1595. source: 'standard',
  1596. filament_type: 'PLA',
  1597. filament_colour: '#FF0000',
  1598. },
  1599. {
  1600. id: 'Bambu PLA Basic @BBL A1',
  1601. name: 'Bambu PLA Basic @BBL A1',
  1602. source: 'standard',
  1603. filament_type: 'PLA',
  1604. filament_colour: '#FFFFFF',
  1605. },
  1606. ],
  1607. },
  1608. });
  1609. const pick = pickFilamentForSlot(
  1610. presets,
  1611. { type: 'PLA', color: '#FF0000' },
  1612. 'Bambu Lab A1 0.4 nozzle',
  1613. index,
  1614. );
  1615. expect(pick).toEqual({ source: 'standard', id: 'Bambu PLA Basic @BBL A1' });
  1616. });
  1617. it('falls back to a mismatched preset when no compatible alternative exists', () => {
  1618. // Graceful degrade: when every available preset is printer-mismatched,
  1619. // returning ``null`` would block the slice entirely. The picker keeps
  1620. // its old behaviour of returning the best-scoring mismatch so the user
  1621. // sees a populated dropdown they can correct, not an empty one.
  1622. const presets = makeUnified({
  1623. standard: {
  1624. printer: [],
  1625. process: [],
  1626. filament: [
  1627. {
  1628. id: 'Generic PLA @BBL H2C',
  1629. name: 'Generic PLA @BBL H2C',
  1630. source: 'standard',
  1631. filament_type: 'PLA',
  1632. filament_colour: '#FF0000',
  1633. },
  1634. ],
  1635. },
  1636. });
  1637. const pick = pickFilamentForSlot(
  1638. presets,
  1639. { type: 'PLA', color: '#FF0000' },
  1640. 'Bambu Lab A1 0.4 nozzle',
  1641. index,
  1642. );
  1643. expect(pick).toEqual({ source: 'standard', id: 'Generic PLA @BBL H2C' });
  1644. });
  1645. it('treats a no-printer-context call as no-mismatch (every preset eligible)', () => {
  1646. // ``printerName === null`` happens transiently on first render before the
  1647. // printer pre-pick effect has run. ``presetCompatibility`` returns
  1648. // ``unknown`` for every preset in that case, so the picker should just
  1649. // pick by metadata score with no compatibility filter active.
  1650. const presets = makeUnified({
  1651. standard: {
  1652. printer: [],
  1653. process: [],
  1654. filament: [
  1655. {
  1656. id: 'Generic PLA @BBL H2C',
  1657. name: 'Generic PLA @BBL H2C',
  1658. source: 'standard',
  1659. filament_type: 'PLA',
  1660. filament_colour: '#FF0000',
  1661. },
  1662. ],
  1663. },
  1664. });
  1665. const pick = pickFilamentForSlot(
  1666. presets,
  1667. { type: 'PLA', color: '#FF0000' },
  1668. null,
  1669. index,
  1670. );
  1671. expect(pick).toEqual({ source: 'standard', id: 'Generic PLA @BBL H2C' });
  1672. });
  1673. });
  1674. describe('pickFilamentForSlot — long-form printer tag (#2628)', () => {
  1675. const index = buildCompatibilityIndex({
  1676. 'Bambu Lab A1': 'A1',
  1677. 'Bambu Lab H2D': 'H2D',
  1678. });
  1679. it('never auto-picks a user-saved preset scoped to another printer', () => {
  1680. // michaelklos's registry: a cloud-tier user preset carrying the full
  1681. // "@Bambu Lab H2D 0.4 nozzle" tag outscores the A1 preset on tier bonus
  1682. // alone. Until the matcher learned the long form it classified 'unknown'
  1683. // — indistinguishable from compatible — so it won the slot, landed in a
  1684. // dropdown the modal disables (slot not used by the plate), and the CLI
  1685. // rejected the whole slice.
  1686. const presets = makeUnified({
  1687. cloud: {
  1688. printer: [],
  1689. process: [],
  1690. filament: [
  1691. {
  1692. id: 'sunlu-tpu-h2d',
  1693. name: 'SUNLU TPU 95A @Bambu Lab H2D 0.4 nozzle',
  1694. source: 'cloud',
  1695. filament_type: 'PLA',
  1696. filament_colour: '#FF0000',
  1697. },
  1698. ],
  1699. },
  1700. standard: {
  1701. printer: [],
  1702. process: [],
  1703. filament: [
  1704. {
  1705. id: 'Bambu PLA Basic @BBL A1',
  1706. name: 'Bambu PLA Basic @BBL A1',
  1707. source: 'standard',
  1708. filament_type: 'PLA',
  1709. filament_colour: '#FFFFFF',
  1710. },
  1711. ],
  1712. },
  1713. });
  1714. const pick = pickFilamentForSlot(
  1715. presets,
  1716. { type: 'PLA', color: '#FF0000' },
  1717. 'Bambu Lab A1 0.4 nozzle',
  1718. index,
  1719. );
  1720. expect(pick).toEqual({ source: 'standard', id: 'Bambu PLA Basic @BBL A1' });
  1721. });
  1722. it('still picks a long-form preset for its own printer', () => {
  1723. const presets = makeUnified({
  1724. cloud: {
  1725. printer: [],
  1726. process: [],
  1727. filament: [
  1728. {
  1729. id: 'sunlu-tpu-h2d',
  1730. name: 'SUNLU TPU 95A @Bambu Lab H2D 0.4 nozzle',
  1731. source: 'cloud',
  1732. filament_type: 'TPU',
  1733. filament_colour: '#FF0000',
  1734. },
  1735. ],
  1736. },
  1737. });
  1738. const pick = pickFilamentForSlot(
  1739. presets,
  1740. { type: 'TPU', color: '#FF0000' },
  1741. 'Bambu Lab H2D 0.4 nozzle',
  1742. index,
  1743. );
  1744. expect(pick).toEqual({ source: 'cloud', id: 'sunlu-tpu-h2d' });
  1745. });
  1746. });