Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
import { describe, expect, test } from "vitest";
import {
END_ANCHOR_BOTTOM_PX,
endAnchoredViewport,
flowJumpVisibility,
isMeaningfulPaneResize,
isViewportStranded,
PANE_RESIZE_EPSILON_PX,
paneRecenterViewport,
relayoutDriftCorrection,
revealNodeViewport,
START_ANCHOR_MARGIN_X_PX,
START_ANCHOR_MIN_ZOOM,
START_ANCHOR_TOP_PX,
startAnchoredViewport,
} from "./paneFit";
describe("isMeaningfulPaneResize", () => {
test("ignores sub-epsilon jitter on both axes", () => {
expect(
isMeaningfulPaneResize(
{ width: 500, height: 800 },
{ width: 500 + PANE_RESIZE_EPSILON_PX - 1, height: 801 },
),
).toBe(false);
});
test("detects a pane-toggle sized width change", () => {
expect(
isMeaningfulPaneResize(
{ width: 1000, height: 800 },
{ width: 330, height: 800 },
),
).toBe(true);
});
test("detects a height-only change", () => {
expect(
isMeaningfulPaneResize(
{ width: 500, height: 800 },
{ width: 500, height: 600 },
),
).toBe(true);
});
});
describe("isViewportStranded", () => {
const chainBounds = { x: 0, y: 0, width: 500, height: 1200 };
test("a freshly fitted viewport is not stranded", () => {
// zoom 0.6 centers the 500-wide chain in a 330-wide pane.
expect(
isViewportStranded({
pane: { width: 330, height: 800 },
viewport: { x: 15, y: 20, zoom: 0.6 },
bounds: chainBounds,
}),
).toBe(false);
});
test("a pane shrink that leaves only a sliver of the chain is stranded", () => {
// Viewport fitted for a 1000-wide pane (chain centered at x=250, zoom 1),
// then the pane shrank to 330: only ~80px of the chain remains visible.
expect(
isViewportStranded({
pane: { width: 330, height: 800 },
viewport: { x: 250, y: 0, zoom: 1 },
bounds: chainBounds,
}),
).toBe(true);
});
test("a viewport panned fully off the flow is stranded", () => {
expect(
isViewportStranded({
pane: { width: 500, height: 800 },
viewport: { x: -2000, y: 0, zoom: 1 },
bounds: chainBounds,
}),
).toBe(true);
});
test("a deliberate zoom into one block keeps the viewport", () => {
// Block region fills the pane: tiny share of the whole flow visible, but
// the pane is fully covered, so the user's focus is preserved.
expect(
isViewportStranded({
pane: { width: 400, height: 300 },
viewport: { x: -50, y: -600, zoom: 1.8 },
bounds: chainBounds,
}),
).toBe(false);
});
test("half the chain visible after a mild resize is not stranded", () => {
expect(
isViewportStranded({
pane: { width: 500, height: 800 },
viewport: { x: 100, y: 0, zoom: 1 },
bounds: chainBounds,
}),
).toBe(false);
});
test("empty flows and hidden panes never count as stranded", () => {
expect(
isViewportStranded({
pane: { width: 500, height: 800 },
viewport: { x: 0, y: 0, zoom: 1 },
bounds: { x: 0, y: 0, width: 0, height: 0 },
}),
).toBe(false);
expect(
isViewportStranded({
pane: { width: 0, height: 0 },
viewport: { x: 0, y: 0, zoom: 1 },
bounds: chainBounds,
}),
).toBe(false);
});
});
describe("startAnchoredViewport", () => {
// A long chain: taller than any pane, so a whole-graph fit would zoom far
// out and center the middle.
const longChain = { x: 0, y: 0, width: 500, height: 4000 };
test("anchors the flow start at the pane top at 1:1 zoom when the chain fits", () => {
expect(
startAnchoredViewport({
pane: { width: 800, height: 600 },
bounds: longChain,
}),
).toEqual({ x: 150, y: START_ANCHOR_TOP_PX, zoom: 1 });
});
test("never zooms in past 1:1 on a very wide pane", () => {
const viewport = startAnchoredViewport({
pane: { width: 2000, height: 900 },
bounds: longChain,
});
expect(viewport?.zoom).toBe(1);
expect(viewport?.x).toBe((2000 - 500) / 2);
});
test("zooms out to fit the chain width in a narrow pane", () => {
const pane = { width: 424, height: 600 };
const viewport = startAnchoredViewport({ pane, bounds: longChain });
expect(viewport?.zoom).toBeCloseTo(
(pane.width - 2 * START_ANCHOR_MARGIN_X_PX) / longChain.width,
);
// Fit-width leaves exactly the margin as equal gutters.
expect(viewport?.x).toBeCloseTo(START_ANCHOR_MARGIN_X_PX);
});
test("respects the canvas minimum zoom in an over-tight pane", () => {
const viewport = startAnchoredViewport({
pane: { width: 220, height: 600 },
bounds: longChain,
});
expect(viewport?.zoom).toBe(START_ANCHOR_MIN_ZOOM);
// Still centered: overflow crops both sides equally.
expect(viewport?.x).toBeCloseTo((220 - 500 * START_ANCHOR_MIN_ZOOM) / 2);
});
test("offset bounds still land the first block at the top margin", () => {
const bounds = { x: -100, y: 300, width: 500, height: 2000 };
const viewport = startAnchoredViewport({
pane: { width: 800, height: 600 },
bounds,
});
expect(viewport).not.toBeNull();
const { x, y, zoom } = viewport!;
// Screen-space position of the flow's top-left corner.
expect(bounds.y * zoom + y).toBeCloseTo(START_ANCHOR_TOP_PX);
expect(bounds.x * zoom + x).toBeCloseTo((800 - 500) / 2);
});
test("degenerate panes and empty flows return null", () => {
expect(
startAnchoredViewport({
pane: { width: 0, height: 0 },
bounds: longChain,
}),
).toBeNull();
expect(
startAnchoredViewport({
pane: { width: 800, height: 600 },
bounds: { x: 0, y: 0, width: 0, height: 0 },
}),
).toBeNull();
});
});
describe("endAnchoredViewport", () => {
const longChain = { x: 0, y: 0, width: 500, height: 4000 };
test("anchors the flow end just above the pane bottom at 1:1 zoom", () => {
expect(
endAnchoredViewport({
pane: { width: 800, height: 600 },
bounds: longChain,
}),
).toEqual({ x: 150, y: 600 - END_ANCHOR_BOTTOM_PX - 4000, zoom: 1 });
});
test("mirrors the start anchor's zoom and horizontal centering", () => {
const pane = { width: 424, height: 600 };
const start = startAnchoredViewport({ pane, bounds: longChain });
const end = endAnchoredViewport({ pane, bounds: longChain });
expect(end).not.toBeNull();
expect(end!.zoom).toBe(start!.zoom);
expect(end!.x).toBe(start!.x);
// Screen-space position of the flow's bottom edge.
expect((longChain.y + longChain.height) * end!.zoom + end!.y).toBeCloseTo(
pane.height - END_ANCHOR_BOTTOM_PX,
);
});
test("offset bounds still land the flow bottom at the bottom margin", () => {
const bounds = { x: -100, y: 300, width: 500, height: 2000 };
const pane = { width: 800, height: 600 };
const viewport = endAnchoredViewport({ pane, bounds });
expect(viewport).not.toBeNull();
expect(
(bounds.y + bounds.height) * viewport!.zoom + viewport!.y,
).toBeCloseTo(pane.height - END_ANCHOR_BOTTOM_PX);
});
test("degenerate panes and empty flows return null", () => {
expect(
endAnchoredViewport({
pane: { width: 0, height: 0 },
bounds: longChain,
}),
).toBeNull();
expect(
endAnchoredViewport({
pane: { width: 800, height: 600 },
bounds: { x: 0, y: 0, width: 0, height: 0 },
}),
).toBeNull();
});
});
describe("flowJumpVisibility", () => {
const pane = { width: 800, height: 600 };
const longChain = { x: 0, y: 0, width: 500, height: 4000 };
test("a start-anchored long flow only offers the jump to its end", () => {
const viewport = startAnchoredViewport({ pane, bounds: longChain });
expect(
flowJumpVisibility({ pane, viewport: viewport!, bounds: longChain }),
).toEqual({ showJumpToStart: false, showJumpToEnd: true });
});
test("an end-anchored long flow only offers the jump back to its start", () => {
const viewport = endAnchoredViewport({ pane, bounds: longChain });
expect(
flowJumpVisibility({ pane, viewport: viewport!, bounds: longChain }),
).toEqual({ showJumpToStart: true, showJumpToEnd: false });
});
test("scrolled into the middle of a long flow offers both jumps", () => {
expect(
flowJumpVisibility({
pane,
viewport: { x: 150, y: -1700, zoom: 1 },
bounds: longChain,
}),
).toEqual({ showJumpToStart: true, showJumpToEnd: true });
});
test("a flow that fits the pane shows neither button", () => {
const shortChain = { x: 0, y: 0, width: 500, height: 400 };
expect(
flowJumpVisibility({
pane,
viewport: { x: 150, y: START_ANCHOR_TOP_PX, zoom: 1 },
bounds: shortChain,
}),
).toEqual({ showJumpToStart: false, showJumpToEnd: false });
});
test("a short flow panned out of view still shows neither button", () => {
const shortChain = { x: 0, y: 0, width: 500, height: 400 };
expect(
flowJumpVisibility({
pane,
viewport: { x: 150, y: -2000, zoom: 1 },
bounds: shortChain,
}),
).toEqual({ showJumpToStart: false, showJumpToEnd: false });
});
test("zooming in far enough makes a short flow jumpable again", () => {
// 400 world px at zoom 2 = 800 screen px > the 600px pane.
const shortChain = { x: 0, y: 0, width: 500, height: 400 };
expect(
flowJumpVisibility({
pane,
viewport: { x: 0, y: -100, zoom: 2 },
bounds: shortChain,
}),
).toEqual({ showJumpToStart: true, showJumpToEnd: true });
});
test("degenerate geometry shows neither button", () => {
expect(
flowJumpVisibility({
pane: { width: 0, height: 0 },
viewport: { x: 0, y: 0, zoom: 1 },
bounds: longChain,
}),
).toEqual({ showJumpToStart: false, showJumpToEnd: false });
expect(
flowJumpVisibility({
pane,
viewport: { x: 0, y: 0, zoom: 1 },
bounds: { x: 0, y: 0, width: 0, height: 0 },
}),
).toEqual({ showJumpToStart: false, showJumpToEnd: false });
});
});
describe("paneRecenterViewport", () => {
const chain = { x: 0, y: 0, width: 500, height: 4000 };
test("recenters horizontally and keeps the scroll position at unchanged zoom", () => {
// Fitted for a 1000-wide pane (x=250), scrolled into the flow (y=-800),
// then a sibling pane opened and the pane shrank to 700.
expect(
paneRecenterViewport({
pane: { width: 700, height: 600 },
bounds: chain,
viewport: { x: 250, y: -800, zoom: 1 },
}),
).toEqual({ x: 100, y: -800, zoom: 1 });
});
test("scales the scroll anchor when the new width changes zoom", () => {
const pane = { width: 424, height: 600 };
const viewport = paneRecenterViewport({
pane,
bounds: chain,
viewport: { x: 250, y: -800, zoom: 1 },
});
expect(viewport).not.toBeNull();
const expectedZoom = (pane.width - 2 * START_ANCHOR_MARGIN_X_PX) / 500;
expect(viewport!.zoom).toBeCloseTo(expectedZoom);
// The content point at the pane's top edge stays put across the zoom.
expect(-viewport!.y / viewport!.zoom).toBeCloseTo(800);
});
test("is a no-op for an already start-anchored viewport", () => {
const pane = { width: 800, height: 600 };
const anchored = startAnchoredViewport({ pane, bounds: chain });
expect(anchored).not.toBeNull();
expect(
paneRecenterViewport({ pane, bounds: chain, viewport: anchored! }),
).toEqual(anchored);
});
test("degenerate geometry returns null", () => {
expect(
paneRecenterViewport({
pane: { width: 0, height: 0 },
bounds: chain,
viewport: { x: 0, y: 0, zoom: 1 },
}),
).toBeNull();
expect(
paneRecenterViewport({
pane: { width: 800, height: 600 },
bounds: chain,
viewport: { x: 0, y: 0, zoom: 0 },
}),
).toBeNull();
});
});
describe("relayoutDriftCorrection", () => {
const viewport = { x: 100, y: -50, zoom: 0.8 };
test("counter-translates x by an anchor node's x delta at the current zoom", () => {
// A collapsing/expanding container's width change shifts every node's x
// uniformly (Dagre re-centers each pass from scratch, so the anchor's
// own x is the only reliable "did the visible content move" signal —
// the union bounding box's left edge is a Dagre normalization artifact
// and does NOT track this). The viewport must slide the opposite way,
// at screen scale (delta * zoom), to keep the anchor visually put.
const corrected = relayoutDriftCorrection({
viewport,
xBefore: 0,
xAfter: 40,
});
expect(corrected).toEqual({ x: 100 - 40 * 0.8, y: -50, zoom: 0.8 });
});
test("a leftward shift (collapse) translates x the other way", () => {
const corrected = relayoutDriftCorrection({
viewport,
xBefore: 40,
xAfter: 0,
});
expect(corrected).toEqual({ x: 100 + 40 * 0.8, y: -50, zoom: 0.8 });
});
test("ignores sub-epsilon drift", () => {
expect(
relayoutDriftCorrection({
viewport,
xBefore: 0,
xAfter: 0.4,
}),
).toBeNull();
});
});
describe("revealNodeViewport", () => {
const pane = { width: 800, height: 600 };
test("centers a node that fits the pane", () => {
const bounds = { x: 100, y: 300, width: 400, height: 200 };
const { x, y, zoom } = revealNodeViewport({ pane, bounds })!;
expect(zoom).toBe(1);
expect(bounds.x + bounds.width / 2 + x).toBeCloseTo(pane.width / 2);
expect(bounds.y + bounds.height / 2 + y).toBeCloseTo(pane.height / 2);
});
test("pins a node taller than the pane by its top so its header shows", () => {
const bounds = { x: 100, y: 2000, width: 400, height: 900 };
const { y } = revealNodeViewport({ pane, bounds })!;
const top = bounds.y + y;
expect(top).toBeGreaterThan(0);
expect(top).toBeLessThan(pane.height / 4);
});
test("degenerate panes and empty nodes return null", () => {
const bounds = { x: 0, y: 0, width: 400, height: 200 };
expect(
revealNodeViewport({ pane: { width: 0, height: 0 }, bounds }),
).toBeNull();
expect(
revealNodeViewport({ pane, bounds: { ...bounds, width: 0, height: 0 } }),
).toBeNull();
});
});