setup.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Jest测试环境设置
  3. *
  4. * 配置全局测试设置,包括超时、模拟和通用工具函数
  5. */
  6. // 扩展Jest超时时间,适应性能测试需求
  7. jest.setTimeout(30000);
  8. // 全局测试工具函数
  9. global.testUtils = {
  10. /**
  11. * 创建测试用的私钥(Ed25519格式)
  12. */
  13. createTestPrivateKey(): string {
  14. return 'abcd1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab';
  15. },
  16. /**
  17. * 创建测试用的消息
  18. */
  19. createTestMessage(): Uint8Array {
  20. return new TextEncoder().encode('Test message for signing');
  21. },
  22. /**
  23. * 验证签名格式
  24. */
  25. isValidSignatureFormat(signature: string): boolean {
  26. return /^[A-Za-z0-9+/]+=*$/.test(signature) && signature.length > 0;
  27. },
  28. /**
  29. * 模拟延迟函数
  30. */
  31. async delay(ms: number): Promise<void> {
  32. return new Promise(resolve => setTimeout(resolve, ms));
  33. },
  34. /**
  35. * 创建测试账户配置
  36. */
  37. createTestAccountConfig() {
  38. return {
  39. version: '1.0',
  40. accounts: [
  41. {
  42. id: 'test-pacifica-001',
  43. platform: 'pacifica',
  44. credentials: {
  45. type: 'pacifica',
  46. privateKey: this.createTestPrivateKey(),
  47. },
  48. metadata: {
  49. alias: '测试Pacifica账户',
  50. description: '用于单元测试',
  51. },
  52. },
  53. {
  54. id: 'test-binance-001',
  55. platform: 'binance',
  56. credentials: {
  57. type: 'binance',
  58. apiKey: 'test_api_key',
  59. secretKey: 'test_secret_key',
  60. },
  61. metadata: {
  62. alias: '测试Binance账户',
  63. },
  64. },
  65. ],
  66. };
  67. },
  68. };
  69. // 类型声明
  70. declare global {
  71. var testUtils: {
  72. createTestPrivateKey(): string;
  73. createTestMessage(): Uint8Array;
  74. isValidSignatureFormat(signature: string): boolean;
  75. delay(ms: number): Promise<void>;
  76. createTestAccountConfig(): any;
  77. };
  78. }
  79. export {};