2023-10-06 21:32:28 +00:00
|
|
|
import { IJobRepository, JobItem, JobItemHandler, QueueName } from '@app/domain';
|
|
|
|
import { AppModule } from '@app/immich';
|
2023-10-18 22:02:42 +00:00
|
|
|
import { dataSource } from '@app/infra';
|
|
|
|
import { INestApplication } from '@nestjs/common';
|
|
|
|
import { Test } from '@nestjs/testing';
|
2023-10-06 21:32:28 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import path from 'path';
|
2023-11-14 22:47:15 +00:00
|
|
|
import { EntityTarget, ObjectLiteral } from 'typeorm';
|
2023-10-06 21:32:28 +00:00
|
|
|
import { AppService } from '../src/microservices/app.service';
|
|
|
|
|
|
|
|
export const IMMICH_TEST_ASSET_PATH = process.env.IMMICH_TEST_ASSET_PATH;
|
|
|
|
export const IMMICH_TEST_ASSET_TEMP_PATH = path.normalize(`${IMMICH_TEST_ASSET_PATH}/temp/`);
|
2023-09-20 11:16:33 +00:00
|
|
|
|
2023-11-14 22:47:15 +00:00
|
|
|
export interface ResetOptions {
|
|
|
|
entities?: EntityTarget<ObjectLiteral>[];
|
|
|
|
}
|
2023-09-20 11:16:33 +00:00
|
|
|
export const db = {
|
2023-11-14 22:47:15 +00:00
|
|
|
reset: async (options?: ResetOptions) => {
|
2023-09-20 11:16:33 +00:00
|
|
|
if (!dataSource.isInitialized) {
|
|
|
|
await dataSource.initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
await dataSource.transaction(async (em) => {
|
2023-11-14 22:47:15 +00:00
|
|
|
const entities = options?.entities || [];
|
|
|
|
const tableNames =
|
|
|
|
entities.length > 0
|
|
|
|
? entities.map((entity) => em.getRepository(entity).metadata.tableName)
|
|
|
|
: dataSource.entityMetadatas.map((entity) => entity.tableName);
|
|
|
|
|
|
|
|
let deleteUsers = false;
|
|
|
|
for (const tableName of tableNames) {
|
|
|
|
if (tableName === 'users') {
|
|
|
|
deleteUsers = true;
|
2023-09-20 11:16:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-11-14 22:47:15 +00:00
|
|
|
await em.query(`DELETE FROM ${tableName} CASCADE;`);
|
|
|
|
}
|
|
|
|
if (deleteUsers) {
|
|
|
|
await em.query(`DELETE FROM "users" CASCADE;`);
|
2023-09-20 11:16:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
disconnect: async () => {
|
|
|
|
if (dataSource.isInitialized) {
|
|
|
|
await dataSource.destroy();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-10-06 21:32:28 +00:00
|
|
|
let _handler: JobItemHandler = () => Promise.resolve();
|
|
|
|
|
2023-10-18 22:02:42 +00:00
|
|
|
interface TestAppOptions {
|
|
|
|
jobs: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
let app: INestApplication;
|
2023-10-06 21:32:28 +00:00
|
|
|
|
2023-10-18 22:02:42 +00:00
|
|
|
export const testApp = {
|
|
|
|
create: async (options?: TestAppOptions): Promise<[any, INestApplication]> => {
|
|
|
|
const { jobs } = options || { jobs: false };
|
2023-10-06 21:32:28 +00:00
|
|
|
|
2023-10-18 22:02:42 +00:00
|
|
|
const moduleFixture = await Test.createTestingModule({ imports: [AppModule], providers: [AppService] })
|
|
|
|
.overrideProvider(IJobRepository)
|
|
|
|
.useValue({
|
|
|
|
addHandler: (_queueName: QueueName, _concurrency: number, handler: JobItemHandler) => (_handler = handler),
|
2023-10-31 20:19:12 +00:00
|
|
|
addCronJob: jest.fn(),
|
|
|
|
updateCronJob: jest.fn(),
|
|
|
|
deleteCronJob: jest.fn(),
|
|
|
|
validateCronExpression: jest.fn(),
|
2023-10-18 22:02:42 +00:00
|
|
|
queue: (item: JobItem) => jobs && _handler(item),
|
|
|
|
resume: jest.fn(),
|
|
|
|
empty: jest.fn(),
|
|
|
|
setConcurrency: jest.fn(),
|
|
|
|
getQueueStatus: jest.fn(),
|
|
|
|
getJobCounts: jest.fn(),
|
|
|
|
pause: jest.fn(),
|
|
|
|
} as IJobRepository)
|
|
|
|
.compile();
|
2023-10-06 21:32:28 +00:00
|
|
|
|
2023-10-18 22:02:42 +00:00
|
|
|
app = await moduleFixture.createNestApplication().init();
|
|
|
|
|
|
|
|
if (jobs) {
|
|
|
|
await app.get(AppService).init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [app.getHttpServer(), app];
|
|
|
|
},
|
|
|
|
reset: async () => {
|
|
|
|
await db.reset();
|
|
|
|
},
|
|
|
|
teardown: async () => {
|
|
|
|
await app.get(AppService).teardown();
|
|
|
|
await db.disconnect();
|
|
|
|
await app.close();
|
|
|
|
},
|
|
|
|
};
|
2023-09-20 11:16:33 +00:00
|
|
|
|
2023-10-06 21:32:28 +00:00
|
|
|
export const runAllTests: boolean = process.env.IMMICH_RUN_ALL_TESTS === 'true';
|
|
|
|
|
|
|
|
const directoryExists = async (dirPath: string) =>
|
|
|
|
await fs.promises
|
|
|
|
.access(dirPath)
|
|
|
|
.then(() => true)
|
|
|
|
.catch(() => false);
|
|
|
|
|
|
|
|
export async function restoreTempFolder(): Promise<void> {
|
|
|
|
if (await directoryExists(`${IMMICH_TEST_ASSET_TEMP_PATH}`)) {
|
|
|
|
// Temp directory exists, delete all files inside it
|
|
|
|
await fs.promises.rm(IMMICH_TEST_ASSET_TEMP_PATH, { recursive: true });
|
|
|
|
}
|
|
|
|
// Create temp folder
|
|
|
|
await fs.promises.mkdir(IMMICH_TEST_ASSET_TEMP_PATH);
|
|
|
|
}
|