RangesTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const { setTimeout } = require('node:timers/promises')
  4. const { db, ObjectId } = require('../../../app/js/mongodb')
  5. const MockWebApi = require('./helpers/MockWebApi')
  6. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  7. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  8. const RangesManager = require('../../../app/js/RangesManager')
  9. const sandbox = sinon.createSandbox()
  10. describe('Ranges', function () {
  11. before(async function () {
  12. await DocUpdaterApp.ensureRunning()
  13. })
  14. describe('tracking changes from ops', function () {
  15. before(async function () {
  16. this.project_id = DocUpdaterClient.randomId()
  17. this.user_id = DocUpdaterClient.randomId()
  18. this.id_seed = '587357bd35e64f6157'
  19. this.doc = {
  20. id: DocUpdaterClient.randomId(),
  21. lines: ['aaa'],
  22. }
  23. this.updates = [
  24. {
  25. doc: this.doc.id,
  26. op: [{ i: '123', p: 1 }],
  27. v: 0,
  28. meta: { user_id: this.user_id },
  29. },
  30. {
  31. doc: this.doc.id,
  32. op: [{ i: '456', p: 5 }],
  33. v: 1,
  34. meta: { user_id: this.user_id, tc: this.id_seed },
  35. },
  36. {
  37. doc: this.doc.id,
  38. op: [{ d: '12', p: 1 }],
  39. v: 2,
  40. meta: { user_id: this.user_id },
  41. },
  42. ]
  43. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  44. lines: this.doc.lines,
  45. version: 0,
  46. })
  47. await DocUpdaterClient.preloadDoc(this.project_id, this.doc.id)
  48. for (const update of this.updates) {
  49. await DocUpdaterClient.sendUpdate(this.project_id, this.doc.id, update)
  50. }
  51. })
  52. it('should update the ranges', async function () {
  53. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc.id)
  54. const { ranges } = doc
  55. const change = ranges.changes[0]
  56. change.op.should.deep.equal({ i: '456', p: 3 })
  57. change.id.should.equal(this.id_seed + '000001')
  58. change.metadata.user_id.should.equal(this.user_id)
  59. })
  60. describe('Adding comments', function () {
  61. describe('standalone', function () {
  62. before(async function () {
  63. this.project_id = DocUpdaterClient.randomId()
  64. this.user_id = DocUpdaterClient.randomId()
  65. this.tid = DocUpdaterClient.randomId()
  66. this.doc = {
  67. id: DocUpdaterClient.randomId(),
  68. lines: ['foo bar baz'],
  69. }
  70. this.updates = [
  71. {
  72. doc: this.doc.id,
  73. op: [{ c: 'bar', p: 4, t: this.tid }],
  74. v: 0,
  75. },
  76. ]
  77. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  78. lines: this.doc.lines,
  79. version: 0,
  80. })
  81. await DocUpdaterClient.preloadDoc(this.project_id, this.doc.id)
  82. for (const update of this.updates) {
  83. await DocUpdaterClient.sendUpdate(
  84. this.project_id,
  85. this.doc.id,
  86. update
  87. )
  88. }
  89. await setTimeout(200)
  90. })
  91. it('should update the ranges', async function () {
  92. const doc = await DocUpdaterClient.getDoc(
  93. this.project_id,
  94. this.doc.id
  95. )
  96. const { ranges } = doc
  97. const comment = ranges.comments[0]
  98. comment.op.should.deep.equal({ c: 'bar', p: 4, t: this.tid })
  99. comment.id.should.equal(this.tid)
  100. })
  101. })
  102. describe('with conflicting ops needing OT', function () {
  103. before(async function () {
  104. this.project_id = DocUpdaterClient.randomId()
  105. this.user_id = DocUpdaterClient.randomId()
  106. this.tid = DocUpdaterClient.randomId()
  107. this.doc = {
  108. id: DocUpdaterClient.randomId(),
  109. lines: ['foo bar baz'],
  110. }
  111. this.updates = [
  112. {
  113. doc: this.doc.id,
  114. op: [{ i: 'ABC', p: 3 }],
  115. v: 0,
  116. meta: { user_id: this.user_id },
  117. },
  118. {
  119. doc: this.doc.id,
  120. op: [{ c: 'bar', p: 4, t: this.tid }],
  121. v: 0,
  122. },
  123. ]
  124. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  125. lines: this.doc.lines,
  126. version: 0,
  127. })
  128. await DocUpdaterClient.preloadDoc(this.project_id, this.doc.id)
  129. for (const update of this.updates) {
  130. await DocUpdaterClient.sendUpdate(
  131. this.project_id,
  132. this.doc.id,
  133. update
  134. )
  135. }
  136. await setTimeout(200)
  137. })
  138. it('should update the comments with the OT shifted comment', async function () {
  139. const doc = await DocUpdaterClient.getDoc(
  140. this.project_id,
  141. this.doc.id
  142. )
  143. const { ranges } = doc
  144. const comment = ranges.comments[0]
  145. comment.op.should.deep.equal({ c: 'bar', p: 7, t: this.tid })
  146. })
  147. })
  148. })
  149. })
  150. describe('Loading ranges from persistence layer', function () {
  151. before(async function () {
  152. this.project_id = DocUpdaterClient.randomId()
  153. this.user_id = DocUpdaterClient.randomId()
  154. this.id_seed = '587357bd35e64f6157'
  155. this.doc = {
  156. id: DocUpdaterClient.randomId(),
  157. lines: ['a123aa'],
  158. }
  159. this.update = {
  160. doc: this.doc.id,
  161. op: [{ i: '456', p: 5 }],
  162. v: 0,
  163. meta: { user_id: this.user_id, tc: this.id_seed },
  164. }
  165. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  166. lines: this.doc.lines,
  167. version: 0,
  168. ranges: {
  169. changes: [
  170. {
  171. op: { i: '123', p: 1 },
  172. metadata: {
  173. user_id: this.user_id,
  174. ts: new Date(),
  175. },
  176. },
  177. ],
  178. },
  179. })
  180. await DocUpdaterClient.preloadDoc(this.project_id, this.doc.id)
  181. await DocUpdaterClient.sendUpdate(
  182. this.project_id,
  183. this.doc.id,
  184. this.update
  185. )
  186. await setTimeout(200)
  187. })
  188. it('should have preloaded the existing ranges', async function () {
  189. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc.id)
  190. const { changes } = doc.ranges
  191. changes[0].op.should.deep.equal({ i: '123', p: 1 })
  192. changes[1].op.should.deep.equal({ i: '456', p: 5 })
  193. })
  194. it('should flush the ranges to the persistence layer again', async function () {
  195. await DocUpdaterClient.flushDoc(this.project_id, this.doc.id)
  196. const doc = await MockWebApi.getDocument(this.project_id, this.doc.id)
  197. const { changes } = doc.ranges
  198. changes[0].op.should.deep.equal({ i: '123', p: 1 })
  199. changes[1].op.should.deep.equal({ i: '456', p: 5 })
  200. })
  201. })
  202. describe('accepting a change', function () {
  203. beforeEach(async function () {
  204. sandbox.spy(MockWebApi, 'setDocument')
  205. this.project_id = DocUpdaterClient.randomId()
  206. this.user_id = DocUpdaterClient.randomId()
  207. this.id_seed = '587357bd35e64f6157'
  208. this.doc = {
  209. id: DocUpdaterClient.randomId(),
  210. lines: ['aaa'],
  211. }
  212. this.update = {
  213. doc: this.doc.id,
  214. op: [{ i: '456', p: 1 }],
  215. v: 0,
  216. meta: { user_id: this.user_id, tc: this.id_seed },
  217. }
  218. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  219. lines: this.doc.lines,
  220. version: 0,
  221. })
  222. await DocUpdaterClient.preloadDoc(this.project_id, this.doc.id)
  223. await DocUpdaterClient.sendUpdate(
  224. this.project_id,
  225. this.doc.id,
  226. this.update
  227. )
  228. await setTimeout(200)
  229. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc.id)
  230. const { ranges } = doc
  231. const change = ranges.changes[0]
  232. change.op.should.deep.equal({ i: '456', p: 1 })
  233. change.id.should.equal(this.id_seed + '000001')
  234. change.metadata.user_id.should.equal(this.user_id)
  235. })
  236. afterEach(function () {
  237. sandbox.restore()
  238. })
  239. it('should remove the change after accepting', async function () {
  240. await DocUpdaterClient.acceptChange(
  241. this.project_id,
  242. this.doc.id,
  243. this.id_seed + '000001'
  244. )
  245. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc.id)
  246. expect(doc.ranges.changes).to.be.undefined
  247. })
  248. it('should persist the ranges after accepting', async function () {
  249. await DocUpdaterClient.flushDoc(this.project_id, this.doc.id)
  250. await DocUpdaterClient.acceptChange(
  251. this.project_id,
  252. this.doc.id,
  253. this.id_seed + '000001'
  254. )
  255. await DocUpdaterClient.flushDoc(this.project_id, this.doc.id)
  256. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc.id)
  257. expect(doc.ranges.changes).to.be.undefined
  258. MockWebApi.setDocument
  259. .calledWith(this.project_id, this.doc.id, ['a456aa'], 1, {})
  260. .should.equal(true)
  261. })
  262. })
  263. describe('accepting multiple changes', function () {
  264. beforeEach(async function () {
  265. this.getHistoryUpdatesSpy = sandbox.spy(
  266. RangesManager,
  267. 'getHistoryUpdatesForAcceptedChanges'
  268. )
  269. this.project_id = DocUpdaterClient.randomId()
  270. this.user_id = DocUpdaterClient.randomId()
  271. this.doc = {
  272. id: DocUpdaterClient.randomId(),
  273. lines: ['aaa', 'bbb', 'ccc', 'ddd', 'eee'],
  274. }
  275. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  276. lines: this.doc.lines,
  277. version: 0,
  278. historyRangesSupport: true,
  279. })
  280. await DocUpdaterClient.preloadDoc(this.project_id, this.doc.id)
  281. this.id_seed_1 = 'tc_1'
  282. this.id_seed_2 = 'tc_2'
  283. this.id_seed_3 = 'tc_3'
  284. this.updates = [
  285. {
  286. doc: this.doc.id,
  287. op: [{ d: 'bbb', p: 4 }],
  288. v: 0,
  289. meta: {
  290. user_id: this.user_id,
  291. tc: this.id_seed_1,
  292. },
  293. },
  294. {
  295. doc: this.doc.id,
  296. op: [{ d: 'ccc', p: 5 }],
  297. v: 1,
  298. meta: {
  299. user_id: this.user_id,
  300. tc: this.id_seed_2,
  301. },
  302. },
  303. {
  304. doc: this.doc.id,
  305. op: [{ d: 'ddd', p: 6 }],
  306. v: 2,
  307. meta: {
  308. user_id: this.user_id,
  309. tc: this.id_seed_3,
  310. },
  311. },
  312. ]
  313. await DocUpdaterClient.sendUpdates(
  314. this.project_id,
  315. this.doc.id,
  316. this.updates
  317. )
  318. await setTimeout(200)
  319. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc.id)
  320. const { ranges } = doc
  321. const changeOps = ranges.changes.map(change => change.op).flat()
  322. changeOps.should.deep.equal([
  323. { d: 'bbb', p: 4 },
  324. { d: 'ccc', p: 5 },
  325. { d: 'ddd', p: 6 },
  326. ])
  327. })
  328. afterEach(function () {
  329. sandbox.restore()
  330. })
  331. it('accepting changes in order', async function () {
  332. await DocUpdaterClient.acceptChanges(this.project_id, this.doc.id, [
  333. this.id_seed_1 + '000001',
  334. this.id_seed_2 + '000001',
  335. this.id_seed_3 + '000001',
  336. ])
  337. const historyUpdates = this.getHistoryUpdatesSpy.returnValues[0]
  338. expect(historyUpdates[0]).to.deep.equal({
  339. doc: this.doc.id,
  340. meta: {
  341. pathname: '/a/b/c.tex',
  342. doc_length: 10,
  343. history_doc_length: 19,
  344. ts: historyUpdates[0].meta.ts,
  345. user_id: this.user_id,
  346. },
  347. op: [{ p: 4, d: 'bbb' }],
  348. })
  349. expect(historyUpdates[1]).to.deep.equal({
  350. doc: this.doc.id,
  351. meta: {
  352. pathname: '/a/b/c.tex',
  353. doc_length: 10,
  354. history_doc_length: 16,
  355. ts: historyUpdates[1].meta.ts,
  356. user_id: this.user_id,
  357. },
  358. op: [{ p: 5, d: 'ccc' }],
  359. })
  360. expect(historyUpdates[2]).to.deep.equal({
  361. doc: this.doc.id,
  362. meta: {
  363. pathname: '/a/b/c.tex',
  364. doc_length: 10,
  365. history_doc_length: 13,
  366. ts: historyUpdates[2].meta.ts,
  367. user_id: this.user_id,
  368. },
  369. op: [{ p: 6, d: 'ddd' }],
  370. })
  371. })
  372. it('accepting changes in reverse order', async function () {
  373. await DocUpdaterClient.acceptChanges(this.project_id, this.doc.id, [
  374. this.id_seed_3 + '000001',
  375. this.id_seed_2 + '000001',
  376. this.id_seed_1 + '000001',
  377. ])
  378. const historyUpdates = this.getHistoryUpdatesSpy.returnValues[0]
  379. expect(historyUpdates[0]).to.deep.equal({
  380. doc: this.doc.id,
  381. meta: {
  382. pathname: '/a/b/c.tex',
  383. doc_length: 10,
  384. history_doc_length: 19,
  385. ts: historyUpdates[0].meta.ts,
  386. user_id: this.user_id,
  387. },
  388. op: [{ p: 4, d: 'bbb' }],
  389. })
  390. expect(historyUpdates[1]).to.deep.equal({
  391. doc: this.doc.id,
  392. meta: {
  393. pathname: '/a/b/c.tex',
  394. doc_length: 10,
  395. history_doc_length: 16,
  396. ts: historyUpdates[1].meta.ts,
  397. user_id: this.user_id,
  398. },
  399. op: [{ p: 5, d: 'ccc' }],
  400. })
  401. expect(historyUpdates[2]).to.deep.equal({
  402. doc: this.doc.id,
  403. meta: {
  404. pathname: '/a/b/c.tex',
  405. doc_length: 10,
  406. history_doc_length: 13,
  407. ts: historyUpdates[2].meta.ts,
  408. user_id: this.user_id,
  409. },
  410. op: [{ p: 6, d: 'ddd' }],
  411. })
  412. })
  413. })
  414. describe('deleting a comment range', function () {
  415. before(async function () {
  416. this.project_id = DocUpdaterClient.randomId()
  417. this.user_id = DocUpdaterClient.randomId()
  418. this.doc = {
  419. id: DocUpdaterClient.randomId(),
  420. lines: ['foo bar'],
  421. }
  422. this.update = {
  423. doc: this.doc.id,
  424. op: [{ c: 'bar', p: 4, t: (this.tid = DocUpdaterClient.randomId()) }],
  425. v: 0,
  426. }
  427. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  428. lines: this.doc.lines,
  429. version: 0,
  430. })
  431. await DocUpdaterClient.preloadDoc(this.project_id, this.doc.id)
  432. await DocUpdaterClient.sendUpdate(
  433. this.project_id,
  434. this.doc.id,
  435. this.update
  436. )
  437. await setTimeout(200)
  438. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc.id)
  439. const { ranges } = doc
  440. const change = ranges.comments[0]
  441. change.op.should.deep.equal({ c: 'bar', p: 4, t: this.tid })
  442. change.id.should.equal(this.tid)
  443. })
  444. it('should remove the comment range', async function () {
  445. await DocUpdaterClient.removeComment(
  446. this.project_id,
  447. this.doc.id,
  448. this.tid
  449. )
  450. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc.id)
  451. expect(doc.ranges.comments).to.be.undefined
  452. })
  453. })
  454. describe('tripping range size limit', function () {
  455. before(async function () {
  456. this.project_id = DocUpdaterClient.randomId()
  457. this.user_id = DocUpdaterClient.randomId()
  458. this.id_seed = DocUpdaterClient.randomId()
  459. this.doc = {
  460. id: DocUpdaterClient.randomId(),
  461. lines: ['aaa'],
  462. }
  463. this.i = new Array(3 * 1024 * 1024).join('a')
  464. this.updates = [
  465. {
  466. doc: this.doc.id,
  467. op: [{ i: this.i, p: 1 }],
  468. v: 0,
  469. meta: { user_id: this.user_id, tc: this.id_seed },
  470. },
  471. ]
  472. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  473. lines: this.doc.lines,
  474. version: 0,
  475. })
  476. await DocUpdaterClient.preloadDoc(this.project_id, this.doc.id)
  477. for (const update of this.updates) {
  478. await DocUpdaterClient.sendUpdate(this.project_id, this.doc.id, update)
  479. }
  480. await setTimeout(200)
  481. })
  482. it('should not update the ranges', async function () {
  483. const doc = await DocUpdaterClient.getDoc(this.project_id, this.doc.id)
  484. expect(doc.ranges.changes).to.be.undefined
  485. })
  486. })
  487. describe('deleting text surrounding a comment', function () {
  488. before(async function () {
  489. this.project_id = DocUpdaterClient.randomId()
  490. this.user_id = DocUpdaterClient.randomId()
  491. this.doc_id = DocUpdaterClient.randomId()
  492. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  493. lines: ['foo bar baz'],
  494. version: 0,
  495. ranges: {
  496. comments: [
  497. {
  498. op: {
  499. c: 'a',
  500. p: 5,
  501. tid: (this.tid = DocUpdaterClient.randomId()),
  502. },
  503. metadata: {
  504. user_id: this.user_id,
  505. ts: new Date(),
  506. },
  507. },
  508. ],
  509. },
  510. })
  511. this.updates = [
  512. {
  513. doc: this.doc_id,
  514. op: [{ d: 'foo ', p: 0 }],
  515. v: 0,
  516. meta: { user_id: this.user_id },
  517. },
  518. {
  519. doc: this.doc_id,
  520. op: [{ d: 'bar ', p: 0 }],
  521. v: 1,
  522. meta: { user_id: this.user_id },
  523. },
  524. ]
  525. await DocUpdaterClient.preloadDoc(this.project_id, this.doc_id)
  526. for (const update of this.updates) {
  527. await DocUpdaterClient.sendUpdate(this.project_id, this.doc_id, update)
  528. }
  529. await setTimeout(200)
  530. await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  531. })
  532. it('should write a snapshot from before the destructive change', async function () {
  533. await DocUpdaterClient.getDoc(this.project_id, this.doc_id)
  534. const docSnapshots = await db.docSnapshots
  535. .find({
  536. project_id: new ObjectId(this.project_id),
  537. doc_id: new ObjectId(this.doc_id),
  538. })
  539. .toArray()
  540. expect(docSnapshots.length).to.equal(1)
  541. expect(docSnapshots[0].version).to.equal(1)
  542. expect(docSnapshots[0].lines).to.deep.equal(['bar baz'])
  543. expect(docSnapshots[0].ranges.comments[0].op).to.deep.equal({
  544. c: 'a',
  545. p: 1,
  546. tid: this.tid,
  547. })
  548. })
  549. })
  550. })