immich/server/src/services/timeline.service.spec.ts
Min Idzelis e7edbcdf04
feat(server): lighter buckets (#17831)
* feat(web): lighter timeline buckets

* GalleryViewer

* weird ssr

* Remove generics from AssetInteraction

* ensure keys on getAssetInfo, alt-text

* empty - trigger ci

* re-add alt-text

* test fix

* update tests

* tests

* missing import

* feat(server): lighter buckets

* fix: flappy e2e test

* lint

* revert settings

* unneeded cast

* fix after merge

* Adapt web client to consume new server response format

* test

* missing import

* lint

* Use nulls, make-sql

* openapi battle

* date->string

* tests

* tests

* lint/tests

* lint

* test

* push aggregation to query

* openapi

* stack as tuple

* openapi

* update references to description

* update alt text tests

* update sql

* update sql

* update timeline tests

* linting, fix expected response

* string tuple

* fix spec

* fix

* silly generator

* rename patch

* minimize sorting

* review

* lint

* lint

* sql

* test

* avoid abbreviations

* review comment - type safety in test

* merge conflicts

* lint

* lint/abbreviations

* remove unncessary code

* review comments

* sql

* re-add package-lock

* use booleans, fix visibility in openapi spec, less cursed controller

* update sql

* no need to use sql template

* array access actually doesn't seem to matter

* remove redundant code

* re-add sql decorator

* unused type

* remove null assertions

* bad merge

* Fix test

* shave

* extra clean shave

* use decorator for content type

* redundant types

* redundant comment

* update comment

* unnecessary res

---------

Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com>
Co-authored-by: Alex <alex.tran1502@gmail.com>
2025-05-19 16:40:48 -05:00

177 lines
5.9 KiB
TypeScript

import { BadRequestException } from '@nestjs/common';
import { AssetVisibility } from 'src/enum';
import { TimelineService } from 'src/services/timeline.service';
import { authStub } from 'test/fixtures/auth.stub';
import { newTestService, ServiceMocks } from 'test/utils';
describe(TimelineService.name, () => {
let sut: TimelineService;
let mocks: ServiceMocks;
beforeEach(() => {
({ sut, mocks } = newTestService(TimelineService));
});
describe('getTimeBuckets', () => {
it("should return buckets if userId and albumId aren't set", async () => {
mocks.asset.getTimeBuckets.mockResolvedValue([{ timeBucket: 'bucket', count: 1 }]);
await expect(sut.getTimeBuckets(authStub.admin, {})).resolves.toEqual(
expect.arrayContaining([{ timeBucket: 'bucket', count: 1 }]),
);
expect(mocks.asset.getTimeBuckets).toHaveBeenCalledWith({
userIds: [authStub.admin.user.id],
});
});
});
describe('getTimeBucket', () => {
it('should return the assets for a album time bucket if user has album.read', async () => {
mocks.access.album.checkOwnerAccess.mockResolvedValue(new Set(['album-id']));
const json = `[{ id: ['asset-id'] }]`;
mocks.asset.getTimeBucket.mockResolvedValue({ assets: json });
await expect(sut.getTimeBucket(authStub.admin, { timeBucket: 'bucket', albumId: 'album-id' })).resolves.toEqual(
json,
);
expect(mocks.access.album.checkOwnerAccess).toHaveBeenCalledWith(authStub.admin.user.id, new Set(['album-id']));
expect(mocks.asset.getTimeBucket).toHaveBeenCalledWith('bucket', {
timeBucket: 'bucket',
albumId: 'album-id',
});
});
it('should return the assets for a archive time bucket if user has archive.read', async () => {
const json = `[{ id: ['asset-id'] }]`;
mocks.asset.getTimeBucket.mockResolvedValue({ assets: json });
await expect(
sut.getTimeBucket(authStub.admin, {
timeBucket: 'bucket',
visibility: AssetVisibility.ARCHIVE,
userId: authStub.admin.user.id,
}),
).resolves.toEqual(json);
expect(mocks.asset.getTimeBucket).toHaveBeenCalledWith(
'bucket',
expect.objectContaining({
timeBucket: 'bucket',
visibility: AssetVisibility.ARCHIVE,
userIds: [authStub.admin.user.id],
}),
);
});
it('should include partner shared assets', async () => {
const json = `[{ id: ['asset-id'] }]`;
mocks.asset.getTimeBucket.mockResolvedValue({ assets: json });
mocks.partner.getAll.mockResolvedValue([]);
await expect(
sut.getTimeBucket(authStub.admin, {
timeBucket: 'bucket',
visibility: AssetVisibility.TIMELINE,
userId: authStub.admin.user.id,
withPartners: true,
}),
).resolves.toEqual(json);
expect(mocks.asset.getTimeBucket).toHaveBeenCalledWith('bucket', {
timeBucket: 'bucket',
visibility: AssetVisibility.TIMELINE,
withPartners: true,
userIds: [authStub.admin.user.id],
});
});
it('should check permissions to read tag', async () => {
const json = `[{ id: ['asset-id'] }]`;
mocks.asset.getTimeBucket.mockResolvedValue({ assets: json });
mocks.access.tag.checkOwnerAccess.mockResolvedValue(new Set(['tag-123']));
await expect(
sut.getTimeBucket(authStub.admin, {
timeBucket: 'bucket',
userId: authStub.admin.user.id,
tagId: 'tag-123',
}),
).resolves.toEqual(json);
expect(mocks.asset.getTimeBucket).toHaveBeenCalledWith('bucket', {
tagId: 'tag-123',
timeBucket: 'bucket',
userIds: [authStub.admin.user.id],
});
});
it('should return the assets for a library time bucket if user has library.read', async () => {
const json = `[{ id: ['asset-id'] }]`;
mocks.asset.getTimeBucket.mockResolvedValue({ assets: json });
await expect(
sut.getTimeBucket(authStub.admin, {
timeBucket: 'bucket',
userId: authStub.admin.user.id,
}),
).resolves.toEqual(json);
expect(mocks.asset.getTimeBucket).toHaveBeenCalledWith(
'bucket',
expect.objectContaining({
timeBucket: 'bucket',
userIds: [authStub.admin.user.id],
}),
);
});
it('should throw an error if withParners is true and visibility true or undefined', async () => {
await expect(
sut.getTimeBucket(authStub.admin, {
timeBucket: 'bucket',
visibility: AssetVisibility.ARCHIVE,
withPartners: true,
userId: authStub.admin.user.id,
}),
).rejects.toThrow(BadRequestException);
await expect(
sut.getTimeBucket(authStub.admin, {
timeBucket: 'bucket',
visibility: undefined,
withPartners: true,
userId: authStub.admin.user.id,
}),
).rejects.toThrow(BadRequestException);
});
it('should throw an error if withParners is true and isFavorite is either true or false', async () => {
await expect(
sut.getTimeBucket(authStub.admin, {
timeBucket: 'bucket',
isFavorite: true,
withPartners: true,
userId: authStub.admin.user.id,
}),
).rejects.toThrow(BadRequestException);
await expect(
sut.getTimeBucket(authStub.admin, {
timeBucket: 'bucket',
isFavorite: false,
withPartners: true,
userId: authStub.admin.user.id,
}),
).rejects.toThrow(BadRequestException);
});
it('should throw an error if withParners is true and isTrash is true', async () => {
await expect(
sut.getTimeBucket(authStub.admin, {
timeBucket: 'bucket',
isTrashed: true,
withPartners: true,
userId: authStub.admin.user.id,
}),
).rejects.toThrow(BadRequestException);
});
});
});