Merge branch 'codex/fix/account-import-active'

This commit is contained in:
Tommaso Casaburi
2026-05-08 16:52:24 +07:00
2 changed files with 44 additions and 2 deletions
@@ -350,6 +350,40 @@ describe('AccountSettings', () => {
expect(getLocationText()).toBe('/subs/settings#account-settings');
});
it('activates the resolved account name when an imported account name already exists', async () => {
hookMocks.useAccounts.mockReturnValue({
accounts: [
{ id: 'test-id', name: 'Account 1', author: { shortAddress: '0x1...3' } },
{ id: 'existing-imported-id', name: 'Imported', author: { shortAddress: '0x9...9' } },
],
});
fileReaderState.result = JSON.stringify({
account: {
id: 'imported-id',
name: 'Imported',
author: { address: '0xabc' },
},
});
hookMocks.importAccount.mockResolvedValue(undefined);
hookMocks.setActiveAccount.mockResolvedValue(undefined);
render();
await act(async () => {
getButtonByText('import_account_backup').click();
});
const file = new File(['{}'], 'account.json', { type: 'application/json' });
await act(async () => {
createdInput?.onchange?.({ target: { files: [file] } } as unknown as Event);
await Promise.resolve();
});
await flushMicrotasks();
expect(hookMocks.importAccount).toHaveBeenCalledOnce();
expect(hookMocks.setActiveAccount).toHaveBeenCalledWith('Imported 2');
});
it('surfaces import errors without navigating or reloading', async () => {
fileReaderState.result = JSON.stringify({
account: {
@@ -57,6 +57,13 @@ const getSafeAccountBackupFileName = (accountName: string | undefined): string =
return `${safeName}.json`;
};
const getImportedAccountActiveName = (importedAccountName: string | undefined, accounts: Array<{ name?: string }>): string | undefined => {
if (!importedAccountName) {
return undefined;
}
return accounts.some((account) => account?.name === importedAccountName) ? `${importedAccountName} 2` : importedAccountName;
};
// Inner component keyed by account id so state resets when user switches account
const AccountSettingsEditor = ({
account,
@@ -154,14 +161,15 @@ const AccountSettingsEditor = ({
}
const modifiedAccountJson = JSON.stringify(accountData);
const importedAccountActiveName = getImportedAccountActiveName(accountData.account?.name, accounts);
const result = await withErrorHandling(
async () => {
await importAccount(modifiedAccountJson);
if (accountData.account?.author?.address) {
rememberImportedAccountAddress(accountData.account.author.address);
}
if (accountData.account?.name) {
await setActiveAccount(accountData.account.name);
if (importedAccountActiveName) {
await setActiveAccount(importedAccountActiveName);
}
return true;
},