|
| 1 | +import type { MultichainAccountGroupId } from './group'; |
| 2 | +import { |
| 3 | + getGroupIndexFromMultichainAccountGroupId, |
| 4 | + isMultichainAccountGroupId, |
| 5 | + toMultichainAccountGroupId, |
| 6 | +} from './group'; |
| 7 | +import { toMultichainAccountWalletId } from './wallet'; |
| 8 | +import { MOCK_ENTROPY_SOURCE_1 } from '../../mocks'; |
| 9 | +import { toAccountGroupId } from '../group'; |
| 10 | +import { AccountWalletType, toAccountWalletId } from '../wallet'; |
| 11 | + |
| 12 | +describe('multichain group', () => { |
| 13 | + describe('toMultichainAccountGroupId', () => { |
| 14 | + it('computes a multichain account group id with a group index', () => { |
| 15 | + const groupIndex = 1; |
| 16 | + |
| 17 | + const walletId = toMultichainAccountWalletId(MOCK_ENTROPY_SOURCE_1); |
| 18 | + const groupId = toMultichainAccountGroupId(walletId, groupIndex); |
| 19 | + |
| 20 | + expect(groupId.startsWith(walletId)).toBe(true); |
| 21 | + expect(groupId.endsWith(`/${groupIndex}`)).toBe(true); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('isMultichainAccountGroupId', () => { |
| 26 | + it('returns true if a group id is a multichain group id', () => { |
| 27 | + const walletId = toMultichainAccountWalletId(MOCK_ENTROPY_SOURCE_1); |
| 28 | + const groupId = toMultichainAccountGroupId(walletId, 0); |
| 29 | + |
| 30 | + expect(isMultichainAccountGroupId(groupId)).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it('fails if a group id is not a multichain group id', () => { |
| 34 | + const walletId = toAccountWalletId( |
| 35 | + AccountWalletType.Keyring, |
| 36 | + MOCK_ENTROPY_SOURCE_1, |
| 37 | + ); |
| 38 | + const groupId = toAccountGroupId(walletId, 'test'); |
| 39 | + |
| 40 | + expect(isMultichainAccountGroupId(groupId)).toBe(false); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('getGroupIndexFromMultichainAccountGroupId', () => { |
| 45 | + it('extracts the group index from its group id', () => { |
| 46 | + const groupIndex = 2; |
| 47 | + |
| 48 | + const walletId = toMultichainAccountWalletId(MOCK_ENTROPY_SOURCE_1); |
| 49 | + const groupId = toMultichainAccountGroupId(walletId, groupIndex); |
| 50 | + |
| 51 | + expect(getGroupIndexFromMultichainAccountGroupId(groupId)).toBe( |
| 52 | + groupIndex, |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('throws if it cannot extract group index', () => { |
| 57 | + const walletId = toAccountWalletId( |
| 58 | + AccountWalletType.Keyring, |
| 59 | + MOCK_ENTROPY_SOURCE_1, |
| 60 | + ); |
| 61 | + const groupId = toAccountGroupId(walletId, 'test'); |
| 62 | + |
| 63 | + expect(() => |
| 64 | + getGroupIndexFromMultichainAccountGroupId( |
| 65 | + // Force the error case even though, type wise, this should not |
| 66 | + // be possible! |
| 67 | + groupId as unknown as MultichainAccountGroupId, |
| 68 | + ), |
| 69 | + ).toThrow('Unable to extract group index'); |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments