import { describe, expect, test } from "bun:test"; import type { TrendPoint } from "../../../src/shared/api"; import { computeTrendStats } from "../../../src/web/utils/stats"; describe("computeTrendStats", () => { test("空趋势返回 0 统计", () => { expect(computeTrendStats([])).toEqual({ downChecks: 0, totalChecks: 0, upChecks: 0 }); }); test("汇总总检查、正常和异常数量", () => { const points: TrendPoint[] = [ { availability: 80, avgDurationMs: 100, hour: "2025-01-01T00:00:00.000Z", totalChecks: 10 }, { availability: 40, avgDurationMs: 200, hour: "2025-01-01T01:00:00.000Z", totalChecks: 5 }, ]; expect(computeTrendStats(points)).toEqual({ downChecks: 5, totalChecks: 15, upChecks: 10 }); }); test("按每个趋势点四舍五入正常数量", () => { const points: TrendPoint[] = [ { availability: 33.3, avgDurationMs: null, hour: "2025-01-01T00:00:00.000Z", totalChecks: 3 }, { availability: 66.7, avgDurationMs: null, hour: "2025-01-01T01:00:00.000Z", totalChecks: 3 }, ]; expect(computeTrendStats(points)).toEqual({ downChecks: 3, totalChecks: 6, upChecks: 3 }); }); });