Files
FileCodeBoxFronted2026/tests/trashPresentation.test.mjs
orion 9b22534e2b ✨ feat: 重构管理后台并增强组件系统与回收站功能
* 新增自适应数值与窗口分析工具,提升交互体验
* 优化回收站模块逻辑,增加展示与选择功能
* 引入 Macaron 主题配置文件并更新 Tailwind 配置
* 精简基础组件代码,移除冗余样式与设置项
* 完善测试用例,覆盖 UI 系统与回收站工作流
* 更新多语言配置,支持功能变更后的文案需求
2026-09-18 10:18:07 +08:00

68 lines
3.5 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { getTrashPresentation, formatTrashTime } from '../src/utils/trashPresentation.ts'
const now = new Date('2026-09-16T12:00:00+08:00').getTime()
const day = 86400000
const item = { id: 1, code: 'ABCD1234', size: 100, trashReason: 'manual' }
const present = (patch = {}) => getTrashPresentation({ ...item, ...patch }, now)
const expiry = days => new Date(now + days * day).toISOString()
test('normal retention shows date without duplicate countdown', () => {
const p = present({ trashExpiresAt: expiry(27.3) })
assert.equal(p.reminder, '')
assert.equal(p.state, '可恢复')
assert.equal(p.reason, '手动删除')
assert.equal(p.tone, 'normal')
})
test('three-day boundary, under-one-day and elapsed deadline are distinct', () => {
assert.equal(present({ trashExpiresAt: expiry(3) }).tone, 'warning')
assert.equal(present({ trashExpiresAt: expiry(3.01) }).tone, 'normal')
assert.equal(present({ trashExpiresAt: expiry(1) }).reminder, '即将清理')
assert.equal(present({ trashExpiresAt: expiry(0.1) }).reminder, '即将清理')
assert.equal(present({ trashExpiresAt: expiry(0) }).reminder, '等待清理')
assert.equal(present({ trashExpiresAt: expiry(-1) }).state, '可恢复')
})
test('purged records take priority over permanent and stale expiry', () => {
const p = present({ isTrashPurged: true, isTrashPermanent: true, trashExpiresAt: expiry(2), trashPurgedAt: expiry(-1) })
assert.equal(p.state, '已清理')
assert.equal(p.tone, 'muted')
assert.equal(p.timeLabel, '清理时间')
assert.equal(p.reminder, '')
assert.match(p.note, /不可恢复/)
})
test('permanent records do not show a countdown', () => {
const p = present({ isTrashPermanent: true })
assert.equal(p.state, '永久保留')
assert.equal(p.reminder, '')
assert.equal(p.timeText, '不自动清理')
})
test('missing and invalid deadlines never imply permanent retention or NaN dates', () => {
for (const trashExpiresAt of [undefined, null, '', 'invalid']) {
const p = present({ trashExpiresAt })
assert.equal(p.timeText, '待确认')
assert.equal(p.reminder, '')
}
for (const date of [undefined, null, '', 'invalid']) assert.equal(formatTrashTime(date), '未记录')
assert.match(formatTrashTime('2026-09-14T16:51:32'), /2026-09-14 16:51:32/)
})
test('expiry reason does not claim that count exhaustion was a manual deletion', () => {
assert.equal(present({ trashReason: 'expired' }).reason, '历史自动回收')
assert.equal(present({ trashReason: 'unexpected' }).reason, '其他原因')
assert.equal(present({ trashReason: null }).reason, '未记录')
})
test('relative time uses seven days inclusively in both directions', () => {
assert.equal(formatTrashTime(expiry(-2), now), '2 天前')
assert.equal(formatTrashTime(expiry(3), now), '3 天后')
assert.equal(formatTrashTime(expiry(-7), now), '7 天前')
assert.equal(formatTrashTime(expiry(7), now), '7 天后')
assert.match(formatTrashTime(expiry(-7.01), now), /^2026-/)
assert.match(formatTrashTime(expiry(7.01), now), /^2026-/)
})
test('relative times handle hours, minutes and immediate deadlines', () => {
assert.equal(formatTrashTime(new Date(now - 2 * 3600000).toISOString(), now), '2 小时前')
assert.equal(formatTrashTime(new Date(now + 30 * 60000).toISOString(), now), '30 分钟后')
assert.equal(formatTrashTime(new Date(now - 1000).toISOString(), now), '刚刚')
assert.equal(formatTrashTime(new Date(now + 1000).toISOString(), now), '即将到达')
})