Files
privacy.sexy/tests/unit/stubs/CategoryCollectionStub.ts
undergroundwires 5b1fbe1e2f Refactor code to comply with ESLint rules
Major refactoring using ESLint with rules from AirBnb and Vue.

Enable most of the ESLint rules and do necessary linting in the code.
Also add more information for rules that are disabled to describe what
they are and why they are disabled.

Allow logging (`console.log`) in test files, and in development mode
(e.g. when working with `npm run serve`), but disable it when
environment is production (as pre-configured by Vue). Also add flag
(`--mode production`) in `lint:eslint` command so production linting is
executed earlier in lifecycle.

Disable rules that requires a separate work. Such as ESLint rules that
are broken in TypeScript: no-useless-constructor (eslint/eslint#14118)
and no-shadow (eslint/eslint#13014).
2022-01-02 18:20:14 +01:00

107 lines
3.2 KiB
TypeScript

import { OperatingSystem } from '@/domain/OperatingSystem';
import { IScriptingDefinition } from '@/domain/IScriptingDefinition';
import { IScript } from '@/domain/IScript';
import { ICategory } from '@/domain/ICategory';
import { ICategoryCollection } from '@/domain/ICategoryCollection';
import { RecommendationLevel } from '@/domain/RecommendationLevel';
import { ScriptStub } from './ScriptStub';
import { ScriptingDefinitionStub } from './ScriptingDefinitionStub';
export class CategoryCollectionStub implements ICategoryCollection {
public scripting: IScriptingDefinition = new ScriptingDefinitionStub();
public os = OperatingSystem.Linux;
public initialScript: IScript = new ScriptStub('55');
public totalScripts = 0;
public totalCategories = 0;
public readonly actions = new Array<ICategory>();
public withAction(category: ICategory): CategoryCollectionStub {
this.actions.push(category);
return this;
}
public withOs(os: OperatingSystem): CategoryCollectionStub {
this.os = os;
return this;
}
public withScripting(scripting: IScriptingDefinition): CategoryCollectionStub {
this.scripting = scripting;
return this;
}
public withInitialScript(script: IScript): CategoryCollectionStub {
this.initialScript = script;
return this;
}
public withTotalScripts(totalScripts: number) {
this.totalScripts = totalScripts;
return this;
}
public findCategory(categoryId: number): ICategory {
return this.getAllCategories()
.find((category) => category.id === categoryId);
}
public getScriptsByLevel(level: RecommendationLevel): readonly IScript[] {
return this.getAllScripts()
.filter((script) => script.level !== undefined && script.level <= level);
}
public findScript(scriptId: string): IScript {
return this.getAllScripts()
.find((script) => scriptId === script.id);
}
public getAllScripts(): ReadonlyArray<IScript> {
const scripts = [];
for (const category of this.actions) {
const categoryScripts = getScriptsRecursively(category);
scripts.push(...categoryScripts);
}
return scripts;
}
public getAllCategories(): ReadonlyArray<ICategory> {
const categories = [];
categories.push(...this.actions);
for (const category of this.actions) {
const subCategories = getSubCategoriesRecursively(category);
categories.push(...subCategories);
}
return categories;
}
}
function getSubCategoriesRecursively(category: ICategory): ReadonlyArray<ICategory> {
const subCategories = [];
if (category.subCategories) {
for (const subCategory of category.subCategories) {
subCategories.push(subCategory);
subCategories.push(...getSubCategoriesRecursively(subCategory));
}
}
return subCategories;
}
function getScriptsRecursively(category: ICategory): ReadonlyArray<IScript> {
const categoryScripts = [];
if (category.scripts) {
categoryScripts.push(...category.scripts);
}
if (category.subCategories) {
for (const subCategory of category.subCategories) {
const subCategoryScripts = getScriptsRecursively(subCategory);
categoryScripts.push(...subCategoryScripts);
}
}
return categoryScripts;
}