From 2e2ade85b74d46b6e48b6c72dd6856da322e0df9 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 29 Oct 2026 15:32:56 +0000 Subject: [PATCH 1/2] fix(app): highlight region above threshold for 'above' alerts Recharts v3 flipped the default edge for open-ended ReferenceAreas: on the Y axis a missing y1 now snaps to the top and a missing y2 to the bottom, the reverse of v2. ABOVE/ABOVE_EXCLUSIVE used only y1, so the red firing region filled below the threshold. NOT_BETWEEN was similarly inverted. Move the single open-ended bound to the correct prop so the shaded region lands on the firing side of the threshold, and pin the y1/y2 props per threshold type in unit tests. Co-authored-by: Drew Davis --- .changeset/alert-above-threshold-highlight.md | 5 ++ packages/app/src/components/Alerts.tsx | 16 ++++- .../src/components/__tests__/Alerts.test.ts | 67 ++++++++++++++++++- 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 .changeset/alert-above-threshold-highlight.md diff --git a/.changeset/alert-above-threshold-highlight.md b/.changeset/alert-above-threshold-highlight.md new file mode 100644 index 0000000000..4779178469 --- /dev/null +++ b/.changeset/alert-above-threshold-highlight.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +Fix the alert preview chart highlighting the region below the threshold for "above" alerts. Recharts v3 flipped the default edge for open-ended reference areas, so "above" and "not between" alerts shaded the wrong side of the threshold line. diff --git a/packages/app/src/components/Alerts.tsx b/packages/app/src/components/Alerts.tsx index 360d797fb8..8b4c1da29a 100644 --- a/packages/app/src/components/Alerts.tsx +++ b/packages/app/src/components/Alerts.tsx @@ -191,10 +191,16 @@ export const getAlertReferenceLines = ({ thresholdType === AlertThresholdType.NOT_BETWEEN && thresholdMax != null ) { + // Recharts v3 flipped the default edge for an open-ended ReferenceArea: + // on the Y axis a missing y1 now snaps to the top (highest value) and a + // missing y2 to the bottom (lowest value) - the reverse of v2. So the + // open end must be the prop we omit: omit y2 to fill down from `threshold` + // (values below the lower bound) and omit y1 to fill up from `thresholdMax` + // (values above the upper bound). return [ , { it('returns only threshold for a simple (non-range) alert type', () => { @@ -78,3 +82,64 @@ describe('getAlertReferenceLineValues', () => { ).toEqual([0, 550]); }); }); + +describe('getAlertReferenceLines', () => { + // Recharts v3 fills an open-ended ReferenceArea from the set bound to the + // opposite edge: a set y2 (with y1 omitted) fills up to the top, and a set + // y1 (with y2 omitted) fills down to the bottom. These tests pin which bound + // each threshold type sets so the red firing region stays on the correct + // side of the threshold. + it.each([AlertThresholdType.ABOVE, AlertThresholdType.ABOVE_EXCLUSIVE])( + 'fills up from the threshold for %s', + thresholdType => { + const area = getAlertReferenceLines({ + threshold: 50, + thresholdMax: undefined, + thresholdType, + }) as ReactElement<{ y1?: number; y2?: number }>; + expect(isValidElement(area)).toBe(true); + expect(area.props.y2).toBe(50); + expect(area.props.y1).toBeUndefined(); + }, + ); + + it.each([AlertThresholdType.BELOW, AlertThresholdType.BELOW_OR_EQUAL])( + 'fills from 0 up to the threshold for %s', + thresholdType => { + const area = getAlertReferenceLines({ + threshold: 50, + thresholdMax: undefined, + thresholdType, + }) as ReactElement<{ y1?: number; y2?: number }>; + expect(area.props.y1).toBe(0); + expect(area.props.y2).toBe(50); + }, + ); + + it('fills the band between the bounds for BETWEEN', () => { + const area = getAlertReferenceLines({ + threshold: 10, + thresholdMax: 20, + thresholdType: AlertThresholdType.BETWEEN, + }) as ReactElement<{ y1?: number; y2?: number }>; + expect(area.props.y1).toBe(10); + expect(area.props.y2).toBe(20); + }); + + it('fills below the lower bound and above the upper bound for NOT_BETWEEN', () => { + const areas = getAlertReferenceLines({ + threshold: 10, + thresholdMax: 20, + thresholdType: AlertThresholdType.NOT_BETWEEN, + }) as ReactElement<{ y1?: number; y2?: number }>[]; + expect(Array.isArray(areas)).toBe(true); + + const [lower, upper] = areas; + // Lower region: fills down from the lower bound (y2 omitted -> bottom). + expect(lower.props.y1).toBe(10); + expect(lower.props.y2).toBeUndefined(); + // Upper region: fills up from the upper bound (y1 omitted -> top). + expect(upper.props.y2).toBe(20); + expect(upper.props.y1).toBeUndefined(); + }); +}); From 99ca2c93f4745017d543d72cbdb3bb1c73aeb986 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 29 Oct 2026 15:43:25 +0000 Subject: [PATCH 2/2] test(app): assert reference-area shading by rendering, not props Replace the prop-only getAlertReferenceLines tests with ones that render the areas in a fixed-size chart and check which side of the threshold line gets shaded. The prop-only checks restated the implementation and would survive a future Recharts default-edge flip; they also tripped the app lint warning cap via unsafe type assertions. Co-authored-by: Drew Davis --- packages/app/src/components/Alerts.tsx | 10 -- .../src/components/__tests__/Alerts.test.ts | 67 +----------- .../__tests__/getAlertReferenceLines.test.tsx | 101 ++++++++++++++++++ 3 files changed, 102 insertions(+), 76 deletions(-) create mode 100644 packages/app/src/components/__tests__/getAlertReferenceLines.test.tsx diff --git a/packages/app/src/components/Alerts.tsx b/packages/app/src/components/Alerts.tsx index 8b4c1da29a..725a64f6e8 100644 --- a/packages/app/src/components/Alerts.tsx +++ b/packages/app/src/components/Alerts.tsx @@ -191,12 +191,6 @@ export const getAlertReferenceLines = ({ thresholdType === AlertThresholdType.NOT_BETWEEN && thresholdMax != null ) { - // Recharts v3 flipped the default edge for an open-ended ReferenceArea: - // on the Y axis a missing y1 now snaps to the top (highest value) and a - // missing y2 to the bottom (lowest value) - the reverse of v2. So the - // open end must be the prop we omit: omit y2 to fill down from `threshold` - // (values below the lower bound) and omit y1 to fill up from `thresholdMax` - // (values above the upper bound). return [ { it('returns only threshold for a simple (non-range) alert type', () => { @@ -82,64 +78,3 @@ describe('getAlertReferenceLineValues', () => { ).toEqual([0, 550]); }); }); - -describe('getAlertReferenceLines', () => { - // Recharts v3 fills an open-ended ReferenceArea from the set bound to the - // opposite edge: a set y2 (with y1 omitted) fills up to the top, and a set - // y1 (with y2 omitted) fills down to the bottom. These tests pin which bound - // each threshold type sets so the red firing region stays on the correct - // side of the threshold. - it.each([AlertThresholdType.ABOVE, AlertThresholdType.ABOVE_EXCLUSIVE])( - 'fills up from the threshold for %s', - thresholdType => { - const area = getAlertReferenceLines({ - threshold: 50, - thresholdMax: undefined, - thresholdType, - }) as ReactElement<{ y1?: number; y2?: number }>; - expect(isValidElement(area)).toBe(true); - expect(area.props.y2).toBe(50); - expect(area.props.y1).toBeUndefined(); - }, - ); - - it.each([AlertThresholdType.BELOW, AlertThresholdType.BELOW_OR_EQUAL])( - 'fills from 0 up to the threshold for %s', - thresholdType => { - const area = getAlertReferenceLines({ - threshold: 50, - thresholdMax: undefined, - thresholdType, - }) as ReactElement<{ y1?: number; y2?: number }>; - expect(area.props.y1).toBe(0); - expect(area.props.y2).toBe(50); - }, - ); - - it('fills the band between the bounds for BETWEEN', () => { - const area = getAlertReferenceLines({ - threshold: 10, - thresholdMax: 20, - thresholdType: AlertThresholdType.BETWEEN, - }) as ReactElement<{ y1?: number; y2?: number }>; - expect(area.props.y1).toBe(10); - expect(area.props.y2).toBe(20); - }); - - it('fills below the lower bound and above the upper bound for NOT_BETWEEN', () => { - const areas = getAlertReferenceLines({ - threshold: 10, - thresholdMax: 20, - thresholdType: AlertThresholdType.NOT_BETWEEN, - }) as ReactElement<{ y1?: number; y2?: number }>[]; - expect(Array.isArray(areas)).toBe(true); - - const [lower, upper] = areas; - // Lower region: fills down from the lower bound (y2 omitted -> bottom). - expect(lower.props.y1).toBe(10); - expect(lower.props.y2).toBeUndefined(); - // Upper region: fills up from the upper bound (y1 omitted -> top). - expect(upper.props.y2).toBe(20); - expect(upper.props.y1).toBeUndefined(); - }); -}); diff --git a/packages/app/src/components/__tests__/getAlertReferenceLines.test.tsx b/packages/app/src/components/__tests__/getAlertReferenceLines.test.tsx new file mode 100644 index 0000000000..02ced709a9 --- /dev/null +++ b/packages/app/src/components/__tests__/getAlertReferenceLines.test.tsx @@ -0,0 +1,101 @@ +import React from 'react'; +import { Line, LineChart, ReferenceLine, XAxis, YAxis } from 'recharts'; +import { AlertThresholdType } from '@hyperdx/common-utils/dist/types'; +import { render } from '@testing-library/react'; + +import { getAlertReferenceLines } from '@/components/Alerts'; + +type Rect = { top: number; bottom: number }; + +function renderAreas( + thresholdType: AlertThresholdType, + threshold: number, + thresholdMax?: number, +): { rects: Rect[]; thresholdY: number; thresholdMaxY?: number } { + const { container } = render( + + + + + {getAlertReferenceLines({ threshold, thresholdMax, thresholdType })} + + {thresholdMax != null ? : null} + , + ); + + const rects = Array.from( + container.querySelectorAll('.recharts-reference-area-rect'), + ).map(r => { + const top = Number(r.getAttribute('y')); + return { top, bottom: top + Number(r.getAttribute('height')) }; + }); + const lineYs = Array.from( + container.querySelectorAll('.recharts-reference-line-line'), + ).map(l => Number(l.getAttribute('y1'))); + + return { + rects, + thresholdY: lineYs[0], + thresholdMaxY: lineYs[1], + }; +} + +const EPS = 1; + +describe('getAlertReferenceLines shading', () => { + it.each([AlertThresholdType.ABOVE, AlertThresholdType.ABOVE_EXCLUSIVE])( + 'shades above the threshold line for %s', + thresholdType => { + const { rects, thresholdY } = renderAreas(thresholdType, 50); + expect(rects).toHaveLength(1); + expect(rects[0].top).toBeLessThan(thresholdY); + expect(rects[0].bottom).toBeLessThanOrEqual(thresholdY + EPS); + }, + ); + + it.each([AlertThresholdType.BELOW, AlertThresholdType.BELOW_OR_EQUAL])( + 'shades below the threshold line for %s', + thresholdType => { + const { rects, thresholdY } = renderAreas(thresholdType, 50); + expect(rects).toHaveLength(1); + expect(rects[0].top).toBeGreaterThanOrEqual(thresholdY - EPS); + expect(rects[0].bottom).toBeGreaterThan(thresholdY); + }, + ); + + it('shades the band between the two thresholds for BETWEEN', () => { + const { rects, thresholdY, thresholdMaxY } = renderAreas( + AlertThresholdType.BETWEEN, + 30, + 70, + ); + expect(rects).toHaveLength(1); + expect(thresholdMaxY).toBeDefined(); + expect(rects[0].top).toBeCloseTo(thresholdMaxY!, 0); + expect(rects[0].bottom).toBeCloseTo(thresholdY, 0); + }); + + it('shades below the lower bound and above the upper bound for NOT_BETWEEN', () => { + const { rects, thresholdY, thresholdMaxY } = renderAreas( + AlertThresholdType.NOT_BETWEEN, + 30, + 70, + ); + expect(rects).toHaveLength(2); + expect(thresholdMaxY).toBeDefined(); + + const lower = rects.find(r => r.bottom > thresholdY)!; + const upper = rects.find(r => r.top < thresholdMaxY!)!; + // Lower firing region: from the lower threshold line downward. + expect(lower.top).toBeCloseTo(thresholdY, 0); + // Upper firing region: from the upper threshold line upward. + expect(upper.bottom).toBeCloseTo(thresholdMaxY!, 0); + }); +});