SliceModal.test.tsx 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711
  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('starts with auto-arrange ticked for a freshly combined file', 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: 'Combined.3mf' },
  676. onClose: vi.fn(),
  677. defaultAutoArrange: true,
  678. });
  679. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  680. expect(screen.getByRole('checkbox', { name: /Auto-arrange/ })).toBeChecked();
  681. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  682. await waitFor(() => {
  683. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  684. expect(body).toHaveProperty('auto_arrange', true);
  685. });
  686. });
  687. it('omits both layout flags when neither box is ticked (#2548)', async () => {
  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: vi.fn(),
  696. });
  697. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  698. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  699. await waitFor(() => {
  700. const [, body] = vi.mocked(mockApi.sliceLibraryFile).mock.calls[0];
  701. expect(body).not.toHaveProperty('auto_orient');
  702. expect(body).not.toHaveProperty('auto_arrange');
  703. });
  704. });
  705. it('lets the user override the default and pick a Standard preset', async () => {
  706. const onClose = vi.fn();
  707. mockApi.sliceLibraryFile.mockResolvedValue({
  708. job_id: 42,
  709. status: 'pending',
  710. status_url: '/api/v1/slice-jobs/42',
  711. });
  712. renderWithTracker({
  713. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  714. onClose,
  715. });
  716. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  717. const user = userEvent.setup();
  718. const selects = presetSelects();
  719. await user.selectOptions(selects[0], 'standard:Bambu Lab X1 Carbon 0.4 nozzle');
  720. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  721. await waitFor(() => {
  722. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  723. 100,
  724. expect.objectContaining({
  725. printer_preset: { source: 'standard', id: 'Bambu Lab X1 Carbon 0.4 nozzle' },
  726. }),
  727. );
  728. });
  729. });
  730. it('routes archive sources to sliceArchive instead of sliceLibraryFile', async () => {
  731. const onClose = vi.fn();
  732. mockApi.sliceArchive.mockResolvedValue({
  733. job_id: 7,
  734. status: 'pending',
  735. status_url: '/api/v1/slice-jobs/7',
  736. });
  737. renderWithTracker({
  738. source: { kind: 'archive', id: 86, filename: 'orca.3mf' },
  739. onClose,
  740. });
  741. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  742. const user = userEvent.setup();
  743. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  744. await waitFor(() => {
  745. expect(mockApi.sliceArchive).toHaveBeenCalledWith(86, expect.any(Object));
  746. expect(mockApi.sliceLibraryFile).not.toHaveBeenCalled();
  747. });
  748. });
  749. it('surfaces enqueue errors inline and keeps the modal open', async () => {
  750. const onClose = vi.fn();
  751. mockApi.sliceLibraryFile.mockRejectedValue(new Error('Server says no'));
  752. renderWithTracker({
  753. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  754. onClose,
  755. });
  756. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  757. const user = userEvent.setup();
  758. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  759. await waitFor(() => {
  760. expect(screen.getByRole('alert')).toHaveTextContent('Server says no');
  761. });
  762. expect(onClose).not.toHaveBeenCalled();
  763. });
  764. it('shows a friendly notice when getSlicerPresets fails', async () => {
  765. mockApi.getSlicerPresets.mockRejectedValue(new Error('500'));
  766. renderWithTracker({
  767. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  768. onClose: vi.fn(),
  769. });
  770. await waitFor(() => {
  771. expect(screen.getByRole('alert')).toHaveTextContent(/Failed to load presets/i);
  772. });
  773. });
  774. it('omits the cloud banner when status is not_authenticated (#1712)', async () => {
  775. // A signed-out user (Bambu or Orca) shouldn't get a permanent "sign in"
  776. // nag at the top of every slice. Sign-in lives on the Profiles page; the
  777. // modal stays silent unless a previously-signed-in session actually broke
  778. // (expired / unreachable).
  779. mockApi.getSlicerPresets.mockResolvedValue(
  780. makeUnified({
  781. cloud_status: 'not_authenticated',
  782. orca_cloud_status: 'not_authenticated',
  783. local: fullThreeTier.local,
  784. standard: fullThreeTier.standard,
  785. }),
  786. );
  787. renderWithTracker({
  788. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  789. onClose: vi.fn(),
  790. });
  791. await waitFor(() => expect(screen.getByText('Imported X1C 0.4')).toBeDefined());
  792. expect(screen.queryByRole('status')).toBeNull();
  793. });
  794. it('renders an "expired" banner when cloud_status is expired', async () => {
  795. mockApi.getSlicerPresets.mockResolvedValue(
  796. makeUnified({
  797. cloud_status: 'expired',
  798. local: fullThreeTier.local,
  799. }),
  800. );
  801. renderWithTracker({
  802. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  803. onClose: vi.fn(),
  804. });
  805. await waitFor(() => {
  806. expect(screen.getByRole('status')).toHaveTextContent(/expired/i);
  807. });
  808. });
  809. it('omits the banner entirely when cloud_status is ok', async () => {
  810. renderWithTracker({
  811. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  812. onClose: vi.fn(),
  813. });
  814. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  815. // No status-role banner should be rendered on the happy path.
  816. expect(screen.queryByRole('status')).toBeNull();
  817. });
  818. // ----- Multi-plate flow -----------------------------------------------
  819. function makeMultiPlateLibraryResponse() {
  820. return {
  821. file_id: 100,
  822. filename: 'Multi.3mf',
  823. is_multi_plate: true,
  824. plates: [
  825. {
  826. index: 1,
  827. name: 'Plate 1',
  828. objects: ['Cube'],
  829. object_count: 1,
  830. has_thumbnail: false,
  831. thumbnail_url: null,
  832. print_time_seconds: 600,
  833. filament_used_grams: 10,
  834. filaments: [],
  835. },
  836. {
  837. index: 2,
  838. name: 'Plate 2',
  839. objects: ['Pyramid'],
  840. object_count: 1,
  841. has_thumbnail: false,
  842. thumbnail_url: null,
  843. print_time_seconds: 800,
  844. filament_used_grams: 12,
  845. filaments: [],
  846. },
  847. ],
  848. };
  849. }
  850. it('shows the plate picker first for multi-plate library files', async () => {
  851. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  852. renderWithTracker({
  853. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  854. onClose: vi.fn(),
  855. });
  856. // Plate picker renders one button per plate — the accessible name
  857. // joins the heading ("Plate N — name") with the object summary line.
  858. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  859. expect(screen.getByRole('button', { name: /Plate 2.*Pyramid/ })).toBeDefined();
  860. // Profile dropdowns must NOT be visible yet — the user has to pick a
  861. // plate first.
  862. expect(screen.queryByRole('combobox')).toBeNull();
  863. });
  864. it('skips the plate picker for single-plate sources', async () => {
  865. mockApi.getLibraryFilePlates.mockResolvedValue({
  866. file_id: 100,
  867. filename: 'Single.3mf',
  868. is_multi_plate: false,
  869. plates: [
  870. {
  871. index: 1,
  872. name: 'Plate 1',
  873. objects: [],
  874. has_thumbnail: false,
  875. thumbnail_url: null,
  876. print_time_seconds: null,
  877. filament_used_grams: null,
  878. filaments: [],
  879. },
  880. ],
  881. });
  882. renderWithTracker({
  883. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  884. onClose: vi.fn(),
  885. });
  886. // Should jump straight to the profile dropdowns.
  887. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  888. });
  889. it('passes the picked plate to the slice request', async () => {
  890. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  891. mockApi.sliceLibraryFile.mockResolvedValue({
  892. job_id: 42,
  893. status: 'pending',
  894. status_url: '/api/v1/slice-jobs/42',
  895. });
  896. renderWithTracker({
  897. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  898. onClose: vi.fn(),
  899. });
  900. const user = userEvent.setup();
  901. // Step 1: pick Plate 2.
  902. const plate2Button = await screen.findByRole('button', { name: /Plate 2.*Pyramid/ });
  903. await user.click(plate2Button);
  904. // Step 2: profile dropdowns are now visible.
  905. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  906. // Step 3: submit and verify the plate index made it into the body.
  907. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  908. await waitFor(() => {
  909. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  910. 100,
  911. expect.objectContaining({ plate: 2 }),
  912. );
  913. });
  914. });
  915. it('"Slice all plates" toggle sends plate=0 sentinel to the backend (#1493)', async () => {
  916. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  917. mockApi.sliceLibraryFile.mockResolvedValue({
  918. job_id: 42,
  919. status: 'pending',
  920. status_url: '/api/v1/slice-jobs/42',
  921. });
  922. renderWithTracker({
  923. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  924. onClose: vi.fn(),
  925. });
  926. const user = userEvent.setup();
  927. const plate1Button = await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  928. await user.click(plate1Button);
  929. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  930. // The "Slice all plates" checkbox only appears for multi-plate sources.
  931. const toggle = await screen.findByRole('checkbox', { name: /Slice all 2 plates/i });
  932. await user.click(toggle);
  933. // The action button's label flips to the "Slice all" form. Click it.
  934. await user.click(screen.getByRole('button', { name: /Slice all 2 plates/i }));
  935. await waitFor(() => {
  936. expect(mockApi.sliceLibraryFile).toHaveBeenCalledTimes(1);
  937. });
  938. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  939. // ``plate=0`` is the BS CLI's all-plates sentinel — one slice call,
  940. // one output 3MF with every plate's gcode inside, one archive.
  941. expect((body as { plate?: number }).plate).toBe(0);
  942. });
  943. it('"Slice all plates" toggle is hidden for single-plate sources', async () => {
  944. mockApi.getLibraryFilePlates.mockResolvedValue({
  945. file_id: 100,
  946. filename: 'Single.3mf',
  947. is_multi_plate: false,
  948. plates: [
  949. {
  950. index: 1,
  951. name: 'Plate 1',
  952. objects: [],
  953. has_thumbnail: false,
  954. thumbnail_url: null,
  955. print_time_seconds: null,
  956. filament_used_grams: null,
  957. filaments: [],
  958. },
  959. ],
  960. });
  961. renderWithTracker({
  962. source: { kind: 'libraryFile', id: 100, filename: 'Single.3mf' },
  963. onClose: vi.fn(),
  964. });
  965. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  966. expect(screen.queryByRole('checkbox', { name: /Slice all/i })).toBeNull();
  967. });
  968. it('routes the plate fetch through getArchivePlates for archive sources', async () => {
  969. mockApi.getArchivePlates.mockResolvedValue({
  970. ...makeMultiPlateLibraryResponse(),
  971. archive_id: 100,
  972. filename: 'Multi.3mf',
  973. });
  974. renderWithTracker({
  975. source: { kind: 'archive', id: 100, filename: 'Multi.3mf' },
  976. onClose: vi.fn(),
  977. });
  978. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  979. expect(mockApi.getArchivePlates).toHaveBeenCalledWith(100);
  980. expect(mockApi.getLibraryFilePlates).not.toHaveBeenCalled();
  981. });
  982. it('cancelling the plate picker closes the entire slice flow', async () => {
  983. const onClose = vi.fn();
  984. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiPlateLibraryResponse());
  985. renderWithTracker({
  986. source: { kind: 'libraryFile', id: 100, filename: 'Multi.3mf' },
  987. onClose,
  988. });
  989. await screen.findByRole('button', { name: /Plate 1.*Cube/ });
  990. const user = userEvent.setup();
  991. await user.click(screen.getByRole('button', { name: /^Close$/i }));
  992. expect(onClose).toHaveBeenCalled();
  993. });
  994. it('omits the plate field when the source is single-plate', async () => {
  995. mockApi.sliceLibraryFile.mockResolvedValue({
  996. job_id: 42,
  997. status: 'pending',
  998. status_url: '/api/v1/slice-jobs/42',
  999. });
  1000. renderWithTracker({
  1001. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1002. onClose: vi.fn(),
  1003. });
  1004. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1005. const user = userEvent.setup();
  1006. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1007. await waitFor(() => {
  1008. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1009. expect(body).not.toHaveProperty('plate');
  1010. });
  1011. });
  1012. // ----- Multi-color flow ------------------------------------------------
  1013. function makeMultiColorPlateResponse() {
  1014. // Single-plate 3MF that uses two filament slots — mirrors the realistic
  1015. // "I have a multi-color file with one plate" case. Multi-plate is a
  1016. // separate axis that's already covered above.
  1017. return {
  1018. file_id: 100,
  1019. filename: 'TwoColor.3mf',
  1020. is_multi_plate: false,
  1021. plates: [
  1022. {
  1023. index: 1,
  1024. name: 'Plate 1',
  1025. objects: ['Logo'],
  1026. object_count: 1,
  1027. has_thumbnail: false,
  1028. thumbnail_url: null,
  1029. print_time_seconds: 600,
  1030. filament_used_grams: 20,
  1031. filaments: [],
  1032. },
  1033. ],
  1034. };
  1035. }
  1036. function makeMultiColorRequirementsResponse() {
  1037. return {
  1038. file_id: 100,
  1039. filename: 'TwoColor.3mf',
  1040. plate_id: 1,
  1041. filaments: [
  1042. { slot_id: 1, type: 'PLA', color: '#000000', used_grams: 10, used_meters: 3 },
  1043. { slot_id: 2, type: 'PLA', color: '#FFFFFF', used_grams: 10, used_meters: 3 },
  1044. ],
  1045. };
  1046. }
  1047. function makeColorAwarePresets(): UnifiedPresetsResponse {
  1048. // Two filament presets in cloud: one black PLA, one white PLA. Pre-pick
  1049. // should match each plate slot to the same-colour preset so the user
  1050. // doesn't have to manually align them.
  1051. return {
  1052. orca_cloud: { printer: [], process: [], filament: [] },
  1053. cloud: {
  1054. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1055. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1056. filament: [
  1057. { id: 'F-BLACK', name: 'Cloud PLA Black', source: 'cloud', filament_type: 'PLA', filament_colour: '#000000' },
  1058. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  1059. ],
  1060. },
  1061. local: { printer: [], process: [], filament: [] },
  1062. standard: { printer: [], process: [], filament: [] },
  1063. cloud_status: 'ok',
  1064. orca_cloud_status: 'ok',
  1065. };
  1066. }
  1067. it('renders one filament dropdown per plate slot when the source is multi-color', async () => {
  1068. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1069. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1070. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1071. renderWithTracker({
  1072. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1073. onClose: vi.fn(),
  1074. });
  1075. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1076. // 1 printer + 1 process + 2 filament + 1 bed-type (#1337) = 5 dropdowns.
  1077. expect(presetSelects()).toHaveLength(5);
  1078. });
  1079. it('pre-picks each filament slot by matching colour metadata', async () => {
  1080. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1081. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1082. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1083. mockApi.sliceLibraryFile.mockResolvedValue({
  1084. job_id: 42,
  1085. status: 'pending',
  1086. status_url: '/api/v1/slice-jobs/42',
  1087. });
  1088. renderWithTracker({
  1089. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1090. onClose: vi.fn(),
  1091. });
  1092. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1093. const user = userEvent.setup();
  1094. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1095. await waitFor(() => {
  1096. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1097. // Slot 1 was black plate → cloud black preset; slot 2 was white →
  1098. // cloud white preset. Pre-pick aligns them by metadata so the user
  1099. // doesn't have to swap them manually.
  1100. expect(body.filament_presets).toEqual([
  1101. { source: 'cloud', id: 'F-BLACK' },
  1102. { source: 'cloud', id: 'F-WHITE' },
  1103. ]);
  1104. });
  1105. });
  1106. it('still sends the legacy filament_preset for single-color flows', async () => {
  1107. // Backwards-compat with backends / proxies that read the singular field.
  1108. mockApi.sliceLibraryFile.mockResolvedValue({
  1109. job_id: 42,
  1110. status: 'pending',
  1111. status_url: '/api/v1/slice-jobs/42',
  1112. });
  1113. renderWithTracker({
  1114. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1115. onClose: vi.fn(),
  1116. });
  1117. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1118. const user = userEvent.setup();
  1119. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1120. await waitFor(() => {
  1121. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1122. // Single-color path mirrors the array's first entry into the legacy
  1123. // singular so older backend clients that only know about
  1124. // `filament_preset` still work.
  1125. expect(body.filament_preset).toEqual(body.filament_presets[0]);
  1126. expect(body.filament_presets).toHaveLength(1);
  1127. });
  1128. });
  1129. it('lets the user override a pre-picked filament slot', async () => {
  1130. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1131. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1132. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1133. mockApi.sliceLibraryFile.mockResolvedValue({
  1134. job_id: 42,
  1135. status: 'pending',
  1136. status_url: '/api/v1/slice-jobs/42',
  1137. });
  1138. renderWithTracker({
  1139. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1140. onClose: vi.fn(),
  1141. });
  1142. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1143. const user = userEvent.setup();
  1144. const selects = presetSelects();
  1145. // Order: 0 printer, 1 process, 2 bed-type, 3 filament-1, 4 filament-2
  1146. // (#1337). Auto-picks land on printer/process/filaments; bed-type
  1147. // defaults to "". Swap filament-1 (index 3) from the auto-picked black
  1148. // to white.
  1149. await user.selectOptions(selects[3], 'cloud:F-WHITE');
  1150. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1151. await waitFor(() => {
  1152. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1153. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  1154. // Slot 1 stayed at the auto-picked white.
  1155. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  1156. });
  1157. });
  1158. // Cross-printer re-slicing is a normal, supported operation as of
  1159. // 2026-05-20 (Step 0 empirical test: sidecar overrides printer / process
  1160. // / bed / kinematics from the picked profile triplet, producing valid
  1161. // target-printer G-code). No banner, no warning — the picker UI already
  1162. // shows which printer the user picked, and that's enough.
  1163. it('does not surface any cross-printer banner and keeps Slice enabled when models differ', async () => {
  1164. mockApi.getLibraryFilePlates.mockResolvedValue({
  1165. file_id: 100,
  1166. filename: 'A1Original.3mf',
  1167. is_multi_plate: false,
  1168. plates: [
  1169. {
  1170. index: 1,
  1171. name: 'Plate 1',
  1172. objects: [],
  1173. has_thumbnail: false,
  1174. thumbnail_url: null,
  1175. print_time_seconds: null,
  1176. filament_used_grams: null,
  1177. filaments: [],
  1178. },
  1179. ],
  1180. });
  1181. // Standard tier offers an X1C profile — the user picks (auto-picks) it.
  1182. mockApi.getSlicerPresets.mockResolvedValue(makeUnified({
  1183. standard: {
  1184. printer: [{ id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' }],
  1185. process: [{ id: '0.20mm Standard', name: '0.20mm Standard', source: 'standard' }],
  1186. filament: [{ id: 'Bambu PLA Basic', name: 'Bambu PLA Basic', source: 'standard' }],
  1187. },
  1188. }));
  1189. renderWithTracker({
  1190. source: { kind: 'libraryFile', id: 100, filename: 'A1Original.3mf' },
  1191. onClose: vi.fn(),
  1192. });
  1193. await waitFor(() =>
  1194. expect(screen.getByText('Bambu Lab X1 Carbon 0.4 nozzle')).toBeDefined(),
  1195. );
  1196. // No banner, no alert — re-slicing across printers is just a normal slice now.
  1197. expect(screen.queryByRole('alert')).toBeNull();
  1198. const sliceButton = screen.getByRole('button', { name: /^Slice$/ }) as HTMLButtonElement;
  1199. expect(sliceButton.disabled).toBe(false);
  1200. });
  1201. // The `used_in_plate` flag tells the modal which AMS slots are
  1202. // actually consumed by the picked plate. Slots flagged as unused
  1203. // are still rendered (the slicer CLI needs a profile per project
  1204. // slot, otherwise it silently fills the gap from embedded defaults
  1205. // and unwanted colours leak into the output) but disabled in the UI
  1206. // so the user only interacts with the dropdowns that matter.
  1207. it('disables filament dropdowns for slots not used by the picked plate', async () => {
  1208. mockApi.getLibraryFilePlates.mockResolvedValue({
  1209. file_id: 100,
  1210. filename: 'Helmet.3mf',
  1211. is_multi_plate: false,
  1212. plates: [
  1213. {
  1214. index: 1,
  1215. name: 'Plate 1',
  1216. objects: ['Helmet'],
  1217. has_thumbnail: false,
  1218. thumbnail_url: null,
  1219. print_time_seconds: 1200,
  1220. filament_used_grams: 80,
  1221. filaments: [],
  1222. },
  1223. ],
  1224. });
  1225. // Project has 2 AMS slots configured (white + grey support), but
  1226. // plate 1 only paints with white (slot 1). The backend now returns
  1227. // BOTH slots with used_in_plate flagging the difference.
  1228. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1229. file_id: 100,
  1230. filename: 'Helmet.3mf',
  1231. plate_id: 1,
  1232. filaments: [
  1233. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  1234. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  1235. ],
  1236. });
  1237. mockApi.getSlicerPresets.mockResolvedValue({
  1238. cloud: {
  1239. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1240. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1241. filament: [
  1242. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  1243. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  1244. ],
  1245. },
  1246. local: { printer: [], process: [], filament: [] },
  1247. standard: { printer: [], process: [], filament: [] },
  1248. cloud_status: 'ok',
  1249. orca_cloud: { printer: [], process: [], filament: [] },
  1250. orca_cloud_status: 'ok',
  1251. });
  1252. renderWithTracker({
  1253. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  1254. onClose: vi.fn(),
  1255. });
  1256. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1257. // Both filament rows render — 1 printer + 1 process + 1 bed-type +
  1258. // 2 filament (#1337) = 5. bed-type sits at index 2, filament slots
  1259. // follow at 3 and 4.
  1260. const selects = presetSelects();
  1261. expect(selects).toHaveLength(5);
  1262. // Slot 1 (used) is editable, slot 2 (not used) is disabled.
  1263. expect(selects[3].disabled).toBe(false);
  1264. expect(selects[4].disabled).toBe(true);
  1265. // The disabled row's label calls out why it's disabled.
  1266. expect(screen.getByText(/not used by this plate/i)).toBeDefined();
  1267. });
  1268. it('still sends both filaments to the backend even when one slot is disabled', async () => {
  1269. // The auto-pick scoring fills the disabled slot from project
  1270. // metadata — the slicer CLI requires a profile for every project
  1271. // slot, otherwise it silently fills the gap. The disabled UI is
  1272. // purely cosmetic; the wire format must include the full list.
  1273. mockApi.getLibraryFilePlates.mockResolvedValue({
  1274. file_id: 100,
  1275. filename: 'Helmet.3mf',
  1276. is_multi_plate: false,
  1277. plates: [
  1278. {
  1279. index: 1,
  1280. name: 'Plate 1',
  1281. objects: ['Helmet'],
  1282. has_thumbnail: false,
  1283. thumbnail_url: null,
  1284. print_time_seconds: 1200,
  1285. filament_used_grams: 80,
  1286. filaments: [],
  1287. },
  1288. ],
  1289. });
  1290. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1291. file_id: 100,
  1292. filename: 'Helmet.3mf',
  1293. plate_id: 1,
  1294. filaments: [
  1295. { slot_id: 1, type: 'PLA', color: '#FFFFFF', used_grams: 80, used_meters: 27, used_in_plate: true },
  1296. { slot_id: 2, type: 'PLA', color: '#808080', used_grams: 0, used_meters: 0, used_in_plate: false },
  1297. ],
  1298. });
  1299. mockApi.getSlicerPresets.mockResolvedValue({
  1300. cloud: {
  1301. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1302. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1303. filament: [
  1304. { id: 'F-WHITE', name: 'Cloud PLA White', source: 'cloud', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  1305. { id: 'F-GREY', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#808080' },
  1306. ],
  1307. },
  1308. local: { printer: [], process: [], filament: [] },
  1309. standard: { printer: [], process: [], filament: [] },
  1310. cloud_status: 'ok',
  1311. orca_cloud: { printer: [], process: [], filament: [] },
  1312. orca_cloud_status: 'ok',
  1313. });
  1314. mockApi.sliceLibraryFile.mockResolvedValue({
  1315. job_id: 50,
  1316. status: 'pending',
  1317. status_url: '/api/v1/slice-jobs/50',
  1318. });
  1319. renderWithTracker({
  1320. source: { kind: 'libraryFile', id: 100, filename: 'Helmet.3mf' },
  1321. onClose: vi.fn(),
  1322. });
  1323. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1324. const user = userEvent.setup();
  1325. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1326. await waitFor(() => {
  1327. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1328. // Both slots populated: slot 1 with the user's white pick, slot
  1329. // 2 auto-picked with grey from the colour-match scoring.
  1330. expect(body.filament_presets).toHaveLength(2);
  1331. expect(body.filament_presets[0]).toEqual({ source: 'cloud', id: 'F-WHITE' });
  1332. expect(body.filament_presets[1]).toEqual({ source: 'cloud', id: 'F-GREY' });
  1333. });
  1334. });
  1335. // #2712: the filament list is positional the whole way down — index 0 is
  1336. // slot 1, and the backend forwards it as filament_1.json..filament_N.json.
  1337. // A MakerWorld source that ships slice_info and paints with slot 4 alone
  1338. // used to yield a one-row list, so the user's only pick was bound to slot 1
  1339. // and slot 4 sliced with the source's embedded default: picking PETG gave a
  1340. // PLA print. The modal now asks for every project slot so the positions
  1341. // line up.
  1342. it('requests every project slot, not just the ones the plate prints with', async () => {
  1343. renderWithTracker({
  1344. source: { kind: 'libraryFile', id: 100, filename: 'Tunnel.3mf' },
  1345. onClose: vi.fn(),
  1346. });
  1347. await waitFor(() => expect(mockApi.getLibraryFileFilamentRequirements).toHaveBeenCalled());
  1348. const [, plateArg, , fullSlots] = mockApi.getLibraryFileFilamentRequirements.mock.calls[0];
  1349. expect(plateArg).toBe(1);
  1350. expect(fullSlots).toBe(true);
  1351. });
  1352. it('sends the pick for a high-numbered slot at its own index', async () => {
  1353. // Four project slots, only slot 4 printed. The pick must arrive as the
  1354. // FOURTH entry; anywhere else and the slicer binds it to the wrong slot.
  1355. mockApi.getLibraryFilePlates.mockResolvedValue({
  1356. file_id: 100,
  1357. filename: 'Tunnel.3mf',
  1358. is_multi_plate: false,
  1359. plates: [
  1360. {
  1361. index: 1,
  1362. name: 'Plate 1',
  1363. objects: ['Tunnel'],
  1364. has_thumbnail: false,
  1365. thumbnail_url: null,
  1366. print_time_seconds: 1200,
  1367. filament_used_grams: 105,
  1368. filaments: [],
  1369. },
  1370. ],
  1371. });
  1372. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1373. file_id: 100,
  1374. filename: 'Tunnel.3mf',
  1375. plate_id: 1,
  1376. filaments: [
  1377. { slot_id: 1, type: 'PLA', color: '#38CC0A', used_grams: 0, used_meters: 0, used_in_plate: false },
  1378. { slot_id: 2, type: 'PLA', color: '#161616', used_grams: 0, used_meters: 0, used_in_plate: false },
  1379. { slot_id: 3, type: 'PLA', color: '#898989', used_grams: 0, used_meters: 0, used_in_plate: false },
  1380. { slot_id: 4, type: 'PLA', color: '#898989', used_grams: 105, used_meters: 35, used_in_plate: true },
  1381. ],
  1382. });
  1383. mockApi.getSlicerPresets.mockResolvedValue({
  1384. cloud: {
  1385. printer: [{ id: 'P1', name: 'X1C', source: 'cloud' }],
  1386. process: [{ id: 'PR1', name: '0.20mm', source: 'cloud' }],
  1387. filament: [
  1388. { id: 'F-PLA', name: 'Cloud PLA Grey', source: 'cloud', filament_type: 'PLA', filament_colour: '#898989' },
  1389. { id: 'F-PETG', name: 'Cloud PETG', source: 'cloud', filament_type: 'PETG', filament_colour: '#00FF00' },
  1390. ],
  1391. },
  1392. local: { printer: [], process: [], filament: [] },
  1393. standard: { printer: [], process: [], filament: [] },
  1394. cloud_status: 'ok',
  1395. orca_cloud: { printer: [], process: [], filament: [] },
  1396. orca_cloud_status: 'ok',
  1397. });
  1398. mockApi.sliceLibraryFile.mockResolvedValue({
  1399. job_id: 51,
  1400. status: 'pending',
  1401. status_url: '/api/v1/slice-jobs/51',
  1402. });
  1403. renderWithTracker({
  1404. source: { kind: 'libraryFile', id: 100, filename: 'Tunnel.3mf' },
  1405. onClose: vi.fn(),
  1406. });
  1407. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1408. // 1 printer + 1 process + 1 bed-type + 4 filament rows.
  1409. const selects = presetSelects();
  1410. expect(selects).toHaveLength(7);
  1411. // Only slot 4 is selectable — the other three are the padding.
  1412. expect([selects[3].disabled, selects[4].disabled, selects[5].disabled]).toEqual([true, true, true]);
  1413. expect(selects[6].disabled).toBe(false);
  1414. const user = userEvent.setup();
  1415. await user.selectOptions(selects[6], 'cloud:F-PETG');
  1416. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1417. await waitFor(() => {
  1418. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1419. expect(body.filament_presets).toHaveLength(4);
  1420. expect(body.filament_presets[3]).toEqual({ source: 'cloud', id: 'F-PETG' });
  1421. });
  1422. });
  1423. // ------------------------------------------------------------------
  1424. // Slicer Pipelines (#1425) — Apply / Save integration in SliceModal
  1425. // ------------------------------------------------------------------
  1426. it('Apply pipeline dropdown is disabled and shows empty hint when no pipelines exist', async () => {
  1427. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1428. renderWithTracker({
  1429. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1430. onClose: vi.fn(),
  1431. });
  1432. await waitFor(() => {
  1433. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  1434. expect(select.disabled).toBe(true);
  1435. expect(select.querySelector('option')?.textContent).toMatch(/No saved pipelines/i);
  1436. });
  1437. });
  1438. it('applies a saved pipeline to printer, process, and bed_type slots on selection', async () => {
  1439. mockApi.listSlicerPipelines.mockResolvedValue({
  1440. pipelines: [
  1441. {
  1442. id: 7,
  1443. name: 'Production Batch',
  1444. description: null,
  1445. printer_preset: { source: 'local', id: '1' },
  1446. process_preset: { source: 'local', id: '2' },
  1447. filament_presets: [{ source: 'local', id: '3' }],
  1448. bed_type: 'Textured PEI Plate',
  1449. target_kind: 'printer_class',
  1450. target_printer_id: null,
  1451. target_model_class: null,
  1452. fanout_strategy: 'max_parallel',
  1453. created_by: null,
  1454. created_at: '2026-06-27T00:00:00Z',
  1455. updated_at: '2026-06-27T00:00:00Z',
  1456. },
  1457. ],
  1458. });
  1459. renderWithTracker({
  1460. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1461. onClose: vi.fn(),
  1462. });
  1463. // Wait for presets + pipelines listing to populate the modal.
  1464. await waitFor(() => {
  1465. const select = screen.getByLabelText(/Apply pipeline/i) as HTMLSelectElement;
  1466. expect(select.disabled).toBe(false);
  1467. expect(within(select).getByText('Production Batch')).toBeDefined();
  1468. });
  1469. const user = userEvent.setup();
  1470. await user.selectOptions(screen.getByLabelText(/Apply pipeline/i), '7');
  1471. // After applying, submitting the slice request should carry the
  1472. // pipeline's preset refs end-to-end.
  1473. mockApi.sliceLibraryFile.mockResolvedValue({
  1474. job_id: 42,
  1475. status: 'queued',
  1476. status_url: '/api/v1/slice-jobs/42',
  1477. });
  1478. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1479. await waitFor(() => {
  1480. expect(mockApi.sliceLibraryFile).toHaveBeenCalled();
  1481. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  1482. expect(body.printer_preset).toEqual({ source: 'local', id: '1' });
  1483. expect(body.process_preset).toEqual({ source: 'local', id: '2' });
  1484. expect(body.filament_presets[0]).toEqual({ source: 'local', id: '3' });
  1485. expect(body.bed_type).toBe('Textured PEI Plate');
  1486. });
  1487. });
  1488. it('saves the current four-slot selection as a new pipeline when the user clicks Save as pipeline', async () => {
  1489. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1490. mockApi.createSlicerPipeline.mockResolvedValue({
  1491. id: 99,
  1492. name: 'My Default',
  1493. description: null,
  1494. printer_preset: { source: 'local', id: '1' },
  1495. process_preset: { source: 'local', id: '2' },
  1496. filament_presets: [{ source: 'local', id: '3' }],
  1497. bed_type: null,
  1498. target_kind: 'printer_class',
  1499. target_printer_id: null,
  1500. target_model_class: null,
  1501. fanout_strategy: 'max_parallel',
  1502. created_by: null,
  1503. created_at: '2026-06-27T00:00:00Z',
  1504. updated_at: '2026-06-27T00:00:00Z',
  1505. });
  1506. renderWithTracker({
  1507. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1508. onClose: vi.fn(),
  1509. });
  1510. // Wait for auto-pick to populate all four slots from the fullThreeTier
  1511. // listing — then Save as pipeline becomes enabled.
  1512. const user = userEvent.setup();
  1513. let saveBtn: HTMLButtonElement;
  1514. await waitFor(() => {
  1515. saveBtn = screen.getByRole('button', { name: /^Save as pipeline$/ }) as HTMLButtonElement;
  1516. expect(saveBtn.disabled).toBe(false);
  1517. });
  1518. await user.click(saveBtn!);
  1519. const nameInput = screen.getByLabelText(/New pipeline name/i);
  1520. await user.type(nameInput, 'My Default');
  1521. await user.click(screen.getByRole('button', { name: /^Save$/ }));
  1522. await waitFor(() => {
  1523. expect(mockApi.createSlicerPipeline).toHaveBeenCalledTimes(1);
  1524. const body = mockApi.createSlicerPipeline.mock.calls[0][0];
  1525. expect(body.name).toBe('My Default');
  1526. // The four slots come from the auto-picked unified-presets listing —
  1527. // local tier wins per SLICE_MODAL_TIER_ORDER.
  1528. expect(body.printer_preset.source).toBe('local');
  1529. expect(body.process_preset.source).toBe('local');
  1530. expect(body.filament_presets[0].source).toBe('local');
  1531. });
  1532. });
  1533. /**
  1534. * Per-slot filament colour (#2977).
  1535. *
  1536. * A filament preset carries no colour in either slicer -- colour is a
  1537. * per-project property their GUIs set from the plate -- so a slice that
  1538. * supplies none records the CLI's compiled-in #00AE42 for every slot. That
  1539. * is what made every internal-slicer thumbnail Bambu green regardless of
  1540. * the filament picked, and what made the print dialog report a colour
  1541. * mismatch against the AMS slot it had just correctly mapped to.
  1542. *
  1543. * The swatch is the only place the colour can come from for an STL, which
  1544. * has none anywhere else, so it is offered for single-slot sources too.
  1545. */
  1546. describe('filament colour swatch', () => {
  1547. function colourInputs(): HTMLInputElement[] {
  1548. return screen
  1549. .getAllByLabelText('Filament colour')
  1550. .filter((el): el is HTMLInputElement => el instanceof HTMLInputElement);
  1551. }
  1552. it('shows the hex beside the swatch so it reads as a control, not a decoration', async () => {
  1553. // The reason this exists: a bare dot next to a label was taken for the
  1554. // read-only swatch multi-colour rows already had, so on the STL -- the
  1555. // one source with no colour to inherit -- nothing said it was settable.
  1556. renderWithTracker({
  1557. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1558. onClose: vi.fn(),
  1559. });
  1560. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1561. expect(screen.getByText('#00AE42')).toBeDefined();
  1562. });
  1563. it('puts the whole control on the dropdown row, not in the label', async () => {
  1564. // Sitting beside the <select> and styled like it is what makes it read
  1565. // as a control. In the label row it read as a caption on the label.
  1566. renderWithTracker({
  1567. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1568. onClose: vi.fn(),
  1569. });
  1570. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1571. const row = colourInputs()[0].closest('div');
  1572. expect(row?.querySelector('select')).not.toBeNull();
  1573. });
  1574. it('wraps the swatch and its hex in one label bound to the input', async () => {
  1575. // So a click anywhere on the pill opens the picker, rather than only a
  1576. // 16px dot being live.
  1577. renderWithTracker({
  1578. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1579. onClose: vi.fn(),
  1580. });
  1581. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1582. const pill = screen.getByText('#00AE42').closest('label');
  1583. expect(pill?.getAttribute('for')).toBe(colourInputs()[0].id);
  1584. expect(pill?.contains(colourInputs()[0])).toBe(true);
  1585. });
  1586. it('the hex follows the swatch when it is changed', 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. fireEvent.change(colourInputs()[0], { target: { value: '#e8b00c' } });
  1593. await waitFor(() => expect(screen.getByText('#E8B00C')).toBeDefined());
  1594. });
  1595. it('paints the colour on the input itself, not only via the native swatch', async () => {
  1596. // An engine that does not paint ::-webkit-color-swatch would otherwise
  1597. // leave an empty ring, which is indistinguishable from no swatch.
  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].style.backgroundColor).not.toBe('');
  1604. });
  1605. it('offers a colour swatch for a single-slot STL, which has no colour of its own', async () => {
  1606. renderWithTracker({
  1607. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1608. onClose: vi.fn(),
  1609. });
  1610. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1611. expect(colourInputs()).toHaveLength(1);
  1612. });
  1613. it("shows the slicer's own default for a source that carries no colour", async () => {
  1614. // Not an invented placeholder: #00AE42 is exactly what the slice would
  1615. // record if nothing were sent, so the swatch tells the truth about the
  1616. // file that is about to be produced.
  1617. renderWithTracker({
  1618. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1619. onClose: vi.fn(),
  1620. });
  1621. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1622. expect(colourInputs()[0].value).toBe('#00ae42');
  1623. });
  1624. it("pre-fills each slot from the source plate's designed colour", async () => {
  1625. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1626. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1627. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1628. renderWithTracker({
  1629. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1630. onClose: vi.fn(),
  1631. });
  1632. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1633. expect(colourInputs().map((i) => i.value)).toEqual(['#000000', '#ffffff']);
  1634. });
  1635. it("sends the source plate's colours when the swatches are left alone", async () => {
  1636. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1637. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1638. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1639. mockApi.sliceLibraryFile.mockResolvedValue({
  1640. job_id: 42,
  1641. status: 'pending',
  1642. status_url: '/api/v1/slice-jobs/42',
  1643. });
  1644. renderWithTracker({
  1645. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1646. onClose: vi.fn(),
  1647. });
  1648. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1649. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  1650. await waitFor(() => {
  1651. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  1652. 100,
  1653. expect.objectContaining({ filament_colours: ['#000000', '#FFFFFF'] }),
  1654. );
  1655. });
  1656. });
  1657. it('sends an empty string for a slot with no colour, so the backend fallbacks still run', async () => {
  1658. // A sent colour outranks the preset's own default_filament_colour, so
  1659. // pinning the picker's displayed #00AE42 here would silently discard
  1660. // the real colour of an Orca-imported profile that carries one.
  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. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  1672. await waitFor(() => {
  1673. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  1674. 100,
  1675. expect.objectContaining({ filament_colours: [''] }),
  1676. );
  1677. });
  1678. });
  1679. it('sends the user\'s pick, upper-cased, once a swatch is changed', async () => {
  1680. mockApi.sliceLibraryFile.mockResolvedValue({
  1681. job_id: 42,
  1682. status: 'pending',
  1683. status_url: '/api/v1/slice-jobs/42',
  1684. });
  1685. renderWithTracker({
  1686. source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' },
  1687. onClose: vi.fn(),
  1688. });
  1689. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1690. const user = userEvent.setup();
  1691. // A colour input has no text entry; fire the change the picker would.
  1692. fireEvent.change(colourInputs()[0], { target: { value: '#e8b00c' } });
  1693. await waitFor(() => expect(colourInputs()[0].value).toBe('#e8b00c'));
  1694. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1695. await waitFor(() => {
  1696. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  1697. 100,
  1698. expect.objectContaining({ filament_colours: ['#E8B00C'] }),
  1699. );
  1700. });
  1701. });
  1702. it('only overrides the slot that was changed', async () => {
  1703. mockApi.getLibraryFilePlates.mockResolvedValue(makeMultiColorPlateResponse());
  1704. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(makeMultiColorRequirementsResponse());
  1705. mockApi.getSlicerPresets.mockResolvedValue(makeColorAwarePresets());
  1706. mockApi.sliceLibraryFile.mockResolvedValue({
  1707. job_id: 42,
  1708. status: 'pending',
  1709. status_url: '/api/v1/slice-jobs/42',
  1710. });
  1711. renderWithTracker({
  1712. source: { kind: 'libraryFile', id: 100, filename: 'TwoColor.3mf' },
  1713. onClose: vi.fn(),
  1714. });
  1715. await waitFor(() => expect(screen.getByText('X1C')).toBeDefined());
  1716. fireEvent.change(colourInputs()[1], { target: { value: '#112233' } });
  1717. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  1718. await waitFor(() => {
  1719. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  1720. 100,
  1721. expect.objectContaining({ filament_colours: ['#000000', '#112233'] }),
  1722. );
  1723. });
  1724. });
  1725. it('trims the alpha byte for display but submits the colour whole', async () => {
  1726. // The AMS reports colours with an alpha byte and a source 3MF can carry
  1727. // one; <input type="color"> accepts only the 6-digit form.
  1728. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1729. file_id: 100,
  1730. filename: 'Alpha.3mf',
  1731. plate_id: 1,
  1732. filaments: [{ slot_id: 1, type: 'PLA', color: '#E8B00CFF', used_grams: 10, used_meters: 3 }],
  1733. });
  1734. mockApi.sliceLibraryFile.mockResolvedValue({
  1735. job_id: 42,
  1736. status: 'pending',
  1737. status_url: '/api/v1/slice-jobs/42',
  1738. });
  1739. renderWithTracker({
  1740. source: { kind: 'libraryFile', id: 100, filename: 'Alpha.3mf' },
  1741. onClose: vi.fn(),
  1742. });
  1743. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1744. expect(colourInputs()[0].value).toBe('#e8b00c');
  1745. await userEvent.setup().click(screen.getByRole('button', { name: /^Slice$/ }));
  1746. await waitFor(() => {
  1747. expect(mockApi.sliceLibraryFile).toHaveBeenCalledWith(
  1748. 100,
  1749. expect.objectContaining({ filament_colours: ['#E8B00CFF'] }),
  1750. );
  1751. });
  1752. });
  1753. it('is disabled in "slice as designed" mode, which sends no filament profiles', async () => {
  1754. mockApi.getLibraryFilePlates.mockResolvedValue({
  1755. file_id: 100,
  1756. filename: 'Designed.3mf',
  1757. plates: [],
  1758. is_multi_plate: false,
  1759. embedded_printer: 'Imported X1C 0.4',
  1760. embedded_process: '0.20mm Standard',
  1761. });
  1762. renderWithTracker({
  1763. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  1764. onClose: vi.fn(),
  1765. });
  1766. await waitFor(() => expect(screen.getByText('My Custom X1C')).toBeDefined());
  1767. const toggle = screen.getByRole('checkbox', { name: /built-in settings/i });
  1768. expect(colourInputs()[0].disabled).toBe(false);
  1769. await userEvent.setup().click(toggle);
  1770. expect(colourInputs()[0].disabled).toBe(true);
  1771. });
  1772. });
  1773. });
  1774. // Pure-function tests for the filament slot picker. Pinned as a separate
  1775. // describe so the contract is visible without needing the modal mount.
  1776. /**
  1777. * The slice dialog switches to a two-column layout once there is room for it,
  1778. * and the process-settings panel then owns the right-hand column. The global
  1779. * test setup pins matchMedia to `matches: false`, so every other test in this
  1780. * file exercises the narrow single-stack path; these override it.
  1781. */
  1782. describe('SliceModal — process settings in "slice as designed" mode', () => {
  1783. beforeEach(() => {
  1784. vi.clearAllMocks();
  1785. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  1786. mockApi.getSlicerPresetValues.mockResolvedValue({ resolved: true, values: {}, reason: 'ok' });
  1787. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1788. mockApi.getSlicerPrinterModels.mockResolvedValue({});
  1789. mockApi.getLibraryFilePlates.mockResolvedValue({
  1790. file_id: 100,
  1791. filename: 'Designed.3mf',
  1792. plates: [],
  1793. is_multi_plate: false,
  1794. embedded_printer: 'Bambu Lab X1 Carbon 0.4 nozzle',
  1795. embedded_process: '0.20mm Standard',
  1796. });
  1797. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1798. file_id: 100, filename: 'Designed.3mf', plate_id: 1, filaments: [],
  1799. });
  1800. });
  1801. it('disables the panel rather than removing it', async () => {
  1802. const user = userEvent.setup();
  1803. renderWithTracker({
  1804. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  1805. onClose: vi.fn(),
  1806. });
  1807. const toggle = (await screen.findByLabelText(/Use the file's built-in settings/)) as HTMLInputElement;
  1808. const header = await screen.findByRole('button', { name: /Process settings/ });
  1809. await user.click(header);
  1810. const search = await screen.findByPlaceholderText('Search settings');
  1811. expect(search).toBeEnabled();
  1812. await user.click(toggle);
  1813. // Still on screen — hiding it made the dialog look like it had lost a
  1814. // feature — but nothing in it can be operated, because nothing in it is
  1815. // sent on this path.
  1816. expect(screen.getByPlaceholderText('Search settings')).toBeDisabled();
  1817. expect(screen.getByRole('button', { name: 'Expert' })).toBeDisabled();
  1818. expect(screen.getByText(/Not used while/)).toBeInTheDocument();
  1819. expect(screen.getByText('Inactive')).toBeInTheDocument();
  1820. });
  1821. it('sends no process overrides once the file drives the slice', async () => {
  1822. mockApi.sliceLibraryFile.mockResolvedValue({ job_id: 42, status: 'pending', status_url: '/x' });
  1823. const user = userEvent.setup();
  1824. renderWithTracker({
  1825. source: { kind: 'libraryFile', id: 100, filename: 'Designed.3mf' },
  1826. onClose: vi.fn(),
  1827. });
  1828. // Edit something, then hand the slice over to the file's own settings.
  1829. await user.click(await screen.findByRole('button', { name: /Process settings/ }));
  1830. const input = await screen.findByLabelText(/^Layer height/);
  1831. await user.clear(input);
  1832. await user.type(input, '0.16');
  1833. await user.click(screen.getByLabelText(/Use the file's built-in settings/));
  1834. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  1835. await waitFor(() => expect(mockApi.sliceLibraryFile).toHaveBeenCalled());
  1836. const payload = mockApi.sliceLibraryFile.mock.calls[0][1] as Record<string, unknown>;
  1837. expect(payload.use_embedded_settings).toBe(true);
  1838. expect(payload).not.toHaveProperty('process_overrides');
  1839. });
  1840. });
  1841. describe('SliceModal — process settings layout', () => {
  1842. const setViewport = (wide: boolean) => {
  1843. Object.defineProperty(window, 'matchMedia', {
  1844. writable: true,
  1845. value: (query: string) => ({
  1846. matches: wide,
  1847. media: query,
  1848. onchange: null,
  1849. addListener: () => {},
  1850. removeListener: () => {},
  1851. addEventListener: () => {},
  1852. removeEventListener: () => {},
  1853. dispatchEvent: () => true,
  1854. }),
  1855. });
  1856. };
  1857. beforeEach(() => {
  1858. vi.clearAllMocks();
  1859. mockApi.getSlicerPresets.mockResolvedValue(fullThreeTier);
  1860. mockApi.getSlicerPresetValues.mockResolvedValue({ resolved: true, values: {}, reason: 'ok' });
  1861. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1862. mockApi.getLibraryFilePlates.mockResolvedValue({
  1863. file_id: 100,
  1864. filename: 'Cube.stl',
  1865. plates: [],
  1866. is_multi_plate: false,
  1867. });
  1868. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1869. file_id: 100,
  1870. filename: 'Cube.stl',
  1871. plate_id: 1,
  1872. filaments: [],
  1873. });
  1874. });
  1875. afterEach(() => setViewport(false));
  1876. it('keeps the panel collapsed behind a disclosure in the narrow layout', async () => {
  1877. setViewport(false);
  1878. renderWithTracker({ source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' }, onClose: vi.fn() });
  1879. const header = await screen.findByRole('button', { name: /Process settings/ });
  1880. expect(header).toBeEnabled();
  1881. expect(screen.queryByPlaceholderText('Search settings')).not.toBeInTheDocument();
  1882. await userEvent.setup().click(header);
  1883. await waitFor(() => expect(screen.getByPlaceholderText('Search settings')).toBeInTheDocument());
  1884. });
  1885. it('opens the panel without a click once it has a column of its own', async () => {
  1886. setViewport(true);
  1887. renderWithTracker({ source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' }, onClose: vi.fn() });
  1888. // No disclosure to operate: the panel is the column, so its header is
  1889. // inert rather than offering to collapse something that has room.
  1890. await waitFor(() => expect(screen.getByPlaceholderText('Search settings')).toBeInTheDocument());
  1891. expect(screen.getByRole('button', { name: /Process settings/ })).toBeDisabled();
  1892. });
  1893. });
  1894. /**
  1895. * Process and filament lists hold back presets that resolve to a *different*
  1896. * printer, behind a per-slot "Show all". Two things must never be hidden: a
  1897. * preset whose compatibility is merely unknown, and whatever is currently
  1898. * selected.
  1899. */
  1900. describe('SliceModal — presets filtered by the selected printer', () => {
  1901. const presets: UnifiedPresetsResponse = {
  1902. cloud: { printer: [], process: [], filament: [] },
  1903. orca_cloud: { printer: [], process: [], filament: [] },
  1904. local: { printer: [], process: [], filament: [] },
  1905. standard: {
  1906. printer: [
  1907. { id: 'Bambu Lab X1 Carbon 0.4 nozzle', name: 'Bambu Lab X1 Carbon 0.4 nozzle', source: 'standard' },
  1908. ],
  1909. process: [
  1910. { id: 'p-x1c', name: '0.20mm Standard @BBL X1C', source: 'standard' },
  1911. { id: 'p-h2d', name: '0.20mm Standard @BBL H2D', source: 'standard' },
  1912. { id: 'p-a1m', name: '0.20mm Standard @BBL A1M', source: 'standard' },
  1913. // No printer tag at all — compatibility is unknown, never hidden.
  1914. { id: 'p-custom', name: 'My own profile', source: 'standard' },
  1915. ],
  1916. filament: [{ id: 'f-x1c', name: 'Bambu PLA Basic @BBL X1C', source: 'standard' }],
  1917. },
  1918. cloud_status: 'ok',
  1919. orca_cloud_status: 'ok',
  1920. } as UnifiedPresetsResponse;
  1921. const processOptionNames = () =>
  1922. Array.from(presetSelects()[1].options).map((o) => o.textContent);
  1923. beforeEach(() => {
  1924. vi.clearAllMocks();
  1925. mockApi.getSlicerPresets.mockResolvedValue(presets);
  1926. mockApi.getSlicerPrinterModels.mockResolvedValue({ 'Bambu Lab X1 Carbon': 'X1C' });
  1927. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  1928. mockApi.getLibraryFilePlates.mockResolvedValue({
  1929. file_id: 100, filename: 'Cube.stl', plates: [], is_multi_plate: false,
  1930. });
  1931. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue({
  1932. file_id: 100, filename: 'Cube.stl', plate_id: 1, filaments: [],
  1933. });
  1934. });
  1935. const open = async () => {
  1936. renderWithTracker({ source: { kind: 'libraryFile', id: 100, filename: 'Cube.stl' }, onClose: vi.fn() });
  1937. await waitFor(() => expect(presetSelects().length).toBeGreaterThan(1));
  1938. };
  1939. it('leaves out presets belonging to another printer', async () => {
  1940. await open();
  1941. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL X1C'));
  1942. expect(processOptionNames()).not.toContain('0.20mm Standard @BBL H2D');
  1943. expect(processOptionNames()).not.toContain('0.20mm Standard @BBL A1M');
  1944. });
  1945. it('keeps a preset whose compatibility cannot be determined', async () => {
  1946. await open();
  1947. // An untagged preset carries no evidence either way; hiding it would make
  1948. // a user's own imported profiles vanish.
  1949. await waitFor(() => expect(processOptionNames()).toContain('My own profile'));
  1950. });
  1951. it('says how many it held back and reveals them on request', async () => {
  1952. const user = userEvent.setup();
  1953. await open();
  1954. const hidden = await screen.findByText('2 hidden');
  1955. expect(hidden).toBeInTheDocument();
  1956. await user.click(within(hidden.parentElement as HTMLElement).getByRole('button', { name: 'Show all' }));
  1957. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  1958. expect(processOptionNames()).toContain('0.20mm Standard @BBL A1M');
  1959. });
  1960. it('collapses the list again on Show fewer', async () => {
  1961. const user = userEvent.setup();
  1962. await open();
  1963. await user.click((await screen.findAllByRole('button', { name: 'Show all' }))[0]);
  1964. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  1965. await user.click(screen.getAllByRole('button', { name: 'Show fewer' })[0]);
  1966. await waitFor(() => expect(processOptionNames()).not.toContain('0.20mm Standard @BBL H2D'));
  1967. });
  1968. it('never hides the preset that is currently selected', async () => {
  1969. const user = userEvent.setup();
  1970. await open();
  1971. // Reach a cross-printer preset, pick it, then collapse the list again.
  1972. await user.click((await screen.findAllByRole('button', { name: 'Show all' }))[0]);
  1973. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  1974. await user.selectOptions(presetSelects()[1], 'standard:p-h2d');
  1975. await user.click(screen.getAllByRole('button', { name: 'Show fewer' })[0]);
  1976. // Dropping it from the options would blank the select and silently discard
  1977. // a deliberate cross-printer choice.
  1978. await waitFor(() => expect(processOptionNames()).toContain('0.20mm Standard @BBL H2D'));
  1979. expect(presetSelects()[1].value).toBe('standard:p-h2d');
  1980. // The one still-hidden preset is counted; the selected one is not.
  1981. expect(screen.getByText('1 hidden')).toBeInTheDocument();
  1982. });
  1983. });
  1984. describe('pickFilamentForSlot — printer-compat contract (#1851)', () => {
  1985. // Index that recognises @BBL H2C / @BBL A1 tokens via the canonical
  1986. // PRINTER_MODEL_MAP. Real production data comes through
  1987. // ``api.getSlicerPrinterModels`` — the H2C / A1 fragments are the ones
  1988. // the production registry ships.
  1989. const index = buildCompatibilityIndex({
  1990. 'Bambu Lab A1': 'A1',
  1991. 'Bambu Lab H2C': 'H2C',
  1992. });
  1993. it('prefers a printer-compatible preset over a printer-mismatched one even with better colour match', () => {
  1994. // The OP scenario for #1851: a Bambu Lab A1 is selected; the unused-slot
  1995. // requirement carries the original H2C plate's PLA colour. With the
  1996. // legacy soft-penalty scoring an H2C-bound preset whose colour matches
  1997. // exactly could still rise above the A1-compatible PLA Basic whose
  1998. // colour doesn't, and then the unused-slot substitution propagated the
  1999. // H2C-bound preset across every unused slot — the CLI rejected with
  2000. // ``filament preset Generic PLA @BBL H2C (slot 1) is not compatible
  2001. // with printer Bambu Lab A1 0.4 nozzle``. The hard-skip contract makes
  2002. // sure a mismatched preset is never chosen while any compatible
  2003. // alternative exists, irrespective of metadata-score arithmetic.
  2004. const presets = makeUnified({
  2005. standard: {
  2006. printer: [],
  2007. process: [],
  2008. filament: [
  2009. {
  2010. id: 'Generic PLA @BBL H2C',
  2011. name: 'Generic PLA @BBL H2C',
  2012. source: 'standard',
  2013. filament_type: 'PLA',
  2014. filament_colour: '#FF0000',
  2015. },
  2016. {
  2017. id: 'Bambu PLA Basic @BBL A1',
  2018. name: 'Bambu PLA Basic @BBL A1',
  2019. source: 'standard',
  2020. filament_type: 'PLA',
  2021. filament_colour: '#FFFFFF',
  2022. },
  2023. ],
  2024. },
  2025. });
  2026. const pick = pickFilamentForSlot(
  2027. presets,
  2028. { type: 'PLA', color: '#FF0000' },
  2029. 'Bambu Lab A1 0.4 nozzle',
  2030. index,
  2031. );
  2032. expect(pick).toEqual({ source: 'standard', id: 'Bambu PLA Basic @BBL A1' });
  2033. });
  2034. it('falls back to a mismatched preset when no compatible alternative exists', () => {
  2035. // Graceful degrade: when every available preset is printer-mismatched,
  2036. // returning ``null`` would block the slice entirely. The picker keeps
  2037. // its old behaviour of returning the best-scoring mismatch so the user
  2038. // sees a populated dropdown they can correct, not an empty one.
  2039. const presets = makeUnified({
  2040. standard: {
  2041. printer: [],
  2042. process: [],
  2043. filament: [
  2044. {
  2045. id: 'Generic PLA @BBL H2C',
  2046. name: 'Generic PLA @BBL H2C',
  2047. source: 'standard',
  2048. filament_type: 'PLA',
  2049. filament_colour: '#FF0000',
  2050. },
  2051. ],
  2052. },
  2053. });
  2054. const pick = pickFilamentForSlot(
  2055. presets,
  2056. { type: 'PLA', color: '#FF0000' },
  2057. 'Bambu Lab A1 0.4 nozzle',
  2058. index,
  2059. );
  2060. expect(pick).toEqual({ source: 'standard', id: 'Generic PLA @BBL H2C' });
  2061. });
  2062. it('treats a no-printer-context call as no-mismatch (every preset eligible)', () => {
  2063. // ``printerName === null`` happens transiently on first render before the
  2064. // printer pre-pick effect has run. ``presetCompatibility`` returns
  2065. // ``unknown`` for every preset in that case, so the picker should just
  2066. // pick by metadata score with no compatibility filter active.
  2067. const presets = makeUnified({
  2068. standard: {
  2069. printer: [],
  2070. process: [],
  2071. filament: [
  2072. {
  2073. id: 'Generic PLA @BBL H2C',
  2074. name: 'Generic PLA @BBL H2C',
  2075. source: 'standard',
  2076. filament_type: 'PLA',
  2077. filament_colour: '#FF0000',
  2078. },
  2079. ],
  2080. },
  2081. });
  2082. const pick = pickFilamentForSlot(
  2083. presets,
  2084. { type: 'PLA', color: '#FF0000' },
  2085. null,
  2086. index,
  2087. );
  2088. expect(pick).toEqual({ source: 'standard', id: 'Generic PLA @BBL H2C' });
  2089. });
  2090. });
  2091. describe('pickFilamentForSlot — long-form printer tag (#2628)', () => {
  2092. const index = buildCompatibilityIndex({
  2093. 'Bambu Lab A1': 'A1',
  2094. 'Bambu Lab H2D': 'H2D',
  2095. });
  2096. it('never auto-picks a user-saved preset scoped to another printer', () => {
  2097. // michaelklos's registry: a cloud-tier user preset carrying the full
  2098. // "@Bambu Lab H2D 0.4 nozzle" tag outscores the A1 preset on tier bonus
  2099. // alone. Until the matcher learned the long form it classified 'unknown'
  2100. // — indistinguishable from compatible — so it won the slot, landed in a
  2101. // dropdown the modal disables (slot not used by the plate), and the CLI
  2102. // rejected the whole slice.
  2103. const presets = makeUnified({
  2104. cloud: {
  2105. printer: [],
  2106. process: [],
  2107. filament: [
  2108. {
  2109. id: 'sunlu-tpu-h2d',
  2110. name: 'SUNLU TPU 95A @Bambu Lab H2D 0.4 nozzle',
  2111. source: 'cloud',
  2112. filament_type: 'PLA',
  2113. filament_colour: '#FF0000',
  2114. },
  2115. ],
  2116. },
  2117. standard: {
  2118. printer: [],
  2119. process: [],
  2120. filament: [
  2121. {
  2122. id: 'Bambu PLA Basic @BBL A1',
  2123. name: 'Bambu PLA Basic @BBL A1',
  2124. source: 'standard',
  2125. filament_type: 'PLA',
  2126. filament_colour: '#FFFFFF',
  2127. },
  2128. ],
  2129. },
  2130. });
  2131. const pick = pickFilamentForSlot(
  2132. presets,
  2133. { type: 'PLA', color: '#FF0000' },
  2134. 'Bambu Lab A1 0.4 nozzle',
  2135. index,
  2136. );
  2137. expect(pick).toEqual({ source: 'standard', id: 'Bambu PLA Basic @BBL A1' });
  2138. });
  2139. it('still picks a long-form preset for its own printer', () => {
  2140. const presets = makeUnified({
  2141. cloud: {
  2142. printer: [],
  2143. process: [],
  2144. filament: [
  2145. {
  2146. id: 'sunlu-tpu-h2d',
  2147. name: 'SUNLU TPU 95A @Bambu Lab H2D 0.4 nozzle',
  2148. source: 'cloud',
  2149. filament_type: 'TPU',
  2150. filament_colour: '#FF0000',
  2151. },
  2152. ],
  2153. },
  2154. });
  2155. const pick = pickFilamentForSlot(
  2156. presets,
  2157. { type: 'TPU', color: '#FF0000' },
  2158. 'Bambu Lab H2D 0.4 nozzle',
  2159. index,
  2160. );
  2161. expect(pick).toEqual({ source: 'cloud', id: 'sunlu-tpu-h2d' });
  2162. });
  2163. });
  2164. describe('SliceModal — material and printer filtering (#2982)', () => {
  2165. const A1 = 'Bambu Lab A1 0.4 nozzle';
  2166. const P1S = 'Bambu Lab P1S 0.4 nozzle';
  2167. beforeEach(() => {
  2168. vi.clearAllMocks();
  2169. mockApi.getSlicerPresetValues.mockResolvedValue({ resolved: true, values: {}, reason: 'ok' });
  2170. mockApi.getSlicerPrinterModels.mockResolvedValue({
  2171. 'Bambu Lab A1': 'A1',
  2172. 'Bambu Lab P1S': 'P1S',
  2173. 'Bambu Lab X1 Carbon': 'X1C',
  2174. });
  2175. mockApi.listSlicerPipelines.mockResolvedValue({ pipelines: [] });
  2176. mockApi.getLibraryFilePlates.mockResolvedValue({ file_id: 100, filename: 'Plate.3mf', plates: [] });
  2177. mockApi.sliceLibraryFile.mockResolvedValue({
  2178. job_id: 42,
  2179. status: 'pending',
  2180. status_url: '/api/v1/slice-jobs/42',
  2181. });
  2182. mockApi.getSliceJob.mockResolvedValue({
  2183. job_id: 42,
  2184. status: 'running',
  2185. kind: 'library_file',
  2186. source_id: 100,
  2187. source_name: 'Plate.3mf',
  2188. created_at: new Date().toISOString(),
  2189. started_at: null,
  2190. completed_at: null,
  2191. });
  2192. });
  2193. // One PLA plate slot — the case the report is about.
  2194. function plaPlate() {
  2195. return {
  2196. file_id: 100,
  2197. filename: 'Plate.3mf',
  2198. plate_id: 1,
  2199. filaments: [{ slot_id: 1, type: 'PLA', color: '#FF0000', used_grams: 10, used_meters: 3 }],
  2200. };
  2201. }
  2202. function presetsFor(printer: string, filaments: UnifiedPresetsResponse['standard']['filament']) {
  2203. return makeUnified({
  2204. standard: {
  2205. printer: [{ id: printer, name: printer, source: 'standard' }],
  2206. process: [
  2207. {
  2208. id: '0.20mm Standard @BBL X1C',
  2209. name: '0.20mm Standard @BBL X1C',
  2210. source: 'standard',
  2211. compatible_printers: [P1S, 'Bambu Lab X1 Carbon 0.4 nozzle'],
  2212. },
  2213. ],
  2214. filament: filaments,
  2215. },
  2216. });
  2217. }
  2218. it('does not auto-pick a stated PETG for a PLA plate', async () => {
  2219. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(plaPlate());
  2220. mockApi.getSlicerPresets.mockResolvedValue(
  2221. presetsFor(A1, [
  2222. { id: 'petg', name: 'eSUN PETG Basic @BBL A1', source: 'standard', filament_type: 'PETG', filament_colour: '#FF0000' },
  2223. { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  2224. ]),
  2225. );
  2226. renderWithTracker({
  2227. source: { kind: 'libraryFile', id: 100, filename: 'Plate.3mf' },
  2228. onClose: vi.fn(),
  2229. });
  2230. await waitFor(() => expect(screen.getByText(A1)).toBeDefined());
  2231. const user = userEvent.setup();
  2232. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  2233. await waitFor(() => {
  2234. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  2235. expect(body.filament_presets).toEqual([{ source: 'standard', id: 'pla' }]);
  2236. });
  2237. });
  2238. it('drops a wrong-material pick once the listing learns the material', async () => {
  2239. // The sequence a user upgrading their sidecar actually goes through. The
  2240. // first listing reports no material for anything, so the pre-pick has
  2241. // only colour to go on and lands on the PETG. The second reports the
  2242. // materials — and the wrong pick has to go, which it did not, because the
  2243. // slot was held on printer-compatibility alone.
  2244. const colourless = presetsFor(A1, [
  2245. { id: 'petg', name: 'eSUN PETG Basic @BBL A1', source: 'standard', filament_type: null, filament_colour: '#FF0000' },
  2246. { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard', filament_type: null, filament_colour: '#FFFFFF' },
  2247. ]);
  2248. const typed = presetsFor(A1, [
  2249. { id: 'petg', name: 'eSUN PETG Basic @BBL A1', source: 'standard', filament_type: 'PETG', filament_colour: '#FF0000' },
  2250. { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard', filament_type: 'PLA', filament_colour: '#FFFFFF' },
  2251. ]);
  2252. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(plaPlate());
  2253. mockApi.getSlicerPresets.mockResolvedValueOnce(colourless).mockResolvedValue(typed);
  2254. renderWithTracker({
  2255. source: { kind: 'libraryFile', id: 100, filename: 'Plate.3mf' },
  2256. onClose: vi.fn(),
  2257. });
  2258. await waitFor(() => expect(screen.getByText(A1)).toBeDefined());
  2259. const user = userEvent.setup();
  2260. // The colour-matched PETG is what the first listing produces.
  2261. await waitFor(() => {
  2262. const select = presetSelects().find((el) => el.value.startsWith('standard:p'));
  2263. expect(select?.value).toBe('standard:petg');
  2264. });
  2265. await user.click(screen.getByRole('button', { name: 'Refresh' }));
  2266. await waitFor(() => {
  2267. const select = presetSelects().find((el) => el.value.startsWith('standard:p'));
  2268. expect(select?.value).toBe('standard:pla');
  2269. });
  2270. });
  2271. it('keeps a wrong-material preset the user picked on purpose', async () => {
  2272. // Printing PETG on a plate a designer labelled PLA is a legitimate thing
  2273. // to do. The material rule corrects the auto-pick; it must not overrule
  2274. // a choice made in the dropdown.
  2275. const pla = { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard' as const, filament_type: 'PLA', filament_colour: '#FF0000' };
  2276. const petg = { id: 'petg', name: 'eSUN PETG Basic @BBL A1', source: 'standard' as const, filament_type: 'PETG', filament_colour: '#FFFFFF' };
  2277. const matte = { id: 'matte', name: 'Bambu PLA Matte @BBL A1', source: 'standard' as const, filament_type: 'PLA', filament_colour: '#00FF00' };
  2278. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(plaPlate());
  2279. // The refresh has to return a *different* listing, or React Query's
  2280. // structural sharing hands back the same object and the pre-pick never
  2281. // re-runs — which would make this test pass without proving anything.
  2282. mockApi.getSlicerPresets
  2283. .mockResolvedValueOnce(presetsFor(A1, [pla, petg]))
  2284. .mockResolvedValue(presetsFor(A1, [pla, petg, matte]));
  2285. renderWithTracker({
  2286. source: { kind: 'libraryFile', id: 100, filename: 'Plate.3mf' },
  2287. onClose: vi.fn(),
  2288. });
  2289. await waitFor(() => expect(screen.getByText(A1)).toBeDefined());
  2290. const user = userEvent.setup();
  2291. const filamentSelect = presetSelects().find((el) =>
  2292. Array.from(el.options).some((o) => o.textContent?.includes('eSUN PETG Basic')),
  2293. );
  2294. expect(filamentSelect).toBeDefined();
  2295. await user.selectOptions(filamentSelect!, 'standard:petg');
  2296. // Refreshing re-runs the pre-pick over the same slots. That is exactly
  2297. // where an un-exempted material rule would quietly undo the choice.
  2298. await user.click(screen.getByRole('button', { name: 'Refresh' }));
  2299. await waitFor(() => {
  2300. const select = presetSelects().find((el) => el.value.startsWith('standard:p'));
  2301. expect(select?.value).toBe('standard:petg');
  2302. });
  2303. await user.click(screen.getByRole('button', { name: /^Slice$/ }));
  2304. await waitFor(() => {
  2305. const [, body] = mockApi.sliceLibraryFile.mock.calls[0];
  2306. expect(body.filament_presets).toEqual([{ source: 'standard', id: 'petg' }]);
  2307. });
  2308. });
  2309. it('shows every process when the printer filter would leave the list empty', async () => {
  2310. // A P1S against a sidecar too old to report compatible_printers: all 198
  2311. // processes read as another printer's, so the dropdown held one
  2312. // auto-picked entry and a "Show all" link, with nothing saying the list
  2313. // itself was the problem.
  2314. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(plaPlate());
  2315. mockApi.getSlicerPresets.mockResolvedValue(
  2316. makeUnified({
  2317. standard: {
  2318. printer: [{ id: P1S, name: P1S, source: 'standard' }],
  2319. process: [
  2320. { id: 'x1c', name: '0.20mm Standard @BBL X1C', source: 'standard' },
  2321. { id: 'a1', name: '0.06mm Fine @BBL A1 0.2 nozzle', source: 'standard' },
  2322. ],
  2323. filament: [
  2324. { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard', filament_type: 'PLA' },
  2325. ],
  2326. },
  2327. }),
  2328. );
  2329. renderWithTracker({
  2330. source: { kind: 'libraryFile', id: 100, filename: 'Plate.3mf' },
  2331. onClose: vi.fn(),
  2332. });
  2333. await waitFor(() => expect(screen.getByText(P1S)).toBeDefined());
  2334. const processSelect = presetSelects().find((el) =>
  2335. Array.from(el.options).some((o) => o.textContent?.includes('0.20mm Standard @BBL X1C')),
  2336. );
  2337. expect(processSelect).toBeDefined();
  2338. // Both are visible rather than one hidden behind "Show all".
  2339. const labels = Array.from(processSelect!.options).map((o) => o.textContent);
  2340. expect(labels.some((l) => l?.includes('0.20mm Standard @BBL X1C'))).toBe(true);
  2341. expect(labels.some((l) => l?.includes('0.06mm Fine @BBL A1 0.2 nozzle'))).toBe(true);
  2342. });
  2343. it('still hides other-printer presets when some do match', async () => {
  2344. // The guard is for an empty list only — the normal filter must survive.
  2345. mockApi.getLibraryFileFilamentRequirements.mockResolvedValue(plaPlate());
  2346. mockApi.getSlicerPresets.mockResolvedValue(
  2347. makeUnified({
  2348. standard: {
  2349. printer: [{ id: P1S, name: P1S, source: 'standard' }],
  2350. process: [
  2351. {
  2352. id: 'x1c',
  2353. name: '0.20mm Standard @BBL X1C',
  2354. source: 'standard',
  2355. compatible_printers: [P1S],
  2356. },
  2357. { id: 'a1', name: '0.06mm Fine @BBL A1 0.2 nozzle', source: 'standard' },
  2358. ],
  2359. filament: [
  2360. { id: 'pla', name: 'Bambu PLA Basic @BBL A1', source: 'standard', filament_type: 'PLA' },
  2361. ],
  2362. },
  2363. }),
  2364. );
  2365. renderWithTracker({
  2366. source: { kind: 'libraryFile', id: 100, filename: 'Plate.3mf' },
  2367. onClose: vi.fn(),
  2368. });
  2369. await waitFor(() => expect(screen.getByText(P1S)).toBeDefined());
  2370. const processSelect = presetSelects().find((el) =>
  2371. Array.from(el.options).some((o) => o.textContent?.includes('0.20mm Standard @BBL X1C')),
  2372. );
  2373. const labels = Array.from(processSelect!.options).map((o) => o.textContent);
  2374. expect(labels.some((l) => l?.includes('0.06mm Fine @BBL A1 0.2 nozzle'))).toBe(false);
  2375. });
  2376. });