PrintersPageDrying.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * Tests for AMS drying feature logic.
  3. *
  4. * The drying presets, time formatting, module type gating, and temperature
  5. * clamping are all defined inline in PrintersPage.tsx. These tests validate
  6. * the logic directly by mirroring the relevant constants and functions.
  7. */
  8. import { describe, it, expect } from 'vitest';
  9. /**
  10. * Mirrors the DRYING_PRESETS constant from PrintersPage.tsx.
  11. * Format: { n3f temp, n3s temp, n3f hours, n3s hours }
  12. */
  13. const DRYING_PRESETS: Record<string, { n3f: number; n3s: number; n3f_hours: number; n3s_hours: number }> = {
  14. 'PLA': { n3f: 45, n3s: 45, n3f_hours: 12, n3s_hours: 12 },
  15. 'PETG': { n3f: 65, n3s: 65, n3f_hours: 12, n3s_hours: 12 },
  16. 'TPU': { n3f: 65, n3s: 75, n3f_hours: 12, n3s_hours: 18 },
  17. 'ABS': { n3f: 65, n3s: 80, n3f_hours: 12, n3s_hours: 8 },
  18. 'ASA': { n3f: 65, n3s: 80, n3f_hours: 12, n3s_hours: 8 },
  19. 'PA': { n3f: 65, n3s: 85, n3f_hours: 12, n3s_hours: 12 },
  20. 'PC': { n3f: 65, n3s: 80, n3f_hours: 12, n3s_hours: 8 },
  21. 'PVA': { n3f: 65, n3s: 85, n3f_hours: 12, n3s_hours: 18 },
  22. };
  23. /**
  24. * Mirrors the inline dry_time formatting from PrintersPage.tsx:
  25. * dry_time >= 60 ? `${Math.floor(dry_time / 60)}h ${dry_time % 60}m` : `${dry_time}m`
  26. */
  27. function formatDryTime(dry_time: number): string {
  28. if (dry_time >= 60) {
  29. return `${Math.floor(dry_time / 60)}h ${dry_time % 60}m`;
  30. }
  31. return `${dry_time}m`;
  32. }
  33. /**
  34. * Mirrors the temperature clamping from PrintersPage.tsx:
  35. * Math.min(maxTemp, Math.max(45, value))
  36. * where maxTemp is 65 for n3f and 85 for n3s.
  37. */
  38. function clampTemp(value: number, moduleType: 'n3f' | 'n3s'): number {
  39. const maxTemp = moduleType === 'n3s' ? 85 : 65;
  40. return Math.min(maxTemp, Math.max(45, value));
  41. }
  42. describe('DRYING_PRESETS structure', () => {
  43. const expectedFilaments = ['PLA', 'PETG', 'TPU', 'ABS', 'ASA', 'PA', 'PC', 'PVA'];
  44. it('contains all expected filament types', () => {
  45. for (const fil of expectedFilaments) {
  46. expect(DRYING_PRESETS).toHaveProperty(fil);
  47. }
  48. });
  49. it('has no unexpected filament types', () => {
  50. expect(Object.keys(DRYING_PRESETS).sort()).toEqual(expectedFilaments.sort());
  51. });
  52. it('n3f temps are all within 45-65 range', () => {
  53. for (const [fil, preset] of Object.entries(DRYING_PRESETS)) {
  54. expect(preset.n3f, `${fil} n3f temp`).toBeGreaterThanOrEqual(45);
  55. expect(preset.n3f, `${fil} n3f temp`).toBeLessThanOrEqual(65);
  56. }
  57. });
  58. it('n3s temps are all within 45-85 range', () => {
  59. for (const [fil, preset] of Object.entries(DRYING_PRESETS)) {
  60. expect(preset.n3s, `${fil} n3s temp`).toBeGreaterThanOrEqual(45);
  61. expect(preset.n3s, `${fil} n3s temp`).toBeLessThanOrEqual(85);
  62. }
  63. });
  64. it('all hours are between 1-24', () => {
  65. for (const [fil, preset] of Object.entries(DRYING_PRESETS)) {
  66. expect(preset.n3f_hours, `${fil} n3f_hours`).toBeGreaterThanOrEqual(1);
  67. expect(preset.n3f_hours, `${fil} n3f_hours`).toBeLessThanOrEqual(24);
  68. expect(preset.n3s_hours, `${fil} n3s_hours`).toBeGreaterThanOrEqual(1);
  69. expect(preset.n3s_hours, `${fil} n3s_hours`).toBeLessThanOrEqual(24);
  70. }
  71. });
  72. it('n3s temp is always >= n3f temp for same filament', () => {
  73. for (const [fil, preset] of Object.entries(DRYING_PRESETS)) {
  74. expect(preset.n3s, `${fil}: n3s should be >= n3f`).toBeGreaterThanOrEqual(preset.n3f);
  75. }
  76. });
  77. });
  78. describe('dry_time formatting', () => {
  79. it('formats >= 60 minutes as hours and minutes', () => {
  80. expect(formatDryTime(119)).toBe('1h 59m');
  81. });
  82. it('formats exactly 60 minutes as 1h 0m', () => {
  83. expect(formatDryTime(60)).toBe('1h 0m');
  84. });
  85. it('formats large values correctly', () => {
  86. expect(formatDryTime(750)).toBe('12h 30m');
  87. });
  88. it('formats < 60 minutes as minutes only', () => {
  89. expect(formatDryTime(42)).toBe('42m');
  90. });
  91. it('formats 1 minute', () => {
  92. expect(formatDryTime(1)).toBe('1m');
  93. });
  94. it('dry_time = 0 means not drying (shows 0m)', () => {
  95. // In the UI, dry_time > 0 gates whether the drying bar is shown at all,
  96. // so formatDryTime(0) would not be called. But the value itself means "not drying".
  97. expect(formatDryTime(0)).toBe('0m');
  98. });
  99. });
  100. describe('module type detection — drying button visibility', () => {
  101. /**
  102. * Mirrors the condition from PrintersPage.tsx:
  103. * ams.module_type === 'n3f' || ams.module_type === 'n3s'
  104. * The drying button only shows for AMS 2 Pro (n3f) and AMS-HT (n3s).
  105. */
  106. function shouldShowDryingButton(moduleType: string): boolean {
  107. return moduleType === 'n3f' || moduleType === 'n3s';
  108. }
  109. it('shows for n3f (AMS 2 Pro)', () => {
  110. expect(shouldShowDryingButton('n3f')).toBe(true);
  111. });
  112. it('shows for n3s (AMS-HT)', () => {
  113. expect(shouldShowDryingButton('n3s')).toBe(true);
  114. });
  115. it('does not show for ams (original AMS)', () => {
  116. expect(shouldShowDryingButton('ams')).toBe(false);
  117. });
  118. it('does not show for empty string', () => {
  119. expect(shouldShowDryingButton('')).toBe(false);
  120. });
  121. it('does not show for unknown types', () => {
  122. expect(shouldShowDryingButton('unknown')).toBe(false);
  123. });
  124. });
  125. describe('temperature clamping', () => {
  126. describe('n3f (max 65)', () => {
  127. it('clamps value below minimum to 45', () => {
  128. expect(clampTemp(30, 'n3f')).toBe(45);
  129. });
  130. it('clamps value above maximum to 65', () => {
  131. expect(clampTemp(80, 'n3f')).toBe(65);
  132. });
  133. it('keeps value within range unchanged', () => {
  134. expect(clampTemp(55, 'n3f')).toBe(55);
  135. });
  136. it('keeps minimum boundary value', () => {
  137. expect(clampTemp(45, 'n3f')).toBe(45);
  138. });
  139. it('keeps maximum boundary value', () => {
  140. expect(clampTemp(65, 'n3f')).toBe(65);
  141. });
  142. });
  143. describe('n3s (max 85)', () => {
  144. it('clamps value below minimum to 45', () => {
  145. expect(clampTemp(10, 'n3s')).toBe(45);
  146. });
  147. it('clamps value above maximum to 85', () => {
  148. expect(clampTemp(100, 'n3s')).toBe(85);
  149. });
  150. it('keeps value within range unchanged', () => {
  151. expect(clampTemp(70, 'n3s')).toBe(70);
  152. });
  153. it('keeps minimum boundary value', () => {
  154. expect(clampTemp(45, 'n3s')).toBe(45);
  155. });
  156. it('keeps maximum boundary value', () => {
  157. expect(clampTemp(85, 'n3s')).toBe(85);
  158. });
  159. it('allows values above n3f max (e.g. 75)', () => {
  160. expect(clampTemp(75, 'n3s')).toBe(75);
  161. });
  162. });
  163. });
  164. describe('rotate tray option', () => {
  165. it('defaults to false', () => {
  166. // Mirrors the initial state: useState(false)
  167. const defaultRotateTray = false;
  168. expect(defaultRotateTray).toBe(false);
  169. });
  170. it('is included in the API URL when true', () => {
  171. // Mirrors the API call construction from client.ts
  172. const buildUrl = (rotateTray: boolean) =>
  173. `/printers/1/drying/start?ams_id=0&temp=55&duration=4&filament=PLA&rotate_tray=${rotateTray}`;
  174. expect(buildUrl(true)).toContain('rotate_tray=true');
  175. expect(buildUrl(false)).toContain('rotate_tray=false');
  176. });
  177. it('resets to false when opening popover for a new AMS unit', () => {
  178. // Mirrors the popover open logic: setDryingRotateTray(false) is called
  179. // each time the popover opens for any AMS unit
  180. let rotateTray = true; // user enabled it for previous AMS
  181. // Simulates opening popover for a different AMS
  182. rotateTray = false; // setDryingRotateTray(false)
  183. expect(rotateTray).toBe(false);
  184. });
  185. });
  186. describe('rotate tray gate (per-AMS tray.state === 11)', () => {
  187. /**
  188. * Mirrors the gate from PrintersPage.tsx — rotation is physically impossible
  189. * when ANY tray in the targeted AMS has its filament threaded out into the
  190. * feed tube. The whole AMS rotates as one mechanism (all 4 spools turn
  191. * together), so a single loaded slot locks the entire unit.
  192. *
  193. * Per-tray Bambu `state`:
  194. * 9 = empty (no spool)
  195. * 10 = spool present, NOT loaded into tube (rotation possible)
  196. * 11 = loaded into tube (rotation impossible)
  197. *
  198. * This catches both mid-print (active feed) AND idle-with-threaded-filament
  199. * — the H2D's post-print state leaves filament in the tube but tray_now
  200. * resets to 255, which a tray_now-only check would silently miss.
  201. */
  202. type TrayLike = { state?: number };
  203. type AmsLike = { id: number; tray?: TrayLike[] };
  204. function isTrayLoadedInThisAms(
  205. amsData: AmsLike[],
  206. targetAmsId: number | null,
  207. ): boolean {
  208. if (targetAmsId === null) return false;
  209. const targetAms = amsData.find(a => a.id === targetAmsId);
  210. return (targetAms?.tray ?? []).some(tray => tray.state === 11);
  211. }
  212. it('returns false when AMS id is null (modal closed)', () => {
  213. const ams = [{ id: 0, tray: [{ state: 11 }] }];
  214. expect(isTrayLoadedInThisAms(ams, null)).toBe(false);
  215. });
  216. it('returns false when targeted AMS not found in amsData', () => {
  217. const ams = [{ id: 0, tray: [{ state: 11 }] }];
  218. expect(isTrayLoadedInThisAms(ams, 1)).toBe(false);
  219. });
  220. it('returns false when all trays are empty (state=9)', () => {
  221. const ams = [{ id: 0, tray: [{ state: 9 }, { state: 9 }, { state: 9 }, { state: 9 }] }];
  222. expect(isTrayLoadedInThisAms(ams, 0)).toBe(false);
  223. });
  224. it('returns false when all trays have spools but none loaded (state=10)', () => {
  225. // The "all AMS have spools loaded" case the gate now catches correctly:
  226. // spool present in the slot, NOT threaded into the tube → rotation possible.
  227. const ams = [{ id: 0, tray: [{ state: 10 }, { state: 10 }, { state: 10 }, { state: 10 }] }];
  228. expect(isTrayLoadedInThisAms(ams, 0)).toBe(false);
  229. });
  230. it('returns true when ANY tray is loaded into tube (state=11)', () => {
  231. // H2D's typical post-print state: one tray's filament is still threaded out
  232. // into the feed tube even after the print finishes. tray_now=255 but
  233. // this tray's state stays at 11. The whole AMS is mechanically locked.
  234. const ams = [{ id: 0, tray: [{ state: 10 }, { state: 11 }, { state: 9 }, { state: 10 }] }];
  235. expect(isTrayLoadedInThisAms(ams, 0)).toBe(true);
  236. });
  237. it('returns true when targeted AMS-B has a loaded tray (per-AMS isolation)', () => {
  238. // AMS-A locked, AMS-B free; targeting AMS-A → true, targeting AMS-B → false
  239. const ams = [
  240. { id: 0, tray: [{ state: 11 }, { state: 9 }] },
  241. { id: 1, tray: [{ state: 10 }, { state: 10 }] },
  242. ];
  243. expect(isTrayLoadedInThisAms(ams, 0)).toBe(true);
  244. expect(isTrayLoadedInThisAms(ams, 1)).toBe(false);
  245. });
  246. it('returns false when targeted AMS has no trays array', () => {
  247. const ams = [{ id: 0 }];
  248. expect(isTrayLoadedInThisAms(ams, 0)).toBe(false);
  249. });
  250. it('treats missing state as not-loaded (conservative default-allow)', () => {
  251. // If firmware doesn't report a state field, default to allowing rotate.
  252. // The firmware-side dry_sf_reason check still rejects on the route side
  253. // if rotation is actually impossible, so being lenient here is safe.
  254. const ams = [{ id: 0, tray: [{ state: undefined }, { state: undefined }] }];
  255. expect(isTrayLoadedInThisAms(ams, 0)).toBe(false);
  256. });
  257. it('submission clamp: rotateTray collapses to false when gate is active', () => {
  258. // Mirrors: rotateTray: dryingRotateTray && !isTrayLoadedInThisAms
  259. // A user enabling rotate before tray.state shifts to 11 (e.g. user loads
  260. // filament from the AMS UI while popover is open) sees the toggle disable,
  261. // and the submit also sends rotate_tray=false. Without the clamp, firmware
  262. // would reject with dry_sf_reason=[3] (ConsumableAtAmsOutlet) post-click.
  263. const userToggleState = true;
  264. const ams = [{ id: 0, tray: [{ state: 11 }, { state: 10 }] }];
  265. const trayLoaded = isTrayLoadedInThisAms(ams, 0);
  266. const submittedValue = userToggleState && !trayLoaded;
  267. expect(trayLoaded).toBe(true);
  268. expect(submittedValue).toBe(false);
  269. });
  270. it('submission clamp: rotateTray passes through when gate is inactive', () => {
  271. const userToggleState = true;
  272. const ams = [{ id: 0, tray: [{ state: 10 }, { state: 10 }] }];
  273. const trayLoaded = isTrayLoadedInThisAms(ams, 0);
  274. const submittedValue = userToggleState && !trayLoaded;
  275. expect(trayLoaded).toBe(false);
  276. expect(submittedValue).toBe(true);
  277. });
  278. });