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.
135 lines
10 KiB
JavaScript
135 lines
10 KiB
JavaScript
// New-tab background images: large uploads, cross-window sync, reload, theme reuse, removal, picker menus, and narrow settings.
|
|
import assert from 'node:assert/strict';
|
|
import { writeFile } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { mockTauri, openPage, outputDir, runSmoke, smokeUrl } from '../_harness/index.mjs';
|
|
|
|
// The image is painted on .workarea-background, not on the empty tab (src/lib/layout/Workbench.svelte:1258, 1543).
|
|
|
|
await runSmoke('appearance/backgroundImages', async ({ browser }) => {
|
|
const output = await outputDir('appearance/backgroundImages');
|
|
const context = await browser.newContext({ viewport: { width: 1100, height: 780 } });
|
|
await mockTauri(context, {
|
|
commands: test => {
|
|
const workspace = test.workspace({
|
|
root: 'C:\\host-todo-test',
|
|
files: {
|
|
'a.md': '# Note\n\nOriginal text.',
|
|
'.runestone/templates/Daily.md': '# Daily\n\n## Tasks',
|
|
'.runestone/marks.json': JSON.stringify({ pins: ['a.md'], bookmarks: ['a.md'], order: { '': ['a.md'] } }),
|
|
},
|
|
});
|
|
return {
|
|
...workspace,
|
|
workspace_list: args => workspace.workspace_list(args).filter(entry => entry.rel_path !== '.runestone'),
|
|
plugins_snapshot: () => ({ root: 'C:/plugins', plugins: [] }),
|
|
'plugin:window|is_maximized': () => false,
|
|
};
|
|
},
|
|
});
|
|
const main = await openPage(context), settings = await openPage(context);
|
|
const imageInput = () => settings.getByLabel('Background image file', { exact: true });
|
|
const background = () => main.locator('.workarea-background').evaluate(node => /url\("(blob:[^"]+)"\)/.exec(getComputedStyle(node).backgroundImage)?.[1]);
|
|
const decoded = (page, url) => page.evaluate(async url => { const image = new Image(); image.src = url; await image.decode(); return image.naturalWidth; }, url);
|
|
const savedImageId = () => settings.evaluate(() => window.__TEST__.settings('appearance').newTabBackground.imageId);
|
|
const picker = name => settings.locator('.picker').and(settings.getByRole('button', { name, exact: true }));
|
|
|
|
await main.goto(smokeUrl); await main.locator('.empty').waitFor();
|
|
await settings.goto(smokeUrl + '/?settingsWindow=true');
|
|
await settings.getByRole('button', { name: 'Appearance', exact: true }).click();
|
|
const image = await main.evaluate(() => {
|
|
const canvas = document.createElement('canvas'); canvas.width = canvas.height = 700;
|
|
const ctx = canvas.getContext('2d'), pixels = ctx.createImageData(700, 700);
|
|
let seed = 23;
|
|
for (let i = 0; i < pixels.data.length; i++) { seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; pixels.data[i] = i % 4 === 3 ? 255 : seed >>> 24; }
|
|
ctx.putImageData(pixels, 0, 0); return canvas.toDataURL();
|
|
});
|
|
const bytes = Buffer.from(image.split(',')[1], 'base64');
|
|
assert.ok(image.length > 2 ** 21 && bytes.length < 2_000_000, 'Fixture crosses the CSS limit but fits the upload limit');
|
|
await imageInput().setInputFiles({ name: 'large.png', mimeType: 'image/png', buffer: bytes });
|
|
await main.waitForFunction(() => getComputedStyle(document.querySelector('.workarea-background')).backgroundImage.includes('blob:'));
|
|
let url = await background();
|
|
assert.equal(await decoded(main, url), 700, 'The actual new-tab image decodes');
|
|
await main.reload(); await main.locator('.empty').waitFor();
|
|
url = await background(); assert.equal(await decoded(main, url), 700, 'The saved image survives reload');
|
|
|
|
const pixel = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jM1sAAAAASUVORK5CYII=', 'base64');
|
|
const largeFile = path.join(output, '50MiB.png');
|
|
await writeFile(largeFile, Buffer.concat([pixel, Buffer.alloc(50 * 1024 * 1024 - pixel.length)]));
|
|
await imageInput().setInputFiles(largeFile);
|
|
await main.waitForFunction(old => /url\("(blob:[^"]+)"\)/.exec(getComputedStyle(document.querySelector('.workarea-background')).backgroundImage)?.[1] !== old, url);
|
|
url = await background(); assert.equal(await decoded(main, url), 1, '50 MiB image decodes');
|
|
assert.ok(await settings.evaluate(() => JSON.stringify(window.__TEST__.settings('appearance')).length < 500), 'Image bytes stay outside the settings file');
|
|
await main.reload(); await main.locator('.empty').waitFor();
|
|
await main.waitForFunction(() => getComputedStyle(document.querySelector('.workarea-background')).backgroundImage.includes('blob:'));
|
|
url = await background(); assert.equal(await decoded(main, url), 1, '50 MiB image survives reload');
|
|
|
|
const acceptedId = await savedImageId();
|
|
const tooLargeFile = path.join(output, 'too-large.png');
|
|
await writeFile(tooLargeFile, Buffer.alloc(50 * 1024 * 1024 + 1));
|
|
await imageInput().setInputFiles(tooLargeFile);
|
|
await settings.getByRole('alert').filter({ hasText: '50 MiB' }).waitFor();
|
|
assert.equal(await savedImageId(), acceptedId);
|
|
assert.equal(await settings.locator('input[type=color]').count(), 0);
|
|
await settings.getByLabel('Image overlay', { exact: true }).fill('55');
|
|
await main.waitForFunction(() => getComputedStyle(document.querySelector('.workarea-background')).backgroundImage.includes('0.55'));
|
|
await settings.getByLabel('Image overlay', { exact: true }).fill('-40');
|
|
await main.waitForFunction(() => getComputedStyle(document.querySelector('.workarea-background')).backgroundImage.includes('255, 255, 255, 0.4'));
|
|
|
|
// The Color theme picker now chooses the theme and its light or dark mode together (src/lib/theme/AppearanceSettings.svelte:20, 113).
|
|
for (const mode of ['dark', 'light']) {
|
|
await picker('Color theme').click();
|
|
await settings.getByRole('menuitemradio', { name: mode === 'dark' ? 'RuneStone Dark' : 'RuneStone Light', exact: true }).click();
|
|
await main.waitForFunction(mode => document.documentElement.dataset.theme === mode, mode);
|
|
assert.equal(await background(), url, 'Theme changes reuse the current image URL');
|
|
await main.screenshot({ path: `${output}/background-${mode}.png` });
|
|
for (const name of ['Color theme']) {
|
|
await picker(name).click();
|
|
const menu = settings.getByRole('menu', { name, exact: true });
|
|
assert.equal(await menu.evaluate(node => getComputedStyle(node).fontSize), '13px');
|
|
// Shared menu rows are 24px high (src/lib/theme/tokens.css:31, --rs-menu-row-height).
|
|
assert.equal(await menu.locator(':scope > button').first().evaluate(node => getComputedStyle(node).minHeight), '24px');
|
|
assert.equal(await menu.locator('[aria-checked="true"] svg').count(), 0);
|
|
assert.equal(await menu.evaluate(node => { const reference = document.createElement('span'); reference.style.color = 'var(--rs-menu-border)'; node.append(reference); const matches = getComputedStyle(node).borderColor === getComputedStyle(reference).color; reference.remove(); return matches; }), true, 'Menu uses the shared border token');
|
|
assert.equal(await menu.evaluate(node => getComputedStyle(node).borderColor), 'rgba(0, 0, 0, 0)', 'Menus have no visible border');
|
|
await settings.screenshot({ path: `${output}/${name.replaceAll(' ', '-')}-${mode}.png` });
|
|
await settings.keyboard.press('Escape');
|
|
assert.equal(await picker(name).evaluate(node => node === document.activeElement), true);
|
|
}
|
|
}
|
|
|
|
await settings.setViewportSize({ width: 360, height: 480 });
|
|
await settings.getByRole('button', { name: 'Color theme', exact: true }).click();
|
|
const box = await settings.getByRole('menu', { name: 'Color theme', exact: true }).boundingBox();
|
|
assert.ok(box.x >= 0 && box.x + box.width <= 360 && box.y + box.height <= 480);
|
|
await settings.screenshot({ path: `${output}/menu-narrow.png` });
|
|
await settings.keyboard.press('Escape');
|
|
await settings.getByRole('button', { name: 'Remove image', exact: true }).click();
|
|
await main.waitForFunction(() => getComputedStyle(document.querySelector('.workarea-background')).backgroundImage === 'none');
|
|
assert.equal(await main.evaluate(async url => { try { await fetch(url); return false; } catch { return true; } }, url), true, 'Removing an image releases its URL');
|
|
assert.equal(await settings.evaluate(async id => (await import('/src/lib/theme/backgroundImages.ts')).readBackgroundImage(id).then(Boolean), acceptedId), false, 'Removal deletes stored image bytes');
|
|
|
|
await settings.evaluate(async () => {
|
|
const external = await import(performance.getEntriesByType('resource').find(entry => new URL(entry.name).pathname === '/src/lib/plugins/external.ts').name);
|
|
external.pluginTools.update(items => [...items, ...Array.from({ length: 30 }, (_, index) => ({ owner: `test.plugin${index}`, id: 'settings', title: `Plugin settings ${index}`, kind: 'view', surface: 'settings' }))]);
|
|
});
|
|
for (const theme of ['light', 'dark']) {
|
|
await settings.evaluate(async mode => { const { saveAppearance } = await import('/src/lib/theme/appearance.ts'); saveAppearance({ theme: 'default', mode }); window.dispatchEvent(new Event('focus')); }, theme);
|
|
await settings.setViewportSize({ width: 360, height: 600 });
|
|
assert.equal(await settings.getByRole('navigation', { name: 'Settings categories' }).getByRole('button').count() >= 34, true);
|
|
assert.equal(await settings.locator('.content').evaluate(node => node.clientHeight > 240 && node.clientWidth > 300), true, 'Long navigation leaves room for settings');
|
|
await settings.screenshot({ path: `${output}/long-plugin-list-${theme}-narrow.png` });
|
|
await main.setViewportSize({ width: 600, height: 700 });
|
|
await main.keyboard.press('F1');
|
|
await main.getByRole('dialog', { name: 'Help and about' }).waitFor();
|
|
assert.equal(await main.locator('.help-card details[open]').count(), 0);
|
|
await main.locator('.help-card summary').first().focus(); await main.keyboard.press('Enter');
|
|
assert.equal(await main.locator('.help-card details[open]').count(), 1);
|
|
await main.keyboard.press('Enter');
|
|
await main.screenshot({ path: `${output}/help-${theme}-narrow.png` });
|
|
await main.getByRole('button', { name: 'Close help', exact: true }).click();
|
|
}
|
|
assert.deepEqual([...main.errors, ...settings.errors], []);
|
|
console.log('PASS: large image upload, decoding, cross-window sync, reload, theme reuse, removal, shared menu dimensions, keyboard dismissal, and narrow viewport.');
|
|
});
|