project_flush.test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict'
  2. const BPromise = require('bluebird')
  3. const { expect } = require('chai')
  4. const HTTPStatus = require('http-status')
  5. const fetch = require('node-fetch')
  6. const fs = BPromise.promisifyAll(require('node:fs'))
  7. const cleanup = require('../storage/support/cleanup')
  8. const fixtures = require('../storage/support/fixtures')
  9. const testFiles = require('../storage/support/test_files')
  10. const testProjects = require('./support/test_projects')
  11. const testServer = require('./support/test_server')
  12. const { Change, File, Operation } = require('overleaf-editor-core')
  13. const queueChanges = require('../../../../storage/lib/queue_changes')
  14. const { getState } = require('../../../../storage/lib/chunk_store/redis')
  15. describe('project flush', function () {
  16. beforeEach(cleanup.everything)
  17. beforeEach(fixtures.create)
  18. it('persists queued changes to the chunk store', async function () {
  19. const basicAuthClient = testServer.basicAuthClient
  20. const projectId = await testProjects.createEmptyProject()
  21. // upload an empty file
  22. const response = await fetch(
  23. testServer.url(
  24. `/api/projects/${projectId}/blobs/${File.EMPTY_FILE_HASH}`,
  25. { qs: { pathname: 'main.tex' } }
  26. ),
  27. {
  28. method: 'PUT',
  29. body: fs.createReadStream(testFiles.path('empty.tex')),
  30. headers: {
  31. Authorization: testServer.basicAuthHeader,
  32. },
  33. }
  34. )
  35. expect(response.ok).to.be.true
  36. const testFile = File.fromHash(File.EMPTY_FILE_HASH)
  37. const testChange = new Change(
  38. [Operation.addFile('main.tex', testFile)],
  39. new Date()
  40. )
  41. await queueChanges(projectId, [testChange], 0)
  42. // Verify that the changes are queued and not yet persisted
  43. const initialState = await getState(projectId)
  44. expect(initialState.persistedVersion).to.be.null
  45. expect(initialState.changes).to.have.lengthOf(1)
  46. const importResponse =
  47. await basicAuthClient.apis.ProjectImport.flushChanges({
  48. project_id: projectId,
  49. })
  50. expect(importResponse.status).to.equal(HTTPStatus.OK)
  51. // Verify that the changes were persisted to the chunk store
  52. const finalState = await getState(projectId)
  53. expect(finalState.persistedVersion).to.equal(1)
  54. })
  55. })