ClientTrackingTests.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. chai = require("chai")
  2. expect = chai.expect
  3. chai.should()
  4. RealTimeClient = require "./helpers/RealTimeClient"
  5. MockWebServer = require "./helpers/MockWebServer"
  6. FixturesManager = require "./helpers/FixturesManager"
  7. async = require "async"
  8. describe "clientTracking", ->
  9. describe "when a client updates its cursor location", ->
  10. before (done) ->
  11. async.series [
  12. (cb) =>
  13. FixturesManager.setUpProject {
  14. privilegeLevel: "owner"
  15. project: { name: "Test Project" }
  16. }, (error, {@user_id, @project_id}) => cb()
  17. (cb) =>
  18. FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) =>
  19. cb(e)
  20. (cb) =>
  21. @clientA = RealTimeClient.connect()
  22. @clientA.on "connectionAccepted", cb
  23. (cb) =>
  24. @clientB = RealTimeClient.connect()
  25. @clientB.on "connectionAccepted", cb
  26. (cb) =>
  27. @clientA.emit "joinProject", {
  28. project_id: @project_id
  29. }, cb
  30. (cb) =>
  31. @clientA.emit "joinDoc", @doc_id, cb
  32. (cb) =>
  33. @clientB.emit "joinProject", {
  34. project_id: @project_id
  35. }, cb
  36. (cb) =>
  37. @updates = []
  38. @clientB.on "clientTracking.clientUpdated", (data) =>
  39. @updates.push data
  40. @clientA.emit "clientTracking.updatePosition", {
  41. row: @row = 42
  42. column: @column = 36
  43. doc_id: @doc_id
  44. }, (error) ->
  45. throw error if error?
  46. setTimeout cb, 300 # Give the message a chance to reach client B.
  47. ], done
  48. it "should tell other clients about the update", ->
  49. @updates.should.deep.equal [
  50. {
  51. row: @row
  52. column: @column
  53. doc_id: @doc_id
  54. id: @clientA.publicId
  55. user_id: @user_id
  56. name: "Joe Bloggs"
  57. }
  58. ]
  59. it "should record the update in getConnectedUsers", (done) ->
  60. @clientB.emit "clientTracking.getConnectedUsers", (error, users) =>
  61. for user in users
  62. if user.client_id == @clientA.publicId
  63. expect(user.cursorData).to.deep.equal({
  64. row: @row
  65. column: @column
  66. doc_id: @doc_id
  67. })
  68. return done()
  69. throw new Error("user was never found")
  70. describe "when an anonymous client updates its cursor location", ->
  71. before (done) ->
  72. async.series [
  73. (cb) =>
  74. FixturesManager.setUpProject {
  75. privilegeLevel: "owner"
  76. project: { name: "Test Project" }
  77. publicAccess: "readAndWrite"
  78. }, (error, {@user_id, @project_id}) => cb()
  79. (cb) =>
  80. FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) =>
  81. cb(e)
  82. (cb) =>
  83. @clientA = RealTimeClient.connect()
  84. @clientA.on "connectionAccepted", cb
  85. (cb) =>
  86. @clientA.emit "joinProject", {
  87. project_id: @project_id
  88. }, cb
  89. (cb) =>
  90. RealTimeClient.setSession({}, cb)
  91. (cb) =>
  92. @anonymous = RealTimeClient.connect()
  93. @anonymous.on "connectionAccepted", cb
  94. (cb) =>
  95. @anonymous.emit "joinProject", {
  96. project_id: @project_id
  97. }, cb
  98. (cb) =>
  99. @anonymous.emit "joinDoc", @doc_id, cb
  100. (cb) =>
  101. @updates = []
  102. @clientA.on "clientTracking.clientUpdated", (data) =>
  103. @updates.push data
  104. @anonymous.emit "clientTracking.updatePosition", {
  105. row: @row = 42
  106. column: @column = 36
  107. doc_id: @doc_id
  108. }, (error) ->
  109. throw error if error?
  110. setTimeout cb, 300 # Give the message a chance to reach client B.
  111. ], done
  112. it "should tell other clients about the update", ->
  113. @updates.should.deep.equal [
  114. {
  115. row: @row
  116. column: @column
  117. doc_id: @doc_id
  118. id: @anonymous.publicId
  119. user_id: "anonymous-user"
  120. name: ""
  121. }
  122. ]