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:
@@ -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,
|
||||
} from '@/application/Common/Enum';
|
||||
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';
|
||||
|
||||
describe('Enum', () => {
|
||||
@@ -37,7 +37,7 @@ describe('Enum', () => {
|
||||
// arrange
|
||||
const enumName = 'ParsableEnum';
|
||||
const testCases = [
|
||||
...AbsentStringTestCases.map((test) => ({
|
||||
...getAbsentStringTestCases().map((test) => ({
|
||||
name: test.valueName,
|
||||
value: test.absentValue,
|
||||
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 { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
||||
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 { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
|
||||
import { CategoryCollectionParserStub } from '@tests/unit/shared/Stubs/CategoryCollectionParserStub';
|
||||
@@ -85,7 +85,7 @@ describe('ApplicationParser', () => {
|
||||
});
|
||||
it('defaults to metadata from factory', () => {
|
||||
// arrange
|
||||
const expectedMetadata = AppMetadataFactory.Current;
|
||||
const expectedMetadata: IAppMetadata = AppMetadataFactory.Current.instance;
|
||||
const infoParserStub = new ProjectInformationParserStub();
|
||||
// act
|
||||
new ApplicationParserBuilder()
|
||||
@@ -157,7 +157,7 @@ describe('ApplicationParser', () => {
|
||||
value: testCase.absentValue,
|
||||
expectedError: 'missing collections',
|
||||
})).filter((test) => test.value !== undefined /* the default value is set */),
|
||||
...AbsentObjectTestCases.map((testCase) => ({
|
||||
...getAbsentObjectTestCases().map((testCase) => ({
|
||||
name: `given absent item "${testCase.valueName}"`,
|
||||
value: [testCase.absentValue],
|
||||
expectedError: 'missing collection provided',
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, it } from 'vitest';
|
||||
import { NodeDataError, INodeDataErrorContext } from '@/application/Parser/NodeValidation/NodeDataError';
|
||||
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';
|
||||
|
||||
export interface ITestScenario {
|
||||
@@ -16,7 +16,7 @@ export class NodeValidationTestRunner {
|
||||
describe('throws given invalid names', () => {
|
||||
// arrange
|
||||
const testCases = [
|
||||
...AbsentStringTestCases.map((testCase) => ({
|
||||
...getAbsentStringTestCases().map((testCase) => ({
|
||||
testName: `missing name (${testCase.valueName})`,
|
||||
nameValue: testCase.absentValue,
|
||||
expectedMessage: 'missing name',
|
||||
@@ -42,7 +42,7 @@ export class NodeValidationTestRunner {
|
||||
) {
|
||||
describe('throws given missing node data', () => {
|
||||
itEachAbsentTestCase([
|
||||
...AbsentObjectTestCases,
|
||||
...getAbsentObjectTestCases(),
|
||||
{
|
||||
valueName: 'empty object',
|
||||
absentValue: {},
|
||||
|
||||
@@ -9,7 +9,7 @@ import { ExpressionEvaluationContextStub } from '@tests/unit/shared/Stubs/Expres
|
||||
import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipelineCompiler';
|
||||
import { PipelineCompilerStub } from '@tests/unit/shared/Stubs/PipelineCompilerStub';
|
||||
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';
|
||||
|
||||
describe('Expression', () => {
|
||||
@@ -91,7 +91,7 @@ describe('Expression', () => {
|
||||
expectedError: string,
|
||||
sutBuilder?: (builder: ExpressionBuilder) => ExpressionBuilder,
|
||||
}[] = [
|
||||
...AbsentObjectTestCases.map((testCase) => ({
|
||||
...getAbsentObjectTestCases().map((testCase) => ({
|
||||
name: `throws if arguments is ${testCase.valueName}`,
|
||||
context: testCase.absentValue,
|
||||
expectedError: 'missing context',
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe } from 'vitest';
|
||||
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';
|
||||
|
||||
describe('EscapeDoubleQuotes', () => {
|
||||
@@ -23,7 +23,7 @@ describe('EscapeDoubleQuotes', () => {
|
||||
input: '""hello world""',
|
||||
expectedOutput: '"^"""^""hello world"^"""^""',
|
||||
},
|
||||
...AbsentStringTestCases.map((testCase) => ({
|
||||
...getAbsentStringTestCases().map((testCase) => ({
|
||||
name: 'returns as it is when if input is missing',
|
||||
input: testCase.absentValue,
|
||||
expectedOutput: testCase.absentValue,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { PipeFactory } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory';
|
||||
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('ctor', () => {
|
||||
@@ -82,7 +82,7 @@ describe('PipeFactory', () => {
|
||||
function testPipeNameValidation(testRunner: (invalidName: string) => void) {
|
||||
const testCases = [
|
||||
// Validate missing value
|
||||
...AbsentStringTestCases.map((testCase) => ({
|
||||
...getAbsentStringTestCases().map((testCase) => ({
|
||||
name: `empty pipe name (${testCase.valueName})`,
|
||||
value: testCase.absentValue,
|
||||
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 { PipeStub } from '@tests/unit/shared/Stubs/PipeStub';
|
||||
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('compile', () => {
|
||||
@@ -15,12 +15,12 @@ describe('PipelineCompiler', () => {
|
||||
expectedError: string;
|
||||
}
|
||||
const testCases: ITestCase[] = [
|
||||
...AbsentStringTestCases.map((testCase) => ({
|
||||
...getAbsentStringTestCases().map((testCase) => ({
|
||||
name: `"value" is ${testCase.valueName}`,
|
||||
act: (test) => test.withValue(testCase.absentValue),
|
||||
expectedError: 'missing value',
|
||||
})),
|
||||
...AbsentStringTestCases.map((testCase) => ({
|
||||
...getAbsentStringTestCases().map((testCase) => ({
|
||||
name: `"pipeline" is ${testCase.valueName}`,
|
||||
act: (test) => test.withPipeline(testCase.absentValue),
|
||||
expectedError: 'missing pipeline',
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe } from 'vitest';
|
||||
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
|
||||
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';
|
||||
|
||||
describe('WithParser', () => {
|
||||
@@ -86,7 +86,7 @@ describe('WithParser', () => {
|
||||
describe('renders scope conditionally', () => {
|
||||
describe('does not render scope if argument is undefined', () => {
|
||||
runner.expectResults(
|
||||
...AbsentStringTestCases.map((testCase) => ({
|
||||
...getAbsentStringTestCases().map((testCase) => ({
|
||||
name: `does not render when value is "${testCase.valueName}"`,
|
||||
code: '{{ with $parameter }}dark{{ end }} ',
|
||||
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 { FunctionBodyType, ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
|
||||
import {
|
||||
AbsentStringTestCases, itEachAbsentCollectionValue, itEachAbsentObjectValue,
|
||||
getAbsentStringTestCases, itEachAbsentCollectionValue, itEachAbsentObjectValue,
|
||||
itEachAbsentStringValue,
|
||||
} from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
|
||||
@@ -100,7 +100,7 @@ describe('SharedFunction', () => {
|
||||
// arrange
|
||||
const testData = [
|
||||
'expected-revert-code',
|
||||
...AbsentStringTestCases.map((testCase) => testCase.absentValue),
|
||||
...getAbsentStringTestCases().map((testCase) => testCase.absentValue),
|
||||
];
|
||||
for (const data of testData) {
|
||||
// act
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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) {
|
||||
describe('name', () => {
|
||||
@@ -22,7 +22,7 @@ export function testParameterName(action: (parameterName: string) => string) {
|
||||
describe('throws if invalid', () => {
|
||||
// arrange
|
||||
const testCases = [
|
||||
...AbsentStringTestCases.map((test) => ({
|
||||
...getAbsentStringTestCases().map((test) => ({
|
||||
name: test.valueName,
|
||||
value: test.absentValue,
|
||||
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 { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
|
||||
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('throws with invalid parameters', () => {
|
||||
// arrange
|
||||
const testCases = [
|
||||
...AbsentStringTestCases.map((testCase) => ({
|
||||
...getAbsentStringTestCases().map((testCase) => ({
|
||||
name: `given code: ${testCase.valueName}`,
|
||||
expectedError: 'missing code',
|
||||
parameters: {
|
||||
@@ -17,7 +17,7 @@ describe('CodeSubstituter', () => {
|
||||
info: new ProjectInformationStub(),
|
||||
},
|
||||
})),
|
||||
...AbsentObjectTestCases.map((testCase) => ({
|
||||
...getAbsentObjectTestCases().map((testCase) => ({
|
||||
name: `given info: ${testCase.valueName}`,
|
||||
expectedError: 'missing info',
|
||||
parameters: {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Application } from '@/domain/Application';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
||||
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';
|
||||
|
||||
describe('Application', () => {
|
||||
@@ -69,7 +69,7 @@ describe('Application', () => {
|
||||
expectedError: 'missing collections',
|
||||
value: testCase.absentValue,
|
||||
})),
|
||||
...AbsentObjectTestCases.map((testCase) => ({
|
||||
...getAbsentObjectTestCases().map((testCase) => ({
|
||||
name: `${testCase.valueName} value in list`,
|
||||
expectedError: 'missing collection in the list',
|
||||
value: [new CategoryCollectionStub(), testCase.absentValue],
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { ScriptCode } from '@/domain/ScriptCode';
|
||||
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
|
||||
describe('ScriptCode', () => {
|
||||
describe('code', () => {
|
||||
@@ -15,7 +15,7 @@ describe('ScriptCode', () => {
|
||||
},
|
||||
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"`,
|
||||
code: {
|
||||
execute: testCase.absentValue,
|
||||
|
||||
@@ -3,16 +3,48 @@ import { EnvironmentStub } from '@tests/unit/shared/Stubs/EnvironmentStub';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { CodeRunner } from '@/infrastructure/CodeRunner';
|
||||
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('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', () => {
|
||||
it('creates temporary directory recursively', async () => {
|
||||
// arrange
|
||||
const expectedDir = 'expected-dir';
|
||||
const expectedIsRecursive = true;
|
||||
|
||||
const folderName = 'privacy.sexy';
|
||||
const context = new TestContext();
|
||||
context.mocks.os.setupTmpdir('tmp');
|
||||
context.mocks.path.setupJoin(expectedDir, 'tmp', folderName);
|
||||
const temporaryDirName = 'tmp';
|
||||
const filesystem = new FileSystemOpsStub();
|
||||
const context = new TestContext()
|
||||
.withSystemOperationsStub((ops) => ops
|
||||
.withOperatingSystem(
|
||||
new OperatingSystemOpsStub()
|
||||
.withTemporaryDirectoryResult(temporaryDirName),
|
||||
)
|
||||
.withLocation(
|
||||
new LocationOpsStub()
|
||||
.withJoinResult(expectedDir, temporaryDirName, folderName),
|
||||
)
|
||||
.withFileSystem(filesystem));
|
||||
|
||||
// act
|
||||
await context
|
||||
@@ -20,22 +52,34 @@ describe('CodeRunner', () => {
|
||||
.runCode();
|
||||
|
||||
// assert
|
||||
expect(context.mocks.fs.mkdirHistory.length).to.equal(1);
|
||||
expect(context.mocks.fs.mkdirHistory[0].isRecursive).to.equal(true);
|
||||
expect(context.mocks.fs.mkdirHistory[0].path).to.equal(expectedDir);
|
||||
const calls = filesystem.callHistory.filter((call) => call.methodName === 'createDirectory');
|
||||
expect(calls.length).to.equal(1);
|
||||
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 () => {
|
||||
// arrange
|
||||
const expectedCode = 'expected-code';
|
||||
const expectedFilePath = 'expected-file-path';
|
||||
|
||||
const filesystem = new FileSystemOpsStub();
|
||||
const extension = '.sh';
|
||||
const expectedName = `run.${extension}`;
|
||||
const folderName = 'privacy.sexy';
|
||||
const context = new TestContext();
|
||||
context.mocks.os.setupTmpdir('tmp');
|
||||
context.mocks.path.setupJoin('folder', 'tmp', folderName);
|
||||
context.mocks.path.setupJoin(expectedFilePath, 'folder', expectedName);
|
||||
const temporaryDirName = 'tmp';
|
||||
const context = new TestContext()
|
||||
.withSystemOperationsStub((ops) => ops
|
||||
.withOperatingSystem(
|
||||
new OperatingSystemOpsStub()
|
||||
.withTemporaryDirectoryResult(temporaryDirName),
|
||||
)
|
||||
.withLocation(
|
||||
new LocationOpsStub()
|
||||
.withJoinResult('folder', temporaryDirName, folderName)
|
||||
.withJoinResult(expectedFilePath, 'folder', expectedName),
|
||||
)
|
||||
.withFileSystem(filesystem));
|
||||
|
||||
// act
|
||||
await context
|
||||
@@ -45,22 +89,34 @@ describe('CodeRunner', () => {
|
||||
.runCode();
|
||||
|
||||
// assert
|
||||
expect(context.mocks.fs.writeFileHistory.length).to.equal(1);
|
||||
expect(context.mocks.fs.writeFileHistory[0].data).to.equal(expectedCode);
|
||||
expect(context.mocks.fs.writeFileHistory[0].path).to.equal(expectedFilePath);
|
||||
const calls = filesystem.callHistory.filter((call) => call.methodName === 'writeToFile');
|
||||
expect(calls.length).to.equal(1);
|
||||
const [actualFilePath, actualData] = calls[0].args;
|
||||
expect(actualFilePath).to.equal(expectedFilePath);
|
||||
expect(actualData).to.equal(expectedCode);
|
||||
});
|
||||
it('set file permissions as expected', async () => {
|
||||
// arrange
|
||||
const expectedMode = '755';
|
||||
const expectedFilePath = 'expected-file-path';
|
||||
|
||||
const filesystem = new FileSystemOpsStub();
|
||||
const extension = '.sh';
|
||||
const expectedName = `run.${extension}`;
|
||||
const folderName = 'privacy.sexy';
|
||||
const context = new TestContext();
|
||||
context.mocks.os.setupTmpdir('tmp');
|
||||
context.mocks.path.setupJoin('folder', 'tmp', folderName);
|
||||
context.mocks.path.setupJoin(expectedFilePath, 'folder', expectedName);
|
||||
const temporaryDirName = 'tmp';
|
||||
const context = new TestContext()
|
||||
.withSystemOperationsStub((ops) => ops
|
||||
.withOperatingSystem(
|
||||
new OperatingSystemOpsStub()
|
||||
.withTemporaryDirectoryResult(temporaryDirName),
|
||||
)
|
||||
.withLocation(
|
||||
new LocationOpsStub()
|
||||
.withJoinResult('folder', temporaryDirName, folderName)
|
||||
.withJoinResult(expectedFilePath, 'folder', expectedName),
|
||||
)
|
||||
.withFileSystem(filesystem));
|
||||
|
||||
// act
|
||||
await context
|
||||
@@ -69,57 +125,74 @@ describe('CodeRunner', () => {
|
||||
.runCode();
|
||||
|
||||
// assert
|
||||
expect(context.mocks.fs.chmodCallHistory.length).to.equal(1);
|
||||
expect(context.mocks.fs.chmodCallHistory[0].mode).to.equal(expectedMode);
|
||||
expect(context.mocks.fs.chmodCallHistory[0].path).to.equal(expectedFilePath);
|
||||
const calls = filesystem.callHistory.filter((call) => call.methodName === 'setFilePermissions');
|
||||
expect(calls.length).to.equal(1);
|
||||
const [actualFilePath, actualMode] = calls[0].args;
|
||||
expect(actualFilePath).to.equal(expectedFilePath);
|
||||
expect(actualMode).to.equal(expectedMode);
|
||||
});
|
||||
describe('executes as expected', () => {
|
||||
// arrange
|
||||
const filePath = 'expected-file-path';
|
||||
const testData = [
|
||||
interface IExecutionTestCase {
|
||||
readonly givenOs: OperatingSystem;
|
||||
readonly expectedCommand: string;
|
||||
}
|
||||
const testData: readonly IExecutionTestCase[] = [
|
||||
{
|
||||
os: OperatingSystem.Windows,
|
||||
expected: filePath,
|
||||
givenOs: OperatingSystem.Windows,
|
||||
expectedCommand: filePath,
|
||||
},
|
||||
{
|
||||
os: OperatingSystem.macOS,
|
||||
expected: `open -a Terminal.app ${filePath}`,
|
||||
givenOs: OperatingSystem.macOS,
|
||||
expectedCommand: `open -a Terminal.app ${filePath}`,
|
||||
},
|
||||
{
|
||||
os: OperatingSystem.Linux,
|
||||
expected: `x-terminal-emulator -e '${filePath}'`,
|
||||
givenOs: OperatingSystem.Linux,
|
||||
expectedCommand: `x-terminal-emulator -e '${filePath}'`,
|
||||
},
|
||||
];
|
||||
for (const data of testData) {
|
||||
it(`returns ${data.expected} on ${OperatingSystem[data.os]}`, async () => {
|
||||
const context = new TestContext();
|
||||
context.mocks.os.setupTmpdir('non-important-temp-dir-name');
|
||||
context.mocks.path.setupJoinSequence('non-important-folder-name', filePath);
|
||||
context.withOs(data.os);
|
||||
for (const { givenOs, expectedCommand } of testData) {
|
||||
it(`returns ${expectedCommand} on ${OperatingSystem[givenOs]}`, async () => {
|
||||
const command = new CommandOpsStub();
|
||||
const context = new TestContext()
|
||||
.withSystemOperationsStub((ops) => ops
|
||||
.withLocation(
|
||||
new LocationOpsStub()
|
||||
.withJoinResultSequence('non-important-folder-name', filePath),
|
||||
)
|
||||
.withCommand(command));
|
||||
|
||||
// act
|
||||
await context
|
||||
.withOs(data.os)
|
||||
.withOs(givenOs)
|
||||
.runCode();
|
||||
|
||||
// assert
|
||||
expect(context.mocks.child_process.executionHistory.length).to.equal(1);
|
||||
expect(context.mocks.child_process.executionHistory[0]).to.equal(data.expected);
|
||||
const calls = command.callHistory.filter((c) => c.methodName === 'execute');
|
||||
expect(calls.length).to.equal(1);
|
||||
const [actualCommand] = calls[0].args;
|
||||
expect(actualCommand).to.equal(expectedCommand);
|
||||
});
|
||||
}
|
||||
});
|
||||
it('runs in expected order', async () => {
|
||||
// arrange
|
||||
const expectedOrder = [NodeJsCommand.mkdir, NodeJsCommand.writeFile, NodeJsCommand.chmod];
|
||||
const context = new TestContext();
|
||||
context.mocks.os.setupTmpdir('non-important-temp-dir-name');
|
||||
context.mocks.path.setupJoinSequence('non-important-folder-name1', 'non-important-folder-name2');
|
||||
it('runs in expected order', async () => { // verifies correct `async`, `await` usage.
|
||||
const expectedOrder: readonly FunctionKeys<IFileSystemOps>[] = [
|
||||
'createDirectory',
|
||||
'writeToFile',
|
||||
'setFilePermissions',
|
||||
];
|
||||
const fileSystem = new FileSystemOpsStub();
|
||||
const context = new TestContext()
|
||||
.withSystemOperationsStub((ops) => ops
|
||||
.withFileSystem(fileSystem));
|
||||
|
||||
// act
|
||||
await context.runCode();
|
||||
|
||||
// assert
|
||||
const actualOrder = context.mocks.commandHistory
|
||||
const actualOrder = fileSystem.callHistory
|
||||
.map((c) => c.methodName)
|
||||
.filter((command) => expectedOrder.includes(command));
|
||||
expect(expectedOrder).to.deep.equal(actualOrder);
|
||||
});
|
||||
@@ -138,23 +211,40 @@ describe('CodeRunner', () => {
|
||||
});
|
||||
|
||||
class TestContext {
|
||||
public mocks = getNodeJsMocks();
|
||||
|
||||
private code = 'code';
|
||||
|
||||
private folderName = 'folderName';
|
||||
|
||||
private fileExtension = 'fileExtension';
|
||||
|
||||
private env = mockEnvironment(OperatingSystem.Windows);
|
||||
private os = OperatingSystem.Windows;
|
||||
|
||||
private systemOperations: ISystemOperations = new SystemOperationsStub();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
this.env = mockEnvironment(os);
|
||||
this.os = os;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -173,104 +263,3 @@ class TestContext {
|
||||
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 { 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 { 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,
|
||||
} from 'vitest';
|
||||
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 { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||
|
||||
class TestableAppMetadataFactory extends AppMetadataFactory {
|
||||
public constructor(validator: MetadataValidator = () => { /* NO OP */ }) {
|
||||
super(validator);
|
||||
}
|
||||
}
|
||||
|
||||
describe('AppMetadataFactory', () => {
|
||||
describe('instance', () => {
|
||||
itIsSingleton({
|
||||
getter: () => AppMetadataFactory.Current,
|
||||
getter: () => AppMetadataFactory.Current.instance,
|
||||
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];
|
||||
readonly expected: string;
|
||||
}
|
||||
const testCases: { [K in PropertyKeys<ViteAppMetadata>]: ITestCase } = {
|
||||
const testCases: { readonly [K in PropertyKeys<ViteAppMetadata>]: ITestCase } = {
|
||||
name: {
|
||||
environmentVariable: VITE_ENVIRONMENT_KEYS.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 { 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 { ISanityValidator } from '@/infrastructure/RuntimeSanity/ISanityValidator';
|
||||
import { ISanityValidator } from '@/infrastructure/RuntimeSanity/Common/ISanityValidator';
|
||||
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('validateRuntimeSanity', () => {
|
||||
@@ -21,15 +21,31 @@ describe('SanityChecks', () => {
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
it('throws when validators are empty', () => {
|
||||
// arrange
|
||||
const expectedError = 'missing validators';
|
||||
const context = new TestContext()
|
||||
.withValidators([]);
|
||||
// act
|
||||
const act = () => context.validateRuntimeSanity();
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
describe('throws when validators are empty', () => {
|
||||
itEachAbsentCollectionValue((absentCollection) => {
|
||||
// arrange
|
||||
const expectedError = 'missing validators';
|
||||
const validators = absentCollection;
|
||||
const context = new TestContext()
|
||||
.withValidators(validators);
|
||||
// act
|
||||
const act = () => context.validateRuntimeSanity();
|
||||
// assert
|
||||
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(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', () => {
|
||||
// arrange
|
||||
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 { SanityCheckOptionsStub } from '@tests/unit/shared/Stubs/SanityCheckOptionsStub';
|
||||
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
import { AppMetadataStub } from '@tests/unit/shared/Stubs/AppMetadataStub';
|
||||
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||
import { itNoErrorsOnCurrentEnvironment } from './ValidatorTestRunner';
|
||||
|
||||
describe('MetadataValidator', () => {
|
||||
describe('shouldValidate', () => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
itNoErrorsOnCurrentEnvironment(() => new MetadataValidator());
|
||||
});
|
||||
|
||||
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 { EventSource } from '@/infrastructure/Events/EventSource';
|
||||
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('validates parameters', () => {
|
||||
@@ -30,7 +30,7 @@ describe('throttle', () => {
|
||||
value: -2,
|
||||
expectedError: 'negative delay',
|
||||
},
|
||||
...AbsentObjectTestCases.map((testCase) => ({
|
||||
...getAbsentObjectTestCases().map((testCase) => ({
|
||||
name: `when absent (given ${testCase.valueName})`,
|
||||
value: testCase.absentValue,
|
||||
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 { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||
import { SystemOperationsStub } from './SystemOperationsStub';
|
||||
|
||||
export class EnvironmentStub implements IEnvironment {
|
||||
public isDesktop = true;
|
||||
|
||||
public os = OperatingSystem.Windows;
|
||||
|
||||
public withOs(os: OperatingSystem): EnvironmentStub {
|
||||
public system: ISystemOperations = new SystemOperationsStub();
|
||||
|
||||
public withOs(os: OperatingSystem): this {
|
||||
this.os = os;
|
||||
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 {
|
||||
public validateEnvironment = false;
|
||||
|
||||
public validateMetadata = false;
|
||||
|
||||
public withValidateMetadata(value: boolean): this {
|
||||
this.validateMetadata = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withValidateEnvironment(value: boolean): this {
|
||||
this.validateEnvironment = value;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/ISanityCheckOptions';
|
||||
import { ISanityValidator } from '@/infrastructure/RuntimeSanity/ISanityValidator';
|
||||
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/Common/ISanityCheckOptions';
|
||||
import { ISanityValidator } from '@/infrastructure/RuntimeSanity/Common/ISanityValidator';
|
||||
|
||||
export class SanityValidatorStub implements ISanityValidator {
|
||||
public shouldValidateArgs = new Array<ISanityCheckOptions>();
|
||||
|
||||
public name = 'sanity-validator-stub';
|
||||
|
||||
private errors: readonly string[] = [];
|
||||
|
||||
private shouldValidateResult = true;
|
||||
@@ -17,6 +19,11 @@ export class SanityValidatorStub implements ISanityValidator {
|
||||
return this.errors;
|
||||
}
|
||||
|
||||
public withName(name: string): this {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withErrorsResult(errors: readonly string[]): this {
|
||||
this.errors = errors;
|
||||
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';
|
||||
|
||||
export function itEachAbsentStringValue(runner: (absentValue: string) => void): void {
|
||||
itEachAbsentTestCase(AbsentStringTestCases, runner);
|
||||
export function itEachAbsentStringValue(
|
||||
runner: (absentValue: string) => void,
|
||||
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||
): void {
|
||||
itEachAbsentTestCase(getAbsentStringTestCases(options), runner);
|
||||
}
|
||||
|
||||
export function itEachAbsentObjectValue(
|
||||
runner: (absentValue: AbsentObjectType) => void,
|
||||
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||
): void {
|
||||
itEachAbsentTestCase(AbsentObjectTestCases, runner);
|
||||
itEachAbsentTestCase(getAbsentObjectTestCases(options), runner);
|
||||
}
|
||||
|
||||
export function itEachAbsentCollectionValue<T>(runner: (absentValue: []) => void): void {
|
||||
itEachAbsentTestCase(getAbsentCollectionTestCases<T>(), runner);
|
||||
export function itEachAbsentCollectionValue<T>(
|
||||
runner: (absentValue: []) => void,
|
||||
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||
): void {
|
||||
itEachAbsentTestCase(getAbsentCollectionTestCases<T>(options), runner);
|
||||
}
|
||||
|
||||
export function itEachAbsentTestCase<T>(
|
||||
@@ -25,28 +32,40 @@ export function itEachAbsentTestCase<T>(
|
||||
}
|
||||
}
|
||||
|
||||
export const AbsentObjectTestCases: readonly IAbsentTestCase<AbsentObjectType>[] = [
|
||||
{
|
||||
valueName: 'undefined',
|
||||
absentValue: undefined,
|
||||
},
|
||||
{
|
||||
valueName: 'null',
|
||||
absentValue: null,
|
||||
},
|
||||
];
|
||||
|
||||
export const AbsentStringTestCases: readonly IAbsentStringCase[] = [
|
||||
{
|
||||
valueName: 'empty',
|
||||
absentValue: '',
|
||||
},
|
||||
...AbsentObjectTestCases,
|
||||
];
|
||||
|
||||
export function getAbsentCollectionTestCases<T>(): readonly IAbsentCollectionCase<T>[] {
|
||||
export function getAbsentObjectTestCases(
|
||||
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||
): IAbsentTestCase<AbsentObjectType>[] {
|
||||
return [
|
||||
...AbsentObjectTestCases,
|
||||
{
|
||||
valueName: 'null',
|
||||
absentValue: null,
|
||||
},
|
||||
...(options.excludeUndefined ? [] : [
|
||||
{
|
||||
valueName: 'undefined',
|
||||
absentValue: undefined,
|
||||
},
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
export function getAbsentStringTestCases(
|
||||
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||
): IAbsentStringCase[] {
|
||||
return [
|
||||
{
|
||||
valueName: 'empty',
|
||||
absentValue: '',
|
||||
},
|
||||
...getAbsentObjectTestCases(options),
|
||||
];
|
||||
}
|
||||
|
||||
export function getAbsentCollectionTestCases<T>(
|
||||
options: IAbsentTestCaseOptions = DefaultAbsentTestCaseOptions,
|
||||
): readonly IAbsentCollectionCase<T>[] {
|
||||
return [
|
||||
...getAbsentObjectTestCases(options),
|
||||
{
|
||||
valueName: 'empty',
|
||||
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;
|
||||
|
||||
interface IAbsentTestCase<T> {
|
||||
|
||||
Reference in New Issue
Block a user