-
-
Notifications
You must be signed in to change notification settings - Fork 276
feat: defer eligibility to allow for onboarding to proceed without le… #8197
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
Merged
+304
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06be61c
feat: defer eligibility to allow for onboarding to proceed without le…
gambinish 55936ac
fix: move the #eligibilityCheckDeferred guard from refreshEligibility…
gambinish 935b604
fix: lint
gambinish d9c9893
fix: PR link in changelog
gambinish 3304a44
chore: Remove changelog before release
gambinish 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
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
258 changes: 258 additions & 0 deletions
258
packages/perps-controller/tests/defer-eligibility.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,258 @@ | ||
| import { Messenger, MOCK_ANY_NAMESPACE } from '@metamask/messenger'; | ||
| import type { | ||
| MockAnyNamespace, | ||
| MessengerActions, | ||
| MessengerEvents, | ||
| } from '@metamask/messenger'; | ||
|
|
||
| import { PerpsController } from '../src/PerpsController'; | ||
| import type { PerpsControllerMessenger } from '../src/PerpsController'; | ||
| import type { PerpsPlatformDependencies } from '../src/types'; | ||
|
|
||
| jest.mock('@nktkas/hyperliquid', () => ({})); | ||
| jest.mock('@myx-trade/sdk', () => ({})); | ||
|
|
||
| type RootMessenger = Messenger< | ||
| MockAnyNamespace, | ||
| MessengerActions<PerpsControllerMessenger>, | ||
| MessengerEvents<PerpsControllerMessenger> | ||
| >; | ||
|
|
||
| const noopLogger = { | ||
| error: jest.fn(), | ||
| warn: jest.fn(), | ||
| }; | ||
|
|
||
| const noopDebugLogger = { | ||
| log: jest.fn(), | ||
| warn: jest.fn(), | ||
| error: jest.fn(), | ||
| }; | ||
|
|
||
| function buildMockInfrastructure(): PerpsPlatformDependencies { | ||
| return { | ||
| logger: noopLogger as unknown as PerpsPlatformDependencies['logger'], | ||
| debugLogger: | ||
| noopDebugLogger as unknown as PerpsPlatformDependencies['debugLogger'], | ||
| metrics: { | ||
| trackEvent: jest.fn(), | ||
| } as unknown as PerpsPlatformDependencies['metrics'], | ||
| performance: { | ||
| startTrace: jest.fn(), | ||
| endTrace: jest.fn(), | ||
| } as unknown as PerpsPlatformDependencies['performance'], | ||
| tracer: { | ||
| trace: jest.fn(), | ||
| } as unknown as PerpsPlatformDependencies['tracer'], | ||
| streamManager: { | ||
| subscribe: jest.fn(), | ||
| unsubscribe: jest.fn(), | ||
| pauseChannel: jest.fn(), | ||
| resumeChannel: jest.fn(), | ||
| } as unknown as PerpsPlatformDependencies['streamManager'], | ||
| featureFlags: { validateVersionGated: jest.fn() }, | ||
| marketDataFormatters: { | ||
| formatPrice: jest.fn(), | ||
| formatSize: jest.fn(), | ||
| } as unknown as PerpsPlatformDependencies['marketDataFormatters'], | ||
| cacheInvalidator: { | ||
| invalidate: jest.fn(), | ||
| } as unknown as PerpsPlatformDependencies['cacheInvalidator'], | ||
| rewards: { getPerpsDiscountForAccount: jest.fn().mockResolvedValue(0) }, | ||
| }; | ||
| } | ||
|
|
||
| const MOCK_REMOTE_FEATURE_FLAG_STATE = { | ||
| remoteFeatureFlags: {}, | ||
| cacheTimestamp: 0, | ||
| }; | ||
|
|
||
| function getRootMessenger(): RootMessenger { | ||
| return new Messenger({ namespace: MOCK_ANY_NAMESPACE }); | ||
| } | ||
|
|
||
| function getControllerMessenger( | ||
| rootMessenger: RootMessenger, | ||
| ): PerpsControllerMessenger { | ||
| const messenger: PerpsControllerMessenger = new Messenger({ | ||
| namespace: 'PerpsController', | ||
| parent: rootMessenger, | ||
| }); | ||
|
|
||
| rootMessenger.delegate({ | ||
| actions: [ | ||
| 'RemoteFeatureFlagController:getState', | ||
| 'NetworkController:getState', | ||
| 'NetworkController:getNetworkClientById', | ||
| 'NetworkController:findNetworkClientIdByChainId', | ||
| 'KeyringController:getState', | ||
| 'KeyringController:signTypedMessage', | ||
| 'TransactionController:addTransaction', | ||
| 'AccountTreeController:getAccountsFromSelectedAccountGroup', | ||
| 'AuthenticationController:getBearerToken', | ||
| ], | ||
| events: [ | ||
| 'RemoteFeatureFlagController:stateChange', | ||
| 'AccountTreeController:selectedAccountGroupChange', | ||
| ], | ||
| messenger, | ||
| }); | ||
|
|
||
| return messenger; | ||
| } | ||
|
|
||
| type BuildControllerOptions = { | ||
| deferEligibilityCheck?: boolean; | ||
| }; | ||
|
|
||
| function buildController({ | ||
| deferEligibilityCheck, | ||
| }: BuildControllerOptions = {}): { | ||
| controller: PerpsController; | ||
| rootMessenger: RootMessenger; | ||
| controllerMessenger: PerpsControllerMessenger; | ||
| } { | ||
| const rootMessenger = getRootMessenger(); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'RemoteFeatureFlagController:getState', | ||
| () => MOCK_REMOTE_FEATURE_FLAG_STATE, | ||
| ); | ||
|
|
||
| const controllerMessenger = getControllerMessenger(rootMessenger); | ||
|
|
||
| const controller = new PerpsController({ | ||
| messenger: controllerMessenger, | ||
| infrastructure: buildMockInfrastructure(), | ||
| deferEligibilityCheck, | ||
| }); | ||
|
|
||
| return { controller, rootMessenger, controllerMessenger }; | ||
| } | ||
|
|
||
| describe('PerpsController - deferEligibilityCheck', () => { | ||
| describe('when deferEligibilityCheck is true', () => { | ||
| it('does not trigger a geolocation fetch during construction', async () => { | ||
| const fetchSpy = jest.spyOn(globalThis, 'fetch').mockResolvedValue({ | ||
| text: () => Promise.resolve('US'), | ||
| } as globalThis.Response); | ||
|
|
||
| buildController({ deferEligibilityCheck: true }); | ||
|
|
||
| // Allow any pending microtasks to flush | ||
| await new Promise((resolve) => process.nextTick(resolve)); | ||
|
|
||
| expect(fetchSpy).not.toHaveBeenCalled(); | ||
| fetchSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('does not trigger a geolocation fetch from subscription events', async () => { | ||
| const fetchSpy = jest.spyOn(globalThis, 'fetch').mockResolvedValue({ | ||
| text: () => Promise.resolve('US'), | ||
| } as globalThis.Response); | ||
|
|
||
| const { rootMessenger } = buildController({ | ||
| deferEligibilityCheck: true, | ||
| }); | ||
|
|
||
| rootMessenger.publish( | ||
| 'RemoteFeatureFlagController:stateChange', | ||
| { ...MOCK_REMOTE_FEATURE_FLAG_STATE }, | ||
| [], | ||
| ); | ||
|
|
||
| await new Promise((resolve) => process.nextTick(resolve)); | ||
|
|
||
| expect(fetchSpy).not.toHaveBeenCalled(); | ||
| fetchSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('keeps isEligible at default (false) during deferral', () => { | ||
| const { controller } = buildController({ | ||
| deferEligibilityCheck: true, | ||
| }); | ||
|
|
||
| expect(controller.state.isEligible).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('startEligibilityMonitoring', () => { | ||
| it('is callable after deferred construction', () => { | ||
| const { controller } = buildController({ | ||
| deferEligibilityCheck: true, | ||
| }); | ||
|
|
||
| expect(() => controller.startEligibilityMonitoring()).not.toThrow(); | ||
| }); | ||
|
|
||
| it('reads current RemoteFeatureFlagController state', () => { | ||
| const rootMessenger = getRootMessenger(); | ||
| const getStateMock = jest | ||
| .fn() | ||
| .mockReturnValue(MOCK_REMOTE_FEATURE_FLAG_STATE); | ||
|
|
||
| rootMessenger.registerActionHandler( | ||
| 'RemoteFeatureFlagController:getState', | ||
| getStateMock, | ||
| ); | ||
|
|
||
| const controllerMessenger = getControllerMessenger(rootMessenger); | ||
| const controller = new PerpsController({ | ||
| messenger: controllerMessenger, | ||
| infrastructure: buildMockInfrastructure(), | ||
| deferEligibilityCheck: true, | ||
| }); | ||
|
|
||
| getStateMock.mockClear(); | ||
|
|
||
| controller.startEligibilityMonitoring(); | ||
|
|
||
| expect(getStateMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('unblocks future subscription-driven eligibility checks', () => { | ||
| const refreshSpy = jest.spyOn( | ||
| PerpsController.prototype as unknown as { | ||
| refreshEligibilityOnFeatureFlagChange: (...args: unknown[]) => void; | ||
| }, | ||
| 'refreshEligibilityOnFeatureFlagChange', | ||
| ); | ||
|
|
||
| const { controller, rootMessenger } = buildController({ | ||
| deferEligibilityCheck: true, | ||
| }); | ||
|
|
||
| const callCountAfterConstruction = refreshSpy.mock.calls.length; | ||
|
|
||
| controller.startEligibilityMonitoring(); | ||
|
|
||
| const callCountAfterStart = refreshSpy.mock.calls.length; | ||
| expect(callCountAfterStart).toBe(callCountAfterConstruction + 1); | ||
|
|
||
| rootMessenger.publish( | ||
| 'RemoteFeatureFlagController:stateChange', | ||
| { ...MOCK_REMOTE_FEATURE_FLAG_STATE }, | ||
| [], | ||
| ); | ||
|
|
||
| expect(refreshSpy.mock.calls).toHaveLength(callCountAfterStart + 1); | ||
| refreshSpy.mockRestore(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when deferEligibilityCheck is false (default)', () => { | ||
| it('triggers eligibility processing during construction', () => { | ||
| const refreshSpy = jest.spyOn( | ||
| PerpsController.prototype as unknown as { | ||
| refreshEligibilityOnFeatureFlagChange: (...args: unknown[]) => void; | ||
| }, | ||
| 'refreshEligibilityOnFeatureFlagChange', | ||
| ); | ||
|
|
||
| buildController({ deferEligibilityCheck: false }); | ||
|
|
||
| expect(refreshSpy).toHaveBeenCalled(); | ||
| refreshSpy.mockRestore(); | ||
| }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.