add tests to core
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import {
|
||||
RequestAvatarGenerationUseCase,
|
||||
type RequestAvatarGenerationInput,
|
||||
type RequestAvatarGenerationErrorCode,
|
||||
type RequestAvatarGenerationResult,
|
||||
} from './RequestAvatarGenerationUseCase';
|
||||
import type { IAvatarGenerationRepository } from '../../domain/repositories/IAvatarGenerationRepository';
|
||||
import type { FaceValidationPort } from '../ports/FaceValidationPort';
|
||||
import type { AvatarGenerationPort } from '../ports/AvatarGenerationPort';
|
||||
|
||||
vi.mock('uuid', () => ({
|
||||
v4: () => 'request-1',
|
||||
}));
|
||||
|
||||
interface TestOutputPort extends UseCaseOutputPort<RequestAvatarGenerationResult> {
|
||||
present: Mock;
|
||||
result?: RequestAvatarGenerationResult;
|
||||
}
|
||||
|
||||
describe('RequestAvatarGenerationUseCase', () => {
|
||||
let avatarRepo: { save: Mock };
|
||||
let faceValidation: { validateFacePhoto: Mock };
|
||||
let avatarGeneration: { generateAvatars: Mock };
|
||||
let output: TestOutputPort;
|
||||
let logger: Logger;
|
||||
let useCase: RequestAvatarGenerationUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
avatarRepo = {
|
||||
save: vi.fn(),
|
||||
};
|
||||
|
||||
faceValidation = {
|
||||
validateFacePhoto: vi.fn(),
|
||||
};
|
||||
|
||||
avatarGeneration = {
|
||||
generateAvatars: vi.fn(),
|
||||
};
|
||||
|
||||
output = {
|
||||
present: vi.fn((result: RequestAvatarGenerationResult) => {
|
||||
output.result = result;
|
||||
}),
|
||||
} as unknown as TestOutputPort;
|
||||
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
useCase = new RequestAvatarGenerationUseCase(
|
||||
avatarRepo as unknown as IAvatarGenerationRepository,
|
||||
faceValidation as unknown as FaceValidationPort,
|
||||
avatarGeneration as unknown as AvatarGenerationPort,
|
||||
output,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
|
||||
it('completes generation and presents avatar URLs', async () => {
|
||||
faceValidation.validateFacePhoto.mockResolvedValue({
|
||||
isValid: true,
|
||||
hasFace: true,
|
||||
faceCount: 1,
|
||||
});
|
||||
|
||||
avatarGeneration.generateAvatars.mockResolvedValue({
|
||||
success: true,
|
||||
avatars: [{ url: 'https://example.com/a.png' }, { url: 'https://example.com/b.png' }],
|
||||
});
|
||||
|
||||
const input: RequestAvatarGenerationInput = {
|
||||
userId: 'user-1',
|
||||
facePhotoData: 'data:image/png;base64,abc',
|
||||
suitColor: 'red' as unknown as RequestAvatarGenerationInput['suitColor'],
|
||||
style: 'cartoon',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result).toBeInstanceOf(Result);
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
expect(faceValidation.validateFacePhoto).toHaveBeenCalledWith(input.facePhotoData);
|
||||
expect(avatarGeneration.generateAvatars).toHaveBeenCalled();
|
||||
expect(avatarRepo.save).toHaveBeenCalledTimes(4);
|
||||
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
requestId: 'request-1',
|
||||
status: 'completed',
|
||||
avatarUrls: ['https://example.com/a.png', 'https://example.com/b.png'],
|
||||
});
|
||||
});
|
||||
|
||||
it('returns FACE_VALIDATION_FAILED when face validation fails', async () => {
|
||||
faceValidation.validateFacePhoto.mockResolvedValue({
|
||||
isValid: false,
|
||||
hasFace: true,
|
||||
faceCount: 1,
|
||||
errorMessage: 'Bad image',
|
||||
});
|
||||
|
||||
const input: RequestAvatarGenerationInput = {
|
||||
userId: 'user-1',
|
||||
facePhotoData: 'data:image/png;base64,abc',
|
||||
suitColor: 'red' as unknown as RequestAvatarGenerationInput['suitColor'],
|
||||
};
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<RequestAvatarGenerationErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr();
|
||||
expect(err.code).toBe('FACE_VALIDATION_FAILED');
|
||||
expect(err.details?.message).toBe('Bad image');
|
||||
|
||||
expect(avatarGeneration.generateAvatars).not.toHaveBeenCalled();
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect(avatarRepo.save).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it('returns GENERATION_FAILED when avatar generation fails', async () => {
|
||||
faceValidation.validateFacePhoto.mockResolvedValue({
|
||||
isValid: true,
|
||||
hasFace: true,
|
||||
faceCount: 1,
|
||||
});
|
||||
|
||||
avatarGeneration.generateAvatars.mockResolvedValue({
|
||||
success: false,
|
||||
errorMessage: 'Generation service down',
|
||||
avatars: [],
|
||||
});
|
||||
|
||||
const input: RequestAvatarGenerationInput = {
|
||||
userId: 'user-1',
|
||||
facePhotoData: 'data:image/png;base64,abc',
|
||||
suitColor: 'red' as unknown as RequestAvatarGenerationInput['suitColor'],
|
||||
};
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<RequestAvatarGenerationErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr();
|
||||
expect(err.code).toBe('GENERATION_FAILED');
|
||||
expect(err.details?.message).toBe('Generation service down');
|
||||
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect(avatarRepo.save).toHaveBeenCalledTimes(4);
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws', async () => {
|
||||
avatarRepo.save.mockRejectedValueOnce(new Error('DB error'));
|
||||
|
||||
const input: RequestAvatarGenerationInput = {
|
||||
userId: 'user-1',
|
||||
facePhotoData: 'data:image/png;base64,abc',
|
||||
suitColor: 'red' as unknown as RequestAvatarGenerationInput['suitColor'],
|
||||
};
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<RequestAvatarGenerationErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr();
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details?.message).toBe('DB error');
|
||||
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
126
core/media/application/use-cases/SelectAvatarUseCase.test.ts
Normal file
126
core/media/application/use-cases/SelectAvatarUseCase.test.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import {
|
||||
SelectAvatarUseCase,
|
||||
type SelectAvatarErrorCode,
|
||||
type SelectAvatarInput,
|
||||
type SelectAvatarResult,
|
||||
} from './SelectAvatarUseCase';
|
||||
import type { IAvatarGenerationRepository } from '../../domain/repositories/IAvatarGenerationRepository';
|
||||
import { AvatarGenerationRequest } from '../../domain/entities/AvatarGenerationRequest';
|
||||
|
||||
interface TestOutputPort extends UseCaseOutputPort<SelectAvatarResult> {
|
||||
present: Mock;
|
||||
result?: SelectAvatarResult;
|
||||
}
|
||||
|
||||
describe('SelectAvatarUseCase', () => {
|
||||
let avatarRepo: { findById: Mock; save: Mock };
|
||||
let logger: Logger;
|
||||
let output: TestOutputPort;
|
||||
let useCase: SelectAvatarUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
avatarRepo = {
|
||||
findById: vi.fn(),
|
||||
save: vi.fn(),
|
||||
};
|
||||
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn((result: SelectAvatarResult) => {
|
||||
output.result = result;
|
||||
}),
|
||||
} as unknown as TestOutputPort;
|
||||
|
||||
useCase = new SelectAvatarUseCase(
|
||||
avatarRepo as unknown as IAvatarGenerationRepository,
|
||||
output,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns REQUEST_NOT_FOUND when request does not exist', async () => {
|
||||
avatarRepo.findById.mockResolvedValue(null);
|
||||
|
||||
const input: SelectAvatarInput = { requestId: 'missing', selectedIndex: 0 };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(avatarRepo.findById).toHaveBeenCalledWith('missing');
|
||||
expect(result).toBeInstanceOf(Result);
|
||||
expect(result.isErr()).toBe(true);
|
||||
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<SelectAvatarErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('REQUEST_NOT_FOUND');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns REQUEST_NOT_COMPLETED when request is not completed', async () => {
|
||||
const request = AvatarGenerationRequest.create({
|
||||
id: 'req-1',
|
||||
userId: 'user-1',
|
||||
facePhotoUrl: 'data:image/png;base64,abc',
|
||||
suitColor: 'red' as unknown as Parameters<typeof AvatarGenerationRequest.create>[0]['suitColor'],
|
||||
});
|
||||
|
||||
avatarRepo.findById.mockResolvedValue(request);
|
||||
|
||||
const input: SelectAvatarInput = { requestId: 'req-1', selectedIndex: 0 };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<SelectAvatarErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('REQUEST_NOT_COMPLETED');
|
||||
|
||||
expect(avatarRepo.save).not.toHaveBeenCalled();
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('selects avatar and presents selected URL when request is completed', async () => {
|
||||
const request = AvatarGenerationRequest.create({
|
||||
id: 'req-1',
|
||||
userId: 'user-1',
|
||||
facePhotoUrl: 'data:image/png;base64,abc',
|
||||
suitColor: 'red' as unknown as Parameters<typeof AvatarGenerationRequest.create>[0]['suitColor'],
|
||||
});
|
||||
request.completeWithAvatars(['https://example.com/a.png', 'https://example.com/b.png']);
|
||||
|
||||
avatarRepo.findById.mockResolvedValue(request);
|
||||
|
||||
const input: SelectAvatarInput = { requestId: 'req-1', selectedIndex: 1 };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(request.selectedAvatarIndex).toBe(1);
|
||||
expect(request.selectedAvatarUrl).toBe('https://example.com/b.png');
|
||||
|
||||
expect(avatarRepo.save).toHaveBeenCalledWith(request);
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
requestId: 'req-1',
|
||||
selectedAvatarUrl: 'https://example.com/b.png',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws', async () => {
|
||||
avatarRepo.findById.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const input: SelectAvatarInput = { requestId: 'req-1', selectedIndex: 0 };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<SelectAvatarErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details?.message).toBe('DB error');
|
||||
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
130
core/media/application/use-cases/UpdateAvatarUseCase.test.ts
Normal file
130
core/media/application/use-cases/UpdateAvatarUseCase.test.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import {
|
||||
UpdateAvatarUseCase,
|
||||
type UpdateAvatarErrorCode,
|
||||
type UpdateAvatarInput,
|
||||
type UpdateAvatarResult,
|
||||
} from './UpdateAvatarUseCase';
|
||||
import type { IAvatarRepository } from '../../domain/repositories/IAvatarRepository';
|
||||
import { Avatar } from '../../domain/entities/Avatar';
|
||||
|
||||
vi.mock('uuid', () => ({
|
||||
v4: () => 'avatar-1',
|
||||
}));
|
||||
|
||||
interface TestOutputPort extends UseCaseOutputPort<UpdateAvatarResult> {
|
||||
present: Mock;
|
||||
result?: UpdateAvatarResult;
|
||||
}
|
||||
|
||||
describe('UpdateAvatarUseCase', () => {
|
||||
let avatarRepo: { findActiveByDriverId: Mock; save: Mock };
|
||||
let logger: Logger;
|
||||
let output: TestOutputPort;
|
||||
let useCase: UpdateAvatarUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
avatarRepo = {
|
||||
findActiveByDriverId: vi.fn(),
|
||||
save: vi.fn(),
|
||||
};
|
||||
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn((result: UpdateAvatarResult) => {
|
||||
output.result = result;
|
||||
}),
|
||||
} as unknown as TestOutputPort;
|
||||
|
||||
useCase = new UpdateAvatarUseCase(
|
||||
avatarRepo as unknown as IAvatarRepository,
|
||||
output,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
|
||||
it('creates new avatar when no current active avatar exists', async () => {
|
||||
avatarRepo.findActiveByDriverId.mockResolvedValue(null);
|
||||
|
||||
const input: UpdateAvatarInput = {
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result).toBeInstanceOf(Result);
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
expect(avatarRepo.findActiveByDriverId).toHaveBeenCalledWith('driver-1');
|
||||
expect(avatarRepo.save).toHaveBeenCalledTimes(1);
|
||||
|
||||
const saved = (avatarRepo.save as unknown as Mock).mock.calls[0]![0] as Avatar;
|
||||
expect(saved.driverId).toBe('driver-1');
|
||||
expect(saved.mediaUrl.value).toBe('https://example.com/avatar.png');
|
||||
expect(saved.isActive).toBe(true);
|
||||
|
||||
expect(output.present).toHaveBeenCalledWith({ avatarId: 'avatar-1', driverId: 'driver-1' });
|
||||
});
|
||||
|
||||
it('deactivates current avatar before saving new avatar', async () => {
|
||||
const currentAvatar = Avatar.reconstitute({
|
||||
id: 'old-avatar',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/old.png',
|
||||
selectedAt: new Date('2020-01-01T00:00:00.000Z'),
|
||||
isActive: true,
|
||||
});
|
||||
|
||||
avatarRepo.findActiveByDriverId.mockResolvedValue(currentAvatar);
|
||||
|
||||
const input: UpdateAvatarInput = {
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/new.png',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(avatarRepo.save).toHaveBeenCalledTimes(2);
|
||||
|
||||
const firstSaved = (avatarRepo.save as unknown as Mock).mock.calls[0]![0] as Avatar;
|
||||
expect(firstSaved.id).toBe('old-avatar');
|
||||
expect(firstSaved.isActive).toBe(false);
|
||||
|
||||
const secondSaved = (avatarRepo.save as unknown as Mock).mock.calls[1]![0] as Avatar;
|
||||
expect(secondSaved.id).toBe('avatar-1');
|
||||
expect(secondSaved.isActive).toBe(true);
|
||||
|
||||
expect(output.present).toHaveBeenCalledWith({ avatarId: 'avatar-1', driverId: 'driver-1' });
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws', async () => {
|
||||
avatarRepo.findActiveByDriverId.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const input: UpdateAvatarInput = {
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
};
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<UpdateAvatarErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr();
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details?.message).toBe('DB error');
|
||||
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
156
core/media/application/use-cases/UploadMediaUseCase.test.ts
Normal file
156
core/media/application/use-cases/UploadMediaUseCase.test.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import { Readable } from 'node:stream';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import {
|
||||
UploadMediaUseCase,
|
||||
type MulterFile,
|
||||
type UploadMediaErrorCode,
|
||||
type UploadMediaInput,
|
||||
type UploadMediaResult,
|
||||
} from './UploadMediaUseCase';
|
||||
import type { IMediaRepository } from '../../domain/repositories/IMediaRepository';
|
||||
import type { MediaStoragePort } from '../ports/MediaStoragePort';
|
||||
import { Media } from '../../domain/entities/Media';
|
||||
|
||||
vi.mock('uuid', () => ({
|
||||
v4: () => 'media-1',
|
||||
}));
|
||||
|
||||
interface TestOutputPort extends UseCaseOutputPort<UploadMediaResult> {
|
||||
present: Mock;
|
||||
result?: UploadMediaResult;
|
||||
}
|
||||
|
||||
describe('UploadMediaUseCase', () => {
|
||||
let mediaRepo: { save: Mock };
|
||||
let mediaStorage: { uploadMedia: Mock };
|
||||
let logger: Logger;
|
||||
let output: TestOutputPort;
|
||||
let useCase: UploadMediaUseCase;
|
||||
|
||||
const baseFile: MulterFile = {
|
||||
fieldname: 'file',
|
||||
originalname: 'avatar.png',
|
||||
encoding: '7bit',
|
||||
mimetype: 'image/png',
|
||||
size: 123,
|
||||
buffer: Buffer.from('abc'),
|
||||
stream: Readable.from([]),
|
||||
destination: '/tmp',
|
||||
filename: 'avatar.png',
|
||||
path: '/tmp/avatar.png',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mediaRepo = {
|
||||
save: vi.fn(),
|
||||
};
|
||||
|
||||
mediaStorage = {
|
||||
uploadMedia: vi.fn(),
|
||||
};
|
||||
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn((result: UploadMediaResult) => {
|
||||
output.result = result;
|
||||
}),
|
||||
} as unknown as TestOutputPort;
|
||||
|
||||
useCase = new UploadMediaUseCase(
|
||||
mediaRepo as unknown as IMediaRepository,
|
||||
mediaStorage as unknown as MediaStoragePort,
|
||||
output,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns UPLOAD_FAILED when storage returns unsuccessful result', async () => {
|
||||
mediaStorage.uploadMedia.mockResolvedValue({
|
||||
success: false,
|
||||
errorMessage: 'Upload error',
|
||||
});
|
||||
|
||||
const input: UploadMediaInput = { file: baseFile, uploadedBy: 'user-1' };
|
||||
const result: Result<void, ApplicationErrorCode<UploadMediaErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr();
|
||||
expect(err.code).toBe('UPLOAD_FAILED');
|
||||
expect(err.details?.message).toBe('Upload error');
|
||||
|
||||
expect(mediaRepo.save).not.toHaveBeenCalled();
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('creates media and presents mediaId/url on success (includes metadata)', async () => {
|
||||
mediaStorage.uploadMedia.mockResolvedValue({
|
||||
success: true,
|
||||
url: 'https://example.com/media.png',
|
||||
filename: 'stored.png',
|
||||
});
|
||||
|
||||
const input: UploadMediaInput = {
|
||||
file: baseFile,
|
||||
uploadedBy: 'user-1',
|
||||
metadata: { foo: 'bar' },
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result).toBeInstanceOf(Result);
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
expect(mediaStorage.uploadMedia).toHaveBeenCalledWith(baseFile.buffer, {
|
||||
filename: baseFile.originalname,
|
||||
mimeType: baseFile.mimetype,
|
||||
metadata: { foo: 'bar' },
|
||||
});
|
||||
|
||||
expect(mediaRepo.save).toHaveBeenCalledTimes(1);
|
||||
const saved = (mediaRepo.save as unknown as Mock).mock.calls[0]![0] as Media;
|
||||
expect(saved.id).toBe('media-1');
|
||||
expect(saved.url.value).toBe('https://example.com/media.png');
|
||||
expect(saved.filename).toBe('stored.png');
|
||||
expect(saved.originalName).toBe('avatar.png');
|
||||
expect(saved.mimeType).toBe('image/png');
|
||||
expect(saved.size).toBe(123);
|
||||
expect(saved.type).toBe('image');
|
||||
expect(saved.uploadedBy).toBe('user-1');
|
||||
expect(saved.metadata).toEqual({ foo: 'bar' });
|
||||
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
mediaId: 'media-1',
|
||||
url: 'https://example.com/media.png',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws', async () => {
|
||||
mediaStorage.uploadMedia.mockResolvedValue({
|
||||
success: true,
|
||||
url: 'https://example.com/media.png',
|
||||
});
|
||||
mediaRepo.save.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const input: UploadMediaInput = { file: baseFile, uploadedBy: 'user-1' };
|
||||
const result: Result<void, ApplicationErrorCode<UploadMediaErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr();
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details?.message).toBe('DB error');
|
||||
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user