cc-haha/desktop/src/lib/formatMessageTimestamp.test.ts
程序员阿江(Relakkes) b483a9929d fix: reveal message actions with timestamps on hover (#642)
Message rows should stay visually quiet until the user needs copy or fork controls. The action row now appears on message hover/focus, uses icon-sized controls, and shows the message timestamp beside the actions with locale-aware recent and historical formats.

Constraint: Desktop chat actions already own copy and fork affordances, so the change keeps that surface instead of adding a separate always-visible timestamp row.
Rejected: Keep text-labeled Copy buttons | the row remained too visually heavy for every message.
Confidence: high
Scope-risk: narrow
Directive: Keep copy, fork, and timestamp as one hover/focus metadata row unless the message layout is redesigned as a whole.
Tested: bun run check:desktop
Not-tested: Live Tauri native packaging
2026-05-31 17:41:43 +08:00

30 lines
1.1 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { translate, type Locale } from '../i18n'
import { formatMessageTimestamp } from './formatMessageTimestamp'
const t = (locale: Locale) => (
key: Parameters<typeof translate>[1],
params?: Parameters<typeof translate>[2],
) => translate(locale, key, params)
describe('formatMessageTimestamp', () => {
const now = new Date(2026, 4, 29, 16, 0).getTime()
it('uses relative labels for recent messages', () => {
expect(formatMessageTimestamp(now - 5 * 60_000, t('zh'), 'zh', now)).toBe('5分钟前')
expect(formatMessageTimestamp(now - 2 * 60 * 60_000, t('en'), 'en', now)).toBe('2h ago')
})
it('uses weekday and clock time for recent history', () => {
const value = new Date(2026, 4, 24, 15, 50).getTime()
expect(formatMessageTimestamp(value, t('zh'), 'zh', now)).toBe('星期日15:50')
})
it('includes the calendar date for older messages', () => {
const value = new Date(2026, 3, 20, 9, 30).getTime()
expect(formatMessageTimestamp(value, t('zh'), 'zh', now)).toBe('4月20日 09:30')
})
})