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.
112 lines
3.9 KiB
TypeScript
112 lines
3.9 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { CodeRunnerStub } from '@tests/unit/shared/Stubs/CodeRunnerStub';
|
|
import { IpcChannelDefinitions } from '@/presentation/electron/shared/IpcBridging/IpcChannelDefinitions';
|
|
import { CodeRunnerFactory, IpcRegistrar, registerAllIpcChannels } from '@/presentation/electron/main/IpcRegistration';
|
|
import { IpcChannel } from '@/presentation/electron/shared/IpcBridging/IpcChannel';
|
|
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
|
|
import { collectExceptionMessage } from '@tests/unit/shared/ExceptionCollector';
|
|
|
|
describe('IpcRegistration', () => {
|
|
describe('registerAllIpcChannels', () => {
|
|
it('registers all defined IPC channels', () => {
|
|
Object.entries(IpcChannelDefinitions).forEach(([key, expectedChannel]) => {
|
|
it(key, () => {
|
|
// arrange
|
|
const { registrarMock, isChannelRegistered } = createIpcRegistrarMock();
|
|
const context = new IpcRegistrationTestSetup()
|
|
.withRegistrar(registrarMock);
|
|
// act
|
|
context.run();
|
|
// assert
|
|
expect(isChannelRegistered(expectedChannel)).to.equal(true);
|
|
});
|
|
});
|
|
});
|
|
describe('registers expected instances', () => {
|
|
const testScenarios: Record<keyof typeof IpcChannelDefinitions, {
|
|
buildContext: (context: IpcRegistrationTestSetup) => IpcRegistrationTestSetup,
|
|
expectedInstance: object,
|
|
}> = {
|
|
CodeRunner: (() => {
|
|
const expectedInstance = new CodeRunnerStub();
|
|
return {
|
|
buildContext: (c) => c.withCodeRunnerFactory(() => expectedInstance),
|
|
expectedInstance,
|
|
};
|
|
})(),
|
|
};
|
|
Object.entries(testScenarios).forEach(([
|
|
key, { buildContext, expectedInstance },
|
|
]) => {
|
|
it(key, () => {
|
|
// arrange
|
|
const { registrarMock, getRegisteredInstance } = createIpcRegistrarMock();
|
|
const context = buildContext(new IpcRegistrationTestSetup()
|
|
.withRegistrar(registrarMock));
|
|
// act
|
|
context.run();
|
|
// assert
|
|
const actualInstance = getRegisteredInstance(IpcChannelDefinitions.CodeRunner);
|
|
expect(actualInstance).to.equal(expectedInstance);
|
|
});
|
|
});
|
|
});
|
|
it('throws an error if registration fails', () => {
|
|
// arrange
|
|
const expectedError = 'registrar error';
|
|
const registrarMock: IpcRegistrar = () => {
|
|
throw new Error(expectedError);
|
|
};
|
|
const context = new IpcRegistrationTestSetup()
|
|
.withRegistrar(registrarMock);
|
|
// act
|
|
const exceptionMessage = collectExceptionMessage(() => context.run());
|
|
// assert
|
|
expect(exceptionMessage).to.include(expectedError);
|
|
});
|
|
});
|
|
});
|
|
|
|
class IpcRegistrationTestSetup {
|
|
private codeRunnerFactory: CodeRunnerFactory = () => new CodeRunnerStub();
|
|
|
|
private registrar: IpcRegistrar = () => { /* NOOP */ };
|
|
|
|
public withRegistrar(registrar: IpcRegistrar): this {
|
|
this.registrar = registrar;
|
|
return this;
|
|
}
|
|
|
|
public withCodeRunnerFactory(codeRunnerFactory: CodeRunnerFactory): this {
|
|
this.codeRunnerFactory = codeRunnerFactory;
|
|
return this;
|
|
}
|
|
|
|
public run() {
|
|
registerAllIpcChannels(
|
|
this.codeRunnerFactory,
|
|
this.registrar,
|
|
);
|
|
}
|
|
}
|
|
|
|
function createIpcRegistrarMock() {
|
|
const registeredChannels = new Array<Parameters<IpcRegistrar>>();
|
|
const registrarMock: IpcRegistrar = <T>(channel: IpcChannel<T>, obj: T) => {
|
|
registeredChannels.push([channel as IpcChannel<unknown>, obj]);
|
|
};
|
|
const isChannelRegistered = <T>(channel: IpcChannel<T>): boolean => {
|
|
return registeredChannels.some((i) => i[0] === channel);
|
|
};
|
|
const getRegisteredInstance = <T>(channel: IpcChannel<T>): unknown => {
|
|
const registration = registeredChannels.find((i) => i[0] === channel);
|
|
expectExists(registration);
|
|
return registration[1];
|
|
};
|
|
return {
|
|
registrarMock,
|
|
isChannelRegistered,
|
|
getRegisteredInstance,
|
|
};
|
|
}
|