Files
privacy.sexy/tests/unit/presentation/electron/shared/IpcChannelDefinitions.spec.ts
undergroundwires e17744faf0 Bump to TypeScript 5.5 and enable noImplicitAny
This commit upgrades TypeScript from 5.4 to 5.5 and enables the
`noImplicitAny` option for stricter type checking. It refactors code to
comply with `noImplicitAny` and adapts to new TypeScript features and
limitations.

Key changes:

- Migrate from TypeScript 5.4 to 5.5
- Enable `noImplicitAny` for stricter type checking
- Refactor code to comply with new TypeScript features and limitations

Other supporting changes:

- Refactor progress bar handling for type safety
- Drop 'I' prefix from interfaces to align with new code convention
- Update TypeScript target from `ES2017` and `ES2018`.
  This allows named capturing groups. Otherwise, new TypeScript compiler
  does not compile the project and shows the following error:
  ```
  ...
  TimestampedFilenameGenerator.spec.ts:105:23 - error TS1503: Named capturing groups are only available when targeting 'ES2018' or later
  const pattern = /^(?<timestamp>\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})-(?<scriptName>[^.]+?)(?:\.(?<extension>[^.]+))?$/;// timestamp-scriptName.extension
  ...
  ```
- Refactor usage of `electron-progressbar` for type safety and
  less complexity.
2024-09-26 16:07:37 +02:00

69 lines
2.7 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { type 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 ChannelDefinitionKey];
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);
});
});
});
});