-
-
Notifications
You must be signed in to change notification settings - Fork 6
Adding controller #4
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
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
15e6bf7
Adding controller
jpuri 4717b62
Fix build
jpuri 3b35aa7
fix
jpuri e39ec39
fix
jpuri a81b9a1
fix
jpuri 828f2f9
Fix build
jpuri b66ccd6
Fix build
jpuri 680683a
Update
jpuri 5cb4bb1
Update
jpuri 1fe5187
Update
jpuri 00964f5
Update
jpuri 3ee7640
Fix
jpuri c00b3a0
Update
jpuri f035ab1
Update src/ppom-storage.ts
jpuri 855747f
Remove checksum
jpuri ef6232e
Update src/ppom-controller.ts
jpuri 93269ef
Updates
jpuri cbc83c1
Updates
jpuri 8ebb1ba
Update
jpuri 8133afc
Updates
jpuri c3f71be
Updates
jpuri 1036aef
Updates
jpuri 5878f0b
Updates
jpuri b63fdf7
fix
jpuri 5d0f3ce
Updates
jpuri fd6c640
Updates
jpuri d359a87
fix
jpuri 0eef176
fix
jpuri c96f5ac
fix
jpuri 6004210
fix
jpuri 818c664
fix
jpuri 7305cbd
fix
jpuri 3437bfb
fix
jpuri 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
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
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,3 @@ | ||
| export * from './ppom-controller'; | ||
|
|
||
| export type { StorageBackend, StorageKey } from './ppom-storage'; |
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,281 @@ | ||
| import { ControllerMessenger } from '@metamask/base-controller'; | ||
|
|
||
| import { VERSION_INFO, storageBackendReturningData } from '../test/test-utils'; | ||
| import { PPOM } from './ppom'; | ||
| import { PPOMController, DAY_IN_MILLISECONDS } from './ppom-controller'; | ||
|
|
||
| Object.defineProperty(globalThis, 'fetch', { | ||
| writable: true, | ||
| value: () => undefined, | ||
| }); | ||
|
|
||
| jest.mock('./ppom.ts', () => ({ | ||
| PPOM: class PPOMClass { | ||
| #jsonRpcRequest; | ||
|
|
||
| constructor(jsonRpcRequest: any) { | ||
| this.#jsonRpcRequest = jsonRpcRequest; | ||
| } | ||
|
|
||
| validateJsonRpc = async () => { | ||
| return Promise.resolve(); | ||
| }; | ||
|
|
||
| free = () => undefined; | ||
|
|
||
| testJsonRPCRequest = async () => await this.#jsonRpcRequest(); | ||
| }, | ||
| ppomInit: () => undefined, | ||
| })); | ||
|
|
||
| const PPOM_VERSION_PATH = | ||
| 'https://storage.googleapis.com/ppom-cdn/ppom_version.json'; | ||
|
|
||
| const buildFetchSpy = ( | ||
| versionData: any = { | ||
| status: 200, | ||
| json: () => VERSION_INFO, | ||
| }, | ||
| blobData: any = { | ||
| status: 200, | ||
| arrayBuffer: () => new ArrayBuffer(123), | ||
| }, | ||
| ) => { | ||
| return jest | ||
| .spyOn(globalThis, 'fetch' as any) | ||
| .mockImplementation((url: any) => { | ||
| if (url === PPOM_VERSION_PATH) { | ||
| return versionData; | ||
| } | ||
| return blobData; | ||
| }); | ||
| }; | ||
|
|
||
| const buildPPOMController = (args?: any) => { | ||
| const controllerMessenger = new ControllerMessenger(); | ||
| const ppomController = new PPOMController({ | ||
| storageBackend: storageBackendReturningData, | ||
| provider: () => undefined, | ||
| chainId: '0x1', | ||
| onNetworkChange: () => undefined, | ||
| messenger: controllerMessenger.getRestricted({ | ||
| name: 'PPOMController', | ||
| }), | ||
| ...args, | ||
| }); | ||
| return ppomController; | ||
| }; | ||
|
|
||
| describe('PPOMController', () => { | ||
| describe('usePPOM', () => { | ||
| it('should provide instance of ppom to the passed ballback', async () => { | ||
| const ppomController = buildPPOMController(); | ||
| buildFetchSpy(); | ||
|
|
||
| await ppomController.usePPOM(async (ppom: PPOM) => { | ||
| expect(ppom).toBeDefined(); | ||
| return Promise.resolve(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return the value returned by callback', async () => { | ||
| const ppomController = buildPPOMController(); | ||
| buildFetchSpy(); | ||
|
|
||
| const result = await ppomController.usePPOM(async (ppom: PPOM) => { | ||
| expect(ppom).toBeDefined(); | ||
| return Promise.resolve('DUMMY_VALUE'); | ||
| }); | ||
| expect(result).toBe('DUMMY_VALUE'); | ||
| }); | ||
|
|
||
| it('should refresh data if network is changed', async () => { | ||
| const spy = buildFetchSpy({ | ||
| status: 200, | ||
| json: () => [ | ||
| ...VERSION_INFO, | ||
| { | ||
| name: 'data', | ||
| chainId: '0x2', | ||
| version: '1.0.3', | ||
| checksum: | ||
| '409a7f83ac6b31dc8c77e3ec18038f209bd2f545e0f4177c2e2381aa4e067b49', | ||
| filePath: 'data', | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| let callBack: any; | ||
| const ppomController = buildPPOMController({ | ||
| onNetworkChange: (func: any) => { | ||
| callBack = func; | ||
| }, | ||
| }); | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(3); | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(3); | ||
|
|
||
| callBack('0x2'); | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(4); | ||
| callBack('0x1'); | ||
| }); | ||
|
|
||
| it('should pass instance of provider to ppom to enable it to send JSON RPC request on it', async () => { | ||
| buildFetchSpy(); | ||
|
|
||
| const ppomController = buildPPOMController({ | ||
| provider: { | ||
| sendAsync: (_arg1: any, arg2: any) => { | ||
| arg2(undefined, 'DUMMY_VALUE'); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| await ppomController.usePPOM(async (ppom: PPOM) => { | ||
| const result = await (ppom as any).testJsonRPCRequest({}); | ||
| expect(result).toBe('DUMMY_VALUE'); | ||
| }); | ||
| }); | ||
|
|
||
| it('should propogate to ppom if JSON RPC request on provider fails', async () => { | ||
| const ppomController = buildPPOMController({ | ||
| provider: { | ||
| sendAsync: (_arg1: any, arg2: any) => { | ||
| arg2('DUMMY_ERROR'); | ||
| }, | ||
| }, | ||
| }); | ||
| buildFetchSpy(); | ||
| await ppomController.usePPOM(async (ppom: PPOM) => { | ||
| (ppom as any).testJsonRPCRequest({}).catch((exp: any) => { | ||
| // eslint-disable-next-line jest/no-conditional-expect | ||
| expect(exp).toBe('DUMMY_ERROR'); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updatePPOM', () => { | ||
| it('should not fetch file if chainId of the file is different from current chainId', async () => { | ||
| const spy = buildFetchSpy({ | ||
| status: 200, | ||
| json: () => [ | ||
| ...VERSION_INFO, | ||
| { | ||
| name: 'data', | ||
| chainId: '0x2', | ||
| version: '1.0.3', | ||
| checksum: | ||
| '409a7f83ac6b31dc8c77e3ec18038f209bd2f545e0f4177c2e2381aa4e067b49', | ||
| filePath: 'data', | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const ppomController = buildPPOMController(); | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
|
|
||
| expect(spy).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it('should not fetch file if it already exists', async () => { | ||
| const spy = buildFetchSpy(); | ||
|
|
||
| const ppomController = buildPPOMController(); | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(3); | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it('should throw error if fetch for version info return 500', async () => { | ||
| buildFetchSpy({ | ||
| status: 500, | ||
| }); | ||
|
|
||
| const ppomController = buildPPOMController(); | ||
| await expect(async () => { | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| }).rejects.toThrow('Failed to fetch version info'); | ||
| }); | ||
|
|
||
| it('should throw error if fetch for blob return 500', async () => { | ||
| buildFetchSpy(undefined, { | ||
| status: 500, | ||
| }); | ||
|
|
||
| const ppomController = buildPPOMController(); | ||
| await expect(async () => { | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| }).rejects.toThrow( | ||
| 'Failed to fetch file with url https://storage.googleapis.com/ppom-cdn/blob', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('setRefreshInterval', () => { | ||
| it('should update refresh interval', async () => { | ||
| const ppomController = buildPPOMController(); | ||
| const spy = buildFetchSpy(); | ||
|
|
||
| // controller fetches new data files is difference from last updated time | ||
| // is greater than refresh interval. | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(3); | ||
| ppomController.setRefreshInterval(0); | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(4); | ||
|
|
||
| ppomController.setRefreshInterval(1000); | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(4); | ||
| ppomController.setRefreshInterval(DAY_IN_MILLISECONDS); | ||
| }); | ||
| }); | ||
|
|
||
| describe('clear', () => { | ||
| it('should clear controller state', async () => { | ||
| const ppomController = buildPPOMController(); | ||
| const spy = buildFetchSpy(); | ||
|
|
||
| // controller fetches new data files when state is cleared | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(3); | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(3); | ||
| ppomController.clear(); | ||
| await ppomController.usePPOM(async () => { | ||
| return Promise.resolve(); | ||
| }); | ||
| expect(spy).toHaveBeenCalledTimes(6); | ||
| }); | ||
| }); | ||
| }); | ||
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.