Files
gridpilot.gg/tests/integration/media/avatars/avatar-retrieval-and-updates.test.ts
Marc Mintel 6df38a462a
Some checks failed
CI / lint-typecheck (pull_request) Failing after 4m50s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
integration tests
2026-01-23 11:44:59 +01:00

90 lines
3.2 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest';
import { MediaTestContext } from '../MediaTestContext';
import { Avatar } from '@core/media/domain/entities/Avatar';
describe('Avatar Management: Retrieval and Updates', () => {
let ctx: MediaTestContext;
beforeEach(() => {
ctx = MediaTestContext.create();
ctx.reset();
});
describe('GetAvatarUseCase', () => {
it('should retrieve driver avatar when avatar exists', async () => {
const avatar = Avatar.create({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'https://example.com/avatar.png',
});
await ctx.avatarRepository.save(avatar);
const result = await ctx.getAvatarUseCase.execute({ driverId: 'driver-1' });
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult.avatar.id).toBe('avatar-1');
expect(successResult.avatar.driverId).toBe('driver-1');
expect(successResult.avatar.mediaUrl).toBe('https://example.com/avatar.png');
});
it('should return AVATAR_NOT_FOUND when driver has no avatar', async () => {
const result = await ctx.getAvatarUseCase.execute({ driverId: 'driver-1' });
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('AVATAR_NOT_FOUND');
});
it('should handle repository errors gracefully', async () => {
const originalFind = ctx.avatarRepository.findActiveByDriverId;
ctx.avatarRepository.findActiveByDriverId = async () => {
throw new Error('Database connection error');
};
const result = await ctx.getAvatarUseCase.execute({ driverId: 'driver-1' });
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('REPOSITORY_ERROR');
ctx.avatarRepository.findActiveByDriverId = originalFind;
});
});
describe('UpdateAvatarUseCase', () => {
it('should update existing avatar for a driver', async () => {
const existingAvatar = Avatar.create({
id: 'avatar-1',
driverId: 'driver-1',
mediaUrl: 'https://example.com/old-avatar.png',
});
await ctx.avatarRepository.save(existingAvatar);
const result = await ctx.updateAvatarUseCase.execute({
driverId: 'driver-1',
mediaUrl: 'https://example.com/new-avatar.png',
});
expect(result.isOk()).toBe(true);
const oldAvatar = await ctx.avatarRepository.findById('avatar-1');
expect(oldAvatar?.isActive).toBe(false);
const newAvatar = await ctx.avatarRepository.findActiveByDriverId('driver-1');
expect(newAvatar).not.toBeNull();
expect(newAvatar?.mediaUrl.value).toBe('https://example.com/new-avatar.png');
});
it('should update avatar when driver has no existing avatar', async () => {
const result = await ctx.updateAvatarUseCase.execute({
driverId: 'driver-1',
mediaUrl: 'https://example.com/avatar.png',
});
expect(result.isOk()).toBe(true);
const newAvatar = await ctx.avatarRepository.findActiveByDriverId('driver-1');
expect(newAvatar).not.toBeNull();
expect(newAvatar?.mediaUrl.value).toBe('https://example.com/avatar.png');
});
});
});