feat: 新增示例种子数据与核心单元测试

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nixevol
2026-06-28 16:33:35 +08:00
co-authored by Cursor
parent 23cbf37aa1
commit fa61840cfb
7 changed files with 291 additions and 21 deletions
+29
View File
@@ -0,0 +1,29 @@
import { parseUserAgent, extractIp } from './ua.util';
describe('parseUserAgent', () => {
it('识别移动端 iOS', () => {
const r = parseUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)');
expect(r.deviceType).toBe('Mobile');
expect(r.os).toBe('iOS');
});
it('识别 PC Windows', () => {
const r = parseUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
expect(r.deviceType).toBe('PC');
expect(r.os).toBe('Windows');
});
it('空 UA 返回 Unknown', () => {
expect(parseUserAgent(undefined).deviceType).toBe('Unknown');
});
});
describe('extractIp', () => {
it('优先取 x-forwarded-for 首个', () => {
expect(extractIp({ 'x-forwarded-for': '1.2.3.4, 5.6.7.8' })).toBe('1.2.3.4');
});
it('回退到 fallback 并去除 ipv6 前缀', () => {
expect(extractIp({}, '::ffff:9.9.9.9')).toBe('9.9.9.9');
});
});