RangeManagerTests.js 7.0 KB

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