import assert from 'node:assert/strict' import test from 'node:test' import { formatGiftCooldownRemaining } from '../src/utils/consignment-cooldown.ts' const MINUTE_MS = 60_000 const HOUR_MS = 3_600_000 const DAY_MS = 24 * HOUR_MS const BASE_MS = Date.parse('2026-03-01T00:00:00.000Z') function iso(offsetMs: number) { return new Date(BASE_MS + offsetMs).toISOString() } test('未来时间:按「X 天 Y 小时」展示剩余冷却', () => { const result = formatGiftCooldownRemaining(iso(2 * DAY_MS + 3 * HOUR_MS), BASE_MS) assert.deepEqual(result, { text: '冷却中,剩余 2 天 3 小时', finished: false }) }) test('跨天的整小时剩余省略「0 小时」', () => { assert.deepEqual(formatGiftCooldownRemaining(iso(3 * DAY_MS), BASE_MS), { text: '冷却中,剩余 3 天', finished: false, }) }) test('不足一天:按「X 小时 Y 分」展示,整小时省略「0 分」', () => { assert.deepEqual(formatGiftCooldownRemaining(iso(2 * HOUR_MS + 30 * MINUTE_MS), BASE_MS), { text: '冷却中,剩余 2 小时 30 分', finished: false, }) assert.deepEqual(formatGiftCooldownRemaining(iso(2 * HOUR_MS), BASE_MS), { text: '冷却中,剩余 2 小时', finished: false, }) }) test('小于 1 小时:按分钟向上取整,不会出现「剩余 0 分」', () => { assert.deepEqual(formatGiftCooldownRemaining(iso(25 * MINUTE_MS), BASE_MS), { text: '冷却中,剩余 25 分', finished: false, }) assert.deepEqual(formatGiftCooldownRemaining(iso(30_000), BASE_MS), { text: '冷却中,剩余 1 分', finished: false, }) }) test('已过期(或刚好到点)判定为冷却结束', () => { assert.deepEqual(formatGiftCooldownRemaining(iso(-1), BASE_MS), { text: '冷却已结束,可赠送', finished: true, }) assert.deepEqual(formatGiftCooldownRemaining(iso(0), BASE_MS), { text: '冷却已结束,可赠送', finished: true, }) }) test('非法与空输入返回空文案且不误判为已结束', () => { const empty = { text: '', finished: false } assert.deepEqual(formatGiftCooldownRemaining(null, BASE_MS), empty) assert.deepEqual(formatGiftCooldownRemaining(undefined, BASE_MS), empty) assert.deepEqual(formatGiftCooldownRemaining('', BASE_MS), empty) assert.deepEqual(formatGiftCooldownRemaining(' ', BASE_MS), empty) assert.deepEqual(formatGiftCooldownRemaining('2026-03-01 不是时间', BASE_MS), empty) })