ClientTrackingTests.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const chai = require('chai')
  16. const { expect } = chai
  17. chai.should()
  18. const RealTimeClient = require('./helpers/RealTimeClient')
  19. const MockWebServer = require('./helpers/MockWebServer')
  20. const FixturesManager = require('./helpers/FixturesManager')
  21. const async = require('async')
  22. describe('clientTracking', function () {
  23. describe('when a client updates its cursor location', function () {
  24. before(function (done) {
  25. return async.series(
  26. [
  27. (cb) => {
  28. return FixturesManager.setUpProject(
  29. {
  30. privilegeLevel: 'owner',
  31. project: { name: 'Test Project' }
  32. },
  33. (error, { user_id, project_id }) => {
  34. this.user_id = user_id
  35. this.project_id = project_id
  36. return cb()
  37. }
  38. )
  39. },
  40. (cb) => {
  41. return FixturesManager.setUpDoc(
  42. this.project_id,
  43. { lines: this.lines, version: this.version, ops: this.ops },
  44. (e, { doc_id }) => {
  45. this.doc_id = doc_id
  46. return cb(e)
  47. }
  48. )
  49. },
  50. (cb) => {
  51. this.clientA = RealTimeClient.connect()
  52. return this.clientA.on('connectionAccepted', cb)
  53. },
  54. (cb) => {
  55. this.clientB = RealTimeClient.connect()
  56. return this.clientB.on('connectionAccepted', cb)
  57. },
  58. (cb) => {
  59. return this.clientA.emit(
  60. 'joinProject',
  61. {
  62. project_id: this.project_id
  63. },
  64. cb
  65. )
  66. },
  67. (cb) => {
  68. return this.clientA.emit('joinDoc', this.doc_id, cb)
  69. },
  70. (cb) => {
  71. return this.clientB.emit(
  72. 'joinProject',
  73. {
  74. project_id: this.project_id
  75. },
  76. cb
  77. )
  78. },
  79. (cb) => {
  80. this.updates = []
  81. this.clientB.on('clientTracking.clientUpdated', (data) => {
  82. return this.updates.push(data)
  83. })
  84. return this.clientA.emit(
  85. 'clientTracking.updatePosition',
  86. {
  87. row: (this.row = 42),
  88. column: (this.column = 36),
  89. doc_id: this.doc_id
  90. },
  91. (error) => {
  92. if (error != null) {
  93. throw error
  94. }
  95. return setTimeout(cb, 300)
  96. }
  97. )
  98. } // Give the message a chance to reach client B.
  99. ],
  100. done
  101. )
  102. })
  103. it('should tell other clients about the update', function () {
  104. return this.updates.should.deep.equal([
  105. {
  106. row: this.row,
  107. column: this.column,
  108. doc_id: this.doc_id,
  109. id: this.clientA.publicId,
  110. user_id: this.user_id,
  111. name: 'Joe Bloggs'
  112. }
  113. ])
  114. })
  115. return it('should record the update in getConnectedUsers', function (done) {
  116. return this.clientB.emit(
  117. 'clientTracking.getConnectedUsers',
  118. (error, users) => {
  119. for (const user of Array.from(users)) {
  120. if (user.client_id === this.clientA.publicId) {
  121. expect(user.cursorData).to.deep.equal({
  122. row: this.row,
  123. column: this.column,
  124. doc_id: this.doc_id
  125. })
  126. return done()
  127. }
  128. }
  129. throw new Error('user was never found')
  130. }
  131. )
  132. })
  133. })
  134. return describe('when an anonymous client updates its cursor location', function () {
  135. before(function (done) {
  136. return async.series(
  137. [
  138. (cb) => {
  139. return FixturesManager.setUpProject(
  140. {
  141. privilegeLevel: 'owner',
  142. project: { name: 'Test Project' },
  143. publicAccess: 'readAndWrite'
  144. },
  145. (error, { user_id, project_id }) => {
  146. this.user_id = user_id
  147. this.project_id = project_id
  148. return cb()
  149. }
  150. )
  151. },
  152. (cb) => {
  153. return FixturesManager.setUpDoc(
  154. this.project_id,
  155. { lines: this.lines, version: this.version, ops: this.ops },
  156. (e, { doc_id }) => {
  157. this.doc_id = doc_id
  158. return cb(e)
  159. }
  160. )
  161. },
  162. (cb) => {
  163. this.clientA = RealTimeClient.connect()
  164. return this.clientA.on('connectionAccepted', cb)
  165. },
  166. (cb) => {
  167. return this.clientA.emit(
  168. 'joinProject',
  169. {
  170. project_id: this.project_id
  171. },
  172. cb
  173. )
  174. },
  175. (cb) => {
  176. return RealTimeClient.setSession({}, cb)
  177. },
  178. (cb) => {
  179. this.anonymous = RealTimeClient.connect()
  180. return this.anonymous.on('connectionAccepted', cb)
  181. },
  182. (cb) => {
  183. return this.anonymous.emit(
  184. 'joinProject',
  185. {
  186. project_id: this.project_id
  187. },
  188. cb
  189. )
  190. },
  191. (cb) => {
  192. return this.anonymous.emit('joinDoc', this.doc_id, cb)
  193. },
  194. (cb) => {
  195. this.updates = []
  196. this.clientA.on('clientTracking.clientUpdated', (data) => {
  197. return this.updates.push(data)
  198. })
  199. return this.anonymous.emit(
  200. 'clientTracking.updatePosition',
  201. {
  202. row: (this.row = 42),
  203. column: (this.column = 36),
  204. doc_id: this.doc_id
  205. },
  206. (error) => {
  207. if (error != null) {
  208. throw error
  209. }
  210. return setTimeout(cb, 300)
  211. }
  212. )
  213. } // Give the message a chance to reach client B.
  214. ],
  215. done
  216. )
  217. })
  218. return it('should tell other clients about the update', function () {
  219. return this.updates.should.deep.equal([
  220. {
  221. row: this.row,
  222. column: this.column,
  223. doc_id: this.doc_id,
  224. id: this.anonymous.publicId,
  225. user_id: 'anonymous-user',
  226. name: ''
  227. }
  228. ])
  229. })
  230. })
  231. })