修复趋势图表加载与比例展示

This commit is contained in:
2026-07-29 14:18:45 +08:00
parent 1372ec19b8
commit 19b0c4d4c8
5 changed files with 124 additions and 45 deletions
+47
View File
@@ -0,0 +1,47 @@
const test = require('node:test')
const assert = require('node:assert/strict')
const { getTrendData, getBarPercent } = require('../utils/util')
test('weekly trend combines multiple sessions from the same day', () => {
const records = {
'2026-07': [
{ date: '2026-07-29 08:00:00', duration: 30 },
{ date: '2026-07-29 20:00:00', duration: 45 },
{ date: '2026-07-23 08:00:00', duration: 60 }
]
}
assert.deepEqual(getTrendData(records, 'week', new Date(2026, 6, 29)), [
{ label: '四', value: 60, valueText: '01:00', highlight: false },
{ label: '五', value: 0, valueText: '', highlight: false },
{ label: '六', value: 0, valueText: '', highlight: false },
{ label: '日', value: 0, valueText: '', highlight: false },
{ label: '一', value: 0, valueText: '', highlight: false },
{ label: '二', value: 0, valueText: '', highlight: false },
{ label: '今天', value: 75, valueText: '01:15', highlight: true }
])
})
test('monthly trend spans a year boundary from a single record snapshot', () => {
const records = {
'2025-08': [{ date: '2025-08-31 08:00:00', duration: 60 }],
'2025-12': [{ date: '2025-12-01 08:00:00', duration: 120 }],
'2026-01': [{ date: '2026-01-02 08:00:00', duration: 3600 }]
}
assert.deepEqual(getTrendData(records, 'month', new Date(2026, 0, 15)), [
{ label: '8月', value: 60, valueText: '01:00', highlight: false },
{ label: '9月', value: 0, valueText: '', highlight: false },
{ label: '10月', value: 0, valueText: '', highlight: false },
{ label: '11月', value: 0, valueText: '', highlight: false },
{ label: '12月', value: 120, valueText: '02:00', highlight: false },
{ label: '1月', value: 3600, valueText: '60m', highlight: true }
])
})
test('bar height remains proportional for very short sessions', () => {
assert.equal(getBarPercent(0, 120), 0)
assert.ok(getBarPercent(1, 120) < 1)
assert.equal(getBarPercent(120, 120), 100)
})