SliceModal.test.tsx 77 KB

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