http-client-setup.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * HTTP Client Module Test Setup
  3. *
  4. * This file provides setup and utilities for HTTP client module testing,
  5. * including mock configurations and test helpers.
  6. */
  7. import { jest } from '@jest/globals'
  8. // Mock credential manager for tests
  9. jest.mock('@/core/credential-manager', () => ({
  10. CredentialManager: jest.fn(),
  11. SignerFactory: {
  12. createSigner: jest.fn()
  13. },
  14. PlatformDetector: {
  15. detectPlatform: jest.fn()
  16. }
  17. }))
  18. // Mock external dependencies
  19. jest.mock('https-proxy-agent', () => ({
  20. HttpsProxyAgent: jest.fn()
  21. }))
  22. jest.mock('winston', () => ({
  23. createLogger: jest.fn(() => ({
  24. info: jest.fn(),
  25. error: jest.fn(),
  26. warn: jest.fn(),
  27. debug: jest.fn()
  28. })),
  29. format: {
  30. combine: jest.fn(),
  31. timestamp: jest.fn(),
  32. json: jest.fn()
  33. },
  34. transports: {
  35. Console: jest.fn(),
  36. File: jest.fn()
  37. }
  38. }))
  39. // Global test utilities
  40. global.testUtils = {
  41. // Mock HTTP response
  42. mockHttpResponse: (data: any, status = 200) => ({
  43. status,
  44. statusText: status >= 200 && status < 300 ? 'OK' : 'Error',
  45. ok: status >= 200 && status < 300,
  46. data,
  47. headers: new Headers({ 'content-type': 'application/json' })
  48. }),
  49. // Mock platform request
  50. mockPlatformRequest: (platform: string, accountId: string) => ({
  51. platform,
  52. accountId,
  53. method: 'GET' as const,
  54. url: '/api/test',
  55. headers: {},
  56. options: {}
  57. }),
  58. // Mock authentication context
  59. mockAuthContext: (platform: string) => ({
  60. accountId: 'test-account',
  61. platform,
  62. authType: 'signature' as const,
  63. isValid: true,
  64. lastRefresh: new Date(),
  65. failureCount: 0
  66. })
  67. }
  68. // Extend global types for test utilities
  69. declare global {
  70. var testUtils: {
  71. mockHttpResponse: (data: any, status?: number) => any
  72. mockPlatformRequest: (platform: string, accountId: string) => any
  73. mockAuthContext: (platform: string) => any
  74. }
  75. }
  76. export {}