ArchivingUpdatesTests.js 8.7 KB

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