adb_cli/tests/deviceParser.test.ts
2026-09-07 16:16:22 +08:00

73 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { devicesEqual, parseDevicesOutput } from '../src/adb/deviceParser.js';
describe('parseDevicesOutput', () => {
it('解析常规 devices -l 输出', () => {
const output = [
'List of devices attached',
'emulator-5554 device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64xa',
'192.168.1.8:5555 device product:xyz model:Pixel_7 device:xyz',
'',
].join('\n');
const devices = parseDevicesOutput(output);
expect(devices).toHaveLength(2);
expect(devices[0]).toEqual({
serial: 'emulator-5554',
state: 'device',
model: 'sdk_gphone64_arm64',
});
expect(devices[1]?.serial).toBe('192.168.1.8:5555');
});
it('跳过 daemon 启动提示行', () => {
const output = [
'* daemon not running; starting now at tcp:5037',
'* daemon started successfully',
'List of devices attached',
'ABC123456 device',
].join('\n');
const devices = parseDevicesOutput(output);
expect(devices).toHaveLength(1);
expect(devices[0]?.serial).toBe('ABC123456');
});
it('保留 offline / unauthorized 状态设备', () => {
const output = [
'List of devices attached',
'abc\t offline',
'def\t unauthorized',
].join('\n');
const devices = parseDevicesOutput(output);
expect(devices.map((device) => device.state)).toEqual(['offline', 'unauthorized']);
});
it('空输出返回空列表', () => {
expect(parseDevicesOutput('List of devices attached\n')).toEqual([]);
expect(parseDevicesOutput('')).toEqual([]);
});
});
describe('devicesEqual', () => {
it('相同列表返回 true', () => {
const a = [{ serial: 's1', state: 'device', model: 'm' }];
const b = [{ serial: 's1', state: 'device', model: 'm' }];
expect(devicesEqual(a, b)).toBe(true);
});
it('状态或顺序变化返回 false', () => {
const a = [
{ serial: 's1', state: 'device' },
{ serial: 's2', state: 'device' },
];
const b = [
{ serial: 's1', state: 'offline' },
{ serial: 's2', state: 'device' },
];
expect(devicesEqual(a, b)).toBe(false);
expect(devicesEqual(a, [a[1]!, a[0]!])).toBe(false);
});
});