amsUnitState.test.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * AMS-HT is told apart by its unit id (128-135), not by reporting one tray;
  3. * and an AMS whose filament is threaded into the feed tube cannot rotate its
  4. * spools for drying. The tray state code for "loaded" (11) differs between
  5. * firmware versions, so the slot the printer reports as loaded counts too.
  6. */
  7. import { describe, it, expect } from 'vitest';
  8. import { amsHasLoadedTray, isAmsHtUnit, trayForSlot } from '../../utils/amsHelpers';
  9. const tray = (id: number, state?: number) => ({ id, state });
  10. describe('isAmsHtUnit', () => {
  11. it('is the unit id range', () => {
  12. expect(isAmsHtUnit(128)).toBe(true);
  13. expect(isAmsHtUnit(135)).toBe(true);
  14. expect(isAmsHtUnit(0)).toBe(false);
  15. expect(isAmsHtUnit(136)).toBe(false);
  16. });
  17. });
  18. describe('amsHasLoadedTray', () => {
  19. it('sees the loaded state code', () => {
  20. expect(amsHasLoadedTray({ id: 0, tray: [tray(0, 10), tray(1, 11)] }, 255, {})).toBe(true);
  21. });
  22. it('sees the slot the printer reports as loaded, whatever its state code', () => {
  23. expect(amsHasLoadedTray({ id: 1, tray: [tray(0, 10), tray(1, 10)] }, 5, {})).toBe(true);
  24. expect(amsHasLoadedTray({ id: 1, tray: [tray(0, 10), tray(1, 10)] }, 1, {})).toBe(false);
  25. });
  26. it('sees an AMS-HT that is loaded', () => {
  27. expect(amsHasLoadedTray({ id: 128, tray: [tray(0, 10)] }, 128, {})).toBe(true);
  28. });
  29. it('sees a hotend fed from it on a two-nozzle printer', () => {
  30. // tray_now names only the active nozzle's slot; the other hotend's comes
  31. // from extruder_slots.
  32. const slots = { '1': { ams_id: 2, slot_id: 3, has_filament: true } };
  33. expect(amsHasLoadedTray({ id: 2, tray: [tray(3, 10)] }, 255, slots)).toBe(true);
  34. expect(amsHasLoadedTray({ id: 2, tray: [tray(3, 10)] }, 255, { '1': { ...slots['1'], has_filament: false } })).toBe(false);
  35. });
  36. it('is false for an idle AMS', () => {
  37. expect(amsHasLoadedTray({ id: 0, tray: [tray(0, 10), tray(1, 9)] }, 255, {})).toBe(false);
  38. expect(amsHasLoadedTray(undefined, 0, {})).toBe(false);
  39. });
  40. });
  41. describe('trayForSlot', () => {
  42. it('finds a slot by its tray id when the AMS reports only some trays', () => {
  43. const trays = [{ id: 2, tray_type: 'PETG' }];
  44. expect(trayForSlot(trays, 2)?.tray_type).toBe('PETG');
  45. expect(trayForSlot(trays, 0)).toBeUndefined();
  46. });
  47. it('falls back to the position when the ids do not tell the trays apart', () => {
  48. const trays = [{ id: 0, tray_type: 'PLA' }, { id: 0, tray_type: 'PETG' }];
  49. expect(trayForSlot(trays, 1)?.tray_type).toBe('PETG');
  50. });
  51. });