SliceModal.test.tsx 44 KB

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