123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- /**
- * IM客户端单元测试。单元测试编写规则:至少对对每个接口执行正反例测试,若有条件可以增加接口的性能测试。
- *
- * 此部分为P2P会话测试。
- *
- * @author sand
- * @since 2016/12/24
- */
- "use strict";
- let assert = require('assert');
- let imClient = require('../../src/client/im.client');
- // 测试会话用的数据, test data
- let TD = {
- SessionId: 'd5cbe9d10669f8741bc0805b20697ad1254c8e90', //'b07e8c4eabcb0c6fe790843026424afb2fb64d80',
- UnreadMessageCount: 0,
- P2P: {
- DoctorA: {
- id: "cd92414c-5b06-11e6-8344-fa163e8aee56",
- name: "周美丽",
- token: "0PFWlKmLBN9YzhCfFWVgYA",
- clientId: "c9e1c2cef42e609085ff15bac54004e3",
- platform: 1
- },
- DoctorB: {
- id: "cd919343-5b06-11e6-8344-fa163e8aee56",
- name: "李毅",
- token: "8F01F2780AA3677B1F01BDBD63BEBFA7530E1E73FC7FA4C110A5F475ECFD4ADE",
- clientId: "2fa4119e7f77a8077dbd0324838f963b",
- platform: 0
- },
- PatientA: {
- id: "d5cbe9d10669f8741bc0805b20697ad1254c8e90",
- name: "陈爱珍"
- }
- }
- };
- /**
- * 会话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.data.id.length > 0, "Create session failed.");
- TD.SessionId = session.id;
- imClient.Sessions.sendMessage(session.data.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.data.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.data.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("B: Get sessions and unread messages", 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();
- });
- });
- });
- // 获取最近会话列表,默认按7天
- describe("Get recent sessions", function () {
- it("should return recent session in 7 days", function (done) {
- imClient.Sessions.getRecentSessions('shiliuD20160926008',
- function (data) {
- assert.ok(data.length > 0, "Test data must return at least one session");
- console.log(data);
- done();
- },
- function (xhr, status, error) {
- assert.ok(false, xhr.responseJSON.message);
- done();
- });
- });
- });
- // 获取会话的消息
- describe("Get session messages", function () {
- it("should return session messages by page", function (done) {
- imClient.Sessions.getMessagesBySession(TD.SessionId, TD.DoctorB.id, null, null, null, null,
- function (messages) {
- assert.ok(messages.length > 0);
- },
- function (xhr, status, error) {
- assert.ok(false, message.responseJSON.message);
- });
- });
- });
- // 获取会话成员及头像
- describe("Get session participants", function () {
- it("should return two participants and avatars", function (done) {
- imClient.Sessions.getParticipants(TD.SessionId,
- function (data) {
- assert.ok(data.length, 2, "P2P session participant number must be 2");
- data.forEach(function (participant) {
- console.log(participant);
- });
- },
- function (xhr, status, error) {
- assert.ok(false, xhr.responseJSON.message);
- });
- imClient.Sessions.getParticipantsAvatars(TD.SessionId,
- function (avatars) {
- assert.ok(avatars.length, 2, "P2P session participant avatars number must be 2");
- avatars.forEach(function (avatars) {
- console.log(avatars);
- });
- done();
- },
- function (xhr, status, error) {
- assert.ok(false, xhr.responseJSON.message);
- 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();
- });
- });
- });
- });
|