GettingADiffTests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon')
  13. const chai = require('chai')
  14. chai.should()
  15. const { expect } = chai
  16. const mongojs = require('../../../app/js/mongojs')
  17. const { db } = mongojs
  18. const { ObjectId } = mongojs
  19. const Settings = require('settings-sharelatex')
  20. const TrackChangesApp = require('./helpers/TrackChangesApp')
  21. const TrackChangesClient = require('./helpers/TrackChangesClient')
  22. const MockDocUpdaterApi = require('./helpers/MockDocUpdaterApi')
  23. const MockWebApi = require('./helpers/MockWebApi')
  24. describe('Getting a diff', function () {
  25. beforeEach(function (done) {
  26. sinon.spy(MockDocUpdaterApi, 'getDoc')
  27. this.now = Date.now()
  28. this.from = this.now - 100000000
  29. this.to = this.now
  30. this.user_id = ObjectId().toString()
  31. this.doc_id = ObjectId().toString()
  32. this.project_id = ObjectId().toString()
  33. MockWebApi.projects[this.project_id] = { features: { versioning: true } }
  34. MockWebApi.users[this.user_id] = this.user = {
  35. email: 'user@sharelatex.com',
  36. first_name: 'Leo',
  37. last_name: 'Lion',
  38. id: this.user_id
  39. }
  40. sinon.spy(MockWebApi, 'getUserInfo')
  41. const twoMinutes = 2 * 60 * 1000
  42. this.updates = [
  43. {
  44. op: [{ i: 'one ', p: 0 }],
  45. meta: { ts: this.from - twoMinutes, user_id: this.user_id },
  46. v: 3
  47. },
  48. {
  49. op: [{ i: 'two ', p: 4 }],
  50. meta: { ts: this.from + twoMinutes, user_id: this.user_id },
  51. v: (this.fromVersion = 4)
  52. },
  53. {
  54. op: [{ i: 'three ', p: 8 }],
  55. meta: { ts: this.to - twoMinutes, user_id: this.user_id },
  56. v: (this.toVersion = 5)
  57. },
  58. {
  59. op: [{ i: 'four', p: 14 }],
  60. meta: { ts: this.to + twoMinutes, user_id: this.user_id },
  61. v: 6
  62. }
  63. ]
  64. this.lines = ['one two three four']
  65. this.expected_diff = [
  66. { u: 'one ' },
  67. {
  68. i: 'two three ',
  69. meta: {
  70. start_ts: this.from + twoMinutes,
  71. end_ts: this.to - twoMinutes,
  72. user: this.user
  73. }
  74. }
  75. ]
  76. MockDocUpdaterApi.docs[this.doc_id] = {
  77. lines: this.lines,
  78. version: 7
  79. }
  80. TrackChangesApp.ensureRunning(() => {
  81. return TrackChangesClient.pushRawUpdates(
  82. this.project_id,
  83. this.doc_id,
  84. this.updates,
  85. (error) => {
  86. if (error != null) {
  87. throw error
  88. }
  89. return TrackChangesClient.getDiff(
  90. this.project_id,
  91. this.doc_id,
  92. this.fromVersion,
  93. this.toVersion,
  94. (error, diff) => {
  95. if (error != null) {
  96. throw error
  97. }
  98. this.diff = diff.diff
  99. return done()
  100. }
  101. )
  102. }
  103. )
  104. })
  105. return null
  106. })
  107. afterEach(function () {
  108. MockDocUpdaterApi.getDoc.restore()
  109. MockWebApi.getUserInfo.restore()
  110. return null
  111. })
  112. it('should return the diff', function () {
  113. return expect(this.diff).to.deep.equal(this.expected_diff)
  114. })
  115. return it('should get the doc from the doc updater', function () {
  116. MockDocUpdaterApi.getDoc
  117. .calledWith(this.project_id, this.doc_id)
  118. .should.equal(true)
  119. return null
  120. })
  121. })