Keep the user preferences in settings.json in the app config folder. WebView storage can be lost, and the host cannot read it. The file has a schema number, a revision number, and one value for each section: general, appearance, customThemes, customThemesLegacy, and language. - settings_read gives all sections, the revision, and a one-time notice. - settings_write replaces one section, writes the file atomically, and sends settings-changed to all windows. The limit is 8 MiB. - settings_migrate writes the first file only when no file exists. - The host moves a damaged file to settings.corrupt-<seconds>.json, starts from the defaults, and reports this one time. Each window loads the sections before its first render. One store in src/lib/settings/store.ts replaces the storage listeners. When no file exists, the window normalizes the old localStorage values and migrates them. The old keys stay in place for one release. The window layout and the open tabs stay in localStorage on purpose. The browser preview keeps all preferences in localStorage. Keep tab transfer offers in host memory, not in localStorage, because an offer can hold unsaved text. A window takes an offer once. The host checks the workspace root and a 120 second expiry. When the receiver accepts the tab, the host sends tab-transfer-accepted to the source window only. A refused offer stays open, and the source tab stays open. A new window and a drop on the tab ribbon use one check. The smoke harness serves the new commands from one shared state for each browser context. Tests read saved values from the mock settings file. Rust, unit, and smoke tests cover the changes.
70 lines
3.6 KiB
TypeScript
70 lines
3.6 KiB
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { migrateLegacyPreferences } from '../../../src/lib/settings/migration.ts';
|
|
import { LEGACY_KEYS } from '../../../src/lib/settings/store.ts';
|
|
|
|
const palette = { bg: '#123', text: '#abcdef', accent: '#AABBCC' };
|
|
const reader = (values: Record<string, string>) => (key: string) => Object.hasOwn(values, key) ? values[key] : null;
|
|
|
|
test('Migration reads the four old keys and the theme backup, and normalizes each value', () => {
|
|
const custom = { schema: 2, id: 'night', name: 'Night', colors: { ...palette, bg: '#000000' } };
|
|
const sections = migrateLegacyPreferences(reader({
|
|
'runestone.settings': JSON.stringify({ autosaveEnabled: false, autosaveSeconds: 900, extra: true }),
|
|
'runestone.appearance': JSON.stringify({ theme: 'custom:night', mode: 'light', transparency: { amount: 140 } }),
|
|
'runestone.custom-themes': JSON.stringify([custom, { schema: 2, id: '../bad' }]),
|
|
'runestone.custom-themes.legacy': JSON.stringify([{ schema: 1, id: 'kept' }]),
|
|
'runestone.language': 'fr',
|
|
}));
|
|
assert.deepEqual(sections, {
|
|
general: { autosaveEnabled: false, autosaveSeconds: 5, showInternalFolder: false },
|
|
language: 'fr',
|
|
customThemes: [custom],
|
|
customThemesLegacy: [{ schema: 1, id: 'kept' }],
|
|
// The mode comes from the migrated custom theme, not from a store that is still empty.
|
|
appearance: { theme: 'custom:night', mode: 'dark', transparency: { enabled: true, amount: 100 } },
|
|
});
|
|
});
|
|
|
|
test('Migration of version 1 themes keeps the saved palette and adds the originals to the backup once', () => {
|
|
const legacy = { schema: 1, id: 'old', name: 'Old', light: { ...palette, bg: '#fff' }, dark: palette };
|
|
const sections = migrateLegacyPreferences(reader({
|
|
'runestone.appearance': JSON.stringify({ theme: 'custom:old', mode: 'light' }),
|
|
'runestone.custom-themes': JSON.stringify([legacy]),
|
|
'runestone.custom-themes.legacy': JSON.stringify([legacy]),
|
|
}));
|
|
assert.deepEqual(sections.customThemes, [{ schema: 2, id: 'old', name: 'Old', colors: legacy.light }]);
|
|
assert.deepEqual(sections.customThemesLegacy, [legacy]);
|
|
assert.deepEqual(sections.appearance, { theme: 'custom:old', mode: 'light' });
|
|
});
|
|
|
|
test('Missing keys give no sections, so the defaults apply', () => {
|
|
assert.deepEqual(migrateLegacyPreferences(() => null), {});
|
|
assert.deepEqual(migrateLegacyPreferences(() => { throw new Error('Storage unavailable'); }), {});
|
|
});
|
|
|
|
test('Damaged old values become defaults and unknown languages become English', () => {
|
|
const sections = migrateLegacyPreferences(reader({
|
|
'runestone.settings': '{broken',
|
|
'runestone.appearance': '[1, 2]',
|
|
'runestone.custom-themes': '{"not":"a list"}',
|
|
'runestone.language': 'de',
|
|
}));
|
|
assert.deepEqual(sections, {
|
|
general: { autosaveEnabled: true, autosaveSeconds: 5, showInternalFolder: false },
|
|
language: 'en',
|
|
appearance: { theme: 'default', mode: 'dark' },
|
|
});
|
|
});
|
|
|
|
test('Migration keeps renamed theme IDs and a background image data URL', () => {
|
|
const image = 'data:image/png;base64,' + 'A'.repeat(40);
|
|
const sections = migrateLegacyPreferences(reader({
|
|
'runestone.appearance': JSON.stringify({ theme: 'holo', mode: 'dark', newTabBackground: { image, overlay: 0.4 } }),
|
|
}));
|
|
assert.deepEqual(sections.appearance, { theme: 'harvest-moon', mode: 'dark', newTabBackground: { image, overlay: 0.4, position: 'center', size: 'cover' } });
|
|
});
|
|
|
|
test('Every section has one old key', () => {
|
|
assert.deepEqual(Object.values(LEGACY_KEYS).sort(), ['runestone.appearance', 'runestone.custom-themes', 'runestone.custom-themes.legacy', 'runestone.language', 'runestone.settings']);
|
|
});
|