Files
mihomo-patchbay/shared/host-info.test.ts
T
im0o ce181e9ae5 服务端代答主机指标与公网 IP 检测
mihomo 的管理接口只报自己的内存,CPU、主机内存、网关、内网地址只有
跑在同一台机器上的 Patchbay 服务端答得出,所以加 /api/host 由它代答。
/api/host/exit 把本机某个入站端口设成代理去访问 ip.sb——浏览器发不出
「经由某个端口」的请求,这条只能走服务端。
2026-08-11 22:26:17 +08:00

54 lines
1.8 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import {
cpuUsageBetween,
parseGateway,
parseMemAvailable,
parseNameservers,
} from './host-info.ts'
describe('parseMemAvailable', () => {
test('取 MemAvailable 并换算成字节', () => {
const meminfo = 'MemTotal: 2048000 kB\nMemFree: 100000 kB\nMemAvailable: 512000 kB\n'
expect(parseMemAvailable(meminfo)).toBe(512000 * 1024)
})
test('没有这一行(老内核)返回 null', () => {
expect(parseMemAvailable('MemTotal: 1 kB\nMemFree: 1 kB\n')).toBeNull()
})
})
describe('parseGateway', () => {
test('默认路由的网关按小端序倒回来', () => {
// 0102A8C0 → c0.a8.02.01 → 192.168.2.1
const table = [
'Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU\tWindow\tIRTT',
'eth0\t00000000\t0102A8C0\t0003\t0\t0\t0\t00000000\t0\t0\t0',
'eth0\t0002A8C0\t00000000\t0001\t0\t0\t0\t00FFFFFF\t0\t0\t0',
].join('\n')
expect(parseGateway(table)).toBe('192.168.2.1')
})
test('没有默认路由返回 null', () => {
const table = 'Iface\tDestination\tGateway\neth0\t0002A8C0\t00000000'
expect(parseGateway(table)).toBeNull()
})
})
describe('parseNameservers', () => {
test('按出现顺序取 nameserver', () => {
const conf = '# comment\nnameserver 223.5.5.5\nsearch lan\nnameserver 114.114.114.114\n'
expect(parseNameservers(conf)).toEqual(['223.5.5.5', '114.114.114.114'])
})
})
describe('cpuUsageBetween', () => {
test('按两次快照的差值算百分比', () => {
const usage = cpuUsageBetween({ busy: 100, total: 1000 }, { busy: 350, total: 2000 })
expect(usage).toBe(25)
})
test('时间没有前进时返回 null', () => {
expect(cpuUsageBetween({ busy: 1, total: 10 }, { busy: 1, total: 10 })).toBeNull()
})
})