48 lines
2.7 KiB
TypeScript
48 lines
2.7 KiB
TypeScript
import assert from "node:assert/strict";
|
||
import test from "node:test";
|
||
|
||
import { clockLabel, distanceText, minutesFrom, otherSide, rangeLabel, windowAround } from "../src/lib/chart-choice/view.ts";
|
||
|
||
// 生时纠正(选盘页,2026-11-06)的纯函数。时间都是虚构的本地钟点。
|
||
|
||
test("clockLabel drops the date on the same day and keeps it across midnight", () => {
|
||
assert.equal(clockLabel("1997-08-08T04:59:43"), "04:59");
|
||
assert.equal(clockLabel("1997-08-08T04:59:43", undefined, true), "04:59:43");
|
||
assert.equal(clockLabel("1997-08-08T23:40", "1997-08-08T00:10"), "23:40");
|
||
assert.equal(clockLabel("1997-08-07T23:40", "1997-08-08T00:10"), "8月7日 23:40");
|
||
});
|
||
|
||
test("rangeLabel joins two clocks with an en dash", () => {
|
||
assert.equal(rangeLabel({ start_at: "1997-08-08T04:45", end_at: "1997-08-08T05:15" }), "04:45–05:15");
|
||
});
|
||
|
||
test("windowAround is symmetric and crosses midnight", () => {
|
||
assert.deepEqual(windowAround("1997-08-08T05:00", 15), { start_at: "1997-08-08T04:45", end_at: "1997-08-08T05:15" });
|
||
assert.deepEqual(windowAround("1997-08-08T00:10", 30), { start_at: "1997-08-07T23:40", end_at: "1997-08-08T00:40" });
|
||
});
|
||
|
||
test("minutesFrom accepts minute and second stamps; only differences matter", () => {
|
||
assert.equal(minutesFrom("1997-08-08T05:00") - minutesFrom("1997-08-08T04:45"), 15);
|
||
assert.equal(minutesFrom("1997-08-08T05:00:30") - minutesFrom("1997-08-08T05:00"), 0.5);
|
||
assert.equal(minutesFrom("1997-08-08T00:00") - minutesFrom("1997-08-07T23:00"), 60);
|
||
});
|
||
|
||
test("distanceText says seconds under a minute and rounded minutes above", () => {
|
||
assert.equal(distanceText(17), "17 秒");
|
||
assert.equal(distanceText(60), "1 分钟");
|
||
assert.equal(distanceText(150), "3 分钟");
|
||
});
|
||
|
||
test("otherSide flips the side", () => {
|
||
assert.equal(otherSide("before"), "after");
|
||
assert.equal(otherSide("after"), "before");
|
||
});
|
||
|
||
test("BUG-1654: a history row says how the side was decided and which range was used", async () => {
|
||
const { recordSummary } = await import("../src/lib/chart-choice/view.ts");
|
||
const window = { start_at: "1997-08-08T04:45", end_at: "1997-08-08T05:15" };
|
||
assert.equal(recordSummary({ chosen_side: "before", rule: "majority", detail: { window, decision: { votes: { before: 3, after: 2, unsure: 0 } } } }), "5 题 3 比 2,范围前后 15 分钟");
|
||
assert.equal(recordSummary({ chosen_side: "after", rule: "user_override", detail: { window, decision: { votes: null } } }), "直接选了线后,范围前后 15 分钟");
|
||
assert.equal(recordSummary({ chosen_side: "none", rule: "no_line_in_range", detail: { window: { start_at: "1997-08-08T04:00", end_at: "1997-08-08T06:00" }, decision: { votes: null } } }), "范围里没有分界线,范围前后 1 小时");
|
||
});
|