ArchivingUpdatesTests.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 mongojs = require('../../../app/js/mongojs')
  22. const { db } = mongojs
  23. const { ObjectId } = mongojs
  24. const Settings = require('settings-sharelatex')
  25. const request = require('request')
  26. const rclient = require('redis').createClient(Settings.redis.history) // Only works locally for now
  27. const TrackChangesApp = require('./helpers/TrackChangesApp')
  28. const TrackChangesClient = require('./helpers/TrackChangesClient')
  29. const MockDocStoreApi = require('./helpers/MockDocStoreApi')
  30. const MockWebApi = require('./helpers/MockWebApi')
  31. describe('Archiving updates', function() {
  32. before(function(done) {
  33. if (
  34. __guard__(
  35. __guard__(
  36. Settings != null ? Settings.trackchanges : undefined,
  37. x1 => x1.s3
  38. ),
  39. x => x.key.length
  40. ) < 1
  41. ) {
  42. const message = new Error('s3 keys not setup, this test setup will fail')
  43. return done(message)
  44. }
  45. return TrackChangesClient.waitForS3(done)
  46. })
  47. before(function(done) {
  48. this.now = Date.now()
  49. this.to = this.now
  50. this.user_id = ObjectId().toString()
  51. this.doc_id = ObjectId().toString()
  52. this.project_id = ObjectId().toString()
  53. this.minutes = 60 * 1000
  54. this.hours = 60 * this.minutes
  55. MockWebApi.projects[this.project_id] = {
  56. features: {
  57. versioning: true
  58. }
  59. }
  60. sinon.spy(MockWebApi, 'getProjectDetails')
  61. MockWebApi.users[this.user_id] = this.user = {
  62. email: 'user@sharelatex.com',
  63. first_name: 'Leo',
  64. last_name: 'Lion',
  65. id: this.user_id
  66. }
  67. sinon.spy(MockWebApi, 'getUserInfo')
  68. MockDocStoreApi.docs[this.doc_id] = this.doc = {
  69. _id: this.doc_id,
  70. project_id: this.project_id
  71. }
  72. sinon.spy(MockDocStoreApi, 'getAllDoc')
  73. this.updates = []
  74. for (
  75. let i = 0, end = 512 + 10, asc = end >= 0;
  76. asc ? i <= end : i >= end;
  77. asc ? i++ : i--
  78. ) {
  79. this.updates.push({
  80. op: [{ i: 'a', p: 0 }],
  81. meta: { ts: this.now + (i - 2048) * this.hours, user_id: this.user_id },
  82. v: 2 * i + 1
  83. })
  84. this.updates.push({
  85. op: [{ i: 'b', p: 0 }],
  86. meta: {
  87. ts: this.now + (i - 2048) * this.hours + 10 * this.minutes,
  88. user_id: this.user_id
  89. },
  90. v: 2 * i + 2
  91. })
  92. }
  93. TrackChangesApp.ensureRunning(() => {
  94. return TrackChangesClient.pushRawUpdates(
  95. this.project_id,
  96. this.doc_id,
  97. this.updates,
  98. error => {
  99. if (error != null) {
  100. throw error
  101. }
  102. return TrackChangesClient.flushDoc(
  103. this.project_id,
  104. this.doc_id,
  105. error => {
  106. if (error != null) {
  107. throw error
  108. }
  109. return done()
  110. }
  111. )
  112. }
  113. )
  114. })
  115. return null
  116. })
  117. after(function(done) {
  118. MockWebApi.getUserInfo.restore()
  119. return db.docHistory.remove(
  120. { project_id: ObjectId(this.project_id) },
  121. () => {
  122. return db.docHistoryIndex.remove(
  123. { project_id: ObjectId(this.project_id) },
  124. () => {
  125. return TrackChangesClient.removeS3Doc(
  126. this.project_id,
  127. this.doc_id,
  128. done
  129. )
  130. }
  131. )
  132. }
  133. )
  134. })
  135. describe("archiving a doc's updates", function() {
  136. before(function(done) {
  137. TrackChangesClient.pushDocHistory(this.project_id, this.doc_id, error => {
  138. if (error != null) {
  139. throw error
  140. }
  141. return done()
  142. })
  143. return null
  144. })
  145. it('should have one cached pack', function(done) {
  146. return db.docHistory.count(
  147. { doc_id: ObjectId(this.doc_id), expiresAt: { $exists: true } },
  148. (error, count) => {
  149. if (error != null) {
  150. throw error
  151. }
  152. count.should.equal(1)
  153. return done()
  154. }
  155. )
  156. })
  157. it('should have one remaining pack after cache is expired', function(done) {
  158. return db.docHistory.remove(
  159. {
  160. doc_id: ObjectId(this.doc_id),
  161. expiresAt: { $exists: true }
  162. },
  163. (err, result) => {
  164. if (typeof error !== 'undefined' && error !== null) {
  165. throw error
  166. }
  167. return db.docHistory.count(
  168. { doc_id: ObjectId(this.doc_id) },
  169. (error, count) => {
  170. if (error != null) {
  171. throw error
  172. }
  173. count.should.equal(1)
  174. return done()
  175. }
  176. )
  177. }
  178. )
  179. })
  180. it('should have a docHistoryIndex entry marked as inS3', function(done) {
  181. return db.docHistoryIndex.findOne(
  182. { _id: ObjectId(this.doc_id) },
  183. (error, index) => {
  184. if (error != null) {
  185. throw error
  186. }
  187. index.packs[0].inS3.should.equal(true)
  188. return done()
  189. }
  190. )
  191. })
  192. it('should have a docHistoryIndex entry with the last version', function(done) {
  193. return db.docHistoryIndex.findOne(
  194. { _id: ObjectId(this.doc_id) },
  195. (error, index) => {
  196. if (error != null) {
  197. throw error
  198. }
  199. index.packs[0].v_end.should.equal(1024)
  200. return done()
  201. }
  202. )
  203. })
  204. return it('should store 1024 doc changes in S3 in one pack', function(done) {
  205. return db.docHistoryIndex.findOne(
  206. { _id: ObjectId(this.doc_id) },
  207. (error, index) => {
  208. if (error != null) {
  209. throw error
  210. }
  211. const pack_id = index.packs[0]._id
  212. return TrackChangesClient.getS3Doc(
  213. this.project_id,
  214. this.doc_id,
  215. pack_id,
  216. (error, doc) => {
  217. doc.n.should.equal(1024)
  218. doc.pack.length.should.equal(1024)
  219. return done()
  220. }
  221. )
  222. }
  223. )
  224. })
  225. })
  226. return describe("unarchiving a doc's updates", function() {
  227. before(function(done) {
  228. TrackChangesClient.pullDocHistory(this.project_id, this.doc_id, error => {
  229. if (error != null) {
  230. throw error
  231. }
  232. return done()
  233. })
  234. return null
  235. })
  236. return it('should restore both packs', function(done) {
  237. return db.docHistory.count(
  238. { doc_id: ObjectId(this.doc_id) },
  239. (error, count) => {
  240. if (error != null) {
  241. throw error
  242. }
  243. count.should.equal(2)
  244. return done()
  245. }
  246. )
  247. })
  248. })
  249. })
  250. function __guard__(value, transform) {
  251. return typeof value !== 'undefined' && value !== null
  252. ? transform(value)
  253. : undefined
  254. }