1/*
2 * session-launcher.test.ts
3 *
4 * Copyright (C) 2021 by RStudio, PBC
5 *
6 * Unless you have received this program directly from RStudio pursuant
7 * to the terms of a commercial license agreement with RStudio, then
8 * this program is licensed to you under the terms of version 3 of the
9 * GNU Affero General Public License. This program is distributed WITHOUT
10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13 *
14 */
15
16import { describe } from 'mocha';
17import { assert } from 'chai';
18import { restore, saveAndClear } from '../unit-utils';
19
20import { FilePath } from '../../../src/core/file-path';
21import { getenv } from '../../../src/core/environment';
22
23import { SessionLauncher } from '../../../src/main/session-launcher';
24import { ApplicationLaunch } from '../../../src/main/application-launch';
25import { Application } from '../../../src/main/application';
26import { appState, clearApplicationSingleton, setApplication } from '../../../src/main/app-state';
27
28function getNewLauncher(): SessionLauncher {
29  return new SessionLauncher(new FilePath(), new FilePath(), new FilePath(), new ApplicationLaunch());
30}
31
32describe('session-launcher', () => {
33  const saveVars: Record<string, string> = {
34    RS_LOCAL_PEER: ''
35  };
36
37  beforeEach(() => {
38    setApplication(new Application());
39    saveAndClear(saveVars);
40  });
41
42  afterEach(() => {
43    clearApplicationSingleton();
44    restore(saveVars);
45  });
46
47  it('generates and stores launcher token', () => {
48    const token = SessionLauncher.launcherToken;
49    assert.isNotEmpty(token);
50    assert.strictEqual(SessionLauncher.launcherToken, token);
51  });
52  it('buildLaunchContext sets RS_LOCAL_PEER on Win32', () => {
53    const launcher = getNewLauncher();
54    launcher.buildLaunchContext();
55    const localPeer = getenv('RS_LOCAL_PEER');
56    if (process.platform === 'win32') {
57      assert.isNotEmpty(localPeer);
58      assert.isAbove(localPeer.indexOf(appState().port.toString()), -1);
59    } else {
60      assert.isEmpty(localPeer);
61    }
62  });
63  it('buildLaunchContext reuses same port', () => {
64    const launcher = getNewLauncher();
65    const origPort = appState().port;
66    launcher.buildLaunchContext(true);
67    const newPort = appState().port;
68    assert.equal(origPort, newPort);
69  });
70  it('buildLaunchContext triggers new port number', () => {
71    const launcher = getNewLauncher();
72    const origPort = appState().port;
73    launcher.buildLaunchContext(false);
74    const newPort = appState().port;
75    assert.notEqual(origPort, newPort);
76  });
77});
78