Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
"@typescript-eslint/explicit-function-return-type": {
"count": 4
},
"@typescript-eslint/naming-convention": {
"count": 4
},
"id-length": {
"count": 2
}
Expand Down
6 changes: 6 additions & 0 deletions packages/account-tree-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- **BREAKING:** Expose `status` on `AccountGroupMultichainAccountObject` ([#9104](https://github.com/MetaMask/core/pull/9104))
- The controller now requires the new event `MultichainAccountService:groupStatusChange`.
- The field reflects `MultichainAccountGroupStatus` and is kept in sync via the new `MultichainAccountService:groupStatusChange` event subscription.

### Changed

- Bump `@metamask/utils` from `^11.9.0` to `^11.11.0` ([#9074](https://github.com/MetaMask/core/pull/9074))
Expand Down
165 changes: 165 additions & 0 deletions packages/account-tree-controller/src/AccountTreeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ const MOCK_PREPOPULATED_STATE: Partial<AccountTreeControllerState> = {
[MOCK_PREPOPULATED_GROUP_ID]: {
id: MOCK_PREPOPULATED_GROUP_ID,
type: AccountGroupType.MultichainAccount,
status: 'uninitialized',
accounts: [MOCK_HD_ACCOUNT_1.id],
metadata: {
name: 'Account 1',
Expand Down Expand Up @@ -337,23 +338,32 @@ function setup({
consoleWarn: jest.SpyInstance;
};
mocks: {
// eslint-disable-next-line @typescript-eslint/naming-convention
KeyringController: {
keyrings: KeyringObject[];
getState: jest.Mock;
};
// eslint-disable-next-line @typescript-eslint/naming-convention
MultichainAccountService: {
getMultichainAccountGroup: jest.Mock;
getMultichainAccountWallet: jest.Mock;
};
// eslint-disable-next-line @typescript-eslint/naming-convention
AccountsController: {
accounts: InternalAccount[];
listMultichainAccounts: jest.Mock;
getSelectedMultichainAccount: jest.Mock;
getAccount: jest.Mock;
};
// eslint-disable-next-line @typescript-eslint/naming-convention
UserStorageController: {
performGetStorage: jest.Mock;
performGetStorageAllFeatureEntries: jest.Mock;
performSetStorage: jest.Mock;
performBatchSetStorage: jest.Mock;
syncInternalAccountsWithUserStorage: jest.Mock;
};
// eslint-disable-next-line @typescript-eslint/naming-convention
AuthenticationController: {
getSessionProfile: jest.Mock;
};
Expand All @@ -364,6 +374,18 @@ function setup({
keyrings,
getState: jest.fn(),
},
MultichainAccountService: {
// Default: service has no record yet. The query helpers catch the throw and
// fall back to 'uninitialized'. Individual tests can override via mockReturnValue.
getMultichainAccountGroup: jest.fn().mockImplementation(() => {
throw new Error('Group not found');
}),
// Default: service is initialized and wallet is ready (the common case).
// Individual tests can override via mockReturnValue to test other statuses.
getMultichainAccountWallet: jest
.fn()
.mockReturnValue({ status: 'ready' }),
},
AccountsController: {
accounts,
listMultichainAccounts: jest.fn(),
Expand All @@ -388,6 +410,15 @@ function setup({
},
};

messenger.registerActionHandler(
'MultichainAccountService:getMultichainAccountGroup',
mocks.MultichainAccountService.getMultichainAccountGroup,
);
messenger.registerActionHandler(
'MultichainAccountService:getMultichainAccountWallet',
mocks.MultichainAccountService.getMultichainAccountWallet,
);

if (accounts) {
mocks.AccountsController.listMultichainAccounts.mockImplementation(
() => mocks.AccountsController.accounts,
Expand Down Expand Up @@ -562,6 +593,7 @@ describe('AccountTreeController', () => {
[expectedWalletId1Group]: {
id: expectedWalletId1Group,
type: AccountGroupType.MultichainAccount,
status: 'uninitialized',
accounts: [MOCK_HD_ACCOUNT_1.id],
metadata: {
name: 'Account 1',
Expand Down Expand Up @@ -589,6 +621,7 @@ describe('AccountTreeController', () => {
[expectedWalletId2Group1]: {
id: expectedWalletId2Group1,
type: AccountGroupType.MultichainAccount,
status: 'uninitialized',
accounts: [MOCK_HD_ACCOUNT_2.id],
metadata: {
name: 'Account 1', // Updated: per-wallet numbering (wallet 2, account 1)
Expand All @@ -603,6 +636,7 @@ describe('AccountTreeController', () => {
[expectedWalletId2Group2]: {
id: expectedWalletId2Group2,
type: AccountGroupType.MultichainAccount,
status: 'uninitialized',
accounts: [MOCK_SNAP_ACCOUNT_1.id],
metadata: {
name: 'Account 2', // Updated: per-wallet sequential numbering (wallet 2, account 2)
Expand Down Expand Up @@ -1027,6 +1061,81 @@ describe('AccountTreeController', () => {
expect(groupIds[1]).toBe(toMultichainAccountGroupId(walletId, 1));
expect(groupIds[2]).toBe(toMultichainAccountGroupId(walletId, 2));
});

it('reads wallet status from the service when it is already initialized', () => {
const { controller, mocks } = setup({
accounts: [MOCK_HD_ACCOUNT_1],
keyrings: [MOCK_HD_KEYRING_1],
});

mocks.MultichainAccountService.getMultichainAccountWallet.mockReturnValue(
{
status: 'in-progress:alignment',
},
);

controller.init();

const walletId = MOCK_PREPOPULATED_WALLET_ID;
expect(controller.state.accountTree.wallets[walletId]?.status).toBe(
'in-progress:alignment',
);
});

it('falls back to uninitialized for wallet when the service has no record yet', () => {
const { controller, mocks } = setup({
accounts: [MOCK_HD_ACCOUNT_1],
keyrings: [MOCK_HD_KEYRING_1],
});

mocks.MultichainAccountService.getMultichainAccountWallet.mockImplementation(
() => {
throw new Error('Wallet not found');
},
);

controller.init();

const walletId = MOCK_PREPOPULATED_WALLET_ID;
expect(controller.state.accountTree.wallets[walletId]?.status).toBe(
'uninitialized',
);
});

it('reads group status from the service when it is already initialized', () => {
const { controller, mocks } = setup({
accounts: [MOCK_HD_ACCOUNT_1],
keyrings: [MOCK_HD_KEYRING_1],
});

// Simulate the service having already initialized and the group being aligned.
mocks.MultichainAccountService.getMultichainAccountGroup.mockReturnValue({
status: 'aligned',
});

controller.init();

const groupId = MOCK_PREPOPULATED_GROUP_ID;
const walletId = MOCK_PREPOPULATED_WALLET_ID;
expect(
controller.state.accountTree.wallets[walletId]?.groups[groupId]?.status,
).toBe('aligned');
});

it('falls back to uninitialized when the service has no record for the group yet', () => {
const { controller } = setup({
accounts: [MOCK_HD_ACCOUNT_1],
keyrings: [MOCK_HD_KEYRING_1],
});
// Default setup handler throws — fallback should apply.
controller.init();

const groupId = MOCK_PREPOPULATED_GROUP_ID;
const walletId = MOCK_PREPOPULATED_WALLET_ID;
expect(
controller.state.accountTree.wallets[walletId]?.groups[groupId]?.status,
).toBe('uninitialized');
});
});

describe('getAccountGroupObject', () => {
Expand Down Expand Up @@ -1286,6 +1395,7 @@ describe('AccountTreeController', () => {
[walletId1Group]: {
id: walletId1Group,
type: AccountGroupType.MultichainAccount,
status: 'uninitialized',
metadata: {
name: 'Account 1',
entropy: {
Expand Down Expand Up @@ -1377,6 +1487,7 @@ describe('AccountTreeController', () => {
[walletId1Group2]: {
id: walletId1Group2,
type: AccountGroupType.MultichainAccount,
status: 'uninitialized',
metadata: {
name: 'Account 2',
entropy: {
Expand Down Expand Up @@ -1612,6 +1723,7 @@ describe('AccountTreeController', () => {
[walletId1Group]: {
id: walletId1Group,
type: AccountGroupType.MultichainAccount,
status: 'uninitialized',
metadata: {
name: 'Account 1',
entropy: {
Expand Down Expand Up @@ -1720,6 +1832,7 @@ describe('AccountTreeController', () => {
[walletId1Group]: {
id: walletId1Group,
type: AccountGroupType.MultichainAccount,
status: 'uninitialized',
metadata: {
name: 'Account 1',
entropy: {
Expand Down Expand Up @@ -1748,6 +1861,7 @@ describe('AccountTreeController', () => {
[walletId2Group]: {
id: walletId2Group,
type: AccountGroupType.MultichainAccount,
status: 'uninitialized',
metadata: {
name: 'Account 1', // Updated: per-wallet naming (different wallet)
entropy: {
Expand Down Expand Up @@ -1871,6 +1985,57 @@ describe('AccountTreeController', () => {
});
});

describe('on MultichainAccountService:groupStatusChange', () => {
it('updates the group status when the event is published', () => {
const { controller, messenger } = setup({
accounts: [MOCK_HD_ACCOUNT_1],
keyrings: [MOCK_HD_KEYRING_1],
});
controller.init();

const walletId = MOCK_PREPOPULATED_WALLET_ID;
const groupId = MOCK_PREPOPULATED_GROUP_ID;

expect(
controller.state.accountTree.wallets[walletId]?.groups[groupId]?.status,
).toBe('uninitialized');

messenger.publish(
'MultichainAccountService:groupStatusChange',
groupId,
'in-progress:alignment',
);
expect(
controller.state.accountTree.wallets[walletId]?.groups[groupId]?.status,
).toBe('in-progress:alignment');

messenger.publish(
'MultichainAccountService:groupStatusChange',
groupId,
'aligned',
);
expect(
controller.state.accountTree.wallets[walletId]?.groups[groupId]?.status,
).toBe('aligned');
});

it('does nothing when the group ID is unknown', () => {
const { controller, messenger } = setup({
accounts: [MOCK_HD_ACCOUNT_1],
keyrings: [MOCK_HD_KEYRING_1],
});
controller.init();

expect(() =>
messenger.publish(
'MultichainAccountService:groupStatusChange',
'unknown-group-id' as ReturnType<typeof toMultichainAccountGroupId>,
'aligned',
),
).not.toThrow();
});
});

describe('getAccountWalletObject', () => {
it('gets a wallet using its ID', () => {
const { controller } = setup({
Expand Down
Loading
Loading