Files
privacy.sexy/tests/unit/presentation/electron/shared/IpcChannelDefinitions.spec.ts
undergroundwires 6ada8d425c Improve script error dialogs #304
- Include the script's directory path #304.
- Exclude Windows-specific instructions on non-Windows OS.
- Standardize language across dialogs for consistency.

Other supporting changes:

- Add script diagnostics data collection from main process.
- Document script file storage and execution tamper protection in
  SECURITY.md.
- Remove redundant comment in `NodeReadbackFileWriter`.
- Centralize error display for uniformity and simplicity.
- Simpify `WindowVariablesValidator` to omit checks when not on the
  renderer process.
- Improve and centralize Electron environment detection.
- Use more emphatic language (don't worry) in error messages.
2024-01-17 23:59:05 +01:00

70 lines
2.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { IpcChannel } from '@/presentation/electron/shared/IpcBridging/IpcChannel';
import { ChannelDefinitionKey, IpcChannelDefinitions } from '@/presentation/electron/shared/IpcBridging/IpcChannelDefinitions';
describe('IpcChannelDefinitions', () => {
it('defines IPC channels correctly', () => {
const testScenarios: Record<ChannelDefinitionKey, {
readonly expectedNamespace: string;
readonly expectedAccessibleMembers: readonly string[];
}> = {
CodeRunner: {
expectedNamespace: 'code-run',
expectedAccessibleMembers: ['runCode'],
},
Dialog: {
expectedNamespace: 'dialogs',
expectedAccessibleMembers: ['saveFile'],
},
ScriptDiagnosticsCollector: {
expectedNamespace: 'script-diagnostics-collector',
expectedAccessibleMembers: ['collectDiagnosticInformation'],
},
};
Object.entries(testScenarios).forEach((
[definitionKey, { expectedNamespace, expectedAccessibleMembers }],
) => {
describe(`channel: "${definitionKey}"`, () => {
const ipcChannelUnderTest = IpcChannelDefinitions[definitionKey] as IpcChannel<unknown>;
it('has expected namespace', () => {
// act
const actualNamespace = ipcChannelUnderTest.namespace;
// assert
expect(actualNamespace).to.equal(expectedNamespace);
});
it('has expected accessible members', () => {
// act
const actualAccessibleMembers = ipcChannelUnderTest.accessibleMembers;
// assert
expect(actualAccessibleMembers).to.have.lengthOf(expectedAccessibleMembers.length);
expect(actualAccessibleMembers).to.have.members(expectedAccessibleMembers);
});
});
});
});
describe('IPC channel uniqueness', () => {
it('has unique namespaces', () => {
// arrange
const extractedNamespacesFromDefinitions = Object
.values(IpcChannelDefinitions)
.map((channel) => channel.namespace);
// act
const duplicateNamespaceEntries = extractedNamespacesFromDefinitions
.filter((item, index) => extractedNamespacesFromDefinitions.indexOf(item) !== index);
// assert
expect(duplicateNamespaceEntries).to.have.lengthOf(0);
});
it('has unique accessible members within each channel', () => {
Object.values(IpcChannelDefinitions).forEach((channel) => {
// arrange
const accessibleMembersOfChannel = channel.accessibleMembers as string[];
// act
const repeatedAccessibleMembersInChannel = accessibleMembersOfChannel
.filter((item, index) => accessibleMembersOfChannel.indexOf(item) !== index);
// assert
expect(repeatedAccessibleMembersInChannel).to.have.lengthOf(0);
});
});
});
});