import { describe, it, expect } from 'vitest'; import { Mnemonic, MnemonicRaw } from '~/src/mnemonic'; describe('Success cases', () => { // Simple array of cases const testCases: Array<{ fromString: string; fromWords: Array; passphrase?: string; raw: MnemonicRaw; toString: string; toStringWithoutPassphrase: string; toWords: Array; toWordsWithoutPassphrase: Array; }> = [ // No pass-phrase { fromString: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust', fromWords: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], raw: { words: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], }, toString: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust', toStringWithoutPassphrase: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust', toWords: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], toWordsWithoutPassphrase: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], }, // Passphrase { fromString: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust GeneralProtocols!', fromWords: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', 'GeneralProtocols!', ], passphrase: 'GeneralProtocols!', raw: { words: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], passphrase: 'GeneralProtocols!', }, toString: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust GeneralProtocols!', toStringWithoutPassphrase: 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust', toWords: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', 'GeneralProtocols!', ], toWordsWithoutPassphrase: [ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ], }, ]; it.each(testCases)( 'from(string)', ({ fromString: phrase, raw: expected }) => { const mnemonic = Mnemonic.from(phrase); expect(mnemonic.toRaw()).toStrictEqual(expected); }, ); it.each(testCases)('from(words)', ({ fromWords, raw: expected }) => { const mnemonic = Mnemonic.from(fromWords); expect(mnemonic.toRaw()).toStrictEqual(expected); }); it.each(testCases)('fromString', ({ fromString, raw: expected }) => { const mnemonic = Mnemonic.fromString(fromString); expect(mnemonic.toRaw()).toStrictEqual(expected); }); it.each(testCases)('fromWords', ({ fromWords, raw: expected }) => { const mnemonic = Mnemonic.fromWords(fromWords); expect(mnemonic.toRaw()).toStrictEqual(expected); }); it.each(testCases)('fromWords', ({ fromWords, raw: expected }) => { const mnemonic = Mnemonic.fromWords(fromWords); expect(mnemonic.toRaw()).toStrictEqual(expected); }); it.each(testCases)('toString', ({ toString, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.toString()).toStrictEqual(toString); }); it.each(testCases)( 'toString(withoutPassphrase)', ({ toStringWithoutPassphrase, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.toString(false)).toStrictEqual(toStringWithoutPassphrase); }, ); it.each(testCases)('toWords', ({ toWords, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.toWords()).toStrictEqual(toWords); }); it.each(testCases)( 'toWords(withoutPassphrase)', ({ toWordsWithoutPassphrase, passphrase, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.toWords(false)).toStrictEqual(toWordsWithoutPassphrase); }, ); it.each(testCases)('getPassphrase', ({ passphrase, raw }) => { const mnemonic = Mnemonic.fromRaw(raw); expect(mnemonic.getPassphrase()).toStrictEqual(passphrase); }); }); describe('Generate Cases', () => { it('generateRandom should return instance of Mnemonic', () => { const mnemonic = Mnemonic.generateRandom(); expect(mnemonic).toBeInstanceOf(Mnemonic); }); it('generateRandom should return instance of Mnemonic with passphrase', () => { const mnemonic = Mnemonic.generateRandom('GeneralProtocols!'); expect(mnemonic).toBeInstanceOf(Mnemonic); expect(mnemonic.getPassphrase()).toBe('GeneralProtocols!'); }); }); describe('Validations', () => { it('isValid should succeed if valid mnemonic', () => { const isValid = Mnemonic.isValid( 'goose just fan kidney barrel album wave crowd movie flame cousin exhaust', ); expect(isValid).toBe(true); }); it('isValid should fail if invalid mnemonic', () => { // 11 words. const isValid = Mnemonic.isValid( 'goose just fan kidney barrel album wave crowd movie flame cousin', ); expect(isValid).toBe(false); }); it('isValidWord should succeed if word is in word list', () => { const isValid = Mnemonic.isValidWord('goose'); expect(isValid).toBe(true); }); it('isValidWord should fail if word is not in word list', () => { const isValid = Mnemonic.isValidWord('fnord'); expect(isValid).toBe(false); }); }); describe('Fail cases', () => { it('fromString should fail if invalid number of words', () => { expect(() => { // 11 words. Mnemonic.fromString( 'goose just fan kidney barrel album wave crowd movie flame cousin', ); }).toThrow(); }); it('generateRandom should fail if invalid wordList contains invalid number of words', () => { expect(() => { // 11 words. Mnemonic.generateRandom('GeneralProtocols!', ['one', 'two', 'three']); }).toThrow(); }); it('fromWords should fail if invalid number of words', () => { expect(() => { // 11 words. Mnemonic.fromWords([ 'goose', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', ]); }).toThrow(); }); it('fromString should fail if word not in dictionary', () => { expect(() => { // Invalid word "fnord". Mnemonic.fromString( 'fnord just fan kidney barrel album wave crowd movie flame cousin exhaust', ); }).toThrow(); }); it('fromWord should fail if word not in dictionary', () => { expect(() => { // Invalid word "fnord". Mnemonic.fromWords([ 'fnord', 'just', 'fan', 'kidney', 'barrel', 'album', 'wave', 'crowd', 'movie', 'flame', 'cousin', 'exhaust', ]); }).toThrow(); }); });