SliceModal.test.tsx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. cloud: { printer: [], process: [], filament: [] },
  44. local: { printer: [], process: [], filament: [] },
  45. standard: { printer: [], process: [], filament: [] },
  46. cloud_status: 'ok',
  47. ...overrides,
  48. };
  49. }
  50. const fullThreeTier: UnifiedPresetsResponse = makeUnified({
  51. cloud: {
  52. printer: [{ id: 'PFUcloud-printer', name: 'My Custom X1C', source: 'cloud' }],
  53. process: [{ id: 'PFUcloud-process', name: 'My 0.16mm Tweaked', source: 'cloud' }],
  54. filament: [{ id: 'PFUcloud-filament', name: 'My PLA Black', source: 'cloud' }],
  55. },
  56. local: {
  57. printer: [{ id: '1', name: 'Imported X1C 0.4', source: 'local' }],
  58. process: [{ id: '2', name: 'Imported 0.20mm', source: 'local' }],
  59. filament: [{ id: '3', name: 'Imported PLA Basic', source: 'local' }],
  60. },
  61. standard: {
  62. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  63. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  64. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  65. },
  66. });
  67. function renderWithTracker(props: Parameters<typeof SliceModal>[0]) {
  68. return render(
  69. <SliceJobTrackerProvider>
  70. <SliceModal {...props} />
  71. </SliceJobTrackerProvider>,
  72. );
  73. }
  74. describe('SliceModal', () => {
  75. beforeEach(() => {
  76. vi.clearAllMocks();
  77. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  78. mockApi.getSliceJob.mockResolvedValue({
  79. job_id: 42,
  80. status: 'running',
  81. kind: 'library_file',
  82. source_id: 100,
  83. source_name: 'Cube.stl',
  84. created_at: new Date().toISOString(),
  85. started_at: null,
  86. completed_at: null,
  87. });
  88. // Default: single-plate (or non-3MF). Multi-plate tests override this.
  89. mockApi.getLibraryFilePlates.mockResolvedValue({
  90. file_id: 100,
  91. filename: 'Cube.stl',
  92. plates: [],
  93. is_multi_plate: false,
  94. });
  95. mockApi.getArchivePlates.mockResolvedValue({
  96. archive_id: 100,
  97. filename: 'Cube.3mf',
  98. plates: [],
  99. is_multi_plate: false,
  100. });
  101. // Default: no per-plate filament metadata available (mirrors STL or
  102. // unsliced source). Multi-color tests override this.
  103. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  104. file_id: 100,
  105. filename: 'Cube.stl',
  106. plate_id: 1,
  107. filaments: [],
  108. });
  109. mockApi.getArchiveFilamentRequirements.mockResolvedValue({
  110. archive_id: 100,
  111. filename: 'Cube.3mf',
  112. plate_id: 1,
  113. filaments: [],
  114. });
  115. });
  116. it('auto-selects the highest-priority tier per slot on first load', async () => {
  117. renderWithTracker({
  118. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  119. onClose: vi.fn(),
  120. });
  121. // SliceModal-specific tier priority: imported (local) wins over cloud
  122. // and standard so the user's curated picks come first.
  123. await waitFor(() => {
  124. expect(screen.getByText('My Custom X1C')).toBeDefined();
  125. });
  126. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  127. expect(selects).toHaveLength(3);
  128. expect(selects[0].value).toBe('local:1');
  129. expect(selects[1].value).toBe('local:2');
  130. expect(selects[2].value).toBe('local:3');
  131. // Slice button is enabled because all three slots auto-defaulted.
  132. const sliceBtn = screen.getByRole('button', { name: /^Slice$/ });
  133. expect((sliceBtn as HTMLButtonElement).disabled).toBe(false);
  134. });
  135. it('renders Imported / Cloud / Standard sections via <optgroup>', async () => {
  136. renderWithTracker({
  137. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  138. onClose: vi.fn(),
  139. });
  140. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  141. const printerSelect = screen.getAllByRole('combobox')[0];
  142. const groups = printerSelect.querySelectorAll('optgroup');
  143. expect(Array.from(groups).map((g) => g.label)).toEqual([
  144. 'Imported',
  145. 'Cloud',
  146. 'Standard',
  147. ]);
  148. // Each entry sits inside its own tier's group — pin the assignment so
  149. // a future render-shape change can't quietly mix them. Order matches
  150. // SLICE_MODAL_TIER_ORDER (local → cloud → standard).
  151. const localGroup = groups[0];
  152. expect(within(localGroup as HTMLElement).getByText('Imported X1C 0.4')).toBeDefined();
  153. const cloudGroup = groups[1];
  154. expect(within(cloudGroup as HTMLElement).getByText('My Custom X1C')).toBeDefined();
  155. const standardGroup = groups[2];
  156. expect(within(standardGroup as HTMLElement).getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined();
  157. });
  158. it('falls back to local when cloud is empty (auto-pick respects priority)', async () => {
  159. mockApi.getSlicerPresets.mockResolvedValue(
  160. makeUnified({
  161. local: fullThreeTier.local,
  162. standard: fullThreeTier.standard,
  163. }),
  164. );
  165. renderWithTracker({
  166. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  167. onClose: vi.fn(),
  168. });
  169. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  170. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  171. expect(selects[0].value).toBe('local:1');
  172. });
  173. it('falls back to standard when both cloud and local are empty', async () => {
  174. mockApi.getSlicerPresets.mockResolvedValue(
  175. makeUnified({ standard: fullThreeTier.standard }),
  176. );
  177. renderWithTracker({
  178. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  179. onClose: vi.fn(),
  180. });
  181. await waitFor(() => expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined());
  182. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  183. expect(selects[0].value).toBe('standard:Bambu Lab X1 Carbon 0.4 nozzle');
  184. });
  185. it('sends source-aware refs (not legacy bare ints) on submit', async () => {
  186. const onClose = vi.fn();
  187. mockApi.sliceLibraryFile.mockResolvedValue({
  188. job_id: 42,
  189. status: 'pending',
  190. status_url: '/api/v1/slice-jobs/42',
  191. });
  192. renderWithTracker({
  193. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  194. onClose,
  195. });
  196. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  197. const user = userEvent.setup();
  198. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  199. await waitFor(() => {
  200. // SliceModal-specific tier priority puts imported (local) above cloud,
  201. // so the auto-pick lands on the local entries even when a cloud entry
  202. // with the same slot is also available in the listing.
  203. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(100, {
  204. printer_preset: { source: 'local', id: '1' },
  205. process_preset: { source: 'local', id: '2' },
  206. filament_preset: { source: 'local', id: '3' },
  207. filament_presets: [{ source: 'local', id: '3' }],
  208. });
  209. });
  210. await waitFor(() => expect(onClose).toHaveBeenCalled());
  211. });
  212. it('lets the user override the default and pick a Standard preset', async () => {
  213. const onClose = vi.fn();
  214. mockApi.sliceLibraryFile.mockResolvedValue({
  215. job_id: 42,
  216. status: 'pending',
  217. status_url: '/api/v1/slice-jobs/42',
  218. });
  219. renderWithTracker({
  220. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  221. onClose,
  222. });
  223. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  224. const user = userEvent.setup();
  225. const selects = screen.getAllByRole('combobox');
  226. await user.selectOptions(selects[0], 'standard:Bambu Lab X1 Carbon 0.4 nozzle');
  227. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  228. await waitFor(() => {
  229. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  230. 100,
  231. expect.objectContaining({
  232. printer_preset: { source: 'standard', id: 'Bambu Lab X1 Carbon 0.4 nozzle' },
  233. }),
  234. );
  235. });
  236. });
  237. it('routes archive sources to sliceArchive instead of sliceLibraryFile', async () => {
  238. const onClose = vi.fn();
  239. mockApi.sliceArchive.mockResolvedValue({
  240. job_id: 7,
  241. status: 'pending',
  242. status_url: '/api/v1/slice-jobs/7',
  243. });
  244. renderWithTracker({
  245. source: { kind: 'archive', id: 86, filename: 'orca.3mf' },
  246. onClose,
  247. });
  248. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  249. const user = userEvent.setup();
  250. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  251. await waitFor(() => {
  252. expect(mockApi.sliceArchive).toHaveBeenCalledWith(86, expect.any(Object));
  253. expect(mockApi.sliceLibraryFile).not.toHaveBeenCalled();
  254. });
  255. });
  256. it('surfaces enqueue errors inline and keeps the modal open', async () => {
  257. const onClose = vi.fn();
  258. mockApi.sliceLibraryFile.mockRejectedValue(new Error('Server says no'));
  259. renderWithTracker({
  260. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  261. onClose,
  262. });
  263. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  264. const user = userEvent.setup();
  265. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  266. await waitFor(() => {
  267. expect(screen.getByRole('alert')).toHaveTextContent('Server says no');
  268. });
  269. expect(onClose).not.toHaveBeenCalled();
  270. });
  271. it('shows a friendly notice when getSlicerPresets fails', async () => {
  272. mockApi.getSlicerPresets.mockRejectedValue(new Error('500'));
  273. renderWithTracker({
  274. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  275. onClose: vi.fn(),
  276. });
  277. await waitFor(() => {
  278. expect(screen.getByRole('alert')).toHaveTextContent(/Failed to load presets/i);
  279. });
  280. });
  281. it('renders a "sign in" banner when cloud_status is not_authenticated', async () => {
  282. mockApi.getSlicerPresets.mockResolvedValue(
  283. makeUnified({
  284. cloud_status: 'not_authenticated',
  285. local: fullThreeTier.local,
  286. standard: fullThreeTier.standard,
  287. }),
  288. );
  289. renderWithTracker({
  290. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  291. onClose: vi.fn(),
  292. });
  293. await waitFor(() => {
  294. expect(screen.getByRole('status')).toHaveTextContent(/Sign in to Bambu Cloud/i);
  295. });
  296. });
  297. it('renders an "expired" banner when cloud_status is expired', async () => {
  298. mockApi.getSlicerPresets.mockResolvedValue(
  299. makeUnified({
  300. cloud_status: 'expired',
  301. local: fullThreeTier.local,
  302. }),
  303. );
  304. renderWithTracker({
  305. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  306. onClose: vi.fn(),
  307. });
  308. await waitFor(() => {
  309. expect(screen.getByRole('status')).toHaveTextContent(/expired/i);
  310. });
  311. });
  312. it('omits the banner entirely when cloud_status is ok', async () => {
  313. renderWithTracker({
  314. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  315. onClose: vi.fn(),
  316. });
  317. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  318. // No status-role banner should be rendered on the happy path.
  319. expect(screen.queryByRole('status')).toBeNull();
  320. });
  321. // ----- Multi-plate flow -----------------------------------------------
  322. function makeMultiPlateLibraryResponse() {
  323. return {
  324. file_id: 100,
  325. filename: 'Multi.3mf',
  326. is_multi_plate: true,
  327. plates: [
  328. {
  329. index: 1,
  330. name: 'Plate 1',
  331. objects: ['Cube'],
  332. object_count: 1,
  333. has_thumbnail: false,
  334. thumbnail_url: null,
  335. print_time_seconds: 600,
  336. filament_used_grams: 10,
  337. filaments: [],
  338. },
  339. {
  340. index: 2,
  341. name: 'Plate 2',
  342. objects: ['Pyramid'],
  343. object_count: 1,
  344. has_thumbnail: false,
  345. thumbnail_url: null,
  346. print_time_seconds: 800,
  347. filament_used_grams: 12,
  348. filaments: [],
  349. },
  350. ],
  351. };
  352. }
  353. it('shows the plate picker first for multi-plate library files', async () => {
  354. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  355. renderWithTracker({
  356. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  357. onClose: vi.fn(),
  358. });
  359. // Plate picker renders one button per plate — the accessible name
  360. // joins the heading ("Plate N — name") with the object summary line.
  361. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  362. expect(screen.getByRole('button', { name: /Plate 2.*Pyramid/ })).toBeDefined();
  363. // Profile dropdowns must NOT be visible yet — the user has to pick a
  364. // plate first.
  365. expect(screen.queryByRole('combobox')).toBeNull();
  366. });
  367. it('skips the plate picker for single-plate sources', async () => {
  368. mockApi.getLibraryFilePlates.mockResolvedValue({
  369. file_id: 100,
  370. filename: 'Single.3mf',
  371. is_multi_plate: false,
  372. plates: [
  373. {
  374. index: 1,
  375. name: 'Plate 1',
  376. objects: [],
  377. has_thumbnail: false,
  378. thumbnail_url: null,
  379. print_time_seconds: null,
  380. filament_used_grams: null,
  381. filaments: [],
  382. },
  383. ],
  384. });
  385. renderWithTracker({
  386. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  387. onClose: vi.fn(),
  388. });
  389. // Should jump straight to the profile dropdowns.
  390. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  391. });
  392. it('passes the picked plate to the slice request', async () => {
  393. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  394. mockApi.sliceLibraryFile.mockResolvedValue({
  395. job_id: 42,
  396. status: 'pending',
  397. status_url: '/api/v1/slice-jobs/42',
  398. });
  399. renderWithTracker({
  400. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  401. onClose: vi.fn(),
  402. });
  403. const user = userEvent.setup();
  404. // Step 1: pick Plate 2.
  405. const plate2Button = await screen.findByRole('button', { name: /Plate 2.*Pyramid/ });
  406. await user.click(plate2Button);
  407. // Step 2: profile dropdowns are now visible.
  408. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  409. // Step 3: submit and verify the plate index made it into the body.
  410. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  411. await waitFor(() => {
  412. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  413. 100,
  414. expect.objectContaining({ plate: 2 }),
  415. );
  416. });
  417. });
  418. it('routes the plate fetch through getArchivePlates for archive sources', async () => {
  419. mockApi.getArchivePlates.mockResolvedValue({
  420. ...makeMultiPlateLibraryResponse(),
  421. archive_id: 100,
  422. filename: 'Multi.3mf',
  423. });
  424. renderWithTracker({
  425. source: { kind: 'archive', id: 100, filename: 'Multi.3mf' },
  426. onClose: vi.fn(),
  427. });
  428. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  429. expect(mockApi.getArchivePlates).toHaveBeenCalledWith(100);
  430. expect(mockApi.getLibraryFilePlates).not.toHaveBeenCalled();
  431. });
  432. it('cancelling the plate picker closes the entire slice flow', async () => {
  433. const onClose = vi.fn();
  434. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  435. renderWithTracker({
  436. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  437. onClose,
  438. });
  439. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  440. const user = userEvent.setup();
  441. await user.click(screen.getByRole('button', { name: /^Close$/i }));
  442. expect(onClose).toHaveBeenCalled();
  443. });
  444. it('omits the plate field when the source is single-plate', async () => {
  445. mockApi.sliceLibraryFile.mockResolvedValue({
  446. job_id: 42,
  447. status: 'pending',
  448. status_url: '/api/v1/slice-jobs/42',
  449. });
  450. renderWithTracker({
  451. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  452. onClose: vi.fn(),
  453. });
  454. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  455. const user = userEvent.setup();
  456. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  457. await waitFor(() => {
  458. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  459. expect(body).not.toHaveProperty('plate');
  460. });
  461. });
  462. // ----- Multi-color flow ------------------------------------------------
  463. function makeMultiColorPlateResponse() {
  464. // Single-plate 3MF that uses two filament slots — mirrors the realistic
  465. // "I have a multi-color file with one plate" case. Multi-plate is a
  466. // separate axis that's already covered above.
  467. return {
  468. file_id: 100,
  469. filename: 'TwoColor.3mf',
  470. is_multi_plate: false,
  471. plates: [
  472. {
  473. index: 1,
  474. name: 'Plate 1',
  475. objects: ['Logo'],
  476. object_count: 1,
  477. has_thumbnail: false,
  478. thumbnail_url: null,
  479. print_time_seconds: 600,
  480. filament_used_grams: 20,
  481. filaments: [],
  482. },
  483. ],
  484. };
  485. }
  486. function makeMultiColorRequirementsResponse() {
  487. return {
  488. file_id: 100,
  489. filename: 'TwoColor.3mf',
  490. plate_id: 1,
  491. filaments: [
  492. { slot_id: 1, type: 'PLA', color: '#000000', used_grams: 10, used_meters: 3 },
  493. { slot_id: 2, type: 'PLA', color: '#FFFFFF', used_grams: 10, used_meters: 3 },
  494. ],
  495. };
  496. }
  497. function makeColorAwarePresets(): UnifiedPresetsResponse {
  498. // Two filament presets in cloud: one black PLA, one white PLA. Pre-pick
  499. // should match each plate slot to the same-colour preset so the user
  500. // doesn't have to manually align them.
  501. return {
  502. cloud: {
  503. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  504. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  505. filament: [
  506. { id: 'F-BLACK', name: 'Cloud PLA Black', source: 'cloud', filament_type: 'PLA', filament_colour: '#000000' },
  507. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  508. ],
  509. },
  510. local: { printer: [], process: [], filament: [] },
  511. standard: { printer: [], process: [], filament: [] },
  512. cloud_status: 'ok',
  513. };
  514. }
  515. it('renders one filament dropdown per plate slot when the source is multi-color', async () => {
  516. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  517. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  518. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  519. renderWithTracker({
  520. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  521. onClose: vi.fn(),
  522. });
  523. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  524. // 1 printer + 1 process + 2 filament = 4 dropdowns.
  525. expect(screen.getAllByRole('combobox')).toHaveLength(4);
  526. });
  527. it('pre-picks each filament slot by matching colour metadata', async () => {
  528. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  529. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  530. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  531. mockApi.sliceLibraryFile.mockResolvedValue({
  532. job_id: 42,
  533. status: 'pending',
  534. status_url: '/api/v1/slice-jobs/42',
  535. });
  536. renderWithTracker({
  537. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  538. onClose: vi.fn(),
  539. });
  540. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  541. const user = userEvent.setup();
  542. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  543. await waitFor(() => {
  544. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  545. // Slot 1 was black plate → cloud black preset; slot 2 was white →
  546. // cloud white preset. Pre-pick aligns them by metadata so the user
  547. // doesn't have to swap them manually.
  548. expect(body.filament_presets).toEqual([
  549. { source: 'cloud', id: 'F-BLACK' },
  550. { source: 'cloud', id: 'F-WHITE' },
  551. ]);
  552. });
  553. });
  554. it('still sends the legacy filament_preset for single-color flows', async () => {
  555. // Backwards-compat with backends / proxies that read the singular field.
  556. mockApi.sliceLibraryFile.mockResolvedValue({
  557. job_id: 42,
  558. status: 'pending',
  559. status_url: '/api/v1/slice-jobs/42',
  560. });
  561. renderWithTracker({
  562. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  563. onClose: vi.fn(),
  564. });
  565. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  566. const user = userEvent.setup();
  567. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  568. await waitFor(() => {
  569. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  570. // Single-color path mirrors the array's first entry into the legacy
  571. // singular so older backend clients that only know about
  572. // `filament_preset` still work.
  573. expect(body.filament_preset).toEqual(body.filament_presets[0]);
  574. expect(body.filament_presets).toHaveLength(1);
  575. });
  576. });
  577. it('lets the user override a pre-picked filament slot', async () => {
  578. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  579. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  580. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  581. mockApi.sliceLibraryFile.mockResolvedValue({
  582. job_id: 42,
  583. status: 'pending',
  584. status_url: '/api/v1/slice-jobs/42',
  585. });
  586. renderWithTracker({
  587. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  588. onClose: vi.fn(),
  589. });
  590. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  591. const user = userEvent.setup();
  592. const selects = screen.getAllByRole('combobox') as HTMLSelectElement[];
  593. // Slots 0 (printer) and 1 (process) are auto-picked. Slots 2 and 3 are
  594. // the two filament dropdowns. Swap slot-2 (was black) to white.
  595. await user.selectOptions(selects[2], 'cloud:F-WHITE');
  596. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  597. await waitFor(() => {
  598. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  599. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  600. // Slot 1 stayed at the auto-picked white.
  601. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  602. });
  603. });
  604. // Pre-slice printer-mismatch warning. The slicer CLI cannot re-slice a
  605. // 3MF for a different printer model — clicking Slice in that state
  606. // would silently fall back to the embedded settings and produce a
  607. // wrong-printer file. The modal surfaces a warning and disables Slice
  608. // when the source's source_printer_model doesn't match the picked
  609. // printer profile.
  610. it('shows a printer-mismatch warning and disables Slice when models differ', async () => {
  611. mockApi.getLibraryFilePlates.mockResolvedValue({
  612. file_id: 100,
  613. filename: 'A1Original.3mf',
  614. is_multi_plate: false,
  615. source_printer_model: 'A1',
  616. plates: [
  617. {
  618. index: 1,
  619. name: 'Plate 1',
  620. objects: [],
  621. has_thumbnail: false,
  622. thumbnail_url: null,
  623. print_time_seconds: null,
  624. filament_used_grams: null,
  625. filaments: [],
  626. },
  627. ],
  628. });
  629. // Standard tier offers an X1C profile — the user picks (auto-picks) it.
  630. mockApi.getSlicerPresets.mockResolvedValue(makeUnified({
  631. standard: {
  632. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  633. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  634. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  635. },
  636. }));
  637. renderWithTracker({
  638. source: { kind: 'libraryFile', id: 100, filename: 'A1Original.3mf' },
  639. onClose: vi.fn(),
  640. });
  641. await waitFor(() =>
  642. expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined(),
  643. );
  644. // Warning banner is visible (role=alert) and references both models.
  645. const alert = await screen.findByRole('alert');
  646. expect(alert.textContent).toMatch(/A1/);
  647. expect(alert.textContent).toMatch(/X1 Carbon/);
  648. // Slice button is disabled while the warning is up.
  649. const sliceButton = screen.getByRole('button', { name: /^Slice$/ }) as HTMLButtonElement;
  650. expect(sliceButton.disabled).toBe(true);
  651. });
  652. it('keeps Slice enabled when the picked profile matches the source printer model', async () => {
  653. mockApi.getLibraryFilePlates.mockResolvedValue({
  654. file_id: 100,
  655. filename: 'X1COriginal.3mf',
  656. is_multi_plate: false,
  657. source_printer_model: 'X1 Carbon',
  658. plates: [
  659. {
  660. index: 1,
  661. name: 'Plate 1',
  662. objects: [],
  663. has_thumbnail: false,
  664. thumbnail_url: null,
  665. print_time_seconds: null,
  666. filament_used_grams: null,
  667. filaments: [],
  668. },
  669. ],
  670. });
  671. mockApi.getSlicerPresets.mockResolvedValue(makeUnified({
  672. standard: {
  673. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  674. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  675. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  676. },
  677. }));
  678. renderWithTracker({
  679. source: { kind: 'libraryFile', id: 100, filename: 'X1COriginal.3mf' },
  680. onClose: vi.fn(),
  681. });
  682. await waitFor(() =>
  683. expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined(),
  684. );
  685. // No mismatch warning.
  686. expect(screen.queryByRole('alert')).toBeNull();
  687. const sliceButton = screen.getByRole('button', { name: /^Slice$/ }) as HTMLButtonElement;
  688. expect(sliceButton.disabled).toBe(false);
  689. });
  690. it('keeps Slice enabled when source_printer_model is unknown (legacy archives)', async () => {
  691. // Older 3MFs without project_settings.printer_model fall through to
  692. // no-warning — we don't have enough info to gate the user.
  693. mockApi.getLibraryFilePlates.mockResolvedValue({
  694. file_id: 100,
  695. filename: 'Legacy.3mf',
  696. is_multi_plate: false,
  697. source_printer_model: null,
  698. plates: [
  699. {
  700. index: 1,
  701. name: 'Plate 1',
  702. objects: [],
  703. has_thumbnail: false,
  704. thumbnail_url: null,
  705. print_time_seconds: null,
  706. filament_used_grams: null,
  707. filaments: [],
  708. },
  709. ],
  710. });
  711. renderWithTracker({
  712. source: { kind: 'libraryFile', id: 100, filename: 'Legacy.3mf' },
  713. onClose: vi.fn(),
  714. });
  715. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  716. expect(screen.queryByRole('alert')).toBeNull();
  717. const sliceButton = screen.getByRole('button', { name: /^Slice$/ }) as HTMLButtonElement;
  718. expect(sliceButton.disabled).toBe(false);
  719. });
  720. });