RangesManagerTests.js 32 KB

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