Skip to content

Commit d4c8aaa

Browse files
authored
test(control-ui): guard the hand-mirrored theme --bg copies (#130444)
Each theme's --bg is copied by hand into two places outside base.css, because both have to paint before the app stylesheet is parsed: index.html's pre-paint block, which fills the page during first paint, and the Appearance preview chips, which are deliberately theme-invariant and so cannot read the live token. Nothing tied those copies to the palette. Darkening Absolutely moved --bg and left both behind; only a pre-paint assertion that happened to hardcode the old value caught one of them. It is the same shape as the two defects review found in the theme work — a hand-maintained list extended for new themes but not exhaustively — and the third time is enough. Derives the expected values from base.css and asserts both mirrors for every palette (12 themed pre-paint rules plus the default), so a palette edit cannot leave either copy stale. Verified by mutating each mirror: tide: pre-paint #123456 != --bg #10151b beacon: chip #abcdef != --bg #000000 Test-only; no production change.
1 parent b10beee commit d4c8aaa

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

‎ui/src/styles/base-theme-tokens.node.test.ts‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,130 @@ describe("Control UI base theme tokens", () => {
9191
expect(localOverrides).toEqual([]);
9292
});
9393
});
94+
95+
/*
96+
* --bg is mirrored by hand in two places outside base.css, because both must
97+
* paint before the app stylesheet is parsed or the picker would advertise a
98+
* colour the theme no longer uses:
99+
* - index.html's pre-paint block, which fills the page during first paint
100+
* - the Appearance preview chips in config.css, which are theme-invariant and
101+
* so cannot read the live token
102+
* Nothing tied those copies to the palette, and every review pass on the theme
103+
* work turned up another hand-maintained list that had drifted. Darkening
104+
* Absolutely moved --bg and silently left both copies behind until a pre-paint
105+
* assertion caught one of them. This derives the expected values from base.css
106+
* so a palette edit cannot leave either copy stale again.
107+
*/
108+
describe("Control UI theme --bg mirrors", () => {
109+
const RESOLVED_THEME_BG_SELECTOR = new Map<string, string>([
110+
["dark", ":root"],
111+
["light", ':root[data-theme-mode="light"]'],
112+
]);
113+
114+
function readBlockToken(css: string, selector: string, token: string): string | undefined {
115+
const body = css.split(new RegExp(`${escapeSelector(selector)}\\s*\\{`, "u"))[1]?.split("}")[0];
116+
return body?.match(new RegExp(`${token}:\\s*([^;]+);`, "u"))?.[1]?.trim();
117+
}
118+
119+
function escapeSelector(selector: string): string {
120+
return selector.replaceAll(/[.*+?^${}()|[\]\\]/gu, String.raw`\$&`);
121+
}
122+
123+
/** Every theme's canonical --bg, keyed by the resolved data-theme value. */
124+
function readCanonicalBackgrounds(): Map<string, string> {
125+
const baseCss = fs.readFileSync(path.join(stylesDir, "base.css"), "utf8");
126+
const backgrounds = new Map<string, string>();
127+
for (const [resolved, selector] of RESOLVED_THEME_BG_SELECTOR) {
128+
const value = readBlockToken(baseCss, selector, "--bg");
129+
if (value) {
130+
backgrounds.set(resolved, value);
131+
}
132+
}
133+
for (const match of baseCss.matchAll(/:root\[data-theme="([^"]+)"\]\s*\{/gu)) {
134+
const resolved = match[1] ?? "";
135+
const value = readBlockToken(baseCss, `:root[data-theme="${resolved}"]`, "--bg");
136+
if (value) {
137+
backgrounds.set(resolved, value);
138+
}
139+
}
140+
return backgrounds;
141+
}
142+
143+
it("keeps the index.html pre-paint background on every theme's --bg", () => {
144+
const indexHtml = fs.readFileSync(path.join(uiSrcDir, "..", "index.html"), "utf8");
145+
const canonical = readCanonicalBackgrounds();
146+
const mismatches: string[] = [];
147+
let checked = 0;
148+
149+
const prePaint = new Map<string, string>();
150+
for (const match of indexHtml.matchAll(
151+
/html(?:\[data-theme(?:-mode)?="([^"]+)"\])?\s*\{\s*background:\s*([^;]+);/gu,
152+
)) {
153+
const attribute = match[1];
154+
const value = (match[2] ?? "").trim();
155+
// Bare `html` is the default dark paint; the -mode selector is light.
156+
prePaint.set(attribute ?? "dark", value);
157+
}
158+
159+
for (const [resolved, background] of canonical) {
160+
const painted = prePaint.get(resolved);
161+
if (painted === undefined) {
162+
mismatches.push(`${resolved}: no pre-paint background in index.html`);
163+
continue;
164+
}
165+
checked += 1;
166+
if (painted.toLowerCase() !== background.toLowerCase()) {
167+
mismatches.push(`${resolved}: pre-paint ${painted} != --bg ${background}`);
168+
}
169+
}
170+
171+
expect(mismatches).toEqual([]);
172+
expect(checked).toBeGreaterThanOrEqual(RESOLVED_THEME_BG_SELECTOR.size);
173+
});
174+
175+
it("keeps the Appearance preview chips on every theme's --bg", () => {
176+
const configCss = fs.readFileSync(path.join(stylesDir, "config.css"), "utf8");
177+
const canonical = readCanonicalBackgrounds();
178+
const mismatches: string[] = [];
179+
let checked = 0;
180+
181+
for (const match of configCss.matchAll(/\.settings-theme-card--([\w-]+)[^{]*\{([^}]*)\}/gu)) {
182+
const family = match[1] ?? "";
183+
const body = match[2] ?? "";
184+
const chip = body.match(/--theme-chip-bg:\s*([^;]+);/u)?.[1]?.trim();
185+
if (!chip || chip.startsWith("var(")) {
186+
continue;
187+
}
188+
// Light chip rules are nested under the light-mode selector, so the
189+
// preceding text decides which resolved palette this chip mirrors.
190+
const isLight = configCss
191+
.slice(0, match.index ?? 0)
192+
.split(".settings-theme-card--")
193+
.at(-1)
194+
?.includes('data-theme-mode="light"');
195+
const resolved = resolveFamily(family, isLight === true);
196+
const background = canonical.get(resolved);
197+
if (!background) {
198+
continue;
199+
}
200+
checked += 1;
201+
if (chip.toLowerCase() !== background.toLowerCase()) {
202+
mismatches.push(`${resolved}: chip ${chip} != --bg ${background}`);
203+
}
204+
}
205+
206+
expect(mismatches).toEqual([]);
207+
expect(checked).toBeGreaterThan(0);
208+
});
209+
210+
/** Appearance uses theme family names; base.css keys off resolved values. */
211+
function resolveFamily(family: string, light: boolean): string {
212+
if (family === "claw") {
213+
return light ? "light" : "dark";
214+
}
215+
if (family === "knot") {
216+
return light ? "openknot-light" : "openknot";
217+
}
218+
return light ? `${family}-light` : family;
219+
}
220+
});

0 commit comments

Comments
 (0)