Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pm-stack/aggregator/src/assets/aa-explorer-computations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ export function projectTask(
aaPoints: any[],
caiPoints: any[],
): { cost: number | null; time: number | null; band: any };

export interface ChartViewState {
base: number[];
cur: number[] | null;
}
export function parseChartViewBox(s: string | null | undefined): number[] | null;
export function effectiveChartView(st: ChartViewState): number[];
export function clampChartView(st: ChartViewState): void;
export function zoomChartView(st: ChartViewState, factor: number, fx: number | null, fy: number | null): void;
export function panChartView(st: ChartViewState, dx: number, dy: number, width: number, height: number): boolean;
export function resetChartView(st: ChartViewState): void;
export function chartClickSuppressed(rawStamp: string | number | null | undefined, now: number): boolean;
59 changes: 59 additions & 0 deletions pm-stack/aggregator/src/assets/aa-explorer-computations.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,62 @@ export function projectTask(e, settings, aaPoints, caiPoints) {
if (timing && timing.tps) time = turns * ((timing.ttft || 0) + answer / timing.tps + (Number(settings.overheadSec) || 0));
return { cost: cost, time: time, band: band };
}

// --- /aa chart zoom/pan viewBox behavior (#9581) ---------------------------
// Pure, DOM-free chart-view math shared by the browser enhancer in
// render.ts (which delegates to these functions — no inline copies)
// and Bun tests. A chart view state is { base: [x, y, w, h], cur:
// [x, y, w, h] | null }; cur === null means "show the base viewBox".

export function parseChartViewBox(s) {
var p = (s || "").split(/\s+/).map(Number);
if (p.length !== 4 || p.some(function (n) { return !isFinite(n); })) return null;
return p;
}

export function effectiveChartView(st) {
return st.cur || st.base;
}

export function clampChartView(st) {
if (!st.cur) return;
var b = st.base, c = st.cur;
c[0] = Math.min(Math.max(c[0], b[0]), b[0] + b[2] - c[2]);
c[1] = Math.min(Math.max(c[1], b[1]), b[1] + b[3] - c[3]);
}

export function zoomChartView(st, factor, fx, fy) {
var cur = st.cur ? st.cur.slice() : st.base.slice();
var nw = Math.min(Math.max(cur[2] * factor, st.base[2] / 16), st.base[2]);
var nh = nw * st.base[3] / st.base[2];
var cx = cur[0] + (fx == null ? 0.5 : fx) * cur[2];
var cy = cur[1] + (fy == null ? 0.5 : fy) * cur[3];
cur[0] = cx - (fx == null ? 0.5 : fx) * nw;
cur[1] = cy - (fy == null ? 0.5 : fy) * nh;
cur[2] = nw; cur[3] = nh;
st.cur = nw >= st.base[2] ? null : cur;
if (st.cur) clampChartView(st);
}

// Returns true when the pan was applied, false when it is a no-op
// (not zoomed, or the element has no layout size to scale against).
export function panChartView(st, dx, dy, width, height) {
if (!st.cur || !width || !height) return false;
st.cur[0] -= dx * st.cur[2] / width;
st.cur[1] -= dy * st.cur[3] / height;
clampChartView(st);
return true;
}

export function resetChartView(st) {
st.cur = null;
}

