FlushingUpdatesTests.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 { expect } = require('chai')
  14. const { ObjectId } = require('../../../app/js/mongodb')
  15. const Settings = require('@overleaf/settings')
  16. const request = require('request')
  17. const rclient = require('redis').createClient(Settings.redis.history) // Only works locally for now
  18. const TrackChangesApp = require('./helpers/TrackChangesApp')
  19. const TrackChangesClient = require('./helpers/TrackChangesClient')
  20. const MockWebApi = require('./helpers/MockWebApi')
  21. describe('Flushing updates', function () {
  22. before(function (done) {
  23. return TrackChangesApp.ensureRunning(done)
  24. })
  25. describe("flushing a doc's updates", function () {
  26. before(function (done) {
  27. this.project_id = ObjectId().toString()
  28. this.doc_id = ObjectId().toString()
  29. this.user_id = ObjectId().toString()
  30. MockWebApi.projects[this.project_id] = { features: { versioning: true } }
  31. TrackChangesClient.pushRawUpdates(
  32. this.project_id,
  33. this.doc_id,
  34. [
  35. {
  36. op: [{ i: 'f', p: 3 }],
  37. meta: { ts: Date.now(), user_id: this.user_id },
  38. v: 3,
  39. },
  40. ],
  41. error => {
  42. if (error != null) {
  43. throw error
  44. }
  45. return TrackChangesClient.flushDoc(
  46. this.project_id,
  47. this.doc_id,
  48. error => {
  49. if (error != null) {
  50. throw error
  51. }
  52. return done()
  53. }
  54. )
  55. }
  56. )
  57. return null
  58. })
  59. return it('should flush the op into mongo', function (done) {
  60. TrackChangesClient.getCompressedUpdates(this.doc_id, (error, updates) => {
  61. if (error) return done(error)
  62. expect(updates[0].pack[0].op).to.deep.equal([
  63. {
  64. p: 3,
  65. i: 'f',
  66. },
  67. ])
  68. return done()
  69. })
  70. return null
  71. })
  72. })
  73. return describe("flushing a project's updates", function () {
  74. describe('with versioning enabled', function () {
  75. before(function (done) {
  76. this.project_id = ObjectId().toString()
  77. this.doc_id = ObjectId().toString()
  78. this.user_id = ObjectId().toString()
  79. this.weeks = 7 * 24 * 60 * 60 * 1000
  80. MockWebApi.projects[this.project_id] = {
  81. features: {
  82. versioning: true,
  83. },
  84. }
  85. TrackChangesClient.pushRawUpdates(
  86. this.project_id,
  87. this.doc_id,
  88. [
  89. {
  90. op: [{ i: 'g', p: 2 }],
  91. meta: { ts: Date.now() - 2 * this.weeks, user_id: this.user_id },
  92. v: 2,
  93. },
  94. {
  95. op: [{ i: 'f', p: 3 }],
  96. meta: { ts: Date.now(), user_id: this.user_id },
  97. v: 3,
  98. },
  99. ],
  100. error => {
  101. if (error != null) {
  102. throw error
  103. }
  104. return TrackChangesClient.flushProject(this.project_id, error => {
  105. if (error != null) {
  106. throw error
  107. }
  108. return done()
  109. })
  110. }
  111. )
  112. return null
  113. })
  114. it('should not mark the updates for deletion', function (done) {
  115. TrackChangesClient.getCompressedUpdates(
  116. this.doc_id,
  117. (error, updates) => {
  118. if (error) return done(error)
  119. expect(updates[0].expiresAt).to.not.exist
  120. return done()
  121. }
  122. )
  123. return null
  124. })
  125. return it('should preserve history forever', function (done) {
  126. TrackChangesClient.getProjectMetaData(
  127. this.project_id,
  128. (error, project) => {
  129. if (error) return done(error)
  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. if (error) return done(error)
  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. if (error) return done(error)
  248. expect(updates[0].expiresAt).to.not.exist
  249. return done()
  250. }
  251. )
  252. return null
  253. })
  254. })
  255. })
  256. })