fix(insights): align BD-Zielbereich delta math with comparison window

The tile computed `bp30 - bpAll` regardless of which baseline the
user picked under Settings → Dashboard, while rendering a caption
keyed on `comparisonBaseline` ("vs. last month" / "vs. last year").
The math therefore lied to the user. Every other trend tile uses
`tileCompareDelta()` against period-aligned `avg30LastMonth` /
`avg30LastYear` baselines.

Add `priorMonth` (now-60d…now-30d) and `priorYear` (now-395d…
now-365d) windows to `computeBpInTargetWindows()`. Ship the pcts on
the analytics envelope as `bpInTargetPctPriorMonth` /
`bpInTargetPctPriorYear`. Route the dashboard's `bpCompareDelta` to
subtract the prior window matching the user's baseline; fall back
to null for `none`.

Three new bp-in-target unit tests (priorMonth, priorYear, null
windows) and two insights-polish guards (envelope contains the
fields, page no longer subtracts `bpAll`).

Closes Code-H2.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.7 committed 2026-05-10 22:08:03 +02:00
1 parent 75ccc01984
commit ef597bfbbd
5 files changed
+180 -2

No files matched your search

+34
View File
@@ -181,6 +181,40 @@ describe("v1.4.19 A3 — chart-token leak hardening", () => {
});
});
describe("v1.4.22 W5 reconcile (Code-H2) — BD-Zielbereich tile compareDelta uses period-aligned baseline", () => {
/**
* Up to v1.4.22 W4 the BD tile computed `bp30 - bpAll`
* (last-30-days minus all-time) regardless of which window the user
* picked under Settings → Dashboard. The caption still rendered "vs.
* last month" / "vs. last year" via `comparison.captionLastMonth` /
* `comparison.captionLastYear`, so the user read a sentence whose
* numerator was actually a 30d-vs-all-time delta. Every other tile
* routes through `tileCompareDelta()` with `summary.avg30LastMonth`
* / `summary.avg30LastYear` for honest period-aligned math.
*
* This test pins that the dashboard now subtracts the prior-period
* window (priorMonth / priorYear) instead of all-time.
*/
it("subtracts bpInTargetPctPriorMonth when comparisonBaseline=lastMonth", () => {
const src = load(DASHBOARD_PATH);
// The fix introduces `bpComparePrior` keyed by `compareBaseline`.
expect(src).toContain("bpInTargetPctPriorMonth");
expect(src).toContain("bpInTargetPctPriorYear");
// The all-time shortcut from v1.4.22 A2 must not subtract `bpAll`
// for the comparison delta any more.
expect(src).not.toMatch(/bp30\s*-\s*bpAll/);
});
it("ships priorMonth + priorYear pcts in the analytics envelope", () => {
const ROUTE_SRC = readFileSync(
join(ROOT, "src/app/api/analytics/route.ts"),
"utf8",
);
expect(ROUTE_SRC).toContain("bpInTargetPctPriorMonth");
expect(ROUTE_SRC).toContain("bpInTargetPctPriorYear");
});
});
describe("v1.4.22 A3 — comparison toggle is global Settings only", () => {
/**
* The comparison-overlay toggle is a global preference. Up to v1.4.21