|
| 1 | +import { |
| 2 | + isBaselineMetricRow, |
| 3 | + isNumericHistogram, |
| 4 | + selectLatestWindowMetric, |
| 5 | + rebinNumericHistogram, |
| 6 | + histogramDensities, |
| 7 | +} from "./histogramUtils"; |
| 8 | +import type { NumericHistogram } from "../../../queries/useMonitoringApi"; |
| 9 | + |
| 10 | +const windowRow = (overrides: Record<string, unknown>) => ({ |
| 11 | + granularity: "daily", |
| 12 | + is_baseline: false, |
| 13 | + metric_date: "2026-10-18", |
| 14 | + row_count: 100, |
| 15 | + ...overrides, |
| 16 | +}); |
| 17 | + |
| 18 | +describe("isBaselineMetricRow", () => { |
| 19 | + it("marks rows by is_baseline flag or baseline granularity", () => { |
| 20 | + expect( |
| 21 | + isBaselineMetricRow({ granularity: "baseline", is_baseline: true }), |
| 22 | + ).toBe(true); |
| 23 | + expect( |
| 24 | + isBaselineMetricRow({ granularity: "baseline", is_baseline: false }), |
| 25 | + ).toBe(true); |
| 26 | + expect(isBaselineMetricRow({ granularity: "daily" })).toBe(false); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("selectLatestWindowMetric", () => { |
| 31 | + it("never picks the baseline row even when it has the newest metric_date", () => { |
| 32 | + // The baseline is dated by computation day (today), windows by start (#7054). |
| 33 | + const baseline = windowRow({ |
| 34 | + granularity: "baseline", |
| 35 | + is_baseline: true, |
| 36 | + metric_date: "2026-10-20", |
| 37 | + row_count: 1807, |
| 38 | + }); |
| 39 | + const daily = windowRow({ metric_date: "2026-10-18" }); |
| 40 | + const weekly = windowRow({ |
| 41 | + granularity: "weekly", |
| 42 | + metric_date: "2026-10-12", |
| 43 | + }); |
| 44 | + expect(selectLatestWindowMetric([baseline, daily, weekly])).toBe(daily); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns null when only baseline rows exist", () => { |
| 48 | + const baseline = windowRow({ granularity: "baseline", is_baseline: true }); |
| 49 | + expect(selectLatestWindowMetric([baseline])).toBeNull(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("prefers rows with data, like the previous selection did", () => { |
| 53 | + const empty = windowRow({ metric_date: "2026-10-19", row_count: 0 }); |
| 54 | + const withData = windowRow({ metric_date: "2026-10-18", row_count: 12 }); |
| 55 | + expect(selectLatestWindowMetric([empty, withData])).toBe(withData); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns null for empty input", () => { |
| 59 | + expect(selectLatestWindowMetric([])).toBeNull(); |
| 60 | + expect(selectLatestWindowMetric(null)).toBeNull(); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe("isNumericHistogram", () => { |
| 65 | + it("distinguishes numeric and categorical payloads", () => { |
| 66 | + expect(isNumericHistogram({ bins: [0, 1], counts: [5], bin_width: 1 })).toBe( |
| 67 | + true, |
| 68 | + ); |
| 69 | + expect( |
| 70 | + isNumericHistogram({ |
| 71 | + values: [{ value: "a", count: 3 }], |
| 72 | + other_count: 0, |
| 73 | + unique_count: 1, |
| 74 | + }), |
| 75 | + ).toBe(false); |
| 76 | + expect(isNumericHistogram(null)).toBe(false); |
| 77 | + expect(isNumericHistogram(undefined)).toBe(false); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe("rebinNumericHistogram", () => { |
| 82 | + it("maps a differently-ranged baseline onto the window's bin edges", () => { |
| 83 | + // Baseline covers [0, 2] in 2 bins of width 1; window covers [0, 1] in 2 |
| 84 | + // bins of width 0.5 (the x100-unit-bug case from #7054, inverted). |
| 85 | + const baseline: NumericHistogram = { |
| 86 | + bins: [0, 1], |
| 87 | + counts: [10, 30], |
| 88 | + bin_width: 1, |
| 89 | + }; |
| 90 | + const window: NumericHistogram = { |
| 91 | + bins: [0, 0.5], |
| 92 | + counts: [1, 1], |
| 93 | + bin_width: 0.5, |
| 94 | + }; |
| 95 | + const rebinned = rebinNumericHistogram(baseline, window); |
| 96 | + // window bin [0,0.5) overlaps half of baseline bin [0,1): 5 |
| 97 | + // window bin [0.5,1) overlaps the other half of [0,1): 5 |
| 98 | + expect(rebinned[0]).toBeCloseTo(5); |
| 99 | + expect(rebinned[1]).toBeCloseTo(5); |
| 100 | + // Total mass is preserved when the target covers the source range. |
| 101 | + expect(rebinned[0] + rebinned[1]).toBeCloseTo(10); |
| 102 | + }); |
| 103 | + |
| 104 | + it("keeps identical bins identical", () => { |
| 105 | + const h: NumericHistogram = { bins: [0, 1, 2], counts: [3, 7], bin_width: 1 }; |
| 106 | + expect(rebinNumericHistogram(h, h)).toEqual([3, 7]); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe("histogramDensities", () => { |
| 111 | + it("normalizes to shares of the total so window size stops mattering", () => { |
| 112 | + expect(histogramDensities([125, 125])).toEqual([0.5, 0.5]); |
| 113 | + expect(histogramDensities([1807, 1807])).toEqual([0.5, 0.5]); |
| 114 | + expect(histogramDensities([0, 0])).toEqual([0, 0]); |
| 115 | + }); |
| 116 | +}); |
0 commit comments