Improve desktop security by isolating Electron
Enable `contextIsolation` in Electron to securely expose a limited set of Node.js APIs to the renderer process. It: 1. Isolates renderer and main process contexts. It ensures that the powerful main process functions aren't directly accessible from renderer process(es), adding a security boundary. 2. Mitigates remote exploitation risks. By isolating contexts, potential malicious code injections in the renderer can't directly reach and compromise the main process. 3. Reduces attack surface. 4. Protect against prototype pollution: It prevents tampering of JavaScript object prototypes in one context from affecting another context, improving app reliability and security. Supporting changes include: - Extract environment and system operations classes to the infrastructure layer. This removes node dependencies from core domain and application code. - Introduce `ISystemOperations` to encapsulate OS interactions. Use it from `CodeRunner` to isolate node API usage. - Add a preloader script to inject validated environment variables into renderer context. This keeps Electron integration details encapsulated. - Add new sanity check to fail fast on issues with preloader injected variables. - Improve test coverage of runtime sanity checks and environment components. Move validation logic into separate classes for Single Responsibility. - Improve absent value test case generation.
This commit is contained in:
@@ -57,7 +57,7 @@ jobs:
|
|||||||
-
|
-
|
||||||
name: Test
|
name: Test
|
||||||
shell: bash
|
shell: bash
|
||||||
run: node scripts/check-desktop-runtime-errors --screenshot
|
run: node ./scripts/check-desktop-runtime-errors --screenshot
|
||||||
-
|
-
|
||||||
name: Upload screenshot
|
name: Upload screenshot
|
||||||
if: always() # Run even if previous step fails
|
if: always() # Run even if previous step fails
|
||||||
|
|||||||
@@ -15,11 +15,23 @@ Application is
|
|||||||
|
|
||||||
Application uses highly decoupled models & services in different DDD layers:
|
Application uses highly decoupled models & services in different DDD layers:
|
||||||
|
|
||||||
- presentation layer (see [presentation.md](./presentation.md)),
|
**Application layer** (see [application.md](./application.md)):
|
||||||
- application layer (see [application.md](./application.md)),
|
|
||||||
- and domain layer.
|
|
||||||
|
|
||||||
Application layer depends on and consumes domain layer. [Presentation layer](./presentation.md) consumes and depends on application layer along with domain layer. Application and presentation layers can communicate through domain model.
|
- Coordinates application activities and consumes the domain layer.
|
||||||
|
|
||||||
|
**Presentation layer** (see [presentation.md](./presentation.md)):
|
||||||
|
|
||||||
|
- Handles UI/UX, consumes both the application and domain layers.
|
||||||
|
- May communicate directly with the infrastructure layer for technical needs, but avoids domain logic.
|
||||||
|
|
||||||
|
**Domain layer**:
|
||||||
|
|
||||||
|
- Serves as the system's core and central truth.
|
||||||
|
- Facilitates communication between the application and presentation layers through the domain model.
|
||||||
|
|
||||||
|
**Infrastructure layer**:
|
||||||
|
|
||||||
|
- Manages technical implementations without dependencies on other layers or domain knowledge.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ export default defineConfig({
|
|||||||
input: {
|
input: {
|
||||||
index: WEB_INDEX_HTML_PATH,
|
index: WEB_INDEX_HTML_PATH,
|
||||||
},
|
},
|
||||||
external: ['os', 'child_process', 'fs', 'path'],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -9,3 +9,7 @@ export type PropertyKeys<T> = {
|
|||||||
|
|
||||||
export type ConstructorArguments<T> =
|
export type ConstructorArguments<T> =
|
||||||
T extends new (...args: infer U) => unknown ? U : never;
|
T extends new (...args: infer U) => unknown ? U : never;
|
||||||
|
|
||||||
|
export type FunctionKeys<T> = {
|
||||||
|
[K in keyof T]: T[K] extends (...args: unknown[]) => unknown ? K : never;
|
||||||
|
}[keyof T];
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { IApplicationContext } from '@/application/Context/IApplicationContext';
|
import { IApplicationContext } from '@/application/Context/IApplicationContext';
|
||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
import { IApplication } from '@/domain/IApplication';
|
import { IApplication } from '@/domain/IApplication';
|
||||||
import { Environment } from '../Environment/Environment';
|
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||||
import { IEnvironment } from '../Environment/IEnvironment';
|
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
|
||||||
import { IApplicationFactory } from '../IApplicationFactory';
|
import { IApplicationFactory } from '../IApplicationFactory';
|
||||||
import { ApplicationFactory } from '../ApplicationFactory';
|
import { ApplicationFactory } from '../ApplicationFactory';
|
||||||
import { ApplicationContext } from './ApplicationContext';
|
import { ApplicationContext } from './ApplicationContext';
|
||||||
|
|||||||
@@ -1,89 +0,0 @@
|
|||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
|
||||||
import { BrowserOsDetector } from './BrowserOs/BrowserOsDetector';
|
|
||||||
import { IBrowserOsDetector } from './BrowserOs/IBrowserOsDetector';
|
|
||||||
import { IEnvironment } from './IEnvironment';
|
|
||||||
|
|
||||||
export interface IEnvironmentVariables {
|
|
||||||
readonly window: Window & typeof globalThis;
|
|
||||||
readonly process: NodeJS.Process;
|
|
||||||
readonly navigator: Navigator;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Environment implements IEnvironment {
|
|
||||||
public static readonly CurrentEnvironment: IEnvironment = new Environment({
|
|
||||||
window,
|
|
||||||
process: typeof process !== 'undefined' ? process /* electron only */ : undefined,
|
|
||||||
navigator,
|
|
||||||
});
|
|
||||||
|
|
||||||
public readonly isDesktop: boolean;
|
|
||||||
|
|
||||||
public readonly os: OperatingSystem;
|
|
||||||
|
|
||||||
protected constructor(
|
|
||||||
variables: IEnvironmentVariables,
|
|
||||||
browserOsDetector: IBrowserOsDetector = new BrowserOsDetector(),
|
|
||||||
) {
|
|
||||||
if (!variables) {
|
|
||||||
throw new Error('variables is null or empty');
|
|
||||||
}
|
|
||||||
this.isDesktop = isDesktop(variables);
|
|
||||||
if (this.isDesktop) {
|
|
||||||
this.os = getDesktopOsType(getProcessPlatform(variables));
|
|
||||||
} else {
|
|
||||||
const userAgent = getUserAgent(variables);
|
|
||||||
this.os = !userAgent ? undefined : browserOsDetector.detect(userAgent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getUserAgent(variables: IEnvironmentVariables): string {
|
|
||||||
if (!variables.window || !variables.window.navigator) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return variables.window.navigator.userAgent;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getProcessPlatform(variables: IEnvironmentVariables): string {
|
|
||||||
if (!variables.process || !variables.process.platform) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return variables.process.platform;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getDesktopOsType(processPlatform: string): OperatingSystem | undefined {
|
|
||||||
// https://nodejs.org/api/process.html#process_process_platform
|
|
||||||
switch (processPlatform) {
|
|
||||||
case 'darwin':
|
|
||||||
return OperatingSystem.macOS;
|
|
||||||
case 'win32':
|
|
||||||
return OperatingSystem.Windows;
|
|
||||||
case 'linux':
|
|
||||||
return OperatingSystem.Linux;
|
|
||||||
default:
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isDesktop(variables: IEnvironmentVariables): boolean {
|
|
||||||
// More: https://github.com/electron/electron/issues/2288
|
|
||||||
// Renderer process
|
|
||||||
if (variables.window
|
|
||||||
&& variables.window.process
|
|
||||||
&& variables.window.process.type === 'renderer') {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// Main process
|
|
||||||
if (variables.process
|
|
||||||
&& variables.process.versions
|
|
||||||
&& Boolean(variables.process.versions.electron)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// Detect the user agent when the `nodeIntegration` option is set to true
|
|
||||||
if (variables.navigator
|
|
||||||
&& variables.navigator.userAgent
|
|
||||||
&& variables.navigator.userAgent.includes('Electron')) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
|
||||||
|
|
||||||
export interface IEnvironment {
|
|
||||||
readonly isDesktop: boolean;
|
|
||||||
readonly os: OperatingSystem;
|
|
||||||
}
|
|
||||||
@@ -14,7 +14,7 @@ import { parseCategoryCollection } from './CategoryCollectionParser';
|
|||||||
export function parseApplication(
|
export function parseApplication(
|
||||||
categoryParser = parseCategoryCollection,
|
categoryParser = parseCategoryCollection,
|
||||||
informationParser = parseProjectInformation,
|
informationParser = parseProjectInformation,
|
||||||
metadata: IAppMetadata = AppMetadataFactory.Current,
|
metadata: IAppMetadata = AppMetadataFactory.Current.instance,
|
||||||
collectionsData = PreParsedCollections,
|
collectionsData = PreParsedCollections,
|
||||||
): IApplication {
|
): IApplication {
|
||||||
validateCollectionsData(collectionsData);
|
validateCollectionsData(collectionsData);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { ConstructorArguments } from '@/TypeHelpers';
|
|||||||
|
|
||||||
export function
|
export function
|
||||||
parseProjectInformation(
|
parseProjectInformation(
|
||||||
metadata: IAppMetadata = AppMetadataFactory.Current,
|
metadata: IAppMetadata = AppMetadataFactory.Current.instance,
|
||||||
createProjectInformation: ProjectInformationFactory = (
|
createProjectInformation: ProjectInformationFactory = (
|
||||||
...args
|
...args
|
||||||
) => new ProjectInformation(...args),
|
) => new ProjectInformation(...args),
|
||||||
|
|||||||
@@ -1,25 +1,27 @@
|
|||||||
import os from 'os';
|
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||||
import path from 'path';
|
|
||||||
import fs from 'fs';
|
|
||||||
import child_process from 'child_process';
|
|
||||||
import { Environment } from '@/application/Environment/Environment';
|
|
||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
|
|
||||||
export class CodeRunner {
|
export class CodeRunner {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly node = getNodeJs(),
|
|
||||||
private readonly environment = Environment.CurrentEnvironment,
|
private readonly environment = Environment.CurrentEnvironment,
|
||||||
) {
|
) {
|
||||||
|
if (!environment.system) {
|
||||||
|
throw new Error('missing system operations');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async runCode(code: string, folderName: string, fileExtension: string): Promise<void> {
|
public async runCode(code: string, folderName: string, fileExtension: string): Promise<void> {
|
||||||
const dir = this.node.path.join(this.node.os.tmpdir(), folderName);
|
const { system } = this.environment;
|
||||||
await this.node.fs.promises.mkdir(dir, { recursive: true });
|
const dir = system.location.combinePaths(
|
||||||
const filePath = this.node.path.join(dir, `run.${fileExtension}`);
|
system.operatingSystem.getTempDirectory(),
|
||||||
await this.node.fs.promises.writeFile(filePath, code);
|
folderName,
|
||||||
await this.node.fs.promises.chmod(filePath, '755');
|
);
|
||||||
|
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);
|
const command = getExecuteCommand(filePath, this.environment);
|
||||||
this.node.child_process.exec(command);
|
system.command.execute(command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,43 +40,3 @@ function getExecuteCommand(scriptPath: string, environment: Environment): string
|
|||||||
throw Error(`unsupported os: ${OperatingSystem[environment.os]}`);
|
throw Error(`unsupported os: ${OperatingSystem[environment.os]}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNodeJs(): INodeJs {
|
|
||||||
return {
|
|
||||||
os, path, fs, child_process,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface INodeJs {
|
|
||||||
os: INodeOs;
|
|
||||||
path: INodePath;
|
|
||||||
fs: INodeFs;
|
|
||||||
// eslint-disable-next-line camelcase
|
|
||||||
child_process: INodeChildProcess;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface INodeOs {
|
|
||||||
tmpdir(): string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface INodePath {
|
|
||||||
join(...paths: string[]): string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface INodeChildProcess {
|
|
||||||
exec(command: string): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface INodeFs {
|
|
||||||
readonly promises: INodeFsPromises;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface INodeFsPromisesMakeDirectoryOptions {
|
|
||||||
recursive?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface INodeFsPromises { // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v13/fs.d.ts
|
|
||||||
chmod(path: string, mode: string | number): Promise<void>;
|
|
||||||
mkdir(path: string, options: INodeFsPromisesMakeDirectoryOptions): Promise<string>;
|
|
||||||
writeFile(path: string, data: string): Promise<void>;
|
|
||||||
}
|
|
||||||
|
|||||||
49
src/infrastructure/Environment/Environment.ts
Normal file
49
src/infrastructure/Environment/Environment.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
|
import { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||||
|
import { BrowserOsDetector } from './BrowserOs/BrowserOsDetector';
|
||||||
|
import { IBrowserOsDetector } from './BrowserOs/IBrowserOsDetector';
|
||||||
|
import { IEnvironment } from './IEnvironment';
|
||||||
|
import { WindowVariables } from './WindowVariables';
|
||||||
|
import { validateWindowVariables } from './WindowVariablesValidator';
|
||||||
|
|
||||||
|
export class Environment implements IEnvironment {
|
||||||
|
public static readonly CurrentEnvironment: IEnvironment = new Environment(window);
|
||||||
|
|
||||||
|
public readonly isDesktop: boolean;
|
||||||
|
|
||||||
|
public readonly os: OperatingSystem | undefined;
|
||||||
|
|
||||||
|
public readonly system: ISystemOperations | undefined;
|
||||||
|
|
||||||
|
protected constructor(
|
||||||
|
window: Partial<Window>,
|
||||||
|
browserOsDetector: IBrowserOsDetector = new BrowserOsDetector(),
|
||||||
|
windowValidator: WindowValidator = validateWindowVariables,
|
||||||
|
) {
|
||||||
|
if (!window) {
|
||||||
|
throw new Error('missing window');
|
||||||
|
}
|
||||||
|
windowValidator(window);
|
||||||
|
this.isDesktop = isDesktop(window);
|
||||||
|
if (this.isDesktop) {
|
||||||
|
this.os = window?.os;
|
||||||
|
} else {
|
||||||
|
this.os = undefined;
|
||||||
|
const userAgent = getUserAgent(window);
|
||||||
|
if (userAgent) {
|
||||||
|
this.os = browserOsDetector.detect(userAgent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.system = window?.system;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getUserAgent(window: Partial<Window>): string {
|
||||||
|
return window?.navigator?.userAgent;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDesktop(window: Partial<WindowVariables>): boolean {
|
||||||
|
return window?.isDesktop === true;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type WindowValidator = typeof validateWindowVariables;
|
||||||
8
src/infrastructure/Environment/IEnvironment.ts
Normal file
8
src/infrastructure/Environment/IEnvironment.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
export interface ISystemOperations {
|
||||||
|
readonly operatingSystem: IOperatingSystemOps;
|
||||||
|
readonly location: ILocationOps;
|
||||||
|
readonly fileSystem: IFileSystemOps;
|
||||||
|
readonly command: ICommandOps;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IOperatingSystemOps {
|
||||||
|
getTempDirectory(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ILocationOps {
|
||||||
|
combinePaths(...pathSegments: string[]): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICommandOps {
|
||||||
|
execute(command: string): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IFileSystemOps {
|
||||||
|
setFilePermissions(filePath: string, mode: string | number): Promise<void>;
|
||||||
|
createDirectory(directoryPath: string, isRecursive?: boolean): Promise<string>;
|
||||||
|
writeToFile(filePath: string, data: string): Promise<void>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import { tmpdir } from 'os';
|
||||||
|
import { join } from 'path';
|
||||||
|
import { chmod, mkdir, writeFile } from 'fs/promises';
|
||||||
|
import { exec } from 'child_process';
|
||||||
|
import { ISystemOperations } from './ISystemOperations';
|
||||||
|
|
||||||
|
export function createNodeSystemOperations(): ISystemOperations {
|
||||||
|
return {
|
||||||
|
operatingSystem: {
|
||||||
|
getTempDirectory: () => tmpdir(),
|
||||||
|
},
|
||||||
|
location: {
|
||||||
|
combinePaths: (...pathSegments) => join(...pathSegments),
|
||||||
|
},
|
||||||
|
fileSystem: {
|
||||||
|
setFilePermissions: (
|
||||||
|
filePath: string,
|
||||||
|
mode: string | number,
|
||||||
|
) => chmod(filePath, mode),
|
||||||
|
createDirectory: (
|
||||||
|
directoryPath: string,
|
||||||
|
isRecursive?: boolean,
|
||||||
|
) => mkdir(directoryPath, { recursive: isRecursive }),
|
||||||
|
writeToFile: (
|
||||||
|
filePath: string,
|
||||||
|
data: string,
|
||||||
|
) => writeFile(filePath, data),
|
||||||
|
},
|
||||||
|
command: {
|
||||||
|
execute: (command) => exec(command),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
13
src/infrastructure/Environment/WindowVariables.ts
Normal file
13
src/infrastructure/Environment/WindowVariables.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
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 { }
|
||||||
|
}
|
||||||
76
src/infrastructure/Environment/WindowVariablesValidator.ts
Normal file
76
src/infrastructure/Environment/WindowVariablesValidator.ts
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
|
import { PropertyKeys } from '@/TypeHelpers';
|
||||||
|
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}`);
|
||||||
|
}
|
||||||
|
const errors = [...testEveryProperty(variables)];
|
||||||
|
if (errors.length > 0) {
|
||||||
|
throw new Error(errors.join('\n'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function* testEveryProperty(variables: Partial<WindowVariables>): Iterable<string> {
|
||||||
|
const tests: {
|
||||||
|
[K in PropertyKeys<WindowVariables>]: boolean;
|
||||||
|
} = {
|
||||||
|
os: testOperatingSystem(variables.os),
|
||||||
|
isDesktop: testIsDesktop(variables.isDesktop),
|
||||||
|
system: testSystem(variables),
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const [propertyName, testResult] of Object.entries(tests)) {
|
||||||
|
if (!testResult) {
|
||||||
|
const propertyValue = variables[propertyName as keyof WindowVariables];
|
||||||
|
yield `Unexpected ${propertyName} (${typeof propertyValue})`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function testOperatingSystem(os: unknown): boolean {
|
||||||
|
if (os === undefined) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!isNumber(os)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Object
|
||||||
|
.values(OperatingSystem)
|
||||||
|
.includes(os);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testSystem(variables: Partial<WindowVariables>): boolean {
|
||||||
|
if (!variables.isDesktop) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return variables.system !== undefined && isObject(variables.system);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testIsDesktop(isDesktop: unknown): boolean {
|
||||||
|
if (isDesktop === undefined) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return isBoolean(isDesktop);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isNumber(variable: unknown): variable is number {
|
||||||
|
return typeof variable === 'number';
|
||||||
|
}
|
||||||
|
|
||||||
|
function isBoolean(variable: unknown): variable is boolean {
|
||||||
|
return typeof variable === 'boolean';
|
||||||
|
}
|
||||||
|
|
||||||
|
function isObject(variable: unknown): variable is object {
|
||||||
|
return typeof variable === 'object'
|
||||||
|
&& variable !== null // the data type of null is an object
|
||||||
|
&& !Array.isArray(variable);
|
||||||
|
}
|
||||||
@@ -1,16 +1,18 @@
|
|||||||
import { IAppMetadata } from './IAppMetadata';
|
import { IAppMetadata } from './IAppMetadata';
|
||||||
|
import { IAppMetadataFactory } from './IAppMetadataFactory';
|
||||||
|
import { validateMetadata } from './MetadataValidator';
|
||||||
import { ViteAppMetadata } from './Vite/ViteAppMetadata';
|
import { ViteAppMetadata } from './Vite/ViteAppMetadata';
|
||||||
|
|
||||||
export class AppMetadataFactory {
|
export class AppMetadataFactory implements IAppMetadataFactory {
|
||||||
public static get Current(): IAppMetadata {
|
public static readonly Current = new AppMetadataFactory();
|
||||||
if (!this.instance) {
|
|
||||||
this.instance = new ViteAppMetadata();
|
public readonly instance: IAppMetadata;
|
||||||
|
|
||||||
|
protected constructor(validator: MetadataValidator = validateMetadata) {
|
||||||
|
const metadata = new ViteAppMetadata();
|
||||||
|
validator(metadata);
|
||||||
|
this.instance = metadata;
|
||||||
}
|
}
|
||||||
return this.instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static instance: IAppMetadata;
|
export type MetadataValidator = typeof validateMetadata;
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
||||||
private constructor() {}
|
|
||||||
}
|
|
||||||
|
|||||||
5
src/infrastructure/Metadata/IAppMetadataFactory.ts
Normal file
5
src/infrastructure/Metadata/IAppMetadataFactory.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { IAppMetadata } from './IAppMetadata';
|
||||||
|
|
||||||
|
export interface IAppMetadataFactory {
|
||||||
|
readonly instance: IAppMetadata;
|
||||||
|
}
|
||||||
50
src/infrastructure/Metadata/MetadataValidator.ts
Normal file
50
src/infrastructure/Metadata/MetadataValidator.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||||
|
|
||||||
|
/* Validation is externalized to keep the environment objects simple */
|
||||||
|
export function validateMetadata(metadata: IAppMetadata): void {
|
||||||
|
if (!metadata) {
|
||||||
|
throw new Error('missing metadata');
|
||||||
|
}
|
||||||
|
const keyValues = capturePropertyValues(metadata);
|
||||||
|
if (!Object.keys(keyValues).length) {
|
||||||
|
throw new Error('Unable to capture metadata key/value pairs');
|
||||||
|
}
|
||||||
|
const keysMissingValue = getMissingMetadataKeys(keyValues);
|
||||||
|
if (keysMissingValue.length > 0) {
|
||||||
|
throw new Error(`Metadata keys missing: ${keysMissingValue.join(', ')}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMissingMetadataKeys(keyValuePairs: Record<string, unknown>): string[] {
|
||||||
|
return Object.entries(keyValuePairs)
|
||||||
|
.reduce((acc, [key, value]) => {
|
||||||
|
if (!value) {
|
||||||
|
acc.push(key);
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, new Array<string>());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Captures values of properties and getters from the provided instance.
|
||||||
|
* Necessary because code transformations can make class getters non-enumerable during bundling.
|
||||||
|
* This ensures that even if getters are non-enumerable, their values are still captured and used.
|
||||||
|
*/
|
||||||
|
function capturePropertyValues(instance: unknown): Record<string, unknown> {
|
||||||
|
const obj: Record<string, unknown> = {};
|
||||||
|
const descriptors = Object.getOwnPropertyDescriptors(instance.constructor.prototype);
|
||||||
|
|
||||||
|
// Capture regular properties from the instance
|
||||||
|
for (const [key, value] of Object.entries(instance)) {
|
||||||
|
obj[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Capture getter properties from the instance's prototype
|
||||||
|
for (const [key, descriptor] of Object.entries(descriptors)) {
|
||||||
|
if (typeof descriptor.get === 'function') {
|
||||||
|
obj[key] = descriptor.get.call(instance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
31
src/infrastructure/RuntimeSanity/Common/FactoryValidator.ts
Normal file
31
src/infrastructure/RuntimeSanity/Common/FactoryValidator.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { ISanityCheckOptions } from './ISanityCheckOptions';
|
||||||
|
import { ISanityValidator } from './ISanityValidator';
|
||||||
|
|
||||||
|
export type FactoryFunction<T> = () => T;
|
||||||
|
|
||||||
|
export abstract class FactoryValidator<T> implements ISanityValidator {
|
||||||
|
private readonly factory: FactoryFunction<T>;
|
||||||
|
|
||||||
|
protected constructor(factory: FactoryFunction<T>) {
|
||||||
|
if (!factory) {
|
||||||
|
throw new Error('missing factory');
|
||||||
|
}
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract shouldValidate(options: ISanityCheckOptions): boolean;
|
||||||
|
|
||||||
|
public abstract name: string;
|
||||||
|
|
||||||
|
public* collectErrors(): Iterable<string> {
|
||||||
|
try {
|
||||||
|
const value = this.factory();
|
||||||
|
if (!value) {
|
||||||
|
// Do not remove this check, it ensures that the factory call is not optimized away.
|
||||||
|
yield 'Factory resulted in a falsy value';
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
yield `Error in factory creation: ${error.message}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
export interface ISanityCheckOptions {
|
export interface ISanityCheckOptions {
|
||||||
readonly validateMetadata: boolean;
|
readonly validateMetadata: boolean;
|
||||||
|
readonly validateEnvironment: boolean;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { ISanityCheckOptions } from './ISanityCheckOptions';
|
import { ISanityCheckOptions } from './ISanityCheckOptions';
|
||||||
|
|
||||||
export interface ISanityValidator {
|
export interface ISanityValidator {
|
||||||
|
readonly name: string;
|
||||||
shouldValidate(options: ISanityCheckOptions): boolean;
|
shouldValidate(options: ISanityCheckOptions): boolean;
|
||||||
collectErrors(): Iterable<string>;
|
collectErrors(): Iterable<string>;
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,23 @@
|
|||||||
import { ISanityCheckOptions } from './ISanityCheckOptions';
|
import { ISanityCheckOptions } from './Common/ISanityCheckOptions';
|
||||||
import { ISanityValidator } from './ISanityValidator';
|
import { ISanityValidator } from './Common/ISanityValidator';
|
||||||
import { MetadataValidator } from './Validators/MetadataValidator';
|
import { MetadataValidator } from './Validators/MetadataValidator';
|
||||||
|
|
||||||
const SanityValidators: ISanityValidator[] = [
|
const DefaultSanityValidators: ISanityValidator[] = [
|
||||||
new MetadataValidator(),
|
new MetadataValidator(),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/* Helps to fail-fast on errors */
|
||||||
export function validateRuntimeSanity(
|
export function validateRuntimeSanity(
|
||||||
options: ISanityCheckOptions,
|
options: ISanityCheckOptions,
|
||||||
validators: readonly ISanityValidator[] = SanityValidators,
|
validators: readonly ISanityValidator[] = DefaultSanityValidators,
|
||||||
): void {
|
): void {
|
||||||
if (!options) {
|
validateContext(options, validators);
|
||||||
throw new Error('missing options');
|
|
||||||
}
|
|
||||||
if (!validators?.length) {
|
|
||||||
throw new Error('missing validators');
|
|
||||||
}
|
|
||||||
const errorMessages = validators.reduce((errors, validator) => {
|
const errorMessages = validators.reduce((errors, validator) => {
|
||||||
if (validator.shouldValidate(options)) {
|
if (validator.shouldValidate(options)) {
|
||||||
errors.push(...validator.collectErrors());
|
const errorMessage = getErrorMessage(validator);
|
||||||
|
if (errorMessage) {
|
||||||
|
errors.push(errorMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return errors;
|
return errors;
|
||||||
}, new Array<string>());
|
}, new Array<string>());
|
||||||
@@ -26,3 +25,26 @@ export function validateRuntimeSanity(
|
|||||||
throw new Error(`Sanity check failed.\n${errorMessages.join('\n---\n')}`);
|
throw new Error(`Sanity check failed.\n${errorMessages.join('\n---\n')}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateContext(
|
||||||
|
options: ISanityCheckOptions,
|
||||||
|
validators: readonly ISanityValidator[],
|
||||||
|
) {
|
||||||
|
if (!options) {
|
||||||
|
throw new Error('missing options');
|
||||||
|
}
|
||||||
|
if (!validators?.length) {
|
||||||
|
throw new Error('missing validators');
|
||||||
|
}
|
||||||
|
if (validators.some((validator) => !validator)) {
|
||||||
|
throw new Error('missing validator in validators');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getErrorMessage(validator: ISanityValidator): string | undefined {
|
||||||
|
const errorMessages = [...validator.collectErrors()];
|
||||||
|
if (!errorMessages.length) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return `${validator.name}:\n${errorMessages.join('\n')}`;
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,66 +1,16 @@
|
|||||||
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||||
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
|
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
|
||||||
import { ISanityCheckOptions } from '../ISanityCheckOptions';
|
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
|
||||||
import { ISanityValidator } from '../ISanityValidator';
|
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
|
||||||
|
|
||||||
export class MetadataValidator implements ISanityValidator {
|
export class MetadataValidator extends FactoryValidator<IAppMetadata> {
|
||||||
private readonly metadata: IAppMetadata;
|
constructor(factory: FactoryFunction<IAppMetadata> = () => AppMetadataFactory.Current.instance) {
|
||||||
|
super(factory);
|
||||||
constructor(metadataFactory: () => IAppMetadata = () => AppMetadataFactory.Current) {
|
|
||||||
this.metadata = metadataFactory();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public shouldValidate(options: ISanityCheckOptions): boolean {
|
public override name = 'metadata';
|
||||||
|
|
||||||
|
public override shouldValidate(options: ISanityCheckOptions): boolean {
|
||||||
return options.validateMetadata;
|
return options.validateMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
public* collectErrors(): Iterable<string> {
|
|
||||||
if (!this.metadata) {
|
|
||||||
yield 'missing metadata';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const keyValues = capturePropertyValues(this.metadata);
|
|
||||||
if (!Object.keys(keyValues).length) {
|
|
||||||
yield 'Unable to capture metadata key/value pairs';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const keysMissingValue = getMissingMetadataKeys(keyValues);
|
|
||||||
if (keysMissingValue.length > 0) {
|
|
||||||
yield `Metadata keys missing: ${keysMissingValue.join(', ')}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getMissingMetadataKeys(keyValuePairs: Record<string, unknown>): string[] {
|
|
||||||
return Object.entries(keyValuePairs)
|
|
||||||
.reduce((acc, [key, value]) => {
|
|
||||||
if (!value) {
|
|
||||||
acc.push(key);
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
}, new Array<string>());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Captures values of properties and getters from the provided instance.
|
|
||||||
* Necessary because code transformations can make class getters non-enumerable during bundling.
|
|
||||||
* This ensures that even if getters are non-enumerable, their values are still captured and used.
|
|
||||||
*/
|
|
||||||
function capturePropertyValues(instance: unknown): Record<string, unknown> {
|
|
||||||
const obj: Record<string, unknown> = {};
|
|
||||||
const descriptors = Object.getOwnPropertyDescriptors(instance.constructor.prototype);
|
|
||||||
|
|
||||||
// Capture regular properties from the instance
|
|
||||||
for (const [key, value] of Object.entries(instance)) {
|
|
||||||
obj[key] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Capture getter properties from the instance's prototype
|
|
||||||
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
||||||
if (typeof descriptor.get === 'function') {
|
|
||||||
obj[key] = descriptor.get.call(instance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {
|
|||||||
useCollectionStateKey, useApplicationKey, useEnvironmentKey,
|
useCollectionStateKey, useApplicationKey, useEnvironmentKey,
|
||||||
} from '@/presentation/injectionSymbols';
|
} from '@/presentation/injectionSymbols';
|
||||||
import { IApplicationContext } from '@/application/Context/IApplicationContext';
|
import { IApplicationContext } from '@/application/Context/IApplicationContext';
|
||||||
import { Environment } from '@/application/Environment/Environment';
|
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||||
|
|
||||||
export function provideDependencies(context: IApplicationContext) {
|
export function provideDependencies(context: IApplicationContext) {
|
||||||
registerSingleton(useApplicationKey, useApplication(context.app));
|
registerSingleton(useApplicationKey, useApplication(context.app));
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ export default defineComponent({
|
|||||||
provideDependencies(singletonAppContext); // In Vue 3.0 we can move it to main.ts
|
provideDependencies(singletonAppContext); // In Vue 3.0 we can move it to main.ts
|
||||||
validateRuntimeSanity({
|
validateRuntimeSanity({
|
||||||
validateMetadata: true,
|
validateMetadata: true,
|
||||||
|
validateEnvironment: true,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ import { useCollectionStateKey, useEnvironmentKey } from '@/presentation/injecti
|
|||||||
import { SaveFileDialog, FileType } from '@/infrastructure/SaveFileDialog';
|
import { SaveFileDialog, FileType } from '@/infrastructure/SaveFileDialog';
|
||||||
import { Clipboard } from '@/infrastructure/Clipboard';
|
import { Clipboard } from '@/infrastructure/Clipboard';
|
||||||
import ModalDialog from '@/presentation/components/Shared/Modal/ModalDialog.vue';
|
import ModalDialog from '@/presentation/components/Shared/Modal/ModalDialog.vue';
|
||||||
import { Environment } from '@/application/Environment/Environment';
|
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||||
import { IReadOnlyCategoryCollectionState } from '@/application/Context/State/ICategoryCollectionState';
|
import { IReadOnlyCategoryCollectionState } from '@/application/Context/State/ICategoryCollectionState';
|
||||||
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
||||||
import { IApplicationCode } from '@/application/Context/State/Code/IApplicationCode';
|
import { IApplicationCode } from '@/application/Context/State/Code/IApplicationCode';
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { IEnvironment } from '@/application/Environment/IEnvironment';
|
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
|
||||||
|
|
||||||
export function useEnvironment(environment: IEnvironment) {
|
export function useEnvironment(environment: IEnvironment) {
|
||||||
if (!environment) {
|
if (!environment) {
|
||||||
|
|||||||
@@ -24,7 +24,12 @@ protocol.registerSchemesAsPrivileged([
|
|||||||
|
|
||||||
setupLogger();
|
setupLogger();
|
||||||
validateRuntimeSanity({
|
validateRuntimeSanity({
|
||||||
|
// Metadata is used by manual updates.
|
||||||
validateMetadata: true,
|
validateMetadata: true,
|
||||||
|
|
||||||
|
// Environment is populated by the preload script and is in the renderer's context;
|
||||||
|
// it's not directly accessible from the main process.
|
||||||
|
validateEnvironment: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
function createWindow() {
|
function createWindow() {
|
||||||
@@ -34,8 +39,8 @@ function createWindow() {
|
|||||||
width: size.width,
|
width: size.width,
|
||||||
height: size.height,
|
height: size.height,
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
nodeIntegration: true,
|
nodeIntegration: true, // disabling does not work with electron-vite, https://electron-vite.org/guide/dev.html#nodeintegration
|
||||||
contextIsolation: false,
|
contextIsolation: true,
|
||||||
preload: PRELOADER_SCRIPT_PATH,
|
preload: PRELOADER_SCRIPT_PATH,
|
||||||
},
|
},
|
||||||
icon: APP_ICON_PATH,
|
icon: APP_ICON_PATH,
|
||||||
|
|||||||
14
src/presentation/electron/preload/NodeOsMapper.ts
Normal file
14
src/presentation/electron/preload/NodeOsMapper.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
|
|
||||||
|
export function convertPlatformToOs(platform: NodeJS.Platform): OperatingSystem | undefined {
|
||||||
|
switch (platform) {
|
||||||
|
case 'darwin':
|
||||||
|
return OperatingSystem.macOS;
|
||||||
|
case 'win32':
|
||||||
|
return OperatingSystem.Windows;
|
||||||
|
case 'linux':
|
||||||
|
return OperatingSystem.Linux;
|
||||||
|
default:
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/presentation/electron/preload/WindowVariablesProvider.ts
Normal file
14
src/presentation/electron/preload/WindowVariablesProvider.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { createNodeSystemOperations } from '@/infrastructure/Environment/SystemOperations/NodeSystemOperations';
|
||||||
|
import { WindowVariables } from '@/infrastructure/Environment/WindowVariables';
|
||||||
|
import { convertPlatformToOs } from './NodeOsMapper';
|
||||||
|
|
||||||
|
export function provideWindowVariables(
|
||||||
|
createSystem = createNodeSystemOperations,
|
||||||
|
convertToOs = convertPlatformToOs,
|
||||||
|
): WindowVariables {
|
||||||
|
return {
|
||||||
|
system: createSystem(),
|
||||||
|
isDesktop: true,
|
||||||
|
os: convertToOs(process.platform),
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,10 +1,22 @@
|
|||||||
// This preload script serves as a placeholder to securely expose Electron APIs to the application.
|
// This file is used to securely expose Electron APIs to the application.
|
||||||
// As of now, the application does not utilize any specific Electron APIs through this script.
|
|
||||||
|
import { contextBridge } from 'electron';
|
||||||
import log from 'electron-log';
|
import log from 'electron-log';
|
||||||
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
|
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
|
||||||
|
import { provideWindowVariables } from './WindowVariablesProvider';
|
||||||
|
|
||||||
validateRuntimeSanity({
|
validateRuntimeSanity({
|
||||||
|
// Validate metadata as a preventive measure for fail-fast,
|
||||||
|
// even if it's not currently used in the preload script.
|
||||||
validateMetadata: true,
|
validateMetadata: true,
|
||||||
|
|
||||||
|
// The preload script cannot access variables on the window object.
|
||||||
|
validateEnvironment: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const windowVariables = provideWindowVariables();
|
||||||
|
Object.entries(windowVariables).forEach(([key, value]) => {
|
||||||
|
contextBridge.exposeInMainWorld(key, value);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Do not remove [PRELOAD_INIT]; it's a marker used in tests.
|
// Do not remove [PRELOAD_INIT]; it's a marker used in tests.
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import { describe } from 'vitest';
|
||||||
|
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/Common/ISanityCheckOptions';
|
||||||
|
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
|
||||||
|
|
||||||
|
describe('SanityChecks', () => {
|
||||||
|
describe('validateRuntimeSanity', () => {
|
||||||
|
describe('does not throw on current environment', () => {
|
||||||
|
// arrange
|
||||||
|
const testOptions = generateTestOptions();
|
||||||
|
testOptions.forEach((options) => {
|
||||||
|
it(`options: ${JSON.stringify(options)}`, () => {
|
||||||
|
// act
|
||||||
|
const act = () => validateRuntimeSanity(options);
|
||||||
|
|
||||||
|
// assert
|
||||||
|
expect(act).to.not.throw();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function generateTestOptions(): ISanityCheckOptions[] {
|
||||||
|
const defaultOptions: ISanityCheckOptions = {
|
||||||
|
validateMetadata: true,
|
||||||
|
validateEnvironment: true,
|
||||||
|
};
|
||||||
|
return generateBooleanPermutations(defaultOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateBooleanPermutations<T>(object: T): T[] {
|
||||||
|
const keys = Object.keys(object) as (keyof T)[];
|
||||||
|
|
||||||
|
if (keys.length === 0) {
|
||||||
|
return [object];
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentKey = keys[0];
|
||||||
|
const currentValue = object[currentKey];
|
||||||
|
|
||||||
|
if (typeof currentValue !== 'boolean') {
|
||||||
|
return generateBooleanPermutations({
|
||||||
|
...object,
|
||||||
|
[currentKey]: currentValue,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const remainingKeys = Object.fromEntries(
|
||||||
|
keys.slice(1).map((key) => [key, object[key]]),
|
||||||
|
) as unknown as T;
|
||||||
|
|
||||||
|
const subPermutations = generateBooleanPermutations(remainingKeys);
|
||||||
|
|
||||||
|
return [
|
||||||
|
...subPermutations.map((p) => ({ ...p, [currentKey]: true })),
|
||||||
|
...subPermutations.map((p) => ({ ...p, [currentKey]: false })),
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { describe } from 'vitest';
|
||||||
|
import { EnvironmentValidator } from '@/infrastructure/RuntimeSanity/Validators/EnvironmentValidator';
|
||||||
|
import { FactoryFunction } from '@/infrastructure/RuntimeSanity/Common/FactoryValidator';
|
||||||
|
import { EnvironmentStub } from '@tests/unit/shared/Stubs/EnvironmentStub';
|
||||||
|
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
|
||||||
|
import { runFactoryValidatorTests } from './FactoryValidatorConcreteTestRunner';
|
||||||
|
|
||||||
|
describe('EnvironmentValidator', () => {
|
||||||
|
runFactoryValidatorTests({
|
||||||
|
createValidator: (factory?: FactoryFunction<IEnvironment>) => new EnvironmentValidator(factory),
|
||||||
|
enablingOptionProperty: 'validateEnvironment',
|
||||||
|
factoryFunctionStub: () => new EnvironmentStub(),
|
||||||
|
expectedValidatorName: 'environment',
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import { PropertyKeys } from '@/TypeHelpers';
|
||||||
|
import { FactoryFunction, FactoryValidator } from '@/infrastructure/RuntimeSanity/Common/FactoryValidator';
|
||||||
|
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/Common/ISanityCheckOptions';
|
||||||
|
import { SanityCheckOptionsStub } from '@tests/unit/shared/Stubs/SanityCheckOptionsStub';
|
||||||
|
|
||||||
|
interface ITestOptions<T> {
|
||||||
|
createValidator: (factory?: FactoryFunction<T>) => FactoryValidator<T>;
|
||||||
|
enablingOptionProperty: PropertyKeys<ISanityCheckOptions>;
|
||||||
|
factoryFunctionStub: FactoryFunction<T>;
|
||||||
|
expectedValidatorName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function runFactoryValidatorTests<T>(
|
||||||
|
testOptions: ITestOptions<T>,
|
||||||
|
) {
|
||||||
|
if (!testOptions) {
|
||||||
|
throw new Error('missing options');
|
||||||
|
}
|
||||||
|
describe('shouldValidate', () => {
|
||||||
|
it('returns true when option is true', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedValue = true;
|
||||||
|
const options: ISanityCheckOptions = {
|
||||||
|
...new SanityCheckOptionsStub(),
|
||||||
|
[testOptions.enablingOptionProperty]: true,
|
||||||
|
};
|
||||||
|
const validatorUnderTest = testOptions.createValidator(testOptions.factoryFunctionStub);
|
||||||
|
// act
|
||||||
|
const actualValue = validatorUnderTest.shouldValidate(options);
|
||||||
|
// assert
|
||||||
|
expect(actualValue).to.equal(expectedValue);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false when option is false', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedValue = false;
|
||||||
|
const options: ISanityCheckOptions = {
|
||||||
|
...new SanityCheckOptionsStub(),
|
||||||
|
[testOptions.enablingOptionProperty]: false,
|
||||||
|
};
|
||||||
|
const validatorUnderTest = testOptions.createValidator(testOptions.factoryFunctionStub);
|
||||||
|
// act
|
||||||
|
const actualValue = validatorUnderTest.shouldValidate(options);
|
||||||
|
// assert
|
||||||
|
expect(actualValue).to.equal(expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('name', () => {
|
||||||
|
it('returns as expected', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedName = testOptions.expectedValidatorName;
|
||||||
|
// act
|
||||||
|
const validatorUnderTest = testOptions.createValidator(testOptions.factoryFunctionStub);
|
||||||
|
// assert
|
||||||
|
const actualName = validatorUnderTest.name;
|
||||||
|
expect(actualName).to.equal(expectedName);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { describe } from 'vitest';
|
||||||
|
import { MetadataValidator } from '@/infrastructure/RuntimeSanity/Validators/MetadataValidator';
|
||||||
|
import { FactoryFunction } from '@/infrastructure/RuntimeSanity/Common/FactoryValidator';
|
||||||
|
import { AppMetadataStub } from '@tests/unit/shared/Stubs/AppMetadataStub';
|
||||||
|
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||||
|
import { runFactoryValidatorTests } from './FactoryValidatorConcreteTestRunner';
|
||||||
|
|
||||||
|
describe('MetadataValidator', () => {
|
||||||
|
runFactoryValidatorTests({
|
||||||
|
createValidator: (factory?: FactoryFunction<IAppMetadata>) => new MetadataValidator(factory),
|
||||||
|
enablingOptionProperty: 'validateMetadata',
|
||||||
|
factoryFunctionStub: () => new AppMetadataStub(),
|
||||||
|
expectedValidatorName: 'metadata',
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -3,7 +3,7 @@ import {
|
|||||||
getEnumNames, getEnumValues, createEnumParser, assertInRange,
|
getEnumNames, getEnumValues, createEnumParser, assertInRange,
|
||||||
} from '@/application/Common/Enum';
|
} from '@/application/Common/Enum';
|
||||||
import { scrambledEqual } from '@/application/Common/Array';
|
import { scrambledEqual } from '@/application/Common/Array';
|
||||||
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
import { EnumRangeTestRunner } from './EnumRangeTestRunner';
|
import { EnumRangeTestRunner } from './EnumRangeTestRunner';
|
||||||
|
|
||||||
describe('Enum', () => {
|
describe('Enum', () => {
|
||||||
@@ -37,7 +37,7 @@ describe('Enum', () => {
|
|||||||
// arrange
|
// arrange
|
||||||
const enumName = 'ParsableEnum';
|
const enumName = 'ParsableEnum';
|
||||||
const testCases = [
|
const testCases = [
|
||||||
...AbsentStringTestCases.map((test) => ({
|
...getAbsentStringTestCases().map((test) => ({
|
||||||
name: test.valueName,
|
name: test.valueName,
|
||||||
value: test.absentValue,
|
value: test.absentValue,
|
||||||
expectedError: `missing ${enumName}`,
|
expectedError: `missing ${enumName}`,
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
|
||||||
|
|
||||||
interface IDesktopTestCase {
|
|
||||||
processPlatform: string;
|
|
||||||
expectedOs: OperatingSystem;
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://nodejs.org/api/process.html#process_process_platform
|
|
||||||
export const DesktopOsTestCases: ReadonlyArray<IDesktopTestCase> = [
|
|
||||||
{
|
|
||||||
processPlatform: 'aix',
|
|
||||||
expectedOs: undefined,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
processPlatform: 'darwin',
|
|
||||||
expectedOs: OperatingSystem.macOS,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
processPlatform: 'freebsd',
|
|
||||||
expectedOs: undefined,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
processPlatform: 'linux',
|
|
||||||
expectedOs: OperatingSystem.Linux,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
processPlatform: 'openbsd',
|
|
||||||
expectedOs: undefined,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
processPlatform: 'sunos',
|
|
||||||
expectedOs: undefined,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
processPlatform: 'win32',
|
|
||||||
expectedOs: OperatingSystem.Windows,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
|
||||||
import { IBrowserOsDetector } from '@/application/Environment/BrowserOs/IBrowserOsDetector';
|
|
||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
|
||||||
import { Environment, IEnvironmentVariables } from '@/application/Environment/Environment';
|
|
||||||
import { DesktopOsTestCases } from './DesktopOsTestCases';
|
|
||||||
|
|
||||||
interface EnvironmentVariables {
|
|
||||||
window?: unknown;
|
|
||||||
process?: unknown;
|
|
||||||
navigator?: unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
class SystemUnderTest extends Environment {
|
|
||||||
constructor(variables: EnvironmentVariables, browserOsDetector?: IBrowserOsDetector) {
|
|
||||||
super(variables as unknown as IEnvironmentVariables, browserOsDetector);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('Environment', () => {
|
|
||||||
describe('isDesktop', () => {
|
|
||||||
it('returns true if process type is renderer', () => {
|
|
||||||
// arrange
|
|
||||||
const window = {
|
|
||||||
process: {
|
|
||||||
type: 'renderer',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
// act
|
|
||||||
const sut = new SystemUnderTest({ window });
|
|
||||||
// assert
|
|
||||||
expect(sut.isDesktop).to.equal(true);
|
|
||||||
});
|
|
||||||
it('returns true if electron is defined as process version', () => {
|
|
||||||
// arrange
|
|
||||||
const process = {
|
|
||||||
versions: {
|
|
||||||
electron: true,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
// act
|
|
||||||
const sut = new SystemUnderTest({ process });
|
|
||||||
// assert
|
|
||||||
expect(sut.isDesktop).to.equal(true);
|
|
||||||
});
|
|
||||||
it('returns true if navigator user agent has electron', () => {
|
|
||||||
// arrange
|
|
||||||
const navigator = {
|
|
||||||
userAgent: 'Electron',
|
|
||||||
};
|
|
||||||
// act
|
|
||||||
const sut = new SystemUnderTest({ navigator });
|
|
||||||
// assert
|
|
||||||
expect(sut.isDesktop).to.equal(true);
|
|
||||||
});
|
|
||||||
it('returns false as default', () => {
|
|
||||||
const sut = new SystemUnderTest({});
|
|
||||||
expect(sut.isDesktop).to.equal(false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('os', () => {
|
|
||||||
it('returns undefined without user agent', () => {
|
|
||||||
// arrange
|
|
||||||
const expected = undefined;
|
|
||||||
const mock: IBrowserOsDetector = {
|
|
||||||
detect: () => {
|
|
||||||
throw new Error('should not reach here');
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const sut = new SystemUnderTest({}, mock);
|
|
||||||
// act
|
|
||||||
const actual = sut.os;
|
|
||||||
// assert
|
|
||||||
expect(actual).to.equal(expected);
|
|
||||||
});
|
|
||||||
it('browser os from BrowserOsDetector', () => {
|
|
||||||
// arrange
|
|
||||||
const givenUserAgent = 'testUserAgent';
|
|
||||||
const expected = OperatingSystem.macOS;
|
|
||||||
const window = {
|
|
||||||
navigator: {
|
|
||||||
userAgent: givenUserAgent,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const mock: IBrowserOsDetector = {
|
|
||||||
detect: (agent) => {
|
|
||||||
if (agent !== givenUserAgent) {
|
|
||||||
throw new Error('Unexpected user agent');
|
|
||||||
}
|
|
||||||
return expected;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
// act
|
|
||||||
const sut = new SystemUnderTest({ window }, mock);
|
|
||||||
const actual = sut.os;
|
|
||||||
// assert
|
|
||||||
expect(actual).to.equal(expected);
|
|
||||||
});
|
|
||||||
describe('desktop os', () => {
|
|
||||||
const navigator = {
|
|
||||||
userAgent: 'Electron',
|
|
||||||
};
|
|
||||||
for (const testCase of DesktopOsTestCases) {
|
|
||||||
it(testCase.processPlatform, () => {
|
|
||||||
// arrange
|
|
||||||
const process = {
|
|
||||||
platform: testCase.processPlatform,
|
|
||||||
};
|
|
||||||
// act
|
|
||||||
const sut = new SystemUnderTest({ navigator, process });
|
|
||||||
// assert
|
|
||||||
expect(sut.os).to.equal(testCase.expectedOs, printMessage());
|
|
||||||
function printMessage(): string {
|
|
||||||
return `Expected: "${OperatingSystem[testCase.expectedOs]}"\n`
|
|
||||||
+ `Actual: "${OperatingSystem[sut.os]}"\n`
|
|
||||||
+ `Platform: "${testCase.processPlatform}"`;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -9,7 +9,7 @@ import LinuxData from '@/application/collections/linux.yaml';
|
|||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
||||||
import { CollectionDataStub } from '@tests/unit/shared/Stubs/CollectionDataStub';
|
import { CollectionDataStub } from '@tests/unit/shared/Stubs/CollectionDataStub';
|
||||||
import { getAbsentCollectionTestCases, AbsentObjectTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentCollectionTestCases, getAbsentObjectTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
import { AppMetadataStub } from '@tests/unit/shared/Stubs/AppMetadataStub';
|
import { AppMetadataStub } from '@tests/unit/shared/Stubs/AppMetadataStub';
|
||||||
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
|
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
|
||||||
import { CategoryCollectionParserStub } from '@tests/unit/shared/Stubs/CategoryCollectionParserStub';
|
import { CategoryCollectionParserStub } from '@tests/unit/shared/Stubs/CategoryCollectionParserStub';
|
||||||
@@ -85,7 +85,7 @@ describe('ApplicationParser', () => {
|
|||||||
});
|
});
|
||||||
it('defaults to metadata from factory', () => {
|
it('defaults to metadata from factory', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const expectedMetadata = AppMetadataFactory.Current;
|
const expectedMetadata: IAppMetadata = AppMetadataFactory.Current.instance;
|
||||||
const infoParserStub = new ProjectInformationParserStub();
|
const infoParserStub = new ProjectInformationParserStub();
|
||||||
// act
|
// act
|
||||||
new ApplicationParserBuilder()
|
new ApplicationParserBuilder()
|
||||||
@@ -157,7 +157,7 @@ describe('ApplicationParser', () => {
|
|||||||
value: testCase.absentValue,
|
value: testCase.absentValue,
|
||||||
expectedError: 'missing collections',
|
expectedError: 'missing collections',
|
||||||
})).filter((test) => test.value !== undefined /* the default value is set */),
|
})).filter((test) => test.value !== undefined /* the default value is set */),
|
||||||
...AbsentObjectTestCases.map((testCase) => ({
|
...getAbsentObjectTestCases().map((testCase) => ({
|
||||||
name: `given absent item "${testCase.valueName}"`,
|
name: `given absent item "${testCase.valueName}"`,
|
||||||
value: [testCase.absentValue],
|
value: [testCase.absentValue],
|
||||||
expectedError: 'missing collection provided',
|
expectedError: 'missing collection provided',
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { describe, it } from 'vitest';
|
import { describe, it } from 'vitest';
|
||||||
import { NodeDataError, INodeDataErrorContext } from '@/application/Parser/NodeValidation/NodeDataError';
|
import { NodeDataError, INodeDataErrorContext } from '@/application/Parser/NodeValidation/NodeDataError';
|
||||||
import { NodeData } from '@/application/Parser/NodeValidation/NodeData';
|
import { NodeData } from '@/application/Parser/NodeValidation/NodeData';
|
||||||
import { AbsentObjectTestCases, AbsentStringTestCases, itEachAbsentTestCase } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentObjectTestCases, getAbsentStringTestCases, itEachAbsentTestCase } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
import { expectThrowsError } from '@tests/unit/shared/Assertions/ExpectThrowsError';
|
import { expectThrowsError } from '@tests/unit/shared/Assertions/ExpectThrowsError';
|
||||||
|
|
||||||
export interface ITestScenario {
|
export interface ITestScenario {
|
||||||
@@ -16,7 +16,7 @@ export class NodeValidationTestRunner {
|
|||||||
describe('throws given invalid names', () => {
|
describe('throws given invalid names', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const testCases = [
|
const testCases = [
|
||||||
...AbsentStringTestCases.map((testCase) => ({
|
...getAbsentStringTestCases().map((testCase) => ({
|
||||||
testName: `missing name (${testCase.valueName})`,
|
testName: `missing name (${testCase.valueName})`,
|
||||||
nameValue: testCase.absentValue,
|
nameValue: testCase.absentValue,
|
||||||
expectedMessage: 'missing name',
|
expectedMessage: 'missing name',
|
||||||
@@ -42,7 +42,7 @@ export class NodeValidationTestRunner {
|
|||||||
) {
|
) {
|
||||||
describe('throws given missing node data', () => {
|
describe('throws given missing node data', () => {
|
||||||
itEachAbsentTestCase([
|
itEachAbsentTestCase([
|
||||||
...AbsentObjectTestCases,
|
...getAbsentObjectTestCases(),
|
||||||
{
|
{
|
||||||
valueName: 'empty object',
|
valueName: 'empty object',
|
||||||
absentValue: {},
|
absentValue: {},
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import { ExpressionEvaluationContextStub } from '@tests/unit/shared/Stubs/Expres
|
|||||||
import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipelineCompiler';
|
import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipelineCompiler';
|
||||||
import { PipelineCompilerStub } from '@tests/unit/shared/Stubs/PipelineCompilerStub';
|
import { PipelineCompilerStub } from '@tests/unit/shared/Stubs/PipelineCompilerStub';
|
||||||
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
|
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
|
||||||
import { AbsentObjectTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentObjectTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
import { IExpressionEvaluationContext } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionEvaluationContext';
|
import { IExpressionEvaluationContext } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionEvaluationContext';
|
||||||
|
|
||||||
describe('Expression', () => {
|
describe('Expression', () => {
|
||||||
@@ -91,7 +91,7 @@ describe('Expression', () => {
|
|||||||
expectedError: string,
|
expectedError: string,
|
||||||
sutBuilder?: (builder: ExpressionBuilder) => ExpressionBuilder,
|
sutBuilder?: (builder: ExpressionBuilder) => ExpressionBuilder,
|
||||||
}[] = [
|
}[] = [
|
||||||
...AbsentObjectTestCases.map((testCase) => ({
|
...getAbsentObjectTestCases().map((testCase) => ({
|
||||||
name: `throws if arguments is ${testCase.valueName}`,
|
name: `throws if arguments is ${testCase.valueName}`,
|
||||||
context: testCase.absentValue,
|
context: testCase.absentValue,
|
||||||
expectedError: 'missing context',
|
expectedError: 'missing context',
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { describe } from 'vitest';
|
import { describe } from 'vitest';
|
||||||
import { EscapeDoubleQuotes } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes';
|
import { EscapeDoubleQuotes } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes';
|
||||||
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
import { runPipeTests } from './PipeTestRunner';
|
import { runPipeTests } from './PipeTestRunner';
|
||||||
|
|
||||||
describe('EscapeDoubleQuotes', () => {
|
describe('EscapeDoubleQuotes', () => {
|
||||||
@@ -23,7 +23,7 @@ describe('EscapeDoubleQuotes', () => {
|
|||||||
input: '""hello world""',
|
input: '""hello world""',
|
||||||
expectedOutput: '"^"""^""hello world"^"""^""',
|
expectedOutput: '"^"""^""hello world"^"""^""',
|
||||||
},
|
},
|
||||||
...AbsentStringTestCases.map((testCase) => ({
|
...getAbsentStringTestCases().map((testCase) => ({
|
||||||
name: 'returns as it is when if input is missing',
|
name: 'returns as it is when if input is missing',
|
||||||
input: testCase.absentValue,
|
input: testCase.absentValue,
|
||||||
expectedOutput: testCase.absentValue,
|
expectedOutput: testCase.absentValue,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { PipeFactory } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory';
|
import { PipeFactory } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory';
|
||||||
import { PipeStub } from '@tests/unit/shared/Stubs/PipeStub';
|
import { PipeStub } from '@tests/unit/shared/Stubs/PipeStub';
|
||||||
import { AbsentStringTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentStringTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
describe('PipeFactory', () => {
|
describe('PipeFactory', () => {
|
||||||
describe('ctor', () => {
|
describe('ctor', () => {
|
||||||
@@ -82,7 +82,7 @@ describe('PipeFactory', () => {
|
|||||||
function testPipeNameValidation(testRunner: (invalidName: string) => void) {
|
function testPipeNameValidation(testRunner: (invalidName: string) => void) {
|
||||||
const testCases = [
|
const testCases = [
|
||||||
// Validate missing value
|
// Validate missing value
|
||||||
...AbsentStringTestCases.map((testCase) => ({
|
...getAbsentStringTestCases().map((testCase) => ({
|
||||||
name: `empty pipe name (${testCase.valueName})`,
|
name: `empty pipe name (${testCase.valueName})`,
|
||||||
value: testCase.absentValue,
|
value: testCase.absentValue,
|
||||||
expectedError: 'empty pipe name',
|
expectedError: 'empty pipe name',
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressi
|
|||||||
import { IPipeFactory } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory';
|
import { IPipeFactory } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory';
|
||||||
import { PipeStub } from '@tests/unit/shared/Stubs/PipeStub';
|
import { PipeStub } from '@tests/unit/shared/Stubs/PipeStub';
|
||||||
import { PipeFactoryStub } from '@tests/unit/shared/Stubs/PipeFactoryStub';
|
import { PipeFactoryStub } from '@tests/unit/shared/Stubs/PipeFactoryStub';
|
||||||
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
describe('PipelineCompiler', () => {
|
describe('PipelineCompiler', () => {
|
||||||
describe('compile', () => {
|
describe('compile', () => {
|
||||||
@@ -15,12 +15,12 @@ describe('PipelineCompiler', () => {
|
|||||||
expectedError: string;
|
expectedError: string;
|
||||||
}
|
}
|
||||||
const testCases: ITestCase[] = [
|
const testCases: ITestCase[] = [
|
||||||
...AbsentStringTestCases.map((testCase) => ({
|
...getAbsentStringTestCases().map((testCase) => ({
|
||||||
name: `"value" is ${testCase.valueName}`,
|
name: `"value" is ${testCase.valueName}`,
|
||||||
act: (test) => test.withValue(testCase.absentValue),
|
act: (test) => test.withValue(testCase.absentValue),
|
||||||
expectedError: 'missing value',
|
expectedError: 'missing value',
|
||||||
})),
|
})),
|
||||||
...AbsentStringTestCases.map((testCase) => ({
|
...getAbsentStringTestCases().map((testCase) => ({
|
||||||
name: `"pipeline" is ${testCase.valueName}`,
|
name: `"pipeline" is ${testCase.valueName}`,
|
||||||
act: (test) => test.withPipeline(testCase.absentValue),
|
act: (test) => test.withPipeline(testCase.absentValue),
|
||||||
expectedError: 'missing pipeline',
|
expectedError: 'missing pipeline',
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { describe } from 'vitest';
|
import { describe } from 'vitest';
|
||||||
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
|
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
|
||||||
import { WithParser } from '@/application/Parser/Script/Compiler/Expressions/SyntaxParsers/WithParser';
|
import { WithParser } from '@/application/Parser/Script/Compiler/Expressions/SyntaxParsers/WithParser';
|
||||||
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
import { SyntaxParserTestsRunner } from './SyntaxParserTestsRunner';
|
import { SyntaxParserTestsRunner } from './SyntaxParserTestsRunner';
|
||||||
|
|
||||||
describe('WithParser', () => {
|
describe('WithParser', () => {
|
||||||
@@ -86,7 +86,7 @@ describe('WithParser', () => {
|
|||||||
describe('renders scope conditionally', () => {
|
describe('renders scope conditionally', () => {
|
||||||
describe('does not render scope if argument is undefined', () => {
|
describe('does not render scope if argument is undefined', () => {
|
||||||
runner.expectResults(
|
runner.expectResults(
|
||||||
...AbsentStringTestCases.map((testCase) => ({
|
...getAbsentStringTestCases().map((testCase) => ({
|
||||||
name: `does not render when value is "${testCase.valueName}"`,
|
name: `does not render when value is "${testCase.valueName}"`,
|
||||||
code: '{{ with $parameter }}dark{{ end }} ',
|
code: '{{ with $parameter }}dark{{ end }} ',
|
||||||
args: (args) => args
|
args: (args) => args
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { IFunctionCall } from '@/application/Parser/Script/Compiler/Function/Cal
|
|||||||
import { FunctionCallStub } from '@tests/unit/shared/Stubs/FunctionCallStub';
|
import { FunctionCallStub } from '@tests/unit/shared/Stubs/FunctionCallStub';
|
||||||
import { FunctionBodyType, ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
|
import { FunctionBodyType, ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
|
||||||
import {
|
import {
|
||||||
AbsentStringTestCases, itEachAbsentCollectionValue, itEachAbsentObjectValue,
|
getAbsentStringTestCases, itEachAbsentCollectionValue, itEachAbsentObjectValue,
|
||||||
itEachAbsentStringValue,
|
itEachAbsentStringValue,
|
||||||
} from '@tests/unit/shared/TestCases/AbsentTests';
|
} from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
@@ -100,7 +100,7 @@ describe('SharedFunction', () => {
|
|||||||
// arrange
|
// arrange
|
||||||
const testData = [
|
const testData = [
|
||||||
'expected-revert-code',
|
'expected-revert-code',
|
||||||
...AbsentStringTestCases.map((testCase) => testCase.absentValue),
|
...getAbsentStringTestCases().map((testCase) => testCase.absentValue),
|
||||||
];
|
];
|
||||||
for (const data of testData) {
|
for (const data of testData) {
|
||||||
// act
|
// act
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
export function testParameterName(action: (parameterName: string) => string) {
|
export function testParameterName(action: (parameterName: string) => string) {
|
||||||
describe('name', () => {
|
describe('name', () => {
|
||||||
@@ -22,7 +22,7 @@ export function testParameterName(action: (parameterName: string) => string) {
|
|||||||
describe('throws if invalid', () => {
|
describe('throws if invalid', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const testCases = [
|
const testCases = [
|
||||||
...AbsentStringTestCases.map((test) => ({
|
...getAbsentStringTestCases().map((test) => ({
|
||||||
name: test.valueName,
|
name: test.valueName,
|
||||||
value: test.absentValue,
|
value: test.absentValue,
|
||||||
expectedError: 'missing parameter name',
|
expectedError: 'missing parameter name',
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import { CodeSubstituter } from '@/application/Parser/ScriptingDefinition/CodeSu
|
|||||||
import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler';
|
import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler';
|
||||||
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
|
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
|
||||||
import { ExpressionsCompilerStub } from '@tests/unit/shared/Stubs/ExpressionsCompilerStub';
|
import { ExpressionsCompilerStub } from '@tests/unit/shared/Stubs/ExpressionsCompilerStub';
|
||||||
import { AbsentObjectTestCases, AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentObjectTestCases, getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
describe('CodeSubstituter', () => {
|
describe('CodeSubstituter', () => {
|
||||||
describe('throws with invalid parameters', () => {
|
describe('throws with invalid parameters', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const testCases = [
|
const testCases = [
|
||||||
...AbsentStringTestCases.map((testCase) => ({
|
...getAbsentStringTestCases().map((testCase) => ({
|
||||||
name: `given code: ${testCase.valueName}`,
|
name: `given code: ${testCase.valueName}`,
|
||||||
expectedError: 'missing code',
|
expectedError: 'missing code',
|
||||||
parameters: {
|
parameters: {
|
||||||
@@ -17,7 +17,7 @@ describe('CodeSubstituter', () => {
|
|||||||
info: new ProjectInformationStub(),
|
info: new ProjectInformationStub(),
|
||||||
},
|
},
|
||||||
})),
|
})),
|
||||||
...AbsentObjectTestCases.map((testCase) => ({
|
...getAbsentObjectTestCases().map((testCase) => ({
|
||||||
name: `given info: ${testCase.valueName}`,
|
name: `given info: ${testCase.valueName}`,
|
||||||
expectedError: 'missing info',
|
expectedError: 'missing info',
|
||||||
parameters: {
|
parameters: {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Application } from '@/domain/Application';
|
|||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
||||||
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
|
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
|
||||||
import { AbsentObjectTestCases, getAbsentCollectionTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentObjectTestCases, getAbsentCollectionTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
import { ICategoryCollection } from '@/domain/ICategoryCollection';
|
import { ICategoryCollection } from '@/domain/ICategoryCollection';
|
||||||
|
|
||||||
describe('Application', () => {
|
describe('Application', () => {
|
||||||
@@ -69,7 +69,7 @@ describe('Application', () => {
|
|||||||
expectedError: 'missing collections',
|
expectedError: 'missing collections',
|
||||||
value: testCase.absentValue,
|
value: testCase.absentValue,
|
||||||
})),
|
})),
|
||||||
...AbsentObjectTestCases.map((testCase) => ({
|
...getAbsentObjectTestCases().map((testCase) => ({
|
||||||
name: `${testCase.valueName} value in list`,
|
name: `${testCase.valueName} value in list`,
|
||||||
expectedError: 'missing collection in the list',
|
expectedError: 'missing collection in the list',
|
||||||
value: [new CategoryCollectionStub(), testCase.absentValue],
|
value: [new CategoryCollectionStub(), testCase.absentValue],
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { ScriptCode } from '@/domain/ScriptCode';
|
import { ScriptCode } from '@/domain/ScriptCode';
|
||||||
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
describe('ScriptCode', () => {
|
describe('ScriptCode', () => {
|
||||||
describe('code', () => {
|
describe('code', () => {
|
||||||
@@ -15,7 +15,7 @@ describe('ScriptCode', () => {
|
|||||||
},
|
},
|
||||||
expectedError: '(revert): Code itself and its reverting code cannot be the same',
|
expectedError: '(revert): Code itself and its reverting code cannot be the same',
|
||||||
},
|
},
|
||||||
...AbsentStringTestCases.map((testCase) => ({
|
...getAbsentStringTestCases().map((testCase) => ({
|
||||||
name: `cannot construct with ${testCase.valueName} "execute"`,
|
name: `cannot construct with ${testCase.valueName} "execute"`,
|
||||||
code: {
|
code: {
|
||||||
execute: testCase.absentValue,
|
execute: testCase.absentValue,
|
||||||
|
|||||||
@@ -3,16 +3,48 @@ import { EnvironmentStub } from '@tests/unit/shared/Stubs/EnvironmentStub';
|
|||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
import { CodeRunner } from '@/infrastructure/CodeRunner';
|
import { CodeRunner } from '@/infrastructure/CodeRunner';
|
||||||
import { expectThrowsAsync } from '@tests/unit/shared/Assertions/ExpectThrowsAsync';
|
import { expectThrowsAsync } from '@tests/unit/shared/Assertions/ExpectThrowsAsync';
|
||||||
|
import { SystemOperationsStub } from '@tests/unit/shared/Stubs/SystemOperationsStub';
|
||||||
|
import { OperatingSystemOpsStub } from '@tests/unit/shared/Stubs/OperatingSystemOpsStub';
|
||||||
|
import { LocationOpsStub } from '@tests/unit/shared/Stubs/LocationOpsStub';
|
||||||
|
import { FileSystemOpsStub } from '@tests/unit/shared/Stubs/FileSystemOpsStub';
|
||||||
|
import { CommandOpsStub } from '@tests/unit/shared/Stubs/CommandOpsStub';
|
||||||
|
import { IFileSystemOps, ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||||
|
import { FunctionKeys } from '@/TypeHelpers';
|
||||||
|
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
describe('CodeRunner', () => {
|
describe('CodeRunner', () => {
|
||||||
|
describe('ctor throws if system is missing', () => {
|
||||||
|
itEachAbsentObjectValue((absentValue) => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'missing system operations';
|
||||||
|
const environment = new EnvironmentStub()
|
||||||
|
.withSystemOperations(absentValue);
|
||||||
|
// act
|
||||||
|
const act = () => new CodeRunner(environment);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
});
|
||||||
describe('runCode', () => {
|
describe('runCode', () => {
|
||||||
it('creates temporary directory recursively', async () => {
|
it('creates temporary directory recursively', async () => {
|
||||||
// arrange
|
// arrange
|
||||||
const expectedDir = 'expected-dir';
|
const expectedDir = 'expected-dir';
|
||||||
|
const expectedIsRecursive = true;
|
||||||
|
|
||||||
const folderName = 'privacy.sexy';
|
const folderName = 'privacy.sexy';
|
||||||
const context = new TestContext();
|
const temporaryDirName = 'tmp';
|
||||||
context.mocks.os.setupTmpdir('tmp');
|
const filesystem = new FileSystemOpsStub();
|
||||||
context.mocks.path.setupJoin(expectedDir, 'tmp', folderName);
|
const context = new TestContext()
|
||||||
|
.withSystemOperationsStub((ops) => ops
|
||||||
|
.withOperatingSystem(
|
||||||
|
new OperatingSystemOpsStub()
|
||||||
|
.withTemporaryDirectoryResult(temporaryDirName),
|
||||||
|
)
|
||||||
|
.withLocation(
|
||||||
|
new LocationOpsStub()
|
||||||
|
.withJoinResult(expectedDir, temporaryDirName, folderName),
|
||||||
|
)
|
||||||
|
.withFileSystem(filesystem));
|
||||||
|
|
||||||
// act
|
// act
|
||||||
await context
|
await context
|
||||||
@@ -20,22 +52,34 @@ describe('CodeRunner', () => {
|
|||||||
.runCode();
|
.runCode();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
expect(context.mocks.fs.mkdirHistory.length).to.equal(1);
|
const calls = filesystem.callHistory.filter((call) => call.methodName === 'createDirectory');
|
||||||
expect(context.mocks.fs.mkdirHistory[0].isRecursive).to.equal(true);
|
expect(calls.length).to.equal(1);
|
||||||
expect(context.mocks.fs.mkdirHistory[0].path).to.equal(expectedDir);
|
const [actualPath, actualIsRecursive] = calls[0].args;
|
||||||
|
expect(actualPath).to.equal(expectedDir);
|
||||||
|
expect(actualIsRecursive).to.equal(expectedIsRecursive);
|
||||||
});
|
});
|
||||||
it('creates a file with expected code and path', async () => {
|
it('creates a file with expected code and path', async () => {
|
||||||
// arrange
|
// arrange
|
||||||
const expectedCode = 'expected-code';
|
const expectedCode = 'expected-code';
|
||||||
const expectedFilePath = 'expected-file-path';
|
const expectedFilePath = 'expected-file-path';
|
||||||
|
|
||||||
|
const filesystem = new FileSystemOpsStub();
|
||||||
const extension = '.sh';
|
const extension = '.sh';
|
||||||
const expectedName = `run.${extension}`;
|
const expectedName = `run.${extension}`;
|
||||||
const folderName = 'privacy.sexy';
|
const folderName = 'privacy.sexy';
|
||||||
const context = new TestContext();
|
const temporaryDirName = 'tmp';
|
||||||
context.mocks.os.setupTmpdir('tmp');
|
const context = new TestContext()
|
||||||
context.mocks.path.setupJoin('folder', 'tmp', folderName);
|
.withSystemOperationsStub((ops) => ops
|
||||||
context.mocks.path.setupJoin(expectedFilePath, 'folder', expectedName);
|
.withOperatingSystem(
|
||||||
|
new OperatingSystemOpsStub()
|
||||||
|
.withTemporaryDirectoryResult(temporaryDirName),
|
||||||
|
)
|
||||||
|
.withLocation(
|
||||||
|
new LocationOpsStub()
|
||||||
|
.withJoinResult('folder', temporaryDirName, folderName)
|
||||||
|
.withJoinResult(expectedFilePath, 'folder', expectedName),
|
||||||
|
)
|
||||||
|
.withFileSystem(filesystem));
|
||||||
|
|
||||||
// act
|
// act
|
||||||
await context
|
await context
|
||||||
@@ -45,22 +89,34 @@ describe('CodeRunner', () => {
|
|||||||
.runCode();
|
.runCode();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
expect(context.mocks.fs.writeFileHistory.length).to.equal(1);
|
const calls = filesystem.callHistory.filter((call) => call.methodName === 'writeToFile');
|
||||||
expect(context.mocks.fs.writeFileHistory[0].data).to.equal(expectedCode);
|
expect(calls.length).to.equal(1);
|
||||||
expect(context.mocks.fs.writeFileHistory[0].path).to.equal(expectedFilePath);
|
const [actualFilePath, actualData] = calls[0].args;
|
||||||
|
expect(actualFilePath).to.equal(expectedFilePath);
|
||||||
|
expect(actualData).to.equal(expectedCode);
|
||||||
});
|
});
|
||||||
it('set file permissions as expected', async () => {
|
it('set file permissions as expected', async () => {
|
||||||
// arrange
|
// arrange
|
||||||
const expectedMode = '755';
|
const expectedMode = '755';
|
||||||
const expectedFilePath = 'expected-file-path';
|
const expectedFilePath = 'expected-file-path';
|
||||||
|
|
||||||
|
const filesystem = new FileSystemOpsStub();
|
||||||
const extension = '.sh';
|
const extension = '.sh';
|
||||||
const expectedName = `run.${extension}`;
|
const expectedName = `run.${extension}`;
|
||||||
const folderName = 'privacy.sexy';
|
const folderName = 'privacy.sexy';
|
||||||
const context = new TestContext();
|
const temporaryDirName = 'tmp';
|
||||||
context.mocks.os.setupTmpdir('tmp');
|
const context = new TestContext()
|
||||||
context.mocks.path.setupJoin('folder', 'tmp', folderName);
|
.withSystemOperationsStub((ops) => ops
|
||||||
context.mocks.path.setupJoin(expectedFilePath, 'folder', expectedName);
|
.withOperatingSystem(
|
||||||
|
new OperatingSystemOpsStub()
|
||||||
|
.withTemporaryDirectoryResult(temporaryDirName),
|
||||||
|
)
|
||||||
|
.withLocation(
|
||||||
|
new LocationOpsStub()
|
||||||
|
.withJoinResult('folder', temporaryDirName, folderName)
|
||||||
|
.withJoinResult(expectedFilePath, 'folder', expectedName),
|
||||||
|
)
|
||||||
|
.withFileSystem(filesystem));
|
||||||
|
|
||||||
// act
|
// act
|
||||||
await context
|
await context
|
||||||
@@ -69,57 +125,74 @@ describe('CodeRunner', () => {
|
|||||||
.runCode();
|
.runCode();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
expect(context.mocks.fs.chmodCallHistory.length).to.equal(1);
|
const calls = filesystem.callHistory.filter((call) => call.methodName === 'setFilePermissions');
|
||||||
expect(context.mocks.fs.chmodCallHistory[0].mode).to.equal(expectedMode);
|
expect(calls.length).to.equal(1);
|
||||||
expect(context.mocks.fs.chmodCallHistory[0].path).to.equal(expectedFilePath);
|
const [actualFilePath, actualMode] = calls[0].args;
|
||||||
|
expect(actualFilePath).to.equal(expectedFilePath);
|
||||||
|
expect(actualMode).to.equal(expectedMode);
|
||||||
});
|
});
|
||||||
describe('executes as expected', () => {
|
describe('executes as expected', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const filePath = 'expected-file-path';
|
const filePath = 'expected-file-path';
|
||||||
const testData = [
|
interface IExecutionTestCase {
|
||||||
|
readonly givenOs: OperatingSystem;
|
||||||
|
readonly expectedCommand: string;
|
||||||
|
}
|
||||||
|
const testData: readonly IExecutionTestCase[] = [
|
||||||
{
|
{
|
||||||
os: OperatingSystem.Windows,
|
givenOs: OperatingSystem.Windows,
|
||||||
expected: filePath,
|
expectedCommand: filePath,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
os: OperatingSystem.macOS,
|
givenOs: OperatingSystem.macOS,
|
||||||
expected: `open -a Terminal.app ${filePath}`,
|
expectedCommand: `open -a Terminal.app ${filePath}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
os: OperatingSystem.Linux,
|
givenOs: OperatingSystem.Linux,
|
||||||
expected: `x-terminal-emulator -e '${filePath}'`,
|
expectedCommand: `x-terminal-emulator -e '${filePath}'`,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
for (const data of testData) {
|
for (const { givenOs, expectedCommand } of testData) {
|
||||||
it(`returns ${data.expected} on ${OperatingSystem[data.os]}`, async () => {
|
it(`returns ${expectedCommand} on ${OperatingSystem[givenOs]}`, async () => {
|
||||||
const context = new TestContext();
|
const command = new CommandOpsStub();
|
||||||
context.mocks.os.setupTmpdir('non-important-temp-dir-name');
|
const context = new TestContext()
|
||||||
context.mocks.path.setupJoinSequence('non-important-folder-name', filePath);
|
.withSystemOperationsStub((ops) => ops
|
||||||
context.withOs(data.os);
|
.withLocation(
|
||||||
|
new LocationOpsStub()
|
||||||
|
.withJoinResultSequence('non-important-folder-name', filePath),
|
||||||
|
)
|
||||||
|
.withCommand(command));
|
||||||
|
|
||||||
// act
|
// act
|
||||||
await context
|
await context
|
||||||
.withOs(data.os)
|
.withOs(givenOs)
|
||||||
.runCode();
|
.runCode();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
expect(context.mocks.child_process.executionHistory.length).to.equal(1);
|
const calls = command.callHistory.filter((c) => c.methodName === 'execute');
|
||||||
expect(context.mocks.child_process.executionHistory[0]).to.equal(data.expected);
|
expect(calls.length).to.equal(1);
|
||||||
|
const [actualCommand] = calls[0].args;
|
||||||
|
expect(actualCommand).to.equal(expectedCommand);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
it('runs in expected order', async () => {
|
it('runs in expected order', async () => { // verifies correct `async`, `await` usage.
|
||||||
// arrange
|
const expectedOrder: readonly FunctionKeys<IFileSystemOps>[] = [
|
||||||
const expectedOrder = [NodeJsCommand.mkdir, NodeJsCommand.writeFile, NodeJsCommand.chmod];
|
'createDirectory',
|
||||||
const context = new TestContext();
|
'writeToFile',
|
||||||
context.mocks.os.setupTmpdir('non-important-temp-dir-name');
|
'setFilePermissions',
|
||||||
context.mocks.path.setupJoinSequence('non-important-folder-name1', 'non-important-folder-name2');
|
];
|
||||||
|
const fileSystem = new FileSystemOpsStub();
|
||||||
|
const context = new TestContext()
|
||||||
|
.withSystemOperationsStub((ops) => ops
|
||||||
|
.withFileSystem(fileSystem));
|
||||||
|
|
||||||
// act
|
// act
|
||||||
await context.runCode();
|
await context.runCode();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
const actualOrder = context.mocks.commandHistory
|
const actualOrder = fileSystem.callHistory
|
||||||
|
.map((c) => c.methodName)
|
||||||
.filter((command) => expectedOrder.includes(command));
|
.filter((command) => expectedOrder.includes(command));
|
||||||
expect(expectedOrder).to.deep.equal(actualOrder);
|
expect(expectedOrder).to.deep.equal(actualOrder);
|
||||||
});
|
});
|
||||||
@@ -138,23 +211,40 @@ describe('CodeRunner', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
class TestContext {
|
class TestContext {
|
||||||
public mocks = getNodeJsMocks();
|
|
||||||
|
|
||||||
private code = 'code';
|
private code = 'code';
|
||||||
|
|
||||||
private folderName = 'folderName';
|
private folderName = 'folderName';
|
||||||
|
|
||||||
private fileExtension = 'fileExtension';
|
private fileExtension = 'fileExtension';
|
||||||
|
|
||||||
private env = mockEnvironment(OperatingSystem.Windows);
|
private os = OperatingSystem.Windows;
|
||||||
|
|
||||||
|
private systemOperations: ISystemOperations = new SystemOperationsStub();
|
||||||
|
|
||||||
public async runCode(): Promise<void> {
|
public async runCode(): Promise<void> {
|
||||||
const runner = new CodeRunner(this.mocks, this.env);
|
const environment = new EnvironmentStub()
|
||||||
|
.withOs(this.os)
|
||||||
|
.withSystemOperations(this.systemOperations);
|
||||||
|
const runner = new CodeRunner(environment);
|
||||||
await runner.runCode(this.code, this.folderName, this.fileExtension);
|
await runner.runCode(this.code, this.folderName, this.fileExtension);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public withSystemOperations(
|
||||||
|
systemOperations: ISystemOperations,
|
||||||
|
): this {
|
||||||
|
this.systemOperations = systemOperations;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public withSystemOperationsStub(
|
||||||
|
setup: (stub: SystemOperationsStub) => SystemOperationsStub,
|
||||||
|
): this {
|
||||||
|
const stub = setup(new SystemOperationsStub());
|
||||||
|
return this.withSystemOperations(stub);
|
||||||
|
}
|
||||||
|
|
||||||
public withOs(os: OperatingSystem) {
|
public withOs(os: OperatingSystem) {
|
||||||
this.env = mockEnvironment(os);
|
this.os = os;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,104 +263,3 @@ class TestContext {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function mockEnvironment(os: OperatingSystem) {
|
|
||||||
return new EnvironmentStub().withOs(os);
|
|
||||||
}
|
|
||||||
|
|
||||||
const enum NodeJsCommand { tmpdir, join, exec, mkdir, writeFile, chmod }
|
|
||||||
|
|
||||||
function getNodeJsMocks() {
|
|
||||||
const commandHistory = new Array<NodeJsCommand>();
|
|
||||||
return {
|
|
||||||
os: mockOs(commandHistory),
|
|
||||||
path: mockPath(commandHistory),
|
|
||||||
fs: mockNodeFs(commandHistory),
|
|
||||||
child_process: mockChildProcess(commandHistory),
|
|
||||||
commandHistory,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function mockOs(commandHistory: NodeJsCommand[]) {
|
|
||||||
let tmpDir = '/stub-temp-dir/';
|
|
||||||
return {
|
|
||||||
setupTmpdir: (value: string): void => {
|
|
||||||
tmpDir = value;
|
|
||||||
},
|
|
||||||
tmpdir: (): string => {
|
|
||||||
if (!tmpDir) {
|
|
||||||
throw new Error('tmpdir not set up');
|
|
||||||
}
|
|
||||||
commandHistory.push(NodeJsCommand.tmpdir);
|
|
||||||
return tmpDir;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function mockPath(commandHistory: NodeJsCommand[]) {
|
|
||||||
const sequence = new Array<string>();
|
|
||||||
const scenarios = new Map<string, string>();
|
|
||||||
const getScenarioKey = (paths: string[]) => paths.join('|');
|
|
||||||
return {
|
|
||||||
setupJoin: (returnValue: string, ...paths: string[]): void => {
|
|
||||||
scenarios.set(getScenarioKey(paths), returnValue);
|
|
||||||
},
|
|
||||||
setupJoinSequence: (...valuesToReturn: string[]): void => {
|
|
||||||
sequence.push(...valuesToReturn);
|
|
||||||
sequence.reverse();
|
|
||||||
},
|
|
||||||
join: (...paths: string[]): string => {
|
|
||||||
commandHistory.push(NodeJsCommand.join);
|
|
||||||
if (sequence.length > 0) {
|
|
||||||
return sequence.pop();
|
|
||||||
}
|
|
||||||
const key = getScenarioKey(paths);
|
|
||||||
if (!scenarios.has(key)) {
|
|
||||||
return paths.join('/');
|
|
||||||
}
|
|
||||||
return scenarios.get(key);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function mockChildProcess(commandHistory: NodeJsCommand[]) {
|
|
||||||
const executionHistory = new Array<string>();
|
|
||||||
return {
|
|
||||||
exec: (command: string): void => {
|
|
||||||
commandHistory.push(NodeJsCommand.exec);
|
|
||||||
executionHistory.push(command);
|
|
||||||
},
|
|
||||||
executionHistory,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function mockNodeFs(commandHistory: NodeJsCommand[]) {
|
|
||||||
interface IMkdirCall { path: string; isRecursive: boolean; }
|
|
||||||
interface IWriteFileCall { path: string; data: string; }
|
|
||||||
interface IChmodCall { path: string; mode: string | number; }
|
|
||||||
const mkdirHistory = new Array<IMkdirCall>();
|
|
||||||
const writeFileHistory = new Array<IWriteFileCall>();
|
|
||||||
const chmodCallHistory = new Array<IChmodCall>();
|
|
||||||
return {
|
|
||||||
promises: {
|
|
||||||
mkdir: (path, options) => {
|
|
||||||
commandHistory.push(NodeJsCommand.mkdir);
|
|
||||||
mkdirHistory.push({ path, isRecursive: options && options.recursive });
|
|
||||||
return Promise.resolve(path);
|
|
||||||
},
|
|
||||||
writeFile: (path, data) => {
|
|
||||||
commandHistory.push(NodeJsCommand.writeFile);
|
|
||||||
writeFileHistory.push({ path, data });
|
|
||||||
return Promise.resolve();
|
|
||||||
},
|
|
||||||
chmod: (path, mode) => {
|
|
||||||
commandHistory.push(NodeJsCommand.chmod);
|
|
||||||
chmodCallHistory.push({ path, mode });
|
|
||||||
return Promise.resolve();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
mkdirHistory,
|
|
||||||
writeFileHistory,
|
|
||||||
chmodCallHistory,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
import { BrowserOsDetector } from '@/application/Environment/BrowserOs/BrowserOsDetector';
|
import { BrowserOsDetector } from '@/infrastructure/Environment/BrowserOs/BrowserOsDetector';
|
||||||
import { itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
import { BrowserOsTestCases } from './BrowserOsTestCases';
|
import { BrowserOsTestCases } from './BrowserOsTestCases';
|
||||||
|
|
||||||
219
tests/unit/infrastructure/Environment/Environment.spec.ts
Normal file
219
tests/unit/infrastructure/Environment/Environment.spec.ts
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { IBrowserOsDetector } from '@/infrastructure/Environment/BrowserOs/IBrowserOsDetector';
|
||||||
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
|
import { Environment, WindowValidator } from '@/infrastructure/Environment/Environment';
|
||||||
|
import { SystemOperationsStub } from '@tests/unit/shared/Stubs/SystemOperationsStub';
|
||||||
|
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
import { WindowVariables } from '@/infrastructure/Environment/WindowVariables';
|
||||||
|
import { BrowserOsDetectorStub } from '@tests/unit/shared/Stubs/BrowserOsDetectorStub';
|
||||||
|
|
||||||
|
describe('Environment', () => {
|
||||||
|
describe('ctor', () => {
|
||||||
|
describe('throws if window is absent', () => {
|
||||||
|
itEachAbsentObjectValue((absentValue) => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'missing window';
|
||||||
|
const absentWindow = absentValue;
|
||||||
|
// act
|
||||||
|
const act = () => createEnvironment({
|
||||||
|
window: absentWindow,
|
||||||
|
});
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('isDesktop', () => {
|
||||||
|
it('returns true when window property isDesktop is true', () => {
|
||||||
|
// arrange
|
||||||
|
const desktopWindow = {
|
||||||
|
isDesktop: true,
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const sut = createEnvironment({
|
||||||
|
window: desktopWindow,
|
||||||
|
});
|
||||||
|
// assert
|
||||||
|
expect(sut.isDesktop).to.equal(true);
|
||||||
|
});
|
||||||
|
it('returns false when window property isDesktop is false', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedValue = false;
|
||||||
|
const browserWindow = {
|
||||||
|
isDesktop: false,
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const sut = createEnvironment({
|
||||||
|
window: browserWindow,
|
||||||
|
});
|
||||||
|
// assert
|
||||||
|
expect(sut.isDesktop).to.equal(expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('os', () => {
|
||||||
|
it('returns undefined if user agent is missing', () => {
|
||||||
|
// arrange
|
||||||
|
const expected = undefined;
|
||||||
|
const browserDetectorMock: IBrowserOsDetector = {
|
||||||
|
detect: () => {
|
||||||
|
throw new Error('should not reach here');
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const sut = createEnvironment({
|
||||||
|
browserOsDetector: browserDetectorMock,
|
||||||
|
});
|
||||||
|
// 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: IBrowserOsDetector = {
|
||||||
|
detect: (agent) => {
|
||||||
|
if (agent !== givenUserAgent) {
|
||||||
|
throw new Error('Unexpected user agent');
|
||||||
|
}
|
||||||
|
return expected;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const sut = createEnvironment({
|
||||||
|
window: windowWithUserAgent as Partial<Window>,
|
||||||
|
browserOsDetector: browserDetectorMock,
|
||||||
|
});
|
||||||
|
const actual = sut.os;
|
||||||
|
// assert
|
||||||
|
expect(actual).to.equal(expected);
|
||||||
|
});
|
||||||
|
describe('desktop os', () => {
|
||||||
|
describe('returns from window property `os`', () => {
|
||||||
|
const testValues = [
|
||||||
|
OperatingSystem.macOS,
|
||||||
|
OperatingSystem.Windows,
|
||||||
|
OperatingSystem.Linux,
|
||||||
|
];
|
||||||
|
testValues.forEach((testValue) => {
|
||||||
|
it(`given ${OperatingSystem[testValue]}`, () => {
|
||||||
|
// arrange
|
||||||
|
const expectedOs = testValue;
|
||||||
|
const desktopWindowWithOs = {
|
||||||
|
isDesktop: true,
|
||||||
|
os: expectedOs,
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const sut = createEnvironment({
|
||||||
|
window: desktopWindowWithOs,
|
||||||
|
});
|
||||||
|
// assert
|
||||||
|
const actualOs = sut.os;
|
||||||
|
expect(actualOs).to.equal(expectedOs);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('returns undefined when window property `os` is absent', () => {
|
||||||
|
itEachAbsentObjectValue((absentValue) => {
|
||||||
|
// arrange
|
||||||
|
const expectedValue = undefined;
|
||||||
|
const windowWithAbsentOs = {
|
||||||
|
os: absentValue,
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const sut = createEnvironment({
|
||||||
|
window: windowWithAbsentOs,
|
||||||
|
});
|
||||||
|
// assert
|
||||||
|
expect(sut.os).to.equal(expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('system', () => {
|
||||||
|
it('fetches system operations from window', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedSystem = new SystemOperationsStub();
|
||||||
|
const windowWithSystem = {
|
||||||
|
system: expectedSystem,
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const sut = createEnvironment({
|
||||||
|
window: windowWithSystem,
|
||||||
|
});
|
||||||
|
// assert
|
||||||
|
const actualSystem = sut.system;
|
||||||
|
expect(actualSystem).to.equal(expectedSystem);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('validateWindow', () => {
|
||||||
|
it('throws when validator throws', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedErrorMessage = 'expected error thrown from window validator';
|
||||||
|
const mockValidator: WindowValidator = () => {
|
||||||
|
throw new Error(expectedErrorMessage);
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => createEnvironment({
|
||||||
|
windowValidator: mockValidator,
|
||||||
|
});
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedErrorMessage);
|
||||||
|
});
|
||||||
|
it('does not throw when validator does not throw', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedErrorMessage = 'expected error thrown from window validator';
|
||||||
|
const mockValidator: WindowValidator = () => {
|
||||||
|
// do not throw
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => createEnvironment({
|
||||||
|
windowValidator: mockValidator,
|
||||||
|
});
|
||||||
|
// assert
|
||||||
|
expect(act).to.not.throw(expectedErrorMessage);
|
||||||
|
});
|
||||||
|
it('sends expected window to validator', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedVariables: Partial<WindowVariables> = {};
|
||||||
|
let actualVariables: Partial<WindowVariables>;
|
||||||
|
const mockValidator: WindowValidator = (variables) => {
|
||||||
|
actualVariables = variables;
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
createEnvironment({
|
||||||
|
window: expectedVariables,
|
||||||
|
windowValidator: mockValidator,
|
||||||
|
});
|
||||||
|
// assert
|
||||||
|
expect(actualVariables).to.equal(expectedVariables);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
interface EnvironmentOptions {
|
||||||
|
window: Partial<Window>;
|
||||||
|
browserOsDetector?: IBrowserOsDetector;
|
||||||
|
windowValidator?: WindowValidator;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createEnvironment(options: Partial<EnvironmentOptions> = {}): TestableEnvironment {
|
||||||
|
const defaultOptions: EnvironmentOptions = {
|
||||||
|
window: {},
|
||||||
|
browserOsDetector: new BrowserOsDetectorStub(),
|
||||||
|
windowValidator: () => { /* NO OP */ },
|
||||||
|
};
|
||||||
|
|
||||||
|
return new TestableEnvironment({ ...defaultOptions, ...options });
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestableEnvironment extends Environment {
|
||||||
|
public constructor(options: EnvironmentOptions) {
|
||||||
|
super(options.window, options.browserOsDetector, options.windowValidator);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { validateWindowVariables } from '@/infrastructure/Environment/WindowVariablesValidator';
|
||||||
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
|
import { SystemOperationsStub } from '@tests/unit/shared/Stubs/SystemOperationsStub';
|
||||||
|
import { WindowVariables } from '@/infrastructure/Environment/WindowVariables';
|
||||||
|
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
|
describe('WindowVariablesValidator', () => {
|
||||||
|
describe('validateWindowVariables', () => {
|
||||||
|
describe('invalid types', () => {
|
||||||
|
it('throws an error if variables is not an object', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'window is not an object but string';
|
||||||
|
const variablesAsString = 'not an object';
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(variablesAsString as unknown);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws an error if variables is an array', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'window is not an object but object';
|
||||||
|
const arrayVariables: unknown = [];
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(arrayVariables as unknown);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('throws an error if variables is null', () => {
|
||||||
|
itEachAbsentObjectValue((absentValue) => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'missing variables';
|
||||||
|
const variables = absentValue;
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(variables as unknown);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('property validations', () => {
|
||||||
|
it('throws an error with a description of all invalid properties', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'Unexpected os (string)\nUnexpected isDesktop (string)';
|
||||||
|
const input = {
|
||||||
|
os: 'invalid',
|
||||||
|
isDesktop: 'not a boolean',
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(input as unknown);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('`os` property', () => {
|
||||||
|
it('throws an error when os is not a number', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'Unexpected os (string)';
|
||||||
|
const input = {
|
||||||
|
os: 'Linux',
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(input as unknown);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws an error for an invalid numeric os value', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'Unexpected os (number)';
|
||||||
|
const input = {
|
||||||
|
os: Number.MAX_SAFE_INTEGER,
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(input as unknown);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not throw for a missing os value', () => {
|
||||||
|
const input = {
|
||||||
|
isDesktop: true,
|
||||||
|
system: new SystemOperationsStub(),
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(input);
|
||||||
|
// assert
|
||||||
|
expect(act).to.not.throw();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('`isDesktop` property', () => {
|
||||||
|
it('throws an error when only isDesktop is provided and it is true without a system object', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'Unexpected system (undefined)';
|
||||||
|
const input = {
|
||||||
|
isDesktop: true,
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(input as unknown);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not throw when isDesktop is true with a valid system object', () => {
|
||||||
|
// arrange
|
||||||
|
const input = {
|
||||||
|
isDesktop: true,
|
||||||
|
system: new SystemOperationsStub(),
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(input);
|
||||||
|
// assert
|
||||||
|
expect(act).to.not.throw();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not throw when isDesktop is false without a system object', () => {
|
||||||
|
// arrange
|
||||||
|
const input = {
|
||||||
|
isDesktop: false,
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(input);
|
||||||
|
// assert
|
||||||
|
expect(act).to.not.throw();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('`system` property', () => {
|
||||||
|
it('throws an error if system is not an object', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'Unexpected system (string)';
|
||||||
|
const input = {
|
||||||
|
isDesktop: true,
|
||||||
|
system: 'invalid system',
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(input as unknown);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not throw for a valid object', () => {
|
||||||
|
const input: WindowVariables = {
|
||||||
|
os: OperatingSystem.Windows,
|
||||||
|
isDesktop: true,
|
||||||
|
system: new SystemOperationsStub(),
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateWindowVariables(input);
|
||||||
|
// assert
|
||||||
|
expect(act).to.not.throw();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -2,14 +2,52 @@ import {
|
|||||||
describe,
|
describe,
|
||||||
} from 'vitest';
|
} from 'vitest';
|
||||||
import { itIsSingleton } from '@tests/unit/shared/TestCases/SingletonTests';
|
import { itIsSingleton } from '@tests/unit/shared/TestCases/SingletonTests';
|
||||||
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
|
import { AppMetadataFactory, MetadataValidator } from '@/infrastructure/Metadata/AppMetadataFactory';
|
||||||
import { ViteAppMetadata } from '@/infrastructure/Metadata/Vite/ViteAppMetadata';
|
import { ViteAppMetadata } from '@/infrastructure/Metadata/Vite/ViteAppMetadata';
|
||||||
|
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||||
|
|
||||||
|
class TestableAppMetadataFactory extends AppMetadataFactory {
|
||||||
|
public constructor(validator: MetadataValidator = () => { /* NO OP */ }) {
|
||||||
|
super(validator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe('AppMetadataFactory', () => {
|
describe('AppMetadataFactory', () => {
|
||||||
describe('instance', () => {
|
describe('instance', () => {
|
||||||
itIsSingleton({
|
itIsSingleton({
|
||||||
getter: () => AppMetadataFactory.Current,
|
getter: () => AppMetadataFactory.Current.instance,
|
||||||
expectedType: ViteAppMetadata,
|
expectedType: ViteAppMetadata,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it('creates the correct type of metadata', () => {
|
||||||
|
// arrange
|
||||||
|
const sut = new TestableAppMetadataFactory();
|
||||||
|
// act
|
||||||
|
const metadata = sut.instance;
|
||||||
|
// assert
|
||||||
|
expect(metadata).to.be.instanceOf(ViteAppMetadata);
|
||||||
|
});
|
||||||
|
it('validates its instance', () => {
|
||||||
|
// arrange
|
||||||
|
let validatedMetadata: IAppMetadata;
|
||||||
|
const validatorMock = (metadata: IAppMetadata) => {
|
||||||
|
validatedMetadata = metadata;
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const sut = new TestableAppMetadataFactory(validatorMock);
|
||||||
|
const actualInstance = sut.instance;
|
||||||
|
// assert
|
||||||
|
expect(actualInstance).to.equal(validatedMetadata);
|
||||||
|
});
|
||||||
|
it('throws error if validator fails', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'validator failed';
|
||||||
|
const failingValidator = () => {
|
||||||
|
throw new Error(expectedError);
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => new TestableAppMetadataFactory(failingValidator);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
74
tests/unit/infrastructure/Metadata/MetadataValidator.spec.ts
Normal file
74
tests/unit/infrastructure/Metadata/MetadataValidator.spec.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
import { AppMetadataStub } from '@tests/unit/shared/Stubs/AppMetadataStub';
|
||||||
|
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||||
|
import { validateMetadata } from '@/infrastructure/Metadata/MetadataValidator';
|
||||||
|
|
||||||
|
describe('MetadataValidator', () => {
|
||||||
|
it('does not throw if all metadata keys have values', () => {
|
||||||
|
// arrange
|
||||||
|
const metadata = new AppMetadataStub();
|
||||||
|
// act
|
||||||
|
const act = () => validateMetadata(metadata);
|
||||||
|
// assert
|
||||||
|
expect(act).to.not.throw();
|
||||||
|
});
|
||||||
|
describe('throws as expected', () => {
|
||||||
|
describe('"missing metadata" if metadata is not provided', () => {
|
||||||
|
itEachAbsentObjectValue((absentValue) => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'missing metadata';
|
||||||
|
const metadata = absentValue;
|
||||||
|
// act
|
||||||
|
const act = () => validateMetadata(metadata);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('"missing keys" if metadata has properties with missing values', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'Metadata keys missing: name, homepageUrl';
|
||||||
|
const missingData: Partial<IAppMetadata> = {
|
||||||
|
name: undefined,
|
||||||
|
homepageUrl: undefined,
|
||||||
|
};
|
||||||
|
const metadata: IAppMetadata = {
|
||||||
|
...new AppMetadataStub(),
|
||||||
|
...missingData,
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateMetadata(metadata);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
it('"missing keys" if metadata has getters with missing values', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'Metadata keys missing: name, homepageUrl';
|
||||||
|
const stubWithGetters: Partial<IAppMetadata> = {
|
||||||
|
get name() {
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
|
get homepageUrl() {
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const metadata: IAppMetadata = {
|
||||||
|
...new AppMetadataStub(),
|
||||||
|
...stubWithGetters,
|
||||||
|
};
|
||||||
|
// act
|
||||||
|
const act = () => validateMetadata(metadata);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
it('"unable to capture metadata" if metadata has no getters or properties', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'Unable to capture metadata key/value pairs';
|
||||||
|
const metadata = {} as IAppMetadata;
|
||||||
|
// act
|
||||||
|
const act = () => validateMetadata(metadata);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -21,7 +21,7 @@ describe('ViteAppMetadata', () => {
|
|||||||
keyof typeof VITE_ENVIRONMENT_KEYS];
|
keyof typeof VITE_ENVIRONMENT_KEYS];
|
||||||
readonly expected: string;
|
readonly expected: string;
|
||||||
}
|
}
|
||||||
const testCases: { [K in PropertyKeys<ViteAppMetadata>]: ITestCase } = {
|
const testCases: { readonly [K in PropertyKeys<ViteAppMetadata>]: ITestCase } = {
|
||||||
name: {
|
name: {
|
||||||
environmentVariable: VITE_ENVIRONMENT_KEYS.NAME,
|
environmentVariable: VITE_ENVIRONMENT_KEYS.NAME,
|
||||||
expected: 'expected-name',
|
expected: 'expected-name',
|
||||||
|
|||||||
@@ -0,0 +1,116 @@
|
|||||||
|
import { describe } from 'vitest';
|
||||||
|
import { FactoryValidator, FactoryFunction } from '@/infrastructure/RuntimeSanity/Common/FactoryValidator';
|
||||||
|
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
|
describe('FactoryValidator', () => {
|
||||||
|
describe('ctor', () => {
|
||||||
|
describe('throws when factory is absent', () => {
|
||||||
|
itEachAbsentObjectValue((absentValue) => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'missing factory';
|
||||||
|
const factory = absentValue;
|
||||||
|
// act
|
||||||
|
const act = () => new TestableFactoryValidator(factory);
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('collectErrors', () => {
|
||||||
|
it('reports error thrown by factory function', () => {
|
||||||
|
// arrange
|
||||||
|
const errorFromFactory = 'Error from factory function';
|
||||||
|
const expectedError = `Error in factory creation: ${errorFromFactory}`;
|
||||||
|
const factory: FactoryFunction<number | undefined> = () => {
|
||||||
|
throw new Error(errorFromFactory);
|
||||||
|
};
|
||||||
|
const sut = new TestableFactoryValidator(factory);
|
||||||
|
// act
|
||||||
|
const errors = [...sut.collectErrors()];
|
||||||
|
// assert
|
||||||
|
expect(errors).to.have.lengthOf(1);
|
||||||
|
expect(errors[0]).to.equal(expectedError);
|
||||||
|
});
|
||||||
|
describe('reports when factory returns falsy values', () => {
|
||||||
|
const falsyValueTestCases = [
|
||||||
|
{
|
||||||
|
name: '`false` boolean',
|
||||||
|
value: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'number zero',
|
||||||
|
value: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'empty string',
|
||||||
|
value: '',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'null',
|
||||||
|
value: null,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'undefined',
|
||||||
|
value: undefined,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'NaN (Not-a-Number)',
|
||||||
|
value: Number.NaN,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
falsyValueTestCases.forEach(({ name, value }) => {
|
||||||
|
it(`reports for value: ${name}`, () => {
|
||||||
|
// arrange
|
||||||
|
const errorFromFactory = 'Factory resulted in a falsy value';
|
||||||
|
const factory: FactoryFunction<number | undefined> = () => {
|
||||||
|
return value as never;
|
||||||
|
};
|
||||||
|
const sut = new TestableFactoryValidator(factory);
|
||||||
|
// act
|
||||||
|
const errors = [...sut.collectErrors()];
|
||||||
|
// assert
|
||||||
|
expect(errors).to.have.lengthOf(1);
|
||||||
|
expect(errors[0]).to.equal(errorFromFactory);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('does not report when factory returns a truthy value', () => {
|
||||||
|
// arrange
|
||||||
|
const factory: FactoryFunction<number | undefined> = () => {
|
||||||
|
return 35;
|
||||||
|
};
|
||||||
|
const sut = new TestableFactoryValidator(factory);
|
||||||
|
// act
|
||||||
|
const errors = [...sut.collectErrors()];
|
||||||
|
// assert
|
||||||
|
expect(errors).to.have.lengthOf(0);
|
||||||
|
});
|
||||||
|
it('executes factory for each method call', () => {
|
||||||
|
// arrange
|
||||||
|
let forceFalsyValue = false;
|
||||||
|
const complexFactory: FactoryFunction<number | undefined> = () => {
|
||||||
|
return forceFalsyValue ? undefined : 42;
|
||||||
|
};
|
||||||
|
const sut = new TestableFactoryValidator(complexFactory);
|
||||||
|
// act
|
||||||
|
const firstErrors = [...sut.collectErrors()];
|
||||||
|
forceFalsyValue = true;
|
||||||
|
const secondErrors = [...sut.collectErrors()];
|
||||||
|
// assert
|
||||||
|
expect(firstErrors).to.have.lengthOf(0);
|
||||||
|
expect(secondErrors).to.have.lengthOf(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
class TestableFactoryValidator extends FactoryValidator<number | undefined> {
|
||||||
|
public constructor(factory: FactoryFunction<number | undefined>) {
|
||||||
|
super(factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public name = 'test';
|
||||||
|
|
||||||
|
public shouldValidate(): boolean {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
|
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
|
||||||
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/ISanityCheckOptions';
|
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/Common/ISanityCheckOptions';
|
||||||
import { SanityCheckOptionsStub } from '@tests/unit/shared/Stubs/SanityCheckOptionsStub';
|
import { SanityCheckOptionsStub } from '@tests/unit/shared/Stubs/SanityCheckOptionsStub';
|
||||||
import { ISanityValidator } from '@/infrastructure/RuntimeSanity/ISanityValidator';
|
import { ISanityValidator } from '@/infrastructure/RuntimeSanity/Common/ISanityValidator';
|
||||||
import { SanityValidatorStub } from '@tests/unit/shared/Stubs/SanityValidatorStub';
|
import { SanityValidatorStub } from '@tests/unit/shared/Stubs/SanityValidatorStub';
|
||||||
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { itEachAbsentCollectionValue, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
describe('SanityChecks', () => {
|
describe('SanityChecks', () => {
|
||||||
describe('validateRuntimeSanity', () => {
|
describe('validateRuntimeSanity', () => {
|
||||||
@@ -21,15 +21,31 @@ describe('SanityChecks', () => {
|
|||||||
expect(act).to.throw(expectedError);
|
expect(act).to.throw(expectedError);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('throws when validators are empty', () => {
|
describe('throws when validators are empty', () => {
|
||||||
|
itEachAbsentCollectionValue((absentCollection) => {
|
||||||
// arrange
|
// arrange
|
||||||
const expectedError = 'missing validators';
|
const expectedError = 'missing validators';
|
||||||
|
const validators = absentCollection;
|
||||||
const context = new TestContext()
|
const context = new TestContext()
|
||||||
.withValidators([]);
|
.withValidators(validators);
|
||||||
// act
|
// act
|
||||||
const act = () => context.validateRuntimeSanity();
|
const act = () => context.validateRuntimeSanity();
|
||||||
// assert
|
// assert
|
||||||
expect(act).to.throw(expectedError);
|
expect(act).to.throw(expectedError);
|
||||||
|
}, { excludeUndefined: true });
|
||||||
|
});
|
||||||
|
describe('throws when single validator is absent', () => {
|
||||||
|
itEachAbsentObjectValue((absentValue) => {
|
||||||
|
// arrange
|
||||||
|
const expectedError = 'missing validator in validators';
|
||||||
|
const absentValidator = absentValue;
|
||||||
|
const context = new TestContext()
|
||||||
|
.withValidators([new SanityValidatorStub(), absentValidator]);
|
||||||
|
// act
|
||||||
|
const act = () => context.validateRuntimeSanity();
|
||||||
|
// assert
|
||||||
|
expect(act).to.throw(expectedError);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -99,6 +115,35 @@ describe('SanityChecks', () => {
|
|||||||
expect(actualError).to.include(firstError);
|
expect(actualError).to.include(firstError);
|
||||||
expect(actualError).to.include(secondError);
|
expect(actualError).to.include(secondError);
|
||||||
});
|
});
|
||||||
|
it('throws with validators name', () => {
|
||||||
|
// arrange
|
||||||
|
const validatorWithErrors = 'validator-with-errors';
|
||||||
|
const validatorWithNoErrors = 'validator-with-no-errors';
|
||||||
|
let actualError = '';
|
||||||
|
const context = new TestContext()
|
||||||
|
.withValidators([
|
||||||
|
new SanityValidatorStub()
|
||||||
|
.withName(validatorWithErrors)
|
||||||
|
.withShouldValidateResult(true)
|
||||||
|
.withErrorsResult(['error']),
|
||||||
|
new SanityValidatorStub()
|
||||||
|
.withShouldValidateResult(true)
|
||||||
|
.withErrorsResult([]),
|
||||||
|
new SanityValidatorStub()
|
||||||
|
.withShouldValidateResult(true)
|
||||||
|
.withErrorsResult([]),
|
||||||
|
]);
|
||||||
|
// act
|
||||||
|
try {
|
||||||
|
context.validateRuntimeSanity();
|
||||||
|
} catch (err) {
|
||||||
|
actualError = err.toString();
|
||||||
|
}
|
||||||
|
// assert
|
||||||
|
expect(actualError).to.have.length.above(0);
|
||||||
|
expect(actualError).to.include(validatorWithErrors);
|
||||||
|
expect(actualError).to.not.include(validatorWithNoErrors);
|
||||||
|
});
|
||||||
it('accumulates error messages from validators', () => {
|
it('accumulates error messages from validators', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const errorFromFirstValidator = 'first-error';
|
const errorFromFirstValidator = 'first-error';
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { describe } from 'vitest';
|
||||||
|
import { EnvironmentValidator } from '@/infrastructure/RuntimeSanity/Validators/EnvironmentValidator';
|
||||||
|
import { itNoErrorsOnCurrentEnvironment } from './ValidatorTestRunner';
|
||||||
|
|
||||||
|
describe('EnvironmentValidator', () => {
|
||||||
|
itNoErrorsOnCurrentEnvironment(() => new EnvironmentValidator());
|
||||||
|
});
|
||||||
@@ -1,133 +1,7 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe } from 'vitest';
|
||||||
import { MetadataValidator } from '@/infrastructure/RuntimeSanity/Validators/MetadataValidator';
|
import { MetadataValidator } from '@/infrastructure/RuntimeSanity/Validators/MetadataValidator';
|
||||||
import { SanityCheckOptionsStub } from '@tests/unit/shared/Stubs/SanityCheckOptionsStub';
|
import { itNoErrorsOnCurrentEnvironment } from './ValidatorTestRunner';
|
||||||
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
|
||||||
import { AppMetadataStub } from '@tests/unit/shared/Stubs/AppMetadataStub';
|
|
||||||
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
|
||||||
|
|
||||||
describe('MetadataValidator', () => {
|
describe('MetadataValidator', () => {
|
||||||
describe('shouldValidate', () => {
|
itNoErrorsOnCurrentEnvironment(() => new MetadataValidator());
|
||||||
it('returns true when validateMetadata is true', () => {
|
|
||||||
// arrange
|
|
||||||
const expectedValue = true;
|
|
||||||
const options = new SanityCheckOptionsStub()
|
|
||||||
.withValidateMetadata(true);
|
|
||||||
const validator = new TestContext()
|
|
||||||
.createSut();
|
|
||||||
// act
|
|
||||||
const actualValue = validator.shouldValidate(options);
|
|
||||||
// assert
|
|
||||||
expect(actualValue).to.equal(expectedValue);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns false when validateMetadata is false', () => {
|
|
||||||
// arrange
|
|
||||||
const expectedValue = false;
|
|
||||||
const options = new SanityCheckOptionsStub()
|
|
||||||
.withValidateMetadata(false);
|
|
||||||
const validator = new TestContext()
|
|
||||||
.createSut();
|
|
||||||
// act
|
|
||||||
const actualValue = validator.shouldValidate(options);
|
|
||||||
// assert
|
|
||||||
expect(actualValue).to.equal(expectedValue);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('collectErrors', () => {
|
|
||||||
describe('yields "missing metadata" if metadata is not provided', () => {
|
|
||||||
itEachAbsentObjectValue((absentValue) => {
|
|
||||||
// arrange
|
|
||||||
const expectedError = 'missing metadata';
|
|
||||||
const validator = new TestContext()
|
|
||||||
.withMetadata(absentValue)
|
|
||||||
.createSut();
|
|
||||||
// act
|
|
||||||
const errors = [...validator.collectErrors()];
|
|
||||||
// assert
|
|
||||||
expect(errors).to.have.lengthOf(1);
|
|
||||||
expect(errors[0]).to.equal(expectedError);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it('yields missing keys if metadata has keys without values', () => {
|
|
||||||
// arrange
|
|
||||||
const expectedError = 'Metadata keys missing: name, homepageUrl';
|
|
||||||
const metadata = new AppMetadataStub()
|
|
||||||
.witName(undefined)
|
|
||||||
.withHomepageUrl(undefined);
|
|
||||||
const validator = new TestContext()
|
|
||||||
.withMetadata(metadata)
|
|
||||||
.createSut();
|
|
||||||
// act
|
|
||||||
const errors = [...validator.collectErrors()];
|
|
||||||
// assert
|
|
||||||
expect(errors).to.have.lengthOf(1);
|
|
||||||
expect(errors[0]).to.equal(expectedError);
|
|
||||||
});
|
|
||||||
it('yields missing keys if metadata has getters instead of properties', () => {
|
|
||||||
/*
|
|
||||||
This test may behave differently in unit testing vs. production due to how code
|
|
||||||
is transformed, especially around class getters and their enumerability during bundling.
|
|
||||||
*/
|
|
||||||
// arrange
|
|
||||||
const expectedError = 'Metadata keys missing: name, homepageUrl';
|
|
||||||
const stubWithGetters: Partial<IAppMetadata> = {
|
|
||||||
get name() {
|
|
||||||
return undefined;
|
|
||||||
},
|
|
||||||
get homepageUrl() {
|
|
||||||
return undefined;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const stub: IAppMetadata = {
|
|
||||||
...new AppMetadataStub(),
|
|
||||||
...stubWithGetters,
|
|
||||||
};
|
|
||||||
const validator = new TestContext()
|
|
||||||
.withMetadata(stub)
|
|
||||||
.createSut();
|
|
||||||
// act
|
|
||||||
const errors = [...validator.collectErrors()];
|
|
||||||
// assert
|
|
||||||
expect(errors).to.have.lengthOf(1);
|
|
||||||
expect(errors[0]).to.equal(expectedError);
|
|
||||||
});
|
|
||||||
it('yields unable to capture metadata if metadata has no getter values', () => {
|
|
||||||
// arrange
|
|
||||||
const expectedError = 'Unable to capture metadata key/value pairs';
|
|
||||||
const stub = {} as IAppMetadata;
|
|
||||||
const validator = new TestContext()
|
|
||||||
.withMetadata(stub)
|
|
||||||
.createSut();
|
|
||||||
// act
|
|
||||||
const errors = [...validator.collectErrors()];
|
|
||||||
// assert
|
|
||||||
expect(errors).to.have.lengthOf(1);
|
|
||||||
expect(errors[0]).to.equal(expectedError);
|
|
||||||
});
|
|
||||||
it('does not yield errors if all metadata keys have values', () => {
|
|
||||||
// arrange
|
|
||||||
const metadata = new AppMetadataStub();
|
|
||||||
const validator = new TestContext()
|
|
||||||
.withMetadata(metadata)
|
|
||||||
.createSut();
|
|
||||||
// act
|
|
||||||
const errors = [...validator.collectErrors()];
|
|
||||||
// assert
|
|
||||||
expect(errors).to.have.lengthOf(0);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
class TestContext {
|
|
||||||
public metadata: IAppMetadata = new AppMetadataStub();
|
|
||||||
|
|
||||||
public withMetadata(metadata: IAppMetadata): this {
|
|
||||||
this.metadata = metadata;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public createSut(): MetadataValidator {
|
|
||||||
const mockFactory = () => this.metadata;
|
|
||||||
return new MetadataValidator(mockFactory);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { it, expect } from 'vitest';
|
||||||
|
import { ISanityValidator } from '@/infrastructure/RuntimeSanity/Common/ISanityValidator';
|
||||||
|
|
||||||
|
export function itNoErrorsOnCurrentEnvironment(
|
||||||
|
factory: () => ISanityValidator,
|
||||||
|
) {
|
||||||
|
if (!factory) {
|
||||||
|
throw new Error('missing factory');
|
||||||
|
}
|
||||||
|
it('it does report errors on current environment', () => {
|
||||||
|
// arrange
|
||||||
|
const validator = factory();
|
||||||
|
// act
|
||||||
|
const errors = [...validator.collectErrors()];
|
||||||
|
// assert
|
||||||
|
expect(errors).to.have.lengthOf(0);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest';
|
|||||||
import { throttle, ITimer, TimeoutType } from '@/presentation/components/Shared/Throttle';
|
import { throttle, ITimer, TimeoutType } from '@/presentation/components/Shared/Throttle';
|
||||||
import { EventSource } from '@/infrastructure/Events/EventSource';
|
import { EventSource } from '@/infrastructure/Events/EventSource';
|
||||||
import { IEventSubscription } from '@/infrastructure/Events/IEventSource';
|
import { IEventSubscription } from '@/infrastructure/Events/IEventSource';
|
||||||
import { AbsentObjectTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
import { getAbsentObjectTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||||
|
|
||||||
describe('throttle', () => {
|
describe('throttle', () => {
|
||||||
describe('validates parameters', () => {
|
describe('validates parameters', () => {
|
||||||
@@ -30,7 +30,7 @@ describe('throttle', () => {
|
|||||||
value: -2,
|
value: -2,
|
||||||
expectedError: 'negative delay',
|
expectedError: 'negative delay',
|
||||||
},
|
},
|
||||||
...AbsentObjectTestCases.map((testCase) => ({
|
...getAbsentObjectTestCases().map((testCase) => ({
|
||||||
name: `when absent (given ${testCase.valueName})`,
|
name: `when absent (given ${testCase.valueName})`,
|
||||||
value: testCase.absentValue,
|
value: testCase.absentValue,
|
||||||
expectedError: 'missing delay',
|
expectedError: 'missing delay',
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import { describe } from 'vitest';
|
||||||
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
|
import { convertPlatformToOs } from '@/presentation/electron/preload/NodeOsMapper';
|
||||||
|
|
||||||
|
describe('NodeOsMapper', () => {
|
||||||
|
describe('convertPlatformToOs', () => {
|
||||||
|
describe('determines desktop OS', () => {
|
||||||
|
// arrange
|
||||||
|
interface IDesktopTestCase {
|
||||||
|
nodePlatform: NodeJS.Platform;
|
||||||
|
expectedOs: OperatingSystem;
|
||||||
|
}
|
||||||
|
const testCases: readonly IDesktopTestCase[] = [ // https://nodejs.org/api/process.html#process_process_platform
|
||||||
|
{
|
||||||
|
nodePlatform: 'aix',
|
||||||
|
expectedOs: undefined,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
nodePlatform: 'darwin',
|
||||||
|
expectedOs: OperatingSystem.macOS,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
nodePlatform: 'freebsd',
|
||||||
|
expectedOs: undefined,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
nodePlatform: 'linux',
|
||||||
|
expectedOs: OperatingSystem.Linux,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
nodePlatform: 'openbsd',
|
||||||
|
expectedOs: undefined,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
nodePlatform: 'sunos',
|
||||||
|
expectedOs: undefined,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
nodePlatform: 'win32',
|
||||||
|
expectedOs: OperatingSystem.Windows,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
testCases.forEach(({ nodePlatform, expectedOs }) => {
|
||||||
|
it(nodePlatform, () => {
|
||||||
|
// act
|
||||||
|
const actualOs = convertPlatformToOs(nodePlatform);
|
||||||
|
// assert
|
||||||
|
expect(actualOs).to.equal(expectedOs, printMessage());
|
||||||
|
function printMessage(): string {
|
||||||
|
return `Expected: "${OperatingSystem[expectedOs]}"\n`
|
||||||
|
+ `Actual: "${OperatingSystem[actualOs]}"\n`
|
||||||
|
+ `Platform: "${nodePlatform}"`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { provideWindowVariables } from '@/presentation/electron/preload/WindowVariablesProvider';
|
||||||
|
import { SystemOperationsStub } from '@tests/unit/shared/Stubs/SystemOperationsStub';
|
||||||
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
|
import { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||||
|
|
||||||
|
describe('WindowVariablesProvider', () => {
|
||||||
|
describe('provideWindowVariables', () => {
|
||||||
|
it('returns expected system', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedValue = new SystemOperationsStub();
|
||||||
|
// act
|
||||||
|
const variables = new TestContext()
|
||||||
|
.withSystem(expectedValue)
|
||||||
|
.provideWindowVariables();
|
||||||
|
// assert
|
||||||
|
expect(variables.system).to.equal(expectedValue);
|
||||||
|
});
|
||||||
|
it('returns expected os', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedValue = OperatingSystem.WindowsPhone;
|
||||||
|
// act
|
||||||
|
const variables = new TestContext()
|
||||||
|
.withOs(expectedValue)
|
||||||
|
.provideWindowVariables();
|
||||||
|
// assert
|
||||||
|
expect(variables.os).to.equal(expectedValue);
|
||||||
|
});
|
||||||
|
it('`isDesktop` is true', () => {
|
||||||
|
// arrange
|
||||||
|
const expectedValue = true;
|
||||||
|
// act
|
||||||
|
const variables = new TestContext()
|
||||||
|
.provideWindowVariables();
|
||||||
|
// assert
|
||||||
|
expect(variables.isDesktop).to.equal(expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
class TestContext {
|
||||||
|
private system: ISystemOperations = new SystemOperationsStub();
|
||||||
|
|
||||||
|
private os: OperatingSystem = OperatingSystem.Android;
|
||||||
|
|
||||||
|
public withSystem(system: ISystemOperations): this {
|
||||||
|
this.system = system;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public withOs(os: OperatingSystem): this {
|
||||||
|
this.os = os;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public provideWindowVariables() {
|
||||||
|
return provideWindowVariables(
|
||||||
|
() => this.system,
|
||||||
|
() => this.os,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
8
tests/unit/shared/Stubs/BrowserOsDetectorStub.ts
Normal file
8
tests/unit/shared/Stubs/BrowserOsDetectorStub.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
|
import { IBrowserOsDetector } from '@/infrastructure/Environment/BrowserOs/IBrowserOsDetector';
|
||||||
|
|
||||||
|
export class BrowserOsDetectorStub implements IBrowserOsDetector {
|
||||||
|
public detect(): OperatingSystem {
|
||||||
|
return OperatingSystem.BlackBerryTabletOS;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
tests/unit/shared/Stubs/CommandOpsStub.ts
Normal file
13
tests/unit/shared/Stubs/CommandOpsStub.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { ICommandOps } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||||
|
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
|
||||||
|
|
||||||
|
export class CommandOpsStub
|
||||||
|
extends StubWithObservableMethodCalls<ICommandOps>
|
||||||
|
implements ICommandOps {
|
||||||
|
public execute(command: string): void {
|
||||||
|
this.registerMethodCall({
|
||||||
|
methodName: 'execute',
|
||||||
|
args: [command],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,22 @@
|
|||||||
import { IEnvironment } from '@/application/Environment/IEnvironment';
|
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
|
||||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||||
|
import { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||||
|
import { SystemOperationsStub } from './SystemOperationsStub';
|
||||||
|
|
||||||
export class EnvironmentStub implements IEnvironment {
|
export class EnvironmentStub implements IEnvironment {
|
||||||
public isDesktop = true;
|
public isDesktop = true;
|
||||||
|
|
||||||
public os = OperatingSystem.Windows;
|
public os = OperatingSystem.Windows;
|
||||||
|
|
||||||
public withOs(os: OperatingSystem): EnvironmentStub {
|
public system: ISystemOperations = new SystemOperationsStub();
|
||||||
|
|
||||||
|
public withOs(os: OperatingSystem): this {
|
||||||
this.os = os;
|
this.os = os;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public withSystemOperations(system: ISystemOperations): this {
|
||||||
|
this.system = system;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
tests/unit/shared/Stubs/FileSystemOpsStub.ts
Normal file
30
tests/unit/shared/Stubs/FileSystemOpsStub.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { IFileSystemOps } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||||
|
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
|
||||||
|
|
||||||
|
export class FileSystemOpsStub
|
||||||
|
extends StubWithObservableMethodCalls<IFileSystemOps>
|
||||||
|
implements IFileSystemOps {
|
||||||
|
public setFilePermissions(filePath: string, mode: string | number): Promise<void> {
|
||||||
|
this.registerMethodCall({
|
||||||
|
methodName: 'setFilePermissions',
|
||||||
|
args: [filePath, mode],
|
||||||
|
});
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
public createDirectory(directoryPath: string, isRecursive?: boolean): Promise<string> {
|
||||||
|
this.registerMethodCall({
|
||||||
|
methodName: 'createDirectory',
|
||||||
|
args: [directoryPath, isRecursive],
|
||||||
|
});
|
||||||
|
return Promise.resolve(directoryPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public writeToFile(filePath: string, data: string): Promise<void> {
|
||||||
|
this.registerMethodCall({
|
||||||
|
methodName: 'writeToFile',
|
||||||
|
args: [filePath, data],
|
||||||
|
});
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
}
|
||||||
40
tests/unit/shared/Stubs/LocationOpsStub.ts
Normal file
40
tests/unit/shared/Stubs/LocationOpsStub.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { ILocationOps } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||||
|
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
|
||||||
|
|
||||||
|
export class LocationOpsStub
|
||||||
|
extends StubWithObservableMethodCalls<ILocationOps>
|
||||||
|
implements ILocationOps {
|
||||||
|
private sequence = new Array<string>();
|
||||||
|
|
||||||
|
private scenarios = new Map<string, string>();
|
||||||
|
|
||||||
|
public withJoinResult(returnValue: string, ...paths: string[]): this {
|
||||||
|
this.scenarios.set(LocationOpsStub.getScenarioKey(paths), returnValue);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public withJoinResultSequence(...valuesToReturn: string[]): this {
|
||||||
|
this.sequence.push(...valuesToReturn);
|
||||||
|
this.sequence.reverse();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public combinePaths(...pathSegments: string[]): string {
|
||||||
|
this.registerMethodCall({
|
||||||
|
methodName: 'combinePaths',
|
||||||
|
args: pathSegments,
|
||||||
|
});
|
||||||
|
if (this.sequence.length > 0) {
|
||||||
|
return this.sequence.pop();
|
||||||
|
}
|
||||||
|
const key = LocationOpsStub.getScenarioKey(pathSegments);
|
||||||
|
if (!this.scenarios.has(key)) {
|
||||||
|
return pathSegments.join('/PATH-SEGMENT-SEPARATOR/');
|
||||||
|
}
|
||||||
|
return this.scenarios.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getScenarioKey(paths: string[]): string {
|
||||||
|
return paths.join('|');
|
||||||
|
}
|
||||||
|
}
|
||||||
21
tests/unit/shared/Stubs/OperatingSystemOpsStub.ts
Normal file
21
tests/unit/shared/Stubs/OperatingSystemOpsStub.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import { IOperatingSystemOps } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||||
|
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
|
||||||
|
|
||||||
|
export class OperatingSystemOpsStub
|
||||||
|
extends StubWithObservableMethodCalls<IOperatingSystemOps>
|
||||||
|
implements IOperatingSystemOps {
|
||||||
|
private temporaryDirectory = '/stub-temp-dir/';
|
||||||
|
|
||||||
|
public withTemporaryDirectoryResult(directory: string): this {
|
||||||
|
this.temporaryDirectory = directory;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getTempDirectory(): string {
|
||||||
|
this.registerMethodCall({
|
||||||
|
methodName: 'getTempDirectory',
|
||||||
|
args: [],
|
||||||
|
});
|
||||||
|
return this.temporaryDirectory;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,17 @@
|
|||||||
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/ISanityCheckOptions';
|
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/Common/ISanityCheckOptions';
|
||||||
|
|
||||||
export class SanityCheckOptionsStub implements ISanityCheckOptions {
|
export class SanityCheckOptionsStub implements ISanityCheckOptions {
|
||||||
|
public validateEnvironment = false;
|
||||||
|
|
||||||
public validateMetadata = false;
|
public validateMetadata = false;
|
||||||
|
|
||||||
public withValidateMetadata(value: boolean): this {
|
public withValidateMetadata(value: boolean): this {
|
||||||
this.validateMetadata = value;
|
this.validateMetadata = value;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public withValidateEnvironment(value: boolean): this {
|
||||||
|
this.validateEnvironment = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/ISanityCheckOptions';
|
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/Common/ISanityCheckOptions';
|
||||||
import { ISanityValidator } from '@/infrastructure/RuntimeSanity/ISanityValidator';
|
import { ISanityValidator } from '@/infrastructure/RuntimeSanity/Common/ISanityValidator';
|
||||||
|
|
||||||
export class SanityValidatorStub implements ISanityValidator {
|
export class SanityValidatorStub implements ISanityValidator {
|
||||||
public shouldValidateArgs = new Array<ISanityCheckOptions>();
|
public shouldValidateArgs = new Array<ISanityCheckOptions>();
|
||||||
|
|
||||||
|
public name = 'sanity-validator-stub';
|
||||||
|
|
||||||
private errors: readonly string[] = [];
|
private errors: readonly string[] = [];
|
||||||
|
|
||||||
private shouldValidateResult = true;
|
private shouldValidateResult = true;
|
||||||
@@ -17,6 +19,11 @@ export class SanityValidatorStub implements ISanityValidator {
|
|||||||
return this.errors;
|
return this.errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public withName(name: string): this {
|
||||||
|
this.name = name;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public withErrorsResult(errors: readonly string[]): this {
|
public withErrorsResult(errors: readonly string[]): this {
|
||||||
this.errors = errors;
|
this.errors = errors;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
25
tests/unit/shared/Stubs/StubWithObservableMethodCalls.ts
Normal file
25
tests/unit/shared/Stubs/StubWithObservableMethodCalls.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { EventSource } from '@/infrastructure/Events/EventSource';
|
||||||
|
import { IEventSource } from '@/infrastructure/Events/IEventSource';
|
||||||
|
import { FunctionKeys } from '@/TypeHelpers';
|
||||||
|
|
||||||
|
export abstract class StubWithObservableMethodCalls<T> {
|
||||||
|
public readonly callHistory = new Array<MethodCall<T>>();
|
||||||
|
|
||||||
|
public get methodCalls(): IEventSource<MethodCall<T>> {
|
||||||
|
return this.notifiableMethodCalls;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly notifiableMethodCalls = new EventSource<MethodCall<T>>();
|
||||||
|
|
||||||
|
protected registerMethodCall(name: MethodCall<T>) {
|
||||||
|
this.callHistory.push(name);
|
||||||
|
this.notifiableMethodCalls.notify(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type MethodCall<T> = {
|
||||||
|
[K in FunctionKeys<T>]: {
|
||||||
|
methodName: K;
|
||||||
|
args: T[K] extends (...args: infer A) => unknown ? A : never;
|
||||||
|
}
|
||||||
|
}[FunctionKeys<T>];
|
||||||
41
tests/unit/shared/Stubs/SystemOperationsStub.ts
Normal file
41
tests/unit/shared/Stubs/SystemOperationsStub.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import {
|
||||||
|
ICommandOps,
|
||||||
|
IFileSystemOps,
|
||||||
|
IOperatingSystemOps,
|
||||||
|
ILocationOps,
|
||||||
|
ISystemOperations,
|
||||||
|
} from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||||
|
import { CommandOpsStub } from './CommandOpsStub';
|
||||||
|
import { FileSystemOpsStub } from './FileSystemOpsStub';
|
||||||
|
import { LocationOpsStub } from './LocationOpsStub';
|
||||||
|
import { OperatingSystemOpsStub } from './OperatingSystemOpsStub';
|
||||||
|
|
||||||
|
export class SystemOperationsStub implements ISystemOperations {
|
||||||
|
public operatingSystem: IOperatingSystemOps = new OperatingSystemOpsStub();
|
||||||
|
|
||||||
|
public location: ILocationOps = new LocationOpsStub();
|
||||||
|
|
||||||
|
public fileSystem: IFileSystemOps = new FileSystemOpsStub();
|
||||||
|
|
||||||
|
public command: ICommandOps = new CommandOpsStub();
|
||||||
|
|
||||||
|
public withOperatingSystem(operatingSystemOps: IOperatingSystemOps): this {
|
||||||
|
this.operatingSystem = operatingSystemOps;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public withLocation(location: ILocationOps): this {
|
||||||
|
this.location = location;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public withFileSystem(fileSystem: IFileSystemOps): this {
|
||||||
|
this.fileSystem = fileSystem;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public withCommand(command: ICommandOps): this {
|
||||||
|
this.command = command;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,24 @@
|
|||||||
import { it } from 'vitest';
|
import { it } from 'vitest';
|
||||||
|
|
||||||
export function itEachAbsentStringValue(runner: (absentValue: string) => void): void {
|
export function itEachAbsentStringValue(
|
||||||
itEachAbsentTestCase(AbsentStringTestCases, runner);
|
runner: (absentValue: string) => void,
|
||||||
|
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||||
|
): void {
|
||||||
|
itEachAbsentTestCase(getAbsentStringTestCases(options), runner);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function itEachAbsentObjectValue(
|
export function itEachAbsentObjectValue(
|
||||||
runner: (absentValue: AbsentObjectType) => void,
|
runner: (absentValue: AbsentObjectType) => void,
|
||||||
|
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||||
): void {
|
): void {
|
||||||
itEachAbsentTestCase(AbsentObjectTestCases, runner);
|
itEachAbsentTestCase(getAbsentObjectTestCases(options), runner);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function itEachAbsentCollectionValue<T>(runner: (absentValue: []) => void): void {
|
export function itEachAbsentCollectionValue<T>(
|
||||||
itEachAbsentTestCase(getAbsentCollectionTestCases<T>(), runner);
|
runner: (absentValue: []) => void,
|
||||||
|
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||||
|
): void {
|
||||||
|
itEachAbsentTestCase(getAbsentCollectionTestCases<T>(options), runner);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function itEachAbsentTestCase<T>(
|
export function itEachAbsentTestCase<T>(
|
||||||
@@ -25,28 +32,40 @@ export function itEachAbsentTestCase<T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AbsentObjectTestCases: readonly IAbsentTestCase<AbsentObjectType>[] = [
|
export function getAbsentObjectTestCases(
|
||||||
{
|
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||||
valueName: 'undefined',
|
): IAbsentTestCase<AbsentObjectType>[] {
|
||||||
absentValue: undefined,
|
return [
|
||||||
},
|
|
||||||
{
|
{
|
||||||
valueName: 'null',
|
valueName: 'null',
|
||||||
absentValue: null,
|
absentValue: null,
|
||||||
},
|
},
|
||||||
|
...(options.excludeUndefined ? [] : [
|
||||||
|
{
|
||||||
|
valueName: 'undefined',
|
||||||
|
absentValue: undefined,
|
||||||
|
},
|
||||||
|
]),
|
||||||
];
|
];
|
||||||
|
}
|
||||||
|
|
||||||
export const AbsentStringTestCases: readonly IAbsentStringCase[] = [
|
export function getAbsentStringTestCases(
|
||||||
|
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||||
|
): IAbsentStringCase[] {
|
||||||
|
return [
|
||||||
{
|
{
|
||||||
valueName: 'empty',
|
valueName: 'empty',
|
||||||
absentValue: '',
|
absentValue: '',
|
||||||
},
|
},
|
||||||
...AbsentObjectTestCases,
|
...getAbsentObjectTestCases(options),
|
||||||
];
|
];
|
||||||
|
}
|
||||||
|
|
||||||
export function getAbsentCollectionTestCases<T>(): readonly IAbsentCollectionCase<T>[] {
|
export function getAbsentCollectionTestCases<T>(
|
||||||
|
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||||
|
): readonly IAbsentCollectionCase<T>[] {
|
||||||
return [
|
return [
|
||||||
...AbsentObjectTestCases,
|
...getAbsentObjectTestCases(options),
|
||||||
{
|
{
|
||||||
valueName: 'empty',
|
valueName: 'empty',
|
||||||
absentValue: new Array<T>(),
|
absentValue: new Array<T>(),
|
||||||
@@ -54,6 +73,14 @@ export function getAbsentCollectionTestCases<T>(): readonly IAbsentCollectionCas
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DefaultAbsentTestCaseOptions: IAbsentTestCaseOptions = {
|
||||||
|
excludeUndefined: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
interface IAbsentTestCaseOptions {
|
||||||
|
readonly excludeUndefined: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
type AbsentObjectType = undefined | null;
|
type AbsentObjectType = undefined | null;
|
||||||
|
|
||||||
interface IAbsentTestCase<T> {
|
interface IAbsentTestCase<T> {
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import { getAliasesFromTsConfig, getClientEnvironmentVariables, getSelfDirectory
|
|||||||
|
|
||||||
const WEB_DIRECTORY = resolve(getSelfDirectoryAbsolutePath(), 'src/presentation');
|
const WEB_DIRECTORY = resolve(getSelfDirectoryAbsolutePath(), 'src/presentation');
|
||||||
const TEST_INITIALIZATION_FILE = resolve(getSelfDirectoryAbsolutePath(), 'tests/shared/bootstrap/setup.ts');
|
const TEST_INITIALIZATION_FILE = resolve(getSelfDirectoryAbsolutePath(), 'tests/shared/bootstrap/setup.ts');
|
||||||
const NODE_CORE_MODULES = ['os', 'child_process', 'fs', 'path'];
|
|
||||||
|
|
||||||
export function createVueConfig(options?: {
|
export function createVueConfig(options?: {
|
||||||
readonly supportLegacyBrowsers: boolean,
|
readonly supportLegacyBrowsers: boolean,
|
||||||
@@ -33,14 +32,6 @@ export function createVueConfig(options?: {
|
|||||||
...getAliasesFromTsConfig(),
|
...getAliasesFromTsConfig(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
build: {
|
|
||||||
rollupOptions: {
|
|
||||||
// Ensure Node core modules are externalized and don't trigger warnings in browser builds
|
|
||||||
external: {
|
|
||||||
...NODE_CORE_MODULES,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
server: {
|
server: {
|
||||||
port: 3169,
|
port: 3169,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user