Files
ZYZ/game-server/test/chat.test.ts
2021-03-07 15:27:40 +08:00

120 lines
3.9 KiB
TypeScript

import { DEFAULT_MSG_PER_PAGE, MSG_TYPE, ON_MSG_ROUTE, ON_GROUP_MSG_ROUTE, CHANNEL_PREFIX } from './../app/consts';
import { Client } from './Client';
import 'mocha';
import { PinusWSClient } from 'pinus-robot-plugin';
import { expect } from 'chai';
import { checkSuccessResponse } from './CheckPatten';
const TEXT_MSG = {type: MSG_TYPE.TEXT, content: 'hello world'}
function sendPrivateMessageParm(targetRoleInfo, msg) {
const { roleId: targetRoleId, roleName: targetRoleName } = targetRoleInfo;
const result = {targetRoleId, targetRoleName, ...msg};
return result;
}
function sendGroupMessageParm(channel, channelId, msg) {
const result = {channel, channelId, ...msg};
return result;
}
function checkGroupMsg(msg, roleId, roomId, content) {
expect(msg.content).to.equal(TEXT_MSG.content);
expect(msg.roleId).to.equal(roleId);
expect(msg.content).to.equal(content);
expect(msg.roomId).to.equal(roomId);
}
function testGroupMsg(pinusClient: PinusWSClient, roleInfo: any, pinusClientT: PinusWSClient, channel: string, channelId: string, done: Mocha.Done) {
let msgReceiveCnt = 0;
const roomId = `${channel}_${channelId}`;
pinusClientT.on(ON_GROUP_MSG_ROUTE, (res) => {
checkSuccessResponse(res);
console.log('got res:', roomId, res);
checkGroupMsg(res.data, roleInfo.roleId, roomId, TEXT_MSG.content);
msgReceiveCnt += 1;
});
pinusClient.on(ON_GROUP_MSG_ROUTE, (res) => {
checkSuccessResponse(res);
console.log('got res:', roomId, res);
checkGroupMsg(res.data, roleInfo.roleId, roomId, TEXT_MSG.content);
msgReceiveCnt += 1;
});
pinusClient.request('chat.chatHandler.sendGroupMessage', sendGroupMessageParm(channel, channelId, TEXT_MSG), (res) => {
checkSuccessResponse(res, false);
setTimeout(() => {
expect(msgReceiveCnt).to.be.equal(2);
done();
}, 2000);
});
}
describe('聊天测试', function() {
let pinusClient: PinusWSClient;
let pinusClientT: PinusWSClient; // 用做测试队友
let roleInfo;
let roleInfoT;
beforeEach(function(done) {
const c = new Client();
const cT = new Client('13121622738');
const timer = setInterval(() => {
if (c.client && cT.client) {
pinusClient = c.client;
roleInfo = c.roleInfo;
pinusClientT = cT.client;
roleInfoT = cT.roleInfo;
clearInterval(timer);
done();
}
}, 500);
});
afterEach(function(done) {
pinusClient.disconnect();
pinusClientT.disconnect();
done();
});
it('两个玩家互相发送', function(done) {
pinusClientT.on(ON_MSG_ROUTE, (msg) => {
checkSuccessResponse(msg);
expect(msg.data.content).to.equal(TEXT_MSG.content);
});
pinusClient.request('chat.chatHandler.sendPrivateMessage', sendPrivateMessageParm(roleInfoT, TEXT_MSG), (res) => {
checkSuccessResponse(res, false);
setTimeout(() => {
done();
}, 200);
});
});
it('获取私聊历史消息', function(done) {
for (let i = 0; i < DEFAULT_MSG_PER_PAGE; i++) {
pinusClient.request('chat.chatHandler.sendPrivateMessage', sendPrivateMessageParm(roleInfoT, TEXT_MSG), (res) => {
checkSuccessResponse(res, false);
});
}
setTimeout(() => {
pinusClient.request('chat.chatHandler.getPrivateMessage', {targetRoleId: roleInfoT.roleId}, (res) => {
checkSuccessResponse(res);
expect(res.data.msgs).to.be.an('array');
expect(res.data.msgs.length).to.be.equal(DEFAULT_MSG_PER_PAGE);
res.data.msgs.forEach(msg => {
expect(msg.content).to.equal(TEXT_MSG.content);
});
done();
})
}, 1000);
});
it('测试系统频道消息', function(done) {
testGroupMsg(pinusClient, roleInfo, pinusClientT, CHANNEL_PREFIX.SYS, roleInfo.serverId, done);
});
it('测试世界频道消息', function(done) {
testGroupMsg(pinusClient, roleInfo, pinusClientT, CHANNEL_PREFIX.WORLD, roleInfo.serverId, done);
});
});