From 544502e78236f5919e9077790d5e60c64e20dfe4 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 05:24:47 -0600 Subject: [PATCH] test(aggregator): behavioural tests for bandLaneChart; demote source-string pins (#8600) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New test/band-lane-chart.test.ts extracts the real bandLaneChart + helpers verbatim from the emitted explorer script (renderAa page) and executes them against known band data: axis range comes from members while plotted points come from choices (a member-only $500 stretches the axis, last tick $500.00, but draws no point), log geometry (0.5/5/500 -> cx 232.0/402.7), leader vs near-leader styling (r 5/#fff/0.95 vs 3.8/0.65, explicit leader by key wins), the lo===hi widening for cost ([/1.4, *1.4], point at log centre 488.0) and score ([-1, +1], linear centre), and the empty render cases. Lane cy geometry (38.0/40.5) pins the pointY-vs-cy behaviour in executed form. aa.test.ts: the five lane-chart source-string assertions (function bandLaneChart, class=xpl-band-lanes, the group-6 call-site text, const pointY / not-const-cy) are removed in favour of these; bandGroup composition and g6Lanes.push data-plumbing pins stay. New file: 5 pass / 0 fail. aa.test.ts itself cannot run in this sandbox (its server-spawning beforeAll hook times out identically on a pristine worktree of the same base — pre-existing environmental). --- pm-stack/aggregator/test/aa.test.ts | 5 - .../aggregator/test/band-lane-chart.test.ts | 145 ++++++++++++++++++ 2 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 pm-stack/aggregator/test/band-lane-chart.test.ts diff --git a/pm-stack/aggregator/test/aa.test.ts b/pm-stack/aggregator/test/aa.test.ts index a325400b3c..ff955b8198 100644 --- a/pm-stack/aggregator/test/aa.test.ts +++ b/pm-stack/aggregator/test/aa.test.ts @@ -711,17 +711,12 @@ describe("aa-explorer manifest + render", () => { expect(html).toContain('data-recpreset="fast"'); expect(html).toContain('id="xpl-bandgroups"'); // band comparisons expect(html).toContain('id="xf-bandscale"'); - expect(html).toContain("function bandLaneChart"); // lane chart renderer is part of the emitted explorer - expect(html).toContain("const pointY = py.toFixed(1)"); // do not shadow the lane's cy before computing py - expect(html).not.toContain("const cy = py.toFixed(1)"); - expect(html).toContain('class="xpl-band-lanes"'); expect(html).toContain('bandGroup("Fastest within each score band"'); expect(html).toContain('g3, "time", "Time / task (lower is faster)"'); expect(html).toContain('bandGroup("Smartest within each speed band"'); expect(html).toContain('g4, "score", scoreWord()'); expect(html).toContain('bandGroup("Fastest within each task-cost band"'); expect(html).toContain('g5, "time", "Time / task (lower is faster)"'); - expect(html).toContain('bandLaneChart("Lowest price within each speed target", "cost"'); expect(html).toContain("g6Lanes.push"); // group 6 charts reuse the table's same qualifying choices expect(html).toContain('data-pj="inputTokens"'); // task-assumptions projection expect(html).toContain('data-pjpreset="repo"'); diff --git a/pm-stack/aggregator/test/band-lane-chart.test.ts b/pm-stack/aggregator/test/band-lane-chart.test.ts new file mode 100644 index 0000000000..a05f1fb657 --- /dev/null +++ b/pm-stack/aggregator/test/band-lane-chart.test.ts @@ -0,0 +1,145 @@ +// Behavioural tests for the Cost Explorer band lane charts (#8600). +// +// aa.test.ts historically pinned the lane chart only by asserting the +// emitted page CONTAINS the renderer's source strings — the renderer +// never executed against data anywhere. Here the real emitted +// explorer script is taken from renderAa's page, the renderer's own +// function definitions are extracted from it verbatim (no copies), +// and bandLaneChart runs against known band data with assertions on +// the SVG it produces. +import { describe, expect, test } from "bun:test"; +import { renderAa } from "../src/render.ts"; +import { FIXTURE_AA } from "../src/sources/fixtures.ts"; +import type { Provenance } from "../src/model.ts"; + +const PROV: Provenance = { + board: "x", + roadmap: "x", + items: "x", + comments: "x", + quota: "x", + usage: "x", + attention: "x", + orchestration: "x", + dns: "x", + registry: "x", + aa: "fixtures", +}; + +/** The emitted explorer script body that defines bandLaneChart. */ +function explorerScript(): string { + const html = renderAa(FIXTURE_AA, PROV); + const scripts = [...html.matchAll(/]*)?>([\s\S]*?)<\/script>/g)].map((m) => m[1]); + const body = scripts.find((b) => b.includes("function bandLaneChart")); + if (!body) throw new Error("emitted page has no script defining bandLaneChart"); + return body; +} + +/** Slice [startMarker, endMarker) out of the emitted script; both must exist. */ +function sliceFn(script: string, startMarker: string, endMarker: string): string { + const a = script.indexOf(startMarker); + const b = script.indexOf(endMarker); + if (a < 0 || b < 0 || b <= a) throw new Error(`cannot extract ${startMarker} .. ${endMarker}`); + return script.slice(a, b); +} + +type BandPoint = { key: string; family: string; effort?: string; cost?: number | null; score?: number | null; time?: number | null }; +type Band = { label: string; members: BandPoint[]; choices: BandPoint[]; leader?: BandPoint }; +type BandLaneChart = (title: string, metric: string, axisLabel: string, bands: Band[]) => string; + +function loadBandLaneChart(): BandLaneChart { + const script = explorerScript(); + const helpers = sliceFn(script, "function esc(s)", "function fmtCtx("); + const labels = sliceFn(script, "var ESHORT", "var goCapByName"); + const chart = sliceFn(script, "function bandLaneChart(", "function bandGroup("); + // famColor is populated from the page's point data at runtime; the + // chart falls back to a default fill for unmapped families, and the + // assertions below pin geometry/styling roles, not palette values. + const factory = new Function(`"use strict";\nvar famColor = {};\n${labels}\n${helpers}\n${chart}\nreturn bandLaneChart;`); + return factory() as BandLaneChart; +} + +const bandLaneChart = loadBandLaneChart(); + +const pt = (key: string, cost: number | null): BandPoint => ({ key, family: "Fam" + key, effort: "low", cost }); + +function circles(svg: string): string[] { + return [...svg.matchAll(/]*>/g)].map((m) => m[0]); +} + +describe("bandLaneChart behavioural (#8600)", () => { + test("axis range comes from members; plotted points come from choices", () => { + const bands: Band[] = [ + { + label: "Band A", + // m3 is a member only: it must stretch the axis to $500 but + // must NOT be plotted. + members: [pt("m1", 0.5), pt("m2", 5), pt("m3", 500)], + choices: [pt("cheap", 0.5), pt("mid", 5)], + }, + ]; + const svg = bandLaneChart("T", "cost", "USD / task (lower is cheaper)", bands); + expect(svg).toContain('class="xpl-band-lanes"'); + expect(svg).toContain("(log scale)"); + // Two choices plotted, not three members. + expect(circles(svg)).toHaveLength(2); + // The axis reaches the member-only maximum: the last tick is $500. + expect(svg).toContain(">$500.00<"); + // Log geometry: values 0.5 / 5 / 500 are log-spaced 0 / 1/3 / 1 of + // the 512-wide plot starting at x=232. + expect(svg).toContain('cx="232.0"'); // cheap at lo + expect(svg).toContain('cx="402.7"'); // mid at 232 + 512/3 + // Lane geometry (the pointY-vs-cy regression the old string + // assertions guarded): lane 0 centre is cy=43, point jitter is + // ((pi % 5) - 2) * 2.5, so the points land at 38.0 and 40.5. + expect(svg).toContain('cy="38.0"'); + expect(svg).toContain('cy="40.5"'); + }); + + test("the band leader is styled as leader; other choices as near-leaders", () => { + const bands: Band[] = [{ label: "Band A", members: [pt("cheap", 0.5), pt("mid", 5)], choices: [pt("cheap", 0.5), pt("mid", 5)] }]; + const svg = bandLaneChart("T", "cost", "USD / task", bands); + const [leader, other] = circles(svg); + // No explicit band.leader: choices[0] leads. + expect(leader).toContain('r="5"'); + expect(leader).toContain('stroke="#fff"'); + expect(leader).toContain('fill-opacity="0.95"'); + expect(other).toContain('r="3.8"'); + expect(other).toContain('fill-opacity="0.65"'); + expect(svg).toContain("— band leader"); + expect(svg).toContain("— near-leader"); + // An explicit leader by key wins over position. + const flipped: Band[] = [ + { label: "Band A", members: [pt("cheap", 0.5), pt("mid", 5)], choices: [pt("cheap", 0.5), pt("mid", 5)], leader: pt("mid", 5) }, + ]; + const svg2 = bandLaneChart("T", "cost", "USD / task", flipped); + const [first, second] = circles(svg2); + expect(first).toContain('r="3.8"'); + expect(second).toContain('r="5"'); + }); + + test("lo === hi widens: a single cost value lands at the log centre", () => { + const bands: Band[] = [{ label: "Solo", members: [pt("only", 10)], choices: [pt("only", 10)] }]; + const svg = bandLaneChart("T", "cost", "USD / task", bands); + // Widened to [10/1.4, 10*1.4]; log-midpoint is 10 itself, which is + // the plot centre: 232 + 512/2 = 488. + expect(svg).toContain('cx="488.0"'); + expect(circles(svg)).toHaveLength(1); + }); + + test("lo === hi widens for score too, on the linear scale", () => { + const p: BandPoint = { key: "s1", family: "Fams1", effort: "low", score: 80 }; + const svg = bandLaneChart("T", "score", "Score", [{ label: "Solo", members: [p], choices: [p] }]); + expect(svg).toContain("(linear scale)"); + // Widened to [79, 81]; 80 is the centre. + expect(svg).toContain('cx="488.0"'); + }); + + test("no usable member values renders nothing", () => { + const bands: Band[] = [{ label: "Empty", members: [pt("a", null)], choices: [pt("a", null)] }]; + expect(bandLaneChart("T", "cost", "USD / task", bands)).toBe(""); + // Bands without choices are dropped before the axis is computed. + const noChoices: Band[] = [{ label: "Empty", members: [pt("a", 3)], choices: [] }]; + expect(bandLaneChart("T", "cost", "USD / task", noChoices)).toBe(""); + }); +});