From d871dfc5e123af23112c584b43806bdab6e3b3f8 Mon Sep 17 00:00:00 2001 From: Alejo Acosta Date: Tue, 5 Nov 2024 13:58:12 -0300 Subject: [PATCH] add unit tests for qi wallet importPrivateKey() --- .../qihdwallet-import-privkey.unit.test.ts | 151 ++++++++++++++++++ testcases/qi-wallet-import-privkey.json.gz | Bin 0 -> 425 bytes 2 files changed, 151 insertions(+) create mode 100644 src/_tests/unit/qihdwallet-import-privkey.unit.test.ts create mode 100644 testcases/qi-wallet-import-privkey.json.gz diff --git a/src/_tests/unit/qihdwallet-import-privkey.unit.test.ts b/src/_tests/unit/qihdwallet-import-privkey.unit.test.ts new file mode 100644 index 00000000..725b0c58 --- /dev/null +++ b/src/_tests/unit/qihdwallet-import-privkey.unit.test.ts @@ -0,0 +1,151 @@ +import assert from 'assert'; +import { loadTests } from '../utils.js'; +import { QiHDWallet, Mnemonic, Zone } from '../../index.js'; + +interface TestCaseImportPrivKey { + shouldSucceed: boolean; + privateKey: string; + error?: string; + pubKey?: string; + address?: string; + zone?: string; +} + +describe('QiHDWallet Import Private Key', function () { + const tests = loadTests('qi-wallet-import-privkey'); + + let wallet: QiHDWallet; + + beforeEach(function () { + const mnemonic = Mnemonic.fromPhrase('test test test test test test test test test test test junk'); + wallet = QiHDWallet.fromMnemonic(mnemonic); + }); + + for (const test of tests) { + if (test.shouldSucceed) { + it(`should successfully import private key ${test.privateKey}`, async function () { + const addressInfo = await wallet.importPrivateKey(test.privateKey); + + assert.strictEqual( + addressInfo.pubKey, + test.pubKey, + `Public key mismatch, expected: ${test.pubKey}, got: ${addressInfo.pubKey}`, + ); + + assert.strictEqual( + addressInfo.address, + test.address, + `Address mismatch, expected: ${test.address}, got: ${addressInfo.address}`, + ); + + assert.strictEqual( + addressInfo.zone, + test.zone, + `Zone mismatch, expected: ${test.zone}, got: ${addressInfo.zone}`, + ); + + assert.strictEqual( + addressInfo.derivationPath, + test.privateKey, + 'Private key should be stored in derivationPath', + ); + }); + } else { + it(`should fail to import invalid private key ${test.privateKey}`, async function () { + await assert.rejects( + async () => { + await wallet.importPrivateKey(test.privateKey); + }, + (error: Error) => { + assert.ok( + error.message.includes(test.error!), + `Expected error message to include "${test.error}", got "${error.message}"`, + ); + return true; + }, + ); + }); + } + } + + it('should prevent duplicate imports of the same private key', async function () { + const validPrivateKey = tests.find((t) => t.shouldSucceed)!.privateKey; + + // First import should succeed + await wallet.importPrivateKey(validPrivateKey); + + // Second import should fail + await assert.rejects( + async () => { + await wallet.importPrivateKey(validPrivateKey); + }, + (error: Error) => { + assert.ok( + error.message.includes('already exists in wallet'), + 'Expected error message to indicate duplicate address', + ); + return true; + }, + ); + }); + + it('should return all imported addresses when no zone specified', async function () { + const validTests = tests.filter((t) => t.shouldSucceed); + for (const test of validTests) { + await wallet.importPrivateKey(test.privateKey); + } + + const importedAddresses = wallet.getImportedAddresses(); + + assert.strictEqual(importedAddresses.length, validTests.length, 'Should return all imported addresses'); + + for (let i = 0; i < validTests.length; i++) { + assert.strictEqual( + importedAddresses[i].address, + validTests[i].address, + 'Imported address should match test data', + ); + } + }); + + it('should return only addresses for specified zone', async function () { + const validTests = tests.filter((t) => t.shouldSucceed); + for (const test of validTests) { + await wallet.importPrivateKey(test.privateKey); + } + + const testZone = validTests[0].zone; + const zoneAddresses = wallet.getImportedAddresses(testZone as Zone); + + const expectedAddresses = validTests.filter((t) => t.zone === testZone); + + assert.strictEqual( + zoneAddresses.length, + expectedAddresses.length, + `Should return only addresses for zone ${testZone}`, + ); + + for (let i = 0; i < expectedAddresses.length; i++) { + assert.strictEqual( + zoneAddresses[i].address, + expectedAddresses[i].address, + 'Zone-filtered address should match test data', + ); + } + }); + + it('should return empty array when no addresses imported', function () { + const addresses = wallet.getImportedAddresses(); + assert.deepStrictEqual(addresses, [], 'Should return empty array when no addresses imported'); + }); + + it('should return empty array when no addresses in specified zone', async function () { + const validTest = tests.find((t) => t.shouldSucceed)!; + await wallet.importPrivateKey(validTest.privateKey); + + const differentZone = '0x22'; + const addresses = wallet.getImportedAddresses(differentZone as Zone); + + assert.deepStrictEqual(addresses, [], 'Should return empty array when no addresses in specified zone'); + }); +}); diff --git a/testcases/qi-wallet-import-privkey.json.gz b/testcases/qi-wallet-import-privkey.json.gz new file mode 100644 index 0000000000000000000000000000000000000000..4de7f9014eda2b81961379548aecdc7caa02e1d9 GIT binary patch literal 425 zcmV;a0apGWiwFpc8!2Z11953BcVTR7WppiRZE$aLbS-dlX?AO6c`j;mZ*Bm!l0T2s zKn%s}&8HZpzGD7)JVVEIhz3N7D8^%3tXPs&l0C5?zB|e04pAHhLO0dOmY)57&mX(P zkM3~j=g;Hi#2+rLiE#hQEYn5qw#IonKGa3t%5|;w_FI-5P%&b)Ef?hw3P9FeYly}A zTG&~zkkCmhMIBg0;HIFK0%PfKs^7;!c0FtN%K1XSXIO6$0WC`lUNRMh8pKKRUaAXZ zu__}Q>Xu>6tWkV(ppcq2D%8;0 TAhdbtK6SqUZ!aQwb^`zaq9V^| literal 0 HcmV?d00001