ClsiStateManagerTests.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* eslint-disable
  2. 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 chai = require('chai')
  17. const should = chai.should()
  18. const { expect } = chai
  19. const modulePath = '../../../../app/src/Features/Compile/ClsiStateManager.js'
  20. const SandboxedModule = require('sandboxed-module')
  21. describe('ClsiStateManager', function() {
  22. beforeEach(function() {
  23. this.ClsiStateManager = SandboxedModule.require(modulePath, {
  24. globals: {
  25. console: console
  26. },
  27. requires: {
  28. 'settings-sharelatex': (this.settings = {}),
  29. '../Project/ProjectEntityHandler': (this.ProjectEntityHandler = {}),
  30. 'logger-sharelatex': (this.logger = {
  31. log: sinon.stub(),
  32. error: sinon.stub(),
  33. warn: sinon.stub()
  34. })
  35. }
  36. })
  37. this.project = 'project'
  38. this.options = { draft: true, isAutoCompile: false }
  39. return (this.callback = sinon.stub())
  40. })
  41. describe('computeHash', function() {
  42. beforeEach(function(done) {
  43. this.docs = [
  44. { path: '/main.tex', doc: { _id: 'doc-id-1' } },
  45. { path: '/folder/sub.tex', doc: { _id: 'doc-id-2' } }
  46. ]
  47. this.files = [
  48. {
  49. path: '/figure.pdf',
  50. file: { _id: 'file-id-1', rev: 123, created: 'aaaaaa' }
  51. },
  52. {
  53. path: '/folder/fig2.pdf',
  54. file: { _id: 'file-id-2', rev: 456, created: 'bbbbbb' }
  55. }
  56. ]
  57. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  58. .stub()
  59. .callsArgWith(1, null, this.docs, this.files)
  60. return this.ClsiStateManager.computeHash(
  61. this.project,
  62. this.options,
  63. (err, hash) => {
  64. this.hash0 = hash
  65. return done()
  66. }
  67. )
  68. })
  69. describe('with a sample project', function() {
  70. beforeEach(function() {
  71. return this.ClsiStateManager.computeHash(
  72. this.project,
  73. this.options,
  74. this.callback
  75. )
  76. })
  77. it('should call the callback with a hash value', function() {
  78. return this.callback
  79. .calledWith(null, '21b1ab73aa3892bec452baf8ffa0956179e1880f')
  80. .should.equal(true)
  81. })
  82. })
  83. describe('when the files and docs are in a different order', function() {
  84. beforeEach(function() {
  85. ;[this.docs[0], this.docs[1]] = Array.from([this.docs[1], this.docs[0]])
  86. ;[this.files[0], this.files[1]] = Array.from([
  87. this.files[1],
  88. this.files[0]
  89. ])
  90. return this.ClsiStateManager.computeHash(
  91. this.project,
  92. this.options,
  93. this.callback
  94. )
  95. })
  96. it('should call the callback with the same hash value', function() {
  97. return this.callback.calledWith(null, this.hash0).should.equal(true)
  98. })
  99. })
  100. describe('when a doc is renamed', function() {
  101. beforeEach(function(done) {
  102. this.docs[0].path = '/new.tex'
  103. return this.ClsiStateManager.computeHash(
  104. this.project,
  105. this.options,
  106. (err, hash) => {
  107. this.hash1 = hash
  108. return done()
  109. }
  110. )
  111. })
  112. it('should call the callback with a different hash value', function() {
  113. return this.callback
  114. .neverCalledWith(null, this.hash0)
  115. .should.equal(true)
  116. })
  117. })
  118. describe('when a file is renamed', function() {
  119. beforeEach(function(done) {
  120. this.files[0].path = '/newfigure.pdf'
  121. return this.ClsiStateManager.computeHash(
  122. this.project,
  123. this.options,
  124. (err, hash) => {
  125. this.hash1 = hash
  126. return done()
  127. }
  128. )
  129. })
  130. it('should call the callback with a different hash value', function() {
  131. return this.callback
  132. .neverCalledWith(null, this.hash0)
  133. .should.equal(true)
  134. })
  135. })
  136. describe('when a doc is added', function() {
  137. beforeEach(function(done) {
  138. this.docs.push({ path: '/newdoc.tex', doc: { _id: 'newdoc-id' } })
  139. return this.ClsiStateManager.computeHash(
  140. this.project,
  141. this.options,
  142. (err, hash) => {
  143. this.hash1 = hash
  144. return done()
  145. }
  146. )
  147. })
  148. it('should call the callback with a different hash value', function() {
  149. return this.callback
  150. .neverCalledWith(null, this.hash0)
  151. .should.equal(true)
  152. })
  153. })
  154. describe('when a file is added', function() {
  155. beforeEach(function(done) {
  156. this.files.push({
  157. path: '/newfile.tex',
  158. file: { _id: 'newfile-id', rev: 123 }
  159. })
  160. return this.ClsiStateManager.computeHash(
  161. this.project,
  162. this.options,
  163. (err, hash) => {
  164. this.hash1 = hash
  165. return done()
  166. }
  167. )
  168. })
  169. it('should call the callback with a different hash value', function() {
  170. return this.callback
  171. .neverCalledWith(null, this.hash0)
  172. .should.equal(true)
  173. })
  174. })
  175. describe('when a doc is removed', function() {
  176. beforeEach(function(done) {
  177. this.docs.pop()
  178. return this.ClsiStateManager.computeHash(
  179. this.project,
  180. this.options,
  181. (err, hash) => {
  182. this.hash1 = hash
  183. return done()
  184. }
  185. )
  186. })
  187. it('should call the callback with a different hash value', function() {
  188. return this.callback
  189. .neverCalledWith(null, this.hash0)
  190. .should.equal(true)
  191. })
  192. })
  193. describe('when a file is removed', function() {
  194. beforeEach(function(done) {
  195. this.files.pop()
  196. return this.ClsiStateManager.computeHash(
  197. this.project,
  198. this.options,
  199. (err, hash) => {
  200. this.hash1 = hash
  201. return done()
  202. }
  203. )
  204. })
  205. it('should call the callback with a different hash value', function() {
  206. return this.callback
  207. .neverCalledWith(null, this.hash0)
  208. .should.equal(true)
  209. })
  210. })
  211. describe("when a file's revision is updated", function() {
  212. beforeEach(function(done) {
  213. this.files[0].file.rev++
  214. return this.ClsiStateManager.computeHash(
  215. this.project,
  216. this.options,
  217. (err, hash) => {
  218. this.hash1 = hash
  219. return done()
  220. }
  221. )
  222. })
  223. it('should call the callback with a different hash value', function() {
  224. return this.callback
  225. .neverCalledWith(null, this.hash0)
  226. .should.equal(true)
  227. })
  228. })
  229. describe("when a file's date is updated", function() {
  230. beforeEach(function(done) {
  231. this.files[0].file.created = 'zzzzzz'
  232. return this.ClsiStateManager.computeHash(
  233. this.project,
  234. this.options,
  235. (err, hash) => {
  236. this.hash1 = hash
  237. return done()
  238. }
  239. )
  240. })
  241. it('should call the callback with a different hash value', function() {
  242. return this.callback
  243. .neverCalledWith(null, this.hash0)
  244. .should.equal(true)
  245. })
  246. })
  247. describe('when the compile options are changed', function() {
  248. beforeEach(function(done) {
  249. this.options.draft = !this.options.draft
  250. return this.ClsiStateManager.computeHash(
  251. this.project,
  252. this.options,
  253. (err, hash) => {
  254. this.hash1 = hash
  255. return done()
  256. }
  257. )
  258. })
  259. it('should call the callback with a different hash value', function() {
  260. return this.callback
  261. .neverCalledWith(null, this.hash0)
  262. .should.equal(true)
  263. })
  264. })
  265. describe('when the isAutoCompile option is changed', function() {
  266. beforeEach(function() {
  267. this.options.isAutoCompile = !this.options.isAutoCompile
  268. return this.ClsiStateManager.computeHash(
  269. this.project,
  270. this.options,
  271. this.callback
  272. )
  273. })
  274. it('should call the callback with the same hash value', function() {
  275. return this.callback.calledWith(null, this.hash0).should.equal(true)
  276. })
  277. })
  278. })
  279. })