Fix script deletion during execution on desktop

This commit fixes an issue seen on certain Windows environments (Windows
10 22H2 and 11 23H2 Pro Azure VMs) where scripts were being deleted
during execution due to temporary directory usage. To resolve this,
scripts are now stored in a persistent directory, enhancing reliability
for long-running scripts and improving auditability along with
troubleshooting.

Key changes:

- Move script execution logic to the `main` process from `preloader` to
  utilize Electron's `app.getPath`.
- Improve runtime environment detection for non-browser environments to
  allow its usage in Electron main process.
- Introduce a secure module to expose IPC channels from the main process
  to the renderer via the preloader process.

Supporting refactorings include:

- Simplify `CodeRunner` interface by removing the `tempScriptFolderName`
  parameter.
- Rename `NodeSystemOperations` to `NodeElectronSystemOperations` as it
  now wraps electron APIs too, and convert it to class for simplicity.
- Rename `TemporaryFileCodeRunner` to `ScriptFileCodeRunner` to reflect
  its new functinoality.
- Rename `SystemOperations` folder to `System` for simplicity.
- Rename `HostRuntimeEnvironment` to `BrowserRuntimeEnvironment` for
  clarity.
- Refactor main Electron process configuration to align with latest
  Electron documentation/recommendations.
- Refactor unit tests `BrowserRuntimeEnvironment` to simplify singleton
  workaround.
- Use alias imports like `electron/main` and `electron/common` for
  better clarity.
This commit is contained in:
undergroundwires
2024-01-06 18:47:58 +01:00
parent bf7fb0732c
commit c84a1bb74c
75 changed files with 1809 additions and 574 deletions

View File

@@ -0,0 +1,146 @@
// eslint-disable-next-line max-classes-per-file
import { describe, it, expect } from 'vitest';
import { NodeJSProcessAccessor, NodeRuntimeEnvironment, PlatformToOperatingSystemConverter } from '@/infrastructure/RuntimeEnvironment/Node/NodeRuntimeEnvironment';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
import { OperatingSystem } from '@/domain/OperatingSystem';
describe('NodeRuntimeEnvironment', () => {
describe('constructor', () => {
describe('throws with missing process', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing process';
const absentProcess = absentValue;
// act
const act = () => new NodeRuntimeEnvironmentBuilder()
.withProcess(absentProcess as never)
.build();
// assert
expect(act).to.throw(expectedError);
}, { excludeUndefined: true });
});
});
describe('os', () => {
it('matches specified OS', () => {
// arrange
const expectedOs = OperatingSystem.Android;
const osConverterMock: PlatformToOperatingSystemConverter = () => expectedOs;
// act
const environment = new NodeRuntimeEnvironmentBuilder()
.withOsConverter(osConverterMock)
.build();
// assert
expect(environment.os).to.equal(expectedOs);
});
it('uses process platform to determine OS', () => {
// arrange
const expectedPlatform: NodeJS.Platform = 'win32';
let actualPlatform: NodeJS.Platform | undefined;
const osConverterMock: PlatformToOperatingSystemConverter = (platform) => {
actualPlatform = platform;
return undefined;
};
const nodeProcessMock = new NodeJSProcessAccessorStub()
.withPlatform(expectedPlatform);
// act
new NodeRuntimeEnvironmentBuilder()
.withOsConverter(osConverterMock)
.withProcess(nodeProcessMock)
.build();
// assert
expect(expectedPlatform).to.equal(actualPlatform);
});
it('is undefined for unknown platforms', () => {
// arrange
const expectedOs = undefined;
const osConverterMock: PlatformToOperatingSystemConverter = () => undefined;
// act
const environment = new NodeRuntimeEnvironmentBuilder()
.withOsConverter(osConverterMock)
.build();
// assert
expect(environment.os).to.equal(expectedOs);
});
});
describe('isDesktop', () => {
it('is always true', () => {
// arrange
const expectedDesktopCondition = true;
// act
const environment = new NodeRuntimeEnvironment();
/// assert
expect(environment.isDesktop).to.equal(expectedDesktopCondition);
});
});
describe('isNonProduction', () => {
it('identifies development mode', () => {
// arrange
const expectedNonProductionCondition = true;
const nodeProcess = new NodeJSProcessAccessorStub()
.withNodeEnv('development');
// act
const environment = new NodeRuntimeEnvironmentBuilder()
.withProcess(nodeProcess)
.build();
/// assert
expect(environment.isNonProduction).to.equal(expectedNonProductionCondition);
});
it('identifies production mode', () => {
// arrange
const expectedNonProductionCondition = false;
const nodeProcess = new NodeJSProcessAccessorStub()
.withNodeEnv('production');
// act
const environment = new NodeRuntimeEnvironmentBuilder()
.withProcess(nodeProcess)
.build();
/// assert
expect(environment.isNonProduction).to.equal(expectedNonProductionCondition);
});
});
});
class NodeJSProcessAccessorStub implements NodeJSProcessAccessor {
private nodeEnv?: string = 'development';
public platform: NodeJS.Platform = 'linux';
public get env() {
return {
NODE_ENV: this.nodeEnv,
};
}
public withNodeEnv(nodeEnv?: string): this {
this.nodeEnv = nodeEnv;
return this;
}
public withPlatform(platform: NodeJS.Platform): this {
this.platform = platform;
return this;
}
}
class NodeRuntimeEnvironmentBuilder {
private process?: NodeJSProcessAccessor = new NodeJSProcessAccessorStub();
private osConverter?: PlatformToOperatingSystemConverter = () => OperatingSystem.Windows;
public withProcess(process?: NodeJSProcessAccessor): this {
this.process = process;
return this;
}
public withOsConverter(osConverter?: PlatformToOperatingSystemConverter): this {
this.osConverter = osConverter;
return this;
}
public build(): NodeRuntimeEnvironment {
return new NodeRuntimeEnvironment(
this.process,
this.osConverter,
);
}
}