ClientTrackingTests.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* eslint-disable
  2. camelcase,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const { expect } = require('chai')
  15. const RealTimeClient = require('./helpers/RealTimeClient')
  16. const MockWebServer = require('./helpers/MockWebServer')
  17. const FixturesManager = require('./helpers/FixturesManager')
  18. const async = require('async')
  19. describe('clientTracking', function () {
  20. describe('when a client updates its cursor location', function () {
  21. before(function (done) {
  22. return async.series(
  23. [
  24. cb => {
  25. return FixturesManager.setUpProject(
  26. {
  27. privilegeLevel: 'owner',
  28. project: { name: 'Test Project' },
  29. },
  30. (error, { user_id, project_id }) => {
  31. if (error) return done(error)
  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. if (error) return done(error)
  118. for (const user of Array.from(users)) {
  119. if (user.client_id === this.clientA.publicId) {
  120. expect(user.cursorData).to.deep.equal({
  121. row: this.row,
  122. column: this.column,
  123. doc_id: this.doc_id,
  124. })
  125. return done()
  126. }
  127. }
  128. throw new Error('user was never found')
  129. }
  130. )
  131. })
  132. })
  133. return describe('when an anonymous client updates its cursor location', function () {
  134. before(function (done) {
  135. return async.series(
  136. [
  137. cb => {
  138. return FixturesManager.setUpProject(
  139. {
  140. privilegeLevel: 'owner',
  141. project: { name: 'Test Project' },
  142. publicAccess: 'readAndWrite',
  143. },
  144. (error, { user_id, project_id }) => {
  145. if (error) return done(error)
  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. })