RangesManagerTests.js 18 KB

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