feat(self-operated): unify completed-day ranges and analytics sorting

This commit is contained in:
2026-10-14 12:39:16 +08:00
parent 6b30e35d07
commit e7411eb03f
17 changed files with 426 additions and 135 deletions
@@ -0,0 +1,23 @@
import { describe, expect, it } from 'vitest'
import { beijingDate, overviewRange, overviewRangeOptions } from './overviewRange'
describe('overview completed calendar days', () => {
it('excludes today and uses exactly fifteen or three days through yesterday', () => {
expect(overviewRangeOptions.some(option => option.value === 'TODAY')).toBe(false)
expect(overviewRange('LAST_15_DAYS', '2026-10-14')).toEqual({ rangePreset: 'CUSTOM', startDate: '2026-09-29', endDate: '2026-10-13' })
expect(overviewRange('LAST_3_DAYS', '2026-01-02')).toMatchObject({ startDate: '2025-12-30', endDate: '2026-01-01' })
expect(() => overviewRange('TODAY', '2026-10-14')).toThrow()
})
it('offers the same four complete-day shortcuts and custom range', () => {
expect(overviewRangeOptions.map(option => option.label)).toEqual(['最近3天', '最近7天', '最近15天', '最近30天', '自定义'])
expect(overviewRange('LAST_7_DAYS', '2026-10-14')).toMatchObject({ startDate: '2026-10-07', endDate: '2026-10-13' })
expect(overviewRange('LAST_30_DAYS', '2026-10-14')).toMatchObject({ startDate: '2026-09-14', endDate: '2026-10-13' })
})
it('rejects custom today and future, allows yesterday and follows Beijing midnight', () => {
expect(() => overviewRange('CUSTOM', '2026-10-14', '2026-10-01', '2026-10-14')).toThrow('最晚为昨日')
expect(() => overviewRange('CUSTOM', '2026-10-14', '2026-10-01', '2026-10-15')).toThrow('最晚为昨日')
expect(overviewRange('CUSTOM', '2026-10-14', '2026-10-13', '2026-10-13')).toMatchObject({ startDate: '2026-10-13', endDate: '2026-10-13' })
expect(beijingDate(Date.parse('2026-10-13T15:59:59Z'))).toBe('2026-10-13')
expect(beijingDate(Date.parse('2026-10-13T16:00:00Z'))).toBe('2026-10-14')
})
})