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:
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { BrowserOsDetector } from '@/infrastructure/Environment/BrowserOs/BrowserOsDetector';
|
||||
import { itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
import { BrowserOsTestCases } from './BrowserOsTestCases';
|
||||
|
||||
describe('BrowserOsDetector', () => {
|
||||
describe('returns undefined when user agent is absent', () => {
|
||||
itEachAbsentStringValue((absentValue) => {
|
||||
// arrange
|
||||
const expected = undefined;
|
||||
const userAgent = absentValue;
|
||||
const sut = new BrowserOsDetector();
|
||||
// act
|
||||
const actual = sut.detect(userAgent);
|
||||
// assert
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
});
|
||||
it('detects as expected', () => {
|
||||
BrowserOsTestCases.forEach((testCase) => {
|
||||
// arrange
|
||||
const sut = new BrowserOsDetector();
|
||||
// act
|
||||
const actual = sut.detect(testCase.userAgent);
|
||||
// assert
|
||||
expect(actual).to.equal(testCase.expectedOs, printMessage());
|
||||
function printMessage(): string {
|
||||
return `Expected: "${OperatingSystem[testCase.expectedOs]}"\n`
|
||||
+ `Actual: "${OperatingSystem[actual]}"\n`
|
||||
+ `UserAgent: "${testCase.userAgent}"`;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,337 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
|
||||
interface IBrowserOsTestCase {
|
||||
userAgent: string;
|
||||
expectedOs: OperatingSystem;
|
||||
}
|
||||
|
||||
export const BrowserOsTestCases: ReadonlyArray<IBrowserOsTestCase> = [
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 6.3; Win64, x64; Trident/7.0; rv:11.0) like Gecko',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; WebView/3.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36 Edge/14.14316',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows Phone 10.0; Android 5.1.1; NOKIA; Lumia 1520) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/13.10586',
|
||||
expectedOs: OperatingSystem.WindowsPhone,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0',
|
||||
expectedOs: OperatingSystem.macOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36',
|
||||
expectedOs: OperatingSystem.macOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (X11; CrOS x86_64 11316.165.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.122 Safari/537.36',
|
||||
expectedOs: OperatingSystem.ChromeOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (X11; CrOS x86_64 8872.76.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.105 Safari/537.36',
|
||||
expectedOs: OperatingSystem.ChromeOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (X11; CrOS armv7l 4537.56.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.38 Safari/537.36',
|
||||
expectedOs: OperatingSystem.ChromeOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15',
|
||||
expectedOs: OperatingSystem.macOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15',
|
||||
expectedOs: OperatingSystem.macOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36 OPR/58.0.3135.114',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36 OPR/53.0.2907.68',
|
||||
expectedOs: OperatingSystem.macOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2393.94 Safari/537.36 OPR/42.0.2393.94',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.82 Safari/537.36 OPR/29.0.1795.41 (Edition beta)',
|
||||
expectedOs: OperatingSystem.macOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36 OPR/15.0.1147.100',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.2.15 Version/10.10',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Opera/9.27 (Windows NT 5.1; U; en)',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1',
|
||||
expectedOs: OperatingSystem.iOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
|
||||
expectedOs: OperatingSystem.iOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1',
|
||||
expectedOs: OperatingSystem.iOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
|
||||
expectedOs: OperatingSystem.iOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Opera/9.80 (Android; Opera Mini/32.0/88.150; U; sr) Presto/2.12 Version/12.16',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Opera/9.80 (Android; Opera Mini/8.0.1807/36.1609; U; en) Presto/2.12.423 Version/12.16',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 4.4.4; pt-br; SM-G530BT Build/KTU84P) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 4.4.2; zh-cn; Q40; Android/4.4.2; Release/12.15.2015) AppleWebKit/534.30 (KHTML, like Gecko) Mobile Safari/534.30',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 4.1; en-us; GT-N7100 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.1.0.1429 Mobile Safari/537.10+',
|
||||
expectedOs: OperatingSystem.BlackBerry,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.0.0; en-US) AppleWebKit/535.8+ (KHTML, like Gecko) Version/7.2.0.0 Safari/535.8+',
|
||||
expectedOs: OperatingSystem.BlackBerryTabletOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-US) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.466 Mobile Safari/534.8+',
|
||||
expectedOs: OperatingSystem.BlackBerryOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 4.4.4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36 Mobile OPR/15.0.1147.100',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 2.3.4; MT11i Build/4.0.2.A.0.62) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.123 Mobile Safari/537.22 OPR/14.0.1025.52315',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Opera/9.80 (Windows NT 6.1; Opera Tablet/15165; U; en) Presto/2.8.149 Version/11.1',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Opera/9.80 (Android 2.2; Opera Mobi/-2118645896; U; pl) Presto/2.7.60 Version/10.5',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 9; SM-G960U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 6.0; CAM-L03) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.99 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 4.2.2; GT-I9505 Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 4.3; Nexus 10 Build/JSS15Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; LGMS323 Build/KOT49I.MS32310c) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/46.0.2490.76 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Android 9; Mobile; rv:64.0) Gecko/64.0 Firefox/64.0',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4',
|
||||
expectedOs: OperatingSystem.iOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 625) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537',
|
||||
expectedOs: OperatingSystem.WindowsPhone,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 520)',
|
||||
expectedOs: OperatingSystem.WindowsPhone,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 6.0; en-US; CPH1609 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.10.2.1164 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'UCWEB/2.0 (Linux; U; Adr 5.1; en-US; Lenovo Z90a40 Build/LMY47O) U2/1.0.0 UCBrowser/11.1.5.890 U2/1.0.0 Mobile',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 5.1; en-US; Lenovo Z90a40 Build/LMY47O) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 UCBrowser/11.1.5.890 U3/0.8.0 Mobile Safari/534.30',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'UCWEB/2.0 (Linux; U; Adr 2.3; en-US; MI-ONEPlus) U2/1.0.0 UCBrowser/8.6.0.199 U2/1.0.0 Mobile',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 2.3; zh-CN; MI-ONEPlus) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Mobile Safari/534.13',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G965F Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/9.0 Chrome/67.0.3396.87 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 8.0.0; SAMSUNG SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/8.2 Chrome/63.0.3239.111 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 7.0; SAMSUNG SM-J330FN Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/7.2 Chrome/59.0.3071.125 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-G925F Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/44.0.2403.133 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-G925F Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/44.0.2403.133 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; vivo X21A Build/OPM1.171019.011) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/9.1 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; U; Android 4.4.2; zh-cn; GT-I9500 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.0 QQ-URL-Manager Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 9; ONEPLUS A6003) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Mobile Safari/537.36',
|
||||
expectedOs: OperatingSystem.Android,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 6.4; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1',
|
||||
expectedOs: OperatingSystem.iOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
|
||||
expectedOs: OperatingSystem.macOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0',
|
||||
expectedOs: OperatingSystem.Linux,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (X11; CrOS x86_64 11316.165.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.122 Safari/537.36',
|
||||
expectedOs: OperatingSystem.ChromeOS,
|
||||
},
|
||||
{
|
||||
userAgent: 'Mozilla/5.0 (Mobile; LYF/F90M/LYF_F90M_000-03-12-110119; Android; rv:48.0) Gecko/48.0 Firefox/48.0 KAIOS/2.5',
|
||||
expectedOs: OperatingSystem.KaiOS,
|
||||
},
|
||||
];
|
||||
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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user