RangeManagerTests.js 6.9 KB

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