RangeManagerTests.js 6.9 KB

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