ClientTrackingTests.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. @clientA = RealTimeClient.connect()
  19. @clientA.on "connect", cb
  20. (cb) =>
  21. @clientB = RealTimeClient.connect()
  22. @clientB.on "connect", cb
  23. (cb) =>
  24. @clientA.emit "joinProject", {
  25. project_id: @project_id
  26. }, cb
  27. (cb) =>
  28. @clientB.emit "joinProject", {
  29. project_id: @project_id
  30. }, cb
  31. (cb) =>
  32. @updates = []
  33. @clientB.on "clientTracking.clientUpdated", (data) =>
  34. @updates.push data
  35. @clientA.emit "clientTracking.updatePosition", {
  36. row: @row = 42
  37. column: @column = 36
  38. doc_id: @doc_id = "mock-doc-id"
  39. }, (error) ->
  40. throw error if error?
  41. setTimeout cb, 300 # Give the message a chance to reach client B.
  42. ], done
  43. it "should tell other clients about the update", ->
  44. @updates.should.deep.equal [
  45. {
  46. row: @row
  47. column: @column
  48. doc_id: @doc_id
  49. id: @clientA.socket.sessionid
  50. user_id: @user_id
  51. name: "Joe Bloggs"
  52. }
  53. ]
  54. it "should record the update in getConnectedUsers", (done) ->
  55. @clientB.emit "clientTracking.getConnectedUsers", (error, users) =>
  56. for user in users
  57. if user.client_id == @clientA.socket.sessionid
  58. expect(user.cursorData).to.deep.equal({
  59. row: @row
  60. column: @column
  61. doc_id: @doc_id
  62. })
  63. return done()
  64. throw new Error("user was never found")
  65. describe "when an anonymous client updates its cursor location", ->
  66. before (done) ->
  67. async.series [
  68. (cb) =>
  69. FixturesManager.setUpProject {
  70. privilegeLevel: "owner"
  71. project: { name: "Test Project" }
  72. publicAccess: "readAndWrite"
  73. }, (error, {@user_id, @project_id}) => cb()
  74. (cb) =>
  75. @clientA = RealTimeClient.connect()
  76. @clientA.on "connect", cb
  77. (cb) =>
  78. @clientA.emit "joinProject", {
  79. project_id: @project_id
  80. }, cb
  81. (cb) =>
  82. RealTimeClient.setSession({}, cb)
  83. (cb) =>
  84. @anonymous = RealTimeClient.connect()
  85. @anonymous.on "connect", cb
  86. (cb) =>
  87. @anonymous.emit "joinProject", {
  88. project_id: @project_id
  89. }, cb
  90. (cb) =>
  91. @updates = []
  92. @clientA.on "clientTracking.clientUpdated", (data) =>
  93. @updates.push data
  94. @anonymous.emit "clientTracking.updatePosition", {
  95. row: @row = 42
  96. column: @column = 36
  97. doc_id: @doc_id = "mock-doc-id"
  98. }, (error) ->
  99. throw error if error?
  100. setTimeout cb, 300 # Give the message a chance to reach client B.
  101. ], done
  102. it "should tell other clients about the update", ->
  103. @updates.should.deep.equal [
  104. {
  105. row: @row
  106. column: @column
  107. doc_id: @doc_id
  108. id: @anonymous.socket.sessionid
  109. user_id: "anonymous-user"
  110. name: "Anonymous"
  111. }
  112. ]