RangesManagerTests.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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.change_ids,
  622. this.ranges
  623. )
  624. })
  625. it('should log the call with the correct number of changes', function () {
  626. this.logger.debug
  627. .calledWith('accepting 1 changes in ranges')
  628. .should.equal(true)
  629. })
  630. it('should delegate the change removal to the ranges tracker', function () {
  631. this.removeChangeIdsSpy.calledWith(this.change_ids).should.equal(true)
  632. })
  633. it('should remove the change', function () {
  634. expect(
  635. this.result.changes.find(
  636. change => change.id === this.ranges.changes[1].id
  637. )
  638. ).to.be.undefined
  639. })
  640. it('should return the original number of changes minus 1', function () {
  641. this.result.changes.length.should.equal(this.ranges.changes.length - 1)
  642. })
  643. it('should not touch other changes', function () {
  644. for (const i of [0, 2, 3, 4]) {
  645. expect(
  646. this.result.changes.find(
  647. change => change.id === this.ranges.changes[i].id
  648. )
  649. ).to.deep.equal(this.ranges.changes[i])
  650. }
  651. })
  652. })
  653. describe('successfully with multiple changes', function () {
  654. beforeEach(function () {
  655. this.change_ids = [
  656. this.ranges.changes[1].id,
  657. this.ranges.changes[3].id,
  658. this.ranges.changes[4].id,
  659. ]
  660. this.result = this.RangesManager.acceptChanges(
  661. this.change_ids,
  662. this.ranges
  663. )
  664. })
  665. it('should log the call with the correct number of changes', function () {
  666. this.logger.debug
  667. .calledWith(`accepting ${this.change_ids.length} 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 changes', function () {
  674. for (const i of [1, 3, 4]) {
  675. expect(
  676. this.result.changes.find(
  677. change => change.id === this.ranges.changes[i].id
  678. )
  679. ).to.be.undefined
  680. }
  681. })
  682. it('should return the original number of changes minus the number of accepted changes', function () {
  683. this.result.changes.length.should.equal(this.ranges.changes.length - 3)
  684. })
  685. it('should not touch other changes', function () {
  686. for (const i of [0, 2]) {
  687. expect(
  688. this.result.changes.find(
  689. change => change.id === this.ranges.changes[i].id
  690. )
  691. ).to.deep.equal(this.ranges.changes[i])
  692. }
  693. })
  694. })
  695. })
  696. describe('getHistoryUpdatesForAcceptedChanges', function () {
  697. beforeEach(function () {
  698. this.clock = sinon.useFakeTimers()
  699. this.RangesManager = SandboxedModule.require(MODULE_PATH, {
  700. requires: {
  701. '@overleaf/ranges-tracker': (this.RangesTracker =
  702. SandboxedModule.require('@overleaf/ranges-tracker')),
  703. '@overleaf/metrics': {},
  704. },
  705. })
  706. })
  707. afterEach(function () {
  708. this.clock.restore()
  709. })
  710. it('should create history updates for accepted track inserts', function () {
  711. // 'one two three four five' <-- text before changes
  712. const ranges = {
  713. comments: [],
  714. changes: makeRanges([
  715. { i: 'lorem', p: 0 },
  716. { i: 'ipsum', p: 15 },
  717. ]),
  718. }
  719. const lines = ['loremone two thipsumree four five']
  720. const now = Date.now()
  721. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  722. docId: this.doc_id,
  723. acceptedChangeIds: ranges.changes.map(change => change.id),
  724. changes: ranges.changes,
  725. pathname: '',
  726. projectHistoryId: '',
  727. lines,
  728. })
  729. expect(result).to.deep.equal([
  730. {
  731. doc: this.doc_id,
  732. meta: {
  733. user_id: TEST_USER_ID,
  734. doc_length: 33,
  735. pathname: '',
  736. ts: now,
  737. },
  738. op: [
  739. {
  740. r: 'lorem',
  741. p: 0,
  742. tracking: { type: 'none' },
  743. },
  744. ],
  745. },
  746. {
  747. doc: this.doc_id,
  748. meta: {
  749. user_id: TEST_USER_ID,
  750. doc_length: 33,
  751. pathname: '',
  752. ts: now,
  753. },
  754. op: [
  755. {
  756. r: 'ipsum',
  757. p: 15,
  758. tracking: { type: 'none' },
  759. },
  760. ],
  761. },
  762. ])
  763. })
  764. it('should create history updates for accepted track deletes', function () {
  765. // 'one two three four five' <-- text before changes
  766. const ranges = {
  767. comments: [],
  768. changes: makeRanges([
  769. { d: 'two', p: 4 },
  770. { d: 'three', p: 5 },
  771. ]),
  772. }
  773. const lines = ['one four five']
  774. const now = Date.now()
  775. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  776. docId: this.doc_id,
  777. acceptedChangeIds: ranges.changes.map(change => change.id),
  778. changes: ranges.changes,
  779. pathname: '',
  780. projectHistoryId: '',
  781. lines,
  782. })
  783. expect(result).to.deep.equal([
  784. {
  785. doc: this.doc_id,
  786. meta: {
  787. user_id: TEST_USER_ID,
  788. doc_length: 15,
  789. history_doc_length: 23,
  790. pathname: '',
  791. ts: now,
  792. },
  793. op: [
  794. {
  795. d: 'two',
  796. p: 4,
  797. },
  798. ],
  799. },
  800. {
  801. doc: this.doc_id,
  802. meta: {
  803. user_id: TEST_USER_ID,
  804. doc_length: 15,
  805. history_doc_length: 20,
  806. pathname: '',
  807. ts: now,
  808. },
  809. op: [
  810. {
  811. d: 'three',
  812. p: 5,
  813. },
  814. ],
  815. },
  816. ])
  817. })
  818. it('should create history updates with unaccepted deletes', function () {
  819. // 'one two three four five' <-- text before changes
  820. const ranges = {
  821. comments: [],
  822. changes: makeRanges([
  823. { d: 'two', p: 4 },
  824. { d: 'three', p: 5 },
  825. ]),
  826. }
  827. const lines = ['one four five']
  828. const now = Date.now()
  829. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  830. docId: this.doc_id,
  831. acceptedChangeIds: [ranges.changes[1].id],
  832. changes: ranges.changes,
  833. pathname: '',
  834. projectHistoryId: '',
  835. lines,
  836. })
  837. expect(result).to.deep.equal([
  838. {
  839. doc: this.doc_id,
  840. meta: {
  841. user_id: TEST_USER_ID,
  842. doc_length: 15,
  843. history_doc_length: 23,
  844. pathname: '',
  845. ts: now,
  846. },
  847. op: [
  848. {
  849. d: 'three',
  850. p: 5,
  851. hpos: 8,
  852. },
  853. ],
  854. },
  855. ])
  856. })
  857. it('should create history updates with mixed track changes', function () {
  858. // 'one two three four five' <-- text before changes
  859. const ranges = {
  860. comments: [],
  861. changes: makeRanges([
  862. { d: 'two', p: 4 },
  863. { d: 'three', p: 5 },
  864. { i: 'xxx ', p: 6 },
  865. { d: 'five', p: 15 },
  866. ]),
  867. }
  868. const lines = ['one xxx four ']
  869. const now = Date.now()
  870. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  871. docId: this.doc_id,
  872. acceptedChangeIds: [
  873. ranges.changes[0].id,
  874. // ranges.changes[1].id - second delete is not accepted
  875. ranges.changes[2].id,
  876. ranges.changes[3].id,
  877. ],
  878. changes: ranges.changes,
  879. pathname: '',
  880. projectHistoryId: '',
  881. lines,
  882. })
  883. expect(result).to.deep.equal([
  884. {
  885. doc: this.doc_id,
  886. meta: {
  887. user_id: TEST_USER_ID,
  888. doc_length: 15,
  889. history_doc_length: 27,
  890. pathname: '',
  891. ts: now,
  892. },
  893. op: [
  894. {
  895. d: 'two',
  896. p: 4,
  897. },
  898. ],
  899. },
  900. {
  901. doc: this.doc_id,
  902. meta: {
  903. user_id: TEST_USER_ID,
  904. doc_length: 15,
  905. history_doc_length: 24,
  906. pathname: '',
  907. ts: now,
  908. },
  909. op: [
  910. {
  911. r: 'xxx ',
  912. p: 6,
  913. hpos: 11,
  914. tracking: { type: 'none' },
  915. },
  916. ],
  917. },
  918. {
  919. doc: this.doc_id,
  920. meta: {
  921. user_id: TEST_USER_ID,
  922. doc_length: 15,
  923. history_doc_length: 24,
  924. pathname: '',
  925. ts: now,
  926. },
  927. op: [
  928. {
  929. d: 'five',
  930. p: 15,
  931. hpos: 20,
  932. },
  933. ],
  934. },
  935. ])
  936. })
  937. })
  938. })
  939. function makeRanges(ops) {
  940. let id = 1
  941. const changes = []
  942. let ts = Date.now()
  943. for (const op of ops) {
  944. changes.push({
  945. id: id.toString(),
  946. op,
  947. metadata: { user_id: TEST_USER_ID, ts: new Date(ts).toISOString() },
  948. })
  949. id += 1
  950. ts += 1000 // use a unique timestamp for each change
  951. }
  952. return changes
  953. }
  954. function makeUpdates(ops, meta = {}) {
  955. const updates = []
  956. for (const op of ops) {
  957. updates.push({
  958. meta: { user_id: TEST_USER_ID, ...meta },
  959. op: [op],
  960. })
  961. }
  962. return updates
  963. }