ClientTrackingTests.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. (error, { user_id: userId, project_id: projectId }) => {
  144. if (error) return done(error)
  145. this.user_id = userId
  146. this.project_id = projectId
  147. return cb()
  148. }
  149. )
  150. },
  151. cb => {
  152. return FixturesManager.setUpDoc(
  153. this.project_id,
  154. { lines: this.lines, version: this.version, ops: this.ops },
  155. (e, { doc_id: docId }) => {
  156. this.doc_id = docId
  157. return cb(e)
  158. }
  159. )
  160. },
  161. cb => {
  162. this.clientA = RealTimeClient.connect()
  163. return this.clientA.on('connectionAccepted', cb)
  164. },
  165. cb => {
  166. return this.clientA.emit(
  167. 'joinProject',
  168. {
  169. project_id: this.project_id,
  170. },
  171. cb
  172. )
  173. },
  174. cb => {
  175. return RealTimeClient.setSession({}, cb)
  176. },
  177. cb => {
  178. this.anonymous = RealTimeClient.connect()
  179. return this.anonymous.on('connectionAccepted', cb)
  180. },
  181. cb => {
  182. return this.anonymous.emit(
  183. 'joinProject',
  184. {
  185. project_id: this.project_id,
  186. },
  187. cb
  188. )
  189. },
  190. cb => {
  191. return this.anonymous.emit('joinDoc', this.doc_id, cb)
  192. },
  193. cb => {
  194. this.updates = []
  195. this.clientA.on('clientTracking.clientUpdated', data => {
  196. return this.updates.push(data)
  197. })
  198. return this.anonymous.emit(
  199. 'clientTracking.updatePosition',
  200. {
  201. row: (this.row = 42),
  202. column: (this.column = 36),
  203. doc_id: this.doc_id,
  204. },
  205. error => {
  206. if (error != null) {
  207. throw error
  208. }
  209. return setTimeout(cb, 300)
  210. }
  211. )
  212. }, // Give the message a chance to reach client B.
  213. ],
  214. done
  215. )
  216. })
  217. return it('should tell other clients about the update', function () {
  218. return this.updates.should.deep.equal([
  219. {
  220. row: this.row,
  221. column: this.column,
  222. doc_id: this.doc_id,
  223. id: this.anonymous.publicId,
  224. user_id: 'anonymous-user',
  225. name: '',
  226. },
  227. ])
  228. })
  229. })
  230. })