credential-manager.contract.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * 凭证管理器契约测试
  3. *
  4. * 这些测试验证ICredentialManager接口的契约是否被正确实现
  5. */
  6. import { describe, test, expect, beforeEach, afterEach } from '@jest/globals';
  7. import {
  8. ICredentialManager,
  9. Platform,
  10. AccountStatus,
  11. SignRequest,
  12. VerifyRequest,
  13. AccountConfig,
  14. PacificaCredentials,
  15. CredentialManagerFactory
  16. } from '../../src/index';
  17. describe('ICredentialManager Contract Tests', () => {
  18. let manager: ICredentialManager;
  19. let factory: CredentialManagerFactory;
  20. beforeEach(async () => {
  21. factory = new CredentialManagerFactory();
  22. manager = await factory.create({ enableFileWatching: false });
  23. });
  24. afterEach(async () => {
  25. if (manager) {
  26. await manager.destroy();
  27. }
  28. });
  29. describe('基础功能测试', () => {
  30. test('应该能初始化凭证管理器', async () => {
  31. expect(manager).toBeDefined();
  32. expect(typeof manager.loadConfig).toBe('function');
  33. expect(typeof manager.getAccount).toBe('function');
  34. expect(typeof manager.listAccounts).toBe('function');
  35. expect(typeof manager.sign).toBe('function');
  36. expect(typeof manager.verify).toBe('function');
  37. });
  38. test('应该能处理配置加载错误', async () => {
  39. const invalidConfigPath = './non-existent.json';
  40. const result = await manager.loadConfig(invalidConfigPath);
  41. expect(result.success).toBe(false);
  42. expect(result.errors).toBeDefined();
  43. expect(result.errors!.length).toBeGreaterThan(0);
  44. });
  45. test('应该能获取特定账户', async () => {
  46. const accountId = 'test-account-1';
  47. const account = manager.getAccount(accountId);
  48. if (account) {
  49. expect(account.id).toBe(accountId);
  50. expect(account.platform).toBeDefined();
  51. expect(account.credentials).toBeDefined();
  52. expect(account.status).toBeDefined();
  53. } else {
  54. expect(account).toBeNull();
  55. }
  56. const nonExistentAccount = manager.getAccount('non-existent-account');
  57. expect(nonExistentAccount).toBeNull();
  58. });
  59. test('应该能列出所有账户', async () => {
  60. const accounts = manager.listAccounts();
  61. expect(Array.isArray(accounts)).toBe(true);
  62. if (accounts.length > 0) {
  63. accounts.forEach(account => {
  64. expect(account.id).toBeDefined();
  65. expect(account.platform).toBeDefined();
  66. expect(account.credentials).toBeDefined();
  67. expect(account.status).toBeDefined();
  68. });
  69. }
  70. });
  71. });
  72. describe('签名功能测试', () => {
  73. test('应该能处理签名请求', async () => {
  74. const message = new Uint8Array([1, 2, 3, 4]);
  75. const accountId = 'test-account';
  76. try {
  77. const result = await manager.sign(accountId, message);
  78. expect(result).toBeDefined();
  79. expect(typeof result.success).toBe('boolean');
  80. expect(result.timestamp).toBeInstanceOf(Date);
  81. expect(typeof result.algorithm).toBe('string');
  82. if (!result.success) {
  83. expect(result.error).toBeDefined();
  84. }
  85. } catch (error) {
  86. // 账户不存在是预期的
  87. expect(error).toBeDefined();
  88. }
  89. });
  90. test('应该能处理验证请求', async () => {
  91. const message = new Uint8Array([1, 2, 3, 4]);
  92. const signature = 'test-signature';
  93. const accountId = 'test-account';
  94. try {
  95. const result = await manager.verify(accountId, message, signature);
  96. expect(typeof result).toBe('boolean');
  97. } catch (error) {
  98. // 账户不存在是预期的
  99. expect(error).toBeDefined();
  100. }
  101. });
  102. });
  103. describe('账户管理功能', () => {
  104. test('应该能添加账户', async () => {
  105. const accountConfig: AccountConfig = {
  106. id: 'test-pacifica-account',
  107. platform: Platform.PACIFICA,
  108. credentials: {
  109. type: 'pacifica',
  110. privateKey: '1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
  111. } as PacificaCredentials
  112. };
  113. try {
  114. const result = await manager.addAccount(accountConfig);
  115. expect(typeof result).toBe('boolean');
  116. } catch (error) {
  117. // 可能因为无效的私钥格式失败
  118. expect(error).toBeDefined();
  119. }
  120. });
  121. test('应该能移除账户', async () => {
  122. const accountId = 'test-account';
  123. try {
  124. const result = await manager.removeAccount(accountId);
  125. expect(typeof result).toBe('boolean');
  126. } catch (error) {
  127. expect(error).toBeDefined();
  128. }
  129. });
  130. });
  131. describe('统计和监控', () => {
  132. test('应该能获取统计信息', async () => {
  133. try {
  134. const stats = await manager.getStats();
  135. expect(stats).toBeDefined();
  136. expect(typeof stats.totalAccounts).toBe('number');
  137. expect(typeof stats.accountsByPlatform).toBe('object');
  138. expect(typeof stats.accountsByStatus).toBe('object');
  139. expect(typeof stats.totalSignatures).toBe('number');
  140. expect(typeof stats.successRate).toBe('number');
  141. expect(typeof stats.averageSignatureTime).toBe('number');
  142. expect(typeof stats.memoryUsage).toBe('number');
  143. expect(typeof stats.uptime).toBe('number');
  144. } catch (error) {
  145. expect(error).toBeDefined();
  146. }
  147. });
  148. });
  149. describe('配置监听功能', () => {
  150. test('应该能启动和停止配置监听', async () => {
  151. expect(() => {
  152. manager.watchConfig('./test-config.json');
  153. }).not.toThrow();
  154. expect(() => {
  155. manager.stopWatching();
  156. }).not.toThrow();
  157. });
  158. });
  159. describe('清理功能', () => {
  160. test('应该能正确销毁实例', async () => {
  161. expect(typeof manager.destroy).toBe('function');
  162. // destroy方法应该能被调用而不抛出异常
  163. await expect(manager.destroy()).resolves.toBeUndefined();
  164. });
  165. });
  166. });