/** * 凭证管理器契约测试 * * 这些测试验证ICredentialManager接口的契约是否被正确实现 */ import { describe, test, expect, beforeEach, afterEach } from '@jest/globals'; import { ICredentialManager, Platform, AccountStatus, SignRequest, VerifyRequest, AccountConfig, PacificaCredentials, CredentialManagerFactory } from '../../src/index'; describe('ICredentialManager Contract Tests', () => { let manager: ICredentialManager; let factory: CredentialManagerFactory; beforeEach(async () => { factory = new CredentialManagerFactory(); manager = await factory.create({ enableFileWatching: false }); }); afterEach(async () => { if (manager) { await manager.destroy(); } }); describe('基础功能测试', () => { test('应该能初始化凭证管理器', async () => { expect(manager).toBeDefined(); expect(typeof manager.loadConfig).toBe('function'); expect(typeof manager.getAccount).toBe('function'); expect(typeof manager.listAccounts).toBe('function'); expect(typeof manager.sign).toBe('function'); expect(typeof manager.verify).toBe('function'); }); test('应该能处理配置加载错误', async () => { const invalidConfigPath = './non-existent.json'; const result = await manager.loadConfig(invalidConfigPath); expect(result.success).toBe(false); expect(result.errors).toBeDefined(); expect(result.errors!.length).toBeGreaterThan(0); }); test('应该能获取特定账户', async () => { const accountId = 'test-account-1'; const account = manager.getAccount(accountId); if (account) { expect(account.id).toBe(accountId); expect(account.platform).toBeDefined(); expect(account.credentials).toBeDefined(); expect(account.status).toBeDefined(); } else { expect(account).toBeNull(); } const nonExistentAccount = manager.getAccount('non-existent-account'); expect(nonExistentAccount).toBeNull(); }); test('应该能列出所有账户', async () => { const accounts = manager.listAccounts(); expect(Array.isArray(accounts)).toBe(true); if (accounts.length > 0) { accounts.forEach(account => { expect(account.id).toBeDefined(); expect(account.platform).toBeDefined(); expect(account.credentials).toBeDefined(); expect(account.status).toBeDefined(); }); } }); }); describe('签名功能测试', () => { test('应该能处理签名请求', async () => { const message = new Uint8Array([1, 2, 3, 4]); const accountId = 'test-account'; try { const result = await manager.sign(accountId, message); expect(result).toBeDefined(); expect(typeof result.success).toBe('boolean'); expect(result.timestamp).toBeInstanceOf(Date); expect(typeof result.algorithm).toBe('string'); if (!result.success) { expect(result.error).toBeDefined(); } } catch (error) { // 账户不存在是预期的 expect(error).toBeDefined(); } }); test('应该能处理验证请求', async () => { const message = new Uint8Array([1, 2, 3, 4]); const signature = 'test-signature'; const accountId = 'test-account'; try { const result = await manager.verify(accountId, message, signature); expect(typeof result).toBe('boolean'); } catch (error) { // 账户不存在是预期的 expect(error).toBeDefined(); } }); }); describe('账户管理功能', () => { test('应该能添加账户', async () => { const accountConfig: AccountConfig = { id: 'test-pacifica-account', platform: Platform.PACIFICA, credentials: { type: 'pacifica', privateKey: '1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' } as PacificaCredentials }; try { const result = await manager.addAccount(accountConfig); expect(typeof result).toBe('boolean'); } catch (error) { // 可能因为无效的私钥格式失败 expect(error).toBeDefined(); } }); test('应该能移除账户', async () => { const accountId = 'test-account'; try { const result = await manager.removeAccount(accountId); expect(typeof result).toBe('boolean'); } catch (error) { expect(error).toBeDefined(); } }); }); describe('统计和监控', () => { test('应该能获取统计信息', async () => { try { const stats = await manager.getStats(); expect(stats).toBeDefined(); expect(typeof stats.totalAccounts).toBe('number'); expect(typeof stats.accountsByPlatform).toBe('object'); expect(typeof stats.accountsByStatus).toBe('object'); expect(typeof stats.totalSignatures).toBe('number'); expect(typeof stats.successRate).toBe('number'); expect(typeof stats.averageSignatureTime).toBe('number'); expect(typeof stats.memoryUsage).toBe('number'); expect(typeof stats.uptime).toBe('number'); } catch (error) { expect(error).toBeDefined(); } }); }); describe('配置监听功能', () => { test('应该能启动和停止配置监听', async () => { expect(() => { manager.watchConfig('./test-config.json'); }).not.toThrow(); expect(() => { manager.stopWatching(); }).not.toThrow(); }); }); describe('清理功能', () => { test('应该能正确销毁实例', async () => { expect(typeof manager.destroy).toBe('function'); // destroy方法应该能被调用而不抛出异常 await expect(manager.destroy()).resolves.toBeUndefined(); }); }); });