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:
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