RangesManagerTests.js 31 KB

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