mirror of
https://github.com/immich-app/immich.git
synced 2026-03-26 11:50:53 +03:00
refactor: test mocks (#16008)
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
import { IJobRepository, JobName, JobStatus } from 'src/interfaces/job.interface';
|
||||
import { JobName, JobStatus } from 'src/interfaces/job.interface';
|
||||
import { TrashService } from 'src/services/trash.service';
|
||||
import { ITrashRepository } from 'src/types';
|
||||
import { authStub } from 'test/fixtures/auth.stub';
|
||||
import { IAccessRepositoryMock } from 'test/repositories/access.repository.mock';
|
||||
import { newTestService } from 'test/utils';
|
||||
import { Mocked } from 'vitest';
|
||||
import { newTestService, ServiceMocks } from 'test/utils';
|
||||
|
||||
async function* makeAssetIdStream(count: number): AsyncIterableIterator<{ id: string }> {
|
||||
for (let i = 0; i < count; i++) {
|
||||
@@ -16,17 +13,14 @@ async function* makeAssetIdStream(count: number): AsyncIterableIterator<{ id: st
|
||||
|
||||
describe(TrashService.name, () => {
|
||||
let sut: TrashService;
|
||||
|
||||
let accessMock: IAccessRepositoryMock;
|
||||
let jobMock: Mocked<IJobRepository>;
|
||||
let trashMock: Mocked<ITrashRepository>;
|
||||
let mocks: ServiceMocks;
|
||||
|
||||
it('should work', () => {
|
||||
expect(sut).toBeDefined();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
({ sut, accessMock, jobMock, trashMock } = newTestService(TrashService));
|
||||
({ sut, mocks } = newTestService(TrashService));
|
||||
});
|
||||
|
||||
describe('restoreAssets', () => {
|
||||
@@ -40,64 +34,64 @@ describe(TrashService.name, () => {
|
||||
|
||||
it('should handle an empty list', async () => {
|
||||
await expect(sut.restoreAssets(authStub.user1, { ids: [] })).resolves.toEqual({ count: 0 });
|
||||
expect(accessMock.asset.checkOwnerAccess).not.toHaveBeenCalled();
|
||||
expect(mocks.access.asset.checkOwnerAccess).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should restore a batch of assets', async () => {
|
||||
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset1', 'asset2']));
|
||||
mocks.access.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset1', 'asset2']));
|
||||
|
||||
await sut.restoreAssets(authStub.user1, { ids: ['asset1', 'asset2'] });
|
||||
|
||||
expect(trashMock.restoreAll).toHaveBeenCalledWith(['asset1', 'asset2']);
|
||||
expect(jobMock.queue.mock.calls).toEqual([]);
|
||||
expect(mocks.trash.restoreAll).toHaveBeenCalledWith(['asset1', 'asset2']);
|
||||
expect(mocks.job.queue.mock.calls).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('restore', () => {
|
||||
it('should handle an empty trash', async () => {
|
||||
trashMock.getDeletedIds.mockResolvedValue(makeAssetIdStream(0));
|
||||
trashMock.restore.mockResolvedValue(0);
|
||||
mocks.trash.getDeletedIds.mockResolvedValue(makeAssetIdStream(0));
|
||||
mocks.trash.restore.mockResolvedValue(0);
|
||||
await expect(sut.restore(authStub.user1)).resolves.toEqual({ count: 0 });
|
||||
expect(trashMock.restore).toHaveBeenCalledWith('user-id');
|
||||
expect(mocks.trash.restore).toHaveBeenCalledWith('user-id');
|
||||
});
|
||||
|
||||
it('should restore', async () => {
|
||||
trashMock.getDeletedIds.mockResolvedValue(makeAssetIdStream(1));
|
||||
trashMock.restore.mockResolvedValue(1);
|
||||
mocks.trash.getDeletedIds.mockResolvedValue(makeAssetIdStream(1));
|
||||
mocks.trash.restore.mockResolvedValue(1);
|
||||
await expect(sut.restore(authStub.user1)).resolves.toEqual({ count: 1 });
|
||||
expect(trashMock.restore).toHaveBeenCalledWith('user-id');
|
||||
expect(mocks.trash.restore).toHaveBeenCalledWith('user-id');
|
||||
});
|
||||
});
|
||||
|
||||
describe('empty', () => {
|
||||
it('should handle an empty trash', async () => {
|
||||
trashMock.getDeletedIds.mockResolvedValue(makeAssetIdStream(0));
|
||||
trashMock.empty.mockResolvedValue(0);
|
||||
mocks.trash.getDeletedIds.mockResolvedValue(makeAssetIdStream(0));
|
||||
mocks.trash.empty.mockResolvedValue(0);
|
||||
await expect(sut.empty(authStub.user1)).resolves.toEqual({ count: 0 });
|
||||
expect(jobMock.queue).not.toHaveBeenCalled();
|
||||
expect(mocks.job.queue).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should empty the trash', async () => {
|
||||
trashMock.getDeletedIds.mockResolvedValue(makeAssetIdStream(1));
|
||||
trashMock.empty.mockResolvedValue(1);
|
||||
mocks.trash.getDeletedIds.mockResolvedValue(makeAssetIdStream(1));
|
||||
mocks.trash.empty.mockResolvedValue(1);
|
||||
await expect(sut.empty(authStub.user1)).resolves.toEqual({ count: 1 });
|
||||
expect(trashMock.empty).toHaveBeenCalledWith('user-id');
|
||||
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.QUEUE_TRASH_EMPTY, data: {} });
|
||||
expect(mocks.trash.empty).toHaveBeenCalledWith('user-id');
|
||||
expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.QUEUE_TRASH_EMPTY, data: {} });
|
||||
});
|
||||
});
|
||||
|
||||
describe('onAssetsDelete', () => {
|
||||
it('should queue the empty trash job', async () => {
|
||||
await expect(sut.onAssetsDelete()).resolves.toBeUndefined();
|
||||
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.QUEUE_TRASH_EMPTY, data: {} });
|
||||
expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.QUEUE_TRASH_EMPTY, data: {} });
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleQueueEmptyTrash', () => {
|
||||
it('should queue asset delete jobs', async () => {
|
||||
trashMock.getDeletedIds.mockReturnValue(makeAssetIdStream(1));
|
||||
mocks.trash.getDeletedIds.mockReturnValue(makeAssetIdStream(1));
|
||||
await expect(sut.handleQueueEmptyTrash()).resolves.toEqual(JobStatus.SUCCESS);
|
||||
expect(jobMock.queueAll).toHaveBeenCalledWith([
|
||||
expect(mocks.job.queueAll).toHaveBeenCalledWith([
|
||||
{
|
||||
name: JobName.ASSET_DELETION,
|
||||
data: { id: 'asset-1', deleteOnDisk: true },
|
||||
|
||||
Reference in New Issue
Block a user