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.
This commit is contained in:
undergroundwires
2024-06-16 11:44:48 +02:00
parent 19ea8dbc5b
commit 48d6dbd700
96 changed files with 1417 additions and 1109 deletions

View File

@@ -1,5 +1,5 @@
import type { IApplication } from '@/domain/IApplication';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { OperatingSystem } from '@/domain/OperatingSystem';
import type { ProjectDetails } from '@/domain/Project/ProjectDetails';
import { ProjectDetailsStub } from './ProjectDetailsStub';

View File

@@ -1,6 +1,6 @@
import type { CategoryCollectionFactory } from '@/application/Parser/CategoryCollectionParser';
import type { CategoryCollectionInitParameters } from '@/domain/CategoryCollection';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { CategoryCollectionInitParameters } from '@/domain/Collection/CategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { CategoryCollectionStub } from './CategoryCollectionStub';
export function createCategoryCollectionFactorySpy(): {

View File

@@ -1,5 +1,5 @@
import type { ProjectDetails } from '@/domain/Project/ProjectDetails';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { getEnumValues } from '@/application/Common/Enum';
import type { CollectionData } from '@/application/collections/';
import { OperatingSystem } from '@/domain/OperatingSystem';

View File

@@ -3,7 +3,7 @@ import type { ICategoryCollectionState } from '@/application/Context/State/ICate
import { OperatingSystem } from '@/domain/OperatingSystem';
import type { Script } from '@/domain/Executables/Script/Script';
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import type { UserSelection } from '@/application/Context/State/Selection/UserSelection';
import type { SelectedScript } from '@/application/Context/State/Selection/Script/SelectedScript';
import type { FilterContext } from '@/application/Context/State/Filter/FilterContext';

View File

@@ -2,8 +2,9 @@ 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 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';
@@ -22,9 +23,9 @@ export class CategoryCollectionStub implements ICategoryCollection {
public readonly actions = new Array<Category>();
public withSomeActions(): this {
this.withAction(new CategoryStub(1));
this.withAction(new CategoryStub(2));
this.withAction(new CategoryStub(3));
this.withAction(new CategoryStub(`[${CategoryCollectionStub}]-action-1`));
this.withAction(new CategoryStub(`[${CategoryCollectionStub}]-action-2`));
this.withAction(new CategoryStub(`[${CategoryCollectionStub}]-action-3`));
return this;
}
@@ -60,9 +61,9 @@ export class CategoryCollectionStub implements ICategoryCollection {
return this;
}
public getCategory(categoryId: number): Category {
public getCategory(categoryId: ExecutableId): Category {
return this.getAllCategories()
.find((category) => category.id === categoryId)
.find((category) => category.executableId === categoryId)
?? new CategoryStub(categoryId);
}
@@ -73,7 +74,7 @@ export class CategoryCollectionStub implements ICategoryCollection {
public getScript(scriptId: string): Script {
return this.getAllScripts()
.find((script) => scriptId === script.id)
.find((script) => scriptId === script.executableId)
?? new ScriptStub(scriptId);
}
@@ -89,7 +90,7 @@ export class CategoryCollectionStub implements ICategoryCollection {
}
function getSubCategoriesRecursively(category: Category): ReadonlyArray<Category> {
return (category.subCategories || []).flatMap(
return (category.subcategories || []).flatMap(
(subCategory) => [subCategory, ...getSubCategoriesRecursively(subCategory)],
);
}
@@ -97,7 +98,7 @@ function getSubCategoriesRecursively(category: Category): ReadonlyArray<Category
function getScriptsRecursively(category: Category): ReadonlyArray<Script> {
return [
...(category.scripts || []),
...(category.subCategories || []).flatMap(
...(category.subcategories || []).flatMap(
(subCategory) => getScriptsRecursively(subCategory),
),
];

View File

@@ -1,6 +1,5 @@
import type { CategoryFactory } from '@/application/Parser/Executable/CategoryParser';
import type { CategoryInitParameters } from '@/domain/Executables/Category/CollectionCategory';
import type { Category } from '@/domain/Executables/Category/Category';
import type { CategoryFactory, CategoryInitParameters } from '@/domain/Executables/Category/CategoryFactory';
import { CategoryStub } from './CategoryStub';
export function createCategoryFactorySpy(): {
@@ -10,7 +9,7 @@ export function createCategoryFactorySpy(): {
const createdCategories = new Map<Category, CategoryInitParameters>();
return {
categoryFactorySpy: (parameters) => {
const category = new CategoryStub(55);
const category = new CategoryStub('category-from-factory-stub');
createdCategories.set(category, parameters);
return category;
},

View File

@@ -1,13 +1,13 @@
import { BaseEntity } from '@/infrastructure/Entity/BaseEntity';
import type { Category } from '@/domain/Executables/Category/Category';
import { RecommendationLevel } from '@/domain/Executables/Script/RecommendationLevel';
import type { Script } from '@/domain/Executables/Script/Script';
import type { ExecutableId } from '@/domain/Executables/Identifiable';
import { ScriptStub } from './ScriptStub';
export class CategoryStub extends BaseEntity<number> implements Category {
public name = `category-with-id-${this.id}`;
export class CategoryStub implements Category {
public name = `[${CategoryStub.name}] name (ID: ${this.executableId})`;
public readonly subCategories = new Array<Category>();
public readonly subcategories = new Array<Category>();
public readonly scripts = new Array<Script>();
@@ -15,25 +15,25 @@ export class CategoryStub extends BaseEntity<number> implements Category {
private allScriptsRecursively: (readonly Script[]) | undefined;
public constructor(id: number) {
super(id);
}
public constructor(
readonly executableId: ExecutableId,
) { }
public includes(script: Script): boolean {
return this.getAllScriptsRecursively().some((s) => s.id === script.id);
return this.getAllScriptsRecursively().some((s) => s.executableId === script.executableId);
}
public getAllScriptsRecursively(): readonly Script[] {
if (this.allScriptsRecursively === undefined) {
return [
...this.scripts,
...this.subCategories.flatMap((c) => c.getAllScriptsRecursively()),
...this.subcategories.flatMap((c) => c.getAllScriptsRecursively()),
];
}
return this.allScriptsRecursively;
}
public withScriptIds(...scriptIds: readonly string[]): this {
public withScriptIds(...scriptIds: readonly ExecutableId[]): this {
return this.withScripts(
...scriptIds.map((id) => new ScriptStub(id)),
);
@@ -70,7 +70,7 @@ export class CategoryStub extends BaseEntity<number> implements Category {
}
public withCategory(category: Category): this {
this.subCategories.push(category);
this.subcategories.push(category);
return this;
}

View File

@@ -1,6 +1,6 @@
import type { FilterResult } from '@/application/Context/State/Filter/Result/FilterResult';
import type { FilterStrategy } from '@/application/Context/State/Filter/Strategy/FilterStrategy';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { FilterResultStub } from './FilterResultStub';
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';

View File

@@ -1,14 +0,0 @@
import { BaseEntity } from '@/infrastructure/Entity/BaseEntity';
export class NumericEntityStub extends BaseEntity<number> {
public customProperty = 'customProperty';
public constructor(id: number) {
super(id);
}
public withCustomProperty(value: string): NumericEntityStub {
this.customProperty = value;
return this;
}
}

View File

@@ -0,0 +1,14 @@
import type { RepositoryEntity, RepositoryEntityId } from '@/application/Repository/RepositoryEntity';
export class RepositoryEntityStub implements RepositoryEntity {
public customProperty = 'customProperty';
public constructor(
public readonly id: RepositoryEntityId,
) { }
public withCustomPropertyValue(value: string): RepositoryEntityStub {
this.customProperty = value;
return this;
}
}

View File

@@ -1,6 +1,5 @@
import type { ScriptFactory } from '@/application/Parser/Executable/Script/ScriptParser';
import type { Script } from '@/domain/Executables/Script/Script';
import type { ScriptInitParameters } from '@/domain/Executables/Script/CollectionScript';
import type { ScriptFactory, ScriptInitParameters } from '@/domain/Executables/Script/ScriptFactory';
import { ScriptStub } from './ScriptStub';
export function createScriptFactorySpy(): {

View File

@@ -1,15 +1,15 @@
import { BaseEntity } from '@/infrastructure/Entity/BaseEntity';
import type { Script } from '@/domain/Executables/Script/Script';
import { RecommendationLevel } from '@/domain/Executables/Script/RecommendationLevel';
import type { ScriptCode } from '@/domain/Executables/Script/Code/ScriptCode';
import { SelectedScriptStub } from './SelectedScriptStub';
import type { ExecutableId } from '@/domain/Executables/Identifiable';
export class ScriptStub extends BaseEntity<string> implements Script {
public name = `name${this.id}`;
export class ScriptStub implements Script {
public name = `name${this.executableId}`;
public code: ScriptCode = {
execute: `REM execute-code (${this.id})`,
revert: `REM revert-code (${this.id})`,
execute: `REM execute-code (${this.executableId})`,
revert: `REM revert-code (${this.executableId})`,
};
public docs: readonly string[] = new Array<string>();
@@ -18,9 +18,7 @@ export class ScriptStub extends BaseEntity<string> implements Script {
private isReversible: boolean | undefined = undefined;
constructor(public readonly id: string) {
super(id);
}
constructor(public readonly executableId: ExecutableId) { }
public canRevert(): boolean {
if (this.isReversible === undefined) {

View File

@@ -1,17 +1,18 @@
import type { SelectedScript } from '@/application/Context/State/Selection/Script/SelectedScript';
import type { RepositoryEntityId } from '@/application/Repository/RepositoryEntity';
import type { Script } from '@/domain/Executables/Script/Script';
export class SelectedScriptStub implements SelectedScript {
public readonly script: Script;
public readonly id: string;
public readonly id: RepositoryEntityId;
public revert: boolean;
constructor(
script: Script,
) {
this.id = script.id;
this.id = script.executableId;
this.script = script;
}