RangesManagerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. * DS101: Remove unnecessary use of Array.from
  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 sinon = require('sinon')
  14. const { expect } = require('chai')
  15. const modulePath = '../../../../app/js/RangesManager.js'
  16. const SandboxedModule = require('sandboxed-module')
  17. describe('RangesManager', function () {
  18. beforeEach(function () {
  19. this.RangesManager = SandboxedModule.require(modulePath, {
  20. requires: { './Metrics': (this.Metrics = { histogram: sinon.stub() }) },
  21. })
  22. this.doc_id = 'doc-id-123'
  23. this.project_id = 'project-id-123'
  24. this.user_id = 'user-id-123'
  25. return (this.callback = sinon.stub())
  26. })
  27. describe('applyUpdate', function () {
  28. beforeEach(function () {
  29. this.updates = [
  30. {
  31. meta: {
  32. user_id: this.user_id,
  33. },
  34. op: [
  35. {
  36. i: 'two ',
  37. p: 4,
  38. },
  39. ],
  40. },
  41. ]
  42. this.entries = {
  43. comments: [
  44. {
  45. op: {
  46. c: 'three ',
  47. p: 4,
  48. },
  49. metadata: {
  50. user_id: this.user_id,
  51. },
  52. },
  53. ],
  54. changes: [
  55. {
  56. op: {
  57. i: 'five',
  58. p: 15,
  59. },
  60. metadata: {
  61. user_id: this.user_id,
  62. },
  63. },
  64. ],
  65. }
  66. return (this.newDocLines = ['one two three four five'])
  67. }) // old is "one three four five"
  68. describe('successfully', function () {
  69. beforeEach(function () {
  70. return this.RangesManager.applyUpdate(
  71. this.project_id,
  72. this.doc_id,
  73. this.entries,
  74. this.updates,
  75. this.newDocLines,
  76. this.callback
  77. )
  78. })
  79. return it('should return the modified the comments and changes', function () {
  80. this.callback.called.should.equal(true)
  81. const [error, entries, rangesWereCollapsed] = Array.from(
  82. this.callback.args[0]
  83. )
  84. expect(error).to.be.null
  85. expect(rangesWereCollapsed).to.equal(false)
  86. entries.comments[0].op.should.deep.equal({
  87. c: 'three ',
  88. p: 8,
  89. })
  90. return entries.changes[0].op.should.deep.equal({
  91. i: 'five',
  92. p: 19,
  93. })
  94. })
  95. })
  96. describe('with empty comments', function () {
  97. beforeEach(function () {
  98. this.entries.comments = []
  99. return this.RangesManager.applyUpdate(
  100. this.project_id,
  101. this.doc_id,
  102. this.entries,
  103. this.updates,
  104. this.newDocLines,
  105. this.callback
  106. )
  107. })
  108. return it('should return an object with no comments', function () {
  109. // Save space in redis and don't store just {}
  110. this.callback.called.should.equal(true)
  111. const [error, entries] = Array.from(this.callback.args[0])
  112. expect(error).to.be.null
  113. return expect(entries.comments).to.be.undefined
  114. })
  115. })
  116. describe('with empty changes', function () {
  117. beforeEach(function () {
  118. this.entries.changes = []
  119. return this.RangesManager.applyUpdate(
  120. this.project_id,
  121. this.doc_id,
  122. this.entries,
  123. this.updates,
  124. this.newDocLines,
  125. this.callback
  126. )
  127. })
  128. return it('should return an object with no changes', function () {
  129. // Save space in redis and don't store just {}
  130. this.callback.called.should.equal(true)
  131. const [error, entries] = Array.from(this.callback.args[0])
  132. expect(error).to.be.null
  133. return expect(entries.changes).to.be.undefined
  134. })
  135. })
  136. describe('with too many comments', function () {
  137. beforeEach(function () {
  138. this.RangesManager.MAX_COMMENTS = 2
  139. this.updates = [
  140. {
  141. meta: {
  142. user_id: this.user_id,
  143. },
  144. op: [
  145. {
  146. c: 'one',
  147. p: 0,
  148. t: 'thread-id-1',
  149. },
  150. ],
  151. },
  152. ]
  153. this.entries = {
  154. comments: [
  155. {
  156. op: {
  157. c: 'three ',
  158. p: 4,
  159. t: 'thread-id-2',
  160. },
  161. metadata: {
  162. user_id: this.user_id,
  163. },
  164. },
  165. {
  166. op: {
  167. c: 'four ',
  168. p: 10,
  169. t: 'thread-id-3',
  170. },
  171. metadata: {
  172. user_id: this.user_id,
  173. },
  174. },
  175. ],
  176. changes: [],
  177. }
  178. return this.RangesManager.applyUpdate(
  179. this.project_id,
  180. this.doc_id,
  181. this.entries,
  182. this.updates,
  183. this.newDocLines,
  184. this.callback
  185. )
  186. })
  187. return it('should return an error', function () {
  188. this.callback.called.should.equal(true)
  189. const [error, entries] = Array.from(this.callback.args[0])
  190. expect(error).to.not.be.null
  191. return expect(error.message).to.equal(
  192. 'too many comments or tracked changes'
  193. )
  194. })
  195. })
  196. describe('with too many changes', function () {
  197. beforeEach(function () {
  198. this.RangesManager.MAX_CHANGES = 2
  199. this.updates = [
  200. {
  201. meta: {
  202. user_id: this.user_id,
  203. tc: 'track-changes-id-yes',
  204. },
  205. op: [
  206. {
  207. i: 'one ',
  208. p: 0,
  209. },
  210. ],
  211. },
  212. ]
  213. this.entries = {
  214. changes: [
  215. {
  216. op: {
  217. i: 'three',
  218. p: 4,
  219. },
  220. metadata: {
  221. user_id: this.user_id,
  222. },
  223. },
  224. {
  225. op: {
  226. i: 'four',
  227. p: 10,
  228. },
  229. metadata: {
  230. user_id: this.user_id,
  231. },
  232. },
  233. ],
  234. comments: [],
  235. }
  236. this.newDocLines = ['one two three four']
  237. return this.RangesManager.applyUpdate(
  238. this.project_id,
  239. this.doc_id,
  240. this.entries,
  241. this.updates,
  242. this.newDocLines,
  243. this.callback
  244. )
  245. })
  246. return it('should return an error', function () {
  247. // Save space in redis and don't store just {}
  248. this.callback.called.should.equal(true)
  249. const [error, entries] = Array.from(this.callback.args[0])
  250. expect(error).to.not.be.null
  251. return expect(error.message).to.equal(
  252. 'too many comments or tracked changes'
  253. )
  254. })
  255. })
  256. describe('inconsistent changes', function () {
  257. beforeEach(function () {
  258. this.updates = [
  259. {
  260. meta: {
  261. user_id: this.user_id,
  262. },
  263. op: [
  264. {
  265. c: "doesn't match",
  266. p: 0,
  267. },
  268. ],
  269. },
  270. ]
  271. return this.RangesManager.applyUpdate(
  272. this.project_id,
  273. this.doc_id,
  274. this.entries,
  275. this.updates,
  276. this.newDocLines,
  277. this.callback
  278. )
  279. })
  280. return it('should return an error', function () {
  281. // Save space in redis and don't store just {}
  282. this.callback.called.should.equal(true)
  283. const [error, entries] = Array.from(this.callback.args[0])
  284. expect(error).to.not.be.null
  285. return expect(error.message).to.equal(
  286. 'Change ({"op":{"i":"five","p":15},"metadata":{"user_id":"user-id-123"}}) doesn\'t match text ("our ")'
  287. )
  288. })
  289. })
  290. describe('with an update that collapses a range', function () {
  291. beforeEach(function () {
  292. this.updates = [
  293. {
  294. meta: {
  295. user_id: this.user_id,
  296. },
  297. op: [
  298. {
  299. d: 'one',
  300. p: 0,
  301. t: 'thread-id-1',
  302. },
  303. ],
  304. },
  305. ]
  306. this.entries = {
  307. comments: [
  308. {
  309. op: {
  310. c: 'n',
  311. p: 1,
  312. t: 'thread-id-2',
  313. },
  314. metadata: {
  315. user_id: this.user_id,
  316. },
  317. },
  318. ],
  319. changes: [],
  320. }
  321. return this.RangesManager.applyUpdate(
  322. this.project_id,
  323. this.doc_id,
  324. this.entries,
  325. this.updates,
  326. this.newDocLines,
  327. this.callback
  328. )
  329. })
  330. return it('should return ranges_were_collapsed == true', function () {
  331. this.callback.called.should.equal(true)
  332. const [error, entries, rangesWereCollapsed] = Array.from(
  333. this.callback.args[0]
  334. )
  335. return expect(rangesWereCollapsed).to.equal(true)
  336. })
  337. })
  338. describe('with an update that deletes ranges', function () {
  339. beforeEach(function () {
  340. this.updates = [
  341. {
  342. meta: {
  343. user_id: this.user_id,
  344. },
  345. op: [
  346. {
  347. d: 'one two three four five',
  348. p: 0,
  349. },
  350. ],
  351. },
  352. ]
  353. this.entries = {
  354. comments: [
  355. {
  356. op: {
  357. c: 'n',
  358. p: 1,
  359. t: 'thread-id-2',
  360. },
  361. metadata: {
  362. user_id: this.user_id,
  363. },
  364. },
  365. ],
  366. changes: [
  367. {
  368. op: {
  369. i: 'hello',
  370. p: 1,
  371. t: 'thread-id-2',
  372. },
  373. metadata: {
  374. user_id: this.user_id,
  375. },
  376. },
  377. ],
  378. }
  379. return this.RangesManager.applyUpdate(
  380. this.project_id,
  381. this.doc_id,
  382. this.entries,
  383. this.updates,
  384. this.newDocLines,
  385. this.callback
  386. )
  387. })
  388. it('should increment the range-delta histogram', function () {
  389. this.Metrics.histogram.called.should.equal(true)
  390. })
  391. return it('should return ranges_were_collapsed == true', function () {
  392. this.callback.called.should.equal(true)
  393. const [error, entries, rangesWereCollapsed] = Array.from(
  394. this.callback.args[0]
  395. )
  396. return expect(rangesWereCollapsed).to.equal(true)
  397. })
  398. })
  399. })
  400. return describe('acceptChanges', function () {
  401. beforeEach(function () {
  402. this.RangesManager = SandboxedModule.require(modulePath, {
  403. requires: {
  404. '@overleaf/ranges-tracker': (this.RangesTracker =
  405. SandboxedModule.require('@overleaf/ranges-tracker')),
  406. },
  407. })
  408. this.ranges = {
  409. comments: [],
  410. changes: [
  411. {
  412. id: 'a1',
  413. op: {
  414. i: 'lorem',
  415. p: 0,
  416. },
  417. },
  418. {
  419. id: 'a2',
  420. op: {
  421. i: 'ipsum',
  422. p: 10,
  423. },
  424. },
  425. {
  426. id: 'a3',
  427. op: {
  428. i: 'dolor',
  429. p: 20,
  430. },
  431. },
  432. {
  433. id: 'a4',
  434. op: {
  435. i: 'sit',
  436. p: 30,
  437. },
  438. },
  439. {
  440. id: 'a5',
  441. op: {
  442. i: 'amet',
  443. p: 40,
  444. },
  445. },
  446. ],
  447. }
  448. return (this.removeChangeIdsSpy = sinon.spy(
  449. this.RangesTracker.prototype,
  450. 'removeChangeIds'
  451. ))
  452. })
  453. describe('successfully with a single change', function () {
  454. beforeEach(function (done) {
  455. this.change_ids = [this.ranges.changes[1].id]
  456. return this.RangesManager.acceptChanges(
  457. this.change_ids,
  458. this.ranges,
  459. (err, ranges) => {
  460. if (err) return done(err)
  461. this.rangesResponse = ranges
  462. return done()
  463. }
  464. )
  465. })
  466. it('should log the call with the correct number of changes', function () {
  467. return this.logger.debug
  468. .calledWith('accepting 1 changes in ranges')
  469. .should.equal(true)
  470. })
  471. it('should delegate the change removal to the ranges tracker', function () {
  472. return this.removeChangeIdsSpy
  473. .calledWith(this.change_ids)
  474. .should.equal(true)
  475. })
  476. it('should remove the change', function () {
  477. return expect(
  478. this.rangesResponse.changes.find(
  479. change => change.id === this.ranges.changes[1].id
  480. )
  481. ).to.be.undefined
  482. })
  483. it('should return the original number of changes minus 1', function () {
  484. return this.rangesResponse.changes.length.should.equal(
  485. this.ranges.changes.length - 1
  486. )
  487. })
  488. return it('should not touch other changes', function () {
  489. return [0, 2, 3, 4].map(i =>
  490. expect(
  491. this.rangesResponse.changes.find(
  492. change => change.id === this.ranges.changes[i].id
  493. )
  494. ).to.deep.equal(this.ranges.changes[i])
  495. )
  496. })
  497. })
  498. return describe('successfully with multiple changes', function () {
  499. beforeEach(function (done) {
  500. this.change_ids = [
  501. this.ranges.changes[1].id,
  502. this.ranges.changes[3].id,
  503. this.ranges.changes[4].id,
  504. ]
  505. return this.RangesManager.acceptChanges(
  506. this.change_ids,
  507. this.ranges,
  508. (err, ranges) => {
  509. if (err) return done(err)
  510. this.rangesResponse = ranges
  511. return done()
  512. }
  513. )
  514. })
  515. it('should log the call with the correct number of changes', function () {
  516. return this.logger.debug
  517. .calledWith(`accepting ${this.change_ids.length} changes in ranges`)
  518. .should.equal(true)
  519. })
  520. it('should delegate the change removal to the ranges tracker', function () {
  521. return this.removeChangeIdsSpy
  522. .calledWith(this.change_ids)
  523. .should.equal(true)
  524. })
  525. it('should remove the changes', function () {
  526. return [1, 3, 4].map(
  527. i =>
  528. expect(
  529. this.rangesResponse.changes.find(
  530. change => change.id === this.ranges.changes[1].id
  531. )
  532. ).to.be.undefined
  533. )
  534. })
  535. it('should return the original number of changes minus the number of accepted changes', function () {
  536. return this.rangesResponse.changes.length.should.equal(
  537. this.ranges.changes.length - 3
  538. )
  539. })
  540. return it('should not touch other changes', function () {
  541. return [0, 2].map(i =>
  542. expect(
  543. this.rangesResponse.changes.find(
  544. change => change.id === this.ranges.changes[i].id
  545. )
  546. ).to.deep.equal(this.ranges.changes[i])
  547. )
  548. })
  549. })
  550. })
  551. })