RangeManagerTests.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  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. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. require('chai').should()
  16. const { expect } = require('chai')
  17. const modulePath = require('path').join(
  18. __dirname,
  19. '../../../app/js/RangeManager'
  20. )
  21. const { ObjectId } = require('mongodb')
  22. const { assert } = require('chai')
  23. const _ = require('underscore')
  24. describe('RangeManager', function () {
  25. beforeEach(function () {
  26. return (this.RangeManager = SandboxedModule.require(modulePath, {
  27. requires: {
  28. './mongodb': {
  29. ObjectId
  30. }
  31. }
  32. }))
  33. })
  34. describe('jsonRangesToMongo', function () {
  35. it('should convert ObjectIds and dates to proper objects', function () {
  36. const change_id = ObjectId().toString()
  37. const comment_id = ObjectId().toString()
  38. const user_id = ObjectId().toString()
  39. const thread_id = ObjectId().toString()
  40. const ts = new Date().toJSON()
  41. return this.RangeManager.jsonRangesToMongo({
  42. changes: [
  43. {
  44. id: change_id,
  45. op: { i: 'foo', p: 3 },
  46. metadata: {
  47. user_id,
  48. ts
  49. }
  50. }
  51. ],
  52. comments: [
  53. {
  54. id: comment_id,
  55. op: { c: 'foo', p: 3, t: thread_id }
  56. }
  57. ]
  58. }).should.deep.equal({
  59. changes: [
  60. {
  61. id: ObjectId(change_id),
  62. op: { i: 'foo', p: 3 },
  63. metadata: {
  64. user_id: ObjectId(user_id),
  65. ts: new Date(ts)
  66. }
  67. }
  68. ],
  69. comments: [
  70. {
  71. id: ObjectId(comment_id),
  72. op: { c: 'foo', p: 3, t: ObjectId(thread_id) }
  73. }
  74. ]
  75. })
  76. })
  77. it('should leave malformed ObjectIds as they are', function () {
  78. const change_id = 'foo'
  79. const comment_id = 'bar'
  80. const user_id = 'baz'
  81. return this.RangeManager.jsonRangesToMongo({
  82. changes: [
  83. {
  84. id: change_id,
  85. metadata: {
  86. user_id
  87. }
  88. }
  89. ],
  90. comments: [
  91. {
  92. id: comment_id
  93. }
  94. ]
  95. }).should.deep.equal({
  96. changes: [
  97. {
  98. id: change_id,
  99. metadata: {
  100. user_id
  101. }
  102. }
  103. ],
  104. comments: [
  105. {
  106. id: comment_id
  107. }
  108. ]
  109. })
  110. })
  111. return it('should be consistent when transformed through json -> mongo -> json', function () {
  112. const change_id = ObjectId().toString()
  113. const comment_id = ObjectId().toString()
  114. const user_id = ObjectId().toString()
  115. const thread_id = ObjectId().toString()
  116. const ts = new Date().toJSON()
  117. const ranges1 = {
  118. changes: [
  119. {
  120. id: change_id,
  121. op: { i: 'foo', p: 3 },
  122. metadata: {
  123. user_id,
  124. ts
  125. }
  126. }
  127. ],
  128. comments: [
  129. {
  130. id: comment_id,
  131. op: { c: 'foo', p: 3, t: thread_id }
  132. }
  133. ]
  134. }
  135. const ranges1_copy = JSON.parse(JSON.stringify(ranges1)) // jsonRangesToMongo modifies in place
  136. const ranges2 = JSON.parse(
  137. JSON.stringify(this.RangeManager.jsonRangesToMongo(ranges1_copy))
  138. )
  139. return ranges1.should.deep.equal(ranges2)
  140. })
  141. })
  142. return describe('shouldUpdateRanges', function () {
  143. beforeEach(function () {
  144. this.ranges = {
  145. changes: [
  146. {
  147. id: ObjectId(),
  148. op: { i: 'foo', p: 3 },
  149. metadata: {
  150. user_id: ObjectId(),
  151. ts: new Date()
  152. }
  153. }
  154. ],
  155. comments: [
  156. {
  157. id: ObjectId(),
  158. op: { c: 'foo', p: 3, t: ObjectId() }
  159. }
  160. ]
  161. }
  162. return (this.ranges_copy = this.RangeManager.jsonRangesToMongo(
  163. JSON.parse(JSON.stringify(this.ranges))
  164. ))
  165. })
  166. describe('with a blank new range', function () {
  167. return it('should throw an error', function () {
  168. return expect(() => {
  169. return this.RangeManager.shouldUpdateRanges(this.ranges, null)
  170. }).to.throw(Error)
  171. })
  172. })
  173. describe('with a blank old range', function () {
  174. return it('should treat it like {}', function () {
  175. this.RangeManager.shouldUpdateRanges(null, {}).should.equal(false)
  176. return this.RangeManager.shouldUpdateRanges(
  177. null,
  178. this.ranges
  179. ).should.equal(true)
  180. })
  181. })
  182. describe('with no changes', function () {
  183. return it('should return false', function () {
  184. return this.RangeManager.shouldUpdateRanges(
  185. this.ranges,
  186. this.ranges_copy
  187. ).should.equal(false)
  188. })
  189. })
  190. return describe('with changes', function () {
  191. it('should return true when the change id changes', function () {
  192. this.ranges_copy.changes[0].id = ObjectId()
  193. return this.RangeManager.shouldUpdateRanges(
  194. this.ranges,
  195. this.ranges_copy
  196. ).should.equal(true)
  197. })
  198. it('should return true when the change user id changes', function () {
  199. this.ranges_copy.changes[0].metadata.user_id = ObjectId()
  200. return this.RangeManager.shouldUpdateRanges(
  201. this.ranges,
  202. this.ranges_copy
  203. ).should.equal(true)
  204. })
  205. it('should return true when the change ts changes', function () {
  206. this.ranges_copy.changes[0].metadata.ts = new Date(Date.now() + 1000)
  207. return this.RangeManager.shouldUpdateRanges(
  208. this.ranges,
  209. this.ranges_copy
  210. ).should.equal(true)
  211. })
  212. it('should return true when the change op changes', function () {
  213. this.ranges_copy.changes[0].op.i = 'bar'
  214. return this.RangeManager.shouldUpdateRanges(
  215. this.ranges,
  216. this.ranges_copy
  217. ).should.equal(true)
  218. })
  219. it('should return true when the comment id changes', function () {
  220. this.ranges_copy.comments[0].id = ObjectId()
  221. return this.RangeManager.shouldUpdateRanges(
  222. this.ranges,
  223. this.ranges_copy
  224. ).should.equal(true)
  225. })
  226. it('should return true when the comment offset changes', function () {
  227. this.ranges_copy.comments[0].op.p = 17
  228. return this.RangeManager.shouldUpdateRanges(
  229. this.ranges,
  230. this.ranges_copy
  231. ).should.equal(true)
  232. })
  233. return it('should return true when the comment content changes', function () {
  234. this.ranges_copy.comments[0].op.c = 'bar'
  235. return this.RangeManager.shouldUpdateRanges(
  236. this.ranges,
  237. this.ranges_copy
  238. ).should.equal(true)
  239. })
  240. })
  241. })
  242. })