RangesTests.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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(this.project_id, this.doc.id, error => {
  319. if (error != null) {
  320. throw error
  321. }
  322. return MockWebApi.getDocument(
  323. this.project_id,
  324. this.doc.id,
  325. (error, doc) => {
  326. const { changes } = doc.ranges
  327. changes[0].op.should.deep.equal({ i: '123', p: 1 })
  328. changes[1].op.should.deep.equal({ i: '456', p: 5 })
  329. return done()
  330. }
  331. )
  332. })
  333. })
  334. })
  335. describe('accepting a change', function () {
  336. before(function (done) {
  337. this.project_id = DocUpdaterClient.randomId()
  338. this.user_id = DocUpdaterClient.randomId()
  339. this.id_seed = '587357bd35e64f6157'
  340. this.doc = {
  341. id: DocUpdaterClient.randomId(),
  342. lines: ['aaa'],
  343. }
  344. this.update = {
  345. doc: this.doc.id,
  346. op: [{ i: '456', p: 1 }],
  347. v: 0,
  348. meta: { user_id: this.user_id, tc: this.id_seed },
  349. }
  350. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  351. lines: this.doc.lines,
  352. version: 0,
  353. })
  354. return DocUpdaterClient.preloadDoc(
  355. this.project_id,
  356. this.doc.id,
  357. error => {
  358. if (error != null) {
  359. throw error
  360. }
  361. return DocUpdaterClient.sendUpdate(
  362. this.project_id,
  363. this.doc.id,
  364. this.update,
  365. error => {
  366. if (error != null) {
  367. throw error
  368. }
  369. return setTimeout(() => {
  370. return DocUpdaterClient.getDoc(
  371. this.project_id,
  372. this.doc.id,
  373. (error, res, data) => {
  374. if (error != null) {
  375. throw error
  376. }
  377. const { ranges } = data
  378. const change = ranges.changes[0]
  379. change.op.should.deep.equal({ i: '456', p: 1 })
  380. change.id.should.equal(this.id_seed + '000001')
  381. change.metadata.user_id.should.equal(this.user_id)
  382. return done()
  383. }
  384. )
  385. }, 200)
  386. }
  387. )
  388. }
  389. )
  390. })
  391. return it('should remove the change after accepting', function (done) {
  392. return DocUpdaterClient.acceptChange(
  393. this.project_id,
  394. this.doc.id,
  395. this.id_seed + '000001',
  396. error => {
  397. if (error != null) {
  398. throw error
  399. }
  400. return DocUpdaterClient.getDoc(
  401. this.project_id,
  402. this.doc.id,
  403. (error, res, data) => {
  404. if (error != null) {
  405. throw error
  406. }
  407. expect(data.ranges.changes).to.be.undefined
  408. return done()
  409. }
  410. )
  411. }
  412. )
  413. })
  414. })
  415. describe('deleting a comment range', function () {
  416. before(function (done) {
  417. this.project_id = DocUpdaterClient.randomId()
  418. this.user_id = DocUpdaterClient.randomId()
  419. this.doc = {
  420. id: DocUpdaterClient.randomId(),
  421. lines: ['foo bar'],
  422. }
  423. this.update = {
  424. doc: this.doc.id,
  425. op: [{ c: 'bar', p: 4, t: (this.tid = DocUpdaterClient.randomId()) }],
  426. v: 0,
  427. }
  428. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  429. lines: this.doc.lines,
  430. version: 0,
  431. })
  432. return DocUpdaterClient.preloadDoc(
  433. this.project_id,
  434. this.doc.id,
  435. error => {
  436. if (error != null) {
  437. throw error
  438. }
  439. return DocUpdaterClient.sendUpdate(
  440. this.project_id,
  441. this.doc.id,
  442. this.update,
  443. error => {
  444. if (error != null) {
  445. throw error
  446. }
  447. return setTimeout(() => {
  448. return DocUpdaterClient.getDoc(
  449. this.project_id,
  450. this.doc.id,
  451. (error, res, data) => {
  452. if (error != null) {
  453. throw error
  454. }
  455. const { ranges } = data
  456. const change = ranges.comments[0]
  457. change.op.should.deep.equal({ c: 'bar', p: 4, t: this.tid })
  458. change.id.should.equal(this.tid)
  459. return done()
  460. }
  461. )
  462. }, 200)
  463. }
  464. )
  465. }
  466. )
  467. })
  468. return it('should remove the comment range', function (done) {
  469. return DocUpdaterClient.removeComment(
  470. this.project_id,
  471. this.doc.id,
  472. this.tid,
  473. (error, res) => {
  474. if (error != null) {
  475. throw error
  476. }
  477. expect(res.statusCode).to.equal(204)
  478. return DocUpdaterClient.getDoc(
  479. this.project_id,
  480. this.doc.id,
  481. (error, res, data) => {
  482. if (error != null) {
  483. throw error
  484. }
  485. expect(data.ranges.comments).to.be.undefined
  486. return done()
  487. }
  488. )
  489. }
  490. )
  491. })
  492. })
  493. describe('tripping range size limit', function () {
  494. before(function (done) {
  495. this.project_id = DocUpdaterClient.randomId()
  496. this.user_id = DocUpdaterClient.randomId()
  497. this.id_seed = DocUpdaterClient.randomId()
  498. this.doc = {
  499. id: DocUpdaterClient.randomId(),
  500. lines: ['aaa'],
  501. }
  502. this.i = new Array(3 * 1024 * 1024).join('a')
  503. this.updates = [
  504. {
  505. doc: this.doc.id,
  506. op: [{ i: this.i, p: 1 }],
  507. v: 0,
  508. meta: { user_id: this.user_id, tc: this.id_seed },
  509. },
  510. ]
  511. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  512. lines: this.doc.lines,
  513. version: 0,
  514. })
  515. const jobs = []
  516. for (const update of Array.from(this.updates)) {
  517. ;(update => {
  518. return jobs.push(callback =>
  519. DocUpdaterClient.sendUpdate(
  520. this.project_id,
  521. this.doc.id,
  522. update,
  523. callback
  524. )
  525. )
  526. })(update)
  527. }
  528. return DocUpdaterClient.preloadDoc(
  529. this.project_id,
  530. this.doc.id,
  531. error => {
  532. if (error != null) {
  533. throw error
  534. }
  535. return async.series(jobs, error => {
  536. if (error != null) {
  537. throw error
  538. }
  539. return setTimeout(done, 200)
  540. })
  541. }
  542. )
  543. })
  544. return it('should not update the ranges', function (done) {
  545. return DocUpdaterClient.getDoc(
  546. this.project_id,
  547. this.doc.id,
  548. (error, res, data) => {
  549. if (error != null) {
  550. throw error
  551. }
  552. const { ranges } = data
  553. expect(ranges.changes).to.be.undefined
  554. return done()
  555. }
  556. )
  557. })
  558. })
  559. return describe('deleting text surrounding a comment', function () {
  560. before(function (done) {
  561. this.project_id = DocUpdaterClient.randomId()
  562. this.user_id = DocUpdaterClient.randomId()
  563. this.doc_id = DocUpdaterClient.randomId()
  564. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  565. lines: ['foo bar baz'],
  566. version: 0,
  567. ranges: {
  568. comments: [
  569. {
  570. op: {
  571. c: 'a',
  572. p: 5,
  573. tid: (this.tid = DocUpdaterClient.randomId()),
  574. },
  575. metadata: {
  576. user_id: this.user_id,
  577. ts: new Date(),
  578. },
  579. },
  580. ],
  581. },
  582. })
  583. this.updates = [
  584. {
  585. doc: this.doc_id,
  586. op: [{ d: 'foo ', p: 0 }],
  587. v: 0,
  588. meta: { user_id: this.user_id },
  589. },
  590. {
  591. doc: this.doc_id,
  592. op: [{ d: 'bar ', p: 0 }],
  593. v: 1,
  594. meta: { user_id: this.user_id },
  595. },
  596. ]
  597. const jobs = []
  598. for (const update of Array.from(this.updates)) {
  599. ;(update => {
  600. return jobs.push(callback =>
  601. DocUpdaterClient.sendUpdate(
  602. this.project_id,
  603. this.doc_id,
  604. update,
  605. callback
  606. )
  607. )
  608. })(update)
  609. }
  610. return DocUpdaterClient.preloadDoc(
  611. this.project_id,
  612. this.doc_id,
  613. error => {
  614. if (error != null) {
  615. throw error
  616. }
  617. return async.series(jobs, function (error) {
  618. if (error != null) {
  619. throw error
  620. }
  621. return setTimeout(() => {
  622. return DocUpdaterClient.getDoc(
  623. this.project_id,
  624. this.doc_id,
  625. (error, res, data) => {
  626. if (error != null) {
  627. throw error
  628. }
  629. return done()
  630. }
  631. )
  632. }, 200)
  633. })
  634. }
  635. )
  636. })
  637. return it('should write a snapshot from before the destructive change', function (done) {
  638. return DocUpdaterClient.getDoc(
  639. this.project_id,
  640. this.doc_id,
  641. (error, res, data) => {
  642. if (error != null) {
  643. return done(error)
  644. }
  645. db.docSnapshots
  646. .find({
  647. project_id: ObjectId(this.project_id),
  648. doc_id: ObjectId(this.doc_id),
  649. })
  650. .toArray((error, docSnapshots) => {
  651. if (error != null) {
  652. return done(error)
  653. }
  654. expect(docSnapshots.length).to.equal(1)
  655. expect(docSnapshots[0].version).to.equal(1)
  656. expect(docSnapshots[0].lines).to.deep.equal(['bar baz'])
  657. expect(docSnapshots[0].ranges.comments[0].op).to.deep.equal({
  658. c: 'a',
  659. p: 1,
  660. tid: this.tid,
  661. })
  662. return done()
  663. })
  664. }
  665. )
  666. })
  667. })
  668. })