2023-05-08 17:45:18 +02:00
|
|
|
import Setting, { SettingItemType, SettingSectionSource, SettingStorage } from '../models/Setting';
|
2021-05-21 15:17:21 +02:00
|
|
|
import { setupDatabaseAndSynchronizer, switchClient, expectThrow, expectNotThrow, msleep } from '../testing/test-utils';
|
2022-04-11 17:49:32 +02:00
|
|
|
import { readFile, stat, mkdirp, writeFile, pathExists, readdir } from 'fs-extra';
|
2023-07-27 17:05:56 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
2022-04-11 17:49:32 +02:00
|
|
|
import { defaultProfileConfig } from '../services/profileConfig/types';
|
|
|
|
import { createNewProfile, saveProfileConfig } from '../services/profileConfig';
|
|
|
|
import initProfile from '../services/profileConfig/initProfile';
|
2024-06-25 15:01:39 +02:00
|
|
|
import { defaultPluginSetting } from '../services/plugins/PluginService';
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-02-09 19:54:29 +02:00
|
|
|
async function loadSettingsFromFile(): Promise<any> {
|
2022-04-11 17:49:32 +02:00
|
|
|
return JSON.parse(await readFile(Setting.settingFilePath, 'utf8'));
|
2021-02-09 19:54:29 +02:00
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2022-04-11 17:49:32 +02:00
|
|
|
const switchToSubProfileSettings = async () => {
|
|
|
|
await Setting.reset();
|
|
|
|
const rootProfileDir = Setting.value('profileDir');
|
|
|
|
const profileConfigPath = `${rootProfileDir}/profiles.json`;
|
|
|
|
let profileConfig = defaultProfileConfig();
|
2022-04-16 16:04:06 +02:00
|
|
|
const { newConfig, newProfile } = createNewProfile(profileConfig, 'Sub-profile');
|
|
|
|
profileConfig = newConfig;
|
|
|
|
profileConfig.currentProfileId = newProfile.id;
|
2022-04-11 17:49:32 +02:00
|
|
|
await saveProfileConfig(profileConfigPath, profileConfig);
|
|
|
|
const { profileDir } = await initProfile(rootProfileDir);
|
|
|
|
await mkdirp(profileDir);
|
|
|
|
await Setting.load();
|
|
|
|
};
|
|
|
|
|
2023-02-20 17:02:29 +02:00
|
|
|
describe('models/Setting', () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2022-11-15 12:23:50 +02:00
|
|
|
beforeEach(async () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
});
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should return only sub-values', (async () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
const settings = {
|
|
|
|
'sync.5.path': 'http://example.com',
|
|
|
|
'sync.5.username': 'testing',
|
|
|
|
};
|
|
|
|
|
|
|
|
let output = Setting.subValues('sync.5', settings);
|
|
|
|
expect(output['path']).toBe('http://example.com');
|
|
|
|
expect(output['username']).toBe('testing');
|
|
|
|
|
|
|
|
output = Setting.subValues('sync.4', settings);
|
|
|
|
expect('path' in output).toBe(false);
|
|
|
|
expect('username' in output).toBe(false);
|
|
|
|
}));
|
|
|
|
|
2021-06-16 19:22:32 +02:00
|
|
|
it('should not fail when trying to load a key that no longer exist from the setting file', (async () => {
|
|
|
|
// To handle the case where a setting value exists in the database but
|
|
|
|
// the metadata has been removed in a new Joplin version.
|
|
|
|
// https://github.com/laurent22/joplin/issues/5086
|
|
|
|
|
|
|
|
Setting.setValue('sync.target', 9); // Saved to file
|
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
const settingValues = await Setting.fileHandler.load();
|
|
|
|
settingValues['itsgone'] = 'myvalue';
|
|
|
|
await Setting.fileHandler.save(settingValues);
|
|
|
|
|
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
await expectNotThrow(async () => Setting.load());
|
2023-05-08 18:30:30 +02:00
|
|
|
await expectThrow(async () => Setting.value('itsgone'), 'unknown_key');
|
2021-06-16 19:22:32 +02:00
|
|
|
}));
|
|
|
|
|
2024-05-02 16:00:12 +02:00
|
|
|
it.each([
|
|
|
|
SettingStorage.Database, SettingStorage.File,
|
|
|
|
])('should allow registering new settings dynamically (storage: %d)', (async (storage) => {
|
2023-05-08 18:30:30 +02:00
|
|
|
await expectThrow(async () => Setting.setValue('myCustom', '123'), 'unknown_key');
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
await Setting.registerSetting('myCustom', {
|
|
|
|
public: true,
|
|
|
|
value: 'default',
|
|
|
|
type: Setting.TYPE_STRING,
|
2024-05-02 16:00:12 +02:00
|
|
|
storage,
|
2020-10-09 19:35:46 +02:00
|
|
|
});
|
|
|
|
|
2024-05-02 16:00:12 +02:00
|
|
|
expect(Setting.value('myCustom')).toBe('default');
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
await expectNotThrow(async () => Setting.setValue('myCustom', '123'));
|
|
|
|
|
|
|
|
expect(Setting.value('myCustom')).toBe('123');
|
|
|
|
}));
|
|
|
|
|
2024-05-28 14:42:52 +02:00
|
|
|
it.each([SettingStorage.Database, SettingStorage.File])('should not clear old custom settings if not registered immediately', (async (storage) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
// In general the following should work:
|
|
|
|
//
|
|
|
|
// - Plugin register a new setting
|
|
|
|
// - User set new value for setting
|
|
|
|
// - Settings are saved
|
|
|
|
// - => App restart
|
|
|
|
// - Plugin does not register setting again
|
|
|
|
// - Settings are loaded
|
|
|
|
// - Settings are saved
|
|
|
|
// - Plugin register setting again
|
|
|
|
// - Previous value set by user should still be there.
|
|
|
|
//
|
|
|
|
// In other words, once a custom setting has been set we don't clear it
|
|
|
|
// even if registration doesn't happen immediately. That allows for example
|
|
|
|
// to delay setting registration without a risk for the custom setting to be deleted.
|
|
|
|
|
|
|
|
await Setting.registerSetting('myCustom', {
|
|
|
|
public: true,
|
|
|
|
value: 'default',
|
|
|
|
type: Setting.TYPE_STRING,
|
2024-05-28 14:42:52 +02:00
|
|
|
storage,
|
2020-10-09 19:35:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Setting.setValue('myCustom', '123');
|
|
|
|
|
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
await Setting.load();
|
|
|
|
|
|
|
|
await Setting.registerSetting('myCustom', {
|
|
|
|
public: true,
|
|
|
|
value: 'default',
|
|
|
|
type: Setting.TYPE_STRING,
|
2024-05-28 14:42:52 +02:00
|
|
|
storage,
|
2020-10-09 19:35:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
expect(Setting.value('myCustom')).toBe('123');
|
|
|
|
}));
|
|
|
|
|
2024-05-28 14:42:52 +02:00
|
|
|
it.each([SettingStorage.Database, SettingStorage.File])('should not clear old custom settings if not registered until restart', async (storage) => {
|
|
|
|
const registerCustom = async () => {
|
|
|
|
await Setting.registerSetting('myCustom', {
|
|
|
|
public: true,
|
|
|
|
value: 'test',
|
|
|
|
type: Setting.TYPE_STRING,
|
|
|
|
storage,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
await registerCustom();
|
|
|
|
Setting.setValue('myCustom', 'test2');
|
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
await Setting.reset();
|
|
|
|
await Setting.load();
|
|
|
|
|
|
|
|
// Change a file setting
|
|
|
|
Setting.setValue('sync.target', 9);
|
|
|
|
|
|
|
|
await Setting.saveAll();
|
|
|
|
await Setting.reset();
|
|
|
|
await Setting.load();
|
|
|
|
|
|
|
|
await registerCustom();
|
|
|
|
expect(Setting.value('myCustom')).toBe('test2');
|
|
|
|
});
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should return values with correct type for custom settings', (async () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
await Setting.registerSetting('myCustom', {
|
|
|
|
public: true,
|
|
|
|
value: 123,
|
|
|
|
type: Setting.TYPE_INT,
|
|
|
|
});
|
|
|
|
|
|
|
|
Setting.setValue('myCustom', 456);
|
|
|
|
|
|
|
|
await Setting.saveAll();
|
|
|
|
await Setting.reset();
|
|
|
|
await Setting.load();
|
|
|
|
|
|
|
|
await Setting.registerSetting('myCustom', {
|
|
|
|
public: true,
|
|
|
|
value: 123,
|
|
|
|
type: Setting.TYPE_INT,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(typeof Setting.value('myCustom')).toBe('number');
|
|
|
|
expect(Setting.value('myCustom')).toBe(456);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should validate registered keys', (async () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
const md = {
|
|
|
|
public: true,
|
|
|
|
value: 'default',
|
|
|
|
type: Setting.TYPE_STRING,
|
|
|
|
};
|
|
|
|
|
|
|
|
await expectThrow(async () => await Setting.registerSetting('', md));
|
|
|
|
await expectThrow(async () => await Setting.registerSetting('no spaces', md));
|
|
|
|
await expectThrow(async () => await Setting.registerSetting('nospecialcharacters!!!', md));
|
|
|
|
await expectThrow(async () => await Setting.registerSetting('Robert\'); DROP TABLE Students;--', md));
|
|
|
|
|
|
|
|
await expectNotThrow(async () => await Setting.registerSetting('numbersareok123', md));
|
|
|
|
await expectNotThrow(async () => await Setting.registerSetting('so-ARE-dashes_123', md));
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should register new sections', (async () => {
|
2021-01-22 00:40:14 +02:00
|
|
|
await Setting.registerSection('mySection', SettingSectionSource.Default, {
|
2020-10-09 19:35:46 +02:00
|
|
|
label: 'My section',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(Setting.sectionNameToLabel('mySection')).toBe('My section');
|
|
|
|
}));
|
2021-02-09 19:54:29 +02:00
|
|
|
|
|
|
|
it('should save and load settings from file', (async () => {
|
|
|
|
Setting.setValue('sync.target', 9); // Saved to file
|
|
|
|
Setting.setValue('encryption.passwordCache', {}); // Saved to keychain or db
|
2024-06-25 15:01:39 +02:00
|
|
|
Setting.setValue('plugins.states', { test: defaultPluginSetting() }); // Always saved to db
|
2021-02-09 19:54:29 +02:00
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
{
|
|
|
|
const settings = await loadSettingsFromFile();
|
|
|
|
expect(settings['sync.target']).toBe(9);
|
|
|
|
expect(settings).not.toContain('encryption.passwordCache');
|
|
|
|
expect(settings).not.toContain('plugins.states');
|
|
|
|
}
|
|
|
|
|
|
|
|
Setting.setValue('sync.target', 8);
|
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
{
|
|
|
|
const settings = await loadSettingsFromFile();
|
|
|
|
expect(settings['sync.target']).toBe(8);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not save to file if nothing has changed', (async () => {
|
2021-09-25 20:51:44 +02:00
|
|
|
Setting.setValue('sync.mobileWifiOnly', true);
|
2021-02-09 19:54:29 +02:00
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
{
|
|
|
|
// Double-check that timestamp is indeed changed when the content is
|
|
|
|
// changed.
|
2022-04-11 17:49:32 +02:00
|
|
|
const beforeStat = await stat(Setting.settingFilePath);
|
2021-02-09 19:54:29 +02:00
|
|
|
await msleep(1001);
|
2021-09-25 20:51:44 +02:00
|
|
|
Setting.setValue('sync.mobileWifiOnly', false);
|
2021-02-09 19:54:29 +02:00
|
|
|
await Setting.saveAll();
|
2022-04-11 17:49:32 +02:00
|
|
|
const afterStat = await stat(Setting.settingFilePath);
|
2021-02-09 19:54:29 +02:00
|
|
|
expect(afterStat.mtime.getTime()).toBeGreaterThan(beforeStat.mtime.getTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-04-11 17:49:32 +02:00
|
|
|
const beforeStat = await stat(Setting.settingFilePath);
|
2021-02-09 19:54:29 +02:00
|
|
|
await msleep(1001);
|
2021-09-25 20:51:44 +02:00
|
|
|
Setting.setValue('sync.mobileWifiOnly', false);
|
2022-04-11 17:49:32 +02:00
|
|
|
const afterStat = await stat(Setting.settingFilePath);
|
2021-02-09 19:54:29 +02:00
|
|
|
await Setting.saveAll();
|
|
|
|
expect(afterStat.mtime.getTime()).toBe(beforeStat.mtime.getTime());
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle invalid JSON', (async () => {
|
|
|
|
const badContent = '{ oopsIforgotTheQuotes: true}';
|
2022-04-11 17:49:32 +02:00
|
|
|
await writeFile(Setting.settingFilePath, badContent, 'utf8');
|
2021-02-09 19:54:29 +02:00
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
Logger.globalLogger.enabled = false;
|
|
|
|
await Setting.load();
|
|
|
|
Logger.globalLogger.enabled = true;
|
|
|
|
|
|
|
|
// Invalid JSON file has been moved to .bak file
|
2022-04-11 17:49:32 +02:00
|
|
|
expect(await pathExists(Setting.settingFilePath)).toBe(false);
|
2021-02-09 19:54:29 +02:00
|
|
|
|
2022-04-11 17:49:32 +02:00
|
|
|
const files = await readdir(Setting.value('profileDir'));
|
2021-02-09 19:54:29 +02:00
|
|
|
expect(files.length).toBe(1);
|
|
|
|
expect(files[0].endsWith('.bak')).toBe(true);
|
2022-04-11 17:49:32 +02:00
|
|
|
expect(await readFile(`${Setting.value('profileDir')}/${files[0]}`, 'utf8')).toBe(badContent);
|
2021-02-09 19:54:29 +02:00
|
|
|
}));
|
|
|
|
|
2021-10-18 13:37:25 +02:00
|
|
|
it('should allow applying default migrations', (async () => {
|
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
expect(Setting.value('sync.target')).toBe(0);
|
|
|
|
expect(Setting.value('style.editor.contentMaxWidth')).toBe(0);
|
|
|
|
Setting.applyDefaultMigrations();
|
|
|
|
expect(Setting.value('sync.target')).toBe(7); // Changed
|
|
|
|
expect(Setting.value('style.editor.contentMaxWidth')).toBe(600); // Changed
|
|
|
|
}));
|
|
|
|
|
2021-10-28 23:25:27 +02:00
|
|
|
it('should skip values that are already set', (async () => {
|
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
Setting.setValue('sync.target', 9);
|
|
|
|
Setting.applyDefaultMigrations();
|
|
|
|
expect(Setting.value('sync.target')).toBe(9); // Not changed
|
|
|
|
}));
|
|
|
|
|
2021-10-18 13:37:25 +02:00
|
|
|
it('should allow skipping default migrations', (async () => {
|
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
expect(Setting.value('sync.target')).toBe(0);
|
|
|
|
expect(Setting.value('style.editor.contentMaxWidth')).toBe(0);
|
|
|
|
Setting.skipDefaultMigrations();
|
|
|
|
Setting.applyDefaultMigrations();
|
|
|
|
expect(Setting.value('sync.target')).toBe(0); // Not changed
|
|
|
|
expect(Setting.value('style.editor.contentMaxWidth')).toBe(0); // Not changed
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not apply migrations that have already been applied', (async () => {
|
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
Setting.setValue('lastSettingDefaultMigration', 0);
|
|
|
|
expect(Setting.value('sync.target')).toBe(0);
|
|
|
|
expect(Setting.value('style.editor.contentMaxWidth')).toBe(0);
|
|
|
|
Setting.applyDefaultMigrations();
|
|
|
|
expect(Setting.value('sync.target')).toBe(0); // Not changed
|
|
|
|
expect(Setting.value('style.editor.contentMaxWidth')).toBe(600); // Changed
|
|
|
|
}));
|
|
|
|
|
2022-08-27 13:05:44 +02:00
|
|
|
it('should migrate to new setting', (async () => {
|
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
Setting.setValue('spellChecker.language', 'fr-FR');
|
|
|
|
Setting.applyUserSettingMigration();
|
|
|
|
expect(Setting.value('spellChecker.languages')).toStrictEqual(['fr-FR']);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not override new setting, if it already set', (async () => {
|
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
Setting.setValue('spellChecker.languages', ['fr-FR', 'en-US']);
|
|
|
|
Setting.setValue('spellChecker.language', 'fr-FR');
|
|
|
|
Setting.applyUserSettingMigration();
|
|
|
|
expect(Setting.value('spellChecker.languages')).toStrictEqual(['fr-FR', 'en-US']);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not set new setting, if old setting is not set', (async () => {
|
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
expect(Setting.isSet('spellChecker.language')).toBe(false);
|
|
|
|
Setting.applyUserSettingMigration();
|
|
|
|
expect(Setting.isSet('spellChecker.languages')).toBe(false);
|
|
|
|
}));
|
|
|
|
|
2023-05-08 17:45:18 +02:00
|
|
|
it('should load sub-profile settings', async () => {
|
2022-04-11 17:49:32 +02:00
|
|
|
await Setting.reset();
|
|
|
|
|
2023-05-08 17:45:18 +02:00
|
|
|
await Setting.registerSetting('non_builtin', {
|
|
|
|
public: true,
|
|
|
|
storage: SettingStorage.File,
|
|
|
|
isGlobal: true,
|
|
|
|
type: SettingItemType.Bool,
|
|
|
|
value: false,
|
|
|
|
});
|
|
|
|
|
2022-04-11 17:49:32 +02:00
|
|
|
Setting.setValue('locale', 'fr_FR'); // Global setting
|
|
|
|
Setting.setValue('theme', Setting.THEME_DARK); // Global setting
|
|
|
|
Setting.setValue('sync.target', 9); // Local setting
|
2023-05-08 17:45:18 +02:00
|
|
|
Setting.setValue('non_builtin', true); // Local setting
|
2022-04-11 17:49:32 +02:00
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
await switchToSubProfileSettings();
|
|
|
|
|
|
|
|
expect(Setting.value('locale')).toBe('fr_FR'); // Should come from the root profile
|
|
|
|
expect(Setting.value('theme')).toBe(Setting.THEME_DARK); // Should come from the root profile
|
|
|
|
expect(Setting.value('sync.target')).toBe(0); // Should come from the local profile
|
|
|
|
|
2023-05-08 17:45:18 +02:00
|
|
|
// Non-built-in variables are not copied
|
|
|
|
expect(() => Setting.value('non_builtin')).toThrow();
|
|
|
|
|
2022-04-11 17:49:32 +02:00
|
|
|
// Also check that the special loadOne() function works as expected
|
|
|
|
|
|
|
|
expect((await Setting.loadOne('locale')).value).toBe('fr_FR');
|
|
|
|
expect((await Setting.loadOne('theme')).value).toBe(Setting.THEME_DARK);
|
2024-05-02 16:00:12 +02:00
|
|
|
expect((await Setting.loadOne('sync.target'))).toBe(null);
|
2022-04-11 17:49:32 +02:00
|
|
|
});
|
|
|
|
|
2023-05-08 17:45:18 +02:00
|
|
|
it('should save sub-profile settings', async () => {
|
2022-04-11 17:49:32 +02:00
|
|
|
await Setting.reset();
|
|
|
|
Setting.setValue('locale', 'fr_FR'); // Global setting
|
|
|
|
Setting.setValue('theme', Setting.THEME_DARK); // Global setting
|
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
await switchToSubProfileSettings();
|
|
|
|
|
|
|
|
Setting.setValue('locale', 'en_GB'); // Should be saved to global
|
|
|
|
Setting.setValue('sync.target', 8); // Should be saved to local
|
|
|
|
|
|
|
|
await Setting.saveAll();
|
|
|
|
await Setting.reset();
|
|
|
|
await Setting.load();
|
|
|
|
|
|
|
|
expect(Setting.value('locale')).toBe('en_GB');
|
|
|
|
expect(Setting.value('theme')).toBe(Setting.THEME_DARK);
|
|
|
|
expect(Setting.value('sync.target')).toBe(8);
|
|
|
|
|
|
|
|
// Double-check that actual file content is correct
|
|
|
|
|
|
|
|
const globalSettings = JSON.parse(await readFile(`${Setting.value('rootProfileDir')}/settings-1.json`, 'utf8'));
|
|
|
|
const localSettings = JSON.parse(await readFile(`${Setting.value('profileDir')}/settings-1.json`, 'utf8'));
|
|
|
|
|
|
|
|
expect(globalSettings).toEqual({
|
|
|
|
'$schema': 'https://joplinapp.org/schema/settings.json',
|
|
|
|
locale: 'en_GB',
|
|
|
|
theme: 2,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(localSettings).toEqual({
|
|
|
|
'$schema': 'https://joplinapp.org/schema/settings.json',
|
|
|
|
'sync.target': 8,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-05 19:15:10 +02:00
|
|
|
it('should not erase settings of parent profile', async () => {
|
|
|
|
// When a sub-profile settings are saved, we should ensure that the
|
|
|
|
// local settings of the root profiles are not lost.
|
|
|
|
// https://github.com/laurent22/joplin/issues/6459
|
|
|
|
|
|
|
|
await Setting.reset();
|
|
|
|
|
|
|
|
Setting.setValue('sync.target', 9); // Local setting (Root profile)
|
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
await switchToSubProfileSettings();
|
|
|
|
|
|
|
|
Setting.setValue('sync.target', 2); // Local setting (Sub-profile)
|
|
|
|
await Setting.saveAll();
|
|
|
|
|
|
|
|
const globalSettings = JSON.parse(await readFile(`${Setting.value('rootProfileDir')}/settings-1.json`, 'utf8'));
|
|
|
|
expect(globalSettings['sync.target']).toBe(9);
|
|
|
|
});
|
|
|
|
|
2022-04-11 17:49:32 +02:00
|
|
|
it('all global settings should be saved to file', async () => {
|
|
|
|
for (const [k, v] of Object.entries(Setting.metadata())) {
|
|
|
|
if (v.isGlobal && v.storage !== SettingStorage.File) throw new Error(`Setting "${k}" is global but storage is not "file"`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-17 17:34:04 +02:00
|
|
|
test('values should not be undefined when they are set', async () => {
|
|
|
|
Setting.setValue('locale', undefined);
|
|
|
|
expect(Setting.value('locale')).toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('values should not be undefined when registering a setting', async () => {
|
|
|
|
await Setting.registerSetting('myCustom', {
|
|
|
|
public: true,
|
|
|
|
value: undefined,
|
|
|
|
type: Setting.TYPE_STRING,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(Setting.value('myCustom')).toBe('');
|
|
|
|
});
|
|
|
|
|
2024-03-09 13:03:57 +02:00
|
|
|
test('should not fail Sqlite UNIQUE constraint when re-registering saved settings', async () => {
|
|
|
|
// Re-registering a saved database setting previously caused issues with saving.
|
|
|
|
for (let i = 0; i < 2; i++) {
|
|
|
|
await Setting.registerSetting('myCustom', {
|
|
|
|
public: true,
|
|
|
|
value: `${i}`,
|
|
|
|
type: Setting.TYPE_STRING,
|
|
|
|
storage: SettingStorage.Database,
|
|
|
|
});
|
|
|
|
Setting.setValue('myCustom', 'test');
|
|
|
|
await Setting.saveAll();
|
|
|
|
}
|
|
|
|
});
|
2020-10-09 19:35:46 +02:00
|
|
|
});
|