SliceModal.test.tsx 49 KB

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