ProjectPersistenceManagerTests.js 4.9 KB

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