Skip to content

High performance self-hosted photo and video management solution.

chore(server): migrate library e2e tests (#27277)

Jonathan Jogenfors2018e4f8c438
diff --git a/e2e/src/specs/server/api/library.e2e-spec.ts b/e2e/src/specs/server/api/library.e2e-spec.ts
index ccb594610c..47a6da0648 100644
--- a/e2e/src/specs/server/api/library.e2e-spec.ts
+++ b/e2e/src/specs/server/api/library.e2e-spec.ts
@@ -1,5 +1,5 @@
import { LibraryResponseDto, LoginResponseDto, getAllLibraries } from '@immich/sdk';
-import { cpSync, existsSync, rmSync, unlinkSync } from 'node:fs';
+import { cpSync, existsSync } from 'node:fs';
import { Socket } from 'socket.io-client';
import { userDto, uuidDto } from 'src/fixtures';
import { errorDto } from 'src/responses';
@@ -782,553 +782,6 @@ describe('/libraries', () => {
utils.removeImageFile(`${testAssetDir}/temp/reimport/asset.jpg`);
});
-
- it('should set an asset offline if its file is missing', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/offline`],
- });
-
- utils.createImageFile(`${testAssetDir}/temp/offline/offline.png`);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
- expect(assets.count).toBe(1);
-
- utils.removeImageFile(`${testAssetDir}/temp/offline/offline.png`);
-
- await utils.scan(admin.accessToken, library.id);
-
- const trashedAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
- expect(trashedAsset.originalPath).toBe(`${testAssetDirInternal}/temp/offline/offline.png`);
- expect(trashedAsset.isOffline).toEqual(true);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
- expect(newAssets.items).toEqual([]);
- });
-
- it('should set an asset offline if its file is not in any import path', async () => {
- utils.createImageFile(`${testAssetDir}/temp/offline/offline.png`);
-
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/offline`],
- });
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
- expect(assets.count).toBe(1);
-
- utils.createDirectory(`${testAssetDir}/temp/another-path/`);
-
- await utils.updateLibrary(admin.accessToken, library.id, {
- importPaths: [`${testAssetDirInternal}/temp/another-path/`],
- });
-
- await utils.scan(admin.accessToken, library.id);
-
- const trashedAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
- expect(trashedAsset.originalPath).toBe(`${testAssetDirInternal}/temp/offline/offline.png`);
- expect(trashedAsset.isOffline).toBe(true);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([]);
-
- utils.removeImageFile(`${testAssetDir}/temp/offline/offline.png`);
- utils.removeDirectory(`${testAssetDir}/temp/another-path/`);
- });
-
- it('should set an asset offline if its file is covered by an exclusion pattern', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp`],
- });
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets } = await utils.searchAssets(admin.accessToken, {
- libraryId: library.id,
- originalFileName: 'assetB.png',
- });
- expect(assets.count).toBe(1);
-
- await utils.updateLibrary(admin.accessToken, library.id, { exclusionPatterns: ['**/directoryB/**'] });
-
- await utils.scan(admin.accessToken, library.id);
-
- const trashedAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
- expect(trashedAsset.isTrashed).toBe(true);
- expect(trashedAsset.originalPath).toBe(`${testAssetDirInternal}/temp/directoryB/assetB.png`);
- expect(trashedAsset.isOffline).toBe(true);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([
- expect.objectContaining({
- originalFileName: 'assetA.png',
- }),
- ]);
- });
-
- it('should not set an asset offline if its file exists, is in an import path, and not covered by an exclusion pattern', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp`],
- });
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets: assetsBefore } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
- expect(assetsBefore.count).toBeGreaterThan(1);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(assets).toEqual(assetsBefore);
- });
-
- describe('xmp metadata', async () => {
- it('should import metadata from file.xmp', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/xmp`],
- });
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2000.xmp`, `${testAssetDir}/temp/xmp/glarus.xmp`);
- cpSync(`${testAssetDir}/formats/raw/Nikon/D80/glarus.nef`, `${testAssetDir}/temp/xmp/glarus.nef`);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([
- expect.objectContaining({
- originalFileName: 'glarus.nef',
- fileCreatedAt: '2000-09-27T12:35:33.000Z',
- }),
- ]);
-
- rmSync(`${testAssetDir}/temp/xmp`, { recursive: true, force: true });
- });
-
- it('should import metadata from file.ext.xmp', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/xmp`],
- });
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2000.xmp`, `${testAssetDir}/temp/xmp/glarus.nef.xmp`);
- cpSync(`${testAssetDir}/formats/raw/Nikon/D80/glarus.nef`, `${testAssetDir}/temp/xmp/glarus.nef`);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([
- expect.objectContaining({
- originalFileName: 'glarus.nef',
- fileCreatedAt: '2000-09-27T12:35:33.000Z',
- }),
- ]);
-
- rmSync(`${testAssetDir}/temp/xmp`, { recursive: true, force: true });
- });
-
- it('should import metadata in file.ext.xmp before file.xmp if both exist', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/xmp`],
- });
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2000.xmp`, `${testAssetDir}/temp/xmp/glarus.nef.xmp`);
- cpSync(`${testAssetDir}/metadata/xmp/dates/2010.xmp`, `${testAssetDir}/temp/xmp/glarus.xmp`);
- cpSync(`${testAssetDir}/formats/raw/Nikon/D80/glarus.nef`, `${testAssetDir}/temp/xmp/glarus.nef`);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([
- expect.objectContaining({
- originalFileName: 'glarus.nef',
- fileCreatedAt: '2000-09-27T12:35:33.000Z',
- }),
- ]);
-
- rmSync(`${testAssetDir}/temp/xmp`, { recursive: true, force: true });
- });
-
- it('should switch from using file.xmp to file.ext.xmp when asset refreshes', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/xmp`],
- });
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2000.xmp`, `${testAssetDir}/temp/xmp/glarus.xmp`);
- cpSync(`${testAssetDir}/formats/raw/Nikon/D80/glarus.nef`, `${testAssetDir}/temp/xmp/glarus.nef`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_000);
-
- await utils.scan(admin.accessToken, library.id);
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2010.xmp`, `${testAssetDir}/temp/xmp/glarus.nef.xmp`);
- unlinkSync(`${testAssetDir}/temp/xmp/glarus.xmp`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_001);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([
- expect.objectContaining({
- originalFileName: 'glarus.nef',
- fileCreatedAt: '2010-09-27T12:35:33.000Z',
- }),
- ]);
-
- rmSync(`${testAssetDir}/temp/xmp`, { recursive: true, force: true });
- });
-
- it('should switch from using file metadata to file.xmp metadata when asset refreshes', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/xmp`],
- });
-
- cpSync(`${testAssetDir}/formats/raw/Nikon/D80/glarus.nef`, `${testAssetDir}/temp/xmp/glarus.nef`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_000);
-
- await utils.scan(admin.accessToken, library.id);
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2000.xmp`, `${testAssetDir}/temp/xmp/glarus.xmp`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_001);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([
- expect.objectContaining({
- originalFileName: 'glarus.nef',
- fileCreatedAt: '2000-09-27T12:35:33.000Z',
- }),
- ]);
-
- rmSync(`${testAssetDir}/temp/xmp`, { recursive: true, force: true });
- });
-
- it('should switch from using file metadata to file.ext.xmp metadata when asset refreshes', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/xmp`],
- });
-
- cpSync(`${testAssetDir}/formats/raw/Nikon/D80/glarus.nef`, `${testAssetDir}/temp/xmp/glarus.nef`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_000);
-
- await utils.scan(admin.accessToken, library.id);
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2000.xmp`, `${testAssetDir}/temp/xmp/glarus.nef.xmp`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_001);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([
- expect.objectContaining({
- originalFileName: 'glarus.nef',
- fileCreatedAt: '2000-09-27T12:35:33.000Z',
- }),
- ]);
-
- rmSync(`${testAssetDir}/temp/xmp`, { recursive: true, force: true });
- });
-
- it('should switch from using file.ext.xmp to file.xmp when asset refreshes', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/xmp`],
- });
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2000.xmp`, `${testAssetDir}/temp/xmp/glarus.nef.xmp`);
- cpSync(`${testAssetDir}/formats/raw/Nikon/D80/glarus.nef`, `${testAssetDir}/temp/xmp/glarus.nef`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_000);
-
- await utils.scan(admin.accessToken, library.id);
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2010.xmp`, `${testAssetDir}/temp/xmp/glarus.xmp`);
- unlinkSync(`${testAssetDir}/temp/xmp/glarus.nef.xmp`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_001);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([
- expect.objectContaining({
- originalFileName: 'glarus.nef',
- fileCreatedAt: '2010-09-27T12:35:33.000Z',
- }),
- ]);
-
- rmSync(`${testAssetDir}/temp/xmp`, { recursive: true, force: true });
- });
-
- it('should switch from using file.ext.xmp to file metadata', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/xmp`],
- });
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2000.xmp`, `${testAssetDir}/temp/xmp/glarus.nef.xmp`);
- cpSync(`${testAssetDir}/formats/raw/Nikon/D80/glarus.nef`, `${testAssetDir}/temp/xmp/glarus.nef`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_000);
-
- await utils.scan(admin.accessToken, library.id);
-
- unlinkSync(`${testAssetDir}/temp/xmp/glarus.nef.xmp`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_001);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([
- expect.objectContaining({
- originalFileName: 'glarus.nef',
- fileCreatedAt: '2010-07-20T17:27:12.000Z',
- }),
- ]);
-
- rmSync(`${testAssetDir}/temp/xmp`, { recursive: true, force: true });
- });
-
- it('should switch from using file.xmp to file metadata', async () => {
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/xmp`],
- });
-
- cpSync(`${testAssetDir}/metadata/xmp/dates/2000.xmp`, `${testAssetDir}/temp/xmp/glarus.xmp`);
- cpSync(`${testAssetDir}/formats/raw/Nikon/D80/glarus.nef`, `${testAssetDir}/temp/xmp/glarus.nef`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_000);
-
- await utils.scan(admin.accessToken, library.id);
-
- unlinkSync(`${testAssetDir}/temp/xmp/glarus.xmp`);
- await utimes(`${testAssetDir}/temp/xmp/glarus.nef`, 447_775_200_001);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets: newAssets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(newAssets.items).toEqual([
- expect.objectContaining({
- originalFileName: 'glarus.nef',
- fileCreatedAt: '2010-07-20T17:27:12.000Z',
- }),
- ]);
-
- rmSync(`${testAssetDir}/temp/xmp`, { recursive: true, force: true });
- });
- });
-
- it('should set an offline asset to online if its file exists, is in an import path, and not covered by an exclusion pattern', async () => {
- utils.createImageFile(`${testAssetDir}/temp/offline/offline.png`);
-
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/offline`],
- });
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(assets.count).toBe(1);
-
- utils.renameImageFile(`${testAssetDir}/temp/offline/offline.png`, `${testAssetDir}/temp/offline.png`);
-
- await utils.scan(admin.accessToken, library.id);
-
- const offlineAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
- expect(offlineAsset.isTrashed).toBe(true);
- expect(offlineAsset.originalPath).toBe(`${testAssetDirInternal}/temp/offline/offline.png`);
- expect(offlineAsset.isOffline).toBe(true);
-
- {
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id, withDeleted: true });
- expect(assets.count).toBe(1);
- }
-
- utils.renameImageFile(`${testAssetDir}/temp/offline.png`, `${testAssetDir}/temp/offline/offline.png`);
-
- await utils.scan(admin.accessToken, library.id);
-
- const backOnlineAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
-
- expect(backOnlineAsset.isTrashed).toBe(false);
- expect(backOnlineAsset.originalPath).toBe(`${testAssetDirInternal}/temp/offline/offline.png`);
- expect(backOnlineAsset.isOffline).toBe(false);
-
- {
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
- expect(assets.count).toBe(1);
- }
- });
-
- it('should set a trashed offline asset to online but keep it in trash', async () => {
- utils.createImageFile(`${testAssetDir}/temp/offline/offline.png`);
-
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/offline`],
- });
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- expect(assets.count).toBe(1);
-
- await utils.deleteAssets(admin.accessToken, [assets.items[0].id]);
-
- {
- const trashedAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
-
- expect(trashedAsset.isTrashed).toBe(true);
- }
-
- utils.renameImageFile(`${testAssetDir}/temp/offline/offline.png`, `${testAssetDir}/temp/offline.png`);
-
- await utils.scan(admin.accessToken, library.id);
-
- const offlineAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
- expect(offlineAsset.isTrashed).toBe(true);
- expect(offlineAsset.originalPath).toBe(`${testAssetDirInternal}/temp/offline/offline.png`);
- expect(offlineAsset.isOffline).toBe(true);
-
- {
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id, withDeleted: true });
- expect(assets.count).toBe(1);
- }
-
- utils.renameImageFile(`${testAssetDir}/temp/offline.png`, `${testAssetDir}/temp/offline/offline.png`);
-
- await utils.scan(admin.accessToken, library.id);
-
- const backOnlineAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
-
- expect(backOnlineAsset.originalPath).toBe(`${testAssetDirInternal}/temp/offline/offline.png`);
- expect(backOnlineAsset.isOffline).toBe(false);
- expect(backOnlineAsset.isTrashed).toBe(true);
-
- {
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id, withDeleted: true });
- expect(assets.count).toBe(1);
- }
- });
-
- it('should not set an offline asset to online if its file exists, is not covered by an exclusion pattern, but is outside of all import paths', async () => {
- utils.createImageFile(`${testAssetDir}/temp/offline/offline.png`);
-
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/offline`],
- });
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
-
- utils.renameImageFile(`${testAssetDir}/temp/offline/offline.png`, `${testAssetDir}/temp/offline.png`);
-
- await utils.scan(admin.accessToken, library.id);
-
- {
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id, withDeleted: true });
- expect(assets.count).toBe(1);
- }
-
- const offlineAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
-
- expect(offlineAsset.isTrashed).toBe(true);
- expect(offlineAsset.originalPath).toBe(`${testAssetDirInternal}/temp/offline/offline.png`);
- expect(offlineAsset.isOffline).toBe(true);
-
- utils.renameImageFile(`${testAssetDir}/temp/offline.png`, `${testAssetDir}/temp/offline/offline.png`);
-
- utils.createDirectory(`${testAssetDir}/temp/another-path/`);
-
- await utils.updateLibrary(admin.accessToken, library.id, {
- importPaths: [`${testAssetDirInternal}/temp/another-path`],
- });
-
- await utils.scan(admin.accessToken, library.id);
-
- const stillOfflineAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
-
- expect(stillOfflineAsset.isTrashed).toBe(true);
- expect(stillOfflineAsset.originalPath).toBe(`${testAssetDirInternal}/temp/offline/offline.png`);
- expect(stillOfflineAsset.isOffline).toBe(true);
-
- {
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id, withDeleted: true });
- expect(assets.count).toBe(1);
- }
-
- utils.removeDirectory(`${testAssetDir}/temp/another-path/`);
- });
-
- it('should not set an offline asset to online if its file exists, is in an import path, but is covered by an exclusion pattern', async () => {
- utils.createImageFile(`${testAssetDir}/temp/offline/offline.png`);
-
- const library = await utils.createLibrary(admin.accessToken, {
- ownerId: admin.userId,
- importPaths: [`${testAssetDirInternal}/temp/offline`],
- });
-
- await utils.scan(admin.accessToken, library.id);
-
- {
- const { assets: assetsBefore } = await utils.searchAssets(admin.accessToken, { libraryId: library.id });
- expect(assetsBefore.count).toBe(1);
- }
-
- utils.renameImageFile(`${testAssetDir}/temp/offline/offline.png`, `${testAssetDir}/temp/offline.png`);
-
- await utils.scan(admin.accessToken, library.id);
-
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id, withDeleted: true });
- expect(assets.count).toBe(1);
-
- const offlineAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
-
- expect(offlineAsset.isTrashed).toBe(true);
- expect(offlineAsset.originalPath).toBe(`${testAssetDirInternal}/temp/offline/offline.png`);
- expect(offlineAsset.isOffline).toBe(true);
-
- utils.renameImageFile(`${testAssetDir}/temp/offline.png`, `${testAssetDir}/temp/offline/offline.png`);
-
- await utils.updateLibrary(admin.accessToken, library.id, { exclusionPatterns: ['**/offline/**'] });
-
- await utils.scan(admin.accessToken, library.id);
-
- const stillOfflineAsset = await utils.getAssetInfo(admin.accessToken, assets.items[0].id);
-
- expect(stillOfflineAsset.isTrashed).toBe(true);
- expect(stillOfflineAsset.originalPath).toBe(`${testAssetDirInternal}/temp/offline/offline.png`);
- expect(stillOfflineAsset.isOffline).toBe(true);
-
- {
- const { assets } = await utils.searchAssets(admin.accessToken, { libraryId: library.id, withDeleted: true });
- expect(assets.count).toBe(1);
- }
- });
});
describe('POST /libraries/:id/validate', () => {
diff --git a/server/test/medium.factory.ts b/server/test/medium.factory.ts
index dc3bc34e3c..1fbe41d964 100644
--- a/server/test/medium.factory.ts
+++ b/server/test/medium.factory.ts
@@ -35,6 +35,7 @@ import { EmailRepository } from 'src/repositories/email.repository';
import { EventRepository } from 'src/repositories/event.repository';
import { IntegrityRepository } from 'src/repositories/integrity.repository';
import { JobRepository } from 'src/repositories/job.repository';
+import { LibraryRepository } from 'src/repositories/library.repository';
import { LoggingRepository } from 'src/repositories/logging.repository';
import { MachineLearningRepository } from 'src/repositories/machine-learning.repository';
import { MapRepository } from 'src/repositories/map.repository';
@@ -451,6 +452,7 @@ const newRealRepository = <T extends BaseServiceDeps[number]>(key: T, db: Kysely
case DuplicateRepository:
case IntegrityRepository:
case MemoryRepository:
+ case LibraryRepository:
case NotificationRepository:
case OcrRepository:
case PartnerRepository:
@@ -517,6 +519,7 @@ const newMockRepository = <T>(key: ClassConstructor<T>) => {
case AssetJobRepository:
case ConfigRepository:
case CryptoRepository:
+ case LibraryRepository:
case MemoryRepository:
case IntegrityRepository:
case NotificationRepository:
diff --git a/server/test/medium/specs/services/library.service.spec.ts b/server/test/medium/specs/services/library.service.spec.ts
new file mode 100644
index 0000000000..e551869064
--- /dev/null
+++ b/server/test/medium/specs/services/library.service.spec.ts
@@ -0,0 +1,455 @@
+import { Kysely } from 'kysely';
+import { copyFile, mkdir, mkdtemp, rm, utimes, writeFile } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { dirname, join } from 'node:path';
+import { AssetStatus, JobName, JobStatus } from 'src/enum';
+import { AssetJobRepository } from 'src/repositories/asset-job.repository';
+import { AssetRepository } from 'src/repositories/asset.repository';
+import { CryptoRepository } from 'src/repositories/crypto.repository';
+import { EventRepository } from 'src/repositories/event.repository';
+import { JobRepository } from 'src/repositories/job.repository';
+import { LibraryRepository } from 'src/repositories/library.repository';
+import { LoggingRepository } from 'src/repositories/logging.repository';
+import { StorageRepository } from 'src/repositories/storage.repository';
+import { DB } from 'src/schema';
+import { LibraryService } from 'src/services/library.service';
+import { newMediumService, testAssetsDir } from 'test/medium.factory';
+import { getKyselyDB } from 'test/utils';
+
+let defaultDatabase: Kysely<DB>;
+
+const fileModifiedAt = new Date(1_700_000_000_000);
+
+const createFile = async (filePath: string, modifiedAt: Date = fileModifiedAt) => {
+ await mkdir(dirname(filePath), { recursive: true });
+ await writeFile(filePath, 'test');
+ await utimes(filePath, modifiedAt, modifiedAt);
+ return filePath;
+};
+
+const copyTestAsset = async (source: string, filePath: string, modifiedAt: Date = fileModifiedAt) => {
+ await mkdir(dirname(filePath), { recursive: true });
+ await copyFile(join(testAssetsDir, source), filePath);
+ await utimes(filePath, modifiedAt, modifiedAt);
+ return filePath;
+};
+
+const setup = (db?: Kysely<DB>) => {
+ const context = newMediumService(LibraryService, {
+ database: db || defaultDatabase,
+ real: [AssetRepository, AssetJobRepository, CryptoRepository, LibraryRepository, StorageRepository],
+ mock: [EventRepository, JobRepository, LoggingRepository],
+ });
+
+ const jobs = context.ctx.getMock(JobRepository);
+ jobs.queue.mockResolvedValue();
+ jobs.queueAll.mockResolvedValue();
+
+ context.ctx.getMock(EventRepository).emit.mockResolvedValue();
+
+ return context;
+};
+
+beforeAll(async () => {
+ defaultDatabase = await getKyselyDB();
+});
+
+describe(LibraryService.name, () => {
+ let tempDir: string;
+ let importRoot: string;
+ let importPath: string;
+ let excludedPath: string;
+ let outsidePath: string;
+
+ beforeEach(async () => {
+ tempDir = await mkdtemp(join(tmpdir(), 'immich-library-'));
+ importRoot = join(tempDir, 'libraries/offline');
+ importPath = join(importRoot, 'in-path');
+ excludedPath = join(importRoot, 'excluded');
+ outsidePath = join(tempDir, 'libraries/outside');
+ });
+
+ afterEach(async () => {
+ await rm(tempDir, { recursive: true, force: true });
+ });
+
+ const createLibrary = async (
+ ctx: ReturnType<typeof setup>['ctx'],
+ options: { importPaths?: string[]; exclusionPatterns?: string[] } = {},
+ ) => {
+ const { user } = await ctx.newUser();
+ return ctx.get(LibraryRepository).create({
+ ownerId: user.id,
+ name: 'Medium test library',
+ importPaths: options.importPaths ?? [importPath],
+ exclusionPatterns: options.exclusionPatterns ?? [],
+ });
+ };
+
+ describe('offline asset handling', () => {
+ it('should set an asset offline if its file is missing', async () => {
+ const { sut, ctx } = setup();
+ const assetRepo = ctx.get(AssetRepository);
+
+ const library = await createLibrary(ctx);
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ // the file is intentionally never created on disk
+ originalPath: join(importPath, 'offline.png'),
+ isExternal: true,
+ isOffline: false,
+ status: AssetStatus.Active,
+ });
+
+ await expect(
+ sut.handleSyncAssets({
+ libraryId: library.id,
+ importPaths: library.importPaths,
+ exclusionPatterns: library.exclusionPatterns,
+ assetIds: [asset.id],
+ progressCounter: 1,
+ totalAssets: 1,
+ }),
+ ).resolves.toBe(JobStatus.Success);
+
+ const updated = await assetRepo.getById(asset.id);
+ expect(updated).toEqual(expect.objectContaining({ isOffline: true }));
+ expect(updated?.deletedAt).toBeInstanceOf(Date);
+ });
+
+ it('should set an asset offline if its file is not in any import path', async () => {
+ const { sut, ctx } = setup();
+ const assetRepo = ctx.get(AssetRepository);
+
+ const library = await createLibrary(ctx, { importPaths: [importPath] });
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ originalPath: await createFile(join(outsidePath, 'offline.png')),
+ isExternal: true,
+ isOffline: false,
+ status: AssetStatus.Active,
+ });
+
+ await expect(sut.handleQueueSyncAssets({ id: library.id })).resolves.toBe(JobStatus.Success);
+
+ const updated = await assetRepo.getById(asset.id);
+ expect(updated).toEqual(expect.objectContaining({ isOffline: true }));
+ expect(updated?.deletedAt).toBeInstanceOf(Date);
+ });
+
+ it('should set an asset offline if its file is covered by an exclusion pattern', async () => {
+ const { sut, ctx } = setup();
+ const assetRepo = ctx.get(AssetRepository);
+
+ const library = await createLibrary(ctx, {
+ importPaths: [importRoot],
+ exclusionPatterns: ['**/excluded/**'],
+ });
+
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ originalPath: await createFile(join(excludedPath, 'offline.png')),
+ isExternal: true,
+ isOffline: false,
+ status: AssetStatus.Active,
+ });
+
+ await expect(sut.handleQueueSyncAssets({ id: library.id })).resolves.toBe(JobStatus.Success);
+
+ const updated = await assetRepo.getById(asset.id);
+ expect(updated).toEqual(expect.objectContaining({ isOffline: true }));
+ expect(updated?.deletedAt).toBeInstanceOf(Date);
+ });
+
+ it('should not set an asset offline if file exists in import path and is not excluded', async () => {
+ const { sut, ctx } = setup();
+ const assetRepo = ctx.get(AssetRepository);
+
+ const library = await createLibrary(ctx, {
+ importPaths: [importRoot],
+ exclusionPatterns: ['**/excluded/**'],
+ });
+
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ originalPath: await createFile(join(importPath, 'online.png')),
+ fileModifiedAt,
+ isExternal: true,
+ isOffline: false,
+ status: AssetStatus.Active,
+ });
+
+ await expect(
+ sut.handleSyncAssets({
+ libraryId: library.id,
+ importPaths: library.importPaths,
+ exclusionPatterns: library.exclusionPatterns,
+ assetIds: [asset.id],
+ progressCounter: 1,
+ totalAssets: 1,
+ }),
+ ).resolves.toBe(JobStatus.Success);
+
+ const updated = await assetRepo.getById(asset.id);
+ expect(updated).toEqual(expect.objectContaining({ isOffline: false }));
+ expect(updated?.deletedAt).toBeNull();
+ });
+
+ it('should set an offline asset to online if its file exists in an import path and is not excluded', async () => {
+ const { sut, ctx } = setup();
+ const assetRepo = ctx.get(AssetRepository);
+
+ const library = await createLibrary(ctx, { importPaths: [importPath] });
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ originalPath: await createFile(join(importPath, 'offline.png')),
+ isExternal: true,
+ isOffline: true,
+ deletedAt: new Date(),
+ status: AssetStatus.Active,
+ });
+
+ await expect(
+ sut.handleSyncAssets({
+ libraryId: library.id,
+ importPaths: library.importPaths,
+ exclusionPatterns: library.exclusionPatterns,
+ assetIds: [asset.id],
+ progressCounter: 1,
+ totalAssets: 1,
+ }),
+ ).resolves.toBe(JobStatus.Success);
+
+ const updated = await assetRepo.getById(asset.id);
+ expect(updated).toEqual(expect.objectContaining({ isOffline: false }));
+ expect(updated?.deletedAt).toBeNull();
+ });
+
+ it('should not set an offline asset to online if its file exists in an import path but is excluded', async () => {
+ const { sut, ctx } = setup();
+ const assetRepo = ctx.get(AssetRepository);
+
+ const library = await createLibrary(ctx, {
+ importPaths: [importRoot],
+ exclusionPatterns: ['**/excluded/**'],
+ });
+
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ originalPath: await createFile(join(excludedPath, 'offline.png')),
+ isExternal: true,
+ isOffline: true,
+ deletedAt: new Date(),
+ status: AssetStatus.Active,
+ });
+
+ await expect(
+ sut.handleSyncAssets({
+ libraryId: library.id,
+ importPaths: library.importPaths,
+ exclusionPatterns: library.exclusionPatterns,
+ assetIds: [asset.id],
+ progressCounter: 1,
+ totalAssets: 1,
+ }),
+ ).resolves.toBe(JobStatus.Success);
+
+ const updated = await assetRepo.getById(asset.id);
+ expect(updated).toEqual(expect.objectContaining({ isOffline: true }));
+ expect(updated?.deletedAt).toBeInstanceOf(Date);
+ });
+
+ it('should keep an offline asset offline if it is outside import paths', async () => {
+ const { sut, ctx } = setup();
+ const assetRepo = ctx.get(AssetRepository);
+
+ const library = await createLibrary(ctx, { importPaths: [importPath] });
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ originalPath: await createFile(join(outsidePath, 'offline.png')),
+ isExternal: true,
+ isOffline: true,
+ deletedAt: new Date(),
+ status: AssetStatus.Active,
+ });
+
+ await expect(
+ sut.handleSyncAssets({
+ libraryId: library.id,
+ importPaths: library.importPaths,
+ exclusionPatterns: library.exclusionPatterns,
+ assetIds: [asset.id],
+ progressCounter: 1,
+ totalAssets: 1,
+ }),
+ ).resolves.toBe(JobStatus.Success);
+
+ const updated = await assetRepo.getById(asset.id);
+ expect(updated).toEqual(expect.objectContaining({ isOffline: true }));
+ expect(updated?.deletedAt).toBeInstanceOf(Date);
+ });
+
+ it('should set a trashed asset offline if its file is missing', async () => {
+ const { sut, ctx } = setup();
+ const assetRepo = ctx.get(AssetRepository);
+
+ const library = await createLibrary(ctx, { importPaths: [importPath] });
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ // the file is intentionally never created on disk
+ originalPath: join(importPath, 'offline.png'),
+ isExternal: true,
+ isOffline: false,
+ deletedAt: new Date(),
+ status: AssetStatus.Trashed,
+ });
+
+ await expect(
+ sut.handleSyncAssets({
+ libraryId: library.id,
+ importPaths: library.importPaths,
+ exclusionPatterns: library.exclusionPatterns,
+ assetIds: [asset.id],
+ progressCounter: 1,
+ totalAssets: 1,
+ }),
+ ).resolves.toBe(JobStatus.Success);
+
+ const updated = await assetRepo.getById(asset.id);
+ expect(updated).toEqual(expect.objectContaining({ isOffline: true }));
+ expect(updated?.deletedAt).toBeInstanceOf(Date);
+ });
+
+ it('should set a trashed offline asset to online but keep it in trash', async () => {
+ const { sut, ctx } = setup();
+ const assetRepo = ctx.get(AssetRepository);
+
+ const library = await createLibrary(ctx, { importPaths: [importPath] });
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ originalPath: await createFile(join(importPath, 'offline.png')),
+ isExternal: true,
+ isOffline: true,
+ deletedAt: new Date(),
+ status: AssetStatus.Trashed,
+ });
+
+ await expect(
+ sut.handleSyncAssets({
+ libraryId: library.id,
+ importPaths: library.importPaths,
+ exclusionPatterns: library.exclusionPatterns,
+ assetIds: [asset.id],
+ progressCounter: 1,
+ totalAssets: 1,
+ }),
+ ).resolves.toBe(JobStatus.Success);
+
+ const updated = await assetRepo.getById(asset.id);
+ expect(updated).toEqual(expect.objectContaining({ isOffline: false }));
+ expect(updated?.deletedAt).toBeInstanceOf(Date);
+ });
+ });
+
+ describe('xmp scan behavior', () => {
+ it('should queue sidecar checks for newly imported assets', async () => {
+ const { sut, ctx } = setup();
+ const jobs = ctx.getMock(JobRepository);
+
+ const library = await createLibrary(ctx, { importPaths: [importPath] });
+ const rawPath = await copyTestAsset('formats/raw/Nikon/D80/glarus.nef', join(importPath, 'glarus.nef'));
+
+ await expect(
+ sut.handleSyncFiles({
+ libraryId: library.id,
+ paths: [rawPath],
+ progressCounter: 1,
+ }),
+ ).resolves.toBe(JobStatus.Success);
+
+ expect(jobs.queueAll).toHaveBeenCalledWith([
+ expect.objectContaining({
+ name: JobName.SidecarCheck,
+ data: expect.objectContaining({ id: expect.any(String) }),
+ }),
+ ]);
+ });
+
+ it('should queue sidecar checks for assets whose file changed', async () => {
+ const { sut, ctx } = setup();
+ const jobs = ctx.getMock(JobRepository);
+
+ const library = await createLibrary(ctx, { importPaths: [importPath] });
+ const rawPath = await copyTestAsset('formats/raw/Nikon/D80/glarus.nef', join(importPath, 'glarus.nef'));
+
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ originalPath: rawPath,
+ // the file on disk has a newer modified time
+ fileModifiedAt: new Date(fileModifiedAt.valueOf() - 1000),
+ isExternal: true,
+ isOffline: false,
+ status: AssetStatus.Active,
+ });
+
+ await expect(
+ sut.handleSyncAssets({
+ libraryId: library.id,
+ importPaths: library.importPaths,
+ exclusionPatterns: library.exclusionPatterns,
+ assetIds: [asset.id],
+ progressCounter: 1,
+ totalAssets: 1,
+ }),
+ ).resolves.toBe(JobStatus.Success);
+
+ expect(jobs.queueAll).toHaveBeenCalledWith([
+ {
+ name: JobName.SidecarCheck,
+ data: { id: asset.id, source: 'upload' },
+ },
+ ]);
+ });
+
+ it('should not queue sidecar checks for unchanged assets', async () => {
+ const { sut, ctx } = setup();
+ const jobs = ctx.getMock(JobRepository);
+
+ const library = await createLibrary(ctx, { importPaths: [importPath] });
+ const rawPath = await copyTestAsset('formats/raw/Nikon/D80/glarus.nef', join(importPath, 'glarus.nef'));
+
+ const { asset } = await ctx.newAsset({
+ ownerId: library.ownerId,
+ libraryId: library.id,
+ originalPath: rawPath,
+ fileModifiedAt,
+ isExternal: true,
+ isOffline: false,
+ status: AssetStatus.Active,
+ });
+
+ await expect(
+ sut.handleSyncAssets({
+ libraryId: library.id,
+ importPaths: library.importPaths,
+ exclusionPatterns: library.exclusionPatterns,
+ assetIds: [asset.id],
+ progressCounter: 1,
+ totalAssets: 1,
+ }),
+ ).resolves.toBe(JobStatus.Success);
+
+ expect(jobs.queueAll).not.toHaveBeenCalled();
+ });
+ });
+});