ClsiStateManagerTests.coffee 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Compile/ClsiStateManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "ClsiStateManager", ->
  8. beforeEach ->
  9. @ClsiStateManager = SandboxedModule.require modulePath, requires:
  10. "settings-sharelatex": @settings = {}
  11. "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
  12. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
  13. @project = "project"
  14. @options = {"draft":true,"isAutoCompile":false}
  15. @callback = sinon.stub()
  16. describe "computeHash", ->
  17. beforeEach (done) ->
  18. @docs = [
  19. {path: "/main.tex", doc: {_id: "doc-id-1"}}
  20. {path: "/folder/sub.tex", doc: {_id: "doc-id-2"}}
  21. ]
  22. @files = [
  23. {path: "/figure.pdf", file: {_id: "file-id-1", rev: 123, created: "aaaaaa"}}
  24. {path: "/folder/fig2.pdf", file: {_id: "file-id-2", rev: 456, created: "bbbbbb"}}
  25. ]
  26. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub().callsArgWith(1, null, @docs, @files)
  27. @ClsiStateManager.computeHash @project, @options, (err, hash) =>
  28. @hash0 = hash
  29. done()
  30. describe "with a sample project", ->
  31. beforeEach ->
  32. @ClsiStateManager.computeHash @project, @options, @callback
  33. it "should call the callback with a hash value", ->
  34. @callback
  35. .calledWith(null, "21b1ab73aa3892bec452baf8ffa0956179e1880f")
  36. .should.equal true
  37. describe "when the files and docs are in a different order", ->
  38. beforeEach ->
  39. [@docs[0], @docs[1]] = [@docs[1], @docs[0]]
  40. [@files[0], @files[1]] = [@files[1], @files[0]]
  41. @ClsiStateManager.computeHash @project, @options, @callback
  42. it "should call the callback with the same hash value", ->
  43. @callback
  44. .calledWith(null, @hash0)
  45. .should.equal true
  46. describe "when a doc is renamed", ->
  47. beforeEach (done) ->
  48. @docs[0].path = "/new.tex"
  49. @ClsiStateManager.computeHash @project, @options, (err, hash) =>
  50. @hash1 = hash
  51. done()
  52. it "should call the callback with a different hash value", ->
  53. @callback
  54. .neverCalledWith(null, @hash0)
  55. .should.equal true
  56. describe "when a file is renamed", ->
  57. beforeEach (done) ->
  58. @files[0].path = "/newfigure.pdf"
  59. @ClsiStateManager.computeHash @project, @options, (err, hash) =>
  60. @hash1 = hash
  61. done()
  62. it "should call the callback with a different hash value", ->
  63. @callback
  64. .neverCalledWith(null, @hash0)
  65. .should.equal true
  66. describe "when a doc is added", ->
  67. beforeEach (done) ->
  68. @docs.push { path: "/newdoc.tex", doc: {_id: "newdoc-id"}}
  69. @ClsiStateManager.computeHash @project, @options, (err, hash) =>
  70. @hash1 = hash
  71. done()
  72. it "should call the callback with a different hash value", ->
  73. @callback
  74. .neverCalledWith(null, @hash0)
  75. .should.equal true
  76. describe "when a file is added", ->
  77. beforeEach (done) ->
  78. @files.push { path: "/newfile.tex", file: {_id: "newfile-id", rev: 123}}
  79. @ClsiStateManager.computeHash @project, @options, (err, hash) =>
  80. @hash1 = hash
  81. done()
  82. it "should call the callback with a different hash value", ->
  83. @callback
  84. .neverCalledWith(null, @hash0)
  85. .should.equal true
  86. describe "when a doc is removed", ->
  87. beforeEach (done) ->
  88. @docs.pop()
  89. @ClsiStateManager.computeHash @project, @options, (err, hash) =>
  90. @hash1 = hash
  91. done()
  92. it "should call the callback with a different hash value", ->
  93. @callback
  94. .neverCalledWith(null, @hash0)
  95. .should.equal true
  96. describe "when a file is removed", ->
  97. beforeEach (done) ->
  98. @files.pop()
  99. @ClsiStateManager.computeHash @project, @options, (err, hash) =>
  100. @hash1 = hash
  101. done()
  102. it "should call the callback with a different hash value", ->
  103. @callback
  104. .neverCalledWith(null, @hash0)
  105. .should.equal true
  106. describe "when a file's revision is updated", ->
  107. beforeEach (done) ->
  108. @files[0].file.rev++
  109. @ClsiStateManager.computeHash @project, @options, (err, hash) =>
  110. @hash1 = hash
  111. done()
  112. it "should call the callback with a different hash value", ->
  113. @callback
  114. .neverCalledWith(null, @hash0)
  115. .should.equal true
  116. describe "when a file's date is updated", ->
  117. beforeEach (done) ->
  118. @files[0].file.created = "zzzzzz"
  119. @ClsiStateManager.computeHash @project, @options, (err, hash) =>
  120. @hash1 = hash
  121. done()
  122. it "should call the callback with a different hash value", ->
  123. @callback
  124. .neverCalledWith(null, @hash0)
  125. .should.equal true
  126. describe "when the compile options are changed", ->
  127. beforeEach (done) ->
  128. @options.draft = !@options.draft
  129. @ClsiStateManager.computeHash @project, @options, (err, hash) =>
  130. @hash1 = hash
  131. done()
  132. it "should call the callback with a different hash value", ->
  133. @callback
  134. .neverCalledWith(null, @hash0)
  135. .should.equal true
  136. describe "when the isAutoCompile option is changed", ->
  137. beforeEach () ->
  138. @options.isAutoCompile = !@options.isAutoCompile
  139. @ClsiStateManager.computeHash @project, @options, @callback
  140. it "should call the callback with the same hash value", ->
  141. @callback
  142. .calledWith(null, @hash0)
  143. .should.equal true