This commit improves the management of script execution process by enhancing the way terminal commands are handled, paving the way for easier future modifications and providing clearer feedback to users when scripts are cancelled. Previously, the UI displayed a generic error message which could lead to confusion if the user intentionally cancelled the script execution. Now, a specific error dialog will appear, improving the user experience by accurately reflecting the action taken by the user. This change affects code execution on Linux where closing GNOME terminal returns exit code `137` which is then treated by script cancellation by privacy.sexy to show the accurate error dialog. It does not affect macOS and Windows as curret commands result in success (`0`) exit code on cancellation. Additionally, this update encapsulates OS-specific logic into dedicated classes, promoting better separation of concerns and increasing the modularity of the codebase. This makes it simpler to maintain and extend the application. Key changes: - Display a specific error message for script cancellations. - Refactor command execution into dedicated classes. - Improve file permission setting flexibility and avoid setting file permissions on Windows as it's not required to execute files. - Introduce more granular error types for script execution. - Increase logging for shell commands to aid in debugging. - Expand test coverage to ensure reliability. - Fix error dialogs not showing the error messages due to incorrect propagation of errors. Other supported changes: - Update `SECURITY.md` with details on script readback and verification. - Fix a typo in `IpcRegistration.spec.ts`. - Document antivirus scans in `desktop-vs-web-features.md`.
156 lines
5.6 KiB
TypeScript
156 lines
5.6 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { CodeRunnerStub } from '@tests/unit/shared/Stubs/CodeRunnerStub';
|
|
import { type ChannelDefinitionKey, IpcChannelDefinitions } from '@/presentation/electron/shared/IpcBridging/IpcChannelDefinitions';
|
|
import {
|
|
type CodeRunnerFactory, type DialogFactory, type IpcChannelRegistrar,
|
|
type ScriptDiagnosticsCollectorFactory, registerAllIpcChannels,
|
|
} from '@/presentation/electron/main/IpcRegistration';
|
|
import type { IpcChannel } from '@/presentation/electron/shared/IpcBridging/IpcChannel';
|
|
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
|
|
import { collectExceptionMessage } from '@tests/unit/shared/ExceptionCollector';
|
|
import { DialogStub } from '@tests/unit/shared/Stubs/DialogStub';
|
|
import { ScriptDiagnosticsCollectorStub } from '../../../shared/Stubs/ScriptDiagnosticsCollectorStub';
|
|
|
|
describe('IpcRegistration', () => {
|
|
describe('registerAllIpcChannels', () => {
|
|
describe('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<ChannelDefinitionKey, {
|
|
buildContext: (context: IpcRegistrationTestSetup) => IpcRegistrationTestSetup,
|
|
expectedInstance: object,
|
|
}> = {
|
|
CodeRunner: (() => {
|
|
const expectedInstance = new CodeRunnerStub();
|
|
return {
|
|
buildContext: (c) => c.withCodeRunnerFactory(() => expectedInstance),
|
|
expectedInstance,
|
|
};
|
|
})(),
|
|
Dialog: (() => {
|
|
const expectedInstance = new DialogStub();
|
|
return {
|
|
buildContext: (c) => c.withDialogFactory(() => expectedInstance),
|
|
expectedInstance,
|
|
};
|
|
})(),
|
|
ScriptDiagnosticsCollector: (() => {
|
|
const expectedInstance = new ScriptDiagnosticsCollectorStub();
|
|
return {
|
|
buildContext: (c) => c.withScriptDiagnosticsCollectorFactory(() => 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 channel = IpcChannelDefinitions[key];
|
|
const actualInstance = getRegisteredInstance(channel);
|
|
expect(actualInstance).to.equal(expectedInstance);
|
|
});
|
|
});
|
|
});
|
|
it('throws an error if registration fails', () => {
|
|
// arrange
|
|
const expectedError = 'registrar error';
|
|
const registrarMock: IpcChannelRegistrar = () => {
|
|
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 registrar: IpcChannelRegistrar = () => { /* NOOP */ };
|
|
|
|
private codeRunnerFactory: CodeRunnerFactory = () => new CodeRunnerStub();
|
|
|
|
private dialogFactory: DialogFactory = () => new DialogStub();
|
|
|
|
private scriptDiagnosticsCollectorFactory
|
|
: ScriptDiagnosticsCollectorFactory = () => new ScriptDiagnosticsCollectorStub();
|
|
|
|
public withRegistrar(registrar: IpcChannelRegistrar): this {
|
|
this.registrar = registrar;
|
|
return this;
|
|
}
|
|
|
|
public withCodeRunnerFactory(codeRunnerFactory: CodeRunnerFactory): this {
|
|
this.codeRunnerFactory = codeRunnerFactory;
|
|
return this;
|
|
}
|
|
|
|
public withDialogFactory(dialogFactory: DialogFactory): this {
|
|
this.dialogFactory = dialogFactory;
|
|
return this;
|
|
}
|
|
|
|
public withScriptDiagnosticsCollectorFactory(
|
|
scriptDiagnosticsCollectorFactory: ScriptDiagnosticsCollectorFactory,
|
|
): this {
|
|
this.scriptDiagnosticsCollectorFactory = scriptDiagnosticsCollectorFactory;
|
|
return this;
|
|
}
|
|
|
|
public run() {
|
|
registerAllIpcChannels(
|
|
this.registrar,
|
|
this.codeRunnerFactory,
|
|
this.dialogFactory,
|
|
this.scriptDiagnosticsCollectorFactory,
|
|
);
|
|
}
|
|
}
|
|
|
|
type DefinedIpcChannelTypes = {
|
|
[K in ChannelDefinitionKey]: (typeof IpcChannelDefinitions)[K]
|
|
}[ChannelDefinitionKey];
|
|
|
|
function createIpcRegistrarMock() {
|
|
const registeredChannels = new Array<Parameters<IpcChannelRegistrar>>();
|
|
const registrarMock: IpcChannelRegistrar = <T>(channel: IpcChannel<T>, obj: T) => {
|
|
registeredChannels.push([channel as IpcChannel<unknown>, obj]);
|
|
};
|
|
const isChannelRegistered = (channel: DefinedIpcChannelTypes): boolean => {
|
|
return registeredChannels.some((i) => i[0] === channel);
|
|
};
|
|
const getRegisteredInstance = <T>(channel: IpcChannel<T>): T => {
|
|
const registration = registeredChannels.find((i) => i[0] === channel);
|
|
expectExists(registration);
|
|
const [, registeredInstance] = registration;
|
|
return registeredInstance as T;
|
|
};
|
|
return {
|
|
registrarMock,
|
|
isChannelRegistered,
|
|
getRegisteredInstance,
|
|
};
|
|
}
|