ClientTrackingTests.js 6.8 KB

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