2023-12-19 04:29:26 +02:00
|
|
|
import fs from 'node:fs';
|
|
|
|
import path from 'node:path';
|
2024-02-09 22:53:37 +02:00
|
|
|
import { ImmichApi } from 'src/services/api.service';
|
2023-12-19 04:29:26 +02:00
|
|
|
|
|
|
|
export const TEST_CONFIG_DIR = '/tmp/immich/';
|
|
|
|
export const TEST_AUTH_FILE = path.join(TEST_CONFIG_DIR, 'auth.yml');
|
|
|
|
export const TEST_IMMICH_INSTANCE_URL = 'https://test/api';
|
|
|
|
export const TEST_IMMICH_API_KEY = 'pNussssKSYo5WasdgalvKJ1n9kdvaasdfbluPg';
|
|
|
|
|
2024-02-06 01:40:22 +02:00
|
|
|
export const CLI_BASE_OPTIONS = { configDirectory: TEST_CONFIG_DIR };
|
2024-01-30 14:55:34 +02:00
|
|
|
|
|
|
|
export const setup = async () => {
|
|
|
|
const api = new ImmichApi(process.env.IMMICH_INSTANCE_URL as string, '');
|
2024-02-09 22:53:37 +02:00
|
|
|
await api.signUpAdmin({ email: 'cli@immich.app', password: 'password', name: 'Administrator' });
|
|
|
|
const admin = await api.login({ email: 'cli@immich.app', password: 'password' });
|
|
|
|
const apiKey = await api.createApiKey(
|
|
|
|
{ name: 'CLI Test' },
|
2024-01-30 14:55:34 +02:00
|
|
|
{ headers: { Authorization: `Bearer ${admin.accessToken}` } },
|
|
|
|
);
|
|
|
|
|
|
|
|
api.setApiKey(apiKey.secret);
|
|
|
|
|
|
|
|
return api;
|
|
|
|
};
|
2023-12-19 04:29:26 +02:00
|
|
|
|
2024-01-31 01:23:33 +02:00
|
|
|
export const spyOnConsole = () => vi.spyOn(console, 'log').mockImplementation(() => {});
|
2023-12-19 04:29:26 +02:00
|
|
|
|
|
|
|
export const createTestAuthFile = async (contents: string) => {
|
|
|
|
if (!fs.existsSync(TEST_CONFIG_DIR)) {
|
|
|
|
// Create config folder if it doesn't exist
|
|
|
|
const created = await fs.promises.mkdir(TEST_CONFIG_DIR, { recursive: true });
|
|
|
|
if (!created) {
|
|
|
|
throw new Error(`Failed to create config folder ${TEST_CONFIG_DIR}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(TEST_AUTH_FILE, contents);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const readTestAuthFile = async (): Promise<string> => {
|
|
|
|
return await fs.promises.readFile(TEST_AUTH_FILE, 'utf8');
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteAuthFile = () => {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(TEST_AUTH_FILE);
|
|
|
|
} catch (error: any) {
|
|
|
|
if (error.code !== 'ENOENT') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|