SliceModal.test.tsx 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  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. // #2622: a MakerWorld 3MF designed for another printer carries the author's
  303. // own process tweaks. BambuStudio records which keys deviate from the stock
  304. // preset inside the file, so a cross-printer re-slice can carry them instead
  305. // of losing them to the picked process profile.
  306. const designedFor = {
  307. file_id: 100,
  308. filename: 'Designed.3mf',
  309. plates: [],
  310. is_multi_plate: false,
  311. embedded_printer: 'Bambu Lab A1 0.4 nozzle',
  312. embedded_process: '0.20mm Standard @BBL A1',
  313. design_overrides: [
  314. { key: 'wall_loops', value: '5', printer_coupled: false },
  315. { key: 'sparse_infill_density', value: '100%', printer_coupled: false },
  316. { key: 'outer_wall_speed', value: '200', printer_coupled: true },
  317. ],
  318. };
  319. async function openDesignSection() {
  320. const user = userEvent.setup();
  321. await user.click(await screen.findByText(/Keep the designer's settings/));
  322. return user;
  323. }
  324. it("carries the design's printer-independent settings by default (#2622)", async () => {
  325. mockApi.sliceLibraryFile.mockResolvedValue({
  326. job_id: 42,
  327. status: 'pending',
  328. status_url: '/api/v1/slice-jobs/42',
  329. });
  330. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  331. renderWithTracker({
  332. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  333. onClose: vi.fn(),
  334. });
  335. // Two of three pre-selected: the speed key is machine-coupled.
  336. expect(await screen.findByText('2 of 3 selected')).toBeInTheDocument();
  337. const user = userEvent.setup();
  338. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  339. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  340. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as { design_overrides?: string[] };
  341. expect([...(payload.design_overrides ?? [])].sort()).toEqual(['sparse_infill_density', 'wall_loops']);
  342. });
  343. it('lists every changed setting with its value and flags the machine-coupled ones (#2622)', async () => {
  344. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  345. renderWithTracker({
  346. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  347. onClose: vi.fn(),
  348. });
  349. await openDesignSection();
  350. expect(screen.getByText('wall_loops')).toBeInTheDocument();
  351. expect(screen.getByText('5')).toBeInTheDocument();
  352. expect(screen.getByText('sparse_infill_density')).toBeInTheDocument();
  353. expect(screen.getByText('100%')).toBeInTheDocument();
  354. // The risky one is listed too — visible, explained, just not pre-ticked.
  355. expect(screen.getByText('outer_wall_speed')).toBeInTheDocument();
  356. expect(screen.getByText('printer-specific')).toBeInTheDocument();
  357. });
  358. it('lets the user opt a machine-coupled setting in and a safe one out (#2622)', async () => {
  359. mockApi.sliceLibraryFile.mockResolvedValue({
  360. job_id: 42,
  361. status: 'pending',
  362. status_url: '/api/v1/slice-jobs/42',
  363. });
  364. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  365. renderWithTracker({
  366. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  367. onClose: vi.fn(),
  368. });
  369. const user = await openDesignSection();
  370. const boxes = screen.getAllByRole('checkbox') as HTMLInputElement[];
  371. const byKey = (key: string) =>
  372. boxes.find((b) => b.closest('label')?.textContent?.includes(key)) as HTMLInputElement;
  373. await user.click(byKey('outer_wall_speed'));
  374. await user.click(byKey('wall_loops'));
  375. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  376. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  377. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as { design_overrides?: string[] };
  378. expect([...(payload.design_overrides ?? [])].sort()).toEqual(['outer_wall_speed', 'sparse_infill_density']);
  379. });
  380. it('omits design_overrides entirely when the user unticks everything (#2622)', async () => {
  381. mockApi.sliceLibraryFile.mockResolvedValue({
  382. job_id: 42,
  383. status: 'pending',
  384. status_url: '/api/v1/slice-jobs/42',
  385. });
  386. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  387. renderWithTracker({
  388. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  389. onClose: vi.fn(),
  390. });
  391. const user = await openDesignSection();
  392. const boxes = screen.getAllByRole('checkbox') as HTMLInputElement[];
  393. for (const box of boxes) {
  394. if (box.checked) await user.click(box);
  395. }
  396. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  397. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  398. expect(mockApi.sliceLibraryFile.mock.calls[0][1]).not.toHaveProperty('design_overrides');
  399. });
  400. it('hides the section for a file that changes nothing (#2622)', async () => {
  401. mockApi.getLibraryFilePlates.mockResolvedValue({ ...designedFor, design_overrides: [] });
  402. renderWithTracker({
  403. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  404. onClose: vi.fn(),
  405. });
  406. await waitFor(() => expect(screen.getByRole('button', { name: /^Slice$/ })).toBeEnabled());
  407. expect(screen.queryByText(/Keep the designer's settings/)).toBeNull();
  408. });
  409. it('includes bed_type in the request when the user picks a non-auto plate (#1337)', async () => {
  410. const onClose = vi.fn();
  411. mockApi.sliceLibraryFile.mockResolvedValue({
  412. job_id: 42,
  413. status: 'pending',
  414. status_url: '/api/v1/slice-jobs/42',
  415. });
  416. renderWithTracker({
  417. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  418. onClose,
  419. });
  420. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  421. const user = userEvent.setup();
  422. // Order with the dropdown now sits between Process and Filament:
  423. // printer (0), process (1), bed-type (2), filament (3+). Find the
  424. // bed-type select by name rather than positional index so this stays
  425. // green if the layout adds another control around it.
  426. const bedSelect = presetSelects().find((el) =>
  427. (el as HTMLSelectElement).options[0]?.textContent?.toLowerCase().includes('auto'),
  428. ) as HTMLSelectElement;
  429. expect(bedSelect).toBeDefined();
  430. await user.selectOptions(bedSelect, 'Textured PEI Plate');
  431. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  432. await waitFor(() => {
  433. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  434. 100,
  435. expect.objectContaining({ bed_type: 'Textured PEI Plate' }),
  436. );
  437. });
  438. });
  439. it('omits bed_type when the user leaves it on Auto (no override)', async () => {
  440. const onClose = vi.fn();
  441. mockApi.sliceLibraryFile.mockResolvedValue({
  442. job_id: 42,
  443. status: 'pending',
  444. status_url: '/api/v1/slice-jobs/42',
  445. });
  446. renderWithTracker({
  447. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  448. onClose,
  449. });
  450. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  451. const user = userEvent.setup();
  452. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  453. await waitFor(() => {
  454. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  455. expect(body).not.toHaveProperty('bed_type');
  456. });
  457. });
  458. it('sends the layout flags only for the boxes the user ticked (#2548)', async () => {
  459. mockApi.sliceLibraryFile.mockResolvedValue({
  460. job_id: 42,
  461. status: 'pending',
  462. status_url: '/api/v1/slice-jobs/42',
  463. });
  464. renderWithTracker({
  465. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  466. onClose: vi.fn(),
  467. });
  468. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  469. const user = userEvent.setup();
  470. await user.click(screen.getByRole('checkbox', { name: /Auto-orient objects/ }));
  471. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  472. await waitFor(() => {
  473. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  474. expect(body).toHaveProperty('auto_orient', true);
  475. // The untouched box is omitted, not sent as false. The sidecar reads
  476. // any present value as truthy, so a literal false would arrange every
  477. // slice — the flag has to travel by absence.
  478. expect(body).not.toHaveProperty('auto_arrange');
  479. });
  480. });
  481. it('omits both layout flags when neither box is ticked (#2548)', async () => {
  482. mockApi.sliceLibraryFile.mockResolvedValue({
  483. job_id: 42,
  484. status: 'pending',
  485. status_url: '/api/v1/slice-jobs/42',
  486. });
  487. renderWithTracker({
  488. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  489. onClose: vi.fn(),
  490. });
  491. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  492. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  493. await waitFor(() => {
  494. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  495. expect(body).not.toHaveProperty('auto_orient');
  496. expect(body).not.toHaveProperty('auto_arrange');
  497. });
  498. });
  499. it('lets the user override the default and pick a Standard preset', async () => {
  500. const onClose = vi.fn();
  501. mockApi.sliceLibraryFile.mockResolvedValue({
  502. job_id: 42,
  503. status: 'pending',
  504. status_url: '/api/v1/slice-jobs/42',
  505. });
  506. renderWithTracker({
  507. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  508. onClose,
  509. });
  510. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  511. const user = userEvent.setup();
  512. const selects = presetSelects();
  513. await user.selectOptions(selects[0], 'standard:Bambu Lab X1 Carbon 0.4 nozzle');
  514. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  515. await waitFor(() => {
  516. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  517. 100,
  518. expect.objectContaining({
  519. printer_preset: { source: 'standard', id: 'Bambu Lab X1 Carbon 0.4 nozzle' },
  520. }),
  521. );
  522. });
  523. });
  524. it('routes archive sources to sliceArchive instead of sliceLibraryFile', async () => {
  525. const onClose = vi.fn();
  526. mockApi.sliceArchive.mockResolvedValue({
  527. job_id: 7,
  528. status: 'pending',
  529. status_url: '/api/v1/slice-jobs/7',
  530. });
  531. renderWithTracker({
  532. source: { kind: 'archive', id: 86, filename: 'orca.3mf' },
  533. onClose,
  534. });
  535. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  536. const user = userEvent.setup();
  537. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  538. await waitFor(() => {
  539. expect(mockApi.sliceArchive).toHaveBeenCalledWith(86, expect.any(Object));
  540. expect(mockApi.sliceLibraryFile).not.toHaveBeenCalled();
  541. });
  542. });
  543. it('surfaces enqueue errors inline and keeps the modal open', async () => {
  544. const onClose = vi.fn();
  545. mockApi.sliceLibraryFile.mockRejectedValue(new Error('Server says no'));
  546. renderWithTracker({
  547. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  548. onClose,
  549. });
  550. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  551. const user = userEvent.setup();
  552. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  553. await waitFor(() => {
  554. expect(screen.getByRole('alert')).toHaveTextContent('Server says no');
  555. });
  556. expect(onClose).not.toHaveBeenCalled();
  557. });
  558. it('shows a friendly notice when getSlicerPresets fails', async () => {
  559. mockApi.getSlicerPresets.mockRejectedValue(new Error('500'));
  560. renderWithTracker({
  561. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  562. onClose: vi.fn(),
  563. });
  564. await waitFor(() => {
  565. expect(screen.getByRole('alert')).toHaveTextContent(/Failed to load presets/i);
  566. });
  567. });
  568. it('omits the cloud banner when status is not_authenticated (#1712)', async () => {
  569. // A signed-out user (Bambu or Orca) shouldn't get a permanent "sign in"
  570. // nag at the top of every slice. Sign-in lives on the Profiles page; the
  571. // modal stays silent unless a previously-signed-in session actually broke
  572. // (expired / unreachable).
  573. mockApi.getSlicerPresets.mockResolvedValue(
  574. makeUnified({
  575. cloud_status: 'not_authenticated',
  576. orca_cloud_status: 'not_authenticated',
  577. local: fullThreeTier.local,
  578. standard: fullThreeTier.standard,
  579. }),
  580. );
  581. renderWithTracker({
  582. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  583. onClose: vi.fn(),
  584. });
  585. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  586. expect(screen.queryByRole('status')).toBeNull();
  587. });
  588. it('renders an "expired" banner when cloud_status is expired', async () => {
  589. mockApi.getSlicerPresets.mockResolvedValue(
  590. makeUnified({
  591. cloud_status: 'expired',
  592. local: fullThreeTier.local,
  593. }),
  594. );
  595. renderWithTracker({
  596. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  597. onClose: vi.fn(),
  598. });
  599. await waitFor(() => {
  600. expect(screen.getByRole('status')).toHaveTextContent(/expired/i);
  601. });
  602. });
  603. it('omits the banner entirely when cloud_status is ok', async () => {
  604. renderWithTracker({
  605. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  606. onClose: vi.fn(),
  607. });
  608. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  609. // No status-role banner should be rendered on the happy path.
  610. expect(screen.queryByRole('status')).toBeNull();
  611. });
  612. // ----- Multi-plate flow -----------------------------------------------
  613. function makeMultiPlateLibraryResponse() {
  614. return {
  615. file_id: 100,
  616. filename: 'Multi.3mf',
  617. is_multi_plate: true,
  618. plates: [
  619. {
  620. index: 1,
  621. name: 'Plate 1',
  622. objects: ['Cube'],
  623. object_count: 1,
  624. has_thumbnail: false,
  625. thumbnail_url: null,
  626. print_time_seconds: 600,
  627. filament_used_grams: 10,
  628. filaments: [],
  629. },
  630. {
  631. index: 2,
  632. name: 'Plate 2',
  633. objects: ['Pyramid'],
  634. object_count: 1,
  635. has_thumbnail: false,
  636. thumbnail_url: null,
  637. print_time_seconds: 800,
  638. filament_used_grams: 12,
  639. filaments: [],
  640. },
  641. ],
  642. };
  643. }
  644. it('shows the plate picker first for multi-plate library files', async () => {
  645. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  646. renderWithTracker({
  647. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  648. onClose: vi.fn(),
  649. });
  650. // Plate picker renders one button per plate — the accessible name
  651. // joins the heading ("Plate N — name") with the object summary line.
  652. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  653. expect(screen.getByRole('button', { name: /Plate 2.*Pyramid/ })).toBeDefined();
  654. // Profile dropdowns must NOT be visible yet — the user has to pick a
  655. // plate first.
  656. expect(screen.queryByRole('combobox')).toBeNull();
  657. });
  658. it('skips the plate picker for single-plate sources', async () => {
  659. mockApi.getLibraryFilePlates.mockResolvedValue({
  660. file_id: 100,
  661. filename: 'Single.3mf',
  662. is_multi_plate: false,
  663. plates: [
  664. {
  665. index: 1,
  666. name: 'Plate 1',
  667. objects: [],
  668. has_thumbnail: false,
  669. thumbnail_url: null,
  670. print_time_seconds: null,
  671. filament_used_grams: null,
  672. filaments: [],
  673. },
  674. ],
  675. });
  676. renderWithTracker({
  677. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  678. onClose: vi.fn(),
  679. });
  680. // Should jump straight to the profile dropdowns.
  681. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  682. });
  683. it('passes the picked plate to the slice request', async () => {
  684. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  685. mockApi.sliceLibraryFile.mockResolvedValue({
  686. job_id: 42,
  687. status: 'pending',
  688. status_url: '/api/v1/slice-jobs/42',
  689. });
  690. renderWithTracker({
  691. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  692. onClose: vi.fn(),
  693. });
  694. const user = userEvent.setup();
  695. // Step 1: pick Plate 2.
  696. const plate2Button = await screen.findByRole('button', { name: /Plate 2.*Pyramid/ });
  697. await user.click(plate2Button);
  698. // Step 2: profile dropdowns are now visible.
  699. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  700. // Step 3: submit and verify the plate index made it into the body.
  701. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  702. await waitFor(() => {
  703. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  704. 100,
  705. expect.objectContaining({ plate: 2 }),
  706. );
  707. });
  708. });
  709. it('"Slice all plates" toggle sends plate=0 sentinel to the backend (#1493)', async () => {
  710. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  711. mockApi.sliceLibraryFile.mockResolvedValue({
  712. job_id: 42,
  713. status: 'pending',
  714. status_url: '/api/v1/slice-jobs/42',
  715. });
  716. renderWithTracker({
  717. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  718. onClose: vi.fn(),
  719. });
  720. const user = userEvent.setup();
  721. const plate1Button = await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  722. await user.click(plate1Button);
  723. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  724. // The "Slice all plates" checkbox only appears for multi-plate sources.
  725. const toggle = await screen.findByRole('checkbox', { name: /Slice all 2 plates/i });
  726. await user.click(toggle);
  727. // The action button's label flips to the "Slice all" form. Click it.
  728. await user.click(screen.getByRole('button', { name: /Slice all 2 plates/i }));
  729. await waitFor(() => {
  730. expect(mockApi.sliceLibraryFile).toHaveBeenCalledTimes(1);
  731. });
  732. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  733. // ``plate=0`` is the BS CLI's all-plates sentinel — one slice call,
  734. // one output 3MF with every plate's gcode inside, one archive.
  735. expect((body as { plate?: number }).plate).toBe(0);
  736. });
  737. it('"Slice all plates" toggle is hidden for single-plate sources', async () => {
  738. mockApi.getLibraryFilePlates.mockResolvedValue({
  739. file_id: 100,
  740. filename: 'Single.3mf',
  741. is_multi_plate: false,
  742. plates: [
  743. {
  744. index: 1,
  745. name: 'Plate 1',
  746. objects: [],
  747. has_thumbnail: false,
  748. thumbnail_url: null,
  749. print_time_seconds: null,
  750. filament_used_grams: null,
  751. filaments: [],
  752. },
  753. ],
  754. });
  755. renderWithTracker({
  756. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  757. onClose: vi.fn(),
  758. });
  759. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  760. expect(screen.queryByRole('checkbox', { name: /Slice all/i })).toBeNull();
  761. });
  762. it('routes the plate fetch through getArchivePlates for archive sources', async () => {
  763. mockApi.getArchivePlates.mockResolvedValue({
  764. ...makeMultiPlateLibraryResponse(),
  765. archive_id: 100,
  766. filename: 'Multi.3mf',
  767. });
  768. renderWithTracker({
  769. source: { kind: 'archive', id: 100, filename: 'Multi.3mf' },
  770. onClose: vi.fn(),
  771. });
  772. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  773. expect(mockApi.getArchivePlates).toHaveBeenCalledWith(100);
  774. expect(mockApi.getLibraryFilePlates).not.toHaveBeenCalled();
  775. });
  776. it('cancelling the plate picker closes the entire slice flow', async () => {
  777. const onClose = vi.fn();
  778. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  779. renderWithTracker({
  780. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  781. onClose,
  782. });
  783. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  784. const user = userEvent.setup();
  785. await user.click(screen.getByRole('button', { name: /^Close$/i }));
  786. expect(onClose).toHaveBeenCalled();
  787. });
  788. it('omits the plate field when the source is single-plate', async () => {
  789. mockApi.sliceLibraryFile.mockResolvedValue({
  790. job_id: 42,
  791. status: 'pending',
  792. status_url: '/api/v1/slice-jobs/42',
  793. });
  794. renderWithTracker({
  795. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  796. onClose: vi.fn(),
  797. });
  798. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  799. const user = userEvent.setup();
  800. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  801. await waitFor(() => {
  802. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  803. expect(body).not.toHaveProperty('plate');
  804. });
  805. });
  806. // ----- Multi-color flow ------------------------------------------------
  807. function makeMultiColorPlateResponse() {
  808. // Single-plate 3MF that uses two filament slots — mirrors the realistic
  809. // "I have a multi-color file with one plate" case. Multi-plate is a
  810. // separate axis that's already covered above.
  811. return {
  812. file_id: 100,
  813. filename: 'TwoColor.3mf',
  814. is_multi_plate: false,
  815. plates: [
  816. {
  817. index: 1,
  818. name: 'Plate 1',
  819. objects: ['Logo'],
  820. object_count: 1,
  821. has_thumbnail: false,
  822. thumbnail_url: null,
  823. print_time_seconds: 600,
  824. filament_used_grams: 20,
  825. filaments: [],
  826. },
  827. ],
  828. };
  829. }
  830. function makeMultiColorRequirementsResponse() {
  831. return {
  832. file_id: 100,
  833. filename: 'TwoColor.3mf',
  834. plate_id: 1,
  835. filaments: [
  836. { slot_id: 1, type: 'PLA', color: '#000000', used_grams: 10, used_meters: 3 },
  837. { slot_id: 2, type: 'PLA', color: '#FFFFFF', used_grams: 10, used_meters: 3 },
  838. ],
  839. };
  840. }
  841. function makeColorAwarePresets(): UnifiedPresetsResponse {
  842. // Two filament presets in cloud: one black PLA, one white PLA. Pre-pick
  843. // should match each plate slot to the same-colour preset so the user
  844. // doesn't have to manually align them.
  845. return {
  846. orca_cloud: { printer: [], process: [], filament: [] },
  847. cloud: {
  848. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  849. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  850. filament: [
  851. { id: 'F-BLACK', name: 'Cloud PLA Black', source: 'cloud', filament_type: 'PLA', filament_colour: '#000000' },
  852. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  853. ],
  854. },
  855. local: { printer: [], process: [], filament: [] },
  856. standard: { printer: [], process: [], filament: [] },
  857. cloud_status: 'ok',
  858. orca_cloud_status: 'ok',
  859. };
  860. }
  861. it('renders one filament dropdown per plate slot when the source is multi-color', async () => {
  862. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  863. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  864. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  865. renderWithTracker({
  866. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  867. onClose: vi.fn(),
  868. });
  869. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  870. // 1 printer + 1 process + 2 filament + 1 bed-type (#1337) = 5 dropdowns.
  871. expect(presetSelects()).toHaveLength(5);
  872. });
  873. it('pre-picks each filament slot by matching colour metadata', async () => {
  874. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  875. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  876. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  877. mockApi.sliceLibraryFile.mockResolvedValue({
  878. job_id: 42,
  879. status: 'pending',
  880. status_url: '/api/v1/slice-jobs/42',
  881. });
  882. renderWithTracker({
  883. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  884. onClose: vi.fn(),
  885. });
  886. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  887. const user = userEvent.setup();
  888. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  889. await waitFor(() => {
  890. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  891. // Slot 1 was black plate → cloud black preset; slot 2 was white →
  892. // cloud white preset. Pre-pick aligns them by metadata so the user
  893. // doesn't have to swap them manually.
  894. expect(body.filament_presets).toEqual([
  895. { source: 'cloud', id: 'F-BLACK' },
  896. { source: 'cloud', id: 'F-WHITE' },
  897. ]);
  898. });
  899. });
  900. it('still sends the legacy filament_preset for single-color flows', async () => {
  901. // Backwards-compat with backends / proxies that read the singular field.
  902. mockApi.sliceLibraryFile.mockResolvedValue({
  903. job_id: 42,
  904. status: 'pending',
  905. status_url: '/api/v1/slice-jobs/42',
  906. });
  907. renderWithTracker({
  908. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  909. onClose: vi.fn(),
  910. });
  911. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  912. const user = userEvent.setup();
  913. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  914. await waitFor(() => {
  915. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  916. // Single-color path mirrors the array's first entry into the legacy
  917. // singular so older backend clients that only know about
  918. // `filament_preset` still work.
  919. expect(body.filament_preset).toEqual(body.filament_presets[0]);
  920. expect(body.filament_presets).toHaveLength(1);
  921. });
  922. });
  923. it('lets the user override a pre-picked filament slot', async () => {
  924. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  925. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  926. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  927. mockApi.sliceLibraryFile.mockResolvedValue({
  928. job_id: 42,
  929. status: 'pending',
  930. status_url: '/api/v1/slice-jobs/42',
  931. });
  932. renderWithTracker({
  933. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  934. onClose: vi.fn(),
  935. });
  936. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  937. const user = userEvent.setup();
  938. const selects = presetSelects();
  939. // Order: 0 printer, 1 process, 2 bed-type, 3 filament-1, 4 filament-2
  940. // (#1337). Auto-picks land on printer/process/filaments; bed-type
  941. // defaults to "". Swap filament-1 (index 3) from the auto-picked black
  942. // to white.
  943. await user.selectOptions(selects[3], 'cloud:F-WHITE');
  944. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  945. await waitFor(() => {
  946. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  947. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  948. // Slot 1 stayed at the auto-picked white.
  949. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  950. });
  951. });
  952. // Cross-printer re-slicing is a normal, supported operation as of
  953. // 2026-05-20 (Step 0 empirical test: sidecar overrides printer / process
  954. // / bed / kinematics from the picked profile triplet, producing valid
  955. // target-printer G-code). No banner, no warning — the picker UI already
  956. // shows which printer the user picked, and that's enough.
  957. it('does not surface any cross-printer banner and keeps Slice enabled when models differ', async () => {
  958. mockApi.getLibraryFilePlates.mockResolvedValue({
  959. file_id: 100,
  960. filename: 'A1Original.3mf',
  961. is_multi_plate: false,
  962. plates: [
  963. {
  964. index: 1,
  965. name: 'Plate 1',
  966. objects: [],
  967. has_thumbnail: false,
  968. thumbnail_url: null,
  969. print_time_seconds: null,
  970. filament_used_grams: null,
  971. filaments: [],
  972. },
  973. ],
  974. });
  975. // Standard tier offers an X1C profile — the user picks (auto-picks) it.
  976. mockApi.getSlicerPresets.mockResolvedValue(makeUnified({
  977. standard: {
  978. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  979. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  980. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  981. },
  982. }));
  983. renderWithTracker({
  984. source: { kind: 'libraryFile', id: 100, filename: 'A1Original.3mf' },
  985. onClose: vi.fn(),
  986. });
  987. await waitFor(() =>
  988. expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined(),
  989. );
  990. // No banner, no alert — re-slicing across printers is just a normal slice now.
  991. expect(screen.queryByRole('alert')).toBeNull();
  992. const sliceButton = screen.getByRole('button', { name: /^Slice$/ }) as HTMLButtonElement;
  993. expect(sliceButton.disabled).toBe(false);
  994. });
  995. // The `used_in_plate` flag tells the modal which AMS slots are
  996. // actually consumed by the picked plate. Slots flagged as unused
  997. // are still rendered (the slicer CLI needs a profile per project
  998. // slot, otherwise it silently fills the gap from embedded defaults
  999. // and unwanted colours leak into the output) but disabled in the UI
  1000. // so the user only interacts with the dropdowns that matter.
  1001. it('disables filament dropdowns for slots not used by the picked plate', async () => {
  1002. mockApi.getLibraryFilePlates.mockResolvedValue({
  1003. file_id: 100,
  1004. filename: 'Helmet.3mf',
  1005. is_multi_plate: false,
  1006. plates: [
  1007. {
  1008. index: 1,
  1009. name: 'Plate 1',
  1010. objects: ['Helmet'],
  1011. has_thumbnail: false,
  1012. thumbnail_url: null,
  1013. print_time_seconds: 1200,
  1014. filament_used_grams: 80,
  1015. filaments: [],
  1016. },
  1017. ],
  1018. });
  1019. // Project has 2 AMS slots configured (white + grey support), but
  1020. // plate 1 only paints with white (slot 1). The backend now returns
  1021. // BOTH slots with used_in_plate flagging the difference.
  1022. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1023. file_id: 100,
  1024. filename: 'Helmet.3mf',
  1025. plate_id: 1,
  1026. filaments: [
  1027. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  1028. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  1029. ],
  1030. });
  1031. mockApi.getSlicerPresets.mockResolvedValue({
  1032. cloud: {
  1033. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1034. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1035. filament: [
  1036. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  1037. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  1038. ],
  1039. },
  1040. local: { printer: [], process: [], filament: [] },
  1041. standard: { printer: [], process: [], filament: [] },
  1042. cloud_status: 'ok',
  1043. orca_cloud: { printer: [], process: [], filament: [] },
  1044. orca_cloud_status: 'ok',
  1045. });
  1046. renderWithTracker({
  1047. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  1048. onClose: vi.fn(),
  1049. });
  1050. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1051. // Both filament rows render — 1 printer + 1 process + 1 bed-type +
  1052. // 2 filament (#1337) = 5. bed-type sits at index 2, filament slots
  1053. // follow at 3 and 4.
  1054. const selects = presetSelects();
  1055. expect(selects).toHaveLength(5);
  1056. // Slot 1 (used) is editable, slot 2 (not used) is disabled.
  1057. expect(selects[3].disabled).toBe(false);
  1058. expect(selects[4].disabled).toBe(true);
  1059. // The disabled row's label calls out why it's disabled.
  1060. expect(screen.getByText(/not used by this plate/i)).toBeDefined();
  1061. });
  1062. it('still sends both filaments to the backend even when one slot is disabled', async () => {
  1063. // The auto-pick scoring fills the disabled slot from project
  1064. // metadata — the slicer CLI requires a profile for every project
  1065. // slot, otherwise it silently fills the gap. The disabled UI is
  1066. // purely cosmetic; the wire format must include the full list.
  1067. mockApi.getLibraryFilePlates.mockResolvedValue({
  1068. file_id: 100,
  1069. filename: 'Helmet.3mf',
  1070. is_multi_plate: false,
  1071. plates: [
  1072. {
  1073. index: 1,
  1074. name: 'Plate 1',
  1075. objects: ['Helmet'],
  1076. has_thumbnail: false,
  1077. thumbnail_url: null,
  1078. print_time_seconds: 1200,
  1079. filament_used_grams: 80,
  1080. filaments: [],
  1081. },
  1082. ],
  1083. });
  1084. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1085. file_id: 100,
  1086. filename: 'Helmet.3mf',
  1087. plate_id: 1,
  1088. filaments: [
  1089. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  1090. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  1091. ],
  1092. });
  1093. mockApi.getSlicerPresets.mockResolvedValue({
  1094. cloud: {
  1095. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1096. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1097. filament: [
  1098. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  1099. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  1100. ],
  1101. },
  1102. local: { printer: [], process: [], filament: [] },
  1103. standard: { printer: [], process: [], filament: [] },
  1104. cloud_status: 'ok',
  1105. orca_cloud: { printer: [], process: [], filament: [] },
  1106. orca_cloud_status: 'ok',
  1107. });
  1108. mockApi.sliceLibraryFile.mockResolvedValue({
  1109. job_id: 50,
  1110. status: 'pending',
  1111. status_url: '/api/v1/slice-jobs/50',
  1112. });
  1113. renderWithTracker({
  1114. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  1115. onClose: vi.fn(),
  1116. });
  1117. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1118. const user = userEvent.setup();
  1119. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1120. await waitFor(() => {
  1121. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1122. // Both slots populated: slot 1 with the user's white pick, slot
  1123. // 2 auto-picked with grey from the colour-match scoring.
  1124. expect(body.filament_presets).toHaveLength(2);
  1125. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  1126. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-GREY' });
  1127. });
  1128. });
  1129. // #2712: the filament list is positional the whole way down — index 0 is
  1130. // slot 1, and the backend forwards it as filament_1.json..filament_N.json.
  1131. // A MakerWorld source that ships slice_info and paints with slot 4 alone
  1132. // used to yield a one-row list, so the user's only pick was bound to slot 1
  1133. // and slot 4 sliced with the source's embedded default: picking PETG gave a
  1134. // PLA print. The modal now asks for every project slot so the positions
  1135. // line up.
  1136. it('requests every project slot, not just the ones the plate prints with', async () => {
  1137. renderWithTracker({
  1138. source: { kind: 'libraryFile', id: 100, filename: 'Tunnel.3mf' },
  1139. onClose: vi.fn(),
  1140. });
  1141. await waitFor(() => expect(mockApi.getLibraryFileFilamentRequirements).toHaveBeenCalled());
  1142. const [, plateArg, , fullSlots] = mockApi.getLibraryFileFilamentRequirements.mock.calls[0];
  1143. expect(plateArg).toBe(1);
  1144. expect(fullSlots).toBe(true);
  1145. });
  1146. it('sends the pick for a high-numbered slot at its own index', async () => {
  1147. // Four project slots, only slot 4 printed. The pick must arrive as the
  1148. // FOURTH entry; anywhere else and the slicer binds it to the wrong slot.
  1149. mockApi.getLibraryFilePlates.mockResolvedValue({
  1150. file_id: 100,
  1151. filename: 'Tunnel.3mf',
  1152. is_multi_plate: false,
  1153. plates: [
  1154. {
  1155. index: 1,
  1156. name: 'Plate 1',
  1157. objects: ['Tunnel'],
  1158. has_thumbnail: false,
  1159. thumbnail_url: null,
  1160. print_time_seconds: 1200,
  1161. filament_used_grams: 105,
  1162. filaments: [],
  1163. },
  1164. ],
  1165. });
  1166. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1167. file_id: 100,
  1168. filename: 'Tunnel.3mf',
  1169. plate_id: 1,
  1170. filaments: [
  1171. { slot_id: 1, type: 'PLA', color: '#38CC0A', used_grams: 0, used_meters: 0, used_in_plate: false },
  1172. { slot_id: 2, type: 'PLA', color: '#161616', used_grams: 0, used_meters: 0, used_in_plate: false },
  1173. { slot_id: 3, type: 'PLA', color: '#898989', used_grams: 0, used_meters: 0, used_in_plate: false },
  1174. { slot_id: 4, type: 'PLA', color: '#898989', used_grams: 105, used_meters: 35, used_in_plate: true },
  1175. ],
  1176. });
  1177. mockApi.getSlicerPresets.mockResolvedValue({
  1178. cloud: {
  1179. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1180. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1181. filament: [
  1182. { id: 'F-PLA', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#898989' },
  1183. { id: 'F-PETG', name: 'Cloud PETG', source: 'cloud', filament_type: 'PETG', filament_colour: '#00FF00' },
  1184. ],
  1185. },
  1186. local: { printer: [], process: [], filament: [] },
  1187. standard: { printer: [], process: [], filament: [] },
  1188. cloud_status: 'ok',
  1189. orca_cloud: { printer: [], process: [], filament: [] },
  1190. orca_cloud_status: 'ok',
  1191. });
  1192. mockApi.sliceLibraryFile.mockResolvedValue({
  1193. job_id: 51,
  1194. status: 'pending',
  1195. status_url: '/api/v1/slice-jobs/51',
  1196. });
  1197. renderWithTracker({
  1198. source: { kind: 'libraryFile', id: 100, filename: 'Tunnel.3mf' },
  1199. onClose: vi.fn(),
  1200. });
  1201. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1202. // 1 printer + 1 process + 1 bed-type + 4 filament rows.
  1203. const selects = presetSelects();
  1204. expect(selects).toHaveLength(7);
  1205. // Only slot 4 is selectable — the other three are the padding.
  1206. expect([selects[3].disabled, selects[4].disabled, selects[5].disabled]).toEqual([true, true, true]);
  1207. expect(selects[6].disabled).toBe(false);
  1208. const user = userEvent.setup();
  1209. await user.selectOptions(selects[6], 'cloud:F-PETG');
  1210. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1211. await waitFor(() => {
  1212. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1213. expect(body.filament_presets).toHaveLength(4);
  1214. expect(body.filament_presets[3]).toEqual({ source: 'cloud', id: 'F-PETG' });
  1215. });
  1216. });
  1217. // ------------------------------------------------------------------
  1218. // Slicer Pipelines (#1425) — Apply / Save integration in SliceModal
  1219. // ------------------------------------------------------------------
  1220. it('Apply pipeline dropdown is disabled and shows empty hint when no pipelines exist', async () => {
  1221. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1222. renderWithTracker({
  1223. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1224. onClose: vi.fn(),
  1225. });
  1226. await waitFor(() => {
  1227. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  1228. expect(select.disabled).toBe(true);
  1229. expect(select.querySelector('option')?.textContent).toMatch(/No saved pipelines/i);
  1230. });
  1231. });
  1232. it('applies a saved pipeline to printer, process, and bed_type slots on selection', async () => {
  1233. mockApi.listSlicerPipelines.mockResolvedValue({
  1234. pipelines: [
  1235. {
  1236. id: 7,
  1237. name: 'Production Batch',
  1238. description: null,
  1239. printer_preset: { source: 'local', id: '1' },
  1240. process_preset: { source: 'local', id: '2' },
  1241. filament_presets: [{ source: 'local', id: '3' }],
  1242. bed_type: 'Textured PEI Plate',
  1243. target_kind: 'printer_class',
  1244. target_printer_id: null,
  1245. target_model_class: null,
  1246. fanout_strategy: 'max_parallel',
  1247. created_by: null,
  1248. created_at: '2026-06-27T00:00:00Z',
  1249. updated_at: '2026-06-27T00:00:00Z',
  1250. },
  1251. ],
  1252. });
  1253. renderWithTracker({
  1254. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1255. onClose: vi.fn(),
  1256. });
  1257. // Wait for presets + pipelines listing to populate the modal.
  1258. await waitFor(() => {
  1259. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  1260. expect(select.disabled).toBe(false);
  1261. expect(within(select).getByText('Production Batch')).toBeDefined();
  1262. });
  1263. const user = userEvent.setup();
  1264. await user.selectOptions(screen.getByLabelText(/Apply pipeline/i), '7');
  1265. // After applying, submitting the slice request should carry the
  1266. // pipeline's preset refs end-to-end.
  1267. mockApi.sliceLibraryFile.mockResolvedValue({
  1268. job_id: 42,
  1269. status: 'queued',
  1270. status_url: '/api/v1/slice-jobs/42',
  1271. });
  1272. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1273. await waitFor(() => {
  1274. expect(mockApi.sliceLibraryFile).toHaveBeenCalled();
  1275. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1276. expect(body.printer_preset).toEqual({ source: 'local', id: '1' });
  1277. expect(body.process_preset).toEqual({ source: 'local', id: '2' });
  1278. expect(body.filament_presets[0]).toEqual({ source: 'local', id: '3' });
  1279. expect(body.bed_type).toBe('Textured PEI Plate');
  1280. });
  1281. });
  1282. it('saves the current four-slot selection as a new pipeline when the user clicks Save as pipeline', async () => {
  1283. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1284. mockApi.createSlicerPipeline.mockResolvedValue({
  1285. id: 99,
  1286. name: 'My Default',
  1287. description: null,
  1288. printer_preset: { source: 'local', id: '1' },
  1289. process_preset: { source: 'local', id: '2' },
  1290. filament_presets: [{ source: 'local', id: '3' }],
  1291. bed_type: null,
  1292. target_kind: 'printer_class',
  1293. target_printer_id: null,
  1294. target_model_class: null,
  1295. fanout_strategy: 'max_parallel',
  1296. created_by: null,
  1297. created_at: '2026-06-27T00:00:00Z',
  1298. updated_at: '2026-06-27T00:00:00Z',
  1299. });
  1300. renderWithTracker({
  1301. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1302. onClose: vi.fn(),
  1303. });
  1304. // Wait for auto-pick to populate all four slots from the fullThreeTier
  1305. // listing — then Save as pipeline becomes enabled.
  1306. const user = userEvent.setup();
  1307. let saveBtn: HTMLButtonElement;
  1308. await waitFor(() => {
  1309. saveBtn = screen.getByRole('button', { name: /^Save as pipeline$/ }) as HTMLButtonElement;
  1310. expect(saveBtn.disabled).toBe(false);
  1311. });
  1312. await user.click(saveBtn!);
  1313. const nameInput = screen.getByLabelText(/New pipeline name/i);
  1314. await user.type(nameInput, 'My Default');
  1315. await user.click(screen.getByRole('button', { name: /^Save$/ }));
  1316. await waitFor(() => {
  1317. expect(mockApi.createSlicerPipeline).toHaveBeenCalledTimes(1);
  1318. const body = mockApi.createSlicerPipeline.mock.calls[0][0];
  1319. expect(body.name).toBe('My Default');
  1320. // The four slots come from the auto-picked unified-presets listing —
  1321. // local tier wins per SLICE_MODAL_TIER_ORDER.
  1322. expect(body.printer_preset.source).toBe('local');
  1323. expect(body.process_preset.source).toBe('local');
  1324. expect(body.filament_presets[0].source).toBe('local');
  1325. });
  1326. });
  1327. });
  1328. // Pure-function tests for the filament slot picker. Pinned as a separate
  1329. // describe so the contract is visible without needing the modal mount.
  1330. describe('pickFilamentForSlot — printer-compat contract (#1851)', () => {
  1331. // Index that recognises @BBL H2C / @BBL A1 tokens via the canonical
  1332. // PRINTER_MODEL_MAP. Real production data comes through
  1333. // ``api.getSlicerPrinterModels`` — the H2C / A1 fragments are the ones
  1334. // the production registry ships.
  1335. const index = buildCompatibilityIndex({
  1336. 'Bambu Lab A1': 'A1',
  1337. 'Bambu Lab H2C': 'H2C',
  1338. });
  1339. it('prefers a printer-compatible preset over a printer-mismatched one even with better colour match', () => {
  1340. // The OP scenario for #1851: a Bambu Lab A1 is selected; the unused-slot
  1341. // requirement carries the original H2C plate's PLA colour. With the
  1342. // legacy soft-penalty scoring an H2C-bound preset whose colour matches
  1343. // exactly could still rise above the A1-compatible PLA Basic whose
  1344. // colour doesn't, and then the unused-slot substitution propagated the
  1345. // H2C-bound preset across every unused slot — the CLI rejected with
  1346. // ``filament preset Generic PLA @BBL H2C (slot 1) is not compatible
  1347. // with printer Bambu Lab A1 0.4 nozzle``. The hard-skip contract makes
  1348. // sure a mismatched preset is never chosen while any compatible
  1349. // alternative exists, irrespective of metadata-score arithmetic.
  1350. const presets = makeUnified({
  1351. standard: {
  1352. printer: [],
  1353. process: [],
  1354. filament: [
  1355. {
  1356. id: 'Generic PLA @BBL H2C',
  1357. name: 'Generic PLA @BBL H2C',
  1358. source: 'standard',
  1359. filament_type: 'PLA',
  1360. filament_colour: '#FF0000',
  1361. },
  1362. {
  1363. id: 'Bambu PLA Basic @BBL A1',
  1364. name: 'Bambu PLA Basic @BBL A1',
  1365. source: 'standard',
  1366. filament_type: 'PLA',
  1367. filament_colour: '#FFFFFF',
  1368. },
  1369. ],
  1370. },
  1371. });
  1372. const pick = pickFilamentForSlot(
  1373. presets,
  1374. { type: 'PLA', color: '#FF0000' },
  1375. 'Bambu Lab A1 0.4 nozzle',
  1376. index,
  1377. );
  1378. expect(pick).toEqual({ source: 'standard', id: 'Bambu PLA Basic @BBL A1' });
  1379. });
  1380. it('falls back to a mismatched preset when no compatible alternative exists', () => {
  1381. // Graceful degrade: when every available preset is printer-mismatched,
  1382. // returning ``null`` would block the slice entirely. The picker keeps
  1383. // its old behaviour of returning the best-scoring mismatch so the user
  1384. // sees a populated dropdown they can correct, not an empty one.
  1385. const presets = makeUnified({
  1386. standard: {
  1387. printer: [],
  1388. process: [],
  1389. filament: [
  1390. {
  1391. id: 'Generic PLA @BBL H2C',
  1392. name: 'Generic PLA @BBL H2C',
  1393. source: 'standard',
  1394. filament_type: 'PLA',
  1395. filament_colour: '#FF0000',
  1396. },
  1397. ],
  1398. },
  1399. });
  1400. const pick = pickFilamentForSlot(
  1401. presets,
  1402. { type: 'PLA', color: '#FF0000' },
  1403. 'Bambu Lab A1 0.4 nozzle',
  1404. index,
  1405. );
  1406. expect(pick).toEqual({ source: 'standard', id: 'Generic PLA @BBL H2C' });
  1407. });
  1408. it('treats a no-printer-context call as no-mismatch (every preset eligible)', () => {
  1409. // ``printerName === null`` happens transiently on first render before the
  1410. // printer pre-pick effect has run. ``presetCompatibility`` returns
  1411. // ``unknown`` for every preset in that case, so the picker should just
  1412. // pick by metadata score with no compatibility filter active.
  1413. const presets = makeUnified({
  1414. standard: {
  1415. printer: [],
  1416. process: [],
  1417. filament: [
  1418. {
  1419. id: 'Generic PLA @BBL H2C',
  1420. name: 'Generic PLA @BBL H2C',
  1421. source: 'standard',
  1422. filament_type: 'PLA',
  1423. filament_colour: '#FF0000',
  1424. },
  1425. ],
  1426. },
  1427. });
  1428. const pick = pickFilamentForSlot(
  1429. presets,
  1430. { type: 'PLA', color: '#FF0000' },
  1431. null,
  1432. index,
  1433. );
  1434. expect(pick).toEqual({ source: 'standard', id: 'Generic PLA @BBL H2C' });
  1435. });
  1436. });
  1437. describe('pickFilamentForSlot — long-form printer tag (#2628)', () => {
  1438. const index = buildCompatibilityIndex({
  1439. 'Bambu Lab A1': 'A1',
  1440. 'Bambu Lab H2D': 'H2D',
  1441. });
  1442. it('never auto-picks a user-saved preset scoped to another printer', () => {
  1443. // michaelklos's registry: a cloud-tier user preset carrying the full
  1444. // "@Bambu Lab H2D 0.4 nozzle" tag outscores the A1 preset on tier bonus
  1445. // alone. Until the matcher learned the long form it classified 'unknown'
  1446. // — indistinguishable from compatible — so it won the slot, landed in a
  1447. // dropdown the modal disables (slot not used by the plate), and the CLI
  1448. // rejected the whole slice.
  1449. const presets = makeUnified({
  1450. cloud: {
  1451. printer: [],
  1452. process: [],
  1453. filament: [
  1454. {
  1455. id: 'sunlu-tpu-h2d',
  1456. name: 'SUNLU TPU 95A @Bambu Lab H2D 0.4 nozzle',
  1457. source: 'cloud',
  1458. filament_type: 'PLA',
  1459. filament_colour: '#FF0000',
  1460. },
  1461. ],
  1462. },
  1463. standard: {
  1464. printer: [],
  1465. process: [],
  1466. filament: [
  1467. {
  1468. id: 'Bambu PLA Basic @BBL A1',
  1469. name: 'Bambu PLA Basic @BBL A1',
  1470. source: 'standard',
  1471. filament_type: 'PLA',
  1472. filament_colour: '#FFFFFF',
  1473. },
  1474. ],
  1475. },
  1476. });
  1477. const pick = pickFilamentForSlot(
  1478. presets,
  1479. { type: 'PLA', color: '#FF0000' },
  1480. 'Bambu Lab A1 0.4 nozzle',
  1481. index,
  1482. );
  1483. expect(pick).toEqual({ source: 'standard', id: 'Bambu PLA Basic @BBL A1' });
  1484. });
  1485. it('still picks a long-form preset for its own printer', () => {
  1486. const presets = makeUnified({
  1487. cloud: {
  1488. printer: [],
  1489. process: [],
  1490. filament: [
  1491. {
  1492. id: 'sunlu-tpu-h2d',
  1493. name: 'SUNLU TPU 95A @Bambu Lab H2D 0.4 nozzle',
  1494. source: 'cloud',
  1495. filament_type: 'TPU',
  1496. filament_colour: '#FF0000',
  1497. },
  1498. ],
  1499. },
  1500. });
  1501. const pick = pickFilamentForSlot(
  1502. presets,
  1503. { type: 'TPU', color: '#FF0000' },
  1504. 'Bambu Lab H2D 0.4 nozzle',
  1505. index,
  1506. );
  1507. expect(pick).toEqual({ source: 'cloud', id: 'sunlu-tpu-h2d' });
  1508. });
  1509. });