useSpoolBuddyState.test.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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('TAG_REMOVED clears both matchedSpool and unknownTagUid', () => {
  129. const { result } = renderHook(() => useSpoolBuddyState());
  130. // Set a matched spool
  131. act(() => {
  132. dispatchCustomEvent('spoolbuddy-tag-matched', {
  133. tag_uid: 'AA:BB:CC',
  134. device_id: 'dev-1',
  135. spool: {
  136. id: 1,
  137. material: 'PLA',
  138. label_weight: 1000,
  139. core_weight: 250,
  140. weight_used: 0,
  141. },
  142. });
  143. });
  144. expect(result.current.matchedSpool).not.toBeNull();
  145. // Remove tag
  146. act(() => {
  147. dispatchCustomEvent('spoolbuddy-tag-removed', { device_id: 'dev-1' });
  148. });
  149. expect(result.current.matchedSpool).toBeNull();
  150. expect(result.current.unknownTagUid).toBeNull();
  151. });
  152. it('DEVICE_ONLINE sets deviceOnline=true', () => {
  153. const { result } = renderHook(() => useSpoolBuddyState());
  154. expect(result.current.deviceOnline).toBe(false);
  155. act(() => {
  156. dispatchCustomEvent('spoolbuddy-online', { device_id: 'dev-1' });
  157. });
  158. expect(result.current.deviceOnline).toBe(true);
  159. expect(result.current.deviceId).toBe('dev-1');
  160. });
  161. it('DEVICE_OFFLINE sets deviceOnline=false and clears weight/rawAdc', () => {
  162. const { result } = renderHook(() => useSpoolBuddyState());
  163. // First get some weight data
  164. act(() => {
  165. dispatchCustomEvent('spoolbuddy-weight', {
  166. weight_grams: 500,
  167. stable: true,
  168. raw_adc: 54321,
  169. device_id: 'dev-1',
  170. });
  171. });
  172. expect(result.current.weight).toBe(500);
  173. expect(result.current.rawAdc).toBe(54321);
  174. expect(result.current.deviceOnline).toBe(true);
  175. // Go offline
  176. act(() => {
  177. dispatchCustomEvent('spoolbuddy-offline', { device_id: 'dev-1' });
  178. });
  179. expect(result.current.deviceOnline).toBe(false);
  180. expect(result.current.weight).toBeNull();
  181. expect(result.current.weightStable).toBe(false);
  182. expect(result.current.rawAdc).toBeNull();
  183. });
  184. it('computes remainingWeight from matchedSpool', () => {
  185. const { result } = renderHook(() => useSpoolBuddyState());
  186. act(() => {
  187. dispatchCustomEvent('spoolbuddy-tag-matched', {
  188. tag_uid: 'AA:BB:CC',
  189. device_id: 'dev-1',
  190. spool: {
  191. id: 1,
  192. material: 'PLA',
  193. label_weight: 1000,
  194. core_weight: 250,
  195. weight_used: 300,
  196. },
  197. });
  198. });
  199. // remainingWeight = label_weight - weight_used = 1000 - 300 = 700
  200. expect(result.current.remainingWeight).toBe(700);
  201. });
  202. it('remainingWeight is clamped to 0 when weight_used exceeds label_weight', () => {
  203. const { result } = renderHook(() => useSpoolBuddyState());
  204. act(() => {
  205. dispatchCustomEvent('spoolbuddy-tag-matched', {
  206. tag_uid: 'AA:BB:CC',
  207. device_id: 'dev-1',
  208. spool: {
  209. id: 1,
  210. material: 'PLA',
  211. label_weight: 1000,
  212. core_weight: 250,
  213. weight_used: 1200,
  214. },
  215. });
  216. });
  217. expect(result.current.remainingWeight).toBe(0);
  218. });
  219. it('computes netWeight from weight and matchedSpool core_weight', () => {
  220. const { result } = renderHook(() => useSpoolBuddyState());
  221. // Set weight first
  222. act(() => {
  223. dispatchCustomEvent('spoolbuddy-weight', {
  224. weight_grams: 800,
  225. stable: true,
  226. raw_adc: 11111,
  227. device_id: 'dev-1',
  228. });
  229. });
  230. // Match a spool
  231. act(() => {
  232. dispatchCustomEvent('spoolbuddy-tag-matched', {
  233. tag_uid: 'AA:BB:CC',
  234. device_id: 'dev-1',
  235. spool: {
  236. id: 1,
  237. material: 'PLA',
  238. label_weight: 1000,
  239. core_weight: 250,
  240. weight_used: 0,
  241. },
  242. });
  243. });
  244. // netWeight = weight - core_weight = 800 - 250 = 550
  245. expect(result.current.netWeight).toBe(550);
  246. });
  247. it('netWeight is null when weight is null', () => {
  248. const { result } = renderHook(() => useSpoolBuddyState());
  249. act(() => {
  250. dispatchCustomEvent('spoolbuddy-tag-matched', {
  251. tag_uid: 'AA:BB:CC',
  252. device_id: 'dev-1',
  253. spool: {
  254. id: 1,
  255. material: 'PLA',
  256. label_weight: 1000,
  257. core_weight: 250,
  258. weight_used: 0,
  259. },
  260. });
  261. });
  262. expect(result.current.netWeight).toBeNull();
  263. });
  264. it('netWeight is null when no matchedSpool', () => {
  265. const { result } = renderHook(() => useSpoolBuddyState());
  266. act(() => {
  267. dispatchCustomEvent('spoolbuddy-weight', {
  268. weight_grams: 800,
  269. stable: true,
  270. raw_adc: 11111,
  271. device_id: 'dev-1',
  272. });
  273. });
  274. expect(result.current.netWeight).toBeNull();
  275. });
  276. it('cleans up event listeners on unmount', () => {
  277. const removeSpy = vi.spyOn(window, 'removeEventListener');
  278. const { unmount } = renderHook(() => useSpoolBuddyState());
  279. unmount();
  280. const removedEvents = removeSpy.mock.calls.map((c) => c[0]);
  281. expect(removedEvents).toContain('spoolbuddy-weight');
  282. expect(removedEvents).toContain('spoolbuddy-tag-matched');
  283. expect(removedEvents).toContain('spoolbuddy-unknown-tag');
  284. expect(removedEvents).toContain('spoolbuddy-tag-removed');
  285. expect(removedEvents).toContain('spoolbuddy-online');
  286. expect(removedEvents).toContain('spoolbuddy-offline');
  287. });
  288. });