waybetter/packages/runtime/tests/themeGuards.test.ts
CRT a9c48c5d35 Theme engine v3 step 8: the layer guard
themeGuards.ts: watches the site's layer container; every new popover (and one that the
site moves or that grows) is measured in an idle callback and shifted into the viewport
minus the bar insets with a translate (data-wb-clamped, --wb-clamp-x/y); taller than the
viewport it scrolls. Bounded: only nodes that hold a popover are measured, at most 4 per
pass; a moved popover is seen through its own style attribute and a ResizeObserver.
clampShift/viewportBox are pure and unit tested.
2026-10-02 05:46:52 +07:00

27 lines
1.3 KiB
TypeScript

// Theme engine v3, step 8: the layer guard's geometry (pure part).
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { clampShift, viewportBox } from '../src/themeGuards.ts';
const box = viewportBox(2000, 1000, { top: 0, bottom: 56 });
test('the account menu that ran 52px past a 2000px window comes back inside, 8px from the edge', () => {
// Live 1.0.2: left 1737, width 300 in a 1985px viewport.
const shift = clampShift({ left: 1737, top: 822, width: 300, height: 108 }, viewportBox(1985, 1000, { top: 0, bottom: 56 }));
assert.equal(1737 + shift.dx + 300, 1985 - 8);
assert.equal(shift.dy, 0);
});
test('a hover card cut at the bottom moves up above the bar inset', () => {
const shift = clampShift({ left: 400, top: 900, width: 300, height: 200 }, box);
assert.equal(900 + shift.dy + 200, 1000 - 8 - 56);
});
test('inside stays put; taller than the box keeps its top edge and asks to scroll', () => {
assert.deepEqual(clampShift({ left: 100, top: 100, width: 200, height: 200 }, box), { dx: 0, dy: 0, overflowY: false });
const tall = clampShift({ left: 100, top: 300, width: 200, height: 2000 }, box);
assert.equal(300 + tall.dy, 8);
assert.ok(tall.overflowY);
const left = clampShift({ left: -40, top: 100, width: 200, height: 100 }, box);
assert.equal(-40 + left.dx, 8);
});