ProjectPersistenceManagerTests.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const SandboxedModule = require('sandboxed-module')
  15. const sinon = require('sinon')
  16. const assert = require('chai').assert
  17. const modulePath = require('path').join(
  18. __dirname,
  19. '../../../app/js/ProjectPersistenceManager'
  20. )
  21. const tk = require('timekeeper')
  22. describe('ProjectPersistenceManager', function () {
  23. beforeEach(function () {
  24. this.ProjectPersistenceManager = SandboxedModule.require(modulePath, {
  25. requires: {
  26. './UrlCache': (this.UrlCache = {}),
  27. './CompileManager': (this.CompileManager = {}),
  28. diskusage: (this.diskusage = { check: sinon.stub() }),
  29. '@overleaf/settings': (this.settings = {
  30. project_cache_length_ms: 1000,
  31. path: {
  32. compilesDir: '/compiles',
  33. outputDir: '/output',
  34. clsiCacheDir: '/cache',
  35. },
  36. }),
  37. },
  38. })
  39. this.callback = sinon.stub()
  40. this.project_id = 'project-id-123'
  41. return (this.user_id = '1234')
  42. })
  43. describe('refreshExpiryTimeout', function () {
  44. it('should leave expiry alone if plenty of disk', function (done) {
  45. this.diskusage.check.resolves({
  46. available: 40,
  47. total: 100,
  48. })
  49. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  50. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(
  51. this.settings.project_cache_length_ms
  52. )
  53. done()
  54. })
  55. })
  56. it('should drop EXPIRY_TIMEOUT 10% if low disk usage', function (done) {
  57. this.diskusage.check.resolves({
  58. available: 5,
  59. total: 100,
  60. })
  61. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  62. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(900)
  63. done()
  64. })
  65. })
  66. it('should not drop EXPIRY_TIMEOUT to below 50% of project_cache_length_ms', function (done) {
  67. this.diskusage.check.resolves({
  68. available: 5,
  69. total: 100,
  70. })
  71. this.ProjectPersistenceManager.EXPIRY_TIMEOUT = 500
  72. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  73. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(500)
  74. done()
  75. })
  76. })
  77. it('should not modify EXPIRY_TIMEOUT if there is an error getting disk values', function (done) {
  78. this.diskusage.check.throws(new Error())
  79. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  80. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(1000)
  81. done()
  82. })
  83. })
  84. })
  85. describe('clearExpiredProjects', function () {
  86. beforeEach(function () {
  87. this.project_ids = ['project-id-1', 'project-id-2']
  88. this.ProjectPersistenceManager._findExpiredProjectIds = sinon
  89. .stub()
  90. .callsArgWith(0, null, this.project_ids)
  91. this.ProjectPersistenceManager.clearProjectFromCache = sinon
  92. .stub()
  93. .callsArg(1)
  94. this.CompileManager.clearExpiredProjects = sinon.stub().callsArg(1)
  95. return this.ProjectPersistenceManager.clearExpiredProjects(this.callback)
  96. })
  97. it('should clear each expired project', function () {
  98. return Array.from(this.project_ids).map(project_id =>
  99. this.ProjectPersistenceManager.clearProjectFromCache
  100. .calledWith(project_id)
  101. .should.equal(true)
  102. )
  103. })
  104. return it('should call the callback', function () {
  105. return this.callback.called.should.equal(true)
  106. })
  107. })
  108. return describe('clearProject', function () {
  109. beforeEach(function () {
  110. this.ProjectPersistenceManager._clearProjectFromDatabase = sinon
  111. .stub()
  112. .callsArg(1)
  113. this.UrlCache.clearProject = sinon.stub().callsArg(1)
  114. this.CompileManager.clearProject = sinon.stub().callsArg(2)
  115. return this.ProjectPersistenceManager.clearProject(
  116. this.project_id,
  117. this.user_id,
  118. this.callback
  119. )
  120. })
  121. it('should clear the project from the database', function () {
  122. return this.ProjectPersistenceManager._clearProjectFromDatabase
  123. .calledWith(this.project_id)
  124. .should.equal(true)
  125. })
  126. it('should clear all the cached Urls for the project', function () {
  127. return this.UrlCache.clearProject
  128. .calledWith(this.project_id)
  129. .should.equal(true)
  130. })
  131. it('should clear the project compile folder', function () {
  132. return this.CompileManager.clearProject
  133. .calledWith(this.project_id, this.user_id)
  134. .should.equal(true)
  135. })
  136. return it('should call the callback', function () {
  137. return this.callback.called.should.equal(true)
  138. })
  139. })
  140. })