RangesManagerTests.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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('insertion does not match text in document')
  164. })
  165. })
  166. describe('with an update that collapses a range', function () {
  167. beforeEach(function () {
  168. this.updates = makeUpdates([{ d: 'one', p: 0, t: 'thread-id-1' }])
  169. this.ranges = {
  170. comments: makeRanges([
  171. {
  172. c: 'n',
  173. p: 1,
  174. t: 'thread-id-2',
  175. },
  176. ]),
  177. changes: [],
  178. }
  179. this.result = this.RangesManager.applyUpdate(
  180. this.project_id,
  181. this.doc_id,
  182. this.ranges,
  183. this.updates,
  184. this.newDocLines
  185. )
  186. })
  187. it('should return ranges_were_collapsed == true', function () {
  188. expect(this.result.rangesWereCollapsed).to.equal(true)
  189. })
  190. })
  191. describe('with an update that deletes ranges', function () {
  192. beforeEach(function () {
  193. this.updates = makeUpdates([{ d: 'one two three four five', p: 0 }])
  194. this.ranges = {
  195. comments: makeRanges([{ c: 'n', p: 1, t: 'thread-id-2' }]),
  196. changes: makeRanges([{ i: 'hello', p: 1, t: 'thread-id-2' }]),
  197. }
  198. this.result = this.RangesManager.applyUpdate(
  199. this.project_id,
  200. this.doc_id,
  201. this.ranges,
  202. this.updates,
  203. this.newDocLines
  204. )
  205. })
  206. it('should increment the range-delta histogram', function () {
  207. this.Metrics.histogram.called.should.equal(true)
  208. })
  209. it('should return ranges_were_collapsed == true', function () {
  210. expect(this.result.rangesWereCollapsed).to.equal(true)
  211. })
  212. })
  213. describe('with comment updates', function () {
  214. beforeEach(function () {
  215. this.updates = makeUpdates([
  216. { i: 'two ', p: 4 },
  217. { c: 'one', p: 0 },
  218. ])
  219. this.ranges = {}
  220. this.result = this.RangesManager.applyUpdate(
  221. this.project_id,
  222. this.doc_id,
  223. this.ranges,
  224. this.updates,
  225. this.newDocLines
  226. )
  227. })
  228. it('should not send comments to the history', function () {
  229. expect(this.result.historyUpdates[0].op).to.deep.equal([
  230. { i: 'two ', p: 4 },
  231. ])
  232. })
  233. })
  234. describe('with history ranges support', function () {
  235. describe('inserts among tracked deletes', function () {
  236. beforeEach(function () {
  237. // original text is "on[1]e[22] [333](three) fo[4444]ur five"
  238. // [] denotes tracked deletes
  239. // () denotes tracked inserts
  240. this.ranges = {
  241. changes: makeRanges([
  242. { d: '1', p: 2 },
  243. { d: '22', p: 3 },
  244. { d: '333', p: 4 },
  245. { i: 'three', p: 4 },
  246. { d: '4444', p: 12 },
  247. ]),
  248. }
  249. this.updates = makeUpdates([
  250. { i: 'zero ', p: 0 },
  251. { i: 'two ', p: 9, u: true },
  252. ])
  253. this.newDocLines = ['zero one two three four five']
  254. this.result = this.RangesManager.applyUpdate(
  255. this.project_id,
  256. this.doc_id,
  257. this.ranges,
  258. this.updates,
  259. this.newDocLines,
  260. { historyRangesSupport: true }
  261. )
  262. })
  263. it('should offset the hpos by the length of tracked deletes before the insert', function () {
  264. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  265. [{ i: 'zero ', p: 0 }],
  266. // 'two' is added just before the "333" tracked delete
  267. [{ i: 'two ', p: 9, u: true, hpos: 12 }],
  268. ])
  269. })
  270. })
  271. describe('tracked delete rejections', function () {
  272. beforeEach(function () {
  273. // original text is "one [two ]three four five"
  274. // [] denotes tracked deletes
  275. this.ranges = {
  276. changes: makeRanges([{ d: 'two ', p: 4 }]),
  277. }
  278. this.updates = makeUpdates([{ i: 'tw', p: 4, u: true }])
  279. this.newDocLines = ['one twthree four five']
  280. this.result = this.RangesManager.applyUpdate(
  281. this.project_id,
  282. this.doc_id,
  283. this.ranges,
  284. this.updates,
  285. this.newDocLines,
  286. { historyRangesSupport: true }
  287. )
  288. })
  289. it('should mark the insert as a tracked delete rejection where appropriate', function () {
  290. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  291. [{ i: 'tw', p: 4, u: true, trackedDeleteRejection: true }],
  292. ])
  293. })
  294. })
  295. describe('deletes among tracked deletes', function () {
  296. beforeEach(function () {
  297. // original text is "on[1]e [22](three) f[333]ou[4444]r [55555]five"
  298. // [] denotes tracked deletes
  299. // () denotes tracked inserts
  300. this.ranges = {
  301. comments: [],
  302. changes: makeRanges([
  303. { d: '1', p: 2 },
  304. { d: '22', p: 4 },
  305. { i: 'three', p: 4 },
  306. { d: '333', p: 11 },
  307. { d: '4444', p: 13 },
  308. { d: '55555', p: 15 },
  309. ]),
  310. }
  311. this.updates = makeUpdates([
  312. { d: 'four ', p: 10 },
  313. { d: 'three ', p: 4 },
  314. ])
  315. this.newDocLines = ['one five']
  316. this.result = this.RangesManager.applyUpdate(
  317. this.project_id,
  318. this.doc_id,
  319. this.ranges,
  320. this.updates,
  321. this.newDocLines,
  322. { historyRangesSupport: true }
  323. )
  324. })
  325. it('should split and offset deletes appropriately', function () {
  326. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  327. [
  328. // the "four" delete has tracked deletes inside it, add splits
  329. {
  330. d: 'four ',
  331. p: 10,
  332. hpos: 13,
  333. hsplits: [
  334. { offset: 1, length: 3 },
  335. { offset: 3, length: 4 },
  336. ],
  337. },
  338. ],
  339. // the "three" delete is offset to the right by the two first tracked
  340. // deletes
  341. [{ d: 'three ', p: 4, hpos: 7 }],
  342. ])
  343. })
  344. })
  345. describe('comments among tracked deletes', function () {
  346. beforeEach(function () {
  347. // original text is "on[1]e[22] [333](three) fo[4444]ur five"
  348. // [] denotes tracked deletes
  349. // () denotes tracked inserts
  350. this.ranges = {
  351. changes: makeRanges([
  352. { d: '1', p: 2 },
  353. { d: '22', p: 3 },
  354. { d: '333', p: 4 },
  355. { i: 'three', p: 4 },
  356. { d: '4444', p: 12 },
  357. ]),
  358. }
  359. this.updates = makeUpdates([
  360. { c: 'three ', p: 4 },
  361. { c: 'four ', p: 10 },
  362. ])
  363. this.newDocLines = ['one three four five']
  364. this.result = this.RangesManager.applyUpdate(
  365. this.project_id,
  366. this.doc_id,
  367. this.ranges,
  368. this.updates,
  369. this.newDocLines,
  370. { historyRangesSupport: true }
  371. )
  372. })
  373. it('should offset the hpos by the length of tracked deletes before the insert', function () {
  374. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  375. [{ c: 'three ', p: 4, hpos: 10 }],
  376. [{ c: 'four ', p: 10, hpos: 16, hlen: 9 }],
  377. ])
  378. })
  379. })
  380. describe('inserts inside comments', function () {
  381. beforeEach(function () {
  382. // original text is "one three four five"
  383. this.ranges = {
  384. comments: makeRanges([
  385. { c: 'three', p: 4, t: 'comment-id-1' },
  386. { c: 'ree four', p: 6, t: 'comment-id-2' },
  387. ]),
  388. }
  389. this.updates = makeUpdates([
  390. { i: '[before]', p: 4 },
  391. { i: '[inside]', p: 13 }, // 4 + 8 + 1
  392. { i: '[overlap]', p: 23 }, // 13 + 8 + 2
  393. { i: '[after]', p: 39 }, // 23 + 9 + 7
  394. ])
  395. this.newDocLines = [
  396. 'one [before]t[inside]hr[overlap]ee four[after] five',
  397. ]
  398. this.result = this.RangesManager.applyUpdate(
  399. this.project_id,
  400. this.doc_id,
  401. this.ranges,
  402. this.updates,
  403. this.newDocLines,
  404. { historyRangesSupport: true }
  405. )
  406. })
  407. it('should add the proper commentIds properties to ops', function () {
  408. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  409. [{ i: '[before]', p: 4 }],
  410. [{ i: '[inside]', p: 13, commentIds: ['comment-id-1'] }],
  411. [
  412. {
  413. i: '[overlap]',
  414. p: 23,
  415. commentIds: ['comment-id-1', 'comment-id-2'],
  416. },
  417. ],
  418. [{ i: '[after]', p: 39 }],
  419. ])
  420. })
  421. })
  422. })
  423. })
  424. describe('acceptChanges', function () {
  425. beforeEach(function () {
  426. this.RangesManager = SandboxedModule.require(MODULE_PATH, {
  427. requires: {
  428. '@overleaf/ranges-tracker': (this.RangesTracker =
  429. SandboxedModule.require('@overleaf/ranges-tracker')),
  430. '@overleaf/metrics': {},
  431. },
  432. })
  433. this.ranges = {
  434. comments: [],
  435. changes: makeRanges([
  436. { i: 'lorem', p: 0 },
  437. { i: 'ipsum', p: 10 },
  438. { i: 'dolor', p: 20 },
  439. { i: 'sit', p: 30 },
  440. { i: 'amet', p: 40 },
  441. ]),
  442. }
  443. this.removeChangeIdsSpy = sinon.spy(
  444. this.RangesTracker.prototype,
  445. 'removeChangeIds'
  446. )
  447. })
  448. describe('successfully with a single change', function () {
  449. beforeEach(function () {
  450. this.change_ids = [this.ranges.changes[1].id]
  451. this.result = this.RangesManager.acceptChanges(
  452. this.change_ids,
  453. this.ranges
  454. )
  455. })
  456. it('should log the call with the correct number of changes', function () {
  457. this.logger.debug
  458. .calledWith('accepting 1 changes in ranges')
  459. .should.equal(true)
  460. })
  461. it('should delegate the change removal to the ranges tracker', function () {
  462. this.removeChangeIdsSpy.calledWith(this.change_ids).should.equal(true)
  463. })
  464. it('should remove the change', function () {
  465. expect(
  466. this.result.changes.find(
  467. change => change.id === this.ranges.changes[1].id
  468. )
  469. ).to.be.undefined
  470. })
  471. it('should return the original number of changes minus 1', function () {
  472. this.result.changes.length.should.equal(this.ranges.changes.length - 1)
  473. })
  474. it('should not touch other changes', function () {
  475. for (const i of [0, 2, 3, 4]) {
  476. expect(
  477. this.result.changes.find(
  478. change => change.id === this.ranges.changes[i].id
  479. )
  480. ).to.deep.equal(this.ranges.changes[i])
  481. }
  482. })
  483. })
  484. describe('successfully with multiple changes', function () {
  485. beforeEach(function () {
  486. this.change_ids = [
  487. this.ranges.changes[1].id,
  488. this.ranges.changes[3].id,
  489. this.ranges.changes[4].id,
  490. ]
  491. this.result = this.RangesManager.acceptChanges(
  492. this.change_ids,
  493. this.ranges
  494. )
  495. })
  496. it('should log the call with the correct number of changes', function () {
  497. this.logger.debug
  498. .calledWith(`accepting ${this.change_ids.length} changes in ranges`)
  499. .should.equal(true)
  500. })
  501. it('should delegate the change removal to the ranges tracker', function () {
  502. this.removeChangeIdsSpy.calledWith(this.change_ids).should.equal(true)
  503. })
  504. it('should remove the changes', function () {
  505. for (const i of [1, 3, 4]) {
  506. expect(
  507. this.result.changes.find(
  508. change => change.id === this.ranges.changes[i].id
  509. )
  510. ).to.be.undefined
  511. }
  512. })
  513. it('should return the original number of changes minus the number of accepted changes', function () {
  514. this.result.changes.length.should.equal(this.ranges.changes.length - 3)
  515. })
  516. it('should not touch other changes', function () {
  517. for (const i of [0, 2]) {
  518. expect(
  519. this.result.changes.find(
  520. change => change.id === this.ranges.changes[i].id
  521. )
  522. ).to.deep.equal(this.ranges.changes[i])
  523. }
  524. })
  525. })
  526. })
  527. })
  528. function makeRanges(ops) {
  529. let id = 1
  530. const changes = []
  531. for (const op of ops) {
  532. changes.push({
  533. id: id.toString(),
  534. op,
  535. metadata: { user_id: TEST_USER_ID },
  536. })
  537. id += 1
  538. }
  539. return changes
  540. }
  541. function makeUpdates(ops, meta = {}) {
  542. const updates = []
  543. for (const op of ops) {
  544. updates.push({
  545. meta: { user_id: TEST_USER_ID, ...meta },
  546. op: [op],
  547. })
  548. }
  549. return updates
  550. }