import { test, expect, describe } from "bun:test"; import { niceScale, isWeekendDay, toggleSeries, peakOfVisible, SERIES_ALL_ON, createdOf, runsOf, landedOf, allTrendKinds, defaultTrendKinds, toggleTrendKind, isAllTrendKinds, type TrendKind } from "../src/components/TrendChart"; import type { DailyTrendPoint } from "../src/lib/api"; import type { BoardItem } from "../src/types"; import { overlayDailyLanded, accumulateTrend, cnDayKey, allTimeSpanDays } from "../src/lib/trendSeries"; /** * Y 轴刻度。这块出错不会报任何异常,只是把 86 画到一根 "87" 的顶格线上,或者 * 让刻度落在 33.33 这种数上——图还是图,只是读不出数。 */ describe("niceScale", () => { test("刻度落在整数上,且上界覆盖峰值", () => { for (const peak of [1, 3, 7, 12, 46, 77, 86, 130, 512, 2400]) { const { max, step } = niceScale(peak); expect(max).toBeGreaterThanOrEqual(peak); // 上界必须是步长的整数倍,否则最后一根网格线画不到顶。 expect(Math.abs(max / step - Math.round(max / step))).toBeLessThan(1e-9); // 计数图的步长必须是整数,否则轴上会出现“2.5 个任务”。 expect(Number.isInteger(step)).toBe(true); // 3~5 格:太少读不出量级,太多网格盖过数据。 const ticks = max / step; expect(ticks).toBeGreaterThanOrEqual(3); expect(ticks).toBeLessThanOrEqual(5); } }); test("全 0 的一周也给出可画的轴,而不是除零", () => { const { max, step } = niceScale(0); expect(max).toBeGreaterThan(0); expect(step).toBeGreaterThan(0); }); test("常见峰值给出的是人读得懂的整数刻度", () => { expect(niceScale(86)).toEqual({ max: 100, step: 25 }); expect(niceScale(46)).toEqual({ max: 60, step: 20 }); expect(niceScale(12)).toEqual({ max: 15, step: 5 }); }); }); describe("toggleSeries / peakOfVisible — 图例开关", () => { test("关掉一条,不能把最后一条也关掉", () => { const one = toggleSeries(SERIES_ALL_ON, "runs"); expect(one).toEqual({ created: true, runs: false, landed: true }); const two = toggleSeries(one, "created"); expect(two).toEqual({ created: false, runs: false, landed: true }); expect(toggleSeries(two, "landed")).toEqual(two); }); test("峰值只看仍显示的系列,关掉高线后 Y 轴收缩", () => { const created = [1, 2, 3]; const runs = [10, 80, 40]; const landed = [0, 1, 2]; expect(peakOfVisible(created, runs, landed, SERIES_ALL_ON)).toBe(80); expect(peakOfVisible(created, runs, landed, { created: true, runs: false, landed: true })).toBe(3); expect(peakOfVisible(created, runs, landed, { created: false, runs: false, landed: true })).toBe(2); }); }); describe("isWeekendDay — 按 ISO 日历日,不跟本机时区走", () => { test("周六日为 true,工作日为 false", () => { // 2026-01-02 周五 … 01-05 周一。用 UTC 构造,避免 CI 在 UTC-7 上把 // `new Date("YYYY-MM-DD").getDay()` 提前一天。 expect(isWeekendDay("2026-01-02")).toBe(false); // Fri expect(isWeekendDay("2026-01-03")).toBe(true); // Sat expect(isWeekendDay("2026-01-04")).toBe(true); // Sun expect(isWeekendDay("2026-01-05")).toBe(false); // Mon }); test("坏日期不当周末", () => { expect(isWeekendDay("")).toBe(false); expect(isWeekendDay("nope")).toBe(false); }); }); function pt(over: Partial & { day: string }): DailyTrendPoint { return { created: 0, createdByKind: { bug: 0, feature: 0, security: 0 }, runs: 0, runsByKind: { bug: 0, feature: 0, security: 0 }, ...over, }; } function bugItem(over: Record): BoardItem { return { kind: "bug", id: "b", title: "t", platform: "android", status: "fix_succeeded", attempts: 0, createdAt: 1, ...over, } as BoardItem; } function featItem(over: Record): BoardItem { return { kind: "feature", id: "f", title: "t", platform: "ios", status: "completed", attempts: 0, createdAt: 1, ...over, } as BoardItem; } describe("cnDayKey — 与 stats_daily 同一套 UTC+8 日界", () => { test("北京午夜前一秒还是前一天", () => { const midnight = Date.UTC(2026, 7, 12, 16, 0, 0); // 2026-08-13 00:00 CST expect(cnDayKey(midnight)).toBe("2026-08-13"); expect(cnDayKey(midnight - 1)).toBe("2026-08-12"); }); }); describe("allTimeSpanDays — 全期从最早 createdAt 记到今天", () => { test("Jun 1 … Sep 2 含首尾 94 天", () => { const now = Date.UTC(2026, 8, 2, 8, 0, 0); // 2026-09-02 16:00 CST const first = Date.UTC(2026, 5, 1, 4, 0, 0); // 2026-06-01 12:00 CST expect(allTimeSpanDays([{ createdAt: first }], now)).toBe(94); }); test("没有时间戳时至少两天,好画线", () => { expect(allTimeSpanDays([])).toBe(2); }); }); describe("overlayDailyLanded — 生产 API 还没有 merged 时从看板补", () => { test("缺字段时按 mrMergedAt / finishedAt 填当天", () => { // 2026-08-12 20:00 CST = 12:00 UTC const tue = Date.UTC(2026, 7, 12, 12, 0, 0); const wed = Date.UTC(2026, 7, 13, 12, 0, 0); const points = [ pt({ day: "2026-08-12" }), pt({ day: "2026-08-13" }), pt({ day: "2026-08-14" }), ]; const items: BoardItem[] = [ bugItem({ id: "m1", mrMergedAt: tue, mrState: "merged", meegoState: "done" }), bugItem({ id: "m2", mrMergedAt: tue, mrState: "merged", meegoState: "done" }), bugItem({ id: "m3", mrMergedAt: wed, mrState: "merged", meegoState: "done" }), featItem({ id: "f1", finishedAt: tue }), bugItem({ id: "qa1", status: "fix_succeeded", mrState: "opened", meegoState: "resolved", finishedAt: tue, }), bugItem({ id: "u1", status: "unreproducible", meegoState: "done", finishedAt: tue, }), ]; const out = overlayDailyLanded(points, items); expect(out.map((p) => p.merged)).toEqual([2, 1, 0]); expect(out[0].completedByKind).toEqual({ feature: 1, security: 0 }); expect(out[1].completedByKind).toEqual({ feature: 0, security: 0 }); expect(out.map((p) => p.finishedByKind?.bug)).toEqual([4, 1, 0]); expect(out.map((p) => p.finishedByKind?.feature)).toEqual([1, 0, 0]); }); test("API 已给出数字时不覆盖(含 0)", () => { const points = [pt({ day: "2026-08-12", merged: 0, completed: 3, completedByKind: { feature: 2, security: 1 } })]; const items: BoardItem[] = [bugItem({ mrMergedAt: Date.UTC(2026, 7, 12, 12, 0, 0) })]; const out = overlayDailyLanded(points, items); expect(out[0].merged).toBe(0); expect(out[0].completed).toBe(3); expect(out[0].completedByKind).toEqual({ feature: 2, security: 1 }); }); }); describe("accumulateTrend — 按日累加,null 批次不断成 0", () => { test("创建 / 合入累加,周末也留着", () => { const points = [ pt({ day: "2026-08-14", created: 2, merged: 1 }), // Fri pt({ day: "2026-08-15", created: 0, merged: 1 }), // Sat pt({ day: "2026-08-16", created: 3, merged: 0 }), // Sun ]; const out = accumulateTrend(points); expect(out.map((p) => p.created)).toEqual([2, 2, 5]); expect(out.map((p) => p.merged)).toEqual([1, 2, 2]); expect(out.map((p) => p.day)).toEqual(["2026-08-14", "2026-08-15", "2026-08-16"]); }); test("ledger 之前 runs 保持 null,从第一天有数开始加", () => { const points = [ pt({ day: "2026-07-21", runs: null, runsByKind: null, created: 1 }), pt({ day: "2026-07-22", runs: null, runsByKind: null, created: 1 }), pt({ day: "2026-07-23", runs: 4, runsByKind: { bug: 4, feature: 0, security: 0 }, created: 1 }), pt({ day: "2026-07-24", runs: 6, runsByKind: { bug: 1, feature: 5, security: 0 }, created: 1 }), ]; const out = accumulateTrend(points); expect(out.map((p) => p.runs)).toEqual([null, null, 4, 10]); expect(out[2].runsByKind).toEqual({ bug: 4, feature: 0, security: 0 }); expect(out[3].runsByKind).toEqual({ bug: 5, feature: 5, security: 0 }); expect(out.map((p) => p.created)).toEqual([1, 2, 3, 4]); }); }); describe("createdOf / runsOf / landedOf — 搜证加进全部,完成数=创建数", () => { const p = pt({ day: "2026-09-01", created: 10, createdByKind: { bug: 4, feature: 3, security: 3 }, runs: 20, runsByKind: { bug: 8, feature: 7, security: 5 }, merged: 2, completedByKind: { feature: 1, security: 0 }, finishedByKind: { bug: 2, feature: 1, security: 0 }, }); const ALL = allTrendKinds(); const one = (k: TrendKind) => new Set([k]); test("全部:创建、批次含搜证", () => { expect(createdOf(p, ALL)).toBe(10); expect(runsOf(p, ALL)).toBe(20); }); test("单选与多选:按所选类型相加", () => { expect(createdOf(p, one("bug"))).toBe(4); expect(createdOf(p, new Set(["bug", "feature"]))).toBe(7); expect(runsOf(p, new Set(["bug", "security"]))).toBe(13); }); test("完成线:bug/需求用 finished,搜证用当天创建数", () => { expect(landedOf(p, one("bug"))).toBe(2); expect(landedOf(p, one("feature"))).toBe(1); expect(landedOf(p, one("security"))).toBe(3); expect(landedOf(p, ALL)).toBe(2 + 1 + 3); expect(landedOf(p, new Set(["bug", "security"]))).toBe(2 + 3); }); }); describe("toggleTrendKind — 与看板类型筛选同一套", () => { test("默认是 Bugfix + Feature,不含搜证", () => { const d = defaultTrendKinds(); expect(d.has("bug")).toBe(true); expect(d.has("feature")).toBe(true); expect(d.has("security")).toBe(false); expect(isAllTrendKinds(d)).toBe(false); }); test("从全部点一项就收成只看该项", () => { const next = toggleTrendKind(allTrendKinds(), "bug"); expect([...next]).toEqual(["bug"]); }); test("再点另一项是并上,不是互斥", () => { const next = toggleTrendKind(new Set(["bug"]), "feature"); expect(next.has("bug")).toBe(true); expect(next.has("feature")).toBe(true); expect(next.size).toBe(2); }); test("去掉最后一项回到全部", () => { expect(isAllTrendKinds(toggleTrendKind(new Set(["security"]), "security"))).toBe(true); }); });