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

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