diff --git a/addons/api/mirage/config.js b/addons/api/mirage/config.js index bbd5b1637..5ae912f69 100644 --- a/addons/api/mirage/config.js +++ b/addons/api/mirage/config.js @@ -3,9 +3,12 @@ import config from '../config/environment'; import { authHandler, deauthHandler } from './route-handlers/auth'; import { pickRandomStatusString } from './factories/session'; import { Response } from 'miragejs'; +import initializeMockIPC from './scenarios/ipc'; export default function() { + initializeMockIPC(this); + // make this `http://localhost:8080`, for example, if your API is on a different server // this.urlPrefix = ''; // make this `/api`, for example, if your API is namespaced diff --git a/addons/api/mirage/scenarios/ipc.js b/addons/api/mirage/scenarios/ipc.js new file mode 100644 index 000000000..d72a57b32 --- /dev/null +++ b/addons/api/mirage/scenarios/ipc.js @@ -0,0 +1,76 @@ +import Ember from 'ember'; +import config from 'ember-get-config'; +import { random } from 'faker'; + +export default function initializeMockIPC (server) { + /** + * We strive to make this application runnable in a regular web browser, since + * it is a convenient environment for development and testing. But only an + * Electron environment has true IPC. Outside of Electron, we mock the handling + * of the message-based IPC requests originating from the + * renderer (the Ember app). + */ + class MockIPC { + async invoke(method, payload) { + return await this[method](payload); + } + + origin = null; + + getOrigin() { + return this.origin; + } + + setOrigin(origin) { + this.origin = origin; + return this.origin; + } + + cliExists() { + return true; + } + + /** + * Creates a new session instance associated with the specified target. + * @param connect_details + * @param + */ + connect({ target_id }) { + const target = server.schema.targets.find(target_id); + const { scope, type } = target; + const newSession = server.schema.sessions.create({ + scope, + target, + userId: 'authenticateduser', + type, + created_time: new Date().toISOString() + }); + return { + address: `${window.location.protocol}//${window.location.hostname}`, + port: random.number(), + protocol: type, + session_id: newSession.id + }; + } + } + + /** + * Establishes a mock IPC handler, which mimics the behavior of the Electron + * main process, and routes messages to it in a way similar to our + * preload.js script. + * + * Initializes mock IPC only in a non-Electron non-testing context. + */ + if (!config.isElectron && !Ember.testing) { + const mockIPC = new MockIPC(); + window.addEventListener('message', async function (event) { + if (event.origin !== window.location.origin) return; + const { method, payload } = event?.data ?? {}; + if (method) { + const response = await mockIPC.invoke(method, payload); + event.ports[0].postMessage(response); + } + }); + } + +} diff --git a/ui/desktop/app/initializers/ipc.js b/ui/desktop/app/initializers/ipc.js deleted file mode 100644 index 74e9d8545..000000000 --- a/ui/desktop/app/initializers/ipc.js +++ /dev/null @@ -1,52 +0,0 @@ -import Ember from 'ember'; -import config from '../config/environment'; - -/** - * We strive to make this application runnable in a regular web browser, since - * it is a convenient environment for development and testing. But only an - * Electron environment has true IPC. Outside of Electron, we mock the handling - * of the message-based IPC requests originating from the - * renderer (the Ember app). - */ -class MockIPC { - async invoke(method, payload) { - console.log('MockIPC:', method, payload); - return await this[method](payload); - } - - origin = null; - - getOrigin() { - return this.origin; - } - - setOrigin(origin) { - this.origin = origin; - return this.origin; - } - - connect() {} -} - -/** - * Establishes a mock IPC handler, which mimics the behavior of the Electron - * main process, and routes messages to it in a way similar to our - * preload.js script. - * - * Initializes mock IPC only in a non-Electron non-testing context. - */ -export function initialize() { - if (!config.isElectron && !Ember.testing) { - const mockIPC = new MockIPC(); - window.addEventListener('message', async function (event) { - if (event.origin !== window.location.origin) return; - const { method, payload } = event?.data ?? {}; - if (method) { - const response = await mockIPC.invoke(method, payload); - event.ports[0].postMessage(response); - } - }); - } -} - -export default { initialize }; diff --git a/ui/desktop/tests/unit/initializers/ipc-test.js b/ui/desktop/tests/unit/initializers/ipc-test.js deleted file mode 100644 index 6efc5c816..000000000 --- a/ui/desktop/tests/unit/initializers/ipc-test.js +++ /dev/null @@ -1,28 +0,0 @@ -import Application from '@ember/application'; - -import { initialize } from 'desktop/initializers/ipc'; -import { module, test } from 'qunit'; -import { run } from '@ember/runloop'; - -module('Unit | Initializer | ipc', function(hooks) { - hooks.beforeEach(function() { - this.TestApplication = Application.extend(); - this.TestApplication.initializer({ - name: 'initializer under test', - initialize - }); - - this.application = this.TestApplication.create({ autoboot: false }); - }); - - hooks.afterEach(function() { - run(this.application, 'destroy'); - }); - - // TODO: Replace this with your real tests. - test('it works', async function(assert) { - await this.application.boot(); - - assert.ok(true); - }); -});