-
-
Notifications
You must be signed in to change notification settings - Fork 308
refactor(snap-account-service): add SnapAccountCache #10290
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
Draft
ccharly
wants to merge
2
commits into
gar/fix/snap-account-event-publish
Choose a base branch
from
cc/refactor/snap-account-cache
base: gar/fix/snap-account-event-publish
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+432
−153
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
305 changes: 305 additions & 0 deletions
305
packages/snap-account-service/src/SnapAccountCache.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,305 @@ | ||
| import type { | ||
| AccountsControllerAccountsAddedEvent, | ||
| AccountsControllerAccountsRemovedEvent, | ||
| AccountsControllerGetStateAction, | ||
| AccountsControllerState, | ||
| } from '@metamask/accounts-controller'; | ||
| import { Messenger, MOCK_ANY_NAMESPACE } from '@metamask/messenger'; | ||
| import type { | ||
| MockAnyNamespace, | ||
| MessengerActions, | ||
| MessengerEvents, | ||
| } from '@metamask/messenger'; | ||
| import type { SnapId } from '@metamask/snaps-sdk'; | ||
|
|
||
| import { SnapAccountCache } from './SnapAccountCache.js'; | ||
| import type { SnapAccountServiceMessenger } from './SnapAccountService.js'; | ||
|
|
||
| type RootMessenger = Messenger< | ||
| MockAnyNamespace, | ||
| MessengerActions<SnapAccountServiceMessenger>, | ||
| MessengerEvents<SnapAccountServiceMessenger> | ||
| >; | ||
|
|
||
| /** | ||
| * Constructs the root messenger for the cache under test. | ||
| * | ||
| * @returns The root messenger. | ||
| */ | ||
| function getRootMessenger(): RootMessenger { | ||
| return new Messenger({ namespace: MOCK_ANY_NAMESPACE }); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs the messenger for the cache under test, and delegates all | ||
| * required external actions and events from the root messenger to it. | ||
| * | ||
| * @param rootMessenger - The root messenger. | ||
| * @returns The cache messenger. | ||
| */ | ||
| function getMessenger( | ||
| rootMessenger: RootMessenger, | ||
| ): SnapAccountServiceMessenger { | ||
| const messenger = new Messenger({ | ||
| namespace: 'SnapAccountService', | ||
| parent: rootMessenger, | ||
| }); | ||
| rootMessenger.delegate({ | ||
| messenger, | ||
| actions: ['AccountsController:getState'], | ||
| events: [ | ||
| 'AccountsController:accountsAdded', | ||
| 'AccountsController:accountsRemoved', | ||
| ], | ||
| }); | ||
| return messenger; | ||
| } | ||
|
|
||
| /** | ||
| * Builds a minimal `AccountsControllerState` whose `internalAccounts.accounts` | ||
| * maps each given account ID to an account owned by the given Snap ID. | ||
| * | ||
| * @param accounts - The accounts to include. | ||
| * @returns A minimal `AccountsControllerState`. | ||
| */ | ||
| function buildAccountsState( | ||
| accounts: { id: string; snapId?: string }[], | ||
| ): AccountsControllerState { | ||
| const accountsRecord = Object.fromEntries( | ||
| accounts.map(({ id, snapId }) => [ | ||
| id, | ||
| { id, metadata: snapId ? { snap: { id: snapId } } : {} }, | ||
| ]), | ||
| ); | ||
| return { | ||
| internalAccounts: { accounts: accountsRecord }, | ||
| } as unknown as AccountsControllerState; | ||
| } | ||
|
|
||
| /** | ||
| * Publishes an `AccountsController:accountsAdded` event. | ||
| * | ||
| * @param rootMessenger - The root messenger. | ||
| * @param accounts - The accounts that were added. | ||
| */ | ||
| function publishAccountsAdded( | ||
| rootMessenger: RootMessenger, | ||
| accounts: { id: string; snapId?: string }[], | ||
| ): void { | ||
| rootMessenger.publish( | ||
| 'AccountsController:accountsAdded', | ||
| accounts.map(({ id, snapId }) => ({ | ||
| id, | ||
| metadata: snapId ? { snap: { id: snapId } } : {}, | ||
| })) as AccountsControllerAccountsAddedEvent['payload'][0], | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Publishes an `AccountsController:accountsRemoved` event. | ||
| * | ||
| * @param rootMessenger - The root messenger. | ||
| * @param accountIds - The IDs of the accounts that were removed. | ||
| */ | ||
| function publishAccountsRemoved( | ||
| rootMessenger: RootMessenger, | ||
| accountIds: string[], | ||
| ): void { | ||
| rootMessenger.publish( | ||
| 'AccountsController:accountsRemoved', | ||
| accountIds as AccountsControllerAccountsRemovedEvent['payload'][0], | ||
|
Member
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. Flagged by sonar: |
||
| ); | ||
| } | ||
|
|
||
| type Mocks = { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| AccountsController: { | ||
| getState: jest.MockedFunction<() => AccountsControllerState>; | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Constructs the cache under test with sensible defaults. | ||
| * | ||
| * @param args - The arguments to this function. | ||
| * @param args.accounts - Accounts to seed into the `AccountsController:getState` mock. | ||
| * @returns The new cache, root messenger, cache messenger, and mocks. | ||
| */ | ||
| function setup({ | ||
| accounts = [], | ||
| }: { | ||
| accounts?: { id: string; snapId?: string }[]; | ||
| } = {}): { | ||
| cache: SnapAccountCache; | ||
| rootMessenger: RootMessenger; | ||
| mocks: Mocks; | ||
| } { | ||
| const rootMessenger = getRootMessenger(); | ||
| const messenger = getMessenger(rootMessenger); | ||
|
|
||
| const mocks: Mocks = { | ||
| AccountsController: { | ||
| getState: jest.fn().mockReturnValue(buildAccountsState(accounts)), | ||
| }, | ||
| }; | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'AccountsController:getState', | ||
| mocks.AccountsController.getState as never, | ||
| ); | ||
|
|
||
| const cache = new SnapAccountCache(messenger); | ||
|
|
||
| return { cache, rootMessenger, mocks }; | ||
| } | ||
|
|
||
| const MOCK_SNAP_ID = 'npm:@metamask/mock-snap' as SnapId; | ||
| const MOCK_OTHER_SNAP_ID = 'npm:@metamask/other-snap' as SnapId; | ||
| const MOCK_ACCOUNT_ID = '00000000-0000-0000-0000-000000000001'; | ||
| const MOCK_OTHER_ACCOUNT_ID = '00000000-0000-0000-0000-000000000002'; | ||
| const MOCK_NO_SNAP_ACCOUNT_ID = '00000000-0000-0000-0000-000000000003'; | ||
|
|
||
| describe('SnapAccountCache', () => { | ||
| describe('getSnapId', () => { | ||
| it('returns undefined for an unknown account before the cache is built', () => { | ||
| const { cache } = setup(); | ||
|
|
||
| expect(cache.getSnapId(MOCK_ACCOUNT_ID)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('lazily builds the cache from AccountsController state on first use', () => { | ||
| const { cache, mocks } = setup({ | ||
| accounts: [{ id: MOCK_ACCOUNT_ID, snapId: MOCK_SNAP_ID as string }], | ||
| }); | ||
|
|
||
| expect(mocks.AccountsController.getState).not.toHaveBeenCalled(); | ||
|
|
||
| expect(cache.getSnapId(MOCK_ACCOUNT_ID)).toBe(MOCK_SNAP_ID); | ||
|
|
||
| expect(mocks.AccountsController.getState).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not rebuild the cache on subsequent calls', () => { | ||
| const { cache, mocks } = setup({ | ||
| accounts: [{ id: MOCK_ACCOUNT_ID, snapId: MOCK_SNAP_ID as string }], | ||
| }); | ||
|
|
||
| cache.getSnapId(MOCK_ACCOUNT_ID); | ||
| cache.getSnapId(MOCK_ACCOUNT_ID); | ||
|
|
||
| expect(mocks.AccountsController.getState).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('skips accounts without a snap ID when building the cache', () => { | ||
| const { cache } = setup({ | ||
| accounts: [ | ||
| { id: MOCK_ACCOUNT_ID, snapId: MOCK_SNAP_ID as string }, | ||
| { id: MOCK_NO_SNAP_ACCOUNT_ID }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(cache.getSnapId(MOCK_ACCOUNT_ID)).toBe(MOCK_SNAP_ID); | ||
| expect(cache.getSnapId(MOCK_NO_SNAP_ACCOUNT_ID)).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('invalidate', () => { | ||
| it('causes the next getSnapId call to rebuild from fresh state', () => { | ||
| const { cache, mocks } = setup({ | ||
| accounts: [{ id: MOCK_ACCOUNT_ID, snapId: MOCK_SNAP_ID as string }], | ||
| }); | ||
|
|
||
| expect(cache.getSnapId(MOCK_ACCOUNT_ID)).toBe(MOCK_SNAP_ID); | ||
| expect(mocks.AccountsController.getState).toHaveBeenCalledTimes(1); | ||
|
|
||
| mocks.AccountsController.getState.mockReturnValue( | ||
| buildAccountsState([ | ||
| { id: MOCK_ACCOUNT_ID, snapId: MOCK_OTHER_SNAP_ID as string }, | ||
| ]), | ||
| ); | ||
|
|
||
| cache.invalidate(); | ||
|
|
||
| expect(cache.getSnapId(MOCK_ACCOUNT_ID)).toBe(MOCK_OTHER_SNAP_ID); | ||
| expect(mocks.AccountsController.getState).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe('on AccountsController:accountsAdded', () => { | ||
| it('adds Snap-owned accounts to an initialized cache', () => { | ||
| const { cache, rootMessenger } = setup(); | ||
|
|
||
| cache.getSnapId(MOCK_ACCOUNT_ID); // initialize | ||
|
|
||
| publishAccountsAdded(rootMessenger, [ | ||
| { id: MOCK_ACCOUNT_ID, snapId: MOCK_SNAP_ID as string }, | ||
| ]); | ||
|
|
||
| expect(cache.getSnapId(MOCK_ACCOUNT_ID)).toBe(MOCK_SNAP_ID); | ||
| }); | ||
|
|
||
| it('skips accounts without a snap ID', () => { | ||
| const { cache, rootMessenger } = setup(); | ||
|
|
||
| cache.getSnapId(MOCK_ACCOUNT_ID); // initialize | ||
|
|
||
| publishAccountsAdded(rootMessenger, [{ id: MOCK_NO_SNAP_ACCOUNT_ID }]); | ||
|
|
||
| expect(cache.getSnapId(MOCK_NO_SNAP_ACCOUNT_ID)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('is a no-op when the cache is not initialized', () => { | ||
| const { cache, rootMessenger, mocks } = setup({ | ||
| accounts: [{ id: MOCK_ACCOUNT_ID, snapId: MOCK_SNAP_ID as string }], | ||
| }); | ||
|
|
||
| publishAccountsAdded(rootMessenger, [ | ||
| { id: MOCK_ACCOUNT_ID, snapId: MOCK_OTHER_SNAP_ID as string }, | ||
| ]); | ||
|
|
||
| // The cache is still uninitialized — the next getSnapId call rebuilds | ||
| // from state, which still maps MOCK_ACCOUNT_ID to MOCK_SNAP_ID. | ||
| expect(cache.getSnapId(MOCK_ACCOUNT_ID)).toBe(MOCK_SNAP_ID); | ||
| expect(mocks.AccountsController.getState).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('on AccountsController:accountsRemoved', () => { | ||
| it('removes accounts from an initialized cache', () => { | ||
| const { cache, rootMessenger } = setup({ | ||
| accounts: [{ id: MOCK_ACCOUNT_ID, snapId: MOCK_SNAP_ID as string }], | ||
| }); | ||
|
|
||
| cache.getSnapId(MOCK_ACCOUNT_ID); // initialize | ||
|
|
||
| publishAccountsRemoved(rootMessenger, [MOCK_ACCOUNT_ID]); | ||
|
|
||
| expect(cache.getSnapId(MOCK_ACCOUNT_ID)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('is a no-op for unknown account IDs', () => { | ||
| const { cache, rootMessenger } = setup({ | ||
| accounts: [{ id: MOCK_ACCOUNT_ID, snapId: MOCK_SNAP_ID as string }], | ||
| }); | ||
|
|
||
| cache.getSnapId(MOCK_ACCOUNT_ID); // initialize | ||
|
|
||
| publishAccountsRemoved(rootMessenger, [MOCK_OTHER_ACCOUNT_ID]); | ||
|
|
||
| expect(cache.getSnapId(MOCK_ACCOUNT_ID)).toBe(MOCK_SNAP_ID); | ||
| }); | ||
|
|
||
| it('is a no-op when the cache is not initialized', () => { | ||
| const { cache, rootMessenger, mocks } = setup({ | ||
| accounts: [{ id: MOCK_ACCOUNT_ID, snapId: MOCK_SNAP_ID as string }], | ||
| }); | ||
|
|
||
| publishAccountsRemoved(rootMessenger, [MOCK_ACCOUNT_ID]); | ||
|
|
||
| // The cache is still uninitialized — the next getSnapId call rebuilds | ||
| // from state, which still has MOCK_ACCOUNT_ID mapped to MOCK_SNAP_ID. | ||
| expect(cache.getSnapId(MOCK_ACCOUNT_ID)).toBe(MOCK_SNAP_ID); | ||
| expect(mocks.AccountsController.getState).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
flagged by sonar:
'AccountsControllerGetStateAction' is defined but never used