SliceModal.test.tsx 103 KB

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