Skip to content

Commit

Permalink
tests and minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldietzler committed Feb 5, 2024
1 parent a90d992 commit 2ef4583
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
49 changes: 43 additions & 6 deletions server/src/domain/metadata/metadata.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,11 @@ describe(MetadataService.name, () => {

describe('handleQueueSidecar', () => {
it('should queue assets with sidecar files', async () => {
assetMock.getWith.mockResolvedValue({ items: [assetStub.sidecar], hasNextPage: false });
assetMock.getAll.mockResolvedValue({ items: [assetStub.sidecar], hasNextPage: false });

await sut.handleQueueSidecar({ force: true });

expect(assetMock.getWith).toHaveBeenCalledWith({ take: 1000, skip: 0 }, WithProperty.SIDECAR);
expect(assetMock.getAll).toHaveBeenCalledWith({ take: 1000, skip: 0 });
expect(assetMock.getWithout).not.toHaveBeenCalled();
expect(jobMock.queueAll).toHaveBeenCalledWith([
{
Expand All @@ -618,7 +618,7 @@ describe(MetadataService.name, () => {
await sut.handleQueueSidecar({ force: false });

expect(assetMock.getWithout).toHaveBeenCalledWith({ take: 1000, skip: 0 }, WithoutProperty.SIDECAR);
expect(assetMock.getWith).not.toHaveBeenCalled();
expect(assetMock.getAll).not.toHaveBeenCalled();
expect(jobMock.queueAll).toHaveBeenCalledWith([
{
name: JobName.SIDECAR_DISCOVERY,
Expand All @@ -629,9 +629,46 @@ describe(MetadataService.name, () => {
});

describe('handleSidecarSync', () => {
it('should not error', async () => {
assetMock.getByIds.mockResolvedValue([assetStub.livePhotoMotionAsset]);
await sut.handleSidecarSync({ id: assetStub.livePhotoMotionAsset.id });
it('should do nothing if asset could not be found', async () => {
assetMock.getByIds.mockResolvedValue([]);
await expect(sut.handleSidecarSync({ id: assetStub.image.id })).resolves.toBe(false);
expect(assetMock.save).not.toHaveBeenCalled();
});

it('should do nothing if asset has no sidecar path', async () => {
assetMock.getByIds.mockResolvedValue([assetStub.image]);
await expect(sut.handleSidecarSync({ id: assetStub.image.id })).resolves.toBe(false);
expect(assetMock.save).not.toHaveBeenCalled();
});

it('should do nothing if asset has no sidecar path', async () => {
assetMock.getByIds.mockResolvedValue([assetStub.image]);
await expect(sut.handleSidecarSync({ id: assetStub.image.id })).resolves.toBe(false);
expect(assetMock.save).not.toHaveBeenCalled();
});

it('should set sidecar path if exists', async () => {
assetMock.getByIds.mockResolvedValue([assetStub.sidecar]);
storageMock.checkFileExists.mockResolvedValue(true);

await expect(sut.handleSidecarSync({ id: assetStub.sidecar.id })).resolves.toBe(true);
expect(storageMock.checkFileExists).toHaveBeenCalledWith(`${assetStub.sidecar.originalPath}.xmp`, constants.R_OK);
expect(assetMock.save).toHaveBeenCalledWith({
id: assetStub.sidecar.id,
sidecarPath: assetStub.sidecar.sidecarPath,
});
});

it('should unset sidecar path if file does not exist anymore', async () => {
assetMock.getByIds.mockResolvedValue([assetStub.sidecar]);
storageMock.checkFileExists.mockResolvedValue(false);

await expect(sut.handleSidecarSync({ id: assetStub.sidecar.id })).resolves.toBe(true);
expect(storageMock.checkFileExists).toHaveBeenCalledWith(`${assetStub.sidecar.originalPath}.xmp`, constants.R_OK);
expect(assetMock.save).toHaveBeenCalledWith({
id: assetStub.sidecar.id,
sidecarPath: null,
});
});
});

Expand Down
25 changes: 16 additions & 9 deletions server/src/domain/metadata/metadata.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,25 +554,32 @@ export class MetadataService {
private async processSidecar(id: string, isSync: boolean) {
const [asset] = await this.assetRepository.getByIds([id]);

if (!asset || (isSync && !asset.sidecarPath) || (!isSync && (!asset.isVisible || asset.sidecarPath))) {
if (!asset) {
return false;
}

if (isSync && !asset.sidecarPath) {
return false;
}

if (!isSync && (!asset.isVisible || asset.sidecarPath)) {
return false;
}

const sidecarPath = `${asset.originalPath}.xmp`;
const exists = await this.storageRepository.checkFileExists(sidecarPath, constants.R_OK);

if (!exists) {
if (isSync) {
this.logger.debug(`Sidecar File '${sidecarPath}' was not found, removing sidecarPath for asset ${asset.id}`);
await this.assetRepository.save({ id: asset.id, sidecarPath: null });
return true;
}
if (exists) {
await this.assetRepository.save({ id: asset.id, sidecarPath });
return true;
}

if (!isSync) {
return false;
}

await this.assetRepository.save({ id: asset.id, sidecarPath });

this.logger.debug(`Sidecar File '${sidecarPath}' was not found, removing sidecarPath for asset ${asset.id}`);
await this.assetRepository.save({ id: asset.id, sidecarPath: null });
return true;
}
}

0 comments on commit 2ef4583

Please sign in to comment.