Files
Immich/server/test/medium/specs/repositories/asset-job.repository.spec.ts
T
renovate[bot]andCodex 7fb7699c06 chore(deps): update dependency eslint-plugin-unicorn to v78 (#32588)
Co-authored-by: Jason Rasmussen <jason@rasm.me>
Co-authored-by: Daniel Dietzler <mail@ddietzler.dev>
(cherry picked from commit 72e2bc8b0bc4a7e0a84ac16c0bde7f5e538729fb)
2026-10-28 20:41:28 +03:00

145 lines
5.9 KiB
TypeScript

import { Kysely } from 'kysely';
import { AssetFileType } from 'src/enum.js';
import { AssetJobRepository } from 'src/repositories/asset-job.repository.js';
import { LoggingRepository } from 'src/repositories/logging.repository.js';
import { DB } from 'src/schema/index.js';
import { BaseService } from 'src/services/base.service.js';
import { newMediumService } from 'test/medium.factory.js';
import { getKyselyDB } from 'test/utils.js';
const consume = async <T>(generator: AsyncIterableIterator<T>) => {
const values: T[] = await Array.fromAsync(generator);
return values;
};
let defaultDatabase: Kysely<DB>;
// eslint-disable-next-line unicorn/no-unnecessary-parameters
const setup = (db?: Kysely<DB>) => {
const { ctx } = newMediumService(BaseService, {
database: db || defaultDatabase,
real: [],
mock: [LoggingRepository],
});
return { ctx, sut: ctx.get(AssetJobRepository) };
};
beforeAll(async () => {
defaultDatabase = await getKyselyDB();
});
describe(AssetJobRepository.name, () => {
describe('streamForThumbnailJob', () => {
it('should work', async () => {
const { sut } = setup();
const stream = sut.streamForThumbnailJob({ force: false, fullsizeEnabled: false });
await expect(stream.next()).resolves.toEqual({ done: true, value: undefined });
});
it('should queue an asset with missing thumbnails', async () => {
const { ctx, sut } = setup();
const { user } = await ctx.newUser();
const { asset } = await ctx.newAsset({ ownerId: user.id });
await ctx.newJobStatus({ assetId: asset.id, metadataExtractedAt: new Date() });
const stream = sut.streamForThumbnailJob({ force: false, fullsizeEnabled: false });
await expect(consume(stream)).resolves.toEqual([expect.objectContaining({ id: asset.id })]);
});
it('should skip assets without missing thumbnails', async () => {
const { ctx, sut } = setup();
const { user } = await ctx.newUser();
const { asset } = await ctx.newAsset({ ownerId: user.id, thumbhash: Buffer.from('fake-thumbhash-buffer') });
await ctx.newJobStatus({ assetId: asset.id, metadataExtractedAt: new Date() });
await ctx.newAssetFile({ assetId: asset.id, type: AssetFileType.Thumbnail, path: 'thumbnail.jpg' });
await ctx.newAssetFile({ assetId: asset.id, type: AssetFileType.Preview, path: 'preview.jpg' });
const stream = sut.streamForThumbnailJob({ force: false, fullsizeEnabled: false });
await expect(consume(stream)).resolves.not.toEqual(
expect.arrayContaining([expect.objectContaining({ id: asset.id })]),
);
});
it('should queue assets with a missing full size', async () => {
const { ctx, sut } = setup();
const { user } = await ctx.newUser();
const { asset } = await ctx.newAsset({
ownerId: user.id,
thumbhash: Buffer.from('fake-thumbhash-buffer'),
originalFileName: 'photo.cr2',
});
await ctx.newJobStatus({ assetId: asset.id, metadataExtractedAt: new Date() });
await ctx.newAssetFile({ assetId: asset.id, type: AssetFileType.Thumbnail, path: 'thumbnail.jpg' });
await ctx.newAssetFile({ assetId: asset.id, type: AssetFileType.Preview, path: 'preview.jpg' });
const stream = sut.streamForThumbnailJob({ force: false, fullsizeEnabled: true });
await expect(consume(stream)).resolves.toEqual(
expect.arrayContaining([expect.objectContaining({ id: asset.id })]),
);
});
it('should skip assets with after they have full size previews', async () => {
const { ctx, sut } = setup();
const { user } = await ctx.newUser();
const { asset } = await ctx.newAsset({ ownerId: user.id, thumbhash: Buffer.from('fake-thumbhash-buffer') });
await ctx.newJobStatus({ assetId: asset.id, metadataExtractedAt: new Date() });
await ctx.newAssetFile({ assetId: asset.id, type: AssetFileType.Thumbnail, path: 'thumbnail.jpg' });
await ctx.newAssetFile({ assetId: asset.id, type: AssetFileType.Preview, path: 'preview.jpg' });
await ctx.newAssetFile({ assetId: asset.id, type: AssetFileType.FullSize, path: 'fullsize.jpg' });
const stream = sut.streamForThumbnailJob({ force: false, fullsizeEnabled: true });
await expect(consume(stream)).resolves.not.toEqual(
expect.arrayContaining([expect.objectContaining({ id: asset.id })]),
);
});
it('should skip assets with web-compatible originals', async () => {
const { ctx, sut } = setup();
const { user } = await ctx.newUser();
const { asset } = await ctx.newAsset({
ownerId: user.id,
thumbhash: Buffer.from('fake-thumbhash-buffer'),
originalFileName: 'photo.jpg',
});
await ctx.newJobStatus({ assetId: asset.id, metadataExtractedAt: new Date() });
await ctx.newAssetFile({ assetId: asset.id, type: AssetFileType.Thumbnail, path: 'thumbnail.jpg' });
await ctx.newAssetFile({ assetId: asset.id, type: AssetFileType.Preview, path: 'preview.jpg' });
const stream = sut.streamForThumbnailJob({ force: false, fullsizeEnabled: true });
await expect(consume(stream)).resolves.not.toEqual(
expect.arrayContaining([expect.objectContaining({ id: asset.id })]),
);
});
});
describe('getForOcr', () => {
it('should not return the edited preview file', async () => {
const { ctx, sut } = setup();
const { user } = await ctx.newUser();
const { asset } = await ctx.newAsset({ ownerId: user.id });
await ctx.newAssetFile({
assetId: asset.id,
type: AssetFileType.Preview,
path: 'preview_edited.jpg',
isEdited: true,
});
await ctx.newAssetFile({
assetId: asset.id,
type: AssetFileType.Preview,
path: 'preview_unedited.jpg',
isEdited: false,
});
const result = await sut.getForOcr(asset.id);
expect(result).toEqual(
expect.objectContaining({
previewFile: 'preview_unedited.jpg',
}),
);
});
});
});