Improve desktop runtime execution tests
Test improvements: - Capture titles for all macOS windows, not just the frontmost. - Incorporate missing application log files. - Improve log clarity with enriched context. - Improve application termination on macOS by reducing grace period. - Ensure complete application termination on macOS. - Validate Vue application loading through an initial log. - Support ignoring environment-specific `stderr` errors. - Do not fail the test if working directory cannot be deleted. - Use retry pattern when installing dependencies due to network errors. Refactorings: - Migrate the test code to TypeScript. - Replace deprecated `rmdir` with `rm` for error-resistant directory removal. - Improve sanity checking by shifting from App.vue to Vue bootstrapper. - Centralize environment variable management with `EnvironmentVariables` construct. - Rename infrastructure/Environment to RuntimeEnvironment for clarity. - Isolate WindowVariables and SystemOperations from RuntimeEnvironment. - Inject logging via preloader. - Correct mislabeled RuntimeSanity tests. Configuration: - Introduce `npm run check:desktop` for simplified execution. - Omit `console.log` override due to `nodeIntegration` restrictions and reveal logging functionality using context-bridging.
This commit is contained in:
@@ -1,32 +1,37 @@
|
||||
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||
import { RuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/RuntimeEnvironment';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { getWindowInjectedSystemOperations } from './SystemOperations/WindowInjectedSystemOperations';
|
||||
|
||||
export class CodeRunner {
|
||||
constructor(
|
||||
private readonly environment = Environment.CurrentEnvironment,
|
||||
private readonly system = getWindowInjectedSystemOperations(),
|
||||
private readonly environment = RuntimeEnvironment.CurrentEnvironment,
|
||||
) {
|
||||
if (!environment.system) {
|
||||
if (!system) {
|
||||
throw new Error('missing system operations');
|
||||
}
|
||||
}
|
||||
|
||||
public async runCode(code: string, folderName: string, fileExtension: string): Promise<void> {
|
||||
const { system } = this.environment;
|
||||
const dir = system.location.combinePaths(
|
||||
system.operatingSystem.getTempDirectory(),
|
||||
const { os } = this.environment;
|
||||
const dir = this.system.location.combinePaths(
|
||||
this.system.operatingSystem.getTempDirectory(),
|
||||
folderName,
|
||||
);
|
||||
await system.fileSystem.createDirectory(dir, true);
|
||||
const filePath = system.location.combinePaths(dir, `run.${fileExtension}`);
|
||||
await system.fileSystem.writeToFile(filePath, code);
|
||||
await system.fileSystem.setFilePermissions(filePath, '755');
|
||||
const command = getExecuteCommand(filePath, this.environment);
|
||||
system.command.execute(command);
|
||||
await this.system.fileSystem.createDirectory(dir, true);
|
||||
const filePath = this.system.location.combinePaths(dir, `run.${fileExtension}`);
|
||||
await this.system.fileSystem.writeToFile(filePath, code);
|
||||
await this.system.fileSystem.setFilePermissions(filePath, '755');
|
||||
const command = getExecuteCommand(filePath, os);
|
||||
this.system.command.execute(command);
|
||||
}
|
||||
}
|
||||
|
||||
function getExecuteCommand(scriptPath: string, environment: Environment): string {
|
||||
switch (environment.os) {
|
||||
function getExecuteCommand(
|
||||
scriptPath: string,
|
||||
currentOperatingSystem: OperatingSystem,
|
||||
): string {
|
||||
switch (currentOperatingSystem) {
|
||||
case OperatingSystem.Linux:
|
||||
return `x-terminal-emulator -e '${scriptPath}'`;
|
||||
case OperatingSystem.macOS:
|
||||
@@ -37,6 +42,6 @@ function getExecuteCommand(scriptPath: string, environment: Environment): string
|
||||
case OperatingSystem.Windows:
|
||||
return scriptPath;
|
||||
default:
|
||||
throw Error(`unsupported os: ${OperatingSystem[environment.os]}`);
|
||||
throw Error(`unsupported os: ${OperatingSystem[currentOperatingSystem]}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||
|
||||
export interface IEnvironment {
|
||||
readonly isDesktop: boolean;
|
||||
readonly os: OperatingSystem | undefined;
|
||||
readonly system: ISystemOperations | undefined;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ISystemOperations } from './SystemOperations/ISystemOperations';
|
||||
|
||||
export type WindowVariables = {
|
||||
system: ISystemOperations;
|
||||
isDesktop: boolean;
|
||||
os: OperatingSystem;
|
||||
};
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
interface Window extends WindowVariables { }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { IEnvironmentVariablesFactory } from './IEnvironmentVariablesFactory';
|
||||
import { validateEnvironmentVariables } from './EnvironmentVariablesValidator';
|
||||
import { ViteEnvironmentVariables } from './Vite/ViteEnvironmentVariables';
|
||||
import { IEnvironmentVariables } from './IEnvironmentVariables';
|
||||
|
||||
export class EnvironmentVariablesFactory implements IEnvironmentVariablesFactory {
|
||||
public static readonly Current = new EnvironmentVariablesFactory();
|
||||
|
||||
public readonly instance: IEnvironmentVariables;
|
||||
|
||||
protected constructor(validator: EnvironmentVariablesValidator = validateEnvironmentVariables) {
|
||||
const environment = new ViteEnvironmentVariables();
|
||||
validator(environment);
|
||||
this.instance = environment;
|
||||
}
|
||||
}
|
||||
|
||||
export type EnvironmentVariablesValidator = typeof validateEnvironmentVariables;
|
||||
@@ -1,24 +1,24 @@
|
||||
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||
import { IEnvironmentVariables } from './IEnvironmentVariables';
|
||||
|
||||
/* Validation is externalized to keep the environment objects simple */
|
||||
export function validateMetadata(metadata: IAppMetadata): void {
|
||||
if (!metadata) {
|
||||
throw new Error('missing metadata');
|
||||
export function validateEnvironmentVariables(environment: IEnvironmentVariables): void {
|
||||
if (!environment) {
|
||||
throw new Error('missing environment');
|
||||
}
|
||||
const keyValues = capturePropertyValues(metadata);
|
||||
const keyValues = capturePropertyValues(environment);
|
||||
if (!Object.keys(keyValues).length) {
|
||||
throw new Error('Unable to capture metadata key/value pairs');
|
||||
throw new Error('Unable to capture key/value pairs');
|
||||
}
|
||||
const keysMissingValue = getMissingMetadataKeys(keyValues);
|
||||
const keysMissingValue = getKeysMissingValues(keyValues);
|
||||
if (keysMissingValue.length > 0) {
|
||||
throw new Error(`Metadata keys missing: ${keysMissingValue.join(', ')}`);
|
||||
throw new Error(`Environment keys missing: ${keysMissingValue.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
function getMissingMetadataKeys(keyValuePairs: Record<string, unknown>): string[] {
|
||||
function getKeysMissingValues(keyValuePairs: Record<string, unknown>): string[] {
|
||||
return Object.entries(keyValuePairs)
|
||||
.reduce((acc, [key, value]) => {
|
||||
if (!value) {
|
||||
if (!value && typeof value !== 'boolean') {
|
||||
acc.push(key);
|
||||
}
|
||||
return acc;
|
||||
@@ -1,8 +1,5 @@
|
||||
/**
|
||||
* Represents essential metadata about the application.
|
||||
*
|
||||
* Designed to decouple the process of retrieving metadata
|
||||
* (e.g., from the build environment) from the rest of the application.
|
||||
*/
|
||||
export interface IAppMetadata {
|
||||
readonly version: string;
|
||||
@@ -0,0 +1,9 @@
|
||||
import { IAppMetadata } from './IAppMetadata';
|
||||
|
||||
/**
|
||||
* Designed to decouple the process of retrieving environment variables
|
||||
* (e.g., from the build environment) from the rest of the application.
|
||||
*/
|
||||
export interface IEnvironmentVariables extends IAppMetadata {
|
||||
readonly isNonProduction: boolean;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { IEnvironmentVariables } from './IEnvironmentVariables';
|
||||
|
||||
export interface IEnvironmentVariablesFactory {
|
||||
readonly instance: IEnvironmentVariables;
|
||||
}
|
||||
@@ -1,8 +1,13 @@
|
||||
// Only variables prefixed with VITE_ are exposed to Vite-processed code
|
||||
export const VITE_ENVIRONMENT_KEYS = {
|
||||
export const VITE_USER_DEFINED_ENVIRONMENT_KEYS = {
|
||||
VERSION: 'VITE_APP_VERSION',
|
||||
NAME: 'VITE_APP_NAME',
|
||||
SLOGAN: 'VITE_APP_SLOGAN',
|
||||
REPOSITORY_URL: 'VITE_APP_REPOSITORY_URL',
|
||||
HOMEPAGE_URL: 'VITE_APP_HOMEPAGE_URL',
|
||||
} as const;
|
||||
|
||||
export const VITE_ENVIRONMENT_KEYS = {
|
||||
...VITE_USER_DEFINED_ENVIRONMENT_KEYS,
|
||||
DEV: 'DEV',
|
||||
} as const;
|
||||
@@ -1,9 +1,9 @@
|
||||
import { IAppMetadata } from '../IAppMetadata';
|
||||
import { IEnvironmentVariables } from '../IEnvironmentVariables';
|
||||
|
||||
/**
|
||||
* Provides the application's metadata using Vite's environment variables.
|
||||
* Provides the application's environment variables.
|
||||
*/
|
||||
export class ViteAppMetadata implements IAppMetadata {
|
||||
export class ViteEnvironmentVariables implements IEnvironmentVariables {
|
||||
// Ensure the use of import.meta.env prefix for the following properties.
|
||||
// Vue will replace these statically during production builds.
|
||||
|
||||
@@ -26,4 +26,8 @@ export class ViteAppMetadata implements IAppMetadata {
|
||||
public get homepageUrl(): string {
|
||||
return import.meta.env.VITE_APP_HOMEPAGE_URL;
|
||||
}
|
||||
|
||||
public get isNonProduction(): boolean {
|
||||
return import.meta.env.DEV;
|
||||
}
|
||||
}
|
||||
13
src/infrastructure/Log/ConsoleLogger.ts
Normal file
13
src/infrastructure/Log/ConsoleLogger.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { ILogger } from './ILogger';
|
||||
|
||||
export class ConsoleLogger implements ILogger {
|
||||
constructor(private readonly globalConsole: Partial<Console> = console) {
|
||||
if (!globalConsole) {
|
||||
throw new Error('missing console');
|
||||
}
|
||||
}
|
||||
|
||||
public info(...params: unknown[]): void {
|
||||
this.globalConsole.info(...params);
|
||||
}
|
||||
}
|
||||
12
src/infrastructure/Log/ElectronLogger.ts
Normal file
12
src/infrastructure/Log/ElectronLogger.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { ElectronLog } from 'electron-log';
|
||||
import { ILogger } from './ILogger';
|
||||
|
||||
// Using plain-function rather than class so it can be used in Electron's context-bridging.
|
||||
export function createElectronLogger(logger: Partial<ElectronLog>): ILogger {
|
||||
if (!logger) {
|
||||
throw new Error('missing logger');
|
||||
}
|
||||
return {
|
||||
info: (...params) => logger.info(...params),
|
||||
};
|
||||
}
|
||||
3
src/infrastructure/Log/ILogger.ts
Normal file
3
src/infrastructure/Log/ILogger.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface ILogger {
|
||||
info (...params: unknown[]): void;
|
||||
}
|
||||
5
src/infrastructure/Log/ILoggerFactory.ts
Normal file
5
src/infrastructure/Log/ILoggerFactory.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { ILogger } from './ILogger';
|
||||
|
||||
export interface ILoggerFactory {
|
||||
readonly logger: ILogger;
|
||||
}
|
||||
5
src/infrastructure/Log/NoopLogger.ts
Normal file
5
src/infrastructure/Log/NoopLogger.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { ILogger } from './ILogger';
|
||||
|
||||
export class NoopLogger implements ILogger {
|
||||
public info(): void { /* NOOP */ }
|
||||
}
|
||||
20
src/infrastructure/Log/WindowInjectedLogger.ts
Normal file
20
src/infrastructure/Log/WindowInjectedLogger.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { WindowVariables } from '../WindowVariables/WindowVariables';
|
||||
import { ILogger } from './ILogger';
|
||||
|
||||
export class WindowInjectedLogger implements ILogger {
|
||||
private readonly logger: ILogger;
|
||||
|
||||
constructor(windowVariables: WindowVariables = window) {
|
||||
if (!windowVariables) {
|
||||
throw new Error('missing window');
|
||||
}
|
||||
if (!windowVariables.log) {
|
||||
throw new Error('missing log');
|
||||
}
|
||||
this.logger = windowVariables.log;
|
||||
}
|
||||
|
||||
public info(...params: unknown[]): void {
|
||||
this.logger.info(...params);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { IAppMetadata } from './IAppMetadata';
|
||||
import { IAppMetadataFactory } from './IAppMetadataFactory';
|
||||
import { validateMetadata } from './MetadataValidator';
|
||||
import { ViteAppMetadata } from './Vite/ViteAppMetadata';
|
||||
|
||||
export class AppMetadataFactory implements IAppMetadataFactory {
|
||||
public static readonly Current = new AppMetadataFactory();
|
||||
|
||||
public readonly instance: IAppMetadata;
|
||||
|
||||
protected constructor(validator: MetadataValidator = validateMetadata) {
|
||||
const metadata = new ViteAppMetadata();
|
||||
validator(metadata);
|
||||
this.instance = metadata;
|
||||
}
|
||||
}
|
||||
|
||||
export type MetadataValidator = typeof validateMetadata;
|
||||
@@ -1,5 +0,0 @@
|
||||
import { IAppMetadata } from './IAppMetadata';
|
||||
|
||||
export interface IAppMetadataFactory {
|
||||
readonly instance: IAppMetadata;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
|
||||
export interface IRuntimeEnvironment {
|
||||
readonly isDesktop: boolean;
|
||||
readonly os: OperatingSystem | undefined;
|
||||
readonly isNonProduction: boolean;
|
||||
}
|
||||
@@ -1,29 +1,29 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||
import { WindowVariables } from '@/infrastructure/WindowVariables/WindowVariables';
|
||||
import { IEnvironmentVariables } from '@/infrastructure/EnvironmentVariables/IEnvironmentVariables';
|
||||
import { EnvironmentVariablesFactory } from '@/infrastructure/EnvironmentVariables/EnvironmentVariablesFactory';
|
||||
import { BrowserOsDetector } from './BrowserOs/BrowserOsDetector';
|
||||
import { IBrowserOsDetector } from './BrowserOs/IBrowserOsDetector';
|
||||
import { IEnvironment } from './IEnvironment';
|
||||
import { WindowVariables } from './WindowVariables';
|
||||
import { validateWindowVariables } from './WindowVariablesValidator';
|
||||
import { IRuntimeEnvironment } from './IRuntimeEnvironment';
|
||||
|
||||
export class Environment implements IEnvironment {
|
||||
public static readonly CurrentEnvironment: IEnvironment = new Environment(window);
|
||||
export class RuntimeEnvironment implements IRuntimeEnvironment {
|
||||
public static readonly CurrentEnvironment: IRuntimeEnvironment = new RuntimeEnvironment(window);
|
||||
|
||||
public readonly isDesktop: boolean;
|
||||
|
||||
public readonly os: OperatingSystem | undefined;
|
||||
|
||||
public readonly system: ISystemOperations | undefined;
|
||||
public readonly isNonProduction: boolean;
|
||||
|
||||
protected constructor(
|
||||
window: Partial<Window>,
|
||||
environmentVariables: IEnvironmentVariables = EnvironmentVariablesFactory.Current.instance,
|
||||
browserOsDetector: IBrowserOsDetector = new BrowserOsDetector(),
|
||||
windowValidator: WindowValidator = validateWindowVariables,
|
||||
) {
|
||||
if (!window) {
|
||||
throw new Error('missing window');
|
||||
}
|
||||
windowValidator(window);
|
||||
this.isNonProduction = environmentVariables.isNonProduction;
|
||||
this.isDesktop = isDesktop(window);
|
||||
if (this.isDesktop) {
|
||||
this.os = window?.os;
|
||||
@@ -34,7 +34,6 @@ export class Environment implements IEnvironment {
|
||||
this.os = browserOsDetector.detect(userAgent);
|
||||
}
|
||||
}
|
||||
this.system = window?.system;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,5 +44,3 @@ function getUserAgent(window: Partial<Window>): string {
|
||||
function isDesktop(window: Partial<WindowVariables>): boolean {
|
||||
return window?.isDesktop === true;
|
||||
}
|
||||
|
||||
export type WindowValidator = typeof validateWindowVariables;
|
||||
@@ -1,4 +1,4 @@
|
||||
export interface ISanityCheckOptions {
|
||||
readonly validateMetadata: boolean;
|
||||
readonly validateEnvironment: boolean;
|
||||
readonly validateEnvironmentVariables: boolean;
|
||||
readonly validateWindowVariables: boolean;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { ISanityCheckOptions } from './Common/ISanityCheckOptions';
|
||||
import { ISanityValidator } from './Common/ISanityValidator';
|
||||
import { MetadataValidator } from './Validators/MetadataValidator';
|
||||
import { EnvironmentVariablesValidator } from './Validators/EnvironmentVariablesValidator';
|
||||
|
||||
const DefaultSanityValidators: ISanityValidator[] = [
|
||||
new MetadataValidator(),
|
||||
new EnvironmentVariablesValidator(),
|
||||
];
|
||||
|
||||
/* Helps to fail-fast on errors */
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
|
||||
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
|
||||
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
|
||||
|
||||
export class EnvironmentValidator extends FactoryValidator<IEnvironment> {
|
||||
constructor(factory: FactoryFunction<IEnvironment> = () => Environment.CurrentEnvironment) {
|
||||
super(factory);
|
||||
}
|
||||
|
||||
public override name = 'environment';
|
||||
|
||||
public override shouldValidate(options: ISanityCheckOptions): boolean {
|
||||
return options.validateEnvironment;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { IEnvironmentVariables } from '@/infrastructure/EnvironmentVariables/IEnvironmentVariables';
|
||||
import { EnvironmentVariablesFactory } from '@/infrastructure/EnvironmentVariables/EnvironmentVariablesFactory';
|
||||
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
|
||||
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
|
||||
|
||||
export class EnvironmentVariablesValidator extends FactoryValidator<IEnvironmentVariables> {
|
||||
constructor(
|
||||
factory: FactoryFunction<IEnvironmentVariables> = () => {
|
||||
return EnvironmentVariablesFactory.Current.instance;
|
||||
},
|
||||
) {
|
||||
super(factory);
|
||||
}
|
||||
|
||||
public override name = 'environment variables';
|
||||
|
||||
public override shouldValidate(options: ISanityCheckOptions): boolean {
|
||||
return options.validateEnvironmentVariables;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
|
||||
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
|
||||
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
|
||||
|
||||
export class MetadataValidator extends FactoryValidator<IAppMetadata> {
|
||||
constructor(factory: FactoryFunction<IAppMetadata> = () => AppMetadataFactory.Current.instance) {
|
||||
super(factory);
|
||||
}
|
||||
|
||||
public override name = 'metadata';
|
||||
|
||||
public override shouldValidate(options: ISanityCheckOptions): boolean {
|
||||
return options.validateMetadata;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { WindowVariables } from '@/infrastructure/WindowVariables/WindowVariables';
|
||||
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
|
||||
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
|
||||
|
||||
export class WindowVariablesValidator extends FactoryValidator<WindowVariables> {
|
||||
constructor(factory: FactoryFunction<WindowVariables> = () => window) {
|
||||
super(factory);
|
||||
}
|
||||
|
||||
public override name = 'window variables';
|
||||
|
||||
public override shouldValidate(options: ISanityCheckOptions): boolean {
|
||||
return options.validateWindowVariables;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { WindowVariables } from '../WindowVariables/WindowVariables';
|
||||
import { ISystemOperations } from './ISystemOperations';
|
||||
|
||||
export function getWindowInjectedSystemOperations(
|
||||
windowVariables: Partial<WindowVariables> = window,
|
||||
): ISystemOperations {
|
||||
if (!windowVariables) {
|
||||
throw new Error('missing window');
|
||||
}
|
||||
if (!windowVariables.system) {
|
||||
throw new Error('missing system');
|
||||
}
|
||||
return windowVariables.system;
|
||||
}
|
||||
11
src/infrastructure/WindowVariables/WindowVariables.ts
Normal file
11
src/infrastructure/WindowVariables/WindowVariables.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ISystemOperations } from '@/infrastructure/SystemOperations/ISystemOperations';
|
||||
import { ILogger } from '@/infrastructure/Log/ILogger';
|
||||
|
||||
/* Primary entry point for platform-specific injections */
|
||||
export interface WindowVariables {
|
||||
readonly system: ISystemOperations;
|
||||
readonly isDesktop: boolean;
|
||||
readonly os: OperatingSystem;
|
||||
readonly log: ILogger;
|
||||
}
|
||||
@@ -6,11 +6,8 @@ import { WindowVariables } from './WindowVariables';
|
||||
* Checks for consistency in runtime environment properties injected by Electron preloader.
|
||||
*/
|
||||
export function validateWindowVariables(variables: Partial<WindowVariables>) {
|
||||
if (!variables) {
|
||||
throw new Error('missing variables');
|
||||
}
|
||||
if (!isObject(variables)) {
|
||||
throw new Error(`window is not an object but ${typeof variables}`);
|
||||
throw new Error('window is not an object');
|
||||
}
|
||||
const errors = [...testEveryProperty(variables)];
|
||||
if (errors.length > 0) {
|
||||
@@ -25,6 +22,7 @@ function* testEveryProperty(variables: Partial<WindowVariables>): Iterable<strin
|
||||
os: testOperatingSystem(variables.os),
|
||||
isDesktop: testIsDesktop(variables.isDesktop),
|
||||
system: testSystem(variables),
|
||||
log: testLogger(variables),
|
||||
};
|
||||
|
||||
for (const [propertyName, testResult] of Object.entries(tests)) {
|
||||
@@ -47,11 +45,18 @@ function testOperatingSystem(os: unknown): boolean {
|
||||
.includes(os);
|
||||
}
|
||||
|
||||
function testLogger(variables: Partial<WindowVariables>): boolean {
|
||||
if (!variables.isDesktop) {
|
||||
return true;
|
||||
}
|
||||
return isObject(variables.log);
|
||||
}
|
||||
|
||||
function testSystem(variables: Partial<WindowVariables>): boolean {
|
||||
if (!variables.isDesktop) {
|
||||
return true;
|
||||
}
|
||||
return variables.system !== undefined && isObject(variables.system);
|
||||
return isObject(variables.system);
|
||||
}
|
||||
|
||||
function testIsDesktop(isDesktop: unknown): boolean {
|
||||
@@ -70,7 +75,7 @@ function isBoolean(variable: unknown): variable is boolean {
|
||||
}
|
||||
|
||||
function isObject(variable: unknown): variable is object {
|
||||
return typeof variable === 'object'
|
||||
&& variable !== null // the data type of null is an object
|
||||
return Boolean(variable) // the data type of null is an object
|
||||
&& typeof variable === 'object'
|
||||
&& !Array.isArray(variable);
|
||||
}
|
||||
6
src/infrastructure/WindowVariables/window-variables.d.ts
vendored
Normal file
6
src/infrastructure/WindowVariables/window-variables.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
import { WindowVariables } from './WindowVariables';
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
interface Window extends WindowVariables { }
|
||||
}
|
||||
Reference in New Issue
Block a user