Integrate the current development branch, preserve the album comment fixes, and align the changes with repository formatting and dependency tooling.
78 lines
2.5 KiB
TypeScript
78 lines
2.5 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const execMock = vi.hoisted(() => vi.fn());
|
|
const socketCtorMock = vi.hoisted(() =>
|
|
vi.fn(function MockSocket() {
|
|
throw new Error('interactive QQ probe must not run');
|
|
}),
|
|
);
|
|
|
|
vi.mock('child_process', () => ({ exec: execMock }));
|
|
vi.mock('net', () => ({
|
|
default: { Socket: socketCtorMock },
|
|
Socket: socketCtorMock,
|
|
}));
|
|
|
|
import { probeQqLoginInfo } from '../src/qq-port-probe';
|
|
|
|
describe('probeQqLoginInfo — bounded execution', () => {
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
execMock.mockReset();
|
|
socketCtorMock.mockClear();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('allows a later probe after a timed-out attempt', async () => {
|
|
execMock.mockImplementation(() => undefined);
|
|
const first = probeQqLoginInfo(4242);
|
|
await vi.advanceTimersByTimeAsync(10_000);
|
|
await expect(first).resolves.toBeNull();
|
|
|
|
execMock
|
|
.mockImplementationOnce((_command, _options, callback) => {
|
|
callback(null, { stdout: '', stderr: '' });
|
|
})
|
|
.mockImplementationOnce((_command, _options, callback) => {
|
|
callback(null, {
|
|
stdout: process.platform === 'win32' ? 'QQ.exe 4242 Console 1 1234 K\n' : '1\n',
|
|
stderr: '',
|
|
});
|
|
});
|
|
|
|
await expect(probeQqLoginInfo(4242)).resolves.toEqual({
|
|
port: 0,
|
|
uin: '',
|
|
identityKnown: false,
|
|
});
|
|
});
|
|
|
|
it('keeps background identity discovery passive', async () => {
|
|
execMock
|
|
.mockImplementationOnce((_command, _options, callback) => {
|
|
callback(null, {
|
|
stdout:
|
|
process.platform === 'win32'
|
|
? 'TCP 127.0.0.1:9218 0.0.0.0:0 LISTENING 4242\n'
|
|
: 'LISTEN 0 128 127.0.0.1:9218 0.0.0.0:* users:(("qq",pid=4242,fd=12))\n',
|
|
stderr: '',
|
|
});
|
|
})
|
|
.mockImplementationOnce((_command, _options, callback) => {
|
|
callback(null, {
|
|
stdout:
|
|
process.platform === 'win32'
|
|
? 'QQ.exe 4242 Console 1 1234 K\n'.repeat(8)
|
|
: '8\n',
|
|
stderr: '',
|
|
});
|
|
});
|
|
|
|
await expect(probeQqLoginInfo(4242)).resolves.toBeNull();
|
|
expect(socketCtorMock).not.toHaveBeenCalled();
|
|
});
|
|
});
|