hot-reload.integration.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /**
  2. * Integration Test for Hot-Reload Configuration
  3. *
  4. * Tests the file watching and hot-reload functionality of the credential manager.
  5. * Validates that configuration changes are detected and applied without manual restarts.
  6. */
  7. import { CredentialManager } from '@/core/credential-manager/CredentialManager'
  8. import { Platform } from '@/types/credential'
  9. import fs from 'fs/promises'
  10. import path from 'path'
  11. import { tmpdir } from 'os'
  12. describe('Hot-Reload Integration Test', () => {
  13. let credentialManager: CredentialManager
  14. let tempConfigPath: string
  15. let tempDir: string
  16. beforeEach(async () => {
  17. // Create temporary directory for test configs
  18. tempDir = await fs.mkdtemp(path.join(tmpdir(), 'credential-manager-test-'))
  19. tempConfigPath = path.join(tempDir, 'accounts.json')
  20. // Initial configuration
  21. const initialConfig = {
  22. accounts: [
  23. {
  24. id: 'initial-account',
  25. name: 'Initial Account',
  26. platform: Platform.PACIFICA,
  27. enabled: true,
  28. credentials: {
  29. type: 'ed25519' as const,
  30. privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
  31. }
  32. }
  33. ],
  34. hedging: {
  35. platforms: {
  36. [Platform.PACIFICA]: {
  37. enabled: true,
  38. primaryAccounts: ['initial-account'],
  39. backupAccounts: [],
  40. loadBalanceStrategy: 'round-robin',
  41. healthCheckInterval: 30000,
  42. failoverThreshold: 3
  43. }
  44. },
  45. hedging: {
  46. enableCrossplatformBalancing: false,
  47. maxAccountsPerPlatform: 5,
  48. reservationTimeoutMs: 60000
  49. }
  50. }
  51. }
  52. await fs.writeFile(tempConfigPath, JSON.stringify(initialConfig, null, 2))
  53. credentialManager = new CredentialManager()
  54. await credentialManager.loadConfigurationFromFile(tempConfigPath, {
  55. enableHotReload: true,
  56. watchDebounceMs: 100
  57. })
  58. })
  59. afterEach(async () => {
  60. await credentialManager.shutdown()
  61. // Clean up temp files
  62. try {
  63. await fs.rm(tempDir, { recursive: true })
  64. } catch (error) {
  65. // Ignore cleanup errors
  66. }
  67. })
  68. describe('Configuration File Watching', () => {
  69. test('should detect new account additions', async () => {
  70. // Initial state - should have 1 account
  71. let accounts = credentialManager.getAllAccounts()
  72. expect(accounts).toHaveLength(1)
  73. expect(accounts[0].id).toBe('initial-account')
  74. // Update configuration with new account
  75. const updatedConfig = {
  76. accounts: [
  77. {
  78. id: 'initial-account',
  79. name: 'Initial Account',
  80. platform: Platform.PACIFICA,
  81. enabled: true,
  82. credentials: {
  83. type: 'ed25519' as const,
  84. privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
  85. }
  86. },
  87. {
  88. id: 'new-account',
  89. name: 'New Account',
  90. platform: Platform.ASTER,
  91. enabled: true,
  92. credentials: {
  93. type: 'secp256k1' as const,
  94. privateKey: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
  95. }
  96. }
  97. ],
  98. hedging: {
  99. platforms: {
  100. [Platform.PACIFICA]: {
  101. enabled: true,
  102. primaryAccounts: ['initial-account'],
  103. backupAccounts: [],
  104. loadBalanceStrategy: 'round-robin',
  105. healthCheckInterval: 30000,
  106. failoverThreshold: 3
  107. },
  108. [Platform.ASTER]: {
  109. enabled: true,
  110. primaryAccounts: ['new-account'],
  111. backupAccounts: [],
  112. loadBalanceStrategy: 'round-robin',
  113. healthCheckInterval: 30000,
  114. failoverThreshold: 3
  115. }
  116. },
  117. hedging: {
  118. enableCrossplatformBalancing: true,
  119. maxAccountsPerPlatform: 5,
  120. reservationTimeoutMs: 60000
  121. }
  122. }
  123. }
  124. await fs.writeFile(tempConfigPath, JSON.stringify(updatedConfig, null, 2))
  125. // Wait for file watcher to detect change
  126. await new Promise(resolve => setTimeout(resolve, 200))
  127. // Should now have 2 accounts
  128. accounts = credentialManager.getAllAccounts()
  129. expect(accounts).toHaveLength(2)
  130. expect(accounts.map(a => a.id)).toContain('initial-account')
  131. expect(accounts.map(a => a.id)).toContain('new-account')
  132. })
  133. test('should detect account removal', async () => {
  134. // Initial state
  135. let accounts = credentialManager.getAllAccounts()
  136. expect(accounts).toHaveLength(1)
  137. // Remove account from configuration
  138. const updatedConfig = {
  139. accounts: [],
  140. hedging: {
  141. platforms: {},
  142. hedging: {
  143. enableCrossplatformBalancing: false,
  144. maxAccountsPerPlatform: 5,
  145. reservationTimeoutMs: 60000
  146. }
  147. }
  148. }
  149. await fs.writeFile(tempConfigPath, JSON.stringify(updatedConfig, null, 2))
  150. // Wait for file watcher
  151. await new Promise(resolve => setTimeout(resolve, 200))
  152. // Should have no accounts
  153. accounts = credentialManager.getAllAccounts()
  154. expect(accounts).toHaveLength(0)
  155. })
  156. test('should detect account modifications', async () => {
  157. // Initial state
  158. let account = credentialManager.getAccount('initial-account')
  159. expect(account?.name).toBe('Initial Account')
  160. expect(account?.enabled).toBe(true)
  161. // Update account properties
  162. const updatedConfig = {
  163. accounts: [
  164. {
  165. id: 'initial-account',
  166. name: 'Modified Account Name',
  167. platform: Platform.PACIFICA,
  168. enabled: false, // Disable the account
  169. credentials: {
  170. type: 'ed25519' as const,
  171. privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
  172. }
  173. }
  174. ],
  175. hedging: {
  176. platforms: {
  177. [Platform.PACIFICA]: {
  178. enabled: true,
  179. primaryAccounts: ['initial-account'],
  180. backupAccounts: [],
  181. loadBalanceStrategy: 'round-robin',
  182. healthCheckInterval: 30000,
  183. failoverThreshold: 3
  184. }
  185. },
  186. hedging: {
  187. enableCrossplatformBalancing: false,
  188. maxAccountsPerPlatform: 5,
  189. reservationTimeoutMs: 60000
  190. }
  191. }
  192. }
  193. await fs.writeFile(tempConfigPath, JSON.stringify(updatedConfig, null, 2))
  194. // Wait for file watcher
  195. await new Promise(resolve => setTimeout(resolve, 200))
  196. // Account should be updated
  197. account = credentialManager.getAccount('initial-account')
  198. expect(account?.name).toBe('Modified Account Name')
  199. expect(account?.enabled).toBe(false)
  200. })
  201. })
  202. describe('Performance Requirements', () => {
  203. test('should reload configuration within 100ms', async () => {
  204. const updatedConfig = {
  205. accounts: [
  206. {
  207. id: 'performance-test',
  208. name: 'Performance Test Account',
  209. platform: Platform.PACIFICA,
  210. enabled: true,
  211. credentials: {
  212. type: 'ed25519' as const,
  213. privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
  214. }
  215. }
  216. ],
  217. hedging: {
  218. platforms: {
  219. [Platform.PACIFICA]: {
  220. enabled: true,
  221. primaryAccounts: ['performance-test'],
  222. backupAccounts: [],
  223. loadBalanceStrategy: 'round-robin',
  224. healthCheckInterval: 30000,
  225. failoverThreshold: 3
  226. }
  227. },
  228. hedging: {
  229. enableCrossplatformBalancing: false,
  230. maxAccountsPerPlatform: 5,
  231. reservationTimeoutMs: 60000
  232. }
  233. }
  234. }
  235. const startTime = Date.now()
  236. await fs.writeFile(tempConfigPath, JSON.stringify(updatedConfig, null, 2))
  237. // Wait for reload to complete
  238. let reloaded = false
  239. const maxWaitTime = 100
  240. const checkInterval = 10
  241. for (let elapsed = 0; elapsed < maxWaitTime; elapsed += checkInterval) {
  242. await new Promise(resolve => setTimeout(resolve, checkInterval))
  243. const account = credentialManager.getAccount('performance-test')
  244. if (account) {
  245. reloaded = true
  246. break
  247. }
  248. }
  249. const reloadTime = Date.now() - startTime
  250. expect(reloaded).toBe(true)
  251. expect(reloadTime).toBeLessThan(100) // Performance requirement
  252. })
  253. test('should handle rapid configuration changes', async () => {
  254. const configs = [
  255. {
  256. accounts: [
  257. {
  258. id: 'rapid-test-1',
  259. name: 'Rapid Test 1',
  260. platform: Platform.PACIFICA,
  261. enabled: true,
  262. credentials: {
  263. type: 'ed25519' as const,
  264. privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
  265. }
  266. }
  267. ]
  268. },
  269. {
  270. accounts: [
  271. {
  272. id: 'rapid-test-2',
  273. name: 'Rapid Test 2',
  274. platform: Platform.ASTER,
  275. enabled: true,
  276. credentials: {
  277. type: 'secp256k1' as const,
  278. privateKey: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
  279. }
  280. }
  281. ]
  282. }
  283. ]
  284. // Rapid succession of changes
  285. for (const config of configs) {
  286. const fullConfig = {
  287. ...config,
  288. hedging: {
  289. platforms: {},
  290. hedging: {
  291. enableCrossplatformBalancing: false,
  292. maxAccountsPerPlatform: 5,
  293. reservationTimeoutMs: 60000
  294. }
  295. }
  296. }
  297. await fs.writeFile(tempConfigPath, JSON.stringify(fullConfig, null, 2))
  298. await new Promise(resolve => setTimeout(resolve, 50))
  299. }
  300. // Wait for final state
  301. await new Promise(resolve => setTimeout(resolve, 200))
  302. // Should have the last configuration
  303. const accounts = credentialManager.getAllAccounts()
  304. expect(accounts).toHaveLength(1)
  305. expect(accounts[0].id).toBe('rapid-test-2')
  306. })
  307. })
  308. describe('Error Handling', () => {
  309. test('should handle invalid JSON gracefully', async () => {
  310. const invalidJson = '{ invalid json content'
  311. await fs.writeFile(tempConfigPath, invalidJson)
  312. // Wait for file watcher
  313. await new Promise(resolve => setTimeout(resolve, 200))
  314. // Should still have original accounts (no change due to error)
  315. const accounts = credentialManager.getAllAccounts()
  316. expect(accounts).toHaveLength(1)
  317. expect(accounts[0].id).toBe('initial-account')
  318. })
  319. test('should handle missing file gracefully', async () => {
  320. // Delete the config file
  321. await fs.unlink(tempConfigPath)
  322. // Wait for file watcher
  323. await new Promise(resolve => setTimeout(resolve, 200))
  324. // Should handle missing file without crashing
  325. // Behavior depends on implementation - might keep existing accounts or clear them
  326. const accounts = credentialManager.getAllAccounts()
  327. expect(Array.isArray(accounts)).toBe(true)
  328. })
  329. test('should validate configuration before applying changes', async () => {
  330. const invalidConfig = {
  331. accounts: [
  332. {
  333. id: '', // Invalid empty ID
  334. name: 'Invalid Account',
  335. platform: Platform.PACIFICA,
  336. enabled: true,
  337. credentials: {
  338. type: 'ed25519' as const,
  339. privateKey: 'invalid-key' // Invalid key
  340. }
  341. }
  342. ]
  343. }
  344. await fs.writeFile(tempConfigPath, JSON.stringify(invalidConfig, null, 2))
  345. // Wait for file watcher
  346. await new Promise(resolve => setTimeout(resolve, 200))
  347. // Should reject invalid config and keep original
  348. const accounts = credentialManager.getAllAccounts()
  349. expect(accounts).toHaveLength(1)
  350. expect(accounts[0].id).toBe('initial-account')
  351. })
  352. })
  353. describe('Configuration Backup and Recovery', () => {
  354. test('should maintain service availability during configuration updates', async () => {
  355. const message = new TextEncoder().encode('availability test')
  356. // Service should work before update
  357. let result = await credentialManager.sign('initial-account', message)
  358. expect(result.success).toBe(true)
  359. // Update configuration
  360. const updatedConfig = {
  361. accounts: [
  362. {
  363. id: 'initial-account',
  364. name: 'Updated Account',
  365. platform: Platform.PACIFICA,
  366. enabled: true,
  367. credentials: {
  368. type: 'ed25519' as const,
  369. privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
  370. }
  371. }
  372. ],
  373. hedging: {
  374. platforms: {
  375. [Platform.PACIFICA]: {
  376. enabled: true,
  377. primaryAccounts: ['initial-account'],
  378. backupAccounts: [],
  379. loadBalanceStrategy: 'round-robin',
  380. healthCheckInterval: 30000,
  381. failoverThreshold: 3
  382. }
  383. },
  384. hedging: {
  385. enableCrossplatformBalancing: false,
  386. maxAccountsPerPlatform: 5,
  387. reservationTimeoutMs: 60000
  388. }
  389. }
  390. }
  391. await fs.writeFile(tempConfigPath, JSON.stringify(updatedConfig, null, 2))
  392. // Wait for reload
  393. await new Promise(resolve => setTimeout(resolve, 200))
  394. // Service should still work after update
  395. result = await credentialManager.sign('initial-account', message)
  396. expect(result.success).toBe(true)
  397. // Account should have updated name
  398. const account = credentialManager.getAccount('initial-account')
  399. expect(account?.name).toBe('Updated Account')
  400. })
  401. test('should handle partial configuration updates', async () => {
  402. // Add second account first
  403. const configWithTwoAccounts = {
  404. accounts: [
  405. {
  406. id: 'initial-account',
  407. name: 'Initial Account',
  408. platform: Platform.PACIFICA,
  409. enabled: true,
  410. credentials: {
  411. type: 'ed25519' as const,
  412. privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
  413. }
  414. },
  415. {
  416. id: 'second-account',
  417. name: 'Second Account',
  418. platform: Platform.ASTER,
  419. enabled: true,
  420. credentials: {
  421. type: 'secp256k1' as const,
  422. privateKey: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
  423. }
  424. }
  425. ],
  426. hedging: {
  427. platforms: {
  428. [Platform.PACIFICA]: {
  429. enabled: true,
  430. primaryAccounts: ['initial-account'],
  431. backupAccounts: [],
  432. loadBalanceStrategy: 'round-robin',
  433. healthCheckInterval: 30000,
  434. failoverThreshold: 3
  435. },
  436. [Platform.ASTER]: {
  437. enabled: true,
  438. primaryAccounts: ['second-account'],
  439. backupAccounts: [],
  440. loadBalanceStrategy: 'round-robin',
  441. healthCheckInterval: 30000,
  442. failoverThreshold: 3
  443. }
  444. },
  445. hedging: {
  446. enableCrossplatformBalancing: true,
  447. maxAccountsPerPlatform: 5,
  448. reservationTimeoutMs: 60000
  449. }
  450. }
  451. }
  452. await fs.writeFile(tempConfigPath, JSON.stringify(configWithTwoAccounts, null, 2))
  453. await new Promise(resolve => setTimeout(resolve, 200))
  454. // Should have both accounts
  455. let accounts = credentialManager.getAllAccounts()
  456. expect(accounts).toHaveLength(2)
  457. // Now remove one account
  458. const configWithOneAccount = {
  459. accounts: [
  460. {
  461. id: 'initial-account',
  462. name: 'Initial Account',
  463. platform: Platform.PACIFICA,
  464. enabled: true,
  465. credentials: {
  466. type: 'ed25519' as const,
  467. privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
  468. }
  469. }
  470. ],
  471. hedging: {
  472. platforms: {
  473. [Platform.PACIFICA]: {
  474. enabled: true,
  475. primaryAccounts: ['initial-account'],
  476. backupAccounts: [],
  477. loadBalanceStrategy: 'round-robin',
  478. healthCheckInterval: 30000,
  479. failoverThreshold: 3
  480. }
  481. },
  482. hedging: {
  483. enableCrossplatformBalancing: false,
  484. maxAccountsPerPlatform: 5,
  485. reservationTimeoutMs: 60000
  486. }
  487. }
  488. }
  489. await fs.writeFile(tempConfigPath, JSON.stringify(configWithOneAccount, null, 2))
  490. await new Promise(resolve => setTimeout(resolve, 200))
  491. // Should have only one account
  492. accounts = credentialManager.getAllAccounts()
  493. expect(accounts).toHaveLength(1)
  494. expect(accounts[0].id).toBe('initial-account')
  495. })
  496. })
  497. })