ClsiFormatCheckerTests.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * 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 sinon = require('sinon')
  15. const { expect } = require('chai')
  16. const modulePath = '../../../../app/src/Features/Compile/ClsiFormatChecker.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. describe('ClsiFormatChecker', function () {
  19. beforeEach(function () {
  20. this.ClsiFormatChecker = SandboxedModule.require(modulePath, {
  21. requires: {
  22. '@overleaf/settings': (this.settings = {
  23. compileBodySizeLimitMb: 5,
  24. }),
  25. },
  26. })
  27. return (this.project_id = 'project-id')
  28. })
  29. describe('checkRecoursesForProblems', function () {
  30. beforeEach(function () {
  31. return (this.resources = [
  32. {
  33. path: 'main.tex',
  34. content: 'stuff',
  35. },
  36. {
  37. path: 'chapters/chapter1',
  38. content: 'other stuff',
  39. },
  40. {
  41. path: 'stuff/image/image.png',
  42. url: `http:somewhere.com/project/${this.project_id}/file/1234124321312`,
  43. modified: 'more stuff',
  44. },
  45. ])
  46. })
  47. it('should call _checkDocsAreUnderSizeLimit and _checkForConflictingPaths', async function () {
  48. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  49. .stub()
  50. .callsArgWith(1, null)
  51. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  52. .stub()
  53. .callsArgWith(1)
  54. const problems =
  55. await this.ClsiFormatChecker.promises.checkRecoursesForProblems(
  56. this.resources
  57. )
  58. this.ClsiFormatChecker._checkForConflictingPaths.called.should.equal(true)
  59. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit.called.should.equal(
  60. true
  61. )
  62. })
  63. it('should remove undefined errors', async function () {
  64. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  65. .stub()
  66. .callsArgWith(1, null, [])
  67. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  68. .stub()
  69. .callsArgWith(1, null, {})
  70. const problems =
  71. await this.ClsiFormatChecker.promises.checkRecoursesForProblems(
  72. this.resources
  73. )
  74. expect(problems).to.not.exist
  75. })
  76. it('should keep populated arrays', async function () {
  77. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  78. .stub()
  79. .callsArgWith(1, null, [{ path: 'somewhere/main.tex' }])
  80. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  81. .stub()
  82. .callsArgWith(1, null, {})
  83. const problems =
  84. await this.ClsiFormatChecker.promises.checkRecoursesForProblems(
  85. this.resources
  86. )
  87. problems.conflictedPaths[0].path.should.equal('somewhere/main.tex')
  88. expect(problems.sizeCheck).to.not.exist
  89. })
  90. it('should keep populated object', async function () {
  91. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  92. .stub()
  93. .callsArgWith(1, null, [])
  94. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  95. .stub()
  96. .callsArgWith(1, null, {
  97. resources: [{ 'a.tex': 'a.tex' }, { 'b.tex': 'b.tex' }],
  98. totalSize: 1000000,
  99. })
  100. const problems =
  101. await this.ClsiFormatChecker.promises.checkRecoursesForProblems(
  102. this.resources
  103. )
  104. problems.sizeCheck.resources.length.should.equal(2)
  105. problems.sizeCheck.totalSize.should.equal(1000000)
  106. expect(problems.conflictedPaths).to.not.exist
  107. })
  108. describe('_checkForConflictingPaths', function () {
  109. beforeEach(function () {
  110. this.resources.push({
  111. path: 'chapters/chapter1.tex',
  112. content: 'other stuff',
  113. })
  114. return this.resources.push({
  115. path: 'chapters.tex',
  116. content: 'other stuff',
  117. })
  118. })
  119. it('should flag up when a nested file has folder with same subpath as file elsewhere', async function () {
  120. this.resources.push({
  121. path: 'stuff/image',
  122. url: 'http://somwhere.com',
  123. })
  124. const conflictPathErrors =
  125. await this.ClsiFormatChecker.promises._checkForConflictingPaths(
  126. this.resources
  127. )
  128. conflictPathErrors.length.should.equal(1)
  129. conflictPathErrors[0].path.should.equal('stuff/image')
  130. })
  131. it('should flag up when a root level file has folder with same subpath as file elsewhere', async function () {
  132. this.resources.push({
  133. path: 'stuff',
  134. content: 'other stuff',
  135. })
  136. const conflictPathErrors =
  137. await this.ClsiFormatChecker.promises._checkForConflictingPaths(
  138. this.resources
  139. )
  140. conflictPathErrors.length.should.equal(1)
  141. conflictPathErrors[0].path.should.equal('stuff')
  142. })
  143. it('should not flag up when the file is a substring of a path', async function () {
  144. this.resources.push({
  145. path: 'stuf',
  146. content: 'other stuff',
  147. })
  148. const conflictPathErrors =
  149. await this.ClsiFormatChecker.promises._checkForConflictingPaths(
  150. this.resources
  151. )
  152. conflictPathErrors.length.should.equal(0)
  153. })
  154. })
  155. describe('_checkDocsAreUnderSizeLimit', function () {
  156. it('should error when there is more than 5mb of data', async function () {
  157. this.resources.push({
  158. path: 'massive.tex',
  159. content: 'hello world'.repeat(833333), // over 5mb limit
  160. })
  161. while (this.resources.length < 20) {
  162. this.resources.push({
  163. path: 'chapters/chapter1.tex',
  164. url: 'http://somwhere.com',
  165. })
  166. }
  167. const sizeError =
  168. await this.ClsiFormatChecker.promises._checkDocsAreUnderSizeLimit(
  169. this.resources
  170. )
  171. sizeError.totalSize.should.equal(16 + 833333 * 11) // 16 is for earlier resources
  172. sizeError.resources.length.should.equal(10)
  173. sizeError.resources[0].path.should.equal('massive.tex')
  174. sizeError.resources[0].size.should.equal(833333 * 11)
  175. })
  176. it('should return nothing when project is correct size', async function () {
  177. this.resources.push({
  178. path: 'massive.tex',
  179. content: 'x'.repeat(2 * 1000 * 1000),
  180. })
  181. while (this.resources.length < 20) {
  182. this.resources.push({
  183. path: 'chapters/chapter1.tex',
  184. url: 'http://somwhere.com',
  185. })
  186. }
  187. const sizeError =
  188. await this.ClsiFormatChecker.promises._checkDocsAreUnderSizeLimit(
  189. this.resources
  190. )
  191. expect(sizeError).to.not.exist
  192. })
  193. })
  194. })
  195. })