RangesTests.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const { expect } = require('chai')
  15. const async = require('async')
  16. const { db, ObjectId } = require('../../../app/js/mongodb')
  17. const MockWebApi = require('./helpers/MockWebApi')
  18. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  19. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  20. describe('Ranges', function () {
  21. before(function (done) {
  22. return DocUpdaterApp.ensureRunning(done)
  23. })
  24. describe('tracking changes from ops', function () {
  25. before(function (done) {
  26. this.project_id = DocUpdaterClient.randomId()
  27. this.user_id = DocUpdaterClient.randomId()
  28. this.id_seed = '587357bd35e64f6157'
  29. this.doc = {
  30. id: DocUpdaterClient.randomId(),
  31. lines: ['aaa'],
  32. }
  33. this.updates = [
  34. {
  35. doc: this.doc.id,
  36. op: [{ i: '123', p: 1 }],
  37. v: 0,
  38. meta: { user_id: this.user_id },
  39. },
  40. {
  41. doc: this.doc.id,
  42. op: [{ i: '456', p: 5 }],
  43. v: 1,
  44. meta: { user_id: this.user_id, tc: this.id_seed },
  45. },
  46. {
  47. doc: this.doc.id,
  48. op: [{ d: '12', p: 1 }],
  49. v: 2,
  50. meta: { user_id: this.user_id },
  51. },
  52. ]
  53. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  54. lines: this.doc.lines,
  55. version: 0,
  56. })
  57. const jobs = []
  58. for (const update of Array.from(this.updates)) {
  59. ;(update => {
  60. return jobs.push(callback =>
  61. DocUpdaterClient.sendUpdate(
  62. this.project_id,
  63. this.doc.id,
  64. update,
  65. callback
  66. )
  67. )
  68. })(update)
  69. }
  70. return DocUpdaterApp.ensureRunning(error => {
  71. if (error != null) {
  72. throw error
  73. }
  74. return DocUpdaterClient.preloadDoc(
  75. this.project_id,
  76. this.doc.id,
  77. error => {
  78. if (error != null) {
  79. throw error
  80. }
  81. return async.series(jobs, error => {
  82. if (error != null) {
  83. throw error
  84. }
  85. return done()
  86. })
  87. }
  88. )
  89. })
  90. })
  91. it('should update the ranges', function (done) {
  92. return DocUpdaterClient.getDoc(
  93. this.project_id,
  94. this.doc.id,
  95. (error, res, data) => {
  96. if (error != null) {
  97. throw error
  98. }
  99. const { ranges } = data
  100. const change = ranges.changes[0]
  101. change.op.should.deep.equal({ i: '456', p: 3 })
  102. change.id.should.equal(this.id_seed + '000001')
  103. change.metadata.user_id.should.equal(this.user_id)
  104. return done()
  105. }
  106. )
  107. })
  108. return describe('Adding comments', function () {
  109. describe('standalone', function () {
  110. before(function (done) {
  111. this.project_id = DocUpdaterClient.randomId()
  112. this.user_id = DocUpdaterClient.randomId()
  113. this.doc = {
  114. id: DocUpdaterClient.randomId(),
  115. lines: ['foo bar baz'],
  116. }
  117. this.updates = [
  118. {
  119. doc: this.doc.id,
  120. op: [
  121. { c: 'bar', p: 4, t: (this.tid = DocUpdaterClient.randomId()) },
  122. ],
  123. v: 0,
  124. },
  125. ]
  126. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  127. lines: this.doc.lines,
  128. version: 0,
  129. })
  130. const jobs = []
  131. for (const update of Array.from(this.updates)) {
  132. ;(update => {
  133. return jobs.push(callback =>
  134. DocUpdaterClient.sendUpdate(
  135. this.project_id,
  136. this.doc.id,
  137. update,
  138. callback
  139. )
  140. )
  141. })(update)
  142. }
  143. return DocUpdaterClient.preloadDoc(
  144. this.project_id,
  145. this.doc.id,
  146. error => {
  147. if (error != null) {
  148. throw error
  149. }
  150. return async.series(jobs, error => {
  151. if (error != null) {
  152. throw error
  153. }
  154. return setTimeout(done, 200)
  155. })
  156. }
  157. )
  158. })
  159. return it('should update the ranges', function (done) {
  160. return DocUpdaterClient.getDoc(
  161. this.project_id,
  162. this.doc.id,
  163. (error, res, data) => {
  164. if (error != null) {
  165. throw error
  166. }
  167. const { ranges } = data
  168. const comment = ranges.comments[0]
  169. comment.op.should.deep.equal({ c: 'bar', p: 4, t: this.tid })
  170. comment.id.should.equal(this.tid)
  171. return done()
  172. }
  173. )
  174. })
  175. })
  176. return describe('with conflicting ops needing OT', function () {
  177. before(function (done) {
  178. this.project_id = DocUpdaterClient.randomId()
  179. this.user_id = DocUpdaterClient.randomId()
  180. this.doc = {
  181. id: DocUpdaterClient.randomId(),
  182. lines: ['foo bar baz'],
  183. }
  184. this.updates = [
  185. {
  186. doc: this.doc.id,
  187. op: [{ i: 'ABC', p: 3 }],
  188. v: 0,
  189. meta: { user_id: this.user_id },
  190. },
  191. {
  192. doc: this.doc.id,
  193. op: [
  194. { c: 'bar', p: 4, t: (this.tid = DocUpdaterClient.randomId()) },
  195. ],
  196. v: 0,
  197. },
  198. ]
  199. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  200. lines: this.doc.lines,
  201. version: 0,
  202. })
  203. const jobs = []
  204. for (const update of Array.from(this.updates)) {
  205. ;(update => {
  206. return jobs.push(callback =>
  207. DocUpdaterClient.sendUpdate(
  208. this.project_id,
  209. this.doc.id,
  210. update,
  211. callback
  212. )
  213. )
  214. })(update)
  215. }
  216. return DocUpdaterClient.preloadDoc(
  217. this.project_id,
  218. this.doc.id,
  219. error => {
  220. if (error != null) {
  221. throw error
  222. }
  223. return async.series(jobs, error => {
  224. if (error != null) {
  225. throw error
  226. }
  227. return setTimeout(done, 200)
  228. })
  229. }
  230. )
  231. })
  232. return it('should update the comments with the OT shifted comment', function (done) {
  233. return DocUpdaterClient.getDoc(
  234. this.project_id,
  235. this.doc.id,
  236. (error, res, data) => {
  237. if (error != null) {
  238. throw error
  239. }
  240. const { ranges } = data
  241. const comment = ranges.comments[0]
  242. comment.op.should.deep.equal({ c: 'bar', p: 7, t: this.tid })
  243. return done()
  244. }
  245. )
  246. })
  247. })
  248. })
  249. })
  250. describe('Loading ranges from persistence layer', function () {
  251. before(function (done) {
  252. this.project_id = DocUpdaterClient.randomId()
  253. this.user_id = DocUpdaterClient.randomId()
  254. this.id_seed = '587357bd35e64f6157'
  255. this.doc = {
  256. id: DocUpdaterClient.randomId(),
  257. lines: ['a123aa'],
  258. }
  259. this.update = {
  260. doc: this.doc.id,
  261. op: [{ i: '456', p: 5 }],
  262. v: 0,
  263. meta: { user_id: this.user_id, tc: this.id_seed },
  264. }
  265. MockWebApi.insertDoc(this.project_id, this.doc.id, {
  266. lines: this.doc.lines,
  267. version: 0,
  268. ranges: {
  269. changes: [
  270. {
  271. op: { i: '123', p: 1 },
  272. metadata: {
  273. user_id: this.user_id,
  274. ts: new Date(),
  275. },
  276. },
  277. ],
  278. },
  279. })
  280. return DocUpdaterClient.preloadDoc(
  281. this.project_id,
  282. this.doc.id,
  283. error => {
  284. if (error != null) {
  285. throw error
  286. }
  287. return DocUpdaterClient.sendUpdate(
  288. this.project_id,
  289. this.doc.id,
  290. this.update,
  291. error => {
  292. if (error != null) {
  293. throw error
  294. }
  295. return setTimeout(done, 200)
  296. }
  297. )
  298. }
  299. )
  300. })
  301. it('should have preloaded the existing ranges', function (done) {
  302. return DocUpdaterClient.getDoc(
  303. this.project_id,
  304. this.doc.id,
  305. (error, res, data) => {
  306. if (error != null) {
  307. throw error
  308. }
  309. const { changes } = data.ranges
  310. changes[0].op.should.deep.equal({ i: '123', p: 1 })
  311. changes[1].op.should.deep.equal({ i: '456', p: 5 })
  312. return done()
  313. }
  314. )
  315. })
  316. return it('should flush the ranges to the persistence layer again', function (done) {
  317. return DocUpdaterClient.flushDoc(this.project_id, this.doc.id, error => {
  318. if (error != null) {
  319. throw error
  320. }
  321. return MockWebApi.getDocument(
  322. this.project_id,
  323. this.doc.id,
  324. (error, doc) => {
  325. if (error) return done(error)
  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. })