RangesManagerTests.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. })
  483. })
  484. describe('acceptChanges', function () {
  485. beforeEach(function () {
  486. this.RangesManager = SandboxedModule.require(MODULE_PATH, {
  487. requires: {
  488. '@overleaf/ranges-tracker': (this.RangesTracker =
  489. SandboxedModule.require('@overleaf/ranges-tracker')),
  490. '@overleaf/metrics': {},
  491. },
  492. })
  493. this.ranges = {
  494. comments: [],
  495. changes: makeRanges([
  496. { i: 'lorem', p: 0 },
  497. { i: 'ipsum', p: 10 },
  498. { i: 'dolor', p: 20 },
  499. { i: 'sit', p: 30 },
  500. { i: 'amet', p: 40 },
  501. ]),
  502. }
  503. this.removeChangeIdsSpy = sinon.spy(
  504. this.RangesTracker.prototype,
  505. 'removeChangeIds'
  506. )
  507. })
  508. describe('successfully with a single change', function () {
  509. beforeEach(function () {
  510. this.change_ids = [this.ranges.changes[1].id]
  511. this.result = this.RangesManager.acceptChanges(
  512. this.change_ids,
  513. this.ranges
  514. )
  515. })
  516. it('should log the call with the correct number of changes', function () {
  517. this.logger.debug
  518. .calledWith('accepting 1 changes in ranges')
  519. .should.equal(true)
  520. })
  521. it('should delegate the change removal to the ranges tracker', function () {
  522. this.removeChangeIdsSpy.calledWith(this.change_ids).should.equal(true)
  523. })
  524. it('should remove the change', function () {
  525. expect(
  526. this.result.changes.find(
  527. change => change.id === this.ranges.changes[1].id
  528. )
  529. ).to.be.undefined
  530. })
  531. it('should return the original number of changes minus 1', function () {
  532. this.result.changes.length.should.equal(this.ranges.changes.length - 1)
  533. })
  534. it('should not touch other changes', function () {
  535. for (const i of [0, 2, 3, 4]) {
  536. expect(
  537. this.result.changes.find(
  538. change => change.id === this.ranges.changes[i].id
  539. )
  540. ).to.deep.equal(this.ranges.changes[i])
  541. }
  542. })
  543. })
  544. describe('successfully with multiple changes', function () {
  545. beforeEach(function () {
  546. this.change_ids = [
  547. this.ranges.changes[1].id,
  548. this.ranges.changes[3].id,
  549. this.ranges.changes[4].id,
  550. ]
  551. this.result = this.RangesManager.acceptChanges(
  552. this.change_ids,
  553. this.ranges
  554. )
  555. })
  556. it('should log the call with the correct number of changes', function () {
  557. this.logger.debug
  558. .calledWith(`accepting ${this.change_ids.length} changes in ranges`)
  559. .should.equal(true)
  560. })
  561. it('should delegate the change removal to the ranges tracker', function () {
  562. this.removeChangeIdsSpy.calledWith(this.change_ids).should.equal(true)
  563. })
  564. it('should remove the changes', function () {
  565. for (const i of [1, 3, 4]) {
  566. expect(
  567. this.result.changes.find(
  568. change => change.id === this.ranges.changes[i].id
  569. )
  570. ).to.be.undefined
  571. }
  572. })
  573. it('should return the original number of changes minus the number of accepted changes', function () {
  574. this.result.changes.length.should.equal(this.ranges.changes.length - 3)
  575. })
  576. it('should not touch other changes', function () {
  577. for (const i of [0, 2]) {
  578. expect(
  579. this.result.changes.find(
  580. change => change.id === this.ranges.changes[i].id
  581. )
  582. ).to.deep.equal(this.ranges.changes[i])
  583. }
  584. })
  585. })
  586. })
  587. describe('getHistoryUpdatesForAcceptedChanges', function () {
  588. beforeEach(function () {
  589. this.RangesManager = SandboxedModule.require(MODULE_PATH, {
  590. requires: {
  591. '@overleaf/ranges-tracker': (this.RangesTracker =
  592. SandboxedModule.require('@overleaf/ranges-tracker')),
  593. '@overleaf/metrics': {},
  594. },
  595. })
  596. })
  597. it('should create history updates for accepted track inserts', function () {
  598. // 'one two three four five' <-- text before changes
  599. const ranges = {
  600. comments: [],
  601. changes: makeRanges([
  602. { i: 'lorem', p: 0 },
  603. { i: 'ipsum', p: 15 },
  604. ]),
  605. }
  606. const lines = ['loremone two thipsumree four five']
  607. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  608. docId: this.doc_id,
  609. acceptedChangeIds: ranges.changes.map(change => change.id),
  610. changes: ranges.changes,
  611. pathname: '',
  612. projectHistoryId: '',
  613. lines,
  614. })
  615. expect(result).to.deep.equal([
  616. {
  617. doc: this.doc_id,
  618. meta: {
  619. user_id: TEST_USER_ID,
  620. doc_length: 33,
  621. pathname: '',
  622. ts: ranges.changes[0].metadata.ts,
  623. },
  624. op: [
  625. {
  626. r: 'lorem',
  627. p: 0,
  628. tracking: { type: 'none' },
  629. },
  630. ],
  631. },
  632. {
  633. doc: this.doc_id,
  634. meta: {
  635. user_id: TEST_USER_ID,
  636. doc_length: 33,
  637. pathname: '',
  638. ts: ranges.changes[1].metadata.ts,
  639. },
  640. op: [
  641. {
  642. r: 'ipsum',
  643. p: 15,
  644. tracking: { type: 'none' },
  645. },
  646. ],
  647. },
  648. ])
  649. })
  650. it('should create history updates for accepted track deletes', function () {
  651. // 'one two three four five' <-- text before changes
  652. const ranges = {
  653. comments: [],
  654. changes: makeRanges([
  655. { d: 'two', p: 4 },
  656. { d: 'three', p: 5 },
  657. ]),
  658. }
  659. const lines = ['one four five']
  660. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  661. docId: this.doc_id,
  662. acceptedChangeIds: ranges.changes.map(change => change.id),
  663. changes: ranges.changes,
  664. pathname: '',
  665. projectHistoryId: '',
  666. lines,
  667. })
  668. expect(result).to.deep.equal([
  669. {
  670. doc: this.doc_id,
  671. meta: {
  672. user_id: TEST_USER_ID,
  673. doc_length: 15,
  674. history_doc_length: 23,
  675. pathname: '',
  676. ts: ranges.changes[0].metadata.ts,
  677. },
  678. op: [
  679. {
  680. d: 'two',
  681. p: 4,
  682. },
  683. ],
  684. },
  685. {
  686. doc: this.doc_id,
  687. meta: {
  688. user_id: TEST_USER_ID,
  689. doc_length: 15,
  690. history_doc_length: 20,
  691. pathname: '',
  692. ts: ranges.changes[1].metadata.ts,
  693. },
  694. op: [
  695. {
  696. d: 'three',
  697. p: 5,
  698. },
  699. ],
  700. },
  701. ])
  702. })
  703. it('should create history updates with unaccepted deletes', function () {
  704. // 'one two three four five' <-- text before changes
  705. const ranges = {
  706. comments: [],
  707. changes: makeRanges([
  708. { d: 'two', p: 4 },
  709. { d: 'three', p: 5 },
  710. ]),
  711. }
  712. const lines = ['one four five']
  713. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  714. docId: this.doc_id,
  715. acceptedChangeIds: [ranges.changes[1].id],
  716. changes: ranges.changes,
  717. pathname: '',
  718. projectHistoryId: '',
  719. lines,
  720. })
  721. expect(result).to.deep.equal([
  722. {
  723. doc: this.doc_id,
  724. meta: {
  725. user_id: TEST_USER_ID,
  726. doc_length: 15,
  727. history_doc_length: 23,
  728. pathname: '',
  729. ts: ranges.changes[1].metadata.ts,
  730. },
  731. op: [
  732. {
  733. d: 'three',
  734. p: 5,
  735. hpos: 8,
  736. },
  737. ],
  738. },
  739. ])
  740. })
  741. it('should create history updates with mixed track changes', function () {
  742. // 'one two three four five' <-- text before changes
  743. const ranges = {
  744. comments: [],
  745. changes: makeRanges([
  746. { d: 'two', p: 4 },
  747. { d: 'three', p: 5 },
  748. { i: 'xxx ', p: 6 },
  749. { d: 'five', p: 15 },
  750. ]),
  751. }
  752. const lines = ['one xxx four ']
  753. const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
  754. docId: this.doc_id,
  755. acceptedChangeIds: [
  756. ranges.changes[0].id,
  757. // ranges.changes[1].id - second delete is not accepted
  758. ranges.changes[2].id,
  759. ranges.changes[3].id,
  760. ],
  761. changes: ranges.changes,
  762. pathname: '',
  763. projectHistoryId: '',
  764. lines,
  765. })
  766. expect(result).to.deep.equal([
  767. {
  768. doc: this.doc_id,
  769. meta: {
  770. user_id: TEST_USER_ID,
  771. doc_length: 15,
  772. history_doc_length: 27,
  773. pathname: '',
  774. ts: ranges.changes[0].metadata.ts,
  775. },
  776. op: [
  777. {
  778. d: 'two',
  779. p: 4,
  780. },
  781. ],
  782. },
  783. {
  784. doc: this.doc_id,
  785. meta: {
  786. user_id: TEST_USER_ID,
  787. doc_length: 15,
  788. history_doc_length: 24,
  789. pathname: '',
  790. ts: ranges.changes[1].metadata.ts,
  791. },
  792. op: [
  793. {
  794. r: 'xxx ',
  795. p: 6,
  796. hpos: 11,
  797. tracking: { type: 'none' },
  798. },
  799. ],
  800. },
  801. {
  802. doc: this.doc_id,
  803. meta: {
  804. user_id: TEST_USER_ID,
  805. doc_length: 15,
  806. history_doc_length: 24,
  807. pathname: '',
  808. ts: ranges.changes[2].metadata.ts,
  809. },
  810. op: [
  811. {
  812. d: 'five',
  813. p: 15,
  814. hpos: 20,
  815. },
  816. ],
  817. },
  818. ])
  819. })
  820. })
  821. })
  822. function makeRanges(ops) {
  823. let id = 1
  824. const changes = []
  825. for (const op of ops) {
  826. changes.push({
  827. id: id.toString(),
  828. op,
  829. metadata: { user_id: TEST_USER_ID, ts: new Date() },
  830. })
  831. id += 1
  832. }
  833. return changes
  834. }
  835. function makeUpdates(ops, meta = {}) {
  836. const updates = []
  837. for (const op of ops) {
  838. updates.push({
  839. meta: { user_id: TEST_USER_ID, ...meta },
  840. op: [op],
  841. })
  842. }
  843. return updates
  844. }