useSpoolBuddyState.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * Tests for useSpoolBuddyState hook:
  3. * - Reducer handles all action types correctly
  4. * - Computed properties (remainingWeight, netWeight) work
  5. * - Window events dispatch state updates
  6. */
  7. import { describe, it, expect, vi, afterEach } from 'vitest';
  8. import { renderHook, act } from '@testing-library/react';
  9. import { useSpoolBuddyState } from '../../hooks/useSpoolBuddyState';
  10. function dispatchCustomEvent(name: string, detail: Record<string, unknown>) {
  11. window.dispatchEvent(new CustomEvent(name, { detail }));
  12. }
  13. describe('useSpoolBuddyState', () => {
  14. afterEach(() => {
  15. vi.restoreAllMocks();
  16. });
  17. it('starts with initial state', () => {
  18. const { result } = renderHook(() => useSpoolBuddyState());
  19. expect(result.current.weight).toBeNull();
  20. expect(result.current.weightStable).toBe(false);
  21. expect(result.current.rawAdc).toBeNull();
  22. expect(result.current.matchedSpool).toBeNull();
  23. expect(result.current.unknownTagUid).toBeNull();
  24. expect(result.current.deviceOnline).toBe(false);
  25. expect(result.current.deviceId).toBeNull();
  26. expect(result.current.remainingWeight).toBeNull();
  27. expect(result.current.netWeight).toBeNull();
  28. });
  29. it('WEIGHT_UPDATE sets weight, stable, rawAdc, deviceOnline=true', () => {
  30. const { result } = renderHook(() => useSpoolBuddyState());
  31. act(() => {
  32. dispatchCustomEvent('spoolbuddy-weight', {
  33. weight_grams: 250.5,
  34. stable: true,
  35. raw_adc: 12345,
  36. device_id: 'dev-1',
  37. });
  38. });
  39. expect(result.current.weight).toBe(250.5);
  40. expect(result.current.weightStable).toBe(true);
  41. expect(result.current.rawAdc).toBe(12345);
  42. expect(result.current.deviceOnline).toBe(true);
  43. expect(result.current.deviceId).toBe('dev-1');
  44. });
  45. it('WEIGHT_UPDATE handles nested data format', () => {
  46. const { result } = renderHook(() => useSpoolBuddyState());
  47. act(() => {
  48. dispatchCustomEvent('spoolbuddy-weight', {
  49. data: {
  50. weight_grams: 100,
  51. stable: false,
  52. raw_adc: 9999,
  53. device_id: 'dev-2',
  54. },
  55. });
  56. });
  57. expect(result.current.weight).toBe(100);
  58. expect(result.current.weightStable).toBe(false);
  59. expect(result.current.rawAdc).toBe(9999);
  60. expect(result.current.deviceId).toBe('dev-2');
  61. });
  62. it('TAG_MATCHED sets matchedSpool and clears unknownTagUid', () => {
  63. const { result } = renderHook(() => useSpoolBuddyState());
  64. // First set an unknown tag
  65. act(() => {
  66. dispatchCustomEvent('spoolbuddy-unknown-tag', {
  67. tag_uid: 'AA:BB:CC',
  68. device_id: 'dev-1',
  69. });
  70. });
  71. expect(result.current.unknownTagUid).toBe('AA:BB:CC');
  72. // Now match a spool
  73. act(() => {
  74. dispatchCustomEvent('spoolbuddy-tag-matched', {
  75. tag_uid: 'AA:BB:CC',
  76. device_id: 'dev-1',
  77. spool: {
  78. id: 42,
  79. material: 'PLA',
  80. subtype: 'Silk',
  81. color_name: 'Red',
  82. rgba: 'FF0000FF',
  83. brand: 'Bambu',
  84. label_weight: 1000,
  85. core_weight: 250,
  86. weight_used: 100,
  87. },
  88. });
  89. });
  90. expect(result.current.matchedSpool).not.toBeNull();
  91. expect(result.current.matchedSpool!.id).toBe(42);
  92. expect(result.current.matchedSpool!.material).toBe('PLA');
  93. expect(result.current.matchedSpool!.subtype).toBe('Silk');
  94. expect(result.current.matchedSpool!.color_name).toBe('Red');
  95. expect(result.current.matchedSpool!.brand).toBe('Bambu');
  96. expect(result.current.matchedSpool!.label_weight).toBe(1000);
  97. expect(result.current.matchedSpool!.core_weight).toBe(250);
  98. expect(result.current.matchedSpool!.weight_used).toBe(100);
  99. expect(result.current.unknownTagUid).toBeNull();
  100. });
  101. it('UNKNOWN_TAG sets unknownTagUid and clears matchedSpool', () => {
  102. const { result } = renderHook(() => useSpoolBuddyState());
  103. // First match a spool
  104. act(() => {
  105. dispatchCustomEvent('spoolbuddy-tag-matched', {
  106. tag_uid: 'AA:BB:CC',
  107. device_id: 'dev-1',
  108. spool: {
  109. id: 1,
  110. material: 'PLA',
  111. label_weight: 1000,
  112. core_weight: 250,
  113. weight_used: 0,
  114. },
  115. });
  116. });
  117. expect(result.current.matchedSpool).not.toBeNull();
  118. // Now detect unknown tag
  119. act(() => {
  120. dispatchCustomEvent('spoolbuddy-unknown-tag', {
  121. tag_uid: 'DD:EE:FF',
  122. device_id: 'dev-1',
  123. });
  124. });
  125. expect(result.current.unknownTagUid).toBe('DD:EE:FF');
  126. expect(result.current.matchedSpool).toBeNull();
  127. });
  128. it('UNKNOWN_TAG with tray_uuid sets unknownTrayUuid', () => {
  129. const { result } = renderHook(() => useSpoolBuddyState());
  130. act(() => {
  131. dispatchCustomEvent('spoolbuddy-unknown-tag', {
  132. tag_uid: 'AABB1122334455FF',
  133. tray_uuid: 'DEADBEEFDEADBEEFDEADBEEFDEADBEEF',
  134. device_id: 'dev-1',
  135. });
  136. });
  137. expect(result.current.unknownTagUid).toBe('AABB1122334455FF');
  138. expect(result.current.unknownTrayUuid).toBe('DEADBEEFDEADBEEFDEADBEEFDEADBEEF');
  139. });
  140. it('UNKNOWN_TAG without tray_uuid leaves unknownTrayUuid null', () => {
  141. const { result } = renderHook(() => useSpoolBuddyState());
  142. act(() => {
  143. dispatchCustomEvent('spoolbuddy-unknown-tag', {
  144. tag_uid: 'AABB1122',
  145. device_id: 'dev-1',
  146. });
  147. });
  148. expect(result.current.unknownTagUid).toBe('AABB1122');
  149. expect(result.current.unknownTrayUuid).toBeNull();
  150. });
  151. it('TAG_MATCHED clears unknownTrayUuid', () => {
  152. const { result } = renderHook(() => useSpoolBuddyState());
  153. // Set tray_uuid via unknown tag
  154. act(() => {
  155. dispatchCustomEvent('spoolbuddy-unknown-tag', {
  156. tag_uid: 'AABB1122334455FF',
  157. tray_uuid: 'DEADBEEFDEADBEEFDEADBEEFDEADBEEF',
  158. device_id: 'dev-1',
  159. });
  160. });
  161. expect(result.current.unknownTrayUuid).toBe('DEADBEEFDEADBEEFDEADBEEFDEADBEEF');
  162. // Match resolves it
  163. act(() => {
  164. dispatchCustomEvent('spoolbuddy-tag-matched', {
  165. tag_uid: 'AABB1122334455FF',
  166. device_id: 'dev-1',
  167. spool: {
  168. id: 5,
  169. material: 'PLA',
  170. label_weight: 1000,
  171. core_weight: 250,
  172. weight_used: 0,
  173. },
  174. });
  175. });
  176. expect(result.current.unknownTrayUuid).toBeNull();
  177. expect(result.current.matchedSpool).not.toBeNull();
  178. });
  179. it('TAG_REMOVED clears unknownTrayUuid', () => {
  180. const { result } = renderHook(() => useSpoolBuddyState());
  181. act(() => {
  182. dispatchCustomEvent('spoolbuddy-unknown-tag', {
  183. tag_uid: 'AABB1122334455FF',
  184. tray_uuid: 'CAFEBABECAFEBABECAFEBABECAFEBABE',
  185. device_id: 'dev-1',
  186. });
  187. });
  188. expect(result.current.unknownTrayUuid).toBe('CAFEBABECAFEBABECAFEBABECAFEBABE');
  189. act(() => {
  190. dispatchCustomEvent('spoolbuddy-tag-removed', { device_id: 'dev-1' });
  191. });
  192. expect(result.current.unknownTrayUuid).toBeNull();
  193. expect(result.current.unknownTagUid).toBeNull();
  194. });
  195. it('TAG_REMOVED clears both matchedSpool and unknownTagUid', () => {
  196. const { result } = renderHook(() => useSpoolBuddyState());
  197. // Set a matched spool
  198. act(() => {
  199. dispatchCustomEvent('spoolbuddy-tag-matched', {
  200. tag_uid: 'AA:BB:CC',
  201. device_id: 'dev-1',
  202. spool: {
  203. id: 1,
  204. material: 'PLA',
  205. label_weight: 1000,
  206. core_weight: 250,
  207. weight_used: 0,
  208. },
  209. });
  210. });
  211. expect(result.current.matchedSpool).not.toBeNull();
  212. // Remove tag
  213. act(() => {
  214. dispatchCustomEvent('spoolbuddy-tag-removed', { device_id: 'dev-1' });
  215. });
  216. expect(result.current.matchedSpool).toBeNull();
  217. expect(result.current.unknownTagUid).toBeNull();
  218. });
  219. it('DEVICE_ONLINE sets deviceOnline=true', () => {
  220. const { result } = renderHook(() => useSpoolBuddyState());
  221. expect(result.current.deviceOnline).toBe(false);
  222. act(() => {
  223. dispatchCustomEvent('spoolbuddy-online', { device_id: 'dev-1' });
  224. });
  225. expect(result.current.deviceOnline).toBe(true);
  226. expect(result.current.deviceId).toBe('dev-1');
  227. });
  228. it('DEVICE_OFFLINE sets deviceOnline=false and clears weight/rawAdc', () => {
  229. const { result } = renderHook(() => useSpoolBuddyState());
  230. // First get some weight data
  231. act(() => {
  232. dispatchCustomEvent('spoolbuddy-weight', {
  233. weight_grams: 500,
  234. stable: true,
  235. raw_adc: 54321,
  236. device_id: 'dev-1',
  237. });
  238. });
  239. expect(result.current.weight).toBe(500);
  240. expect(result.current.rawAdc).toBe(54321);
  241. expect(result.current.deviceOnline).toBe(true);
  242. // Go offline
  243. act(() => {
  244. dispatchCustomEvent('spoolbuddy-offline', { device_id: 'dev-1' });
  245. });
  246. expect(result.current.deviceOnline).toBe(false);
  247. expect(result.current.weight).toBeNull();
  248. expect(result.current.weightStable).toBe(false);
  249. expect(result.current.rawAdc).toBeNull();
  250. });
  251. it('computes remainingWeight from matchedSpool', () => {
  252. const { result } = renderHook(() => useSpoolBuddyState());
  253. act(() => {
  254. dispatchCustomEvent('spoolbuddy-tag-matched', {
  255. tag_uid: 'AA:BB:CC',
  256. device_id: 'dev-1',
  257. spool: {
  258. id: 1,
  259. material: 'PLA',
  260. label_weight: 1000,
  261. core_weight: 250,
  262. weight_used: 300,
  263. },
  264. });
  265. });
  266. // remainingWeight = label_weight - weight_used = 1000 - 300 = 700
  267. expect(result.current.remainingWeight).toBe(700);
  268. });
  269. it('remainingWeight is clamped to 0 when weight_used exceeds label_weight', () => {
  270. const { result } = renderHook(() => useSpoolBuddyState());
  271. act(() => {
  272. dispatchCustomEvent('spoolbuddy-tag-matched', {
  273. tag_uid: 'AA:BB:CC',
  274. device_id: 'dev-1',
  275. spool: {
  276. id: 1,
  277. material: 'PLA',
  278. label_weight: 1000,
  279. core_weight: 250,
  280. weight_used: 1200,
  281. },
  282. });
  283. });
  284. expect(result.current.remainingWeight).toBe(0);
  285. });
  286. it('computes netWeight from weight and matchedSpool core_weight', () => {
  287. const { result } = renderHook(() => useSpoolBuddyState());
  288. // Set weight first
  289. act(() => {
  290. dispatchCustomEvent('spoolbuddy-weight', {
  291. weight_grams: 800,
  292. stable: true,
  293. raw_adc: 11111,
  294. device_id: 'dev-1',
  295. });
  296. });
  297. // Match a spool
  298. act(() => {
  299. dispatchCustomEvent('spoolbuddy-tag-matched', {
  300. tag_uid: 'AA:BB:CC',
  301. device_id: 'dev-1',
  302. spool: {
  303. id: 1,
  304. material: 'PLA',
  305. label_weight: 1000,
  306. core_weight: 250,
  307. weight_used: 0,
  308. },
  309. });
  310. });
  311. // netWeight = weight - core_weight = 800 - 250 = 550
  312. expect(result.current.netWeight).toBe(550);
  313. });
  314. it('netWeight is null when weight is null', () => {
  315. const { result } = renderHook(() => useSpoolBuddyState());
  316. act(() => {
  317. dispatchCustomEvent('spoolbuddy-tag-matched', {
  318. tag_uid: 'AA:BB:CC',
  319. device_id: 'dev-1',
  320. spool: {
  321. id: 1,
  322. material: 'PLA',
  323. label_weight: 1000,
  324. core_weight: 250,
  325. weight_used: 0,
  326. },
  327. });
  328. });
  329. expect(result.current.netWeight).toBeNull();
  330. });
  331. it('netWeight is null when no matchedSpool', () => {
  332. const { result } = renderHook(() => useSpoolBuddyState());
  333. act(() => {
  334. dispatchCustomEvent('spoolbuddy-weight', {
  335. weight_grams: 800,
  336. stable: true,
  337. raw_adc: 11111,
  338. device_id: 'dev-1',
  339. });
  340. });
  341. expect(result.current.netWeight).toBeNull();
  342. });
  343. it('cleans up event listeners on unmount', () => {
  344. const removeSpy = vi.spyOn(window, 'removeEventListener');
  345. const { unmount } = renderHook(() => useSpoolBuddyState());
  346. unmount();
  347. const removedEvents = removeSpy.mock.calls.map((c) => c[0]);
  348. expect(removedEvents).toContain('spoolbuddy-weight');
  349. expect(removedEvents).toContain('spoolbuddy-tag-matched');
  350. expect(removedEvents).toContain('spoolbuddy-unknown-tag');
  351. expect(removedEvents).toContain('spoolbuddy-tag-removed');
  352. expect(removedEvents).toContain('spoolbuddy-online');
  353. expect(removedEvents).toContain('spoolbuddy-offline');
  354. });
  355. });