im.client.session.p2p.Test.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * IM客户端单元测试。单元测试编写规则:至少对对每个接口执行正反例测试,若有条件可以增加接口的性能测试。
  3. *
  4. * 此部分为P2P会话测试。
  5. *
  6. * @author sand
  7. * @since 2016/12/24
  8. */
  9. "use strict";
  10. let assert = require('assert');
  11. let imClient = require('../../src/client/im.client');
  12. // 测试会话用的数据, test data
  13. let TD = {
  14. SessionId: 'd5cbe9d10669f8741bc0805b20697ad1254c8e90', //'b07e8c4eabcb0c6fe790843026424afb2fb64d80',
  15. UnreadMessageCount: 0,
  16. P2P: {
  17. DoctorA: {
  18. id: "cd92414c-5b06-11e6-8344-fa163e8aee56",
  19. name: "周美丽",
  20. token: "0PFWlKmLBN9YzhCfFWVgYA",
  21. clientId: "c9e1c2cef42e609085ff15bac54004e3",
  22. platform: 1
  23. },
  24. DoctorB: {
  25. id: "cd919343-5b06-11e6-8344-fa163e8aee56",
  26. name: "李毅",
  27. token: "8F01F2780AA3677B1F01BDBD63BEBFA7530E1E73FC7FA4C110A5F475ECFD4ADE",
  28. clientId: "2fa4119e7f77a8077dbd0324838f963b",
  29. platform: 0
  30. },
  31. PatientA: {
  32. id: "d5cbe9d10669f8741bc0805b20697ad1254c8e90",
  33. name: "陈爱珍"
  34. }
  35. }
  36. };
  37. /**
  38. * 会话API。测试逻辑:
  39. * A医生登录
  40. * B医生登录
  41. *
  42. * A医生向B医生发送3条消息
  43. * B医生获取会话列表
  44. * B医生获取与A医生的会话未读消息列表
  45. *
  46. * B医生向A医生发送5条消息
  47. * A医生获取会话列表
  48. * A医生获取与A医生的会话未读消息列表
  49. *
  50. * A医生退出
  51. * B医生退出
  52. */
  53. describe("Session P2P", function () {
  54. // 登录
  55. describe("User login", function () {
  56. it("all user must be success", function (done) {
  57. imClient.Users.login(TD.P2P.DoctorA.id, TD.P2P.DoctorA.token, TD.P2P.DoctorA.clientId, TD.P2P.DoctorA.platform,
  58. function (data) {
  59. assert.ok(Object.keys(data).length === 0, "Doctor A login failed.");
  60. },
  61. function (xhr, status, error) {
  62. assert.ok(false, xhr.responseJSON.message);
  63. });
  64. imClient.Users.login(TD.P2P.DoctorB.id, TD.P2P.DoctorB.token, TD.P2P.DoctorB.clientId, TD.P2P.DoctorB.platform,
  65. function (data) {
  66. assert.ok(Object.keys(data).length === 0, "Doctor B login failed.");
  67. done();
  68. },
  69. function (xhr, status, error) {
  70. assert.ok(false, xhr.responseJSON.message);
  71. done();
  72. });
  73. });
  74. });
  75. // 发送消息: A -> B
  76. describe("Send message from A to B", function () {
  77. it("every message must be ok", function (done) {
  78. // 创建会话并发送消息
  79. imClient.Sessions.createP2pSession(TD.P2P.DoctorA.id, TD.P2P.DoctorB.id,
  80. function (session) {
  81. assert.ok(session.data.id.length > 0, "Create session failed.");
  82. TD.SessionId = session.id;
  83. imClient.Sessions.sendMessage(session.data.id, TD.P2P.DoctorA.id, TD.P2P.DoctorA.name, "李医生,你好", 1,
  84. function (data) {
  85. assert.ok(Object.keys(data).length > 0, "Send message failed.");
  86. }, function (xhr, status, error) {
  87. assert.ok(false, xhr.responseJSON.message)
  88. });
  89. imClient.Sessions.sendMessage(session.data.id, TD.P2P.DoctorA.id, TD.P2P.DoctorA.name, "莲前社区糖尿病患者已进入随访跟踪状态", 1,
  90. function (data) {
  91. assert.ok(Object.keys(data).length > 0, "Send message failed.");
  92. }, function (xhr, status, error) {
  93. assert.ok(false, xhr.responseJSON.message)
  94. });
  95. imClient.Sessions.sendMessage(session.data.id, TD.P2P.DoctorA.id, TD.P2P.DoctorA.name, "但处方信息还需要您的确认,请尽快回复,谢谢!", 1,
  96. function (data) {
  97. assert.ok(Object.keys(data).length > 0, "Send message failed.");
  98. done();
  99. }, function (xhr, status, error) {
  100. assert.ok(false, xhr.responseJSON.message)
  101. });
  102. },
  103. function (xhr, status, error) {
  104. assert.ok(false, xhr.responseJSON.message);
  105. done();
  106. });
  107. });
  108. });
  109. // 获取会话列表及消息数
  110. describe("B: Get sessions and unread messages", function () {
  111. it("every operation must be ok", function (done) {
  112. imClient.Sessions.getSessionsWithDoctor(TD.P2P.DoctorB.id, 0, 10,
  113. function (sessions) {
  114. let isPass = false;
  115. for (let i in sessions) {
  116. if (sessions[i].id == TD.SessionId) {
  117. isPass = true;
  118. TD.UnreadMessageCount = sessions[i].unread_count;
  119. break;
  120. }
  121. }
  122. assert.ok(isPass, "Get sessions with doctor failed.");
  123. // 读取未读消息数
  124. imClient.Sessions.getSessionUnreadMessageCount(TD.SessionId, TD.P2P.DoctorB.id,
  125. function (data) {
  126. assert.strictEqual(data.count, TD.UnreadMessageCount, "Unread message count dismatch.");
  127. },
  128. function (xhr, status, error) {
  129. assert(false, xhr.responseJSON.message);
  130. });
  131. // 获取未读消息
  132. imClient.Sessions.getSessionUnreadMessages(TD.SessionId, TD.P2P.DoctorB.id,
  133. function (data) {
  134. assert.strictEqual(data.length, TD.UnreadMessageCount, "Get session unread messages failed.");
  135. data.forEach(function (message) {
  136. console.log(message);
  137. });
  138. done();
  139. },
  140. function (xhr, status, error) {
  141. assert(false, xhr.responseJSON.message);
  142. done();
  143. });
  144. },
  145. function (xhr, status, error) {
  146. assert.ok(false, xhr.responseJSON.message);
  147. done();
  148. });
  149. });
  150. });
  151. // 获取最近会话列表,默认按7天
  152. describe("Get recent sessions", function () {
  153. it("should return recent session in 7 days", function (done) {
  154. imClient.Sessions.getRecentSessions('shiliuD20160926008',
  155. function (data) {
  156. assert.ok(data.length > 0, "Test data must return at least one session");
  157. console.log(data);
  158. done();
  159. },
  160. function (xhr, status, error) {
  161. assert.ok(false, xhr.responseJSON.message);
  162. done();
  163. });
  164. });
  165. });
  166. // 获取会话的消息
  167. describe("Get session messages", function () {
  168. it("should return session messages by page", function (done) {
  169. imClient.Sessions.getMessagesBySession(TD.SessionId, TD.DoctorB.id, null, null, null, null,
  170. function (messages) {
  171. assert.ok(messages.length > 0);
  172. },
  173. function (xhr, status, error) {
  174. assert.ok(false, message.responseJSON.message);
  175. });
  176. });
  177. });
  178. // 获取会话成员及头像
  179. describe("Get session participants", function () {
  180. it("should return two participants and avatars", function (done) {
  181. imClient.Sessions.getParticipants(TD.SessionId,
  182. function (data) {
  183. assert.ok(data.length, 2, "P2P session participant number must be 2");
  184. data.forEach(function (participant) {
  185. console.log(participant);
  186. });
  187. },
  188. function (xhr, status, error) {
  189. assert.ok(false, xhr.responseJSON.message);
  190. });
  191. imClient.Sessions.getParticipantsAvatars(TD.SessionId,
  192. function (avatars) {
  193. assert.ok(avatars.length, 2, "P2P session participant avatars number must be 2");
  194. avatars.forEach(function (avatars) {
  195. console.log(avatars);
  196. });
  197. done();
  198. },
  199. function (xhr, status, error) {
  200. assert.ok(false, xhr.responseJSON.message);
  201. done();
  202. });
  203. });
  204. });
  205. // 退出
  206. describe("User logout", function () {
  207. it("all user must be success", function (done) {
  208. imClient.Users.logout(TD.P2P.DoctorA.id, function (data) {
  209. assert.ok(Object.keys(data).length === 0, "Doctor A logout failed.");
  210. }, function (xhr, status, error) {
  211. assert.ok(false, xhr.responseJSON.message);
  212. });
  213. imClient.Users.logout(TD.P2P.DoctorB.id, function (data) {
  214. assert.ok(Object.keys(data).length === 0, "Doctor B logout failed.");
  215. done();
  216. }, function (xhr, status, error) {
  217. assert.ok(false, xhr.responseJSON.message);
  218. done();
  219. });
  220. });
  221. });
  222. });