ArchivingUpdatesTests.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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(
  138. this.project_id,
  139. this.doc_id,
  140. (error) => {
  141. if (error != null) {
  142. throw error
  143. }
  144. return done()
  145. }
  146. )
  147. return null
  148. })
  149. it('should have one cached pack', function (done) {
  150. return db.docHistory.count(
  151. { doc_id: ObjectId(this.doc_id), expiresAt: { $exists: true } },
  152. (error, count) => {
  153. if (error != null) {
  154. throw error
  155. }
  156. count.should.equal(1)
  157. return done()
  158. }
  159. )
  160. })
  161. it('should have one remaining pack after cache is expired', function (done) {
  162. return db.docHistory.remove(
  163. {
  164. doc_id: ObjectId(this.doc_id),
  165. expiresAt: { $exists: true }
  166. },
  167. (err, result) => {
  168. if (typeof error !== 'undefined' && error !== null) {
  169. throw error
  170. }
  171. return db.docHistory.count(
  172. { doc_id: ObjectId(this.doc_id) },
  173. (error, count) => {
  174. if (error != null) {
  175. throw error
  176. }
  177. count.should.equal(1)
  178. return done()
  179. }
  180. )
  181. }
  182. )
  183. })
  184. it('should have a docHistoryIndex entry marked as inS3', function (done) {
  185. return db.docHistoryIndex.findOne(
  186. { _id: ObjectId(this.doc_id) },
  187. (error, index) => {
  188. if (error != null) {
  189. throw error
  190. }
  191. index.packs[0].inS3.should.equal(true)
  192. return done()
  193. }
  194. )
  195. })
  196. it('should have a docHistoryIndex entry with the last version', function (done) {
  197. return db.docHistoryIndex.findOne(
  198. { _id: ObjectId(this.doc_id) },
  199. (error, index) => {
  200. if (error != null) {
  201. throw error
  202. }
  203. index.packs[0].v_end.should.equal(1024)
  204. return done()
  205. }
  206. )
  207. })
  208. return it('should store 1024 doc changes in S3 in one pack', function (done) {
  209. return db.docHistoryIndex.findOne(
  210. { _id: ObjectId(this.doc_id) },
  211. (error, index) => {
  212. if (error != null) {
  213. throw error
  214. }
  215. const pack_id = index.packs[0]._id
  216. return TrackChangesClient.getS3Doc(
  217. this.project_id,
  218. this.doc_id,
  219. pack_id,
  220. (error, doc) => {
  221. doc.n.should.equal(1024)
  222. doc.pack.length.should.equal(1024)
  223. return done()
  224. }
  225. )
  226. }
  227. )
  228. })
  229. })
  230. return describe("unarchiving a doc's updates", function () {
  231. before(function (done) {
  232. TrackChangesClient.pullDocHistory(
  233. this.project_id,
  234. this.doc_id,
  235. (error) => {
  236. if (error != null) {
  237. throw error
  238. }
  239. return done()
  240. }
  241. )
  242. return null
  243. })
  244. return it('should restore both packs', function (done) {
  245. return db.docHistory.count(
  246. { doc_id: ObjectId(this.doc_id) },
  247. (error, count) => {
  248. if (error != null) {
  249. throw error
  250. }
  251. count.should.equal(2)
  252. return done()
  253. }
  254. )
  255. })
  256. })
  257. })
  258. function __guard__(value, transform) {
  259. return typeof value !== 'undefined' && value !== null
  260. ? transform(value)
  261. : undefined
  262. }