PrinterSelector.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. label: 'AMS1-T1',
  26. trayInfoIdx: '',
  27. extruderId: undefined,
  28. ...overrides,
  29. };
  30. }
  31. function makeReq(overrides: Partial<FilamentRequirement> = {}): FilamentRequirement {
  32. return {
  33. slot_id: 1,
  34. type: 'PLA',
  35. color: '#FFFFFF',
  36. used_grams: 10,
  37. ...overrides,
  38. };
  39. }
  40. // Dual-nozzle H2D-like setup:
  41. // Left nozzle (extruderId=1): AMS0 with PLA Black (tray 0) and PETG White (tray 1)
  42. // Right nozzle (extruderId=0): AMS1 with PLA White (tray 4) and PLA Red (tray 5)
  43. const H2D_FILAMENTS: LoadedFilament[] = [
  44. makeFilament({ globalTrayId: 0, type: 'PLA', color: '#000000', colorName: 'Black', amsId: 0, trayId: 0, label: 'AMS1-T1', extruderId: 1 }),
  45. makeFilament({ globalTrayId: 1, type: 'PETG', color: '#FFFFFF', colorName: 'White', amsId: 0, trayId: 1, label: 'AMS1-T2', extruderId: 1 }),
  46. makeFilament({ globalTrayId: 4, type: 'PLA', color: '#FFFFFF', colorName: 'White', amsId: 1, trayId: 0, label: 'AMS2-T1', extruderId: 0 }),
  47. makeFilament({ globalTrayId: 5, type: 'PLA', color: '#FF0000', colorName: 'Red', amsId: 1, trayId: 1, label: 'AMS2-T2', extruderId: 0 }),
  48. ];
  49. // -- canonicalFilamentType / filamentTypesCompatible -------------------------
  50. describe('canonicalFilamentType', () => {
  51. it('maps PA-CF variants to the same canonical type', () => {
  52. const canonical = canonicalFilamentType('PA-CF');
  53. expect(canonicalFilamentType('PA12-CF')).toBe(canonical);
  54. expect(canonicalFilamentType('PAHT-CF')).toBe(canonical);
  55. });
  56. it('is case-insensitive', () => {
  57. expect(canonicalFilamentType('pa-cf')).toBe(canonicalFilamentType('PA-CF'));
  58. expect(canonicalFilamentType('Pa12-Cf')).toBe(canonicalFilamentType('PA12-CF'));
  59. });
  60. it('returns the type unchanged for non-equivalent types', () => {
  61. expect(canonicalFilamentType('PLA')).toBe('PLA');
  62. expect(canonicalFilamentType('PETG')).toBe('PETG');
  63. expect(canonicalFilamentType('ABS')).toBe('ABS');
  64. });
  65. it('returns empty string for undefined/empty input', () => {
  66. expect(canonicalFilamentType(undefined)).toBe('');
  67. expect(canonicalFilamentType('')).toBe('');
  68. });
  69. });
  70. describe('filamentTypesCompatible', () => {
  71. it('treats PA-CF and PA12-CF as compatible', () => {
  72. expect(filamentTypesCompatible('PA-CF', 'PA12-CF')).toBe(true);
  73. });
  74. it('treats PA-CF and PAHT-CF as compatible', () => {
  75. expect(filamentTypesCompatible('PA-CF', 'PAHT-CF')).toBe(true);
  76. });
  77. it('treats PLA and PETG as incompatible', () => {
  78. expect(filamentTypesCompatible('PLA', 'PETG')).toBe(false);
  79. });
  80. it('treats same types as compatible', () => {
  81. expect(filamentTypesCompatible('PLA', 'PLA')).toBe(true);
  82. });
  83. });
  84. // -- filterFilamentsByNozzle -------------------------------------------------
  85. describe('filterFilamentsByNozzle', () => {
  86. it('returns all filaments when nozzle_id is null', () => {
  87. const result = filterFilamentsByNozzle(H2D_FILAMENTS, null);
  88. expect(result).toHaveLength(4);
  89. });
  90. it('returns all filaments when nozzle_id is undefined', () => {
  91. const result = filterFilamentsByNozzle(H2D_FILAMENTS, undefined);
  92. expect(result).toHaveLength(4);
  93. });
  94. it('filters to left nozzle (extruderId=1)', () => {
  95. const result = filterFilamentsByNozzle(H2D_FILAMENTS, 1);
  96. expect(result).toHaveLength(2);
  97. expect(result.every((f) => f.extruderId === 1)).toBe(true);
  98. });
  99. it('filters to right nozzle (extruderId=0)', () => {
  100. const result = filterFilamentsByNozzle(H2D_FILAMENTS, 0);
  101. expect(result).toHaveLength(2);
  102. expect(result.every((f) => f.extruderId === 0)).toBe(true);
  103. });
  104. });
  105. // -- autoMatchFilament -------------------------------------------------------
  106. describe('autoMatchFilament', () => {
  107. it('matches exact type+color on correct nozzle', () => {
  108. const req = makeReq({ type: 'PLA', color: '#FFFFFF', nozzle_id: 0 });
  109. const result = autoMatchFilament(req, H2D_FILAMENTS, new Set());
  110. expect(result).toBeDefined();
  111. expect(result!.globalTrayId).toBe(4); // AMS2-T1 on right nozzle
  112. });
  113. it('does NOT match filament on wrong nozzle — regression #624', () => {
  114. // Require PLA Black on right nozzle (extruderId=0).
  115. // PLA Black exists only on left nozzle (tray 0, extruderId=1).
  116. const req = makeReq({ type: 'PLA', color: '#000000', nozzle_id: 0 });
  117. const result = autoMatchFilament(req, H2D_FILAMENTS, new Set());
  118. // Should NOT match tray 0 (wrong nozzle). May match tray 4 or 5 as type-only.
  119. if (result) {
  120. expect(result.extruderId).toBe(0);
  121. expect(result.globalTrayId).not.toBe(0);
  122. }
  123. });
  124. it('matches without nozzle constraint for single-nozzle printers', () => {
  125. const req = makeReq({ type: 'PLA', color: '#000000' }); // no nozzle_id
  126. const result = autoMatchFilament(req, H2D_FILAMENTS, new Set());
  127. expect(result).toBeDefined();
  128. expect(result!.globalTrayId).toBe(0); // Exact match: PLA Black
  129. });
  130. it('falls back to type-only match on correct nozzle', () => {
  131. // Require PETG Green on left nozzle — no exact color match, but PETG White exists
  132. const req = makeReq({ type: 'PETG', color: '#00FF00', nozzle_id: 1 });
  133. const result = autoMatchFilament(req, H2D_FILAMENTS, new Set());
  134. expect(result).toBeDefined();
  135. expect(result!.globalTrayId).toBe(1); // PETG White on left nozzle
  136. expect(result!.extruderId).toBe(1);
  137. });
  138. it('returns undefined when no filament matches on required nozzle', () => {
  139. // Require PETG on right nozzle — PETG only exists on left nozzle
  140. const req = makeReq({ type: 'PETG', color: '#FFFFFF', nozzle_id: 0 });
  141. const result = autoMatchFilament(req, H2D_FILAMENTS, new Set());
  142. expect(result).toBeUndefined();
  143. });
  144. it('skips already-used tray IDs', () => {
  145. const req = makeReq({ type: 'PLA', color: '#FFFFFF', nozzle_id: 0 });
  146. const used = new Set([4]); // AMS2-T1 already used
  147. const result = autoMatchFilament(req, H2D_FILAMENTS, used);
  148. // Should fall back to PLA Red (tray 5) as type-only match
  149. expect(result).toBeDefined();
  150. expect(result!.globalTrayId).toBe(5);
  151. });
  152. it('matches PA-CF requirement to PA12-CF filament — #688', () => {
  153. const filaments: LoadedFilament[] = [
  154. makeFilament({ globalTrayId: 0, type: 'PA12-CF', color: '#000000', colorName: 'Black' }),
  155. ];
  156. const req = makeReq({ type: 'PA-CF', color: '#000000' });
  157. const result = autoMatchFilament(req, filaments, new Set());
  158. expect(result).toBeDefined();
  159. expect(result!.globalTrayId).toBe(0);
  160. });
  161. it('matches PAHT-CF requirement to PA-CF filament — #688', () => {
  162. const filaments: LoadedFilament[] = [
  163. makeFilament({ globalTrayId: 0, type: 'PA-CF', color: '#333333', colorName: 'Dark Gray' }),
  164. ];
  165. const req = makeReq({ type: 'PAHT-CF', color: '#333333' });
  166. const result = autoMatchFilament(req, filaments, new Set());
  167. expect(result).toBeDefined();
  168. expect(result!.globalTrayId).toBe(0);
  169. });
  170. });