ClientTrackingTests.js 7.1 KB

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