Files
privacy.sexy/tests/unit/infrastructure/RuntimeEnvironment/Browser/BrowserRuntimeEnvironment.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

173 lines
5.9 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { BrowserOsDetector } from '@/infrastructure/RuntimeEnvironment/Browser/BrowserOs/BrowserOsDetector';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { BrowserRuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/Browser/BrowserRuntimeEnvironment';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
import { BrowserOsDetectorStub } from '@tests/unit/shared/Stubs/BrowserOsDetectorStub';
import { IEnvironmentVariables } from '@/infrastructure/EnvironmentVariables/IEnvironmentVariables';
import { EnvironmentVariablesStub } from '@tests/unit/shared/Stubs/EnvironmentVariablesStub';
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
describe('BrowserRuntimeEnvironment', () => {
describe('ctor', () => {
describe('throws if window is absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing window';
const absentWindow = absentValue;
// act
const act = () => new BrowserRuntimeEnvironmentBuilder()
.withWindow(absentWindow as never)
.build();
// assert
expect(act).to.throw(expectedError);
});
});
it('uses browser OS detector with current touch support', () => {
// arrange
const expectedTouchSupport = true;
const osDetector = new BrowserOsDetectorStub();
const window = { os: undefined, navigator: { userAgent: 'Forcing touch detection' } } as Partial<Window>;
// act
new BrowserRuntimeEnvironmentBuilder()
.withWindow(window)
.withBrowserOsDetector(osDetector)
.withTouchSupported(expectedTouchSupport)
.build();
// assert
const actualCall = osDetector.callHistory.find((c) => c.methodName === 'detect');
expectExists(actualCall);
const [{ isTouchSupported: actualTouchSupport }] = actualCall.args;
expect(actualTouchSupport).to.equal(expectedTouchSupport);
});
});
describe('isRunningAsDesktopApplication', () => {
it('returns true when window property `isRunningAsDesktopApplication` is true', () => {
// arrange
const expectedValue = true;
const desktopWindow: Partial<Window> = {
isRunningAsDesktopApplication: true,
};
// act
const sut = new BrowserRuntimeEnvironmentBuilder()
.withWindow(desktopWindow)
.build();
// assert
expect(sut.isRunningAsDesktopApplication).to.equal(expectedValue);
});
it('returns false when window property `isRunningAsDesktopApplication` is undefined', () => {
// arrange
const expectedValue = false;
const browserWindow: Partial<Window> = {
isRunningAsDesktopApplication: undefined,
};
// act
const sut = new BrowserRuntimeEnvironmentBuilder()
.withWindow(browserWindow)
.build();
// assert
expect(sut.isRunningAsDesktopApplication).to.equal(expectedValue);
});
});
describe('os', () => {
it('returns undefined if user agent is missing', () => {
// arrange
const expected = undefined;
const browserDetectorMock: BrowserOsDetector = {
detect: () => {
throw new Error('should not reach here');
},
};
const sut = new BrowserRuntimeEnvironmentBuilder()
.withBrowserOsDetector(browserDetectorMock)
.build();
// act
const actual = sut.os;
// assert
expect(actual).to.equal(expected);
});
it('gets browser os from BrowserOsDetector', () => {
// arrange
const givenUserAgent = 'testUserAgent';
const expected = OperatingSystem.macOS;
const windowWithUserAgent = {
navigator: {
userAgent: givenUserAgent,
},
};
const browserDetectorMock: BrowserOsDetector = {
detect: (environment) => {
if (environment.userAgent !== givenUserAgent) {
throw new Error('Unexpected user agent');
}
return expected;
},
};
// act
const sut = new BrowserRuntimeEnvironmentBuilder()
.withWindow(windowWithUserAgent as Partial<Window>)
.withBrowserOsDetector(browserDetectorMock)
.build();
const actual = sut.os;
// assert
expect(actual).to.equal(expected);
});
});
describe('isNonProduction', () => {
[true, false].forEach((value) => {
it(`sets ${value} from environment variables`, () => {
// arrange
const expectedValue = value;
const environment = new EnvironmentVariablesStub()
.withIsNonProduction(expectedValue);
// act
const sut = new BrowserRuntimeEnvironmentBuilder()
.withEnvironmentVariables(environment)
.build();
// assert
const actualValue = sut.isNonProduction;
expect(actualValue).to.equal(expectedValue);
});
});
});
});
class BrowserRuntimeEnvironmentBuilder {
private window: Partial<Window> = {};
private browserOsDetector: BrowserOsDetector = new BrowserOsDetectorStub();
private environmentVariables: IEnvironmentVariables = new EnvironmentVariablesStub();
private isTouchSupported = false;
public withEnvironmentVariables(environmentVariables: IEnvironmentVariables): this {
this.environmentVariables = environmentVariables;
return this;
}
public withWindow(window: Partial<Window>): this {
this.window = window;
return this;
}
public withBrowserOsDetector(browserOsDetector: BrowserOsDetector): this {
this.browserOsDetector = browserOsDetector;
return this;
}
public withTouchSupported(isTouchSupported: boolean): this {
this.isTouchSupported = isTouchSupported;
return this;
}
public build() {
return new BrowserRuntimeEnvironment(
this.window,
this.environmentVariables,
this.browserOsDetector,
() => this.isTouchSupported,
);
}
}