ModelViewerModal.test.tsx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /**
  2. * Tests for the ModelViewerModal component.
  3. * Tests fullscreen toggle, plate selector, object counts, and tab switching.
  4. */
  5. import { describe, it, expect, vi, beforeEach } from 'vitest';
  6. import { screen, fireEvent, waitFor } from '@testing-library/react';
  7. import { render } from '../utils';
  8. import { ModelViewerModal } from '../../components/ModelViewerModal';
  9. import { setMediaToken } from '../../api/client';
  10. import { openInSlicer } from '../../utils/slicer';
  11. import { http, HttpResponse } from 'msw';
  12. import { server } from '../mocks/server';
  13. // Mock ModelViewer to avoid WebGL/Three.js issues in tests
  14. vi.mock('../../components/ModelViewer', () => ({
  15. ModelViewer: ({ className }: { className?: string }) => (
  16. <div data-testid="model-viewer" className={className}>
  17. Model Viewer Mock
  18. </div>
  19. ),
  20. }));
  21. // Only the protocol-handler launch is stubbed — it would navigate the jsdom
  22. // window. Everything else in the module is a pure predicate, so keep the real
  23. // implementations: re-declaring them here would let the file-type rule these
  24. // tests assert on drift away from the one the component actually runs.
  25. vi.mock('../../utils/slicer', async (importOriginal) => ({
  26. ...(await importOriginal<typeof import('../../utils/slicer')>()),
  27. openInSlicer: vi.fn(),
  28. }));
  29. const mockCapabilities = {
  30. has_model: true,
  31. has_gcode: true,
  32. has_source: false,
  33. build_volume: { x: 256, y: 256, z: 256 },
  34. filament_colors: ['#00ae42'],
  35. };
  36. const mockPlatesResponse = {
  37. is_multi_plate: true,
  38. plates: [
  39. {
  40. index: 1,
  41. name: 'Plate 1',
  42. has_thumbnail: true,
  43. thumbnail_url: '/api/v1/archives/1/plates/1/thumbnail',
  44. print_time_seconds: 3600,
  45. filament_used_grams: 50.5,
  46. object_count: 3,
  47. objects: ['Cube', 'Sphere', 'Cylinder'],
  48. filaments: [{ color: '#00ae42', type: 'PLA', name: 'Bambu PLA Basic' }],
  49. },
  50. {
  51. index: 2,
  52. name: 'Plate 2',
  53. has_thumbnail: true,
  54. thumbnail_url: '/api/v1/archives/1/plates/2/thumbnail',
  55. print_time_seconds: 1800,
  56. filament_used_grams: 25.0,
  57. object_count: 2,
  58. objects: ['Base', 'Cover'],
  59. filaments: [{ color: '#ff0000', type: 'PLA', name: 'Red PLA' }],
  60. },
  61. ],
  62. };
  63. const mockSinglePlateResponse = {
  64. is_multi_plate: false,
  65. plates: [
  66. {
  67. index: 1,
  68. name: null,
  69. has_thumbnail: false,
  70. thumbnail_url: null,
  71. print_time_seconds: 7200,
  72. filament_used_grams: 100.0,
  73. object_count: 5,
  74. objects: ['Model 1', 'Model 2', 'Model 3', 'Model 4', 'Model 5'],
  75. filaments: [],
  76. },
  77. ],
  78. };
  79. describe('ModelViewerModal', () => {
  80. const mockOnClose = vi.fn();
  81. beforeEach(() => {
  82. vi.clearAllMocks();
  83. server.use(
  84. http.get('/api/v1/archives/:id/capabilities', () => {
  85. return HttpResponse.json(mockCapabilities);
  86. }),
  87. http.get('/api/v1/archives/:id/plates', () => {
  88. return HttpResponse.json(mockPlatesResponse);
  89. })
  90. );
  91. });
  92. describe('rendering', () => {
  93. it('renders the modal with title', async () => {
  94. render(
  95. <ModelViewerModal
  96. archiveId={1}
  97. title="Test Model.3mf"
  98. onClose={mockOnClose}
  99. />
  100. );
  101. expect(screen.getByText('Test Model.3mf')).toBeInTheDocument();
  102. });
  103. it('renders Open in Slicer button', async () => {
  104. render(
  105. <ModelViewerModal
  106. archiveId={1}
  107. title="Test Model"
  108. onClose={mockOnClose}
  109. />
  110. );
  111. await waitFor(() => {
  112. expect(screen.getByText('Open in Slicer')).toBeInTheDocument();
  113. });
  114. });
  115. it('shows loading spinner while fetching capabilities', () => {
  116. server.use(
  117. http.get('/api/v1/archives/:id/capabilities', async () => {
  118. await new Promise((r) => setTimeout(r, 100));
  119. return HttpResponse.json(mockCapabilities);
  120. })
  121. );
  122. render(
  123. <ModelViewerModal
  124. archiveId={1}
  125. title="Test Model"
  126. onClose={mockOnClose}
  127. />
  128. );
  129. const loader = document.querySelector('.animate-spin');
  130. expect(loader).toBeInTheDocument();
  131. });
  132. });
  133. describe('tabs', () => {
  134. it('renders the 3D Model tab and no G-code tab', async () => {
  135. // G-code has its own full-page viewer; the modal is model-only.
  136. render(
  137. <ModelViewerModal
  138. archiveId={1}
  139. title="Test Model"
  140. onClose={mockOnClose}
  141. />
  142. );
  143. await waitFor(() => {
  144. expect(screen.getByText('3D Model')).toBeInTheDocument();
  145. });
  146. expect(screen.queryByText('G-code Preview')).not.toBeInTheDocument();
  147. });
  148. it('shows not available label when model is not available', async () => {
  149. server.use(
  150. http.get('/api/v1/archives/:id/capabilities', () => {
  151. return HttpResponse.json({
  152. ...mockCapabilities,
  153. has_model: false,
  154. });
  155. })
  156. );
  157. render(
  158. <ModelViewerModal
  159. archiveId={1}
  160. title="Test Model"
  161. onClose={mockOnClose}
  162. />
  163. );
  164. await waitFor(() => {
  165. expect(screen.getByText('(not available)')).toBeInTheDocument();
  166. });
  167. });
  168. });
  169. describe('fullscreen', () => {
  170. it('renders fullscreen toggle button', async () => {
  171. render(
  172. <ModelViewerModal
  173. archiveId={1}
  174. title="Test Model"
  175. onClose={mockOnClose}
  176. />
  177. );
  178. await waitFor(() => {
  179. // Look for the maximize icon button
  180. const buttons = screen.getAllByRole('button');
  181. const fullscreenButton = buttons.find(
  182. (btn) => btn.querySelector('.lucide-maximize-2') || btn.title === 'Fullscreen'
  183. );
  184. expect(fullscreenButton).toBeInTheDocument();
  185. });
  186. });
  187. it('double-click on the viewer requests fullscreen for the panel (#2976)', async () => {
  188. const requestFullscreen = vi.fn().mockResolvedValue(undefined);
  189. Object.defineProperty(document, 'fullscreenEnabled', { configurable: true, value: true });
  190. Object.defineProperty(HTMLElement.prototype, 'requestFullscreen', { configurable: true, value: requestFullscreen });
  191. try {
  192. render(
  193. <ModelViewerModal
  194. archiveId={1}
  195. title="Test Model"
  196. onClose={mockOnClose}
  197. />
  198. );
  199. const viewer = await screen.findByTestId('model-viewer-area');
  200. fireEvent.doubleClick(viewer);
  201. expect(requestFullscreen).toHaveBeenCalledTimes(1);
  202. const panel = screen.getByText('Test Model').closest('.flex-col');
  203. expect(requestFullscreen.mock.instances[0]).toBe(panel);
  204. } finally {
  205. delete (document as { fullscreenEnabled?: boolean }).fullscreenEnabled;
  206. delete (HTMLElement.prototype as { requestFullscreen?: () => Promise<void> }).requestFullscreen;
  207. }
  208. });
  209. it('fills the viewport as a fallback where the Fullscreen API is missing', async () => {
  210. render(
  211. <ModelViewerModal
  212. archiveId={1}
  213. title="Test Model"
  214. onClose={mockOnClose}
  215. />
  216. );
  217. const viewer = await screen.findByTestId('model-viewer-area');
  218. const panel = screen.getByText('Test Model').closest('.flex-col') as HTMLElement;
  219. // Windowed: the size every preview shares (#2976), not a per-modal max-w.
  220. expect(panel.className).toContain('w-[min(1800px,96vw)]');
  221. fireEvent.doubleClick(viewer);
  222. expect(panel.className).toContain('max-w-none');
  223. expect(screen.getByTitle('Exit fullscreen')).toBeInTheDocument();
  224. });
  225. });
  226. describe('object count', () => {
  227. it('displays object count for multi-plate files', async () => {
  228. render(
  229. <ModelViewerModal
  230. archiveId={1}
  231. title="Test Model"
  232. onClose={mockOnClose}
  233. />
  234. );
  235. await waitFor(() => {
  236. // Total objects across both plates = 3 + 2 = 5
  237. // The header shows "All Plates: 5 objects" in a span
  238. const objectCountBadge = screen.getByText(/All Plates.*5 objects/);
  239. expect(objectCountBadge).toBeInTheDocument();
  240. });
  241. });
  242. it('updates object count when plate is selected', async () => {
  243. render(
  244. <ModelViewerModal
  245. archiveId={1}
  246. title="Test Model"
  247. onClose={mockOnClose}
  248. />
  249. );
  250. await waitFor(() => {
  251. expect(screen.getByText('Plate 1')).toBeInTheDocument();
  252. });
  253. // Click on Plate 1
  254. fireEvent.click(screen.getByText('Plate 1'));
  255. await waitFor(() => {
  256. // Plate 1 has 3 objects - header should update to show "Plate 1: 3 objects"
  257. const objectCountBadge = screen.getByText(/Plate 1.*3 objects/);
  258. expect(objectCountBadge).toBeInTheDocument();
  259. });
  260. });
  261. });
  262. describe('plate selector', () => {
  263. it('shows plates panel for multi-plate files', async () => {
  264. render(
  265. <ModelViewerModal
  266. archiveId={1}
  267. title="Test Model"
  268. onClose={mockOnClose}
  269. />
  270. );
  271. await waitFor(() => {
  272. expect(screen.getByText('Plates')).toBeInTheDocument();
  273. // Use getAllByText for "All Plates" since it appears in header and panel
  274. const allPlatesElements = screen.getAllByText('All Plates');
  275. expect(allPlatesElements.length).toBeGreaterThan(0);
  276. expect(screen.getByText('2 plates')).toBeInTheDocument();
  277. });
  278. });
  279. it('shows individual plate buttons', async () => {
  280. render(
  281. <ModelViewerModal
  282. archiveId={1}
  283. title="Test Model"
  284. onClose={mockOnClose}
  285. />
  286. );
  287. await waitFor(() => {
  288. expect(screen.getByText('Plate 1')).toBeInTheDocument();
  289. expect(screen.getByText('Plate 2')).toBeInTheDocument();
  290. });
  291. });
  292. it('shows object count for each plate', async () => {
  293. render(
  294. <ModelViewerModal
  295. archiveId={1}
  296. title="Test Model"
  297. onClose={mockOnClose}
  298. />
  299. );
  300. await waitFor(() => {
  301. // Each plate shows its object count in the grid
  302. expect(screen.getByText('3 objects')).toBeInTheDocument();
  303. expect(screen.getByText('2 objects')).toBeInTheDocument();
  304. });
  305. });
  306. it('hides plates panel for single-plate files', async () => {
  307. server.use(
  308. http.get('/api/v1/archives/:id/plates', () => {
  309. return HttpResponse.json(mockSinglePlateResponse);
  310. })
  311. );
  312. render(
  313. <ModelViewerModal
  314. archiveId={1}
  315. title="Test Model"
  316. onClose={mockOnClose}
  317. />
  318. );
  319. await waitFor(() => {
  320. // Should show object count but not plate selector
  321. expect(screen.getByText(/5 objects/)).toBeInTheDocument();
  322. });
  323. // Plates panel should not be shown for single plate
  324. expect(screen.queryByText('2 plates')).not.toBeInTheDocument();
  325. });
  326. it('selects All Plates by default', async () => {
  327. render(
  328. <ModelViewerModal
  329. archiveId={1}
  330. title="Test Model"
  331. onClose={mockOnClose}
  332. />
  333. );
  334. await waitFor(() => {
  335. // Find the All Plates button in the grid (the one with "2 plates" sibling text)
  336. const platesCountText = screen.getByText('2 plates');
  337. const allPlatesButton = platesCountText.closest('button');
  338. // The selected button should have the green border class
  339. expect(allPlatesButton).toHaveClass('border-bambu-green');
  340. });
  341. });
  342. // #2661: plate-thumbnail endpoints are gated behind a query token
  343. // (an <img> can't send a Bearer header), so the src must carry ?token=.
  344. // Without it the 3D Preview thumbnails 401 while the Slice dialog (which
  345. // already appends the token) shows the same file's thumbnails fine.
  346. // #3025 moved these off the camera token onto the media token, so a user
  347. // without camera:view can see them; the src requirement is unchanged.
  348. it('appends the media token to plate thumbnail URLs', async () => {
  349. setMediaToken('tok-2661');
  350. try {
  351. render(
  352. <ModelViewerModal
  353. archiveId={1}
  354. title="Test Model"
  355. onClose={mockOnClose}
  356. />
  357. );
  358. await waitFor(() => {
  359. expect(screen.getByText('Plate 1')).toBeInTheDocument();
  360. });
  361. const thumb = screen.getByAltText('Plate 1') as HTMLImageElement;
  362. expect(thumb.src).toContain('/api/v1/archives/1/plates/1/thumbnail');
  363. expect(thumb.src).toContain('token=tok-2661');
  364. } finally {
  365. setMediaToken(null);
  366. }
  367. });
  368. it('allows plate selection via click', async () => {
  369. render(
  370. <ModelViewerModal
  371. archiveId={1}
  372. title="Test Model"
  373. onClose={mockOnClose}
  374. />
  375. );
  376. await waitFor(() => {
  377. expect(screen.getByText('Plate 1')).toBeInTheDocument();
  378. });
  379. // Click on Plate 1 - this should not throw
  380. const plate1Button = screen.getByText('Plate 1').closest('button');
  381. expect(plate1Button).toBeInTheDocument();
  382. fireEvent.click(plate1Button!);
  383. // After clicking, the header should show Plate 1 info
  384. await waitFor(() => {
  385. expect(screen.getByText(/Plate 1.*3 objects/)).toBeInTheDocument();
  386. });
  387. });
  388. });
  389. describe('close behavior', () => {
  390. it('calls onClose when X button is clicked', async () => {
  391. render(
  392. <ModelViewerModal
  393. archiveId={1}
  394. title="Test Model"
  395. onClose={mockOnClose}
  396. />
  397. );
  398. const closeButton = screen.getAllByRole('button').find(
  399. (btn) => btn.querySelector('.lucide-x')
  400. );
  401. if (closeButton) {
  402. fireEvent.click(closeButton);
  403. expect(mockOnClose).toHaveBeenCalled();
  404. }
  405. });
  406. it('calls onClose when Escape key is pressed', () => {
  407. render(
  408. <ModelViewerModal
  409. archiveId={1}
  410. title="Test Model"
  411. onClose={mockOnClose}
  412. />
  413. );
  414. fireEvent.keyDown(window, { key: 'Escape' });
  415. expect(mockOnClose).toHaveBeenCalled();
  416. });
  417. it('calls onClose when backdrop is clicked', () => {
  418. render(
  419. <ModelViewerModal
  420. archiveId={1}
  421. title="Test Model"
  422. onClose={mockOnClose}
  423. />
  424. );
  425. const backdrop = document.querySelector('.fixed.inset-0');
  426. if (backdrop) {
  427. fireEvent.click(backdrop);
  428. expect(mockOnClose).toHaveBeenCalled();
  429. }
  430. });
  431. });
  432. describe('library file mode', () => {
  433. it('renders for library file', async () => {
  434. server.use(
  435. http.get('/api/v1/library/files/:id/plates', () => {
  436. return HttpResponse.json(mockSinglePlateResponse);
  437. })
  438. );
  439. render(
  440. <ModelViewerModal
  441. libraryFileId={1}
  442. title="Library Model.3mf"
  443. fileType="3mf"
  444. onClose={mockOnClose}
  445. />
  446. );
  447. expect(screen.getByText('Library Model.3mf')).toBeInTheDocument();
  448. await waitFor(() => {
  449. expect(screen.getByText('3D Model')).toBeInTheDocument();
  450. });
  451. });
  452. it('disables Open in Slicer for library files that cannot be handed to a slicer', async () => {
  453. render(
  454. <ModelViewerModal
  455. libraryFileId={1}
  456. title="Model.gcode"
  457. fileType="gcode"
  458. onClose={mockOnClose}
  459. />
  460. );
  461. await waitFor(() => {
  462. const slicerButton = screen.getByText('Open in Slicer').closest('button');
  463. expect(slicerButton).toBeDisabled();
  464. });
  465. });
  466. });
  467. describe('slicer split button (#2725)', () => {
  468. it('shows both slicers in the dropdown when Bambuddy is the default slicer', async () => {
  469. server.use(
  470. http.get('/api/v1/settings/', () => {
  471. return HttpResponse.json({ use_slicer_api: true });
  472. }),
  473. http.get('/api/v1/library/files/:id/plates', () => {
  474. return HttpResponse.json(mockSinglePlateResponse);
  475. })
  476. );
  477. render(
  478. <ModelViewerModal
  479. libraryFileId={1}
  480. title="Model.3mf"
  481. fileType="3mf"
  482. onClose={mockOnClose}
  483. onSliceWithBambuddy={vi.fn()}
  484. />
  485. );
  486. await waitFor(() => {
  487. expect(screen.getByRole('button', { name: 'Slice' })).toBeInTheDocument();
  488. });
  489. fireEvent.click(screen.getByRole('button', { name: 'More slicer options' }));
  490. await waitFor(() => {
  491. expect(screen.getByText('Open in Bambu Studio')).toBeInTheDocument();
  492. expect(screen.getByText('Open in OrcaSlicer')).toBeInTheDocument();
  493. });
  494. });
  495. it('opens the selected local slicer from the Bambuddy dropdown', async () => {
  496. server.use(
  497. http.get('/api/v1/settings/', () => {
  498. return HttpResponse.json({ use_slicer_api: true });
  499. }),
  500. http.get('/api/v1/library/files/:id/plates', () => {
  501. return HttpResponse.json(mockSinglePlateResponse);
  502. })
  503. );
  504. render(
  505. <ModelViewerModal
  506. libraryFileId={1}
  507. title="Model.3mf"
  508. fileType="3mf"
  509. onClose={mockOnClose}
  510. onSliceWithBambuddy={vi.fn()}
  511. />
  512. );
  513. await waitFor(() => {
  514. expect(screen.getByRole('button', { name: 'Slice' })).toBeInTheDocument();
  515. });
  516. fireEvent.click(screen.getByRole('button', { name: 'More slicer options' }));
  517. const orcaItem = await screen.findByText('Open in OrcaSlicer');
  518. fireEvent.click(orcaItem);
  519. await waitFor(() => {
  520. expect(openInSlicer).toHaveBeenCalledWith(expect.any(String), 'orcaslicer');
  521. });
  522. });
  523. it('shows only the non-preferred slicer in the dropdown for a desktop handoff', async () => {
  524. render(
  525. <ModelViewerModal
  526. archiveId={1}
  527. title="Test Model"
  528. onClose={mockOnClose}
  529. />
  530. );
  531. await waitFor(() => {
  532. expect(screen.getByRole('button', { name: 'Open in Slicer' })).toBeInTheDocument();
  533. });
  534. fireEvent.click(screen.getByRole('button', { name: 'More slicer options' }));
  535. await waitFor(() => {
  536. expect(screen.getByText('Open in OrcaSlicer')).toBeInTheDocument();
  537. });
  538. expect(screen.queryByText('Open in Bambu Studio')).not.toBeInTheDocument();
  539. });
  540. it('offers Bambu Studio when the preferred desktop slicer is OrcaSlicer', async () => {
  541. server.use(
  542. http.get('/api/v1/settings/', () => {
  543. return HttpResponse.json({ preferred_slicer: 'orcaslicer' });
  544. })
  545. );
  546. render(
  547. <ModelViewerModal
  548. archiveId={1}
  549. title="Test Model"
  550. onClose={mockOnClose}
  551. />
  552. );
  553. await waitFor(() => {
  554. expect(screen.getByRole('button', { name: 'Open in Slicer' })).toBeInTheDocument();
  555. });
  556. fireEvent.click(screen.getByRole('button', { name: 'More slicer options' }));
  557. await waitFor(() => {
  558. expect(screen.getByText('Open in Bambu Studio')).toBeInTheDocument();
  559. });
  560. expect(screen.queryByText('Open in OrcaSlicer')).not.toBeInTheDocument();
  561. });
  562. it('closes the split dropdown on Escape without closing the modal', async () => {
  563. render(
  564. <ModelViewerModal
  565. archiveId={1}
  566. title="Test Model"
  567. onClose={mockOnClose}
  568. />
  569. );
  570. await waitFor(() => {
  571. expect(screen.getByRole('button', { name: 'Open in Slicer' })).toBeInTheDocument();
  572. });
  573. fireEvent.click(screen.getByRole('button', { name: 'More slicer options' }));
  574. await waitFor(() => {
  575. expect(screen.getByRole('menu')).toBeInTheDocument();
  576. });
  577. fireEvent.keyDown(document, { key: 'Escape' });
  578. expect(screen.queryByRole('menu')).not.toBeInTheDocument();
  579. expect(mockOnClose).not.toHaveBeenCalled();
  580. });
  581. it('closes the split dropdown on an outside click without closing the modal', async () => {
  582. render(
  583. <ModelViewerModal
  584. archiveId={1}
  585. title="Test Model"
  586. onClose={mockOnClose}
  587. />
  588. );
  589. await waitFor(() => {
  590. expect(screen.getByRole('button', { name: 'Open in Slicer' })).toBeInTheDocument();
  591. });
  592. fireEvent.click(screen.getByRole('button', { name: 'More slicer options' }));
  593. await waitFor(() => {
  594. expect(screen.getByRole('menu')).toBeInTheDocument();
  595. });
  596. fireEvent.mouseDown(document.body);
  597. expect(screen.queryByRole('menu')).not.toBeInTheDocument();
  598. expect(mockOnClose).not.toHaveBeenCalled();
  599. });
  600. it('does not render a split chevron when the file cannot open in a slicer', async () => {
  601. render(
  602. <ModelViewerModal
  603. libraryFileId={1}
  604. title="Model.gcode"
  605. fileType="gcode"
  606. onClose={mockOnClose}
  607. />
  608. );
  609. await waitFor(() => {
  610. expect(screen.getByRole('button', { name: 'Open in Slicer' })).toBeDisabled();
  611. });
  612. expect(screen.queryByRole('button', { name: 'More slicer options' })).not.toBeInTheDocument();
  613. });
  614. it('offers the desktop handoff for an STL library file, naming the slicer', async () => {
  615. // Default settings mean Bambu Studio is preferred, and its protocol
  616. // handler takes 3MF only (#3029) -- so OrcaSlicer becomes the primary
  617. // action. It is named rather than hidden behind a generic "Open in
  618. // Slicer", because the file is not going where the setting says.
  619. render(
  620. <ModelViewerModal
  621. libraryFileId={1}
  622. title="Model.stl"
  623. fileType="stl"
  624. onClose={mockOnClose}
  625. />
  626. );
  627. const button = await screen.findByRole('button', { name: 'Open in OrcaSlicer' });
  628. expect(button).toBeEnabled();
  629. // OrcaSlicer is the only slicer that can take it, so there is no
  630. // alternative to offer and the split collapses to a plain button.
  631. expect(screen.queryByRole('button', { name: 'More slicer options' })).not.toBeInTheDocument();
  632. fireEvent.click(button);
  633. await waitFor(() => {
  634. expect(openInSlicer).toHaveBeenCalledWith(expect.any(String), 'orcaslicer');
  635. });
  636. });
  637. it('does not offer Bambu Studio a file its handler will refuse', async () => {
  638. server.use(
  639. http.get('/api/v1/settings/', () => {
  640. return HttpResponse.json({ preferred_slicer: 'orcaslicer' });
  641. })
  642. );
  643. render(
  644. <ModelViewerModal
  645. libraryFileId={1}
  646. title="Model.stl"
  647. fileType="stl"
  648. onClose={mockOnClose}
  649. />
  650. );
  651. // OrcaSlicer is preferred and takes an STL, so it is the plain primary.
  652. await screen.findByRole('button', { name: 'Open in Slicer' });
  653. expect(screen.queryByRole('button', { name: 'More slicer options' })).not.toBeInTheDocument();
  654. expect(screen.queryByText('Open in Bambu Studio')).not.toBeInTheDocument();
  655. });
  656. it('offers the split Slice button for an STL when the slicer API is enabled', async () => {
  657. server.use(
  658. http.get('/api/v1/settings/', () => {
  659. return HttpResponse.json({ use_slicer_api: true });
  660. })
  661. );
  662. render(
  663. <ModelViewerModal
  664. libraryFileId={1}
  665. title="Model.stl"
  666. fileType="stl"
  667. onClose={mockOnClose}
  668. onSliceWithBambuddy={vi.fn()}
  669. />
  670. );
  671. await waitFor(() => {
  672. expect(screen.getByRole('button', { name: 'Slice' })).toBeInTheDocument();
  673. });
  674. fireEvent.click(screen.getByRole('button', { name: 'More slicer options' }));
  675. await waitFor(() => {
  676. expect(screen.getByText('Open in OrcaSlicer')).toBeInTheDocument();
  677. });
  678. // Bambu Studio is absent: the sidecar can slice an STL, but Bambu
  679. // Studio's URL handler cannot load one (#3029).
  680. expect(screen.queryByText('Open in Bambu Studio')).not.toBeInTheDocument();
  681. });
  682. });
  683. });