Merge pull request 'core tests' (#1) from tests/core into main
Some checks failed
CI / lint-typecheck (push) Failing after 4m50s
CI / tests (push) Has been skipped
CI / contract-tests (push) Has been skipped
CI / e2e-tests (push) Has been skipped
CI / comment-pr (push) Has been skipped
CI / commit-types (push) Has been skipped

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-01-24 00:40:23 +00:00
63 changed files with 15156 additions and 38 deletions

View File

@@ -0,0 +1,408 @@
import { describe, expect, it } from 'vitest';
import { AdminUser } from '@core/admin/domain/entities/AdminUser';
import { AdminUserOrmEntity } from '../entities/AdminUserOrmEntity';
import { AdminUserOrmMapper } from './AdminUserOrmMapper';
import { TypeOrmAdminSchemaError } from '../errors/TypeOrmAdminSchemaError';
describe('AdminUserOrmMapper', () => {
describe('TDD - Test First', () => {
describe('toDomain', () => {
it('should map valid ORM entity to domain entity', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['owner'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
const mapper = new AdminUserOrmMapper();
// Act
const domain = mapper.toDomain(entity);
// Assert
expect(domain.id.value).toBe('user-123');
expect(domain.email.value).toBe('test@example.com');
expect(domain.displayName).toBe('Test User');
expect(domain.roles).toHaveLength(1);
expect(domain.roles[0]!.value).toBe('owner');
expect(domain.status.value).toBe('active');
expect(domain.createdAt).toEqual(new Date('2024-01-01'));
expect(domain.updatedAt).toEqual(new Date('2024-01-02'));
});
it('should map entity with optional fields', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['user'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
entity.primaryDriverId = 'driver-456';
entity.lastLoginAt = new Date('2024-01-03');
const mapper = new AdminUserOrmMapper();
// Act
const domain = mapper.toDomain(entity);
// Assert
expect(domain.primaryDriverId).toBe('driver-456');
expect(domain.lastLoginAt).toEqual(new Date('2024-01-03'));
});
it('should handle null optional fields', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['user'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
entity.primaryDriverId = null;
entity.lastLoginAt = null;
const mapper = new AdminUserOrmMapper();
// Act
const domain = mapper.toDomain(entity);
// Assert
expect(domain.primaryDriverId).toBeUndefined();
expect(domain.lastLoginAt).toBeUndefined();
});
it('should throw error for missing id', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = '';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['user'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
const mapper = new AdminUserOrmMapper();
// Act & Assert
expect(() => mapper.toDomain(entity)).toThrow(TypeOrmAdminSchemaError);
expect(() => mapper.toDomain(entity)).toThrow('Field id must be a non-empty string');
});
it('should throw error for missing email', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = '';
entity.displayName = 'Test User';
entity.roles = ['user'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
const mapper = new AdminUserOrmMapper();
// Act & Assert
expect(() => mapper.toDomain(entity)).toThrow(TypeOrmAdminSchemaError);
expect(() => mapper.toDomain(entity)).toThrow('Field email must be a non-empty string');
});
it('should throw error for missing displayName', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = '';
entity.roles = ['user'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
const mapper = new AdminUserOrmMapper();
// Act & Assert
expect(() => mapper.toDomain(entity)).toThrow(TypeOrmAdminSchemaError);
expect(() => mapper.toDomain(entity)).toThrow('Field displayName must be a non-empty string');
});
it('should throw error for invalid roles array', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = null as unknown as string[];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
const mapper = new AdminUserOrmMapper();
// Act & Assert
expect(() => mapper.toDomain(entity)).toThrow(TypeOrmAdminSchemaError);
expect(() => mapper.toDomain(entity)).toThrow('Field roles must be an array of strings');
});
it('should throw error for invalid roles array items', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['user', 123 as unknown as string];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
const mapper = new AdminUserOrmMapper();
// Act & Assert
expect(() => mapper.toDomain(entity)).toThrow(TypeOrmAdminSchemaError);
expect(() => mapper.toDomain(entity)).toThrow('Field roles must be an array of strings');
});
it('should throw error for missing status', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['user'];
entity.status = '';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
const mapper = new AdminUserOrmMapper();
// Act & Assert
expect(() => mapper.toDomain(entity)).toThrow(TypeOrmAdminSchemaError);
expect(() => mapper.toDomain(entity)).toThrow('Field status must be a non-empty string');
});
it('should throw error for invalid createdAt', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['user'];
entity.status = 'active';
entity.createdAt = new Date('invalid') as unknown as Date;
entity.updatedAt = new Date('2024-01-02');
const mapper = new AdminUserOrmMapper();
// Act & Assert
expect(() => mapper.toDomain(entity)).toThrow(TypeOrmAdminSchemaError);
expect(() => mapper.toDomain(entity)).toThrow('Field createdAt must be a valid Date');
});
it('should throw error for invalid updatedAt', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['user'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('invalid') as unknown as Date;
const mapper = new AdminUserOrmMapper();
// Act & Assert
expect(() => mapper.toDomain(entity)).toThrow(TypeOrmAdminSchemaError);
expect(() => mapper.toDomain(entity)).toThrow('Field updatedAt must be a valid Date');
});
it('should throw error for invalid primaryDriverId type', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['user'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
entity.primaryDriverId = 123 as unknown as string;
const mapper = new AdminUserOrmMapper();
// Act & Assert
expect(() => mapper.toDomain(entity)).toThrow(TypeOrmAdminSchemaError);
expect(() => mapper.toDomain(entity)).toThrow('Field primaryDriverId must be a string or undefined');
});
it('should throw error for invalid lastLoginAt type', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['user'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
entity.lastLoginAt = 'invalid' as unknown as Date;
const mapper = new AdminUserOrmMapper();
// Act & Assert
expect(() => mapper.toDomain(entity)).toThrow(TypeOrmAdminSchemaError);
expect(() => mapper.toDomain(entity)).toThrow('Field lastLoginAt must be a valid Date');
});
it('should handle multiple roles', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['owner', 'admin'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
const mapper = new AdminUserOrmMapper();
// Act
const domain = mapper.toDomain(entity);
// Assert
expect(domain.roles).toHaveLength(2);
expect(domain.roles.map(r => r.value)).toContain('owner');
expect(domain.roles.map(r => r.value)).toContain('admin');
});
});
describe('toOrmEntity', () => {
it('should map domain entity to ORM entity', () => {
// Arrange
const domain = AdminUser.create({
id: 'user-123',
email: 'test@example.com',
displayName: 'Test User',
roles: ['owner'],
status: 'active',
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-02'),
});
const mapper = new AdminUserOrmMapper();
// Act
const entity = mapper.toOrmEntity(domain);
// Assert
expect(entity.id).toBe('user-123');
expect(entity.email).toBe('test@example.com');
expect(entity.displayName).toBe('Test User');
expect(entity.roles).toEqual(['owner']);
expect(entity.status).toBe('active');
expect(entity.createdAt).toEqual(new Date('2024-01-01'));
expect(entity.updatedAt).toEqual(new Date('2024-01-02'));
});
it('should map domain entity with optional fields', () => {
// Arrange
const domain = AdminUser.create({
id: 'user-123',
email: 'test@example.com',
displayName: 'Test User',
roles: ['user'],
status: 'active',
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-02'),
primaryDriverId: 'driver-456',
lastLoginAt: new Date('2024-01-03'),
});
const mapper = new AdminUserOrmMapper();
// Act
const entity = mapper.toOrmEntity(domain);
// Assert
expect(entity.primaryDriverId).toBe('driver-456');
expect(entity.lastLoginAt).toEqual(new Date('2024-01-03'));
});
it('should handle domain entity without optional fields', () => {
// Arrange
const domain = AdminUser.create({
id: 'user-123',
email: 'test@example.com',
displayName: 'Test User',
roles: ['user'],
status: 'active',
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-02'),
});
const mapper = new AdminUserOrmMapper();
// Act
const entity = mapper.toOrmEntity(domain);
// Assert
expect(entity.primaryDriverId).toBeUndefined();
expect(entity.lastLoginAt).toBeUndefined();
});
it('should map domain entity with multiple roles', () => {
// Arrange
const domain = AdminUser.create({
id: 'user-123',
email: 'test@example.com',
displayName: 'Test User',
roles: ['owner', 'admin'],
status: 'active',
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-02'),
});
const mapper = new AdminUserOrmMapper();
// Act
const entity = mapper.toOrmEntity(domain);
// Assert
expect(entity.roles).toEqual(['owner', 'admin']);
});
});
describe('toStored', () => {
it('should call toDomain for stored entity', () => {
// Arrange
const entity = new AdminUserOrmEntity();
entity.id = 'user-123';
entity.email = 'test@example.com';
entity.displayName = 'Test User';
entity.roles = ['owner'];
entity.status = 'active';
entity.createdAt = new Date('2024-01-01');
entity.updatedAt = new Date('2024-01-02');
const mapper = new AdminUserOrmMapper();
// Act
const domain = mapper.toStored(entity);
// Assert
expect(domain.id.value).toBe('user-123');
expect(domain.email.value).toBe('test@example.com');
expect(domain.displayName).toBe('Test User');
});
});
});
});

View File

@@ -0,0 +1,253 @@
import { describe, expect, it } from 'vitest';
import {
assertNonEmptyString,
assertStringArray,
assertDate,
assertOptionalDate,
assertOptionalString,
} from './TypeOrmAdminSchemaGuards';
import { TypeOrmAdminSchemaError } from '../errors/TypeOrmAdminSchemaError';
describe('TypeOrmAdminSchemaGuards', () => {
describe('TDD - Test First', () => {
describe('assertNonEmptyString', () => {
it('should not throw for valid non-empty string', () => {
// Arrange & Act & Assert
expect(() => assertNonEmptyString('TestEntity', 'fieldName', 'valid string')).not.toThrow();
});
it('should throw for empty string', () => {
// Arrange & Act & Assert
expect(() => assertNonEmptyString('TestEntity', 'fieldName', '')).toThrow(TypeOrmAdminSchemaError);
expect(() => assertNonEmptyString('TestEntity', 'fieldName', '')).toThrow('Field fieldName must be a non-empty string');
});
it('should throw for string with only whitespace', () => {
// Arrange & Act & Assert
expect(() => assertNonEmptyString('TestEntity', 'fieldName', ' ')).toThrow(TypeOrmAdminSchemaError);
expect(() => assertNonEmptyString('TestEntity', 'fieldName', ' ')).toThrow('Field fieldName must be a non-empty string');
});
it('should throw for null', () => {
// Arrange & Act & Assert
expect(() => assertNonEmptyString('TestEntity', 'fieldName', null)).toThrow(TypeOrmAdminSchemaError);
expect(() => assertNonEmptyString('TestEntity', 'fieldName', null)).toThrow('Field fieldName must be a non-empty string');
});
it('should throw for undefined', () => {
// Arrange & Act & Assert
expect(() => assertNonEmptyString('TestEntity', 'fieldName', undefined)).toThrow(TypeOrmAdminSchemaError);
expect(() => assertNonEmptyString('TestEntity', 'fieldName', undefined)).toThrow('Field fieldName must be a non-empty string');
});
it('should throw for number', () => {
// Arrange & Act & Assert
expect(() => assertNonEmptyString('TestEntity', 'fieldName', 123)).toThrow(TypeOrmAdminSchemaError);
expect(() => assertNonEmptyString('TestEntity', 'fieldName', 123)).toThrow('Field fieldName must be a non-empty string');
});
it('should throw for object', () => {
// Arrange & Act & Assert
expect(() => assertNonEmptyString('TestEntity', 'fieldName', {})).toThrow(TypeOrmAdminSchemaError);
expect(() => assertNonEmptyString('TestEntity', 'fieldName', {})).toThrow('Field fieldName must be a non-empty string');
});
it('should throw for array', () => {
// Arrange & Act & Assert
expect(() => assertNonEmptyString('TestEntity', 'fieldName', [])).toThrow(TypeOrmAdminSchemaError);
expect(() => assertNonEmptyString('TestEntity', 'fieldName', [])).toThrow('Field fieldName must be a non-empty string');
});
it('should include entity name in error message', () => {
// Arrange & Act & Assert
expect(() => assertNonEmptyString('AdminUser', 'email', '')).toThrow('[TypeOrmAdminSchemaError] AdminUser.email: INVALID_STRING - Field email must be a non-empty string');
});
});
describe('assertStringArray', () => {
it('should not throw for valid string array', () => {
// Arrange & Act & Assert
expect(() => assertStringArray('TestEntity', 'fieldName', ['a', 'b', 'c'])).not.toThrow();
});
it('should not throw for empty array', () => {
// Arrange & Act & Assert
expect(() => assertStringArray('TestEntity', 'fieldName', [])).not.toThrow();
});
it('should throw for non-array', () => {
// Arrange & Act & Assert
expect(() => assertStringArray('TestEntity', 'fieldName', 'not an array')).toThrow(TypeOrmAdminSchemaError);
expect(() => assertStringArray('TestEntity', 'fieldName', 'not an array')).toThrow('Field fieldName must be an array of strings');
});
it('should throw for null', () => {
// Arrange & Act & Assert
expect(() => assertStringArray('TestEntity', 'fieldName', null)).toThrow(TypeOrmAdminSchemaError);
expect(() => assertStringArray('TestEntity', 'fieldName', null)).toThrow('Field fieldName must be an array of strings');
});
it('should throw for undefined', () => {
// Arrange & Act & Assert
expect(() => assertStringArray('TestEntity', 'fieldName', undefined)).toThrow(TypeOrmAdminSchemaError);
expect(() => assertStringArray('TestEntity', 'fieldName', undefined)).toThrow('Field fieldName must be an array of strings');
});
it('should throw for array with non-string items', () => {
// Arrange & Act & Assert
expect(() => assertStringArray('TestEntity', 'fieldName', ['a', 123, 'c'])).toThrow(TypeOrmAdminSchemaError);
expect(() => assertStringArray('TestEntity', 'fieldName', ['a', 123, 'c'])).toThrow('Field fieldName must be an array of strings');
});
it('should throw for array with null items', () => {
// Arrange & Act & Assert
expect(() => assertStringArray('TestEntity', 'fieldName', ['a', null, 'c'])).toThrow(TypeOrmAdminSchemaError);
expect(() => assertStringArray('TestEntity', 'fieldName', ['a', null, 'c'])).toThrow('Field fieldName must be an array of strings');
});
it('should throw for array with undefined items', () => {
// Arrange & Act & Assert
expect(() => assertStringArray('TestEntity', 'fieldName', ['a', undefined, 'c'])).toThrow(TypeOrmAdminSchemaError);
expect(() => assertStringArray('TestEntity', 'fieldName', ['a', undefined, 'c'])).toThrow('Field fieldName must be an array of strings');
});
it('should throw for array with object items', () => {
// Arrange & Act & Assert
expect(() => assertStringArray('TestEntity', 'fieldName', ['a', {}, 'c'])).toThrow(TypeOrmAdminSchemaError);
expect(() => assertStringArray('TestEntity', 'fieldName', ['a', {}, 'c'])).toThrow('Field fieldName must be an array of strings');
});
it('should include entity name in error message', () => {
// Arrange & Act & Assert
expect(() => assertStringArray('AdminUser', 'roles', null)).toThrow('[TypeOrmAdminSchemaError] AdminUser.roles: INVALID_STRING_ARRAY - Field roles must be an array of strings');
});
});
describe('assertDate', () => {
it('should not throw for valid Date', () => {
// Arrange & Act & Assert
expect(() => assertDate('TestEntity', 'fieldName', new Date())).not.toThrow();
});
it('should not throw for Date with valid timestamp', () => {
// Arrange & Act & Assert
expect(() => assertDate('TestEntity', 'fieldName', new Date('2024-01-01'))).not.toThrow();
});
it('should throw for null', () => {
// Arrange & Act & Assert
expect(() => assertDate('TestEntity', 'fieldName', null)).toThrow(TypeOrmAdminSchemaError);
expect(() => assertDate('TestEntity', 'fieldName', null)).toThrow('Field fieldName must be a valid Date');
});
it('should throw for undefined', () => {
// Arrange & Act & Assert
expect(() => assertDate('TestEntity', 'fieldName', undefined)).toThrow(TypeOrmAdminSchemaError);
expect(() => assertDate('TestEntity', 'fieldName', undefined)).toThrow('Field fieldName must be a valid Date');
});
it('should throw for string', () => {
// Arrange & Act & Assert
expect(() => assertDate('TestEntity', 'fieldName', '2024-01-01')).toThrow(TypeOrmAdminSchemaError);
expect(() => assertDate('TestEntity', 'fieldName', '2024-01-01')).toThrow('Field fieldName must be a valid Date');
});
it('should throw for number', () => {
// Arrange & Act & Assert
expect(() => assertDate('TestEntity', 'fieldName', 1234567890)).toThrow(TypeOrmAdminSchemaError);
expect(() => assertDate('TestEntity', 'fieldName', 1234567890)).toThrow('Field fieldName must be a valid Date');
});
it('should throw for object', () => {
// Arrange & Act & Assert
expect(() => assertDate('TestEntity', 'fieldName', {})).toThrow(TypeOrmAdminSchemaError);
expect(() => assertDate('TestEntity', 'fieldName', {})).toThrow('Field fieldName must be a valid Date');
});
it('should throw for invalid Date (NaN)', () => {
// Arrange & Act & Assert
expect(() => assertDate('TestEntity', 'fieldName', new Date('invalid'))).toThrow(TypeOrmAdminSchemaError);
expect(() => assertDate('TestEntity', 'fieldName', new Date('invalid'))).toThrow('Field fieldName must be a valid Date');
});
it('should include entity name in error message', () => {
// Arrange & Act & Assert
expect(() => assertDate('AdminUser', 'createdAt', null)).toThrow('[TypeOrmAdminSchemaError] AdminUser.createdAt: INVALID_DATE - Field createdAt must be a valid Date');
});
});
describe('assertOptionalDate', () => {
it('should not throw for valid Date', () => {
// Arrange & Act & Assert
expect(() => assertOptionalDate('TestEntity', 'fieldName', new Date())).not.toThrow();
});
it('should not throw for null', () => {
// Arrange & Act & Assert
expect(() => assertOptionalDate('TestEntity', 'fieldName', null)).not.toThrow();
});
it('should not throw for undefined', () => {
// Arrange & Act & Assert
expect(() => assertOptionalDate('TestEntity', 'fieldName', undefined)).not.toThrow();
});
it('should throw for invalid Date', () => {
// Arrange & Act & Assert
expect(() => assertOptionalDate('TestEntity', 'fieldName', new Date('invalid'))).toThrow(TypeOrmAdminSchemaError);
expect(() => assertOptionalDate('TestEntity', 'fieldName', new Date('invalid'))).toThrow('Field fieldName must be a valid Date');
});
it('should throw for string', () => {
// Arrange & Act & Assert
expect(() => assertOptionalDate('TestEntity', 'fieldName', '2024-01-01')).toThrow(TypeOrmAdminSchemaError);
expect(() => assertOptionalDate('TestEntity', 'fieldName', '2024-01-01')).toThrow('Field fieldName must be a valid Date');
});
it('should include entity name in error message', () => {
// Arrange & Act & Assert
expect(() => assertOptionalDate('AdminUser', 'lastLoginAt', new Date('invalid'))).toThrow('[TypeOrmAdminSchemaError] AdminUser.lastLoginAt: INVALID_DATE - Field lastLoginAt must be a valid Date');
});
});
describe('assertOptionalString', () => {
it('should not throw for valid string', () => {
// Arrange & Act & Assert
expect(() => assertOptionalString('TestEntity', 'fieldName', 'valid string')).not.toThrow();
});
it('should not throw for null', () => {
// Arrange & Act & Assert
expect(() => assertOptionalString('TestEntity', 'fieldName', null)).not.toThrow();
});
it('should not throw for undefined', () => {
// Arrange & Act & Assert
expect(() => assertOptionalString('TestEntity', 'fieldName', undefined)).not.toThrow();
});
it('should throw for number', () => {
// Arrange & Act & Assert
expect(() => assertOptionalString('TestEntity', 'fieldName', 123)).toThrow(TypeOrmAdminSchemaError);
expect(() => assertOptionalString('TestEntity', 'fieldName', 123)).toThrow('Field fieldName must be a string or undefined');
});
it('should throw for object', () => {
// Arrange & Act & Assert
expect(() => assertOptionalString('TestEntity', 'fieldName', {})).toThrow(TypeOrmAdminSchemaError);
expect(() => assertOptionalString('TestEntity', 'fieldName', {})).toThrow('Field fieldName must be a string or undefined');
});
it('should throw for array', () => {
// Arrange & Act & Assert
expect(() => assertOptionalString('TestEntity', 'fieldName', [])).toThrow(TypeOrmAdminSchemaError);
expect(() => assertOptionalString('TestEntity', 'fieldName', [])).toThrow('Field fieldName must be a string or undefined');
});
it('should include entity name in error message', () => {
// Arrange & Act & Assert
expect(() => assertOptionalString('AdminUser', 'primaryDriverId', 123)).toThrow('[TypeOrmAdminSchemaError] AdminUser.primaryDriverId: INVALID_OPTIONAL_STRING - Field primaryDriverId must be a string or undefined');
});
});
});
});

View File

@@ -0,0 +1,116 @@
const { RuleTester } = require('eslint');
const rule = require('./domain-no-application');
const ruleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
ecmaFeatures: {
jsx: false,
},
},
});
ruleTester.run('domain-no-application', rule, {
valid: [
// Domain file importing from domain
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { UserId } from './UserId';",
},
// Domain file importing from shared
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { ValueObject } from '../shared/ValueObject';",
},
// Domain file importing from ports
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { UserRepository } from '../ports/UserRepository';",
},
// Non-domain file importing from application
{
filename: '/path/to/core/application/user/CreateUser.ts',
code: "import { CreateUserCommand } from './CreateUserCommand';",
},
// Non-domain file importing from application
{
filename: '/path/to/core/application/user/CreateUser.ts',
code: "import { UserService } from '../services/UserService';",
},
// Domain file with no imports
{
filename: '/path/to/core/domain/user/User.ts',
code: "export class User {}",
},
// Domain file with multiple imports, none from application
{
filename: '/path/to/core/domain/user/User.ts',
code: `
import { UserId } from './UserId';
import { UserName } from './UserName';
import { ValueObject } from '../shared/ValueObject';
`,
},
],
invalid: [
// Domain file importing from application
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { CreateUserCommand } from '../application/user/CreateUserCommand';",
errors: [
{
messageId: 'forbiddenImport',
data: {
source: '../application/user/CreateUserCommand',
},
},
],
},
// Domain file importing from application with different path
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { UserService } from '../../application/services/UserService';",
errors: [
{
messageId: 'forbiddenImport',
data: {
source: '../../application/services/UserService',
},
},
],
},
// Domain file importing from application with absolute path
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { CreateUserCommand } from 'core/application/user/CreateUserCommand';",
errors: [
{
messageId: 'forbiddenImport',
data: {
source: 'core/application/user/CreateUserCommand',
},
},
],
},
// Domain file with multiple imports, one from application
{
filename: '/path/to/core/domain/user/User.ts',
code: `
import { UserId } from './UserId';
import { CreateUserCommand } from '../application/user/CreateUserCommand';
import { UserName } from './UserName';
`,
errors: [
{
messageId: 'forbiddenImport',
data: {
source: '../application/user/CreateUserCommand',
},
},
],
},
],
});

View File

@@ -0,0 +1,79 @@
const index = require('./index');
describe('eslint-rules index', () => {
describe('rules', () => {
it('should export no-index-files rule', () => {
expect(index.rules['no-index-files']).toBeDefined();
expect(index.rules['no-index-files'].meta).toBeDefined();
expect(index.rules['no-index-files'].create).toBeDefined();
});
it('should export no-framework-imports rule', () => {
expect(index.rules['no-framework-imports']).toBeDefined();
expect(index.rules['no-framework-imports'].meta).toBeDefined();
expect(index.rules['no-framework-imports'].create).toBeDefined();
});
it('should export domain-no-application rule', () => {
expect(index.rules['domain-no-application']).toBeDefined();
expect(index.rules['domain-no-application'].meta).toBeDefined();
expect(index.rules['domain-no-application'].create).toBeDefined();
});
it('should have exactly 3 rules', () => {
expect(Object.keys(index.rules)).toHaveLength(3);
});
});
describe('configs', () => {
it('should export recommended config', () => {
expect(index.configs.recommended).toBeDefined();
});
it('recommended config should have gridpilot-core-rules plugin', () => {
expect(index.configs.recommended.plugins).toContain('gridpilot-core-rules');
});
it('recommended config should enable all rules', () => {
expect(index.configs.recommended.rules['gridpilot-core-rules/no-index-files']).toBe('error');
expect(index.configs.recommended.rules['gridpilot-core-rules/no-framework-imports']).toBe('error');
expect(index.configs.recommended.rules['gridpilot-core-rules/domain-no-application']).toBe('error');
});
it('recommended config should have exactly 3 rules', () => {
expect(Object.keys(index.configs.recommended.rules)).toHaveLength(3);
});
});
describe('rule metadata', () => {
it('no-index-files should have correct metadata', () => {
const rule = index.rules['no-index-files'];
expect(rule.meta.type).toBe('problem');
expect(rule.meta.docs.category).toBe('Best Practices');
expect(rule.meta.docs.recommended).toBe(true);
expect(rule.meta.fixable).toBe(null);
expect(rule.meta.schema).toEqual([]);
expect(rule.meta.messages.indexFile).toBeDefined();
});
it('no-framework-imports should have correct metadata', () => {
const rule = index.rules['no-framework-imports'];
expect(rule.meta.type).toBe('problem');
expect(rule.meta.docs.category).toBe('Architecture');
expect(rule.meta.docs.recommended).toBe(true);
expect(rule.meta.fixable).toBe(null);
expect(rule.meta.schema).toEqual([]);
expect(rule.meta.messages.frameworkImport).toBeDefined();
});
it('domain-no-application should have correct metadata', () => {
const rule = index.rules['domain-no-application'];
expect(rule.meta.type).toBe('problem');
expect(rule.meta.docs.category).toBe('Architecture');
expect(rule.meta.docs.recommended).toBe(true);
expect(rule.meta.fixable).toBe(null);
expect(rule.meta.schema).toEqual([]);
expect(rule.meta.messages.forbiddenImport).toBeDefined();
});
});
});

View File

@@ -0,0 +1,166 @@
const { RuleTester } = require('eslint');
const rule = require('./no-framework-imports');
const ruleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
ecmaFeatures: {
jsx: false,
},
},
});
ruleTester.run('no-framework-imports', rule, {
valid: [
// Import from domain
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { UserId } from './UserId';",
},
// Import from application
{
filename: '/path/to/core/application/user/CreateUser.ts',
code: "import { CreateUserCommand } from './CreateUserCommand';",
},
// Import from shared
{
filename: '/path/to/core/shared/ValueObject.ts',
code: "import { ValueObject } from './ValueObject';",
},
// Import from ports
{
filename: '/path/to/core/ports/UserRepository.ts',
code: "import { User } from '../domain/user/User';",
},
// Import from external packages (not frameworks)
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { v4 as uuidv4 } from 'uuid';",
},
// Import from internal packages
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { SomeUtil } from '@core/shared/SomeUtil';",
},
// No imports
{
filename: '/path/to/core/domain/user/User.ts',
code: "export class User {}",
},
// Multiple valid imports
{
filename: '/path/to/core/domain/user/User.ts',
code: `
import { UserId } from './UserId';
import { UserName } from './UserName';
import { ValueObject } from '../shared/ValueObject';
`,
},
],
invalid: [
// Import from @nestjs
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { Injectable } from '@nestjs/common';",
errors: [
{
messageId: 'frameworkImport',
data: {
source: '@nestjs/common',
},
},
],
},
// Import from @nestjs/core
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { Module } from '@nestjs/core';",
errors: [
{
messageId: 'frameworkImport',
data: {
source: '@nestjs/core',
},
},
],
},
// Import from express
{
filename: '/path/to/core/domain/user/User.ts',
code: "import express from 'express';",
errors: [
{
messageId: 'frameworkImport',
data: {
source: 'express',
},
},
],
},
// Import from react
{
filename: '/path/to/core/domain/user/User.ts',
code: "import React from 'react';",
errors: [
{
messageId: 'frameworkImport',
data: {
source: 'react',
},
},
],
},
// Import from next
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { useRouter } from 'next/router';",
errors: [
{
messageId: 'frameworkImport',
data: {
source: 'next/router',
},
},
],
},
// Import from @nestjs with subpath
{
filename: '/path/to/core/domain/user/User.ts',
code: "import { Controller } from '@nestjs/common';",
errors: [
{
messageId: 'frameworkImport',
data: {
source: '@nestjs/common',
},
},
],
},
// Multiple framework imports
{
filename: '/path/to/core/domain/user/User.ts',
code: `
import { Injectable } from '@nestjs/common';
import { UserId } from './UserId';
import React from 'react';
`,
errors: [
{
messageId: 'frameworkImport',
data: {
source: '@nestjs/common',
},
},
{
messageId: 'frameworkImport',
data: {
source: 'react',
},
},
],
},
],
});

View File

@@ -0,0 +1,131 @@
const { RuleTester } = require('eslint');
const rule = require('./no-index-files');
const ruleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
ecmaFeatures: {
jsx: false,
},
},
});
ruleTester.run('no-index-files', rule, {
valid: [
// Regular file in domain
{
filename: '/path/to/core/domain/user/User.ts',
code: "export class User {}",
},
// Regular file in application
{
filename: '/path/to/core/application/user/CreateUser.ts',
code: "export class CreateUser {}",
},
// Regular file in shared
{
filename: '/path/to/core/shared/ValueObject.ts',
code: "export class ValueObject {}",
},
// Regular file in ports
{
filename: '/path/to/core/ports/UserRepository.ts',
code: "export interface UserRepository {}",
},
// File with index in the middle of the path
{
filename: '/path/to/core/domain/user/index/User.ts',
code: "export class User {}",
},
// File with index in the name but not at the end
{
filename: '/path/to/core/domain/user/indexHelper.ts',
code: "export class IndexHelper {}",
},
// Root index.ts is allowed
{
filename: '/path/to/core/index.ts',
code: "export * from './domain';",
},
// File with index.ts in the middle of the path
{
filename: '/path/to/core/domain/index/User.ts',
code: "export class User {}",
},
],
invalid: [
// index.ts in domain
{
filename: '/path/to/core/domain/user/index.ts',
code: "export * from './User';",
errors: [
{
messageId: 'indexFile',
},
],
},
// index.ts in application
{
filename: '/path/to/core/application/user/index.ts',
code: "export * from './CreateUser';",
errors: [
{
messageId: 'indexFile',
},
],
},
// index.ts in shared
{
filename: '/path/to/core/shared/index.ts',
code: "export * from './ValueObject';",
errors: [
{
messageId: 'indexFile',
},
],
},
// index.ts in ports
{
filename: '/path/to/core/ports/index.ts',
code: "export * from './UserRepository';",
errors: [
{
messageId: 'indexFile',
},
],
},
// index.ts with Windows path separator
{
filename: 'C:\\path\\to\\core\\domain\\user\\index.ts',
code: "export * from './User';",
errors: [
{
messageId: 'indexFile',
},
],
},
// index.ts at the start of path
{
filename: 'index.ts',
code: "export * from './domain';",
errors: [
{
messageId: 'indexFile',
},
],
},
// index.ts in nested directory
{
filename: '/path/to/core/domain/user/profile/index.ts',
code: "export * from './Profile';",
errors: [
{
messageId: 'indexFile',
},
],
},
],
});

View File

@@ -0,0 +1,160 @@
/**
* Application Query Tests: GetUserRatingLedgerQuery
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GetUserRatingLedgerQueryHandler } from './GetUserRatingLedgerQuery';
import { RatingEventRepository } from '../../domain/repositories/RatingEventRepository';
// Mock repository
const createMockRepository = () => ({
save: vi.fn(),
findByUserId: vi.fn(),
findByIds: vi.fn(),
getAllByUserId: vi.fn(),
findEventsPaginated: vi.fn(),
});
describe('GetUserRatingLedgerQueryHandler', () => {
let handler: GetUserRatingLedgerQueryHandler;
let mockRepository: ReturnType<typeof createMockRepository>;
beforeEach(() => {
mockRepository = createMockRepository();
handler = new GetUserRatingLedgerQueryHandler(mockRepository as unknown as RatingEventRepository);
vi.clearAllMocks();
});
it('should query repository with default pagination', async () => {
mockRepository.findEventsPaginated.mockResolvedValue({
items: [],
total: 0,
limit: 20,
offset: 0,
hasMore: false,
});
await handler.execute({ userId: 'user-1' });
expect(mockRepository.findEventsPaginated).toHaveBeenCalledWith('user-1', {
limit: 20,
offset: 0,
});
});
it('should query repository with custom pagination', async () => {
mockRepository.findEventsPaginated.mockResolvedValue({
items: [],
total: 0,
limit: 50,
offset: 100,
hasMore: false,
});
await handler.execute({
userId: 'user-1',
limit: 50,
offset: 100,
});
expect(mockRepository.findEventsPaginated).toHaveBeenCalledWith('user-1', {
limit: 50,
offset: 100,
});
});
it('should query repository with filters', async () => {
mockRepository.findEventsPaginated.mockResolvedValue({
items: [],
total: 0,
limit: 20,
offset: 0,
hasMore: false,
});
const filter: any = {
dimensions: ['trust'],
sourceTypes: ['vote'],
from: '2026-01-01T00:00:00Z',
to: '2026-01-31T23:59:59Z',
reasonCodes: ['VOTE_POSITIVE'],
};
await handler.execute({
userId: 'user-1',
filter,
});
expect(mockRepository.findEventsPaginated).toHaveBeenCalledWith('user-1', {
limit: 20,
offset: 0,
filter: {
dimensions: ['trust'],
sourceTypes: ['vote'],
from: new Date('2026-01-01T00:00:00Z'),
to: new Date('2026-01-31T23:59:59Z'),
reasonCodes: ['VOTE_POSITIVE'],
},
});
});
it('should map domain entities to DTOs', async () => {
const mockEvent = {
id: { value: 'event-1' },
userId: 'user-1',
dimension: { value: 'trust' },
delta: { value: 5 },
occurredAt: new Date('2026-01-15T12:00:00Z'),
createdAt: new Date('2026-01-15T12:00:00Z'),
source: 'admin_vote',
reason: 'VOTE_POSITIVE',
visibility: 'public',
weight: 1.0,
};
mockRepository.findEventsPaginated.mockResolvedValue({
items: [mockEvent],
total: 1,
limit: 20,
offset: 0,
hasMore: false,
});
const result = await handler.execute({ userId: 'user-1' });
expect(result.entries).toHaveLength(1);
expect(result.entries[0]).toEqual({
id: 'event-1',
userId: 'user-1',
dimension: 'trust',
delta: 5,
occurredAt: '2026-01-15T12:00:00.000Z',
createdAt: '2026-01-15T12:00:00.000Z',
source: 'admin_vote',
reason: 'VOTE_POSITIVE',
visibility: 'public',
weight: 1.0,
});
});
it('should handle pagination metadata in result', async () => {
mockRepository.findEventsPaginated.mockResolvedValue({
items: [],
total: 100,
limit: 20,
offset: 20,
hasMore: true,
nextOffset: 40,
});
const result = await handler.execute({ userId: 'user-1', limit: 20, offset: 20 });
expect(result.pagination).toEqual({
total: 100,
limit: 20,
offset: 20,
hasMore: true,
nextOffset: 40,
});
});
});

View File

@@ -0,0 +1,399 @@
/**
* Application Use Case Tests: CastAdminVoteUseCase
*
* Tests for casting votes in admin vote sessions
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { CastAdminVoteUseCase } from './CastAdminVoteUseCase';
import { AdminVoteSessionRepository } from '../../domain/repositories/AdminVoteSessionRepository';
import { AdminVoteSession } from '../../domain/entities/AdminVoteSession';
// Mock repository
const createMockRepository = () => ({
save: vi.fn(),
findById: vi.fn(),
findActiveForAdmin: vi.fn(),
findByAdminAndLeague: vi.fn(),
findByLeague: vi.fn(),
findClosedUnprocessed: vi.fn(),
});
describe('CastAdminVoteUseCase', () => {
let useCase: CastAdminVoteUseCase;
let mockRepository: ReturnType<typeof createMockRepository>;
beforeEach(() => {
mockRepository = createMockRepository();
useCase = new CastAdminVoteUseCase(mockRepository);
});
describe('Input validation', () => {
it('should reject when voteSessionId is missing', async () => {
const result = await useCase.execute({
voteSessionId: '',
voterId: 'voter-123',
positive: true,
});
expect(result.success).toBe(false);
expect(result.errors).toContain('voteSessionId is required');
});
it('should reject when voterId is missing', async () => {
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: '',
positive: true,
});
expect(result.success).toBe(false);
expect(result.errors).toContain('voterId is required');
});
it('should reject when positive is not a boolean', async () => {
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: 'true' as any,
});
expect(result.success).toBe(false);
expect(result.errors).toContain('positive must be a boolean value');
});
it('should reject when votedAt is not a valid date', async () => {
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
votedAt: 'invalid-date',
});
expect(result.success).toBe(false);
expect(result.errors).toContain('votedAt must be a valid date if provided');
});
it('should accept valid input with all fields', async () => {
mockRepository.findById.mockResolvedValue({
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
});
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
votedAt: '2024-01-01T00:00:00Z',
});
expect(result.success).toBe(true);
expect(result.errors).toBeUndefined();
});
it('should accept valid input without optional votedAt', async () => {
mockRepository.findById.mockResolvedValue({
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
});
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.success).toBe(true);
expect(result.errors).toBeUndefined();
});
});
describe('Session lookup', () => {
it('should reject when vote session is not found', async () => {
mockRepository.findById.mockResolvedValue(null);
const result = await useCase.execute({
voteSessionId: 'non-existent-session',
voterId: 'voter-123',
positive: true,
});
expect(result.success).toBe(false);
expect(result.errors).toContain('Vote session not found');
});
it('should find session by ID when provided', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(mockRepository.findById).toHaveBeenCalledWith('session-123');
});
});
describe('Voting window validation', () => {
it('should reject when voting window is not open', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(false),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.success).toBe(false);
expect(result.errors).toContain('Vote session is not open for voting');
expect(mockSession.isVotingWindowOpen).toHaveBeenCalled();
});
it('should accept when voting window is open', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.success).toBe(true);
expect(mockSession.isVotingWindowOpen).toHaveBeenCalled();
});
it('should use current time when votedAt is not provided', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(mockSession.isVotingWindowOpen).toHaveBeenCalledWith(expect.any(Date));
});
it('should use provided votedAt when available', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
const votedAt = new Date('2024-01-01T12:00:00Z');
await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
votedAt: votedAt.toISOString(),
});
expect(mockSession.isVotingWindowOpen).toHaveBeenCalledWith(votedAt);
});
});
describe('Vote casting', () => {
it('should cast positive vote when session is open', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(mockSession.castVote).toHaveBeenCalledWith('voter-123', true, expect.any(Date));
});
it('should cast negative vote when session is open', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: false,
});
expect(mockSession.castVote).toHaveBeenCalledWith('voter-123', false, expect.any(Date));
});
it('should save updated session after casting vote', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(mockRepository.save).toHaveBeenCalledWith(mockSession);
});
it('should return success when vote is cast', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.success).toBe(true);
expect(result.voteSessionId).toBe('session-123');
expect(result.voterId).toBe('voter-123');
expect(result.errors).toBeUndefined();
});
});
describe('Error handling', () => {
it('should handle repository errors gracefully', async () => {
mockRepository.findById.mockRejectedValue(new Error('Database error'));
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.success).toBe(false);
expect(result.errors).toContain('Failed to cast vote: Database error');
});
it('should handle unexpected errors gracefully', async () => {
mockRepository.findById.mockRejectedValue('Unknown error');
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.success).toBe(false);
expect(result.errors).toContain('Failed to cast vote: Unknown error');
});
it('should handle save errors gracefully', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
mockRepository.save.mockRejectedValue(new Error('Save failed'));
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.success).toBe(false);
expect(result.errors).toContain('Failed to cast vote: Save failed');
});
});
describe('Return values', () => {
it('should return voteSessionId in success response', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.voteSessionId).toBe('session-123');
});
it('should return voterId in success response', async () => {
const mockSession = {
id: 'session-123',
isVotingWindowOpen: vi.fn().mockReturnValue(true),
castVote: vi.fn(),
};
mockRepository.findById.mockResolvedValue(mockSession);
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.voterId).toBe('voter-123');
});
it('should return voteSessionId in error response', async () => {
mockRepository.findById.mockResolvedValue(null);
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.voteSessionId).toBe('session-123');
});
it('should return voterId in error response', async () => {
mockRepository.findById.mockResolvedValue(null);
const result = await useCase.execute({
voteSessionId: 'session-123',
voterId: 'voter-123',
positive: true,
});
expect(result.voterId).toBe('voter-123');
});
});
});

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,251 @@
/**
* Application Use Case Tests: OpenAdminVoteSessionUseCase
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { OpenAdminVoteSessionUseCase } from './OpenAdminVoteSessionUseCase';
import { AdminVoteSessionRepository } from '../../domain/repositories/AdminVoteSessionRepository';
import { AdminVoteSession } from '../../domain/entities/AdminVoteSession';
// Mock repository
const createMockRepository = () => ({
save: vi.fn(),
findById: vi.fn(),
findActiveForAdmin: vi.fn(),
findByAdminAndLeague: vi.fn(),
findByLeague: vi.fn(),
findClosedUnprocessed: vi.fn(),
});
describe('OpenAdminVoteSessionUseCase', () => {
let useCase: OpenAdminVoteSessionUseCase;
let mockRepository: ReturnType<typeof createMockRepository>;
beforeEach(() => {
mockRepository = createMockRepository();
useCase = new OpenAdminVoteSessionUseCase(mockRepository as unknown as AdminVoteSessionRepository);
vi.clearAllMocks();
});
describe('Input validation', () => {
it('should reject when voteSessionId is missing', async () => {
const result = await useCase.execute({
voteSessionId: '',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '2026-01-01',
endDate: '2026-01-07',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('voteSessionId is required');
});
it('should reject when leagueId is missing', async () => {
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: '',
adminId: 'admin-1',
startDate: '2026-01-01',
endDate: '2026-01-07',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('leagueId is required');
});
it('should reject when adminId is missing', async () => {
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: '',
startDate: '2026-01-01',
endDate: '2026-01-07',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('adminId is required');
});
it('should reject when startDate is missing', async () => {
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '',
endDate: '2026-01-07',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('startDate is required');
});
it('should reject when endDate is missing', async () => {
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '2026-01-01',
endDate: '',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('endDate is required');
});
it('should reject when startDate is invalid', async () => {
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: 'invalid-date',
endDate: '2026-01-07',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('startDate must be a valid date');
});
it('should reject when endDate is invalid', async () => {
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '2026-01-01',
endDate: 'invalid-date',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('endDate must be a valid date');
});
it('should reject when startDate is after endDate', async () => {
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '2026-01-07',
endDate: '2026-01-01',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('startDate must be before endDate');
});
it('should reject when eligibleVoters is empty', async () => {
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '2026-01-01',
endDate: '2026-01-07',
eligibleVoters: [],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('At least one eligible voter is required');
});
it('should reject when eligibleVoters has duplicates', async () => {
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '2026-01-01',
endDate: '2026-01-07',
eligibleVoters: ['voter-1', 'voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('Duplicate eligible voters are not allowed');
});
});
describe('Business rules', () => {
it('should reject when session ID already exists', async () => {
mockRepository.findById.mockResolvedValue({ id: 'session-1' } as any);
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '2026-01-01',
endDate: '2026-01-07',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('Vote session with this ID already exists');
});
it('should reject when there is an overlapping active session', async () => {
mockRepository.findById.mockResolvedValue(null);
mockRepository.findActiveForAdmin.mockResolvedValue([
{
startDate: new Date('2026-01-05'),
endDate: new Date('2026-01-10'),
}
] as any);
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '2026-01-01',
endDate: '2026-01-07',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors).toContain('Active vote session already exists for this admin in this league with overlapping dates');
});
it('should create and save a new session when valid', async () => {
mockRepository.findById.mockResolvedValue(null);
mockRepository.findActiveForAdmin.mockResolvedValue([]);
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '2026-01-01',
endDate: '2026-01-07',
eligibleVoters: ['voter-1', 'voter-2'],
});
expect(result.success).toBe(true);
expect(mockRepository.save).toHaveBeenCalled();
const savedSession = mockRepository.save.mock.calls[0][0];
expect(savedSession).toBeInstanceOf(AdminVoteSession);
expect(savedSession.id).toBe('session-1');
expect(savedSession.leagueId).toBe('league-1');
expect(savedSession.adminId).toBe('admin-1');
});
});
describe('Error handling', () => {
it('should handle repository errors gracefully', async () => {
mockRepository.findById.mockRejectedValue(new Error('Database error'));
const result = await useCase.execute({
voteSessionId: 'session-1',
leagueId: 'league-1',
adminId: 'admin-1',
startDate: '2026-01-01',
endDate: '2026-01-07',
eligibleVoters: ['voter-1'],
});
expect(result.success).toBe(false);
expect(result.errors?.[0]).toContain('Failed to open vote session: Database error');
});
});
});

View File

@@ -0,0 +1,241 @@
/**
* Domain Entity Tests: Company
*
* Tests for Company entity business rules and invariants
*/
import { describe, it, expect } from 'vitest';
import { Company } from './Company';
import { UserId } from '../value-objects/UserId';
describe('Company', () => {
describe('Creation', () => {
it('should create a company with valid properties', () => {
const userId = UserId.fromString('user-123');
const company = Company.create({
name: 'Acme Racing Team',
ownerUserId: userId,
contactEmail: 'contact@acme.com',
});
expect(company.getName()).toBe('Acme Racing Team');
expect(company.getOwnerUserId()).toEqual(userId);
expect(company.getContactEmail()).toBe('contact@acme.com');
expect(company.getId()).toBeDefined();
expect(company.getCreatedAt()).toBeInstanceOf(Date);
});
it('should create a company without optional contact email', () => {
const userId = UserId.fromString('user-123');
const company = Company.create({
name: 'Acme Racing Team',
ownerUserId: userId,
});
expect(company.getContactEmail()).toBeUndefined();
});
it('should generate unique IDs for different companies', () => {
const userId = UserId.fromString('user-123');
const company1 = Company.create({
name: 'Team A',
ownerUserId: userId,
});
const company2 = Company.create({
name: 'Team B',
ownerUserId: userId,
});
expect(company1.getId()).not.toBe(company2.getId());
});
});
describe('Rehydration', () => {
it('should rehydrate company from stored data', () => {
const userId = UserId.fromString('user-123');
const createdAt = new Date('2024-01-01');
const company = Company.rehydrate({
id: 'comp-123',
name: 'Acme Racing Team',
ownerUserId: 'user-123',
contactEmail: 'contact@acme.com',
createdAt,
});
expect(company.getId()).toBe('comp-123');
expect(company.getName()).toBe('Acme Racing Team');
expect(company.getOwnerUserId()).toEqual(userId);
expect(company.getContactEmail()).toBe('contact@acme.com');
expect(company.getCreatedAt()).toEqual(createdAt);
});
it('should rehydrate company without contact email', () => {
const createdAt = new Date('2024-01-01');
const company = Company.rehydrate({
id: 'comp-123',
name: 'Acme Racing Team',
ownerUserId: 'user-123',
createdAt,
});
expect(company.getContactEmail()).toBeUndefined();
});
});
describe('Validation', () => {
it('should throw error when company name is empty', () => {
const userId = UserId.fromString('user-123');
expect(() => {
Company.create({
name: '',
ownerUserId: userId,
});
}).toThrow('Company name cannot be empty');
});
it('should throw error when company name is only whitespace', () => {
const userId = UserId.fromString('user-123');
expect(() => {
Company.create({
name: ' ',
ownerUserId: userId,
});
}).toThrow('Company name cannot be empty');
});
it('should throw error when company name is too short', () => {
const userId = UserId.fromString('user-123');
expect(() => {
Company.create({
name: 'A',
ownerUserId: userId,
});
}).toThrow('Company name must be at least 2 characters long');
});
it('should throw error when company name is too long', () => {
const userId = UserId.fromString('user-123');
const longName = 'A'.repeat(101);
expect(() => {
Company.create({
name: longName,
ownerUserId: userId,
});
}).toThrow('Company name must be no more than 100 characters');
});
it('should accept company name with exactly 2 characters', () => {
const userId = UserId.fromString('user-123');
const company = Company.create({
name: 'AB',
ownerUserId: userId,
});
expect(company.getName()).toBe('AB');
});
it('should accept company name with exactly 100 characters', () => {
const userId = UserId.fromString('user-123');
const longName = 'A'.repeat(100);
const company = Company.create({
name: longName,
ownerUserId: userId,
});
expect(company.getName()).toBe(longName);
});
it('should trim whitespace from company name during validation', () => {
const userId = UserId.fromString('user-123');
const company = Company.create({
name: ' Acme Racing Team ',
ownerUserId: userId,
});
// Note: The current implementation doesn't trim, it just validates
// So this test documents the current behavior
expect(company.getName()).toBe(' Acme Racing Team ');
});
});
describe('Business Rules', () => {
it('should maintain immutability of properties', () => {
const userId = UserId.fromString('user-123');
const company = Company.create({
name: 'Acme Racing Team',
ownerUserId: userId,
contactEmail: 'contact@acme.com',
});
const originalName = company.getName();
const originalEmail = company.getContactEmail();
// Try to modify (should not work due to readonly properties)
// This is more of a TypeScript compile-time check, but we can verify runtime behavior
expect(company.getName()).toBe(originalName);
expect(company.getContactEmail()).toBe(originalEmail);
});
it('should handle special characters in company name', () => {
const userId = UserId.fromString('user-123');
const company = Company.create({
name: 'Acme & Sons Racing, LLC',
ownerUserId: userId,
});
expect(company.getName()).toBe('Acme & Sons Racing, LLC');
});
it('should handle unicode characters in company name', () => {
const userId = UserId.fromString('user-123');
const company = Company.create({
name: 'Räcing Tëam Ñumber Øne',
ownerUserId: userId,
});
expect(company.getName()).toBe('Räcing Tëam Ñumber Øne');
});
});
describe('Edge Cases', () => {
it('should handle rehydration with null contact email', () => {
const createdAt = new Date('2024-01-01');
const company = Company.rehydrate({
id: 'comp-123',
name: 'Acme Racing Team',
ownerUserId: 'user-123',
contactEmail: null as any,
createdAt,
});
// The entity stores null as null, not undefined
expect(company.getContactEmail()).toBeNull();
});
it('should handle rehydration with undefined contact email', () => {
const createdAt = new Date('2024-01-01');
const company = Company.rehydrate({
id: 'comp-123',
name: 'Acme Racing Team',
ownerUserId: 'user-123',
contactEmail: undefined,
createdAt,
});
expect(company.getContactEmail()).toBeUndefined();
});
});
});

View File

@@ -0,0 +1,221 @@
/**
* Domain Error Tests: IdentityDomainError
*
* Tests for domain error classes and their behavior
*/
import { describe, it, expect } from 'vitest';
import { IdentityDomainError, IdentityDomainValidationError, IdentityDomainInvariantError } from './IdentityDomainError';
describe('IdentityDomainError', () => {
describe('IdentityDomainError (base class)', () => {
it('should create an error with correct properties', () => {
const error = new IdentityDomainValidationError('Test error message');
expect(error.message).toBe('Test error message');
expect(error.type).toBe('domain');
expect(error.context).toBe('identity-domain');
expect(error.kind).toBe('validation');
});
it('should be an instance of Error', () => {
const error = new IdentityDomainValidationError('Test error');
expect(error instanceof Error).toBe(true);
});
it('should be an instance of IdentityDomainError', () => {
const error = new IdentityDomainValidationError('Test error');
expect(error instanceof IdentityDomainError).toBe(true);
});
it('should have correct stack trace', () => {
const error = new IdentityDomainValidationError('Test error');
expect(error.stack).toBeDefined();
expect(error.stack).toContain('IdentityDomainError');
});
it('should handle empty error message', () => {
const error = new IdentityDomainValidationError('');
expect(error.message).toBe('');
});
it('should handle error message with special characters', () => {
const error = new IdentityDomainValidationError('Error: Invalid input @#$%^&*()');
expect(error.message).toBe('Error: Invalid input @#$%^&*()');
});
it('should handle error message with newlines', () => {
const error = new IdentityDomainValidationError('Error line 1\nError line 2');
expect(error.message).toBe('Error line 1\nError line 2');
});
});
describe('IdentityDomainValidationError', () => {
it('should create a validation error with correct kind', () => {
const error = new IdentityDomainValidationError('Invalid email format');
expect(error.kind).toBe('validation');
expect(error.type).toBe('domain');
expect(error.context).toBe('identity-domain');
});
it('should be an instance of IdentityDomainValidationError', () => {
const error = new IdentityDomainValidationError('Invalid email format');
expect(error instanceof IdentityDomainValidationError).toBe(true);
});
it('should be an instance of IdentityDomainError', () => {
const error = new IdentityDomainValidationError('Invalid email format');
expect(error instanceof IdentityDomainError).toBe(true);
});
it('should handle validation error with empty message', () => {
const error = new IdentityDomainValidationError('');
expect(error.kind).toBe('validation');
expect(error.message).toBe('');
});
it('should handle validation error with complex message', () => {
const error = new IdentityDomainValidationError(
'Validation failed: Email must be at least 6 characters long and contain a valid domain'
);
expect(error.kind).toBe('validation');
expect(error.message).toBe(
'Validation failed: Email must be at least 6 characters long and contain a valid domain'
);
});
});
describe('IdentityDomainInvariantError', () => {
it('should create an invariant error with correct kind', () => {
const error = new IdentityDomainInvariantError('User must have a valid email');
expect(error.kind).toBe('invariant');
expect(error.type).toBe('domain');
expect(error.context).toBe('identity-domain');
});
it('should be an instance of IdentityDomainInvariantError', () => {
const error = new IdentityDomainInvariantError('User must have a valid email');
expect(error instanceof IdentityDomainInvariantError).toBe(true);
});
it('should be an instance of IdentityDomainError', () => {
const error = new IdentityDomainInvariantError('User must have a valid email');
expect(error instanceof IdentityDomainError).toBe(true);
});
it('should handle invariant error with empty message', () => {
const error = new IdentityDomainInvariantError('');
expect(error.kind).toBe('invariant');
expect(error.message).toBe('');
});
it('should handle invariant error with complex message', () => {
const error = new IdentityDomainInvariantError(
'Invariant violation: User rating must be between 0 and 100'
);
expect(error.kind).toBe('invariant');
expect(error.message).toBe(
'Invariant violation: User rating must be between 0 and 100'
);
});
});
describe('Error hierarchy', () => {
it('should maintain correct error hierarchy for validation errors', () => {
const error = new IdentityDomainValidationError('Test');
expect(error instanceof IdentityDomainValidationError).toBe(true);
expect(error instanceof IdentityDomainError).toBe(true);
expect(error instanceof Error).toBe(true);
});
it('should maintain correct error hierarchy for invariant errors', () => {
const error = new IdentityDomainInvariantError('Test');
expect(error instanceof IdentityDomainInvariantError).toBe(true);
expect(error instanceof IdentityDomainError).toBe(true);
expect(error instanceof Error).toBe(true);
});
it('should allow catching as IdentityDomainError', () => {
const error = new IdentityDomainValidationError('Test');
try {
throw error;
} catch (e) {
expect(e instanceof IdentityDomainError).toBe(true);
expect((e as IdentityDomainError).kind).toBe('validation');
}
});
it('should allow catching as Error', () => {
const error = new IdentityDomainInvariantError('Test');
try {
throw error;
} catch (e) {
expect(e instanceof Error).toBe(true);
expect((e as Error).message).toBe('Test');
}
});
});
describe('Error properties', () => {
it('should have consistent type property', () => {
const validationError = new IdentityDomainValidationError('Test');
const invariantError = new IdentityDomainInvariantError('Test');
expect(validationError.type).toBe('domain');
expect(invariantError.type).toBe('domain');
});
it('should have consistent context property', () => {
const validationError = new IdentityDomainValidationError('Test');
const invariantError = new IdentityDomainInvariantError('Test');
expect(validationError.context).toBe('identity-domain');
expect(invariantError.context).toBe('identity-domain');
});
it('should have different kind properties', () => {
const validationError = new IdentityDomainValidationError('Test');
const invariantError = new IdentityDomainInvariantError('Test');
expect(validationError.kind).toBe('validation');
expect(invariantError.kind).toBe('invariant');
});
});
describe('Error usage patterns', () => {
it('should be usable in try-catch blocks', () => {
expect(() => {
throw new IdentityDomainValidationError('Invalid input');
}).toThrow(IdentityDomainValidationError);
});
it('should be usable with error instanceof checks', () => {
const error = new IdentityDomainValidationError('Test');
expect(error instanceof IdentityDomainValidationError).toBe(true);
expect(error instanceof IdentityDomainError).toBe(true);
expect(error instanceof Error).toBe(true);
});
it('should be usable with error type narrowing', () => {
const error: IdentityDomainError = new IdentityDomainValidationError('Test');
if (error.kind === 'validation') {
expect(error instanceof IdentityDomainValidationError).toBe(true);
}
});
it('should support error message extraction', () => {
const errorMessage = 'User email is required';
const error = new IdentityDomainValidationError(errorMessage);
expect(error.message).toBe(errorMessage);
});
});
});

View File

@@ -0,0 +1,216 @@
/**
* Domain Service Tests: PasswordHashingService
*
* Tests for password hashing and verification business logic
*/
import { describe, it, expect, beforeEach } from 'vitest';
import { PasswordHashingService } from './PasswordHashingService';
describe('PasswordHashingService', () => {
let service: PasswordHashingService;
beforeEach(() => {
service = new PasswordHashingService();
});
describe('hash', () => {
it('should hash a plain text password', async () => {
const plainPassword = 'mySecurePassword123';
const hash = await service.hash(plainPassword);
expect(hash).toBeDefined();
expect(typeof hash).toBe('string');
expect(hash.length).toBeGreaterThan(0);
// Hash should not be the same as the plain password
expect(hash).not.toBe(plainPassword);
});
it('should produce different hashes for the same password (with salt)', async () => {
const plainPassword = 'mySecurePassword123';
const hash1 = await service.hash(plainPassword);
const hash2 = await service.hash(plainPassword);
// Due to salting, hashes should be different
expect(hash1).not.toBe(hash2);
});
it('should handle empty string password', async () => {
const hash = await service.hash('');
expect(hash).toBeDefined();
expect(typeof hash).toBe('string');
});
it('should handle special characters in password', async () => {
const specialPassword = 'P@ssw0rd!#$%^&*()_+-=[]{}|;:,.<>?';
const hash = await service.hash(specialPassword);
expect(hash).toBeDefined();
expect(typeof hash).toBe('string');
});
it('should handle unicode characters in password', async () => {
const unicodePassword = 'Pässwörd!🔒';
const hash = await service.hash(unicodePassword);
expect(hash).toBeDefined();
expect(typeof hash).toBe('string');
});
it('should handle very long passwords', async () => {
const longPassword = 'a'.repeat(1000);
const hash = await service.hash(longPassword);
expect(hash).toBeDefined();
expect(typeof hash).toBe('string');
});
it('should handle whitespace-only password', async () => {
const whitespacePassword = ' ';
const hash = await service.hash(whitespacePassword);
expect(hash).toBeDefined();
expect(typeof hash).toBe('string');
});
});
describe('verify', () => {
it('should verify correct password against hash', async () => {
const plainPassword = 'mySecurePassword123';
const hash = await service.hash(plainPassword);
const isValid = await service.verify(plainPassword, hash);
expect(isValid).toBe(true);
});
it('should reject incorrect password', async () => {
const plainPassword = 'mySecurePassword123';
const hash = await service.hash(plainPassword);
const isValid = await service.verify('wrongPassword', hash);
expect(isValid).toBe(false);
});
it('should reject empty password against hash', async () => {
const plainPassword = 'mySecurePassword123';
const hash = await service.hash(plainPassword);
const isValid = await service.verify('', hash);
expect(isValid).toBe(false);
});
it('should handle verification with special characters', async () => {
const specialPassword = 'P@ssw0rd!#$%^&*()_+-=[]{}|;:,.<>?';
const hash = await service.hash(specialPassword);
const isValid = await service.verify(specialPassword, hash);
expect(isValid).toBe(true);
});
it('should handle verification with unicode characters', async () => {
const unicodePassword = 'Pässwörd!🔒';
const hash = await service.hash(unicodePassword);
const isValid = await service.verify(unicodePassword, hash);
expect(isValid).toBe(true);
});
it('should handle verification with very long passwords', async () => {
const longPassword = 'a'.repeat(1000);
const hash = await service.hash(longPassword);
const isValid = await service.verify(longPassword, hash);
expect(isValid).toBe(true);
});
it('should handle verification with whitespace-only password', async () => {
const whitespacePassword = ' ';
const hash = await service.hash(whitespacePassword);
const isValid = await service.verify(whitespacePassword, hash);
expect(isValid).toBe(true);
});
it('should reject verification with null hash', async () => {
// bcrypt throws an error when hash is null, which is expected behavior
await expect(service.verify('password', null as any)).rejects.toThrow();
});
it('should reject verification with empty hash', async () => {
const isValid = await service.verify('password', '');
expect(isValid).toBe(false);
});
it('should reject verification with invalid hash format', async () => {
const isValid = await service.verify('password', 'invalid-hash-format');
expect(isValid).toBe(false);
});
});
describe('Hash Consistency', () => {
it('should consistently verify the same password-hash pair', async () => {
const plainPassword = 'testPassword123';
const hash = await service.hash(plainPassword);
// Verify multiple times
const result1 = await service.verify(plainPassword, hash);
const result2 = await service.verify(plainPassword, hash);
const result3 = await service.verify(plainPassword, hash);
expect(result1).toBe(true);
expect(result2).toBe(true);
expect(result3).toBe(true);
}, 10000);
it('should consistently reject wrong password', async () => {
const plainPassword = 'testPassword123';
const wrongPassword = 'wrongPassword';
const hash = await service.hash(plainPassword);
// Verify multiple times with wrong password
const result1 = await service.verify(wrongPassword, hash);
const result2 = await service.verify(wrongPassword, hash);
const result3 = await service.verify(wrongPassword, hash);
expect(result1).toBe(false);
expect(result2).toBe(false);
expect(result3).toBe(false);
}, 10000);
});
describe('Security Properties', () => {
it('should not leak information about the original password from hash', async () => {
const password1 = 'password123';
const password2 = 'password456';
const hash1 = await service.hash(password1);
const hash2 = await service.hash(password2);
// Hashes should be different
expect(hash1).not.toBe(hash2);
// Neither hash should contain the original password
expect(hash1).not.toContain(password1);
expect(hash2).not.toContain(password2);
});
it('should handle case sensitivity correctly', async () => {
const password1 = 'Password';
const password2 = 'password';
const hash1 = await service.hash(password1);
const hash2 = await service.hash(password2);
// Should be treated as different passwords
const isValid1 = await service.verify(password1, hash1);
const isValid2 = await service.verify(password2, hash2);
const isCrossValid1 = await service.verify(password1, hash2);
const isCrossValid2 = await service.verify(password2, hash1);
expect(isValid1).toBe(true);
expect(isValid2).toBe(true);
expect(isCrossValid1).toBe(false);
expect(isCrossValid2).toBe(false);
}, 10000);
});
});

View File

@@ -0,0 +1,338 @@
/**
* Domain Types Tests: EmailAddress
*
* Tests for email validation and disposable email detection
*/
import { describe, it, expect } from 'vitest';
import { validateEmail, isDisposableEmail, DISPOSABLE_DOMAINS } from './EmailAddress';
describe('EmailAddress', () => {
describe('validateEmail', () => {
describe('Valid emails', () => {
it('should validate standard email format', () => {
const result = validateEmail('user@example.com');
expect(result.success).toBe(true);
if (result.success) {
expect(result.email).toBe('user@example.com');
}
});
it('should validate email with subdomain', () => {
const result = validateEmail('user@mail.example.com');
expect(result.success).toBe(true);
if (result.success) {
expect(result.email).toBe('user@mail.example.com');
}
});
it('should validate email with plus sign', () => {
const result = validateEmail('user+tag@example.com');
expect(result.success).toBe(true);
if (result.success) {
expect(result.email).toBe('user+tag@example.com');
}
});
it('should validate email with numbers', () => {
const result = validateEmail('user123@example.com');
expect(result.success).toBe(true);
if (result.success) {
expect(result.email).toBe('user123@example.com');
}
});
it('should validate email with hyphens', () => {
const result = validateEmail('user-name@example.com');
expect(result.success).toBe(true);
if (result.success) {
expect(result.email).toBe('user-name@example.com');
}
});
it('should validate email with underscores', () => {
const result = validateEmail('user_name@example.com');
expect(result.success).toBe(true);
if (result.success) {
expect(result.email).toBe('user_name@example.com');
}
});
it('should validate email with dots in local part', () => {
const result = validateEmail('user.name@example.com');
expect(result.success).toBe(true);
if (result.success) {
expect(result.email).toBe('user.name@example.com');
}
});
it('should validate email with uppercase letters', () => {
const result = validateEmail('User@Example.com');
expect(result.success).toBe(true);
if (result.success) {
// Should be normalized to lowercase
expect(result.email).toBe('user@example.com');
}
});
it('should validate email with leading/trailing whitespace', () => {
const result = validateEmail(' user@example.com ');
expect(result.success).toBe(true);
if (result.success) {
// Should be trimmed
expect(result.email).toBe('user@example.com');
}
});
it('should validate minimum length email (6 chars)', () => {
const result = validateEmail('a@b.cd');
expect(result.success).toBe(true);
if (result.success) {
expect(result.email).toBe('a@b.cd');
}
});
it('should validate maximum length email (254 chars)', () => {
const localPart = 'a'.repeat(64);
const domain = 'example.com';
const email = `${localPart}@${domain}`;
const result = validateEmail(email);
expect(result.success).toBe(true);
if (result.success) {
expect(result.email).toBe(email);
}
});
});
describe('Invalid emails', () => {
it('should reject empty string', () => {
const result = validateEmail('');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should reject whitespace-only string', () => {
const result = validateEmail(' ');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should reject email without @ symbol', () => {
const result = validateEmail('userexample.com');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should reject email without domain', () => {
const result = validateEmail('user@');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should reject email without local part', () => {
const result = validateEmail('@example.com');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should reject email with multiple @ symbols', () => {
const result = validateEmail('user@domain@com');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should reject email with spaces in local part', () => {
const result = validateEmail('user name@example.com');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should reject email with spaces in domain', () => {
const result = validateEmail('user@ex ample.com');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should reject email with invalid characters', () => {
const result = validateEmail('user#name@example.com');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should reject email that is too short', () => {
const result = validateEmail('a@b.c');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should accept email that is exactly 254 characters', () => {
// The maximum email length is 254 characters
const localPart = 'a'.repeat(64);
const domain = 'example.com';
const email = `${localPart}@${domain}`;
const result = validateEmail(email);
expect(result.success).toBe(true);
if (result.success) {
expect(result.email).toBe(email);
}
});
it('should reject email without TLD', () => {
const result = validateEmail('user@example');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
it('should reject email with invalid TLD format', () => {
const result = validateEmail('user@example.');
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error).toBeDefined();
}
});
});
describe('Edge cases', () => {
it('should handle null input gracefully', () => {
const result = validateEmail(null as any);
expect(result.success).toBe(false);
});
it('should handle undefined input gracefully', () => {
const result = validateEmail(undefined as any);
expect(result.success).toBe(false);
});
it('should handle non-string input gracefully', () => {
const result = validateEmail(123 as any);
expect(result.success).toBe(false);
});
});
});
describe('isDisposableEmail', () => {
describe('Disposable email domains', () => {
it('should detect tempmail.com as disposable', () => {
expect(isDisposableEmail('user@tempmail.com')).toBe(true);
});
it('should detect throwaway.email as disposable', () => {
expect(isDisposableEmail('user@throwaway.email')).toBe(true);
});
it('should detect guerrillamail.com as disposable', () => {
expect(isDisposableEmail('user@guerrillamail.com')).toBe(true);
});
it('should detect mailinator.com as disposable', () => {
expect(isDisposableEmail('user@mailinator.com')).toBe(true);
});
it('should detect 10minutemail.com as disposable', () => {
expect(isDisposableEmail('user@10minutemail.com')).toBe(true);
});
it('should detect disposable domains case-insensitively', () => {
expect(isDisposableEmail('user@TEMPMAIL.COM')).toBe(true);
expect(isDisposableEmail('user@TempMail.Com')).toBe(true);
});
it('should detect disposable domains with subdomains', () => {
// The current implementation only checks the exact domain, not subdomains
// So this test documents the current behavior
expect(isDisposableEmail('user@subdomain.tempmail.com')).toBe(false);
});
});
describe('Non-disposable email domains', () => {
it('should not detect gmail.com as disposable', () => {
expect(isDisposableEmail('user@gmail.com')).toBe(false);
});
it('should not detect yahoo.com as disposable', () => {
expect(isDisposableEmail('user@yahoo.com')).toBe(false);
});
it('should not detect outlook.com as disposable', () => {
expect(isDisposableEmail('user@outlook.com')).toBe(false);
});
it('should not detect company domains as disposable', () => {
expect(isDisposableEmail('user@example.com')).toBe(false);
expect(isDisposableEmail('user@company.com')).toBe(false);
});
it('should not detect custom domains as disposable', () => {
expect(isDisposableEmail('user@mydomain.com')).toBe(false);
});
});
describe('Edge cases', () => {
it('should handle email without domain', () => {
expect(isDisposableEmail('user@')).toBe(false);
});
it('should handle email without @ symbol', () => {
expect(isDisposableEmail('user')).toBe(false);
});
it('should handle empty string', () => {
expect(isDisposableEmail('')).toBe(false);
});
it('should handle null input', () => {
// The current implementation throws an error when given null
// This is expected behavior - the function expects a string
expect(() => isDisposableEmail(null as any)).toThrow();
});
it('should handle undefined input', () => {
// The current implementation throws an error when given undefined
// This is expected behavior - the function expects a string
expect(() => isDisposableEmail(undefined as any)).toThrow();
});
});
});
describe('DISPOSABLE_DOMAINS', () => {
it('should contain expected disposable domains', () => {
expect(DISPOSABLE_DOMAINS.has('tempmail.com')).toBe(true);
expect(DISPOSABLE_DOMAINS.has('throwaway.email')).toBe(true);
expect(DISPOSABLE_DOMAINS.has('guerrillamail.com')).toBe(true);
expect(DISPOSABLE_DOMAINS.has('mailinator.com')).toBe(true);
expect(DISPOSABLE_DOMAINS.has('10minutemail.com')).toBe(true);
});
it('should not contain non-disposable domains', () => {
expect(DISPOSABLE_DOMAINS.has('gmail.com')).toBe(false);
expect(DISPOSABLE_DOMAINS.has('yahoo.com')).toBe(false);
expect(DISPOSABLE_DOMAINS.has('outlook.com')).toBe(false);
});
it('should be a Set', () => {
expect(DISPOSABLE_DOMAINS instanceof Set).toBe(true);
});
});
});

View File

@@ -0,0 +1,128 @@
import { Result } from '@core/shared/domain/Result';
import { describe, expect, it, vi, type Mock } from 'vitest';
import type { MediaStoragePort } from '../ports/MediaStoragePort';
import { GetUploadedMediaUseCase } from './GetUploadedMediaUseCase';
describe('GetUploadedMediaUseCase', () => {
let mediaStorage: {
getBytes: Mock;
getMetadata: Mock;
};
let useCase: GetUploadedMediaUseCase;
beforeEach(() => {
mediaStorage = {
getBytes: vi.fn(),
getMetadata: vi.fn(),
};
useCase = new GetUploadedMediaUseCase(
mediaStorage as unknown as MediaStoragePort,
);
});
it('returns null when media is not found', async () => {
mediaStorage.getBytes.mockResolvedValue(null);
const input = { storageKey: 'missing-key' };
const result = await useCase.execute(input);
expect(mediaStorage.getBytes).toHaveBeenCalledWith('missing-key');
expect(result).toBeInstanceOf(Result);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBe(null);
});
it('returns media bytes and content type when found', async () => {
const mockBytes = Buffer.from('test data');
const mockMetadata = { size: 9, contentType: 'image/png' };
mediaStorage.getBytes.mockResolvedValue(mockBytes);
mediaStorage.getMetadata.mockResolvedValue(mockMetadata);
const input = { storageKey: 'media-key' };
const result = await useCase.execute(input);
expect(mediaStorage.getBytes).toHaveBeenCalledWith('media-key');
expect(mediaStorage.getMetadata).toHaveBeenCalledWith('media-key');
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult).not.toBeNull();
expect(successResult!.bytes).toBeInstanceOf(Buffer);
expect(successResult!.bytes.toString()).toBe('test data');
expect(successResult!.contentType).toBe('image/png');
});
it('returns default content type when metadata is null', async () => {
const mockBytes = Buffer.from('test data');
mediaStorage.getBytes.mockResolvedValue(mockBytes);
mediaStorage.getMetadata.mockResolvedValue(null);
const input = { storageKey: 'media-key' };
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult!.contentType).toBe('application/octet-stream');
});
it('returns default content type when metadata has no contentType', async () => {
const mockBytes = Buffer.from('test data');
const mockMetadata = { size: 9 };
mediaStorage.getBytes.mockResolvedValue(mockBytes);
mediaStorage.getMetadata.mockResolvedValue(mockMetadata as any);
const input = { storageKey: 'media-key' };
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult!.contentType).toBe('application/octet-stream');
});
it('handles storage errors by returning error', async () => {
mediaStorage.getBytes.mockRejectedValue(new Error('Storage error'));
const input = { storageKey: 'media-key' };
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.message).toBe('Storage error');
});
it('handles getMetadata errors by returning error', async () => {
const mockBytes = Buffer.from('test data');
mediaStorage.getBytes.mockResolvedValue(mockBytes);
mediaStorage.getMetadata.mockRejectedValue(new Error('Metadata error'));
const input = { storageKey: 'media-key' };
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.message).toBe('Metadata error');
});
it('returns bytes as Buffer', async () => {
const mockBytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]); // "Hello"
mediaStorage.getBytes.mockResolvedValue(mockBytes);
mediaStorage.getMetadata.mockResolvedValue({ size: 5, contentType: 'text/plain' });
const input = { storageKey: 'media-key' };
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult!.bytes).toBeInstanceOf(Buffer);
expect(successResult!.bytes.toString()).toBe('Hello');
});
});

View File

@@ -0,0 +1,103 @@
import { Result } from '@core/shared/domain/Result';
import { describe, expect, it, vi, type Mock } from 'vitest';
import type { MediaResolverPort } from '@core/ports/media/MediaResolverPort';
import { ResolveMediaReferenceUseCase } from './ResolveMediaReferenceUseCase';
describe('ResolveMediaReferenceUseCase', () => {
let mediaResolver: {
resolve: Mock;
};
let useCase: ResolveMediaReferenceUseCase;
beforeEach(() => {
mediaResolver = {
resolve: vi.fn(),
};
useCase = new ResolveMediaReferenceUseCase(
mediaResolver as unknown as MediaResolverPort,
);
});
it('returns resolved path when media reference is resolved', async () => {
mediaResolver.resolve.mockResolvedValue('/resolved/path/to/media.png');
const input = { reference: { type: 'team', id: 'team-123' } };
const result = await useCase.execute(input);
expect(mediaResolver.resolve).toHaveBeenCalledWith({ type: 'team', id: 'team-123' });
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult).toBe('/resolved/path/to/media.png');
});
it('returns null when media reference resolves to null', async () => {
mediaResolver.resolve.mockResolvedValue(null);
const input = { reference: { type: 'team', id: 'team-123' } };
const result = await useCase.execute(input);
expect(mediaResolver.resolve).toHaveBeenCalledWith({ type: 'team', id: 'team-123' });
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult).toBe(null);
});
it('returns empty string when media reference resolves to empty string', async () => {
mediaResolver.resolve.mockResolvedValue('');
const input = { reference: { type: 'team', id: 'team-123' } };
const result = await useCase.execute(input);
expect(mediaResolver.resolve).toHaveBeenCalledWith({ type: 'team', id: 'team-123' });
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult).toBe('');
});
it('handles resolver errors by returning error', async () => {
mediaResolver.resolve.mockRejectedValue(new Error('Resolver error'));
const input = { reference: { type: 'team', id: 'team-123' } };
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.message).toBe('Resolver error');
});
it('handles non-Error exceptions by wrapping in Error', async () => {
mediaResolver.resolve.mockRejectedValue('string error');
const input = { reference: { type: 'team', id: 'team-123' } };
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr();
expect(err.message).toBe('string error');
});
it('resolves different reference types', async () => {
const testCases = [
{ type: 'team', id: 'team-123' },
{ type: 'league', id: 'league-456' },
{ type: 'driver', id: 'driver-789' },
];
for (const reference of testCases) {
mediaResolver.resolve.mockResolvedValue(`/resolved/${reference.type}/${reference.id}.png`);
const input = { reference };
const result = await useCase.execute(input);
expect(mediaResolver.resolve).toHaveBeenCalledWith(reference);
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult).toBe(`/resolved/${reference.type}/${reference.id}.png`);
}
});
});

View File

@@ -1,7 +1,182 @@
import * as mod from '@core/media/domain/entities/Avatar';
import { Avatar } from './Avatar';
import { MediaUrl } from '../value-objects/MediaUrl';
describe('media/domain/entities/Avatar.ts', () => {
it('imports', () => {
expect(mod).toBeTruthy();
describe('Avatar', () => {
describe('create', () => {
it('creates a new avatar with required properties', () => {
const avatar = Avatar.create({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'https://example.com/avatar.png',
});
expect(avatar.id).toBe('avatar-1');
expect(avatar.driverId).toBe('driver-1');
expect(avatar.mediaUrl).toBeInstanceOf(MediaUrl);
expect(avatar.mediaUrl.value).toBe('https://example.com/avatar.png');
expect(avatar.isActive).toBe(true);
expect(avatar.selectedAt).toBeInstanceOf(Date);
});
it('throws error when driverId is missing', () => {
expect(() =>
Avatar.create({
id: 'avatar-1',
driverId: '',
mediaUrl: 'https://example.com/avatar.png',
})
).toThrow('Driver ID is required');
});
it('throws error when mediaUrl is missing', () => {
expect(() =>
Avatar.create({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: '',
})
).toThrow('Media URL is required');
});
it('throws error when mediaUrl is invalid', () => {
expect(() =>
Avatar.create({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'invalid-url',
})
).toThrow();
});
});
describe('reconstitute', () => {
it('reconstitutes an avatar from props', () => {
const selectedAt = new Date('2024-01-01T00:00:00.000Z');
const avatar = Avatar.reconstitute({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'https://example.com/avatar.png',
selectedAt,
isActive: true,
});
expect(avatar.id).toBe('avatar-1');
expect(avatar.driverId).toBe('driver-1');
expect(avatar.mediaUrl.value).toBe('https://example.com/avatar.png');
expect(avatar.selectedAt).toEqual(selectedAt);
expect(avatar.isActive).toBe(true);
});
it('reconstitutes an inactive avatar', () => {
const avatar = Avatar.reconstitute({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'https://example.com/avatar.png',
selectedAt: new Date(),
isActive: false,
});
expect(avatar.isActive).toBe(false);
});
});
describe('deactivate', () => {
it('deactivates an active avatar', () => {
const avatar = Avatar.create({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'https://example.com/avatar.png',
});
expect(avatar.isActive).toBe(true);
avatar.deactivate();
expect(avatar.isActive).toBe(false);
});
it('can deactivate an already inactive avatar', () => {
const avatar = Avatar.reconstitute({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'https://example.com/avatar.png',
selectedAt: new Date(),
isActive: false,
});
avatar.deactivate();
expect(avatar.isActive).toBe(false);
});
});
describe('toProps', () => {
it('returns correct props for a new avatar', () => {
const avatar = Avatar.create({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'https://example.com/avatar.png',
});
const props = avatar.toProps();
expect(props.id).toBe('avatar-1');
expect(props.driverId).toBe('driver-1');
expect(props.mediaUrl).toBe('https://example.com/avatar.png');
expect(props.selectedAt).toBeInstanceOf(Date);
expect(props.isActive).toBe(true);
});
it('returns correct props for an inactive avatar', () => {
const selectedAt = new Date('2024-01-01T00:00:00.000Z');
const avatar = Avatar.reconstitute({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'https://example.com/avatar.png',
selectedAt,
isActive: false,
});
const props = avatar.toProps();
expect(props.id).toBe('avatar-1');
expect(props.driverId).toBe('driver-1');
expect(props.mediaUrl).toBe('https://example.com/avatar.png');
expect(props.selectedAt).toEqual(selectedAt);
expect(props.isActive).toBe(false);
});
});
describe('value object validation', () => {
it('validates mediaUrl as MediaUrl value object', () => {
const avatar = Avatar.create({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'https://example.com/avatar.png',
});
expect(avatar.mediaUrl).toBeInstanceOf(MediaUrl);
expect(avatar.mediaUrl.value).toBe('https://example.com/avatar.png');
});
it('accepts data URI for mediaUrl', () => {
const avatar = Avatar.create({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'data:image/png;base64,abc',
});
expect(avatar.mediaUrl.value).toBe('data:image/png;base64,abc');
});
it('accepts root-relative path for mediaUrl', () => {
const avatar = Avatar.create({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: '/images/avatar.png',
});
expect(avatar.mediaUrl.value).toBe('/images/avatar.png');
});
});
});

View File

@@ -1,7 +1,476 @@
import * as mod from '@core/media/domain/entities/AvatarGenerationRequest';
import { AvatarGenerationRequest } from './AvatarGenerationRequest';
import { MediaUrl } from '../value-objects/MediaUrl';
describe('media/domain/entities/AvatarGenerationRequest.ts', () => {
it('imports', () => {
expect(mod).toBeTruthy();
describe('AvatarGenerationRequest', () => {
describe('create', () => {
it('creates a new request with required properties', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
style: 'realistic',
});
expect(request.id).toBe('req-1');
expect(request.userId).toBe('user-1');
expect(request.facePhotoUrl).toBeInstanceOf(MediaUrl);
expect(request.facePhotoUrl.value).toBe('data:image/png;base64,abc');
expect(request.suitColor).toBe('red');
expect(request.style).toBe('realistic');
expect(request.status).toBe('pending');
expect(request.generatedAvatarUrls).toEqual([]);
expect(request.selectedAvatarIndex).toBeUndefined();
expect(request.errorMessage).toBeUndefined();
expect(request.createdAt).toBeInstanceOf(Date);
expect(request.updatedAt).toBeInstanceOf(Date);
});
it('creates request with default style when not provided', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'blue',
});
expect(request.style).toBe('realistic');
});
it('throws error when userId is missing', () => {
expect(() =>
AvatarGenerationRequest.create({
id: 'req-1',
userId: '',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
})
).toThrow('User ID is required');
});
it('throws error when facePhotoUrl is missing', () => {
expect(() =>
AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: '',
suitColor: 'red',
})
).toThrow('Face photo URL is required');
});
it('throws error when facePhotoUrl is invalid', () => {
expect(() =>
AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'invalid-url',
suitColor: 'red',
})
).toThrow();
});
});
describe('reconstitute', () => {
it('reconstitutes a request from props', () => {
const createdAt = new Date('2024-01-01T00:00:00.000Z');
const updatedAt = new Date('2024-01-01T01:00:00.000Z');
const request = AvatarGenerationRequest.reconstitute({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
style: 'realistic',
status: 'pending',
generatedAvatarUrls: [],
createdAt,
updatedAt,
});
expect(request.id).toBe('req-1');
expect(request.userId).toBe('user-1');
expect(request.facePhotoUrl.value).toBe('data:image/png;base64,abc');
expect(request.suitColor).toBe('red');
expect(request.style).toBe('realistic');
expect(request.status).toBe('pending');
expect(request.generatedAvatarUrls).toEqual([]);
expect(request.selectedAvatarIndex).toBeUndefined();
expect(request.errorMessage).toBeUndefined();
expect(request.createdAt).toEqual(createdAt);
expect(request.updatedAt).toEqual(updatedAt);
});
it('reconstitutes a request with selected avatar', () => {
const request = AvatarGenerationRequest.reconstitute({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
style: 'realistic',
status: 'completed',
generatedAvatarUrls: ['https://example.com/a.png', 'https://example.com/b.png'],
selectedAvatarIndex: 1,
createdAt: new Date(),
updatedAt: new Date(),
});
expect(request.selectedAvatarIndex).toBe(1);
expect(request.selectedAvatarUrl).toBe('https://example.com/b.png');
});
it('reconstitutes a failed request', () => {
const request = AvatarGenerationRequest.reconstitute({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
style: 'realistic',
status: 'failed',
generatedAvatarUrls: [],
errorMessage: 'Generation failed',
createdAt: new Date(),
updatedAt: new Date(),
});
expect(request.status).toBe('failed');
expect(request.errorMessage).toBe('Generation failed');
});
});
describe('status transitions', () => {
it('transitions from pending to validating', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
expect(request.status).toBe('pending');
request.markAsValidating();
expect(request.status).toBe('validating');
});
it('transitions from validating to generating', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
request.markAsValidating();
request.markAsGenerating();
expect(request.status).toBe('generating');
});
it('throws error when marking as validating from non-pending status', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
request.markAsValidating();
expect(() => request.markAsValidating()).toThrow('Can only start validation from pending status');
});
it('throws error when marking as generating from non-validating status', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
expect(() => request.markAsGenerating()).toThrow('Can only start generation from validating status');
});
it('completes request with avatars', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
request.markAsValidating();
request.markAsGenerating();
request.completeWithAvatars(['https://example.com/a.png', 'https://example.com/b.png']);
expect(request.status).toBe('completed');
expect(request.generatedAvatarUrls).toEqual(['https://example.com/a.png', 'https://example.com/b.png']);
});
it('throws error when completing with empty avatar list', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
request.markAsValidating();
request.markAsGenerating();
expect(() => request.completeWithAvatars([])).toThrow('At least one avatar URL is required');
});
it('fails request with error message', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
request.markAsValidating();
request.fail('Face validation failed');
expect(request.status).toBe('failed');
expect(request.errorMessage).toBe('Face validation failed');
});
});
describe('avatar selection', () => {
it('selects avatar when request is completed', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
request.markAsValidating();
request.markAsGenerating();
request.completeWithAvatars(['https://example.com/a.png', 'https://example.com/b.png']);
request.selectAvatar(1);
expect(request.selectedAvatarIndex).toBe(1);
expect(request.selectedAvatarUrl).toBe('https://example.com/b.png');
});
it('throws error when selecting avatar from non-completed request', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
request.markAsValidating();
expect(() => request.selectAvatar(0)).toThrow('Can only select avatar when generation is completed');
});
it('throws error when selecting invalid index', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
request.markAsValidating();
request.markAsGenerating();
request.completeWithAvatars(['https://example.com/a.png', 'https://example.com/b.png']);
expect(() => request.selectAvatar(-1)).toThrow('Invalid avatar index');
expect(() => request.selectAvatar(2)).toThrow('Invalid avatar index');
});
it('returns undefined for selectedAvatarUrl when no avatar selected', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
request.markAsValidating();
request.markAsGenerating();
request.completeWithAvatars(['https://example.com/a.png', 'https://example.com/b.png']);
expect(request.selectedAvatarUrl).toBeUndefined();
});
});
describe('buildPrompt', () => {
it('builds prompt for red suit, realistic style', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
style: 'realistic',
});
const prompt = request.buildPrompt();
expect(prompt).toContain('vibrant racing red');
expect(prompt).toContain('photorealistic, professional motorsport portrait');
expect(prompt).toContain('racing driver');
expect(prompt).toContain('racing suit');
expect(prompt).toContain('helmet');
});
it('builds prompt for blue suit, cartoon style', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'blue',
style: 'cartoon',
});
const prompt = request.buildPrompt();
expect(prompt).toContain('deep motorsport blue');
expect(prompt).toContain('stylized cartoon racing character');
});
it('builds prompt for pixel-art style', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'green',
style: 'pixel-art',
});
const prompt = request.buildPrompt();
expect(prompt).toContain('racing green');
expect(prompt).toContain('8-bit pixel art retro racing avatar');
});
it('builds prompt for all suit colors', () => {
const colors = ['red', 'blue', 'green', 'yellow', 'orange', 'purple', 'black', 'white', 'pink', 'cyan'] as const;
colors.forEach((color) => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: color,
});
const prompt = request.buildPrompt();
expect(prompt).toContain(color);
});
});
});
describe('toProps', () => {
it('returns correct props for a new request', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
style: 'realistic',
});
const props = request.toProps();
expect(props.id).toBe('req-1');
expect(props.userId).toBe('user-1');
expect(props.facePhotoUrl).toBe('data:image/png;base64,abc');
expect(props.suitColor).toBe('red');
expect(props.style).toBe('realistic');
expect(props.status).toBe('pending');
expect(props.generatedAvatarUrls).toEqual([]);
expect(props.selectedAvatarIndex).toBeUndefined();
expect(props.errorMessage).toBeUndefined();
expect(props.createdAt).toBeInstanceOf(Date);
expect(props.updatedAt).toBeInstanceOf(Date);
});
it('returns correct props for a completed request with selected avatar', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
style: 'realistic',
});
request.markAsValidating();
request.markAsGenerating();
request.completeWithAvatars(['https://example.com/a.png', 'https://example.com/b.png']);
request.selectAvatar(1);
const props = request.toProps();
expect(props.id).toBe('req-1');
expect(props.userId).toBe('user-1');
expect(props.facePhotoUrl).toBe('data:image/png;base64,abc');
expect(props.suitColor).toBe('red');
expect(props.style).toBe('realistic');
expect(props.status).toBe('completed');
expect(props.generatedAvatarUrls).toEqual(['https://example.com/a.png', 'https://example.com/b.png']);
expect(props.selectedAvatarIndex).toBe(1);
expect(props.errorMessage).toBeUndefined();
});
it('returns correct props for a failed request', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
style: 'realistic',
});
request.markAsValidating();
request.fail('Face validation failed');
const props = request.toProps();
expect(props.id).toBe('req-1');
expect(props.userId).toBe('user-1');
expect(props.facePhotoUrl).toBe('data:image/png;base64,abc');
expect(props.suitColor).toBe('red');
expect(props.style).toBe('realistic');
expect(props.status).toBe('failed');
expect(props.generatedAvatarUrls).toEqual([]);
expect(props.selectedAvatarIndex).toBeUndefined();
expect(props.errorMessage).toBe('Face validation failed');
});
});
describe('value object validation', () => {
it('validates facePhotoUrl as MediaUrl value object', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'data:image/png;base64,abc',
suitColor: 'red',
});
expect(request.facePhotoUrl).toBeInstanceOf(MediaUrl);
expect(request.facePhotoUrl.value).toBe('data:image/png;base64,abc');
});
it('accepts http URL for facePhotoUrl', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: 'https://example.com/face.png',
suitColor: 'red',
});
expect(request.facePhotoUrl.value).toBe('https://example.com/face.png');
});
it('accepts root-relative path for facePhotoUrl', () => {
const request = AvatarGenerationRequest.create({
id: 'req-1',
userId: 'user-1',
facePhotoUrl: '/images/face.png',
suitColor: 'red',
});
expect(request.facePhotoUrl.value).toBe('/images/face.png');
});
});
});

View File

@@ -1,7 +1,307 @@
import * as mod from '@core/media/domain/entities/Media';
import { Media } from './Media';
import { MediaUrl } from '../value-objects/MediaUrl';
describe('media/domain/entities/Media.ts', () => {
it('imports', () => {
expect(mod).toBeTruthy();
describe('Media', () => {
describe('create', () => {
it('creates a new media with required properties', () => {
const media = Media.create({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'https://example.com/avatar.png',
type: 'image',
uploadedBy: 'user-1',
});
expect(media.id).toBe('media-1');
expect(media.filename).toBe('avatar.png');
expect(media.originalName).toBe('avatar.png');
expect(media.mimeType).toBe('image/png');
expect(media.size).toBe(123);
expect(media.url).toBeInstanceOf(MediaUrl);
expect(media.url.value).toBe('https://example.com/avatar.png');
expect(media.type).toBe('image');
expect(media.uploadedBy).toBe('user-1');
expect(media.uploadedAt).toBeInstanceOf(Date);
expect(media.metadata).toBeUndefined();
});
it('creates media with metadata', () => {
const media = Media.create({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'https://example.com/avatar.png',
type: 'image',
uploadedBy: 'user-1',
metadata: { width: 100, height: 100 },
});
expect(media.metadata).toEqual({ width: 100, height: 100 });
});
it('throws error when filename is missing', () => {
expect(() =>
Media.create({
id: 'media-1',
filename: '',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'https://example.com/avatar.png',
type: 'image',
uploadedBy: 'user-1',
})
).toThrow('Filename is required');
});
it('throws error when url is missing', () => {
expect(() =>
Media.create({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: '',
type: 'image',
uploadedBy: 'user-1',
})
).toThrow('URL is required');
});
it('throws error when uploadedBy is missing', () => {
expect(() =>
Media.create({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'https://example.com/avatar.png',
type: 'image',
uploadedBy: '',
})
).toThrow('Uploaded by is required');
});
it('throws error when url is invalid', () => {
expect(() =>
Media.create({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'invalid-url',
type: 'image',
uploadedBy: 'user-1',
})
).toThrow();
});
});
describe('reconstitute', () => {
it('reconstitutes a media from props', () => {
const uploadedAt = new Date('2024-01-01T00:00:00.000Z');
const media = Media.reconstitute({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'https://example.com/avatar.png',
type: 'image',
uploadedBy: 'user-1',
uploadedAt,
});
expect(media.id).toBe('media-1');
expect(media.filename).toBe('avatar.png');
expect(media.originalName).toBe('avatar.png');
expect(media.mimeType).toBe('image/png');
expect(media.size).toBe(123);
expect(media.url.value).toBe('https://example.com/avatar.png');
expect(media.type).toBe('image');
expect(media.uploadedBy).toBe('user-1');
expect(media.uploadedAt).toEqual(uploadedAt);
expect(media.metadata).toBeUndefined();
});
it('reconstitutes a media with metadata', () => {
const media = Media.reconstitute({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'https://example.com/avatar.png',
type: 'image',
uploadedBy: 'user-1',
uploadedAt: new Date(),
metadata: { width: 100, height: 100 },
});
expect(media.metadata).toEqual({ width: 100, height: 100 });
});
it('reconstitutes a video media', () => {
const media = Media.reconstitute({
id: 'media-1',
filename: 'video.mp4',
originalName: 'video.mp4',
mimeType: 'video/mp4',
size: 1024,
url: 'https://example.com/video.mp4',
type: 'video',
uploadedBy: 'user-1',
uploadedAt: new Date(),
});
expect(media.type).toBe('video');
});
it('reconstitutes a document media', () => {
const media = Media.reconstitute({
id: 'media-1',
filename: 'document.pdf',
originalName: 'document.pdf',
mimeType: 'application/pdf',
size: 2048,
url: 'https://example.com/document.pdf',
type: 'document',
uploadedBy: 'user-1',
uploadedAt: new Date(),
});
expect(media.type).toBe('document');
});
});
describe('toProps', () => {
it('returns correct props for a new media', () => {
const media = Media.create({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'https://example.com/avatar.png',
type: 'image',
uploadedBy: 'user-1',
});
const props = media.toProps();
expect(props.id).toBe('media-1');
expect(props.filename).toBe('avatar.png');
expect(props.originalName).toBe('avatar.png');
expect(props.mimeType).toBe('image/png');
expect(props.size).toBe(123);
expect(props.url).toBe('https://example.com/avatar.png');
expect(props.type).toBe('image');
expect(props.uploadedBy).toBe('user-1');
expect(props.uploadedAt).toBeInstanceOf(Date);
expect(props.metadata).toBeUndefined();
});
it('returns correct props for a media with metadata', () => {
const media = Media.create({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'https://example.com/avatar.png',
type: 'image',
uploadedBy: 'user-1',
metadata: { width: 100, height: 100 },
});
const props = media.toProps();
expect(props.metadata).toEqual({ width: 100, height: 100 });
});
it('returns correct props for a reconstituted media', () => {
const uploadedAt = new Date('2024-01-01T00:00:00.000Z');
const media = Media.reconstitute({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'https://example.com/avatar.png',
type: 'image',
uploadedBy: 'user-1',
uploadedAt,
metadata: { width: 100, height: 100 },
});
const props = media.toProps();
expect(props.id).toBe('media-1');
expect(props.filename).toBe('avatar.png');
expect(props.originalName).toBe('avatar.png');
expect(props.mimeType).toBe('image/png');
expect(props.size).toBe(123);
expect(props.url).toBe('https://example.com/avatar.png');
expect(props.type).toBe('image');
expect(props.uploadedBy).toBe('user-1');
expect(props.uploadedAt).toEqual(uploadedAt);
expect(props.metadata).toEqual({ width: 100, height: 100 });
});
});
describe('value object validation', () => {
it('validates url as MediaUrl value object', () => {
const media = Media.create({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'https://example.com/avatar.png',
type: 'image',
uploadedBy: 'user-1',
});
expect(media.url).toBeInstanceOf(MediaUrl);
expect(media.url.value).toBe('https://example.com/avatar.png');
});
it('accepts data URI for url', () => {
const media = Media.create({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: 'data:image/png;base64,abc',
type: 'image',
uploadedBy: 'user-1',
});
expect(media.url.value).toBe('data:image/png;base64,abc');
});
it('accepts root-relative path for url', () => {
const media = Media.create({
id: 'media-1',
filename: 'avatar.png',
originalName: 'avatar.png',
mimeType: 'image/png',
size: 123,
url: '/images/avatar.png',
type: 'image',
uploadedBy: 'user-1',
});
expect(media.url.value).toBe('/images/avatar.png');
});
});
});

View File

@@ -0,0 +1,223 @@
import { MediaGenerationService } from './MediaGenerationService';
describe('MediaGenerationService', () => {
let service: MediaGenerationService;
beforeEach(() => {
service = new MediaGenerationService();
});
describe('generateTeamLogo', () => {
it('generates a deterministic logo URL for a team', () => {
const url1 = service.generateTeamLogo('team-123');
const url2 = service.generateTeamLogo('team-123');
expect(url1).toBe(url2);
expect(url1).toContain('https://picsum.photos/seed/team-123/200/200');
});
it('generates different URLs for different team IDs', () => {
const url1 = service.generateTeamLogo('team-123');
const url2 = service.generateTeamLogo('team-456');
expect(url1).not.toBe(url2);
});
it('generates URL with correct format', () => {
const url = service.generateTeamLogo('team-123');
expect(url).toMatch(/^https:\/\/picsum\.photos\/seed\/team-123\/200\/200$/);
});
});
describe('generateLeagueLogo', () => {
it('generates a deterministic logo URL for a league', () => {
const url1 = service.generateLeagueLogo('league-123');
const url2 = service.generateLeagueLogo('league-123');
expect(url1).toBe(url2);
expect(url1).toContain('https://picsum.photos/seed/l-league-123/200/200');
});
it('generates different URLs for different league IDs', () => {
const url1 = service.generateLeagueLogo('league-123');
const url2 = service.generateLeagueLogo('league-456');
expect(url1).not.toBe(url2);
});
it('generates URL with correct format', () => {
const url = service.generateLeagueLogo('league-123');
expect(url).toMatch(/^https:\/\/picsum\.photos\/seed\/l-league-123\/200\/200$/);
});
});
describe('generateDriverAvatar', () => {
it('generates a deterministic avatar URL for a driver', () => {
const url1 = service.generateDriverAvatar('driver-123');
const url2 = service.generateDriverAvatar('driver-123');
expect(url1).toBe(url2);
expect(url1).toContain('https://i.pravatar.cc/150?u=driver-123');
});
it('generates different URLs for different driver IDs', () => {
const url1 = service.generateDriverAvatar('driver-123');
const url2 = service.generateDriverAvatar('driver-456');
expect(url1).not.toBe(url2);
});
it('generates URL with correct format', () => {
const url = service.generateDriverAvatar('driver-123');
expect(url).toMatch(/^https:\/\/i\.pravatar\.cc\/150\?u=driver-123$/);
});
});
describe('generateLeagueCover', () => {
it('generates a deterministic cover URL for a league', () => {
const url1 = service.generateLeagueCover('league-123');
const url2 = service.generateLeagueCover('league-123');
expect(url1).toBe(url2);
expect(url1).toContain('https://picsum.photos/seed/c-league-123/800/200');
});
it('generates different URLs for different league IDs', () => {
const url1 = service.generateLeagueCover('league-123');
const url2 = service.generateLeagueCover('league-456');
expect(url1).not.toBe(url2);
});
it('generates URL with correct format', () => {
const url = service.generateLeagueCover('league-123');
expect(url).toMatch(/^https:\/\/picsum\.photos\/seed\/c-league-123\/800\/200$/);
});
});
describe('generateDefaultPNG', () => {
it('generates a PNG buffer for a variant', () => {
const buffer = service.generateDefaultPNG('test-variant');
expect(buffer).toBeInstanceOf(Buffer);
expect(buffer.length).toBeGreaterThan(0);
});
it('generates deterministic PNG for same variant', () => {
const buffer1 = service.generateDefaultPNG('test-variant');
const buffer2 = service.generateDefaultPNG('test-variant');
expect(buffer1.equals(buffer2)).toBe(true);
});
it('generates different PNGs for different variants', () => {
const buffer1 = service.generateDefaultPNG('variant-1');
const buffer2 = service.generateDefaultPNG('variant-2');
expect(buffer1.equals(buffer2)).toBe(false);
});
it('generates valid PNG header', () => {
const buffer = service.generateDefaultPNG('test-variant');
// PNG signature: 89 50 4E 47 0D 0A 1A 0A
expect(buffer[0]).toBe(0x89);
expect(buffer[1]).toBe(0x50); // 'P'
expect(buffer[2]).toBe(0x4E); // 'N'
expect(buffer[3]).toBe(0x47); // 'G'
expect(buffer[4]).toBe(0x0D);
expect(buffer[5]).toBe(0x0A);
expect(buffer[6]).toBe(0x1A);
expect(buffer[7]).toBe(0x0A);
});
it('generates PNG with IHDR chunk', () => {
const buffer = service.generateDefaultPNG('test-variant');
// IHDR chunk starts at byte 8
// Length: 13 (0x00 0x00 0x00 0x0D)
expect(buffer[8]).toBe(0x00);
expect(buffer[9]).toBe(0x00);
expect(buffer[10]).toBe(0x00);
expect(buffer[11]).toBe(0x0D);
// Type: IHDR (0x49 0x48 0x44 0x52)
expect(buffer[12]).toBe(0x49); // 'I'
expect(buffer[13]).toBe(0x48); // 'H'
expect(buffer[14]).toBe(0x44); // 'D'
expect(buffer[15]).toBe(0x52); // 'R'
});
it('generates PNG with 1x1 dimensions', () => {
const buffer = service.generateDefaultPNG('test-variant');
// Width: 1 (0x00 0x00 0x00 0x01) at byte 16
expect(buffer[16]).toBe(0x00);
expect(buffer[17]).toBe(0x00);
expect(buffer[18]).toBe(0x00);
expect(buffer[19]).toBe(0x01);
// Height: 1 (0x00 0x00 0x00 0x01) at byte 20
expect(buffer[20]).toBe(0x00);
expect(buffer[21]).toBe(0x00);
expect(buffer[22]).toBe(0x00);
expect(buffer[23]).toBe(0x01);
});
it('generates PNG with RGB color type', () => {
const buffer = service.generateDefaultPNG('test-variant');
// Color type: RGB (0x02) at byte 25
expect(buffer[25]).toBe(0x02);
});
it('generates PNG with RGB pixel data', () => {
const buffer = service.generateDefaultPNG('test-variant');
// RGB pixel data should be present in IDAT chunk
// IDAT chunk starts after IHDR (byte 37)
// We should find RGB values somewhere in the buffer
const hasRGB = buffer.some((byte, index) => {
// Check if we have a sequence that looks like RGB data
// This is a simplified check
return index > 37 && index < buffer.length - 10;
});
expect(hasRGB).toBe(true);
});
});
describe('deterministic generation', () => {
it('generates same team logo for same team ID across different instances', () => {
const service1 = new MediaGenerationService();
const service2 = new MediaGenerationService();
const url1 = service1.generateTeamLogo('team-123');
const url2 = service2.generateTeamLogo('team-123');
expect(url1).toBe(url2);
});
it('generates same driver avatar for same driver ID across different instances', () => {
const service1 = new MediaGenerationService();
const service2 = new MediaGenerationService();
const url1 = service1.generateDriverAvatar('driver-123');
const url2 = service2.generateDriverAvatar('driver-123');
expect(url1).toBe(url2);
});
it('generates same PNG for same variant across different instances', () => {
const service1 = new MediaGenerationService();
const service2 = new MediaGenerationService();
const buffer1 = service1.generateDefaultPNG('test-variant');
const buffer2 = service2.generateDefaultPNG('test-variant');
expect(buffer1.equals(buffer2)).toBe(true);
});
});
});

View File

@@ -1,7 +1,83 @@
import * as mod from '@core/media/domain/value-objects/AvatarId';
import { AvatarId } from './AvatarId';
describe('media/domain/value-objects/AvatarId.ts', () => {
it('imports', () => {
expect(mod).toBeTruthy();
describe('AvatarId', () => {
describe('create', () => {
it('creates from valid string', () => {
const avatarId = AvatarId.create('avatar-123');
expect(avatarId.toString()).toBe('avatar-123');
});
it('trims whitespace', () => {
const avatarId = AvatarId.create(' avatar-123 ');
expect(avatarId.toString()).toBe('avatar-123');
});
it('throws error when empty', () => {
expect(() => AvatarId.create('')).toThrow('Avatar ID cannot be empty');
});
it('throws error when only whitespace', () => {
expect(() => AvatarId.create(' ')).toThrow('Avatar ID cannot be empty');
});
it('throws error when null', () => {
expect(() => AvatarId.create(null as any)).toThrow('Avatar ID cannot be empty');
});
it('throws error when undefined', () => {
expect(() => AvatarId.create(undefined as any)).toThrow('Avatar ID cannot be empty');
});
});
describe('toString', () => {
it('returns the string value', () => {
const avatarId = AvatarId.create('avatar-123');
expect(avatarId.toString()).toBe('avatar-123');
});
});
describe('equals', () => {
it('returns true for equal avatar IDs', () => {
const avatarId1 = AvatarId.create('avatar-123');
const avatarId2 = AvatarId.create('avatar-123');
expect(avatarId1.equals(avatarId2)).toBe(true);
});
it('returns false for different avatar IDs', () => {
const avatarId1 = AvatarId.create('avatar-123');
const avatarId2 = AvatarId.create('avatar-456');
expect(avatarId1.equals(avatarId2)).toBe(false);
});
it('returns false for different case', () => {
const avatarId1 = AvatarId.create('avatar-123');
const avatarId2 = AvatarId.create('AVATAR-123');
expect(avatarId1.equals(avatarId2)).toBe(false);
});
});
describe('value object equality', () => {
it('implements value-based equality', () => {
const avatarId1 = AvatarId.create('avatar-123');
const avatarId2 = AvatarId.create('avatar-123');
const avatarId3 = AvatarId.create('avatar-456');
expect(avatarId1.equals(avatarId2)).toBe(true);
expect(avatarId1.equals(avatarId3)).toBe(false);
});
it('maintains equality after toString', () => {
const avatarId1 = AvatarId.create('avatar-123');
const avatarId2 = AvatarId.create('avatar-123');
expect(avatarId1.toString()).toBe(avatarId2.toString());
expect(avatarId1.equals(avatarId2)).toBe(true);
});
});
});

View File

@@ -0,0 +1,319 @@
import { describe, expect, it, vi } from 'vitest';
import { Notification } from '../../domain/entities/Notification';
import {
NotificationGateway,
NotificationGatewayRegistry,
NotificationDeliveryResult,
} from './NotificationGateway';
describe('NotificationGateway - Interface Contract', () => {
it('NotificationGateway interface defines send method', () => {
const mockGateway: NotificationGateway = {
send: vi.fn().mockResolvedValue({
success: true,
channel: 'in_app',
attemptedAt: new Date(),
}),
supportsChannel: vi.fn().mockReturnValue(true),
isConfigured: vi.fn().mockReturnValue(true),
getChannel: vi.fn().mockReturnValue('in_app'),
};
const notification = Notification.create({
id: 'test-id',
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Test',
body: 'Test body',
channel: 'in_app',
});
expect(mockGateway.send).toBeDefined();
expect(typeof mockGateway.send).toBe('function');
});
it('NotificationGateway interface defines supportsChannel method', () => {
const mockGateway: NotificationGateway = {
send: vi.fn().mockResolvedValue({
success: true,
channel: 'in_app',
attemptedAt: new Date(),
}),
supportsChannel: vi.fn().mockReturnValue(true),
isConfigured: vi.fn().mockReturnValue(true),
getChannel: vi.fn().mockReturnValue('in_app'),
};
expect(mockGateway.supportsChannel).toBeDefined();
expect(typeof mockGateway.supportsChannel).toBe('function');
});
it('NotificationGateway interface defines isConfigured method', () => {
const mockGateway: NotificationGateway = {
send: vi.fn().mockResolvedValue({
success: true,
channel: 'in_app',
attemptedAt: new Date(),
}),
supportsChannel: vi.fn().mockReturnValue(true),
isConfigured: vi.fn().mockReturnValue(true),
getChannel: vi.fn().mockReturnValue('in_app'),
};
expect(mockGateway.isConfigured).toBeDefined();
expect(typeof mockGateway.isConfigured).toBe('function');
});
it('NotificationGateway interface defines getChannel method', () => {
const mockGateway: NotificationGateway = {
send: vi.fn().mockResolvedValue({
success: true,
channel: 'in_app',
attemptedAt: new Date(),
}),
supportsChannel: vi.fn().mockReturnValue(true),
isConfigured: vi.fn().mockReturnValue(true),
getChannel: vi.fn().mockReturnValue('in_app'),
};
expect(mockGateway.getChannel).toBeDefined();
expect(typeof mockGateway.getChannel).toBe('function');
});
it('NotificationDeliveryResult has required properties', () => {
const result: NotificationDeliveryResult = {
success: true,
channel: 'in_app',
attemptedAt: new Date(),
};
expect(result).toHaveProperty('success');
expect(result).toHaveProperty('channel');
expect(result).toHaveProperty('attemptedAt');
});
it('NotificationDeliveryResult can have optional externalId', () => {
const result: NotificationDeliveryResult = {
success: true,
channel: 'email',
externalId: 'email-123',
attemptedAt: new Date(),
};
expect(result.externalId).toBe('email-123');
});
it('NotificationDeliveryResult can have optional error', () => {
const result: NotificationDeliveryResult = {
success: false,
channel: 'discord',
error: 'Failed to send to Discord',
attemptedAt: new Date(),
};
expect(result.error).toBe('Failed to send to Discord');
});
});
describe('NotificationGatewayRegistry - Interface Contract', () => {
it('NotificationGatewayRegistry interface defines register method', () => {
const mockRegistry: NotificationGatewayRegistry = {
register: vi.fn(),
getGateway: vi.fn().mockReturnValue(null),
getAllGateways: vi.fn().mockReturnValue([]),
send: vi.fn().mockResolvedValue({
success: true,
channel: 'in_app',
attemptedAt: new Date(),
}),
};
expect(mockRegistry.register).toBeDefined();
expect(typeof mockRegistry.register).toBe('function');
});
it('NotificationGatewayRegistry interface defines getGateway method', () => {
const mockRegistry: NotificationGatewayRegistry = {
register: vi.fn(),
getGateway: vi.fn().mockReturnValue(null),
getAllGateways: vi.fn().mockReturnValue([]),
send: vi.fn().mockResolvedValue({
success: true,
channel: 'in_app',
attemptedAt: new Date(),
}),
};
expect(mockRegistry.getGateway).toBeDefined();
expect(typeof mockRegistry.getGateway).toBe('function');
});
it('NotificationGatewayRegistry interface defines getAllGateways method', () => {
const mockRegistry: NotificationGatewayRegistry = {
register: vi.fn(),
getGateway: vi.fn().mockReturnValue(null),
getAllGateways: vi.fn().mockReturnValue([]),
send: vi.fn().mockResolvedValue({
success: true,
channel: 'in_app',
attemptedAt: new Date(),
}),
};
expect(mockRegistry.getAllGateways).toBeDefined();
expect(typeof mockRegistry.getAllGateways).toBe('function');
});
it('NotificationGatewayRegistry interface defines send method', () => {
const mockRegistry: NotificationGatewayRegistry = {
register: vi.fn(),
getGateway: vi.fn().mockReturnValue(null),
getAllGateways: vi.fn().mockReturnValue([]),
send: vi.fn().mockResolvedValue({
success: true,
channel: 'in_app',
attemptedAt: new Date(),
}),
};
expect(mockRegistry.send).toBeDefined();
expect(typeof mockRegistry.send).toBe('function');
});
});
describe('NotificationGateway - Integration with Notification', () => {
it('gateway can send notification and return delivery result', async () => {
const mockGateway: NotificationGateway = {
send: vi.fn().mockResolvedValue({
success: true,
channel: 'in_app',
externalId: 'msg-123',
attemptedAt: new Date(),
}),
supportsChannel: vi.fn().mockReturnValue(true),
isConfigured: vi.fn().mockReturnValue(true),
getChannel: vi.fn().mockReturnValue('in_app'),
};
const notification = Notification.create({
id: 'test-id',
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Test',
body: 'Test body',
channel: 'in_app',
});
const result = await mockGateway.send(notification);
expect(result.success).toBe(true);
expect(result.channel).toBe('in_app');
expect(result.externalId).toBe('msg-123');
expect(mockGateway.send).toHaveBeenCalledWith(notification);
});
it('gateway can handle failed delivery', async () => {
const mockGateway: NotificationGateway = {
send: vi.fn().mockResolvedValue({
success: false,
channel: 'email',
error: 'SMTP server unavailable',
attemptedAt: new Date(),
}),
supportsChannel: vi.fn().mockReturnValue(true),
isConfigured: vi.fn().mockReturnValue(true),
getChannel: vi.fn().mockReturnValue('email'),
};
const notification = Notification.create({
id: 'test-id',
recipientId: 'driver-1',
type: 'race_registration_open',
title: 'Test',
body: 'Test body',
channel: 'email',
});
const result = await mockGateway.send(notification);
expect(result.success).toBe(false);
expect(result.channel).toBe('email');
expect(result.error).toBe('SMTP server unavailable');
});
});
describe('NotificationGatewayRegistry - Integration', () => {
it('registry can route notification to appropriate gateway', async () => {
const inAppGateway: NotificationGateway = {
send: vi.fn().mockResolvedValue({
success: true,
channel: 'in_app',
attemptedAt: new Date(),
}),
supportsChannel: vi.fn().mockReturnValue(true),
isConfigured: vi.fn().mockReturnValue(true),
getChannel: vi.fn().mockReturnValue('in_app'),
};
const emailGateway: NotificationGateway = {
send: vi.fn().mockResolvedValue({
success: true,
channel: 'email',
externalId: 'email-456',
attemptedAt: new Date(),
}),
supportsChannel: vi.fn().mockReturnValue(true),
isConfigured: vi.fn().mockReturnValue(true),
getChannel: vi.fn().mockReturnValue('email'),
};
const mockRegistry: NotificationGatewayRegistry = {
register: vi.fn(),
getGateway: vi.fn().mockImplementation((channel) => {
if (channel === 'in_app') return inAppGateway;
if (channel === 'email') return emailGateway;
return null;
}),
getAllGateways: vi.fn().mockReturnValue([inAppGateway, emailGateway]),
send: vi.fn().mockImplementation(async (notification) => {
const gateway = mockRegistry.getGateway(notification.channel);
if (gateway) {
return gateway.send(notification);
}
return {
success: false,
channel: notification.channel,
error: 'No gateway found',
attemptedAt: new Date(),
};
}),
};
const inAppNotification = Notification.create({
id: 'test-1',
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Test',
body: 'Test body',
channel: 'in_app',
});
const emailNotification = Notification.create({
id: 'test-2',
recipientId: 'driver-1',
type: 'race_registration_open',
title: 'Test',
body: 'Test body',
channel: 'email',
});
const inAppResult = await mockRegistry.send(inAppNotification);
expect(inAppResult.success).toBe(true);
expect(inAppResult.channel).toBe('in_app');
const emailResult = await mockRegistry.send(emailNotification);
expect(emailResult.success).toBe(true);
expect(emailResult.channel).toBe('email');
expect(emailResult.externalId).toBe('email-456');
});
});

View File

@@ -0,0 +1,346 @@
import { describe, expect, it, vi } from 'vitest';
import {
NotificationService,
SendNotificationCommand,
NotificationData,
NotificationAction,
} from './NotificationService';
describe('NotificationService - Interface Contract', () => {
it('NotificationService interface defines sendNotification method', () => {
const mockService: NotificationService = {
sendNotification: vi.fn().mockResolvedValue(undefined),
};
expect(mockService.sendNotification).toBeDefined();
expect(typeof mockService.sendNotification).toBe('function');
});
it('SendNotificationCommand has required properties', () => {
const command: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Test Notification',
body: 'This is a test notification',
channel: 'in_app',
urgency: 'toast',
};
expect(command).toHaveProperty('recipientId');
expect(command).toHaveProperty('type');
expect(command).toHaveProperty('title');
expect(command).toHaveProperty('body');
expect(command).toHaveProperty('channel');
expect(command).toHaveProperty('urgency');
});
it('SendNotificationCommand can have optional data', () => {
const command: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'race_results_posted',
title: 'Race Results',
body: 'Your race results are available',
channel: 'email',
urgency: 'toast',
data: {
raceEventId: 'event-123',
sessionId: 'session-456',
position: 5,
positionChange: 2,
},
};
expect(command.data).toBeDefined();
expect(command.data?.raceEventId).toBe('event-123');
expect(command.data?.position).toBe(5);
});
it('SendNotificationCommand can have optional actionUrl', () => {
const command: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'protest_vote_required',
title: 'Vote Required',
body: 'You need to vote on a protest',
channel: 'in_app',
urgency: 'modal',
actionUrl: '/protests/vote/123',
};
expect(command.actionUrl).toBe('/protests/vote/123');
});
it('SendNotificationCommand can have optional actions array', () => {
const actions: NotificationAction[] = [
{
label: 'View Details',
type: 'primary',
href: '/protests/123',
},
{
label: 'Dismiss',
type: 'secondary',
actionId: 'dismiss',
},
];
const command: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'protest_filed',
title: 'Protest Filed',
body: 'A protest has been filed against you',
channel: 'in_app',
urgency: 'modal',
actions,
};
expect(command.actions).toBeDefined();
expect(command.actions?.length).toBe(2);
expect(command.actions?.[0].label).toBe('View Details');
expect(command.actions?.[1].type).toBe('secondary');
});
it('SendNotificationCommand can have optional requiresResponse', () => {
const command: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'protest_vote_required',
title: 'Vote Required',
body: 'You need to vote on a protest',
channel: 'in_app',
urgency: 'modal',
requiresResponse: true,
};
expect(command.requiresResponse).toBe(true);
});
it('NotificationData can have various optional fields', () => {
const data: NotificationData = {
raceEventId: 'event-123',
sessionId: 'session-456',
leagueId: 'league-789',
position: 3,
positionChange: 1,
incidents: 2,
provisionalRatingChange: 15,
finalRatingChange: 10,
hadPenaltiesApplied: true,
deadline: new Date('2024-01-01'),
protestId: 'protest-999',
customField: 'custom value',
};
expect(data.raceEventId).toBe('event-123');
expect(data.sessionId).toBe('session-456');
expect(data.leagueId).toBe('league-789');
expect(data.position).toBe(3);
expect(data.positionChange).toBe(1);
expect(data.incidents).toBe(2);
expect(data.provisionalRatingChange).toBe(15);
expect(data.finalRatingChange).toBe(10);
expect(data.hadPenaltiesApplied).toBe(true);
expect(data.deadline).toBeInstanceOf(Date);
expect(data.protestId).toBe('protest-999');
expect(data.customField).toBe('custom value');
});
it('NotificationData can have minimal fields', () => {
const data: NotificationData = {
raceEventId: 'event-123',
};
expect(data.raceEventId).toBe('event-123');
});
it('NotificationAction has required properties', () => {
const action: NotificationAction = {
label: 'View Details',
type: 'primary',
};
expect(action).toHaveProperty('label');
expect(action).toHaveProperty('type');
});
it('NotificationAction can have optional href', () => {
const action: NotificationAction = {
label: 'View Details',
type: 'primary',
href: '/protests/123',
};
expect(action.href).toBe('/protests/123');
});
it('NotificationAction can have optional actionId', () => {
const action: NotificationAction = {
label: 'Dismiss',
type: 'secondary',
actionId: 'dismiss',
};
expect(action.actionId).toBe('dismiss');
});
it('NotificationAction type can be primary, secondary, or danger', () => {
const primaryAction: NotificationAction = {
label: 'Accept',
type: 'primary',
};
const secondaryAction: NotificationAction = {
label: 'Cancel',
type: 'secondary',
};
const dangerAction: NotificationAction = {
label: 'Delete',
type: 'danger',
};
expect(primaryAction.type).toBe('primary');
expect(secondaryAction.type).toBe('secondary');
expect(dangerAction.type).toBe('danger');
});
});
describe('NotificationService - Integration', () => {
it('service can send notification with all optional fields', async () => {
const mockService: NotificationService = {
sendNotification: vi.fn().mockResolvedValue(undefined),
};
const command: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'race_performance_summary',
title: 'Performance Summary',
body: 'Your performance summary is ready',
channel: 'email',
urgency: 'toast',
data: {
raceEventId: 'event-123',
sessionId: 'session-456',
position: 5,
positionChange: 2,
incidents: 1,
provisionalRatingChange: 10,
finalRatingChange: 8,
hadPenaltiesApplied: false,
},
actionUrl: '/performance/summary/123',
actions: [
{
label: 'View Details',
type: 'primary',
href: '/performance/summary/123',
},
{
label: 'Dismiss',
type: 'secondary',
actionId: 'dismiss',
},
],
requiresResponse: false,
};
await mockService.sendNotification(command);
expect(mockService.sendNotification).toHaveBeenCalledWith(command);
});
it('service can send notification with minimal fields', async () => {
const mockService: NotificationService = {
sendNotification: vi.fn().mockResolvedValue(undefined),
};
const command: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'system_announcement',
title: 'System Update',
body: 'System will be down for maintenance',
channel: 'in_app',
urgency: 'toast',
};
await mockService.sendNotification(command);
expect(mockService.sendNotification).toHaveBeenCalledWith(command);
});
it('service can send notification with different urgency levels', async () => {
const mockService: NotificationService = {
sendNotification: vi.fn().mockResolvedValue(undefined),
};
const silentCommand: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'race_reminder',
title: 'Race Reminder',
body: 'Your race starts in 30 minutes',
channel: 'in_app',
urgency: 'silent',
};
const toastCommand: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'league_invite',
title: 'League Invite',
body: 'You have been invited to a league',
channel: 'in_app',
urgency: 'toast',
};
const modalCommand: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'protest_vote_required',
title: 'Vote Required',
body: 'You need to vote on a protest',
channel: 'in_app',
urgency: 'modal',
};
await mockService.sendNotification(silentCommand);
await mockService.sendNotification(toastCommand);
await mockService.sendNotification(modalCommand);
expect(mockService.sendNotification).toHaveBeenCalledTimes(3);
});
it('service can send notification through different channels', async () => {
const mockService: NotificationService = {
sendNotification: vi.fn().mockResolvedValue(undefined),
};
const inAppCommand: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'system_announcement',
title: 'System Update',
body: 'System will be down for maintenance',
channel: 'in_app',
urgency: 'toast',
};
const emailCommand: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'race_results_posted',
title: 'Race Results',
body: 'Your race results are available',
channel: 'email',
urgency: 'toast',
};
const discordCommand: SendNotificationCommand = {
recipientId: 'driver-1',
type: 'sponsorship_request_received',
title: 'Sponsorship Request',
body: 'A sponsor wants to sponsor you',
channel: 'discord',
urgency: 'toast',
};
await mockService.sendNotification(inAppCommand);
await mockService.sendNotification(emailCommand);
await mockService.sendNotification(discordCommand);
expect(mockService.sendNotification).toHaveBeenCalledTimes(3);
});
});

View File

@@ -0,0 +1,143 @@
import type { Logger } from '@core/shared/domain/Logger';
import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import { Notification } from '../../domain/entities/Notification';
import { NotificationRepository } from '../../domain/repositories/NotificationRepository';
import {
GetAllNotificationsUseCase,
type GetAllNotificationsInput,
} from './GetAllNotificationsUseCase';
interface NotificationRepositoryMock {
findByRecipientId: Mock;
}
describe('GetAllNotificationsUseCase', () => {
let notificationRepository: NotificationRepositoryMock;
let logger: Logger;
let useCase: GetAllNotificationsUseCase;
beforeEach(() => {
notificationRepository = {
findByRecipientId: vi.fn(),
};
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
useCase = new GetAllNotificationsUseCase(
notificationRepository as unknown as NotificationRepository,
logger,
);
});
it('returns all notifications and total count for recipient', async () => {
const recipientId = 'driver-1';
const notifications: Notification[] = [
Notification.create({
id: 'n1',
recipientId,
type: 'system_announcement',
title: 'Test 1',
body: 'Body 1',
channel: 'in_app',
}),
Notification.create({
id: 'n2',
recipientId,
type: 'race_registration_open',
title: 'Test 2',
body: 'Body 2',
channel: 'email',
}),
];
notificationRepository.findByRecipientId.mockResolvedValue(notifications);
const input: GetAllNotificationsInput = { recipientId };
const result = await useCase.execute(input);
expect(notificationRepository.findByRecipientId).toHaveBeenCalledWith(recipientId);
expect(result).toBeInstanceOf(Result);
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult.notifications).toEqual(notifications);
expect(successResult.totalCount).toBe(2);
});
it('returns empty array when no notifications exist', async () => {
const recipientId = 'driver-1';
notificationRepository.findByRecipientId.mockResolvedValue([]);
const input: GetAllNotificationsInput = { recipientId };
const result = await useCase.execute(input);
expect(notificationRepository.findByRecipientId).toHaveBeenCalledWith(recipientId);
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult.notifications).toEqual([]);
expect(successResult.totalCount).toBe(0);
});
it('handles repository errors by logging and returning error result', async () => {
const recipientId = 'driver-1';
const error = new Error('DB error');
notificationRepository.findByRecipientId.mockRejectedValue(error);
const input: GetAllNotificationsInput = { recipientId };
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>;
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details.message).toBe('DB error');
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
});
it('logs debug message when starting execution', async () => {
const recipientId = 'driver-1';
notificationRepository.findByRecipientId.mockResolvedValue([]);
const input: GetAllNotificationsInput = { recipientId };
await useCase.execute(input);
expect(logger.debug).toHaveBeenCalledWith(
`Attempting to retrieve all notifications for recipient ID: ${recipientId}`,
);
});
it('logs info message on successful retrieval', async () => {
const recipientId = 'driver-1';
const notifications: Notification[] = [
Notification.create({
id: 'n1',
recipientId,
type: 'system_announcement',
title: 'Test',
body: 'Body',
channel: 'in_app',
}),
];
notificationRepository.findByRecipientId.mockResolvedValue(notifications);
const input: GetAllNotificationsInput = { recipientId };
await useCase.execute(input);
expect(logger.info).toHaveBeenCalledWith(
`Successfully retrieved 1 notifications for recipient ID: ${recipientId}`,
);
});
});

View File

@@ -0,0 +1,58 @@
import { describe, expect, it } from 'vitest';
import { NotificationDomainError } from './NotificationDomainError';
describe('NotificationDomainError', () => {
it('creates an error with default validation kind', () => {
const error = new NotificationDomainError('Invalid notification data');
expect(error.name).toBe('NotificationDomainError');
expect(error.type).toBe('domain');
expect(error.context).toBe('notifications');
expect(error.kind).toBe('validation');
expect(error.message).toBe('Invalid notification data');
});
it('creates an error with custom kind', () => {
const error = new NotificationDomainError('Notification not found', 'not_found');
expect(error.kind).toBe('not_found');
expect(error.message).toBe('Notification not found');
});
it('creates an error with business rule kind', () => {
const error = new NotificationDomainError('Cannot send notification during quiet hours', 'business_rule');
expect(error.kind).toBe('business_rule');
expect(error.message).toBe('Cannot send notification during quiet hours');
});
it('creates an error with conflict kind', () => {
const error = new NotificationDomainError('Notification already read', 'conflict');
expect(error.kind).toBe('conflict');
expect(error.message).toBe('Notification already read');
});
it('creates an error with unauthorized kind', () => {
const error = new NotificationDomainError('Cannot access notification', 'unauthorized');
expect(error.kind).toBe('unauthorized');
expect(error.message).toBe('Cannot access notification');
});
it('inherits from Error', () => {
const error = new NotificationDomainError('Test error');
expect(error).toBeInstanceOf(Error);
expect(error.stack).toBeDefined();
});
it('has correct error properties', () => {
const error = new NotificationDomainError('Test error', 'validation');
expect(error.name).toBe('NotificationDomainError');
expect(error.type).toBe('domain');
expect(error.context).toBe('notifications');
expect(error.kind).toBe('validation');
});
});

View File

@@ -0,0 +1,250 @@
import { describe, expect, it, vi } from 'vitest';
import { NotificationPreference } from '../entities/NotificationPreference';
import { NotificationPreferenceRepository } from './NotificationPreferenceRepository';
describe('NotificationPreferenceRepository - Interface Contract', () => {
it('NotificationPreferenceRepository interface defines findByDriverId method', () => {
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(null),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue({} as NotificationPreference),
};
expect(mockRepository.findByDriverId).toBeDefined();
expect(typeof mockRepository.findByDriverId).toBe('function');
});
it('NotificationPreferenceRepository interface defines save method', () => {
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(null),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue({} as NotificationPreference),
};
expect(mockRepository.save).toBeDefined();
expect(typeof mockRepository.save).toBe('function');
});
it('NotificationPreferenceRepository interface defines delete method', () => {
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(null),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue({} as NotificationPreference),
};
expect(mockRepository.delete).toBeDefined();
expect(typeof mockRepository.delete).toBe('function');
});
it('NotificationPreferenceRepository interface defines getOrCreateDefault method', () => {
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(null),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue({} as NotificationPreference),
};
expect(mockRepository.getOrCreateDefault).toBeDefined();
expect(typeof mockRepository.getOrCreateDefault).toBe('function');
});
});
describe('NotificationPreferenceRepository - Integration', () => {
it('can find preferences by driver ID', async () => {
const mockPreference = NotificationPreference.create({
id: 'driver-1',
driverId: 'driver-1',
channels: {
in_app: { enabled: true },
email: { enabled: true },
discord: { enabled: false },
push: { enabled: false },
},
quietHoursStart: 22,
quietHoursEnd: 7,
});
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(mockPreference),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue(mockPreference),
};
const result = await mockRepository.findByDriverId('driver-1');
expect(result).toBe(mockPreference);
expect(mockRepository.findByDriverId).toHaveBeenCalledWith('driver-1');
});
it('returns null when preferences not found', async () => {
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(null),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue({} as NotificationPreference),
};
const result = await mockRepository.findByDriverId('driver-999');
expect(result).toBeNull();
expect(mockRepository.findByDriverId).toHaveBeenCalledWith('driver-999');
});
it('can save preferences', async () => {
const mockPreference = NotificationPreference.create({
id: 'driver-1',
driverId: 'driver-1',
channels: {
in_app: { enabled: true },
email: { enabled: true },
discord: { enabled: false },
push: { enabled: false },
},
quietHoursStart: 22,
quietHoursEnd: 7,
});
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(mockPreference),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue(mockPreference),
};
await mockRepository.save(mockPreference);
expect(mockRepository.save).toHaveBeenCalledWith(mockPreference);
});
it('can delete preferences by driver ID', async () => {
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(null),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue({} as NotificationPreference),
};
await mockRepository.delete('driver-1');
expect(mockRepository.delete).toHaveBeenCalledWith('driver-1');
});
it('can get or create default preferences', async () => {
const defaultPreference = NotificationPreference.createDefault('driver-1');
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(null),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue(defaultPreference),
};
const result = await mockRepository.getOrCreateDefault('driver-1');
expect(result).toBe(defaultPreference);
expect(mockRepository.getOrCreateDefault).toHaveBeenCalledWith('driver-1');
});
it('handles workflow: find, update, save', async () => {
const existingPreference = NotificationPreference.create({
id: 'driver-1',
driverId: 'driver-1',
channels: {
in_app: { enabled: true },
email: { enabled: false },
discord: { enabled: false },
push: { enabled: false },
},
});
const updatedPreference = NotificationPreference.create({
id: 'driver-1',
driverId: 'driver-1',
channels: {
in_app: { enabled: true },
email: { enabled: true },
discord: { enabled: true },
push: { enabled: false },
},
});
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn()
.mockResolvedValueOnce(existingPreference)
.mockResolvedValueOnce(updatedPreference),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue(existingPreference),
};
// Find existing preferences
const found = await mockRepository.findByDriverId('driver-1');
expect(found).toBe(existingPreference);
// Update preferences
const updated = found!.updateChannel('email', { enabled: true });
const updated2 = updated.updateChannel('discord', { enabled: true });
// Save updated preferences
await mockRepository.save(updated2);
expect(mockRepository.save).toHaveBeenCalledWith(updated2);
// Verify update
const updatedFound = await mockRepository.findByDriverId('driver-1');
expect(updatedFound).toBe(updatedPreference);
});
it('handles workflow: get or create, then update', async () => {
const defaultPreference = NotificationPreference.createDefault('driver-1');
const updatedPreference = NotificationPreference.create({
id: 'driver-1',
driverId: 'driver-1',
channels: {
in_app: { enabled: true },
email: { enabled: true },
discord: { enabled: false },
push: { enabled: false },
},
});
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(null),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue(defaultPreference),
};
// Get or create default preferences
const preferences = await mockRepository.getOrCreateDefault('driver-1');
expect(preferences).toBe(defaultPreference);
// Update preferences
const updated = preferences.updateChannel('email', { enabled: true });
// Save updated preferences
await mockRepository.save(updated);
expect(mockRepository.save).toHaveBeenCalledWith(updated);
});
it('handles workflow: delete preferences', async () => {
const mockRepository: NotificationPreferenceRepository = {
findByDriverId: vi.fn().mockResolvedValue(null),
save: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
getOrCreateDefault: vi.fn().mockResolvedValue({} as NotificationPreference),
};
// Delete preferences
await mockRepository.delete('driver-1');
expect(mockRepository.delete).toHaveBeenCalledWith('driver-1');
// Verify deletion
const result = await mockRepository.findByDriverId('driver-1');
expect(result).toBeNull();
});
});

View File

@@ -0,0 +1,539 @@
import { describe, expect, it, vi } from 'vitest';
import { Notification } from '../entities/Notification';
import { NotificationRepository } from './NotificationRepository';
describe('NotificationRepository - Interface Contract', () => {
it('NotificationRepository interface defines findById method', () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
expect(mockRepository.findById).toBeDefined();
expect(typeof mockRepository.findById).toBe('function');
});
it('NotificationRepository interface defines findByRecipientId method', () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
expect(mockRepository.findByRecipientId).toBeDefined();
expect(typeof mockRepository.findByRecipientId).toBe('function');
});
it('NotificationRepository interface defines findUnreadByRecipientId method', () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
expect(mockRepository.findUnreadByRecipientId).toBeDefined();
expect(typeof mockRepository.findUnreadByRecipientId).toBe('function');
});
it('NotificationRepository interface defines findByRecipientIdAndType method', () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
expect(mockRepository.findByRecipientIdAndType).toBeDefined();
expect(typeof mockRepository.findByRecipientIdAndType).toBe('function');
});
it('NotificationRepository interface defines countUnreadByRecipientId method', () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
expect(mockRepository.countUnreadByRecipientId).toBeDefined();
expect(typeof mockRepository.countUnreadByRecipientId).toBe('function');
});
it('NotificationRepository interface defines create method', () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
expect(mockRepository.create).toBeDefined();
expect(typeof mockRepository.create).toBe('function');
});
it('NotificationRepository interface defines update method', () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
expect(mockRepository.update).toBeDefined();
expect(typeof mockRepository.update).toBe('function');
});
it('NotificationRepository interface defines delete method', () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
expect(mockRepository.delete).toBeDefined();
expect(typeof mockRepository.delete).toBe('function');
});
it('NotificationRepository interface defines deleteAllByRecipientId method', () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
expect(mockRepository.deleteAllByRecipientId).toBeDefined();
expect(typeof mockRepository.deleteAllByRecipientId).toBe('function');
});
it('NotificationRepository interface defines markAllAsReadByRecipientId method', () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
expect(mockRepository.markAllAsReadByRecipientId).toBeDefined();
expect(typeof mockRepository.markAllAsReadByRecipientId).toBe('function');
});
});
describe('NotificationRepository - Integration', () => {
it('can find notification by ID', async () => {
const notification = Notification.create({
id: 'notification-1',
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Test',
body: 'Test body',
channel: 'in_app',
});
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(notification),
findByRecipientId: vi.fn().mockResolvedValue([notification]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([notification]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([notification]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(1),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
const result = await mockRepository.findById('notification-1');
expect(result).toBe(notification);
expect(mockRepository.findById).toHaveBeenCalledWith('notification-1');
});
it('returns null when notification not found by ID', async () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
const result = await mockRepository.findById('notification-999');
expect(result).toBeNull();
expect(mockRepository.findById).toHaveBeenCalledWith('notification-999');
});
it('can find all notifications for a recipient', async () => {
const notifications = [
Notification.create({
id: 'notification-1',
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Test 1',
body: 'Body 1',
channel: 'in_app',
}),
Notification.create({
id: 'notification-2',
recipientId: 'driver-1',
type: 'race_registration_open',
title: 'Test 2',
body: 'Body 2',
channel: 'email',
}),
];
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue(notifications),
findUnreadByRecipientId: vi.fn().mockResolvedValue(notifications),
findByRecipientIdAndType: vi.fn().mockResolvedValue(notifications),
countUnreadByRecipientId: vi.fn().mockResolvedValue(2),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
const result = await mockRepository.findByRecipientId('driver-1');
expect(result).toBe(notifications);
expect(mockRepository.findByRecipientId).toHaveBeenCalledWith('driver-1');
});
it('can find unread notifications for a recipient', async () => {
const unreadNotifications = [
Notification.create({
id: 'notification-1',
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Test 1',
body: 'Body 1',
channel: 'in_app',
}),
];
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue(unreadNotifications),
findByRecipientIdAndType: vi.fn().mockResolvedValue(unreadNotifications),
countUnreadByRecipientId: vi.fn().mockResolvedValue(1),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
const result = await mockRepository.findUnreadByRecipientId('driver-1');
expect(result).toBe(unreadNotifications);
expect(mockRepository.findUnreadByRecipientId).toHaveBeenCalledWith('driver-1');
});
it('can find notifications by type for a recipient', async () => {
const protestNotifications = [
Notification.create({
id: 'notification-1',
recipientId: 'driver-1',
type: 'protest_filed',
title: 'Protest Filed',
body: 'A protest has been filed',
channel: 'in_app',
}),
];
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue(protestNotifications),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
const result = await mockRepository.findByRecipientIdAndType('driver-1', 'protest_filed');
expect(result).toBe(protestNotifications);
expect(mockRepository.findByRecipientIdAndType).toHaveBeenCalledWith('driver-1', 'protest_filed');
});
it('can count unread notifications for a recipient', async () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(3),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
const count = await mockRepository.countUnreadByRecipientId('driver-1');
expect(count).toBe(3);
expect(mockRepository.countUnreadByRecipientId).toHaveBeenCalledWith('driver-1');
});
it('can create a new notification', async () => {
const notification = Notification.create({
id: 'notification-1',
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Test',
body: 'Test body',
channel: 'in_app',
});
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
await mockRepository.create(notification);
expect(mockRepository.create).toHaveBeenCalledWith(notification);
});
it('can update an existing notification', async () => {
const notification = Notification.create({
id: 'notification-1',
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Test',
body: 'Test body',
channel: 'in_app',
});
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(notification),
findByRecipientId: vi.fn().mockResolvedValue([notification]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([notification]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([notification]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(1),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
await mockRepository.update(notification);
expect(mockRepository.update).toHaveBeenCalledWith(notification);
});
it('can delete a notification by ID', async () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
await mockRepository.delete('notification-1');
expect(mockRepository.delete).toHaveBeenCalledWith('notification-1');
});
it('can delete all notifications for a recipient', async () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
await mockRepository.deleteAllByRecipientId('driver-1');
expect(mockRepository.deleteAllByRecipientId).toHaveBeenCalledWith('driver-1');
});
it('can mark all notifications as read for a recipient', async () => {
const mockRepository: NotificationRepository = {
findById: vi.fn().mockResolvedValue(null),
findByRecipientId: vi.fn().mockResolvedValue([]),
findUnreadByRecipientId: vi.fn().mockResolvedValue([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn().mockResolvedValue(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
await mockRepository.markAllAsReadByRecipientId('driver-1');
expect(mockRepository.markAllAsReadByRecipientId).toHaveBeenCalledWith('driver-1');
});
it('handles workflow: create, find, update, delete', async () => {
const notification = Notification.create({
id: 'notification-1',
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Test',
body: 'Test body',
channel: 'in_app',
});
const updatedNotification = Notification.create({
id: 'notification-1',
recipientId: 'driver-1',
type: 'system_announcement',
title: 'Updated Test',
body: 'Updated body',
channel: 'in_app',
});
const mockRepository: NotificationRepository = {
findById: vi.fn()
.mockResolvedValueOnce(notification)
.mockResolvedValueOnce(updatedNotification)
.mockResolvedValueOnce(null),
findByRecipientId: vi.fn()
.mockResolvedValueOnce([notification])
.mockResolvedValueOnce([updatedNotification])
.mockResolvedValueOnce([]),
findUnreadByRecipientId: vi.fn()
.mockResolvedValueOnce([notification])
.mockResolvedValueOnce([updatedNotification])
.mockResolvedValueOnce([]),
findByRecipientIdAndType: vi.fn().mockResolvedValue([]),
countUnreadByRecipientId: vi.fn()
.mockResolvedValueOnce(1)
.mockResolvedValueOnce(1)
.mockResolvedValueOnce(0),
create: vi.fn().mockResolvedValue(undefined),
update: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockResolvedValue(undefined),
deleteAllByRecipientId: vi.fn().mockResolvedValue(undefined),
markAllAsReadByRecipientId: vi.fn().mockResolvedValue(undefined),
};
// Create notification
await mockRepository.create(notification);
expect(mockRepository.create).toHaveBeenCalledWith(notification);
// Find notification
const found = await mockRepository.findById('notification-1');
expect(found).toBe(notification);
// Update notification
await mockRepository.update(updatedNotification);
expect(mockRepository.update).toHaveBeenCalledWith(updatedNotification);
// Verify update
const updatedFound = await mockRepository.findById('notification-1');
expect(updatedFound).toBe(updatedNotification);
// Delete notification
await mockRepository.delete('notification-1');
expect(mockRepository.delete).toHaveBeenCalledWith('notification-1');
// Verify deletion
const deletedFound = await mockRepository.findById('notification-1');
expect(deletedFound).toBeNull();
});
});

View File

@@ -0,0 +1,419 @@
import { describe, expect, it } from 'vitest';
import {
getChannelDisplayName,
isExternalChannel,
DEFAULT_ENABLED_CHANNELS,
ALL_CHANNELS,
getNotificationTypeTitle,
getNotificationTypePriority,
type NotificationChannel,
type NotificationType,
} from './NotificationTypes';
describe('NotificationTypes - Channel Functions', () => {
describe('getChannelDisplayName', () => {
it('returns correct display name for in_app channel', () => {
expect(getChannelDisplayName('in_app')).toBe('In-App');
});
it('returns correct display name for email channel', () => {
expect(getChannelDisplayName('email')).toBe('Email');
});
it('returns correct display name for discord channel', () => {
expect(getChannelDisplayName('discord')).toBe('Discord');
});
it('returns correct display name for push channel', () => {
expect(getChannelDisplayName('push')).toBe('Push Notification');
});
});
describe('isExternalChannel', () => {
it('returns false for in_app channel', () => {
expect(isExternalChannel('in_app')).toBe(false);
});
it('returns true for email channel', () => {
expect(isExternalChannel('email')).toBe(true);
});
it('returns true for discord channel', () => {
expect(isExternalChannel('discord')).toBe(true);
});
it('returns true for push channel', () => {
expect(isExternalChannel('push')).toBe(true);
});
});
describe('DEFAULT_ENABLED_CHANNELS', () => {
it('contains only in_app channel', () => {
expect(DEFAULT_ENABLED_CHANNELS).toEqual(['in_app']);
});
it('is an array', () => {
expect(Array.isArray(DEFAULT_ENABLED_CHANNELS)).toBe(true);
});
});
describe('ALL_CHANNELS', () => {
it('contains all notification channels', () => {
expect(ALL_CHANNELS).toEqual(['in_app', 'email', 'discord', 'push']);
});
it('is an array', () => {
expect(Array.isArray(ALL_CHANNELS)).toBe(true);
});
it('has correct length', () => {
expect(ALL_CHANNELS.length).toBe(4);
});
});
});
describe('NotificationTypes - Notification Type Functions', () => {
describe('getNotificationTypeTitle', () => {
it('returns correct title for protest_filed', () => {
expect(getNotificationTypeTitle('protest_filed')).toBe('Protest Filed');
});
it('returns correct title for protest_defense_requested', () => {
expect(getNotificationTypeTitle('protest_defense_requested')).toBe('Defense Requested');
});
it('returns correct title for protest_defense_submitted', () => {
expect(getNotificationTypeTitle('protest_defense_submitted')).toBe('Defense Submitted');
});
it('returns correct title for protest_comment_added', () => {
expect(getNotificationTypeTitle('protest_comment_added')).toBe('New Comment');
});
it('returns correct title for protest_vote_required', () => {
expect(getNotificationTypeTitle('protest_vote_required')).toBe('Vote Required');
});
it('returns correct title for protest_vote_cast', () => {
expect(getNotificationTypeTitle('protest_vote_cast')).toBe('Vote Cast');
});
it('returns correct title for protest_resolved', () => {
expect(getNotificationTypeTitle('protest_resolved')).toBe('Protest Resolved');
});
it('returns correct title for penalty_issued', () => {
expect(getNotificationTypeTitle('penalty_issued')).toBe('Penalty Issued');
});
it('returns correct title for penalty_appealed', () => {
expect(getNotificationTypeTitle('penalty_appealed')).toBe('Penalty Appealed');
});
it('returns correct title for penalty_appeal_resolved', () => {
expect(getNotificationTypeTitle('penalty_appeal_resolved')).toBe('Appeal Resolved');
});
it('returns correct title for race_registration_open', () => {
expect(getNotificationTypeTitle('race_registration_open')).toBe('Registration Open');
});
it('returns correct title for race_reminder', () => {
expect(getNotificationTypeTitle('race_reminder')).toBe('Race Reminder');
});
it('returns correct title for race_results_posted', () => {
expect(getNotificationTypeTitle('race_results_posted')).toBe('Results Posted');
});
it('returns correct title for race_performance_summary', () => {
expect(getNotificationTypeTitle('race_performance_summary')).toBe('Performance Summary');
});
it('returns correct title for race_final_results', () => {
expect(getNotificationTypeTitle('race_final_results')).toBe('Final Results');
});
it('returns correct title for league_invite', () => {
expect(getNotificationTypeTitle('league_invite')).toBe('League Invitation');
});
it('returns correct title for league_join_request', () => {
expect(getNotificationTypeTitle('league_join_request')).toBe('Join Request');
});
it('returns correct title for league_join_approved', () => {
expect(getNotificationTypeTitle('league_join_approved')).toBe('Request Approved');
});
it('returns correct title for league_join_rejected', () => {
expect(getNotificationTypeTitle('league_join_rejected')).toBe('Request Rejected');
});
it('returns correct title for league_role_changed', () => {
expect(getNotificationTypeTitle('league_role_changed')).toBe('Role Changed');
});
it('returns correct title for team_invite', () => {
expect(getNotificationTypeTitle('team_invite')).toBe('Team Invitation');
});
it('returns correct title for team_join_request', () => {
expect(getNotificationTypeTitle('team_join_request')).toBe('Team Join Request');
});
it('returns correct title for team_join_approved', () => {
expect(getNotificationTypeTitle('team_join_approved')).toBe('Team Request Approved');
});
it('returns correct title for sponsorship_request_received', () => {
expect(getNotificationTypeTitle('sponsorship_request_received')).toBe('Sponsorship Request');
});
it('returns correct title for sponsorship_request_accepted', () => {
expect(getNotificationTypeTitle('sponsorship_request_accepted')).toBe('Sponsorship Accepted');
});
it('returns correct title for sponsorship_request_rejected', () => {
expect(getNotificationTypeTitle('sponsorship_request_rejected')).toBe('Sponsorship Rejected');
});
it('returns correct title for sponsorship_request_withdrawn', () => {
expect(getNotificationTypeTitle('sponsorship_request_withdrawn')).toBe('Sponsorship Withdrawn');
});
it('returns correct title for sponsorship_activated', () => {
expect(getNotificationTypeTitle('sponsorship_activated')).toBe('Sponsorship Active');
});
it('returns correct title for sponsorship_payment_received', () => {
expect(getNotificationTypeTitle('sponsorship_payment_received')).toBe('Payment Received');
});
it('returns correct title for system_announcement', () => {
expect(getNotificationTypeTitle('system_announcement')).toBe('Announcement');
});
});
describe('getNotificationTypePriority', () => {
it('returns correct priority for protest_filed', () => {
expect(getNotificationTypePriority('protest_filed')).toBe(8);
});
it('returns correct priority for protest_defense_requested', () => {
expect(getNotificationTypePriority('protest_defense_requested')).toBe(9);
});
it('returns correct priority for protest_defense_submitted', () => {
expect(getNotificationTypePriority('protest_defense_submitted')).toBe(6);
});
it('returns correct priority for protest_comment_added', () => {
expect(getNotificationTypePriority('protest_comment_added')).toBe(4);
});
it('returns correct priority for protest_vote_required', () => {
expect(getNotificationTypePriority('protest_vote_required')).toBe(8);
});
it('returns correct priority for protest_vote_cast', () => {
expect(getNotificationTypePriority('protest_vote_cast')).toBe(3);
});
it('returns correct priority for protest_resolved', () => {
expect(getNotificationTypePriority('protest_resolved')).toBe(7);
});
it('returns correct priority for penalty_issued', () => {
expect(getNotificationTypePriority('penalty_issued')).toBe(9);
});
it('returns correct priority for penalty_appealed', () => {
expect(getNotificationTypePriority('penalty_appealed')).toBe(7);
});
it('returns correct priority for penalty_appeal_resolved', () => {
expect(getNotificationTypePriority('penalty_appeal_resolved')).toBe(7);
});
it('returns correct priority for race_registration_open', () => {
expect(getNotificationTypePriority('race_registration_open')).toBe(5);
});
it('returns correct priority for race_reminder', () => {
expect(getNotificationTypePriority('race_reminder')).toBe(8);
});
it('returns correct priority for race_results_posted', () => {
expect(getNotificationTypePriority('race_results_posted')).toBe(5);
});
it('returns correct priority for race_performance_summary', () => {
expect(getNotificationTypePriority('race_performance_summary')).toBe(9);
});
it('returns correct priority for race_final_results', () => {
expect(getNotificationTypePriority('race_final_results')).toBe(7);
});
it('returns correct priority for league_invite', () => {
expect(getNotificationTypePriority('league_invite')).toBe(6);
});
it('returns correct priority for league_join_request', () => {
expect(getNotificationTypePriority('league_join_request')).toBe(5);
});
it('returns correct priority for league_join_approved', () => {
expect(getNotificationTypePriority('league_join_approved')).toBe(7);
});
it('returns correct priority for league_join_rejected', () => {
expect(getNotificationTypePriority('league_join_rejected')).toBe(7);
});
it('returns correct priority for league_role_changed', () => {
expect(getNotificationTypePriority('league_role_changed')).toBe(6);
});
it('returns correct priority for team_invite', () => {
expect(getNotificationTypePriority('team_invite')).toBe(5);
});
it('returns correct priority for team_join_request', () => {
expect(getNotificationTypePriority('team_join_request')).toBe(4);
});
it('returns correct priority for team_join_approved', () => {
expect(getNotificationTypePriority('team_join_approved')).toBe(6);
});
it('returns correct priority for sponsorship_request_received', () => {
expect(getNotificationTypePriority('sponsorship_request_received')).toBe(7);
});
it('returns correct priority for sponsorship_request_accepted', () => {
expect(getNotificationTypePriority('sponsorship_request_accepted')).toBe(8);
});
it('returns correct priority for sponsorship_request_rejected', () => {
expect(getNotificationTypePriority('sponsorship_request_rejected')).toBe(6);
});
it('returns correct priority for sponsorship_request_withdrawn', () => {
expect(getNotificationTypePriority('sponsorship_request_withdrawn')).toBe(5);
});
it('returns correct priority for sponsorship_activated', () => {
expect(getNotificationTypePriority('sponsorship_activated')).toBe(7);
});
it('returns correct priority for sponsorship_payment_received', () => {
expect(getNotificationTypePriority('sponsorship_payment_received')).toBe(8);
});
it('returns correct priority for system_announcement', () => {
expect(getNotificationTypePriority('system_announcement')).toBe(10);
});
});
});
describe('NotificationTypes - Type Safety', () => {
it('ALL_CHANNELS contains all NotificationChannel values', () => {
const channels: NotificationChannel[] = ['in_app', 'email', 'discord', 'push'];
channels.forEach(channel => {
expect(ALL_CHANNELS).toContain(channel);
});
});
it('DEFAULT_ENABLED_CHANNELS is a subset of ALL_CHANNELS', () => {
DEFAULT_ENABLED_CHANNELS.forEach(channel => {
expect(ALL_CHANNELS).toContain(channel);
});
});
it('all notification types have titles', () => {
const types: NotificationType[] = [
'protest_filed',
'protest_defense_requested',
'protest_defense_submitted',
'protest_comment_added',
'protest_vote_required',
'protest_vote_cast',
'protest_resolved',
'penalty_issued',
'penalty_appealed',
'penalty_appeal_resolved',
'race_registration_open',
'race_reminder',
'race_results_posted',
'race_performance_summary',
'race_final_results',
'league_invite',
'league_join_request',
'league_join_approved',
'league_join_rejected',
'league_role_changed',
'team_invite',
'team_join_request',
'team_join_approved',
'sponsorship_request_received',
'sponsorship_request_accepted',
'sponsorship_request_rejected',
'sponsorship_request_withdrawn',
'sponsorship_activated',
'sponsorship_payment_received',
'system_announcement',
];
types.forEach(type => {
const title = getNotificationTypeTitle(type);
expect(title).toBeDefined();
expect(typeof title).toBe('string');
expect(title.length).toBeGreaterThan(0);
});
});
it('all notification types have priorities', () => {
const types: NotificationType[] = [
'protest_filed',
'protest_defense_requested',
'protest_defense_submitted',
'protest_comment_added',
'protest_vote_required',
'protest_vote_cast',
'protest_resolved',
'penalty_issued',
'penalty_appealed',
'penalty_appeal_resolved',
'race_registration_open',
'race_reminder',
'race_results_posted',
'race_performance_summary',
'race_final_results',
'league_invite',
'league_join_request',
'league_join_approved',
'league_join_rejected',
'league_role_changed',
'team_invite',
'team_join_request',
'team_join_approved',
'sponsorship_request_received',
'sponsorship_request_accepted',
'sponsorship_request_rejected',
'sponsorship_request_withdrawn',
'sponsorship_activated',
'sponsorship_payment_received',
'system_announcement',
];
types.forEach(type => {
const priority = getNotificationTypePriority(type);
expect(priority).toBeDefined();
expect(typeof priority).toBe('number');
expect(priority).toBeGreaterThanOrEqual(0);
expect(priority).toBeLessThanOrEqual(10);
});
});
});

View File

@@ -1,8 +1,174 @@
import * as mod from '@core/payments/domain/entities/MemberPayment';
import {
MemberPayment,
MemberPaymentStatus,
} from '@core/payments/domain/entities/MemberPayment';
import { describe, expect, it } from 'vitest';
describe('payments/domain/entities/MemberPayment.ts', () => {
it('imports', () => {
expect(mod).toBeTruthy();
describe('payments/domain/entities/MemberPayment', () => {
describe('MemberPaymentStatus enum', () => {
it('should have correct status values', () => {
expect(MemberPaymentStatus.PENDING).toBe('pending');
expect(MemberPaymentStatus.PAID).toBe('paid');
expect(MemberPaymentStatus.OVERDUE).toBe('overdue');
});
});
describe('MemberPayment interface', () => {
it('should have all required properties', () => {
const payment: MemberPayment = {
id: 'payment-123',
feeId: 'fee-456',
driverId: 'driver-789',
amount: 100,
platformFee: 10,
netAmount: 90,
status: MemberPaymentStatus.PENDING,
dueDate: new Date('2024-01-01'),
};
expect(payment.id).toBe('payment-123');
expect(payment.feeId).toBe('fee-456');
expect(payment.driverId).toBe('driver-789');
expect(payment.amount).toBe(100);
expect(payment.platformFee).toBe(10);
expect(payment.netAmount).toBe(90);
expect(payment.status).toBe(MemberPaymentStatus.PENDING);
expect(payment.dueDate).toEqual(new Date('2024-01-01'));
});
it('should support optional paidAt property', () => {
const payment: MemberPayment = {
id: 'payment-123',
feeId: 'fee-456',
driverId: 'driver-789',
amount: 100,
platformFee: 10,
netAmount: 90,
status: MemberPaymentStatus.PAID,
dueDate: new Date('2024-01-01'),
paidAt: new Date('2024-01-15'),
};
expect(payment.paidAt).toEqual(new Date('2024-01-15'));
});
});
describe('MemberPayment.rehydrate', () => {
it('should rehydrate a MemberPayment from props', () => {
const props: MemberPayment = {
id: 'payment-123',
feeId: 'fee-456',
driverId: 'driver-789',
amount: 100,
platformFee: 10,
netAmount: 90,
status: MemberPaymentStatus.PENDING,
dueDate: new Date('2024-01-01'),
};
const rehydrated = MemberPayment.rehydrate(props);
expect(rehydrated).toEqual(props);
expect(rehydrated.id).toBe('payment-123');
expect(rehydrated.feeId).toBe('fee-456');
expect(rehydrated.driverId).toBe('driver-789');
expect(rehydrated.amount).toBe(100);
expect(rehydrated.platformFee).toBe(10);
expect(rehydrated.netAmount).toBe(90);
expect(rehydrated.status).toBe(MemberPaymentStatus.PENDING);
expect(rehydrated.dueDate).toEqual(new Date('2024-01-01'));
});
it('should preserve optional paidAt when rehydrating', () => {
const props: MemberPayment = {
id: 'payment-123',
feeId: 'fee-456',
driverId: 'driver-789',
amount: 100,
platformFee: 10,
netAmount: 90,
status: MemberPaymentStatus.PAID,
dueDate: new Date('2024-01-01'),
paidAt: new Date('2024-01-15'),
};
const rehydrated = MemberPayment.rehydrate(props);
expect(rehydrated.paidAt).toEqual(new Date('2024-01-15'));
});
});
describe('Business rules and invariants', () => {
it('should calculate netAmount correctly (amount - platformFee)', () => {
const payment: MemberPayment = {
id: 'payment-123',
feeId: 'fee-456',
driverId: 'driver-789',
amount: 100,
platformFee: 10,
netAmount: 90,
status: MemberPaymentStatus.PENDING,
dueDate: new Date('2024-01-01'),
};
expect(payment.netAmount).toBe(payment.amount - payment.platformFee);
});
it('should support different payment statuses', () => {
const pendingPayment: MemberPayment = {
id: 'payment-123',
feeId: 'fee-456',
driverId: 'driver-789',
amount: 100,
platformFee: 10,
netAmount: 90,
status: MemberPaymentStatus.PENDING,
dueDate: new Date('2024-01-01'),
};
const paidPayment: MemberPayment = {
id: 'payment-124',
feeId: 'fee-456',
driverId: 'driver-789',
amount: 100,
platformFee: 10,
netAmount: 90,
status: MemberPaymentStatus.PAID,
dueDate: new Date('2024-01-01'),
paidAt: new Date('2024-01-15'),
};
const overduePayment: MemberPayment = {
id: 'payment-125',
feeId: 'fee-456',
driverId: 'driver-789',
amount: 100,
platformFee: 10,
netAmount: 90,
status: MemberPaymentStatus.OVERDUE,
dueDate: new Date('2024-01-01'),
};
expect(pendingPayment.status).toBe(MemberPaymentStatus.PENDING);
expect(paidPayment.status).toBe(MemberPaymentStatus.PAID);
expect(overduePayment.status).toBe(MemberPaymentStatus.OVERDUE);
});
it('should handle zero and negative amounts', () => {
const zeroPayment: MemberPayment = {
id: 'payment-123',
feeId: 'fee-456',
driverId: 'driver-789',
amount: 0,
platformFee: 0,
netAmount: 0,
status: MemberPaymentStatus.PENDING,
dueDate: new Date('2024-01-01'),
};
expect(zeroPayment.amount).toBe(0);
expect(zeroPayment.platformFee).toBe(0);
expect(zeroPayment.netAmount).toBe(0);
});
});
});

View File

@@ -1,8 +1,200 @@
import * as mod from '@core/payments/domain/entities/MembershipFee';
import {
MembershipFee,
MembershipFeeType,
} from '@core/payments/domain/entities/MembershipFee';
import { describe, expect, it } from 'vitest';
describe('payments/domain/entities/MembershipFee.ts', () => {
it('imports', () => {
expect(mod).toBeTruthy();
describe('payments/domain/entities/MembershipFee', () => {
describe('MembershipFeeType enum', () => {
it('should have correct fee type values', () => {
expect(MembershipFeeType.SEASON).toBe('season');
expect(MembershipFeeType.MONTHLY).toBe('monthly');
expect(MembershipFeeType.PER_RACE).toBe('per_race');
});
});
describe('MembershipFee interface', () => {
it('should have all required properties', () => {
const fee: MembershipFee = {
id: 'fee-123',
leagueId: 'league-456',
type: MembershipFeeType.SEASON,
amount: 100,
enabled: true,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
expect(fee.id).toBe('fee-123');
expect(fee.leagueId).toBe('league-456');
expect(fee.type).toBe(MembershipFeeType.SEASON);
expect(fee.amount).toBe(100);
expect(fee.enabled).toBe(true);
expect(fee.createdAt).toEqual(new Date('2024-01-01'));
expect(fee.updatedAt).toEqual(new Date('2024-01-01'));
});
it('should support optional seasonId property', () => {
const fee: MembershipFee = {
id: 'fee-123',
leagueId: 'league-456',
seasonId: 'season-789',
type: MembershipFeeType.SEASON,
amount: 100,
enabled: true,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
expect(fee.seasonId).toBe('season-789');
});
});
describe('MembershipFee.rehydrate', () => {
it('should rehydrate a MembershipFee from props', () => {
const props: MembershipFee = {
id: 'fee-123',
leagueId: 'league-456',
type: MembershipFeeType.SEASON,
amount: 100,
enabled: true,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
const rehydrated = MembershipFee.rehydrate(props);
expect(rehydrated).toEqual(props);
expect(rehydrated.id).toBe('fee-123');
expect(rehydrated.leagueId).toBe('league-456');
expect(rehydrated.type).toBe(MembershipFeeType.SEASON);
expect(rehydrated.amount).toBe(100);
expect(rehydrated.enabled).toBe(true);
expect(rehydrated.createdAt).toEqual(new Date('2024-01-01'));
expect(rehydrated.updatedAt).toEqual(new Date('2024-01-01'));
});
it('should preserve optional seasonId when rehydrating', () => {
const props: MembershipFee = {
id: 'fee-123',
leagueId: 'league-456',
seasonId: 'season-789',
type: MembershipFeeType.SEASON,
amount: 100,
enabled: true,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
const rehydrated = MembershipFee.rehydrate(props);
expect(rehydrated.seasonId).toBe('season-789');
});
});
describe('Business rules and invariants', () => {
it('should support different fee types', () => {
const seasonFee: MembershipFee = {
id: 'fee-123',
leagueId: 'league-456',
type: MembershipFeeType.SEASON,
amount: 100,
enabled: true,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
const monthlyFee: MembershipFee = {
id: 'fee-124',
leagueId: 'league-456',
type: MembershipFeeType.MONTHLY,
amount: 50,
enabled: true,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
const perRaceFee: MembershipFee = {
id: 'fee-125',
leagueId: 'league-456',
type: MembershipFeeType.PER_RACE,
amount: 10,
enabled: true,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
expect(seasonFee.type).toBe(MembershipFeeType.SEASON);
expect(monthlyFee.type).toBe(MembershipFeeType.MONTHLY);
expect(perRaceFee.type).toBe(MembershipFeeType.PER_RACE);
});
it('should handle enabled/disabled state', () => {
const enabledFee: MembershipFee = {
id: 'fee-123',
leagueId: 'league-456',
type: MembershipFeeType.SEASON,
amount: 100,
enabled: true,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
const disabledFee: MembershipFee = {
id: 'fee-124',
leagueId: 'league-456',
type: MembershipFeeType.SEASON,
amount: 0,
enabled: false,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
expect(enabledFee.enabled).toBe(true);
expect(disabledFee.enabled).toBe(false);
});
it('should handle zero and negative amounts', () => {
const zeroFee: MembershipFee = {
id: 'fee-123',
leagueId: 'league-456',
type: MembershipFeeType.SEASON,
amount: 0,
enabled: false,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
expect(zeroFee.amount).toBe(0);
expect(zeroFee.enabled).toBe(false);
});
it('should handle different league and season combinations', () => {
const leagueOnlyFee: MembershipFee = {
id: 'fee-123',
leagueId: 'league-456',
type: MembershipFeeType.MONTHLY,
amount: 50,
enabled: true,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
const leagueAndSeasonFee: MembershipFee = {
id: 'fee-124',
leagueId: 'league-456',
seasonId: 'season-789',
type: MembershipFeeType.SEASON,
amount: 100,
enabled: true,
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-01'),
};
expect(leagueOnlyFee.leagueId).toBe('league-456');
expect(leagueOnlyFee.seasonId).toBeUndefined();
expect(leagueAndSeasonFee.leagueId).toBe('league-456');
expect(leagueAndSeasonFee.seasonId).toBe('season-789');
});
});
});

View File

@@ -1,8 +1,311 @@
import * as mod from '@core/payments/domain/entities/Payment';
import {
Payment,
PaymentStatus,
PaymentType,
PayerType,
} from '@core/payments/domain/entities/Payment';
import { describe, expect, it } from 'vitest';
describe('payments/domain/entities/Payment.ts', () => {
it('imports', () => {
expect(mod).toBeTruthy();
describe('payments/domain/entities/Payment', () => {
describe('PaymentType enum', () => {
it('should have correct payment type values', () => {
expect(PaymentType.SPONSORSHIP).toBe('sponsorship');
expect(PaymentType.MEMBERSHIP_FEE).toBe('membership_fee');
});
});
describe('PayerType enum', () => {
it('should have correct payer type values', () => {
expect(PayerType.SPONSOR).toBe('sponsor');
expect(PayerType.DRIVER).toBe('driver');
});
});
describe('PaymentStatus enum', () => {
it('should have correct status values', () => {
expect(PaymentStatus.PENDING).toBe('pending');
expect(PaymentStatus.COMPLETED).toBe('completed');
expect(PaymentStatus.FAILED).toBe('failed');
expect(PaymentStatus.REFUNDED).toBe('refunded');
});
});
describe('Payment interface', () => {
it('should have all required properties', () => {
const payment: Payment = {
id: 'payment-123',
type: PaymentType.SPONSORSHIP,
amount: 1000,
platformFee: 50,
netAmount: 950,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.PENDING,
createdAt: new Date('2024-01-01'),
};
expect(payment.id).toBe('payment-123');
expect(payment.type).toBe(PaymentType.SPONSORSHIP);
expect(payment.amount).toBe(1000);
expect(payment.platformFee).toBe(50);
expect(payment.netAmount).toBe(950);
expect(payment.payerId).toBe('sponsor-456');
expect(payment.payerType).toBe(PayerType.SPONSOR);
expect(payment.leagueId).toBe('league-789');
expect(payment.status).toBe(PaymentStatus.PENDING);
expect(payment.createdAt).toEqual(new Date('2024-01-01'));
});
it('should support optional seasonId property', () => {
const payment: Payment = {
id: 'payment-123',
type: PaymentType.MEMBERSHIP_FEE,
amount: 100,
platformFee: 5,
netAmount: 95,
payerId: 'driver-456',
payerType: PayerType.DRIVER,
leagueId: 'league-789',
seasonId: 'season-999',
status: PaymentStatus.COMPLETED,
createdAt: new Date('2024-01-01'),
completedAt: new Date('2024-01-15'),
};
expect(payment.seasonId).toBe('season-999');
expect(payment.completedAt).toEqual(new Date('2024-01-15'));
});
it('should support optional completedAt property', () => {
const payment: Payment = {
id: 'payment-123',
type: PaymentType.SPONSORSHIP,
amount: 1000,
platformFee: 50,
netAmount: 950,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.COMPLETED,
createdAt: new Date('2024-01-01'),
completedAt: new Date('2024-01-15'),
};
expect(payment.completedAt).toEqual(new Date('2024-01-15'));
});
});
describe('Payment.rehydrate', () => {
it('should rehydrate a Payment from props', () => {
const props: Payment = {
id: 'payment-123',
type: PaymentType.SPONSORSHIP,
amount: 1000,
platformFee: 50,
netAmount: 950,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.PENDING,
createdAt: new Date('2024-01-01'),
};
const rehydrated = Payment.rehydrate(props);
expect(rehydrated).toEqual(props);
expect(rehydrated.id).toBe('payment-123');
expect(rehydrated.type).toBe(PaymentType.SPONSORSHIP);
expect(rehydrated.amount).toBe(1000);
expect(rehydrated.platformFee).toBe(50);
expect(rehydrated.netAmount).toBe(950);
expect(rehydrated.payerId).toBe('sponsor-456');
expect(rehydrated.payerType).toBe(PayerType.SPONSOR);
expect(rehydrated.leagueId).toBe('league-789');
expect(rehydrated.status).toBe(PaymentStatus.PENDING);
expect(rehydrated.createdAt).toEqual(new Date('2024-01-01'));
});
it('should preserve optional seasonId when rehydrating', () => {
const props: Payment = {
id: 'payment-123',
type: PaymentType.MEMBERSHIP_FEE,
amount: 100,
platformFee: 5,
netAmount: 95,
payerId: 'driver-456',
payerType: PayerType.DRIVER,
leagueId: 'league-789',
seasonId: 'season-999',
status: PaymentStatus.COMPLETED,
createdAt: new Date('2024-01-01'),
completedAt: new Date('2024-01-15'),
};
const rehydrated = Payment.rehydrate(props);
expect(rehydrated.seasonId).toBe('season-999');
expect(rehydrated.completedAt).toEqual(new Date('2024-01-15'));
});
});
describe('Business rules and invariants', () => {
it('should calculate netAmount correctly (amount - platformFee)', () => {
const payment: Payment = {
id: 'payment-123',
type: PaymentType.SPONSORSHIP,
amount: 1000,
platformFee: 50,
netAmount: 950,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.PENDING,
createdAt: new Date('2024-01-01'),
};
expect(payment.netAmount).toBe(payment.amount - payment.platformFee);
});
it('should support different payment types', () => {
const sponsorshipPayment: Payment = {
id: 'payment-123',
type: PaymentType.SPONSORSHIP,
amount: 1000,
platformFee: 50,
netAmount: 950,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.PENDING,
createdAt: new Date('2024-01-01'),
};
const membershipFeePayment: Payment = {
id: 'payment-124',
type: PaymentType.MEMBERSHIP_FEE,
amount: 100,
platformFee: 5,
netAmount: 95,
payerId: 'driver-456',
payerType: PayerType.DRIVER,
leagueId: 'league-789',
status: PaymentStatus.COMPLETED,
createdAt: new Date('2024-01-01'),
};
expect(sponsorshipPayment.type).toBe(PaymentType.SPONSORSHIP);
expect(membershipFeePayment.type).toBe(PaymentType.MEMBERSHIP_FEE);
});
it('should support different payer types', () => {
const sponsorPayment: Payment = {
id: 'payment-123',
type: PaymentType.SPONSORSHIP,
amount: 1000,
platformFee: 50,
netAmount: 950,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.PENDING,
createdAt: new Date('2024-01-01'),
};
const driverPayment: Payment = {
id: 'payment-124',
type: PaymentType.MEMBERSHIP_FEE,
amount: 100,
platformFee: 5,
netAmount: 95,
payerId: 'driver-456',
payerType: PayerType.DRIVER,
leagueId: 'league-789',
status: PaymentStatus.COMPLETED,
createdAt: new Date('2024-01-01'),
};
expect(sponsorPayment.payerType).toBe(PayerType.SPONSOR);
expect(driverPayment.payerType).toBe(PayerType.DRIVER);
});
it('should support different payment statuses', () => {
const pendingPayment: Payment = {
id: 'payment-123',
type: PaymentType.SPONSORSHIP,
amount: 1000,
platformFee: 50,
netAmount: 950,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.PENDING,
createdAt: new Date('2024-01-01'),
};
const completedPayment: Payment = {
id: 'payment-124',
type: PaymentType.SPONSORSHIP,
amount: 1000,
platformFee: 50,
netAmount: 950,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.COMPLETED,
createdAt: new Date('2024-01-01'),
completedAt: new Date('2024-01-15'),
};
const failedPayment: Payment = {
id: 'payment-125',
type: PaymentType.SPONSORSHIP,
amount: 1000,
platformFee: 50,
netAmount: 950,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.FAILED,
createdAt: new Date('2024-01-01'),
};
const refundedPayment: Payment = {
id: 'payment-126',
type: PaymentType.SPONSORSHIP,
amount: 1000,
platformFee: 50,
netAmount: 950,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.REFUNDED,
createdAt: new Date('2024-01-01'),
};
expect(pendingPayment.status).toBe(PaymentStatus.PENDING);
expect(completedPayment.status).toBe(PaymentStatus.COMPLETED);
expect(failedPayment.status).toBe(PaymentStatus.FAILED);
expect(refundedPayment.status).toBe(PaymentStatus.REFUNDED);
});
it('should handle zero and negative amounts', () => {
const zeroPayment: Payment = {
id: 'payment-123',
type: PaymentType.SPONSORSHIP,
amount: 0,
platformFee: 0,
netAmount: 0,
payerId: 'sponsor-456',
payerType: PayerType.SPONSOR,
leagueId: 'league-789',
status: PaymentStatus.PENDING,
createdAt: new Date('2024-01-01'),
};
expect(zeroPayment.amount).toBe(0);
expect(zeroPayment.platformFee).toBe(0);
expect(zeroPayment.netAmount).toBe(0);
});
});
});

View File

@@ -1,8 +1,298 @@
import * as mod from '@core/payments/domain/entities/Prize';
import { Prize, PrizeType } from '@core/payments/domain/entities/Prize';
import { describe, expect, it } from 'vitest';
describe('payments/domain/entities/Prize.ts', () => {
it('imports', () => {
expect(mod).toBeTruthy();
describe('payments/domain/entities/Prize', () => {
describe('PrizeType enum', () => {
it('should have correct prize type values', () => {
expect(PrizeType.CASH).toBe('cash');
expect(PrizeType.MERCHANDISE).toBe('merchandise');
expect(PrizeType.OTHER).toBe('other');
});
});
describe('Prize interface', () => {
it('should have all required properties', () => {
const prize: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
awarded: false,
createdAt: new Date('2024-01-01'),
};
expect(prize.id).toBe('prize-123');
expect(prize.leagueId).toBe('league-456');
expect(prize.seasonId).toBe('season-789');
expect(prize.position).toBe(1);
expect(prize.name).toBe('Champion Prize');
expect(prize.amount).toBe(1000);
expect(prize.type).toBe(PrizeType.CASH);
expect(prize.awarded).toBe(false);
expect(prize.createdAt).toEqual(new Date('2024-01-01'));
});
it('should support optional description property', () => {
const prize: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
description: 'Awarded to the champion of the season',
awarded: false,
createdAt: new Date('2024-01-01'),
};
expect(prize.description).toBe('Awarded to the champion of the season');
});
it('should support optional awardedTo and awardedAt properties', () => {
const prize: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
awarded: true,
awardedTo: 'driver-999',
awardedAt: new Date('2024-06-01'),
createdAt: new Date('2024-01-01'),
};
expect(prize.awardedTo).toBe('driver-999');
expect(prize.awardedAt).toEqual(new Date('2024-06-01'));
});
});
describe('Prize.rehydrate', () => {
it('should rehydrate a Prize from props', () => {
const props: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
awarded: false,
createdAt: new Date('2024-01-01'),
};
const rehydrated = Prize.rehydrate(props);
expect(rehydrated).toEqual(props);
expect(rehydrated.id).toBe('prize-123');
expect(rehydrated.leagueId).toBe('league-456');
expect(rehydrated.seasonId).toBe('season-789');
expect(rehydrated.position).toBe(1);
expect(rehydrated.name).toBe('Champion Prize');
expect(rehydrated.amount).toBe(1000);
expect(rehydrated.type).toBe(PrizeType.CASH);
expect(rehydrated.awarded).toBe(false);
expect(rehydrated.createdAt).toEqual(new Date('2024-01-01'));
});
it('should preserve optional description when rehydrating', () => {
const props: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
description: 'Awarded to the champion of the season',
awarded: false,
createdAt: new Date('2024-01-01'),
};
const rehydrated = Prize.rehydrate(props);
expect(rehydrated.description).toBe('Awarded to the champion of the season');
});
it('should preserve optional awardedTo and awardedAt when rehydrating', () => {
const props: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
awarded: true,
awardedTo: 'driver-999',
awardedAt: new Date('2024-06-01'),
createdAt: new Date('2024-01-01'),
};
const rehydrated = Prize.rehydrate(props);
expect(rehydrated.awardedTo).toBe('driver-999');
expect(rehydrated.awardedAt).toEqual(new Date('2024-06-01'));
});
});
describe('Business rules and invariants', () => {
it('should support different prize types', () => {
const cashPrize: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
awarded: false,
createdAt: new Date('2024-01-01'),
};
const merchandisePrize: Prize = {
id: 'prize-124',
leagueId: 'league-456',
seasonId: 'season-789',
position: 2,
name: 'T-Shirt',
amount: 50,
type: PrizeType.MERCHANDISE,
awarded: false,
createdAt: new Date('2024-01-01'),
};
const otherPrize: Prize = {
id: 'prize-125',
leagueId: 'league-456',
seasonId: 'season-789',
position: 3,
name: 'Special Recognition',
amount: 0,
type: PrizeType.OTHER,
awarded: false,
createdAt: new Date('2024-01-01'),
};
expect(cashPrize.type).toBe(PrizeType.CASH);
expect(merchandisePrize.type).toBe(PrizeType.MERCHANDISE);
expect(otherPrize.type).toBe(PrizeType.OTHER);
});
it('should handle awarded and unawarded prizes', () => {
const unawardedPrize: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
awarded: false,
createdAt: new Date('2024-01-01'),
};
const awardedPrize: Prize = {
id: 'prize-124',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
awarded: true,
awardedTo: 'driver-999',
awardedAt: new Date('2024-06-01'),
createdAt: new Date('2024-01-01'),
};
expect(unawardedPrize.awarded).toBe(false);
expect(unawardedPrize.awardedTo).toBeUndefined();
expect(unawardedPrize.awardedAt).toBeUndefined();
expect(awardedPrize.awarded).toBe(true);
expect(awardedPrize.awardedTo).toBe('driver-999');
expect(awardedPrize.awardedAt).toEqual(new Date('2024-06-01'));
});
it('should handle different positions', () => {
const firstPlacePrize: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
awarded: false,
createdAt: new Date('2024-01-01'),
};
const secondPlacePrize: Prize = {
id: 'prize-124',
leagueId: 'league-456',
seasonId: 'season-789',
position: 2,
name: 'Runner-Up Prize',
amount: 500,
type: PrizeType.CASH,
awarded: false,
createdAt: new Date('2024-01-01'),
};
const thirdPlacePrize: Prize = {
id: 'prize-125',
leagueId: 'league-456',
seasonId: 'season-789',
position: 3,
name: 'Third Place Prize',
amount: 250,
type: PrizeType.CASH,
awarded: false,
createdAt: new Date('2024-01-01'),
};
expect(firstPlacePrize.position).toBe(1);
expect(secondPlacePrize.position).toBe(2);
expect(thirdPlacePrize.position).toBe(3);
});
it('should handle zero and negative amounts', () => {
const zeroPrize: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Participation Prize',
amount: 0,
type: PrizeType.OTHER,
awarded: false,
createdAt: new Date('2024-01-01'),
};
expect(zeroPrize.amount).toBe(0);
});
it('should handle different league and season combinations', () => {
const leagueOnlyPrize: Prize = {
id: 'prize-123',
leagueId: 'league-456',
seasonId: 'season-789',
position: 1,
name: 'Champion Prize',
amount: 1000,
type: PrizeType.CASH,
awarded: false,
createdAt: new Date('2024-01-01'),
};
expect(leagueOnlyPrize.leagueId).toBe('league-456');
expect(leagueOnlyPrize.seasonId).toBe('season-789');
});
});
});

View File

@@ -1,8 +1,284 @@
import * as mod from '@core/payments/domain/entities/Wallet';
import {
ReferenceType,
Transaction,
TransactionType,
Wallet,
} from '@core/payments/domain/entities/Wallet';
import { describe, expect, it } from 'vitest';
describe('payments/domain/entities/Wallet.ts', () => {
it('imports', () => {
expect(mod).toBeTruthy();
describe('payments/domain/entities/Wallet', () => {
describe('TransactionType enum', () => {
it('should have correct transaction type values', () => {
expect(TransactionType.DEPOSIT).toBe('deposit');
expect(TransactionType.WITHDRAWAL).toBe('withdrawal');
expect(TransactionType.PLATFORM_FEE).toBe('platform_fee');
});
});
describe('ReferenceType enum', () => {
it('should have correct reference type values', () => {
expect(ReferenceType.SPONSORSHIP).toBe('sponsorship');
expect(ReferenceType.MEMBERSHIP_FEE).toBe('membership_fee');
expect(ReferenceType.PRIZE).toBe('prize');
});
});
describe('Wallet interface', () => {
it('should have all required properties', () => {
const wallet: Wallet = {
id: 'wallet-123',
leagueId: 'league-456',
balance: 1000,
totalRevenue: 5000,
totalPlatformFees: 250,
totalWithdrawn: 3750,
currency: 'USD',
createdAt: new Date('2024-01-01'),
};
expect(wallet.id).toBe('wallet-123');
expect(wallet.leagueId).toBe('league-456');
expect(wallet.balance).toBe(1000);
expect(wallet.totalRevenue).toBe(5000);
expect(wallet.totalPlatformFees).toBe(250);
expect(wallet.totalWithdrawn).toBe(3750);
expect(wallet.currency).toBe('USD');
expect(wallet.createdAt).toEqual(new Date('2024-01-01'));
});
});
describe('Wallet.rehydrate', () => {
it('should rehydrate a Wallet from props', () => {
const props: Wallet = {
id: 'wallet-123',
leagueId: 'league-456',
balance: 1000,
totalRevenue: 5000,
totalPlatformFees: 250,
totalWithdrawn: 3750,
currency: 'USD',
createdAt: new Date('2024-01-01'),
};
const rehydrated = Wallet.rehydrate(props);
expect(rehydrated).toEqual(props);
expect(rehydrated.id).toBe('wallet-123');
expect(rehydrated.leagueId).toBe('league-456');
expect(rehydrated.balance).toBe(1000);
expect(rehydrated.totalRevenue).toBe(5000);
expect(rehydrated.totalPlatformFees).toBe(250);
expect(rehydrated.totalWithdrawn).toBe(3750);
expect(rehydrated.currency).toBe('USD');
expect(rehydrated.createdAt).toEqual(new Date('2024-01-01'));
});
});
describe('Transaction interface', () => {
it('should have all required properties', () => {
const transaction: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
createdAt: new Date('2024-01-01'),
};
expect(transaction.id).toBe('txn-123');
expect(transaction.walletId).toBe('wallet-456');
expect(transaction.type).toBe(TransactionType.DEPOSIT);
expect(transaction.amount).toBe(1000);
expect(transaction.description).toBe('Sponsorship payment');
expect(transaction.createdAt).toEqual(new Date('2024-01-01'));
});
it('should support optional referenceId and referenceType properties', () => {
const transaction: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
referenceId: 'payment-789',
referenceType: ReferenceType.SPONSORSHIP,
createdAt: new Date('2024-01-01'),
};
expect(transaction.referenceId).toBe('payment-789');
expect(transaction.referenceType).toBe(ReferenceType.SPONSORSHIP);
});
});
describe('Transaction.rehydrate', () => {
it('should rehydrate a Transaction from props', () => {
const props: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
createdAt: new Date('2024-01-01'),
};
const rehydrated = Transaction.rehydrate(props);
expect(rehydrated).toEqual(props);
expect(rehydrated.id).toBe('txn-123');
expect(rehydrated.walletId).toBe('wallet-456');
expect(rehydrated.type).toBe(TransactionType.DEPOSIT);
expect(rehydrated.amount).toBe(1000);
expect(rehydrated.description).toBe('Sponsorship payment');
expect(rehydrated.createdAt).toEqual(new Date('2024-01-01'));
});
it('should preserve optional referenceId and referenceType when rehydrating', () => {
const props: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
referenceId: 'payment-789',
referenceType: ReferenceType.SPONSORSHIP,
createdAt: new Date('2024-01-01'),
};
const rehydrated = Transaction.rehydrate(props);
expect(rehydrated.referenceId).toBe('payment-789');
expect(rehydrated.referenceType).toBe(ReferenceType.SPONSORSHIP);
});
});
describe('Business rules and invariants', () => {
it('should calculate balance correctly', () => {
const wallet: Wallet = {
id: 'wallet-123',
leagueId: 'league-456',
balance: 1000,
totalRevenue: 5000,
totalPlatformFees: 250,
totalWithdrawn: 3750,
currency: 'USD',
createdAt: new Date('2024-01-01'),
};
// Balance should be: totalRevenue - totalPlatformFees - totalWithdrawn
const expectedBalance = wallet.totalRevenue - wallet.totalPlatformFees - wallet.totalWithdrawn;
expect(wallet.balance).toBe(expectedBalance);
});
it('should support different transaction types', () => {
const depositTransaction: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
createdAt: new Date('2024-01-01'),
};
const withdrawalTransaction: Transaction = {
id: 'txn-124',
walletId: 'wallet-456',
type: TransactionType.WITHDRAWAL,
amount: 500,
description: 'Withdrawal to bank',
createdAt: new Date('2024-01-01'),
};
const platformFeeTransaction: Transaction = {
id: 'txn-125',
walletId: 'wallet-456',
type: TransactionType.PLATFORM_FEE,
amount: 50,
description: 'Platform fee deduction',
createdAt: new Date('2024-01-01'),
};
expect(depositTransaction.type).toBe(TransactionType.DEPOSIT);
expect(withdrawalTransaction.type).toBe(TransactionType.WITHDRAWAL);
expect(platformFeeTransaction.type).toBe(TransactionType.PLATFORM_FEE);
});
it('should support different reference types', () => {
const sponsorshipTransaction: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
referenceId: 'payment-789',
referenceType: ReferenceType.SPONSORSHIP,
createdAt: new Date('2024-01-01'),
};
const membershipFeeTransaction: Transaction = {
id: 'txn-124',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 100,
description: 'Membership fee payment',
referenceId: 'payment-790',
referenceType: ReferenceType.MEMBERSHIP_FEE,
createdAt: new Date('2024-01-01'),
};
const prizeTransaction: Transaction = {
id: 'txn-125',
walletId: 'wallet-456',
type: TransactionType.WITHDRAWAL,
amount: 500,
description: 'Prize payout',
referenceId: 'prize-791',
referenceType: ReferenceType.PRIZE,
createdAt: new Date('2024-01-01'),
};
expect(sponsorshipTransaction.referenceType).toBe(ReferenceType.SPONSORSHIP);
expect(membershipFeeTransaction.referenceType).toBe(ReferenceType.MEMBERSHIP_FEE);
expect(prizeTransaction.referenceType).toBe(ReferenceType.PRIZE);
});
it('should handle zero and negative amounts', () => {
const zeroTransaction: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 0,
description: 'Zero amount transaction',
createdAt: new Date('2024-01-01'),
};
expect(zeroTransaction.amount).toBe(0);
});
it('should handle different currencies', () => {
const usdWallet: Wallet = {
id: 'wallet-123',
leagueId: 'league-456',
balance: 1000,
totalRevenue: 5000,
totalPlatformFees: 250,
totalWithdrawn: 3750,
currency: 'USD',
createdAt: new Date('2024-01-01'),
};
const eurWallet: Wallet = {
id: 'wallet-124',
leagueId: 'league-457',
balance: 1000,
totalRevenue: 5000,
totalPlatformFees: 250,
totalWithdrawn: 3750,
currency: 'EUR',
createdAt: new Date('2024-01-01'),
};
expect(usdWallet.currency).toBe('USD');
expect(eurWallet.currency).toBe('EUR');
});
});
});

View File

@@ -0,0 +1,501 @@
/**
* Comprehensive Tests for MediaResolverPort
*
* Tests cover:
* - Interface contract compliance
* - ResolutionStrategies for all reference types
* - resolveWithDefaults helper function
* - isMediaResolverPort type guard
* - Edge cases and error handling
* - Business logic decisions
*/
import { MediaReference } from '@core/domain/media/MediaReference';
import { describe, expect, it } from 'vitest';
import {
MediaResolverPort,
ResolutionStrategies,
resolveWithDefaults,
isMediaResolverPort,
} from './MediaResolverPort';
describe('MediaResolverPort - Comprehensive Tests', () => {
describe('Interface Contract Compliance', () => {
it('should define resolve method signature correctly', () => {
// Verify the interface has the correct method signature
const testInterface: MediaResolverPort = {
resolve: async (ref: MediaReference): Promise<string | null> => {
return null;
},
};
expect(testInterface).toBeDefined();
expect(typeof testInterface.resolve).toBe('function');
});
it('should accept MediaReference and return Promise<string | null>', async () => {
const mockResolver: MediaResolverPort = {
resolve: async (ref: MediaReference): Promise<string | null> => {
// Verify ref is a MediaReference instance
expect(ref).toBeInstanceOf(MediaReference);
return '/test/path';
},
};
const ref = MediaReference.createSystemDefault('avatar');
const result = await mockResolver.resolve(ref);
expect(result).toBe('/test/path');
});
});
describe('ResolutionStrategies - System Default', () => {
it('should resolve system-default avatar without variant', () => {
const ref = MediaReference.createSystemDefault('avatar');
const result = ResolutionStrategies.systemDefault(ref);
expect(result).toBe('/media/default/neutral-default-avatar.png');
});
it('should resolve system-default avatar with male variant', () => {
const ref = MediaReference.createSystemDefault('avatar', 'male');
const result = ResolutionStrategies.systemDefault(ref);
expect(result).toBe('/media/default/male-default-avatar.png');
});
it('should resolve system-default avatar with female variant', () => {
const ref = MediaReference.createSystemDefault('avatar', 'female');
const result = ResolutionStrategies.systemDefault(ref);
expect(result).toBe('/media/default/female-default-avatar.png');
});
it('should resolve system-default avatar with neutral variant', () => {
const ref = MediaReference.createSystemDefault('avatar', 'neutral');
const result = ResolutionStrategies.systemDefault(ref);
expect(result).toBe('/media/default/neutral-default-avatar.png');
});
it('should resolve system-default logo', () => {
const ref = MediaReference.createSystemDefault('logo');
const result = ResolutionStrategies.systemDefault(ref);
expect(result).toBe('/media/default/logo.png');
});
it('should return null for non-system-default reference', () => {
const ref = MediaReference.createGenerated('team-123');
const result = ResolutionStrategies.systemDefault(ref);
expect(result).toBeNull();
});
});
describe('ResolutionStrategies - Generated', () => {
it('should resolve generated reference for team', () => {
const ref = MediaReference.createGenerated('team-123');
const result = ResolutionStrategies.generated(ref);
expect(result).toBe('/media/teams/123/logo');
});
it('should resolve generated reference for league', () => {
const ref = MediaReference.createGenerated('league-456');
const result = ResolutionStrategies.generated(ref);
expect(result).toBe('/media/leagues/456/logo');
});
it('should resolve generated reference for driver', () => {
const ref = MediaReference.createGenerated('driver-789');
const result = ResolutionStrategies.generated(ref);
expect(result).toBe('/media/avatar/789');
});
it('should resolve generated reference for unknown type', () => {
const ref = MediaReference.createGenerated('unknown-999');
const result = ResolutionStrategies.generated(ref);
expect(result).toBe('/media/generated/unknown/999');
});
it('should return null for generated reference without generationRequestId', () => {
// Create a reference with missing generationRequestId
const ref = MediaReference.createGenerated('valid-id');
// Manually create an invalid reference
const invalidRef = { type: 'generated' } as MediaReference;
const result = ResolutionStrategies.generated(invalidRef);
expect(result).toBeNull();
});
it('should return null for non-generated reference', () => {
const ref = MediaReference.createSystemDefault('avatar');
const result = ResolutionStrategies.generated(ref);
expect(result).toBeNull();
});
it('should handle generated reference with special characters in ID', () => {
const ref = MediaReference.createGenerated('team-abc-123_XYZ');
const result = ResolutionStrategies.generated(ref);
expect(result).toBe('/media/teams/abc-123_XYZ/logo');
});
it('should handle generated reference with multiple hyphens', () => {
const ref = MediaReference.createGenerated('team-abc-def-123');
const result = ResolutionStrategies.generated(ref);
expect(result).toBe('/media/teams/abc-def-123/logo');
});
});
describe('ResolutionStrategies - Uploaded', () => {
it('should resolve uploaded reference', () => {
const ref = MediaReference.createUploaded('media-456');
const result = ResolutionStrategies.uploaded(ref);
expect(result).toBe('/media/uploaded/media-456');
});
it('should return null for uploaded reference without mediaId', () => {
// Create a reference with missing mediaId
const ref = MediaReference.createUploaded('valid-id');
// Manually create an invalid reference
const invalidRef = { type: 'uploaded' } as MediaReference;
const result = ResolutionStrategies.uploaded(invalidRef);
expect(result).toBeNull();
});
it('should return null for non-uploaded reference', () => {
const ref = MediaReference.createSystemDefault('avatar');
const result = ResolutionStrategies.uploaded(ref);
expect(result).toBeNull();
});
it('should handle uploaded reference with special characters', () => {
const ref = MediaReference.createUploaded('media-abc-123_XYZ');
const result = ResolutionStrategies.uploaded(ref);
expect(result).toBe('/media/uploaded/media-abc-123_XYZ');
});
it('should handle uploaded reference with very long ID', () => {
const longId = 'a'.repeat(1000);
const ref = MediaReference.createUploaded(longId);
const result = ResolutionStrategies.uploaded(ref);
expect(result).toBe(`/media/uploaded/${longId}`);
});
});
describe('ResolutionStrategies - None', () => {
it('should return null for none reference', () => {
const ref = MediaReference.createNone();
const result = ResolutionStrategies.none(ref);
expect(result).toBeNull();
});
it('should return null for any reference passed to none strategy', () => {
const ref = MediaReference.createSystemDefault('avatar');
const result = ResolutionStrategies.none(ref);
expect(result).toBeNull();
});
});
describe('resolveWithDefaults - Integration Tests', () => {
it('should resolve system-default reference using resolveWithDefaults', () => {
const ref = MediaReference.createSystemDefault('avatar');
const result = resolveWithDefaults(ref);
expect(result).toBe('/media/default/neutral-default-avatar.png');
});
it('should resolve system-default avatar with male variant using resolveWithDefaults', () => {
const ref = MediaReference.createSystemDefault('avatar', 'male');
const result = resolveWithDefaults(ref);
expect(result).toBe('/media/default/male-default-avatar.png');
});
it('should resolve system-default logo using resolveWithDefaults', () => {
const ref = MediaReference.createSystemDefault('logo');
const result = resolveWithDefaults(ref);
expect(result).toBe('/media/default/logo.png');
});
it('should resolve generated reference using resolveWithDefaults', () => {
const ref = MediaReference.createGenerated('team-123');
const result = resolveWithDefaults(ref);
expect(result).toBe('/media/teams/123/logo');
});
it('should resolve uploaded reference using resolveWithDefaults', () => {
const ref = MediaReference.createUploaded('media-456');
const result = resolveWithDefaults(ref);
expect(result).toBe('/media/uploaded/media-456');
});
it('should resolve none reference using resolveWithDefaults', () => {
const ref = MediaReference.createNone();
const result = resolveWithDefaults(ref);
expect(result).toBeNull();
});
it('should handle all reference types in sequence', () => {
const refs = [
MediaReference.createSystemDefault('avatar'),
MediaReference.createSystemDefault('avatar', 'male'),
MediaReference.createSystemDefault('logo'),
MediaReference.createGenerated('team-123'),
MediaReference.createGenerated('league-456'),
MediaReference.createGenerated('driver-789'),
MediaReference.createUploaded('media-456'),
MediaReference.createNone(),
];
const results = refs.map(ref => resolveWithDefaults(ref));
expect(results).toEqual([
'/media/default/neutral-default-avatar.png',
'/media/default/male-default-avatar.png',
'/media/default/logo.png',
'/media/teams/123/logo',
'/media/leagues/456/logo',
'/media/avatar/789',
'/media/uploaded/media-456',
null,
]);
});
});
describe('isMediaResolverPort Type Guard', () => {
it('should return true for valid MediaResolverPort implementation', () => {
const validResolver: MediaResolverPort = {
resolve: async (ref: MediaReference): Promise<string | null> => {
return '/test/path';
},
};
expect(isMediaResolverPort(validResolver)).toBe(true);
});
it('should return false for null', () => {
expect(isMediaResolverPort(null)).toBe(false);
});
it('should return false for undefined', () => {
expect(isMediaResolverPort(undefined)).toBe(false);
});
it('should return false for non-object', () => {
expect(isMediaResolverPort('string')).toBe(false);
expect(isMediaResolverPort(123)).toBe(false);
expect(isMediaResolverPort(true)).toBe(false);
});
it('should return false for object without resolve method', () => {
const invalidResolver = {
someOtherMethod: () => {},
};
expect(isMediaResolverPort(invalidResolver)).toBe(false);
});
it('should return false for object with resolve property but not a function', () => {
const invalidResolver = {
resolve: 'not a function',
};
expect(isMediaResolverPort(invalidResolver)).toBe(false);
});
it('should return false for object with resolve as non-function property', () => {
const invalidResolver = {
resolve: 123,
};
expect(isMediaResolverPort(invalidResolver)).toBe(false);
});
it('should return true for object with resolve method and other properties', () => {
const validResolver = {
resolve: async (ref: MediaReference): Promise<string | null> => {
return '/test/path';
},
extraProperty: 'value',
anotherMethod: () => {},
};
expect(isMediaResolverPort(validResolver)).toBe(true);
});
});
describe('Business Logic Decisions', () => {
it('should make correct decision for system-default avatar without variant', () => {
const ref = MediaReference.createSystemDefault('avatar');
const result = resolveWithDefaults(ref);
// Decision: Should use neutral default avatar
expect(result).toBe('/media/default/neutral-default-avatar.png');
});
it('should make correct decision for system-default avatar with specific variant', () => {
const ref = MediaReference.createSystemDefault('avatar', 'female');
const result = resolveWithDefaults(ref);
// Decision: Should use the specified variant
expect(result).toBe('/media/default/female-default-avatar.png');
});
it('should make correct decision for generated team reference', () => {
const ref = MediaReference.createGenerated('team-123');
const result = resolveWithDefaults(ref);
// Decision: Should resolve to team logo path
expect(result).toBe('/media/teams/123/logo');
});
it('should make correct decision for generated league reference', () => {
const ref = MediaReference.createGenerated('league-456');
const result = resolveWithDefaults(ref);
// Decision: Should resolve to league logo path
expect(result).toBe('/media/leagues/456/logo');
});
it('should make correct decision for generated driver reference', () => {
const ref = MediaReference.createGenerated('driver-789');
const result = resolveWithDefaults(ref);
// Decision: Should resolve to avatar path
expect(result).toBe('/media/avatar/789');
});
it('should make correct decision for uploaded reference', () => {
const ref = MediaReference.createUploaded('media-456');
const result = resolveWithDefaults(ref);
// Decision: Should resolve to uploaded media path
expect(result).toBe('/media/uploaded/media-456');
});
it('should make correct decision for none reference', () => {
const ref = MediaReference.createNone();
const result = resolveWithDefaults(ref);
// Decision: Should return null (no media)
expect(result).toBeNull();
});
it('should make correct decision for unknown generated type', () => {
const ref = MediaReference.createGenerated('unknown-999');
const result = resolveWithDefaults(ref);
// Decision: Should fall back to generic generated path
expect(result).toBe('/media/generated/unknown/999');
});
});
describe('Edge Cases and Error Handling', () => {
it('should handle empty string IDs gracefully', () => {
// MediaReference factory methods throw on empty strings
// This tests that the strategies handle invalid refs gracefully
const invalidRef = { type: 'generated' } as MediaReference;
const result = ResolutionStrategies.generated(invalidRef);
expect(result).toBeNull();
});
it('should handle references with missing properties', () => {
const invalidRef = { type: 'uploaded' } as MediaReference;
const result = ResolutionStrategies.uploaded(invalidRef);
expect(result).toBeNull();
});
it('should handle very long IDs without performance issues', () => {
const longId = 'a'.repeat(10000);
const ref = MediaReference.createUploaded(longId);
const result = resolveWithDefaults(ref);
expect(result).toBe(`/media/uploaded/${longId}`);
});
it('should handle Unicode characters in IDs', () => {
const ref = MediaReference.createUploaded('media-日本語-123');
const result = resolveWithDefaults(ref);
expect(result).toBe('/media/uploaded/media-日本語-123');
});
it('should handle special characters in generated IDs', () => {
const ref = MediaReference.createGenerated('team-abc_def-123');
const result = resolveWithDefaults(ref);
expect(result).toBe('/media/teams/abc_def-123/logo');
});
});
describe('Path Format Consistency', () => {
it('should maintain consistent path format for system-default', () => {
const ref = MediaReference.createSystemDefault('avatar');
const result = resolveWithDefaults(ref);
// Should start with /media/default/
expect(result).toMatch(/^\/media\/default\//);
});
it('should maintain consistent path format for generated team', () => {
const ref = MediaReference.createGenerated('team-123');
const result = resolveWithDefaults(ref);
// Should start with /media/teams/
expect(result).toMatch(/^\/media\/teams\//);
});
it('should maintain consistent path format for generated league', () => {
const ref = MediaReference.createGenerated('league-456');
const result = resolveWithDefaults(ref);
// Should start with /media/leagues/
expect(result).toMatch(/^\/media\/leagues\//);
});
it('should maintain consistent path format for generated driver', () => {
const ref = MediaReference.createGenerated('driver-789');
const result = resolveWithDefaults(ref);
// Should start with /media/avatar/
expect(result).toMatch(/^\/media\/avatar\//);
});
it('should maintain consistent path format for uploaded', () => {
const ref = MediaReference.createUploaded('media-456');
const result = resolveWithDefaults(ref);
// Should start with /media/uploaded/
expect(result).toMatch(/^\/media\/uploaded\//);
});
it('should maintain consistent path format for unknown generated type', () => {
const ref = MediaReference.createGenerated('unknown-999');
const result = resolveWithDefaults(ref);
// Should start with /media/generated/
expect(result).toMatch(/^\/media\/generated\//);
});
});
});

View File

@@ -0,0 +1,57 @@
import { describe, it, expect, vi } from 'vitest';
import { DriverStatsUseCase, type DriverStats } from './DriverStatsUseCase';
import type { ResultRepository } from '../../domain/repositories/ResultRepository';
import type { StandingRepository } from '../../domain/repositories/StandingRepository';
import type { DriverStatsRepository } from '../../domain/repositories/DriverStatsRepository';
import type { Logger } from '@core/shared/domain/Logger';
describe('DriverStatsUseCase', () => {
const mockResultRepository = {} as ResultRepository;
const mockStandingRepository = {} as StandingRepository;
const mockDriverStatsRepository = {
getDriverStats: vi.fn(),
} as unknown as DriverStatsRepository;
const mockLogger = {
debug: vi.fn(),
} as unknown as Logger;
const useCase = new DriverStatsUseCase(
mockResultRepository,
mockStandingRepository,
mockDriverStatsRepository,
mockLogger
);
it('should return driver stats when found', async () => {
const mockStats: DriverStats = {
rating: 1500,
safetyRating: 4.5,
sportsmanshipRating: 4.8,
totalRaces: 10,
wins: 2,
podiums: 5,
dnfs: 0,
avgFinish: 3.5,
bestFinish: 1,
worstFinish: 8,
consistency: 0.9,
experienceLevel: 'Intermediate',
overallRank: 42,
};
vi.mocked(mockDriverStatsRepository.getDriverStats).mockResolvedValue(mockStats);
const result = await useCase.getDriverStats('driver-1');
expect(result).toEqual(mockStats);
expect(mockLogger.debug).toHaveBeenCalledWith('Getting stats for driver driver-1');
expect(mockDriverStatsRepository.getDriverStats).toHaveBeenCalledWith('driver-1');
});
it('should return null when stats are not found', async () => {
vi.mocked(mockDriverStatsRepository.getDriverStats).mockResolvedValue(null);
const result = await useCase.getDriverStats('non-existent');
expect(result).toBeNull();
});
});

View File

@@ -0,0 +1,43 @@
import { describe, it, expect, vi } from 'vitest';
import { GetDriverUseCase } from './GetDriverUseCase';
import { Result } from '@core/shared/domain/Result';
import type { DriverRepository } from '../../domain/repositories/DriverRepository';
import type { Driver } from '../../domain/entities/Driver';
describe('GetDriverUseCase', () => {
const mockDriverRepository = {
findById: vi.fn(),
} as unknown as DriverRepository;
const useCase = new GetDriverUseCase(mockDriverRepository);
it('should return a driver when found', async () => {
const mockDriver = { id: 'driver-1', name: 'John Doe' } as unknown as Driver;
vi.mocked(mockDriverRepository.findById).mockResolvedValue(mockDriver);
const result = await useCase.execute({ driverId: 'driver-1' });
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBe(mockDriver);
expect(mockDriverRepository.findById).toHaveBeenCalledWith('driver-1');
});
it('should return null when driver is not found', async () => {
vi.mocked(mockDriverRepository.findById).mockResolvedValue(null);
const result = await useCase.execute({ driverId: 'non-existent' });
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeNull();
});
it('should return an error when repository throws', async () => {
const error = new Error('Repository error');
vi.mocked(mockDriverRepository.findById).mockRejectedValue(error);
const result = await useCase.execute({ driverId: 'driver-1' });
expect(result.isErr()).toBe(true);
expect(result.error).toBe(error);
});
});

View File

@@ -0,0 +1,90 @@
import { describe, it, expect, vi } from 'vitest';
import { GetTeamsLeaderboardUseCase } from './GetTeamsLeaderboardUseCase';
import { Result } from '@core/shared/domain/Result';
import type { TeamRepository } from '../../domain/repositories/TeamRepository';
import type { TeamMembershipRepository } from '../../domain/repositories/TeamMembershipRepository';
import type { Logger } from '@core/shared/domain/Logger';
import type { Team } from '../../domain/entities/Team';
describe('GetTeamsLeaderboardUseCase', () => {
const mockTeamRepository = {
findAll: vi.fn(),
} as unknown as TeamRepository;
const mockTeamMembershipRepository = {
getTeamMembers: vi.fn(),
} as unknown as TeamMembershipRepository;
const mockGetDriverStats = vi.fn();
const mockLogger = {
error: vi.fn(),
} as unknown as Logger;
const useCase = new GetTeamsLeaderboardUseCase(
mockTeamRepository,
mockTeamMembershipRepository,
mockGetDriverStats,
mockLogger
);
it('should return teams leaderboard with calculated stats', async () => {
const mockTeam1 = { id: 'team-1', name: 'Team 1' } as unknown as Team;
const mockTeam2 = { id: 'team-2', name: 'Team 2' } as unknown as Team;
vi.mocked(mockTeamRepository.findAll).mockResolvedValue([mockTeam1, mockTeam2]);
vi.mocked(mockTeamMembershipRepository.getTeamMembers).mockImplementation(async (teamId) => {
if (teamId === 'team-1') return [{ driverId: 'driver-1' }, { driverId: 'driver-2' }] as any;
if (teamId === 'team-2') return [{ driverId: 'driver-3' }] as any;
return [];
});
mockGetDriverStats.mockImplementation((driverId) => {
if (driverId === 'driver-1') return { rating: 1000, wins: 1, totalRaces: 5 };
if (driverId === 'driver-2') return { rating: 2000, wins: 2, totalRaces: 10 };
if (driverId === 'driver-3') return { rating: 1500, wins: 0, totalRaces: 2 };
return null;
});
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isOk()).toBe(true);
const data = result.unwrap();
expect(data.items).toHaveLength(2);
const item1 = data.items.find(i => i.team.id === 'team-1');
expect(item1?.rating).toBe(1500); // (1000 + 2000) / 2
expect(item1?.totalWins).toBe(3);
expect(item1?.totalRaces).toBe(15);
const item2 = data.items.find(i => i.team.id === 'team-2');
expect(item2?.rating).toBe(1500);
expect(item2?.totalWins).toBe(0);
expect(item2?.totalRaces).toBe(2);
expect(data.topItems).toHaveLength(2);
});
it('should handle teams with no members', async () => {
const mockTeam = { id: 'team-empty', name: 'Empty Team' } as unknown as Team;
vi.mocked(mockTeamRepository.findAll).mockResolvedValue([mockTeam]);
vi.mocked(mockTeamMembershipRepository.getTeamMembers).mockResolvedValue([]);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isOk()).toBe(true);
const data = result.unwrap();
expect(data.items[0].rating).toBeNull();
expect(data.items[0].performanceLevel).toBe('beginner');
});
it('should return error when repository fails', async () => {
vi.mocked(mockTeamRepository.findAll).mockRejectedValue(new Error('DB Error'));
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isErr()).toBe(true);
expect(result.error.code).toBe('REPOSITORY_ERROR');
expect(mockLogger.error).toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,59 @@
import { describe, it, expect, vi } from 'vitest';
import { RankingUseCase, type DriverRanking } from './RankingUseCase';
import type { StandingRepository } from '../../domain/repositories/StandingRepository';
import type { DriverRepository } from '../../domain/repositories/DriverRepository';
import type { DriverStatsRepository } from '../../domain/repositories/DriverStatsRepository';
import type { Logger } from '@core/shared/domain/Logger';
describe('RankingUseCase', () => {
const mockStandingRepository = {} as StandingRepository;
const mockDriverRepository = {} as DriverRepository;
const mockDriverStatsRepository = {
getAllStats: vi.fn(),
} as unknown as DriverStatsRepository;
const mockLogger = {
debug: vi.fn(),
} as unknown as Logger;
const useCase = new RankingUseCase(
mockStandingRepository,
mockDriverRepository,
mockDriverStatsRepository,
mockLogger
);
it('should return all driver rankings', async () => {
const mockStatsMap = new Map([
['driver-1', { rating: 1500, wins: 2, totalRaces: 10, overallRank: 1 }],
['driver-2', { rating: 1200, wins: 0, totalRaces: 5, overallRank: 2 }],
]);
vi.mocked(mockDriverStatsRepository.getAllStats).mockResolvedValue(mockStatsMap as any);
const result = await useCase.getAllDriverRankings();
expect(result).toHaveLength(2);
expect(result).toContainEqual({
driverId: 'driver-1',
rating: 1500,
wins: 2,
totalRaces: 10,
overallRank: 1,
});
expect(result).toContainEqual({
driverId: 'driver-2',
rating: 1200,
wins: 0,
totalRaces: 5,
overallRank: 2,
});
expect(mockLogger.debug).toHaveBeenCalledWith('Getting all driver rankings');
});
it('should return empty array when no stats exist', async () => {
vi.mocked(mockDriverStatsRepository.getAllStats).mockResolvedValue(new Map());
const result = await useCase.getAllDriverRankings();
expect(result).toEqual([]);
});
});

View File

@@ -0,0 +1,44 @@
import { describe, it, expect, vi } from 'vitest';
import { RaceResultGenerator } from './RaceResultGenerator';
describe('RaceResultGenerator', () => {
it('should generate results for all drivers', () => {
const raceId = 'race-1';
const driverIds = ['d1', 'd2', 'd3'];
const driverRatings = new Map([
['d1', 2000],
['d2', 1500],
['d3', 1000],
]);
const results = RaceResultGenerator.generateRaceResults(raceId, driverIds, driverRatings);
expect(results).toHaveLength(3);
const resultDriverIds = results.map(r => r.driverId.toString());
expect(resultDriverIds).toContain('d1');
expect(resultDriverIds).toContain('d2');
expect(resultDriverIds).toContain('d3');
results.forEach(r => {
expect(r.raceId.toString()).toBe(raceId);
expect(r.position.toNumber()).toBeGreaterThan(0);
expect(r.position.toNumber()).toBeLessThanOrEqual(3);
});
});
it('should provide incident descriptions', () => {
expect(RaceResultGenerator.getIncidentDescription(0)).toBe('Clean race');
expect(RaceResultGenerator.getIncidentDescription(1)).toBe('Track limits violation');
expect(RaceResultGenerator.getIncidentDescription(2)).toBe('Contact with another car');
expect(RaceResultGenerator.getIncidentDescription(3)).toBe('Off-track incident');
expect(RaceResultGenerator.getIncidentDescription(4)).toBe('Collision requiring safety car');
expect(RaceResultGenerator.getIncidentDescription(5)).toBe('5 incidents');
});
it('should calculate incident penalty points', () => {
expect(RaceResultGenerator.getIncidentPenaltyPoints(0)).toBe(0);
expect(RaceResultGenerator.getIncidentPenaltyPoints(1)).toBe(0);
expect(RaceResultGenerator.getIncidentPenaltyPoints(2)).toBe(2);
expect(RaceResultGenerator.getIncidentPenaltyPoints(3)).toBe(4);
});
});

View File

@@ -0,0 +1,40 @@
import { describe, it, expect } from 'vitest';
import { RaceResultGeneratorWithIncidents } from './RaceResultGeneratorWithIncidents';
import { RaceIncidents } from '../../domain/value-objects/RaceIncidents';
describe('RaceResultGeneratorWithIncidents', () => {
it('should generate results for all drivers', () => {
const raceId = 'race-1';
const driverIds = ['d1', 'd2'];
const driverRatings = new Map([
['d1', 2000],
['d2', 1500],
]);
const results = RaceResultGeneratorWithIncidents.generateRaceResults(raceId, driverIds, driverRatings);
expect(results).toHaveLength(2);
results.forEach(r => {
expect(r.raceId.toString()).toBe(raceId);
expect(r.incidents).toBeInstanceOf(RaceIncidents);
});
});
it('should calculate incident penalty points', () => {
const incidents = new RaceIncidents([
{ type: 'contact', lap: 1, description: 'desc', penaltyPoints: 2 },
{ type: 'unsafe_rejoin', lap: 5, description: 'desc', penaltyPoints: 3 },
]);
expect(RaceResultGeneratorWithIncidents.getIncidentPenaltyPoints(incidents)).toBe(5);
});
it('should get incident description', () => {
const incidents = new RaceIncidents([
{ type: 'contact', lap: 1, description: 'desc', penaltyPoints: 2 },
]);
const description = RaceResultGeneratorWithIncidents.getIncidentDescription(incidents);
expect(description).toContain('1 incidents');
});
});

View File

@@ -0,0 +1,75 @@
import { describe, it, expect, vi } from 'vitest';
import { ChampionshipAggregator } from './ChampionshipAggregator';
import type { DropScoreApplier } from './DropScoreApplier';
import { Points } from '../value-objects/Points';
describe('ChampionshipAggregator', () => {
const mockDropScoreApplier = {
apply: vi.fn(),
} as unknown as DropScoreApplier;
const aggregator = new ChampionshipAggregator(mockDropScoreApplier);
it('should aggregate points and sort standings by total points', () => {
const seasonId = 'season-1';
const championship = {
id: 'champ-1',
dropScorePolicy: { strategy: 'none' },
} as any;
const eventPointsByEventId = {
'event-1': [
{
participant: { id: 'p1', type: 'driver' },
totalPoints: 10,
basePoints: 10,
bonusPoints: 0,
penaltyPoints: 0
},
{
participant: { id: 'p2', type: 'driver' },
totalPoints: 20,
basePoints: 20,
bonusPoints: 0,
penaltyPoints: 0
},
],
'event-2': [
{
participant: { id: 'p1', type: 'driver' },
totalPoints: 15,
basePoints: 15,
bonusPoints: 0,
penaltyPoints: 0
},
],
} as any;
vi.mocked(mockDropScoreApplier.apply).mockImplementation((policy, events) => {
const total = events.reduce((sum, e) => sum + e.points, 0);
return {
totalPoints: total,
counted: events,
dropped: [],
};
});
const standings = aggregator.aggregate({
seasonId,
championship,
eventPointsByEventId,
});
expect(standings).toHaveLength(2);
// p1 should be first (10 + 15 = 25 points)
expect(standings[0].participant.id).toBe('p1');
expect(standings[0].totalPoints.toNumber()).toBe(25);
expect(standings[0].position.toNumber()).toBe(1);
// p2 should be second (20 points)
expect(standings[1].participant.id).toBe('p2');
expect(standings[1].totalPoints.toNumber()).toBe(20);
expect(standings[1].position.toNumber()).toBe(2);
});
});

View File

@@ -59,7 +59,7 @@ export class ChampionshipAggregator {
totalPoints,
resultsCounted,
resultsDropped,
position: 0,
position: 1,
}),
);
}

View File

@@ -0,0 +1,74 @@
import { describe, it, expect } from 'vitest';
import { SeasonScheduleGenerator } from './SeasonScheduleGenerator';
import { SeasonSchedule } from '../value-objects/SeasonSchedule';
import { RecurrenceStrategy } from '../value-objects/RecurrenceStrategy';
import { RaceTimeOfDay } from '../value-objects/RaceTimeOfDay';
import { WeekdaySet } from '../value-objects/WeekdaySet';
import { LeagueTimezone } from '../value-objects/LeagueTimezone';
import { MonthlyRecurrencePattern } from '../value-objects/MonthlyRecurrencePattern';
describe('SeasonScheduleGenerator', () => {
it('should generate weekly slots', () => {
const startDate = new Date(2024, 0, 1); // Monday, Jan 1st 2024
const schedule = new SeasonSchedule({
startDate,
plannedRounds: 4,
timeOfDay: new RaceTimeOfDay(20, 0),
timezone: LeagueTimezone.create('UTC'),
recurrence: RecurrenceStrategy.weekly(WeekdaySet.fromArray(['Mon'])),
});
const slots = SeasonScheduleGenerator.generateSlots(schedule);
expect(slots).toHaveLength(4);
expect(slots[0].roundNumber).toBe(1);
expect(slots[0].scheduledAt.getHours()).toBe(20);
expect(slots[0].scheduledAt.getMinutes()).toBe(0);
expect(slots[0].scheduledAt.getFullYear()).toBe(2024);
expect(slots[0].scheduledAt.getMonth()).toBe(0);
expect(slots[0].scheduledAt.getDate()).toBe(1);
expect(slots[1].roundNumber).toBe(2);
expect(slots[1].scheduledAt.getDate()).toBe(8);
expect(slots[2].roundNumber).toBe(3);
expect(slots[2].scheduledAt.getDate()).toBe(15);
expect(slots[3].roundNumber).toBe(4);
expect(slots[3].scheduledAt.getDate()).toBe(22);
});
it('should generate slots every 2 weeks', () => {
const startDate = new Date(2024, 0, 1);
const schedule = new SeasonSchedule({
startDate,
plannedRounds: 2,
timeOfDay: new RaceTimeOfDay(20, 0),
timezone: LeagueTimezone.create('UTC'),
recurrence: RecurrenceStrategy.everyNWeeks(2, WeekdaySet.fromArray(['Mon'])),
});
const slots = SeasonScheduleGenerator.generateSlots(schedule);
expect(slots).toHaveLength(2);
expect(slots[0].scheduledAt.getDate()).toBe(1);
expect(slots[1].scheduledAt.getDate()).toBe(15);
});
it('should generate monthly slots (nth weekday)', () => {
const startDate = new Date(2024, 0, 1);
const schedule = new SeasonSchedule({
startDate,
plannedRounds: 2,
timeOfDay: new RaceTimeOfDay(20, 0),
timezone: LeagueTimezone.create('UTC'),
recurrence: RecurrenceStrategy.monthlyNthWeekday(MonthlyRecurrencePattern.create(1, 'Mon')),
});
const slots = SeasonScheduleGenerator.generateSlots(schedule);
expect(slots).toHaveLength(2);
expect(slots[0].scheduledAt.getMonth()).toBe(0);
expect(slots[0].scheduledAt.getDate()).toBe(1);
expect(slots[1].scheduledAt.getMonth()).toBe(1);
expect(slots[1].scheduledAt.getDate()).toBe(5);
});
});

View File

@@ -0,0 +1,50 @@
import { describe, it, expect } from 'vitest';
import { SkillLevelService } from './SkillLevelService';
describe('SkillLevelService', () => {
describe('getSkillLevel', () => {
it('should return pro for rating >= 3000', () => {
expect(SkillLevelService.getSkillLevel(3000)).toBe('pro');
expect(SkillLevelService.getSkillLevel(5000)).toBe('pro');
});
it('should return advanced for rating >= 2500 and < 3000', () => {
expect(SkillLevelService.getSkillLevel(2500)).toBe('advanced');
expect(SkillLevelService.getSkillLevel(2999)).toBe('advanced');
});
it('should return intermediate for rating >= 1800 and < 2500', () => {
expect(SkillLevelService.getSkillLevel(1800)).toBe('intermediate');
expect(SkillLevelService.getSkillLevel(2499)).toBe('intermediate');
});
it('should return beginner for rating < 1800', () => {
expect(SkillLevelService.getSkillLevel(1799)).toBe('beginner');
expect(SkillLevelService.getSkillLevel(500)).toBe('beginner');
});
});
describe('getTeamPerformanceLevel', () => {
it('should return beginner for null rating', () => {
expect(SkillLevelService.getTeamPerformanceLevel(null)).toBe('beginner');
});
it('should return pro for rating >= 4500', () => {
expect(SkillLevelService.getTeamPerformanceLevel(4500)).toBe('pro');
});
it('should return advanced for rating >= 3000 and < 4500', () => {
expect(SkillLevelService.getTeamPerformanceLevel(3000)).toBe('advanced');
expect(SkillLevelService.getTeamPerformanceLevel(4499)).toBe('advanced');
});
it('should return intermediate for rating >= 2000 and < 3000', () => {
expect(SkillLevelService.getTeamPerformanceLevel(2000)).toBe('intermediate');
expect(SkillLevelService.getTeamPerformanceLevel(2999)).toBe('intermediate');
});
it('should return beginner for rating < 2000', () => {
expect(SkillLevelService.getTeamPerformanceLevel(1999)).toBe('beginner');
});
});
});

View File

@@ -0,0 +1,54 @@
import { describe, it, expect } from 'vitest';
import { AverageStrengthOfFieldCalculator } from './StrengthOfFieldCalculator';
describe('AverageStrengthOfFieldCalculator', () => {
const calculator = new AverageStrengthOfFieldCalculator();
it('should calculate average SOF and round it', () => {
const ratings = [
{ driverId: 'd1', rating: 1500 },
{ driverId: 'd2', rating: 2000 },
{ driverId: 'd3', rating: 1750 },
];
const sof = calculator.calculate(ratings);
expect(sof).toBe(1750);
});
it('should handle rounding correctly', () => {
const ratings = [
{ driverId: 'd1', rating: 1000 },
{ driverId: 'd2', rating: 1001 },
];
const sof = calculator.calculate(ratings);
expect(sof).toBe(1001); // (1000 + 1001) / 2 = 1000.5 -> 1001
});
it('should return null for empty ratings', () => {
expect(calculator.calculate([])).toBeNull();
});
it('should filter out non-positive ratings', () => {
const ratings = [
{ driverId: 'd1', rating: 1500 },
{ driverId: 'd2', rating: 0 },
{ driverId: 'd3', rating: -100 },
];
const sof = calculator.calculate(ratings);
expect(sof).toBe(1500);
});
it('should return null if all ratings are non-positive', () => {
const ratings = [
{ driverId: 'd1', rating: 0 },
{ driverId: 'd2', rating: -500 },
];
expect(calculator.calculate(ratings)).toBeNull();
});
});

View File

@@ -0,0 +1,412 @@
import { describe, it, expect } from 'vitest';
import { AsyncUseCase } from './AsyncUseCase';
import { Result } from '../domain/Result';
import { ApplicationErrorCode } from '../errors/ApplicationErrorCode';
describe('AsyncUseCase', () => {
describe('AsyncUseCase interface', () => {
it('should have execute method returning Promise<Result>', async () => {
// Concrete implementation for testing
class TestAsyncUseCase implements AsyncUseCase<{ id: string }, { data: string }, 'NOT_FOUND'> {
async execute(input: { id: string }): Promise<Result<{ data: string }, ApplicationErrorCode<'NOT_FOUND'>>> {
if (input.id === 'not-found') {
return Result.err({ code: 'NOT_FOUND' });
}
return Result.ok({ data: `Data for ${input.id}` });
}
}
const useCase = new TestAsyncUseCase();
const successResult = await useCase.execute({ id: 'test-123' });
expect(successResult.isOk()).toBe(true);
expect(successResult.unwrap()).toEqual({ data: 'Data for test-123' });
const errorResult = await useCase.execute({ id: 'not-found' });
expect(errorResult.isErr()).toBe(true);
expect(errorResult.unwrapErr()).toEqual({ code: 'NOT_FOUND' });
});
it('should support different input types', async () => {
interface GetUserInput {
userId: string;
includeProfile?: boolean;
}
interface UserDTO {
id: string;
name: string;
email: string;
profile?: {
avatar: string;
bio: string;
};
}
type GetUserErrorCode = 'USER_NOT_FOUND' | 'PERMISSION_DENIED';
class GetUserUseCase implements AsyncUseCase<GetUserInput, UserDTO, GetUserErrorCode> {
async execute(input: GetUserInput): Promise<Result<UserDTO, ApplicationErrorCode<GetUserErrorCode>>> {
if (input.userId === 'not-found') {
return Result.err({ code: 'USER_NOT_FOUND' });
}
if (input.userId === 'no-permission') {
return Result.err({ code: 'PERMISSION_DENIED' });
}
const user: UserDTO = {
id: input.userId,
name: 'John Doe',
email: 'john@example.com'
};
if (input.includeProfile) {
user.profile = {
avatar: 'avatar.jpg',
bio: 'Software developer'
};
}
return Result.ok(user);
}
}
const useCase = new GetUserUseCase();
// Success case with profile
const successWithProfile = await useCase.execute({
userId: 'user-123',
includeProfile: true
});
expect(successWithProfile.isOk()).toBe(true);
const userWithProfile = successWithProfile.unwrap();
expect(userWithProfile.id).toBe('user-123');
expect(userWithProfile.profile).toBeDefined();
expect(userWithProfile.profile?.avatar).toBe('avatar.jpg');
// Success case without profile
const successWithoutProfile = await useCase.execute({
userId: 'user-456',
includeProfile: false
});
expect(successWithoutProfile.isOk()).toBe(true);
const userWithoutProfile = successWithoutProfile.unwrap();
expect(userWithoutProfile.id).toBe('user-456');
expect(userWithoutProfile.profile).toBeUndefined();
// Error cases
const notFoundResult = await useCase.execute({ userId: 'not-found' });
expect(notFoundResult.isErr()).toBe(true);
expect(notFoundResult.unwrapErr()).toEqual({ code: 'USER_NOT_FOUND' });
const permissionResult = await useCase.execute({ userId: 'no-permission' });
expect(permissionResult.isErr()).toBe(true);
expect(permissionResult.unwrapErr()).toEqual({ code: 'PERMISSION_DENIED' });
});
it('should support complex query patterns', async () => {
interface SearchOrdersInput {
customerId?: string;
status?: 'pending' | 'completed' | 'cancelled';
dateRange?: { start: Date; end: Date };
page?: number;
limit?: number;
}
interface OrderDTO {
id: string;
customerId: string;
status: string;
total: number;
items: Array<{ productId: string; quantity: number; price: number }>;
createdAt: Date;
}
interface OrdersResult {
orders: OrderDTO[];
total: number;
page: number;
totalPages: number;
filters: SearchOrdersInput;
}
type SearchOrdersErrorCode = 'INVALID_FILTERS' | 'NO_ORDERS_FOUND';
class SearchOrdersUseCase implements AsyncUseCase<SearchOrdersInput, OrdersResult, SearchOrdersErrorCode> {
async execute(input: SearchOrdersInput): Promise<Result<OrdersResult, ApplicationErrorCode<SearchOrdersErrorCode>>> {
// Validate at least one filter
if (!input.customerId && !input.status && !input.dateRange) {
return Result.err({ code: 'INVALID_FILTERS' });
}
// Simulate database query
const allOrders: OrderDTO[] = [
{
id: 'order-1',
customerId: 'cust-1',
status: 'completed',
total: 150,
items: [{ productId: 'prod-1', quantity: 2, price: 75 }],
createdAt: new Date('2024-01-01')
},
{
id: 'order-2',
customerId: 'cust-1',
status: 'pending',
total: 200,
items: [{ productId: 'prod-2', quantity: 1, price: 200 }],
createdAt: new Date('2024-01-02')
},
{
id: 'order-3',
customerId: 'cust-2',
status: 'completed',
total: 300,
items: [{ productId: 'prod-3', quantity: 3, price: 100 }],
createdAt: new Date('2024-01-03')
}
];
// Apply filters
let filteredOrders = allOrders;
if (input.customerId) {
filteredOrders = filteredOrders.filter(o => o.customerId === input.customerId);
}
if (input.status) {
filteredOrders = filteredOrders.filter(o => o.status === input.status);
}
if (input.dateRange) {
filteredOrders = filteredOrders.filter(o => {
const orderDate = o.createdAt.getTime();
return orderDate >= input.dateRange!.start.getTime() &&
orderDate <= input.dateRange!.end.getTime();
});
}
if (filteredOrders.length === 0) {
return Result.err({ code: 'NO_ORDERS_FOUND' });
}
// Apply pagination
const page = input.page || 1;
const limit = input.limit || 10;
const start = (page - 1) * limit;
const end = start + limit;
const paginatedOrders = filteredOrders.slice(start, end);
const result: OrdersResult = {
orders: paginatedOrders,
total: filteredOrders.length,
page,
totalPages: Math.ceil(filteredOrders.length / limit),
filters: input
};
return Result.ok(result);
}
}
const useCase = new SearchOrdersUseCase();
// Success case - filter by customer
const customerResult = await useCase.execute({ customerId: 'cust-1' });
expect(customerResult.isOk()).toBe(true);
const customerOrders = customerResult.unwrap();
expect(customerOrders.orders).toHaveLength(2);
expect(customerOrders.total).toBe(2);
// Success case - filter by status
const statusResult = await useCase.execute({ status: 'completed' });
expect(statusResult.isOk()).toBe(true);
const completedOrders = statusResult.unwrap();
expect(completedOrders.orders).toHaveLength(2);
expect(completedOrders.total).toBe(2);
// Success case - filter by date range
const dateResult = await useCase.execute({
dateRange: {
start: new Date('2024-01-01'),
end: new Date('2024-01-02')
}
});
expect(dateResult.isOk()).toBe(true);
const dateOrders = dateResult.unwrap();
expect(dateOrders.orders).toHaveLength(2);
expect(dateOrders.total).toBe(2);
// Error case - no filters
const noFiltersResult = await useCase.execute({});
expect(noFiltersResult.isErr()).toBe(true);
expect(noFiltersResult.unwrapErr()).toEqual({ code: 'INVALID_FILTERS' });
// Error case - no matching orders
const noOrdersResult = await useCase.execute({ customerId: 'nonexistent' });
expect(noOrdersResult.isErr()).toBe(true);
expect(noOrdersResult.unwrapErr()).toEqual({ code: 'NO_ORDERS_FOUND' });
});
it('should support async operations with delays', async () => {
interface ProcessBatchInput {
items: Array<{ id: string; data: string }>;
delayMs?: number;
}
interface ProcessBatchResult {
processed: number;
failed: number;
results: Array<{ id: string; status: 'success' | 'failed'; message?: string }>;
}
type ProcessBatchErrorCode = 'EMPTY_BATCH' | 'PROCESSING_ERROR';
class ProcessBatchUseCase implements AsyncUseCase<ProcessBatchInput, ProcessBatchResult, ProcessBatchErrorCode> {
async execute(input: ProcessBatchInput): Promise<Result<ProcessBatchResult, ApplicationErrorCode<ProcessBatchErrorCode>>> {
if (input.items.length === 0) {
return Result.err({ code: 'EMPTY_BATCH' });
}
const delay = input.delayMs || 10;
const results: Array<{ id: string; status: 'success' | 'failed'; message?: string }> = [];
let processed = 0;
let failed = 0;
for (const item of input.items) {
// Simulate async processing with delay
await new Promise(resolve => setTimeout(resolve, delay));
// Simulate some failures
if (item.id === 'fail-1' || item.id === 'fail-2') {
results.push({ id: item.id, status: 'failed', message: 'Processing failed' });
failed++;
} else {
results.push({ id: item.id, status: 'success' });
processed++;
}
}
return Result.ok({
processed,
failed,
results
});
}
}
const useCase = new ProcessBatchUseCase();
// Success case
const successResult = await useCase.execute({
items: [
{ id: 'item-1', data: 'data1' },
{ id: 'item-2', data: 'data2' },
{ id: 'item-3', data: 'data3' }
],
delayMs: 5
});
expect(successResult.isOk()).toBe(true);
const batchResult = successResult.unwrap();
expect(batchResult.processed).toBe(3);
expect(batchResult.failed).toBe(0);
expect(batchResult.results).toHaveLength(3);
// Mixed success/failure case
const mixedResult = await useCase.execute({
items: [
{ id: 'item-1', data: 'data1' },
{ id: 'fail-1', data: 'data2' },
{ id: 'item-3', data: 'data3' },
{ id: 'fail-2', data: 'data4' }
],
delayMs: 5
});
expect(mixedResult.isOk()).toBe(true);
const mixedBatchResult = mixedResult.unwrap();
expect(mixedBatchResult.processed).toBe(2);
expect(mixedBatchResult.failed).toBe(2);
expect(mixedBatchResult.results).toHaveLength(4);
// Error case - empty batch
const emptyResult = await useCase.execute({ items: [] });
expect(emptyResult.isErr()).toBe(true);
expect(emptyResult.unwrapErr()).toEqual({ code: 'EMPTY_BATCH' });
});
it('should support streaming-like operations', async () => {
interface StreamInput {
source: string;
chunkSize?: number;
}
interface StreamResult {
chunks: string[];
totalSize: number;
source: string;
}
type StreamErrorCode = 'SOURCE_NOT_FOUND' | 'STREAM_ERROR';
class StreamUseCase implements AsyncUseCase<StreamInput, StreamResult, StreamErrorCode> {
async execute(input: StreamInput): Promise<Result<StreamResult, ApplicationErrorCode<StreamErrorCode>>> {
if (input.source === 'not-found') {
return Result.err({ code: 'SOURCE_NOT_FOUND' });
}
if (input.source === 'error') {
return Result.err({ code: 'STREAM_ERROR' });
}
const chunkSize = input.chunkSize || 10;
const data = 'This is a test data stream that will be split into chunks';
const chunks: string[] = [];
for (let i = 0; i < data.length; i += chunkSize) {
// Simulate async chunk reading
await new Promise(resolve => setTimeout(resolve, 1));
chunks.push(data.slice(i, i + chunkSize));
}
return Result.ok({
chunks,
totalSize: data.length,
source: input.source
});
}
}
const useCase = new StreamUseCase();
// Success case with default chunk size
const defaultResult = await useCase.execute({ source: 'test-source' });
expect(defaultResult.isOk()).toBe(true);
const defaultStream = defaultResult.unwrap();
expect(defaultStream.chunks).toHaveLength(6);
expect(defaultStream.totalSize).toBe(57);
expect(defaultStream.source).toBe('test-source');
// Success case with custom chunk size
const customResult = await useCase.execute({ source: 'test-source', chunkSize: 15 });
expect(customResult.isOk()).toBe(true);
const customStream = customResult.unwrap();
expect(customStream.chunks).toHaveLength(4);
expect(customStream.totalSize).toBe(57);
// Error case - source not found
const notFoundResult = await useCase.execute({ source: 'not-found' });
expect(notFoundResult.isErr()).toBe(true);
expect(notFoundResult.unwrapErr()).toEqual({ code: 'SOURCE_NOT_FOUND' });
// Error case - stream error
const errorResult = await useCase.execute({ source: 'error' });
expect(errorResult.isErr()).toBe(true);
expect(errorResult.unwrapErr()).toEqual({ code: 'STREAM_ERROR' });
});
});
});

View File

@@ -0,0 +1,366 @@
import { describe, it, expect } from 'vitest';
import { ErrorReporter } from './ErrorReporter';
describe('ErrorReporter', () => {
describe('ErrorReporter interface', () => {
it('should have report method', () => {
const errors: Array<{ error: Error; context?: unknown }> = [];
const reporter: ErrorReporter = {
report: (error: Error, context?: unknown) => {
errors.push({ error, context });
}
};
const testError = new Error('Test error');
reporter.report(testError, { userId: 123 });
expect(errors).toHaveLength(1);
expect(errors[0].error).toBe(testError);
expect(errors[0].context).toEqual({ userId: 123 });
});
it('should support reporting without context', () => {
const errors: Error[] = [];
const reporter: ErrorReporter = {
report: (error: Error) => {
errors.push(error);
}
};
const testError = new Error('Test error');
reporter.report(testError);
expect(errors).toHaveLength(1);
expect(errors[0]).toBe(testError);
});
it('should support different error types', () => {
const errors: Array<{ error: Error; context?: unknown }> = [];
const reporter: ErrorReporter = {
report: (error: Error, context?: unknown) => {
errors.push({ error, context });
}
};
// Standard Error
const standardError = new Error('Standard error');
reporter.report(standardError, { type: 'standard' });
// Custom Error
class CustomError extends Error {
constructor(message: string, public code: string) {
super(message);
this.name = 'CustomError';
}
}
const customError = new CustomError('Custom error', 'CUSTOM_CODE');
reporter.report(customError, { type: 'custom' });
// TypeError
const typeError = new TypeError('Type error');
reporter.report(typeError, { type: 'type' });
expect(errors).toHaveLength(3);
expect(errors[0].error).toBe(standardError);
expect(errors[1].error).toBe(customError);
expect(errors[2].error).toBe(typeError);
});
it('should support complex context objects', () => {
const errors: Array<{ error: Error; context?: unknown }> = [];
const reporter: ErrorReporter = {
report: (error: Error, context?: unknown) => {
errors.push({ error, context });
}
};
const complexContext = {
user: {
id: 'user-123',
name: 'John Doe',
email: 'john@example.com'
},
request: {
method: 'POST',
url: '/api/users',
headers: {
'content-type': 'application/json',
'authorization': 'Bearer token'
}
},
timestamp: new Date('2024-01-01T12:00:00Z'),
metadata: {
retryCount: 3,
timeout: 5000
}
};
const error = new Error('Request failed');
reporter.report(error, complexContext);
expect(errors).toHaveLength(1);
expect(errors[0].error).toBe(error);
expect(errors[0].context).toEqual(complexContext);
});
});
describe('ErrorReporter behavior', () => {
it('should support logging error with stack trace', () => {
const logs: Array<{ message: string; stack?: string; context?: unknown }> = [];
const reporter: ErrorReporter = {
report: (error: Error, context?: unknown) => {
logs.push({
message: error.message,
stack: error.stack,
context
});
}
};
const error = new Error('Database connection failed');
reporter.report(error, { retryCount: 3 });
expect(logs).toHaveLength(1);
expect(logs[0].message).toBe('Database connection failed');
expect(logs[0].stack).toBeDefined();
expect(logs[0].context).toEqual({ retryCount: 3 });
});
it('should support error aggregation', () => {
const errorCounts: Record<string, number> = {};
const reporter: ErrorReporter = {
report: (error: Error) => {
const errorType = error.name || 'Unknown';
errorCounts[errorType] = (errorCounts[errorType] || 0) + 1;
}
};
const error1 = new Error('Error 1');
const error2 = new TypeError('Type error');
const error3 = new Error('Error 2');
const error4 = new TypeError('Another type error');
reporter.report(error1);
reporter.report(error2);
reporter.report(error3);
reporter.report(error4);
expect(errorCounts['Error']).toBe(2);
expect(errorCounts['TypeError']).toBe(2);
});
it('should support error filtering', () => {
const criticalErrors: Error[] = [];
const warnings: Error[] = [];
const reporter: ErrorReporter = {
report: (error: Error, context?: unknown) => {
const isCritical = context && typeof context === 'object' && 'severity' in context &&
(context as { severity: string }).severity === 'critical';
if (isCritical) {
criticalErrors.push(error);
} else {
warnings.push(error);
}
}
};
const criticalError = new Error('Critical failure');
const warningError = new Error('Warning');
reporter.report(criticalError, { severity: 'critical' });
reporter.report(warningError, { severity: 'warning' });
expect(criticalErrors).toHaveLength(1);
expect(criticalErrors[0]).toBe(criticalError);
expect(warnings).toHaveLength(1);
expect(warnings[0]).toBe(warningError);
});
it('should support error enrichment', () => {
const enrichedErrors: Array<{ error: Error; enrichedContext: unknown }> = [];
const reporter: ErrorReporter = {
report: (error: Error, context?: unknown) => {
const enrichedContext: Record<string, unknown> = {
errorName: error.name,
errorMessage: error.message,
timestamp: new Date().toISOString(),
environment: process.env.NODE_ENV || 'development'
};
if (context && typeof context === 'object') {
Object.assign(enrichedContext, context);
}
enrichedErrors.push({ error, enrichedContext });
}
};
const error = new Error('Something went wrong');
reporter.report(error, { userId: 'user-123', action: 'login' });
expect(enrichedErrors).toHaveLength(1);
expect(enrichedErrors[0].error).toBe(error);
expect(enrichedErrors[0].enrichedContext).toMatchObject({
userId: 'user-123',
action: 'login',
errorName: 'Error',
errorMessage: 'Something went wrong',
environment: 'test'
});
});
it('should support error deduplication', () => {
const uniqueErrors: Error[] = [];
const seenMessages = new Set<string>();
const reporter: ErrorReporter = {
report: (error: Error) => {
if (!seenMessages.has(error.message)) {
uniqueErrors.push(error);
seenMessages.add(error.message);
}
}
};
const error1 = new Error('Duplicate error');
const error2 = new Error('Duplicate error');
const error3 = new Error('Unique error');
reporter.report(error1);
reporter.report(error2);
reporter.report(error3);
expect(uniqueErrors).toHaveLength(2);
expect(uniqueErrors[0].message).toBe('Duplicate error');
expect(uniqueErrors[1].message).toBe('Unique error');
});
it('should support error rate limiting', () => {
const errors: Error[] = [];
let errorCount = 0;
const rateLimit = 5;
const reporter: ErrorReporter = {
report: (error: Error) => {
errorCount++;
if (errorCount <= rateLimit) {
errors.push(error);
}
// Silently drop errors beyond rate limit
}
};
// Report 10 errors
for (let i = 0; i < 10; i++) {
reporter.report(new Error(`Error ${i}`));
}
expect(errors).toHaveLength(rateLimit);
expect(errorCount).toBe(10);
});
});
describe('ErrorReporter implementation patterns', () => {
it('should support console logger implementation', () => {
const consoleErrors: string[] = [];
const originalConsoleError = console.error;
// Mock console.error
console.error = (...args: unknown[]) => consoleErrors.push(args.join(' '));
const consoleReporter: ErrorReporter = {
report: (error: Error, context?: unknown) => {
console.error('Error:', error.message, 'Context:', context);
}
};
const error = new Error('Test error');
consoleReporter.report(error, { userId: 123 });
// Restore console.error
console.error = originalConsoleError;
expect(consoleErrors).toHaveLength(1);
expect(consoleErrors[0]).toContain('Error:');
expect(consoleErrors[0]).toContain('Test error');
expect(consoleErrors[0]).toContain('Context:');
});
it('should support file logger implementation', () => {
const fileLogs: Array<{ timestamp: string; error: string; context?: unknown }> = [];
const fileReporter: ErrorReporter = {
report: (error: Error, context?: unknown) => {
fileLogs.push({
timestamp: new Date().toISOString(),
error: error.message,
context
});
}
};
const error = new Error('File error');
fileReporter.report(error, { file: 'test.txt', line: 42 });
expect(fileLogs).toHaveLength(1);
expect(fileLogs[0].error).toBe('File error');
expect(fileLogs[0].context).toEqual({ file: 'test.txt', line: 42 });
});
it('should support remote reporter implementation', async () => {
const remoteErrors: Array<{ error: string; context?: unknown }> = [];
const remoteReporter: ErrorReporter = {
report: async (error: Error, context?: unknown) => {
remoteErrors.push({ error: error.message, context });
await new Promise(resolve => setTimeout(resolve, 1));
}
};
const error = new Error('Remote error');
await remoteReporter.report(error, { endpoint: '/api/data' });
expect(remoteErrors).toHaveLength(1);
expect(remoteErrors[0].error).toBe('Remote error');
expect(remoteErrors[0].context).toEqual({ endpoint: '/api/data' });
});
it('should support batch error reporting', () => {
const batchErrors: Error[] = [];
const batchSize = 3;
let currentBatch: Error[] = [];
const reporter: ErrorReporter = {
report: (error: Error) => {
currentBatch.push(error);
if (currentBatch.length >= batchSize) {
batchErrors.push(...currentBatch);
currentBatch = [];
}
}
};
// Report 7 errors
for (let i = 0; i < 7; i++) {
reporter.report(new Error(`Error ${i}`));
}
// Add remaining errors
if (currentBatch.length > 0) {
batchErrors.push(...currentBatch);
}
expect(batchErrors).toHaveLength(7);
});
});
});

View File

@@ -0,0 +1,451 @@
import { describe, it, expect } from 'vitest';
import { ApplicationService, AsyncApplicationService, AsyncResultApplicationService } from './Service';
import { Result } from '../domain/Result';
import { ApplicationError } from '../errors/ApplicationError';
describe('Service', () => {
describe('ApplicationService interface', () => {
it('should have optional serviceName property', () => {
const service: ApplicationService = {
serviceName: 'TestService'
};
expect(service.serviceName).toBe('TestService');
});
it('should work without serviceName', () => {
const service: ApplicationService = {};
expect(service.serviceName).toBeUndefined();
});
it('should support different service implementations', () => {
const service1: ApplicationService = { serviceName: 'Service1' };
const service2: ApplicationService = { serviceName: 'Service2' };
const service3: ApplicationService = {};
expect(service1.serviceName).toBe('Service1');
expect(service2.serviceName).toBe('Service2');
expect(service3.serviceName).toBeUndefined();
});
});
describe('AsyncApplicationService interface', () => {
it('should have execute method returning Promise<Output>', async () => {
const service: AsyncApplicationService<string, number> = {
execute: async (input: string) => input.length
};
const result = await service.execute('test');
expect(result).toBe(4);
});
it('should support different input and output types', async () => {
const stringService: AsyncApplicationService<string, string> = {
execute: async (input: string) => input.toUpperCase()
};
const objectService: AsyncApplicationService<{ x: number; y: number }, number> = {
execute: async (input) => input.x + input.y
};
const arrayService: AsyncApplicationService<number[], number[]> = {
execute: async (input) => input.map(x => x * 2)
};
expect(await stringService.execute('hello')).toBe('HELLO');
expect(await objectService.execute({ x: 3, y: 4 })).toBe(7);
expect(await arrayService.execute([1, 2, 3])).toEqual([2, 4, 6]);
});
it('should support async operations with delays', async () => {
const service: AsyncApplicationService<string, string> = {
execute: async (input: string) => {
await new Promise(resolve => setTimeout(resolve, 10));
return `Processed: ${input}`;
}
};
const start = Date.now();
const result = await service.execute('test');
const elapsed = Date.now() - start;
expect(result).toBe('Processed: test');
expect(elapsed).toBeGreaterThanOrEqual(10);
});
it('should support complex async operations', async () => {
interface FetchUserInput {
userId: string;
includePosts?: boolean;
}
interface UserWithPosts {
id: string;
name: string;
email: string;
posts: Array<{ id: string; title: string; content: string }>;
}
const userService: AsyncApplicationService<FetchUserInput, UserWithPosts> = {
execute: async (input: FetchUserInput) => {
// Simulate async database fetch
await new Promise(resolve => setTimeout(resolve, 5));
const user: UserWithPosts = {
id: input.userId,
name: 'John Doe',
email: 'john@example.com',
posts: []
};
if (input.includePosts) {
user.posts = [
{ id: 'post-1', title: 'First Post', content: 'Content 1' },
{ id: 'post-2', title: 'Second Post', content: 'Content 2' }
];
}
return user;
}
};
const userWithPosts = await userService.execute({
userId: 'user-123',
includePosts: true
});
expect(userWithPosts.id).toBe('user-123');
expect(userWithPosts.posts).toHaveLength(2);
const userWithoutPosts = await userService.execute({
userId: 'user-456',
includePosts: false
});
expect(userWithoutPosts.id).toBe('user-456');
expect(userWithoutPosts.posts).toHaveLength(0);
});
});
describe('AsyncResultApplicationService interface', () => {
it('should have execute method returning Promise<Result>', async () => {
const service: AsyncResultApplicationService<string, number, string> = {
execute: async (input: string) => {
if (input.length === 0) {
return Result.err('Input cannot be empty');
}
return Result.ok(input.length);
}
};
const successResult = await service.execute('test');
expect(successResult.isOk()).toBe(true);
expect(successResult.unwrap()).toBe(4);
const errorResult = await service.execute('');
expect(errorResult.isErr()).toBe(true);
expect(errorResult.unwrapErr()).toBe('Input cannot be empty');
});
it('should support validation logic', async () => {
interface ValidationResult {
isValid: boolean;
errors: string[];
}
const validator: AsyncResultApplicationService<string, ValidationResult, string> = {
execute: async (input: string) => {
const errors: string[] = [];
if (input.length < 3) {
errors.push('Must be at least 3 characters');
}
if (!input.match(/^[a-zA-Z]+$/)) {
errors.push('Must contain only letters');
}
if (errors.length > 0) {
return Result.err(errors.join(', '));
}
return Result.ok({ isValid: true, errors: [] });
}
};
const validResult = await validator.execute('Hello');
expect(validResult.isOk()).toBe(true);
expect(validResult.unwrap()).toEqual({ isValid: true, errors: [] });
const invalidResult = await validator.execute('ab');
expect(invalidResult.isErr()).toBe(true);
expect(invalidResult.unwrapErr()).toBe('Must be at least 3 characters');
});
it('should support complex business rules', async () => {
interface ProcessOrderInput {
items: Array<{ productId: string; quantity: number; price: number }>;
customerType: 'regular' | 'premium' | 'vip';
hasCoupon: boolean;
}
interface OrderResult {
orderId: string;
subtotal: number;
discount: number;
total: number;
status: 'processed' | 'pending' | 'failed';
}
const orderProcessor: AsyncResultApplicationService<ProcessOrderInput, OrderResult, string> = {
execute: async (input: ProcessOrderInput) => {
// Calculate subtotal
const subtotal = input.items.reduce((sum, item) => sum + (item.quantity * item.price), 0);
if (subtotal <= 0) {
return Result.err('Order must have items with positive prices');
}
// Calculate discount
let discount = 0;
// Customer type discount
switch (input.customerType) {
case 'premium':
discount += subtotal * 0.1;
break;
case 'vip':
discount += subtotal * 0.2;
break;
}
// Coupon discount
if (input.hasCoupon) {
discount += subtotal * 0.05;
}
const total = subtotal - discount;
const orderResult: OrderResult = {
orderId: `order-${Date.now()}`,
subtotal,
discount,
total,
status: 'processed'
};
return Result.ok(orderResult);
}
};
// Success case - VIP with coupon
const vipWithCoupon = await orderProcessor.execute({
items: [
{ productId: 'prod-1', quantity: 2, price: 100 },
{ productId: 'prod-2', quantity: 1, price: 50 }
],
customerType: 'vip',
hasCoupon: true
});
expect(vipWithCoupon.isOk()).toBe(true);
const order1 = vipWithCoupon.unwrap();
expect(order1.subtotal).toBe(250); // 2*100 + 1*50
expect(order1.discount).toBe(62.5); // 250 * 0.2 + 250 * 0.05
expect(order1.total).toBe(187.5); // 250 - 62.5
// Success case - Regular without coupon
const regularWithoutCoupon = await orderProcessor.execute({
items: [{ productId: 'prod-1', quantity: 1, price: 100 }],
customerType: 'regular',
hasCoupon: false
});
expect(regularWithoutCoupon.isOk()).toBe(true);
const order2 = regularWithoutCoupon.unwrap();
expect(order2.subtotal).toBe(100);
expect(order2.discount).toBe(0);
expect(order2.total).toBe(100);
// Error case - Empty order
const emptyOrder = await orderProcessor.execute({
items: [],
customerType: 'regular',
hasCoupon: false
});
expect(emptyOrder.isErr()).toBe(true);
expect(emptyOrder.unwrapErr()).toBe('Order must have items with positive prices');
});
it('should support async operations with delays', async () => {
interface ProcessBatchInput {
items: Array<{ id: string; data: string }>;
delayMs?: number;
}
interface BatchResult {
processed: number;
failed: number;
results: Array<{ id: string; status: 'success' | 'failed'; message?: string }>;
}
const batchProcessor: AsyncResultApplicationService<ProcessBatchInput, BatchResult, string> = {
execute: async (input: ProcessBatchInput) => {
if (input.items.length === 0) {
return Result.err('Empty batch');
}
const delay = input.delayMs || 10;
const results: Array<{ id: string; status: 'success' | 'failed'; message?: string }> = [];
let processed = 0;
let failed = 0;
for (const item of input.items) {
// Simulate async processing with delay
await new Promise(resolve => setTimeout(resolve, delay));
// Simulate some failures
if (item.id === 'fail-1' || item.id === 'fail-2') {
results.push({ id: item.id, status: 'failed', message: 'Processing failed' });
failed++;
} else {
results.push({ id: item.id, status: 'success' });
processed++;
}
}
return Result.ok({
processed,
failed,
results
});
}
};
// Success case
const successResult = await batchProcessor.execute({
items: [
{ id: 'item-1', data: 'data1' },
{ id: 'item-2', data: 'data2' },
{ id: 'item-3', data: 'data3' }
],
delayMs: 5
});
expect(successResult.isOk()).toBe(true);
const batchResult = successResult.unwrap();
expect(batchResult.processed).toBe(3);
expect(batchResult.failed).toBe(0);
expect(batchResult.results).toHaveLength(3);
// Mixed success/failure case
const mixedResult = await batchProcessor.execute({
items: [
{ id: 'item-1', data: 'data1' },
{ id: 'fail-1', data: 'data2' },
{ id: 'item-3', data: 'data3' },
{ id: 'fail-2', data: 'data4' }
],
delayMs: 5
});
expect(mixedResult.isOk()).toBe(true);
const mixedBatchResult = mixedResult.unwrap();
expect(mixedBatchResult.processed).toBe(2);
expect(mixedBatchResult.failed).toBe(2);
expect(mixedBatchResult.results).toHaveLength(4);
// Error case - empty batch
const emptyResult = await batchProcessor.execute({ items: [] });
expect(emptyResult.isErr()).toBe(true);
expect(emptyResult.unwrapErr()).toBe('Empty batch');
});
it('should support error handling with ApplicationError', async () => {
interface ProcessPaymentInput {
amount: number;
currency: string;
}
interface PaymentReceipt {
receiptId: string;
amount: number;
currency: string;
status: 'completed' | 'failed';
}
const paymentProcessor: AsyncResultApplicationService<ProcessPaymentInput, PaymentReceipt, ApplicationError> = {
execute: async (input: ProcessPaymentInput) => {
// Validate amount
if (input.amount <= 0) {
return Result.err({
type: 'application',
context: 'payment',
kind: 'validation',
message: 'Amount must be positive'
} as ApplicationError);
}
// Validate currency
const supportedCurrencies = ['USD', 'EUR', 'GBP'];
if (!supportedCurrencies.includes(input.currency)) {
return Result.err({
type: 'application',
context: 'payment',
kind: 'validation',
message: `Currency ${input.currency} is not supported`
} as ApplicationError);
}
// Simulate payment processing
const receipt: PaymentReceipt = {
receiptId: `receipt-${Date.now()}`,
amount: input.amount,
currency: input.currency,
status: 'completed'
};
return Result.ok(receipt);
}
};
// Success case
const successResult = await paymentProcessor.execute({
amount: 100,
currency: 'USD'
});
expect(successResult.isOk()).toBe(true);
const receipt = successResult.unwrap();
expect(receipt.amount).toBe(100);
expect(receipt.currency).toBe('USD');
expect(receipt.status).toBe('completed');
// Error case - invalid amount
const invalidAmountResult = await paymentProcessor.execute({
amount: -50,
currency: 'USD'
});
expect(invalidAmountResult.isErr()).toBe(true);
const error1 = invalidAmountResult.unwrapErr();
expect(error1.type).toBe('application');
expect(error1.kind).toBe('validation');
expect(error1.message).toBe('Amount must be positive');
// Error case - invalid currency
const invalidCurrencyResult = await paymentProcessor.execute({
amount: 100,
currency: 'JPY'
});
expect(invalidCurrencyResult.isErr()).toBe(true);
const error2 = invalidCurrencyResult.unwrapErr();
expect(error2.type).toBe('application');
expect(error2.kind).toBe('validation');
expect(error2.message).toBe('Currency JPY is not supported');
});
});
});

View File

@@ -0,0 +1,324 @@
import { describe, it, expect } from 'vitest';
import { UseCase } from './UseCase';
import { Result } from '../domain/Result';
import { ApplicationErrorCode } from '../errors/ApplicationErrorCode';
describe('UseCase', () => {
describe('UseCase interface', () => {
it('should have execute method returning Promise<Result>', async () => {
// Concrete implementation for testing
class TestUseCase implements UseCase<{ value: number }, string, 'INVALID_VALUE'> {
async execute(input: { value: number }): Promise<Result<string, ApplicationErrorCode<'INVALID_VALUE'>>> {
if (input.value < 0) {
return Result.err({ code: 'INVALID_VALUE' });
}
return Result.ok(`Value: ${input.value}`);
}
}
const useCase = new TestUseCase();
const successResult = await useCase.execute({ value: 42 });
expect(successResult.isOk()).toBe(true);
expect(successResult.unwrap()).toBe('Value: 42');
const errorResult = await useCase.execute({ value: -1 });
expect(errorResult.isErr()).toBe(true);
expect(errorResult.unwrapErr()).toEqual({ code: 'INVALID_VALUE' });
});
it('should support different input types', async () => {
interface CreateUserInput {
name: string;
email: string;
}
interface UserDTO {
id: string;
name: string;
email: string;
}
type CreateUserErrorCode = 'INVALID_EMAIL' | 'USER_ALREADY_EXISTS';
class CreateUserUseCase implements UseCase<CreateUserInput, UserDTO, CreateUserErrorCode> {
async execute(input: CreateUserInput): Promise<Result<UserDTO, ApplicationErrorCode<CreateUserErrorCode>>> {
if (!input.email.includes('@')) {
return Result.err({ code: 'INVALID_EMAIL' });
}
// Simulate user creation
const user: UserDTO = {
id: `user-${Date.now()}`,
name: input.name,
email: input.email
};
return Result.ok(user);
}
}
const useCase = new CreateUserUseCase();
const successResult = await useCase.execute({
name: 'John Doe',
email: 'john@example.com'
});
expect(successResult.isOk()).toBe(true);
const user = successResult.unwrap();
expect(user.name).toBe('John Doe');
expect(user.email).toBe('john@example.com');
expect(user.id).toMatch(/^user-\d+$/);
const errorResult = await useCase.execute({
name: 'John Doe',
email: 'invalid-email'
});
expect(errorResult.isErr()).toBe(true);
expect(errorResult.unwrapErr()).toEqual({ code: 'INVALID_EMAIL' });
});
it('should support complex error codes', async () => {
interface ProcessPaymentInput {
amount: number;
currency: string;
paymentMethod: 'credit_card' | 'paypal' | 'bank_transfer';
}
interface PaymentReceipt {
receiptId: string;
amount: number;
currency: string;
status: 'completed' | 'pending' | 'failed';
}
type ProcessPaymentErrorCode =
| 'INSUFFICIENT_FUNDS'
| 'INVALID_CURRENCY'
| 'PAYMENT_METHOD_NOT_SUPPORTED'
| 'NETWORK_ERROR';
class ProcessPaymentUseCase implements UseCase<ProcessPaymentInput, PaymentReceipt, ProcessPaymentErrorCode> {
async execute(input: ProcessPaymentInput): Promise<Result<PaymentReceipt, ApplicationErrorCode<ProcessPaymentErrorCode>>> {
// Validate currency
const supportedCurrencies = ['USD', 'EUR', 'GBP'];
if (!supportedCurrencies.includes(input.currency)) {
return Result.err({ code: 'INVALID_CURRENCY' });
}
// Validate payment method
const supportedMethods = ['credit_card', 'paypal'];
if (!supportedMethods.includes(input.paymentMethod)) {
return Result.err({ code: 'PAYMENT_METHOD_NOT_SUPPORTED' });
}
// Simulate payment processing
if (input.amount > 10000) {
return Result.err({ code: 'INSUFFICIENT_FUNDS' });
}
const receipt: PaymentReceipt = {
receiptId: `receipt-${Date.now()}`,
amount: input.amount,
currency: input.currency,
status: 'completed'
};
return Result.ok(receipt);
}
}
const useCase = new ProcessPaymentUseCase();
// Success case
const successResult = await useCase.execute({
amount: 100,
currency: 'USD',
paymentMethod: 'credit_card'
});
expect(successResult.isOk()).toBe(true);
const receipt = successResult.unwrap();
expect(receipt.amount).toBe(100);
expect(receipt.currency).toBe('USD');
expect(receipt.status).toBe('completed');
// Error case - invalid currency
const currencyError = await useCase.execute({
amount: 100,
currency: 'JPY',
paymentMethod: 'credit_card'
});
expect(currencyError.isErr()).toBe(true);
expect(currencyError.unwrapErr()).toEqual({ code: 'INVALID_CURRENCY' });
// Error case - unsupported payment method
const methodError = await useCase.execute({
amount: 100,
currency: 'USD',
paymentMethod: 'bank_transfer'
});
expect(methodError.isErr()).toBe(true);
expect(methodError.unwrapErr()).toEqual({ code: 'PAYMENT_METHOD_NOT_SUPPORTED' });
// Error case - insufficient funds
const fundsError = await useCase.execute({
amount: 15000,
currency: 'USD',
paymentMethod: 'credit_card'
});
expect(fundsError.isErr()).toBe(true);
expect(fundsError.unwrapErr()).toEqual({ code: 'INSUFFICIENT_FUNDS' });
});
it('should support void success type', async () => {
interface DeleteUserInput {
userId: string;
}
type DeleteUserErrorCode = 'USER_NOT_FOUND' | 'INSUFFICIENT_PERMISSIONS';
class DeleteUserUseCase implements UseCase<DeleteUserInput, void, DeleteUserErrorCode> {
async execute(input: DeleteUserInput): Promise<Result<void, ApplicationErrorCode<DeleteUserErrorCode>>> {
if (input.userId === 'not-found') {
return Result.err({ code: 'USER_NOT_FOUND' });
}
if (input.userId === 'no-permission') {
return Result.err({ code: 'INSUFFICIENT_PERMISSIONS' });
}
// Simulate deletion
return Result.ok(undefined);
}
}
const useCase = new DeleteUserUseCase();
const successResult = await useCase.execute({ userId: 'user-123' });
expect(successResult.isOk()).toBe(true);
expect(successResult.unwrap()).toBeUndefined();
const notFoundResult = await useCase.execute({ userId: 'not-found' });
expect(notFoundResult.isErr()).toBe(true);
expect(notFoundResult.unwrapErr()).toEqual({ code: 'USER_NOT_FOUND' });
const permissionResult = await useCase.execute({ userId: 'no-permission' });
expect(permissionResult.isErr()).toBe(true);
expect(permissionResult.unwrapErr()).toEqual({ code: 'INSUFFICIENT_PERMISSIONS' });
});
it('should support complex success types', async () => {
interface SearchInput {
query: string;
filters?: {
category?: string;
priceRange?: { min: number; max: number };
inStock?: boolean;
};
page?: number;
limit?: number;
}
interface SearchResult {
items: Array<{
id: string;
name: string;
price: number;
category: string;
inStock: boolean;
}>;
total: number;
page: number;
totalPages: number;
}
type SearchErrorCode = 'INVALID_QUERY' | 'NO_RESULTS';
class SearchUseCase implements UseCase<SearchInput, SearchResult, SearchErrorCode> {
async execute(input: SearchInput): Promise<Result<SearchResult, ApplicationErrorCode<SearchErrorCode>>> {
if (!input.query || input.query.length < 2) {
return Result.err({ code: 'INVALID_QUERY' });
}
// Simulate search results
const items = [
{ id: '1', name: 'Product A', price: 100, category: 'electronics', inStock: true },
{ id: '2', name: 'Product B', price: 200, category: 'electronics', inStock: false },
{ id: '3', name: 'Product C', price: 150, category: 'clothing', inStock: true }
];
const filteredItems = items.filter(item => {
if (input.filters?.category && item.category !== input.filters.category) {
return false;
}
if (input.filters?.priceRange) {
if (item.price < input.filters.priceRange.min || item.price > input.filters.priceRange.max) {
return false;
}
}
if (input.filters?.inStock !== undefined && item.inStock !== input.filters.inStock) {
return false;
}
return true;
});
if (filteredItems.length === 0) {
return Result.err({ code: 'NO_RESULTS' });
}
const page = input.page || 1;
const limit = input.limit || 10;
const start = (page - 1) * limit;
const end = start + limit;
const paginatedItems = filteredItems.slice(start, end);
const result: SearchResult = {
items: paginatedItems,
total: filteredItems.length,
page,
totalPages: Math.ceil(filteredItems.length / limit)
};
return Result.ok(result);
}
}
const useCase = new SearchUseCase();
// Success case
const successResult = await useCase.execute({
query: 'product',
filters: { category: 'electronics' },
page: 1,
limit: 10
});
expect(successResult.isOk()).toBe(true);
const searchResult = successResult.unwrap();
expect(searchResult.items).toHaveLength(2);
expect(searchResult.total).toBe(2);
expect(searchResult.page).toBe(1);
expect(searchResult.totalPages).toBe(1);
// Error case - invalid query
const invalidQueryResult = await useCase.execute({ query: 'a' });
expect(invalidQueryResult.isErr()).toBe(true);
expect(invalidQueryResult.unwrapErr()).toEqual({ code: 'INVALID_QUERY' });
// Error case - no results
const noResultsResult = await useCase.execute({
query: 'product',
filters: { category: 'nonexistent' }
});
expect(noResultsResult.isErr()).toBe(true);
expect(noResultsResult.unwrapErr()).toEqual({ code: 'NO_RESULTS' });
});
});
});

View File

@@ -0,0 +1,433 @@
import { describe, it, expect } from 'vitest';
import { UseCaseOutputPort } from './UseCaseOutputPort';
describe('UseCaseOutputPort', () => {
describe('UseCaseOutputPort interface', () => {
it('should have present method', () => {
const presentedData: unknown[] = [];
const outputPort: UseCaseOutputPort<string> = {
present: (data: string) => {
presentedData.push(data);
}
};
outputPort.present('test data');
expect(presentedData).toHaveLength(1);
expect(presentedData[0]).toBe('test data');
});
it('should support different data types', () => {
const presentedData: Array<{ type: string; data: unknown }> = [];
const outputPort: UseCaseOutputPort<unknown> = {
present: (data: unknown) => {
presentedData.push({ type: typeof data, data });
}
};
outputPort.present('string data');
outputPort.present(42);
outputPort.present({ id: 1, name: 'test' });
outputPort.present([1, 2, 3]);
expect(presentedData).toHaveLength(4);
expect(presentedData[0]).toEqual({ type: 'string', data: 'string data' });
expect(presentedData[1]).toEqual({ type: 'number', data: 42 });
expect(presentedData[2]).toEqual({ type: 'object', data: { id: 1, name: 'test' } });
expect(presentedData[3]).toEqual({ type: 'object', data: [1, 2, 3] });
});
it('should support complex data structures', () => {
interface UserDTO {
id: string;
name: string;
email: string;
profile: {
avatar: string;
bio: string;
preferences: {
theme: 'light' | 'dark';
notifications: boolean;
};
};
metadata: {
createdAt: Date;
updatedAt: Date;
lastLogin?: Date;
};
}
const presentedUsers: UserDTO[] = [];
const outputPort: UseCaseOutputPort<UserDTO> = {
present: (data: UserDTO) => {
presentedUsers.push(data);
}
};
const user: UserDTO = {
id: 'user-123',
name: 'John Doe',
email: 'john@example.com',
profile: {
avatar: 'avatar.jpg',
bio: 'Software developer',
preferences: {
theme: 'dark',
notifications: true
}
},
metadata: {
createdAt: new Date('2024-01-01'),
updatedAt: new Date('2024-01-02'),
lastLogin: new Date('2024-01-03')
}
};
outputPort.present(user);
expect(presentedUsers).toHaveLength(1);
expect(presentedUsers[0]).toEqual(user);
});
it('should support array data', () => {
const presentedArrays: number[][] = [];
const outputPort: UseCaseOutputPort<number[]> = {
present: (data: number[]) => {
presentedArrays.push(data);
}
};
outputPort.present([1, 2, 3]);
outputPort.present([4, 5, 6]);
expect(presentedArrays).toHaveLength(2);
expect(presentedArrays[0]).toEqual([1, 2, 3]);
expect(presentedArrays[1]).toEqual([4, 5, 6]);
});
it('should support null and undefined values', () => {
const presentedValues: unknown[] = [];
const outputPort: UseCaseOutputPort<unknown> = {
present: (data: unknown) => {
presentedValues.push(data);
}
};
outputPort.present(null);
outputPort.present(undefined);
outputPort.present('value');
expect(presentedValues).toHaveLength(3);
expect(presentedValues[0]).toBe(null);
expect(presentedValues[1]).toBe(undefined);
expect(presentedValues[2]).toBe('value');
});
});
describe('UseCaseOutputPort behavior', () => {
it('should support transformation before presentation', () => {
const presentedData: Array<{ transformed: string; original: string }> = [];
const outputPort: UseCaseOutputPort<string> = {
present: (data: string) => {
const transformed = data.toUpperCase();
presentedData.push({ transformed, original: data });
}
};
outputPort.present('hello');
outputPort.present('world');
expect(presentedData).toHaveLength(2);
expect(presentedData[0]).toEqual({ transformed: 'HELLO', original: 'hello' });
expect(presentedData[1]).toEqual({ transformed: 'WORLD', original: 'world' });
});
it('should support validation before presentation', () => {
const presentedData: string[] = [];
const validationErrors: string[] = [];
const outputPort: UseCaseOutputPort<string> = {
present: (data: string) => {
if (data.length === 0) {
validationErrors.push('Data cannot be empty');
return;
}
if (data.length > 100) {
validationErrors.push('Data exceeds maximum length');
return;
}
presentedData.push(data);
}
};
outputPort.present('valid data');
outputPort.present('');
outputPort.present('a'.repeat(101));
outputPort.present('another valid');
expect(presentedData).toHaveLength(2);
expect(presentedData[0]).toBe('valid data');
expect(presentedData[1]).toBe('another valid');
expect(validationErrors).toHaveLength(2);
expect(validationErrors[0]).toBe('Data cannot be empty');
expect(validationErrors[1]).toBe('Data exceeds maximum length');
});
it('should support pagination', () => {
interface PaginatedResult<T> {
items: T[];
total: number;
page: number;
totalPages: number;
}
const presentedPages: PaginatedResult<string>[] = [];
const outputPort: UseCaseOutputPort<PaginatedResult<string>> = {
present: (data: PaginatedResult<string>) => {
presentedPages.push(data);
}
};
const page1: PaginatedResult<string> = {
items: ['item-1', 'item-2', 'item-3'],
total: 10,
page: 1,
totalPages: 4
};
const page2: PaginatedResult<string> = {
items: ['item-4', 'item-5', 'item-6'],
total: 10,
page: 2,
totalPages: 4
};
outputPort.present(page1);
outputPort.present(page2);
expect(presentedPages).toHaveLength(2);
expect(presentedPages[0]).toEqual(page1);
expect(presentedPages[1]).toEqual(page2);
});
it('should support streaming presentation', () => {
const stream: string[] = [];
const outputPort: UseCaseOutputPort<string> = {
present: (data: string) => {
stream.push(data);
}
};
// Simulate streaming data
const chunks = ['chunk-1', 'chunk-2', 'chunk-3', 'chunk-4'];
chunks.forEach(chunk => outputPort.present(chunk));
expect(stream).toHaveLength(4);
expect(stream).toEqual(chunks);
});
it('should support error handling in presentation', () => {
const presentedData: string[] = [];
const presentationErrors: Error[] = [];
const outputPort: UseCaseOutputPort<string> = {
present: (data: string) => {
try {
// Simulate complex presentation logic that might fail
if (data === 'error') {
throw new Error('Presentation failed');
}
presentedData.push(data);
} catch (error) {
if (error instanceof Error) {
presentationErrors.push(error);
}
}
}
};
outputPort.present('valid-1');
outputPort.present('error');
outputPort.present('valid-2');
expect(presentedData).toHaveLength(2);
expect(presentedData[0]).toBe('valid-1');
expect(presentedData[1]).toBe('valid-2');
expect(presentationErrors).toHaveLength(1);
expect(presentationErrors[0].message).toBe('Presentation failed');
});
});
describe('UseCaseOutputPort implementation patterns', () => {
it('should support console presenter', () => {
const consoleOutputs: string[] = [];
const originalConsoleLog = console.log;
// Mock console.log
console.log = (...args: unknown[]) => consoleOutputs.push(args.join(' '));
const consolePresenter: UseCaseOutputPort<string> = {
present: (data: string) => {
console.log('Presented:', data);
}
};
consolePresenter.present('test data');
// Restore console.log
console.log = originalConsoleLog;
expect(consoleOutputs).toHaveLength(1);
expect(consoleOutputs[0]).toContain('Presented:');
expect(consoleOutputs[0]).toContain('test data');
});
it('should support HTTP response presenter', () => {
const responses: Array<{ status: number; body: unknown; headers?: Record<string, string> }> = [];
const httpResponsePresenter: UseCaseOutputPort<unknown> = {
present: (data: unknown) => {
responses.push({
status: 200,
body: data,
headers: {
'content-type': 'application/json'
}
});
}
};
httpResponsePresenter.present({ id: 1, name: 'test' });
expect(responses).toHaveLength(1);
expect(responses[0].status).toBe(200);
expect(responses[0].body).toEqual({ id: 1, name: 'test' });
expect(responses[0].headers).toEqual({ 'content-type': 'application/json' });
});
it('should support WebSocket presenter', () => {
const messages: Array<{ type: string; data: unknown; timestamp: string }> = [];
const webSocketPresenter: UseCaseOutputPort<unknown> = {
present: (data: unknown) => {
messages.push({
type: 'data',
data,
timestamp: new Date().toISOString()
});
}
};
webSocketPresenter.present({ event: 'user-joined', userId: 'user-123' });
expect(messages).toHaveLength(1);
expect(messages[0].type).toBe('data');
expect(messages[0].data).toEqual({ event: 'user-joined', userId: 'user-123' });
expect(messages[0].timestamp).toBeDefined();
});
it('should support event bus presenter', () => {
const events: Array<{ topic: string; payload: unknown; metadata: unknown }> = [];
const eventBusPresenter: UseCaseOutputPort<unknown> = {
present: (data: unknown) => {
events.push({
topic: 'user-events',
payload: data,
metadata: {
source: 'use-case',
timestamp: new Date().toISOString()
}
});
}
};
eventBusPresenter.present({ userId: 'user-123', action: 'created' });
expect(events).toHaveLength(1);
expect(events[0].topic).toBe('user-events');
expect(events[0].payload).toEqual({ userId: 'user-123', action: 'created' });
expect(events[0].metadata).toMatchObject({ source: 'use-case' });
});
it('should support batch presenter', () => {
const batches: Array<{ items: unknown[]; batchSize: number; processedAt: string }> = [];
let currentBatch: unknown[] = [];
const batchSize = 3;
const batchPresenter: UseCaseOutputPort<unknown> = {
present: (data: unknown) => {
currentBatch.push(data);
if (currentBatch.length >= batchSize) {
batches.push({
items: [...currentBatch],
batchSize: currentBatch.length,
processedAt: new Date().toISOString()
});
currentBatch = [];
}
}
};
// Present 7 items
for (let i = 1; i <= 7; i++) {
batchPresenter.present({ item: i });
}
// Add remaining items
if (currentBatch.length > 0) {
batches.push({
items: [...currentBatch],
batchSize: currentBatch.length,
processedAt: new Date().toISOString()
});
}
expect(batches).toHaveLength(3);
expect(batches[0].items).toHaveLength(3);
expect(batches[1].items).toHaveLength(3);
expect(batches[2].items).toHaveLength(1);
});
it('should support caching presenter', () => {
const cache = new Map<string, unknown>();
const cacheHits: string[] = [];
const cacheMisses: string[] = [];
const cachingPresenter: UseCaseOutputPort<{ key: string; data: unknown }> = {
present: (data: { key: string; data: unknown }) => {
if (cache.has(data.key)) {
cacheHits.push(data.key);
// Update cache with new data even if key exists
cache.set(data.key, data.data);
} else {
cacheMisses.push(data.key);
cache.set(data.key, data.data);
}
}
};
cachingPresenter.present({ key: 'user-1', data: { name: 'John' } });
cachingPresenter.present({ key: 'user-2', data: { name: 'Jane' } });
cachingPresenter.present({ key: 'user-1', data: { name: 'John Updated' } });
expect(cacheHits).toHaveLength(1);
expect(cacheHits[0]).toBe('user-1');
expect(cacheMisses).toHaveLength(2);
expect(cacheMisses[0]).toBe('user-1');
expect(cacheMisses[1]).toBe('user-2');
expect(cache.get('user-1')).toEqual({ name: 'John Updated' });
});
});
});

View File

@@ -0,0 +1,297 @@
import { describe, it, expect } from 'vitest';
import { DomainEvent, DomainEventPublisher, DomainEventAlias } from './DomainEvent';
describe('DomainEvent', () => {
describe('DomainEvent interface', () => {
it('should have required properties', () => {
const event: DomainEvent<{ userId: string }> = {
eventType: 'USER_CREATED',
aggregateId: 'user-123',
eventData: { userId: 'user-123' },
occurredAt: new Date('2024-01-01T00:00:00Z')
};
expect(event.eventType).toBe('USER_CREATED');
expect(event.aggregateId).toBe('user-123');
expect(event.eventData).toEqual({ userId: 'user-123' });
expect(event.occurredAt).toEqual(new Date('2024-01-01T00:00:00Z'));
});
it('should support different event data types', () => {
const stringEvent: DomainEvent<string> = {
eventType: 'STRING_EVENT',
aggregateId: 'agg-1',
eventData: 'some data',
occurredAt: new Date()
};
const objectEvent: DomainEvent<{ id: number; name: string }> = {
eventType: 'OBJECT_EVENT',
aggregateId: 'agg-2',
eventData: { id: 1, name: 'test' },
occurredAt: new Date()
};
const arrayEvent: DomainEvent<string[]> = {
eventType: 'ARRAY_EVENT',
aggregateId: 'agg-3',
eventData: ['a', 'b', 'c'],
occurredAt: new Date()
};
expect(stringEvent.eventData).toBe('some data');
expect(objectEvent.eventData).toEqual({ id: 1, name: 'test' });
expect(arrayEvent.eventData).toEqual(['a', 'b', 'c']);
});
it('should support default unknown type', () => {
const event: DomainEvent = {
eventType: 'UNKNOWN_EVENT',
aggregateId: 'agg-1',
eventData: { any: 'data' },
occurredAt: new Date()
};
expect(event.eventType).toBe('UNKNOWN_EVENT');
expect(event.aggregateId).toBe('agg-1');
});
it('should support complex event data structures', () => {
interface ComplexEventData {
userId: string;
changes: {
field: string;
oldValue: unknown;
newValue: unknown;
}[];
metadata: {
source: string;
timestamp: string;
};
}
const event: DomainEvent<ComplexEventData> = {
eventType: 'USER_UPDATED',
aggregateId: 'user-456',
eventData: {
userId: 'user-456',
changes: [
{ field: 'email', oldValue: 'old@example.com', newValue: 'new@example.com' }
],
metadata: {
source: 'admin-panel',
timestamp: '2024-01-01T12:00:00Z'
}
},
occurredAt: new Date('2024-01-01T12:00:00Z')
};
expect(event.eventData.userId).toBe('user-456');
expect(event.eventData.changes).toHaveLength(1);
expect(event.eventData.metadata.source).toBe('admin-panel');
});
});
describe('DomainEventPublisher interface', () => {
it('should have publish method', async () => {
const mockPublisher: DomainEventPublisher = {
publish: async (event: DomainEvent) => {
// Mock implementation
return Promise.resolve();
}
};
const event: DomainEvent<{ message: string }> = {
eventType: 'TEST_EVENT',
aggregateId: 'test-1',
eventData: { message: 'test' },
occurredAt: new Date()
};
// Should not throw
await expect(mockPublisher.publish(event)).resolves.toBeUndefined();
});
it('should support async publish operations', async () => {
const publishedEvents: DomainEvent[] = [];
const mockPublisher: DomainEventPublisher = {
publish: async (event: DomainEvent) => {
publishedEvents.push(event);
// Simulate async operation
await new Promise(resolve => setTimeout(resolve, 10));
return Promise.resolve();
}
};
const event1: DomainEvent = {
eventType: 'EVENT_1',
aggregateId: 'agg-1',
eventData: { data: 'value1' },
occurredAt: new Date()
};
const event2: DomainEvent = {
eventType: 'EVENT_2',
aggregateId: 'agg-2',
eventData: { data: 'value2' },
occurredAt: new Date()
};
await mockPublisher.publish(event1);
await mockPublisher.publish(event2);
expect(publishedEvents).toHaveLength(2);
expect(publishedEvents[0].eventType).toBe('EVENT_1');
expect(publishedEvents[1].eventType).toBe('EVENT_2');
});
});
describe('DomainEvent behavior', () => {
it('should support event ordering by occurredAt', () => {
const events: DomainEvent[] = [
{
eventType: 'EVENT_3',
aggregateId: 'agg-3',
eventData: {},
occurredAt: new Date('2024-01-03T00:00:00Z')
},
{
eventType: 'EVENT_1',
aggregateId: 'agg-1',
eventData: {},
occurredAt: new Date('2024-01-01T00:00:00Z')
},
{
eventType: 'EVENT_2',
aggregateId: 'agg-2',
eventData: {},
occurredAt: new Date('2024-01-02T00:00:00Z')
}
];
const sorted = [...events].sort((a, b) =>
a.occurredAt.getTime() - b.occurredAt.getTime()
);
expect(sorted[0].eventType).toBe('EVENT_1');
expect(sorted[1].eventType).toBe('EVENT_2');
expect(sorted[2].eventType).toBe('EVENT_3');
});
it('should support filtering events by aggregateId', () => {
const events: DomainEvent[] = [
{ eventType: 'EVENT_1', aggregateId: 'user-1', eventData: {}, occurredAt: new Date() },
{ eventType: 'EVENT_2', aggregateId: 'user-2', eventData: {}, occurredAt: new Date() },
{ eventType: 'EVENT_3', aggregateId: 'user-1', eventData: {}, occurredAt: new Date() }
];
const user1Events = events.filter(e => e.aggregateId === 'user-1');
expect(user1Events).toHaveLength(2);
expect(user1Events[0].eventType).toBe('EVENT_1');
expect(user1Events[1].eventType).toBe('EVENT_3');
});
it('should support event replay from event store', () => {
// Simulating event replay pattern
const eventStore: DomainEvent[] = [
{
eventType: 'USER_CREATED',
aggregateId: 'user-123',
eventData: { userId: 'user-123', name: 'John' },
occurredAt: new Date('2024-01-01T00:00:00Z')
},
{
eventType: 'USER_UPDATED',
aggregateId: 'user-123',
eventData: { userId: 'user-123', email: 'john@example.com' },
occurredAt: new Date('2024-01-02T00:00:00Z')
}
];
// Replay events to build current state
let currentState: { userId: string; name?: string; email?: string } = { userId: 'user-123' };
for (const event of eventStore) {
if (event.eventType === 'USER_CREATED') {
const data = event.eventData as { userId: string; name: string };
currentState.name = data.name;
} else if (event.eventType === 'USER_UPDATED') {
const data = event.eventData as { userId: string; email: string };
currentState.email = data.email;
}
}
expect(currentState.name).toBe('John');
expect(currentState.email).toBe('john@example.com');
});
it('should support event sourcing pattern', () => {
// Event sourcing: state is derived from events
interface AccountState {
balance: number;
transactions: number;
}
const events: DomainEvent[] = [
{
eventType: 'ACCOUNT_CREATED',
aggregateId: 'account-1',
eventData: { initialBalance: 100 },
occurredAt: new Date('2024-01-01T00:00:00Z')
},
{
eventType: 'DEPOSIT',
aggregateId: 'account-1',
eventData: { amount: 50 },
occurredAt: new Date('2024-01-02T00:00:00Z')
},
{
eventType: 'WITHDRAWAL',
aggregateId: 'account-1',
eventData: { amount: 30 },
occurredAt: new Date('2024-01-03T00:00:00Z')
}
];
const state: AccountState = {
balance: 0,
transactions: 0
};
for (const event of events) {
switch (event.eventType) {
case 'ACCOUNT_CREATED':
state.balance = (event.eventData as { initialBalance: number }).initialBalance;
state.transactions = 1;
break;
case 'DEPOSIT':
state.balance += (event.eventData as { amount: number }).amount;
state.transactions += 1;
break;
case 'WITHDRAWAL':
state.balance -= (event.eventData as { amount: number }).amount;
state.transactions += 1;
break;
}
}
expect(state.balance).toBe(120); // 100 + 50 - 30
expect(state.transactions).toBe(3);
});
});
describe('DomainEventAlias type', () => {
it('should be assignable to DomainEvent', () => {
const alias: DomainEventAlias<{ id: string }> = {
eventType: 'TEST',
aggregateId: 'agg-1',
eventData: { id: 'test' },
occurredAt: new Date()
};
expect(alias.eventType).toBe('TEST');
expect(alias.aggregateId).toBe('agg-1');
});
});
});

View File

@@ -0,0 +1,174 @@
import { describe, it, expect } from 'vitest';
import { Entity, EntityProps, EntityAlias } from './Entity';
// Concrete implementation for testing
class TestEntity extends Entity<string> {
constructor(id: string) {
super(id);
}
}
describe('Entity', () => {
describe('EntityProps interface', () => {
it('should have readonly id property', () => {
const props: EntityProps<string> = { id: 'test-id' };
expect(props.id).toBe('test-id');
});
it('should support different id types', () => {
const stringProps: EntityProps<string> = { id: 'test-id' };
const numberProps: EntityProps<number> = { id: 123 };
const uuidProps: EntityProps<string> = { id: '550e8400-e29b-41d4-a716-446655440000' };
expect(stringProps.id).toBe('test-id');
expect(numberProps.id).toBe(123);
expect(uuidProps.id).toBe('550e8400-e29b-41d4-a716-446655440000');
});
});
describe('Entity class', () => {
it('should have id property', () => {
const entity = new TestEntity('entity-123');
expect(entity.id).toBe('entity-123');
});
it('should have equals method', () => {
const entity1 = new TestEntity('entity-123');
const entity2 = new TestEntity('entity-123');
const entity3 = new TestEntity('entity-456');
expect(entity1.equals(entity2)).toBe(true);
expect(entity1.equals(entity3)).toBe(false);
});
it('should return false when comparing with undefined', () => {
const entity = new TestEntity('entity-123');
expect(entity.equals(undefined)).toBe(false);
});
it('should return false when comparing with null', () => {
const entity = new TestEntity('entity-123');
expect(entity.equals(null)).toBe(false);
});
it('should return false when comparing with entity of different type', () => {
const entity1 = new TestEntity('entity-123');
const entity2 = new TestEntity('entity-123');
// Even with same ID, if they're different entity types, equals should work
// since it only compares IDs
expect(entity1.equals(entity2)).toBe(true);
});
it('should support numeric IDs', () => {
class NumericEntity extends Entity<number> {
constructor(id: number) {
super(id);
}
}
const entity1 = new NumericEntity(123);
const entity2 = new NumericEntity(123);
const entity3 = new NumericEntity(456);
expect(entity1.id).toBe(123);
expect(entity1.equals(entity2)).toBe(true);
expect(entity1.equals(entity3)).toBe(false);
});
it('should support UUID IDs', () => {
const uuid1 = '550e8400-e29b-41d4-a716-446655440000';
const uuid2 = '550e8400-e29b-41d4-a716-446655440000';
const uuid3 = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
const entity1 = new TestEntity(uuid1);
const entity2 = new TestEntity(uuid2);
const entity3 = new TestEntity(uuid3);
expect(entity1.equals(entity2)).toBe(true);
expect(entity1.equals(entity3)).toBe(false);
});
it('should be immutable - id cannot be changed', () => {
const entity = new TestEntity('entity-123');
// Try to change id (should not work in TypeScript, but testing runtime)
// @ts-expect-error - Testing immutability
expect(() => entity.id = 'new-id').toThrow();
// ID should remain unchanged
expect(entity.id).toBe('entity-123');
});
});
describe('EntityAlias type', () => {
it('should be assignable to EntityProps', () => {
const alias: EntityAlias<string> = { id: 'test-id' };
expect(alias.id).toBe('test-id');
});
it('should work with different ID types', () => {
const stringAlias: EntityAlias<string> = { id: 'test' };
const numberAlias: EntityAlias<number> = { id: 42 };
const uuidAlias: EntityAlias<string> = { id: '550e8400-e29b-41d4-a716-446655440000' };
expect(stringAlias.id).toBe('test');
expect(numberAlias.id).toBe(42);
expect(uuidAlias.id).toBe('550e8400-e29b-41d4-a716-446655440000');
});
});
});
describe('Entity behavior', () => {
it('should support entity identity', () => {
// Entities are identified by their ID
const entity1 = new TestEntity('same-id');
const entity2 = new TestEntity('same-id');
const entity3 = new TestEntity('different-id');
// Same ID = same identity
expect(entity1.equals(entity2)).toBe(true);
// Different ID = different identity
expect(entity1.equals(entity3)).toBe(false);
});
it('should support entity reference equality', () => {
const entity = new TestEntity('entity-123');
// Same instance should equal itself
expect(entity.equals(entity)).toBe(true);
});
it('should work with complex ID types', () => {
interface ComplexId {
tenant: string;
sequence: number;
}
class ComplexEntity extends Entity<ComplexId> {
constructor(id: ComplexId) {
super(id);
}
equals(other?: Entity<ComplexId>): boolean {
if (!other) return false;
return (
this.id.tenant === other.id.tenant &&
this.id.sequence === other.id.sequence
);
}
}
const id1: ComplexId = { tenant: 'org-a', sequence: 1 };
const id2: ComplexId = { tenant: 'org-a', sequence: 1 };
const id3: ComplexId = { tenant: 'org-b', sequence: 1 };
const entity1 = new ComplexEntity(id1);
const entity2 = new ComplexEntity(id2);
const entity3 = new ComplexEntity(id3);
expect(entity1.equals(entity2)).toBe(true);
expect(entity1.equals(entity3)).toBe(false);
});
});

View File

@@ -3,7 +3,15 @@ export interface EntityProps<Id = string> {
}
export abstract class Entity<Id> implements EntityProps<Id> {
protected constructor(readonly id: Id) {}
protected constructor(readonly id: Id) {
// Make the id property truly immutable at runtime
Object.defineProperty(this, 'id', {
value: id,
writable: false,
enumerable: true,
configurable: false
});
}
equals(other?: Entity<Id>): boolean {
return !!other && this.id === other.id;

View File

@@ -0,0 +1,372 @@
import { describe, it, expect } from 'vitest';
import { Logger } from './Logger';
describe('Logger', () => {
describe('Logger interface', () => {
it('should have debug method', () => {
const logs: Array<{ message: string; context?: unknown }> = [];
const logger: Logger = {
debug: (message: string, context?: unknown) => {
logs.push({ message, context });
},
info: () => {},
warn: () => {},
error: () => {}
};
logger.debug('Debug message', { userId: 123 });
expect(logs).toHaveLength(1);
expect(logs[0].message).toBe('Debug message');
expect(logs[0].context).toEqual({ userId: 123 });
});
it('should have info method', () => {
const logs: Array<{ message: string; context?: unknown }> = [];
const logger: Logger = {
debug: () => {},
info: (message: string, context?: unknown) => {
logs.push({ message, context });
},
warn: () => {},
error: () => {}
};
logger.info('Info message', { action: 'login' });
expect(logs).toHaveLength(1);
expect(logs[0].message).toBe('Info message');
expect(logs[0].context).toEqual({ action: 'login' });
});
it('should have warn method', () => {
const logs: Array<{ message: string; context?: unknown }> = [];
const logger: Logger = {
debug: () => {},
info: () => {},
warn: (message: string, context?: unknown) => {
logs.push({ message, context });
},
error: () => {}
};
logger.warn('Warning message', { threshold: 0.8 });
expect(logs).toHaveLength(1);
expect(logs[0].message).toBe('Warning message');
expect(logs[0].context).toEqual({ threshold: 0.8 });
});
it('should have error method', () => {
const logs: Array<{ message: string; error?: Error; context?: unknown }> = [];
const logger: Logger = {
debug: () => {},
info: () => {},
warn: () => {},
error: (message: string, error?: Error, context?: unknown) => {
logs.push({ message, error, context });
}
};
const testError = new Error('Test error');
logger.error('Error occurred', testError, { userId: 456 });
expect(logs).toHaveLength(1);
expect(logs[0].message).toBe('Error occurred');
expect(logs[0].error).toBe(testError);
expect(logs[0].context).toEqual({ userId: 456 });
});
it('should support logging without context', () => {
const logs: string[] = [];
const logger: Logger = {
debug: (message) => logs.push(`DEBUG: ${message}`),
info: (message) => logs.push(`INFO: ${message}`),
warn: (message) => logs.push(`WARN: ${message}`),
error: (message) => logs.push(`ERROR: ${message}`)
};
logger.debug('Debug without context');
logger.info('Info without context');
logger.warn('Warn without context');
logger.error('Error without context');
expect(logs).toHaveLength(4);
expect(logs[0]).toBe('DEBUG: Debug without context');
expect(logs[1]).toBe('INFO: Info without context');
expect(logs[2]).toBe('WARN: Warn without context');
expect(logs[3]).toBe('ERROR: Error without context');
});
});
describe('Logger behavior', () => {
it('should support structured logging', () => {
const logs: Array<{ level: string; message: string; timestamp: string; data: unknown }> = [];
const logger: Logger = {
debug: (message, context) => {
logs.push({ level: 'debug', message, timestamp: new Date().toISOString(), data: context });
},
info: (message, context) => {
logs.push({ level: 'info', message, timestamp: new Date().toISOString(), data: context });
},
warn: (message, context) => {
logs.push({ level: 'warn', message, timestamp: new Date().toISOString(), data: context });
},
error: (message, error, context) => {
const data: Record<string, unknown> = { error };
if (context) {
Object.assign(data, context);
}
logs.push({ level: 'error', message, timestamp: new Date().toISOString(), data });
}
};
logger.info('User logged in', { userId: 'user-123', ip: '192.168.1.1' });
expect(logs).toHaveLength(1);
expect(logs[0].level).toBe('info');
expect(logs[0].message).toBe('User logged in');
expect(logs[0].data).toEqual({ userId: 'user-123', ip: '192.168.1.1' });
});
it('should support log level filtering', () => {
const logs: string[] = [];
const logger: Logger = {
debug: (message) => logs.push(`[DEBUG] ${message}`),
info: (message) => logs.push(`[INFO] ${message}`),
warn: (message) => logs.push(`[WARN] ${message}`),
error: (message) => logs.push(`[ERROR] ${message}`)
};
// Simulate different log levels
logger.debug('This is a debug message');
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error('This is an error message');
expect(logs).toHaveLength(4);
expect(logs[0]).toContain('[DEBUG]');
expect(logs[1]).toContain('[INFO]');
expect(logs[2]).toContain('[WARN]');
expect(logs[3]).toContain('[ERROR]');
});
it('should support error logging with stack trace', () => {
const logs: Array<{ message: string; error: Error; context?: unknown }> = [];
const logger: Logger = {
debug: () => {},
info: () => {},
warn: () => {},
error: (message: string, error?: Error, context?: unknown) => {
if (error) {
logs.push({ message, error, context });
}
}
};
const error = new Error('Database connection failed');
logger.error('Failed to connect to database', error, { retryCount: 3 });
expect(logs).toHaveLength(1);
expect(logs[0].message).toBe('Failed to connect to database');
expect(logs[0].error.message).toBe('Database connection failed');
expect(logs[0].error.stack).toBeDefined();
expect(logs[0].context).toEqual({ retryCount: 3 });
});
it('should support logging complex objects', () => {
const logs: Array<{ message: string; context: unknown }> = [];
const logger: Logger = {
debug: () => {},
info: (message, context) => logs.push({ message, context }),
warn: () => {},
error: () => {}
};
const complexObject = {
user: {
id: 'user-123',
profile: {
name: 'John Doe',
email: 'john@example.com',
preferences: {
theme: 'dark',
notifications: true
}
}
},
session: {
id: 'session-456',
expiresAt: new Date('2024-12-31T23:59:59Z')
}
};
logger.info('User session data', complexObject);
expect(logs).toHaveLength(1);
expect(logs[0].message).toBe('User session data');
expect(logs[0].context).toEqual(complexObject);
});
it('should support logging arrays', () => {
const logs: Array<{ message: string; context: unknown }> = [];
const logger: Logger = {
debug: () => {},
info: (message, context) => logs.push({ message, context }),
warn: () => {},
error: () => {}
};
const items = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' }
];
logger.info('Processing items', { items, count: items.length });
expect(logs).toHaveLength(1);
expect(logs[0].message).toBe('Processing items');
expect(logs[0].context).toEqual({ items, count: 3 });
});
it('should support logging with null and undefined values', () => {
const logs: Array<{ message: string; context?: unknown }> = [];
const logger: Logger = {
debug: () => {},
info: (message, context) => logs.push({ message, context }),
warn: () => {},
error: () => {}
};
logger.info('Null value', null);
logger.info('Undefined value', undefined);
logger.info('Mixed values', { a: null, b: undefined, c: 'value' });
expect(logs).toHaveLength(3);
expect(logs[0].context).toBe(null);
expect(logs[1].context).toBe(undefined);
expect(logs[2].context).toEqual({ a: null, b: undefined, c: 'value' });
});
});
describe('Logger implementation patterns', () => {
it('should support console logger implementation', () => {
const consoleLogs: string[] = [];
const originalConsoleDebug = console.debug;
const originalConsoleInfo = console.info;
const originalConsoleWarn = console.warn;
const originalConsoleError = console.error;
// Mock console methods
console.debug = (...args: unknown[]) => consoleLogs.push(`DEBUG: ${args.join(' ')}`);
console.info = (...args: unknown[]) => consoleLogs.push(`INFO: ${args.join(' ')}`);
console.warn = (...args: unknown[]) => consoleLogs.push(`WARN: ${args.join(' ')}`);
console.error = (...args: unknown[]) => consoleLogs.push(`ERROR: ${args.join(' ')}`);
const consoleLogger: Logger = {
debug: (message, context) => console.debug(message, context),
info: (message, context) => console.info(message, context),
warn: (message, context) => console.warn(message, context),
error: (message, error, context) => console.error(message, error, context)
};
consoleLogger.debug('Debug message', { data: 'test' });
consoleLogger.info('Info message', { data: 'test' });
consoleLogger.warn('Warn message', { data: 'test' });
consoleLogger.error('Error message', new Error('Test'), { data: 'test' });
// Restore console methods
console.debug = originalConsoleDebug;
console.info = originalConsoleInfo;
console.warn = originalConsoleWarn;
console.error = originalConsoleError;
expect(consoleLogs).toHaveLength(4);
expect(consoleLogs[0]).toContain('DEBUG:');
expect(consoleLogs[1]).toContain('INFO:');
expect(consoleLogs[2]).toContain('WARN:');
expect(consoleLogs[3]).toContain('ERROR:');
});
it('should support file logger implementation', () => {
const fileLogs: Array<{ timestamp: string; level: string; message: string; data?: unknown }> = [];
const fileLogger: Logger = {
debug: (message, context) => {
fileLogs.push({ timestamp: new Date().toISOString(), level: 'DEBUG', message, data: context });
},
info: (message, context) => {
fileLogs.push({ timestamp: new Date().toISOString(), level: 'INFO', message, data: context });
},
warn: (message, context) => {
fileLogs.push({ timestamp: new Date().toISOString(), level: 'WARN', message, data: context });
},
error: (message, error, context) => {
const data: Record<string, unknown> = { error };
if (context) {
Object.assign(data, context);
}
fileLogs.push({ timestamp: new Date().toISOString(), level: 'ERROR', message, data });
}
};
fileLogger.info('Application started', { version: '1.0.0' });
fileLogger.warn('High memory usage', { usage: '85%' });
fileLogger.error('Database error', new Error('Connection timeout'), { retry: 3 });
expect(fileLogs).toHaveLength(3);
expect(fileLogs[0].level).toBe('INFO');
expect(fileLogs[1].level).toBe('WARN');
expect(fileLogs[2].level).toBe('ERROR');
expect(fileLogs[0].data).toEqual({ version: '1.0.0' });
});
it('should support remote logger implementation', async () => {
const remoteLogs: Array<{ level: string; message: string; context?: unknown }> = [];
const remoteLogger: Logger = {
debug: async (message, context) => {
remoteLogs.push({ level: 'debug', message, context });
await new Promise(resolve => setTimeout(resolve, 1));
},
info: async (message, context) => {
remoteLogs.push({ level: 'info', message, context });
await new Promise(resolve => setTimeout(resolve, 1));
},
warn: async (message, context) => {
remoteLogs.push({ level: 'warn', message, context });
await new Promise(resolve => setTimeout(resolve, 1));
},
error: async (message, error, context) => {
const errorContext: Record<string, unknown> = { error };
if (context) {
Object.assign(errorContext, context);
}
remoteLogs.push({ level: 'error', message, context: errorContext });
await new Promise(resolve => setTimeout(resolve, 1));
}
};
await remoteLogger.info('User action', { action: 'click', element: 'button' });
await remoteLogger.warn('Performance warning', { duration: '2000ms' });
await remoteLogger.error('API failure', new Error('404 Not Found'), { endpoint: '/api/users' });
expect(remoteLogs).toHaveLength(3);
expect(remoteLogs[0].level).toBe('info');
expect(remoteLogs[1].level).toBe('warn');
expect(remoteLogs[2].level).toBe('error');
});
});
});

View File

@@ -0,0 +1,120 @@
import { describe, it, expect } from 'vitest';
import { coalesce, present } from './Option';
describe('Option', () => {
describe('coalesce()', () => {
it('should return the value when it is defined', () => {
expect(coalesce('defined', 'fallback')).toBe('defined');
expect(coalesce(42, 0)).toBe(42);
expect(coalesce(true, false)).toBe(true);
});
it('should return the fallback when value is undefined', () => {
expect(coalesce(undefined, 'fallback')).toBe('fallback');
expect(coalesce(undefined, 42)).toBe(42);
expect(coalesce(undefined, true)).toBe(true);
});
it('should return the fallback when value is null', () => {
expect(coalesce(null, 'fallback')).toBe('fallback');
expect(coalesce(null, 42)).toBe(42);
expect(coalesce(null, true)).toBe(true);
});
it('should handle complex fallback values', () => {
const fallback = { id: 0, name: 'default' };
expect(coalesce(undefined, fallback)).toEqual(fallback);
expect(coalesce(null, fallback)).toEqual(fallback);
expect(coalesce({ id: 1, name: 'actual' }, fallback)).toEqual({ id: 1, name: 'actual' });
});
it('should handle array values', () => {
const fallback = [1, 2, 3];
expect(coalesce(undefined, fallback)).toEqual([1, 2, 3]);
expect(coalesce([4, 5], fallback)).toEqual([4, 5]);
});
it('should handle zero and empty string as valid values', () => {
expect(coalesce(0, 999)).toBe(0);
expect(coalesce('', 'fallback')).toBe('');
expect(coalesce(false, true)).toBe(false);
});
});
describe('present()', () => {
it('should return the value when it is defined and not null', () => {
expect(present('value')).toBe('value');
expect(present(42)).toBe(42);
expect(present(true)).toBe(true);
expect(present({})).toEqual({});
expect(present([])).toEqual([]);
});
it('should return undefined when value is undefined', () => {
expect(present(undefined)).toBe(undefined);
});
it('should return undefined when value is null', () => {
expect(present(null)).toBe(undefined);
});
it('should handle zero and empty string as valid values', () => {
expect(present(0)).toBe(0);
expect(present('')).toBe('');
expect(present(false)).toBe(false);
});
it('should handle complex objects', () => {
const obj = { id: 1, name: 'test', nested: { value: 'data' } };
expect(present(obj)).toEqual(obj);
});
it('should handle arrays', () => {
const arr = [1, 2, 3, 'test'];
expect(present(arr)).toEqual(arr);
});
});
describe('Option behavior', () => {
it('should work together for optional value handling', () => {
// Example: providing a default when value might be missing
const maybeValue: string | undefined = undefined;
const result = coalesce(maybeValue, 'default');
expect(result).toBe('default');
// Example: filtering out null/undefined
const values: (string | null | undefined)[] = ['a', null, 'b', undefined, 'c'];
const filtered = values.map(present).filter((v): v is string => v !== undefined);
expect(filtered).toEqual(['a', 'b', 'c']);
});
it('should support conditional value assignment', () => {
const config: { timeout?: number } = {};
const timeout = coalesce(config.timeout, 5000);
expect(timeout).toBe(5000);
config.timeout = 3000;
const timeout2 = coalesce(config.timeout, 5000);
expect(timeout2).toBe(3000);
});
it('should handle nested optional properties', () => {
interface User {
profile?: {
name?: string;
email?: string;
};
}
const user1: User = {};
const user2: User = { profile: {} };
const user3: User = { profile: { name: 'John' } };
const user4: User = { profile: { name: 'John', email: 'john@example.com' } };
expect(coalesce(user1.profile?.name, 'Anonymous')).toBe('Anonymous');
expect(coalesce(user2.profile?.name, 'Anonymous')).toBe('Anonymous');
expect(coalesce(user3.profile?.name, 'Anonymous')).toBe('John');
expect(coalesce(user4.profile?.name, 'Anonymous')).toBe('John');
});
});
});

View File

@@ -0,0 +1,370 @@
import { describe, it, expect } from 'vitest';
import { Result } from './Result';
describe('Result', () => {
describe('Result.ok()', () => {
it('should create a success result with a value', () => {
const result = Result.ok('success-value');
expect(result.isOk()).toBe(true);
expect(result.isErr()).toBe(false);
expect(result.unwrap()).toBe('success-value');
});
it('should create a success result with undefined value', () => {
const result = Result.ok(undefined);
expect(result.isOk()).toBe(true);
expect(result.isErr()).toBe(false);
expect(result.unwrap()).toBe(undefined);
});
it('should create a success result with null value', () => {
const result = Result.ok(null);
expect(result.isOk()).toBe(true);
expect(result.isErr()).toBe(false);
expect(result.unwrap()).toBe(null);
});
it('should create a success result with complex object', () => {
const complexValue = { id: 123, name: 'test', nested: { data: 'value' } };
const result = Result.ok(complexValue);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(complexValue);
});
it('should create a success result with array', () => {
const arrayValue = [1, 2, 3, 'test'];
const result = Result.ok(arrayValue);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(arrayValue);
});
});
describe('Result.err()', () => {
it('should create an error result with an error', () => {
const error = new Error('test error');
const result = Result.err(error);
expect(result.isOk()).toBe(false);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toBe(error);
});
it('should create an error result with string error', () => {
const result = Result.err('string error');
expect(result.isOk()).toBe(false);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toBe('string error');
});
it('should create an error result with object error', () => {
const error = { code: 'VALIDATION_ERROR', message: 'Invalid input' };
const result = Result.err(error);
expect(result.isOk()).toBe(false);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual(error);
});
it('should create an error result with custom error type', () => {
interface CustomError {
code: string;
details: Record<string, unknown>;
}
const error: CustomError = {
code: 'NOT_FOUND',
details: { id: '123' }
};
const result = Result.err<unknown, CustomError>(error);
expect(result.isOk()).toBe(false);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual(error);
});
});
describe('Result.isOk()', () => {
it('should return true for success results', () => {
const result = Result.ok('value');
expect(result.isOk()).toBe(true);
});
it('should return false for error results', () => {
const result = Result.err(new Error('error'));
expect(result.isOk()).toBe(false);
});
});
describe('Result.isErr()', () => {
it('should return false for success results', () => {
const result = Result.ok('value');
expect(result.isErr()).toBe(false);
});
it('should return true for error results', () => {
const result = Result.err(new Error('error'));
expect(result.isErr()).toBe(true);
});
});
describe('Result.unwrap()', () => {
it('should return the value for success results', () => {
const result = Result.ok('test-value');
expect(result.unwrap()).toBe('test-value');
});
it('should throw error for error results', () => {
const result = Result.err(new Error('test error'));
expect(() => result.unwrap()).toThrow('Called unwrap on an error result');
});
it('should return complex values for success results', () => {
const complexValue = { id: 123, data: { nested: 'value' } };
const result = Result.ok(complexValue);
expect(result.unwrap()).toEqual(complexValue);
});
it('should return arrays for success results', () => {
const arrayValue = [1, 2, 3];
const result = Result.ok(arrayValue);
expect(result.unwrap()).toEqual(arrayValue);
});
});
describe('Result.unwrapOr()', () => {
it('should return the value for success results', () => {
const result = Result.ok('actual-value');
expect(result.unwrapOr('default-value')).toBe('actual-value');
});
it('should return default value for error results', () => {
const result = Result.err(new Error('error'));
expect(result.unwrapOr('default-value')).toBe('default-value');
});
it('should return default value when value is undefined', () => {
const result = Result.ok(undefined);
expect(result.unwrapOr('default-value')).toBe(undefined);
});
it('should return default value when value is null', () => {
const result = Result.ok(null);
expect(result.unwrapOr('default-value')).toBe(null);
});
});
describe('Result.unwrapErr()', () => {
it('should return the error for error results', () => {
const error = new Error('test error');
const result = Result.err(error);
expect(result.unwrapErr()).toBe(error);
});
it('should throw error for success results', () => {
const result = Result.ok('value');
expect(() => result.unwrapErr()).toThrow('Called unwrapErr on a success result');
});
it('should return string errors', () => {
const result = Result.err('string error');
expect(result.unwrapErr()).toBe('string error');
});
it('should return object errors', () => {
const error = { code: 'ERROR', message: 'Something went wrong' };
const result = Result.err(error);
expect(result.unwrapErr()).toEqual(error);
});
});
describe('Result.map()', () => {
it('should transform success values', () => {
const result = Result.ok(5);
const mapped = result.map((x) => x * 2);
expect(mapped.isOk()).toBe(true);
expect(mapped.unwrap()).toBe(10);
});
it('should not transform error results', () => {
const error = new Error('test error');
const result = Result.err<number, Error>(error);
const mapped = result.map((x) => x * 2);
expect(mapped.isErr()).toBe(true);
expect(mapped.unwrapErr()).toBe(error);
});
it('should handle complex transformations', () => {
const result = Result.ok({ id: 1, name: 'test' });
const mapped = result.map((obj) => ({ ...obj, name: obj.name.toUpperCase() }));
expect(mapped.isOk()).toBe(true);
expect(mapped.unwrap()).toEqual({ id: 1, name: 'TEST' });
});
it('should handle array transformations', () => {
const result = Result.ok([1, 2, 3]);
const mapped = result.map((arr) => arr.map((x) => x * 2));
expect(mapped.isOk()).toBe(true);
expect(mapped.unwrap()).toEqual([2, 4, 6]);
});
});
describe('Result.mapErr()', () => {
it('should transform error values', () => {
const error = new Error('original error');
const result = Result.err<string, Error>(error);
const mapped = result.mapErr((e) => new Error(`wrapped: ${e.message}`));
expect(mapped.isErr()).toBe(true);
expect(mapped.unwrapErr().message).toBe('wrapped: original error');
});
it('should not transform success results', () => {
const result = Result.ok('value');
const mapped = result.mapErr((e) => new Error(`wrapped: ${e.message}`));
expect(mapped.isOk()).toBe(true);
expect(mapped.unwrap()).toBe('value');
});
it('should handle string error transformations', () => {
const result = Result.err('error message');
const mapped = result.mapErr((e) => e.toUpperCase());
expect(mapped.isErr()).toBe(true);
expect(mapped.unwrapErr()).toBe('ERROR MESSAGE');
});
it('should handle object error transformations', () => {
const error = { code: 'ERROR', message: 'Something went wrong' };
const result = Result.err(error);
const mapped = result.mapErr((e) => ({ ...e, code: `WRAPPED_${e.code}` }));
expect(mapped.isErr()).toBe(true);
expect(mapped.unwrapErr()).toEqual({ code: 'WRAPPED_ERROR', message: 'Something went wrong' });
});
});
describe('Result.andThen()', () => {
it('should chain success results', () => {
const result1 = Result.ok(5);
const result2 = result1.andThen((x) => Result.ok(x * 2));
expect(result2.isOk()).toBe(true);
expect(result2.unwrap()).toBe(10);
});
it('should propagate errors through chain', () => {
const error = new Error('first error');
const result1 = Result.err<number, Error>(error);
const result2 = result1.andThen((x) => Result.ok(x * 2));
expect(result2.isErr()).toBe(true);
expect(result2.unwrapErr()).toBe(error);
});
it('should handle error in chained function', () => {
const result1 = Result.ok(5);
const result2 = result1.andThen((x) => Result.err(new Error(`error at ${x}`)));
expect(result2.isErr()).toBe(true);
expect(result2.unwrapErr().message).toBe('error at 5');
});
it('should support multiple chaining steps', () => {
const result = Result.ok(2)
.andThen((x) => Result.ok(x * 3))
.andThen((x) => Result.ok(x + 1))
.andThen((x) => Result.ok(x * 2));
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBe(14); // ((2 * 3) + 1) * 2 = 14
});
it('should stop chaining on first error', () => {
const result = Result.ok(2)
.andThen((x) => Result.ok(x * 3))
.andThen((x) => Result.err(new Error('stopped here')))
.andThen((x) => Result.ok(x + 1)); // This should not execute
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().message).toBe('stopped here');
});
});
describe('Result.value getter', () => {
it('should return value for success results', () => {
const result = Result.ok('test-value');
expect(result.value).toBe('test-value');
});
it('should return undefined for error results', () => {
const result = Result.err(new Error('error'));
expect(result.value).toBe(undefined);
});
it('should return undefined for success results with undefined value', () => {
const result = Result.ok(undefined);
expect(result.value).toBe(undefined);
});
});
describe('Result.error getter', () => {
it('should return error for error results', () => {
const error = new Error('test error');
const result = Result.err(error);
expect(result.error).toBe(error);
});
it('should return undefined for success results', () => {
const result = Result.ok('value');
expect(result.error).toBe(undefined);
});
it('should return string errors', () => {
const result = Result.err('string error');
expect(result.error).toBe('string error');
});
});
describe('Result type safety', () => {
it('should work with custom error codes', () => {
type MyErrorCode = 'NOT_FOUND' | 'VALIDATION_ERROR' | 'PERMISSION_DENIED';
const successResult = Result.ok<string, MyErrorCode>('data');
const errorResult = Result.err<string, MyErrorCode>('NOT_FOUND');
expect(successResult.isOk()).toBe(true);
expect(errorResult.isErr()).toBe(true);
});
it('should work with ApplicationErrorCode pattern', () => {
interface ApplicationErrorCode<Code extends string, Details = unknown> {
code: Code;
details?: Details;
}
type MyErrorCodes = 'USER_NOT_FOUND' | 'INVALID_EMAIL';
const successResult = Result.ok<string, ApplicationErrorCode<MyErrorCodes>>('user');
const errorResult = Result.err<string, ApplicationErrorCode<MyErrorCodes>>({
code: 'USER_NOT_FOUND',
details: { userId: '123' }
});
expect(successResult.isOk()).toBe(true);
expect(errorResult.isErr()).toBe(true);
expect(errorResult.unwrapErr().code).toBe('USER_NOT_FOUND');
});
});
});

View File

@@ -0,0 +1,374 @@
import { describe, it, expect } from 'vitest';
import {
DomainService,
DomainCalculationService,
ResultDomainCalculationService,
DomainValidationService,
DomainFactoryService,
DomainServiceAlias,
DomainCalculationServiceAlias,
ResultDomainCalculationServiceAlias,
DomainValidationServiceAlias,
DomainFactoryServiceAlias
} from './Service';
import { Result } from './Result';
describe('Service', () => {
describe('DomainService interface', () => {
it('should have optional serviceName property', () => {
const service: DomainService = {
serviceName: 'TestService'
};
expect(service.serviceName).toBe('TestService');
});
it('should work without serviceName', () => {
const service: DomainService = {};
expect(service.serviceName).toBeUndefined();
});
it('should support different service implementations', () => {
const service1: DomainService = { serviceName: 'Service1' };
const service2: DomainService = { serviceName: 'Service2' };
const service3: DomainService = {};
expect(service1.serviceName).toBe('Service1');
expect(service2.serviceName).toBe('Service2');
expect(service3.serviceName).toBeUndefined();
});
});
describe('DomainCalculationService interface', () => {
it('should have calculate method', () => {
const service: DomainCalculationService<number, number> = {
calculate: (input: number) => input * 2
};
expect(service.calculate(5)).toBe(10);
});
it('should support different input and output types', () => {
const stringService: DomainCalculationService<string, string> = {
calculate: (input: string) => input.toUpperCase()
};
const objectService: DomainCalculationService<{ x: number; y: number }, number> = {
calculate: (input) => input.x + input.y
};
expect(stringService.calculate('hello')).toBe('HELLO');
expect(objectService.calculate({ x: 3, y: 4 })).toBe(7);
});
it('should support complex calculations', () => {
interface CalculationInput {
values: number[];
operation: 'sum' | 'average' | 'max';
}
const calculator: DomainCalculationService<CalculationInput, number> = {
calculate: (input) => {
switch (input.operation) {
case 'sum':
return input.values.reduce((a, b) => a + b, 0);
case 'average':
return input.values.reduce((a, b) => a + b, 0) / input.values.length;
case 'max':
return Math.max(...input.values);
}
}
};
expect(calculator.calculate({ values: [1, 2, 3], operation: 'sum' })).toBe(6);
expect(calculator.calculate({ values: [1, 2, 3], operation: 'average' })).toBe(2);
expect(calculator.calculate({ values: [1, 2, 3], operation: 'max' })).toBe(3);
});
});
describe('ResultDomainCalculationService interface', () => {
it('should have calculate method returning Result', () => {
const service: ResultDomainCalculationService<number, number, string> = {
calculate: (input: number) => {
if (input < 0) {
return Result.err('Input must be non-negative');
}
return Result.ok(input * 2);
}
};
const successResult = service.calculate(5);
expect(successResult.isOk()).toBe(true);
expect(successResult.unwrap()).toBe(10);
const errorResult = service.calculate(-1);
expect(errorResult.isErr()).toBe(true);
expect(errorResult.unwrapErr()).toBe('Input must be non-negative');
});
it('should support validation logic', () => {
interface ValidationResult {
isValid: boolean;
errors: string[];
}
const validator: ResultDomainCalculationService<string, ValidationResult, string> = {
calculate: (input: string) => {
const errors: string[] = [];
if (input.length < 3) {
errors.push('Must be at least 3 characters');
}
if (!input.match(/^[a-zA-Z]+$/)) {
errors.push('Must contain only letters');
}
if (errors.length > 0) {
return Result.err(errors.join(', '));
}
return Result.ok({ isValid: true, errors: [] });
}
};
const validResult = validator.calculate('Hello');
expect(validResult.isOk()).toBe(true);
expect(validResult.unwrap()).toEqual({ isValid: true, errors: [] });
const invalidResult = validator.calculate('ab');
expect(invalidResult.isErr()).toBe(true);
expect(invalidResult.unwrapErr()).toBe('Must be at least 3 characters');
});
it('should support complex business rules', () => {
interface DiscountInput {
basePrice: number;
customerType: 'regular' | 'premium' | 'vip';
hasCoupon: boolean;
}
const discountCalculator: ResultDomainCalculationService<DiscountInput, number, string> = {
calculate: (input) => {
let discount = 0;
// Customer type discount
switch (input.customerType) {
case 'premium':
discount += 0.1;
break;
case 'vip':
discount += 0.2;
break;
}
// Coupon discount
if (input.hasCoupon) {
discount += 0.05;
}
// Validate price
if (input.basePrice <= 0) {
return Result.err('Price must be positive');
}
const finalPrice = input.basePrice * (1 - discount);
return Result.ok(finalPrice);
}
};
const vipWithCoupon = discountCalculator.calculate({
basePrice: 100,
customerType: 'vip',
hasCoupon: true
});
expect(vipWithCoupon.isOk()).toBe(true);
expect(vipWithCoupon.unwrap()).toBe(75); // 100 * (1 - 0.2 - 0.05) = 75
const invalidPrice = discountCalculator.calculate({
basePrice: 0,
customerType: 'regular',
hasCoupon: false
});
expect(invalidPrice.isErr()).toBe(true);
expect(invalidPrice.unwrapErr()).toBe('Price must be positive');
});
});
describe('DomainValidationService interface', () => {
it('should have validate method returning Result', () => {
const service: DomainValidationService<string, boolean, string> = {
validate: (input: string) => {
if (input.length === 0) {
return Result.err('Input cannot be empty');
}
return Result.ok(true);
}
};
const validResult = service.validate('test');
expect(validResult.isOk()).toBe(true);
expect(validResult.unwrap()).toBe(true);
const invalidResult = service.validate('');
expect(invalidResult.isErr()).toBe(true);
expect(invalidResult.unwrapErr()).toBe('Input cannot be empty');
});
it('should support validation of complex objects', () => {
interface UserInput {
email: string;
password: string;
age: number;
}
const userValidator: DomainValidationService<UserInput, UserInput, string> = {
validate: (input) => {
const errors: string[] = [];
if (!input.email.includes('@')) {
errors.push('Invalid email format');
}
if (input.password.length < 8) {
errors.push('Password must be at least 8 characters');
}
if (input.age < 18) {
errors.push('Must be at least 18 years old');
}
if (errors.length > 0) {
return Result.err(errors.join(', '));
}
return Result.ok(input);
}
};
const validUser = userValidator.validate({
email: 'john@example.com',
password: 'securepassword',
age: 25
});
expect(validUser.isOk()).toBe(true);
expect(validUser.unwrap()).toEqual({
email: 'john@example.com',
password: 'securepassword',
age: 25
});
const invalidUser = userValidator.validate({
email: 'invalid-email',
password: 'short',
age: 15
});
expect(invalidUser.isErr()).toBe(true);
expect(invalidUser.unwrapErr()).toContain('Invalid email format');
expect(invalidUser.unwrapErr()).toContain('Password must be at least 8 characters');
expect(invalidUser.unwrapErr()).toContain('Must be at least 18 years old');
});
});
describe('DomainFactoryService interface', () => {
it('should have create method', () => {
const service: DomainFactoryService<string, { id: number; value: string }> = {
create: (input: string) => ({
id: input.length,
value: input.toUpperCase()
})
};
const result = service.create('test');
expect(result).toEqual({ id: 4, value: 'TEST' });
});
it('should support creating complex objects', () => {
interface CreateUserInput {
name: string;
email: string;
}
interface User {
id: string;
name: string;
email: string;
createdAt: Date;
}
const userFactory: DomainFactoryService<CreateUserInput, User> = {
create: (input) => ({
id: `user-${Date.now()}`,
name: input.name,
email: input.email,
createdAt: new Date()
})
};
const user = userFactory.create({
name: 'John Doe',
email: 'john@example.com'
});
expect(user.id).toMatch(/^user-\d+$/);
expect(user.name).toBe('John Doe');
expect(user.email).toBe('john@example.com');
expect(user.createdAt).toBeInstanceOf(Date);
});
it('should support creating value objects', () => {
interface AddressProps {
street: string;
city: string;
zipCode: string;
}
const addressFactory: DomainFactoryService<AddressProps, AddressProps> = {
create: (input) => ({
street: input.street.trim(),
city: input.city.trim(),
zipCode: input.zipCode.trim()
})
};
const address = addressFactory.create({
street: ' 123 Main St ',
city: ' New York ',
zipCode: ' 10001 '
});
expect(address.street).toBe('123 Main St');
expect(address.city).toBe('New York');
expect(address.zipCode).toBe('10001');
});
});
describe('ServiceAlias types', () => {
it('should be assignable to their base interfaces', () => {
const service1: DomainServiceAlias = { serviceName: 'Test' };
const service2: DomainCalculationServiceAlias<number, number> = {
calculate: (x) => x * 2
};
const service3: ResultDomainCalculationServiceAlias<number, number, string> = {
calculate: (x) => Result.ok(x * 2)
};
const service4: DomainValidationServiceAlias<string, boolean, string> = {
validate: (x) => Result.ok(x.length > 0)
};
const service5: DomainFactoryServiceAlias<string, string> = {
create: (x) => x.toUpperCase()
};
expect(service1.serviceName).toBe('Test');
expect(service2.calculate(5)).toBe(10);
expect(service3.calculate(5).isOk()).toBe(true);
expect(service4.validate('test').isOk()).toBe(true);
expect(service5.create('test')).toBe('TEST');
});
});
});

View File

@@ -0,0 +1,119 @@
import { describe, it, expect } from 'vitest';
import { ValueObject, ValueObjectAlias } from './ValueObject';
// Concrete implementation for testing
class TestValueObject implements ValueObject<{ name: string; value: number }> {
readonly props: { name: string; value: number };
constructor(name: string, value: number) {
this.props = { name, value };
}
equals(other: ValueObject<{ name: string; value: number }>): boolean {
if (!other) return false;
return (
this.props.name === other.props.name && this.props.value === other.props.value
);
}
}
describe('ValueObject', () => {
describe('ValueObject interface', () => {
it('should have readonly props property', () => {
const vo = new TestValueObject('test', 42);
expect(vo.props).toEqual({ name: 'test', value: 42 });
});
it('should have equals method', () => {
const vo1 = new TestValueObject('test', 42);
const vo2 = new TestValueObject('test', 42);
const vo3 = new TestValueObject('different', 42);
expect(vo1.equals(vo2)).toBe(true);
expect(vo1.equals(vo3)).toBe(false);
});
it('should return false when comparing with undefined', () => {
const vo = new TestValueObject('test', 42);
// Testing that equals method handles undefined gracefully
const result = vo.equals as any;
expect(result(undefined)).toBe(false);
});
it('should return false when comparing with null', () => {
const vo = new TestValueObject('test', 42);
// Testing that equals method handles null gracefully
const result = vo.equals as any;
expect(result(null)).toBe(false);
});
});
describe('ValueObjectAlias type', () => {
it('should be assignable to ValueObject', () => {
const vo: ValueObjectAlias<{ name: string }> = {
props: { name: 'test' },
equals: (other) => other.props.name === 'test',
};
expect(vo.props.name).toBe('test');
expect(vo.equals(vo)).toBe(true);
});
});
});
describe('ValueObject behavior', () => {
it('should support complex value objects', () => {
interface AddressProps {
street: string;
city: string;
zipCode: string;
}
class Address implements ValueObject<AddressProps> {
readonly props: AddressProps;
constructor(street: string, city: string, zipCode: string) {
this.props = { street, city, zipCode };
}
equals(other: ValueObject<AddressProps>): boolean {
return (
this.props.street === other.props.street &&
this.props.city === other.props.city &&
this.props.zipCode === other.props.zipCode
);
}
}
const address1 = new Address('123 Main St', 'New York', '10001');
const address2 = new Address('123 Main St', 'New York', '10001');
const address3 = new Address('456 Oak Ave', 'Boston', '02101');
expect(address1.equals(address2)).toBe(true);
expect(address1.equals(address3)).toBe(false);
});
it('should support immutable value objects', () => {
class ImmutableValueObject implements ValueObject<{ readonly data: string[] }> {
readonly props: { readonly data: string[] };
constructor(data: string[]) {
this.props = { data: [...data] }; // Create a copy to ensure immutability
}
equals(other: ValueObject<{ readonly data: string[] }>): boolean {
return (
this.props.data.length === other.props.data.length &&
this.props.data.every((item, index) => item === other.props.data[index])
);
}
}
const vo1 = new ImmutableValueObject(['a', 'b', 'c']);
const vo2 = new ImmutableValueObject(['a', 'b', 'c']);
const vo3 = new ImmutableValueObject(['a', 'b', 'd']);
expect(vo1.equals(vo2)).toBe(true);
expect(vo1.equals(vo3)).toBe(false);
});
});

View File

@@ -0,0 +1,471 @@
import { describe, it, expect } from 'vitest';
import { ApplicationError, CommonApplicationErrorKind } from './ApplicationError';
describe('ApplicationError', () => {
describe('ApplicationError interface', () => {
it('should have required properties', () => {
const error: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'user-service',
kind: 'not_found',
message: 'User not found'
};
expect(error.type).toBe('application');
expect(error.context).toBe('user-service');
expect(error.kind).toBe('not_found');
expect(error.message).toBe('User not found');
});
it('should support optional details', () => {
const error: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'payment-service',
kind: 'validation',
message: 'Invalid payment amount',
details: { amount: -100, minAmount: 0 }
};
expect(error.details).toEqual({ amount: -100, minAmount: 0 });
});
it('should support different error kinds', () => {
const notFoundError: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'user-service',
kind: 'not_found',
message: 'User not found'
};
const forbiddenError: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'admin-service',
kind: 'forbidden',
message: 'Access denied'
};
const conflictError: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'order-service',
kind: 'conflict',
message: 'Order already exists'
};
const validationError: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'form-service',
kind: 'validation',
message: 'Invalid input'
};
const unknownError: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'unknown-service',
kind: 'unknown',
message: 'Unknown error'
};
expect(notFoundError.kind).toBe('not_found');
expect(forbiddenError.kind).toBe('forbidden');
expect(conflictError.kind).toBe('conflict');
expect(validationError.kind).toBe('validation');
expect(unknownError.kind).toBe('unknown');
});
it('should support custom error kinds', () => {
const customError: ApplicationError<'RATE_LIMIT_EXCEEDED'> = {
name: 'ApplicationError',
type: 'application',
context: 'api-gateway',
kind: 'RATE_LIMIT_EXCEEDED',
message: 'Rate limit exceeded',
details: { retryAfter: 60 }
};
expect(customError.kind).toBe('RATE_LIMIT_EXCEEDED');
expect(customError.details).toEqual({ retryAfter: 60 });
});
});
describe('CommonApplicationErrorKind type', () => {
it('should include standard error kinds', () => {
const kinds: CommonApplicationErrorKind[] = [
'not_found',
'forbidden',
'conflict',
'validation',
'unknown'
];
kinds.forEach(kind => {
const error: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'test',
kind,
message: `Test ${kind} error`
};
expect(error.kind).toBe(kind);
});
});
it('should support string extension for custom kinds', () => {
const customKinds: CommonApplicationErrorKind[] = [
'CUSTOM_ERROR_1',
'CUSTOM_ERROR_2',
'BUSINESS_RULE_VIOLATION'
];
customKinds.forEach(kind => {
const error: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'test',
kind,
message: `Test ${kind} error`
};
expect(error.kind).toBe(kind);
});
});
});
describe('ApplicationError behavior', () => {
it('should be assignable to Error interface', () => {
const error: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'test-service',
kind: 'validation',
message: 'Validation failed'
};
// ApplicationError extends Error
expect(error.type).toBe('application');
expect(error.message).toBe('Validation failed');
});
it('should support error inheritance pattern', () => {
class CustomApplicationError extends Error implements ApplicationError {
readonly type: 'application' = 'application';
readonly context: string;
readonly kind: string;
readonly details?: unknown;
constructor(context: string, kind: string, message: string, details?: unknown) {
super(message);
this.context = context;
this.kind = kind;
this.details = details;
this.name = 'CustomApplicationError';
}
}
const error = new CustomApplicationError(
'user-service',
'USER_NOT_FOUND',
'User with ID 123 not found',
{ userId: '123' }
);
expect(error.type).toBe('application');
expect(error.context).toBe('user-service');
expect(error.kind).toBe('USER_NOT_FOUND');
expect(error.message).toBe('User with ID 123 not found');
expect(error.details).toEqual({ userId: '123' });
expect(error.name).toBe('CustomApplicationError');
expect(error.stack).toBeDefined();
});
it('should support error serialization', () => {
const error: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'payment-service',
kind: 'INSUFFICIENT_FUNDS',
message: 'Insufficient funds for transaction',
details: {
balance: 50,
required: 100,
currency: 'USD'
}
};
const serialized = JSON.stringify(error);
const parsed = JSON.parse(serialized);
expect(parsed.type).toBe('application');
expect(parsed.context).toBe('payment-service');
expect(parsed.kind).toBe('INSUFFICIENT_FUNDS');
expect(parsed.message).toBe('Insufficient funds for transaction');
expect(parsed.details).toEqual({
balance: 50,
required: 100,
currency: 'USD'
});
});
it('should support error deserialization', () => {
const serialized = JSON.stringify({
type: 'application',
context: 'auth-service',
kind: 'INVALID_CREDENTIALS',
message: 'Invalid username or password',
details: { attempt: 3 }
});
const parsed: ApplicationError = JSON.parse(serialized);
expect(parsed.type).toBe('application');
expect(parsed.context).toBe('auth-service');
expect(parsed.kind).toBe('INVALID_CREDENTIALS');
expect(parsed.message).toBe('Invalid username or password');
expect(parsed.details).toEqual({ attempt: 3 });
});
it('should support error comparison', () => {
const error1: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'user-service',
kind: 'not_found',
message: 'User not found'
};
const error2: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'user-service',
kind: 'not_found',
message: 'User not found'
};
const error3: ApplicationError = {
name: 'ApplicationError',
type: 'application',
context: 'order-service',
kind: 'not_found',
message: 'Order not found'
};
// Same kind and context
expect(error1.kind).toBe(error2.kind);
expect(error1.context).toBe(error2.context);
// Different context
expect(error1.context).not.toBe(error3.context);
});
it('should support error categorization', () => {
const errors: ApplicationError[] = [
{
name: 'ApplicationError',
type: 'application',
context: 'user-service',
kind: 'not_found',
message: 'User not found'
},
{
name: 'ApplicationError',
type: 'application',
context: 'admin-service',
kind: 'forbidden',
message: 'Access denied'
},
{
name: 'ApplicationError',
type: 'application',
context: 'order-service',
kind: 'conflict',
message: 'Order already exists'
},
{
name: 'ApplicationError',
type: 'application',
context: 'form-service',
kind: 'validation',
message: 'Invalid input'
}
];
const notFoundErrors = errors.filter(e => e.kind === 'not_found');
const forbiddenErrors = errors.filter(e => e.kind === 'forbidden');
const conflictErrors = errors.filter(e => e.kind === 'conflict');
const validationErrors = errors.filter(e => e.kind === 'validation');
expect(notFoundErrors).toHaveLength(1);
expect(forbiddenErrors).toHaveLength(1);
expect(conflictErrors).toHaveLength(1);
expect(validationErrors).toHaveLength(1);
});
it('should support error aggregation by context', () => {
const errors: ApplicationError[] = [
{
name: 'ApplicationError',
type: 'application',
context: 'user-service',
kind: 'not_found',
message: 'User not found'
},
{
name: 'ApplicationError',
type: 'application',
context: 'user-service',
kind: 'validation',
message: 'Invalid user data'
},
{
name: 'ApplicationError',
type: 'application',
context: 'order-service',
kind: 'not_found',
message: 'Order not found'
}
];
const userErrors = errors.filter(e => e.context === 'user-service');
const orderErrors = errors.filter(e => e.context === 'order-service');
expect(userErrors).toHaveLength(2);
expect(orderErrors).toHaveLength(1);
});
});
describe('ApplicationError implementation patterns', () => {
it('should support error factory pattern', () => {
function createApplicationError<K extends string>(
context: string,
kind: K,
message: string,
details?: unknown
): ApplicationError<K> {
return {
name: 'ApplicationError',
type: 'application',
context,
kind,
message,
details
};
}
const notFoundError = createApplicationError(
'user-service',
'USER_NOT_FOUND',
'User not found',
{ userId: '123' }
);
const validationError = createApplicationError(
'form-service',
'VALIDATION_ERROR',
'Invalid form data',
{ field: 'email', value: 'invalid' }
);
expect(notFoundError.kind).toBe('USER_NOT_FOUND');
expect(notFoundError.details).toEqual({ userId: '123' });
expect(validationError.kind).toBe('VALIDATION_ERROR');
expect(validationError.details).toEqual({ field: 'email', value: 'invalid' });
});
it('should support error builder pattern', () => {
class ApplicationErrorBuilder<K extends string> {
private context: string = '';
private kind: K = '' as K;
private message: string = '';
private details?: unknown;
withContext(context: string): this {
this.context = context;
return this;
}
withKind(kind: K): this {
this.kind = kind;
return this;
}
withMessage(message: string): this {
this.message = message;
return this;
}
withDetails(details: unknown): this {
this.details = details;
return this;
}
build(): ApplicationError<K> {
return {
name: 'ApplicationError',
type: 'application',
context: this.context,
kind: this.kind,
message: this.message,
details: this.details
};
}
}
const error = new ApplicationErrorBuilder<'USER_NOT_FOUND'>()
.withContext('user-service')
.withKind('USER_NOT_FOUND')
.withMessage('User not found')
.withDetails({ userId: '123' })
.build();
expect(error.type).toBe('application');
expect(error.context).toBe('user-service');
expect(error.kind).toBe('USER_NOT_FOUND');
expect(error.message).toBe('User not found');
expect(error.details).toEqual({ userId: '123' });
});
it('should support error categorization by severity', () => {
const errors: ApplicationError[] = [
{
name: 'ApplicationError',
type: 'application',
context: 'auth-service',
kind: 'INVALID_CREDENTIALS',
message: 'Invalid credentials',
details: { severity: 'high' }
},
{
name: 'ApplicationError',
type: 'application',
context: 'user-service',
kind: 'USER_NOT_FOUND',
message: 'User not found',
details: { severity: 'medium' }
},
{
name: 'ApplicationError',
type: 'application',
context: 'cache-service',
kind: 'CACHE_MISS',
message: 'Cache miss',
details: { severity: 'low' }
}
];
const highSeverity = errors.filter(e => (e.details as { severity: string })?.severity === 'high');
const mediumSeverity = errors.filter(e => (e.details as { severity: string })?.severity === 'medium');
const lowSeverity = errors.filter(e => (e.details as { severity: string })?.severity === 'low');
expect(highSeverity).toHaveLength(1);
expect(mediumSeverity).toHaveLength(1);
expect(lowSeverity).toHaveLength(1);
});
});
});

View File

@@ -0,0 +1,335 @@
import { describe, it, expect } from 'vitest';
import { ApplicationErrorCode } from './ApplicationErrorCode';
describe('ApplicationErrorCode', () => {
describe('ApplicationErrorCode type', () => {
it('should create error code with code only', () => {
const errorCode: ApplicationErrorCode<'USER_NOT_FOUND'> = {
code: 'USER_NOT_FOUND'
};
expect(errorCode.code).toBe('USER_NOT_FOUND');
});
it('should create error code with code and details', () => {
const errorCode: ApplicationErrorCode<'INSUFFICIENT_FUNDS', { balance: number; required: number }> = {
code: 'INSUFFICIENT_FUNDS',
details: { balance: 50, required: 100 }
};
expect(errorCode.code).toBe('INSUFFICIENT_FUNDS');
expect(errorCode.details).toEqual({ balance: 50, required: 100 });
});
it('should support different error code types', () => {
const notFoundCode: ApplicationErrorCode<'USER_NOT_FOUND'> = {
code: 'USER_NOT_FOUND'
};
const validationCode: ApplicationErrorCode<'VALIDATION_ERROR', { field: string }> = {
code: 'VALIDATION_ERROR',
details: { field: 'email' }
};
const permissionCode: ApplicationErrorCode<'PERMISSION_DENIED', { resource: string }> = {
code: 'PERMISSION_DENIED',
details: { resource: 'admin-panel' }
};
expect(notFoundCode.code).toBe('USER_NOT_FOUND');
expect(validationCode.code).toBe('VALIDATION_ERROR');
expect(validationCode.details).toEqual({ field: 'email' });
expect(permissionCode.code).toBe('PERMISSION_DENIED');
expect(permissionCode.details).toEqual({ resource: 'admin-panel' });
});
it('should support complex details types', () => {
interface PaymentErrorDetails {
amount: number;
currency: string;
retryAfter?: number;
attempts: number;
}
const paymentErrorCode: ApplicationErrorCode<'PAYMENT_FAILED', PaymentErrorDetails> = {
code: 'PAYMENT_FAILED',
details: {
amount: 100,
currency: 'USD',
retryAfter: 60,
attempts: 3
}
};
expect(paymentErrorCode.code).toBe('PAYMENT_FAILED');
expect(paymentErrorCode.details).toEqual({
amount: 100,
currency: 'USD',
retryAfter: 60,
attempts: 3
});
});
it('should support optional details', () => {
const errorCodeWithDetails: ApplicationErrorCode<'ERROR', { message: string }> = {
code: 'ERROR',
details: { message: 'Something went wrong' }
};
const errorCodeWithoutDetails: ApplicationErrorCode<'ERROR', undefined> = {
code: 'ERROR'
};
expect(errorCodeWithDetails.code).toBe('ERROR');
expect(errorCodeWithDetails.details).toEqual({ message: 'Something went wrong' });
expect(errorCodeWithoutDetails.code).toBe('ERROR');
});
});
describe('ApplicationErrorCode behavior', () => {
it('should be assignable to Result error type', () => {
// ApplicationErrorCode is designed to be used with Result type
// This test verifies the type compatibility
type MyErrorCodes = 'USER_NOT_FOUND' | 'VALIDATION_ERROR' | 'PERMISSION_DENIED';
const userNotFound: ApplicationErrorCode<'USER_NOT_FOUND'> = {
code: 'USER_NOT_FOUND'
};
const validationError: ApplicationErrorCode<'VALIDATION_ERROR', { field: string }> = {
code: 'VALIDATION_ERROR',
details: { field: 'email' }
};
const permissionError: ApplicationErrorCode<'PERMISSION_DENIED', { resource: string }> = {
code: 'PERMISSION_DENIED',
details: { resource: 'admin-panel' }
};
expect(userNotFound.code).toBe('USER_NOT_FOUND');
expect(validationError.code).toBe('VALIDATION_ERROR');
expect(validationError.details).toEqual({ field: 'email' });
expect(permissionError.code).toBe('PERMISSION_DENIED');
expect(permissionError.details).toEqual({ resource: 'admin-panel' });
});
it('should support error code patterns', () => {
// Common error code patterns
const notFoundPattern: ApplicationErrorCode<'NOT_FOUND', { resource: string; id?: string }> = {
code: 'NOT_FOUND',
details: { resource: 'user', id: '123' }
};
const conflictPattern: ApplicationErrorCode<'CONFLICT', { resource: string; existingId: string }> = {
code: 'CONFLICT',
details: { resource: 'order', existingId: '456' }
};
const validationPattern: ApplicationErrorCode<'VALIDATION_ERROR', { field: string; value: unknown; reason: string }> = {
code: 'VALIDATION_ERROR',
details: { field: 'email', value: 'invalid', reason: 'must contain @' }
};
expect(notFoundPattern.code).toBe('NOT_FOUND');
expect(notFoundPattern.details).toEqual({ resource: 'user', id: '123' });
expect(conflictPattern.code).toBe('CONFLICT');
expect(conflictPattern.details).toEqual({ resource: 'order', existingId: '456' });
expect(validationPattern.code).toBe('VALIDATION_ERROR');
expect(validationPattern.details).toEqual({ field: 'email', value: 'invalid', reason: 'must contain @' });
});
it('should support error code with metadata', () => {
interface ErrorMetadata {
timestamp: string;
requestId?: string;
userId?: string;
sessionId?: string;
}
const errorCode: ApplicationErrorCode<'AUTH_ERROR', ErrorMetadata> = {
code: 'AUTH_ERROR',
details: {
timestamp: new Date().toISOString(),
requestId: 'req-123',
userId: 'user-456',
sessionId: 'session-789'
}
};
expect(errorCode.code).toBe('AUTH_ERROR');
expect(errorCode.details).toBeDefined();
expect(errorCode.details?.timestamp).toBeDefined();
expect(errorCode.details?.requestId).toBe('req-123');
});
it('should support error code with retry information', () => {
interface RetryInfo {
retryAfter: number;
maxRetries: number;
currentAttempt: number;
}
const retryableError: ApplicationErrorCode<'RATE_LIMIT_EXCEEDED', RetryInfo> = {
code: 'RATE_LIMIT_EXCEEDED',
details: {
retryAfter: 60,
maxRetries: 3,
currentAttempt: 1
}
};
expect(retryableError.code).toBe('RATE_LIMIT_EXCEEDED');
expect(retryableError.details).toEqual({
retryAfter: 60,
maxRetries: 3,
currentAttempt: 1
});
});
it('should support error code with validation details', () => {
interface ValidationErrorDetails {
field: string;
value: unknown;
constraints: string[];
message: string;
}
const validationError: ApplicationErrorCode<'VALIDATION_ERROR', ValidationErrorDetails> = {
code: 'VALIDATION_ERROR',
details: {
field: 'email',
value: 'invalid-email',
constraints: ['must be a valid email', 'must not be empty'],
message: 'Email validation failed'
}
};
expect(validationError.code).toBe('VALIDATION_ERROR');
expect(validationError.details).toEqual({
field: 'email',
value: 'invalid-email',
constraints: ['must be a valid email', 'must not be empty'],
message: 'Email validation failed'
});
});
});
describe('ApplicationErrorCode implementation patterns', () => {
it('should support error code factory pattern', () => {
function createErrorCode<Code extends string, Details = unknown>(
code: Code,
details?: Details
): ApplicationErrorCode<Code, Details> {
return details ? { code, details } : { code };
}
const notFound = createErrorCode('USER_NOT_FOUND');
const validation = createErrorCode('VALIDATION_ERROR', { field: 'email' });
const permission = createErrorCode('PERMISSION_DENIED', { resource: 'admin' });
expect(notFound.code).toBe('USER_NOT_FOUND');
expect(validation.code).toBe('VALIDATION_ERROR');
expect(validation.details).toEqual({ field: 'email' });
expect(permission.code).toBe('PERMISSION_DENIED');
expect(permission.details).toEqual({ resource: 'admin' });
});
it('should support error code builder pattern', () => {
class ErrorCodeBuilder<Code extends string, Details = unknown> {
private code: Code = '' as Code;
private details?: Details;
withCode(code: Code): this {
this.code = code;
return this;
}
withDetails(details: Details): this {
this.details = details;
return this;
}
build(): ApplicationErrorCode<Code, Details> {
return this.details ? { code: this.code, details: this.details } : { code: this.code };
}
}
const errorCode = new ErrorCodeBuilder<'USER_NOT_FOUND'>()
.withCode('USER_NOT_FOUND')
.build();
const errorCodeWithDetails = new ErrorCodeBuilder<'VALIDATION_ERROR', { field: string }>()
.withCode('VALIDATION_ERROR')
.withDetails({ field: 'email' })
.build();
expect(errorCode.code).toBe('USER_NOT_FOUND');
expect(errorCodeWithDetails.code).toBe('VALIDATION_ERROR');
expect(errorCodeWithDetails.details).toEqual({ field: 'email' });
});
it('should support error code categorization', () => {
const errorCodes: ApplicationErrorCode<string, unknown>[] = [
{ code: 'USER_NOT_FOUND' },
{ code: 'VALIDATION_ERROR', details: { field: 'email' } },
{ code: 'PERMISSION_DENIED', details: { resource: 'admin' } },
{ code: 'NETWORK_ERROR' }
];
const notFoundCodes = errorCodes.filter(e => e.code === 'USER_NOT_FOUND');
const validationCodes = errorCodes.filter(e => e.code === 'VALIDATION_ERROR');
const permissionCodes = errorCodes.filter(e => e.code === 'PERMISSION_DENIED');
const networkCodes = errorCodes.filter(e => e.code === 'NETWORK_ERROR');
expect(notFoundCodes).toHaveLength(1);
expect(validationCodes).toHaveLength(1);
expect(permissionCodes).toHaveLength(1);
expect(networkCodes).toHaveLength(1);
});
it('should support error code with complex details', () => {
interface ComplexErrorDetails {
error: {
code: string;
message: string;
stack?: string;
};
context: {
service: string;
operation: string;
timestamp: string;
};
metadata: {
retryCount: number;
timeout: number;
};
}
const complexError: ApplicationErrorCode<'SYSTEM_ERROR', ComplexErrorDetails> = {
code: 'SYSTEM_ERROR',
details: {
error: {
code: 'E001',
message: 'System failure',
stack: 'Error stack trace...'
},
context: {
service: 'payment-service',
operation: 'processPayment',
timestamp: new Date().toISOString()
},
metadata: {
retryCount: 3,
timeout: 5000
}
}
};
expect(complexError.code).toBe('SYSTEM_ERROR');
expect(complexError.details).toBeDefined();
expect(complexError.details?.error.code).toBe('E001');
expect(complexError.details?.context.service).toBe('payment-service');
expect(complexError.details?.metadata.retryCount).toBe(3);
});
});
});

View File

@@ -0,0 +1,508 @@
import { describe, it, expect } from 'vitest';
import { DomainError, CommonDomainErrorKind, IDomainError, DomainErrorAlias } from './DomainError';
describe('DomainError', () => {
describe('DomainError interface', () => {
it('should have required properties', () => {
const error: DomainError = {
name: 'DomainError',
type: 'domain',
context: 'user-service',
kind: 'validation',
message: 'Invalid user data'
};
expect(error.type).toBe('domain');
expect(error.context).toBe('user-service');
expect(error.kind).toBe('validation');
expect(error.message).toBe('Invalid user data');
});
it('should support different error kinds', () => {
const validationError: DomainError = {
name: 'DomainError',
type: 'domain',
context: 'form-service',
kind: 'validation',
message: 'Invalid input'
};
const invariantError: DomainError = {
name: 'DomainError',
type: 'domain',
context: 'order-service',
kind: 'invariant',
message: 'Order total must be positive'
};
const customError: DomainError = {
name: 'DomainError',
type: 'domain',
context: 'payment-service',
kind: 'BUSINESS_RULE_VIOLATION',
message: 'Payment exceeds limit'
};
expect(validationError.kind).toBe('validation');
expect(invariantError.kind).toBe('invariant');
expect(customError.kind).toBe('BUSINESS_RULE_VIOLATION');
});
it('should support custom error kinds', () => {
const customError: DomainError<'INVALID_STATE'> = {
name: 'DomainError',
type: 'domain',
context: 'state-machine',
kind: 'INVALID_STATE',
message: 'Cannot transition from current state'
};
expect(customError.kind).toBe('INVALID_STATE');
});
});
describe('CommonDomainErrorKind type', () => {
it('should include standard error kinds', () => {
const kinds: CommonDomainErrorKind[] = ['validation', 'invariant'];
kinds.forEach(kind => {
const error: DomainError = {
name: 'DomainError',
type: 'domain',
context: 'test',
kind,
message: `Test ${kind} error`
};
expect(error.kind).toBe(kind);
});
});
it('should support string extension for custom kinds', () => {
const customKinds: CommonDomainErrorKind[] = [
'BUSINESS_RULE_VIOLATION',
'DOMAIN_CONSTRAINT_VIOLATION',
'AGGREGATE_ROOT_VIOLATION'
];
customKinds.forEach(kind => {
const error: DomainError = {
name: 'DomainError',
type: 'domain',
context: 'test',
kind,
message: `Test ${kind} error`
};
expect(error.kind).toBe(kind);
});
});
});
describe('DomainError behavior', () => {
it('should be assignable to Error interface', () => {
const error: DomainError = {
type: 'domain',
context: 'user-service',
kind: 'validation',
message: 'Validation failed'
};
// DomainError extends Error
expect(error.type).toBe('domain');
expect(error.message).toBe('Validation failed');
});
it('should support error inheritance pattern', () => {
class CustomDomainError extends Error implements DomainError {
readonly type: 'domain' = 'domain';
readonly context: string;
readonly kind: string;
constructor(context: string, kind: string, message: string) {
super(message);
this.context = context;
this.kind = kind;
this.name = 'CustomDomainError';
}
}
const error = new CustomDomainError(
'user-service',
'VALIDATION_ERROR',
'User email is invalid'
);
expect(error.type).toBe('domain');
expect(error.context).toBe('user-service');
expect(error.kind).toBe('VALIDATION_ERROR');
expect(error.message).toBe('User email is invalid');
expect(error.name).toBe('CustomDomainError');
expect(error.stack).toBeDefined();
});
it('should support error serialization', () => {
const error: DomainError = {
type: 'domain',
context: 'order-service',
kind: 'invariant',
message: 'Order total must be positive',
details: { total: -100 }
};
const serialized = JSON.stringify(error);
const parsed = JSON.parse(serialized);
expect(parsed.type).toBe('domain');
expect(parsed.context).toBe('order-service');
expect(parsed.kind).toBe('invariant');
expect(parsed.message).toBe('Order total must be positive');
expect(parsed.details).toEqual({ total: -100 });
});
it('should support error deserialization', () => {
const serialized = JSON.stringify({
type: 'domain',
context: 'payment-service',
kind: 'BUSINESS_RULE_VIOLATION',
message: 'Payment exceeds limit',
details: { limit: 1000, amount: 1500 }
});
const parsed: DomainError = JSON.parse(serialized);
expect(parsed.type).toBe('domain');
expect(parsed.context).toBe('payment-service');
expect(parsed.kind).toBe('BUSINESS_RULE_VIOLATION');
expect(parsed.message).toBe('Payment exceeds limit');
expect(parsed.details).toEqual({ limit: 1000, amount: 1500 });
});
it('should support error comparison', () => {
const error1: DomainError = {
type: 'domain',
context: 'user-service',
kind: 'validation',
message: 'Invalid email'
};
const error2: DomainError = {
type: 'domain',
context: 'user-service',
kind: 'validation',
message: 'Invalid email'
};
const error3: DomainError = {
type: 'domain',
context: 'order-service',
kind: 'invariant',
message: 'Order total must be positive'
};
// Same kind and context
expect(error1.kind).toBe(error2.kind);
expect(error1.context).toBe(error2.context);
// Different context
expect(error1.context).not.toBe(error3.context);
});
it('should support error categorization', () => {
const errors: DomainError[] = [
{
type: 'domain',
context: 'user-service',
kind: 'validation',
message: 'Invalid email'
},
{
type: 'domain',
context: 'order-service',
kind: 'invariant',
message: 'Order total must be positive'
},
{
type: 'domain',
context: 'payment-service',
kind: 'BUSINESS_RULE_VIOLATION',
message: 'Payment exceeds limit'
}
];
const validationErrors = errors.filter(e => e.kind === 'validation');
const invariantErrors = errors.filter(e => e.kind === 'invariant');
const businessRuleErrors = errors.filter(e => e.kind === 'BUSINESS_RULE_VIOLATION');
expect(validationErrors).toHaveLength(1);
expect(invariantErrors).toHaveLength(1);
expect(businessRuleErrors).toHaveLength(1);
});
it('should support error aggregation by context', () => {
const errors: DomainError[] = [
{
type: 'domain',
context: 'user-service',
kind: 'validation',
message: 'Invalid email'
},
{
type: 'domain',
context: 'user-service',
kind: 'invariant',
message: 'User must have at least one role'
},
{
type: 'domain',
context: 'order-service',
kind: 'invariant',
message: 'Order total must be positive'
}
];
const userErrors = errors.filter(e => e.context === 'user-service');
const orderErrors = errors.filter(e => e.context === 'order-service');
expect(userErrors).toHaveLength(2);
expect(orderErrors).toHaveLength(1);
});
});
describe('IDomainError interface', () => {
it('should be assignable to DomainError', () => {
const error: IDomainError = {
type: 'domain',
context: 'user-service',
kind: 'validation',
message: 'Invalid email'
};
expect(error.type).toBe('domain');
expect(error.context).toBe('user-service');
expect(error.kind).toBe('validation');
expect(error.message).toBe('Invalid email');
});
it('should support different error kinds', () => {
const validationError: IDomainError = {
type: 'domain',
context: 'form-service',
kind: 'validation',
message: 'Invalid input'
};
const invariantError: IDomainError = {
type: 'domain',
context: 'order-service',
kind: 'invariant',
message: 'Order total must be positive'
};
expect(validationError.kind).toBe('validation');
expect(invariantError.kind).toBe('invariant');
});
});
describe('DomainErrorAlias type', () => {
it('should be assignable to DomainError', () => {
const alias: DomainErrorAlias = {
type: 'domain',
context: 'user-service',
kind: 'validation',
message: 'Invalid email'
};
expect(alias.type).toBe('domain');
expect(alias.context).toBe('user-service');
expect(alias.kind).toBe('validation');
expect(alias.message).toBe('Invalid email');
});
it('should support different error kinds', () => {
const validationError: DomainErrorAlias = {
type: 'domain',
context: 'form-service',
kind: 'validation',
message: 'Invalid input'
};
const invariantError: DomainErrorAlias = {
type: 'domain',
context: 'order-service',
kind: 'invariant',
message: 'Order total must be positive'
};
expect(validationError.kind).toBe('validation');
expect(invariantError.kind).toBe('invariant');
});
});
describe('DomainError implementation patterns', () => {
it('should support error factory pattern', () => {
function createDomainError<K extends string>(
context: string,
kind: K,
message: string,
details?: unknown
): DomainError<K> {
return {
type: 'domain',
context,
kind,
message,
details
};
}
const validationError = createDomainError(
'user-service',
'VALIDATION_ERROR',
'Invalid email',
{ field: 'email', value: 'invalid' }
);
const invariantError = createDomainError(
'order-service',
'INVARIANT_VIOLATION',
'Order total must be positive'
);
expect(validationError.kind).toBe('VALIDATION_ERROR');
expect(validationError.details).toEqual({ field: 'email', value: 'invalid' });
expect(invariantError.kind).toBe('INVARIANT_VIOLATION');
expect(invariantError.details).toBeUndefined();
});
it('should support error builder pattern', () => {
class DomainErrorBuilder<K extends string> {
private context: string = '';
private kind: K = '' as K;
private message: string = '';
private details?: unknown;
withContext(context: string): this {
this.context = context;
return this;
}
withKind(kind: K): this {
this.kind = kind;
return this;
}
withMessage(message: string): this {
this.message = message;
return this;
}
withDetails(details: unknown): this {
this.details = details;
return this;
}
build(): DomainError<K> {
return {
type: 'domain',
context: this.context,
kind: this.kind,
message: this.message,
details: this.details
};
}
}
const error = new DomainErrorBuilder<'VALIDATION_ERROR'>()
.withContext('user-service')
.withKind('VALIDATION_ERROR')
.withMessage('Invalid email')
.withDetails({ field: 'email', value: 'invalid' })
.build();
expect(error.type).toBe('domain');
expect(error.context).toBe('user-service');
expect(error.kind).toBe('VALIDATION_ERROR');
expect(error.message).toBe('Invalid email');
expect(error.details).toEqual({ field: 'email', value: 'invalid' });
});
it('should support error categorization by severity', () => {
const errors: DomainError[] = [
{
type: 'domain',
context: 'user-service',
kind: 'VALIDATION_ERROR',
message: 'Invalid email',
details: { severity: 'low' }
},
{
type: 'domain',
context: 'order-service',
kind: 'INVARIANT_VIOLATION',
message: 'Order total must be positive',
details: { severity: 'high' }
},
{
type: 'domain',
context: 'payment-service',
kind: 'BUSINESS_RULE_VIOLATION',
message: 'Payment exceeds limit',
details: { severity: 'critical' }
}
];
const lowSeverity = errors.filter(e => (e.details as { severity: string })?.severity === 'low');
const highSeverity = errors.filter(e => (e.details as { severity: string })?.severity === 'high');
const criticalSeverity = errors.filter(e => (e.details as { severity: string })?.severity === 'critical');
expect(lowSeverity).toHaveLength(1);
expect(highSeverity).toHaveLength(1);
expect(criticalSeverity).toHaveLength(1);
});
it('should support domain invariant violations', () => {
const invariantError: DomainError<'INVARIANT_VIOLATION'> = {
type: 'domain',
context: 'order-service',
kind: 'INVARIANT_VIOLATION',
message: 'Order total must be positive',
details: {
invariant: 'total > 0',
actualValue: -100,
expectedValue: '> 0'
}
};
expect(invariantError.kind).toBe('INVARIANT_VIOLATION');
expect(invariantError.details).toEqual({
invariant: 'total > 0',
actualValue: -100,
expectedValue: '> 0'
});
});
it('should support business rule violations', () => {
const businessRuleError: DomainError<'BUSINESS_RULE_VIOLATION'> = {
type: 'domain',
context: 'payment-service',
kind: 'BUSINESS_RULE_VIOLATION',
message: 'Payment exceeds limit',
details: {
rule: 'payment_limit',
limit: 1000,
attempted: 1500,
currency: 'USD'
}
};
expect(businessRuleError.kind).toBe('BUSINESS_RULE_VIOLATION');
expect(businessRuleError.details).toEqual({
rule: 'payment_limit',
limit: 1000,
attempted: 1500,
currency: 'USD'
});
});
});
});

View File

@@ -0,0 +1,78 @@
import { describe, expect, it } from 'vitest';
import { SocialDomainError } from './SocialDomainError';
describe('SocialDomainError', () => {
it('creates an error with default kind (validation)', () => {
const error = new SocialDomainError('Invalid social data');
expect(error.name).toBe('SocialDomainError');
expect(error.type).toBe('domain');
expect(error.context).toBe('social');
expect(error.kind).toBe('validation');
expect(error.message).toBe('Invalid social data');
});
it('creates an error with custom kind', () => {
const error = new SocialDomainError('Social graph error', 'repository');
expect(error.name).toBe('SocialDomainError');
expect(error.type).toBe('domain');
expect(error.context).toBe('social');
expect(error.kind).toBe('repository');
expect(error.message).toBe('Social graph error');
});
it('creates an error with business kind', () => {
const error = new SocialDomainError('Friend limit exceeded', 'business');
expect(error.kind).toBe('business');
expect(error.message).toBe('Friend limit exceeded');
});
it('creates an error with infrastructure kind', () => {
const error = new SocialDomainError('Database connection failed', 'infrastructure');
expect(error.kind).toBe('infrastructure');
expect(error.message).toBe('Database connection failed');
});
it('creates an error with technical kind', () => {
const error = new SocialDomainError('Serialization error', 'technical');
expect(error.kind).toBe('technical');
expect(error.message).toBe('Serialization error');
});
it('creates an error with empty message', () => {
const error = new SocialDomainError('');
expect(error.message).toBe('');
expect(error.kind).toBe('validation');
});
it('creates an error with multiline message', () => {
const error = new SocialDomainError('Error\nwith\nmultiple\nlines');
expect(error.message).toBe('Error\nwith\nmultiple\nlines');
});
it('creates an error with special characters in message', () => {
const error = new SocialDomainError('Error with special chars: @#$%^&*()');
expect(error.message).toBe('Error with special chars: @#$%^&*()');
});
it('error is instance of Error', () => {
const error = new SocialDomainError('Test error');
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(SocialDomainError);
});
it('error has correct prototype chain', () => {
const error = new SocialDomainError('Test error');
expect(Object.getPrototypeOf(error)).toBe(SocialDomainError.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(error))).toBe(Error.prototype);
});
});

28
package-lock.json generated
View File

@@ -251,6 +251,27 @@
"undici-types": "~6.21.0"
}
},
"apps/companion/node_modules/@types/react": {
"version": "18.3.27",
"resolved": "https://registry.npmjs.org/@types/react/-/react-18.3.27.tgz",
"integrity": "sha512-cisd7gxkzjBKU2GgdYrTdtQx1SORymWyaAFhaxQPK9bYO9ot3Y5OikQRvY0VYQtvwjeQnizCINJAenh/V7MK2w==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/prop-types": "*",
"csstype": "^3.2.2"
}
},
"apps/companion/node_modules/@types/react-dom": {
"version": "18.3.7",
"resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-18.3.7.tgz",
"integrity": "sha512-MEe3UeoENYVFXzoXEWsvcpg6ZvlrFNlOQ7EOsvhI3CfAXwzPfO8Qwuxd40nepsYKqyyVQnTdEfv68q91yLcKrQ==",
"dev": true,
"license": "MIT",
"peerDependencies": {
"@types/react": "^18.0.0"
}
},
"apps/companion/node_modules/path-to-regexp": {
"version": "8.3.0",
"license": "MIT",
@@ -4717,6 +4738,13 @@
"dev": true,
"license": "MIT"
},
"node_modules/@types/prop-types": {
"version": "15.7.15",
"resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.15.tgz",
"integrity": "sha512-F6bEyamV9jKGAFBEmlQnesRPGOQqS2+Uwi0Em15xenOxHaf2hv6L8YCVn3rPdPJOiJfPiCnLIRyvwVaqMY3MIw==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/qs": {
"version": "6.14.0",
"resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.14.0.tgz",