2023-07-09 04:43:11 +02:00
|
|
|
import {
|
|
|
|
AudioCodec,
|
|
|
|
SystemConfig,
|
|
|
|
SystemConfigEntity,
|
|
|
|
SystemConfigKey,
|
|
|
|
TranscodePolicy,
|
|
|
|
VideoCodec,
|
|
|
|
} from '@app/infra/entities';
|
2023-01-21 17:11:55 +01:00
|
|
|
import { BadRequestException } from '@nestjs/common';
|
2023-06-08 17:01:07 +02:00
|
|
|
import { newJobRepositoryMock, newSystemConfigRepositoryMock, systemConfigStub } from '@test';
|
2023-06-01 12:32:51 +02:00
|
|
|
import { IJobRepository, JobName, QueueName } from '../job';
|
2023-01-21 17:11:55 +01:00
|
|
|
import { SystemConfigValidator } from './system-config.core';
|
|
|
|
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 },
|
|
|
|
[QueueName.STORAGE_TEMPLATE_MIGRATION]: { concurrency: 5 },
|
|
|
|
[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',
|
|
|
|
twoPass: false,
|
2023-07-09 04:43:11 +02:00
|
|
|
transcode: TranscodePolicy.REQUIRED,
|
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',
|
|
|
|
},
|
|
|
|
passwordLogin: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
storageTemplate: {
|
|
|
|
template: '{{y}}/{{y}}-{{MM}}-{{dd}}/{{filename}}',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
describe(SystemConfigService.name, () => {
|
|
|
|
let sut: SystemConfigService;
|
|
|
|
let configMock: jest.Mocked<ISystemConfigRepository>;
|
|
|
|
let jobMock: jest.Mocked<IJobRepository>;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
expect(sut.getDefaults()).toEqual(systemConfigStub.defaults);
|
|
|
|
expect(configMock.load).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('addValidator', () => {
|
|
|
|
it('should call the validator on config changes', async () => {
|
|
|
|
const validator: SystemConfigValidator = jest.fn();
|
|
|
|
|
|
|
|
sut.addValidator(validator);
|
|
|
|
|
|
|
|
await sut.updateConfig(systemConfigStub.defaults);
|
|
|
|
|
|
|
|
expect(validator).toHaveBeenCalledWith(systemConfigStub.defaults);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getConfig', () => {
|
|
|
|
it('should return the default config', async () => {
|
|
|
|
configMock.load.mockResolvedValue([]);
|
|
|
|
|
|
|
|
await expect(sut.getConfig()).resolves.toEqual(systemConfigStub.defaults);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('refreshConfig', () => {
|
|
|
|
it('should notify the subscribers', async () => {
|
|
|
|
const changeMock = jest.fn();
|
|
|
|
const subscription = sut.config$.subscribe(changeMock);
|
|
|
|
|
|
|
|
await sut.refreshConfig();
|
|
|
|
|
|
|
|
expect(changeMock).toHaveBeenCalledWith(systemConfigStub.defaults);
|
|
|
|
|
|
|
|
subscription.unsubscribe();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|