2023-03-30 21:38:55 +02:00
|
|
|
import { AssetEntity, AssetType } from '@app/infra/entities';
|
2023-05-21 08:26:06 +02:00
|
|
|
import { assetEntityStub, authStub, newAssetRepositoryMock, newJobRepositoryMock } from '../../test';
|
2023-03-03 03:47:08 +01:00
|
|
|
import { AssetService, IAssetRepository } from '../asset';
|
2023-02-25 15:12:03 +01:00
|
|
|
import { IJobRepository, JobName } from '../job';
|
|
|
|
|
|
|
|
describe(AssetService.name, () => {
|
|
|
|
let sut: AssetService;
|
2023-03-03 03:47:08 +01:00
|
|
|
let assetMock: jest.Mocked<IAssetRepository>;
|
2023-02-25 15:12:03 +01:00
|
|
|
let jobMock: jest.Mocked<IJobRepository>;
|
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-03-03 03:47:08 +01:00
|
|
|
assetMock = newAssetRepositoryMock();
|
2023-02-25 15:12:03 +01:00
|
|
|
jobMock = newJobRepositoryMock();
|
2023-03-05 21:44:31 +01:00
|
|
|
sut = new AssetService(assetMock, jobMock);
|
2023-02-25 15:12:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe(`handle asset upload`, () => {
|
|
|
|
it('should process an uploaded video', async () => {
|
2023-05-22 17:26:56 +02:00
|
|
|
const data = { asset: { type: AssetType.VIDEO } as AssetEntity };
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
await expect(sut.handleAssetUpload(data)).resolves.toBeUndefined();
|
|
|
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledTimes(3);
|
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
[{ name: JobName.GENERATE_JPEG_THUMBNAIL, data }],
|
|
|
|
[{ name: JobName.VIDEO_CONVERSION, data }],
|
|
|
|
[{ name: JobName.EXTRACT_VIDEO_METADATA, data }],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should process an uploaded image', async () => {
|
2023-05-22 17:26:56 +02:00
|
|
|
const data = { asset: { type: AssetType.IMAGE } as AssetEntity };
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
await sut.handleAssetUpload(data);
|
|
|
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledTimes(2);
|
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
[{ name: JobName.GENERATE_JPEG_THUMBNAIL, data }],
|
|
|
|
[{ name: JobName.EXIF_EXTRACTION, data }],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2023-03-03 03:47:08 +01:00
|
|
|
|
|
|
|
describe('save', () => {
|
|
|
|
it('should save an asset', async () => {
|
|
|
|
assetMock.save.mockResolvedValue(assetEntityStub.image);
|
|
|
|
|
|
|
|
await sut.save(assetEntityStub.image);
|
|
|
|
|
|
|
|
expect(assetMock.save).toHaveBeenCalledWith(assetEntityStub.image);
|
2023-03-05 21:44:31 +01:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.SEARCH_INDEX_ASSET,
|
2023-03-18 14:44:42 +01:00
|
|
|
data: { ids: [assetEntityStub.image.id] },
|
2023-03-05 21:44:31 +01:00
|
|
|
});
|
2023-03-03 03:47:08 +01:00
|
|
|
});
|
|
|
|
});
|
2023-05-21 08:26:06 +02:00
|
|
|
|
|
|
|
describe('get map markers', () => {
|
|
|
|
it('should get geo information of assets', async () => {
|
|
|
|
assetMock.getMapMarkers.mockResolvedValue(
|
|
|
|
[assetEntityStub.withLocation].map((asset) => ({
|
|
|
|
id: asset.id,
|
|
|
|
|
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
|
|
|
lat: asset.exifInfo!.latitude!,
|
|
|
|
|
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
|
|
|
lon: asset.exifInfo!.longitude!,
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
|
|
|
|
const markers = await sut.getMapMarkers(authStub.user1, {});
|
|
|
|
|
|
|
|
expect(markers).toHaveLength(1);
|
|
|
|
expect(markers[0]).toEqual({
|
|
|
|
id: assetEntityStub.withLocation.id,
|
|
|
|
lat: 100,
|
|
|
|
lon: 100,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-02-25 15:12:03 +01:00
|
|
|
});
|