// Click-suppression contract (#4154): after a moved drag the enhancer
// stamps data-zsuppress with the pointerup time; a click arriving
// within 400 ms of that stamp is the drag's tail and is swallowed,
// any other click (no stamp, or stamp expired) reaches selection.
export function chartClickSuppressed(rawStamp, now) {
var t = Number(rawStamp || 0);
return !!t && now - t < 400;
}
40 changes: 11 additions & 29 deletions pm-stack/aggregator/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4117,7 +4117,7 @@ ${dnsTable(section.entries, renewingSoon, next)}`;
* Written without template literals on purpose — it lives inside one.
*/
const AA_EXPLORER_SCRIPT = `<script type="module">
import { applyInequalityFilters, bandIndex, computeRecommendations, logEdges, normName, paretoSet, pctRank, projectTask, scoreEdges } from "/assets/aa-explorer-computations.js";
import { applyInequalityFilters, bandIndex, chartClickSuppressed, computeRecommendations, effectiveChartView, logEdges, normName, panChartView, paretoSet, parseChartViewBox, pctRank, projectTask, resetChartView, scoreEdges, zoomChartView } from "/assets/aa-explorer-computations.js";
(function () {
var dataEl = document.getElementById("xpl-data");
if (!dataEl) return;
Expand Down Expand Up @@ -4454,11 +4454,7 @@ import { applyInequalityFilters, bandIndex, computeRecommendations, logEdges, no
if (!svg.getAttribute("data-zkey")) svg.setAttribute("data-zkey", "zx" + Math.random().toString(36).slice(2, 9));
return svg.getAttribute("data-zkey");
}
function parseViewBox(s) {
var p = (s || "").split(/\\s+/).map(Number);
if (p.length !== 4 || p.some(function (n) { return !isFinite(n); })) return null;
return p;
}
function parseViewBox(s) { return parseChartViewBox(s); }
function chartState(svg) {
var key = chartKeyFor(svg);
var attr = svg.getAttribute("viewBox") || "";
Expand All @@ -4471,45 +4467,32 @@ import { applyInequalityFilters, bandIndex, computeRecommendations, logEdges, no
return st;
}
function applyChartView(svg, st) {
var v = st.cur || st.base;
var v = effectiveChartView(st);
var s = v.join(" ");
svg.setAttribute("viewBox", s);
st.applied = s;
}
function clampChartView(st) {
var b = st.base, c = st.cur;
c[0] = Math.min(Math.max(c[0], b[0]), b[0] + b[2] - c[2]);
c[1] = Math.min(Math.max(c[1], b[1]), b[1] + b[3] - c[3]);
}
// The viewBox math itself lives in aa-explorer-computations.js
// (#9581, same pattern as #7562): these wrappers only adapt the
// module functions to an svg's chart state — no inline copies, so
// the Bun tests exercise exactly the code the browser runs.
function zoomChart(svg, factor, fx, fy) {
var st = chartState(svg);
if (!st) return;
var cur = st.cur ? st.cur.slice() : st.base.slice();
var nw = Math.min(Math.max(cur[2] * factor, st.base[2] / 16), st.base[2]);
var nh = nw * st.base[3] / st.base[2];
var cx = cur[0] + (fx == null ? 0.5 : fx) * cur[2];
var cy = cur[1] + (fy == null ? 0.5 : fy) * cur[3];
cur[0] = cx - (fx == null ? 0.5 : fx) * nw;
cur[1] = cy - (fy == null ? 0.5 : fy) * nh;
cur[2] = nw; cur[3] = nh;
st.cur = nw >= st.base[2] ? null : cur;
if (st.cur) clampChartView(st);
zoomChartView(st, factor, fx, fy);
applyChartView(svg, st);
}
function panChart(svg, dx, dy) {
var st = chartState(svg);
if (!st || !st.cur) return;
var r = svg.getBoundingClientRect();
if (!r.width || !r.height) return;
st.cur[0] -= dx * st.cur[2] / r.width;
st.cur[1] -= dy * st.cur[3] / r.height;
clampChartView(st);
if (!panChartView(st, dx, dy, r.width, r.height)) return;
applyChartView(svg, st);
}
function resetChart(svg) {
var st = chartState(svg);
if (!st) return;
st.cur = null;
resetChartView(st);
applyChartView(svg, st);
}
function chartHost(svg) { return svg.closest(".chart") || svg.parentElement; }
Expand Down Expand Up @@ -4588,8 +4571,7 @@ import { applyInequalityFilters, bandIndex, computeRecommendations, logEdges, no
drag = null;
});
svg.addEventListener("click", function (e) {
var t = Number(svg.getAttribute("data-zsuppress") || 0);
if (t && Date.now() - t < 400) { e.stopPropagation(); e.preventDefault(); svg.removeAttribute("data-zsuppress"); }
if (chartClickSuppressed(svg.getAttribute("data-zsuppress"), Date.now())) { e.stopPropagation(); e.preventDefault(); svg.removeAttribute("data-zsuppress"); }
}, true);
svg.addEventListener("wheel", function (e) {
e.preventDefault();
Expand Down
114 changes: 114 additions & 0 deletions pm-stack/aggregator/test/aa-explorer-computations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import { describe, expect, test } from "bun:test";
import {
applyInequalityFilters,
bandIndex,
chartClickSuppressed,
clampChartView,
computeRecommendations,
effectiveChartView,
gapMetrics,
logEdges,
panChartView,
paretoSet,
parseChartViewBox,
pctRank,
projectTask,
resetChartView,
scoreEdges,
zoomChartView,
} from "../src/assets/aa-explorer-computations.js";

describe("Cost Explorer browser computations", () => {
Expand Down Expand Up @@ -127,3 +134,110 @@ describe("Cost Explorer browser computations", () => {
expect(projectTask(missingCacheRate, settings, [point], []).cost).toBeNull();
});
});

// Oracle: the viewBox arithmetic of PR #4154 as specified on issue
// #9581 — focal anchoring keeps the focal data point fixed, zoom is
// floored at 1/16 of the base width, zooming back out to the base
// width clears the zoom state, pan is clamped to the base viewBox,
// reset restores the base, and a click within 400 ms of a moved
// drag's pointerup stamp is swallowed while other clicks are not.
// These drive the same module functions the browser enhancer calls.
describe("aa chart zoom/pan viewBox behavior (#9581)", () => {
const base = [0, 0, 1600, 900];
const state = () => ({ base: base.slice(), cur: null as number[] | null });

test("parseChartViewBox parses a viewBox and rejects malformed values", () => {
expect(parseChartViewBox("0 0 1600 900")).toEqual([0, 0, 1600, 900]);
expect(parseChartViewBox("10 20 30")).toBeNull();
expect(parseChartViewBox("a b c d")).toBeNull();
expect(parseChartViewBox("")).toBeNull();
expect(parseChartViewBox(null)).toBeNull();
});

test("zoom anchors on the focal point and preserves aspect", () => {
const st = state();
zoomChartView(st, 0.5, 0.25, 0.5);
// focal data point (400, 450) sits at the same fraction after zoom
expect(st.cur).toEqual([200, 225, 800, 450]);
expect(effectiveChartView(st)).toEqual([200, 225, 800, 450]);
const centred = state();
zoomChartView(centred, 0.5, null, null);
expect(centred.cur).toEqual([400, 225, 800, 450]);
});

test("zoom is floored at 16x (base width / 16)", () => {
const st = state();
zoomChartView(st, 0.01, null, null);
expect(st.cur).toEqual([750, 421.875, 100, 56.25]);
zoomChartView(st, 0.01, null, null);
expect(st.cur).toEqual([750, 421.875, 100, 56.25]);
});

test("zooming back out to the base width clears the zoom state", () => {
const st = state();
zoomChartView(st, 0.5, null, null);
expect(st.cur).not.toBeNull();
zoomChartView(st, 2, null, null);
expect(st.cur).toBeNull();
expect(effectiveChartView(st)).toEqual(base);
const fresh = state();
zoomChartView(fresh, 1.25, null, null);
expect(fresh.cur).toBeNull();
});

test("pan scales pixels to viewBox units and clamps at the base edges", () => {
const st = state();
zoomChartView(st, 0.5, null, null); // cur [400, 225, 800, 450]
expect(panChartView(st, 80, 45, 800, 450)).toBe(true);
expect(st.cur).toEqual([320, 180, 800, 450]);
// huge drags clamp to the base corners, never past them
expect(panChartView(st, 100000, 100000, 800, 450)).toBe(true);
expect(st.cur).toEqual([0, 0, 800, 450]);
expect(panChartView(st, -100000, -100000, 800, 450)).toBe(true);
expect(st.cur).toEqual([800, 450, 800, 450]);
});

test("pan is a no-op when not zoomed or the element has no size", () => {
const st = state();
expect(panChartView(st, 10, 10, 800, 450)).toBe(false);
expect(st.cur).toBeNull();
zoomChartView(st, 0.5, null, null);
const before = (st.cur as number[]).slice();
expect(panChartView(st, 10, 10, 0, 450)).toBe(false);
expect(st.cur).toEqual(before);
});

test("clamp honours a non-zero base origin", () => {
const st = { base: [100, 50, 1600, 900], cur: [0, 0, 800, 450] };
clampChartView(st);
expect(st.cur).toEqual([100, 50, 800, 450]);
st.cur = [5000, 5000, 800, 450];
clampChartView(st);
expect(st.cur).toEqual([900, 500, 800, 450]);
});

test("reset restores the base viewBox after zoom + pan", () => {
const st = state();
zoomChartView(st, 0.5, 0.25, 0.5);
panChartView(st, 40, 20, 800, 450);
expect(effectiveChartView(st)).not.toEqual(base);
resetChartView(st);
expect(st.cur).toBeNull();
expect(effectiveChartView(st)).toEqual(base);
});

test("click suppression: post-drag click swallowed, ordinary click passes", () => {
const now = 1_700_000_000_000;
// a moved drag stamps pointerup time; the click that follows it
// inside the 400 ms window is the drag's tail — swallowed
expect(chartClickSuppressed(String(now - 399), now)).toBe(true);
expect(chartClickSuppressed(String(now), now)).toBe(true);
// at/after the window the same stamp no longer suppresses
expect(chartClickSuppressed(String(now - 400), now)).toBe(false);
expect(chartClickSuppressed(String(now - 60_000), now)).toBe(false);
// an ordinary click has no drag, hence no stamp — never swallowed
expect(chartClickSuppressed(null, now)).toBe(false);
expect(chartClickSuppressed("0", now)).toBe(false);
expect(chartClickSuppressed("", now)).toBe(false);
});
});
17 changes: 17 additions & 0 deletions pm-stack/aggregator/test/aa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,23 @@ describe("aa chart expand + zoom/pan (#3298)", () => {
expect(scripts.length).toBeGreaterThan(0);
for (const script of scripts) expect(() => new Function(script)).not.toThrow();
});

test("the enhancer delegates its viewBox math to the tested computations module (#9581)", () => {
const html = renderAa(FIXTURE_AA, PROV);
const importMatch = html.match(/import \{([^}]+)\} from "\/assets\/aa-explorer-computations\.js";/);
expect(importMatch).not.toBeNull();
const imported = (importMatch as RegExpMatchArray)[1]
.split(",")
.map((part) => part.trim())
.filter(Boolean);
// the zoom/pan behavior the executable tests in
// aa-explorer-computations.test.ts drive is the module's — the
// page must import it, not carry an inline copy that could drift
for (const name of ["parseChartViewBox", "effectiveChartView", "zoomChartView", "panChartView", "resetChartView", "chartClickSuppressed"]) {
expect(imported).toContain(name);
expect(html).not.toMatch(new RegExp("function " + name + "\\s*\\("));
}
});
});

describe("AA_GO_CAPS keys are reachable (#2645)", () => {
Expand Down