// Theme contract (no Playwright colour emulation: colorScheme null, the app's own nativeTheme decides): // a new profile is dark from the first frame (window background + onboarding), light chosen in Settings // persists across restart, and a stored legacy choice ('system') is kept untouched. // node scripts/smoke-theme.mjs --out docs/evidence/runtime/ [--exe packaged.exe] import { launchApp } from './lib/electron-app.mjs'; import { mkdirSync, writeFileSync, mkdtempSync, rmSync, readFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { execFileSync } from 'node:child_process'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const argv = process.argv.slice(2); const opt = (n, d) => (argv.indexOf(`--${n}`) >= 0 ? argv[argv.indexOf(`--${n}`) + 1] : d); const out = path.resolve(root, opt('out', 'smoke-out')); const exe = opt('exe'); mkdirSync(out, { recursive: true }); const result = { steps: [] }; const step = (name, data = {}) => { result.steps.push({ name, ...data }); console.log(`[theme] ${name}`, JSON.stringify(data)); }; function osTheme() { if (process.platform !== 'win32') return 'unknown'; try { const q = execFileSync('reg', ['query', 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize', '/v', 'AppsUseLightTheme'], { encoding: 'utf8' }); return /0x1\b/.test(q) ? 'light' : 'dark'; } catch { return 'unknown'; } } async function launch(profile) { const app = await launchApp({ exe, profile }); const page = await app.firstWindow(); await page.setViewportSize({ width: 1280, height: 800 }).catch(() => {}); return { app, page }; } async function look(app, page) { const [windowBackground, themeSource] = await app.evaluate(({ BrowserWindow, nativeTheme }) => [BrowserWindow.getAllWindows()[0].getBackgroundColor(), nativeTheme.themeSource]); return page.evaluate(([bg, source]) => ({ themeSource: source, windowBackground: bg, prefersDark: matchMedia('(prefers-color-scheme: dark)').matches, bodyBackground: getComputedStyle(document.body).backgroundColor, bg: getComputedStyle(document.documentElement).getPropertyValue('--bg').trim(), }), [windowBackground, themeSource]); } const profiles = []; const temp = () => { const p = mkdtempSync(path.join(tmpdir(), 'teamspeach-theme-')); profiles.push(p); return p; }; let app; let failed = null; try { step('os app theme', { appsUseTheme: osTheme() }); const fresh = temp(); let page; ({ app, page } = await launch(fresh)); await page.waitForSelector('#view-onboarding', { state: 'visible', timeout: 20000 }); let l = await look(app, page); step('new profile: onboarding', l); await page.screenshot({ path: path.join(out, '70-new-profile-onboarding-dark.png') }); if (!l.prefersDark) throw new Error('new profile is not dark'); // The onboarding mark is the application icon asset, actually decoded. await page.waitForFunction(() => document.querySelector('#view-onboarding .brand-mark')?.complete); const mark = await page.$eval('#view-onboarding .brand-mark', (img) => ({ src: new URL(img.currentSrc).pathname, naturalWidth: img.naturalWidth, naturalHeight: img.naturalHeight, shownWidth: img.getBoundingClientRect().width, })); step('onboarding brand mark', mark); if (mark.src !== '/assets/icon.png' || mark.naturalWidth !== 256 || mark.naturalHeight !== 256 || mark.shownWidth !== 56) throw new Error('brand mark is not the loaded app icon'); await page.fill('#onboarding-nick', 'ThemeSmoke'); await page.click('#onboarding-submit'); await page.waitForSelector('#view-app', { state: 'visible', timeout: 20000 }); await page.click('#btn-settings'); await page.click('#settings-nav .nav-item[data-panel="appearance"]'); const checked = await page.$eval('input[name="theme"]:checked', (el) => el.value); step('settings shows dark selected', { checked }); if (checked !== 'dark') throw new Error('settings radio not dark'); await page.check('input[name="theme"][value="light"]'); await page.waitForTimeout(300); l = await look(app, page); step('switched to light', l); if (l.prefersDark) throw new Error('light not applied'); await app.close(); ({ app, page } = await launch(fresh)); await page.waitForSelector('#view-app', { state: 'visible', timeout: 20000 }); l = await look(app, page); const stored = JSON.parse(readFileSync(path.join(fresh, 'prefs.json'), 'utf8')).theme; step('restart keeps light', { ...l, stored }); await page.screenshot({ path: path.join(out, '71-light-after-restart.png') }); if (l.prefersDark || stored !== 'light') throw new Error('light not persisted'); await app.close(); // A profile from an earlier version with an explicit stored theme keeps it. const legacy = temp(); writeFileSync(path.join(legacy, 'prefs.json'), JSON.stringify({ theme: 'system', voiceVolume: 100 })); ({ app, page } = await launch(legacy)); await page.waitForSelector('#view-onboarding', { state: 'visible', timeout: 20000 }); const legacyStored = JSON.parse(readFileSync(path.join(legacy, 'prefs.json'), 'utf8')).theme; step('stored "system" kept', { legacyStored, prefersDark: (await look(app, page)).prefersDark }); if (legacyStored !== 'system') throw new Error('stored theme rewritten'); } catch (err) { failed = err; step('FAILED', { error: String(err.stack || err) }); } finally { if (app) await app.close().catch(() => {}); for (const p of profiles) rmSync(p, { recursive: true, force: true }); } result.ok = !failed; writeFileSync(path.join(out, 'result.json'), JSON.stringify(result, null, 2)); console.log(`[theme] ${result.ok ? 'OK' : 'FAILED'} → ${out}`); process.exit(result.ok ? 0 : 1);