RangesManagerTests.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 over tracked changes', 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. trackedChanges: [
  334. { type: 'delete', offset: 1, length: 3 },
  335. { type: 'delete', offset: 3, length: 4 },
  336. ],
  337. },
  338. ],
  339. // the "three" delete is offset to the right by the two first tracked
  340. // deletes
  341. [
  342. {
  343. d: 'three ',
  344. p: 4,
  345. hpos: 7,
  346. trackedChanges: [{ type: 'insert', offset: 0, length: 5 }],
  347. },
  348. ],
  349. ])
  350. })
  351. })
  352. describe('deletes that overlap tracked inserts', function () {
  353. beforeEach(function () {
  354. // original text is "(one) (three) (four) five"
  355. // [] denotes tracked deletes
  356. // () denotes tracked inserts
  357. this.ranges = {
  358. comments: [],
  359. changes: makeRanges([
  360. { i: 'one', p: 0 },
  361. { i: 'three', p: 4 },
  362. { i: 'four', p: 10 },
  363. ]),
  364. }
  365. this.updates = makeUpdates(
  366. [
  367. { d: 'ne th', p: 1 },
  368. { d: 'ou', p: 6 },
  369. ],
  370. { tc: 'tracked-change-id' }
  371. )
  372. this.newDocLines = ['oree fr five']
  373. this.result = this.RangesManager.applyUpdate(
  374. this.project_id,
  375. this.doc_id,
  376. this.ranges,
  377. this.updates,
  378. this.newDocLines,
  379. { historyRangesSupport: true }
  380. )
  381. })
  382. it('should split and offset deletes appropriately', function () {
  383. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  384. [
  385. {
  386. d: 'ne th',
  387. p: 1,
  388. trackedChanges: [
  389. { type: 'insert', offset: 0, length: 2 },
  390. { type: 'insert', offset: 3, length: 2 },
  391. ],
  392. },
  393. ],
  394. [
  395. {
  396. d: 'ou',
  397. p: 6,
  398. hpos: 7,
  399. trackedChanges: [{ type: 'insert', offset: 0, length: 2 }],
  400. },
  401. ],
  402. ])
  403. })
  404. })
  405. describe('comments among tracked deletes', function () {
  406. beforeEach(function () {
  407. // original text is "on[1]e[22] [333](three) fo[4444]ur five"
  408. // [] denotes tracked deletes
  409. // () denotes tracked inserts
  410. this.ranges = {
  411. changes: makeRanges([
  412. { d: '1', p: 2 },
  413. { d: '22', p: 3 },
  414. { d: '333', p: 4 },
  415. { i: 'three', p: 4 },
  416. { d: '4444', p: 12 },
  417. ]),
  418. }
  419. this.updates = makeUpdates([
  420. { c: 'three ', p: 4 },
  421. { c: 'four ', p: 10 },
  422. ])
  423. this.newDocLines = ['one three four five']
  424. this.result = this.RangesManager.applyUpdate(
  425. this.project_id,
  426. this.doc_id,
  427. this.ranges,
  428. this.updates,
  429. this.newDocLines,
  430. { historyRangesSupport: true }
  431. )
  432. })
  433. it('should offset the hpos by the length of tracked deletes before the insert', function () {
  434. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  435. [{ c: 'three ', p: 4, hpos: 10 }],
  436. [{ c: 'four ', p: 10, hpos: 16, hlen: 9 }],
  437. ])
  438. })
  439. })
  440. describe('inserts inside comments', function () {
  441. beforeEach(function () {
  442. // original text is "one three four five"
  443. this.ranges = {
  444. comments: makeRanges([
  445. { c: 'three', p: 4, t: 'comment-id-1' },
  446. { c: 'ree four', p: 6, t: 'comment-id-2' },
  447. ]),
  448. }
  449. this.updates = makeUpdates([
  450. { i: '[before]', p: 4 },
  451. { i: '[inside]', p: 13 }, // 4 + 8 + 1
  452. { i: '[overlap]', p: 23 }, // 13 + 8 + 2
  453. { i: '[after]', p: 39 }, // 23 + 9 + 7
  454. ])
  455. this.newDocLines = [
  456. 'one [before]t[inside]hr[overlap]ee four[after] five',
  457. ]
  458. this.result = this.RangesManager.applyUpdate(
  459. this.project_id,
  460. this.doc_id,
  461. this.ranges,
  462. this.updates,
  463. this.newDocLines,
  464. { historyRangesSupport: true }
  465. )
  466. })
  467. it('should add the proper commentIds properties to ops', function () {
  468. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  469. [{ i: '[before]', p: 4 }],
  470. [{ i: '[inside]', p: 13, commentIds: ['comment-id-1'] }],
  471. [
  472. {
  473. i: '[overlap]',
  474. p: 23,
  475. commentIds: ['comment-id-1', 'comment-id-2'],
  476. },
  477. ],
  478. [{ i: '[after]', p: 39 }],
  479. ])
  480. })
  481. })
  482. describe('tracked delete that overlaps the start of a comment', function () {
  483. beforeEach(function () {
  484. // original text is "one three four five"
  485. this.ranges = {
  486. comments: makeRanges([{ c: 'three', p: 4, t: 'comment-id-1' }]),
  487. }
  488. this.updates = makeUpdates([{ d: 'ne thr', p: 1 }], {
  489. tc: 'tracking-id',
  490. })
  491. this.newDocLines = ['oee four five']
  492. this.result = this.RangesManager.applyUpdate(
  493. this.project_id,
  494. this.doc_id,
  495. this.ranges,
  496. this.updates,
  497. this.newDocLines,
  498. { historyRangesSupport: true }
  499. )
  500. })
  501. it('should crop the beginning of the comment', function () {
  502. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  503. [
  504. { d: 'ne thr', p: 1 },
  505. { c: 'ee', p: 1, hpos: 7, t: 'comment-id-1' },
  506. ],
  507. ])
  508. })
  509. })
  510. describe('tracked delete that overlaps a whole comment', function () {
  511. beforeEach(function () {
  512. // original text is "one three four five"
  513. this.ranges = {
  514. comments: makeRanges([{ c: 'three', p: 4, t: 'comment-id-1' }]),
  515. }
  516. this.updates = makeUpdates([{ d: 'ne three f', p: 1 }], {
  517. tc: 'tracking-id',
  518. })
  519. this.newDocLines = ['oour five']
  520. this.result = this.RangesManager.applyUpdate(
  521. this.project_id,
  522. this.doc_id,
  523. this.ranges,
  524. this.updates,
  525. this.newDocLines,
  526. { historyRangesSupport: true }
  527. )
  528. })
  529. it('should crop the beginning of the comment', function () {
  530. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  531. [
  532. { d: 'ne three f', p: 1 },
  533. { c: '', p: 1, hpos: 11, t: 'comment-id-1' },
  534. ],
  535. ])
  536. })
  537. })
  538. describe('tracked delete that overlaps the end of a comment', function () {
  539. beforeEach(function () {
  540. // original text is "one three four five"
  541. this.ranges = {
  542. comments: makeRanges([{ c: 'three', p: 4, t: 'comment-id-1' }]),
  543. }
  544. this.updates = makeUpdates([{ d: 'ee f', p: 7 }], {
  545. tc: 'tracking-id',
  546. })
  547. this.newDocLines = ['one throur five']
  548. this.result = this.RangesManager.applyUpdate(
  549. this.project_id,
  550. this.doc_id,
  551. this.ranges,
  552. this.updates,
  553. this.newDocLines,
  554. { historyRangesSupport: true }
  555. )
  556. })
  557. it('should crop the end of the comment', function () {
  558. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  559. [
  560. { d: 'ee f', p: 7 },
  561. { c: 'thr', p: 4, t: 'comment-id-1' },
  562. ],
  563. ])
  564. })
  565. })
  566. describe('tracked delete that overlaps the inside of a comment', function () {
  567. beforeEach(function () {
  568. // original text is "one three four five"
  569. this.ranges = {
  570. comments: makeRanges([{ c: 'three', p: 4, t: 'comment-id-1' }]),
  571. }
  572. this.updates = makeUpdates([{ d: 'hre', p: 5 }], {
  573. tc: 'tracking-id',
  574. })
  575. this.newDocLines = ['one te four five']
  576. this.result = this.RangesManager.applyUpdate(
  577. this.project_id,
  578. this.doc_id,
  579. this.ranges,
  580. this.updates,
  581. this.newDocLines,
  582. { historyRangesSupport: true }
  583. )
  584. })
  585. it('should not crop the comment', function () {
  586. expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
  587. [{ d: 'hre', p: 5 }],
  588. ])
  589. })
  590. })
  591. })
  592. })
  593. describe('acceptChanges', function () {
  594. beforeEach(function () {
  595. this.RangesManager = SandboxedModule.require(MODULE_PATH, {
  596. requires: {
  597. '@overleaf/ranges-tracker': (this.RangesTracker =
  598. SandboxedModule.require('@overleaf/ranges-tracker')),
  599. '@overleaf/metrics': {},
  600. },
  601. })
  602. this.ranges = {
  603. comments: [],
  604. changes: makeRanges([
  605. { i: 'lorem', p: 0 },
  606. { i: 'ipsum', p: 10 },
  607. { i: 'dolor', p: 20 },
  608. { i: 'sit', p: 30 },
  609. { i: 'amet', p: 40 },
  610. ]),
  611. }
  612. this.removeChangeIdsSpy = sinon.spy(
  613. this.RangesTracker.prototype,
  614. 'removeChangeIds'
  615. )
  616. })
  617. describe('successfully with a single change', function () {
  618. beforeEach(function () {
  619. this.change_ids = [this.ranges.changes[1].id]
  620. this.result = this.RangesManager.acceptChanges(
  621. this.project_id,
  622. this.doc_id,
  623. this.change_ids,
  624. this.ranges
  625. )
  626. })
  627. it('should log the call with the correct number of changes', function () {
  628. this.logger.debug
  629. .calledWith('accepting 1 changes in ranges')
  630. .should.equal(true)
  631. })
  632. it('should delegate the change removal to the ranges tracker', function () {
  633. this.removeChangeIdsSpy.calledWith(this.change_ids).should.equal(true)
  634. })
  635. it('should remove the change', function () {
  636. expect(
  637. this.result.changes.find(
  638. change => change.id === this.ranges.changes[1].id
  639. )
  640. ).to.be.undefined
  641. })
  642. it('should return the original number of changes minus 1', function () {
  643. this.result.changes.length.should.equal(this.ranges.changes.length - 1)
  644. })
  645. it('should not touch other changes', function () {
  646. for (const i of [0, 2, 3, 4]) {
  647. expect(
  648. this.result.changes.find(
  649. change => change.id === this.ranges.changes[i].id
  650. )
  651. ).to.deep.equal(this.ranges.changes[i])
  652. }
  653. })
  654. })
  655. describe('successfully with multiple changes', function () {
  656. beforeEach(function () {
  657. this.change_ids = [
  658. this.ranges.changes[1].id,
  659. this.ranges.changes[3].id,
  660. this.ranges.changes[4].id,
  661. ]
  662. this.result = this.RangesManager.acceptChanges(
  663. this.project_id,
  664. this.doc_id,
  665. this.change_ids,
  666. this.ranges
  667. )
  668. })
  669. it('should log the call with the correct number of changes', function () {
  670. this.logger.debug
  671. .calledWith(`accepting ${this.change_ids.length} changes in ranges`)
  672. .should.equal(true)
  673. })
  674. it('should delegate the change removal to the ranges tracker', function () {
  675. this.removeChangeIdsSpy.calledWith(this.change_ids).should.equal(true)
  676. })
  677. it('should remove the changes', function () {
  678. for (const i of [1, 3, 4]) {
  679. expect(
  680. this.result.changes.find(
  681. change => change.id === this.ranges.changes[i].id
  682. )
  683. ).to.be.undefined
  684. }
  685. })
  686. it('should return the original number of changes minus the number of accepted changes', function () {
  687. this.result.changes.length.should.equal(this.ranges.changes.length - 3)
  688. })
  689. it('should not touch other changes', function () {
  690. for (const i of [0, 2]) {
  691. expect(
  692. this.result.changes.find(
  693. change => change.id === this.ranges.changes[i].id
  694. )
  695. ).to.deep.equal(this.ranges.changes[i])
  696. }
  697. })
  698. })
  699. })
  700. describe('getHistoryUpdatesForAcceptedChanges', function () {
  701. beforeEach(function () {
  702. this.clock = sinon.useFakeTimers()
  703. this.RangesManager = SandboxedModule.require(MODULE_PATH, {
  704. requires: {
  705. '@overleaf/ranges-tracker': (this.RangesTracker =
  706. SandboxedModule.require('@overleaf/ranges-tracker')),
  707. '@overleaf/metrics': {},
  708. },
  709. })
  710. })
  711. afterEach(function () {
  712. this.clock.restore()
  713. })
  714. it('should create history updates for accepted track inserts', function () {
  715. // 'one two three four five' <-- text before changes
  716. const ranges = {
  717. comments: [],
  718. changes: makeRanges([
  719. { i: 'lorem', p: 0 },
  720. { i: 'ipsum', p: 15 },
  721. ]),
  722. }
  723. const lines = ['loremone two thipsumree four five']
  724. const now = Date.now()
  725. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  726. docId: this.doc_id,
  727. acceptedChangeIds: ranges.changes.map(change => change.id),
  728. changes: ranges.changes,
  729. pathname: '',
  730. projectHistoryId: '',
  731. lines,
  732. })
  733. expect(result).to.deep.equal([
  734. {
  735. doc: this.doc_id,
  736. meta: {
  737. user_id: TEST_USER_ID,
  738. doc_length: 33,
  739. pathname: '',
  740. ts: now,
  741. },
  742. op: [
  743. {
  744. r: 'lorem',
  745. p: 0,
  746. tracking: { type: 'none' },
  747. },
  748. ],
  749. },
  750. {
  751. doc: this.doc_id,
  752. meta: {
  753. user_id: TEST_USER_ID,
  754. doc_length: 33,
  755. pathname: '',
  756. ts: now,
  757. },
  758. op: [
  759. {
  760. r: 'ipsum',
  761. p: 15,
  762. tracking: { type: 'none' },
  763. },
  764. ],
  765. },
  766. ])
  767. })
  768. it('should create history updates for accepted track deletes', function () {
  769. // 'one two three four five' <-- text before changes
  770. const ranges = {
  771. comments: [],
  772. changes: makeRanges([
  773. { d: 'two', p: 4 },
  774. { d: 'three', p: 5 },
  775. ]),
  776. }
  777. const lines = ['one four five']
  778. const now = Date.now()
  779. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  780. docId: this.doc_id,
  781. acceptedChangeIds: ranges.changes.map(change => change.id),
  782. changes: ranges.changes,
  783. pathname: '',
  784. projectHistoryId: '',
  785. lines,
  786. })
  787. expect(result).to.deep.equal([
  788. {
  789. doc: this.doc_id,
  790. meta: {
  791. user_id: TEST_USER_ID,
  792. doc_length: 15,
  793. history_doc_length: 23,
  794. pathname: '',
  795. ts: now,
  796. },
  797. op: [
  798. {
  799. d: 'two',
  800. p: 4,
  801. },
  802. ],
  803. },
  804. {
  805. doc: this.doc_id,
  806. meta: {
  807. user_id: TEST_USER_ID,
  808. doc_length: 15,
  809. history_doc_length: 20,
  810. pathname: '',
  811. ts: now,
  812. },
  813. op: [
  814. {
  815. d: 'three',
  816. p: 5,
  817. },
  818. ],
  819. },
  820. ])
  821. })
  822. it('should create history updates with unaccepted deletes', function () {
  823. // 'one two three four five' <-- text before changes
  824. const ranges = {
  825. comments: [],
  826. changes: makeRanges([
  827. { d: 'two', p: 4 },
  828. { d: 'three', p: 5 },
  829. ]),
  830. }
  831. const lines = ['one four five']
  832. const now = Date.now()
  833. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  834. docId: this.doc_id,
  835. acceptedChangeIds: [ranges.changes[1].id],
  836. changes: ranges.changes,
  837. pathname: '',
  838. projectHistoryId: '',
  839. lines,
  840. })
  841. expect(result).to.deep.equal([
  842. {
  843. doc: this.doc_id,
  844. meta: {
  845. user_id: TEST_USER_ID,
  846. doc_length: 15,
  847. history_doc_length: 23,
  848. pathname: '',
  849. ts: now,
  850. },
  851. op: [
  852. {
  853. d: 'three',
  854. p: 5,
  855. hpos: 8,
  856. },
  857. ],
  858. },
  859. ])
  860. })
  861. it('should create history updates with mixed track changes', function () {
  862. // 'one two three four five' <-- text before changes
  863. const ranges = {
  864. comments: [],
  865. changes: makeRanges([
  866. { d: 'two', p: 4 },
  867. { d: 'three', p: 5 },
  868. { i: 'xxx ', p: 6 },
  869. { d: 'five', p: 15 },
  870. ]),
  871. }
  872. const lines = ['one xxx four ']
  873. const now = Date.now()
  874. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  875. docId: this.doc_id,
  876. acceptedChangeIds: [
  877. ranges.changes[0].id,
  878. // ranges.changes[1].id - second delete is not accepted
  879. ranges.changes[2].id,
  880. ranges.changes[3].id,
  881. ],
  882. changes: ranges.changes,
  883. pathname: '',
  884. projectHistoryId: '',
  885. lines,
  886. })
  887. expect(result).to.deep.equal([
  888. {
  889. doc: this.doc_id,
  890. meta: {
  891. user_id: TEST_USER_ID,
  892. doc_length: 15,
  893. history_doc_length: 27,
  894. pathname: '',
  895. ts: now,
  896. },
  897. op: [
  898. {
  899. d: 'two',
  900. p: 4,
  901. },
  902. ],
  903. },
  904. {
  905. doc: this.doc_id,
  906. meta: {
  907. user_id: TEST_USER_ID,
  908. doc_length: 15,
  909. history_doc_length: 24,
  910. pathname: '',
  911. ts: now,
  912. },
  913. op: [
  914. {
  915. r: 'xxx ',
  916. p: 6,
  917. hpos: 11,
  918. tracking: { type: 'none' },
  919. },
  920. ],
  921. },
  922. {
  923. doc: this.doc_id,
  924. meta: {
  925. user_id: TEST_USER_ID,
  926. doc_length: 15,
  927. history_doc_length: 24,
  928. pathname: '',
  929. ts: now,
  930. },
  931. op: [
  932. {
  933. d: 'five',
  934. p: 15,
  935. hpos: 20,
  936. },
  937. ],
  938. },
  939. ])
  940. })
  941. })
  942. })
  943. function makeRanges(ops) {
  944. let id = 1
  945. const changes = []
  946. let ts = Date.now()
  947. for (const op of ops) {
  948. changes.push({
  949. id: id.toString(),
  950. op,
  951. metadata: { user_id: TEST_USER_ID, ts: new Date(ts).toISOString() },
  952. })
  953. id += 1
  954. ts += 1000 // use a unique timestamp for each change
  955. }
  956. return changes
  957. }
  958. function makeUpdates(ops, meta = {}) {
  959. const updates = []
  960. for (const op of ops) {
  961. updates.push({
  962. meta: { user_id: TEST_USER_ID, ...meta },
  963. op: [op],
  964. })
  965. }
  966. return updates
  967. }