ProjectPersistenceManagerTests.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. './db': (this.db = {}),
  38. },
  39. })
  40. this.callback = sinon.stub()
  41. this.project_id = 'project-id-123'
  42. return (this.user_id = '1234')
  43. })
  44. describe('refreshExpiryTimeout', function () {
  45. it('should leave expiry alone if plenty of disk', function (done) {
  46. this.diskusage.check.resolves({
  47. available: 40,
  48. total: 100,
  49. })
  50. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  51. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(
  52. this.settings.project_cache_length_ms
  53. )
  54. done()
  55. })
  56. })
  57. it('should drop EXPIRY_TIMEOUT 10% if low disk usage', function (done) {
  58. this.diskusage.check.resolves({
  59. available: 5,
  60. total: 100,
  61. })
  62. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  63. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(900)
  64. done()
  65. })
  66. })
  67. it('should not drop EXPIRY_TIMEOUT to below 50% of project_cache_length_ms', function (done) {
  68. this.diskusage.check.resolves({
  69. available: 5,
  70. total: 100,
  71. })
  72. this.ProjectPersistenceManager.EXPIRY_TIMEOUT = 500
  73. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  74. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(500)
  75. done()
  76. })
  77. })
  78. it('should not modify EXPIRY_TIMEOUT if there is an error getting disk values', function (done) {
  79. this.diskusage.check.throws(new Error())
  80. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  81. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(1000)
  82. done()
  83. })
  84. })
  85. })
  86. describe('clearExpiredProjects', function () {
  87. beforeEach(function () {
  88. this.project_ids = ['project-id-1', 'project-id-2']
  89. this.ProjectPersistenceManager._findExpiredProjectIds = sinon
  90. .stub()
  91. .callsArgWith(0, null, this.project_ids)
  92. this.ProjectPersistenceManager.clearProjectFromCache = sinon
  93. .stub()
  94. .callsArg(1)
  95. this.CompileManager.clearExpiredProjects = sinon.stub().callsArg(1)
  96. return this.ProjectPersistenceManager.clearExpiredProjects(this.callback)
  97. })
  98. it('should clear each expired project', function () {
  99. return Array.from(this.project_ids).map(project_id =>
  100. this.ProjectPersistenceManager.clearProjectFromCache
  101. .calledWith(project_id)
  102. .should.equal(true)
  103. )
  104. })
  105. return it('should call the callback', function () {
  106. return this.callback.called.should.equal(true)
  107. })
  108. })
  109. return describe('clearProject', function () {
  110. beforeEach(function () {
  111. this.ProjectPersistenceManager._clearProjectFromDatabase = sinon
  112. .stub()
  113. .callsArg(1)
  114. this.UrlCache.clearProject = sinon.stub().callsArg(1)
  115. this.CompileManager.clearProject = sinon.stub().callsArg(2)
  116. return this.ProjectPersistenceManager.clearProject(
  117. this.project_id,
  118. this.user_id,
  119. this.callback
  120. )
  121. })
  122. it('should clear the project from the database', function () {
  123. return this.ProjectPersistenceManager._clearProjectFromDatabase
  124. .calledWith(this.project_id)
  125. .should.equal(true)
  126. })
  127. it('should clear all the cached Urls for the project', function () {
  128. return this.UrlCache.clearProject
  129. .calledWith(this.project_id)
  130. .should.equal(true)
  131. })
  132. it('should clear the project compile folder', function () {
  133. return this.CompileManager.clearProject
  134. .calledWith(this.project_id, this.user_id)
  135. .should.equal(true)
  136. })
  137. return it('should call the callback', function () {
  138. return this.callback.called.should.equal(true)
  139. })
  140. })
  141. })