2023-09-27 22:46:46 +02:00
|
|
|
import { Colorspace, SystemConfigKey } from '@app/infra/entities';
|
2023-05-17 19:07:17 +02:00
|
|
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
|
|
|
import {
|
2023-09-18 23:22:44 +02:00
|
|
|
IAccessRepositoryMock,
|
2023-08-01 03:28:07 +02:00
|
|
|
assetStub,
|
2023-05-17 19:07:17 +02:00
|
|
|
authStub,
|
2023-07-03 00:46:20 +02:00
|
|
|
faceStub,
|
2023-09-18 23:22:44 +02:00
|
|
|
newAccessRepositoryMock,
|
2023-09-27 22:46:46 +02:00
|
|
|
newAssetRepositoryMock,
|
2023-05-17 19:07:17 +02:00
|
|
|
newJobRepositoryMock,
|
2023-09-27 22:46:46 +02:00
|
|
|
newMachineLearningRepositoryMock,
|
|
|
|
newMediaRepositoryMock,
|
2023-10-11 04:14:44 +02:00
|
|
|
newMoveRepositoryMock,
|
2023-05-17 19:07:17 +02:00
|
|
|
newPersonRepositoryMock,
|
2023-09-27 22:46:46 +02:00
|
|
|
newSearchRepositoryMock,
|
2023-05-17 19:07:17 +02:00
|
|
|
newStorageRepositoryMock,
|
2023-09-18 06:05:35 +02:00
|
|
|
newSystemConfigRepositoryMock,
|
2023-05-17 19:07:17 +02:00
|
|
|
personStub,
|
2023-06-08 17:01:07 +02:00
|
|
|
} from '@test';
|
2023-10-09 16:25:03 +02:00
|
|
|
import { BulkIdErrorReason } from '../asset';
|
|
|
|
import { JobName } from '../job';
|
|
|
|
import {
|
|
|
|
IAssetRepository,
|
|
|
|
IJobRepository,
|
|
|
|
IMachineLearningRepository,
|
|
|
|
IMediaRepository,
|
2023-10-11 04:14:44 +02:00
|
|
|
IMoveRepository,
|
2023-10-09 16:25:03 +02:00
|
|
|
IPersonRepository,
|
|
|
|
ISearchRepository,
|
|
|
|
IStorageRepository,
|
|
|
|
ISystemConfigRepository,
|
|
|
|
WithoutProperty,
|
|
|
|
} from '../repositories';
|
2023-07-01 03:52:40 +02:00
|
|
|
import { PersonResponseDto } from './person.dto';
|
2023-05-17 19:07:17 +02:00
|
|
|
import { PersonService } from './person.service';
|
|
|
|
|
|
|
|
const responseDto: PersonResponseDto = {
|
|
|
|
id: 'person-1',
|
|
|
|
name: 'Person 1',
|
2023-08-18 22:10:29 +02:00
|
|
|
birthDate: null,
|
2023-07-10 19:56:45 +02:00
|
|
|
thumbnailPath: '/path/to/thumbnail.jpg',
|
2023-07-18 20:09:43 +02:00
|
|
|
isHidden: false,
|
2023-05-17 19:07:17 +02:00
|
|
|
};
|
|
|
|
|
2023-10-24 17:53:49 +02:00
|
|
|
const statistics = { assets: 3 };
|
|
|
|
|
2023-09-27 22:46:46 +02:00
|
|
|
const croppedFace = Buffer.from('Cropped Face');
|
|
|
|
|
|
|
|
const detectFaceMock = {
|
|
|
|
assetId: 'asset-1',
|
|
|
|
personId: 'person-1',
|
|
|
|
boundingBox: {
|
|
|
|
x1: 100,
|
|
|
|
y1: 100,
|
|
|
|
x2: 200,
|
|
|
|
y2: 200,
|
|
|
|
},
|
|
|
|
imageHeight: 500,
|
|
|
|
imageWidth: 400,
|
|
|
|
embedding: [1, 2, 3, 4],
|
|
|
|
score: 0.2,
|
|
|
|
};
|
|
|
|
|
|
|
|
const faceSearch = {
|
|
|
|
noMatch: {
|
|
|
|
total: 0,
|
|
|
|
count: 0,
|
|
|
|
page: 1,
|
|
|
|
items: [],
|
|
|
|
distances: [],
|
|
|
|
facets: [],
|
|
|
|
},
|
|
|
|
oneMatch: {
|
|
|
|
total: 1,
|
|
|
|
count: 1,
|
|
|
|
page: 1,
|
|
|
|
items: [faceStub.face1],
|
|
|
|
distances: [0.1],
|
|
|
|
facets: [],
|
|
|
|
},
|
|
|
|
oneRemoteMatch: {
|
|
|
|
total: 1,
|
|
|
|
count: 1,
|
|
|
|
page: 1,
|
|
|
|
items: [faceStub.face1],
|
|
|
|
distances: [0.8],
|
|
|
|
facets: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
describe(PersonService.name, () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
let accessMock: IAccessRepositoryMock;
|
2023-09-27 22:46:46 +02:00
|
|
|
let assetMock: jest.Mocked<IAssetRepository>;
|
2023-09-18 06:05:35 +02:00
|
|
|
let configMock: jest.Mocked<ISystemConfigRepository>;
|
2023-05-17 19:07:17 +02:00
|
|
|
let jobMock: jest.Mocked<IJobRepository>;
|
2023-09-27 22:46:46 +02:00
|
|
|
let machineLearningMock: jest.Mocked<IMachineLearningRepository>;
|
|
|
|
let mediaMock: jest.Mocked<IMediaRepository>;
|
2023-10-11 04:14:44 +02:00
|
|
|
let moveMock: jest.Mocked<IMoveRepository>;
|
2023-09-18 23:22:44 +02:00
|
|
|
let personMock: jest.Mocked<IPersonRepository>;
|
2023-09-27 22:46:46 +02:00
|
|
|
let searchMock: jest.Mocked<ISearchRepository>;
|
2023-09-18 23:22:44 +02:00
|
|
|
let storageMock: jest.Mocked<IStorageRepository>;
|
|
|
|
let sut: PersonService;
|
2023-05-17 19:07:17 +02:00
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock = newAccessRepositoryMock();
|
2023-09-27 22:46:46 +02:00
|
|
|
assetMock = newAssetRepositoryMock();
|
2023-09-18 06:05:35 +02:00
|
|
|
configMock = newSystemConfigRepositoryMock();
|
2023-05-17 19:07:17 +02:00
|
|
|
jobMock = newJobRepositoryMock();
|
2023-09-27 22:46:46 +02:00
|
|
|
machineLearningMock = newMachineLearningRepositoryMock();
|
2023-10-11 04:14:44 +02:00
|
|
|
moveMock = newMoveRepositoryMock();
|
2023-09-27 22:46:46 +02:00
|
|
|
mediaMock = newMediaRepositoryMock();
|
2023-09-25 17:07:21 +02:00
|
|
|
personMock = newPersonRepositoryMock();
|
2023-09-27 22:46:46 +02:00
|
|
|
searchMock = newSearchRepositoryMock();
|
2023-09-25 17:07:21 +02:00
|
|
|
storageMock = newStorageRepositoryMock();
|
2023-09-27 22:46:46 +02:00
|
|
|
sut = new PersonService(
|
|
|
|
accessMock,
|
|
|
|
assetMock,
|
|
|
|
machineLearningMock,
|
2023-10-11 04:14:44 +02:00
|
|
|
moveMock,
|
2023-09-27 22:46:46 +02:00
|
|
|
mediaMock,
|
|
|
|
personMock,
|
|
|
|
searchMock,
|
|
|
|
configMock,
|
|
|
|
storageMock,
|
|
|
|
jobMock,
|
|
|
|
);
|
|
|
|
|
|
|
|
mediaMock.crop.mockResolvedValue(croppedFace);
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be defined', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getAll', () => {
|
|
|
|
it('should get all people with thumbnails', async () => {
|
2023-09-08 08:49:43 +02:00
|
|
|
personMock.getAllForUser.mockResolvedValue([personStub.withName, personStub.noThumbnail]);
|
2023-07-18 20:09:43 +02:00
|
|
|
await expect(sut.getAll(authStub.admin, { withHidden: undefined })).resolves.toEqual({
|
|
|
|
total: 1,
|
|
|
|
visible: 1,
|
|
|
|
people: [responseDto],
|
|
|
|
});
|
2023-09-08 08:49:43 +02:00
|
|
|
expect(personMock.getAllForUser).toHaveBeenCalledWith(authStub.admin.id, {
|
|
|
|
minimumFaceCount: 1,
|
|
|
|
withHidden: false,
|
|
|
|
});
|
2023-07-18 20:09:43 +02:00
|
|
|
});
|
|
|
|
it('should get all visible people with thumbnails', async () => {
|
2023-09-08 08:49:43 +02:00
|
|
|
personMock.getAllForUser.mockResolvedValue([personStub.withName, personStub.hidden]);
|
2023-07-18 20:09:43 +02:00
|
|
|
await expect(sut.getAll(authStub.admin, { withHidden: false })).resolves.toEqual({
|
|
|
|
total: 2,
|
|
|
|
visible: 1,
|
|
|
|
people: [responseDto],
|
|
|
|
});
|
2023-09-08 08:49:43 +02:00
|
|
|
expect(personMock.getAllForUser).toHaveBeenCalledWith(authStub.admin.id, {
|
|
|
|
minimumFaceCount: 1,
|
|
|
|
withHidden: false,
|
|
|
|
});
|
2023-07-18 20:09:43 +02:00
|
|
|
});
|
|
|
|
it('should get all hidden and visible people with thumbnails', async () => {
|
2023-09-08 08:49:43 +02:00
|
|
|
personMock.getAllForUser.mockResolvedValue([personStub.withName, personStub.hidden]);
|
2023-07-18 20:09:43 +02:00
|
|
|
await expect(sut.getAll(authStub.admin, { withHidden: true })).resolves.toEqual({
|
|
|
|
total: 2,
|
|
|
|
visible: 1,
|
|
|
|
people: [
|
|
|
|
responseDto,
|
|
|
|
{
|
|
|
|
id: 'person-1',
|
|
|
|
name: '',
|
2023-08-18 22:10:29 +02:00
|
|
|
birthDate: null,
|
2023-07-18 20:09:43 +02:00
|
|
|
thumbnailPath: '/path/to/thumbnail.jpg',
|
|
|
|
isHidden: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2023-09-08 08:49:43 +02:00
|
|
|
expect(personMock.getAllForUser).toHaveBeenCalledWith(authStub.admin.id, {
|
|
|
|
minimumFaceCount: 1,
|
|
|
|
withHidden: true,
|
|
|
|
});
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getById', () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
it('should require person.read permission', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.withName);
|
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
await expect(sut.getById(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
|
|
|
});
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
it('should throw a bad request when person is not found', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-05-17 19:07:17 +02:00
|
|
|
await expect(sut.getById(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException);
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should get a person by id', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.withName);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-05-17 19:07:17 +02:00
|
|
|
await expect(sut.getById(authStub.admin, 'person-1')).resolves.toEqual(responseDto);
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(personMock.getById).toHaveBeenCalledWith('person-1');
|
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getThumbnail', () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
it('should require person.read permission', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noName);
|
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
await expect(sut.getThumbnail(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(storageMock.createReadStream).not.toHaveBeenCalled();
|
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
|
|
|
});
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
it('should throw an error when personId is invalid', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-05-17 19:07:17 +02:00
|
|
|
await expect(sut.getThumbnail(authStub.admin, 'person-1')).rejects.toBeInstanceOf(NotFoundException);
|
|
|
|
expect(storageMock.createReadStream).not.toHaveBeenCalled();
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when person has no thumbnail', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noThumbnail);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-05-17 19:07:17 +02:00
|
|
|
await expect(sut.getThumbnail(authStub.admin, 'person-1')).rejects.toBeInstanceOf(NotFoundException);
|
|
|
|
expect(storageMock.createReadStream).not.toHaveBeenCalled();
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should serve the thumbnail', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noName);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-05-17 19:07:17 +02:00
|
|
|
await sut.getThumbnail(authStub.admin, 'person-1');
|
2023-07-10 19:56:45 +02:00
|
|
|
expect(storageMock.createReadStream).toHaveBeenCalledWith('/path/to/thumbnail.jpg', 'image/jpeg');
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getAssets', () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
it('should require person.read permission', async () => {
|
|
|
|
personMock.getAssets.mockResolvedValue([assetStub.image, assetStub.video]);
|
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
await expect(sut.getAssets(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(personMock.getAssets).not.toHaveBeenCalled();
|
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
|
|
|
});
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
it("should return a person's assets", async () => {
|
2023-08-01 03:28:07 +02:00
|
|
|
personMock.getAssets.mockResolvedValue([assetStub.image, assetStub.video]);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-05-17 19:07:17 +02:00
|
|
|
await sut.getAssets(authStub.admin, 'person-1');
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(personMock.getAssets).toHaveBeenCalledWith('person-1');
|
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('update', () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
it('should require person.write permission', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noName);
|
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { name: 'Person 1' })).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
expect(personMock.update).not.toHaveBeenCalled();
|
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
|
|
|
});
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
it('should throw an error when personId is invalid', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-05-17 19:07:17 +02:00
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { name: 'Person 1' })).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
expect(personMock.update).not.toHaveBeenCalled();
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should update a person's name", async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noName);
|
|
|
|
personMock.update.mockResolvedValue(personStub.withName);
|
2023-08-01 03:28:07 +02:00
|
|
|
personMock.getAssets.mockResolvedValue([assetStub.image]);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-05-17 19:07:17 +02:00
|
|
|
|
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { name: 'Person 1' })).resolves.toEqual(responseDto);
|
|
|
|
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(personMock.getById).toHaveBeenCalledWith('person-1');
|
2023-05-17 19:07:17 +02:00
|
|
|
expect(personMock.update).toHaveBeenCalledWith({ id: 'person-1', name: 'Person 1' });
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.SEARCH_INDEX_ASSET,
|
2023-08-01 03:28:07 +02:00
|
|
|
data: { ids: [assetStub.image.id] },
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
2023-07-03 00:46:20 +02:00
|
|
|
|
2023-08-18 22:10:29 +02:00
|
|
|
it("should update a person's date of birth", async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noBirthDate);
|
|
|
|
personMock.update.mockResolvedValue(personStub.withBirthDate);
|
|
|
|
personMock.getAssets.mockResolvedValue([assetStub.image]);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-08-18 22:10:29 +02:00
|
|
|
|
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { birthDate: new Date('1976-06-30') })).resolves.toEqual({
|
|
|
|
id: 'person-1',
|
|
|
|
name: 'Person 1',
|
|
|
|
birthDate: new Date('1976-06-30'),
|
|
|
|
thumbnailPath: '/path/to/thumbnail.jpg',
|
|
|
|
isHidden: false,
|
|
|
|
});
|
|
|
|
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(personMock.getById).toHaveBeenCalledWith('person-1');
|
2023-08-18 22:10:29 +02:00
|
|
|
expect(personMock.update).toHaveBeenCalledWith({ id: 'person-1', birthDate: new Date('1976-06-30') });
|
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-08-18 22:10:29 +02:00
|
|
|
});
|
|
|
|
|
2023-07-18 20:09:43 +02:00
|
|
|
it('should update a person visibility', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.hidden);
|
|
|
|
personMock.update.mockResolvedValue(personStub.withName);
|
2023-08-01 03:28:07 +02:00
|
|
|
personMock.getAssets.mockResolvedValue([assetStub.image]);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-18 20:09:43 +02:00
|
|
|
|
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { isHidden: false })).resolves.toEqual(responseDto);
|
|
|
|
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(personMock.getById).toHaveBeenCalledWith('person-1');
|
2023-07-18 20:09:43 +02:00
|
|
|
expect(personMock.update).toHaveBeenCalledWith({ id: 'person-1', isHidden: false });
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.SEARCH_INDEX_ASSET,
|
2023-08-01 03:28:07 +02:00
|
|
|
data: { ids: [assetStub.image.id] },
|
2023-07-18 20:09:43 +02:00
|
|
|
});
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-07-18 20:09:43 +02:00
|
|
|
});
|
|
|
|
|
2023-07-03 00:46:20 +02:00
|
|
|
it("should update a person's thumbnailPath", async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.withName);
|
2023-09-26 09:03:22 +02:00
|
|
|
personMock.update.mockResolvedValue(personStub.withName);
|
2023-09-27 22:46:46 +02:00
|
|
|
personMock.getFacesByIds.mockResolvedValue([faceStub.face1]);
|
2023-09-26 09:03:22 +02:00
|
|
|
accessMock.asset.hasOwnerAccess.mockResolvedValue(true);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-03 00:46:20 +02:00
|
|
|
|
|
|
|
await expect(
|
|
|
|
sut.update(authStub.admin, 'person-1', { featureFaceAssetId: faceStub.face1.assetId }),
|
|
|
|
).resolves.toEqual(responseDto);
|
|
|
|
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(personMock.getById).toHaveBeenCalledWith('person-1');
|
2023-09-26 09:03:22 +02:00
|
|
|
expect(personMock.update).toHaveBeenCalledWith({ id: 'person-1', faceAssetId: faceStub.face1.assetId });
|
2023-09-27 22:46:46 +02:00
|
|
|
expect(personMock.getFacesByIds).toHaveBeenCalledWith([
|
|
|
|
{
|
|
|
|
assetId: faceStub.face1.assetId,
|
|
|
|
personId: 'person-1',
|
|
|
|
},
|
|
|
|
]);
|
2023-09-26 09:03:22 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.GENERATE_PERSON_THUMBNAIL, data: { id: 'person-1' } });
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-07-03 00:46:20 +02:00
|
|
|
});
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
it('should throw an error when the face feature assetId is invalid', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.withName);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
await expect(sut.update(authStub.admin, 'person-1', { featureFaceAssetId: '-1' })).rejects.toThrow(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
expect(personMock.update).not.toHaveBeenCalled();
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-09-11 17:56:38 +02:00
|
|
|
});
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
|
2023-07-23 05:00:43 +02:00
|
|
|
describe('updateAll', () => {
|
|
|
|
it('should throw an error when personId is invalid', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
|
2023-07-23 05:00:43 +02:00
|
|
|
await expect(
|
|
|
|
sut.updatePeople(authStub.admin, { people: [{ id: 'person-1', name: 'Person 1' }] }),
|
|
|
|
).resolves.toEqual([{ error: BulkIdErrorReason.UNKNOWN, id: 'person-1', success: false }]);
|
|
|
|
expect(personMock.update).not.toHaveBeenCalled();
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-07-23 05:00:43 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-17 19:07:17 +02:00
|
|
|
describe('handlePersonCleanup', () => {
|
|
|
|
it('should delete people without faces', async () => {
|
|
|
|
personMock.getAllWithoutFaces.mockResolvedValue([personStub.noName]);
|
|
|
|
|
|
|
|
await sut.handlePersonCleanup();
|
|
|
|
|
2023-10-03 03:15:11 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.PERSON_DELETE, data: { id: personStub.noName.id } });
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|
|
|
|
});
|
2023-07-11 23:52:41 +02:00
|
|
|
|
2023-09-27 22:46:46 +02:00
|
|
|
describe('handleQueueRecognizeFaces', () => {
|
|
|
|
it('should return if machine learning is disabled', async () => {
|
|
|
|
configMock.load.mockResolvedValue([{ key: SystemConfigKey.MACHINE_LEARNING_ENABLED, value: false }]);
|
|
|
|
|
|
|
|
await expect(sut.handleQueueRecognizeFaces({})).resolves.toBe(true);
|
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
|
|
|
expect(configMock.load).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should queue missing assets', async () => {
|
|
|
|
assetMock.getWithout.mockResolvedValue({
|
|
|
|
items: [assetStub.image],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
|
|
|
await sut.handleQueueRecognizeFaces({});
|
|
|
|
|
|
|
|
expect(assetMock.getWithout).toHaveBeenCalledWith({ skip: 0, take: 1000 }, WithoutProperty.FACES);
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.RECOGNIZE_FACES,
|
|
|
|
data: { id: assetStub.image.id },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should queue all assets', async () => {
|
|
|
|
assetMock.getAll.mockResolvedValue({
|
|
|
|
items: [assetStub.image],
|
|
|
|
hasNextPage: false,
|
|
|
|
});
|
2023-10-03 03:15:11 +02:00
|
|
|
personMock.getAll.mockResolvedValue([personStub.withName]);
|
2023-09-27 22:46:46 +02:00
|
|
|
searchMock.deleteAllFaces.mockResolvedValue(100);
|
|
|
|
|
|
|
|
await sut.handleQueueRecognizeFaces({ force: true });
|
|
|
|
|
|
|
|
expect(assetMock.getAll).toHaveBeenCalled();
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.RECOGNIZE_FACES,
|
|
|
|
data: { id: assetStub.image.id },
|
|
|
|
});
|
2023-10-03 03:15:11 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.PERSON_DELETE,
|
|
|
|
data: { id: personStub.withName.id },
|
|
|
|
});
|
2023-09-27 22:46:46 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('handleRecognizeFaces', () => {
|
|
|
|
it('should return if machine learning is disabled', async () => {
|
|
|
|
configMock.load.mockResolvedValue([{ key: SystemConfigKey.MACHINE_LEARNING_ENABLED, value: false }]);
|
|
|
|
|
|
|
|
await expect(sut.handleRecognizeFaces({ id: 'foo' })).resolves.toBe(true);
|
|
|
|
expect(assetMock.getByIds).not.toHaveBeenCalled();
|
|
|
|
expect(configMock.load).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip when no resize path', async () => {
|
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.noResizePath]);
|
|
|
|
await sut.handleRecognizeFaces({ id: assetStub.noResizePath.id });
|
|
|
|
expect(machineLearningMock.detectFaces).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle no results', async () => {
|
|
|
|
machineLearningMock.detectFaces.mockResolvedValue([]);
|
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.image]);
|
|
|
|
await sut.handleRecognizeFaces({ id: assetStub.image.id });
|
|
|
|
expect(machineLearningMock.detectFaces).toHaveBeenCalledWith(
|
|
|
|
'http://immich-machine-learning:3003',
|
|
|
|
{
|
|
|
|
imagePath: assetStub.image.resizePath,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
enabled: true,
|
|
|
|
maxDistance: 0.6,
|
|
|
|
minScore: 0.7,
|
|
|
|
minFaces: 1,
|
|
|
|
modelName: 'buffalo_l',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
expect(personMock.createFace).not.toHaveBeenCalled();
|
|
|
|
expect(jobMock.queue).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should match existing people', async () => {
|
|
|
|
machineLearningMock.detectFaces.mockResolvedValue([detectFaceMock]);
|
|
|
|
searchMock.searchFaces.mockResolvedValue(faceSearch.oneMatch);
|
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.image]);
|
|
|
|
await sut.handleRecognizeFaces({ id: assetStub.image.id });
|
|
|
|
|
|
|
|
expect(personMock.createFace).toHaveBeenCalledWith({
|
|
|
|
personId: 'person-1',
|
|
|
|
assetId: 'asset-id',
|
|
|
|
embedding: [1, 2, 3, 4],
|
|
|
|
boundingBoxX1: 100,
|
|
|
|
boundingBoxY1: 100,
|
|
|
|
boundingBoxX2: 200,
|
|
|
|
boundingBoxY2: 200,
|
|
|
|
imageHeight: 500,
|
|
|
|
imageWidth: 400,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a new person', async () => {
|
|
|
|
machineLearningMock.detectFaces.mockResolvedValue([detectFaceMock]);
|
|
|
|
searchMock.searchFaces.mockResolvedValue(faceSearch.oneRemoteMatch);
|
|
|
|
personMock.create.mockResolvedValue(personStub.noName);
|
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.image]);
|
|
|
|
|
|
|
|
await sut.handleRecognizeFaces({ id: assetStub.image.id });
|
|
|
|
|
|
|
|
expect(personMock.create).toHaveBeenCalledWith({ ownerId: assetStub.image.ownerId });
|
|
|
|
expect(personMock.createFace).toHaveBeenCalledWith({
|
|
|
|
personId: 'person-1',
|
|
|
|
assetId: 'asset-id',
|
|
|
|
embedding: [1, 2, 3, 4],
|
|
|
|
boundingBoxX1: 100,
|
|
|
|
boundingBoxY1: 100,
|
|
|
|
boundingBoxX2: 200,
|
|
|
|
boundingBoxY2: 200,
|
|
|
|
imageHeight: 500,
|
|
|
|
imageWidth: 400,
|
|
|
|
});
|
|
|
|
expect(jobMock.queue.mock.calls).toEqual([
|
|
|
|
[{ name: JobName.SEARCH_INDEX_FACE, data: { personId: 'person-1', assetId: 'asset-id' } }],
|
|
|
|
[{ name: JobName.GENERATE_PERSON_THUMBNAIL, data: { id: 'person-1' } }],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('handleGeneratePersonThumbnail', () => {
|
|
|
|
it('should return if machine learning is disabled', async () => {
|
|
|
|
configMock.load.mockResolvedValue([{ key: SystemConfigKey.MACHINE_LEARNING_ENABLED, value: false }]);
|
|
|
|
|
|
|
|
await expect(sut.handleGeneratePersonThumbnail({ id: 'person-1' })).resolves.toBe(true);
|
|
|
|
expect(assetMock.getByIds).not.toHaveBeenCalled();
|
|
|
|
expect(configMock.load).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip a person not found', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
expect(mediaMock.crop).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip a person without a face asset id', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.noThumbnail);
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
expect(mediaMock.crop).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip an person with a face asset id not found', async () => {
|
|
|
|
personMock.getById.mockResolvedValue({ ...personStub.primaryPerson, faceAssetId: faceStub.middle.assetId });
|
|
|
|
personMock.getFacesByIds.mockResolvedValue([faceStub.face1]);
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
expect(mediaMock.crop).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip a person with a face asset id without a thumbnail', async () => {
|
|
|
|
personMock.getById.mockResolvedValue({ ...personStub.primaryPerson, faceAssetId: faceStub.middle.assetId });
|
|
|
|
personMock.getFacesByIds.mockResolvedValue([faceStub.face1]);
|
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.noResizePath]);
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
expect(mediaMock.crop).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a thumbnail', async () => {
|
|
|
|
personMock.getById.mockResolvedValue({ ...personStub.primaryPerson, faceAssetId: faceStub.middle.assetId });
|
|
|
|
personMock.getFacesByIds.mockResolvedValue([faceStub.middle]);
|
2023-10-11 04:14:44 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.primaryImage]);
|
2023-09-27 22:46:46 +02:00
|
|
|
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
|
|
|
|
expect(assetMock.getByIds).toHaveBeenCalledWith([faceStub.middle.assetId]);
|
2023-10-11 04:14:44 +02:00
|
|
|
expect(storageMock.mkdirSync).toHaveBeenCalledWith('upload/thumbs/admin_id/pe/rs');
|
|
|
|
expect(mediaMock.crop).toHaveBeenCalledWith('/uploads/admin-id/thumbs/path.jpg', {
|
2023-09-27 22:46:46 +02:00
|
|
|
left: 95,
|
|
|
|
top: 95,
|
|
|
|
width: 110,
|
|
|
|
height: 110,
|
|
|
|
});
|
2023-10-11 04:14:44 +02:00
|
|
|
expect(mediaMock.resize).toHaveBeenCalledWith(croppedFace, 'upload/thumbs/admin_id/pe/rs/person-1.jpeg', {
|
2023-09-27 22:46:46 +02:00
|
|
|
format: 'jpeg',
|
|
|
|
size: 250,
|
|
|
|
quality: 80,
|
|
|
|
colorspace: Colorspace.P3,
|
|
|
|
});
|
|
|
|
expect(personMock.update).toHaveBeenCalledWith({
|
|
|
|
id: 'person-1',
|
2023-10-11 04:14:44 +02:00
|
|
|
thumbnailPath: 'upload/thumbs/admin_id/pe/rs/person-1.jpeg',
|
2023-09-27 22:46:46 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a thumbnail without going negative', async () => {
|
|
|
|
personMock.getById.mockResolvedValue({ ...personStub.primaryPerson, faceAssetId: faceStub.start.assetId });
|
|
|
|
personMock.getFacesByIds.mockResolvedValue([faceStub.start]);
|
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.image]);
|
|
|
|
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
|
|
|
|
expect(mediaMock.crop).toHaveBeenCalledWith('/uploads/user-id/thumbs/path.jpg', {
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
width: 510,
|
|
|
|
height: 510,
|
|
|
|
});
|
2023-10-11 04:14:44 +02:00
|
|
|
expect(mediaMock.resize).toHaveBeenCalledWith(croppedFace, 'upload/thumbs/admin_id/pe/rs/person-1.jpeg', {
|
2023-09-27 22:46:46 +02:00
|
|
|
format: 'jpeg',
|
|
|
|
size: 250,
|
|
|
|
quality: 80,
|
|
|
|
colorspace: Colorspace.P3,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a thumbnail without overflowing', async () => {
|
|
|
|
personMock.getById.mockResolvedValue({ ...personStub.primaryPerson, faceAssetId: faceStub.end.assetId });
|
|
|
|
personMock.getFacesByIds.mockResolvedValue([faceStub.end]);
|
2023-10-11 04:14:44 +02:00
|
|
|
assetMock.getByIds.mockResolvedValue([assetStub.primaryImage]);
|
2023-09-27 22:46:46 +02:00
|
|
|
|
|
|
|
await sut.handleGeneratePersonThumbnail({ id: 'person-1' });
|
|
|
|
|
2023-10-11 04:14:44 +02:00
|
|
|
expect(mediaMock.crop).toHaveBeenCalledWith('/uploads/admin-id/thumbs/path.jpg', {
|
2023-09-27 22:46:46 +02:00
|
|
|
left: 297,
|
|
|
|
top: 297,
|
|
|
|
width: 202,
|
|
|
|
height: 202,
|
|
|
|
});
|
2023-10-11 04:14:44 +02:00
|
|
|
expect(mediaMock.resize).toHaveBeenCalledWith(croppedFace, 'upload/thumbs/admin_id/pe/rs/person-1.jpeg', {
|
2023-09-27 22:46:46 +02:00
|
|
|
format: 'jpeg',
|
|
|
|
size: 250,
|
|
|
|
quality: 80,
|
|
|
|
colorspace: Colorspace.P3,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
describe('mergePerson', () => {
|
2023-09-18 23:22:44 +02:00
|
|
|
it('should require person.write and person.merge permission', async () => {
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.primaryPerson);
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.mergePerson);
|
|
|
|
personMock.prepareReassignFaces.mockResolvedValue([]);
|
|
|
|
personMock.delete.mockResolvedValue(personStub.mergePerson);
|
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(personMock.prepareReassignFaces).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
expect(personMock.delete).not.toHaveBeenCalled();
|
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
|
|
|
});
|
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
it('should merge two people', async () => {
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.primaryPerson);
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.mergePerson);
|
|
|
|
personMock.prepareReassignFaces.mockResolvedValue([]);
|
|
|
|
personMock.delete.mockResolvedValue(personStub.mergePerson);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-11 23:52:41 +02:00
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).resolves.toEqual([
|
|
|
|
{ id: 'person-2', success: true },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(personMock.prepareReassignFaces).toHaveBeenCalledWith({
|
|
|
|
newPersonId: personStub.primaryPerson.id,
|
|
|
|
oldPersonId: personStub.mergePerson.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(personMock.reassignFaces).toHaveBeenCalledWith({
|
|
|
|
newPersonId: personStub.primaryPerson.id,
|
|
|
|
oldPersonId: personStub.mergePerson.id,
|
|
|
|
});
|
|
|
|
|
2023-10-03 03:15:11 +02:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.PERSON_DELETE,
|
|
|
|
data: { id: personStub.mergePerson.id },
|
|
|
|
});
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-07-11 23:52:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete conflicting faces before merging', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.primaryPerson);
|
|
|
|
personMock.getById.mockResolvedValue(personStub.mergePerson);
|
2023-08-01 03:28:07 +02:00
|
|
|
personMock.prepareReassignFaces.mockResolvedValue([assetStub.image.id]);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-11 23:52:41 +02:00
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).resolves.toEqual([
|
|
|
|
{ id: 'person-2', success: true },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(personMock.prepareReassignFaces).toHaveBeenCalledWith({
|
|
|
|
newPersonId: personStub.primaryPerson.id,
|
|
|
|
oldPersonId: personStub.mergePerson.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({
|
|
|
|
name: JobName.SEARCH_REMOVE_FACE,
|
2023-08-01 03:28:07 +02:00
|
|
|
data: { assetId: assetStub.image.id, personId: personStub.mergePerson.id },
|
2023-07-11 23:52:41 +02:00
|
|
|
});
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-07-11 23:52:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when the primary person is not found', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(null);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-11 23:52:41 +02:00
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(personMock.delete).not.toHaveBeenCalled();
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-07-11 23:52:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle invalid merge ids', async () => {
|
|
|
|
personMock.getById.mockResolvedValueOnce(personStub.primaryPerson);
|
|
|
|
personMock.getById.mockResolvedValueOnce(null);
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-11 23:52:41 +02:00
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).resolves.toEqual([
|
|
|
|
{ id: 'person-2', success: false, error: BulkIdErrorReason.NOT_FOUND },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(personMock.prepareReassignFaces).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.reassignFaces).not.toHaveBeenCalled();
|
|
|
|
expect(personMock.delete).not.toHaveBeenCalled();
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-07-11 23:52:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle an error reassigning faces', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.primaryPerson);
|
|
|
|
personMock.getById.mockResolvedValue(personStub.mergePerson);
|
2023-08-01 03:28:07 +02:00
|
|
|
personMock.prepareReassignFaces.mockResolvedValue([assetStub.image.id]);
|
2023-07-11 23:52:41 +02:00
|
|
|
personMock.reassignFaces.mockRejectedValue(new Error('update failed'));
|
2023-09-18 23:22:44 +02:00
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
2023-07-11 23:52:41 +02:00
|
|
|
|
|
|
|
await expect(sut.mergePerson(authStub.admin, 'person-1', { ids: ['person-2'] })).resolves.toEqual([
|
|
|
|
{ id: 'person-2', success: false, error: BulkIdErrorReason.UNKNOWN },
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(personMock.delete).not.toHaveBeenCalled();
|
2023-09-18 23:22:44 +02:00
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
2023-07-11 23:52:41 +02:00
|
|
|
});
|
|
|
|
});
|
2023-10-24 17:53:49 +02:00
|
|
|
|
|
|
|
describe('getStatistics', () => {
|
|
|
|
it('should get correct number of person', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.primaryPerson);
|
|
|
|
personMock.getStatistics.mockResolvedValue(statistics);
|
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(true);
|
|
|
|
await expect(sut.getStatistics(authStub.admin, 'person-1')).resolves.toEqual({ assets: 3 });
|
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should require person.read permission', async () => {
|
|
|
|
personMock.getById.mockResolvedValue(personStub.primaryPerson);
|
|
|
|
accessMock.person.hasOwnerAccess.mockResolvedValue(false);
|
|
|
|
await expect(sut.getStatistics(authStub.admin, 'person-1')).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(accessMock.person.hasOwnerAccess).toHaveBeenCalledWith(authStub.admin.id, 'person-1');
|
|
|
|
});
|
|
|
|
});
|
2023-05-17 19:07:17 +02:00
|
|
|
});
|