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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * IM客户端单元测试。单元测试编写规则:至少对对每个接口执行正反例测试,若有条件可以增加接口的性能测试。
  3. *
  4. * 此部分为P2P会话测试。
  5. *
  6. * @author sand
  7. * @since 2016/12/24
  8. */
  9. "use strict";
  10. var $ = 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: "H6FYbDejks6VjMmW3uH7V6",
  23. platform: 0
  24. },
  25. DoctorB: {
  26. id: "cd919343-5b06-11e6-8344-fa163e8aee56",
  27. name: "李毅",
  28. token: "0PFWlKmLBN9YzhCfFWVgYA",
  29. clientId: "H6FYbDejks6VjMmW3uH7V6",
  30. platform: 0
  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. // 获取并发送消息: B -> A
  107. describe("Send message from B to A", function () {
  108. it("every message 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,
  122. function (data) {
  123. assert.strictEqual(data.count, TD.UnreadMessageCount, "Get unread message count failed.");
  124. },
  125. function (xhr, status, error) {
  126. });
  127. },
  128. function (xhr, status, error) {
  129. assert.ok(false, xhr.responseJSON.message);
  130. done();
  131. });
  132. });
  133. });
  134. // 退出
  135. describe("User logout", function () {
  136. it("all user must be success", function (done) {
  137. imClient.Users.logout(TD.P2P.DoctorA.id, function (data) {
  138. assert.ok(Object.keys(data).length === 0, "Doctor A logout failed.");
  139. }, function (xhr, status, error) {
  140. assert.ok(false, xhr.responseJSON.message);
  141. });
  142. imClient.Users.logout(TD.P2P.DoctorB.id, function (data) {
  143. assert.ok(Object.keys(data).length === 0, "Doctor B logout failed.");
  144. done();
  145. }, function (xhr, status, error) {
  146. assert.ok(false, xhr.responseJSON.message);
  147. done();
  148. });
  149. });
  150. });
  151. });