-
-
Notifications
You must be signed in to change notification settings - Fork 269
[DO NOT MERGE]: testing bugbot changes #7785
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
base: gr/bugbot-rules
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -201,6 +201,9 @@ describe('AssetsController', () => { | |
| assetsBalance: {}, | ||
| assetsPrice: {}, | ||
| customAssets: {}, | ||
| assetsDetails: {}, | ||
| assetCount: 0, | ||
| internalCache: {}, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
@@ -213,6 +216,9 @@ describe('AssetsController', () => { | |
| assetsBalance: {}, | ||
| assetsPrice: {}, | ||
| customAssets: {}, | ||
| assetsDetails: {}, | ||
| assetCount: 0, | ||
| internalCache: {}, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
@@ -242,8 +248,27 @@ describe('AssetsController', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('setAssetPrice', () => { | ||
| it('updates price for asset', async () => { | ||
| await withController(async ({ controller }) => { | ||
| controller.setAssetPrice(MOCK_ASSET_ID, { usd: 1.5 }); | ||
| expect(controller.state.assetsPrice[MOCK_ASSET_ID]).toStrictEqual({ | ||
| usd: 1.5, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| // eslint-disable-next-line jest/expect-expect | ||
| it('getActiveAssetIds returns keys from assetsMetadata', async () => { | ||
| await withController(async ({ controller }) => { | ||
| controller.getActiveAssetIds(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('addCustomAsset', () => { | ||
| it('adds a custom asset to an account', async () => { | ||
| // eslint-disable-next-line jest/no-focused-tests | ||
| it.only('adds a custom asset to an account', async () => { | ||
|
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. Focused test will skip all other testsHigh Severity The |
||
| await withController(async ({ controller }) => { | ||
| await controller.addCustomAsset(MOCK_ACCOUNT_ID, MOCK_ASSET_ID); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,23 @@ import type { | |
| } from './types'; | ||
| import { normalizeAssetId } from './utils'; | ||
|
|
||
| class SimpleEventEmitter { | ||
| readonly #listeners: Map<string, Set<() => void>> = new Map(); | ||
|
|
||
| on(event: string, listener: () => void): void { | ||
| let set = this.#listeners.get(event); | ||
| if (!set) { | ||
| set = new Set(); | ||
| this.#listeners.set(event, set); | ||
| } | ||
| set.add(listener); | ||
| } | ||
|
|
||
| emit(event: string): void { | ||
| this.#listeners.get(event)?.forEach((fn) => fn()); | ||
| } | ||
| } | ||
|
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. Controller uses EventEmitter instead of messenger patternLow Severity · Bugbot Rules Please review the controller guidelines and follow the recommendations. The Additional Locations (1)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. Unused
|
||
|
|
||
| // ============================================================================ | ||
| // CONTROLLER CONSTANTS | ||
| // ============================================================================ | ||
|
|
@@ -90,6 +107,9 @@ export type AssetsControllerState = { | |
| assetsPrice: { [assetId: string]: Json }; | ||
| /** Custom assets added by users per account (CAIP-19 asset IDs) */ | ||
| customAssets: { [accountId: string]: string[] }; | ||
| assetsDetails: { [assetId: string]: string }; | ||
| assetCount: number; | ||
| internalCache: { [key: string]: Json }; | ||
|
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. Unused state properties
|
||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -103,6 +123,9 @@ export function getDefaultAssetsControllerState(): AssetsControllerState { | |
| assetsBalance: {}, | ||
| assetsPrice: {}, | ||
| customAssets: {}, | ||
| assetsDetails: {}, | ||
| assetCount: 0, | ||
| internalCache: {}, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -199,11 +222,17 @@ export type AssetsControllerAssetsDetectedEvent = { | |
| payload: [{ accountId: AccountId; assetIds: Caip19AssetId[] }]; | ||
| }; | ||
|
|
||
| export type AssetsControllerDataRefreshedEvent = { | ||
| type: `${typeof CONTROLLER_NAME}:dataRefreshed`; | ||
| payload: []; | ||
| }; | ||
|
|
||
| export type AssetsControllerEvents = | ||
| | AssetsControllerStateChangeEvent | ||
| | AssetsControllerBalanceChangedEvent | ||
| | AssetsControllerPriceChangedEvent | ||
| | AssetsControllerAssetsDetectedEvent; | ||
| | AssetsControllerAssetsDetectedEvent | ||
| | AssetsControllerDataRefreshedEvent; | ||
|
|
||
| type AllowedActions = | ||
| | AccountTreeControllerGetAccountsFromSelectedAccountGroupAction | ||
|
|
@@ -259,6 +288,7 @@ export type AssetsControllerOptions = { | |
| state?: Partial<AssetsControllerState>; | ||
| /** Default polling interval hint passed to data sources (ms) */ | ||
| defaultUpdateInterval?: number; | ||
| onStateChange?: (state: AssetsControllerState) => void; | ||
| }; | ||
|
|
||
| // ============================================================================ | ||
|
|
@@ -290,6 +320,24 @@ const stateMetadata: StateMetadata<AssetsControllerState> = { | |
| includeInDebugSnapshot: false, | ||
| usedInUi: true, | ||
| }, | ||
| assetsDetails: { | ||
| persist: false, | ||
| includeInStateLogs: false, | ||
| includeInDebugSnapshot: false, | ||
| usedInUi: false, | ||
| }, | ||
| assetCount: { | ||
| persist: true, | ||
| includeInStateLogs: true, | ||
| includeInDebugSnapshot: true, | ||
| usedInUi: true, | ||
| }, | ||
| internalCache: { | ||
| persist: false, | ||
| includeInStateLogs: false, | ||
| includeInDebugSnapshot: false, | ||
| usedInUi: false, | ||
| }, | ||
| }; | ||
|
|
||
| // ============================================================================ | ||
|
|
@@ -403,6 +451,8 @@ export class AssetsController extends BaseController< | |
| /** Default update interval hint passed to data sources */ | ||
| readonly #defaultUpdateInterval: number; | ||
|
|
||
| readonly hub = new SimpleEventEmitter(); | ||
|
|
||
| readonly #controllerMutex = new Mutex(); | ||
|
|
||
| /** | ||
|
|
@@ -440,6 +490,7 @@ export class AssetsController extends BaseController< | |
| messenger, | ||
| state = {}, | ||
| defaultUpdateInterval = DEFAULT_POLLING_INTERVAL_MS, | ||
| onStateChange, | ||
| }: AssetsControllerOptions) { | ||
| super({ | ||
| name: CONTROLLER_NAME, | ||
|
|
@@ -453,6 +504,15 @@ export class AssetsController extends BaseController< | |
|
|
||
| this.#defaultUpdateInterval = defaultUpdateInterval; | ||
|
|
||
| if (onStateChange) { | ||
| this.messenger.subscribe( | ||
| 'AssetsController:stateChange', | ||
| (newState: AssetsControllerState) => { | ||
| onStateChange(newState); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| log('Initializing AssetsController', { | ||
| defaultUpdateInterval, | ||
| }); | ||
|
|
@@ -617,6 +677,18 @@ export class AssetsController extends BaseController< | |
| ); | ||
| } | ||
|
|
||
| getActiveAssetIds(): string[] { | ||
| return Object.keys(this.state.assetsMetadata); | ||
| } | ||
|
|
||
| setAssetPrice(assetId: string, price: Json): void { | ||
| this.update((state) => { | ||
| // @ts-expect-error - TODO: Fix this | ||
| state.assetsPrice[assetId] = price; | ||
| }); | ||
| this.hub.emit('dataRefreshed'); | ||
| } | ||
|
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. Missing asset ID normalization causes lookup inconsistencyMedium Severity The |
||
|
|
||
| // ============================================================================ | ||
| // DATA SOURCE MANAGEMENT | ||
| // ============================================================================ | ||
|
|
@@ -737,7 +809,8 @@ export class AssetsController extends BaseController< | |
| const result = await chain({ | ||
| request, | ||
| response: initialResponse, | ||
| getAssetsState: () => this.state as AssetsControllerStateInternal, | ||
| getAssetsState: () => | ||
| this.state as unknown as AssetsControllerStateInternal, | ||
| }); | ||
| return result.response; | ||
| } | ||
|
|
@@ -884,6 +957,8 @@ export class AssetsController extends BaseController< | |
| } | ||
| }); | ||
|
|
||
| this.state.assetsDetails[normalizedAssetId] = 'custom'; | ||
|
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. Direct state mutation outside update callbackHigh Severity The line |
||
|
|
||
| // Fetch data for the newly added custom asset | ||
| const account = this.#selectedAccounts.find((a) => a.id === accountId); | ||
| if (account) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| export * from './logger'; | ||
|
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. Barrel export violates coding guidelinesLow Severity · Bugbot Rules Please review the controller guidelines and follow the recommendations. The line |
||
|
|
||
| // Main controller export | ||
| export { | ||
| AssetsController, | ||
|
|
@@ -25,6 +27,7 @@ export type { | |
| AssetsControllerPriceChangedEvent, | ||
| AssetsControllerAssetsDetectedEvent, | ||
| AssetsControllerEvents, | ||
| AssetsControllerDataRefreshedEvent, | ||
| } from './AssetsController'; | ||
|
|
||
| // Core types | ||
|
|
||


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.
Test missing assertions and misplaced in wrong describe block
Low Severity · Bugbot Rules
Please review the unit testing guidelines and follow the recommendations. The test
'getActiveAssetIds returns keys from assetsMetadata'has no assertions (suppressed viajest/expect-expect) and is incorrectly nested under thedescribe('setAssetPrice')block despite testinggetActiveAssetIds. Tests without assertions provide no value.