ClsiStateManager.test.mjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  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. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon')
  16. const { expect } = require('chai')
  17. const modulePath = '../../../../app/src/Features/Compile/ClsiStateManager.js'
  18. const SandboxedModule = require('sandboxed-module')
  19. describe('ClsiStateManager', function () {
  20. beforeEach(function () {
  21. this.ClsiStateManager = SandboxedModule.require(modulePath, {
  22. requires: {
  23. '@overleaf/settings': (this.settings = {}),
  24. '../Project/ProjectEntityHandler': (this.ProjectEntityHandler = {}),
  25. },
  26. })
  27. this.project = 'project'
  28. this.options = { draft: true, isAutoCompile: false }
  29. return (this.callback = sinon.stub())
  30. })
  31. describe('computeHash', function () {
  32. beforeEach(function () {
  33. this.docs = [
  34. { path: '/main.tex', doc: { _id: 'doc-id-1' } },
  35. { path: '/folder/sub.tex', doc: { _id: 'doc-id-2' } },
  36. ]
  37. this.files = [
  38. {
  39. path: '/figure.pdf',
  40. file: { _id: 'file-id-1', rev: 123, created: 'aaaaaa' },
  41. },
  42. {
  43. path: '/folder/fig2.pdf',
  44. file: { _id: 'file-id-2', rev: 456, created: 'bbbbbb' },
  45. },
  46. ]
  47. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  48. .stub()
  49. .returns({ docs: this.docs, files: this.files })
  50. this.hash0 = this.ClsiStateManager.computeHash(this.project, this.options)
  51. })
  52. describe('with a sample project', function () {
  53. beforeEach(function () {})
  54. it('should return a hash value', function () {
  55. expect(
  56. this.ClsiStateManager.computeHash(this.project, this.options)
  57. ).to.equal('21b1ab73aa3892bec452baf8ffa0956179e1880f')
  58. })
  59. })
  60. describe('when the files and docs are in a different order', function () {
  61. beforeEach(function () {
  62. ;[this.docs[0], this.docs[1]] = Array.from([this.docs[1], this.docs[0]])
  63. ;[this.files[0], this.files[1]] = Array.from([
  64. this.files[1],
  65. this.files[0],
  66. ])
  67. })
  68. it('should return the same hash value', function () {
  69. expect(
  70. this.ClsiStateManager.computeHash(this.project, this.options)
  71. ).to.equal(this.hash0)
  72. })
  73. })
  74. describe('when a doc is renamed', function () {
  75. beforeEach(function () {
  76. this.docs[0].path = '/new.tex'
  77. })
  78. it('should return a different hash value', function () {
  79. expect(
  80. this.ClsiStateManager.computeHash(this.project, this.options)
  81. ).not.to.equal(this.hash0)
  82. })
  83. })
  84. describe('when a file is renamed', function () {
  85. beforeEach(function () {
  86. this.files[0].path = '/newfigure.pdf'
  87. })
  88. it('should return a different hash value', function () {
  89. expect(
  90. this.ClsiStateManager.computeHash(this.project, this.options)
  91. ).not.to.equal(this.hash0)
  92. })
  93. })
  94. describe('when a doc is added', function () {
  95. beforeEach(function () {
  96. this.docs.push({ path: '/newdoc.tex', doc: { _id: 'newdoc-id' } })
  97. })
  98. it('should return a different hash value', function () {
  99. expect(
  100. this.ClsiStateManager.computeHash(this.project, this.options)
  101. ).not.to.equal(this.hash0)
  102. })
  103. })
  104. describe('when a file is added', function () {
  105. beforeEach(function () {
  106. this.files.push({
  107. path: '/newfile.tex',
  108. file: { _id: 'newfile-id', rev: 123 },
  109. })
  110. })
  111. it('should return a different hash value', function () {
  112. expect(
  113. this.ClsiStateManager.computeHash(this.project, this.options)
  114. ).not.to.equal(this.hash0)
  115. })
  116. })
  117. describe('when a doc is removed', function () {
  118. beforeEach(function () {
  119. this.docs.pop()
  120. })
  121. it('should return a different hash value', function () {
  122. expect(
  123. this.ClsiStateManager.computeHash(this.project, this.options)
  124. ).not.to.equal(this.hash0)
  125. })
  126. })
  127. describe('when a file is removed', function () {
  128. beforeEach(function () {
  129. this.files.pop()
  130. })
  131. it('should return a different hash value', function () {
  132. expect(
  133. this.ClsiStateManager.computeHash(this.project, this.options)
  134. ).not.to.equal(this.hash0)
  135. })
  136. })
  137. describe("when a file's revision is updated", function () {
  138. beforeEach(function () {
  139. this.files[0].file.rev++
  140. })
  141. it('should return a different hash value', function () {
  142. expect(
  143. this.ClsiStateManager.computeHash(this.project, this.options)
  144. ).not.to.equal(this.hash0)
  145. })
  146. })
  147. describe("when a file's date is updated", function () {
  148. beforeEach(function () {
  149. this.files[0].file.created = 'zzzzzz'
  150. })
  151. it('should return a different hash value', function () {
  152. expect(
  153. this.ClsiStateManager.computeHash(this.project, this.options)
  154. ).not.to.equal(this.hash0)
  155. })
  156. })
  157. describe('when the compile options are changed', function () {
  158. beforeEach(function () {
  159. this.options.draft = !this.options.draft
  160. })
  161. it('should return a different hash value', function () {
  162. expect(
  163. this.ClsiStateManager.computeHash(this.project, this.options)
  164. ).not.to.equal(this.hash0)
  165. })
  166. })
  167. describe('when the isAutoCompile option is changed', function () {
  168. beforeEach(function () {
  169. this.options.isAutoCompile = !this.options.isAutoCompile
  170. })
  171. it('should return the same hash value', function () {
  172. expect(
  173. this.ClsiStateManager.computeHash(this.project, this.options)
  174. ).to.equal(this.hash0)
  175. })
  176. })
  177. })
  178. })