SliceModal.test.tsx 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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 { pickFilamentForSlot } from '../../utils/slicePresetPicker';
  16. import { buildCompatibilityIndex } from '../../utils/slicerPrinterMatch';
  17. import { SliceJobTrackerProvider } from '../../contexts/SliceJobTrackerContext';
  18. import { api, type UnifiedPresetsResponse } from '../../api/client';
  19. vi.mock('../../api/client', () => ({
  20. api: {
  21. getSlicerPresets: vi.fn(),
  22. sliceLibraryFile: vi.fn(),
  23. sliceArchive: vi.fn(),
  24. getSliceJob: vi.fn(),
  25. getLibraryFilePlates: vi.fn(),
  26. getArchivePlates: vi.fn(),
  27. getLibraryFileFilamentRequirements: vi.fn(),
  28. getArchiveFilamentRequirements: vi.fn(),
  29. getSettings: vi.fn().mockResolvedValue({}),
  30. updateSettings: vi.fn().mockResolvedValue({}),
  31. // Slicer Pipelines (#1425)
  32. listSlicerPipelines: vi.fn(),
  33. createSlicerPipeline: vi.fn(),
  34. },
  35. }));
  36. const mockApi = api as unknown as {
  37. getSlicerPresets: ReturnType<typeof vi.fn>;
  38. sliceLibraryFile: ReturnType<typeof vi.fn>;
  39. sliceArchive: ReturnType<typeof vi.fn>;
  40. getSliceJob: ReturnType<typeof vi.fn>;
  41. getLibraryFilePlates: ReturnType<typeof vi.fn>;
  42. getArchivePlates: ReturnType<typeof vi.fn>;
  43. getLibraryFileFilamentRequirements: ReturnType<typeof vi.fn>;
  44. getArchiveFilamentRequirements: ReturnType<typeof vi.fn>;
  45. listSlicerPipelines: ReturnType<typeof vi.fn>;
  46. createSlicerPipeline: ReturnType<typeof vi.fn>;
  47. };
  48. function makeUnified(overrides: Partial<UnifiedPresetsResponse> = {}): UnifiedPresetsResponse {
  49. return {
  50. orca_cloud: { printer: [], process: [], filament: [] },
  51. cloud: { printer: [], process: [], filament: [] },
  52. local: { printer: [], process: [], filament: [] },
  53. standard: { printer: [], process: [], filament: [] },
  54. cloud_status: 'ok',
  55. orca_cloud_status: 'ok',
  56. ...overrides,
  57. };
  58. }
  59. const fullThreeTier: UnifiedPresetsResponse = makeUnified({
  60. cloud: {
  61. printer: [{ id: 'PFUcloud-printer', name: 'My Custom X1C', source: 'cloud' }],
  62. process: [{ id: 'PFUcloud-process', name: 'My 0.16mm Tweaked', source: 'cloud' }],
  63. filament: [{ id: 'PFUcloud-filament', name: 'My PLA Black', source: 'cloud' }],
  64. },
  65. local: {
  66. printer: [{ id: '1', name: 'Imported X1C 0.4', source: 'local' }],
  67. process: [{ id: '2', name: 'Imported 0.20mm', source: 'local' }],
  68. filament: [{ id: '3', name: 'Imported PLA Basic', source: 'local' }],
  69. },
  70. standard: {
  71. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  72. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  73. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  74. },
  75. });
  76. function renderWithTracker(props: Parameters<typeof SliceModal>[0]) {
  77. return render(
  78. <SliceJobTrackerProvider>
  79. <SliceModal {...props} />
  80. </SliceJobTrackerProvider>,
  81. );
  82. }
  83. // SliceModal renders one extra combobox for the Slicer Pipelines (#1425)
  84. // "Apply pipeline" dropdown above the preset slots. Tests written before
  85. // pipelines landed assume selects[0] = printer; this helper drops the
  86. // pipeline combobox so those indices stay stable.
  87. function presetSelects(): HTMLSelectElement[] {
  88. return (screen.getAllByRole('combobox') as HTMLSelectElement[]).filter(
  89. (el) => el.getAttribute('aria-label') !== 'Apply pipeline',
  90. );
  91. }
  92. describe('SliceModal', () => {
  93. beforeEach(() => {
  94. vi.clearAllMocks();
  95. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  96. mockApi.getSliceJob.mockResolvedValue({
  97. job_id: 42,
  98. status: 'running',
  99. kind: 'library_file',
  100. source_id: 100,
  101. source_name: 'Cube.stl',
  102. created_at: new Date().toISOString(),
  103. started_at: null,
  104. completed_at: null,
  105. });
  106. // Default: single-plate (or non-3MF). Multi-plate tests override this.
  107. mockApi.getLibraryFilePlates.mockResolvedValue({
  108. file_id: 100,
  109. filename: 'Cube.stl',
  110. plates: [],
  111. is_multi_plate: false,
  112. });
  113. mockApi.getArchivePlates.mockResolvedValue({
  114. archive_id: 100,
  115. filename: 'Cube.3mf',
  116. plates: [],
  117. is_multi_plate: false,
  118. });
  119. // Default: no per-plate filament metadata available (mirrors STL or
  120. // unsliced source). Multi-color tests override this.
  121. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  122. file_id: 100,
  123. filename: 'Cube.stl',
  124. plate_id: 1,
  125. filaments: [],
  126. });
  127. mockApi.getArchiveFilamentRequirements.mockResolvedValue({
  128. archive_id: 100,
  129. filename: 'Cube.3mf',
  130. plate_id: 1,
  131. filaments: [],
  132. });
  133. // Default: no saved pipelines. Tests opt in by overriding this.
  134. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  135. });
  136. it('auto-selects the highest-priority tier per slot on first load', async () => {
  137. renderWithTracker({
  138. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  139. onClose: vi.fn(),
  140. });
  141. // SliceModal-specific tier priority: imported (local) wins over cloud
  142. // and standard so the user's curated picks come first.
  143. await waitFor(() => {
  144. expect(screen.getByText('My Custom X1C')).toBeDefined();
  145. });
  146. // 4 selects: printer, process, bed-type (#1337), filament. bed-type sits
  147. // between process and filament — it overrides curr_bed_type on the
  148. // process preset so the related controls cluster — and defaults to "".
  149. const selects = presetSelects();
  150. expect(selects).toHaveLength(4);
  151. expect(selects[0].value).toBe('local:1');
  152. expect(selects[1].value).toBe('local:2');
  153. expect(selects[2].value).toBe('');
  154. expect(selects[3].value).toBe('local:3');
  155. // Slice button is enabled because all three slots auto-defaulted and
  156. // the preview-slice query has resolved (mock returns immediately).
  157. const sliceBtn = screen.getByRole('button', { name: /^Slice$/ });
  158. expect((sliceBtn as HTMLButtonElement).disabled).toBe(false);
  159. });
  160. it('renders Imported / Cloud / Standard sections via <optgroup>', async () => {
  161. renderWithTracker({
  162. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  163. onClose: vi.fn(),
  164. });
  165. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  166. const printerSelect = presetSelects()[0];
  167. const groups = printerSelect.querySelectorAll('optgroup');
  168. expect(Array.from(groups).map((g) => g.label)).toEqual([
  169. 'Imported',
  170. 'Bambu Cloud',
  171. 'Standard',
  172. ]);
  173. // Each entry sits inside its own tier's group — pin the assignment so
  174. // a future render-shape change can't quietly mix them. Order matches
  175. // SLICE_MODAL_TIER_ORDER (local → cloud → standard).
  176. const localGroup = groups[0];
  177. expect(within(localGroup as HTMLElement).getByText('Imported X1C 0.4')).toBeDefined();
  178. const cloudGroup = groups[1];
  179. expect(within(cloudGroup as HTMLElement).getByText('My Custom X1C')).toBeDefined();
  180. const standardGroup = groups[2];
  181. expect(within(standardGroup as HTMLElement).getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined();
  182. });
  183. it('falls back to local when cloud is empty (auto-pick respects priority)', async () => {
  184. mockApi.getSlicerPresets.mockResolvedValue(
  185. makeUnified({
  186. local: fullThreeTier.local,
  187. standard: fullThreeTier.standard,
  188. }),
  189. );
  190. renderWithTracker({
  191. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  192. onClose: vi.fn(),
  193. });
  194. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  195. const selects = presetSelects();
  196. expect(selects[0].value).toBe('local:1');
  197. });
  198. it('falls back to standard when both cloud and local are empty', async () => {
  199. mockApi.getSlicerPresets.mockResolvedValue(
  200. makeUnified({ standard: fullThreeTier.standard }),
  201. );
  202. renderWithTracker({
  203. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  204. onClose: vi.fn(),
  205. });
  206. await waitFor(() => expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined());
  207. const selects = presetSelects();
  208. expect(selects[0].value).toBe('standard:Bambu Lab X1 Carbon 0.4 nozzle');
  209. });
  210. it('sends source-aware refs (not legacy bare ints) on submit', async () => {
  211. const onClose = vi.fn();
  212. mockApi.sliceLibraryFile.mockResolvedValue({
  213. job_id: 42,
  214. status: 'pending',
  215. status_url: '/api/v1/slice-jobs/42',
  216. });
  217. renderWithTracker({
  218. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  219. onClose,
  220. });
  221. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  222. const user = userEvent.setup();
  223. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  224. await waitFor(() => {
  225. // SliceModal-specific tier priority puts imported (local) above cloud,
  226. // so the auto-pick lands on the local entries even when a cloud entry
  227. // with the same slot is also available in the listing.
  228. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(100, {
  229. printer_preset: { source: 'local', id: '1' },
  230. process_preset: { source: 'local', id: '2' },
  231. filament_preset: { source: 'local', id: '3' },
  232. filament_presets: [{ source: 'local', id: '3' }],
  233. });
  234. });
  235. await waitFor(() => expect(onClose).toHaveBeenCalled());
  236. });
  237. it('offers "use the file\'s built-in settings" when the printer matches the design, and sends the flag (#2611)', async () => {
  238. const onClose = vi.fn();
  239. mockApi.sliceLibraryFile.mockResolvedValue({
  240. job_id: 42,
  241. status: 'pending',
  242. status_url: '/api/v1/slice-jobs/42',
  243. });
  244. // A project 3MF whose embedded printer matches a listed preset — the
  245. // printer pre-pick lands on it, so selectedPrinterName === embedded and
  246. // the "slice as designed" toggle is offered.
  247. mockApi.getLibraryFilePlates.mockResolvedValue({
  248. file_id: 100,
  249. filename: 'Designed.3mf',
  250. plates: [],
  251. is_multi_plate: false,
  252. embedded_printer: 'Bambu Lab X1 Carbon 0.4 nozzle',
  253. embedded_process: '0.20mm Standard',
  254. });
  255. renderWithTracker({
  256. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  257. onClose,
  258. });
  259. const user = userEvent.setup();
  260. const toggle = (await screen.findByLabelText(
  261. /Use the file's built-in settings/,
  262. )) as HTMLInputElement;
  263. expect(toggle.checked).toBe(false);
  264. // All preset dropdowns are live until the toggle is on, then bypassed —
  265. // the printer included, so changing it can't silently drop the mode.
  266. const printerSelect = presetSelects()[0];
  267. const processSelect = presetSelects()[1];
  268. expect(printerSelect.disabled).toBe(false);
  269. expect(processSelect.disabled).toBe(false);
  270. await user.click(toggle);
  271. expect(printerSelect.disabled).toBe(true);
  272. expect(processSelect.disabled).toBe(true);
  273. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  274. await waitFor(() => {
  275. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  276. 100,
  277. expect.objectContaining({ use_embedded_settings: true }),
  278. );
  279. });
  280. await waitFor(() => expect(onClose).toHaveBeenCalled());
  281. });
  282. it('hides the embedded-settings toggle when the picked printer differs from the design (#2611)', async () => {
  283. // Embedded target is a model with no matching preset in the listing, so
  284. // the printer pre-pick falls back to the local default (Imported X1C),
  285. // which does not match — honouring embedded settings would risk the
  286. // wrong bed, so the toggle stays hidden.
  287. mockApi.getLibraryFilePlates.mockResolvedValue({
  288. file_id: 100,
  289. filename: 'Designed.3mf',
  290. plates: [],
  291. is_multi_plate: false,
  292. embedded_printer: 'Bambu Lab P1S 0.4 nozzle',
  293. embedded_process: '0.20mm Standard',
  294. });
  295. renderWithTracker({
  296. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  297. onClose: vi.fn(),
  298. });
  299. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  300. expect(screen.queryByLabelText(/Use the file's built-in settings/)).toBeNull();
  301. });
  302. it('includes bed_type in the request when the user picks a non-auto plate (#1337)', async () => {
  303. const onClose = vi.fn();
  304. mockApi.sliceLibraryFile.mockResolvedValue({
  305. job_id: 42,
  306. status: 'pending',
  307. status_url: '/api/v1/slice-jobs/42',
  308. });
  309. renderWithTracker({
  310. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  311. onClose,
  312. });
  313. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  314. const user = userEvent.setup();
  315. // Order with the dropdown now sits between Process and Filament:
  316. // printer (0), process (1), bed-type (2), filament (3+). Find the
  317. // bed-type select by name rather than positional index so this stays
  318. // green if the layout adds another control around it.
  319. const bedSelect = presetSelects().find((el) =>
  320. (el as HTMLSelectElement).options[0]?.textContent?.toLowerCase().includes('auto'),
  321. ) as HTMLSelectElement;
  322. expect(bedSelect).toBeDefined();
  323. await user.selectOptions(bedSelect, 'Textured PEI Plate');
  324. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  325. await waitFor(() => {
  326. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  327. 100,
  328. expect.objectContaining({ bed_type: 'Textured PEI Plate' }),
  329. );
  330. });
  331. });
  332. it('omits bed_type when the user leaves it on Auto (no override)', async () => {
  333. const onClose = vi.fn();
  334. mockApi.sliceLibraryFile.mockResolvedValue({
  335. job_id: 42,
  336. status: 'pending',
  337. status_url: '/api/v1/slice-jobs/42',
  338. });
  339. renderWithTracker({
  340. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  341. onClose,
  342. });
  343. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  344. const user = userEvent.setup();
  345. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  346. await waitFor(() => {
  347. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  348. expect(body).not.toHaveProperty('bed_type');
  349. });
  350. });
  351. it('lets the user override the default and pick a Standard preset', async () => {
  352. const onClose = vi.fn();
  353. mockApi.sliceLibraryFile.mockResolvedValue({
  354. job_id: 42,
  355. status: 'pending',
  356. status_url: '/api/v1/slice-jobs/42',
  357. });
  358. renderWithTracker({
  359. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  360. onClose,
  361. });
  362. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  363. const user = userEvent.setup();
  364. const selects = presetSelects();
  365. await user.selectOptions(selects[0], 'standard:Bambu Lab X1 Carbon 0.4 nozzle');
  366. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  367. await waitFor(() => {
  368. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  369. 100,
  370. expect.objectContaining({
  371. printer_preset: { source: 'standard', id: 'Bambu Lab X1 Carbon 0.4 nozzle' },
  372. }),
  373. );
  374. });
  375. });
  376. it('routes archive sources to sliceArchive instead of sliceLibraryFile', async () => {
  377. const onClose = vi.fn();
  378. mockApi.sliceArchive.mockResolvedValue({
  379. job_id: 7,
  380. status: 'pending',
  381. status_url: '/api/v1/slice-jobs/7',
  382. });
  383. renderWithTracker({
  384. source: { kind: 'archive', id: 86, filename: 'orca.3mf' },
  385. onClose,
  386. });
  387. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  388. const user = userEvent.setup();
  389. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  390. await waitFor(() => {
  391. expect(mockApi.sliceArchive).toHaveBeenCalledWith(86, expect.any(Object));
  392. expect(mockApi.sliceLibraryFile).not.toHaveBeenCalled();
  393. });
  394. });
  395. it('surfaces enqueue errors inline and keeps the modal open', async () => {
  396. const onClose = vi.fn();
  397. mockApi.sliceLibraryFile.mockRejectedValue(new Error('Server says no'));
  398. renderWithTracker({
  399. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  400. onClose,
  401. });
  402. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  403. const user = userEvent.setup();
  404. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  405. await waitFor(() => {
  406. expect(screen.getByRole('alert')).toHaveTextContent('Server says no');
  407. });
  408. expect(onClose).not.toHaveBeenCalled();
  409. });
  410. it('shows a friendly notice when getSlicerPresets fails', async () => {
  411. mockApi.getSlicerPresets.mockRejectedValue(new Error('500'));
  412. renderWithTracker({
  413. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  414. onClose: vi.fn(),
  415. });
  416. await waitFor(() => {
  417. expect(screen.getByRole('alert')).toHaveTextContent(/Failed to load presets/i);
  418. });
  419. });
  420. it('omits the cloud banner when status is not_authenticated (#1712)', async () => {
  421. // A signed-out user (Bambu or Orca) shouldn't get a permanent "sign in"
  422. // nag at the top of every slice. Sign-in lives on the Profiles page; the
  423. // modal stays silent unless a previously-signed-in session actually broke
  424. // (expired / unreachable).
  425. mockApi.getSlicerPresets.mockResolvedValue(
  426. makeUnified({
  427. cloud_status: 'not_authenticated',
  428. orca_cloud_status: 'not_authenticated',
  429. local: fullThreeTier.local,
  430. standard: fullThreeTier.standard,
  431. }),
  432. );
  433. renderWithTracker({
  434. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  435. onClose: vi.fn(),
  436. });
  437. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  438. expect(screen.queryByRole('status')).toBeNull();
  439. });
  440. it('renders an "expired" banner when cloud_status is expired', async () => {
  441. mockApi.getSlicerPresets.mockResolvedValue(
  442. makeUnified({
  443. cloud_status: 'expired',
  444. local: fullThreeTier.local,
  445. }),
  446. );
  447. renderWithTracker({
  448. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  449. onClose: vi.fn(),
  450. });
  451. await waitFor(() => {
  452. expect(screen.getByRole('status')).toHaveTextContent(/expired/i);
  453. });
  454. });
  455. it('omits the banner entirely when cloud_status is ok', async () => {
  456. renderWithTracker({
  457. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  458. onClose: vi.fn(),
  459. });
  460. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  461. // No status-role banner should be rendered on the happy path.
  462. expect(screen.queryByRole('status')).toBeNull();
  463. });
  464. // ----- Multi-plate flow -----------------------------------------------
  465. function makeMultiPlateLibraryResponse() {
  466. return {
  467. file_id: 100,
  468. filename: 'Multi.3mf',
  469. is_multi_plate: true,
  470. plates: [
  471. {
  472. index: 1,
  473. name: 'Plate 1',
  474. objects: ['Cube'],
  475. object_count: 1,
  476. has_thumbnail: false,
  477. thumbnail_url: null,
  478. print_time_seconds: 600,
  479. filament_used_grams: 10,
  480. filaments: [],
  481. },
  482. {
  483. index: 2,
  484. name: 'Plate 2',
  485. objects: ['Pyramid'],
  486. object_count: 1,
  487. has_thumbnail: false,
  488. thumbnail_url: null,
  489. print_time_seconds: 800,
  490. filament_used_grams: 12,
  491. filaments: [],
  492. },
  493. ],
  494. };
  495. }
  496. it('shows the plate picker first for multi-plate library files', async () => {
  497. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  498. renderWithTracker({
  499. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  500. onClose: vi.fn(),
  501. });
  502. // Plate picker renders one button per plate — the accessible name
  503. // joins the heading ("Plate N — name") with the object summary line.
  504. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  505. expect(screen.getByRole('button', { name: /Plate 2.*Pyramid/ })).toBeDefined();
  506. // Profile dropdowns must NOT be visible yet — the user has to pick a
  507. // plate first.
  508. expect(screen.queryByRole('combobox')).toBeNull();
  509. });
  510. it('skips the plate picker for single-plate sources', async () => {
  511. mockApi.getLibraryFilePlates.mockResolvedValue({
  512. file_id: 100,
  513. filename: 'Single.3mf',
  514. is_multi_plate: false,
  515. plates: [
  516. {
  517. index: 1,
  518. name: 'Plate 1',
  519. objects: [],
  520. has_thumbnail: false,
  521. thumbnail_url: null,
  522. print_time_seconds: null,
  523. filament_used_grams: null,
  524. filaments: [],
  525. },
  526. ],
  527. });
  528. renderWithTracker({
  529. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  530. onClose: vi.fn(),
  531. });
  532. // Should jump straight to the profile dropdowns.
  533. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  534. });
  535. it('passes the picked plate to the slice request', async () => {
  536. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  537. mockApi.sliceLibraryFile.mockResolvedValue({
  538. job_id: 42,
  539. status: 'pending',
  540. status_url: '/api/v1/slice-jobs/42',
  541. });
  542. renderWithTracker({
  543. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  544. onClose: vi.fn(),
  545. });
  546. const user = userEvent.setup();
  547. // Step 1: pick Plate 2.
  548. const plate2Button = await screen.findByRole('button', { name: /Plate 2.*Pyramid/ });
  549. await user.click(plate2Button);
  550. // Step 2: profile dropdowns are now visible.
  551. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  552. // Step 3: submit and verify the plate index made it into the body.
  553. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  554. await waitFor(() => {
  555. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  556. 100,
  557. expect.objectContaining({ plate: 2 }),
  558. );
  559. });
  560. });
  561. it('"Slice all plates" toggle sends plate=0 sentinel to the backend (#1493)', async () => {
  562. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  563. mockApi.sliceLibraryFile.mockResolvedValue({
  564. job_id: 42,
  565. status: 'pending',
  566. status_url: '/api/v1/slice-jobs/42',
  567. });
  568. renderWithTracker({
  569. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  570. onClose: vi.fn(),
  571. });
  572. const user = userEvent.setup();
  573. const plate1Button = await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  574. await user.click(plate1Button);
  575. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  576. // The "Slice all plates" checkbox only appears for multi-plate sources.
  577. const toggle = await screen.findByRole('checkbox', { name: /Slice all 2 plates/i });
  578. await user.click(toggle);
  579. // The action button's label flips to the "Slice all" form. Click it.
  580. await user.click(screen.getByRole('button', { name: /Slice all 2 plates/i }));
  581. await waitFor(() => {
  582. expect(mockApi.sliceLibraryFile).toHaveBeenCalledTimes(1);
  583. });
  584. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  585. // ``plate=0`` is the BS CLI's all-plates sentinel — one slice call,
  586. // one output 3MF with every plate's gcode inside, one archive.
  587. expect((body as { plate?: number }).plate).toBe(0);
  588. });
  589. it('"Slice all plates" toggle is hidden for single-plate sources', async () => {
  590. mockApi.getLibraryFilePlates.mockResolvedValue({
  591. file_id: 100,
  592. filename: 'Single.3mf',
  593. is_multi_plate: false,
  594. plates: [
  595. {
  596. index: 1,
  597. name: 'Plate 1',
  598. objects: [],
  599. has_thumbnail: false,
  600. thumbnail_url: null,
  601. print_time_seconds: null,
  602. filament_used_grams: null,
  603. filaments: [],
  604. },
  605. ],
  606. });
  607. renderWithTracker({
  608. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  609. onClose: vi.fn(),
  610. });
  611. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  612. expect(screen.queryByRole('checkbox', { name: /Slice all/i })).toBeNull();
  613. });
  614. it('routes the plate fetch through getArchivePlates for archive sources', async () => {
  615. mockApi.getArchivePlates.mockResolvedValue({
  616. ...makeMultiPlateLibraryResponse(),
  617. archive_id: 100,
  618. filename: 'Multi.3mf',
  619. });
  620. renderWithTracker({
  621. source: { kind: 'archive', id: 100, filename: 'Multi.3mf' },
  622. onClose: vi.fn(),
  623. });
  624. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  625. expect(mockApi.getArchivePlates).toHaveBeenCalledWith(100);
  626. expect(mockApi.getLibraryFilePlates).not.toHaveBeenCalled();
  627. });
  628. it('cancelling the plate picker closes the entire slice flow', async () => {
  629. const onClose = vi.fn();
  630. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  631. renderWithTracker({
  632. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  633. onClose,
  634. });
  635. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  636. const user = userEvent.setup();
  637. await user.click(screen.getByRole('button', { name: /^Close$/i }));
  638. expect(onClose).toHaveBeenCalled();
  639. });
  640. it('omits the plate field when the source is single-plate', async () => {
  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: 'Cube.stl' },
  648. onClose: vi.fn(),
  649. });
  650. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  651. const user = userEvent.setup();
  652. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  653. await waitFor(() => {
  654. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  655. expect(body).not.toHaveProperty('plate');
  656. });
  657. });
  658. // ----- Multi-color flow ------------------------------------------------
  659. function makeMultiColorPlateResponse() {
  660. // Single-plate 3MF that uses two filament slots — mirrors the realistic
  661. // "I have a multi-color file with one plate" case. Multi-plate is a
  662. // separate axis that's already covered above.
  663. return {
  664. file_id: 100,
  665. filename: 'TwoColor.3mf',
  666. is_multi_plate: false,
  667. plates: [
  668. {
  669. index: 1,
  670. name: 'Plate 1',
  671. objects: ['Logo'],
  672. object_count: 1,
  673. has_thumbnail: false,
  674. thumbnail_url: null,
  675. print_time_seconds: 600,
  676. filament_used_grams: 20,
  677. filaments: [],
  678. },
  679. ],
  680. };
  681. }
  682. function makeMultiColorRequirementsResponse() {
  683. return {
  684. file_id: 100,
  685. filename: 'TwoColor.3mf',
  686. plate_id: 1,
  687. filaments: [
  688. { slot_id: 1, type: 'PLA', color: '#000000', used_grams: 10, used_meters: 3 },
  689. { slot_id: 2, type: 'PLA', color: '#FFFFFF', used_grams: 10, used_meters: 3 },
  690. ],
  691. };
  692. }
  693. function makeColorAwarePresets(): UnifiedPresetsResponse {
  694. // Two filament presets in cloud: one black PLA, one white PLA. Pre-pick
  695. // should match each plate slot to the same-colour preset so the user
  696. // doesn't have to manually align them.
  697. return {
  698. orca_cloud: { printer: [], process: [], filament: [] },
  699. cloud: {
  700. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  701. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  702. filament: [
  703. { id: 'F-BLACK', name: 'Cloud PLA Black', source: 'cloud', filament_type: 'PLA', filament_colour: '#000000' },
  704. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  705. ],
  706. },
  707. local: { printer: [], process: [], filament: [] },
  708. standard: { printer: [], process: [], filament: [] },
  709. cloud_status: 'ok',
  710. orca_cloud_status: 'ok',
  711. };
  712. }
  713. it('renders one filament dropdown per plate slot when the source is multi-color', async () => {
  714. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  715. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  716. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  717. renderWithTracker({
  718. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  719. onClose: vi.fn(),
  720. });
  721. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  722. // 1 printer + 1 process + 2 filament + 1 bed-type (#1337) = 5 dropdowns.
  723. expect(presetSelects()).toHaveLength(5);
  724. });
  725. it('pre-picks each filament slot by matching colour metadata', async () => {
  726. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  727. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  728. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  729. mockApi.sliceLibraryFile.mockResolvedValue({
  730. job_id: 42,
  731. status: 'pending',
  732. status_url: '/api/v1/slice-jobs/42',
  733. });
  734. renderWithTracker({
  735. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  736. onClose: vi.fn(),
  737. });
  738. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  739. const user = userEvent.setup();
  740. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  741. await waitFor(() => {
  742. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  743. // Slot 1 was black plate → cloud black preset; slot 2 was white →
  744. // cloud white preset. Pre-pick aligns them by metadata so the user
  745. // doesn't have to swap them manually.
  746. expect(body.filament_presets).toEqual([
  747. { source: 'cloud', id: 'F-BLACK' },
  748. { source: 'cloud', id: 'F-WHITE' },
  749. ]);
  750. });
  751. });
  752. it('still sends the legacy filament_preset for single-color flows', async () => {
  753. // Backwards-compat with backends / proxies that read the singular field.
  754. mockApi.sliceLibraryFile.mockResolvedValue({
  755. job_id: 42,
  756. status: 'pending',
  757. status_url: '/api/v1/slice-jobs/42',
  758. });
  759. renderWithTracker({
  760. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  761. onClose: vi.fn(),
  762. });
  763. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  764. const user = userEvent.setup();
  765. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  766. await waitFor(() => {
  767. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  768. // Single-color path mirrors the array's first entry into the legacy
  769. // singular so older backend clients that only know about
  770. // `filament_preset` still work.
  771. expect(body.filament_preset).toEqual(body.filament_presets[0]);
  772. expect(body.filament_presets).toHaveLength(1);
  773. });
  774. });
  775. it('lets the user override a pre-picked filament slot', async () => {
  776. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  777. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  778. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  779. mockApi.sliceLibraryFile.mockResolvedValue({
  780. job_id: 42,
  781. status: 'pending',
  782. status_url: '/api/v1/slice-jobs/42',
  783. });
  784. renderWithTracker({
  785. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  786. onClose: vi.fn(),
  787. });
  788. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  789. const user = userEvent.setup();
  790. const selects = presetSelects();
  791. // Order: 0 printer, 1 process, 2 bed-type, 3 filament-1, 4 filament-2
  792. // (#1337). Auto-picks land on printer/process/filaments; bed-type
  793. // defaults to "". Swap filament-1 (index 3) from the auto-picked black
  794. // to white.
  795. await user.selectOptions(selects[3], 'cloud:F-WHITE');
  796. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  797. await waitFor(() => {
  798. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  799. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  800. // Slot 1 stayed at the auto-picked white.
  801. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  802. });
  803. });
  804. // Cross-printer re-slicing is a normal, supported operation as of
  805. // 2026-05-20 (Step 0 empirical test: sidecar overrides printer / process
  806. // / bed / kinematics from the picked profile triplet, producing valid
  807. // target-printer G-code). No banner, no warning — the picker UI already
  808. // shows which printer the user picked, and that's enough.
  809. it('does not surface any cross-printer banner and keeps Slice enabled when models differ', async () => {
  810. mockApi.getLibraryFilePlates.mockResolvedValue({
  811. file_id: 100,
  812. filename: 'A1Original.3mf',
  813. is_multi_plate: false,
  814. plates: [
  815. {
  816. index: 1,
  817. name: 'Plate 1',
  818. objects: [],
  819. has_thumbnail: false,
  820. thumbnail_url: null,
  821. print_time_seconds: null,
  822. filament_used_grams: null,
  823. filaments: [],
  824. },
  825. ],
  826. });
  827. // Standard tier offers an X1C profile — the user picks (auto-picks) it.
  828. mockApi.getSlicerPresets.mockResolvedValue(makeUnified({
  829. standard: {
  830. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  831. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  832. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  833. },
  834. }));
  835. renderWithTracker({
  836. source: { kind: 'libraryFile', id: 100, filename: 'A1Original.3mf' },
  837. onClose: vi.fn(),
  838. });
  839. await waitFor(() =>
  840. expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined(),
  841. );
  842. // No banner, no alert — re-slicing across printers is just a normal slice now.
  843. expect(screen.queryByRole('alert')).toBeNull();
  844. const sliceButton = screen.getByRole('button', { name: /^Slice$/ }) as HTMLButtonElement;
  845. expect(sliceButton.disabled).toBe(false);
  846. });
  847. // The `used_in_plate` flag tells the modal which AMS slots are
  848. // actually consumed by the picked plate. Slots flagged as unused
  849. // are still rendered (the slicer CLI needs a profile per project
  850. // slot, otherwise it silently fills the gap from embedded defaults
  851. // and unwanted colours leak into the output) but disabled in the UI
  852. // so the user only interacts with the dropdowns that matter.
  853. it('disables filament dropdowns for slots not used by the picked plate', async () => {
  854. mockApi.getLibraryFilePlates.mockResolvedValue({
  855. file_id: 100,
  856. filename: 'Helmet.3mf',
  857. is_multi_plate: false,
  858. plates: [
  859. {
  860. index: 1,
  861. name: 'Plate 1',
  862. objects: ['Helmet'],
  863. has_thumbnail: false,
  864. thumbnail_url: null,
  865. print_time_seconds: 1200,
  866. filament_used_grams: 80,
  867. filaments: [],
  868. },
  869. ],
  870. });
  871. // Project has 2 AMS slots configured (white + grey support), but
  872. // plate 1 only paints with white (slot 1). The backend now returns
  873. // BOTH slots with used_in_plate flagging the difference.
  874. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  875. file_id: 100,
  876. filename: 'Helmet.3mf',
  877. plate_id: 1,
  878. filaments: [
  879. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  880. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  881. ],
  882. });
  883. mockApi.getSlicerPresets.mockResolvedValue({
  884. cloud: {
  885. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  886. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  887. filament: [
  888. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  889. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  890. ],
  891. },
  892. local: { printer: [], process: [], filament: [] },
  893. standard: { printer: [], process: [], filament: [] },
  894. cloud_status: 'ok',
  895. orca_cloud: { printer: [], process: [], filament: [] },
  896. orca_cloud_status: 'ok',
  897. });
  898. renderWithTracker({
  899. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  900. onClose: vi.fn(),
  901. });
  902. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  903. // Both filament rows render — 1 printer + 1 process + 1 bed-type +
  904. // 2 filament (#1337) = 5. bed-type sits at index 2, filament slots
  905. // follow at 3 and 4.
  906. const selects = presetSelects();
  907. expect(selects).toHaveLength(5);
  908. // Slot 1 (used) is editable, slot 2 (not used) is disabled.
  909. expect(selects[3].disabled).toBe(false);
  910. expect(selects[4].disabled).toBe(true);
  911. // The disabled row's label calls out why it's disabled.
  912. expect(screen.getByText(/not used by this plate/i)).toBeDefined();
  913. });
  914. it('still sends both filaments to the backend even when one slot is disabled', async () => {
  915. // The auto-pick scoring fills the disabled slot from project
  916. // metadata — the slicer CLI requires a profile for every project
  917. // slot, otherwise it silently fills the gap. The disabled UI is
  918. // purely cosmetic; the wire format must include the full list.
  919. mockApi.getLibraryFilePlates.mockResolvedValue({
  920. file_id: 100,
  921. filename: 'Helmet.3mf',
  922. is_multi_plate: false,
  923. plates: [
  924. {
  925. index: 1,
  926. name: 'Plate 1',
  927. objects: ['Helmet'],
  928. has_thumbnail: false,
  929. thumbnail_url: null,
  930. print_time_seconds: 1200,
  931. filament_used_grams: 80,
  932. filaments: [],
  933. },
  934. ],
  935. });
  936. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  937. file_id: 100,
  938. filename: 'Helmet.3mf',
  939. plate_id: 1,
  940. filaments: [
  941. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  942. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  943. ],
  944. });
  945. mockApi.getSlicerPresets.mockResolvedValue({
  946. cloud: {
  947. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  948. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  949. filament: [
  950. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  951. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  952. ],
  953. },
  954. local: { printer: [], process: [], filament: [] },
  955. standard: { printer: [], process: [], filament: [] },
  956. cloud_status: 'ok',
  957. orca_cloud: { printer: [], process: [], filament: [] },
  958. orca_cloud_status: 'ok',
  959. });
  960. mockApi.sliceLibraryFile.mockResolvedValue({
  961. job_id: 50,
  962. status: 'pending',
  963. status_url: '/api/v1/slice-jobs/50',
  964. });
  965. renderWithTracker({
  966. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  967. onClose: vi.fn(),
  968. });
  969. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  970. const user = userEvent.setup();
  971. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  972. await waitFor(() => {
  973. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  974. // Both slots populated: slot 1 with the user's white pick, slot
  975. // 2 auto-picked with grey from the colour-match scoring.
  976. expect(body.filament_presets).toHaveLength(2);
  977. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  978. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-GREY' });
  979. });
  980. });
  981. // ------------------------------------------------------------------
  982. // Slicer Pipelines (#1425) — Apply / Save integration in SliceModal
  983. // ------------------------------------------------------------------
  984. it('Apply pipeline dropdown is disabled and shows empty hint when no pipelines exist', async () => {
  985. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  986. renderWithTracker({
  987. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  988. onClose: vi.fn(),
  989. });
  990. await waitFor(() => {
  991. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  992. expect(select.disabled).toBe(true);
  993. expect(select.querySelector('option')?.textContent).toMatch(/No saved pipelines/i);
  994. });
  995. });
  996. it('applies a saved pipeline to printer, process, and bed_type slots on selection', async () => {
  997. mockApi.listSlicerPipelines.mockResolvedValue({
  998. pipelines: [
  999. {
  1000. id: 7,
  1001. name: 'Production Batch',
  1002. description: null,
  1003. printer_preset: { source: 'local', id: '1' },
  1004. process_preset: { source: 'local', id: '2' },
  1005. filament_presets: [{ source: 'local', id: '3' }],
  1006. bed_type: 'Textured PEI Plate',
  1007. target_kind: 'printer_class',
  1008. target_printer_id: null,
  1009. target_model_class: null,
  1010. fanout_strategy: 'max_parallel',
  1011. created_by: null,
  1012. created_at: '2026-06-27T00:00:00Z',
  1013. updated_at: '2026-06-27T00:00:00Z',
  1014. },
  1015. ],
  1016. });
  1017. renderWithTracker({
  1018. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1019. onClose: vi.fn(),
  1020. });
  1021. // Wait for presets + pipelines listing to populate the modal.
  1022. await waitFor(() => {
  1023. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  1024. expect(select.disabled).toBe(false);
  1025. expect(within(select).getByText('Production Batch')).toBeDefined();
  1026. });
  1027. const user = userEvent.setup();
  1028. await user.selectOptions(screen.getByLabelText(/Apply pipeline/i), '7');
  1029. // After applying, submitting the slice request should carry the
  1030. // pipeline's preset refs end-to-end.
  1031. mockApi.sliceLibraryFile.mockResolvedValue({
  1032. job_id: 42,
  1033. status: 'queued',
  1034. status_url: '/api/v1/slice-jobs/42',
  1035. });
  1036. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1037. await waitFor(() => {
  1038. expect(mockApi.sliceLibraryFile).toHaveBeenCalled();
  1039. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1040. expect(body.printer_preset).toEqual({ source: 'local', id: '1' });
  1041. expect(body.process_preset).toEqual({ source: 'local', id: '2' });
  1042. expect(body.filament_presets[0]).toEqual({ source: 'local', id: '3' });
  1043. expect(body.bed_type).toBe('Textured PEI Plate');
  1044. });
  1045. });
  1046. it('saves the current four-slot selection as a new pipeline when the user clicks Save as pipeline', async () => {
  1047. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1048. mockApi.createSlicerPipeline.mockResolvedValue({
  1049. id: 99,
  1050. name: 'My Default',
  1051. description: null,
  1052. printer_preset: { source: 'local', id: '1' },
  1053. process_preset: { source: 'local', id: '2' },
  1054. filament_presets: [{ source: 'local', id: '3' }],
  1055. bed_type: null,
  1056. target_kind: 'printer_class',
  1057. target_printer_id: null,
  1058. target_model_class: null,
  1059. fanout_strategy: 'max_parallel',
  1060. created_by: null,
  1061. created_at: '2026-06-27T00:00:00Z',
  1062. updated_at: '2026-06-27T00:00:00Z',
  1063. });
  1064. renderWithTracker({
  1065. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1066. onClose: vi.fn(),
  1067. });
  1068. // Wait for auto-pick to populate all four slots from the fullThreeTier
  1069. // listing — then Save as pipeline becomes enabled.
  1070. const user = userEvent.setup();
  1071. let saveBtn: HTMLButtonElement;
  1072. await waitFor(() => {
  1073. saveBtn = screen.getByRole('button', { name: /^Save as pipeline$/ }) as HTMLButtonElement;
  1074. expect(saveBtn.disabled).toBe(false);
  1075. });
  1076. await user.click(saveBtn!);
  1077. const nameInput = screen.getByLabelText(/New pipeline name/i);
  1078. await user.type(nameInput, 'My Default');
  1079. await user.click(screen.getByRole('button', { name: /^Save$/ }));
  1080. await waitFor(() => {
  1081. expect(mockApi.createSlicerPipeline).toHaveBeenCalledTimes(1);
  1082. const body = mockApi.createSlicerPipeline.mock.calls[0][0];
  1083. expect(body.name).toBe('My Default');
  1084. // The four slots come from the auto-picked unified-presets listing —
  1085. // local tier wins per SLICE_MODAL_TIER_ORDER.
  1086. expect(body.printer_preset.source).toBe('local');
  1087. expect(body.process_preset.source).toBe('local');
  1088. expect(body.filament_presets[0].source).toBe('local');
  1089. });
  1090. });
  1091. });
  1092. // Pure-function tests for the filament slot picker. Pinned as a separate
  1093. // describe so the contract is visible without needing the modal mount.
  1094. describe('pickFilamentForSlot — printer-compat contract (#1851)', () => {
  1095. // Index that recognises @BBL H2C / @BBL A1 tokens via the canonical
  1096. // PRINTER_MODEL_MAP. Real production data comes through
  1097. // ``api.getSlicerPrinterModels`` — the H2C / A1 fragments are the ones
  1098. // the production registry ships.
  1099. const index = buildCompatibilityIndex({
  1100. 'Bambu Lab A1': 'A1',
  1101. 'Bambu Lab H2C': 'H2C',
  1102. });
  1103. it('prefers a printer-compatible preset over a printer-mismatched one even with better colour match', () => {
  1104. // The OP scenario for #1851: a Bambu Lab A1 is selected; the unused-slot
  1105. // requirement carries the original H2C plate's PLA colour. With the
  1106. // legacy soft-penalty scoring an H2C-bound preset whose colour matches
  1107. // exactly could still rise above the A1-compatible PLA Basic whose
  1108. // colour doesn't, and then the unused-slot substitution propagated the
  1109. // H2C-bound preset across every unused slot — the CLI rejected with
  1110. // ``filament preset Generic PLA @BBL H2C (slot 1) is not compatible
  1111. // with printer Bambu Lab A1 0.4 nozzle``. The hard-skip contract makes
  1112. // sure a mismatched preset is never chosen while any compatible
  1113. // alternative exists, irrespective of metadata-score arithmetic.
  1114. const presets = makeUnified({
  1115. standard: {
  1116. printer: [],
  1117. process: [],
  1118. filament: [
  1119. {
  1120. id: 'Generic PLA @BBL H2C',
  1121. name: 'Generic PLA @BBL H2C',
  1122. source: 'standard',
  1123. filament_type: 'PLA',
  1124. filament_colour: '#FF0000',
  1125. },
  1126. {
  1127. id: 'Bambu PLA Basic @BBL A1',
  1128. name: 'Bambu PLA Basic @BBL A1',
  1129. source: 'standard',
  1130. filament_type: 'PLA',
  1131. filament_colour: '#FFFFFF',
  1132. },
  1133. ],
  1134. },
  1135. });
  1136. const pick = pickFilamentForSlot(
  1137. presets,
  1138. { type: 'PLA', color: '#FF0000' },
  1139. 'Bambu Lab A1 0.4 nozzle',
  1140. index,
  1141. );
  1142. expect(pick).toEqual({ source: 'standard', id: 'Bambu PLA Basic @BBL A1' });
  1143. });
  1144. it('falls back to a mismatched preset when no compatible alternative exists', () => {
  1145. // Graceful degrade: when every available preset is printer-mismatched,
  1146. // returning ``null`` would block the slice entirely. The picker keeps
  1147. // its old behaviour of returning the best-scoring mismatch so the user
  1148. // sees a populated dropdown they can correct, not an empty one.
  1149. const presets = makeUnified({
  1150. standard: {
  1151. printer: [],
  1152. process: [],
  1153. filament: [
  1154. {
  1155. id: 'Generic PLA @BBL H2C',
  1156. name: 'Generic PLA @BBL H2C',
  1157. source: 'standard',
  1158. filament_type: 'PLA',
  1159. filament_colour: '#FF0000',
  1160. },
  1161. ],
  1162. },
  1163. });
  1164. const pick = pickFilamentForSlot(
  1165. presets,
  1166. { type: 'PLA', color: '#FF0000' },
  1167. 'Bambu Lab A1 0.4 nozzle',
  1168. index,
  1169. );
  1170. expect(pick).toEqual({ source: 'standard', id: 'Generic PLA @BBL H2C' });
  1171. });
  1172. it('treats a no-printer-context call as no-mismatch (every preset eligible)', () => {
  1173. // ``printerName === null`` happens transiently on first render before the
  1174. // printer pre-pick effect has run. ``presetCompatibility`` returns
  1175. // ``unknown`` for every preset in that case, so the picker should just
  1176. // pick by metadata score with no compatibility filter active.
  1177. const presets = makeUnified({
  1178. standard: {
  1179. printer: [],
  1180. process: [],
  1181. filament: [
  1182. {
  1183. id: 'Generic PLA @BBL H2C',
  1184. name: 'Generic PLA @BBL H2C',
  1185. source: 'standard',
  1186. filament_type: 'PLA',
  1187. filament_colour: '#FF0000',
  1188. },
  1189. ],
  1190. },
  1191. });
  1192. const pick = pickFilamentForSlot(
  1193. presets,
  1194. { type: 'PLA', color: '#FF0000' },
  1195. null,
  1196. index,
  1197. );
  1198. expect(pick).toEqual({ source: 'standard', id: 'Generic PLA @BBL H2C' });
  1199. });
  1200. });
  1201. describe('pickFilamentForSlot — long-form printer tag (#2628)', () => {
  1202. const index = buildCompatibilityIndex({
  1203. 'Bambu Lab A1': 'A1',
  1204. 'Bambu Lab H2D': 'H2D',
  1205. });
  1206. it('never auto-picks a user-saved preset scoped to another printer', () => {
  1207. // michaelklos's registry: a cloud-tier user preset carrying the full
  1208. // "@Bambu Lab H2D 0.4 nozzle" tag outscores the A1 preset on tier bonus
  1209. // alone. Until the matcher learned the long form it classified 'unknown'
  1210. // — indistinguishable from compatible — so it won the slot, landed in a
  1211. // dropdown the modal disables (slot not used by the plate), and the CLI
  1212. // rejected the whole slice.
  1213. const presets = makeUnified({
  1214. cloud: {
  1215. printer: [],
  1216. process: [],
  1217. filament: [
  1218. {
  1219. id: 'sunlu-tpu-h2d',
  1220. name: 'SUNLU TPU 95A @Bambu Lab H2D 0.4 nozzle',
  1221. source: 'cloud',
  1222. filament_type: 'PLA',
  1223. filament_colour: '#FF0000',
  1224. },
  1225. ],
  1226. },
  1227. standard: {
  1228. printer: [],
  1229. process: [],
  1230. filament: [
  1231. {
  1232. id: 'Bambu PLA Basic @BBL A1',
  1233. name: 'Bambu PLA Basic @BBL A1',
  1234. source: 'standard',
  1235. filament_type: 'PLA',
  1236. filament_colour: '#FFFFFF',
  1237. },
  1238. ],
  1239. },
  1240. });
  1241. const pick = pickFilamentForSlot(
  1242. presets,
  1243. { type: 'PLA', color: '#FF0000' },
  1244. 'Bambu Lab A1 0.4 nozzle',
  1245. index,
  1246. );
  1247. expect(pick).toEqual({ source: 'standard', id: 'Bambu PLA Basic @BBL A1' });
  1248. });
  1249. it('still picks a long-form preset for its own printer', () => {
  1250. const presets = makeUnified({
  1251. cloud: {
  1252. printer: [],
  1253. process: [],
  1254. filament: [
  1255. {
  1256. id: 'sunlu-tpu-h2d',
  1257. name: 'SUNLU TPU 95A @Bambu Lab H2D 0.4 nozzle',
  1258. source: 'cloud',
  1259. filament_type: 'TPU',
  1260. filament_colour: '#FF0000',
  1261. },
  1262. ],
  1263. },
  1264. });
  1265. const pick = pickFilamentForSlot(
  1266. presets,
  1267. { type: 'TPU', color: '#FF0000' },
  1268. 'Bambu Lab H2D 0.4 nozzle',
  1269. index,
  1270. );
  1271. expect(pick).toEqual({ source: 'cloud', id: 'sunlu-tpu-h2d' });
  1272. });
  1273. });