ArchivingUpdatesTests.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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('settings-sharelatex')
  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(
  179. this.project_id,
  180. this.doc_id,
  181. (error) => {
  182. if (error != null) {
  183. throw error
  184. }
  185. return done()
  186. }
  187. )
  188. return null
  189. })
  190. it('should have one cached pack', function (done) {
  191. return db.docHistory.count(
  192. { doc_id: ObjectId(this.doc_id), expiresAt: { $exists: true } },
  193. (error, count) => {
  194. if (error != null) {
  195. throw error
  196. }
  197. count.should.equal(1)
  198. return done()
  199. }
  200. )
  201. })
  202. it('should have one remaining pack after cache is expired', function (done) {
  203. return db.docHistory.deleteMany(
  204. {
  205. doc_id: ObjectId(this.doc_id),
  206. expiresAt: { $exists: true }
  207. },
  208. (err, result) => {
  209. if (typeof error !== 'undefined' && error !== null) {
  210. throw error
  211. }
  212. return db.docHistory.count(
  213. { doc_id: ObjectId(this.doc_id) },
  214. (error, count) => {
  215. if (error != null) {
  216. throw error
  217. }
  218. count.should.equal(1)
  219. return done()
  220. }
  221. )
  222. }
  223. )
  224. })
  225. it('should have a docHistoryIndex entry marked as inS3', function (done) {
  226. return db.docHistoryIndex.findOne(
  227. { _id: ObjectId(this.doc_id) },
  228. (error, index) => {
  229. if (error != null) {
  230. throw error
  231. }
  232. index.packs[0].inS3.should.equal(true)
  233. return done()
  234. }
  235. )
  236. })
  237. it('should have a docHistoryIndex entry with the last version', function (done) {
  238. return db.docHistoryIndex.findOne(
  239. { _id: ObjectId(this.doc_id) },
  240. (error, index) => {
  241. if (error != null) {
  242. throw error
  243. }
  244. index.packs[0].v_end.should.equal(1024)
  245. return done()
  246. }
  247. )
  248. })
  249. it('should store 1024 doc changes in S3 in one pack', function (done) {
  250. return db.docHistoryIndex.findOne(
  251. { _id: ObjectId(this.doc_id) },
  252. (error, index) => {
  253. if (error != null) {
  254. throw error
  255. }
  256. const pack_id = index.packs[0]._id
  257. return TrackChangesClient.getS3Doc(
  258. this.project_id,
  259. this.doc_id,
  260. pack_id,
  261. (error, doc) => {
  262. doc.n.should.equal(1024)
  263. doc.pack.length.should.equal(1024)
  264. return done()
  265. }
  266. )
  267. }
  268. )
  269. })
  270. testExportFeature()
  271. })
  272. return describe("unarchiving a doc's updates", function () {
  273. before(function (done) {
  274. TrackChangesClient.pullDocHistory(
  275. this.project_id,
  276. this.doc_id,
  277. (error) => {
  278. if (error != null) {
  279. throw error
  280. }
  281. return done()
  282. }
  283. )
  284. return null
  285. })
  286. return it('should restore both packs', function (done) {
  287. return db.docHistory.count(
  288. { doc_id: ObjectId(this.doc_id) },
  289. (error, count) => {
  290. if (error != null) {
  291. throw error
  292. }
  293. count.should.equal(2)
  294. return done()
  295. }
  296. )
  297. })
  298. })
  299. })
  300. function __guard__(value, transform) {
  301. return typeof value !== 'undefined' && value !== null
  302. ? transform(value)
  303. : undefined
  304. }