2022-11-02 07:26:53 -05:00
|
|
|
describe('C9 shell handler', () => {
|
2023-02-16 04:10:20 -06:00
|
|
|
const webContentsMocked = {
|
|
|
|
send: jest.fn(),
|
|
|
|
session: {
|
|
|
|
resolveProxy: jest.fn(),
|
|
|
|
},
|
|
|
|
getURL: jest.fn(),
|
|
|
|
};
|
2022-11-02 07:26:53 -05:00
|
|
|
const mockSpawnEvents = new Map<String, any>();
|
|
|
|
const mockSpawn = jest.fn();
|
|
|
|
const mockGetCommandLineArgs = jest.fn();
|
|
|
|
const mockGetGuid = jest.fn();
|
|
|
|
const mockKill = jest.fn();
|
|
|
|
let mockIsWindows: boolean;
|
|
|
|
|
|
|
|
jest.mock('child_process', () => {
|
|
|
|
return {
|
|
|
|
spawn: mockSpawn,
|
|
|
|
ChildProcess: () => {},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.mock('../src/common/utils', () => {
|
|
|
|
return {
|
|
|
|
getCommandLineArgs: mockGetCommandLineArgs,
|
|
|
|
getGuid: mockGetGuid,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.mock('../src/common/env', () => {
|
|
|
|
return {
|
|
|
|
isDevEnv: false,
|
|
|
|
isElectronQA: true,
|
|
|
|
isMac: false,
|
|
|
|
isWindowsOS: mockIsWindows,
|
|
|
|
isLinux: false,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-01-16 03:18:46 -06:00
|
|
|
jest.mock('../src/app/window-handler', () => {
|
|
|
|
return {
|
|
|
|
windowHandler: { isOnline: true },
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-11-02 07:26:53 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks().resetModules().resetAllMocks();
|
|
|
|
mockSpawnEvents.clear();
|
|
|
|
mockSpawn.mockImplementation((_cmd, _args) => {
|
|
|
|
return {
|
|
|
|
on: (event, callback) => {
|
|
|
|
mockSpawnEvents.set(event, callback);
|
|
|
|
},
|
|
|
|
stdout: { on: jest.fn() },
|
|
|
|
stderr: { on: jest.fn() },
|
|
|
|
kill: mockKill,
|
|
|
|
};
|
|
|
|
});
|
2023-02-16 04:10:20 -06:00
|
|
|
webContentsMocked.session.resolveProxy = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(() => Promise.resolve(''));
|
2022-11-02 07:26:53 -05:00
|
|
|
mockIsWindows = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('launch', () => {
|
2023-02-16 04:10:20 -06:00
|
|
|
it('success', async () => {
|
2022-11-02 07:26:53 -05:00
|
|
|
const { loadC9Shell } = require('../src/app/c9-shell-handler');
|
2023-02-16 04:10:20 -06:00
|
|
|
await loadC9Shell(webContentsMocked as any);
|
2022-11-02 07:26:53 -05:00
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: { status: 'starting' },
|
|
|
|
});
|
|
|
|
|
|
|
|
mockSpawnEvents.get('spawn')();
|
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: expect.objectContaining({ status: 'active' }),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-16 04:10:20 -06:00
|
|
|
it('failure', async () => {
|
2022-11-02 07:26:53 -05:00
|
|
|
const { loadC9Shell } = require('../src/app/c9-shell-handler');
|
2023-02-16 04:10:20 -06:00
|
|
|
await loadC9Shell(webContentsMocked as any);
|
2022-11-02 07:26:53 -05:00
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: { status: 'starting' },
|
|
|
|
});
|
|
|
|
|
|
|
|
mockSpawnEvents.get('close')(1);
|
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: expect.objectContaining({ status: 'inactive' }),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-16 04:10:20 -06:00
|
|
|
it('with attach', async () => {
|
2022-11-02 07:26:53 -05:00
|
|
|
mockGetCommandLineArgs.mockReturnValue('--c9pipe=custompipe');
|
|
|
|
|
|
|
|
const { loadC9Shell } = require('../src/app/c9-shell-handler');
|
2023-02-16 04:10:20 -06:00
|
|
|
await loadC9Shell(webContentsMocked as any);
|
2022-11-02 07:26:53 -05:00
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: { status: 'active', pipeName: 'symphony-c9-custompipe' },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-16 04:10:20 -06:00
|
|
|
it('cached status on relaunch', async () => {
|
2022-11-02 07:26:53 -05:00
|
|
|
const { loadC9Shell } = require('../src/app/c9-shell-handler');
|
2023-02-16 04:10:20 -06:00
|
|
|
await loadC9Shell(webContentsMocked as any);
|
2022-11-02 07:26:53 -05:00
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: { status: 'starting' },
|
|
|
|
});
|
|
|
|
|
|
|
|
mockSpawnEvents.get('spawn')();
|
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: expect.objectContaining({ status: 'active' }),
|
|
|
|
});
|
|
|
|
|
|
|
|
loadC9Shell(webContentsMocked as any);
|
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: expect.objectContaining({ status: 'active' }),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-16 04:10:20 -06:00
|
|
|
it('args', async () => {
|
2023-02-20 02:37:51 -06:00
|
|
|
mockGetCommandLineArgs.mockImplementation((_, name) =>
|
|
|
|
name === '--proxy-server=' ? '--proxy-server=512' : null,
|
|
|
|
);
|
|
|
|
webContentsMocked.session.resolveProxy = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(() => Promise.resolve(''));
|
2022-11-02 07:26:53 -05:00
|
|
|
mockGetGuid.mockReturnValue('just-another-guid');
|
|
|
|
const { loadC9Shell } = require('../src/app/c9-shell-handler');
|
2023-02-20 02:37:51 -06:00
|
|
|
|
2023-02-16 04:10:20 -06:00
|
|
|
await loadC9Shell(webContentsMocked as any);
|
2023-02-20 02:37:51 -06:00
|
|
|
|
2022-11-02 07:26:53 -05:00
|
|
|
expect(mockSpawn).toBeCalledWith(
|
|
|
|
expect.stringContaining('c9shell.exe'),
|
2023-02-16 04:10:20 -06:00
|
|
|
['--symphonyHost', 'just-another-guid', '--proxyServer', ''],
|
2022-11-02 07:26:53 -05:00
|
|
|
{ stdio: 'pipe' },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-02-16 04:10:20 -06:00
|
|
|
it('args, when resolveProxy returns DIRECT', async () => {
|
2023-02-20 02:37:51 -06:00
|
|
|
mockGetCommandLineArgs.mockImplementation((_, name) =>
|
|
|
|
name === '--proxy-server=' ? '--proxy-server=512' : null,
|
|
|
|
);
|
2023-02-16 04:10:20 -06:00
|
|
|
webContentsMocked.session.resolveProxy = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(() => Promise.resolve('DIRECT'));
|
|
|
|
mockGetGuid.mockReturnValue('just-another-guid');
|
|
|
|
const { loadC9Shell } = require('../src/app/c9-shell-handler');
|
|
|
|
|
|
|
|
await loadC9Shell(webContentsMocked as any);
|
|
|
|
|
|
|
|
expect(mockSpawn).toBeCalledWith(
|
|
|
|
expect.stringContaining('c9shell.exe'),
|
|
|
|
['--symphonyHost', 'just-another-guid', '--proxyServer', 'DIRECT'],
|
|
|
|
{ stdio: 'pipe' },
|
|
|
|
);
|
|
|
|
});
|
2022-11-02 07:26:53 -05:00
|
|
|
|
2023-02-16 04:10:20 -06:00
|
|
|
it('args, when resolveProxy returns string starting with PROXY ', async () => {
|
2023-02-20 02:37:51 -06:00
|
|
|
mockGetCommandLineArgs.mockImplementation((_, name) =>
|
|
|
|
name === '--proxy-server=' ? '--proxy-server=512' : null,
|
|
|
|
);
|
2023-02-16 04:10:20 -06:00
|
|
|
webContentsMocked.session.resolveProxy = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(() => Promise.resolve('PROXY 52.207.140.132:8443'));
|
|
|
|
mockGetGuid.mockReturnValue('just-another-guid');
|
2022-11-02 07:26:53 -05:00
|
|
|
const { loadC9Shell } = require('../src/app/c9-shell-handler');
|
2023-02-16 04:10:20 -06:00
|
|
|
|
|
|
|
await loadC9Shell(webContentsMocked as any);
|
|
|
|
|
|
|
|
expect(mockSpawn).toBeCalledWith(
|
|
|
|
expect.stringContaining('c9shell.exe'),
|
|
|
|
[
|
|
|
|
'--symphonyHost',
|
|
|
|
'just-another-guid',
|
|
|
|
'--proxyServer',
|
|
|
|
'52.207.140.132:8443',
|
|
|
|
],
|
|
|
|
{ stdio: 'pipe' },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-02-20 02:37:51 -06:00
|
|
|
it('args, when --proxy-server= is not passed as argument, do not pass resolved proxy to cloud9', async () => {
|
|
|
|
mockGetCommandLineArgs.mockReturnValue('');
|
|
|
|
webContentsMocked.session.resolveProxy = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(() => Promise.resolve('DIRECT'));
|
|
|
|
mockGetGuid.mockReturnValue('just-another-guid');
|
|
|
|
const { loadC9Shell } = require('../src/app/c9-shell-handler');
|
|
|
|
|
|
|
|
await loadC9Shell(webContentsMocked as any);
|
|
|
|
|
|
|
|
expect(mockSpawn).toBeCalledWith(
|
|
|
|
expect.stringContaining('c9shell.exe'),
|
|
|
|
['--symphonyHost', 'just-another-guid'],
|
|
|
|
{ stdio: 'pipe' },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('args, when --proxy-pac-url= is not passed as argument, do not pass resolved proxy to cloud9', async () => {
|
|
|
|
mockGetCommandLineArgs.mockReturnValue('');
|
|
|
|
webContentsMocked.session.resolveProxy = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(() => Promise.resolve('DIRECT'));
|
|
|
|
mockGetGuid.mockReturnValue('just-another-guid');
|
|
|
|
const { loadC9Shell } = require('../src/app/c9-shell-handler');
|
|
|
|
|
|
|
|
await loadC9Shell(webContentsMocked as any);
|
|
|
|
|
|
|
|
expect(mockSpawn).toBeCalledWith(
|
|
|
|
expect.stringContaining('c9shell.exe'),
|
|
|
|
['--symphonyHost', 'just-another-guid'],
|
|
|
|
{ stdio: 'pipe' },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-02-16 04:10:20 -06:00
|
|
|
it('non-windows', async () => {
|
|
|
|
mockIsWindows = false;
|
|
|
|
const { loadC9Shell } = require('../src/app/c9-shell-handler');
|
|
|
|
await loadC9Shell(webContentsMocked as any);
|
2022-11-02 07:26:53 -05:00
|
|
|
expect(mockSpawn).not.toBeCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('terminate', () => {
|
2023-02-16 04:10:20 -06:00
|
|
|
it('success', async () => {
|
2022-11-02 07:26:53 -05:00
|
|
|
const {
|
|
|
|
loadC9Shell,
|
|
|
|
terminateC9Shell,
|
|
|
|
} = require('../src/app/c9-shell-handler');
|
2023-02-16 04:10:20 -06:00
|
|
|
await loadC9Shell(webContentsMocked as any);
|
2022-11-02 07:26:53 -05:00
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: { status: 'starting' },
|
|
|
|
});
|
|
|
|
|
|
|
|
terminateC9Shell(webContentsMocked as any);
|
|
|
|
expect(mockKill).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('no terminate if never started', () => {
|
|
|
|
const { terminateC9Shell } = require('../src/app/c9-shell-handler');
|
|
|
|
terminateC9Shell(webContentsMocked as any);
|
|
|
|
expect(mockKill).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
2023-02-16 04:10:20 -06:00
|
|
|
it('no terminate if already exited', async () => {
|
2022-11-02 07:26:53 -05:00
|
|
|
const {
|
|
|
|
loadC9Shell,
|
|
|
|
terminateC9Shell,
|
|
|
|
} = require('../src/app/c9-shell-handler');
|
2023-02-16 04:10:20 -06:00
|
|
|
await loadC9Shell(webContentsMocked as any);
|
2022-11-02 07:26:53 -05:00
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: { status: 'starting' },
|
|
|
|
});
|
|
|
|
|
|
|
|
mockSpawnEvents.get('close')(1);
|
|
|
|
expect(webContentsMocked.send).lastCalledWith('c9-status-event', {
|
|
|
|
status: expect.objectContaining({ status: 'inactive' }),
|
|
|
|
});
|
|
|
|
|
|
|
|
terminateC9Shell(webContentsMocked as any);
|
|
|
|
expect(mockKill).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|