ApplyingUpdatesToADocTests.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const async = require('async')
  4. const Settings = require('@overleaf/settings')
  5. const rclientProjectHistory = require('@overleaf/redis-wrapper').createClient(
  6. Settings.redis.project_history
  7. )
  8. const rclientDU = require('@overleaf/redis-wrapper').createClient(
  9. Settings.redis.documentupdater
  10. )
  11. const Keys = Settings.redis.documentupdater.key_schema
  12. const ProjectHistoryKeys = Settings.redis.project_history.key_schema
  13. const MockWebApi = require('./helpers/MockWebApi')
  14. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  15. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  16. describe('Applying updates to a doc', function () {
  17. beforeEach(function (done) {
  18. sinon.spy(MockWebApi, 'getDocument')
  19. this.lines = ['one', 'two', 'three']
  20. this.version = 42
  21. this.op = {
  22. i: 'one and a half\n',
  23. p: 4,
  24. }
  25. this.project_id = DocUpdaterClient.randomId()
  26. this.doc_id = DocUpdaterClient.randomId()
  27. this.update = {
  28. doc: this.doc_id,
  29. op: [this.op],
  30. v: this.version,
  31. }
  32. this.result = ['one', 'one and a half', 'two', 'three']
  33. DocUpdaterApp.ensureRunning(done)
  34. })
  35. afterEach(function () {
  36. sinon.restore()
  37. })
  38. describe('when the document is not loaded', function () {
  39. beforeEach(function (done) {
  40. this.startTime = Date.now()
  41. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  42. lines: this.lines,
  43. version: this.version,
  44. })
  45. DocUpdaterClient.sendUpdate(
  46. this.project_id,
  47. this.doc_id,
  48. this.update,
  49. error => {
  50. if (error != null) {
  51. throw error
  52. }
  53. setTimeout(() => {
  54. rclientProjectHistory.get(
  55. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  56. project_id: this.project_id,
  57. }),
  58. (error, result) => {
  59. if (error != null) {
  60. throw error
  61. }
  62. result = parseInt(result, 10)
  63. this.firstOpTimestamp = result
  64. done()
  65. }
  66. )
  67. }, 200)
  68. }
  69. )
  70. })
  71. it('should load the document from the web API', function () {
  72. MockWebApi.getDocument
  73. .calledWith(this.project_id, this.doc_id)
  74. .should.equal(true)
  75. })
  76. it('should update the doc', function (done) {
  77. DocUpdaterClient.getDoc(
  78. this.project_id,
  79. this.doc_id,
  80. (error, res, doc) => {
  81. if (error) done(error)
  82. doc.lines.should.deep.equal(this.result)
  83. done()
  84. }
  85. )
  86. })
  87. it('should push the applied updates to the project history changes api', function (done) {
  88. rclientProjectHistory.lrange(
  89. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  90. 0,
  91. -1,
  92. (error, updates) => {
  93. if (error != null) {
  94. throw error
  95. }
  96. JSON.parse(updates[0]).op.should.deep.equal([this.op])
  97. done()
  98. }
  99. )
  100. })
  101. it('should set the first op timestamp', function () {
  102. this.firstOpTimestamp.should.be.within(this.startTime, Date.now())
  103. })
  104. it('should yield last updated time', function (done) {
  105. DocUpdaterClient.getProjectLastUpdatedAt(
  106. this.project_id,
  107. (error, res, body) => {
  108. if (error != null) {
  109. throw error
  110. }
  111. res.statusCode.should.equal(200)
  112. body.lastUpdatedAt.should.be.within(this.startTime, Date.now())
  113. done()
  114. }
  115. )
  116. })
  117. it('should yield no last updated time for another project', function (done) {
  118. DocUpdaterClient.getProjectLastUpdatedAt(
  119. DocUpdaterClient.randomId(),
  120. (error, res, body) => {
  121. if (error != null) {
  122. throw error
  123. }
  124. res.statusCode.should.equal(200)
  125. body.should.deep.equal({})
  126. done()
  127. }
  128. )
  129. })
  130. describe('when sending another update', function () {
  131. beforeEach(function (done) {
  132. this.timeout(10000)
  133. this.second_update = Object.assign({}, this.update)
  134. this.second_update.v = this.version + 1
  135. this.secondStartTime = Date.now()
  136. DocUpdaterClient.sendUpdate(
  137. this.project_id,
  138. this.doc_id,
  139. this.second_update,
  140. error => {
  141. if (error != null) {
  142. throw error
  143. }
  144. setTimeout(done, 200)
  145. }
  146. )
  147. })
  148. it('should update the doc', function (done) {
  149. DocUpdaterClient.getDoc(
  150. this.project_id,
  151. this.doc_id,
  152. (error, res, doc) => {
  153. if (error) done(error)
  154. doc.lines.should.deep.equal([
  155. 'one',
  156. 'one and a half',
  157. 'one and a half',
  158. 'two',
  159. 'three',
  160. ])
  161. done()
  162. }
  163. )
  164. })
  165. it('should not change the first op timestamp', function (done) {
  166. rclientProjectHistory.get(
  167. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  168. project_id: this.project_id,
  169. }),
  170. (error, result) => {
  171. if (error != null) {
  172. throw error
  173. }
  174. result = parseInt(result, 10)
  175. result.should.equal(this.firstOpTimestamp)
  176. done()
  177. }
  178. )
  179. })
  180. it('should yield last updated time', function (done) {
  181. DocUpdaterClient.getProjectLastUpdatedAt(
  182. this.project_id,
  183. (error, res, body) => {
  184. if (error != null) {
  185. throw error
  186. }
  187. res.statusCode.should.equal(200)
  188. body.lastUpdatedAt.should.be.within(
  189. this.secondStartTime,
  190. Date.now()
  191. )
  192. done()
  193. }
  194. )
  195. })
  196. })
  197. describe('when another client is sending a concurrent update', function () {
  198. beforeEach(function (done) {
  199. this.timeout(10000)
  200. this.otherUpdate = {
  201. doc: this.doc_id,
  202. op: [{ p: 8, i: 'two and a half\n' }],
  203. v: this.version,
  204. meta: { source: 'other-random-publicId' },
  205. }
  206. this.secondStartTime = Date.now()
  207. DocUpdaterClient.sendUpdate(
  208. this.project_id,
  209. this.doc_id,
  210. this.otherUpdate,
  211. error => {
  212. if (error != null) {
  213. throw error
  214. }
  215. setTimeout(done, 200)
  216. }
  217. )
  218. })
  219. it('should update the doc', function (done) {
  220. DocUpdaterClient.getDoc(
  221. this.project_id,
  222. this.doc_id,
  223. (error, res, doc) => {
  224. if (error) done(error)
  225. doc.lines.should.deep.equal([
  226. 'one',
  227. 'one and a half',
  228. 'two',
  229. 'two and a half',
  230. 'three',
  231. ])
  232. done()
  233. }
  234. )
  235. })
  236. it('should not change the first op timestamp', function (done) {
  237. rclientProjectHistory.get(
  238. ProjectHistoryKeys.projectHistoryFirstOpTimestamp({
  239. project_id: this.project_id,
  240. }),
  241. (error, result) => {
  242. if (error != null) {
  243. throw error
  244. }
  245. result = parseInt(result, 10)
  246. result.should.equal(this.firstOpTimestamp)
  247. done()
  248. }
  249. )
  250. })
  251. it('should yield last updated time', function (done) {
  252. DocUpdaterClient.getProjectLastUpdatedAt(
  253. this.project_id,
  254. (error, res, body) => {
  255. if (error != null) {
  256. throw error
  257. }
  258. res.statusCode.should.equal(200)
  259. body.lastUpdatedAt.should.be.within(
  260. this.secondStartTime,
  261. Date.now()
  262. )
  263. done()
  264. }
  265. )
  266. })
  267. })
  268. })
  269. describe('when the document is loaded', function () {
  270. beforeEach(function (done) {
  271. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  272. lines: this.lines,
  273. version: this.version,
  274. })
  275. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  276. if (error != null) {
  277. throw error
  278. }
  279. sinon.resetHistory()
  280. DocUpdaterClient.sendUpdate(
  281. this.project_id,
  282. this.doc_id,
  283. this.update,
  284. error => {
  285. if (error != null) {
  286. throw error
  287. }
  288. setTimeout(done, 200)
  289. }
  290. )
  291. })
  292. })
  293. it('should not need to call the web api', function () {
  294. MockWebApi.getDocument.called.should.equal(false)
  295. })
  296. it('should update the doc', function (done) {
  297. DocUpdaterClient.getDoc(
  298. this.project_id,
  299. this.doc_id,
  300. (error, res, doc) => {
  301. if (error) return done(error)
  302. doc.lines.should.deep.equal(this.result)
  303. done()
  304. }
  305. )
  306. })
  307. it('should push the applied updates to the project history changes api', function (done) {
  308. rclientProjectHistory.lrange(
  309. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  310. 0,
  311. -1,
  312. (error, updates) => {
  313. if (error) return done(error)
  314. JSON.parse(updates[0]).op.should.deep.equal([this.op])
  315. done()
  316. }
  317. )
  318. })
  319. })
  320. describe('when the document is loaded and is using project-history only', function () {
  321. beforeEach(function (done) {
  322. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  323. lines: this.lines,
  324. version: this.version,
  325. })
  326. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  327. if (error != null) {
  328. throw error
  329. }
  330. sinon.resetHistory()
  331. DocUpdaterClient.sendUpdate(
  332. this.project_id,
  333. this.doc_id,
  334. this.update,
  335. error => {
  336. if (error != null) {
  337. throw error
  338. }
  339. setTimeout(done, 200)
  340. }
  341. )
  342. })
  343. })
  344. it('should update the doc', function (done) {
  345. DocUpdaterClient.getDoc(
  346. this.project_id,
  347. this.doc_id,
  348. (error, res, doc) => {
  349. if (error) return done(error)
  350. doc.lines.should.deep.equal(this.result)
  351. done()
  352. }
  353. )
  354. })
  355. it('should push the applied updates to the project history changes api', function (done) {
  356. rclientProjectHistory.lrange(
  357. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  358. 0,
  359. -1,
  360. (error, updates) => {
  361. if (error) return done(error)
  362. JSON.parse(updates[0]).op.should.deep.equal([this.op])
  363. done()
  364. }
  365. )
  366. })
  367. })
  368. describe('when the document has been deleted', function () {
  369. describe('when the ops come in a single linear order', function () {
  370. beforeEach(function (done) {
  371. const lines = ['', '', '']
  372. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  373. lines,
  374. version: 0,
  375. })
  376. this.updates = [
  377. { doc_id: this.doc_id, v: 0, op: [{ i: 'h', p: 0 }] },
  378. { doc_id: this.doc_id, v: 1, op: [{ i: 'e', p: 1 }] },
  379. { doc_id: this.doc_id, v: 2, op: [{ i: 'l', p: 2 }] },
  380. { doc_id: this.doc_id, v: 3, op: [{ i: 'l', p: 3 }] },
  381. { doc_id: this.doc_id, v: 4, op: [{ i: 'o', p: 4 }] },
  382. { doc_id: this.doc_id, v: 5, op: [{ i: ' ', p: 5 }] },
  383. { doc_id: this.doc_id, v: 6, op: [{ i: 'w', p: 6 }] },
  384. { doc_id: this.doc_id, v: 7, op: [{ i: 'o', p: 7 }] },
  385. { doc_id: this.doc_id, v: 8, op: [{ i: 'r', p: 8 }] },
  386. { doc_id: this.doc_id, v: 9, op: [{ i: 'l', p: 9 }] },
  387. { doc_id: this.doc_id, v: 10, op: [{ i: 'd', p: 10 }] },
  388. ]
  389. this.my_result = ['hello world', '', '']
  390. const actions = []
  391. for (const update of this.updates.slice(0, 6)) {
  392. actions.push(callback =>
  393. DocUpdaterClient.sendUpdate(
  394. this.project_id,
  395. this.doc_id,
  396. update,
  397. callback
  398. )
  399. )
  400. }
  401. actions.push(callback =>
  402. DocUpdaterClient.deleteDoc(this.project_id, this.doc_id, callback)
  403. )
  404. for (const update of this.updates.slice(6)) {
  405. actions.push(callback =>
  406. DocUpdaterClient.sendUpdate(
  407. this.project_id,
  408. this.doc_id,
  409. update,
  410. callback
  411. )
  412. )
  413. }
  414. // process updates
  415. actions.push(cb =>
  416. DocUpdaterClient.getDoc(this.project_id, this.doc_id, cb)
  417. )
  418. async.series(actions, done)
  419. })
  420. it('should be able to continue applying updates when the project has been deleted', function (done) {
  421. DocUpdaterClient.getDoc(
  422. this.project_id,
  423. this.doc_id,
  424. (error, res, doc) => {
  425. if (error) return done(error)
  426. doc.lines.should.deep.equal(this.my_result)
  427. done()
  428. }
  429. )
  430. })
  431. it('should store the doc ops in the correct order', function (done) {
  432. rclientDU.lrange(
  433. Keys.docOps({ doc_id: this.doc_id }),
  434. 0,
  435. -1,
  436. (error, updates) => {
  437. if (error) return done(error)
  438. updates = updates.map(u => JSON.parse(u))
  439. for (let i = 0; i < this.updates.length; i++) {
  440. const appliedUpdate = this.updates[i]
  441. appliedUpdate.op.should.deep.equal(updates[i].op)
  442. }
  443. done()
  444. }
  445. )
  446. })
  447. })
  448. describe('when older ops come in after the delete', function () {
  449. beforeEach(function (done) {
  450. const lines = ['', '', '']
  451. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  452. lines,
  453. version: 0,
  454. })
  455. this.updates = [
  456. { doc_id: this.doc_id, v: 0, op: [{ i: 'h', p: 0 }] },
  457. { doc_id: this.doc_id, v: 1, op: [{ i: 'e', p: 1 }] },
  458. { doc_id: this.doc_id, v: 2, op: [{ i: 'l', p: 2 }] },
  459. { doc_id: this.doc_id, v: 3, op: [{ i: 'l', p: 3 }] },
  460. { doc_id: this.doc_id, v: 4, op: [{ i: 'o', p: 4 }] },
  461. { doc_id: this.doc_id, v: 0, op: [{ i: 'world', p: 1 }] },
  462. ]
  463. this.my_result = ['hello', 'world', '']
  464. done()
  465. })
  466. it('should be able to continue applying updates when the project has been deleted', function (done) {
  467. let update
  468. const actions = []
  469. for (update of this.updates.slice(0, 5)) {
  470. ;(update => {
  471. actions.push(callback =>
  472. DocUpdaterClient.sendUpdate(
  473. this.project_id,
  474. this.doc_id,
  475. update,
  476. callback
  477. )
  478. )
  479. })(update)
  480. }
  481. actions.push(callback =>
  482. DocUpdaterClient.deleteDoc(this.project_id, this.doc_id, callback)
  483. )
  484. for (update of this.updates.slice(5)) {
  485. ;(update => {
  486. actions.push(callback =>
  487. DocUpdaterClient.sendUpdate(
  488. this.project_id,
  489. this.doc_id,
  490. update,
  491. callback
  492. )
  493. )
  494. })(update)
  495. }
  496. async.series(actions, error => {
  497. if (error != null) {
  498. throw error
  499. }
  500. DocUpdaterClient.getDoc(
  501. this.project_id,
  502. this.doc_id,
  503. (error, res, doc) => {
  504. if (error) return done(error)
  505. doc.lines.should.deep.equal(this.my_result)
  506. done()
  507. }
  508. )
  509. })
  510. })
  511. })
  512. })
  513. describe('with a broken update', function () {
  514. beforeEach(function (done) {
  515. this.broken_update = {
  516. doc: this.doc_id,
  517. v: this.version,
  518. op: [{ d: 'not the correct content', p: 0 }],
  519. }
  520. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  521. lines: this.lines,
  522. version: this.version,
  523. })
  524. DocUpdaterClient.subscribeToAppliedOps(
  525. (this.messageCallback = sinon.stub())
  526. )
  527. DocUpdaterClient.sendUpdate(
  528. this.project_id,
  529. this.doc_id,
  530. this.broken_update,
  531. error => {
  532. if (error != null) {
  533. throw error
  534. }
  535. setTimeout(done, 200)
  536. }
  537. )
  538. })
  539. it('should not update the doc', function (done) {
  540. DocUpdaterClient.getDoc(
  541. this.project_id,
  542. this.doc_id,
  543. (error, res, doc) => {
  544. if (error) return done(error)
  545. doc.lines.should.deep.equal(this.lines)
  546. done()
  547. }
  548. )
  549. })
  550. it('should send a message with an error', function () {
  551. this.messageCallback.called.should.equal(true)
  552. const [channel, message] = this.messageCallback.args[0]
  553. channel.should.equal('applied-ops')
  554. JSON.parse(message).should.deep.include({
  555. project_id: this.project_id,
  556. doc_id: this.doc_id,
  557. error: 'Delete component does not match',
  558. })
  559. })
  560. })
  561. describe('when there is no version in Mongo', function () {
  562. beforeEach(function (done) {
  563. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  564. lines: this.lines,
  565. })
  566. const update = {
  567. doc: this.doc_id,
  568. op: this.update.op,
  569. v: 0,
  570. }
  571. DocUpdaterClient.sendUpdate(
  572. this.project_id,
  573. this.doc_id,
  574. update,
  575. error => {
  576. if (error != null) {
  577. throw error
  578. }
  579. setTimeout(done, 200)
  580. }
  581. )
  582. })
  583. it('should update the doc (using version = 0)', function (done) {
  584. DocUpdaterClient.getDoc(
  585. this.project_id,
  586. this.doc_id,
  587. (error, res, doc) => {
  588. if (error) return done(error)
  589. doc.lines.should.deep.equal(this.result)
  590. done()
  591. }
  592. )
  593. })
  594. })
  595. describe('when the sending duplicate ops', function () {
  596. beforeEach(function (done) {
  597. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  598. lines: this.lines,
  599. version: this.version,
  600. })
  601. DocUpdaterClient.subscribeToAppliedOps(
  602. (this.messageCallback = sinon.stub())
  603. )
  604. // One user delete 'one', the next turns it into 'once'. The second becomes a NOP.
  605. DocUpdaterClient.sendUpdate(
  606. this.project_id,
  607. this.doc_id,
  608. {
  609. doc: this.doc_id,
  610. op: [
  611. {
  612. i: 'one and a half\n',
  613. p: 4,
  614. },
  615. ],
  616. v: this.version,
  617. meta: {
  618. source: 'ikHceq3yfAdQYzBo4-xZ',
  619. },
  620. },
  621. error => {
  622. if (error != null) {
  623. throw error
  624. }
  625. setTimeout(() => {
  626. DocUpdaterClient.sendUpdate(
  627. this.project_id,
  628. this.doc_id,
  629. {
  630. doc: this.doc_id,
  631. op: [
  632. {
  633. i: 'one and a half\n',
  634. p: 4,
  635. },
  636. ],
  637. v: this.version,
  638. dupIfSource: ['ikHceq3yfAdQYzBo4-xZ'],
  639. meta: {
  640. source: 'ikHceq3yfAdQYzBo4-xZ',
  641. },
  642. },
  643. error => {
  644. if (error != null) {
  645. throw error
  646. }
  647. setTimeout(done, 200)
  648. }
  649. )
  650. }, 200)
  651. }
  652. )
  653. })
  654. it('should update the doc', function (done) {
  655. DocUpdaterClient.getDoc(
  656. this.project_id,
  657. this.doc_id,
  658. (error, res, doc) => {
  659. if (error) return done(error)
  660. doc.lines.should.deep.equal(this.result)
  661. done()
  662. }
  663. )
  664. })
  665. it('should return a message about duplicate ops', function () {
  666. this.messageCallback.calledTwice.should.equal(true)
  667. this.messageCallback.args[0][0].should.equal('applied-ops')
  668. expect(JSON.parse(this.messageCallback.args[0][1]).op.dup).to.be.undefined
  669. this.messageCallback.args[1][0].should.equal('applied-ops')
  670. expect(JSON.parse(this.messageCallback.args[1][1]).op.dup).to.equal(true)
  671. })
  672. })
  673. describe('when sending updates for a non-existing doc id', function () {
  674. beforeEach(function (done) {
  675. this.non_existing = {
  676. doc: this.doc_id,
  677. v: this.version,
  678. op: [{ d: 'content', p: 0 }],
  679. }
  680. DocUpdaterClient.subscribeToAppliedOps(
  681. (this.messageCallback = sinon.stub())
  682. )
  683. DocUpdaterClient.sendUpdate(
  684. this.project_id,
  685. this.doc_id,
  686. this.non_existing,
  687. error => {
  688. if (error != null) {
  689. throw error
  690. }
  691. setTimeout(done, 200)
  692. }
  693. )
  694. })
  695. it('should not update or create a doc', function (done) {
  696. DocUpdaterClient.getDoc(
  697. this.project_id,
  698. this.doc_id,
  699. (error, res, doc) => {
  700. if (error) return done(error)
  701. res.statusCode.should.equal(404)
  702. done()
  703. }
  704. )
  705. })
  706. it('should send a message with an error', function () {
  707. this.messageCallback.called.should.equal(true)
  708. const [channel, message] = this.messageCallback.args[0]
  709. channel.should.equal('applied-ops')
  710. JSON.parse(message).should.deep.include({
  711. project_id: this.project_id,
  712. doc_id: this.doc_id,
  713. error: `doc not not found: /project/${this.project_id}/doc/${this.doc_id}`,
  714. })
  715. })
  716. })
  717. })