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,21 +1,35 @@
import { describe, it, expect } from 'vitest';
import { getEnumValues } from '@/application/Common/Enum';
import { CollectionScript } from '@/domain/Executables/Script/CollectionScript';
import { RecommendationLevel } from '@/domain/Executables/Script/RecommendationLevel';
import type { ScriptCode } from '@/domain/Executables/Script/Code/ScriptCode';
import { ScriptCodeStub } from '@tests/unit/shared/Stubs/ScriptCodeStub';
import { createScript } from '@/domain/Executables/Script/ScriptFactory';
import type { ExecutableId } from '@/domain/Executables/Identifiable';
describe('CollectionScript', () => {
describe('ctor', () => {
describe('ScriptFactory', () => {
describe('createScript', () => {
describe('id', () => {
it('correctly assigns id', () => {
// arrange
const expectedId: ExecutableId = 'expected-id';
// act
const script = new TestContext()
.withId(expectedId)
.build();
// assert
const actualId = script.executableId;
expect(actualId).to.equal(expectedId);
});
});
describe('scriptCode', () => {
it('assigns code correctly', () => {
// arrange
const expected = new ScriptCodeStub();
const sut = new ScriptBuilder()
const script = new TestContext()
.withCode(expected)
.build();
// act
const actual = sut.code;
const actual = script.code;
// assert
expect(actual).to.deep.equal(expected);
});
@@ -23,21 +37,21 @@ describe('CollectionScript', () => {
describe('canRevert', () => {
it('returns false without revert code', () => {
// arrange
const sut = new ScriptBuilder()
const script = new TestContext()
.withCodes('code')
.build();
// act
const actual = sut.canRevert();
const actual = script.canRevert();
// assert
expect(actual).to.equal(false);
});
it('returns true with revert code', () => {
// arrange
const sut = new ScriptBuilder()
const script = new TestContext()
.withCodes('code', 'non empty revert code')
.build();
// act
const actual = sut.canRevert();
const actual = script.canRevert();
// assert
expect(actual).to.equal(true);
});
@@ -48,7 +62,7 @@ describe('CollectionScript', () => {
const invalidValue: RecommendationLevel = 55 as never;
const expectedError = 'invalid level';
// act
const construct = () => new ScriptBuilder()
const construct = () => new TestContext()
.withRecommendationLevel(invalidValue)
.build();
// assert
@@ -58,43 +72,46 @@ describe('CollectionScript', () => {
// arrange
const expected = undefined;
// act
const sut = new ScriptBuilder()
const script = new TestContext()
.withRecommendationLevel(expected)
.build();
// assert
expect(sut.level).to.equal(expected);
expect(script.level).to.equal(expected);
});
it('correctly assigns valid recommendation levels', () => {
// arrange
for (const expected of getEnumValues(RecommendationLevel)) {
getEnumValues(RecommendationLevel).forEach((enumValue) => {
// arrange
const expectedRecommendationLevel = enumValue;
// act
const sut = new ScriptBuilder()
.withRecommendationLevel(expected)
const script = new TestContext()
.withRecommendationLevel(expectedRecommendationLevel)
.build();
// assert
const actual = sut.level;
expect(actual).to.equal(expected);
}
const actualRecommendationLevel = script.level;
expect(actualRecommendationLevel).to.equal(expectedRecommendationLevel);
});
});
});
describe('docs', () => {
it('correctly assigns docs', () => {
// arrange
const expected = ['doc1', 'doc2'];
const expectedDocs = ['doc1', 'doc2'];
// act
const sut = new ScriptBuilder()
.withDocs(expected)
const script = new TestContext()
.withDocs(expectedDocs)
.build();
const actual = sut.docs;
// assert
expect(actual).to.equal(expected);
const actualDocs = script.docs;
expect(actualDocs).to.equal(expectedDocs);
});
});
});
});
class ScriptBuilder {
private name = 'test-script';
class TestContext {
private name = `[${TestContext.name}]test-script`;
private id: ExecutableId = `[${TestContext.name}]id`;
private code: ScriptCode = new ScriptCodeStub();
@@ -109,6 +126,11 @@ class ScriptBuilder {
return this;
}
public withId(id: ExecutableId): this {
this.id = id;
return this;
}
public withCode(code: ScriptCode): this {
this.code = code;
return this;
@@ -129,8 +151,9 @@ class ScriptBuilder {
return this;
}
public build(): CollectionScript {
return new CollectionScript({
public build(): ReturnType<typeof createScript> {
return createScript({
executableId: this.id,
name: this.name,
code: this.code,
docs: this.docs,