/** * IM客户端单元测试。单元测试编写规则:至少对对每个接口执行正反例测试,若有条件可以增加接口的性能测试。 * * 此部分为P2P会话测试。 * * @author sand * @since 2016/12/24 */ "use strict"; var $ = require('jquery'); let assert = require('assert'); let imClient = require('../../src/client/im.client'); // 测试会话用的数据, test data let TD = { SessionId: 'b07e8c4eabcb0c6fe790843026424afb2fb64d80', UnreadMessageCount: 0, P2P: { DoctorA: { id: "cd92414c-5b06-11e6-8344-fa163e8aee56", name: "周美丽", token: "0PFWlKmLBN9YzhCfFWVgYA", clientId: "H6FYbDejks6VjMmW3uH7V6", platform: 0 }, DoctorB: { id: "cd919343-5b06-11e6-8344-fa163e8aee56", name: "李毅", token: "0PFWlKmLBN9YzhCfFWVgYA", clientId: "H6FYbDejks6VjMmW3uH7V6", platform: 0 } } }; /** * 会话API。测试逻辑: * A医生登录 * B医生登录 * * A医生向B医生发送3条消息 * B医生获取会话列表 * B医生获取与A医生的会话未读消息列表 * * B医生向A医生发送5条消息 * A医生获取会话列表 * A医生获取与A医生的会话未读消息列表 * * A医生退出 * B医生退出 */ describe("Session P2P", function () { // 登录 describe("User login", function () { it("all user must be success", function (done) { imClient.Users.login(TD.P2P.DoctorA.id, TD.P2P.DoctorA.token, TD.P2P.DoctorA.clientId, TD.P2P.DoctorA.platform, function (data) { assert.ok(Object.keys(data).length === 0, "Doctor A login failed."); }, function (xhr, status, error) { assert.ok(false, xhr.responseJSON.message); }); imClient.Users.login(TD.P2P.DoctorB.id, TD.P2P.DoctorB.token, TD.P2P.DoctorB.clientId, TD.P2P.DoctorB.platform, function (data) { assert.ok(Object.keys(data).length === 0, "Doctor B login failed."); done(); }, function (xhr, status, error) { assert.ok(false, xhr.responseJSON.message); done(); }); }); }); // 发送消息: A -> B describe("Send message from A to B", function () { it("every message must be ok", function (done) { // 创建会话并发送消息 imClient.Sessions.createP2pSession(TD.P2P.DoctorA.id, TD.P2P.DoctorB.id, function (session) { assert.ok(session.id.length > 0, "Create session failed."); TD.SessionId = session.id; imClient.Sessions.sendMessage(session.id, TD.P2P.DoctorA.id, TD.P2P.DoctorA.name, "李医生,你好", 1, function (data) { assert.ok(Object.keys(data).length > 0, "Send message failed."); }, function (xhr, status, error) { assert.ok(false, xhr.responseJSON.message) }); imClient.Sessions.sendMessage(session.id, TD.P2P.DoctorA.id, TD.P2P.DoctorA.name, "莲前社区糖尿病患者已进入随访跟踪状态", 1, function (data) { assert.ok(Object.keys(data).length > 0, "Send message failed."); }, function (xhr, status, error) { assert.ok(false, xhr.responseJSON.message) }); imClient.Sessions.sendMessage(session.id, TD.P2P.DoctorA.id, TD.P2P.DoctorA.name, "但处方信息还需要您的确认,请尽快回复,谢谢!", 1, function (data) { assert.ok(Object.keys(data).length > 0, "Send message failed."); done(); }, function (xhr, status, error) { assert.ok(false, xhr.responseJSON.message) }); }, function (xhr, status, error) { assert.ok(false, xhr.responseJSON.message); done(); }); }); }); // 获取会话列表及消息数 describe("Get sessions and unread messages for B", function () { it("every operation must be ok", function (done) { imClient.Sessions.getSessionsWithDoctor(TD.P2P.DoctorB.id, 0, 10, function (sessions) { let isPass = false; for (let i in sessions) { if (sessions[i].id == TD.SessionId) { isPass = true; TD.UnreadMessageCount = sessions[i].unread_count; break; } } assert.ok(isPass, "Get sessions with doctor failed."); // 读取未读消息数 imClient.Sessions.getSessionUnreadMessageCount(TD.SessionId, TD.P2P.DoctorB.id, function (data) { assert.strictEqual(data.count, TD.UnreadMessageCount, "Unread message count dismatch."); }, function (xhr, status, error) { assert(false, xhr.responseJSON.message); }); // 获取未读消息 imClient.Sessions.getSessionUnreadMessages(TD.SessionId, TD.P2P.DoctorB.id, function (data) { assert.strictEqual(data.length, TD.UnreadMessageCount, "Get session unread messages failed."); data.forEach(function (message) { console.log(message); }); done(); }, function (xhr, status, error) { assert(false, xhr.responseJSON.message); done(); }); }, function (xhr, status, error) { assert.ok(false, xhr.responseJSON.message); done(); }); }); }); // 发送消息: B -> A describe("Send message from B to A", function () { it("every message must be ok", function (done) { }); }); // 退出 describe("User logout", function () { it("all user must be success", function (done) { imClient.Users.logout(TD.P2P.DoctorA.id, function (data) { assert.ok(Object.keys(data).length === 0, "Doctor A logout failed."); }, function (xhr, status, error) { assert.ok(false, xhr.responseJSON.message); }); imClient.Users.logout(TD.P2P.DoctorB.id, function (data) { assert.ok(Object.keys(data).length === 0, "Doctor B logout failed."); done(); }, function (xhr, status, error) { assert.ok(false, xhr.responseJSON.message); done(); }); }); }); });