ArchivingUpdatesTests.js 7.2 KB

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