1
0
mirror of https://github.com/immich-app/immich.git synced 2024-11-28 09:33:27 +02:00
immich/server/test/repositories/storage.repository.mock.ts

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-02-13 15:48:47 +02:00
import { WatchOptions } from 'chokidar';
import { StorageCore } from 'src/cores/storage.core';
import { IStorageRepository, StorageEventType, WatchEvents } from 'src/interfaces/storage.interface';
2024-02-13 15:48:47 +02:00
interface MockWatcherOptions {
items?: Array<{ event: 'change' | 'add' | 'unlink' | 'error'; value: string }>;
close?: () => Promise<void>;
2024-02-13 15:48:47 +02:00
}
export const makeMockWatcher =
({ items, close }: MockWatcherOptions) =>
(paths: string[], options: WatchOptions, events: Partial<WatchEvents>) => {
events.onReady?.();
for (const item of items || []) {
switch (item.event) {
case StorageEventType.ADD: {
2024-02-13 15:48:47 +02:00
events.onAdd?.(item.value);
break;
}
case StorageEventType.CHANGE: {
2024-02-13 15:48:47 +02:00
events.onChange?.(item.value);
break;
}
case StorageEventType.UNLINK: {
2024-02-13 15:48:47 +02:00
events.onUnlink?.(item.value);
break;
}
case StorageEventType.ERROR: {
2024-02-13 15:48:47 +02:00
events.onError?.(new Error(item.value));
}
}
}
if (close) {
return () => close();
}
return () => Promise.resolve();
2024-02-13 15:48:47 +02:00
};
export const newStorageRepositoryMock = (reset = true): jest.Mocked<IStorageRepository> => {
if (reset) {
StorageCore.reset();
}
return {
createZipStream: jest.fn(),
createReadStream: jest.fn(),
readFile: jest.fn(),
writeFile: jest.fn(),
unlink: jest.fn(),
unlinkDir: jest.fn().mockResolvedValue(true),
removeEmptyDirs: jest.fn(),
checkFileExists: jest.fn(),
mkdirSync: jest.fn(),
2023-03-22 04:49:19 +02:00
checkDiskUsage: jest.fn(),
readdir: jest.fn(),
stat: jest.fn(),
crawl: jest.fn(),
walk: jest.fn().mockImplementation(async function* () {}),
rename: jest.fn(),
copyFile: jest.fn(),
utimes: jest.fn(),
2024-02-13 15:48:47 +02:00
watch: jest.fn().mockImplementation(makeMockWatcher({})),
};
};