Files
mihomo-patchbay/shared/format.test.ts
T
im0o 2580c03bfa 设置页第二轮:行内编辑、GEO 资源展开、恢复与导入分家
单值设置从常驻输入框改成 InlineEdit(纯文本加铅笔,Enter 或失焦提交,
失焦无效直接放弃——失焦是「我走了」,不该把人拽回来)。测速地址加恢复
默认按钮;内核参数逐项提交,不再攒一个保存按钮。

资源一节按文件展开:四类 GEO 文件的大小和更新时间由服务端 stat 内核
工作目录得来(GET /api/geo;文件名和大小写不敏感匹配照 constant/
path.go:18-20,193-224),下载地址可改(geox-url,config.go:363-368,
默认值 578-583)。行内的更新按钮仍是全量更新——内核只有这一个口子,
没变化的文件靠内容哈希跳过。

上传拆成两个入口:恢复只认带 Patchbay 标记的文件,导入收任意配置;
新增恢复策略(兼容只搬接管段落 listeners/proxy-groups/rules/
proxy-providers/rule-providers,覆盖整份替换)。平时是按钮,拖着文件
进窗口时整行变成投放区。
2026-08-12 01:40:49 +08:00

51 lines
1.6 KiB
TypeScript

import { describe, expect, it } from 'bun:test'
import { 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('相对时间', () => {
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('刚刚')
})
})