FlushingUpdatesTests.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. * 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 chai = require('chai')
  15. chai.should()
  16. const { expect } = chai
  17. const { ObjectId } = require('../../../app/js/mongodb')
  18. const Settings = require('settings-sharelatex')
  19. const request = require('request')
  20. const rclient = require('redis').createClient(Settings.redis.history) // Only works locally for now
  21. const TrackChangesApp = require('./helpers/TrackChangesApp')
  22. const TrackChangesClient = require('./helpers/TrackChangesClient')
  23. const MockWebApi = require('./helpers/MockWebApi')
  24. describe('Flushing updates', function () {
  25. before(function (done) {
  26. return TrackChangesApp.ensureRunning(done)
  27. })
  28. describe("flushing a doc's updates", function () {
  29. before(function (done) {
  30. this.project_id = ObjectId().toString()
  31. this.doc_id = ObjectId().toString()
  32. this.user_id = ObjectId().toString()
  33. MockWebApi.projects[this.project_id] = { features: { versioning: true } }
  34. TrackChangesClient.pushRawUpdates(
  35. this.project_id,
  36. this.doc_id,
  37. [
  38. {
  39. op: [{ i: 'f', p: 3 }],
  40. meta: { ts: Date.now(), user_id: this.user_id },
  41. v: 3
  42. }
  43. ],
  44. (error) => {
  45. if (error != null) {
  46. throw error
  47. }
  48. return TrackChangesClient.flushDoc(
  49. this.project_id,
  50. this.doc_id,
  51. (error) => {
  52. if (error != null) {
  53. throw error
  54. }
  55. return done()
  56. }
  57. )
  58. }
  59. )
  60. return null
  61. })
  62. return it('should flush the op into mongo', function (done) {
  63. TrackChangesClient.getCompressedUpdates(this.doc_id, (error, updates) => {
  64. expect(updates[0].pack[0].op).to.deep.equal([
  65. {
  66. p: 3,
  67. i: 'f'
  68. }
  69. ])
  70. return done()
  71. })
  72. return null
  73. })
  74. })
  75. return describe("flushing a project's updates", function () {
  76. describe('with versioning enabled', function () {
  77. before(function (done) {
  78. this.project_id = ObjectId().toString()
  79. this.doc_id = ObjectId().toString()
  80. this.user_id = ObjectId().toString()
  81. this.weeks = 7 * 24 * 60 * 60 * 1000
  82. MockWebApi.projects[this.project_id] = {
  83. features: {
  84. versioning: true
  85. }
  86. }
  87. TrackChangesClient.pushRawUpdates(
  88. this.project_id,
  89. this.doc_id,
  90. [
  91. {
  92. op: [{ i: 'g', p: 2 }],
  93. meta: { ts: Date.now() - 2 * this.weeks, user_id: this.user_id },
  94. v: 2
  95. },
  96. {
  97. op: [{ i: 'f', p: 3 }],
  98. meta: { ts: Date.now(), user_id: this.user_id },
  99. v: 3
  100. }
  101. ],
  102. (error) => {
  103. if (error != null) {
  104. throw error
  105. }
  106. return TrackChangesClient.flushProject(this.project_id, (error) => {
  107. if (error != null) {
  108. throw error
  109. }
  110. return done()
  111. })
  112. }
  113. )
  114. return null
  115. })
  116. it('should not mark the updates for deletion', function (done) {
  117. TrackChangesClient.getCompressedUpdates(
  118. this.doc_id,
  119. (error, updates) => {
  120. expect(updates[0].expiresAt).to.not.exist
  121. return done()
  122. }
  123. )
  124. return null
  125. })
  126. return it('should preserve history forever', function (done) {
  127. TrackChangesClient.getProjectMetaData(
  128. this.project_id,
  129. (error, project) => {
  130. expect(project.preserveHistory).to.equal(true)
  131. return done()
  132. }
  133. )
  134. return null
  135. })
  136. })
  137. describe('without versioning enabled', function () {
  138. before(function (done) {
  139. this.project_id = ObjectId().toString()
  140. this.doc_id = ObjectId().toString()
  141. this.user_id = ObjectId().toString()
  142. this.weeks = 7 * 24 * 60 * 60 * 1000
  143. MockWebApi.projects[this.project_id] = {
  144. features: {
  145. versioning: false
  146. }
  147. }
  148. TrackChangesClient.pushRawUpdates(
  149. this.project_id,
  150. this.doc_id,
  151. [
  152. {
  153. op: [{ i: 'g', p: 2 }],
  154. meta: { ts: Date.now() - 2 * this.weeks, user_id: this.user_id },
  155. v: 2
  156. },
  157. {
  158. op: [{ i: 'f', p: 3 }],
  159. meta: { ts: Date.now(), user_id: this.user_id },
  160. v: 3
  161. }
  162. ],
  163. (error) => {
  164. if (error != null) {
  165. throw error
  166. }
  167. return TrackChangesClient.flushProject(this.project_id, (error) => {
  168. if (error != null) {
  169. throw error
  170. }
  171. return done()
  172. })
  173. }
  174. )
  175. return null
  176. })
  177. return it('should mark the updates for deletion', function (done) {
  178. TrackChangesClient.getCompressedUpdates(
  179. this.doc_id,
  180. (error, updates) => {
  181. expect(updates[0].expiresAt).to.exist
  182. return done()
  183. }
  184. )
  185. return null
  186. })
  187. })
  188. return describe('without versioning enabled but with preserveHistory set to true', function () {
  189. before(function (done) {
  190. this.project_id = ObjectId().toString()
  191. this.doc_id = ObjectId().toString()
  192. this.user_id = ObjectId().toString()
  193. this.weeks = 7 * 24 * 60 * 60 * 1000
  194. MockWebApi.projects[this.project_id] = {
  195. features: {
  196. versioning: false
  197. }
  198. }
  199. TrackChangesClient.setPreserveHistoryForProject(
  200. this.project_id,
  201. (error) => {
  202. if (error != null) {
  203. throw error
  204. }
  205. return TrackChangesClient.pushRawUpdates(
  206. this.project_id,
  207. this.doc_id,
  208. [
  209. {
  210. op: [{ i: 'g', p: 2 }],
  211. meta: {
  212. ts: Date.now() - 2 * this.weeks,
  213. user_id: this.user_id
  214. },
  215. v: 2
  216. },
  217. {
  218. op: [{ i: 'f', p: 3 }],
  219. meta: { ts: Date.now(), user_id: this.user_id },
  220. v: 3
  221. }
  222. ],
  223. (error) => {
  224. if (error != null) {
  225. throw error
  226. }
  227. return TrackChangesClient.flushProject(
  228. this.project_id,
  229. (error) => {
  230. if (error != null) {
  231. throw error
  232. }
  233. return done()
  234. }
  235. )
  236. }
  237. )
  238. }
  239. )
  240. return null
  241. })
  242. return it('should not mark the updates for deletion', function (done) {
  243. TrackChangesClient.getCompressedUpdates(
  244. this.doc_id,
  245. (error, updates) => {
  246. expect(updates[0].expiresAt).to.not.exist
  247. return done()
  248. }
  249. )
  250. return null
  251. })
  252. })
  253. })
  254. })