forked from MetaMask/snaps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase execution environments coverage (MetaMask#482)
* added test for isConstructor, added rpcMethods test file * added tests for rpcMethods, sortParams * updated sortParams test description, added comments, created BaseSnapExecutor test file * updated tests * created mock for BaseSnapExecutor * created executor mock w/ pass through stream * added more tests * updated tests * finished BaseSnapExecutor tests * added lockdown test files * some updates * finished coverage * reverted some changes * lint fix * fixed tests * update relevant test * removed lockdown test * update jest config * Update snapshot * updated test Co-authored-by: Frederik Bolding <[email protected]>
- Loading branch information
1 parent
0f39c01
commit 9163ed3
Showing
11 changed files
with
460 additions
and
181 deletions.
There are no files selected for viewing
This file contains 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 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 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
169 changes: 169 additions & 0 deletions
169
packages/execution-environments/src/common/commands.test.ts
This file contains 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,169 @@ | ||
import { | ||
CommandMethodsMapping, | ||
getCommandMethodImplementations, | ||
} from './commands'; | ||
|
||
describe('getCommandMethodImplementations', () => { | ||
it('will return an object with ping, executeSnap, snapRpc and terminate methods', () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
['ping', 'executeSnap', 'snapRpc', 'terminate'].forEach((method) => { | ||
expect( | ||
typeof methodsObj[method as keyof CommandMethodsMapping], | ||
).toStrictEqual('function'); | ||
}); | ||
expect(Object.keys(methodsObj)).toHaveLength(4); | ||
}); | ||
|
||
it("the ping method will return 'OK'", async () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
expect(await methodsObj.ping()).toStrictEqual('OK'); | ||
}); | ||
|
||
it("the terminate method will 'OK'", async () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
expect(await methodsObj.terminate()).toStrictEqual('OK'); | ||
}); | ||
|
||
it("the executeSnap method will return 'OK'", async () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
expect( | ||
await methodsObj.executeSnap('foo', 'code', ['endowment1', 'endowment2']), | ||
).toStrictEqual('OK'); | ||
}); | ||
|
||
it('the executeSnap method will throw an Error if the snapName is not a string', async () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
await expect(async () => { | ||
await methodsObj.executeSnap(1 as any, 'code', [ | ||
'endowment1', | ||
'endowment2', | ||
]); | ||
}).rejects.toThrow('snapName is not a string'); | ||
}); | ||
|
||
it('the executeSnap method will throw an Error if the sourceCode is not a string', async () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
await expect(async () => { | ||
await methodsObj.executeSnap('foo', 2 as any, [ | ||
'endowment1', | ||
'endowment2', | ||
]); | ||
}).rejects.toThrow('sourceCode is not a string'); | ||
}); | ||
|
||
it('the executeSnap method will throw an Error if it is not passed a proper Endowments object', async () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
await expect(async () => { | ||
await methodsObj.executeSnap('foo', 'code', ['endowment1', 2 as any]); | ||
}).rejects.toThrow('endowment is not a proper Endowments object'); | ||
}); | ||
|
||
it('the snapRpc method will invoke the invokeSnapRpc function', async () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
const rpcRequest = { jsonrpc: '2.0', method: 'hello' }; | ||
await methodsObj.snapRpc('foo', 'bar', rpcRequest as any); | ||
expect(invokeSnapRpc).toHaveBeenCalledTimes(1); | ||
expect(invokeSnapRpc).toHaveBeenCalledWith('foo', 'bar', rpcRequest); | ||
}); | ||
|
||
it('the snapRpc method will throw an error if the target is not a string', async () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
const rpcRequest = { jsonrpc: '2.0', method: 'hello' }; | ||
await expect(async () => { | ||
await methodsObj.snapRpc(2 as any, 'bar', rpcRequest as any); | ||
}).rejects.toThrow('target is not a string'); | ||
}); | ||
|
||
it('the snapRpc method will throw an error if the origin is not a string', async () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
const rpcRequest = { jsonrpc: '2.0', method: 'hello' }; | ||
await expect(async () => { | ||
await methodsObj.snapRpc('foo', 2 as any, rpcRequest as any); | ||
}).rejects.toThrow('origin is not a string'); | ||
}); | ||
|
||
it('the snapRpc method will throw an error if the request is not a JSON RPC request', async () => { | ||
const startSnap = jest.fn(); | ||
const invokeSnapRpc = jest.fn(); | ||
const onTerminate = jest.fn(); | ||
const methodsObj = getCommandMethodImplementations( | ||
startSnap, | ||
invokeSnapRpc, | ||
onTerminate, | ||
); | ||
const rpcRequest = { method: 'hello' }; | ||
await expect(async () => { | ||
await methodsObj.snapRpc('foo', 'bar', rpcRequest as any); | ||
}).rejects.toThrow('request is not a proper JSON RPC Request'); | ||
}); | ||
}); |
Oops, something went wrong.