FlushingUpdatesTests.js 7.5 KB

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