2023-06-08 17:01:07 +02:00
|
|
|
import { assetEntityStub, newAssetRepositoryMock, newJobRepositoryMock, newStorageRepositoryMock } from '@test';
|
2023-06-16 21:54:17 +02:00
|
|
|
import { constants } from 'fs/promises';
|
2023-05-26 14:52:52 +02:00
|
|
|
import { IAssetRepository, WithoutProperty, WithProperty } from '../asset';
|
|
|
|
import { IJobRepository, JobName } from '../job';
|
|
|
|
import { IStorageRepository } from '../storage';
|
|
|
|
import { MetadataService } from './metadata.service';
|
|
|
|
|
|
|
|
describe(MetadataService.name, () => {
|
|
|
|
let sut: MetadataService;
|
|
|
|
let assetMock: jest.Mocked<IAssetRepository>;
|
|
|
|
let jobMock: jest.Mocked<IJobRepository>;
|
|
|
|
let storageMock: jest.Mocked<IStorageRepository>;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
assetMock = newAssetRepositoryMock();
|
|
|
|
jobMock = newJobRepositoryMock();
|
|
|
|
storageMock = newStorageRepositoryMock();
|
|
|
|
|
|
|
|
sut = new MetadataService(assetMock, jobMock, storageMock);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be defined', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('handleQueueSidecar', () => {
|
|
|
|
it('should queue assets with sidecar files', async () => {
|
|
|
|
assetMock.getWith.mockResolvedValue({ items: [assetEntityStub.sidecar], hasNextPage: false });
|
|
|
|
|
|
|
|
await sut.handleQueueSidecar({ force: true });
|
|
|
|
|
|
|
|
expect(assetMock.getWith).toHaveBeenCalledWith({ take: 1000, skip: 0 }, WithProperty.SIDECAR);
|
|
|
|
expect(assetMock.getWithout).not.toHaveBeenCalled();
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.SIDECAR_SYNC,
|
2023-05-26 21:43:24 +02:00
|
|
|
data: { id: assetEntityStub.sidecar.id },
|
2023-05-26 14:52:52 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should queue assets without sidecar files', async () => {
|
|
|
|
assetMock.getWithout.mockResolvedValue({ items: [assetEntityStub.image], hasNextPage: false });
|
|
|
|
|
|
|
|
await sut.handleQueueSidecar({ force: false });
|
|
|
|
|
|
|
|
expect(assetMock.getWithout).toHaveBeenCalledWith({ take: 1000, skip: 0 }, WithoutProperty.SIDECAR);
|
|
|
|
expect(assetMock.getWith).not.toHaveBeenCalled();
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.SIDECAR_DISCOVERY,
|
2023-05-26 21:43:24 +02:00
|
|
|
data: { id: assetEntityStub.image.id },
|
2023-05-26 14:52:52 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('handleSidecarSync', () => {
|
2023-05-26 21:43:24 +02:00
|
|
|
it('should not error', async () => {
|
|
|
|
await sut.handleSidecarSync();
|
2023-05-26 14:52:52 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('handleSidecarDiscovery', () => {
|
|
|
|
it('should skip hidden assets', async () => {
|
2023-05-26 21:43:24 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetEntityStub.livePhotoMotionAsset]);
|
|
|
|
await sut.handleSidecarDiscovery({ id: assetEntityStub.livePhotoMotionAsset.id });
|
2023-05-26 14:52:52 +02:00
|
|
|
expect(storageMock.checkFileExists).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip assets with a sidecar path', async () => {
|
2023-05-26 21:43:24 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetEntityStub.sidecar]);
|
|
|
|
await sut.handleSidecarDiscovery({ id: assetEntityStub.sidecar.id });
|
2023-05-26 14:52:52 +02:00
|
|
|
expect(storageMock.checkFileExists).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do nothing when a sidecar is not found ', async () => {
|
2023-05-26 21:43:24 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetEntityStub.image]);
|
2023-05-26 14:52:52 +02:00
|
|
|
storageMock.checkFileExists.mockResolvedValue(false);
|
2023-05-26 21:43:24 +02:00
|
|
|
await sut.handleSidecarDiscovery({ id: assetEntityStub.image.id });
|
2023-05-26 14:52:52 +02:00
|
|
|
expect(assetMock.save).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update a image asset when a sidecar is found', async () => {
|
2023-05-26 21:43:24 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetEntityStub.image]);
|
2023-05-26 14:52:52 +02:00
|
|
|
assetMock.save.mockResolvedValue(assetEntityStub.image);
|
|
|
|
storageMock.checkFileExists.mockResolvedValue(true);
|
2023-05-26 21:43:24 +02:00
|
|
|
await sut.handleSidecarDiscovery({ id: assetEntityStub.image.id });
|
2023-07-10 19:56:45 +02:00
|
|
|
expect(storageMock.checkFileExists).toHaveBeenCalledWith('/original/path.jpg.xmp', constants.R_OK);
|
2023-05-26 14:52:52 +02:00
|
|
|
expect(assetMock.save).toHaveBeenCalledWith({
|
|
|
|
id: assetEntityStub.image.id,
|
2023-07-10 19:56:45 +02:00
|
|
|
sidecarPath: '/original/path.jpg.xmp',
|
2023-05-26 14:52:52 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update a video asset when a sidecar is found', async () => {
|
2023-05-26 21:43:24 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetEntityStub.video]);
|
2023-05-26 14:52:52 +02:00
|
|
|
assetMock.save.mockResolvedValue(assetEntityStub.video);
|
|
|
|
storageMock.checkFileExists.mockResolvedValue(true);
|
2023-05-26 21:43:24 +02:00
|
|
|
await sut.handleSidecarDiscovery({ id: assetEntityStub.video.id });
|
2023-06-30 18:25:08 +02:00
|
|
|
expect(storageMock.checkFileExists).toHaveBeenCalledWith('/original/path.ext.xmp', constants.R_OK);
|
2023-05-26 14:52:52 +02:00
|
|
|
expect(assetMock.save).toHaveBeenCalledWith({
|
|
|
|
id: assetEntityStub.image.id,
|
|
|
|
sidecarPath: '/original/path.ext.xmp',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|