2023-07-09 04:43:11 +02:00
|
|
|
import {
|
|
|
|
AudioCodec,
|
2023-09-03 03:22:42 +02:00
|
|
|
CQMode,
|
2023-09-26 09:03:57 +02:00
|
|
|
CitiesFile,
|
2023-09-03 08:21:51 +02:00
|
|
|
Colorspace,
|
2023-07-09 04:43:11 +02:00
|
|
|
SystemConfig,
|
|
|
|
SystemConfigEntity,
|
|
|
|
SystemConfigKey,
|
2023-08-07 22:35:25 +02:00
|
|
|
ToneMapping,
|
2023-08-02 03:56:10 +02:00
|
|
|
TranscodeHWAccel,
|
2023-07-09 04:43:11 +02:00
|
|
|
TranscodePolicy,
|
|
|
|
VideoCodec,
|
|
|
|
} from '@app/infra/entities';
|
2023-01-21 17:11:55 +01:00
|
|
|
import { BadRequestException } from '@nestjs/common';
|
2023-07-15 06:03:56 +02:00
|
|
|
import { newJobRepositoryMock, newSystemConfigRepositoryMock } from '@test';
|
2023-06-01 12:32:51 +02:00
|
|
|
import { IJobRepository, JobName, QueueName } from '../job';
|
2023-09-04 21:45:59 +02:00
|
|
|
import { SystemConfigValidator, defaults } from './system-config.core';
|
2023-01-21 17:11:55 +01:00
|
|
|
import { ISystemConfigRepository } from './system-config.repository';
|
|
|
|
import { SystemConfigService } from './system-config.service';
|
|
|
|
|
|
|
|
const updates: SystemConfigEntity[] = [
|
2023-05-22 20:07:43 +02:00
|
|
|
{ key: SystemConfigKey.FFMPEG_CRF, value: 30 },
|
2023-01-21 17:11:55 +01:00
|
|
|
{ key: SystemConfigKey.OAUTH_AUTO_LAUNCH, value: true },
|
|
|
|
];
|
|
|
|
|
2023-06-01 12:32:51 +02:00
|
|
|
const updatedConfig = Object.freeze<SystemConfig>({
|
|
|
|
job: {
|
|
|
|
[QueueName.BACKGROUND_TASK]: { concurrency: 5 },
|
|
|
|
[QueueName.CLIP_ENCODING]: { concurrency: 2 },
|
|
|
|
[QueueName.METADATA_EXTRACTION]: { concurrency: 5 },
|
|
|
|
[QueueName.OBJECT_TAGGING]: { concurrency: 2 },
|
|
|
|
[QueueName.RECOGNIZE_FACES]: { concurrency: 2 },
|
|
|
|
[QueueName.SEARCH]: { concurrency: 5 },
|
|
|
|
[QueueName.SIDECAR]: { concurrency: 5 },
|
2023-09-20 13:16:33 +02:00
|
|
|
[QueueName.LIBRARY]: { concurrency: 1 },
|
2023-06-01 12:32:51 +02:00
|
|
|
[QueueName.STORAGE_TEMPLATE_MIGRATION]: { concurrency: 5 },
|
2023-09-25 17:07:21 +02:00
|
|
|
[QueueName.MIGRATION]: { concurrency: 5 },
|
2023-06-01 12:32:51 +02:00
|
|
|
[QueueName.THUMBNAIL_GENERATION]: { concurrency: 5 },
|
|
|
|
[QueueName.VIDEO_CONVERSION]: { concurrency: 1 },
|
|
|
|
},
|
2023-01-21 17:11:55 +01:00
|
|
|
ffmpeg: {
|
2023-05-22 20:07:43 +02:00
|
|
|
crf: 30,
|
|
|
|
threads: 0,
|
2023-01-21 17:11:55 +01:00
|
|
|
preset: 'ultrafast',
|
2023-07-09 04:43:11 +02:00
|
|
|
targetAudioCodec: AudioCodec.AAC,
|
2023-04-04 03:42:53 +02:00
|
|
|
targetResolution: '720',
|
2023-07-09 04:43:11 +02:00
|
|
|
targetVideoCodec: VideoCodec.H264,
|
2023-05-22 20:07:43 +02:00
|
|
|
maxBitrate: '0',
|
2023-09-03 03:22:42 +02:00
|
|
|
bframes: -1,
|
|
|
|
refs: 0,
|
|
|
|
gopSize: 0,
|
|
|
|
npl: 0,
|
|
|
|
temporalAQ: false,
|
|
|
|
cqMode: CQMode.AUTO,
|
2023-05-22 20:07:43 +02:00
|
|
|
twoPass: false,
|
2023-07-09 04:43:11 +02:00
|
|
|
transcode: TranscodePolicy.REQUIRED,
|
2023-08-02 03:56:10 +02:00
|
|
|
accel: TranscodeHWAccel.DISABLED,
|
2023-08-07 22:35:25 +02:00
|
|
|
tonemap: ToneMapping.HABLE,
|
2023-01-21 17:11:55 +01:00
|
|
|
},
|
2023-08-25 06:15:03 +02:00
|
|
|
machineLearning: {
|
|
|
|
enabled: true,
|
|
|
|
url: 'http://immich-machine-learning:3003',
|
2023-08-29 15:58:00 +02:00
|
|
|
classification: {
|
|
|
|
enabled: true,
|
|
|
|
modelName: 'microsoft/resnet-50',
|
|
|
|
minScore: 0.9,
|
|
|
|
},
|
|
|
|
clip: {
|
|
|
|
enabled: true,
|
|
|
|
modelName: 'ViT-B-32::openai',
|
|
|
|
},
|
|
|
|
facialRecognition: {
|
|
|
|
enabled: true,
|
|
|
|
modelName: 'buffalo_l',
|
|
|
|
minScore: 0.7,
|
|
|
|
maxDistance: 0.6,
|
2023-09-18 06:05:35 +02:00
|
|
|
minFaces: 1,
|
2023-08-29 15:58:00 +02:00
|
|
|
},
|
2023-08-25 06:15:03 +02:00
|
|
|
},
|
2023-09-09 04:51:46 +02:00
|
|
|
map: {
|
|
|
|
enabled: true,
|
|
|
|
tileUrl: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
|
|
},
|
2023-09-26 09:03:57 +02:00
|
|
|
reverseGeocoding: {
|
|
|
|
enabled: true,
|
|
|
|
citiesFileOverride: CitiesFile.CITIES_500,
|
|
|
|
},
|
2023-01-21 17:11:55 +01:00
|
|
|
oauth: {
|
|
|
|
autoLaunch: true,
|
|
|
|
autoRegister: true,
|
|
|
|
buttonText: 'Login with OAuth',
|
|
|
|
clientId: '',
|
|
|
|
clientSecret: '',
|
|
|
|
enabled: false,
|
|
|
|
issuerUrl: '',
|
|
|
|
mobileOverrideEnabled: false,
|
|
|
|
mobileRedirectUri: '',
|
|
|
|
scope: 'openid email profile',
|
2023-07-15 21:50:29 +02:00
|
|
|
storageLabelClaim: 'preferred_username',
|
2023-01-21 17:11:55 +01:00
|
|
|
},
|
|
|
|
passwordLogin: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
storageTemplate: {
|
|
|
|
template: '{{y}}/{{y}}-{{MM}}-{{dd}}/{{filename}}',
|
|
|
|
},
|
2023-08-08 16:39:51 +02:00
|
|
|
thumbnail: {
|
|
|
|
webpSize: 250,
|
|
|
|
jpegSize: 1440,
|
2023-09-03 08:21:51 +02:00
|
|
|
quality: 80,
|
|
|
|
colorspace: Colorspace.P3,
|
2023-08-08 16:39:51 +02:00
|
|
|
},
|
2023-01-21 17:11:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe(SystemConfigService.name, () => {
|
|
|
|
let sut: SystemConfigService;
|
|
|
|
let configMock: jest.Mocked<ISystemConfigRepository>;
|
|
|
|
let jobMock: jest.Mocked<IJobRepository>;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-08-25 19:44:52 +02:00
|
|
|
delete process.env.IMMICH_CONFIG_FILE;
|
2023-01-21 17:11:55 +01:00
|
|
|
configMock = newSystemConfigRepositoryMock();
|
|
|
|
jobMock = newJobRepositoryMock();
|
|
|
|
sut = new SystemConfigService(configMock, jobMock);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getDefaults', () => {
|
|
|
|
it('should return the default config', () => {
|
|
|
|
configMock.load.mockResolvedValue(updates);
|
|
|
|
|
2023-07-15 06:03:56 +02:00
|
|
|
expect(sut.getDefaults()).toEqual(defaults);
|
2023-01-21 17:11:55 +01:00
|
|
|
expect(configMock.load).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('addValidator', () => {
|
|
|
|
it('should call the validator on config changes', async () => {
|
|
|
|
const validator: SystemConfigValidator = jest.fn();
|
|
|
|
sut.addValidator(validator);
|
2023-07-15 06:03:56 +02:00
|
|
|
await sut.updateConfig(defaults);
|
|
|
|
expect(validator).toHaveBeenCalledWith(defaults);
|
2023-01-21 17:11:55 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getConfig', () => {
|
|
|
|
it('should return the default config', async () => {
|
|
|
|
configMock.load.mockResolvedValue([]);
|
|
|
|
|
2023-07-15 06:03:56 +02:00
|
|
|
await expect(sut.getConfig()).resolves.toEqual(defaults);
|
2023-01-21 17:11:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should merge the overrides', async () => {
|
|
|
|
configMock.load.mockResolvedValue([
|
2023-05-22 20:07:43 +02:00
|
|
|
{ key: SystemConfigKey.FFMPEG_CRF, value: 30 },
|
2023-01-21 17:11:55 +01:00
|
|
|
{ key: SystemConfigKey.OAUTH_AUTO_LAUNCH, value: true },
|
|
|
|
]);
|
|
|
|
|
|
|
|
await expect(sut.getConfig()).resolves.toEqual(updatedConfig);
|
|
|
|
});
|
2023-08-25 19:44:52 +02:00
|
|
|
|
|
|
|
it('should load the config from a file', async () => {
|
|
|
|
process.env.IMMICH_CONFIG_FILE = 'immich-config.json';
|
|
|
|
const partialConfig = { ffmpeg: { crf: 30 }, oauth: { autoLaunch: true } };
|
|
|
|
configMock.readFile.mockResolvedValue(Buffer.from(JSON.stringify(partialConfig)));
|
|
|
|
|
|
|
|
await expect(sut.getConfig()).resolves.toEqual(updatedConfig);
|
|
|
|
|
|
|
|
expect(configMock.readFile).toHaveBeenCalledWith('immich-config.json');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should accept an empty configuration file', async () => {
|
|
|
|
process.env.IMMICH_CONFIG_FILE = 'immich-config.json';
|
|
|
|
configMock.readFile.mockResolvedValue(Buffer.from(JSON.stringify({})));
|
|
|
|
|
|
|
|
await expect(sut.getConfig()).resolves.toEqual(defaults);
|
|
|
|
|
|
|
|
expect(configMock.readFile).toHaveBeenCalledWith('immich-config.json');
|
|
|
|
});
|
|
|
|
|
|
|
|
const tests = [
|
|
|
|
{ should: 'validate numbers', config: { ffmpeg: { crf: 'not-a-number' } } },
|
|
|
|
{ should: 'validate booleans', config: { oauth: { enabled: 'invalid' } } },
|
|
|
|
{ should: 'validate enums', config: { ffmpeg: { transcode: 'unknown' } } },
|
|
|
|
{ should: 'validate top level unknown options', config: { unknownOption: true } },
|
|
|
|
{ should: 'validate nested unknown options', config: { ffmpeg: { unknownOption: true } } },
|
|
|
|
{ should: 'validate required oauth fields', config: { oauth: { enabled: true } } },
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const test of tests) {
|
|
|
|
it(`should ${test.should}`, async () => {
|
|
|
|
process.env.IMMICH_CONFIG_FILE = 'immich-config.json';
|
|
|
|
configMock.readFile.mockResolvedValue(Buffer.from(JSON.stringify(test.config)));
|
|
|
|
|
|
|
|
await expect(sut.getConfig()).rejects.toBeInstanceOf(Error);
|
|
|
|
});
|
|
|
|
}
|
2023-01-21 17:11:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getStorageTemplateOptions', () => {
|
|
|
|
it('should send back the datetime variables', () => {
|
|
|
|
expect(sut.getStorageTemplateOptions()).toEqual({
|
|
|
|
dayOptions: ['d', 'dd'],
|
|
|
|
hourOptions: ['h', 'hh', 'H', 'HH'],
|
|
|
|
minuteOptions: ['m', 'mm'],
|
|
|
|
monthOptions: ['M', 'MM', 'MMM', 'MMMM'],
|
|
|
|
presetOptions: [
|
|
|
|
'{{y}}/{{y}}-{{MM}}-{{dd}}/{{filename}}',
|
|
|
|
'{{y}}/{{MM}}-{{dd}}/{{filename}}',
|
|
|
|
'{{y}}/{{MMMM}}-{{dd}}/{{filename}}',
|
|
|
|
'{{y}}/{{MM}}/{{filename}}',
|
|
|
|
'{{y}}/{{MMM}}/{{filename}}',
|
|
|
|
'{{y}}/{{MMMM}}/{{filename}}',
|
|
|
|
'{{y}}/{{MM}}/{{dd}}/{{filename}}',
|
|
|
|
'{{y}}/{{MMMM}}/{{dd}}/{{filename}}',
|
|
|
|
'{{y}}/{{y}}-{{MM}}/{{y}}-{{MM}}-{{dd}}/{{filename}}',
|
|
|
|
'{{y}}-{{MM}}-{{dd}}/{{filename}}',
|
|
|
|
'{{y}}-{{MMM}}-{{dd}}/{{filename}}',
|
|
|
|
'{{y}}-{{MMMM}}-{{dd}}/{{filename}}',
|
2023-03-01 14:10:01 +01:00
|
|
|
'{{y}}/{{y}}-{{MM}}/{{filename}}',
|
2023-01-21 17:11:55 +01:00
|
|
|
],
|
|
|
|
secondOptions: ['s', 'ss'],
|
|
|
|
yearOptions: ['y', 'yy'],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('updateConfig', () => {
|
|
|
|
it('should notify the microservices process', async () => {
|
|
|
|
configMock.load.mockResolvedValue(updates);
|
|
|
|
|
|
|
|
await expect(sut.updateConfig(updatedConfig)).resolves.toEqual(updatedConfig);
|
|
|
|
|
|
|
|
expect(configMock.saveAll).toHaveBeenCalledWith(updates);
|
2023-02-25 15:12:03 +01:00
|
|
|
expect(jobMock.queue).toHaveBeenCalledWith({ name: JobName.SYSTEM_CONFIG_CHANGE });
|
2023-01-21 17:11:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if the config is not valid', async () => {
|
|
|
|
const validator = jest.fn().mockRejectedValue('invalid config');
|
|
|
|
|
|
|
|
sut.addValidator(validator);
|
|
|
|
|
|
|
|
await expect(sut.updateConfig(updatedConfig)).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
|
|
|
|
expect(validator).toHaveBeenCalledWith(updatedConfig);
|
|
|
|
expect(configMock.saveAll).not.toHaveBeenCalled();
|
|
|
|
});
|
2023-08-25 19:44:52 +02:00
|
|
|
|
|
|
|
it('should throw an error if a config file is in use', async () => {
|
|
|
|
process.env.IMMICH_CONFIG_FILE = 'immich-config.json';
|
|
|
|
configMock.readFile.mockResolvedValue(Buffer.from(JSON.stringify({})));
|
|
|
|
await expect(sut.updateConfig(defaults)).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(configMock.saveAll).not.toHaveBeenCalled();
|
|
|
|
});
|
2023-01-21 17:11:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('refreshConfig', () => {
|
|
|
|
it('should notify the subscribers', async () => {
|
|
|
|
const changeMock = jest.fn();
|
|
|
|
const subscription = sut.config$.subscribe(changeMock);
|
|
|
|
|
|
|
|
await sut.refreshConfig();
|
|
|
|
|
2023-07-15 06:03:56 +02:00
|
|
|
expect(changeMock).toHaveBeenCalledWith(defaults);
|
2023-01-21 17:11:55 +01:00
|
|
|
|
|
|
|
subscription.unsubscribe();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|