Refactor executable IDs to use strings #262

This commit unifies executable ID structure across categories and
scripts, paving the way for more complex ID solutions for #262.
It also refactors related code to adapt to the changes.

Key changes:

- Change numeric IDs to string IDs for categories
- Use named types for string IDs to improve code clarity
- Add unit tests to verify ID uniqueness

Other supporting changes:

- Separate concerns in entities for data access and executables by using
  separate abstractions (`Identifiable` and `RepositoryEntity`)
- Simplify usage and construction of entities.
- Remove `BaseEntity` for simplicity.
- Move creation of categories/scripts to domain layer
- Refactor CategoryCollection for better validation logic isolation
- Rename some categories to keep the names (used as pseudo-IDs) unique
  on Windows.
This commit is contained in:
undergroundwires
2024-08-03 16:54:14 +02:00
parent 6fbc81675f
commit ded55a66d6
124 changed files with 2286 additions and 1331 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,