import { describe, expect, it } from 'vitest' import { render, screen } from '@testing-library/react' import RelativeTime, { toExactLabel, toRelativeLabel } from './relative-time' const NOW = new Date('2026-07-18T12:00:00.000Z') const ago = (ms: number) => new Date(NOW.getTime() - ms) const SECOND = 1000 const MINUTE = 60 * SECOND const HOUR = 60 * MINUTE const DAY = 24 * HOUR describe('toRelativeLabel', () => { it('collapses anything under a minute to "Just now"', () => { expect(toRelativeLabel(ago(0), NOW)).toBe('Just now') expect(toRelativeLabel(ago(45 * SECOND), NOW)).toBe('Just now') }) it('is strict, so it does not say "about"', () => { // formatDistanceToNow would render "about 3 minutes ago". expect(toRelativeLabel(ago(3 * MINUTE), NOW)).not.toContain('about') }) }) // The pure helper takes an explicit `now`, but the component reads the real // clock, so its inputs must be relative to the real clock too. Anchoring them // to the fixed NOW above made these pass only on that one calendar day: a // value 7 days before 2026-07-18 is 8 days before 2026-07-19. const realAgo = (ms: number) => new Date(Date.now() - ms) describe('RelativeTime', () => { it('renders a relative label for a past date', () => { render() expect(screen.getByText('7 days ago')).toBeInTheDocument() }) it('exposes the machine-readable timestamp on a