Files
privacy.sexy/tests/unit/shared/Stubs/CategoryCollectionStub.ts
undergroundwires 48d6dbd700 Refactor to use string IDs for executables #262
This commit unifies the concepts of executables having same ID
structure. It paves the way for more complex ID structure and using IDs
in collection files as part of new ID solution (#262). Using string IDs
also leads to more expressive test code.

This commit also refactors the rest of the code to adopt to the changes.

This commit:

- Separate concerns from entities for data access (in repositories) and
  executables. Executables use `Identifiable` meanwhile repositories use
  `RepositoryEntity`.
- Refactor unnecessary generic parameters for enttities and ids,
  enforcing string gtype everwyhere.
- Changes numeric IDs to string IDs for categories to unify the
  retrieval and construction for executables, using pseudo-ids (their
  names) just like scripts.
- Remove `BaseEntity` for simplicity.
- Simplify usage and construction of executable objects.
  Move factories responsible for creation of category/scripts to domain
  layer. Do not longer export `CollectionCategorY` and
  `CollectionScript`.
- Use named typed for string IDs for better differentation of different
  ID contexts in code.
2024-07-08 23:23:05 +02:00

106 lines
3.3 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/Collection/ICategoryCollection';
import { RecommendationLevel } from '@/domain/Executables/Script/RecommendationLevel';
import type { ExecutableId } from '@/domain/Executables/Identifiable';
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(`[${CategoryCollectionStub}]-action-1`));
this.withAction(new CategoryStub(`[${CategoryCollectionStub}]-action-2`));
this.withAction(new CategoryStub(`[${CategoryCollectionStub}]-action-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: ExecutableId): Category {
return this.getAllCategories()
.find((category) => category.executableId === 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.executableId)
?? 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),
),
];
}