85 lines
2.4 KiB
TypeScript
85 lines
2.4 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import type { HydrogenDailyRow } from '../types.js';
|
|
import {
|
|
getQuickRange,
|
|
getRangeModeLabel,
|
|
normalizeRange,
|
|
summarizeHydrogenRows,
|
|
} from './model.js';
|
|
|
|
test('快捷日期继续使用本地自然日并覆盖本周、本月和近15天', () => {
|
|
const now = new Date(2026, 7, 12, 23, 30);
|
|
assert.deepEqual(getQuickRange('thisWeek', now), {
|
|
start: '2026-08-10',
|
|
end: '2026-08-12',
|
|
});
|
|
assert.deepEqual(getQuickRange('thisMonth', now), {
|
|
start: '2026-08-01',
|
|
end: '2026-08-12',
|
|
});
|
|
assert.deepEqual(getQuickRange('last15', now), {
|
|
start: '2026-07-29',
|
|
end: '2026-08-12',
|
|
});
|
|
});
|
|
|
|
test('自定义日期倒置时仅交换查询边界', () => {
|
|
assert.deepEqual(normalizeRange('2026-08-12', '2026-08-01'), {
|
|
start: '2026-08-01',
|
|
end: '2026-08-12',
|
|
});
|
|
assert.equal(getRangeModeLabel('custom'), '自定义区间');
|
|
assert.equal(getRangeModeLabel('last15'), '近 15 天');
|
|
});
|
|
|
|
test('每日加氢统计保持排序、有效天、站点去重和峰谷口径', () => {
|
|
const rows: HydrogenDailyRow[] = [
|
|
{
|
|
date: '2026-08-12',
|
|
totalKg: 0,
|
|
chainPct: -1,
|
|
customerType: 'lingniu',
|
|
stations: [{ name: 'A站', kg: 0, pricePerKg: 0, chainPct: -1 }],
|
|
},
|
|
{
|
|
date: '2026-08-10',
|
|
totalKg: 100,
|
|
chainPct: 0,
|
|
customerType: 'lingniu',
|
|
stations: [{ name: 'A站', kg: 100, pricePerKg: 20, chainPct: 0 }],
|
|
},
|
|
{
|
|
date: '2026-08-11',
|
|
totalKg: 300,
|
|
chainPct: 2,
|
|
customerType: 'lingniu',
|
|
stations: [{ name: 'B站', kg: 300, pricePerKg: 25, chainPct: 2 }],
|
|
},
|
|
];
|
|
|
|
const summary = summarizeHydrogenRows(rows);
|
|
assert.deepEqual(rows.map(row => row.date), ['2026-08-12', '2026-08-10', '2026-08-11']);
|
|
assert.deepEqual(summary.trendData.map(row => row.date), ['2026-08-10', '2026-08-11', '2026-08-12']);
|
|
assert.equal(summary.totalKg, 400);
|
|
assert.equal(summary.activeDays, 2);
|
|
assert.equal(summary.avgKg, 200);
|
|
assert.equal(summary.stationCount, 2);
|
|
assert.equal(summary.peakDay?.date, '2026-08-11');
|
|
assert.equal(summary.lowDay?.date, '2026-08-10');
|
|
assert.equal(summary.zeroDays, 1);
|
|
});
|
|
|
|
test('空数据保持零值且没有峰谷日', () => {
|
|
assert.deepEqual(summarizeHydrogenRows(null), {
|
|
trendData: [],
|
|
totalKg: 0,
|
|
activeDays: 0,
|
|
stationCount: 0,
|
|
avgKg: 0,
|
|
peakDay: null,
|
|
lowDay: null,
|
|
zeroDays: 0,
|
|
});
|
|
});
|