Files
privacy.sexy/tests/unit/presentation/components/Shared/Hooks/Log/ClientLoggerFactory.spec.ts
undergroundwires c546a33eff Show native save dialogs in desktop app #50, #264
This commit introduces native operating system file dialogs in the
desktop application replacing the existing web-based dialogs.

It lays the foundation for future enhancements such as:

- Providing error messages when saving or executing files, addressing
  #264.
- Creating system restore points, addressing #50.

Documentation updates:

- Update `desktop-vs-web-features.md` with added functionality.
- Update `README.md` with security feature highlights.
- Update home page documentation to emphasize security features.

Other supporting changes include:

- Integrate IPC communication channels for secure Electron dialog API
  interactions.
- Refactor `IpcRegistration` for more type-safety and simplicity.
- Introduce a Vue hook to encapsulate dialog functionality.
- Improve errors during IPC registration for easier troubleshooting.
- Move `ClientLoggerFactory` for consistency in hooks organization and
  remove `LoggerFactory` interface for simplicity.
- Add tests for the save file dialog in the browser context.
- Add `Blob` polyfill in tests to compensate for the missing
  `blob.text()` function in `jsdom` (see jsdom/jsdom#2555).

Improve environment detection logic:

- Treat test environment as browser environments to correctly activate
  features based on the environment. This resolves issues where the
  environment is misidentified as desktop, but Electron preloader APIs
  are missing.
- Rename `isDesktop` environment identification variable to
  `isRunningAsDesktopApplication` for better clarity and to avoid
  confusion with desktop environments in web/browser/test environments.
- Simplify `BrowserRuntimeEnvironment` to consistently detect
  non-desktop application environments.
- Improve environment detection for Electron main process
  (electron/electron#2288).
2024-01-13 18:04:23 +01:00

160 lines
5.4 KiB
TypeScript

// eslint-disable-next-line max-classes-per-file
import { describe, it } from 'vitest';
import { RuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/RuntimeEnvironment';
import { Logger } from '@/application/Common/Log/Logger';
import { RuntimeEnvironmentStub } from '@tests/unit/shared/Stubs/RuntimeEnvironmentStub';
import { itIsSingleton } from '@tests/unit/shared/TestCases/SingletonTests';
import { LoggerStub } from '@tests/unit/shared/Stubs/LoggerStub';
import { ClientLoggerFactory, LoggerCreationFunction, WindowAccessor } from '@/presentation/components/Shared/Hooks/Log/ClientLoggerFactory';
describe('ClientLoggerFactory', () => {
describe('Current', () => {
describe('singleton behavior', () => {
itIsSingleton({
getter: () => ClientLoggerFactory.Current,
expectedType: ClientLoggerFactory,
});
});
});
describe('logger instantiation based on environment', () => {
const testCases: Array<{
readonly description: string,
readonly build: (
builder: ClientLoggerFactoryBuilder,
expectedLogger: Logger,
) => ClientLoggerFactory,
}> = [
{
description: 'production desktop environment',
build: (b, expectedLogger) => b
.withWindowInjectedLoggerFactory(() => expectedLogger)
.withEnvironment(new RuntimeEnvironmentStub()
.withIsRunningAsDesktopApplication(true)
.withIsNonProduction(false))
.withWindowAccessor(() => createWindowWithLogger())
.build(),
},
{
description: 'non-production desktop environment',
build: (b, expectedLogger) => b
.withWindowInjectedLoggerFactory(() => expectedLogger)
.withEnvironment(new RuntimeEnvironmentStub()
.withIsRunningAsDesktopApplication(true)
.withIsNonProduction(true))
.withWindowAccessor(() => createWindowWithLogger())
.build(),
},
{
description: 'non-production web environment',
build: (b, expectedLogger) => b
.withConsoleLoggerFactory(() => expectedLogger)
.withEnvironment(new RuntimeEnvironmentStub()
.withIsRunningAsDesktopApplication(false)
.withIsNonProduction(true))
.withWindowAccessor(() => createWindowWithLogger())
.build(),
},
{
description: 'production web environment',
build: (b, expectedLogger) => b
.withNoopLoggerFactory(() => expectedLogger)
.withEnvironment(new RuntimeEnvironmentStub()
.withIsRunningAsDesktopApplication(false)
.withIsNonProduction(false))
.withWindowAccessor(() => createWindowWithLogger())
.build(),
},
{
description: 'unit/integration tests environment',
build: (b, expectedLogger) => b
.withNoopLoggerFactory(() => expectedLogger)
.withEnvironment(new RuntimeEnvironmentStub().withIsRunningAsDesktopApplication(true))
.withWindowAccessor(() => createWindowWithLogger(null))
.build(),
},
];
testCases.forEach(({ description, build }) => {
it(`creates correct logger for ${description}`, () => {
// arrange
const expectedLogger = new LoggerStub();
const factory = build(new ClientLoggerFactoryBuilder(), expectedLogger);
// act
const { logger } = factory;
// assert
expect(logger).to.equal(expectedLogger);
});
});
});
});
function createWindowWithLogger(logger: Logger | null = new LoggerStub()): Window {
return {
log: logger,
} as unknown as Window;
}
class ClientLoggerFactoryBuilder {
private environment: RuntimeEnvironment = new RuntimeEnvironmentStub();
private windowAccessor: WindowAccessor = () => ({} as Window);
private noopLoggerFactory: LoggerCreationFunction = () => new LoggerStub();
private windowInjectedLoggerFactory: LoggerCreationFunction = () => new LoggerStub();
private consoleLoggerFactory: LoggerCreationFunction = () => new LoggerStub();
public withWindowAccessor(windowAccessor: WindowAccessor): this {
this.windowAccessor = windowAccessor;
return this;
}
public withEnvironment(environment: RuntimeEnvironment): this {
this.environment = environment;
return this;
}
public withNoopLoggerFactory(factory: LoggerCreationFunction): this {
this.noopLoggerFactory = factory;
return this;
}
public withWindowInjectedLoggerFactory(factory: LoggerCreationFunction): this {
this.windowInjectedLoggerFactory = factory;
return this;
}
public withConsoleLoggerFactory(factory: LoggerCreationFunction): this {
this.consoleLoggerFactory = factory;
return this;
}
public build(): ClientLoggerFactory {
return new TestableClientLoggerFactory(
this.environment,
this.windowAccessor,
this.noopLoggerFactory,
this.windowInjectedLoggerFactory,
this.consoleLoggerFactory,
);
}
}
class TestableClientLoggerFactory extends ClientLoggerFactory {
public constructor(
environment: RuntimeEnvironment,
windowAccessor: WindowAccessor,
noopLoggerFactory: LoggerCreationFunction,
windowInjectedLoggerFactory: LoggerCreationFunction,
consoleLoggerFactory: LoggerCreationFunction,
) {
super(
environment,
windowAccessor,
noopLoggerFactory,
windowInjectedLoggerFactory,
consoleLoggerFactory,
);
}
}