|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + buildReportLayout, |
| 4 | + type LayoutMetricInput, |
| 5 | + type LayoutViewModel, |
| 6 | + REPORT_GLYPH, |
| 7 | +} from "~/presenters/v3/reports/report-layout"; |
| 8 | +import { reportMessages } from "~/presenters/v3/reports/report-messages"; |
| 9 | +import { delta } from "~/presenters/v3/reports/report-view-model"; |
| 10 | + |
| 11 | +const messages = reportMessages("health"); |
| 12 | + |
| 13 | +function viewModel(overrides: Partial<LayoutViewModel> = {}): LayoutViewModel { |
| 14 | + return { |
| 15 | + title: "health", |
| 16 | + scope: "prod", |
| 17 | + period: "last 60m", |
| 18 | + windowMinutes: 60, |
| 19 | + summary: { severity: "warn", statements: [{ findingType: "flow", severity: "warn" }] }, |
| 20 | + findings: [{ type: "flow", severity: "warn", reason: "backlog_growing", metricIds: [] }], |
| 21 | + metrics: [], |
| 22 | + footer: [], |
| 23 | + ...overrides, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +/** Builds the metric the way the presenter does, so the rounded multiplier is the real one. */ |
| 28 | +function metricRow(metric: Omit<LayoutMetricInput, "delta">) { |
| 29 | + const withDelta: LayoutMetricInput = { ...metric, delta: delta(metric.value, metric.normal) }; |
| 30 | + const layout = buildReportLayout( |
| 31 | + viewModel({ |
| 32 | + findings: [ |
| 33 | + { type: "flow", severity: "warn", reason: "backlog_growing", metricIds: [metric.id] }, |
| 34 | + ], |
| 35 | + metrics: [withDelta], |
| 36 | + }), |
| 37 | + messages |
| 38 | + ); |
| 39 | + const rows = [...(layout.hero?.metrics ?? []), ...layout.findings.flatMap((f) => f.metrics)]; |
| 40 | + const row = rows.find((r) => r.id === metric.id); |
| 41 | + if (!row) throw new Error(`metric row ${metric.id} was not rendered`); |
| 42 | + return row; |
| 43 | +} |
| 44 | + |
| 45 | +describe("report layout — a metric's movement against its baseline", () => { |
| 46 | + it("renders a collapse as a down arrow with how far it fell", () => { |
| 47 | + const row = metricRow({ |
| 48 | + id: "throughput", |
| 49 | + value: 5, |
| 50 | + normal: 100, |
| 51 | + unit: "perMin", |
| 52 | + severity: "crit", |
| 53 | + }); |
| 54 | + |
| 55 | + expect(row.delta).toEqual({ text: `${REPORT_GLYPH.down} 20×`, dir: "down" }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("renders a collapse to zero as a bare down arrow", () => { |
| 59 | + const row = metricRow({ |
| 60 | + id: "throughput", |
| 61 | + value: 0, |
| 62 | + normal: 100, |
| 63 | + unit: "perMin", |
| 64 | + severity: "crit", |
| 65 | + }); |
| 66 | + |
| 67 | + expect(row.delta).toEqual({ text: REPORT_GLYPH.down, dir: "down" }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("leaves a dip that doesn't round past 1× reading as flat", () => { |
| 71 | + const row = metricRow({ |
| 72 | + id: "throughput", |
| 73 | + value: 95, |
| 74 | + normal: 100, |
| 75 | + unit: "perMin", |
| 76 | + severity: "ok", |
| 77 | + }); |
| 78 | + |
| 79 | + expect(row.delta).toEqual({ text: `${REPORT_GLYPH.flat} flat`, dir: "flat" }); |
| 80 | + }); |
| 81 | + |
| 82 | + it("still renders a rise with its multiplier", () => { |
| 83 | + const row = metricRow({ |
| 84 | + id: "pending", |
| 85 | + value: 1600, |
| 86 | + normal: 100, |
| 87 | + unit: "count", |
| 88 | + severity: "crit", |
| 89 | + }); |
| 90 | + |
| 91 | + expect(row.delta).toEqual({ text: `${REPORT_GLYPH.up} 16×`, dir: "up" }); |
| 92 | + }); |
| 93 | + |
| 94 | + it("still renders an unmoved metric as flat", () => { |
| 95 | + const row = metricRow({ |
| 96 | + id: "pending", |
| 97 | + value: 100, |
| 98 | + normal: 100, |
| 99 | + unit: "count", |
| 100 | + severity: "ok", |
| 101 | + }); |
| 102 | + |
| 103 | + expect(row.delta).toEqual({ text: `${REPORT_GLYPH.flat} flat`, dir: "flat" }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("says nothing about a metric with no baseline", () => { |
| 107 | + const row = metricRow({ id: "pending", value: 100, unit: "count", severity: "ok" }); |
| 108 | + |
| 109 | + expect(row.delta).toBeUndefined(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments