Refactor to unify scripts/categories as Executable
This commit consolidates scripts and categories under a unified 'Executable' concept. This simplifies the architecture and improves code readability. - Introduce subfolders within `src/domain` to segregate domain elements. - Update class and interface names by removing the 'I' prefix in alignment with new coding standards. - Replace 'Node' with 'Executable' to clarify usage; reserve 'Node' exclusively for the UI's tree component.
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
105
tests/unit/domain/Executables/Script/Code/ScriptCode.spec.ts
Normal file
105
tests/unit/domain/Executables/Script/Code/ScriptCode.spec.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { DistinctReversibleScriptCode } from '@/domain/Executables/Script/Code/DistinctReversibleScriptCode';
|
||||
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
|
||||
describe('DistinctReversibleScriptCode', () => {
|
||||
describe('code', () => {
|
||||
describe('throws with invalid code', () => {
|
||||
// arrange
|
||||
const testScenarios: ReadonlyArray<{
|
||||
readonly description: string;
|
||||
readonly code: {
|
||||
readonly execute: string;
|
||||
readonly revert?: string;
|
||||
},
|
||||
readonly expectedError: string;
|
||||
}> = [
|
||||
{
|
||||
description: 'throws when "execute" and "revert" are same',
|
||||
code: {
|
||||
execute: 'same',
|
||||
revert: 'same',
|
||||
},
|
||||
expectedError: '(revert): Code itself and its reverting code cannot be the same',
|
||||
},
|
||||
...getAbsentStringTestCases({ excludeNull: true, excludeUndefined: true })
|
||||
.map((testCase) => ({
|
||||
description: `cannot construct with ${testCase.valueName} "execute"`,
|
||||
code: {
|
||||
execute: testCase.absentValue,
|
||||
revert: 'code',
|
||||
},
|
||||
expectedError: 'missing code',
|
||||
})),
|
||||
];
|
||||
for (const testCase of testScenarios) {
|
||||
it(testCase.description, () => {
|
||||
// act
|
||||
const act = () => new ScriptCodeBuilder()
|
||||
.withExecute(testCase.code.execute)
|
||||
.withRevert(testCase.code.revert)
|
||||
.build();
|
||||
// assert
|
||||
expect(act).to.throw(testCase.expectedError);
|
||||
});
|
||||
}
|
||||
});
|
||||
describe('sets as expected with valid "execute" or "revert"', () => {
|
||||
// arrange
|
||||
const testScenarios: ReadonlyArray<{
|
||||
readonly description: string;
|
||||
readonly code: string;
|
||||
readonly revertCode: string | undefined;
|
||||
}> = [
|
||||
{
|
||||
description: 'code and revert code is given',
|
||||
code: 'valid code',
|
||||
revertCode: 'valid revert-code',
|
||||
},
|
||||
...getAbsentStringTestCases({ excludeNull: true })
|
||||
.map((testCase) => ({
|
||||
description: `only code is given but not revert code (given ${testCase.valueName})`,
|
||||
code: 'valid code',
|
||||
revertCode: testCase.absentValue,
|
||||
expectedError: 'missing code',
|
||||
})),
|
||||
];
|
||||
// assert
|
||||
for (const testCase of testScenarios) {
|
||||
it(testCase.description, () => {
|
||||
// act
|
||||
const sut = new ScriptCodeBuilder()
|
||||
.withExecute(testCase.code)
|
||||
.withRevert(testCase.revertCode)
|
||||
.build();
|
||||
// assert
|
||||
expect(sut.execute).to.equal(testCase.code);
|
||||
expect(sut.revert).to.equal(testCase.revertCode);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class ScriptCodeBuilder {
|
||||
public execute = 'default-execute-code';
|
||||
|
||||
public revert: string | undefined = '';
|
||||
|
||||
public withExecute(execute: string) {
|
||||
this.execute = execute;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withRevert(revert: string | undefined) {
|
||||
this.revert = revert;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build(): DistinctReversibleScriptCode {
|
||||
return new DistinctReversibleScriptCode(
|
||||
this.execute,
|
||||
this.revert,
|
||||
);
|
||||
}
|
||||
}
|
||||
140
tests/unit/domain/Executables/Script/CollectionScript.spec.ts
Normal file
140
tests/unit/domain/Executables/Script/CollectionScript.spec.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
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';
|
||||
|
||||
describe('CollectionScript', () => {
|
||||
describe('ctor', () => {
|
||||
describe('scriptCode', () => {
|
||||
it('assigns code correctly', () => {
|
||||
// arrange
|
||||
const expected = new ScriptCodeStub();
|
||||
const sut = new ScriptBuilder()
|
||||
.withCode(expected)
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.code;
|
||||
// assert
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
describe('canRevert', () => {
|
||||
it('returns false without revert code', () => {
|
||||
// arrange
|
||||
const sut = new ScriptBuilder()
|
||||
.withCodes('code')
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.canRevert();
|
||||
// assert
|
||||
expect(actual).to.equal(false);
|
||||
});
|
||||
it('returns true with revert code', () => {
|
||||
// arrange
|
||||
const sut = new ScriptBuilder()
|
||||
.withCodes('code', 'non empty revert code')
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.canRevert();
|
||||
// assert
|
||||
expect(actual).to.equal(true);
|
||||
});
|
||||
});
|
||||
describe('level', () => {
|
||||
it('throws when constructed with invalid level', () => {
|
||||
// arrange
|
||||
const invalidValue: RecommendationLevel = 55 as never;
|
||||
const expectedError = 'invalid level';
|
||||
// act
|
||||
const construct = () => new ScriptBuilder()
|
||||
.withRecommendationLevel(invalidValue)
|
||||
.build();
|
||||
// assert
|
||||
expect(construct).to.throw(expectedError);
|
||||
});
|
||||
it('handles undefined level correctly', () => {
|
||||
// arrange
|
||||
const expected = undefined;
|
||||
// act
|
||||
const sut = new ScriptBuilder()
|
||||
.withRecommendationLevel(expected)
|
||||
.build();
|
||||
// assert
|
||||
expect(sut.level).to.equal(expected);
|
||||
});
|
||||
it('correctly assigns valid recommendation levels', () => {
|
||||
// arrange
|
||||
for (const expected of getEnumValues(RecommendationLevel)) {
|
||||
// act
|
||||
const sut = new ScriptBuilder()
|
||||
.withRecommendationLevel(expected)
|
||||
.build();
|
||||
// assert
|
||||
const actual = sut.level;
|
||||
expect(actual).to.equal(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
describe('docs', () => {
|
||||
it('correctly assigns docs', () => {
|
||||
// arrange
|
||||
const expected = ['doc1', 'doc2'];
|
||||
// act
|
||||
const sut = new ScriptBuilder()
|
||||
.withDocs(expected)
|
||||
.build();
|
||||
const actual = sut.docs;
|
||||
// assert
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class ScriptBuilder {
|
||||
private name = 'test-script';
|
||||
|
||||
private code: ScriptCode = new ScriptCodeStub();
|
||||
|
||||
private level? = RecommendationLevel.Standard;
|
||||
|
||||
private docs: readonly string[] = [];
|
||||
|
||||
public withCodes(code: string, revertCode = ''): this {
|
||||
this.code = new ScriptCodeStub()
|
||||
.withExecute(code)
|
||||
.withRevert(revertCode);
|
||||
return this;
|
||||
}
|
||||
|
||||
public withCode(code: ScriptCode): this {
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withName(name: string): this {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withRecommendationLevel(level: RecommendationLevel | undefined): this {
|
||||
this.level = level;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withDocs(docs: readonly string[]): this {
|
||||
this.docs = docs;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build(): CollectionScript {
|
||||
return new CollectionScript({
|
||||
name: this.name,
|
||||
code: this.code,
|
||||
docs: this.docs,
|
||||
level: this.level,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user