Files
privacy.sexy/tests/unit/domain/Executables/Script/ScriptFactory.spec.ts
undergroundwires 48d6dbd700 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.
2024-07-08 23:23:05 +02:00

164 lines
4.7 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { getEnumValues } from '@/application/Common/Enum';
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('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 script = new TestContext()
.withCode(expected)
.build();
// act
const actual = script.code;
// assert
expect(actual).to.deep.equal(expected);
});
});
describe('canRevert', () => {
it('returns false without revert code', () => {
// arrange
const script = new TestContext()
.withCodes('code')
.build();
// act
const actual = script.canRevert();
// assert
expect(actual).to.equal(false);
});
it('returns true with revert code', () => {
// arrange
const script = new TestContext()
.withCodes('code', 'non empty revert code')
.build();
// act
const actual = script.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 TestContext()
.withRecommendationLevel(invalidValue)
.build();
// assert
expect(construct).to.throw(expectedError);
});
it('handles undefined level correctly', () => {
// arrange
const expected = undefined;
// act
const script = new TestContext()
.withRecommendationLevel(expected)
.build();
// assert
expect(script.level).to.equal(expected);
});
it('correctly assigns valid recommendation levels', () => {
getEnumValues(RecommendationLevel).forEach((enumValue) => {
// arrange
const expectedRecommendationLevel = enumValue;
// act
const script = new TestContext()
.withRecommendationLevel(expectedRecommendationLevel)
.build();
// assert
const actualRecommendationLevel = script.level;
expect(actualRecommendationLevel).to.equal(expectedRecommendationLevel);
});
});
});
describe('docs', () => {
it('correctly assigns docs', () => {
// arrange
const expectedDocs = ['doc1', 'doc2'];
// act
const script = new TestContext()
.withDocs(expectedDocs)
.build();
// assert
const actualDocs = script.docs;
expect(actualDocs).to.equal(expectedDocs);
});
});
});
});
class TestContext {
private name = `[${TestContext.name}]test-script`;
private id: ExecutableId = `[${TestContext.name}]id`;
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 withId(id: ExecutableId): this {
this.id = id;
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(): ReturnType<typeof createScript> {
return createScript({
executableId: this.id,
name: this.name,
code: this.code,
docs: this.docs,
level: this.level,
});
}
}