// Dark mode (#80): Settings › Theme is Auto (the device's light or dark setting), Light or Dark, a choice kept on the // device (localStorage sot-theme), never in the save. The page's
sets before the first paint; // the panels follow through CSS custom properties, and the map paints its sea, land, clouds and labels in the night // colours in both renderers, painting its cached layers again once when the theme changes (not on every frame). // jsdom has no matchMedia: the tests lend the page one, before it is parsed (the harness's browser hook) or after. const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const { JSDOM } = require('jsdom'); const { load, browser, flush } = require('./harness'); const { assemble } = require('../scripts/lib/page'); // A device setting the test can flip: matchMedia('(prefers-color-scheme: dark)') with change listeners. const device = (dark) => { const q = { matches: dark, media: '(prefers-color-scheme: dark)', fns: [], addEventListener(t, f) { if (t === 'change') this.fns.push(f); }, removeEventListener(t, f) { this.fns = this.fns.filter((x) => x !== f); }, set(v) { this.matches = v; for (const f of this.fns.slice()) f({ matches: v }); } }; return { q, attach(w) { w.matchMedia = (m) => (/prefers-color-scheme:\s*dark/.test(m) ? q : { matches: false, addEventListener() {}, removeEventListener() {} }); } }; }; const meta = (doc, n) => doc.querySelector(`meta[name="${n}"]`).getAttribute('content'); // WCAG contrast between two #RRGGBB colours. const lum = (h) => { h = h.replace('#', ''); const c = [0, 2, 4].map((i) => parseInt(h.slice(i, i + 2), 16) / 255).map((v) => (v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4)); return 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]; }; const cr = (a, b) => { const x = lum(a), y = lum(b); return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05); }; // Only the page's , up to its styles, as the browser runs it before the first paint: the game script at the end // of (which sets the theme as well, but late) is not there. ls: what window.localStorage gives, or throws. const headOnly = (dark, ls) => { const html = assemble().html, head = html.slice(0, html.indexOf('')); const block = (sel) => { const i = css.indexOf(sel + '{'); assert.ok(i >= 0, sel); return css.slice(i, css.indexOf('}', i)); }; const dark = block(':root[data-theme="dark"]'), light = block(':root'); const val = (b, k) => { const m = new RegExp(`--${k}:\\s*([^;\\n]+);`).exec(b); return m && m[1].trim(); }; // Every var(--tint-…, light) and var(--tx-…, light) in the styles and the game's own markup has a dark value. const src = css + fs.readdirSync(path.join(__dirname, '..', 'src', 'game'), { recursive: true }).filter((f) => f.endsWith('.js')).map((f) => fs.readFileSync(path.join(__dirname, '..', 'src', 'game', f), 'utf8')).join('\n'); const used = new Set([...src.matchAll(/var\(--((?:tint|tx)-[a-z]+),\s*#[0-9A-Fa-f]{3,6}\)/g)].map((m) => m[1])); assert.ok(used.size >= 8, `tints in use (${[...used]})`); for (const k of used) assert.ok(val(dark, k), `--${k} has a dark value`); // Every plain var(--x) in the styles is defined for the light look (dark only overrides). for (const m of css.matchAll(/var\(--([a-z0-9-]+)\)/g)) if (!/^(tabbar|app-h|c|rc|d|x|t|r|dl|pzdock)$/.test(m[1])) assert.ok(val(light, m[1]), `--${m[1]} is defined`); const bgs = ['paper', 'soft', 'tint-y', 'tint-g', 'tint-r', 'tint-p', 'tint-b', 'tint-v', 'tint-o', 'tint-n'].map((k) => [k, val(dark, k)]); for (const t of ['text', 'ink2', 'eye']) for (const [k, b] of bgs) assert.ok(cr(val(dark, t), b) >= 4.5, `--${t} on --${k}: ${cr(val(dark, t), b).toFixed(2)}`); for (const t of ['tx-red', 'tx-amber']) assert.ok(cr(val(dark, t), val(dark, 'paper')) >= 4.5, `--${t} on the dark paper`); // The outline stays dark in both looks, so text on bright fills (yellow buttons, warning tags) keeps reading. assert.ok(cr(val(dark, 'ink'), '#FFD23F') >= 7, 'dark ink on the yellow buttons'); // Badge labels (10px, so 4.5:1) on their night cards: each variant's gradient, and the dimmed card of one not earned now. const rules = css.replace(/\/\*[\s\S]*?\*\//g, '').split('}').map((r) => { const i = r.indexOf('{'); return i < 0 ? null : { sel: r.slice(0, i).split(',').map((x) => x.trim()), body: r.slice(i + 1) }; }).filter(Boolean); const decl = (sel, prop) => { let v = null; for (const r of rules) if (r.sel.includes(sel)) { const m = new RegExp(`(?:^|;)\\s*${prop}\\s*:\\s*([^;]+)`).exec(r.body); if (m) v = m[1].trim(); } return v; }; const D = ':root[data-theme="dark"] '; for (const v of ['freeze', 'bgp']) { const label = decl(D + `.bdg.v-${v} .bv`, 'color') || decl(`.bdg.v-${v} .bv`, 'color'), stops = decl(D + `.bdg.on.v-${v}`, 'background').match(/#[0-9A-Fa-f]{6}/g); assert.ok(label && stops.length >= 2, v); for (const b of [...stops, val(dark, 'tint-n')]) assert.ok(cr(label, b) >= 4.5, `the ${v} badge label ${label} on ${b}: ${cr(label, b).toFixed(2)}`); } // The dashboard's Demand key is an outline in its line's colour (--text): dark ink by day as before, light at night. assert.equal(val(light, 'text'), val(light, 'ink'), 'by day the key looks as it always did'); assert.ok(cr(val(dark, 'text'), val(dark, 'paper')) >= 3, 'at night the key\'s outline shows on the card'); }); test('the dashboard\'s Demand key matches its dashed line, by night too', () => { const { dom, ev, errors } = load({ storage: { 'sot-theme': 'dark' } }); ev(started + "for(let i=0;i<3;i++)dayTick();ACT.dash({});"); const card = dom.window.document.getElementById('dashCard'); const line = card.querySelector('svg path[stroke-dasharray="5 4"]'); assert.ok(line, 'the dashed Demand line'); const stroke = /stroke:\s*([^;"]+)/.exec(line.getAttribute('style'))[1].trim(); const key = [...card.querySelectorAll('.legend .lg')].find((b) => JSON.parse(b.dataset.args).k === 'dem').querySelector('i'); assert.equal(key.style.borderStyle, 'dashed'); assert.equal(key.style.borderColor, stroke, 'the key is outlined in the line\'s own colour'); assert.equal(stroke, 'var(--text)'); assert.deepEqual(errors, []); dom.window.close(); });