@spacesops/wdk-react-native-core 1.0.0-beta.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. package/README.md +551 -0
  2. package/dist/__mocks__/react.d.ts +40 -0
  3. package/dist/__mocks__/react.js +33 -0
  4. package/dist/__mocks__/react.js.map +1 -0
  5. package/dist/__mocks__/secureStorage.d.ts +26 -0
  6. package/dist/__mocks__/secureStorage.js +80 -0
  7. package/dist/__mocks__/secureStorage.js.map +1 -0
  8. package/dist/__tests__/hooks/useBalance.test.d.ts +6 -0
  9. package/dist/__tests__/hooks/useBalance.test.js +217 -0
  10. package/dist/__tests__/hooks/useBalance.test.js.map +1 -0
  11. package/dist/__tests__/hooks/useWallet.test.d.ts +6 -0
  12. package/dist/__tests__/hooks/useWallet.test.js +105 -0
  13. package/dist/__tests__/hooks/useWallet.test.js.map +1 -0
  14. package/dist/__tests__/hooks/useWalletManager.test.d.ts +6 -0
  15. package/dist/__tests__/hooks/useWalletManager.test.js +193 -0
  16. package/dist/__tests__/hooks/useWalletManager.test.js.map +1 -0
  17. package/dist/__tests__/hooks/useWdkApp.test.d.ts +6 -0
  18. package/dist/__tests__/hooks/useWdkApp.test.js +55 -0
  19. package/dist/__tests__/hooks/useWdkApp.test.js.map +1 -0
  20. package/dist/__tests__/hooks/useWorklet.test.d.ts +6 -0
  21. package/dist/__tests__/hooks/useWorklet.test.js +168 -0
  22. package/dist/__tests__/hooks/useWorklet.test.js.map +1 -0
  23. package/dist/__tests__/provider/WdkAppProvider.test.d.ts +6 -0
  24. package/dist/__tests__/provider/WdkAppProvider.test.js +57 -0
  25. package/dist/__tests__/provider/WdkAppProvider.test.js.map +1 -0
  26. package/dist/__tests__/services/accountService.test.d.ts +6 -0
  27. package/dist/__tests__/services/accountService.test.js +216 -0
  28. package/dist/__tests__/services/accountService.test.js.map +1 -0
  29. package/dist/__tests__/services/addressService.test.d.ts +6 -0
  30. package/dist/__tests__/services/addressService.test.js +246 -0
  31. package/dist/__tests__/services/addressService.test.js.map +1 -0
  32. package/dist/__tests__/services/balanceService.test.d.ts +6 -0
  33. package/dist/__tests__/services/balanceService.test.js +333 -0
  34. package/dist/__tests__/services/balanceService.test.js.map +1 -0
  35. package/dist/__tests__/services/walletSetupService.test.d.ts +6 -0
  36. package/dist/__tests__/services/walletSetupService.test.js +304 -0
  37. package/dist/__tests__/services/walletSetupService.test.js.map +1 -0
  38. package/dist/__tests__/services/walletSwitchingService.test.d.ts +6 -0
  39. package/dist/__tests__/services/walletSwitchingService.test.js +209 -0
  40. package/dist/__tests__/services/walletSwitchingService.test.js.map +1 -0
  41. package/dist/__tests__/services/workletLifecycleService.test.d.ts +6 -0
  42. package/dist/__tests__/services/workletLifecycleService.test.js +451 -0
  43. package/dist/__tests__/services/workletLifecycleService.test.js.map +1 -0
  44. package/dist/__tests__/setup.d.ts +7 -0
  45. package/dist/__tests__/setup.js +49 -0
  46. package/dist/__tests__/setup.js.map +1 -0
  47. package/dist/__tests__/storage/mmkvStorage.test.d.ts +6 -0
  48. package/dist/__tests__/storage/mmkvStorage.test.js +213 -0
  49. package/dist/__tests__/storage/mmkvStorage.test.js.map +1 -0
  50. package/dist/__tests__/store/walletStore.test.d.ts +4 -0
  51. package/dist/__tests__/store/walletStore.test.js +86 -0
  52. package/dist/__tests__/store/walletStore.test.js.map +1 -0
  53. package/dist/__tests__/store/workletStore.test.d.ts +4 -0
  54. package/dist/__tests__/store/workletStore.test.js +255 -0
  55. package/dist/__tests__/store/workletStore.test.js.map +1 -0
  56. package/dist/__tests__/types/hrpc.test.d.ts +4 -0
  57. package/dist/__tests__/types/hrpc.test.js +99 -0
  58. package/dist/__tests__/types/hrpc.test.js.map +1 -0
  59. package/dist/__tests__/utils/balanceUtils.test.d.ts +4 -0
  60. package/dist/__tests__/utils/balanceUtils.test.js +133 -0
  61. package/dist/__tests__/utils/balanceUtils.test.js.map +1 -0
  62. package/dist/__tests__/utils/errorHandling.test.d.ts +6 -0
  63. package/dist/__tests__/utils/errorHandling.test.js +119 -0
  64. package/dist/__tests__/utils/errorHandling.test.js.map +1 -0
  65. package/dist/__tests__/utils/errorUtils.test.d.ts +4 -0
  66. package/dist/__tests__/utils/errorUtils.test.js +133 -0
  67. package/dist/__tests__/utils/errorUtils.test.js.map +1 -0
  68. package/dist/__tests__/utils/initializationState.test.d.ts +6 -0
  69. package/dist/__tests__/utils/initializationState.test.js +243 -0
  70. package/dist/__tests__/utils/initializationState.test.js.map +1 -0
  71. package/dist/__tests__/utils/jsonUtils.test.d.ts +4 -0
  72. package/dist/__tests__/utils/jsonUtils.test.js +118 -0
  73. package/dist/__tests__/utils/jsonUtils.test.js.map +1 -0
  74. package/dist/__tests__/utils/mmkvKeyManager.test.d.ts +6 -0
  75. package/dist/__tests__/utils/mmkvKeyManager.test.js +158 -0
  76. package/dist/__tests__/utils/mmkvKeyManager.test.js.map +1 -0
  77. package/dist/__tests__/utils/mnemonicUtils.test.d.ts +4 -0
  78. package/dist/__tests__/utils/mnemonicUtils.test.js +40 -0
  79. package/dist/__tests__/utils/mnemonicUtils.test.js.map +1 -0
  80. package/dist/__tests__/utils/raceConditions.test.d.ts +6 -0
  81. package/dist/__tests__/utils/raceConditions.test.js +239 -0
  82. package/dist/__tests__/utils/raceConditions.test.js.map +1 -0
  83. package/dist/__tests__/utils/result.test.d.ts +4 -0
  84. package/dist/__tests__/utils/result.test.js +66 -0
  85. package/dist/__tests__/utils/result.test.js.map +1 -0
  86. package/dist/__tests__/utils/schemas.test.d.ts +4 -0
  87. package/dist/__tests__/utils/schemas.test.js +66 -0
  88. package/dist/__tests__/utils/schemas.test.js.map +1 -0
  89. package/dist/__tests__/utils/storeHelpers.test.d.ts +4 -0
  90. package/dist/__tests__/utils/storeHelpers.test.js +227 -0
  91. package/dist/__tests__/utils/storeHelpers.test.js.map +1 -0
  92. package/dist/__tests__/utils/typeGuards.test.d.ts +4 -0
  93. package/dist/__tests__/utils/typeGuards.test.js +175 -0
  94. package/dist/__tests__/utils/typeGuards.test.js.map +1 -0
  95. package/dist/__tests__/utils/validation.test.d.ts +4 -0
  96. package/dist/__tests__/utils/validation.test.js +126 -0
  97. package/dist/__tests__/utils/validation.test.js.map +1 -0
  98. package/dist/__tests__/utils/walletUtils.test.d.ts +4 -0
  99. package/dist/__tests__/utils/walletUtils.test.js +156 -0
  100. package/dist/__tests__/utils/walletUtils.test.js.map +1 -0
  101. package/dist/hooks/useBalance.d.ts +170 -0
  102. package/dist/hooks/useBalance.js +442 -0
  103. package/dist/hooks/useBalance.js.map +1 -0
  104. package/dist/hooks/useWallet.d.ts +43 -0
  105. package/dist/hooks/useWallet.js +193 -0
  106. package/dist/hooks/useWallet.js.map +1 -0
  107. package/dist/hooks/useWalletManager.d.ts +100 -0
  108. package/dist/hooks/useWalletManager.js +526 -0
  109. package/dist/hooks/useWalletManager.js.map +1 -0
  110. package/dist/hooks/useWdkApp.d.ts +66 -0
  111. package/dist/hooks/useWdkApp.js +74 -0
  112. package/dist/hooks/useWdkApp.js.map +1 -0
  113. package/dist/hooks/useWorklet.d.ts +66 -0
  114. package/dist/hooks/useWorklet.js +42 -0
  115. package/dist/hooks/useWorklet.js.map +1 -0
  116. package/dist/index.d.ts +33 -0
  117. package/dist/index.js +36 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/provider/WdkAppProvider.d.ts +112 -0
  120. package/dist/provider/WdkAppProvider.js +558 -0
  121. package/dist/provider/WdkAppProvider.js.map +1 -0
  122. package/dist/services/accountService.d.ts +52 -0
  123. package/dist/services/accountService.js +119 -0
  124. package/dist/services/accountService.js.map +1 -0
  125. package/dist/services/addressService.d.ts +22 -0
  126. package/dist/services/addressService.js +110 -0
  127. package/dist/services/addressService.js.map +1 -0
  128. package/dist/services/balanceService.d.ts +114 -0
  129. package/dist/services/balanceService.js +208 -0
  130. package/dist/services/balanceService.js.map +1 -0
  131. package/dist/services/walletSetupService.d.ts +217 -0
  132. package/dist/services/walletSetupService.js +535 -0
  133. package/dist/services/walletSetupService.js.map +1 -0
  134. package/dist/services/walletSwitchingService.d.ts +48 -0
  135. package/dist/services/walletSwitchingService.js +143 -0
  136. package/dist/services/walletSwitchingService.js.map +1 -0
  137. package/dist/services/workletLifecycleService.d.ts +117 -0
  138. package/dist/services/workletLifecycleService.js +433 -0
  139. package/dist/services/workletLifecycleService.js.map +1 -0
  140. package/dist/storage/mmkvStorage.d.ts +44 -0
  141. package/dist/storage/mmkvStorage.js +348 -0
  142. package/dist/storage/mmkvStorage.js.map +1 -0
  143. package/dist/store/walletStore.d.ts +130 -0
  144. package/dist/store/walletStore.js +204 -0
  145. package/dist/store/walletStore.js.map +1 -0
  146. package/dist/store/workletStore.d.ts +121 -0
  147. package/dist/store/workletStore.js +256 -0
  148. package/dist/store/workletStore.js.map +1 -0
  149. package/dist/types/hrpc.d.ts +60 -0
  150. package/dist/types/hrpc.js +26 -0
  151. package/dist/types/hrpc.js.map +1 -0
  152. package/dist/types.d.ts +168 -0
  153. package/dist/types.js +7 -0
  154. package/dist/types.js.map +1 -0
  155. package/dist/utils/balanceUtils.d.ts +30 -0
  156. package/dist/utils/balanceUtils.js +86 -0
  157. package/dist/utils/balanceUtils.js.map +1 -0
  158. package/dist/utils/constants.d.ts +66 -0
  159. package/dist/utils/constants.js +73 -0
  160. package/dist/utils/constants.js.map +1 -0
  161. package/dist/utils/errorHandling.d.ts +47 -0
  162. package/dist/utils/errorHandling.js +54 -0
  163. package/dist/utils/errorHandling.js.map +1 -0
  164. package/dist/utils/errorUtils.d.ts +89 -0
  165. package/dist/utils/errorUtils.js +264 -0
  166. package/dist/utils/errorUtils.js.map +1 -0
  167. package/dist/utils/initializationState.d.ts +129 -0
  168. package/dist/utils/initializationState.js +220 -0
  169. package/dist/utils/initializationState.js.map +1 -0
  170. package/dist/utils/jsonUtils.d.ts +21 -0
  171. package/dist/utils/jsonUtils.js +67 -0
  172. package/dist/utils/jsonUtils.js.map +1 -0
  173. package/dist/utils/logger.d.ts +27 -0
  174. package/dist/utils/logger.js +50 -0
  175. package/dist/utils/logger.js.map +1 -0
  176. package/dist/utils/mmkvKeyManager.d.ts +57 -0
  177. package/dist/utils/mmkvKeyManager.js +214 -0
  178. package/dist/utils/mmkvKeyManager.js.map +1 -0
  179. package/dist/utils/mnemonicUtils.d.ts +17 -0
  180. package/dist/utils/mnemonicUtils.js +28 -0
  181. package/dist/utils/mnemonicUtils.js.map +1 -0
  182. package/dist/utils/operationMutex.d.ts +69 -0
  183. package/dist/utils/operationMutex.js +150 -0
  184. package/dist/utils/operationMutex.js.map +1 -0
  185. package/dist/utils/result.d.ts +34 -0
  186. package/dist/utils/result.js +43 -0
  187. package/dist/utils/result.js.map +1 -0
  188. package/dist/utils/schemas.d.ts +365 -0
  189. package/dist/utils/schemas.js +152 -0
  190. package/dist/utils/schemas.js.map +1 -0
  191. package/dist/utils/storeHelpers.d.ts +112 -0
  192. package/dist/utils/storeHelpers.js +198 -0
  193. package/dist/utils/storeHelpers.js.map +1 -0
  194. package/dist/utils/typeGuards.d.ts +70 -0
  195. package/dist/utils/typeGuards.js +95 -0
  196. package/dist/utils/typeGuards.js.map +1 -0
  197. package/dist/utils/validation.d.ts +53 -0
  198. package/dist/utils/validation.js +152 -0
  199. package/dist/utils/validation.js.map +1 -0
  200. package/dist/utils/walletStateHelpers.d.ts +62 -0
  201. package/dist/utils/walletStateHelpers.js +81 -0
  202. package/dist/utils/walletStateHelpers.js.map +1 -0
  203. package/dist/utils/walletUtils.d.ts +38 -0
  204. package/dist/utils/walletUtils.js +87 -0
  205. package/dist/utils/walletUtils.js.map +1 -0
  206. package/package.json +79 -0
  207. package/src/__mocks__/react.ts +34 -0
  208. package/src/__mocks__/secureStorage.ts +87 -0
  209. package/src/__tests__/hooks/useBalance.test.ts +286 -0
  210. package/src/__tests__/hooks/useWallet.test.ts +122 -0
  211. package/src/__tests__/hooks/useWalletManager.test.ts +271 -0
  212. package/src/__tests__/hooks/useWdkApp.test.tsx +59 -0
  213. package/src/__tests__/hooks/useWorklet.test.ts +195 -0
  214. package/src/__tests__/provider/WdkAppProvider.test.tsx +67 -0
  215. package/src/__tests__/services/accountService.test.ts +337 -0
  216. package/src/__tests__/services/addressService.test.ts +312 -0
  217. package/src/__tests__/services/balanceService.test.ts +411 -0
  218. package/src/__tests__/services/walletSetupService.test.ts +463 -0
  219. package/src/__tests__/services/walletSwitchingService.test.ts +258 -0
  220. package/src/__tests__/services/workletLifecycleService.test.ts +514 -0
  221. package/src/__tests__/setup.ts +54 -0
  222. package/src/__tests__/storage/mmkvStorage.test.ts +325 -0
  223. package/src/__tests__/store/walletStore.test.ts +101 -0
  224. package/src/__tests__/store/workletStore.test.ts +329 -0
  225. package/src/__tests__/types/hrpc.test.ts +125 -0
  226. package/src/__tests__/utils/balanceUtils.test.ts +151 -0
  227. package/src/__tests__/utils/errorHandling.test.ts +147 -0
  228. package/src/__tests__/utils/errorUtils.test.ts +170 -0
  229. package/src/__tests__/utils/initializationState.test.ts +337 -0
  230. package/src/__tests__/utils/jsonUtils.test.ts +131 -0
  231. package/src/__tests__/utils/mmkvKeyManager.test.ts +205 -0
  232. package/src/__tests__/utils/mnemonicUtils.test.ts +48 -0
  233. package/src/__tests__/utils/raceConditions.test.ts +292 -0
  234. package/src/__tests__/utils/result.test.ts +73 -0
  235. package/src/__tests__/utils/schemas.test.ts +82 -0
  236. package/src/__tests__/utils/storeHelpers.test.ts +323 -0
  237. package/src/__tests__/utils/typeGuards.test.ts +209 -0
  238. package/src/__tests__/utils/validation.test.ts +152 -0
  239. package/src/__tests__/utils/walletUtils.test.ts +195 -0
  240. package/src/hooks/useBalance.ts +597 -0
  241. package/src/hooks/useWallet.ts +288 -0
  242. package/src/hooks/useWalletManager.ts +654 -0
  243. package/src/hooks/useWdkApp.ts +78 -0
  244. package/src/hooks/useWorklet.ts +109 -0
  245. package/src/index.ts +112 -0
  246. package/src/provider/WdkAppProvider.tsx +764 -0
  247. package/src/services/accountService.ts +134 -0
  248. package/src/services/addressService.ts +125 -0
  249. package/src/services/balanceService.ts +291 -0
  250. package/src/services/walletSetupService.ts +695 -0
  251. package/src/services/walletSwitchingService.ts +157 -0
  252. package/src/services/workletLifecycleService.ts +561 -0
  253. package/src/storage/mmkvStorage.ts +417 -0
  254. package/src/store/walletStore.ts +301 -0
  255. package/src/store/workletStore.ts +340 -0
  256. package/src/types/hrpc.ts +78 -0
  257. package/src/types.ts +188 -0
  258. package/src/utils/balanceUtils.ts +96 -0
  259. package/src/utils/constants.ts +84 -0
  260. package/src/utils/errorHandling.md +163 -0
  261. package/src/utils/errorHandling.ts +61 -0
  262. package/src/utils/errorUtils.ts +300 -0
  263. package/src/utils/initializationState.ts +242 -0
  264. package/src/utils/jsonUtils.ts +75 -0
  265. package/src/utils/logger.ts +54 -0
  266. package/src/utils/mmkvKeyManager.ts +256 -0
  267. package/src/utils/mnemonicUtils.ts +35 -0
  268. package/src/utils/operationMutex.ts +181 -0
  269. package/src/utils/result.ts +57 -0
  270. package/src/utils/schemas.ts +190 -0
  271. package/src/utils/storeHelpers.ts +236 -0
  272. package/src/utils/typeGuards.ts +129 -0
  273. package/src/utils/validation.ts +174 -0
  274. package/src/utils/walletStateHelpers.ts +104 -0
  275. package/src/utils/walletUtils.ts +107 -0
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Tests for errorHandling utilities
3
+ *
4
+ * Tests service error handling patterns
5
+ */
6
+
7
+ import { handleServiceError } from '../../utils/errorHandling'
8
+ import { normalizeError } from '../../utils/errorUtils'
9
+ import { logError } from '../../utils/logger'
10
+
11
+ // Mock dependencies
12
+ jest.mock('../../utils/errorUtils', () => ({
13
+ normalizeError: jest.fn(),
14
+ }))
15
+
16
+ jest.mock('../../utils/logger', () => ({
17
+ logError: jest.fn(),
18
+ }))
19
+
20
+ describe('errorHandling', () => {
21
+ beforeEach(() => {
22
+ jest.clearAllMocks()
23
+ })
24
+
25
+ describe('handleServiceError', () => {
26
+ it('should normalize and log error', () => {
27
+ const error = new Error('Test error')
28
+ const normalizedError = new Error('Normalized error')
29
+ ;(normalizeError as jest.Mock).mockReturnValue(normalizedError)
30
+
31
+ expect(() => {
32
+ handleServiceError(error, 'TestService', 'testOperation')
33
+ }).toThrow('Normalized error')
34
+
35
+ expect(normalizeError).toHaveBeenCalledWith(error, false, {
36
+ component: 'TestService',
37
+ operation: 'testOperation',
38
+ })
39
+ expect(logError).toHaveBeenCalledWith('[TestService] testOperation failed:', normalizedError)
40
+ })
41
+
42
+ it('should include context in error normalization', () => {
43
+ const error = new Error('Test error')
44
+ const normalizedError = new Error('Normalized error')
45
+ ;(normalizeError as jest.Mock).mockReturnValue(normalizedError)
46
+
47
+ const context = { network: 'ethereum', accountIndex: 0 }
48
+
49
+ expect(() => {
50
+ handleServiceError(error, 'TestService', 'testOperation', context)
51
+ }).toThrow('Normalized error')
52
+
53
+ expect(normalizeError).toHaveBeenCalledWith(error, false, {
54
+ component: 'TestService',
55
+ operation: 'testOperation',
56
+ ...context,
57
+ })
58
+ })
59
+
60
+ it('should not sanitize errors (sanitizeLevel: false)', () => {
61
+ const error = new Error('Test error')
62
+ const normalizedError = new Error('Normalized error')
63
+ ;(normalizeError as jest.Mock).mockReturnValue(normalizedError)
64
+
65
+ expect(() => {
66
+ handleServiceError(error, 'TestService', 'testOperation')
67
+ }).toThrow()
68
+
69
+ expect(normalizeError).toHaveBeenCalledWith(error, false, expect.any(Object))
70
+ })
71
+
72
+ it('should handle non-Error objects', () => {
73
+ const error = 'String error'
74
+ const normalizedError = new Error('Normalized error')
75
+ ;(normalizeError as jest.Mock).mockReturnValue(normalizedError)
76
+
77
+ expect(() => {
78
+ handleServiceError(error, 'TestService', 'testOperation')
79
+ }).toThrow('Normalized error')
80
+
81
+ expect(normalizeError).toHaveBeenCalledWith(error, false, {
82
+ component: 'TestService',
83
+ operation: 'testOperation',
84
+ })
85
+ })
86
+
87
+ it('should handle null/undefined errors', () => {
88
+ const normalizedError = new Error('Normalized error')
89
+ ;(normalizeError as jest.Mock).mockReturnValue(normalizedError)
90
+
91
+ expect(() => {
92
+ handleServiceError(null, 'TestService', 'testOperation')
93
+ }).toThrow('Normalized error')
94
+
95
+ expect(normalizeError).toHaveBeenCalledWith(null, false, {
96
+ component: 'TestService',
97
+ operation: 'testOperation',
98
+ })
99
+ })
100
+
101
+ it('should always throw normalized error', () => {
102
+ const error = new Error('Test error')
103
+ const normalizedError = new Error('Normalized error')
104
+ ;(normalizeError as jest.Mock).mockReturnValue(normalizedError)
105
+
106
+ expect(() => {
107
+ handleServiceError(error, 'TestService', 'testOperation')
108
+ }).toThrow(normalizedError)
109
+
110
+ // Verify it throws (never returns)
111
+ try {
112
+ handleServiceError(error, 'TestService', 'testOperation')
113
+ fail('Should have thrown')
114
+ } catch (e) {
115
+ expect(e).toBe(normalizedError)
116
+ }
117
+ })
118
+
119
+ it('should log error with correct format', () => {
120
+ const error = new Error('Test error')
121
+ const normalizedError = new Error('Normalized error')
122
+ ;(normalizeError as jest.Mock).mockReturnValue(normalizedError)
123
+
124
+ expect(() => {
125
+ handleServiceError(error, 'MyService', 'myOperation')
126
+ }).toThrow()
127
+
128
+ expect(logError).toHaveBeenCalledWith('[MyService] myOperation failed:', normalizedError)
129
+ })
130
+
131
+ it('should handle complex error objects', () => {
132
+ const error = { code: 'ERR_CODE', message: 'Error message', details: { foo: 'bar' } }
133
+ const normalizedError = new Error('Normalized error')
134
+ ;(normalizeError as jest.Mock).mockReturnValue(normalizedError)
135
+
136
+ expect(() => {
137
+ handleServiceError(error, 'TestService', 'testOperation')
138
+ }).toThrow('Normalized error')
139
+
140
+ expect(normalizeError).toHaveBeenCalledWith(error, false, {
141
+ component: 'TestService',
142
+ operation: 'testOperation',
143
+ })
144
+ })
145
+ })
146
+ })
147
+
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Tests for error utility functions
3
+ */
4
+
5
+ import {
6
+ normalizeError,
7
+ getErrorMessage,
8
+ isErrorType,
9
+ createContextualError,
10
+ sanitizeErrorMessage,
11
+ SanitizationLevel,
12
+ } from '../../utils/errorUtils'
13
+
14
+ describe('errorUtils', () => {
15
+ describe('normalizeError', () => {
16
+ it('should normalize Error instances', () => {
17
+ const error = new Error('Test error')
18
+ const normalized = normalizeError(error, false)
19
+ expect(normalized).toBeInstanceOf(Error)
20
+ expect(normalized.message).toBe('Test error')
21
+ })
22
+
23
+ it('should normalize string errors', () => {
24
+ const normalized = normalizeError('String error', false)
25
+ expect(normalized).toBeInstanceOf(Error)
26
+ expect(normalized.message).toBe('String error')
27
+ })
28
+
29
+ it('should normalize object with message property', () => {
30
+ const error = { message: 'Object error' }
31
+ const normalized = normalizeError(error, false)
32
+ expect(normalized).toBeInstanceOf(Error)
33
+ expect(normalized.message).toBe('Object error')
34
+ })
35
+
36
+ it('should normalize other values', () => {
37
+ const normalized = normalizeError(123, false)
38
+ expect(normalized).toBeInstanceOf(Error)
39
+ expect(normalized.message).toBe('123')
40
+ })
41
+
42
+ it('should preserve error name and stack', () => {
43
+ const error = new TypeError('Type error')
44
+ error.stack = 'stack trace'
45
+ const normalized = normalizeError(error, false)
46
+ expect(normalized.name).toBe('TypeError')
47
+ expect(normalized.stack).toBe('stack trace')
48
+ })
49
+
50
+ it('should sanitize error messages in production', () => {
51
+ const originalEnv = process.env.NODE_ENV
52
+ process.env.NODE_ENV = 'production'
53
+
54
+ const error = new Error('Error with 0x1234567890123456789012345678901234567890')
55
+ const normalized = normalizeError(error, true)
56
+ expect(normalized.message).not.toContain('0x1234567890123456789012345678901234567890')
57
+
58
+ process.env.NODE_ENV = originalEnv
59
+ })
60
+
61
+ it('should use SanitizationLevel enum', () => {
62
+ const error = new Error('Error with 0x1234567890123456789012345678901234567890')
63
+
64
+ const none = normalizeError(error, SanitizationLevel.NONE)
65
+ expect(none.message).toContain('0x1234567890123456789012345678901234567890')
66
+
67
+ const dev = normalizeError(error, SanitizationLevel.DEVELOPMENT)
68
+ expect(dev.message).toContain('0x1234')
69
+
70
+ const prod = normalizeError(error, SanitizationLevel.PRODUCTION)
71
+ expect(prod.message).not.toContain('0x1234567890123456789012345678901234567890')
72
+ })
73
+
74
+ it('should handle boolean sanitize parameter for backward compatibility', () => {
75
+ const error = new Error('Error with 0x1234567890123456789012345678901234567890')
76
+
77
+ const sanitized = normalizeError(error, true)
78
+ expect(sanitized.message).not.toContain('0x1234567890123456789012345678901234567890')
79
+
80
+ const notSanitized = normalizeError(error, false)
81
+ expect(notSanitized.message).toContain('0x1234567890123456789012345678901234567890')
82
+ })
83
+
84
+ it('should sanitize stack traces based on level', () => {
85
+ const error = new Error('Test error')
86
+ error.stack = 'Error: Test error\n at file:///path/to/sensitive/key.js:1:1'
87
+
88
+ const prod = normalizeError(error, SanitizationLevel.PRODUCTION)
89
+ expect(prod.stack).not.toContain('/path/to/sensitive/key.js')
90
+
91
+ const none = normalizeError(error, SanitizationLevel.NONE)
92
+ expect(none.stack).toContain('/path/to/sensitive/key.js')
93
+ })
94
+ })
95
+
96
+ describe('getErrorMessage', () => {
97
+ it('should extract message from Error', () => {
98
+ const error = new Error('Test error')
99
+ expect(getErrorMessage(error)).toBe('Test error')
100
+ })
101
+
102
+ it('should extract message from string', () => {
103
+ expect(getErrorMessage('String error')).toBe('String error')
104
+ })
105
+
106
+ it('should extract message from object', () => {
107
+ expect(getErrorMessage({ message: 'Object error' })).toBe('Object error')
108
+ })
109
+ })
110
+
111
+ describe('isErrorType', () => {
112
+ it('should return true for matching error type', () => {
113
+ const error = new TypeError('Type error')
114
+ expect(isErrorType(error, 'TypeError')).toBe(true)
115
+ })
116
+
117
+ it('should return false for non-matching error type', () => {
118
+ const error = new TypeError('Type error')
119
+ expect(isErrorType(error, 'ReferenceError')).toBe(false)
120
+ })
121
+
122
+ it('should return false for non-Error values', () => {
123
+ expect(isErrorType('string', 'Error')).toBe(false)
124
+ expect(isErrorType(null, 'Error')).toBe(false)
125
+ })
126
+ })
127
+
128
+ describe('createContextualError', () => {
129
+ it('should create error with message', () => {
130
+ const error = createContextualError('Test error')
131
+ expect(error).toBeInstanceOf(Error)
132
+ expect(error.message).toBe('Test error')
133
+ })
134
+
135
+ it('should create error with context', () => {
136
+ const context = { operation: 'test', component: 'TestComponent' }
137
+ const error = createContextualError('Test error', context)
138
+ expect(error).toBeInstanceOf(Error)
139
+ expect((error as Error & { context: typeof context }).context).toEqual(context)
140
+ })
141
+ })
142
+
143
+ describe('sanitizeErrorMessage', () => {
144
+ it('should sanitize sensitive patterns in production', () => {
145
+ const message = 'Error with encryptionKey: 0x1234567890123456789012345678901234567890'
146
+ const sanitized = sanitizeErrorMessage(message, false)
147
+ expect(sanitized).not.toContain('0x1234567890123456789012345678901234567890')
148
+ expect(sanitized).toContain('[encryption data]')
149
+ })
150
+
151
+ it('should preserve more info in development', () => {
152
+ const message = 'Error with 0x1234567890123456789012345678901234567890'
153
+ const sanitized = sanitizeErrorMessage(message, true)
154
+ expect(sanitized).toContain('0x1234')
155
+ expect(sanitized).toContain('7890')
156
+ })
157
+
158
+ it('should not sanitize safe patterns', () => {
159
+ const message = 'Error with publicKey: 0x1234567890123456789012345678901234567890'
160
+ const sanitized = sanitizeErrorMessage(message, false)
161
+ expect(sanitized).toContain('publicKey')
162
+ })
163
+
164
+ it('should handle mnemonic phrases', () => {
165
+ const message = 'Error with mnemonic: word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12'
166
+ const sanitized = sanitizeErrorMessage(message, false)
167
+ expect(sanitized).toContain('[mnemonic phrase]')
168
+ })
169
+ })
170
+ })
@@ -0,0 +1,337 @@
1
+ /**
2
+ * Tests for initializationState utilities
3
+ *
4
+ * Tests worklet initialization state machine helpers
5
+ */
6
+
7
+ import {
8
+ InitializationStatus,
9
+ AppStatus,
10
+ isErrorStatus,
11
+ isReadyStatus,
12
+ isInProgressStatus,
13
+ isAppInProgressStatus,
14
+ isAppReadyStatus,
15
+ hasWorkletStarted,
16
+ canLoadWallet,
17
+ hasWorkletStartedApp,
18
+ canLoadWalletApp,
19
+ getStatusMessage,
20
+ getAppStatusMessage,
21
+ getWorkletStatus,
22
+ getCombinedStatus,
23
+ } from '../../utils/initializationState'
24
+
25
+ describe('initializationState', () => {
26
+ describe('isErrorStatus', () => {
27
+ it('should return true for ERROR status', () => {
28
+ expect(isErrorStatus(InitializationStatus.ERROR)).toBe(true)
29
+ })
30
+
31
+ it('should return false for non-error statuses', () => {
32
+ expect(isErrorStatus(InitializationStatus.IDLE)).toBe(false)
33
+ expect(isErrorStatus(InitializationStatus.STARTING_WORKLET)).toBe(false)
34
+ expect(isErrorStatus(InitializationStatus.WORKLET_READY)).toBe(false)
35
+ })
36
+ })
37
+
38
+ describe('isReadyStatus', () => {
39
+ it('should return true for WORKLET_READY status', () => {
40
+ expect(isReadyStatus(InitializationStatus.WORKLET_READY)).toBe(true)
41
+ })
42
+
43
+ it('should return false for non-ready statuses', () => {
44
+ expect(isReadyStatus(InitializationStatus.IDLE)).toBe(false)
45
+ expect(isReadyStatus(InitializationStatus.STARTING_WORKLET)).toBe(false)
46
+ expect(isReadyStatus(InitializationStatus.ERROR)).toBe(false)
47
+ })
48
+ })
49
+
50
+ describe('isInProgressStatus', () => {
51
+ it('should return true for STARTING_WORKLET status', () => {
52
+ expect(isInProgressStatus(InitializationStatus.STARTING_WORKLET)).toBe(true)
53
+ })
54
+
55
+ it('should return false for non-in-progress statuses', () => {
56
+ expect(isInProgressStatus(InitializationStatus.IDLE)).toBe(false)
57
+ expect(isInProgressStatus(InitializationStatus.WORKLET_READY)).toBe(false)
58
+ expect(isInProgressStatus(InitializationStatus.ERROR)).toBe(false)
59
+ })
60
+ })
61
+
62
+ describe('isAppInProgressStatus', () => {
63
+ it('should return true for in-progress app statuses', () => {
64
+ expect(isAppInProgressStatus(AppStatus.STARTING_WORKLET)).toBe(true)
65
+ expect(isAppInProgressStatus(AppStatus.LOADING_WALLET)).toBe(true)
66
+ })
67
+
68
+ it('should return false for non-in-progress app statuses', () => {
69
+ expect(isAppInProgressStatus(AppStatus.IDLE)).toBe(false)
70
+ expect(isAppInProgressStatus(AppStatus.WORKLET_READY)).toBe(false)
71
+ expect(isAppInProgressStatus(AppStatus.READY)).toBe(false)
72
+ expect(isAppInProgressStatus(AppStatus.ERROR)).toBe(false)
73
+ })
74
+ })
75
+
76
+ describe('isAppReadyStatus', () => {
77
+ it('should return true for READY app status', () => {
78
+ expect(isAppReadyStatus(AppStatus.READY)).toBe(true)
79
+ })
80
+
81
+ it('should return false for non-ready app statuses', () => {
82
+ expect(isAppReadyStatus(AppStatus.IDLE)).toBe(false)
83
+ expect(isAppReadyStatus(AppStatus.STARTING_WORKLET)).toBe(false)
84
+ expect(isAppReadyStatus(AppStatus.WORKLET_READY)).toBe(false)
85
+ expect(isAppReadyStatus(AppStatus.LOADING_WALLET)).toBe(false)
86
+ expect(isAppReadyStatus(AppStatus.ERROR)).toBe(false)
87
+ })
88
+ })
89
+
90
+ describe('hasWorkletStarted', () => {
91
+ it('should return true for WORKLET_READY status', () => {
92
+ expect(hasWorkletStarted(InitializationStatus.WORKLET_READY)).toBe(true)
93
+ })
94
+
95
+ it('should return true for ERROR status', () => {
96
+ expect(hasWorkletStarted(InitializationStatus.ERROR)).toBe(true)
97
+ })
98
+
99
+ it('should return false for IDLE and STARTING_WORKLET statuses', () => {
100
+ expect(hasWorkletStarted(InitializationStatus.IDLE)).toBe(false)
101
+ expect(hasWorkletStarted(InitializationStatus.STARTING_WORKLET)).toBe(false)
102
+ })
103
+ })
104
+
105
+ describe('canLoadWallet', () => {
106
+ it('should return true for WORKLET_READY status', () => {
107
+ expect(canLoadWallet(InitializationStatus.WORKLET_READY)).toBe(true)
108
+ })
109
+
110
+ it('should return false for other statuses', () => {
111
+ expect(canLoadWallet(InitializationStatus.IDLE)).toBe(false)
112
+ expect(canLoadWallet(InitializationStatus.STARTING_WORKLET)).toBe(false)
113
+ expect(canLoadWallet(InitializationStatus.ERROR)).toBe(false)
114
+ })
115
+ })
116
+
117
+ describe('hasWorkletStartedApp', () => {
118
+ it('should return true for app statuses where worklet has started', () => {
119
+ expect(hasWorkletStartedApp(AppStatus.WORKLET_READY)).toBe(true)
120
+ expect(hasWorkletStartedApp(AppStatus.LOADING_WALLET)).toBe(true)
121
+ expect(hasWorkletStartedApp(AppStatus.READY)).toBe(true)
122
+ expect(hasWorkletStartedApp(AppStatus.ERROR)).toBe(true)
123
+ })
124
+
125
+ it('should return false for IDLE and STARTING_WORKLET app statuses', () => {
126
+ expect(hasWorkletStartedApp(AppStatus.IDLE)).toBe(false)
127
+ expect(hasWorkletStartedApp(AppStatus.STARTING_WORKLET)).toBe(false)
128
+ })
129
+ })
130
+
131
+ describe('canLoadWalletApp', () => {
132
+ it('should return true for app statuses where wallet can be loaded', () => {
133
+ expect(canLoadWalletApp(AppStatus.WORKLET_READY)).toBe(true)
134
+ expect(canLoadWalletApp(AppStatus.LOADING_WALLET)).toBe(true)
135
+ expect(canLoadWalletApp(AppStatus.READY)).toBe(true)
136
+ })
137
+
138
+ it('should return false for other app statuses', () => {
139
+ expect(canLoadWalletApp(AppStatus.IDLE)).toBe(false)
140
+ expect(canLoadWalletApp(AppStatus.STARTING_WORKLET)).toBe(false)
141
+ expect(canLoadWalletApp(AppStatus.ERROR)).toBe(false)
142
+ })
143
+ })
144
+
145
+ describe('getStatusMessage', () => {
146
+ it('should return correct message for IDLE', () => {
147
+ expect(getStatusMessage(InitializationStatus.IDLE)).toBe('Not started')
148
+ })
149
+
150
+ it('should return correct message for STARTING_WORKLET', () => {
151
+ expect(getStatusMessage(InitializationStatus.STARTING_WORKLET)).toBe('Starting worklet...')
152
+ })
153
+
154
+ it('should return correct message for WORKLET_READY', () => {
155
+ expect(getStatusMessage(InitializationStatus.WORKLET_READY)).toBe('Worklet ready - can load wallets')
156
+ })
157
+
158
+ it('should return correct message for ERROR', () => {
159
+ expect(getStatusMessage(InitializationStatus.ERROR)).toBe('Worklet error')
160
+ })
161
+ })
162
+
163
+ describe('getAppStatusMessage', () => {
164
+ it('should return correct message for IDLE', () => {
165
+ expect(getAppStatusMessage(AppStatus.IDLE)).toBe('Not started')
166
+ })
167
+
168
+ it('should return correct message for STARTING_WORKLET', () => {
169
+ expect(getAppStatusMessage(AppStatus.STARTING_WORKLET)).toBe('Starting worklet...')
170
+ })
171
+
172
+ it('should return correct message for WORKLET_READY', () => {
173
+ expect(getAppStatusMessage(AppStatus.WORKLET_READY)).toBe('Worklet ready - can load wallets')
174
+ })
175
+
176
+ it('should return correct message for LOADING_WALLET', () => {
177
+ expect(getAppStatusMessage(AppStatus.LOADING_WALLET)).toBe('Loading wallet...')
178
+ })
179
+
180
+ it('should return correct message for READY', () => {
181
+ expect(getAppStatusMessage(AppStatus.READY)).toBe('Ready')
182
+ })
183
+
184
+ it('should return correct message for ERROR', () => {
185
+ expect(getAppStatusMessage(AppStatus.ERROR)).toBe('Error')
186
+ })
187
+ })
188
+
189
+ describe('getWorkletStatus', () => {
190
+ it('should return ERROR when error is present', () => {
191
+ const status = getWorkletStatus({
192
+ isWorkletStarted: false,
193
+ isLoading: false,
194
+ error: 'Test error',
195
+ })
196
+
197
+ expect(status).toBe(InitializationStatus.ERROR)
198
+ })
199
+
200
+ it('should return STARTING_WORKLET when loading', () => {
201
+ const status = getWorkletStatus({
202
+ isWorkletStarted: false,
203
+ isLoading: true,
204
+ error: null,
205
+ })
206
+
207
+ expect(status).toBe(InitializationStatus.STARTING_WORKLET)
208
+ })
209
+
210
+ it('should return IDLE when not started and not loading', () => {
211
+ const status = getWorkletStatus({
212
+ isWorkletStarted: false,
213
+ isLoading: false,
214
+ error: null,
215
+ })
216
+
217
+ expect(status).toBe(InitializationStatus.IDLE)
218
+ })
219
+
220
+ it('should return WORKLET_READY when started', () => {
221
+ const status = getWorkletStatus({
222
+ isWorkletStarted: true,
223
+ isLoading: false,
224
+ error: null,
225
+ })
226
+
227
+ expect(status).toBe(InitializationStatus.WORKLET_READY)
228
+ })
229
+ })
230
+
231
+ describe('getCombinedStatus', () => {
232
+ it('should return ERROR when worklet has error', () => {
233
+ const status = getCombinedStatus(
234
+ {
235
+ isWorkletStarted: false,
236
+ isLoading: false,
237
+ error: 'Worklet error',
238
+ },
239
+ { type: 'not_loaded' }
240
+ )
241
+
242
+ expect(status).toBe(AppStatus.ERROR)
243
+ })
244
+
245
+ it('should return STARTING_WORKLET when worklet is loading', () => {
246
+ const status = getCombinedStatus(
247
+ {
248
+ isWorkletStarted: false,
249
+ isLoading: true,
250
+ error: null,
251
+ },
252
+ { type: 'not_loaded' }
253
+ )
254
+
255
+ expect(status).toBe(AppStatus.STARTING_WORKLET)
256
+ })
257
+
258
+ it('should return IDLE when worklet not started', () => {
259
+ const status = getCombinedStatus(
260
+ {
261
+ isWorkletStarted: false,
262
+ isLoading: false,
263
+ error: null,
264
+ },
265
+ { type: 'not_loaded' }
266
+ )
267
+
268
+ expect(status).toBe(AppStatus.IDLE)
269
+ })
270
+
271
+ it('should return WORKLET_READY when worklet ready but wallet not loaded', () => {
272
+ const status = getCombinedStatus(
273
+ {
274
+ isWorkletStarted: true,
275
+ isLoading: false,
276
+ error: null,
277
+ },
278
+ { type: 'not_loaded' }
279
+ )
280
+
281
+ expect(status).toBe(AppStatus.WORKLET_READY)
282
+ })
283
+
284
+ it('should return LOADING_WALLET when wallet is checking', () => {
285
+ const status = getCombinedStatus(
286
+ {
287
+ isWorkletStarted: true,
288
+ isLoading: false,
289
+ error: null,
290
+ },
291
+ { type: 'checking' as const }
292
+ )
293
+
294
+ expect(status).toBe(AppStatus.LOADING_WALLET)
295
+ })
296
+
297
+ it('should return LOADING_WALLET when wallet is loading', () => {
298
+ const status = getCombinedStatus(
299
+ {
300
+ isWorkletStarted: true,
301
+ isLoading: false,
302
+ error: null,
303
+ },
304
+ { type: 'loading' as const }
305
+ )
306
+
307
+ expect(status).toBe(AppStatus.LOADING_WALLET)
308
+ })
309
+
310
+ it('should return READY when wallet is ready', () => {
311
+ const status = getCombinedStatus(
312
+ {
313
+ isWorkletStarted: true,
314
+ isLoading: false,
315
+ error: null,
316
+ },
317
+ { type: 'ready' as const }
318
+ )
319
+
320
+ expect(status).toBe(AppStatus.READY)
321
+ })
322
+
323
+ it('should return ERROR when wallet has error', () => {
324
+ const status = getCombinedStatus(
325
+ {
326
+ isWorkletStarted: true,
327
+ isLoading: false,
328
+ error: null,
329
+ },
330
+ { type: 'error' as const }
331
+ )
332
+
333
+ expect(status).toBe(AppStatus.ERROR)
334
+ })
335
+ })
336
+ })
337
+