ui: a browser layer for the library, and the .media reset that never applied

The PathInput overlap could not have been caught by jest -- jsdom computes no
layout -- so the only guard was a grep for one spelling of one mistake.  This
adds the layer that can actually see it, and it found a real bug immediately.

ui-layout.cy.js mounts library components in a real browser through a new
cy.mountUI(), which supplies Mantine's provider and a stamped data-theme and
nothing else.  That spartan setup is the point: these components read no
ambient state, so a spec can assert what jsdom cannot compute -- real geometry,
resolved custom properties, applied filters -- across all four themes.

It reproduces the leftSectionWidth='auto' overlap on purpose and asserts it
still overlaps, because a geometry check that has never gone red is
indistinguishable from one that cannot fail.

The bug it found: the rule resetting `filter` on leaf media inside a `.media`
wrapper scored (0,2,2) against the (0,3,1) rule it was correcting, so it lost
every time and never applied to anything.  Every image inside a filtered
wrapper has been through the luminance matrix twice since that rule landed --
markedly darker than its surroundings, which is exactly what it was written to
prevent.  tokens.test.js asserted only that the rule's text was present, and so
stayed green throughout; it now compares the two selectors' specificity, which
is computable without a browser and goes red on the old selector.

Three guards in ui.test.js keep the library mock-free by construction rather
than by habit: no jest.mock in its own tests, no fetch in any library file, and
no useContext outside the theme controls, whose job is to read the theme.  All
three were verified red against planted violations.

959 jest tests, 14 new browser tests, clean cache-busted build.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-07-30 15:31:09 -06:00 • committed by lrnselfreliance
parent c534cbfe23
commit c100ffb378
5 changed files with 338 additions and 7 deletions

View file

@ -261,8 +261,15 @@ html[data-media-filter="amber-mono"] :is(img, video, canvas, iframe, embed, obje
*
* The audio player's poster is the case that found this — the poster sits under
* custom control glyphs, so the wrapper is filtered to keep the two tinted together.
*
* The trailing `:not(.night-unfiltered)` is here for specificity, not for its meaning.
* The rule above scores (0,3,1) — an attribute, a `.media` inside `:is()`, and a `:not()`
* — so this reset had to reach three class-level components to win it; without the extra
* one it scored (0,2,2) and silently lost every time, which is exactly what it did until
* a browser test measured the leaf and found it filtered twice. jsdom cannot catch this:
* it never resolves `filter` at all.
*/
html[data-media-filter] .media :is(img, video, canvas, iframe, embed, object) {
html[data-media-filter] .media :is(img, video, canvas, iframe, embed, object):not(.night-unfiltered) {
filter: none;
}

View file

@ -120,11 +120,52 @@ describe('media filter rules', () => {
});
it('does not filter leaf media twice inside a filtered wrapper', () => {
// A `.media` wrapper filters its subtree as a unit; the img/video inside it also
// matches the element-type rule, and two passes of the matrix take the luminance
// of an already-red pixel, leaving that image much darker than its surroundings.
expect(tokensCss).toMatch(
/html\[data-media-filter] \.media :is\([^)]*\)\s*{\s*filter:\s*none/);
/*
* A `.media` wrapper filters its subtree as a unit; the img/video inside it also
* matches the element-type rule, and two passes of the matrix take the luminance of
* an already-red pixel, leaving that image much darker than its surroundings.
*
* This used to assert only that the reset rule's text was present, and so it passed
* for as long as the rule existed -- including the whole time the rule lost on
* specificity and never applied to anything. Presence is not effect. What has to
* hold is that the reset outranks the rule it is correcting, which is computable
* from the selectors themselves. ui-layout.cy.js measures the resulting `filter` in
* a real browser, which is the only place the effect itself is observable.
*/
const specificity = (selector) => {
// Enough of the algorithm for these selectors: :is() and :not() contribute the
// highest specificity among their arguments.
let classes = 0, elements = 0;
const counted = selector.replace(/:(is|not)\(([^)]*)\)/g, (_, __, args) => {
const best = args.split(',')
.map(arg => specificity(arg.trim()))
.sort((a, b) => (b.classes - a.classes) || (b.elements - a.elements))[0];
classes += best.classes;
elements += best.elements;
return ' ';
});
classes += (counted.match(/\.[\w-]+|\[[^\]]*]|:[\w-]+/g) || []).length;
elements += (counted.match(/(^|[\s>+~])[a-z][\w-]*/gi) || []).length;
return {classes, elements};
};
const rank = (selector) => {
const {classes, elements} = specificity(selector);
return classes * 1000 + elements;
};
const selectorFor = (needle) => {
const match = tokensCss.split('}')
.map(block => block.split('{')[0].trim())
.filter(Boolean)
.find(selector => selector.includes(needle));
if (!match) throw new Error(`tokens.css has no rule matching ${needle}`);
return match.split('\n').pop().trim();
};
const reset = selectorFor('.media :is(img');
const filtered = selectorFor('[data-media-filter="night-red"] :is(img');
expect(rank(reset)).toBeGreaterThan(rank(filtered));
});
it('never filters an ancestor, only leaf media', () => {