RangesManagerTests.js 17 KB

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