ClientTrackingTests.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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(this.project_id, cb)
  49. },
  50. cb => {
  51. this.clientB = RealTimeClient.connect(this.project_id, cb)
  52. },
  53. cb => {
  54. return this.clientA.emit('joinDoc', this.doc_id, cb)
  55. },
  56. cb => {
  57. this.updates = []
  58. this.clientB.on('clientTracking.clientUpdated', data => {
  59. return this.updates.push(data)
  60. })
  61. return this.clientA.emit(
  62. 'clientTracking.updatePosition',
  63. {
  64. row: (this.row = 42),
  65. column: (this.column = 36),
  66. doc_id: this.doc_id,
  67. },
  68. error => {
  69. if (error != null) {
  70. throw error
  71. }
  72. return setTimeout(cb, 300)
  73. }
  74. )
  75. }, // Give the message a chance to reach client B.
  76. ],
  77. done
  78. )
  79. })
  80. it('should tell other clients about the update', function () {
  81. return this.updates.should.deep.equal([
  82. {
  83. row: this.row,
  84. column: this.column,
  85. doc_id: this.doc_id,
  86. id: this.clientA.publicId,
  87. user_id: this.user_id,
  88. name: 'Joe Bloggs',
  89. },
  90. ])
  91. })
  92. return it('should record the update in getConnectedUsers', function (done) {
  93. return this.clientB.emit(
  94. 'clientTracking.getConnectedUsers',
  95. (error, users) => {
  96. if (error) return done(error)
  97. for (const user of Array.from(users)) {
  98. if (user.client_id === this.clientA.publicId) {
  99. expect(user.cursorData).to.deep.equal({
  100. row: this.row,
  101. column: this.column,
  102. doc_id: this.doc_id,
  103. })
  104. return done()
  105. }
  106. }
  107. throw new Error('user was never found')
  108. }
  109. )
  110. })
  111. })
  112. return describe('when an anonymous client updates its cursor location', function () {
  113. before(function (done) {
  114. return async.series(
  115. [
  116. cb => {
  117. return FixturesManager.setUpProject(
  118. {
  119. privilegeLevel: 'owner',
  120. project: { name: 'Test Project' },
  121. publicAccess: 'readAndWrite',
  122. },
  123. (
  124. error,
  125. { user_id: userId, project_id: projectId, anonymousAccessToken }
  126. ) => {
  127. if (error) return done(error)
  128. this.user_id = userId
  129. this.project_id = projectId
  130. this.anonymousAccessToken = anonymousAccessToken
  131. return cb()
  132. }
  133. )
  134. },
  135. cb => {
  136. return FixturesManager.setUpDoc(
  137. this.project_id,
  138. { lines: this.lines, version: this.version, ops: this.ops },
  139. (e, { doc_id: docId }) => {
  140. this.doc_id = docId
  141. return cb(e)
  142. }
  143. )
  144. },
  145. cb => {
  146. this.clientA = RealTimeClient.connect(this.project_id, cb)
  147. },
  148. cb => {
  149. RealTimeClient.setAnonSession(
  150. this.project_id,
  151. this.anonymousAccessToken,
  152. cb
  153. )
  154. },
  155. cb => {
  156. this.anonymous = RealTimeClient.connect(this.project_id, cb)
  157. },
  158. cb => {
  159. return this.anonymous.emit('joinDoc', this.doc_id, cb)
  160. },
  161. cb => {
  162. this.updates = []
  163. this.clientA.on('clientTracking.clientUpdated', data => {
  164. return this.updates.push(data)
  165. })
  166. return this.anonymous.emit(
  167. 'clientTracking.updatePosition',
  168. {
  169. row: (this.row = 42),
  170. column: (this.column = 36),
  171. doc_id: this.doc_id,
  172. },
  173. error => {
  174. if (error != null) {
  175. throw error
  176. }
  177. return setTimeout(cb, 300)
  178. }
  179. )
  180. }, // Give the message a chance to reach client B.
  181. ],
  182. done
  183. )
  184. })
  185. return it('should tell other clients about the update', function () {
  186. return this.updates.should.deep.equal([
  187. {
  188. row: this.row,
  189. column: this.column,
  190. doc_id: this.doc_id,
  191. id: this.anonymous.publicId,
  192. user_id: 'anonymous-user',
  193. name: '',
  194. },
  195. ])
  196. })
  197. })
  198. })