Skip to content
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

refactor plugin controller to use v2 #13

Merged
merged 11 commits into from
Jun 1, 2021
1 change: 1 addition & 0 deletions packages/controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"prepare": "yarn lint && yarn build"
},
"dependencies": {
"@metamask/controllers": "^9.1.0",
"@metamask/object-multiplex": "^1.1.0",
"@metamask/obs-store": "^6.0.2",
"@metamask/post-message-stream": "4.0.0",
Expand Down
99 changes: 99 additions & 0 deletions packages/controllers/src/plugins/PluginController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import fs from 'fs';
import { ControllerMessenger } from '@metamask/controllers/dist/ControllerMessenger';
import { WorkerController } from '../workers/WorkerController';
import { PluginController } from './PluginController';

const workerCode = fs.readFileSync(
require.resolve('@mm-snap/workers/dist/PluginWorker.js'),
'utf8',
);

describe('PluginController Controller', () => {
it('can create a worker and plugin controller', async () => {
const workerController = new WorkerController({
setupWorkerConnection: jest.fn(),
workerUrl: new URL(URL.createObjectURL(new Blob([workerCode]))),
});
const pluginController = new PluginController({
command: workerController.command.bind(workerController),
createPluginWorker: workerController.createPluginWorker.bind(
workerController,
),
terminateAll: workerController.terminateAll.bind(workerController),
terminateWorkerOf: workerController.terminateWorkerOf.bind(
workerController,
),
startPlugin: workerController.startPlugin.bind(workerController),
removeAllPermissionsFor: jest.fn(),
getPermissions: jest.fn(),
hasPermission: jest.fn(),
requestPermissions: jest.fn(),
closeAllConnections: jest.fn(),
messenger: new ControllerMessenger<any, any>().getRestricted({
name: 'PluginController',
}),
state: {
pluginStates: {},
plugins: {},
},
});
expect(pluginController).toBeDefined();
});
it('can add a plugin and use its JSON-RPC api', async () => {
shanejonas marked this conversation as resolved.
Show resolved Hide resolved
const workerController = new WorkerController({
setupWorkerConnection: jest.fn(),
workerUrl: new URL(URL.createObjectURL(new Blob([workerCode]))),
});
const pluginController = new PluginController({
command: workerController.command.bind(workerController),
createPluginWorker: workerController.createPluginWorker.bind(
workerController,
),
terminateAll: workerController.terminateAll.bind(workerController),
terminateWorkerOf: workerController.terminateWorkerOf.bind(
workerController,
),
startPlugin: workerController.startPlugin.bind(workerController),
removeAllPermissionsFor: jest.fn(),
getPermissions: jest.fn(),
hasPermission: jest.fn(),
requestPermissions: jest.fn(),
closeAllConnections: jest.fn(),
messenger: new ControllerMessenger<any, any>().getRestricted({
name: 'PluginController',
}),
state: {
pluginStates: {},
plugins: {},
},
});
const plugin = await pluginController.add({
name: 'TestPlugin',
sourceCode: `
wallet.registerRpcMessageHandler(async (origin, request) => {
const {method, params, id} = request;
wallet.request({method: 'setState'})
return method + id;
});
`,
manifest: {
web3Wallet: {
initialPermissions: {},
},
version: '0.0.0-development',
},
});
await pluginController.startPlugin(plugin.name);
const handle = pluginController.getRpcMessageHandler(plugin.name);
if (!handle) {
throw Error('rpc handler not found');
}
const result = await handle('foo.com', {
jsonrpc: '2.0',
method: 'test',
params: {},
id: 1,
});
expect(result).toEqual('test1');
});
});
Loading