Initial import of streamline
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
// MUST be first: points DATABASE_URL at a closed local port before db_client is
|
||||
// evaluated, so importing the module under test can't reach production.
|
||||
import "./setup_env";
|
||||
|
||||
import { test, expect, describe } from "bun:test";
|
||||
import { cnDayKey, clampDays, parseTrendDays, trendWindow } from "../src/stats_daily";
|
||||
|
||||
/**
|
||||
* 只测纯函数部分:日界怎么划、窗口参数怎么收敛。这两处一错,整张趋势图会整体
|
||||
* 平移一天或者悄悄退化成默认窗口,而图看上去仍然“正常”——正是最难靠肉眼发现的
|
||||
* 那一类错误。聚合本身要连库,留给 scripts 里的探针。
|
||||
*/
|
||||
describe("cnDayKey — 日界按 Asia/Shanghai(UTC+8) 划", () => {
|
||||
test("北京时间当天 00:00 归当天,前一毫秒归前一天", () => {
|
||||
// 2026-08-12T00:00:00+08:00 === 2026-08-11T16:00:00Z
|
||||
const midnight = Date.UTC(2026, 7, 11, 16, 0, 0);
|
||||
expect(cnDayKey(midnight)).toBe("2026-08-12");
|
||||
expect(cnDayKey(midnight - 1)).toBe("2026-08-11");
|
||||
});
|
||||
|
||||
test("北京时间 23:59:59 仍是当天(不因 UTC 已翻页而提前)", () => {
|
||||
// 2026-08-12T23:59:59+08:00 === 2026-08-12T15:59:59Z — UTC 还在 08-12,
|
||||
// 但下一秒 UTC 就翻到 08-13 了,naive 的 toISOString 会在这里错开一天。
|
||||
expect(cnDayKey(Date.UTC(2026, 7, 12, 15, 59, 59))).toBe("2026-08-12");
|
||||
});
|
||||
|
||||
test("UTC 已是次日、北京时间也是次日的那一段,两边一致", () => {
|
||||
// 2026-08-12T18:00:00Z === 2026-08-13T02:00:00+08:00
|
||||
expect(cnDayKey(Date.UTC(2026, 7, 12, 18, 0, 0))).toBe("2026-08-13");
|
||||
});
|
||||
});
|
||||
|
||||
describe("clampDays", () => {
|
||||
test("正常值原样通过", () => {
|
||||
expect(clampDays(14)).toBe(14);
|
||||
expect(clampDays("90")).toBe(90);
|
||||
});
|
||||
|
||||
test("越界收敛到 [1, 365]", () => {
|
||||
expect(clampDays(0)).toBe(1);
|
||||
expect(clampDays(-5)).toBe(1);
|
||||
expect(clampDays(10_000)).toBe(365);
|
||||
});
|
||||
|
||||
test("非数字退回默认窗口,而不是 NaN 天", () => {
|
||||
expect(clampDays(undefined)).toBe(30);
|
||||
expect(clampDays("abc")).toBe(30);
|
||||
expect(clampDays(null, 14)).toBe(14);
|
||||
});
|
||||
|
||||
test("空的 ?days= 退回默认窗口,而不是被 Number(\"\")===0 夹成 1 天", () => {
|
||||
expect(clampDays("")).toBe(30);
|
||||
expect(clampDays(" ")).toBe(30);
|
||||
});
|
||||
|
||||
test("小数向下取整(下游要拿它算数组长度)", () => {
|
||||
expect(clampDays(7.9)).toBe(7);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseTrendDays / trendWindow — 全期从第一天起", () => {
|
||||
test("?days=all 原样通过,大小写不敏感", () => {
|
||||
expect(parseTrendDays("all")).toBe("all");
|
||||
expect(parseTrendDays("ALL")).toBe("all");
|
||||
});
|
||||
|
||||
test("缺参 / 空串默认全期", () => {
|
||||
expect(parseTrendDays(undefined)).toBe("all");
|
||||
expect(parseTrendDays("")).toBe("all");
|
||||
});
|
||||
|
||||
test("数字窗口仍走 clampDays", () => {
|
||||
expect(parseTrendDays("90")).toBe(90);
|
||||
expect(parseTrendDays(30)).toBe(30);
|
||||
});
|
||||
|
||||
test("全期 span 从 earliest 的北京日算到今天", () => {
|
||||
// 2026-09-02 16:00 CST
|
||||
const now = Date.UTC(2026, 8, 2, 8, 0, 0);
|
||||
const first = Date.UTC(2026, 5, 1, 4, 0, 0); // 2026-06-01 12:00 CST
|
||||
const { since, span } = trendWindow(now, "all", first);
|
||||
expect(cnDayKey(since)).toBe("2026-06-01");
|
||||
expect(cnDayKey(since + (span - 1) * 86_400_000)).toBe("2026-09-02");
|
||||
expect(span).toBe(94); // Jun 1 … Sep 2 inclusive
|
||||
});
|
||||
|
||||
test("没有 earliest 时全期退化为今天一天", () => {
|
||||
const now = Date.UTC(2026, 8, 2, 8, 0, 0);
|
||||
const { span } = trendWindow(now, "all", null);
|
||||
expect(span).toBe(1);
|
||||
});
|
||||
|
||||
test("30 天窗口不看 earliest", () => {
|
||||
const now = Date.UTC(2026, 8, 2, 8, 0, 0);
|
||||
const { span } = trendWindow(now, 30, Date.UTC(2020, 0, 1));
|
||||
expect(span).toBe(30);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user