// Character theme typography: CSS families and the fonts Chromium renders for UI, prose, headings, and code, plus narrow settings. import assert from 'node:assert/strict'; import { writeFile } from 'node:fs/promises'; import path from 'node:path'; import { mockTauri, openPage, outputDir, runSmoke, settle, smokeUrl } from '../_harness/index.mjs'; await runSmoke('appearance/typography', async ({ browser }) => { const output = await outputDir('appearance/typography'); const report = []; const context = await browser.newContext({ viewport: { width: 1100, height: 780 } }); await mockTauri(context, { commands: test => ({ ...test.workspace({ root: 'C:\\font-smoke', files: { 'Typography.md': '# A place for your ideas\n\nRead the next chapter, collect clear notes, and keep your work close. Small letters, open spaces, and steady lines help each sentence stay readable.\n\n## Details worth keeping\n\nThe quick brown fox jumps over the lazy dog. Il1 O0 rn m, 0123456789.\n\n`const name = "RuneStone";`\n' }, }), plugins_snapshot: () => ({ root: 'C:/font-smoke-plugins', plugins: [] }), }), }); const main = await openPage(context); const settings = await openPage(context); await main.goto(smokeUrl); await main.getByText('font-smoke', { exact: true }).waitFor(); await main.keyboard.press('Control+p'); await main.getByRole('textbox', { name: 'Find a file' }).fill('Typography.md'); await main.getByRole('textbox', { name: 'Find a file' }).press('Enter'); await main.getByRole('textbox', { name: 'Writing editor', exact: true }).waitFor(); await settings.goto(`${smokeUrl}/?settingsWindow=true`); await settings.getByRole('button', { name: 'Appearance', exact: true }).click(); const sessions = new Map(); // The CSS font of `selector` and the platform fonts Chromium used to draw its glyphs. async function rendered(page, selector) { await settle(page); let session = sessions.get(page); if (!session) { session = await context.newCDPSession(page); await session.send('DOM.enable'); await session.send('CSS.enable'); sessions.set(page, session); } const { root } = await session.send('DOM.getDocument'); const { nodeId } = await session.send('DOM.querySelector', { nodeId: root.nodeId, selector }); assert(nodeId, selector); const { fonts } = await session.send('CSS.getPlatformFontsForNode', { nodeId }); const style = await page.locator(selector).first().evaluate(node => { const css = getComputedStyle(node); return { family: css.fontFamily, size: css.fontSize, weight: css.fontWeight, lineHeight: css.lineHeight }; }); return { ...style, rendered: fonts.filter(font => font.glyphCount).map(({ familyName, postScriptName, glyphCount, isCustomFont }) => ({ familyName, postScriptName, glyphCount, isCustomFont })) }; } const setPluginTools = count => settings.evaluate(async count => { const { pluginTools } = await import(performance.getEntriesByType('resource').find(entry => new URL(entry.name).pathname === '/src/lib/plugins/external.ts').name); pluginTools.set(Array.from({ length: count }, (_, index) => ({ owner: 'org.example.font-check', id: `settings-${index}`, title: `Plugin settings ${index + 1}`, surface: 'settings' }))); }, count); const selectors = { ui: '.theme-actions button', prose: '.cm-line:not(.writer-heading):not(.writer-blank)', heading: '.writer-h1' }; const cases = [ ['harvest-moon', 'Harvest Moon', 'Constantia', 'Candara', 'Constantia', 'Constantia'], ['crimson-pilot', 'Crimson Pilot', 'Bahnschrift', 'Bahnschrift', 'Segoe UI', 'Bahnschrift'], ['silver-serpent', 'Silver Serpent', 'Palatino Linotype', 'Corbel', 'Palatino Linotype', 'Palatino Linotype'], ]; for (const [theme, label, oldFont, ui, prose, heading] of cases) { await settings.setViewportSize({ width: 1100, height: 780 }); await settings.getByRole('radio', { name: label, exact: true }).click(); await main.waitForFunction(theme => document.documentElement.dataset.palette === theme, theme); await main.getByRole('textbox', { name: 'Writing editor', exact: true }).press('Control+Home'); await main.evaluate(() => { getSelection().removeAllRanges(); document.activeElement?.blur(); }); // The theme sets its fonts as inline properties of the root. Put back those values after each temporary change. for (const page of [main, settings]) { const saved = await page.evaluate(font => ['ui', 'prose', 'heading'].map(role => { const value = document.documentElement.style.getPropertyValue(`--rs-font-${role}`); document.documentElement.style.setProperty(`--rs-font-${role}`, `"${font}"`); return value; }), oldFont); await page.screenshot({ path: path.join(output, `${theme}-before-${page === main ? 'document' : 'settings'}.png`) }); await page.evaluate(saved => { ['ui', 'prose', 'heading'].forEach((role, index) => document.documentElement.style.setProperty(`--rs-font-${role}`, saved[index])); }, saved); } const roles = { ui: await rendered(settings, selectors.ui), prose: await rendered(main, selectors.prose), heading: await rendered(main, selectors.heading), mono: await rendered(settings, '.preview-code'), }; for (const [role, family] of Object.entries({ ui, prose, heading })) { assert(roles[role].family.includes(family), `${theme}: ${role} CSS family`); assert(roles[role].rendered.some(font => font.familyName === family), `${theme}: ${role} actually renders ${family}: ${JSON.stringify(roles[role])}`); } assert.equal(roles.ui.size, '13px'); assert.equal(roles.prose.size, '15px'); assert(roles.mono.family.includes('Cascadia Mono')); assert(roles.mono.rendered.some(font => font.familyName === 'Cascadia Mono')); await settings.screenshot({ path: path.join(output, `${theme}-after-settings.png`) }); await main.screenshot({ path: path.join(output, `${theme}-after-document.png`) }); await main.getByRole('textbox', { name: 'Writing editor', exact: true }).evaluate(node => { node.focus(); const paragraph = [...node.querySelectorAll('.cm-line')].find(line => line.textContent.includes('Read the next chapter')); const range = document.createRange(); range.selectNodeContents(paragraph); const selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); }); assert((await main.evaluate(() => getSelection().toString())).includes('Read the next chapter')); await main.screenshot({ path: path.join(output, `${theme}-selected-prose.png`) }); await settings.setViewportSize({ width: 360, height: 480 }); const content = settings.locator('dialog .content'); assert(await content.evaluate(node => node.scrollWidth <= node.clientWidth), `${theme}: narrow overflow`); // The current theme card stays reachable from the keyboard in a narrow window. const current = settings.getByRole('radio', { name: label, exact: true }); await current.scrollIntoViewIfNeeded(); await current.focus(); assert.equal(await current.getAttribute('aria-checked'), 'true'); assert(await current.evaluate(node => node === document.activeElement)); await settings.screenshot({ path: path.join(output, `${theme}-narrow-cards.png`) }); await setPluginTools(16); const bodyBounds = await settings.locator('dialog .body').boundingBox(); assert((await settings.locator('dialog aside').boundingBox()).height <= bodyBounds.height * .4 + 1); await settings.getByRole('button', { name: 'Plugin settings 16', exact: true }).scrollIntoViewIfNeeded(); assert(await content.evaluate(node => node.scrollWidth <= node.clientWidth)); await settings.screenshot({ path: path.join(output, `${theme}-narrow-long-plugins.png`) }); await setPluginTools(0); // Drop the first family from each stack and check that the fallback still renders glyphs. const fallback = {}; for (const [role, page] of [['ui', settings], ['prose', main], ['heading', main]]) { const stack = await page.evaluate(role => { const stack = getComputedStyle(document.documentElement).getPropertyValue(`--rs-font-${role}`); document.documentElement.style.setProperty(`--rs-font-${role}`, stack.slice(stack.indexOf(',') + 1)); return stack; }, role); fallback[role] = await rendered(page, selectors[role]); assert(fallback[role].rendered.length > 0); await page.evaluate(([role, stack]) => document.documentElement.style.setProperty(`--rs-font-${role}`, stack), [role, stack]); } report.push({ theme, oldFont, roles, fallback }); console.log(`PASS ${theme}: real UI and prose sizes, rendered fonts, shared monospace, selection, narrow settings, keyboard focus, long plugin navigation`); } assert.deepEqual([...main.errors, ...settings.errors], []); await writeFile(path.join(output, 'rendered-fonts.json'), JSON.stringify(report, null, 2)); });