RangesTests.js 19 KB

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