PrinterSelector.test.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * Tests for InlineMappingEditor auto-match and nozzle filtering logic.
  3. *
  4. * Regression test for #624: multi-printer filament mapping showed filaments
  5. * from both nozzles on dual-nozzle printers (H2D). The single-printer path
  6. * (FilamentMapping.tsx) was fixed in commit 29e9593 but the multi-printer
  7. * path (InlineMappingEditor in PrinterSelector.tsx) was missed.
  8. */
  9. import { describe, it, expect } from 'vitest';
  10. import {
  11. autoMatchFilament,
  12. canonicalFilamentType,
  13. filamentTypesCompatible,
  14. filterFilamentsByNozzle,
  15. } from '../../utils/amsHelpers';
  16. import type { LoadedFilament, FilamentRequirement } from '../../hooks/useFilamentMapping';
  17. // -- helpers -----------------------------------------------------------------
  18. function makeFilament(overrides: Partial<LoadedFilament> & { globalTrayId: number }): LoadedFilament {
  19. return {
  20. type: 'PLA',
  21. color: '#FFFFFF',
  22. colorName: 'White',
  23. amsId: 0,
  24. trayId: 0,
  25. isHt: false,
  26. isExternal: false,
  27. label: 'AMS1-T1',
  28. trayInfoIdx: '',
  29. extruderId: undefined,
  30. remain: -1,
  31. ...overrides,
  32. };
  33. }
  34. function makeReq(overrides: Partial<FilamentRequirement> = {}): FilamentRequirement {
  35. return {
  36. slot_id: 1,
  37. type: 'PLA',
  38. color: '#FFFFFF',
  39. used_grams: 10,
  40. ...overrides,
  41. };
  42. }
  43. // Dual-nozzle H2D-like setup:
  44. // Left nozzle (extruderId=1): AMS0 with PLA Black (tray 0) and PETG White (tray 1)
  45. // Right nozzle (extruderId=0): AMS1 with PLA White (tray 4) and PLA Red (tray 5)
  46. const H2D_FILAMENTS: LoadedFilament[] = [
  47. makeFilament({ globalTrayId: 0, type: 'PLA', color: '#000000', colorName: 'Black', amsId: 0, trayId: 0, label: 'AMS1-T1', extruderId: 1 }),
  48. makeFilament({ globalTrayId: 1, type: 'PETG', color: '#FFFFFF', colorName: 'White', amsId: 0, trayId: 1, label: 'AMS1-T2', extruderId: 1 }),
  49. makeFilament({ globalTrayId: 4, type: 'PLA', color: '#FFFFFF', colorName: 'White', amsId: 1, trayId: 0, label: 'AMS2-T1', extruderId: 0 }),
  50. makeFilament({ globalTrayId: 5, type: 'PLA', color: '#FF0000', colorName: 'Red', amsId: 1, trayId: 1, label: 'AMS2-T2', extruderId: 0 }),
  51. ];
  52. // -- canonicalFilamentType / filamentTypesCompatible -------------------------
  53. describe('canonicalFilamentType', () => {
  54. it('maps PA-CF variants to the same canonical type', () => {
  55. const canonical = canonicalFilamentType('PA-CF');
  56. expect(canonicalFilamentType('PA12-CF')).toBe(canonical);
  57. expect(canonicalFilamentType('PAHT-CF')).toBe(canonical);
  58. });
  59. it('is case-insensitive', () => {
  60. expect(canonicalFilamentType('pa-cf')).toBe(canonicalFilamentType('PA-CF'));
  61. expect(canonicalFilamentType('Pa12-Cf')).toBe(canonicalFilamentType('PA12-CF'));
  62. });
  63. it('returns the type unchanged for non-equivalent types', () => {
  64. expect(canonicalFilamentType('PLA')).toBe('PLA');
  65. expect(canonicalFilamentType('PETG')).toBe('PETG');
  66. expect(canonicalFilamentType('ABS')).toBe('ABS');
  67. });
  68. it('returns empty string for undefined/empty input', () => {
  69. expect(canonicalFilamentType(undefined)).toBe('');
  70. expect(canonicalFilamentType('')).toBe('');
  71. });
  72. });
  73. describe('filamentTypesCompatible', () => {
  74. it('treats PA-CF and PA12-CF as compatible', () => {
  75. expect(filamentTypesCompatible('PA-CF', 'PA12-CF')).toBe(true);
  76. });
  77. it('treats PA-CF and PAHT-CF as compatible', () => {
  78. expect(filamentTypesCompatible('PA-CF', 'PAHT-CF')).toBe(true);
  79. });
  80. it('treats PLA and PETG as incompatible', () => {
  81. expect(filamentTypesCompatible('PLA', 'PETG')).toBe(false);
  82. });
  83. it('treats same types as compatible', () => {
  84. expect(filamentTypesCompatible('PLA', 'PLA')).toBe(true);
  85. });
  86. });
  87. // -- filterFilamentsByNozzle -------------------------------------------------
  88. describe('filterFilamentsByNozzle', () => {
  89. it('returns all filaments when nozzle_id is null', () => {
  90. const result = filterFilamentsByNozzle(H2D_FILAMENTS, null);
  91. expect(result).toHaveLength(4);
  92. });
  93. it('returns all filaments when nozzle_id is undefined', () => {
  94. const result = filterFilamentsByNozzle(H2D_FILAMENTS, undefined);
  95. expect(result).toHaveLength(4);
  96. });
  97. it('filters to left nozzle (extruderId=1)', () => {
  98. const result = filterFilamentsByNozzle(H2D_FILAMENTS, 1);
  99. expect(result).toHaveLength(2);
  100. expect(result.every((f) => f.extruderId === 1)).toBe(true);
  101. });
  102. it('filters to right nozzle (extruderId=0)', () => {
  103. const result = filterFilamentsByNozzle(H2D_FILAMENTS, 0);
  104. expect(result).toHaveLength(2);
  105. expect(result.every((f) => f.extruderId === 0)).toBe(true);
  106. });
  107. });
  108. // -- autoMatchFilament -------------------------------------------------------
  109. describe('autoMatchFilament', () => {
  110. it('matches exact type+color on correct nozzle', () => {
  111. const req = makeReq({ type: 'PLA', color: '#FFFFFF', nozzle_id: 0 });
  112. const result = autoMatchFilament(req, H2D_FILAMENTS, new Set());
  113. expect(result).toBeDefined();
  114. expect(result!.globalTrayId).toBe(4); // AMS2-T1 on right nozzle
  115. });
  116. it('does NOT match filament on wrong nozzle — regression #624', () => {
  117. // Require PLA Black on right nozzle (extruderId=0).
  118. // PLA Black exists only on left nozzle (tray 0, extruderId=1).
  119. const req = makeReq({ type: 'PLA', color: '#000000', nozzle_id: 0 });
  120. const result = autoMatchFilament(req, H2D_FILAMENTS, new Set());
  121. // Should NOT match tray 0 (wrong nozzle). May match tray 4 or 5 as type-only.
  122. if (result) {
  123. expect(result.extruderId).toBe(0);
  124. expect(result.globalTrayId).not.toBe(0);
  125. }
  126. });
  127. it('matches without nozzle constraint for single-nozzle printers', () => {
  128. const req = makeReq({ type: 'PLA', color: '#000000' }); // no nozzle_id
  129. const result = autoMatchFilament(req, H2D_FILAMENTS, new Set());
  130. expect(result).toBeDefined();
  131. expect(result!.globalTrayId).toBe(0); // Exact match: PLA Black
  132. });
  133. it('falls back to type-only match on correct nozzle', () => {
  134. // Require PETG Green on left nozzle — no exact color match, but PETG White exists
  135. const req = makeReq({ type: 'PETG', color: '#00FF00', nozzle_id: 1 });
  136. const result = autoMatchFilament(req, H2D_FILAMENTS, new Set());
  137. expect(result).toBeDefined();
  138. expect(result!.globalTrayId).toBe(1); // PETG White on left nozzle
  139. expect(result!.extruderId).toBe(1);
  140. });
  141. it('returns undefined when no filament matches on required nozzle', () => {
  142. // Require PETG on right nozzle — PETG only exists on left nozzle
  143. const req = makeReq({ type: 'PETG', color: '#FFFFFF', nozzle_id: 0 });
  144. const result = autoMatchFilament(req, H2D_FILAMENTS, new Set());
  145. expect(result).toBeUndefined();
  146. });
  147. it('skips already-used tray IDs', () => {
  148. const req = makeReq({ type: 'PLA', color: '#FFFFFF', nozzle_id: 0 });
  149. const used = new Set([4]); // AMS2-T1 already used
  150. const result = autoMatchFilament(req, H2D_FILAMENTS, used);
  151. // Should fall back to PLA Red (tray 5) as type-only match
  152. expect(result).toBeDefined();
  153. expect(result!.globalTrayId).toBe(5);
  154. });
  155. it('matches PA-CF requirement to PA12-CF filament — #688', () => {
  156. const filaments: LoadedFilament[] = [
  157. makeFilament({ globalTrayId: 0, type: 'PA12-CF', color: '#000000', colorName: 'Black' }),
  158. ];
  159. const req = makeReq({ type: 'PA-CF', color: '#000000' });
  160. const result = autoMatchFilament(req, filaments, new Set());
  161. expect(result).toBeDefined();
  162. expect(result!.globalTrayId).toBe(0);
  163. });
  164. it('matches PAHT-CF requirement to PA-CF filament — #688', () => {
  165. const filaments: LoadedFilament[] = [
  166. makeFilament({ globalTrayId: 0, type: 'PA-CF', color: '#333333', colorName: 'Dark Gray' }),
  167. ];
  168. const req = makeReq({ type: 'PAHT-CF', color: '#333333' });
  169. const result = autoMatchFilament(req, filaments, new Set());
  170. expect(result).toBeDefined();
  171. expect(result!.globalTrayId).toBe(0);
  172. });
  173. });
  174. // -- autoMatchFilament with preferLowest ------------------------------------
  175. describe('autoMatchFilament preferLowest', () => {
  176. it('picks spool with lowest remain when enabled', () => {
  177. const filaments = [
  178. makeFilament({ globalTrayId: 0, type: 'PLA', color: '#FF0000', colorName: 'Red', remain: 80 }),
  179. makeFilament({ globalTrayId: 1, type: 'PLA', color: '#FF0000', colorName: 'Red', remain: 30 }),
  180. ];
  181. const req = makeReq({ type: 'PLA', color: '#FF0000' });
  182. const result = autoMatchFilament(req, filaments, new Set(), true);
  183. expect(result).toBeDefined();
  184. expect(result!.globalTrayId).toBe(1); // 30% < 80%
  185. });
  186. it('picks first spool when disabled (default behavior)', () => {
  187. const filaments = [
  188. makeFilament({ globalTrayId: 0, type: 'PLA', color: '#FF0000', colorName: 'Red', remain: 80 }),
  189. makeFilament({ globalTrayId: 1, type: 'PLA', color: '#FF0000', colorName: 'Red', remain: 30 }),
  190. ];
  191. const req = makeReq({ type: 'PLA', color: '#FF0000' });
  192. const result = autoMatchFilament(req, filaments, new Set(), false);
  193. expect(result).toBeDefined();
  194. expect(result!.globalTrayId).toBe(0); // First match
  195. });
  196. it('sorts unknown remain (-1) to end', () => {
  197. const filaments = [
  198. makeFilament({ globalTrayId: 0, type: 'PLA', color: '#FF0000', colorName: 'Red', remain: -1 }),
  199. makeFilament({ globalTrayId: 1, type: 'PLA', color: '#FF0000', colorName: 'Red', remain: 50 }),
  200. ];
  201. const req = makeReq({ type: 'PLA', color: '#FF0000' });
  202. const result = autoMatchFilament(req, filaments, new Set(), true);
  203. expect(result).toBeDefined();
  204. expect(result!.globalTrayId).toBe(1); // Known 50% over unknown
  205. });
  206. it('still respects nozzle constraint with preferLowest', () => {
  207. const filaments = [
  208. makeFilament({ globalTrayId: 0, type: 'PLA', color: '#FF0000', colorName: 'Red', remain: 10, extruderId: 1 }),
  209. makeFilament({ globalTrayId: 1, type: 'PLA', color: '#FF0000', colorName: 'Red', remain: 80, extruderId: 0 }),
  210. ];
  211. const req = makeReq({ type: 'PLA', color: '#FF0000', nozzle_id: 0 });
  212. const result = autoMatchFilament(req, filaments, new Set(), true);
  213. expect(result).toBeDefined();
  214. expect(result!.globalTrayId).toBe(1); // Only tray on correct nozzle
  215. });
  216. });