123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- /**
- * Contract Test for AccountRegistry Interface
- *
- * Tests the core AccountRegistry functionality following TDD principles.
- * These tests MUST fail initially and pass after implementation.
- */
- import { AccountRegistry } from '@/core/credential-manager/AccountRegistry'
- import { Platform, Account } from '@/types/credential'
- describe('AccountRegistry Contract Test', () => {
- let registry: AccountRegistry
- beforeEach(() => {
- registry = new AccountRegistry()
- })
- describe('Account Loading', () => {
- test('should load accounts from configuration', async () => {
- const config = {
- accounts: [
- {
- id: 'test-account-1',
- name: 'Test Account 1',
- platform: Platform.PACIFICA,
- enabled: true,
- credentials: {
- type: 'ed25519' as const,
- privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
- }
- }
- ]
- }
- await registry.loadAccounts(config)
- const accounts = registry.getAllAccounts()
- expect(accounts).toHaveLength(1)
- expect(accounts[0].id).toBe('test-account-1')
- expect(accounts[0].platform).toBe(Platform.PACIFICA)
- })
- test('should handle empty configuration gracefully', async () => {
- const config = { accounts: [] }
- await registry.loadAccounts(config)
- const accounts = registry.getAllAccounts()
- expect(accounts).toHaveLength(0)
- })
- test('should validate account configuration before loading', async () => {
- const invalidConfig = {
- accounts: [
- {
- id: 'invalid-account',
- // Missing required fields
- platform: Platform.PACIFICA
- }
- ]
- }
- await expect(registry.loadAccounts(invalidConfig as any))
- .rejects.toThrow('Invalid account configuration')
- })
- })
- describe('Account Retrieval', () => {
- beforeEach(async () => {
- const config = {
- accounts: [
- {
- id: 'pacifica-account',
- name: 'Pacifica Test',
- platform: Platform.PACIFICA,
- enabled: true,
- credentials: {
- type: 'ed25519' as const,
- privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
- }
- },
- {
- id: 'aster-account',
- name: 'Aster Test',
- platform: Platform.ASTER,
- enabled: true,
- credentials: {
- type: 'secp256k1' as const,
- privateKey: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
- }
- }
- ]
- }
- await registry.loadAccounts(config)
- })
- test('should get account by ID', () => {
- const account = registry.getAccount('pacifica-account')
- expect(account).toBeDefined()
- expect(account?.id).toBe('pacifica-account')
- expect(account?.platform).toBe(Platform.PACIFICA)
- })
- test('should return undefined for non-existent account', () => {
- const account = registry.getAccount('non-existent')
- expect(account).toBeUndefined()
- })
- test('should get accounts by platform', () => {
- const pacificaAccounts = registry.getAccountsByPlatform(Platform.PACIFICA)
- const asterAccounts = registry.getAccountsByPlatform(Platform.ASTER)
- const binanceAccounts = registry.getAccountsByPlatform(Platform.BINANCE)
- expect(pacificaAccounts).toHaveLength(1)
- expect(asterAccounts).toHaveLength(1)
- expect(binanceAccounts).toHaveLength(0)
- expect(pacificaAccounts[0].id).toBe('pacifica-account')
- expect(asterAccounts[0].id).toBe('aster-account')
- })
- test('should get all accounts', () => {
- const allAccounts = registry.getAllAccounts()
- expect(allAccounts).toHaveLength(2)
- expect(allAccounts.map(a => a.id)).toContain('pacifica-account')
- expect(allAccounts.map(a => a.id)).toContain('aster-account')
- })
- test('should get enabled accounts only', () => {
- const enabledAccounts = registry.getEnabledAccounts()
- expect(enabledAccounts).toHaveLength(2)
- expect(enabledAccounts.every(a => a.enabled)).toBe(true)
- })
- })
- describe('Account Management', () => {
- test('should add new account', () => {
- const newAccount: Account = {
- id: 'new-account',
- name: 'New Account',
- platform: Platform.BINANCE,
- enabled: true,
- credentials: {
- type: 'hmac' as const,
- apiKey: 'test-api-key',
- secretKey: 'test-secret-key'
- }
- }
- registry.addAccount(newAccount)
- const retrieved = registry.getAccount('new-account')
- expect(retrieved).toBeDefined()
- expect(retrieved?.platform).toBe(Platform.BINANCE)
- })
- test('should prevent duplicate account IDs', () => {
- const account1: Account = {
- id: 'duplicate-id',
- name: 'Account 1',
- platform: Platform.PACIFICA,
- enabled: true,
- credentials: {
- type: 'ed25519' as const,
- privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
- }
- }
- const account2: Account = {
- id: 'duplicate-id',
- name: 'Account 2',
- platform: Platform.ASTER,
- enabled: true,
- credentials: {
- type: 'secp256k1' as const,
- privateKey: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
- }
- }
- registry.addAccount(account1)
- expect(() => registry.addAccount(account2))
- .toThrow('Account with ID duplicate-id already exists')
- })
- test('should update existing account', () => {
- const originalAccount: Account = {
- id: 'update-test',
- name: 'Original Name',
- platform: Platform.PACIFICA,
- enabled: true,
- credentials: {
- type: 'ed25519' as const,
- privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
- }
- }
- registry.addAccount(originalAccount)
- const updatedAccount: Account = {
- ...originalAccount,
- name: 'Updated Name',
- enabled: false
- }
- registry.updateAccount('update-test', updatedAccount)
- const retrieved = registry.getAccount('update-test')
- expect(retrieved?.name).toBe('Updated Name')
- expect(retrieved?.enabled).toBe(false)
- })
- test('should remove account', () => {
- const account: Account = {
- id: 'remove-test',
- name: 'Remove Test',
- platform: Platform.PACIFICA,
- enabled: true,
- credentials: {
- type: 'ed25519' as const,
- privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
- }
- }
- registry.addAccount(account)
- expect(registry.getAccount('remove-test')).toBeDefined()
- const removed = registry.removeAccount('remove-test')
- expect(removed).toBe(true)
- expect(registry.getAccount('remove-test')).toBeUndefined()
- })
- test('should return false when removing non-existent account', () => {
- const removed = registry.removeAccount('non-existent')
- expect(removed).toBe(false)
- })
- })
- describe('Platform Support', () => {
- test('should return supported platforms', () => {
- const platforms = registry.getSupportedPlatforms()
- expect(platforms).toContain(Platform.PACIFICA)
- expect(platforms).toContain(Platform.ASTER)
- expect(platforms).toContain(Platform.BINANCE)
- })
- test('should check if platform is supported', () => {
- expect(registry.isPlatformSupported(Platform.PACIFICA)).toBe(true)
- expect(registry.isPlatformSupported(Platform.ASTER)).toBe(true)
- expect(registry.isPlatformSupported(Platform.BINANCE)).toBe(true)
- })
- })
- describe('Performance Requirements', () => {
- test('should load accounts within performance limits', async () => {
- const config = {
- accounts: Array.from({ length: 100 }, (_, i) => ({
- id: `account-${i}`,
- name: `Account ${i}`,
- platform: Platform.PACIFICA,
- enabled: true,
- credentials: {
- type: 'ed25519' as const,
- privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
- }
- }))
- }
- const startTime = Date.now()
- await registry.loadAccounts(config)
- const duration = Date.now() - startTime
- // Should load 100 accounts within 100ms
- expect(duration).toBeLessThan(100)
- expect(registry.getAllAccounts()).toHaveLength(100)
- })
- test('should retrieve accounts efficiently', async () => {
- const config = {
- accounts: Array.from({ length: 50 }, (_, i) => ({
- id: `account-${i}`,
- name: `Account ${i}`,
- platform: i % 2 === 0 ? Platform.PACIFICA : Platform.ASTER,
- enabled: true,
- credentials: {
- type: 'ed25519' as const,
- privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
- }
- }))
- }
- await registry.loadAccounts(config)
- const startTime = Date.now()
- for (let i = 0; i < 1000; i++) {
- registry.getAccount(`account-${i % 50}`)
- }
- const duration = Date.now() - startTime
- // Should handle 1000 lookups within 10ms
- expect(duration).toBeLessThan(10)
- })
- })
- describe('Error Handling', () => {
- test('should handle malformed configuration gracefully', async () => {
- const malformedConfig = {
- accounts: [
- {
- id: 'malformed',
- platform: 'invalid-platform',
- credentials: 'not-an-object'
- }
- ]
- }
- await expect(registry.loadAccounts(malformedConfig as any))
- .rejects.toThrow()
- })
- test('should provide meaningful error messages', async () => {
- const invalidConfig = {
- accounts: [
- {
- id: '', // Empty ID
- name: 'Test',
- platform: Platform.PACIFICA,
- enabled: true,
- credentials: {
- type: 'ed25519' as const,
- privateKey: 'invalid-key'
- }
- }
- ]
- }
- await expect(registry.loadAccounts(invalidConfig))
- .rejects.toThrow('Account ID cannot be empty')
- })
- })
- })
|