RangesTests.js 18 KB

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