slicePresetPicker3234.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { describe, expect, it } from 'vitest';
  2. import type { UnifiedPreset, UnifiedPresetsBySlot, UnifiedPresetsResponse } from '../../api/client';
  3. import { pickFilamentForSlot } from '../../utils/slicePresetPicker';
  4. import { buildCompatibilityIndex } from '../../utils/slicerPrinterMatch';
  5. // #3234 made the sidecar list every filament the slicer ships and resolve the
  6. // material of all of them. Before, a few presets with no known material kept
  7. // the no-such-material fallback on the right printer by accident; with those
  8. // gone, the fallback has to look at the printer itself.
  9. const index = buildCompatibilityIndex({ 'Bambu Lab X1 Carbon': 'X1C', 'Bambu Lab A1': 'A1' });
  10. const X1C = 'Bambu Lab X1 Carbon 0.4 nozzle';
  11. const A1 = 'Bambu Lab A1 0.4 nozzle';
  12. function empty(): UnifiedPresetsBySlot {
  13. return { printer: [], process: [], filament: [] };
  14. }
  15. function tiers(perTier: Partial<Record<'local' | 'orca_cloud' | 'cloud' | 'standard', Partial<UnifiedPreset>[]>>) {
  16. const build = (source: 'local' | 'orca_cloud' | 'cloud' | 'standard') => ({
  17. ...empty(),
  18. filament: (perTier[source] ?? []).map((e) => ({ id: e.name as string, source, ...e })) as UnifiedPreset[],
  19. });
  20. return {
  21. local: build('local'),
  22. orca_cloud: build('orca_cloud'),
  23. cloud: build('cloud'),
  24. standard: build('standard'),
  25. cloud_status: 'ok',
  26. orca_cloud_status: 'ok',
  27. } as UnifiedPresetsResponse;
  28. }
  29. const pick = (by: UnifiedPresetsResponse, type: string, printer = X1C, color = '') =>
  30. pickFilamentForSlot(by, { type, color }, printer, index)?.id;
  31. describe('pickFilamentForSlot — no preset of the plate material (#3234)', () => {
  32. it('falls back to a preset the selected printer can take', () => {
  33. // Alphabetically first and scored the same, but for another printer: the
  34. // slicer would refuse the slice outright.
  35. const by = tiers({
  36. standard: [
  37. { name: 'Bambu ABS @BBL A1', filament_type: 'ABS', compatible_printers: [A1] },
  38. { name: 'Bambu PLA Basic @BBL X1C', filament_type: 'PLA', compatible_printers: [X1C] },
  39. ],
  40. });
  41. expect(pick(by, 'XYZ')).toBe('Bambu PLA Basic @BBL X1C');
  42. });
  43. it('still returns something when only other printers have presets', () => {
  44. const by = tiers({
  45. standard: [{ name: 'Bambu ABS @BBL A1', filament_type: 'ABS', compatible_printers: [A1] }],
  46. });
  47. expect(pick(by, 'XYZ')).toBe('Bambu ABS @BBL A1');
  48. });
  49. });