Files
privacy.sexy/tests/unit/shared/Stubs/CategoryCollectionStub.ts
undergroundwires c138f74460 Refactor to unify scripts/categories as Executable
This commit consolidates scripts and categories under a unified
'Executable' concept. This simplifies the architecture and improves code
readability.

- Introduce subfolders within `src/domain` to segregate domain elements.
- Update class and interface names by removing the 'I' prefix in
  alignment with new coding standards.
- Replace 'Node' with 'Executable' to clarify usage; reserve 'Node'
  exclusively for the UI's tree component.
2024-06-12 12:36:40 +02:00

105 lines
3.1 KiB
TypeScript

import { OperatingSystem } from '@/domain/OperatingSystem';
import type { IScriptingDefinition } from '@/domain/IScriptingDefinition';
import type { Script } from '@/domain/Executables/Script/Script';
import type { Category } from '@/domain/Executables/Category/Category';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import { RecommendationLevel } from '@/domain/Executables/Script/RecommendationLevel';
import { ScriptStub } from './ScriptStub';
import { ScriptingDefinitionStub } from './ScriptingDefinitionStub';
import { CategoryStub } from './CategoryStub';
export class CategoryCollectionStub implements ICategoryCollection {
public scripting: IScriptingDefinition = new ScriptingDefinitionStub();
public os = OperatingSystem.Linux;
public initialScript: Script = new ScriptStub('55');
public totalScripts = 0;
public totalCategories = 0;
public readonly actions = new Array<Category>();
public withSomeActions(): this {
this.withAction(new CategoryStub(1));
this.withAction(new CategoryStub(2));
this.withAction(new CategoryStub(3));
return this;
}
public withAction(category: Category): this {
this.actions.push(category);
return this;
}
public withActions(...actions: readonly Category[]): this {
for (const action of actions) {
this.withAction(action);
}
return this;
}
public withOs(os: OperatingSystem): this {
this.os = os;
return this;
}
public withScripting(scripting: IScriptingDefinition): this {
this.scripting = scripting;
return this;
}
public withInitialScript(script: Script): this {
this.initialScript = script;
return this;
}
public withTotalScripts(totalScripts: number): this {
this.totalScripts = totalScripts;
return this;
}
public getCategory(categoryId: number): Category {
return this.getAllCategories()
.find((category) => category.id === categoryId)
?? new CategoryStub(categoryId);
}
public getScriptsByLevel(level: RecommendationLevel): readonly Script[] {
return this.getAllScripts()
.filter((script) => script.level !== undefined && script.level <= level);
}
public getScript(scriptId: string): Script {
return this.getAllScripts()
.find((script) => scriptId === script.id)
?? new ScriptStub(scriptId);
}
public getAllScripts(): ReadonlyArray<Script> {
return this.actions.flatMap((category) => getScriptsRecursively(category));
}
public getAllCategories(): ReadonlyArray<Category> {
return this.actions.flatMap(
(category) => [category, ...getSubCategoriesRecursively(category)],
);
}
}
function getSubCategoriesRecursively(category: Category): ReadonlyArray<Category> {
return (category.subCategories || []).flatMap(
(subCategory) => [subCategory, ...getSubCategoriesRecursively(subCategory)],
);
}
function getScriptsRecursively(category: Category): ReadonlyArray<Script> {
return [
...(category.scripts || []),
...(category.subCategories || []).flatMap(
(subCategory) => getScriptsRecursively(subCategory),
),
];
}