High performance self-hosted photo and video management solution.
TypeScript backup-toolfluttergoogle-photosgoogle-photos-alternativejavascriptmobile-appnestjsnodejsphoto-galleryphotosphotos-managementself-hostedsveltesveltekittypescriptvideos
import { authManager } from '#lib/managers/auth-manager.svelte.ts';
import { queryClient } from '#lib/queries/client.ts';
import { getUserConfigQuery } from '#lib/queries/config-user.query.ts';
import { getAssetActions, handleDownloadAsset } from '#lib/services/asset.service.ts';
import { setSharedLink } from '#lib/utils.ts';
import { getFormatter } from '#lib/utils/i18n.ts';
import { getAssetInfo, type UserConfigDto } from '@immich/sdk';
import { toastManager } from '@immich/ui';
import { vitest } from 'vitest';
import { assetFactory } from '#test-data/factories/asset-factory';
import { preferencesFactory } from '#test-data/factories/preferences-factory';
import { sharedLinkFactory } from '#test-data/factories/shared-link-factory';
import { userAdminFactory } from '#test-data/factories/user-factory';
vitest.mock('@immich/ui', () => ({
toastManager: {
primary: vitest.fn(),
},
}));
vitest.mock('#lib/utils/i18n.js', () => ({
getFormatter: vitest.fn(),
getPreferredLocale: vitest.fn(),
}));
vitest.mock('@immich/sdk');
vitest.mock('#lib/utils.js', async () => {
const originalModule = await vitest.importActual('#lib/utils.js');
return {
...originalModule,
sleep: vitest.fn(),
};
});
describe('AssetService', () => {
describe('getAssetActions', () => {
beforeEach(() => {
authManager.setPreferences(preferencesFactory.build());
queryClient.setQueryData(getUserConfigQuery().queryKey, {
machineLearning: {
enabled: false,
clip: { enabled: false },
duplicateDetection: { enabled: false },
facialRecognition: { enabled: false },
ocr: { enabled: false },
},
} as UserConfigDto);
});
it('should allow shared link downloads if the user owns the asset and shared link downloads are disabled', () => {
const ownerId = 'owner';
const user = userAdminFactory.build({ id: ownerId });
const asset = assetFactory.build({ ownerId });
authManager.setUser(user);
setSharedLink(sharedLinkFactory.build({ allowDownload: false }));
const assetActions = getAssetActions(() => '', asset);
expect(assetActions.SharedLinkDownload.$if?.()).toStrictEqual(true);
});
it('should not allow shared link downloads if the user does not own the asset and shared link downloads are disabled', () => {
const ownerId = 'owner';
const user = userAdminFactory.build({ id: 'non-owner' });
const asset = assetFactory.build({ ownerId });
authManager.setUser(user);
setSharedLink(sharedLinkFactory.build({ allowDownload: false }));
const assetActions = getAssetActions(() => '', asset);
expect(assetActions.SharedLinkDownload.$if?.()).toStrictEqual(false);
});
it('should allow shared link downloads if shared link downloads are enabled regardless of user', () => {
const asset = assetFactory.build();
setSharedLink(sharedLinkFactory.build({ allowDownload: true }));
const assetActions = getAssetActions(() => '', asset);
expect(assetActions.SharedLinkDownload.$if?.()).toStrictEqual(true);
});
});
describe('handleDownloadAsset', () => {
it('should use the asset originalFileName when showing toasts', async () => {
const $t = vitest.fn().mockReturnValue('formatter');
vitest.mocked(getFormatter).mockResolvedValue($t);
const asset = assetFactory.build({ originalFileName: 'asset.heic' });
await handleDownloadAsset(asset, { edited: false });
expect($t).toHaveBeenNthCalledWith(1, 'downloading_asset_filename', { values: { filename: 'asset.heic' } });
expect(toastManager.primary).toHaveBeenCalledWith('formatter');
});
it('should use the motion asset originalFileName when showing toasts', async () => {
const $t = vitest.fn().mockReturnValue('formatter');
vitest.mocked(getFormatter).mockResolvedValue($t);
const motionAsset = assetFactory.build({ originalFileName: 'asset.mov' });
vitest.mocked(getAssetInfo).mockResolvedValue(motionAsset);
const asset = assetFactory.build({ originalFileName: 'asset.heic', livePhotoVideoId: '1' });
await handleDownloadAsset(asset, { edited: false });
expect($t).toHaveBeenNthCalledWith(1, 'downloading_asset_filename', { values: { filename: 'asset.heic' } });
expect($t).toHaveBeenNthCalledWith(2, 'downloading_asset_filename', { values: { filename: 'asset-motion.mov' } });
expect(toastManager.primary).toHaveBeenCalledWith('formatter');
});
});
});