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

@@ -3,7 +3,7 @@ import { Application } from '@/domain/Application';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
import { ProjectDetailsStub } from '@tests/unit/shared/Stubs/ProjectDetailsStub';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { getAbsentCollectionTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
describe('Application', () => {

View File

@@ -5,7 +5,7 @@ import type { IScriptingDefinition } from '@/domain/IScriptingDefinition';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { RecommendationLevel } from '@/domain/Executables/Script/RecommendationLevel';
import { getEnumValues } from '@/application/Common/Enum';
import { CategoryCollection } from '@/domain/CategoryCollection';
import { CategoryCollection } from '@/domain/Collection/CategoryCollection';
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
import { EnumRangeTestRunner } from '@tests/unit/application/Common/EnumRangeTestRunner';
@@ -20,10 +20,10 @@ describe('CategoryCollection', () => {
);
const toIgnore = new ScriptStub('script-to-ignore').withLevel(undefined);
for (const currentLevel of recommendationLevels) {
const category = new CategoryStub(0)
const category = new CategoryStub('parent-action')
.withScripts(...scriptsWithLevels)
.withScript(toIgnore);
const sut = new CategoryCollectionBuilder()
const sut = new TestContext()
.withActions([category])
.construct();
// act
@@ -40,12 +40,12 @@ describe('CategoryCollection', () => {
new ScriptStub('S2').withLevel(level),
];
const actions = [
new CategoryStub(3).withScripts(
new CategoryStub('parent-category').withScripts(
...expected,
new ScriptStub('S3').withLevel(RecommendationLevel.Strict),
),
];
const sut = new CategoryCollectionBuilder()
const sut = new TestContext()
.withActions(actions)
.construct();
// act
@@ -61,9 +61,9 @@ describe('CategoryCollection', () => {
new ScriptStub('S2').withLevel(RecommendationLevel.Strict),
];
const actions = [
new CategoryStub(3).withScripts(...expected),
new CategoryStub('parent-category').withScripts(...expected),
];
const sut = new CategoryCollectionBuilder()
const sut = new TestContext()
.withActions(actions)
.construct();
// act
@@ -74,7 +74,7 @@ describe('CategoryCollection', () => {
describe('throws when given invalid level', () => {
new EnumRangeTestRunner<RecommendationLevel>((level) => {
// arrange
const sut = new CategoryCollectionBuilder()
const sut = new TestContext()
.construct();
// act
sut.getScriptsByLevel(level);
@@ -84,93 +84,23 @@ describe('CategoryCollection', () => {
.testValidValueDoesNotThrow(RecommendationLevel.Standard);
});
});
describe('actions', () => {
it('cannot construct without actions', () => {
// arrange
const categories = [];
// act
function construct() {
new CategoryCollectionBuilder()
.withActions(categories)
.construct();
}
// assert
expect(construct).to.throw('must consist of at least one category');
});
it('cannot construct without scripts', () => {
// arrange
const categories = [
new CategoryStub(3),
new CategoryStub(2),
];
// act
function construct() {
new CategoryCollectionBuilder()
.withActions(categories)
.construct();
}
// assert
expect(construct).to.throw('must consist of at least one script');
});
describe('cannot construct without any recommended scripts', () => {
describe('single missing', () => {
// arrange
const recommendationLevels = getEnumValues(RecommendationLevel);
for (const missingLevel of recommendationLevels) {
it(`when "${RecommendationLevel[missingLevel]}" is missing`, () => {
const expectedError = `none of the scripts are recommended as "${RecommendationLevel[missingLevel]}".`;
const otherLevels = recommendationLevels.filter((level) => level !== missingLevel);
const categories = otherLevels.map(
(level, index) => new CategoryStub(index)
.withScript(
new ScriptStub(`Script${index}`).withLevel(level),
),
);
// act
const construct = () => new CategoryCollectionBuilder()
.withActions(categories)
.construct();
// assert
expect(construct).to.throw(expectedError);
});
}
});
it('multiple are missing', () => {
// arrange
const expectedError = 'none of the scripts are recommended as '
+ `"${RecommendationLevel[RecommendationLevel.Standard]}, "${RecommendationLevel[RecommendationLevel.Strict]}".`;
const categories = [
new CategoryStub(0)
.withScript(
new ScriptStub(`Script${0}`).withLevel(undefined),
),
];
// act
const construct = () => new CategoryCollectionBuilder()
.withActions(categories)
.construct();
// assert
expect(construct).to.throw(expectedError);
});
});
});
describe('totalScripts', () => {
it('returns total of initial scripts', () => {
// arrange
const categories = [
new CategoryStub(1).withScripts(
new CategoryStub('category-1').withScripts(
new ScriptStub('S1').withLevel(RecommendationLevel.Standard),
),
new CategoryStub(2).withScripts(
new CategoryStub('category-2').withScripts(
new ScriptStub('S2'),
new ScriptStub('S3').withLevel(RecommendationLevel.Strict),
),
new CategoryStub(3).withCategories(
new CategoryStub(4).withScripts(new ScriptStub('S4')),
new CategoryStub('category-3').withCategories(
new CategoryStub('category-3-subcategory-1').withScripts(new ScriptStub('S4')),
),
];
// act
const sut = new CategoryCollectionBuilder()
const sut = new TestContext()
.withActions(categories)
.construct();
// assert
@@ -182,12 +112,12 @@ describe('CategoryCollection', () => {
// arrange
const expected = 4;
const categories = [
new CategoryStub(1).withScripts(new ScriptStub('S1').withLevel(RecommendationLevel.Strict)),
new CategoryStub(2).withScripts(new ScriptStub('S2'), new ScriptStub('S3')),
new CategoryStub(3).withCategories(new CategoryStub(4).withScripts(new ScriptStub('S4'))),
new CategoryStub('category-1').withScripts(new ScriptStub('S1').withLevel(RecommendationLevel.Strict)),
new CategoryStub('category-2').withScripts(new ScriptStub('S2'), new ScriptStub('S3')),
new CategoryStub('category-3').withCategories(new CategoryStub('category-3-subcategory-1').withScripts(new ScriptStub('S4'))),
];
// act
const sut = new CategoryCollectionBuilder()
const sut = new TestContext()
.withActions(categories)
.construct();
// assert
@@ -199,28 +129,19 @@ describe('CategoryCollection', () => {
// arrange
const expected = OperatingSystem.macOS;
// act
const sut = new CategoryCollectionBuilder()
const sut = new TestContext()
.withOs(expected)
.construct();
// assert
expect(sut.os).to.deep.equal(expected);
});
describe('throws when invalid', () => {
// act
const act = (os: OperatingSystem) => new CategoryCollectionBuilder()
.withOs(os)
.construct();
// assert
new EnumRangeTestRunner(act)
.testOutOfRangeThrows();
});
});
describe('scriptingDefinition', () => {
it('sets scriptingDefinition as expected', () => {
// arrange
const expected = getValidScriptingDefinition();
// act
const sut = new CategoryCollectionBuilder()
const sut = new TestContext()
.withScripting(expected)
.construct();
// assert
@@ -230,25 +151,25 @@ describe('CategoryCollection', () => {
describe('getCategory', () => {
it('throws if category is not found', () => {
// arrange
const categoryId = 123;
const expectedError = `Missing category with ID: "${categoryId}"`;
const collection = new CategoryCollectionBuilder()
.withActions([new CategoryStub(456).withMandatoryScripts()])
const missingCategoryId = 'missing-category-id';
const expectedError = `Missing category with ID: "${missingCategoryId}"`;
const collection = new TestContext()
.withActions([new CategoryStub(`different than ${missingCategoryId}`).withMandatoryScripts()])
.construct();
// act
const act = () => collection.getCategory(categoryId);
const act = () => collection.getCategory(missingCategoryId);
// assert
expect(act).to.throw(expectedError);
});
it('finds correct category', () => {
// arrange
const categoryId = 123;
const expectedCategory = new CategoryStub(categoryId).withMandatoryScripts();
const collection = new CategoryCollectionBuilder()
const existingCategoryId = 'expected-action-category-id';
const expectedCategory = new CategoryStub(existingCategoryId).withMandatoryScripts();
const collection = new TestContext()
.withActions([expectedCategory])
.construct();
// act
const actualCategory = collection.getCategory(categoryId);
const actualCategory = collection.getCategory(existingCategoryId);
// assert
expect(actualCategory).to.equal(expectedCategory);
});
@@ -257,9 +178,9 @@ describe('CategoryCollection', () => {
it('throws if script is not found', () => {
// arrange
const scriptId = 'missingScript';
const expectedError = `missing script: ${scriptId}`;
const collection = new CategoryCollectionBuilder()
.withActions([new CategoryStub(456).withMandatoryScripts()])
const expectedError = `Missing script: ${scriptId}`;
const collection = new TestContext()
.withActions([new CategoryStub('parent-action').withMandatoryScripts()])
.construct();
// act
const act = () => collection.getScript(scriptId);
@@ -270,10 +191,10 @@ describe('CategoryCollection', () => {
// arrange
const scriptId = 'existingScript';
const expectedScript = new ScriptStub(scriptId);
const parentCategory = new CategoryStub(123)
const parentCategory = new CategoryStub('parent-action')
.withMandatoryScripts()
.withScript(expectedScript);
const collection = new CategoryCollectionBuilder()
const collection = new TestContext()
.withActions([parentCategory])
.construct();
// act
@@ -293,11 +214,11 @@ function getValidScriptingDefinition(): IScriptingDefinition {
};
}
class CategoryCollectionBuilder {
class TestContext {
private os = OperatingSystem.Windows;
private actions: readonly Category[] = [
new CategoryStub(1).withMandatoryScripts(),
new CategoryStub(`[${TestContext.name}]-action-1`).withMandatoryScripts(),
];
private scriptingDefinition: IScriptingDefinition = getValidScriptingDefinition();

View File

@@ -0,0 +1,170 @@
import { describe, it, expect } from 'vitest';
import { validateCategoryCollection } from '@/domain/Collection/Validation/CompositeCategoryCollectionValidator';
import type { CategoryCollectionValidationContext, CategoryCollectionValidator } from '@/domain/Collection/Validation/CategoryCollectionValidator';
import { CategoryCollectionValidationContextStub } from '@tests/unit/shared/Stubs/CategoryCollectionValidationContextStub';
describe('validateCategoryCollection', () => {
it('throws error when no validators are provided', () => {
// arrange
const emptyValidators: CategoryCollectionValidator[] = [];
const expectedErrorMessage = 'No validators provided.';
// act
const act = () => new TestContext()
.withValidators(emptyValidators)
.runValidation();
// assert
expect(act).to.throw(expectedErrorMessage);
});
describe('validator execution', () => {
it('executes single validator', () => {
// arrange
let isCalled = false;
const singleValidator: CategoryCollectionValidator = () => {
isCalled = true;
};
const validators = [singleValidator];
// act
new TestContext()
.withValidators(validators)
.runValidation();
// assert
expect(isCalled).to.equal(true);
});
it('executes multiple validators in order', () => {
// arrange
const expectedExecutionSequence: readonly string[] = [
'validator1Call',
'validator2Call',
];
const actualExecutionSequence: string[] = [];
const validator1: CategoryCollectionValidator = () => {
actualExecutionSequence.push(expectedExecutionSequence[0]);
};
const validator2: CategoryCollectionValidator = () => {
actualExecutionSequence.push(expectedExecutionSequence[1]);
};
const validators = [validator1, validator2];
// act
new TestContext()
.withValidators(validators)
.runValidation();
// assert
expect(actualExecutionSequence).to.deep.equal(expectedExecutionSequence);
});
it('passes correct context to single validator', () => {
// arrange
const expectedContext = new CategoryCollectionValidationContextStub();
let actualContext: CategoryCollectionValidationContext | undefined;
const validator: CategoryCollectionValidator = (context) => {
actualContext = context;
};
const validators = [validator];
// act
new TestContext()
.withValidators(validators)
.withValidationContext(expectedContext)
.runValidation();
// assert
expect(expectedContext).to.equal(actualContext);
});
it('passes same context to all validators', () => {
// arrange
const expectedContext = new CategoryCollectionValidationContextStub();
const receivedContexts = new Array<CategoryCollectionValidationContext>();
const contextStoringValidator: CategoryCollectionValidator = (context) => {
receivedContexts.push(context);
};
const validators = [
contextStoringValidator,
contextStoringValidator,
contextStoringValidator,
];
// act
new TestContext()
.withValidators(validators)
.withValidationContext(expectedContext)
.runValidation();
// assert
expect(receivedContexts.every((c) => c === expectedContext)).to.equal(true);
});
});
it('propagates error from validator', () => {
// arrange
const expectedError = 'Error from validator';
const errorThrowingValidator: CategoryCollectionValidator = () => {
throw new Error(expectedError);
};
const validators = [errorThrowingValidator];
// act
const act = () => new TestContext()
.withValidators(validators)
.runValidation();
// Act & Assert
expect(act).to.throw(expectedError);
});
it('halts execution on validator error', () => {
// arrange
const errorThrowingValidator: CategoryCollectionValidator = () => {
throw new Error('Error from validator');
};
let isSecondValidatorCalled = false;
const secondValidator: CategoryCollectionValidator = () => {
isSecondValidatorCalled = true;
};
const validators = [errorThrowingValidator, secondValidator];
// act
try {
new TestContext()
.withValidators(validators)
.runValidation();
} catch { /* Swallow */ }
// Act & Assert
expect(isSecondValidatorCalled).to.equal(false);
});
});
class TestContext {
private validators: readonly CategoryCollectionValidator[] = [
() => {},
];
private validationContext
: CategoryCollectionValidationContext = new CategoryCollectionValidationContextStub();
public withValidators(validators: readonly CategoryCollectionValidator[]): this {
this.validators = validators;
return this;
}
public withValidationContext(validationContext: CategoryCollectionValidationContext): this {
this.validationContext = validationContext;
return this;
}
public runValidation(): ReturnType<typeof validateCategoryCollection> {
return validateCategoryCollection(
this.validationContext,
this.validators,
);
}
}

View File

@@ -0,0 +1,21 @@
import { describe } from 'vitest';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { EnumRangeTestRunner } from '@tests/unit/application/Common/EnumRangeTestRunner';
import { ensureKnownOperatingSystem } from '@/domain/Collection/Validation/Rules/EnsureKnownOperatingSystem';
import { CategoryCollectionValidationContextStub } from '@tests/unit/shared/Stubs/CategoryCollectionValidationContextStub';
describe('ensureKnownOperatingSystem', () => {
// act
const act = (os: OperatingSystem) => test(os);
// assert
new EnumRangeTestRunner(act)
.testValidValueDoesNotThrow(OperatingSystem.Android)
.testOutOfRangeThrows();
});
function test(operatingSystem: OperatingSystem):
ReturnType<typeof ensureKnownOperatingSystem> {
const context = new CategoryCollectionValidationContextStub()
.withOperatingSystem(operatingSystem);
return ensureKnownOperatingSystem(context);
}

View File

@@ -0,0 +1,99 @@
import { describe, it, expect } from 'vitest';
import { RecommendationLevel } from '@/domain/Executables/Script/RecommendationLevel';
import type { Script } from '@/domain/Executables/Script/Script';
import { ensurePresenceOfAllRecommendationLevels } from '@/domain/Collection/Validation/Rules/EnsurePresenceOfAllRecommendationLevels';
import { CategoryCollectionValidationContextStub } from '@tests/unit/shared/Stubs/CategoryCollectionValidationContextStub';
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
import { getEnumValues } from '@/application/Common/Enum';
import { collectExceptionMessage } from '@tests/unit/shared/ExceptionCollector';
describe('ensurePresenceOfAllRecommendationLevels', () => {
it('passes when all recommendation levels are present', () => {
// arrange
const scripts = getAllPossibleRecommendationLevels().map((level, index) => {
return new ScriptStub(`script-${index}`)
.withLevel(level);
});
// act
const act = () => test(scripts);
// assert
expect(act).to.not.throw();
});
describe('missing single level', () => {
// arrange
const recommendationLevels = getAllPossibleRecommendationLevels();
recommendationLevels.forEach((missingLevel) => {
const expectedDisplayName = getDisplayName(missingLevel);
it(`throws an error when when "${expectedDisplayName}" is missing`, () => {
const expectedError = `Missing recommendation levels: ${expectedDisplayName}.`;
const otherLevels = recommendationLevels.filter((level) => level !== missingLevel);
const scripts = otherLevels.map(
(level, index) => new ScriptStub(`script-${index}`).withLevel(level),
);
// act
const act = () => test(scripts);
// assert
expect(act).to.throw(expectedError);
});
});
});
it('throws an error with multiple missing recommendation levels', () => {
// arrange
const [
notExpectedLevelInError,
...expectedLevelsInError
] = getAllPossibleRecommendationLevels();
const scripts: Script[] = [
new ScriptStub('recommended').withLevel(notExpectedLevelInError),
];
// act
const act = () => test(scripts);
// assert
const actualErrorMessage = collectExceptionMessage(act);
expectedLevelsInError.forEach((level) => {
const expectedLevelInError = getDisplayName(level);
expect(actualErrorMessage).to.include(expectedLevelInError);
});
expect(actualErrorMessage).to.not.include(getDisplayName(notExpectedLevelInError));
});
it('throws an error when no scripts are provided', () => {
// arrange
const expectedLevelsInError = getAllPossibleRecommendationLevels()
.map((level) => getDisplayName(level));
const scripts: Script[] = [];
// act
const act = () => test(scripts);
// assert
const actualErrorMessage = collectExceptionMessage(act);
expectedLevelsInError.forEach((expectedLevelInError) => {
expect(actualErrorMessage).to.include(expectedLevelInError);
});
});
});
function test(allScripts: Script[]):
ReturnType<typeof ensurePresenceOfAllRecommendationLevels> {
const context = new CategoryCollectionValidationContextStub()
.withAllScripts(allScripts);
return ensurePresenceOfAllRecommendationLevels(context);
}
function getAllPossibleRecommendationLevels(): readonly (RecommendationLevel | undefined)[] {
return [
...getEnumValues(RecommendationLevel),
undefined,
];
}
function getDisplayName(level: RecommendationLevel | undefined): string {
return level === undefined ? 'None' : RecommendationLevel[level];
}

View File

@@ -0,0 +1,39 @@
import { describe, it, expect } from 'vitest';
import { CategoryCollectionValidationContextStub } from '@tests/unit/shared/Stubs/CategoryCollectionValidationContextStub';
import type { Category } from '@/domain/Executables/Category/Category';
import { ensurePresenceOfAtLeastOneCategory } from '@/domain/Collection/Validation/Rules/EnsurePresenceOfAtLeastOneCategory';
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
describe('ensurePresenceOfAtLeastOneCategory', () => {
it('throws an error when no categories are present', () => {
// arrange
const expectedErrorMessage = 'Collection must have at least one category';
const categories: Category[] = [];
// act
const act = () => test(categories);
// assert
expect(act).to.throw(expectedErrorMessage);
});
it('does not throw an error when at least one category is present', () => {
// arrange
const categories: Category[] = [
new CategoryStub('existing-category'),
];
// act
const act = () => test(categories);
// assert
expect(act).not.to.throw();
});
});
function test(allCategories: readonly Category[]):
ReturnType<typeof ensurePresenceOfAtLeastOneCategory> {
const context = new CategoryCollectionValidationContextStub()
.withAllCategories(allCategories);
return ensurePresenceOfAtLeastOneCategory(context);
}

View File

@@ -0,0 +1,39 @@
import { describe, it, expect } from 'vitest';
import { CategoryCollectionValidationContextStub } from '@tests/unit/shared/Stubs/CategoryCollectionValidationContextStub';
import type { Script } from '@/domain/Executables/Script/Script';
import { ensurePresenceOfAtLeastOneScript } from '@/domain/Collection/Validation/Rules/EnsurePresenceOfAtLeastOneScript';
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
describe('ensurePresenceOfAtLeastOneScript', () => {
it('throws an error when no scripts are present', () => {
// arrange
const expectedErrorMessage = 'Collection must have at least one script';
const scripts: Script[] = [];
// act
const act = () => test(scripts);
// assert
expect(act).to.throw(expectedErrorMessage);
});
it('does not throw an error when at least one category is present', () => {
// arrange
const scripts: Script[] = [
new ScriptStub('existing-script'),
];
// act
const act = () => test(scripts);
// assert
expect(act).not.to.throw();
});
});
function test(allScripts: readonly Script[]):
ReturnType<typeof ensurePresenceOfAtLeastOneScript> {
const context = new CategoryCollectionValidationContextStub()
.withAllScripts(allScripts);
return ensurePresenceOfAtLeastOneScript(context);
}

View File

@@ -0,0 +1,146 @@
import { describe, it, expect } from 'vitest';
import { ensureUniqueIdsAcrossExecutables } from '@/domain/Collection/Validation/Rules/EnsureUniqueIdsAcrossExecutables';
import type { Category } from '@/domain/Executables/Category/Category';
import type { Script } from '@/domain/Executables/Script/Script';
import { CategoryCollectionValidationContextStub } from '@tests/unit/shared/Stubs/CategoryCollectionValidationContextStub';
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
import type { ExecutableId } from '@/domain/Executables/Identifiable';
describe('ensureUniqueIdsAcrossExecutables', () => {
it('does not throw an error when all IDs are unique', () => {
// arrange
const testData: TestData = {
categories: [
new CategoryStub('category1'),
new CategoryStub('category2'),
],
scripts: [
new ScriptStub('script1'),
new ScriptStub('script2'),
],
};
// act
const act = () => test(testData);
// assert
expect(act).to.not.throw();
});
it('throws an error when duplicate IDs are found across categories and scripts', () => {
// arrange
const duplicateId: ExecutableId = 'duplicate';
const expectedErrorMessage = `Duplicate executable IDs found: "${duplicateId}"`;
const testData: TestData = {
categories: [
new CategoryStub(duplicateId),
new CategoryStub('category2'),
],
scripts: [
new ScriptStub(duplicateId),
new ScriptStub('script2'),
],
};
// act
const act = () => test(testData);
// assert
expect(act).to.throw(expectedErrorMessage);
});
it('throws an error when duplicate IDs are found within categories', () => {
// arrange
const duplicateId: ExecutableId = 'duplicate';
const expectedErrorMessage = `Duplicate executable IDs found: "${duplicateId}"`;
const testData: TestData = {
categories: [
new CategoryStub(duplicateId),
new CategoryStub(duplicateId),
],
scripts: [
new ScriptStub('script1'),
new ScriptStub('script2'),
],
};
// act
const act = () => test(testData);
// assert
expect(act).to.throw(expectedErrorMessage);
});
it('throws an error when duplicate IDs are found within scripts', () => {
// arrange
const duplicateId: ExecutableId = 'duplicate';
const expectedErrorMessage = `Duplicate executable IDs found: "${duplicateId}"`;
const testData: TestData = {
categories: [
new CategoryStub('category1'),
new CategoryStub('category2'),
],
scripts: [
new ScriptStub(duplicateId),
new ScriptStub(duplicateId),
],
};
// act
const act = () => test(testData);
// assert
expect(act).to.throw(expectedErrorMessage);
});
it('throws an error with multiple duplicate IDs', () => {
// arrange
const duplicateId1: ExecutableId = 'duplicate-1';
const duplicateId2: ExecutableId = 'duplicate-2';
const expectedErrorMessage = `Duplicate executable IDs found: "${duplicateId1}", "${duplicateId2}"`;
const testData: TestData = {
categories: [
new CategoryStub(duplicateId1),
new CategoryStub(duplicateId2),
],
scripts: [
new ScriptStub(duplicateId1),
new ScriptStub(duplicateId2),
],
};
// act
const act = () => test(testData);
// assert
expect(act).to.throw(expectedErrorMessage);
});
it('handles empty categories and scripts arrays', () => {
// arrange
const testData: TestData = {
categories: [],
scripts: [],
};
// act
const act = () => test(testData);
// assert
expect(act).to.not.throw();
});
});
interface TestData {
readonly categories: readonly Category[];
readonly scripts: readonly Script[];
}
function test(testData: TestData):
ReturnType<typeof ensureUniqueIdsAcrossExecutables> {
const context = new CategoryCollectionValidationContextStub()
.withAllCategories(testData.categories)
.withAllScripts(testData.scripts);
return ensureUniqueIdsAcrossExecutables(context);
}

View File

@@ -0,0 +1,316 @@
import { describe, it, expect } from 'vitest';
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
import { itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
import type { Category } from '@/domain/Executables/Category/Category';
import type { Script } from '@/domain/Executables/Script/Script';
import { createCategory } from '@/domain/Executables/Category/CategoryFactory';
import type { ExecutableId } from '@/domain/Executables/Identifiable';
describe('CategoryFactory', () => {
describe('createCategory', () => {
describe('id', () => {
it('assigns id correctly', () => {
// arrange
const expectedId: ExecutableId = 'expected category id';
// act
const category = new TestContext()
.withExecutableId(expectedId)
.build();
// assert
const actualId = category.executableId;
expect(actualId).to.equal(expectedId);
});
describe('throws error if id is absent', () => {
itEachAbsentStringValue((absentValue) => {
// arrange
const expectedError = 'missing ID';
const id = absentValue;
// act
const construct = () => new TestContext()
.withExecutableId(id)
.build();
// assert
expect(construct).to.throw(expectedError);
}, { excludeNull: true, excludeUndefined: true });
});
});
describe('name', () => {
it('assigns name correctly', () => {
// arrange
const expectedName = 'expected category name';
// act
const category = new TestContext()
.withName(expectedName)
.build();
// assert
const actualName = category.name;
expect(actualName).to.equal(expectedName);
});
describe('throws error if name is absent', () => {
itEachAbsentStringValue((absentValue) => {
// arrange
const expectedError = 'missing name';
const name = absentValue;
// act
const construct = () => new TestContext()
.withName(name)
.build();
// assert
expect(construct).to.throw(expectedError);
}, { excludeNull: true, excludeUndefined: true });
});
});
describe('docs', () => {
it('assigns docs correctly', () => {
// arrange
const expectedDocs = ['expected', 'docs'];
// act
const category = new TestContext()
.withDocs(expectedDocs)
.build();
// assert
const actualDocs = category.docs;
expect(actualDocs).to.equal(expectedDocs);
});
});
describe('children', () => {
it('assigns scripts correctly', () => {
// arrange
const expectedScripts = [
new ScriptStub('expected-script-1'),
new ScriptStub('expected-script-2'),
];
// act
const category = new TestContext()
.withScripts(expectedScripts)
.build();
// assert
const actualScripts = category.scripts;
expect(actualScripts).to.equal(expectedScripts);
});
it('assigns categories correctly', () => {
// arrange
const expectedCategories = [
new CategoryStub('expected-subcategory-1'),
new CategoryStub('expected-subcategory-2'),
];
// act
const category = new TestContext()
.withSubcategories(expectedCategories)
.build();
// assert
const actualCategories = category.subcategories;
expect(actualCategories).to.equal(expectedCategories);
});
it('throws error if no children are present', () => {
// arrange
const expectedError = 'A category must have at least one sub-category or script';
const scriptChildren: readonly Script[] = [];
const categoryChildren: readonly Category[] = [];
// act
const construct = () => new TestContext()
.withSubcategories(categoryChildren)
.withScripts(scriptChildren)
.build();
// assert
expect(construct).to.throw(expectedError);
});
});
describe('getAllScriptsRecursively', () => {
it('retrieves direct child scripts', () => {
// arrange
const expectedScripts: readonly Script[] = [
new ScriptStub('expected-script-1'),
new ScriptStub('expected-script-2'),
];
const category = new TestContext()
.withScripts(expectedScripts)
.build();
// act
const actual = category.getAllScriptsRecursively();
// assert
expect(actual).to.have.deep.members(expectedScripts);
});
it('retrieves scripts from direct child categories', () => {
// arrange
const expectedScriptIds: readonly string[] = [
'1', '2', '3', '4',
];
const subcategories: readonly Category[] = [
new CategoryStub('subcategory-1').withScriptIds('1', '2'),
new CategoryStub('subcategory-2').withScriptIds('3', '4'),
];
const category = new TestContext()
.withScripts([])
.withSubcategories(subcategories)
.build();
// act
const actualIds = category
.getAllScriptsRecursively()
.map((s) => s.executableId);
// assert
expect(actualIds).to.have.deep.members(expectedScriptIds);
});
it('retrieves scripts from both direct children and child categories', () => {
// arrange
const expectedScriptIds: readonly string[] = [
'1', '2', '3', '4', '5', '6',
];
const subcategories: readonly Category[] = [
new CategoryStub('subcategory-1').withScriptIds('1', '2'),
new CategoryStub('subcategory-2').withScriptIds('3', '4'),
];
const scripts: readonly Script[] = [
new ScriptStub('5'),
new ScriptStub('6'),
];
const category = new TestContext()
.withSubcategories(subcategories)
.withScripts(scripts)
.build();
// act
const actualIds = category
.getAllScriptsRecursively()
.map((s) => s.executableId);
// assert
expect(actualIds).to.have.deep.members(expectedScriptIds);
});
it('retrieves scripts from nested categories recursively', () => {
// arrange
const expectedScriptIds: readonly string[] = [
'1', '2', '3', '4', '5', '6',
];
const subcategories: readonly Category[] = [
new CategoryStub('subcategory-1')
.withScriptIds('1', '2')
.withCategory(
new CategoryStub('subcategory-1-subcategory-1')
.withScriptIds('3', '4'),
),
new CategoryStub('subcategory-2')
.withCategories(
new CategoryStub('subcategory-2-subcategory-1')
.withScriptIds('5')
.withCategory(
new CategoryStub('subcategory-2-subcategory-1-subcategory-1')
.withCategory(
new CategoryStub('subcategory-2-subcategory-1-subcategory-1-subcategory-1')
.withScriptIds('6'),
),
),
),
];
// assert
const category = new TestContext()
.withScripts([])
.withSubcategories(subcategories)
.build();
// act
const actualIds = category
.getAllScriptsRecursively()
.map((s) => s.executableId);
// assert
expect(actualIds).to.have.deep.members(expectedScriptIds);
});
});
describe('includes', () => {
it('returns false for scripts not included', () => {
// assert
const expectedResult = false;
const script = new ScriptStub('3');
const childCategory = new CategoryStub('subcategory')
.withScriptIds('1', '2');
const category = new TestContext()
.withSubcategories([childCategory])
.build();
// act
const actual = category.includes(script);
// assert
expect(actual).to.equal(expectedResult);
});
it('returns true for scripts directly included', () => {
// assert
const expectedResult = true;
const script = new ScriptStub('3');
const childCategory = new CategoryStub('subcategory')
.withScript(script)
.withScriptIds('non-related');
const category = new TestContext()
.withSubcategories([childCategory])
.build();
// act
const actual = category.includes(script);
// assert
expect(actual).to.equal(expectedResult);
});
it('returns true for scripts included in nested categories', () => {
// assert
const expectedResult = true;
const script = new ScriptStub('3');
const childCategory = new CategoryStub('subcategory')
.withScriptIds('non-related')
.withCategory(
new CategoryStub('nested-subcategory')
.withScript(script),
);
const category = new TestContext()
.withSubcategories([childCategory])
.build();
// act
const actual = category.includes(script);
// assert
expect(actual).to.equal(expectedResult);
});
});
});
});
class TestContext {
private executableId: ExecutableId = `[${TestContext.name}] test category`;
private name = 'test-category';
private docs: ReadonlyArray<string> = [];
private subcategories: ReadonlyArray<Category> = [];
private scripts: ReadonlyArray<Script> = [
new ScriptStub(`[${TestContext.name}] script`),
];
public withExecutableId(executableId: ExecutableId): this {
this.executableId = executableId;
return this;
}
public withName(name: string): this {
this.name = name;
return this;
}
public withDocs(docs: ReadonlyArray<string>): this {
this.docs = docs;
return this;
}
public withScripts(scripts: ReadonlyArray<Script>): this {
this.scripts = scripts;
return this;
}
public withSubcategories(subcategories: ReadonlyArray<Category>): this {
this.subcategories = subcategories;
return this;
}
public build(): ReturnType<typeof createCategory> {
return createCategory({
executableId: this.executableId,
name: this.name,
docs: this.docs,
subcategories: this.subcategories,
scripts: this.scripts,
});
}
}

View File

@@ -1,217 +0,0 @@
import { describe, it, expect } from 'vitest';
import { CollectionCategory } from '@/domain/Executables/Category/CollectionCategory';
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
import { itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
import type { Category } from '@/domain/Executables/Category/Category';
import type { Script } from '@/domain/Executables/Script/Script';
describe('CollectionCategory', () => {
describe('ctor', () => {
describe('throws error if name is absent', () => {
itEachAbsentStringValue((absentValue) => {
// arrange
const expectedError = 'missing name';
const name = absentValue;
// act
const construct = () => new CategoryBuilder()
.withName(name)
.build();
// assert
expect(construct).to.throw(expectedError);
}, { excludeNull: true, excludeUndefined: true });
});
it('throws error if no children are present', () => {
// arrange
const expectedError = 'A category must have at least one sub-category or script';
const scriptChildren: readonly Script[] = [];
const categoryChildren: readonly Category[] = [];
// act
const construct = () => new CategoryBuilder()
.withSubcategories(categoryChildren)
.withScripts(scriptChildren)
.build();
// assert
expect(construct).to.throw(expectedError);
});
});
describe('getAllScriptsRecursively', () => {
it('retrieves direct child scripts', () => {
// arrange
const expectedScripts = [new ScriptStub('1'), new ScriptStub('2')];
const sut = new CategoryBuilder()
.withScripts(expectedScripts)
.build();
// act
const actual = sut.getAllScriptsRecursively();
// assert
expect(actual).to.have.deep.members(expectedScripts);
});
it('retrieves scripts from direct child categories', () => {
// arrange
const expectedScriptIds = ['1', '2', '3', '4'];
const categories = [
new CategoryStub(31).withScriptIds('1', '2'),
new CategoryStub(32).withScriptIds('3', '4'),
];
const sut = new CategoryBuilder()
.withScripts([])
.withSubcategories(categories)
.build();
// act
const actualIds = sut
.getAllScriptsRecursively()
.map((s) => s.id);
// assert
expect(actualIds).to.have.deep.members(expectedScriptIds);
});
it('retrieves scripts from both direct children and child categories', () => {
// arrange
const expectedScriptIds = ['1', '2', '3', '4', '5', '6'];
const categories = [
new CategoryStub(31).withScriptIds('1', '2'),
new CategoryStub(32).withScriptIds('3', '4'),
];
const scripts = [new ScriptStub('5'), new ScriptStub('6')];
const sut = new CategoryBuilder()
.withSubcategories(categories)
.withScripts(scripts)
.build();
// act
const actualIds = sut
.getAllScriptsRecursively()
.map((s) => s.id);
// assert
expect(actualIds).to.have.deep.members(expectedScriptIds);
});
it('retrieves scripts from nested categories recursively', () => {
// arrange
const expectedScriptIds = ['1', '2', '3', '4', '5', '6'];
const categories = [
new CategoryStub(31)
.withScriptIds('1', '2')
.withCategory(
new CategoryStub(32)
.withScriptIds('3', '4'),
),
new CategoryStub(33)
.withCategories(
new CategoryStub(34)
.withScriptIds('5')
.withCategory(
new CategoryStub(35)
.withCategory(
new CategoryStub(35).withScriptIds('6'),
),
),
),
];
// assert
const sut = new CategoryBuilder()
.withScripts([])
.withSubcategories(categories)
.build();
// act
const actualIds = sut
.getAllScriptsRecursively()
.map((s) => s.id);
// assert
expect(actualIds).to.have.deep.members(expectedScriptIds);
});
});
describe('includes', () => {
it('returns false for scripts not included', () => {
// assert
const expectedResult = false;
const script = new ScriptStub('3');
const childCategory = new CategoryStub(33)
.withScriptIds('1', '2');
const sut = new CategoryBuilder()
.withSubcategories([childCategory])
.build();
// act
const actual = sut.includes(script);
// assert
expect(actual).to.equal(expectedResult);
});
it('returns true for scripts directly included', () => {
// assert
const expectedResult = true;
const script = new ScriptStub('3');
const childCategory = new CategoryStub(33)
.withScript(script)
.withScriptIds('non-related');
const sut = new CategoryBuilder()
.withSubcategories([childCategory])
.build();
// act
const actual = sut.includes(script);
// assert
expect(actual).to.equal(expectedResult);
});
it('returns true for scripts included in nested categories', () => {
// assert
const expectedResult = true;
const script = new ScriptStub('3');
const childCategory = new CategoryStub(22)
.withScriptIds('non-related')
.withCategory(new CategoryStub(33).withScript(script));
const sut = new CategoryBuilder()
.withSubcategories([childCategory])
.build();
// act
const actual = sut.includes(script);
// assert
expect(actual).to.equal(expectedResult);
});
});
});
class CategoryBuilder {
private id = 3264;
private name = 'test-script';
private docs: ReadonlyArray<string> = [];
private subcategories: ReadonlyArray<Category> = [];
private scripts: ReadonlyArray<Script> = [
new ScriptStub(`[${CategoryBuilder.name}] script`),
];
public withId(id: number): this {
this.id = id;
return this;
}
public withName(name: string): this {
this.name = name;
return this;
}
public withDocs(docs: ReadonlyArray<string>): this {
this.docs = docs;
return this;
}
public withScripts(scripts: ReadonlyArray<Script>): this {
this.scripts = scripts;
return this;
}
public withSubcategories(subcategories: ReadonlyArray<Category>): this {
this.subcategories = subcategories;
return this;
}
public build(): CollectionCategory {
return new CollectionCategory({
id: this.id,
name: this.name,
docs: this.docs,
subcategories: this.subcategories,
scripts: this.scripts,
});
}
}

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,