ClsiFormatCheckerTests.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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', function (done) {
  48. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  49. .stub()
  50. .callsArgWith(1, null)
  51. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  52. .stub()
  53. .callsArgWith(1)
  54. return this.ClsiFormatChecker.checkRecoursesForProblems(
  55. this.resources,
  56. (err, problems) => {
  57. this.ClsiFormatChecker._checkForConflictingPaths.called.should.equal(
  58. true
  59. )
  60. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit.called.should.equal(
  61. true
  62. )
  63. return done()
  64. }
  65. )
  66. })
  67. it('should remove undefined errors', function (done) {
  68. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  69. .stub()
  70. .callsArgWith(1, null, [])
  71. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  72. .stub()
  73. .callsArgWith(1, null, {})
  74. return this.ClsiFormatChecker.checkRecoursesForProblems(
  75. this.resources,
  76. (err, problems) => {
  77. expect(problems).to.not.exist
  78. expect(problems).to.not.exist
  79. return done()
  80. }
  81. )
  82. })
  83. it('should keep populated arrays', function (done) {
  84. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  85. .stub()
  86. .callsArgWith(1, null, [{ path: 'somewhere/main.tex' }])
  87. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  88. .stub()
  89. .callsArgWith(1, null, {})
  90. return this.ClsiFormatChecker.checkRecoursesForProblems(
  91. this.resources,
  92. (err, problems) => {
  93. problems.conflictedPaths[0].path.should.equal('somewhere/main.tex')
  94. expect(problems.sizeCheck).to.not.exist
  95. return done()
  96. }
  97. )
  98. })
  99. it('should keep populated object', function (done) {
  100. this.ClsiFormatChecker._checkForConflictingPaths = sinon
  101. .stub()
  102. .callsArgWith(1, null, [])
  103. this.ClsiFormatChecker._checkDocsAreUnderSizeLimit = sinon
  104. .stub()
  105. .callsArgWith(1, null, {
  106. resources: [{ 'a.tex': 'a.tex' }, { 'b.tex': 'b.tex' }],
  107. totalSize: 1000000,
  108. })
  109. return this.ClsiFormatChecker.checkRecoursesForProblems(
  110. this.resources,
  111. (err, problems) => {
  112. problems.sizeCheck.resources.length.should.equal(2)
  113. problems.sizeCheck.totalSize.should.equal(1000000)
  114. expect(problems.conflictedPaths).to.not.exist
  115. return done()
  116. }
  117. )
  118. })
  119. describe('_checkForConflictingPaths', function () {
  120. beforeEach(function () {
  121. this.resources.push({
  122. path: 'chapters/chapter1.tex',
  123. content: 'other stuff',
  124. })
  125. return this.resources.push({
  126. path: 'chapters.tex',
  127. content: 'other stuff',
  128. })
  129. })
  130. it('should flag up when a nested file has folder with same subpath as file elsewhere', function (done) {
  131. this.resources.push({
  132. path: 'stuff/image',
  133. url: 'http://somwhere.com',
  134. })
  135. return this.ClsiFormatChecker._checkForConflictingPaths(
  136. this.resources,
  137. (err, conflictPathErrors) => {
  138. conflictPathErrors.length.should.equal(1)
  139. conflictPathErrors[0].path.should.equal('stuff/image')
  140. return done()
  141. }
  142. )
  143. })
  144. it('should flag up when a root level file has folder with same subpath as file elsewhere', function (done) {
  145. this.resources.push({
  146. path: 'stuff',
  147. content: 'other stuff',
  148. })
  149. return this.ClsiFormatChecker._checkForConflictingPaths(
  150. this.resources,
  151. (err, conflictPathErrors) => {
  152. conflictPathErrors.length.should.equal(1)
  153. conflictPathErrors[0].path.should.equal('stuff')
  154. return done()
  155. }
  156. )
  157. })
  158. it('should not flag up when the file is a substring of a path', function (done) {
  159. this.resources.push({
  160. path: 'stuf',
  161. content: 'other stuff',
  162. })
  163. return this.ClsiFormatChecker._checkForConflictingPaths(
  164. this.resources,
  165. (err, conflictPathErrors) => {
  166. conflictPathErrors.length.should.equal(0)
  167. return done()
  168. }
  169. )
  170. })
  171. })
  172. describe('_checkDocsAreUnderSizeLimit', function () {
  173. it('should error when there is more than 5mb of data', function (done) {
  174. this.resources.push({
  175. path: 'massive.tex',
  176. content: 'hello world'.repeat(833333), // over 5mb limit
  177. })
  178. while (this.resources.length < 20) {
  179. this.resources.push({
  180. path: 'chapters/chapter1.tex',
  181. url: 'http://somwhere.com',
  182. })
  183. }
  184. return this.ClsiFormatChecker._checkDocsAreUnderSizeLimit(
  185. this.resources,
  186. (err, sizeError) => {
  187. sizeError.totalSize.should.equal(16 + 833333 * 11) // 16 is for earlier resources
  188. sizeError.resources.length.should.equal(10)
  189. sizeError.resources[0].path.should.equal('massive.tex')
  190. sizeError.resources[0].size.should.equal(833333 * 11)
  191. return done()
  192. }
  193. )
  194. })
  195. it('should return nothing when project is correct size', function (done) {
  196. this.resources.push({
  197. path: 'massive.tex',
  198. content: 'x'.repeat(2 * 1000 * 1000),
  199. })
  200. while (this.resources.length < 20) {
  201. this.resources.push({
  202. path: 'chapters/chapter1.tex',
  203. url: 'http://somwhere.com',
  204. })
  205. }
  206. return this.ClsiFormatChecker._checkDocsAreUnderSizeLimit(
  207. this.resources,
  208. (err, sizeError) => {
  209. expect(sizeError).to.not.exist
  210. return done()
  211. }
  212. )
  213. })
  214. })
  215. })
  216. })