const test = require('node:test') const assert = require('node:assert/strict') const { getTrendData, getBestTrendData, getTrendSummary, 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) }) test('trend summary reports total, active periods, and the highest period', () => { const summary = getTrendSummary([ { label: '一', value: 60 }, { label: '二', value: 0 }, { label: '今天', value: 75 } ], 'week') assert.deepEqual(summary, { totalText: '02:15', activeCount: 2, activeLabel: '训练天数', peakTitle: '最高训练日', peakText: '今天 · 01:15' }) }) test('personal best trend keeps the longest single session in each period', () => { 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 } ] } const data = getBestTrendData(records, 'week', new Date(2026, 6, 29)) assert.equal(data[0].value, 60) assert.equal(data[6].value, 45) assert.equal(data[6].valueText, '00:45') })