FlushingUpdatesTests.js 7.5 KB

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