import { describe, expect, it } from 'bun:test' import { flagEmoji, formatAgo, formatBytes } from './format.ts' describe('流量数字', () => { it('零和不满 1KB 的按字节显示,不带小数', () => { expect(formatBytes(0)).toBe('0 B') expect(formatBytes(512)).toBe('512 B') expect(formatBytes(1023)).toBe('1023 B') }) it('1024 的边界正好换档', () => { expect(formatBytes(1024)).toBe('1 KB') expect(formatBytes(1024 * 1024)).toBe('1 MB') }) it('不满 100 保留一位小数,够 100 取整', () => { expect(formatBytes(23.4 * 1024 * 1024)).toBe('23.4 MB') expect(formatBytes(234.7 * 1024 * 1024)).toBe('235 MB') }) it('超大数停在 TB 档', () => { expect(formatBytes(5 * 1024 ** 4)).toBe('5 TB') expect(formatBytes(5000 * 1024 ** 4)).toBe('5000 TB') }) it('脏输入不炸', () => { expect(formatBytes(-1)).toBe('0 B') expect(formatBytes(Number.NaN)).toBe('0 B') }) }) describe('旗帜 emoji', () => { it('两位国家代码转成区域指示符对,大小写都认', () => { expect(flagEmoji('JP')).toBe('🇯🇵') expect(flagEmoji('us')).toBe('🇺🇸') }) it('不像国家代码的输入原样返回', () => { expect(flagEmoji('')).toBe('') expect(flagEmoji('USA')).toBe('USA') expect(flagEmoji('1X')).toBe('1X') }) }) describe('相对时间', () => { const now = new Date('2026-08-12T12:00:00Z') const ago = (ms: number) => new Date(now.getTime() - ms) it('一分钟内是刚刚,之后按分、时、天换档', () => { expect(formatAgo(ago(30_000), now)).toBe('刚刚') expect(formatAgo(ago(21 * 60_000), now)).toBe('21 分钟前') expect(formatAgo(ago(3 * 3600_000), now)).toBe('3 小时前') expect(formatAgo(ago(5 * 86400_000), now)).toBe('5 天前') }) it('超过 30 天给日期', () => { expect(formatAgo(new Date('2026-05-01T00:00:00'), now)).toBe('2026-05-01') }) it('未来的时间当成刚刚,不出负数', () => { expect(formatAgo(ago(-60_000), now)).toBe('刚刚') }) })