149 lines
4.5 KiB
Plaintext
149 lines
4.5 KiB
Plaintext
import { describe, it, expect, beforeEach, afterEach } from '@ohos/hypium';
|
|
import { RConClient } from '../../main/ets/components/RconClient';
|
|
import { RConResult } from '../../main/ets/components/tools';
|
|
|
|
export default function rConClientTest() {
|
|
describe('RConClient', () => {
|
|
let client: RConClient;
|
|
|
|
beforeEach(() => {
|
|
client = new RConClient("127.0.0.1", 25575, "password");
|
|
});
|
|
|
|
// ===== 构造函数测试 =====
|
|
describe('constructor', () => {
|
|
it('should initialize with correct properties', 0, () => {
|
|
expect(client.isActive).assertFalse();
|
|
});
|
|
|
|
it('should accept different configurations', 0, () => {
|
|
const customClient = new RConClient("192.168.1.100", 25575, "secret");
|
|
expect(customClient.isActive).assertFalse();
|
|
});
|
|
});
|
|
|
|
// ===== setConnect 测试 =====
|
|
describe('setConnect', () => {
|
|
it('should call callback multiple times during connection', 0, () => {
|
|
let callbackCount = 0;
|
|
const results: RConResult[] = [];
|
|
|
|
client.setConnect(5000, (result: RConResult) => {
|
|
callbackCount++;
|
|
results.push(result);
|
|
});
|
|
|
|
// 注意:这是异步操作,实际测试需要等待
|
|
// 这里只是演示测试结构
|
|
});
|
|
|
|
it('should handle connection timeout', 0, () => {
|
|
let errorReceived = false;
|
|
|
|
client.setConnect(1, (result: RConResult) => {
|
|
if (!result.success && result.errorCode === "tcp_fail") {
|
|
errorReceived = true;
|
|
}
|
|
});
|
|
|
|
// 验证超时处理
|
|
});
|
|
|
|
it('should prevent concurrent connections', 0, () => {
|
|
let firstCallback = false;
|
|
let secondCallback = false;
|
|
|
|
// 第一次连接
|
|
client.setConnect(5000, (result: RConResult) => {
|
|
firstCallback = true;
|
|
});
|
|
|
|
// 第二次连接应该被拒绝
|
|
client.setConnect(5000, (result: RConResult) => {
|
|
secondCallback = true;
|
|
if (!result.success && result.errorCode === "ALREADY_CONNECTING") {
|
|
// 预期的错误
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
// ===== sendMessage 测试 =====
|
|
describe('sendMessage', () => {
|
|
it('should send message with correct format', 0, () => {
|
|
// 模拟已连接状态
|
|
// client.setConnect(...) 完成后
|
|
|
|
client.sendMessage("/list", 2, (result: RConResult) => {
|
|
if (result.success) {
|
|
expect(result.resultText).not.assertNull();
|
|
}
|
|
});
|
|
});
|
|
|
|
it('should handle message send errors', 0, () => {
|
|
client.sendMessage("/invalid-command", 2, (result: RConResult) => {
|
|
// 验证错误处理
|
|
});
|
|
});
|
|
|
|
it('should trigger auto-reconnect when inactive', 0, () => {
|
|
// 测试自动重连机制
|
|
});
|
|
});
|
|
|
|
// ===== resetServer 测试 =====
|
|
describe('resetServer', () => {
|
|
it('should reset server configuration', 0, () => {
|
|
let reconnectSuccessful = false;
|
|
|
|
client.resetServer("192.168.1.200", "newpassword", 25575, 5000, (result: RConResult) => {
|
|
reconnectSuccessful = result.success;
|
|
});
|
|
});
|
|
|
|
it('should update internal state after reset', 0, () => {
|
|
// 验证状态更新
|
|
});
|
|
});
|
|
|
|
// ===== setDisconnect 测试 =====
|
|
describe('setDisconnect', () => {
|
|
it('should disconnect when active', 0, () => {
|
|
// 先连接
|
|
client.setConnect(5000, (result: RConResult) => {
|
|
if (result.success && result.type === 2) {
|
|
client.setDisconnect();
|
|
expect(client.isActive).assertFalse();
|
|
}
|
|
});
|
|
});
|
|
|
|
it('should handle disconnect when not connected', 0, () => {
|
|
// 不应该抛出异常
|
|
client.setDisconnect();
|
|
expect(client.isActive).assertFalse();
|
|
});
|
|
});
|
|
|
|
// ===== 状态管理测试 =====
|
|
describe('state management', () => {
|
|
it('should track active state correctly', 0, () => {
|
|
expect(client.isActive).assertFalse();
|
|
|
|
// 连接后应该是true
|
|
client.setConnect(5000, (result: RConResult) => {
|
|
if (result.success && result.type === 2) {
|
|
expect(client.isActive).assertTrue();
|
|
}
|
|
});
|
|
});
|
|
|
|
it('should handle state after disconnect', 0, () => {
|
|
client.setDisconnect();
|
|
expect(client.isActive).assertFalse();
|
|
});
|
|
});
|
|
});
|
|
}
|