-
-
Notifications
You must be signed in to change notification settings - Fork 277
fix(multichain-account-service): prevent spurious alignment logs + smaller map state updates #8136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,121 @@ | ||||||
| import type { Bip44Account } from '@metamask/account-api'; | ||||||
| import type { | ||||||
| CreateAccountOptions, | ||||||
| KeyringAccount, | ||||||
| KeyringCapabilities, | ||||||
| } from '@metamask/keyring-api'; | ||||||
| import type { InternalAccount } from '@metamask/keyring-internal-api'; | ||||||
|
|
||||||
| import { AccountProviderWrapper } from './AccountProviderWrapper'; | ||||||
| import { BaseBip44AccountProvider } from './BaseBip44AccountProvider'; | ||||||
| import { | ||||||
| getMultichainAccountServiceMessenger, | ||||||
| getRootMessenger, | ||||||
| MOCK_HD_KEYRING_1, | ||||||
| MOCK_SOL_ACCOUNT_1, | ||||||
| } from '../tests'; | ||||||
| import type { MultichainAccountServiceMessenger } from '../types'; | ||||||
|
|
||||||
| class MockInnerProvider extends BaseBip44AccountProvider { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| readonly capabilities: KeyringCapabilities = { | ||||||
| scopes: [], | ||||||
| bip44: { deriveIndex: true }, | ||||||
| }; | ||||||
|
|
||||||
| getName(): string { | ||||||
| return 'MockInnerProvider'; | ||||||
| } | ||||||
|
|
||||||
| isAccountCompatible(_account: Bip44Account<KeyringAccount>): boolean { | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| async createAccounts( | ||||||
| _options: CreateAccountOptions, | ||||||
| ): Promise<Bip44Account<KeyringAccount>[]> { | ||||||
| return []; | ||||||
| } | ||||||
|
|
||||||
| async discoverAccounts(_options: { | ||||||
| entropySource: string; | ||||||
| groupIndex: number; | ||||||
| }): Promise<Bip44Account<KeyringAccount>[]> { | ||||||
| return []; | ||||||
| } | ||||||
|
|
||||||
| resyncAccounts(_accounts: Bip44Account<InternalAccount>[]): Promise<void> { | ||||||
| return Promise.resolve(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function setup(): { | ||||||
| wrapper: AccountProviderWrapper; | ||||||
| innerProvider: MockInnerProvider; | ||||||
| } { | ||||||
| const messenger = getRootMessenger(); | ||||||
| const multichainMessenger: MultichainAccountServiceMessenger = | ||||||
| getMultichainAccountServiceMessenger(messenger); | ||||||
|
|
||||||
| const innerProvider = new MockInnerProvider(multichainMessenger); | ||||||
| const wrapper = new AccountProviderWrapper( | ||||||
| multichainMessenger, | ||||||
| innerProvider, | ||||||
| ); | ||||||
|
|
||||||
| return { wrapper, innerProvider }; | ||||||
| } | ||||||
|
|
||||||
| describe('AccountProviderWrapper', () => { | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added some more tests for the wrapper, since we skip the So, with this new file, we can actually get better coverage in a more "unit-tests-like way" for this class. |
||||||
| describe('alignAccounts', () => { | ||||||
| it('returns empty array when disabled', async () => { | ||||||
| const { wrapper } = setup(); | ||||||
|
|
||||||
| wrapper.setEnabled(false); | ||||||
|
|
||||||
| const result = await wrapper.alignAccounts({ | ||||||
| entropySource: MOCK_HD_KEYRING_1.metadata.id, | ||||||
| groupIndex: 0, | ||||||
| }); | ||||||
|
|
||||||
| expect(result).toStrictEqual([]); | ||||||
| }); | ||||||
|
|
||||||
| it('does not delegate to inner provider when disabled', async () => { | ||||||
| const { wrapper, innerProvider } = setup(); | ||||||
| innerProvider.init([MOCK_SOL_ACCOUNT_1.id]); | ||||||
|
|
||||||
| const alignSpy = jest | ||||||
| .spyOn(innerProvider, 'alignAccounts') | ||||||
| .mockResolvedValue([MOCK_SOL_ACCOUNT_1.id]); | ||||||
|
|
||||||
| wrapper.setEnabled(false); | ||||||
|
|
||||||
| await wrapper.alignAccounts({ | ||||||
| entropySource: MOCK_HD_KEYRING_1.metadata.id, | ||||||
| groupIndex: 0, | ||||||
| }); | ||||||
|
|
||||||
| expect(alignSpy).not.toHaveBeenCalled(); | ||||||
| }); | ||||||
|
|
||||||
| it('delegates to inner provider when enabled', async () => { | ||||||
| const { wrapper, innerProvider } = setup(); | ||||||
| innerProvider.init([MOCK_SOL_ACCOUNT_1.id]); | ||||||
|
|
||||||
| const alignSpy = jest | ||||||
| .spyOn(innerProvider, 'alignAccounts') | ||||||
| .mockResolvedValue([MOCK_SOL_ACCOUNT_1.id]); | ||||||
|
|
||||||
| const result = await wrapper.alignAccounts({ | ||||||
| entropySource: MOCK_HD_KEYRING_1.metadata.id, | ||||||
| groupIndex: 0, | ||||||
| }); | ||||||
|
|
||||||
| expect(alignSpy).toHaveBeenCalledWith({ | ||||||
| entropySource: MOCK_HD_KEYRING_1.metadata.id, | ||||||
| groupIndex: 0, | ||||||
| }); | ||||||
| expect(result).toStrictEqual([MOCK_SOL_ACCOUNT_1.id]); | ||||||
| }); | ||||||
| }); | ||||||
| }); | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of the mock providers are not covered/used anymore by our tests.
The
src/tests/providers.tsfile is pretty messy right now, and instead of removing/moving things around, I prefer to not have it in the coverage at all for the time being.I do plan to make a small refactor around that to avoid the
.prototypehacks we're using there.