ArchivingUpdatesTests.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* eslint-disable
  2. camelcase,
  3. no-undef,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS103: Rewrite code to no longer use __guard__
  12. * DS202: Simplify dynamic range loops
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const sinon = require('sinon')
  17. const { expect } = require('chai')
  18. const { db, ObjectId } = require('../../../app/js/mongodb')
  19. const Settings = require('@overleaf/settings')
  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 MockDocStoreApi = require('./helpers/MockDocStoreApi')
  25. const MockWebApi = require('./helpers/MockWebApi')
  26. describe('Archiving updates', function () {
  27. before(function (done) {
  28. if (
  29. __guard__(
  30. __guard__(
  31. Settings != null ? Settings.trackchanges : undefined,
  32. x1 => x1.s3
  33. ),
  34. x => x.key.length
  35. ) < 1
  36. ) {
  37. const message = new Error('s3 keys not setup, this test setup will fail')
  38. return done(message)
  39. }
  40. return TrackChangesClient.waitForS3(done)
  41. })
  42. before(function (done) {
  43. this.now = Date.now()
  44. this.to = this.now
  45. this.user_id = ObjectId().toString()
  46. this.user_id_2 = ObjectId().toString()
  47. this.doc_id = ObjectId().toString()
  48. this.project_id = ObjectId().toString()
  49. this.minutes = 60 * 1000
  50. this.hours = 60 * this.minutes
  51. MockWebApi.projects[this.project_id] = {
  52. features: {
  53. versioning: true,
  54. },
  55. }
  56. sinon.spy(MockWebApi, 'getProjectDetails')
  57. MockWebApi.users[this.user_id] = this.user = {
  58. email: 'user@sharelatex.com',
  59. first_name: 'Leo',
  60. last_name: 'Lion',
  61. id: this.user_id,
  62. }
  63. sinon.spy(MockWebApi, 'getUserInfo')
  64. MockDocStoreApi.docs[this.doc_id] = this.doc = {
  65. _id: this.doc_id,
  66. project_id: this.project_id,
  67. }
  68. sinon.spy(MockDocStoreApi, 'getAllDoc')
  69. this.updates = []
  70. for (
  71. let i = 0, end = 512 + 10, asc = end >= 0;
  72. asc ? i <= end : i >= end;
  73. asc ? i++ : i--
  74. ) {
  75. this.updates.push({
  76. op: [{ i: 'a', p: 0 }],
  77. meta: { ts: this.now + (i - 2048) * this.hours, user_id: this.user_id },
  78. v: 2 * i + 1,
  79. })
  80. this.updates.push({
  81. op: [{ i: 'b', p: 0 }],
  82. meta: {
  83. ts: this.now + (i - 2048) * this.hours + 10 * this.minutes,
  84. user_id: this.user_id_2,
  85. },
  86. v: 2 * i + 2,
  87. })
  88. }
  89. TrackChangesApp.ensureRunning(() => {
  90. return TrackChangesClient.pushRawUpdates(
  91. this.project_id,
  92. this.doc_id,
  93. this.updates,
  94. error => {
  95. if (error != null) {
  96. throw error
  97. }
  98. return TrackChangesClient.flushDoc(
  99. this.project_id,
  100. this.doc_id,
  101. error => {
  102. if (error != null) {
  103. throw error
  104. }
  105. return done()
  106. }
  107. )
  108. }
  109. )
  110. })
  111. return null
  112. })
  113. after(function (done) {
  114. MockWebApi.getUserInfo.restore()
  115. return db.docHistory.deleteMany(
  116. { project_id: ObjectId(this.project_id) },
  117. () => {
  118. return db.docHistoryIndex.remove(
  119. { project_id: ObjectId(this.project_id) },
  120. () => {
  121. return TrackChangesClient.removeS3Doc(
  122. this.project_id,
  123. this.doc_id,
  124. done
  125. )
  126. }
  127. )
  128. }
  129. )
  130. })
  131. function testExportFeature() {
  132. describe('exporting the project', function () {
  133. before('fetch export', function (done) {
  134. TrackChangesClient.exportProject(
  135. this.project_id,
  136. (error, updates, userIds) => {
  137. if (error) {
  138. return done(error)
  139. }
  140. this.exportedUpdates = updates
  141. this.exportedUserIds = userIds
  142. done()
  143. }
  144. )
  145. })
  146. it('should include all the imported updates, with ids, sorted by timestamp', function () {
  147. // Add a safe guard for an empty array matching an empty export.
  148. expect(this.updates).to.have.length(1024 + 22)
  149. const expectedExportedUpdates = this.updates
  150. .slice()
  151. .reverse()
  152. .map(update => {
  153. // clone object, updates are created once in before handler
  154. const exportedUpdate = Object.assign({}, update)
  155. exportedUpdate.meta = Object.assign({}, update.meta)
  156. exportedUpdate.doc_id = this.doc_id
  157. exportedUpdate.project_id = this.project_id
  158. // This is for merged updates, which does not apply here.
  159. exportedUpdate.meta.start_ts = exportedUpdate.meta.end_ts =
  160. exportedUpdate.meta.ts
  161. delete exportedUpdate.meta.ts
  162. return exportedUpdate
  163. })
  164. expect(this.exportedUpdates).to.deep.equal(expectedExportedUpdates)
  165. expect(this.exportedUserIds).to.deep.equal([
  166. this.user_id,
  167. this.user_id_2,
  168. ])
  169. })
  170. })
  171. }
  172. describe("before archiving a doc's updates", function () {
  173. testExportFeature()
  174. })
  175. describe("archiving a doc's updates", function () {
  176. before(function (done) {
  177. TrackChangesClient.pushDocHistory(this.project_id, this.doc_id, error => {
  178. if (error != null) {
  179. throw error
  180. }
  181. return done()
  182. })
  183. return null
  184. })
  185. it('should have one cached pack', function (done) {
  186. return db.docHistory.count(
  187. { doc_id: ObjectId(this.doc_id), expiresAt: { $exists: true } },
  188. (error, count) => {
  189. if (error != null) {
  190. throw error
  191. }
  192. count.should.equal(1)
  193. return done()
  194. }
  195. )
  196. })
  197. it('should have one remaining pack after cache is expired', function (done) {
  198. return db.docHistory.deleteMany(
  199. {
  200. doc_id: ObjectId(this.doc_id),
  201. expiresAt: { $exists: true },
  202. },
  203. (err, result) => {
  204. if (err) return done(err)
  205. return db.docHistory.count(
  206. { doc_id: ObjectId(this.doc_id) },
  207. (error, count) => {
  208. if (error != null) {
  209. throw error
  210. }
  211. count.should.equal(1)
  212. return done()
  213. }
  214. )
  215. }
  216. )
  217. })
  218. it('should have a docHistoryIndex entry marked as inS3', function (done) {
  219. return db.docHistoryIndex.findOne(
  220. { _id: ObjectId(this.doc_id) },
  221. (error, index) => {
  222. if (error != null) {
  223. throw error
  224. }
  225. index.packs[0].inS3.should.equal(true)
  226. return done()
  227. }
  228. )
  229. })
  230. it('should have a docHistoryIndex entry with the last version', function (done) {
  231. return db.docHistoryIndex.findOne(
  232. { _id: ObjectId(this.doc_id) },
  233. (error, index) => {
  234. if (error != null) {
  235. throw error
  236. }
  237. index.packs[0].v_end.should.equal(1024)
  238. return done()
  239. }
  240. )
  241. })
  242. it('should store 1024 doc changes in S3 in one pack', function (done) {
  243. return db.docHistoryIndex.findOne(
  244. { _id: ObjectId(this.doc_id) },
  245. (error, index) => {
  246. if (error != null) {
  247. throw error
  248. }
  249. const pack_id = index.packs[0]._id
  250. return TrackChangesClient.getS3Doc(
  251. this.project_id,
  252. this.doc_id,
  253. pack_id,
  254. (error, doc) => {
  255. if (error) return done(error)
  256. doc.n.should.equal(1024)
  257. doc.pack.length.should.equal(1024)
  258. return done()
  259. }
  260. )
  261. }
  262. )
  263. })
  264. testExportFeature()
  265. })
  266. return describe("unarchiving a doc's updates", function () {
  267. before(function (done) {
  268. TrackChangesClient.pullDocHistory(this.project_id, this.doc_id, error => {
  269. if (error != null) {
  270. throw error
  271. }
  272. return done()
  273. })
  274. return null
  275. })
  276. return it('should restore both packs', function (done) {
  277. return db.docHistory.count(
  278. { doc_id: ObjectId(this.doc_id) },
  279. (error, count) => {
  280. if (error != null) {
  281. throw error
  282. }
  283. count.should.equal(2)
  284. return done()
  285. }
  286. )
  287. })
  288. })
  289. })
  290. function __guard__(value, transform) {
  291. return typeof value !== 'undefined' && value !== null
  292. ? transform(value)
  293. : undefined
  294. }