SliceModal.test.tsx 43 KB

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