SliceModal.test.tsx 36 KB

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