SliceModal.test.tsx 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798
  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, afterEach } from 'vitest';
  11. import { fireEvent, 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. getSlicerPrinterModels: vi.fn(),
  35. getSlicerPresetValues: vi.fn(),
  36. getSlicerLoadedSpools: vi.fn(),
  37. },
  38. }));
  39. const mockApi = api as unknown as {
  40. getSlicerPresets: ReturnType<typeof vi.fn>;
  41. sliceLibraryFile: ReturnType<typeof vi.fn>;
  42. sliceArchive: ReturnType<typeof vi.fn>;
  43. getSliceJob: ReturnType<typeof vi.fn>;
  44. getLibraryFilePlates: ReturnType<typeof vi.fn>;
  45. getArchivePlates: ReturnType<typeof vi.fn>;
  46. getLibraryFileFilamentRequirements: ReturnType<typeof vi.fn>;
  47. getArchiveFilamentRequirements: ReturnType<typeof vi.fn>;
  48. listSlicerPipelines: ReturnType<typeof vi.fn>;
  49. createSlicerPipeline: ReturnType<typeof vi.fn>;
  50. getSlicerPrinterModels: ReturnType<typeof vi.fn>;
  51. getSlicerPresetValues: ReturnType<typeof vi.fn>;
  52. getSlicerLoadedSpools: ReturnType<typeof vi.fn>;
  53. };
  54. function makeUnified(overrides: Partial<UnifiedPresetsResponse> = {}): UnifiedPresetsResponse {
  55. return {
  56. orca_cloud: { printer: [], process: [], filament: [] },
  57. cloud: { printer: [], process: [], filament: [] },
  58. local: { printer: [], process: [], filament: [] },
  59. standard: { printer: [], process: [], filament: [] },
  60. cloud_status: 'ok',
  61. orca_cloud_status: 'ok',
  62. ...overrides,
  63. };
  64. }
  65. const fullThreeTier: UnifiedPresetsResponse = makeUnified({
  66. cloud: {
  67. printer: [{ id: 'PFUcloud-printer', name: 'My Custom X1C', source: 'cloud' }],
  68. process: [{ id: 'PFUcloud-process', name: 'My 0.16mm Tweaked', source: 'cloud' }],
  69. filament: [{ id: 'PFUcloud-filament', name: 'My PLA Black', source: 'cloud' }],
  70. },
  71. local: {
  72. printer: [{ id: '1', name: 'Imported X1C 0.4', source: 'local' }],
  73. process: [{ id: '2', name: 'Imported 0.20mm', source: 'local' }],
  74. filament: [{ id: '3', name: 'Imported PLA Basic', source: 'local' }],
  75. },
  76. standard: {
  77. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  78. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  79. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  80. },
  81. });
  82. function renderWithTracker(props: Parameters<typeof SliceModal>[0]) {
  83. return render(
  84. <SliceJobTrackerProvider>
  85. <SliceModal {...props} />
  86. </SliceJobTrackerProvider>,
  87. );
  88. }
  89. // SliceModal renders one extra combobox for the Slicer Pipelines (#1425)
  90. // "Apply pipeline" dropdown above the preset slots. Tests written before
  91. // pipelines landed assume selects[0] = printer; this helper drops the
  92. // pipeline combobox so those indices stay stable.
  93. function presetSelects(): HTMLSelectElement[] {
  94. return (screen.getAllByRole('combobox') as HTMLSelectElement[]).filter(
  95. (el) => el.getAttribute('aria-label') !== 'Apply pipeline',
  96. );
  97. }
  98. describe('SliceModal', () => {
  99. beforeEach(() => {
  100. vi.clearAllMocks();
  101. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  102. mockApi.getSlicerPresetValues.mockResolvedValue({ resolved: true, values: {}, reason: 'ok' });
  103. mockApi.getSliceJob.mockResolvedValue({
  104. job_id: 42,
  105. status: 'running',
  106. kind: 'library_file',
  107. source_id: 100,
  108. source_name: 'Cube.stl',
  109. created_at: new Date().toISOString(),
  110. started_at: null,
  111. completed_at: null,
  112. });
  113. // Default: single-plate (or non-3MF). Multi-plate tests override this.
  114. mockApi.getLibraryFilePlates.mockResolvedValue({
  115. file_id: 100,
  116. filename: 'Cube.stl',
  117. plates: [],
  118. is_multi_plate: false,
  119. });
  120. mockApi.getArchivePlates.mockResolvedValue({
  121. archive_id: 100,
  122. filename: 'Cube.3mf',
  123. plates: [],
  124. is_multi_plate: false,
  125. });
  126. // Default: no per-plate filament metadata available (mirrors STL or
  127. // unsliced source). Multi-color tests override this.
  128. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  129. file_id: 100,
  130. filename: 'Cube.stl',
  131. plate_id: 1,
  132. filaments: [],
  133. });
  134. mockApi.getArchiveFilamentRequirements.mockResolvedValue({
  135. archive_id: 100,
  136. filename: 'Cube.3mf',
  137. plate_id: 1,
  138. filaments: [],
  139. });
  140. // Default: no saved pipelines. Tests opt in by overriding this.
  141. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  142. // Default: no printer online (#3172), so the dialog's filters do nothing.
  143. mockApi.getSlicerLoadedSpools.mockResolvedValue({ printers: [] });
  144. });
  145. it('auto-selects the highest-priority tier per slot on first load', async () => {
  146. renderWithTracker({
  147. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  148. onClose: vi.fn(),
  149. });
  150. // SliceModal-specific tier priority: imported (local) wins over cloud
  151. // and standard so the user's curated picks come first.
  152. await waitFor(() => {
  153. expect(screen.getByText('My Custom X1C')).toBeDefined();
  154. });
  155. // 4 selects: printer, process, bed-type (#1337), filament. bed-type sits
  156. // between process and filament — it overrides curr_bed_type on the
  157. // process preset so the related controls cluster — and defaults to "".
  158. const selects = presetSelects();
  159. expect(selects).toHaveLength(4);
  160. expect(selects[0].value).toBe('local:1');
  161. expect(selects[1].value).toBe('local:2');
  162. expect(selects[2].value).toBe('');
  163. expect(selects[3].value).toBe('local:3');
  164. // Slice button is enabled because all three slots auto-defaulted and
  165. // the preview-slice query has resolved (mock returns immediately).
  166. const sliceBtn = screen.getByRole('button', { name: /^Slice$/ });
  167. expect((sliceBtn as HTMLButtonElement).disabled).toBe(false);
  168. });
  169. it('renders Imported / Cloud / Standard sections via <optgroup>', async () => {
  170. renderWithTracker({
  171. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  172. onClose: vi.fn(),
  173. });
  174. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  175. const printerSelect = presetSelects()[0];
  176. const groups = printerSelect.querySelectorAll('optgroup');
  177. expect(Array.from(groups).map((g) => g.label)).toEqual([
  178. 'Imported',
  179. 'Bambu Cloud',
  180. 'Standard',
  181. ]);
  182. // Each entry sits inside its own tier's group — pin the assignment so
  183. // a future render-shape change can't quietly mix them. Order matches
  184. // SLICE_MODAL_TIER_ORDER (local → cloud → standard).
  185. const localGroup = groups[0];
  186. expect(within(localGroup as HTMLElement).getByText('Imported X1C 0.4')).toBeDefined();
  187. const cloudGroup = groups[1];
  188. expect(within(cloudGroup as HTMLElement).getByText('My Custom X1C')).toBeDefined();
  189. const standardGroup = groups[2];
  190. expect(within(standardGroup as HTMLElement).getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined();
  191. });
  192. it('falls back to local when cloud is empty (auto-pick respects priority)', async () => {
  193. mockApi.getSlicerPresets.mockResolvedValue(
  194. makeUnified({
  195. local: fullThreeTier.local,
  196. standard: fullThreeTier.standard,
  197. }),
  198. );
  199. renderWithTracker({
  200. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  201. onClose: vi.fn(),
  202. });
  203. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  204. const selects = presetSelects();
  205. expect(selects[0].value).toBe('local:1');
  206. });
  207. it('falls back to standard when both cloud and local are empty', async () => {
  208. mockApi.getSlicerPresets.mockResolvedValue(
  209. makeUnified({ standard: fullThreeTier.standard }),
  210. );
  211. renderWithTracker({
  212. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  213. onClose: vi.fn(),
  214. });
  215. await waitFor(() => expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined());
  216. const selects = presetSelects();
  217. expect(selects[0].value).toBe('standard:Bambu Lab X1 Carbon 0.4 nozzle');
  218. });
  219. it('sends source-aware refs (not legacy bare ints) on submit', async () => {
  220. const onClose = vi.fn();
  221. mockApi.sliceLibraryFile.mockResolvedValue({
  222. job_id: 42,
  223. status: 'pending',
  224. status_url: '/api/v1/slice-jobs/42',
  225. });
  226. renderWithTracker({
  227. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  228. onClose,
  229. });
  230. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  231. const user = userEvent.setup();
  232. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  233. await waitFor(() => {
  234. // SliceModal-specific tier priority puts imported (local) above cloud,
  235. // so the auto-pick lands on the local entries even when a cloud entry
  236. // with the same slot is also available in the listing.
  237. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(100, {
  238. printer_preset: { source: 'local', id: '1' },
  239. process_preset: { source: 'local', id: '2' },
  240. filament_preset: { source: 'local', id: '3' },
  241. filament_presets: [{ source: 'local', id: '3' }],
  242. // An STL has no designed colour and the swatch was not touched, so
  243. // the slot is handed back to the backend's fallback chain rather
  244. // than being pinned to the picker's displayed default (#2977).
  245. filament_colours: [''],
  246. });
  247. });
  248. await waitFor(() => expect(onClose).toHaveBeenCalled());
  249. });
  250. it('offers "use the file\'s built-in settings" when the printer matches the design, and sends the flag (#2611)', async () => {
  251. const onClose = vi.fn();
  252. mockApi.sliceLibraryFile.mockResolvedValue({
  253. job_id: 42,
  254. status: 'pending',
  255. status_url: '/api/v1/slice-jobs/42',
  256. });
  257. // A project 3MF whose embedded printer matches a listed preset — the
  258. // printer pre-pick lands on it, so selectedPrinterName === embedded and
  259. // the "slice as designed" toggle is offered.
  260. mockApi.getLibraryFilePlates.mockResolvedValue({
  261. file_id: 100,
  262. filename: 'Designed.3mf',
  263. plates: [],
  264. is_multi_plate: false,
  265. embedded_printer: 'Bambu Lab X1 Carbon 0.4 nozzle',
  266. embedded_process: '0.20mm Standard',
  267. });
  268. renderWithTracker({
  269. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  270. onClose,
  271. });
  272. const user = userEvent.setup();
  273. const toggle = (await screen.findByLabelText(
  274. /Use the file's built-in settings/,
  275. )) as HTMLInputElement;
  276. expect(toggle.checked).toBe(false);
  277. // All preset dropdowns are live until the toggle is on, then bypassed —
  278. // the printer included, so changing it can't silently drop the mode.
  279. const printerSelect = presetSelects()[0];
  280. const processSelect = presetSelects()[1];
  281. expect(printerSelect.disabled).toBe(false);
  282. expect(processSelect.disabled).toBe(false);
  283. await user.click(toggle);
  284. expect(printerSelect.disabled).toBe(true);
  285. expect(processSelect.disabled).toBe(true);
  286. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  287. await waitFor(() => {
  288. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  289. 100,
  290. expect.objectContaining({ use_embedded_settings: true }),
  291. );
  292. });
  293. await waitFor(() => expect(onClose).toHaveBeenCalled());
  294. });
  295. it('hides the embedded-settings toggle when the picked printer differs from the design (#2611)', async () => {
  296. // Embedded target is a model with no matching preset in the listing, so
  297. // the printer pre-pick falls back to the local default (Imported X1C),
  298. // which does not match — honouring embedded settings would risk the
  299. // wrong bed, so the toggle stays hidden.
  300. mockApi.getLibraryFilePlates.mockResolvedValue({
  301. file_id: 100,
  302. filename: 'Designed.3mf',
  303. plates: [],
  304. is_multi_plate: false,
  305. embedded_printer: 'Bambu Lab P1S 0.4 nozzle',
  306. embedded_process: '0.20mm Standard',
  307. });
  308. renderWithTracker({
  309. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  310. onClose: vi.fn(),
  311. });
  312. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  313. expect(screen.queryByLabelText(/Use the file's built-in settings/)).toBeNull();
  314. });
  315. // #2622: a MakerWorld 3MF designed for another printer carries the author's
  316. // own process tweaks. BambuStudio records which keys deviate from the stock
  317. // preset inside the file, so a cross-printer re-slice can carry them instead
  318. // of losing them to the picked process profile.
  319. const designedFor = {
  320. file_id: 100,
  321. filename: 'Designed.3mf',
  322. plates: [],
  323. is_multi_plate: false,
  324. embedded_printer: 'Bambu Lab A1 0.4 nozzle',
  325. embedded_process: '0.20mm Standard @BBL A1',
  326. design_overrides: [
  327. { key: 'wall_loops', value: '5', printer_coupled: false, preset_defining: false },
  328. { key: 'sparse_infill_density', value: '100%', printer_coupled: false, preset_defining: false },
  329. { key: 'outer_wall_speed', value: '200', printer_coupled: true, preset_defining: false },
  330. ],
  331. };
  332. // The designer's settings are shown inside the process-settings panel now,
  333. // against the options they belong to, rather than in a list of their own.
  334. // The payload contract below is unchanged: their *values* still travel as
  335. // design_overrides keys, read from the file by the backend.
  336. async function openDesignSection() {
  337. const user = userEvent.setup();
  338. await user.click(await screen.findByRole('button', { name: /Process settings/ }));
  339. await screen.findByPlaceholderText('Search settings');
  340. // Every designer key must be reachable, including expert-tier ones.
  341. await user.click(screen.getByRole('button', { name: 'Expert' }));
  342. return user;
  343. }
  344. /** The panel's per-option "use the file's value" checkbox, by option key. */
  345. function sourceCheckbox(key: string): HTMLInputElement {
  346. const boxes = screen.getAllByRole('checkbox') as HTMLInputElement[];
  347. const found = boxes.find((b) => (b.getAttribute('aria-label') ?? '').includes(key));
  348. if (!found) throw new Error(`no source checkbox for ${key}`);
  349. return found;
  350. }
  351. it('carries nothing out of the file until it is asked to (#2942)', async () => {
  352. mockApi.sliceLibraryFile.mockResolvedValue({
  353. job_id: 42,
  354. status: 'pending',
  355. status_url: '/api/v1/slice-jobs/42',
  356. });
  357. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  358. renderWithTracker({
  359. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  360. onClose: vi.fn(),
  361. });
  362. // Nothing pre-ticked: "Use the file's built-in settings" is off, so the
  363. // slice runs on the picked preset and the file's own values wait to be
  364. // asked for by name.
  365. await waitFor(() => expect(screen.getByRole('button', { name: /^Slice$/ })).toBeEnabled());
  366. const user = userEvent.setup();
  367. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  368. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  369. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as { design_overrides?: string[] };
  370. // Empty, not absent: the backend reads the difference. A list that is
  371. // there and empty says the user was shown the file's settings and took
  372. // none of them, which also stands the support carry-over down (#1881).
  373. expect(payload.design_overrides).toEqual([]);
  374. });
  375. // The file's layer height is the one deviation that must not ride along: it
  376. // *is* the process preset the user picked, so carrying it silently sliced a
  377. // file at 0.2 while the process dropdown still read "0.08mm High Quality".
  378. const designedWithLayerHeight = {
  379. ...designedFor,
  380. design_overrides: [
  381. { key: 'wall_loops', value: '5', printer_coupled: false, preset_defining: false },
  382. { key: 'layer_height', value: '0.2', printer_coupled: false, preset_defining: true },
  383. ],
  384. };
  385. it("leaves the file's layer height off even in bulk, so the picked preset wins", async () => {
  386. mockApi.sliceLibraryFile.mockResolvedValue({
  387. job_id: 42,
  388. status: 'pending',
  389. status_url: '/api/v1/slice-jobs/42',
  390. });
  391. mockApi.getLibraryFilePlates.mockResolvedValue(designedWithLayerHeight);
  392. renderWithTracker({
  393. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  394. onClose: vi.fn(),
  395. });
  396. await waitFor(() => expect(screen.getByRole('button', { name: /^Slice$/ })).toBeEnabled());
  397. const user = await openDesignSection();
  398. await user.click(screen.getByRole('button', { name: /Use the designer's settings/ }));
  399. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  400. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  401. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as { design_overrides?: string[] };
  402. // The bulk action takes the keys that carry across printers. Layer height
  403. // *is* the preset that was picked, so it stays a per-key decision.
  404. expect(payload.design_overrides).toEqual(['wall_loops']);
  405. });
  406. it('still applies the file\'s layer height when the user ticks it', async () => {
  407. mockApi.sliceLibraryFile.mockResolvedValue({
  408. job_id: 42,
  409. status: 'pending',
  410. status_url: '/api/v1/slice-jobs/42',
  411. });
  412. mockApi.getLibraryFilePlates.mockResolvedValue(designedWithLayerHeight);
  413. renderWithTracker({
  414. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  415. onClose: vi.fn(),
  416. });
  417. await waitFor(() => expect(screen.getByRole('button', { name: /^Slice$/ })).toBeEnabled());
  418. const user = await openDesignSection();
  419. // Search rather than page-hop: it cuts across every page. The tick's
  420. // aria-label carries the option's schema label, not its key.
  421. await user.type(screen.getByPlaceholderText('Search settings'), 'layer height');
  422. await waitFor(() => expect(sourceCheckbox('Layer height')).toBeInTheDocument());
  423. await user.click(sourceCheckbox('Layer height'));
  424. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  425. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  426. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as { design_overrides?: string[] };
  427. expect(payload.design_overrides).toEqual(['layer_height']);
  428. });
  429. it('lists every changed setting with its value and flags the machine-coupled ones (#2622)', async () => {
  430. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  431. renderWithTracker({
  432. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  433. onClose: vi.fn(),
  434. });
  435. const user = await openDesignSection();
  436. // Offered but not taken: the row is flagged and the control still shows
  437. // the baseline, until the tick says the file's value should win.
  438. await user.type(screen.getByPlaceholderText('Search settings'), 'wall loops');
  439. await waitFor(() => expect(sourceCheckbox('Wall loops')).toBeInTheDocument());
  440. expect(sourceCheckbox('Wall loops').checked).toBe(false);
  441. expect(screen.getByLabelText(/^Wall loops/)).not.toHaveValue(5);
  442. await user.click(sourceCheckbox('Wall loops'));
  443. await waitFor(() => expect(screen.getByLabelText(/^Wall loops/)).toHaveValue(5));
  444. await user.clear(screen.getByPlaceholderText('Search settings'));
  445. await user.type(screen.getByPlaceholderText('Search settings'), 'outer wall speed');
  446. // The machine-coupled one is present and flagged, just not pre-ticked.
  447. await waitFor(() => expect(screen.getAllByText("designer's printer").length).toBeGreaterThan(0));
  448. expect(sourceCheckbox('Outer wall').checked).toBe(false);
  449. });
  450. it('lets the user opt a machine-coupled setting in on its own (#2622)', async () => {
  451. mockApi.sliceLibraryFile.mockResolvedValue({
  452. job_id: 42,
  453. status: 'pending',
  454. status_url: '/api/v1/slice-jobs/42',
  455. });
  456. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  457. renderWithTracker({
  458. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  459. onClose: vi.fn(),
  460. });
  461. const user = await openDesignSection();
  462. await user.type(screen.getByPlaceholderText('Search settings'), 'outer wall speed');
  463. await waitFor(() => expect(sourceCheckbox('Outer wall')).toBeInTheDocument());
  464. await user.click(sourceCheckbox('Outer wall'));
  465. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  466. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  467. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as { design_overrides?: string[] };
  468. // Only the key that was asked for -- the two printer-independent ones are
  469. // still on offer and still untouched.
  470. expect(payload.design_overrides).toEqual(['outer_wall_speed']);
  471. });
  472. it('omits design_overrides entirely for a file that offered nothing (#2942)', async () => {
  473. // The other half of the distinction the backend reads: no list at all
  474. // means there was nothing to decide, which leaves #1881's support
  475. // carry-over unconditional for sources -- an OrcaSlicer export, say --
  476. // that record no deviations to tick in the first place.
  477. mockApi.sliceLibraryFile.mockResolvedValue({
  478. job_id: 42,
  479. status: 'pending',
  480. status_url: '/api/v1/slice-jobs/42',
  481. });
  482. mockApi.getLibraryFilePlates.mockResolvedValue({ ...designedFor, design_overrides: [] });
  483. renderWithTracker({
  484. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  485. onClose: vi.fn(),
  486. });
  487. await waitFor(() => expect(screen.getByRole('button', { name: /^Slice$/ })).toBeEnabled());
  488. const user = userEvent.setup();
  489. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  490. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  491. expect(mockApi.sliceLibraryFile.mock.calls[0][1]).not.toHaveProperty('design_overrides');
  492. });
  493. // #2942: the per-key ticks answer the same question the toggle above them
  494. // does -- where do this slice's settings come from -- so one governs the
  495. // other. They used to be pre-ticked whatever it said, which is how a slice
  496. // with the toggle deliberately off still took sixteen values from the file.
  497. const designedForThisPrinter = {
  498. ...designedFor,
  499. embedded_printer: 'Bambu Lab X1 Carbon 0.4 nozzle',
  500. embedded_process: '0.20mm Standard',
  501. // Seam position sits on the panel's opening page at the simple tier, so
  502. // its tick can be read without driving a panel the toggle has disabled.
  503. design_overrides: [
  504. ...designedFor.design_overrides,
  505. { key: 'seam_position', value: 'rear', printer_coupled: false, preset_defining: false },
  506. ],
  507. };
  508. it('shows every setting as coming from the file while the built-in toggle is on (#2942)', async () => {
  509. mockApi.sliceLibraryFile.mockResolvedValue({
  510. job_id: 42,
  511. status: 'pending',
  512. status_url: '/api/v1/slice-jobs/42',
  513. });
  514. mockApi.getLibraryFilePlates.mockResolvedValue(designedForThisPrinter);
  515. renderWithTracker({
  516. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  517. onClose: vi.fn(),
  518. });
  519. const user = userEvent.setup();
  520. await user.click(await screen.findByLabelText(/Use the file's built-in settings/));
  521. await user.click(await screen.findByRole('button', { name: /Process settings/ }));
  522. await screen.findByPlaceholderText('Search settings');
  523. // A readout, not a control: on this path the file drives the whole slice,
  524. // so a tick that said otherwise would be describing the wrong run. The
  525. // panel is inactive throughout -- nothing here is sent.
  526. await waitFor(() => expect(sourceCheckbox('Seam position').checked).toBe(true));
  527. expect(sourceCheckbox('Seam position').disabled).toBe(true);
  528. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  529. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  530. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as {
  531. design_overrides?: string[];
  532. use_embedded_settings?: boolean;
  533. };
  534. expect(payload.use_embedded_settings).toBe(true);
  535. expect(payload).not.toHaveProperty('design_overrides');
  536. });
  537. it('clears them again when the built-in toggle goes back off (#2942)', async () => {
  538. mockApi.sliceLibraryFile.mockResolvedValue({
  539. job_id: 42,
  540. status: 'pending',
  541. status_url: '/api/v1/slice-jobs/42',
  542. });
  543. mockApi.getLibraryFilePlates.mockResolvedValue(designedForThisPrinter);
  544. renderWithTracker({
  545. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  546. onClose: vi.fn(),
  547. });
  548. const user = userEvent.setup();
  549. const toggle = await screen.findByLabelText(/Use the file's built-in settings/);
  550. await user.click(toggle);
  551. await user.click(toggle);
  552. await openDesignSection();
  553. await user.type(screen.getByPlaceholderText('Search settings'), 'wall loops');
  554. await waitFor(() => expect(sourceCheckbox('Wall loops').checked).toBe(false));
  555. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  556. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  557. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as {
  558. design_overrides?: string[];
  559. use_embedded_settings?: boolean;
  560. };
  561. expect(payload.design_overrides).toEqual([]);
  562. expect(payload).not.toHaveProperty('use_embedded_settings');
  563. });
  564. it("takes the designer's settings in bulk, without the machine-coupled ones (#2942)", async () => {
  565. mockApi.sliceLibraryFile.mockResolvedValue({
  566. job_id: 42,
  567. status: 'pending',
  568. status_url: '/api/v1/slice-jobs/42',
  569. });
  570. mockApi.getLibraryFilePlates.mockResolvedValue(designedFor);
  571. renderWithTracker({
  572. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  573. onClose: vi.fn(),
  574. });
  575. const user = await openDesignSection();
  576. // Without this the file's settings would be reachable only by hunting for
  577. // chips across six pages of 348 options.
  578. expect(screen.getByText(/The designer changed 3 process settings/)).toBeInTheDocument();
  579. await user.click(screen.getByRole('button', { name: /Use the designer's settings/ }));
  580. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  581. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  582. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as { design_overrides?: string[] };
  583. expect([...(payload.design_overrides ?? [])].sort()).toEqual([
  584. 'sparse_infill_density',
  585. 'wall_loops',
  586. ]);
  587. });
  588. it('hides the section for a file that changes nothing (#2622)', async () => {
  589. mockApi.getLibraryFilePlates.mockResolvedValue({ ...designedFor, design_overrides: [] });
  590. renderWithTracker({
  591. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  592. onClose: vi.fn(),
  593. });
  594. await waitFor(() => expect(screen.getByRole('button', { name: /^Slice$/ })).toBeEnabled());
  595. // The panel still exists — it is the editor — but nothing is marked as
  596. // coming from the file.
  597. expect(screen.queryByText('from file')).toBeNull();
  598. expect(screen.queryByText("designer's printer")).toBeNull();
  599. });
  600. it('includes bed_type in the request when the user picks a non-auto plate (#1337)', async () => {
  601. const onClose = vi.fn();
  602. mockApi.sliceLibraryFile.mockResolvedValue({
  603. job_id: 42,
  604. status: 'pending',
  605. status_url: '/api/v1/slice-jobs/42',
  606. });
  607. renderWithTracker({
  608. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  609. onClose,
  610. });
  611. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  612. const user = userEvent.setup();
  613. // Order with the dropdown now sits between Process and Filament:
  614. // printer (0), process (1), bed-type (2), filament (3+). Find the
  615. // bed-type select by name rather than positional index so this stays
  616. // green if the layout adds another control around it.
  617. const bedSelect = presetSelects().find((el) =>
  618. (el as HTMLSelectElement).options[0]?.textContent?.toLowerCase().includes('auto'),
  619. ) as HTMLSelectElement;
  620. expect(bedSelect).toBeDefined();
  621. await user.selectOptions(bedSelect, 'Textured PEI Plate');
  622. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  623. await waitFor(() => {
  624. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  625. 100,
  626. expect.objectContaining({ bed_type: 'Textured PEI Plate' }),
  627. );
  628. });
  629. });
  630. it("sends the slicer's own names for the SuperTack and Smooth PEI plates (#3359)", async () => {
  631. mockApi.sliceLibraryFile.mockResolvedValue({
  632. job_id: 42,
  633. status: 'pending',
  634. status_url: '/api/v1/slice-jobs/42',
  635. });
  636. renderWithTracker({
  637. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  638. onClose: vi.fn(),
  639. });
  640. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  641. const bedSelect = presetSelects().find((el) =>
  642. (el as HTMLSelectElement).options[0]?.textContent?.toLowerCase().includes('auto'),
  643. ) as HTMLSelectElement;
  644. // BambuStudio and OrcaSlicer slice any other name as Cool Plate.
  645. expect(Array.from(bedSelect.options).map((o) => o.value)).toEqual([
  646. '',
  647. 'Cool Plate',
  648. 'Supertack Plate',
  649. 'Engineering Plate',
  650. 'High Temp Plate',
  651. 'Textured PEI Plate',
  652. ]);
  653. const user = userEvent.setup();
  654. await user.selectOptions(bedSelect, 'Supertack Plate');
  655. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  656. await waitFor(() => {
  657. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  658. 100,
  659. expect.objectContaining({ bed_type: 'Supertack Plate' }),
  660. );
  661. });
  662. });
  663. it('omits bed_type when the user leaves it on Auto (no override)', async () => {
  664. const onClose = vi.fn();
  665. mockApi.sliceLibraryFile.mockResolvedValue({
  666. job_id: 42,
  667. status: 'pending',
  668. status_url: '/api/v1/slice-jobs/42',
  669. });
  670. renderWithTracker({
  671. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  672. onClose,
  673. });
  674. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  675. const user = userEvent.setup();
  676. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  677. await waitFor(() => {
  678. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  679. expect(body).not.toHaveProperty('bed_type');
  680. });
  681. });
  682. it('sends the layout flags only for the boxes the user ticked (#2548)', async () => {
  683. mockApi.sliceLibraryFile.mockResolvedValue({
  684. job_id: 42,
  685. status: 'pending',
  686. status_url: '/api/v1/slice-jobs/42',
  687. });
  688. renderWithTracker({
  689. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  690. onClose: vi.fn(),
  691. });
  692. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  693. const user = userEvent.setup();
  694. await user.click(screen.getByRole('checkbox', { name: /Auto-orient objects/ }));
  695. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  696. await waitFor(() => {
  697. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  698. expect(body).toHaveProperty('auto_orient', true);
  699. // The untouched box is omitted, not sent as false. The sidecar reads
  700. // any present value as truthy, so a literal false would arrange every
  701. // slice — the flag has to travel by absence.
  702. expect(body).not.toHaveProperty('auto_arrange');
  703. });
  704. });
  705. it('starts with auto-arrange ticked for a freshly combined file', async () => {
  706. mockApi.sliceLibraryFile.mockResolvedValue({
  707. job_id: 42,
  708. status: 'pending',
  709. status_url: '/api/v1/slice-jobs/42',
  710. });
  711. renderWithTracker({
  712. source: { kind: 'libraryFile', id: 100, filename: 'Combined.3mf' },
  713. onClose: vi.fn(),
  714. defaultAutoArrange: true,
  715. });
  716. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  717. expect(screen.getByRole('checkbox', { name: /Auto-arrange/ })).toBeChecked();
  718. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  719. await waitFor(() => {
  720. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  721. expect(body).toHaveProperty('auto_arrange', true);
  722. });
  723. });
  724. it('omits both layout flags when neither box is ticked (#2548)', async () => {
  725. mockApi.sliceLibraryFile.mockResolvedValue({
  726. job_id: 42,
  727. status: 'pending',
  728. status_url: '/api/v1/slice-jobs/42',
  729. });
  730. renderWithTracker({
  731. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  732. onClose: vi.fn(),
  733. });
  734. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  735. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  736. await waitFor(() => {
  737. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  738. expect(body).not.toHaveProperty('auto_orient');
  739. expect(body).not.toHaveProperty('auto_arrange');
  740. });
  741. });
  742. it('lets the user override the default and pick a Standard preset', async () => {
  743. const onClose = vi.fn();
  744. mockApi.sliceLibraryFile.mockResolvedValue({
  745. job_id: 42,
  746. status: 'pending',
  747. status_url: '/api/v1/slice-jobs/42',
  748. });
  749. renderWithTracker({
  750. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  751. onClose,
  752. });
  753. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  754. const user = userEvent.setup();
  755. const selects = presetSelects();
  756. await user.selectOptions(selects[0], 'standard:Bambu Lab X1 Carbon 0.4 nozzle');
  757. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  758. await waitFor(() => {
  759. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  760. 100,
  761. expect.objectContaining({
  762. printer_preset: { source: 'standard', id: 'Bambu Lab X1 Carbon 0.4 nozzle' },
  763. }),
  764. );
  765. });
  766. });
  767. it('routes archive sources to sliceArchive instead of sliceLibraryFile', async () => {
  768. const onClose = vi.fn();
  769. mockApi.sliceArchive.mockResolvedValue({
  770. job_id: 7,
  771. status: 'pending',
  772. status_url: '/api/v1/slice-jobs/7',
  773. });
  774. renderWithTracker({
  775. source: { kind: 'archive', id: 86, filename: 'orca.3mf' },
  776. onClose,
  777. });
  778. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  779. const user = userEvent.setup();
  780. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  781. await waitFor(() => {
  782. expect(mockApi.sliceArchive).toHaveBeenCalledWith(86, expect.any(Object));
  783. expect(mockApi.sliceLibraryFile).not.toHaveBeenCalled();
  784. });
  785. });
  786. it('surfaces enqueue errors inline and keeps the modal open', async () => {
  787. const onClose = vi.fn();
  788. mockApi.sliceLibraryFile.mockRejectedValue(new Error('Server says no'));
  789. renderWithTracker({
  790. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  791. onClose,
  792. });
  793. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  794. const user = userEvent.setup();
  795. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  796. await waitFor(() => {
  797. expect(screen.getByRole('alert')).toHaveTextContent('Server says no');
  798. });
  799. expect(onClose).not.toHaveBeenCalled();
  800. });
  801. it('shows a friendly notice when getSlicerPresets fails', async () => {
  802. mockApi.getSlicerPresets.mockRejectedValue(new Error('500'));
  803. renderWithTracker({
  804. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  805. onClose: vi.fn(),
  806. });
  807. await waitFor(() => {
  808. expect(screen.getByRole('alert')).toHaveTextContent(/Failed to load presets/i);
  809. });
  810. });
  811. it('omits the cloud banner when status is not_authenticated (#1712)', async () => {
  812. // A signed-out user (Bambu or Orca) shouldn't get a permanent "sign in"
  813. // nag at the top of every slice. Sign-in lives on the Profiles page; the
  814. // modal stays silent unless a previously-signed-in session actually broke
  815. // (expired / unreachable).
  816. mockApi.getSlicerPresets.mockResolvedValue(
  817. makeUnified({
  818. cloud_status: 'not_authenticated',
  819. orca_cloud_status: 'not_authenticated',
  820. local: fullThreeTier.local,
  821. standard: fullThreeTier.standard,
  822. }),
  823. );
  824. renderWithTracker({
  825. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  826. onClose: vi.fn(),
  827. });
  828. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  829. expect(screen.queryByRole('status')).toBeNull();
  830. });
  831. it('renders an "expired" banner when cloud_status is expired', async () => {
  832. mockApi.getSlicerPresets.mockResolvedValue(
  833. makeUnified({
  834. cloud_status: 'expired',
  835. local: fullThreeTier.local,
  836. }),
  837. );
  838. renderWithTracker({
  839. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  840. onClose: vi.fn(),
  841. });
  842. await waitFor(() => {
  843. expect(screen.getByRole('status')).toHaveTextContent(/expired/i);
  844. });
  845. });
  846. it('omits the banner entirely when cloud_status is ok', async () => {
  847. renderWithTracker({
  848. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  849. onClose: vi.fn(),
  850. });
  851. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  852. // No status-role banner should be rendered on the happy path.
  853. expect(screen.queryByRole('status')).toBeNull();
  854. });
  855. // ----- Multi-plate flow -----------------------------------------------
  856. function makeMultiPlateLibraryResponse() {
  857. return {
  858. file_id: 100,
  859. filename: 'Multi.3mf',
  860. is_multi_plate: true,
  861. plates: [
  862. {
  863. index: 1,
  864. name: 'Plate 1',
  865. objects: ['Cube'],
  866. object_count: 1,
  867. has_thumbnail: false,
  868. thumbnail_url: null,
  869. print_time_seconds: 600,
  870. filament_used_grams: 10,
  871. filaments: [],
  872. },
  873. {
  874. index: 2,
  875. name: 'Plate 2',
  876. objects: ['Pyramid'],
  877. object_count: 1,
  878. has_thumbnail: false,
  879. thumbnail_url: null,
  880. print_time_seconds: 800,
  881. filament_used_grams: 12,
  882. filaments: [],
  883. },
  884. ],
  885. };
  886. }
  887. it('shows the plate picker first for multi-plate library files', async () => {
  888. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  889. renderWithTracker({
  890. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  891. onClose: vi.fn(),
  892. });
  893. // Plate picker renders one button per plate — the accessible name
  894. // joins the heading ("Plate N — name") with the object summary line.
  895. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  896. expect(screen.getByRole('button', { name: /Plate 2.*Pyramid/ })).toBeDefined();
  897. // Profile dropdowns must NOT be visible yet — the user has to pick a
  898. // plate first.
  899. expect(screen.queryByRole('combobox')).toBeNull();
  900. });
  901. it('skips the plate picker for single-plate sources', async () => {
  902. mockApi.getLibraryFilePlates.mockResolvedValue({
  903. file_id: 100,
  904. filename: 'Single.3mf',
  905. is_multi_plate: false,
  906. plates: [
  907. {
  908. index: 1,
  909. name: 'Plate 1',
  910. objects: [],
  911. has_thumbnail: false,
  912. thumbnail_url: null,
  913. print_time_seconds: null,
  914. filament_used_grams: null,
  915. filaments: [],
  916. },
  917. ],
  918. });
  919. renderWithTracker({
  920. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  921. onClose: vi.fn(),
  922. });
  923. // Should jump straight to the profile dropdowns.
  924. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  925. });
  926. it('passes the picked plate to the slice request', async () => {
  927. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  928. mockApi.sliceLibraryFile.mockResolvedValue({
  929. job_id: 42,
  930. status: 'pending',
  931. status_url: '/api/v1/slice-jobs/42',
  932. });
  933. renderWithTracker({
  934. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  935. onClose: vi.fn(),
  936. });
  937. const user = userEvent.setup();
  938. // Step 1: pick Plate 2.
  939. const plate2Button = await screen.findByRole('button', { name: /Plate 2.*Pyramid/ });
  940. await user.click(plate2Button);
  941. // Step 2: profile dropdowns are now visible.
  942. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  943. // Step 3: submit and verify the plate index made it into the body.
  944. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  945. await waitFor(() => {
  946. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  947. 100,
  948. expect.objectContaining({ plate: 2 }),
  949. );
  950. });
  951. });
  952. it('"Slice all plates" toggle sends plate=0 sentinel to the backend (#1493)', async () => {
  953. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  954. mockApi.sliceLibraryFile.mockResolvedValue({
  955. job_id: 42,
  956. status: 'pending',
  957. status_url: '/api/v1/slice-jobs/42',
  958. });
  959. renderWithTracker({
  960. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  961. onClose: vi.fn(),
  962. });
  963. const user = userEvent.setup();
  964. const plate1Button = await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  965. await user.click(plate1Button);
  966. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  967. // The "Slice all plates" checkbox only appears for multi-plate sources.
  968. const toggle = await screen.findByRole('checkbox', { name: /Slice all 2 plates/i });
  969. await user.click(toggle);
  970. // The action button's label flips to the "Slice all" form. Click it.
  971. await user.click(screen.getByRole('button', { name: /Slice all 2 plates/i }));
  972. await waitFor(() => {
  973. expect(mockApi.sliceLibraryFile).toHaveBeenCalledTimes(1);
  974. });
  975. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  976. // ``plate=0`` is the BS CLI's all-plates sentinel — one slice call,
  977. // one output 3MF with every plate's gcode inside, one archive.
  978. expect((body as { plate?: number }).plate).toBe(0);
  979. });
  980. it('"Slice all plates" toggle is hidden for single-plate sources', async () => {
  981. mockApi.getLibraryFilePlates.mockResolvedValue({
  982. file_id: 100,
  983. filename: 'Single.3mf',
  984. is_multi_plate: false,
  985. plates: [
  986. {
  987. index: 1,
  988. name: 'Plate 1',
  989. objects: [],
  990. has_thumbnail: false,
  991. thumbnail_url: null,
  992. print_time_seconds: null,
  993. filament_used_grams: null,
  994. filaments: [],
  995. },
  996. ],
  997. });
  998. renderWithTracker({
  999. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  1000. onClose: vi.fn(),
  1001. });
  1002. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1003. expect(screen.queryByRole('checkbox', { name: /Slice all/i })).toBeNull();
  1004. });
  1005. it('routes the plate fetch through getArchivePlates for archive sources', async () => {
  1006. mockApi.getArchivePlates.mockResolvedValue({
  1007. ...makeMultiPlateLibraryResponse(),
  1008. archive_id: 100,
  1009. filename: 'Multi.3mf',
  1010. });
  1011. renderWithTracker({
  1012. source: { kind: 'archive', id: 100, filename: 'Multi.3mf' },
  1013. onClose: vi.fn(),
  1014. });
  1015. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  1016. expect(mockApi.getArchivePlates).toHaveBeenCalledWith(100);
  1017. expect(mockApi.getLibraryFilePlates).not.toHaveBeenCalled();
  1018. });
  1019. it('cancelling the plate picker closes the entire slice flow', async () => {
  1020. const onClose = vi.fn();
  1021. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  1022. renderWithTracker({
  1023. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  1024. onClose,
  1025. });
  1026. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  1027. const user = userEvent.setup();
  1028. await user.click(screen.getByRole('button', { name: /^Close$/i }));
  1029. expect(onClose).toHaveBeenCalled();
  1030. });
  1031. it('omits the plate field when the source is single-plate', async () => {
  1032. mockApi.sliceLibraryFile.mockResolvedValue({
  1033. job_id: 42,
  1034. status: 'pending',
  1035. status_url: '/api/v1/slice-jobs/42',
  1036. });
  1037. renderWithTracker({
  1038. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1039. onClose: vi.fn(),
  1040. });
  1041. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1042. const user = userEvent.setup();
  1043. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1044. await waitFor(() => {
  1045. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1046. expect(body).not.toHaveProperty('plate');
  1047. });
  1048. });
  1049. // ----- Multi-color flow ------------------------------------------------
  1050. function makeMultiColorPlateResponse() {
  1051. // Single-plate 3MF that uses two filament slots — mirrors the realistic
  1052. // "I have a multi-color file with one plate" case. Multi-plate is a
  1053. // separate axis that's already covered above.
  1054. return {
  1055. file_id: 100,
  1056. filename: 'TwoColor.3mf',
  1057. is_multi_plate: false,
  1058. plates: [
  1059. {
  1060. index: 1,
  1061. name: 'Plate 1',
  1062. objects: ['Logo'],
  1063. object_count: 1,
  1064. has_thumbnail: false,
  1065. thumbnail_url: null,
  1066. print_time_seconds: 600,
  1067. filament_used_grams: 20,
  1068. filaments: [],
  1069. },
  1070. ],
  1071. };
  1072. }
  1073. function makeMultiColorRequirementsResponse() {
  1074. return {
  1075. file_id: 100,
  1076. filename: 'TwoColor.3mf',
  1077. plate_id: 1,
  1078. filaments: [
  1079. { slot_id: 1, type: 'PLA', color: '#000000', used_grams: 10, used_meters: 3 },
  1080. { slot_id: 2, type: 'PLA', color: '#FFFFFF', used_grams: 10, used_meters: 3 },
  1081. ],
  1082. };
  1083. }
  1084. function makeColorAwarePresets(): UnifiedPresetsResponse {
  1085. // Two filament presets in cloud: one black PLA, one white PLA. Pre-pick
  1086. // should match each plate slot to the same-colour preset so the user
  1087. // doesn't have to manually align them.
  1088. return {
  1089. orca_cloud: { printer: [], process: [], filament: [] },
  1090. cloud: {
  1091. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1092. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1093. filament: [
  1094. { id: 'F-BLACK', name: 'Cloud PLA Black', source: 'cloud', filament_type: 'PLA', filament_colour: '#000000' },
  1095. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  1096. ],
  1097. },
  1098. local: { printer: [], process: [], filament: [] },
  1099. standard: { printer: [], process: [], filament: [] },
  1100. cloud_status: 'ok',
  1101. orca_cloud_status: 'ok',
  1102. };
  1103. }
  1104. it('renders one filament dropdown per plate slot when the source is multi-color', async () => {
  1105. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1106. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1107. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1108. renderWithTracker({
  1109. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1110. onClose: vi.fn(),
  1111. });
  1112. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1113. // 1 printer + 1 process + 2 filament + 1 bed-type (#1337) = 5 dropdowns.
  1114. expect(presetSelects()).toHaveLength(5);
  1115. });
  1116. it('pre-picks each filament slot by matching colour metadata', async () => {
  1117. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1118. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1119. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1120. mockApi.sliceLibraryFile.mockResolvedValue({
  1121. job_id: 42,
  1122. status: 'pending',
  1123. status_url: '/api/v1/slice-jobs/42',
  1124. });
  1125. renderWithTracker({
  1126. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1127. onClose: vi.fn(),
  1128. });
  1129. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1130. const user = userEvent.setup();
  1131. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1132. await waitFor(() => {
  1133. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1134. // Slot 1 was black plate → cloud black preset; slot 2 was white →
  1135. // cloud white preset. Pre-pick aligns them by metadata so the user
  1136. // doesn't have to swap them manually.
  1137. expect(body.filament_presets).toEqual([
  1138. { source: 'cloud', id: 'F-BLACK' },
  1139. { source: 'cloud', id: 'F-WHITE' },
  1140. ]);
  1141. });
  1142. });
  1143. it('still sends the legacy filament_preset for single-color flows', async () => {
  1144. // Backwards-compat with backends / proxies that read the singular field.
  1145. mockApi.sliceLibraryFile.mockResolvedValue({
  1146. job_id: 42,
  1147. status: 'pending',
  1148. status_url: '/api/v1/slice-jobs/42',
  1149. });
  1150. renderWithTracker({
  1151. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1152. onClose: vi.fn(),
  1153. });
  1154. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1155. const user = userEvent.setup();
  1156. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1157. await waitFor(() => {
  1158. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1159. // Single-color path mirrors the array's first entry into the legacy
  1160. // singular so older backend clients that only know about
  1161. // `filament_preset` still work.
  1162. expect(body.filament_preset).toEqual(body.filament_presets[0]);
  1163. expect(body.filament_presets).toHaveLength(1);
  1164. });
  1165. });
  1166. it('lets the user override a pre-picked filament slot', async () => {
  1167. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1168. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1169. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1170. mockApi.sliceLibraryFile.mockResolvedValue({
  1171. job_id: 42,
  1172. status: 'pending',
  1173. status_url: '/api/v1/slice-jobs/42',
  1174. });
  1175. renderWithTracker({
  1176. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1177. onClose: vi.fn(),
  1178. });
  1179. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1180. const user = userEvent.setup();
  1181. const selects = presetSelects();
  1182. // Order: 0 printer, 1 process, 2 bed-type, 3 filament-1, 4 filament-2
  1183. // (#1337). Auto-picks land on printer/process/filaments; bed-type
  1184. // defaults to "". Swap filament-1 (index 3) from the auto-picked black
  1185. // to white.
  1186. await user.selectOptions(selects[3], 'cloud:F-WHITE');
  1187. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1188. await waitFor(() => {
  1189. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1190. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  1191. // Slot 1 stayed at the auto-picked white.
  1192. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  1193. });
  1194. });
  1195. // Cross-printer re-slicing is a normal, supported operation as of
  1196. // 2026-05-20 (Step 0 empirical test: sidecar overrides printer / process
  1197. // / bed / kinematics from the picked profile triplet, producing valid
  1198. // target-printer G-code). No banner, no warning — the picker UI already
  1199. // shows which printer the user picked, and that's enough.
  1200. it('does not surface any cross-printer banner and keeps Slice enabled when models differ', async () => {
  1201. mockApi.getLibraryFilePlates.mockResolvedValue({
  1202. file_id: 100,
  1203. filename: 'A1Original.3mf',
  1204. is_multi_plate: false,
  1205. plates: [
  1206. {
  1207. index: 1,
  1208. name: 'Plate 1',
  1209. objects: [],
  1210. has_thumbnail: false,
  1211. thumbnail_url: null,
  1212. print_time_seconds: null,
  1213. filament_used_grams: null,
  1214. filaments: [],
  1215. },
  1216. ],
  1217. });
  1218. // Standard tier offers an X1C profile — the user picks (auto-picks) it.
  1219. mockApi.getSlicerPresets.mockResolvedValue(makeUnified({
  1220. standard: {
  1221. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  1222. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  1223. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  1224. },
  1225. }));
  1226. renderWithTracker({
  1227. source: { kind: 'libraryFile', id: 100, filename: 'A1Original.3mf' },
  1228. onClose: vi.fn(),
  1229. });
  1230. await waitFor(() =>
  1231. expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined(),
  1232. );
  1233. // No banner, no alert — re-slicing across printers is just a normal slice now.
  1234. expect(screen.queryByRole('alert')).toBeNull();
  1235. const sliceButton = screen.getByRole('button', { name: /^Slice$/ }) as HTMLButtonElement;
  1236. expect(sliceButton.disabled).toBe(false);
  1237. });
  1238. // The `used_in_plate` flag tells the modal which AMS slots are
  1239. // actually consumed by the picked plate. Slots flagged as unused
  1240. // are still rendered (the slicer CLI needs a profile per project
  1241. // slot, otherwise it silently fills the gap from embedded defaults
  1242. // and unwanted colours leak into the output) but disabled in the UI
  1243. // so the user only interacts with the dropdowns that matter.
  1244. it('disables filament dropdowns for slots not used by the picked plate', async () => {
  1245. mockApi.getLibraryFilePlates.mockResolvedValue({
  1246. file_id: 100,
  1247. filename: 'Helmet.3mf',
  1248. is_multi_plate: false,
  1249. plates: [
  1250. {
  1251. index: 1,
  1252. name: 'Plate 1',
  1253. objects: ['Helmet'],
  1254. has_thumbnail: false,
  1255. thumbnail_url: null,
  1256. print_time_seconds: 1200,
  1257. filament_used_grams: 80,
  1258. filaments: [],
  1259. },
  1260. ],
  1261. });
  1262. // Project has 2 AMS slots configured (white + grey support), but
  1263. // plate 1 only paints with white (slot 1). The backend now returns
  1264. // BOTH slots with used_in_plate flagging the difference.
  1265. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1266. file_id: 100,
  1267. filename: 'Helmet.3mf',
  1268. plate_id: 1,
  1269. filaments: [
  1270. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  1271. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  1272. ],
  1273. });
  1274. mockApi.getSlicerPresets.mockResolvedValue({
  1275. cloud: {
  1276. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1277. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1278. filament: [
  1279. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  1280. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  1281. ],
  1282. },
  1283. local: { printer: [], process: [], filament: [] },
  1284. standard: { printer: [], process: [], filament: [] },
  1285. cloud_status: 'ok',
  1286. orca_cloud: { printer: [], process: [], filament: [] },
  1287. orca_cloud_status: 'ok',
  1288. });
  1289. renderWithTracker({
  1290. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  1291. onClose: vi.fn(),
  1292. });
  1293. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1294. // Both filament rows render — 1 printer + 1 process + 1 bed-type +
  1295. // 2 filament (#1337) = 5. bed-type sits at index 2, filament slots
  1296. // follow at 3 and 4.
  1297. const selects = presetSelects();
  1298. expect(selects).toHaveLength(5);
  1299. // Slot 1 (used) is editable, slot 2 (not used) is disabled.
  1300. expect(selects[3].disabled).toBe(false);
  1301. expect(selects[4].disabled).toBe(true);
  1302. // The disabled row's label calls out why it's disabled.
  1303. expect(screen.getByText(/not used by this plate/i)).toBeDefined();
  1304. });
  1305. it('still sends both filaments to the backend even when one slot is disabled', async () => {
  1306. // The auto-pick scoring fills the disabled slot from project
  1307. // metadata — the slicer CLI requires a profile for every project
  1308. // slot, otherwise it silently fills the gap. The disabled UI is
  1309. // purely cosmetic; the wire format must include the full list.
  1310. mockApi.getLibraryFilePlates.mockResolvedValue({
  1311. file_id: 100,
  1312. filename: 'Helmet.3mf',
  1313. is_multi_plate: false,
  1314. plates: [
  1315. {
  1316. index: 1,
  1317. name: 'Plate 1',
  1318. objects: ['Helmet'],
  1319. has_thumbnail: false,
  1320. thumbnail_url: null,
  1321. print_time_seconds: 1200,
  1322. filament_used_grams: 80,
  1323. filaments: [],
  1324. },
  1325. ],
  1326. });
  1327. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1328. file_id: 100,
  1329. filename: 'Helmet.3mf',
  1330. plate_id: 1,
  1331. filaments: [
  1332. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  1333. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  1334. ],
  1335. });
  1336. mockApi.getSlicerPresets.mockResolvedValue({
  1337. cloud: {
  1338. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1339. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1340. filament: [
  1341. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  1342. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  1343. ],
  1344. },
  1345. local: { printer: [], process: [], filament: [] },
  1346. standard: { printer: [], process: [], filament: [] },
  1347. cloud_status: 'ok',
  1348. orca_cloud: { printer: [], process: [], filament: [] },
  1349. orca_cloud_status: 'ok',
  1350. });
  1351. mockApi.sliceLibraryFile.mockResolvedValue({
  1352. job_id: 50,
  1353. status: 'pending',
  1354. status_url: '/api/v1/slice-jobs/50',
  1355. });
  1356. renderWithTracker({
  1357. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  1358. onClose: vi.fn(),
  1359. });
  1360. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1361. const user = userEvent.setup();
  1362. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1363. await waitFor(() => {
  1364. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1365. // Both slots populated: slot 1 with the user's white pick, slot
  1366. // 2 auto-picked with grey from the colour-match scoring.
  1367. expect(body.filament_presets).toHaveLength(2);
  1368. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  1369. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-GREY' });
  1370. });
  1371. });
  1372. // #2712: the filament list is positional the whole way down — index 0 is
  1373. // slot 1, and the backend forwards it as filament_1.json..filament_N.json.
  1374. // A MakerWorld source that ships slice_info and paints with slot 4 alone
  1375. // used to yield a one-row list, so the user's only pick was bound to slot 1
  1376. // and slot 4 sliced with the source's embedded default: picking PETG gave a
  1377. // PLA print. The modal now asks for every project slot so the positions
  1378. // line up.
  1379. it('requests every project slot, not just the ones the plate prints with', async () => {
  1380. renderWithTracker({
  1381. source: { kind: 'libraryFile', id: 100, filename: 'Tunnel.3mf' },
  1382. onClose: vi.fn(),
  1383. });
  1384. await waitFor(() => expect(mockApi.getLibraryFileFilamentRequirements).toHaveBeenCalled());
  1385. const [, plateArg, , fullSlots] = mockApi.getLibraryFileFilamentRequirements.mock.calls[0];
  1386. expect(plateArg).toBe(1);
  1387. expect(fullSlots).toBe(true);
  1388. });
  1389. it('sends the pick for a high-numbered slot at its own index', async () => {
  1390. // Four project slots, only slot 4 printed. The pick must arrive as the
  1391. // FOURTH entry; anywhere else and the slicer binds it to the wrong slot.
  1392. mockApi.getLibraryFilePlates.mockResolvedValue({
  1393. file_id: 100,
  1394. filename: 'Tunnel.3mf',
  1395. is_multi_plate: false,
  1396. plates: [
  1397. {
  1398. index: 1,
  1399. name: 'Plate 1',
  1400. objects: ['Tunnel'],
  1401. has_thumbnail: false,
  1402. thumbnail_url: null,
  1403. print_time_seconds: 1200,
  1404. filament_used_grams: 105,
  1405. filaments: [],
  1406. },
  1407. ],
  1408. });
  1409. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1410. file_id: 100,
  1411. filename: 'Tunnel.3mf',
  1412. plate_id: 1,
  1413. filaments: [
  1414. { slot_id: 1, type: 'PLA', color: '#38CC0A', used_grams: 0, used_meters: 0, used_in_plate: false },
  1415. { slot_id: 2, type: 'PLA', color: '#161616', used_grams: 0, used_meters: 0, used_in_plate: false },
  1416. { slot_id: 3, type: 'PLA', color: '#898989', used_grams: 0, used_meters: 0, used_in_plate: false },
  1417. { slot_id: 4, type: 'PLA', color: '#898989', used_grams: 105, used_meters: 35, used_in_plate: true },
  1418. ],
  1419. });
  1420. mockApi.getSlicerPresets.mockResolvedValue({
  1421. cloud: {
  1422. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1423. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1424. filament: [
  1425. { id: 'F-PLA', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#898989' },
  1426. { id: 'F-PETG', name: 'Cloud PETG', source: 'cloud', filament_type: 'PETG', filament_colour: '#00FF00' },
  1427. ],
  1428. },
  1429. local: { printer: [], process: [], filament: [] },
  1430. standard: { printer: [], process: [], filament: [] },
  1431. cloud_status: 'ok',
  1432. orca_cloud: { printer: [], process: [], filament: [] },
  1433. orca_cloud_status: 'ok',
  1434. });
  1435. mockApi.sliceLibraryFile.mockResolvedValue({
  1436. job_id: 51,
  1437. status: 'pending',
  1438. status_url: '/api/v1/slice-jobs/51',
  1439. });
  1440. renderWithTracker({
  1441. source: { kind: 'libraryFile', id: 100, filename: 'Tunnel.3mf' },
  1442. onClose: vi.fn(),
  1443. });
  1444. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1445. // 1 printer + 1 process + 1 bed-type + 4 filament rows.
  1446. const selects = presetSelects();
  1447. expect(selects).toHaveLength(7);
  1448. // Only slot 4 is selectable — the other three are the padding.
  1449. expect([selects[3].disabled, selects[4].disabled, selects[5].disabled]).toEqual([true, true, true]);
  1450. expect(selects[6].disabled).toBe(false);
  1451. const user = userEvent.setup();
  1452. await user.selectOptions(selects[6], 'cloud:F-PETG');
  1453. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1454. await waitFor(() => {
  1455. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1456. expect(body.filament_presets).toHaveLength(4);
  1457. expect(body.filament_presets[3]).toEqual({ source: 'cloud', id: 'F-PETG' });
  1458. });
  1459. });
  1460. // ------------------------------------------------------------------
  1461. // Slicer Pipelines (#1425) — Apply / Save integration in SliceModal
  1462. // ------------------------------------------------------------------
  1463. it('Apply pipeline dropdown is disabled and shows empty hint when no pipelines exist', async () => {
  1464. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1465. renderWithTracker({
  1466. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1467. onClose: vi.fn(),
  1468. });
  1469. await waitFor(() => {
  1470. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  1471. expect(select.disabled).toBe(true);
  1472. expect(select.querySelector('option')?.textContent).toMatch(/No saved pipelines/i);
  1473. });
  1474. });
  1475. it('applies a saved pipeline to printer, process, and bed_type slots on selection', async () => {
  1476. mockApi.listSlicerPipelines.mockResolvedValue({
  1477. pipelines: [
  1478. {
  1479. id: 7,
  1480. name: 'Production Batch',
  1481. description: null,
  1482. printer_preset: { source: 'local', id: '1' },
  1483. process_preset: { source: 'local', id: '2' },
  1484. filament_presets: [{ source: 'local', id: '3' }],
  1485. bed_type: 'Textured PEI Plate',
  1486. target_kind: 'printer_class',
  1487. target_printer_id: null,
  1488. target_model_class: null,
  1489. fanout_strategy: 'max_parallel',
  1490. created_by: null,
  1491. created_at: '2026-06-27T00:00:00Z',
  1492. updated_at: '2026-06-27T00:00:00Z',
  1493. },
  1494. ],
  1495. });
  1496. renderWithTracker({
  1497. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1498. onClose: vi.fn(),
  1499. });
  1500. // Wait for presets + pipelines listing to populate the modal.
  1501. await waitFor(() => {
  1502. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  1503. expect(select.disabled).toBe(false);
  1504. expect(within(select).getByText('Production Batch')).toBeDefined();
  1505. });
  1506. const user = userEvent.setup();
  1507. await user.selectOptions(screen.getByLabelText(/Apply pipeline/i), '7');
  1508. // After applying, submitting the slice request should carry the
  1509. // pipeline's preset refs end-to-end.
  1510. mockApi.sliceLibraryFile.mockResolvedValue({
  1511. job_id: 42,
  1512. status: 'queued',
  1513. status_url: '/api/v1/slice-jobs/42',
  1514. });
  1515. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1516. await waitFor(() => {
  1517. expect(mockApi.sliceLibraryFile).toHaveBeenCalled();
  1518. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1519. expect(body.printer_preset).toEqual({ source: 'local', id: '1' });
  1520. expect(body.process_preset).toEqual({ source: 'local', id: '2' });
  1521. expect(body.filament_presets[0]).toEqual({ source: 'local', id: '3' });
  1522. expect(body.bed_type).toBe('Textured PEI Plate');
  1523. });
  1524. });
  1525. it("shows a pipeline's plate the list doesn't offer instead of Auto (#3359)", async () => {
  1526. mockApi.listSlicerPipelines.mockResolvedValue({
  1527. pipelines: [
  1528. {
  1529. id: 7,
  1530. name: 'Orca textured',
  1531. description: null,
  1532. printer_preset: { source: 'local', id: '1' },
  1533. process_preset: { source: 'local', id: '2' },
  1534. filament_presets: [{ source: 'local', id: '3' }],
  1535. bed_type: 'Textured Cool Plate',
  1536. target_kind: 'printer_class',
  1537. target_printer_id: null,
  1538. target_model_class: null,
  1539. fanout_strategy: 'max_parallel',
  1540. created_by: null,
  1541. created_at: '2026-06-27T00:00:00Z',
  1542. updated_at: '2026-06-27T00:00:00Z',
  1543. },
  1544. ],
  1545. });
  1546. renderWithTracker({
  1547. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1548. onClose: vi.fn(),
  1549. });
  1550. await waitFor(() => {
  1551. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  1552. expect(within(select).getByText('Orca textured')).toBeDefined();
  1553. });
  1554. const user = userEvent.setup();
  1555. await user.selectOptions(screen.getByLabelText(/Apply pipeline/i), '7');
  1556. const bedSelect = presetSelects().find((el) =>
  1557. (el as HTMLSelectElement).options[0]?.textContent?.toLowerCase().includes('auto'),
  1558. ) as HTMLSelectElement;
  1559. expect(bedSelect.value).toBe('Textured Cool Plate');
  1560. expect(bedSelect.selectedOptions[0].textContent).toBe('Textured Cool Plate');
  1561. });
  1562. it('saves the current four-slot selection as a new pipeline when the user clicks Save as pipeline', async () => {
  1563. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1564. mockApi.createSlicerPipeline.mockResolvedValue({
  1565. id: 99,
  1566. name: 'My Default',
  1567. description: null,
  1568. printer_preset: { source: 'local', id: '1' },
  1569. process_preset: { source: 'local', id: '2' },
  1570. filament_presets: [{ source: 'local', id: '3' }],
  1571. bed_type: null,
  1572. target_kind: 'printer_class',
  1573. target_printer_id: null,
  1574. target_model_class: null,
  1575. fanout_strategy: 'max_parallel',
  1576. created_by: null,
  1577. created_at: '2026-06-27T00:00:00Z',
  1578. updated_at: '2026-06-27T00:00:00Z',
  1579. });
  1580. renderWithTracker({
  1581. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1582. onClose: vi.fn(),
  1583. });
  1584. // Wait for auto-pick to populate all four slots from the fullThreeTier
  1585. // listing — then Save as pipeline becomes enabled.
  1586. const user = userEvent.setup();
  1587. let saveBtn: HTMLButtonElement;
  1588. await waitFor(() => {
  1589. saveBtn = screen.getByRole('button', { name: /^Save as pipeline$/ }) as HTMLButtonElement;
  1590. expect(saveBtn.disabled).toBe(false);
  1591. });
  1592. await user.click(saveBtn!);
  1593. const nameInput = screen.getByLabelText(/New pipeline name/i);
  1594. await user.type(nameInput, 'My Default');
  1595. await user.click(screen.getByRole('button', { name: /^Save$/ }));
  1596. await waitFor(() => {
  1597. expect(mockApi.createSlicerPipeline).toHaveBeenCalledTimes(1);
  1598. const body = mockApi.createSlicerPipeline.mock.calls[0][0];
  1599. expect(body.name).toBe('My Default');
  1600. // The four slots come from the auto-picked unified-presets listing —
  1601. // local tier wins per SLICE_MODAL_TIER_ORDER.
  1602. expect(body.printer_preset.source).toBe('local');
  1603. expect(body.process_preset.source).toBe('local');
  1604. expect(body.filament_presets[0].source).toBe('local');
  1605. });
  1606. });
  1607. /**
  1608. * Per-slot filament colour (#2977).
  1609. *
  1610. * A filament preset carries no colour in either slicer -- colour is a
  1611. * per-project property their GUIs set from the plate -- so a slice that
  1612. * supplies none records the CLI's compiled-in #00AE42 for every slot. That
  1613. * is what made every internal-slicer thumbnail Bambu green regardless of
  1614. * the filament picked, and what made the print dialog report a colour
  1615. * mismatch against the AMS slot it had just correctly mapped to.
  1616. *
  1617. * The swatch is the only place the colour can come from for an STL, which
  1618. * has none anywhere else, so it is offered for single-slot sources too.
  1619. */
  1620. describe('filament colour swatch', () => {
  1621. function colourInputs(): HTMLInputElement[] {
  1622. return screen
  1623. .getAllByLabelText('Filament colour')
  1624. .filter((el): el is HTMLInputElement => el instanceof HTMLInputElement);
  1625. }
  1626. it('shows the hex beside the swatch so it reads as a control, not a decoration', async () => {
  1627. // The reason this exists: a bare dot next to a label was taken for the
  1628. // read-only swatch multi-colour rows already had, so on the STL -- the
  1629. // one source with no colour to inherit -- nothing said it was settable.
  1630. renderWithTracker({
  1631. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1632. onClose: vi.fn(),
  1633. });
  1634. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1635. expect(screen.getByText('#00AE42')).toBeDefined();
  1636. });
  1637. it('puts the whole control on the dropdown row, not in the label', async () => {
  1638. // Sitting beside the <select> and styled like it is what makes it read
  1639. // as a control. In the label row it read as a caption on the label.
  1640. renderWithTracker({
  1641. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1642. onClose: vi.fn(),
  1643. });
  1644. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1645. const row = colourInputs()[0].closest('div');
  1646. expect(row?.querySelector('select')).not.toBeNull();
  1647. });
  1648. it('wraps the swatch and its hex in one label bound to the input', async () => {
  1649. // So a click anywhere on the pill opens the picker, rather than only a
  1650. // 16px dot being live.
  1651. renderWithTracker({
  1652. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1653. onClose: vi.fn(),
  1654. });
  1655. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1656. const pill = screen.getByText('#00AE42').closest('label');
  1657. expect(pill?.getAttribute('for')).toBe(colourInputs()[0].id);
  1658. expect(pill?.contains(colourInputs()[0])).toBe(true);
  1659. });
  1660. it('the hex follows the swatch when it is changed', async () => {
  1661. renderWithTracker({
  1662. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1663. onClose: vi.fn(),
  1664. });
  1665. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1666. fireEvent.change(colourInputs()[0], { target: { value: '#e8b00c' } });
  1667. await waitFor(() => expect(screen.getByText('#E8B00C')).toBeDefined());
  1668. });
  1669. it('paints the colour on the input itself, not only via the native swatch', async () => {
  1670. // An engine that does not paint ::-webkit-color-swatch would otherwise
  1671. // leave an empty ring, which is indistinguishable from no swatch.
  1672. renderWithTracker({
  1673. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1674. onClose: vi.fn(),
  1675. });
  1676. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1677. expect(colourInputs()[0].style.backgroundColor).not.toBe('');
  1678. });
  1679. it('offers a colour swatch for a single-slot STL, which has no colour of its own', async () => {
  1680. renderWithTracker({
  1681. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1682. onClose: vi.fn(),
  1683. });
  1684. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1685. expect(colourInputs()).toHaveLength(1);
  1686. });
  1687. it("shows the slicer's own default for a source that carries no colour", async () => {
  1688. // Not an invented placeholder: #00AE42 is exactly what the slice would
  1689. // record if nothing were sent, so the swatch tells the truth about the
  1690. // file that is about to be produced.
  1691. renderWithTracker({
  1692. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1693. onClose: vi.fn(),
  1694. });
  1695. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1696. expect(colourInputs()[0].value).toBe('#00ae42');
  1697. });
  1698. it("pre-fills each slot from the source plate's designed colour", async () => {
  1699. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1700. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1701. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1702. renderWithTracker({
  1703. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1704. onClose: vi.fn(),
  1705. });
  1706. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1707. expect(colourInputs().map((i) => i.value)).toEqual(['#000000', '#ffffff']);
  1708. });
  1709. it("sends the source plate's colours when the swatches are left alone", async () => {
  1710. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1711. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1712. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1713. mockApi.sliceLibraryFile.mockResolvedValue({
  1714. job_id: 42,
  1715. status: 'pending',
  1716. status_url: '/api/v1/slice-jobs/42',
  1717. });
  1718. renderWithTracker({
  1719. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1720. onClose: vi.fn(),
  1721. });
  1722. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1723. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  1724. await waitFor(() => {
  1725. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  1726. 100,
  1727. expect.objectContaining({ filament_colours: ['#000000', '#FFFFFF'] }),
  1728. );
  1729. });
  1730. });
  1731. it('sends an empty string for a slot with no colour, so the backend fallbacks still run', async () => {
  1732. // A sent colour outranks the preset's own default_filament_colour, so
  1733. // pinning the picker's displayed #00AE42 here would silently discard
  1734. // the real colour of an Orca-imported profile that carries one.
  1735. mockApi.sliceLibraryFile.mockResolvedValue({
  1736. job_id: 42,
  1737. status: 'pending',
  1738. status_url: '/api/v1/slice-jobs/42',
  1739. });
  1740. renderWithTracker({
  1741. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1742. onClose: vi.fn(),
  1743. });
  1744. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1745. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  1746. await waitFor(() => {
  1747. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  1748. 100,
  1749. expect.objectContaining({ filament_colours: [''] }),
  1750. );
  1751. });
  1752. });
  1753. it('sends the user\'s pick, upper-cased, once a swatch is changed', async () => {
  1754. mockApi.sliceLibraryFile.mockResolvedValue({
  1755. job_id: 42,
  1756. status: 'pending',
  1757. status_url: '/api/v1/slice-jobs/42',
  1758. });
  1759. renderWithTracker({
  1760. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1761. onClose: vi.fn(),
  1762. });
  1763. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1764. const user = userEvent.setup();
  1765. // A colour input has no text entry; fire the change the picker would.
  1766. fireEvent.change(colourInputs()[0], { target: { value: '#e8b00c' } });
  1767. await waitFor(() => expect(colourInputs()[0].value).toBe('#e8b00c'));
  1768. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1769. await waitFor(() => {
  1770. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  1771. 100,
  1772. expect.objectContaining({ filament_colours: ['#E8B00C'] }),
  1773. );
  1774. });
  1775. });
  1776. it('only overrides the slot that was changed', async () => {
  1777. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1778. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1779. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1780. mockApi.sliceLibraryFile.mockResolvedValue({
  1781. job_id: 42,
  1782. status: 'pending',
  1783. status_url: '/api/v1/slice-jobs/42',
  1784. });
  1785. renderWithTracker({
  1786. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1787. onClose: vi.fn(),
  1788. });
  1789. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1790. fireEvent.change(colourInputs()[1], { target: { value: '#112233' } });
  1791. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  1792. await waitFor(() => {
  1793. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  1794. 100,
  1795. expect.objectContaining({ filament_colours: ['#000000', '#112233'] }),
  1796. );
  1797. });
  1798. });
  1799. it('trims the alpha byte for display but submits the colour whole', async () => {
  1800. // The AMS reports colours with an alpha byte and a source 3MF can carry
  1801. // one; <input type="color"> accepts only the 6-digit form.
  1802. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1803. file_id: 100,
  1804. filename: 'Alpha.3mf',
  1805. plate_id: 1,
  1806. filaments: [{ slot_id: 1, type: 'PLA', color: '#E8B00CFF', used_grams: 10, used_meters: 3 }],
  1807. });
  1808. mockApi.sliceLibraryFile.mockResolvedValue({
  1809. job_id: 42,
  1810. status: 'pending',
  1811. status_url: '/api/v1/slice-jobs/42',
  1812. });
  1813. renderWithTracker({
  1814. source: { kind: 'libraryFile', id: 100, filename: 'Alpha.3mf' },
  1815. onClose: vi.fn(),
  1816. });
  1817. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1818. expect(colourInputs()[0].value).toBe('#e8b00c');
  1819. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  1820. await waitFor(() => {
  1821. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  1822. 100,
  1823. expect.objectContaining({ filament_colours: ['#E8B00CFF'] }),
  1824. );
  1825. });
  1826. });
  1827. it('is disabled in "slice as designed" mode, which sends no filament profiles', async () => {
  1828. mockApi.getLibraryFilePlates.mockResolvedValue({
  1829. file_id: 100,
  1830. filename: 'Designed.3mf',
  1831. plates: [],
  1832. is_multi_plate: false,
  1833. embedded_printer: 'Imported X1C 0.4',
  1834. embedded_process: '0.20mm Standard',
  1835. });
  1836. renderWithTracker({
  1837. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  1838. onClose: vi.fn(),
  1839. });
  1840. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1841. const toggle = screen.getByRole('checkbox', { name: /built-in settings/i });
  1842. expect(colourInputs()[0].disabled).toBe(false);
  1843. await userEvent.setup().click(toggle);
  1844. expect(colourInputs()[0].disabled).toBe(true);
  1845. });
  1846. });
  1847. });
  1848. // Pure-function tests for the filament slot picker. Pinned as a separate
  1849. // describe so the contract is visible without needing the modal mount.
  1850. /**
  1851. * The slice dialog switches to a two-column layout once there is room for it,
  1852. * and the process-settings panel then owns the right-hand column. The global
  1853. * test setup pins matchMedia to `matches: false`, so every other test in this
  1854. * file exercises the narrow single-stack path; these override it.
  1855. */
  1856. describe('SliceModal — process settings in "slice as designed" mode', () => {
  1857. beforeEach(() => {
  1858. vi.clearAllMocks();
  1859. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  1860. mockApi.getSlicerPresetValues.mockResolvedValue({ resolved: true, values: {}, reason: 'ok' });
  1861. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1862. mockApi.getSlicerLoadedSpools.mockResolvedValue({ printers: [] });
  1863. mockApi.getSlicerPrinterModels.mockResolvedValue({});
  1864. mockApi.getLibraryFilePlates.mockResolvedValue({
  1865. file_id: 100,
  1866. filename: 'Designed.3mf',
  1867. plates: [],
  1868. is_multi_plate: false,
  1869. embedded_printer: 'Bambu Lab X1 Carbon 0.4 nozzle',
  1870. embedded_process: '0.20mm Standard',
  1871. });
  1872. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1873. file_id: 100, filename: 'Designed.3mf', plate_id: 1, filaments: [],
  1874. });
  1875. });
  1876. it('disables the panel rather than removing it', async () => {
  1877. const user = userEvent.setup();
  1878. renderWithTracker({
  1879. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  1880. onClose: vi.fn(),
  1881. });
  1882. const toggle = (await screen.findByLabelText(/Use the file's built-in settings/)) as HTMLInputElement;
  1883. const header = await screen.findByRole('button', { name: /Process settings/ });
  1884. await user.click(header);
  1885. const search = await screen.findByPlaceholderText('Search settings');
  1886. expect(search).toBeEnabled();
  1887. await user.click(toggle);
  1888. // Still on screen — hiding it made the dialog look like it had lost a
  1889. // feature — but nothing in it can be operated, because nothing in it is
  1890. // sent on this path.
  1891. expect(screen.getByPlaceholderText('Search settings')).toBeDisabled();
  1892. expect(screen.getByRole('button', { name: 'Expert' })).toBeDisabled();
  1893. expect(screen.getByText(/Not used while/)).toBeInTheDocument();
  1894. expect(screen.getByText('Inactive')).toBeInTheDocument();
  1895. });
  1896. it('sends no process overrides once the file drives the slice', async () => {
  1897. mockApi.sliceLibraryFile.mockResolvedValue({ job_id: 42, status: 'pending', status_url: '/x' });
  1898. const user = userEvent.setup();
  1899. renderWithTracker({
  1900. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  1901. onClose: vi.fn(),
  1902. });
  1903. // Edit something, then hand the slice over to the file's own settings.
  1904. await user.click(await screen.findByRole('button', { name: /Process settings/ }));
  1905. const input = await screen.findByLabelText(/^Layer height/);
  1906. await user.clear(input);
  1907. await user.type(input, '0.16');
  1908. await user.click(screen.getByLabelText(/Use the file's built-in settings/));
  1909. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1910. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  1911. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as Record<string, unknown>;
  1912. expect(payload.use_embedded_settings).toBe(true);
  1913. expect(payload).not.toHaveProperty('process_overrides');
  1914. });
  1915. });
  1916. describe('SliceModal — process settings layout', () => {
  1917. const setViewport = (wide: boolean) => {
  1918. Object.defineProperty(window, 'matchMedia', {
  1919. writable: true,
  1920. value: (query: string) => ({
  1921. matches: wide,
  1922. media: query,
  1923. onchange: null,
  1924. addListener: () => {},
  1925. removeListener: () => {},
  1926. addEventListener: () => {},
  1927. removeEventListener: () => {},
  1928. dispatchEvent: () => true,
  1929. }),
  1930. });
  1931. };
  1932. beforeEach(() => {
  1933. vi.clearAllMocks();
  1934. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  1935. mockApi.getSlicerPresetValues.mockResolvedValue({ resolved: true, values: {}, reason: 'ok' });
  1936. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1937. mockApi.getSlicerLoadedSpools.mockResolvedValue({ printers: [] });
  1938. mockApi.getLibraryFilePlates.mockResolvedValue({
  1939. file_id: 100,
  1940. filename: 'Cube.stl',
  1941. plates: [],
  1942. is_multi_plate: false,
  1943. });
  1944. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1945. file_id: 100,
  1946. filename: 'Cube.stl',
  1947. plate_id: 1,
  1948. filaments: [],
  1949. });
  1950. });
  1951. afterEach(() => setViewport(false));
  1952. it('keeps the panel collapsed behind a disclosure in the narrow layout', async () => {
  1953. setViewport(false);
  1954. renderWithTracker({ source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' }, onClose: vi.fn() });
  1955. const header = await screen.findByRole('button', { name: /Process settings/ });
  1956. expect(header).toBeEnabled();
  1957. expect(screen.queryByPlaceholderText('Search settings')).not.toBeInTheDocument();
  1958. await userEvent.setup().click(header);
  1959. await waitFor(() => expect(screen.getByPlaceholderText('Search settings')).toBeInTheDocument());
  1960. });
  1961. it('opens the panel without a click once it has a column of its own', async () => {
  1962. setViewport(true);
  1963. renderWithTracker({ source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' }, onClose: vi.fn() });
  1964. // No disclosure to operate: the panel is the column, so its header is
  1965. // inert rather than offering to collapse something that has room.
  1966. await waitFor(() => expect(screen.getByPlaceholderText('Search settings')).toBeInTheDocument());
  1967. expect(screen.getByRole('button', { name: /Process settings/ })).toBeDisabled();
  1968. });
  1969. });
  1970. /**
  1971. * Process and filament lists hold back presets that resolve to a *different*
  1972. * printer, behind a per-slot "Show all". Two things must never be hidden: a
  1973. * preset whose compatibility is merely unknown, and whatever is currently
  1974. * selected.
  1975. */
  1976. describe('SliceModal — presets filtered by the selected printer', () => {
  1977. const presets: UnifiedPresetsResponse = {
  1978. cloud: { printer: [], process: [], filament: [] },
  1979. orca_cloud: { printer: [], process: [], filament: [] },
  1980. local: { printer: [], process: [], filament: [] },
  1981. standard: {
  1982. printer: [
  1983. { id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' },
  1984. ],
  1985. process: [
  1986. { id: 'p-x1c', name: '0.20mm Standard @BBL X1C', source: 'standard' },
  1987. { id: 'p-h2d', name: '0.20mm Standard @BBL H2D', source: 'standard' },
  1988. { id: 'p-a1m', name: '0.20mm Standard @BBL A1M', source: 'standard' },
  1989. // No printer tag at all — compatibility is unknown, never hidden.
  1990. { id: 'p-custom', name: 'My own profile', source: 'standard' },
  1991. ],
  1992. filament: [{ id: 'f-x1c', name: 'Bambu PLA Basic @BBL X1C', source: 'standard' }],
  1993. },
  1994. cloud_status: 'ok',
  1995. orca_cloud_status: 'ok',
  1996. } as UnifiedPresetsResponse;
  1997. const processOptionNames = () =>
  1998. Array.from(presetSelects()[1].options).map((o) => o.textContent);
  1999. beforeEach(() => {
  2000. vi.clearAllMocks();
  2001. mockApi.getSlicerPresets.mockResolvedValue(presets);
  2002. mockApi.getSlicerPrinterModels.mockResolvedValue({ 'Bambu Lab X1 Carbon': 'X1C' });
  2003. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  2004. mockApi.getSlicerLoadedSpools.mockResolvedValue({ printers: [] });
  2005. mockApi.getLibraryFilePlates.mockResolvedValue({
  2006. file_id: 100, filename: 'Cube.stl', plates: [], is_multi_plate: false,
  2007. });
  2008. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  2009. file_id: 100, filename: 'Cube.stl', plate_id: 1, filaments: [],
  2010. });
  2011. });
  2012. const open = async () => {
  2013. renderWithTracker({ source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' }, onClose: vi.fn() });
  2014. await waitFor(() => expect(presetSelects().length).toBeGreaterThan(1));
  2015. };
  2016. it('leaves out presets belonging to another printer', async () => {
  2017. await open();
  2018. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL X1C'));
  2019. expect(processOptionNames()).not.toContain('0.20mm Standard @BBL H2D');
  2020. expect(processOptionNames()).not.toContain('0.20mm Standard @BBL A1M');
  2021. });
  2022. it('keeps a preset whose compatibility cannot be determined', async () => {
  2023. await open();
  2024. // An untagged preset carries no evidence either way; hiding it would make
  2025. // a user's own imported profiles vanish.
  2026. await waitFor(() => expect(processOptionNames()).toContain('My own profile'));
  2027. });
  2028. it('says how many it held back and reveals them on request', async () => {
  2029. const user = userEvent.setup();
  2030. await open();
  2031. const hidden = await screen.findByText('2 hidden');
  2032. expect(hidden).toBeInTheDocument();
  2033. await user.click(within(hidden.parentElement as HTMLElement).getByRole('button', { name: 'Show all' }));
  2034. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  2035. expect(processOptionNames()).toContain('0.20mm Standard @BBL A1M');
  2036. });
  2037. it('collapses the list again on Show fewer', async () => {
  2038. const user = userEvent.setup();
  2039. await open();
  2040. await user.click((await screen.findAllByRole('button', { name: 'Show all' }))[0]);
  2041. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  2042. await user.click(screen.getAllByRole('button', { name: 'Show fewer' })[0]);
  2043. await waitFor(() => expect(processOptionNames()).not.toContain('0.20mm Standard @BBL H2D'));
  2044. });
  2045. it('never hides the preset that is currently selected', async () => {
  2046. const user = userEvent.setup();
  2047. await open();
  2048. // Reach a cross-printer preset, pick it, then collapse the list again.
  2049. await user.click((await screen.findAllByRole('button', { name: 'Show all' }))[0]);
  2050. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  2051. await user.selectOptions(presetSelects()[1], 'standard:p-h2d');
  2052. await user.click(screen.getAllByRole('button', { name: 'Show fewer' })[0]);
  2053. // Dropping it from the options would blank the select and silently discard
  2054. // a deliberate cross-printer choice.
  2055. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  2056. expect(presetSelects()[1].value).toBe('standard:p-h2d');
  2057. // The one still-hidden preset is counted; the selected one is not.
  2058. expect(screen.getByText('1 hidden')).toBeInTheDocument();
  2059. });
  2060. });
  2061. describe('pickFilamentForSlot — printer-compat contract (#1851)', () => {
  2062. // Index that recognises @BBL H2C / @BBL A1 tokens via the canonical
  2063. // PRINTER_MODEL_MAP. Real production data comes through
  2064. // ``api.getSlicerPrinterModels`` — the H2C / A1 fragments are the ones
  2065. // the production registry ships.
  2066. const index = buildCompatibilityIndex({
  2067. 'Bambu Lab A1': 'A1',
  2068. 'Bambu Lab H2C': 'H2C',
  2069. });
  2070. it('prefers a printer-compatible preset over a printer-mismatched one even with better colour match', () => {
  2071. // The OP scenario for #1851: a Bambu Lab A1 is selected; the unused-slot
  2072. // requirement carries the original H2C plate's PLA colour. With the
  2073. // legacy soft-penalty scoring an H2C-bound preset whose colour matches
  2074. // exactly could still rise above the A1-compatible PLA Basic whose
  2075. // colour doesn't, and then the unused-slot substitution propagated the
  2076. // H2C-bound preset across every unused slot — the CLI rejected with
  2077. // ``filament preset Generic PLA @BBL H2C (slot 1) is not compatible
  2078. // with printer Bambu Lab A1 0.4 nozzle``. The hard-skip contract makes
  2079. // sure a mismatched preset is never chosen while any compatible
  2080. // alternative exists, irrespective of metadata-score arithmetic.
  2081. const presets = makeUnified({
  2082. standard: {
  2083. printer: [],
  2084. process: [],
  2085. filament: [
  2086. {
  2087. id: 'Generic PLA @BBL H2C',
  2088. name: 'Generic PLA @BBL H2C',
  2089. source: 'standard',
  2090. filament_type: 'PLA',
  2091. filament_colour: '#FF0000',
  2092. },
  2093. {
  2094. id: 'Bambu PLA Basic @BBL A1',
  2095. name: 'Bambu PLA Basic @BBL A1',
  2096. source: 'standard',
  2097. filament_type: 'PLA',
  2098. filament_colour: '#FFFFFF',
  2099. },
  2100. ],
  2101. },
  2102. });
  2103. const pick = pickFilamentForSlot(
  2104. presets,
  2105. { type: 'PLA', color: '#FF0000' },
  2106. 'Bambu Lab A1 0.4 nozzle',
  2107. index,
  2108. );
  2109. expect(pick).toEqual({ source: 'standard', id: 'Bambu PLA Basic @BBL A1' });
  2110. });
  2111. it('falls back to a mismatched preset when no compatible alternative exists', () => {
  2112. // Graceful degrade: when every available preset is printer-mismatched,
  2113. // returning ``null`` would block the slice entirely. The picker keeps
  2114. // its old behaviour of returning the best-scoring mismatch so the user
  2115. // sees a populated dropdown they can correct, not an empty one.
  2116. const presets = makeUnified({
  2117. standard: {
  2118. printer: [],
  2119. process: [],
  2120. filament: [
  2121. {
  2122. id: 'Generic PLA @BBL H2C',
  2123. name: 'Generic PLA @BBL H2C',
  2124. source: 'standard',
  2125. filament_type: 'PLA',
  2126. filament_colour: '#FF0000',
  2127. },
  2128. ],
  2129. },
  2130. });
  2131. const pick = pickFilamentForSlot(
  2132. presets,
  2133. { type: 'PLA', color: '#FF0000' },
  2134. 'Bambu Lab A1 0.4 nozzle',
  2135. index,
  2136. );
  2137. expect(pick).toEqual({ source: 'standard', id: 'Generic PLA @BBL H2C' });
  2138. });
  2139. it('treats a no-printer-context call as no-mismatch (every preset eligible)', () => {
  2140. // ``printerName === null`` happens transiently on first render before the
  2141. // printer pre-pick effect has run. ``presetCompatibility`` returns
  2142. // ``unknown`` for every preset in that case, so the picker should just
  2143. // pick by metadata score with no compatibility filter active.
  2144. const presets = makeUnified({
  2145. standard: {
  2146. printer: [],
  2147. process: [],
  2148. filament: [
  2149. {
  2150. id: 'Generic PLA @BBL H2C',
  2151. name: 'Generic PLA @BBL H2C',
  2152. source: 'standard',
  2153. filament_type: 'PLA',
  2154. filament_colour: '#FF0000',
  2155. },
  2156. ],
  2157. },
  2158. });
  2159. const pick = pickFilamentForSlot(
  2160. presets,
  2161. { type: 'PLA', color: '#FF0000' },
  2162. null,
  2163. index,
  2164. );
  2165. expect(pick).toEqual({ source: 'standard', id: 'Generic PLA @BBL H2C' });
  2166. });
  2167. });
  2168. describe('pickFilamentForSlot — long-form printer tag (#2628)', () => {
  2169. const index = buildCompatibilityIndex({
  2170. 'Bambu Lab A1': 'A1',
  2171. 'Bambu Lab H2D': 'H2D',
  2172. });
  2173. it('never auto-picks a user-saved preset scoped to another printer', () => {
  2174. // michaelklos's registry: a cloud-tier user preset carrying the full
  2175. // "@Bambu Lab H2D 0.4 nozzle" tag outscores the A1 preset on tier bonus
  2176. // alone. Until the matcher learned the long form it classified 'unknown'
  2177. // — indistinguishable from compatible — so it won the slot, landed in a
  2178. // dropdown the modal disables (slot not used by the plate), and the CLI
  2179. // rejected the whole slice.
  2180. const presets = makeUnified({
  2181. cloud: {
  2182. printer: [],
  2183. process: [],
  2184. filament: [
  2185. {
  2186. id: 'sunlu-tpu-h2d',
  2187. name: 'SUNLU TPU 95A @Bambu Lab H2D 0.4 nozzle',
  2188. source: 'cloud',
  2189. filament_type: 'PLA',
  2190. filament_colour: '#FF0000',
  2191. },
  2192. ],
  2193. },
  2194. standard: {
  2195. printer: [],
  2196. process: [],
  2197. filament: [
  2198. {
  2199. id: 'Bambu PLA Basic @BBL A1',
  2200. name: 'Bambu PLA Basic @BBL A1',
  2201. source: 'standard',
  2202. filament_type: 'PLA',
  2203. filament_colour: '#FFFFFF',
  2204. },
  2205. ],
  2206. },
  2207. });
  2208. const pick = pickFilamentForSlot(
  2209. presets,
  2210. { type: 'PLA', color: '#FF0000' },
  2211. 'Bambu Lab A1 0.4 nozzle',
  2212. index,
  2213. );
  2214. expect(pick).toEqual({ source: 'standard', id: 'Bambu PLA Basic @BBL A1' });
  2215. });
  2216. it('still picks a long-form preset for its own printer', () => {
  2217. const presets = makeUnified({
  2218. cloud: {
  2219. printer: [],
  2220. process: [],
  2221. filament: [
  2222. {
  2223. id: 'sunlu-tpu-h2d',
  2224. name: 'SUNLU TPU 95A @Bambu Lab H2D 0.4 nozzle',
  2225. source: 'cloud',
  2226. filament_type: 'TPU',
  2227. filament_colour: '#FF0000',
  2228. },
  2229. ],
  2230. },
  2231. });
  2232. const pick = pickFilamentForSlot(
  2233. presets,
  2234. { type: 'TPU', color: '#FF0000' },
  2235. 'Bambu Lab H2D 0.4 nozzle',
  2236. index,
  2237. );
  2238. expect(pick).toEqual({ source: 'cloud', id: 'sunlu-tpu-h2d' });
  2239. });
  2240. });
  2241. describe('SliceModal — material and printer filtering (#2982)', () => {
  2242. const A1 = 'Bambu Lab A1 0.4 nozzle';
  2243. const P1S = 'Bambu Lab P1S 0.4 nozzle';
  2244. beforeEach(() => {
  2245. vi.clearAllMocks();
  2246. mockApi.getSlicerPresetValues.mockResolvedValue({ resolved: true, values: {}, reason: 'ok' });
  2247. mockApi.getSlicerPrinterModels.mockResolvedValue({
  2248. 'Bambu Lab A1': 'A1',
  2249. 'Bambu Lab P1S': 'P1S',
  2250. 'Bambu Lab X1 Carbon': 'X1C',
  2251. });
  2252. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  2253. mockApi.getSlicerLoadedSpools.mockResolvedValue({ printers: [] });
  2254. mockApi.getLibraryFilePlates.mockResolvedValue({ file_id: 100, filename: 'Plate.3mf', plates: [] });
  2255. mockApi.sliceLibraryFile.mockResolvedValue({
  2256. job_id: 42,
  2257. status: 'pending',
  2258. status_url: '/api/v1/slice-jobs/42',
  2259. });
  2260. mockApi.getSliceJob.mockResolvedValue({
  2261. job_id: 42,
  2262. status: 'running',
  2263. kind: 'library_file',
  2264. source_id: 100,
  2265. source_name: 'Plate.3mf',
  2266. created_at: new Date().toISOString(),
  2267. started_at: null,
  2268. completed_at: null,
  2269. });
  2270. });
  2271. // One PLA plate slot — the case the report is about.
  2272. function plaPlate() {
  2273. return {
  2274. file_id: 100,
  2275. filename: 'Plate.3mf',
  2276. plate_id: 1,
  2277. filaments: [{ slot_id: 1, type: 'PLA', color: '#FF0000', used_grams: 10, used_meters: 3 }],
  2278. };
  2279. }
  2280. function presetsFor(printer: string, filaments: UnifiedPresetsResponse['standard']['filament']) {
  2281. return makeUnified({
  2282. standard: {
  2283. printer: [{ id: printer, name: printer, source: 'standard' }],
  2284. process: [
  2285. {
  2286. id: '0.20mm Standard @BBL X1C',
  2287. name: '0.20mm Standard @BBL X1C',
  2288. source: 'standard',
  2289. compatible_printers: [P1S, 'Bambu Lab X1 Carbon 0.4 nozzle'],
  2290. },
  2291. ],
  2292. filament: filaments,
  2293. },
  2294. });
  2295. }
  2296. it('does not auto-pick a stated PETG for a PLA plate', async () => {
  2297. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(plaPlate());
  2298. mockApi.getSlicerPresets.mockResolvedValue(
  2299. presetsFor(A1, [
  2300. { id: 'petg', name: 'eSUN PETG Basic @BBL A1', source: 'standard', filament_type: 'PETG', filament_colour: '#FF0000' },
  2301. { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  2302. ]),
  2303. );
  2304. renderWithTracker({
  2305. source: { kind: 'libraryFile', id: 100, filename: 'Plate.3mf' },
  2306. onClose: vi.fn(),
  2307. });
  2308. await waitFor(() => expect(screen.getByText(A1)).toBeDefined());
  2309. const user = userEvent.setup();
  2310. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  2311. await waitFor(() => {
  2312. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  2313. expect(body.filament_presets).toEqual([{ source: 'standard', id: 'pla' }]);
  2314. });
  2315. });
  2316. it('drops a wrong-material pick once the listing learns the material', async () => {
  2317. // The sequence a user upgrading their sidecar actually goes through. The
  2318. // first listing reports no material for anything, so the pre-pick has
  2319. // only colour to go on and lands on the PETG. The second reports the
  2320. // materials — and the wrong pick has to go, which it did not, because the
  2321. // slot was held on printer-compatibility alone.
  2322. const colourless = presetsFor(A1, [
  2323. { id: 'petg', name: 'eSUN PETG Basic @BBL A1', source: 'standard', filament_type: null, filament_colour: '#FF0000' },
  2324. { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard', filament_type: null, filament_colour: '#FFFFFF' },
  2325. ]);
  2326. const typed = presetsFor(A1, [
  2327. { id: 'petg', name: 'eSUN PETG Basic @BBL A1', source: 'standard', filament_type: 'PETG', filament_colour: '#FF0000' },
  2328. { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  2329. ]);
  2330. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(plaPlate());
  2331. mockApi.getSlicerPresets.mockResolvedValueOnce(colourless).mockResolvedValue(typed);
  2332. renderWithTracker({
  2333. source: { kind: 'libraryFile', id: 100, filename: 'Plate.3mf' },
  2334. onClose: vi.fn(),
  2335. });
  2336. await waitFor(() => expect(screen.getByText(A1)).toBeDefined());
  2337. const user = userEvent.setup();
  2338. // The colour-matched PETG is what the first listing produces.
  2339. await waitFor(() => {
  2340. const select = presetSelects().find((el) => el.value.startsWith('standard:p'));
  2341. expect(select?.value).toBe('standard:petg');
  2342. });
  2343. await user.click(screen.getByRole('button', { name: 'Refresh' }));
  2344. await waitFor(() => {
  2345. const select = presetSelects().find((el) => el.value.startsWith('standard:p'));
  2346. expect(select?.value).toBe('standard:pla');
  2347. });
  2348. });
  2349. it('keeps a wrong-material preset the user picked on purpose', async () => {
  2350. // Printing PETG on a plate a designer labelled PLA is a legitimate thing
  2351. // to do. The material rule corrects the auto-pick; it must not overrule
  2352. // a choice made in the dropdown.
  2353. const pla = { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard' as const, filament_type: 'PLA', filament_colour: '#FF0000' };
  2354. const petg = { id: 'petg', name: 'eSUN PETG Basic @BBL A1', source: 'standard' as const, filament_type: 'PETG', filament_colour: '#FFFFFF' };
  2355. const matte = { id: 'matte', name: 'Bambu PLA Matte @BBL A1', source: 'standard' as const, filament_type: 'PLA', filament_colour: '#00FF00' };
  2356. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(plaPlate());
  2357. // The refresh has to return a *different* listing, or React Query's
  2358. // structural sharing hands back the same object and the pre-pick never
  2359. // re-runs — which would make this test pass without proving anything.
  2360. mockApi.getSlicerPresets
  2361. .mockResolvedValueOnce(presetsFor(A1, [pla, petg]))
  2362. .mockResolvedValue(presetsFor(A1, [pla, petg, matte]));
  2363. renderWithTracker({
  2364. source: { kind: 'libraryFile', id: 100, filename: 'Plate.3mf' },
  2365. onClose: vi.fn(),
  2366. });
  2367. await waitFor(() => expect(screen.getByText(A1)).toBeDefined());
  2368. const user = userEvent.setup();
  2369. const filamentSelect = presetSelects().find((el) =>
  2370. Array.from(el.options).some((o) => o.textContent?.includes('eSUN PETG Basic')),
  2371. );
  2372. expect(filamentSelect).toBeDefined();
  2373. await user.selectOptions(filamentSelect!, 'standard:petg');
  2374. // Refreshing re-runs the pre-pick over the same slots. That is exactly
  2375. // where an un-exempted material rule would quietly undo the choice.
  2376. await user.click(screen.getByRole('button', { name: 'Refresh' }));
  2377. await waitFor(() => {
  2378. const select = presetSelects().find((el) => el.value.startsWith('standard:p'));
  2379. expect(select?.value).toBe('standard:petg');
  2380. });
  2381. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  2382. await waitFor(() => {
  2383. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  2384. expect(body.filament_presets).toEqual([{ source: 'standard', id: 'petg' }]);
  2385. });
  2386. });
  2387. it('shows every process when the printer filter would leave the list empty', async () => {
  2388. // A P1S against a sidecar too old to report compatible_printers: all 198
  2389. // processes read as another printer's, so the dropdown held one
  2390. // auto-picked entry and a "Show all" link, with nothing saying the list
  2391. // itself was the problem.
  2392. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(plaPlate());
  2393. mockApi.getSlicerPresets.mockResolvedValue(
  2394. makeUnified({
  2395. standard: {
  2396. printer: [{ id: P1S, name: P1S, source: 'standard' }],
  2397. process: [
  2398. { id: 'x1c', name: '0.20mm Standard @BBL X1C', source: 'standard' },
  2399. { id: 'a1', name: '0.06mm Fine @BBL A1 0.2 nozzle', source: 'standard' },
  2400. ],
  2401. filament: [
  2402. { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard', filament_type: 'PLA' },
  2403. ],
  2404. },
  2405. }),
  2406. );
  2407. renderWithTracker({
  2408. source: { kind: 'libraryFile', id: 100, filename: 'Plate.3mf' },
  2409. onClose: vi.fn(),
  2410. });
  2411. await waitFor(() => expect(screen.getByText(P1S)).toBeDefined());
  2412. const processSelect = presetSelects().find((el) =>
  2413. Array.from(el.options).some((o) => o.textContent?.includes('0.20mm Standard @BBL X1C')),
  2414. );
  2415. expect(processSelect).toBeDefined();
  2416. // Both are visible rather than one hidden behind "Show all".
  2417. const labels = Array.from(processSelect!.options).map((o) => o.textContent);
  2418. expect(labels.some((l) => l?.includes('0.20mm Standard @BBL X1C'))).toBe(true);
  2419. expect(labels.some((l) => l?.includes('0.06mm Fine @BBL A1 0.2 nozzle'))).toBe(true);
  2420. });
  2421. it('still hides other-printer presets when some do match', async () => {
  2422. // The guard is for an empty list only — the normal filter must survive.
  2423. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(plaPlate());
  2424. mockApi.getSlicerPresets.mockResolvedValue(
  2425. makeUnified({
  2426. standard: {
  2427. printer: [{ id: P1S, name: P1S, source: 'standard' }],
  2428. process: [
  2429. {
  2430. id: 'x1c',
  2431. name: '0.20mm Standard @BBL X1C',
  2432. source: 'standard',
  2433. compatible_printers: [P1S],
  2434. },
  2435. { id: 'a1', name: '0.06mm Fine @BBL A1 0.2 nozzle', source: 'standard' },
  2436. ],
  2437. filament: [
  2438. { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard', filament_type: 'PLA' },
  2439. ],
  2440. },
  2441. }),
  2442. );
  2443. renderWithTracker({
  2444. source: { kind: 'libraryFile', id: 100, filename: 'Plate.3mf' },
  2445. onClose: vi.fn(),
  2446. });
  2447. await waitFor(() => expect(screen.getByText(P1S)).toBeDefined());
  2448. const processSelect = presetSelects().find((el) =>
  2449. Array.from(el.options).some((o) => o.textContent?.includes('0.20mm Standard @BBL X1C')),
  2450. );
  2451. const labels = Array.from(processSelect!.options).map((o) => o.textContent);
  2452. expect(labels.some((l) => l?.includes('0.06mm Fine @BBL A1 0.2 nozzle'))).toBe(false);
  2453. });
  2454. });