ArchivingUpdatesTests.js 8.9 KB

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