ClsiStateManagerTests.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. @callback = sinon.stub()
  15. describe "computeHash", ->
  16. beforeEach (done) ->
  17. @docs = [
  18. {path: "/main.tex", doc: {_id: "doc-id-1"}}
  19. {path: "/folder/sub.tex", doc: {_id: "doc-id-2"}}
  20. ]
  21. @files = [
  22. {path: "/figure.pdf", file: {_id: "file-id-1", rev: 123, created: "aaaaaa"}}
  23. {path: "/folder/fig2.pdf", file: {_id: "file-id-2", rev: 456, created: "bbbbbb"}}
  24. ]
  25. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub().callsArgWith(1, null, @docs, @files)
  26. @ClsiStateManager.computeHash @project, (err, hash) =>
  27. @hash0 = hash
  28. done()
  29. describe "with a sample project", ->
  30. beforeEach ->
  31. @ClsiStateManager.computeHash @project, @callback
  32. it "should call the callback with a hash value", ->
  33. @callback
  34. .calledWith(null, "9c2c2428e4147db63cacabf6f357af483af6551d")
  35. .should.equal true
  36. describe "when the files and docs are in a different order", ->
  37. beforeEach ->
  38. [@docs[0], @docs[1]] = [@docs[1], @docs[0]]
  39. [@files[0], @files[1]] = [@files[1], @files[0]]
  40. @ClsiStateManager.computeHash @project, @callback
  41. it "should call the callback with the same hash value", ->
  42. @callback
  43. .calledWith(null, @hash0)
  44. .should.equal true
  45. describe "when a doc is renamed", ->
  46. beforeEach (done) ->
  47. @docs[0].path = "/new.tex"
  48. @ClsiStateManager.computeHash @project, (err, hash) =>
  49. @hash1 = hash
  50. done()
  51. it "should call the callback with a different hash value", ->
  52. @callback
  53. .neverCalledWith(null, @hash0)
  54. .should.equal true
  55. describe "when a file is renamed", ->
  56. beforeEach (done) ->
  57. @files[0].path = "/newfigure.pdf"
  58. @ClsiStateManager.computeHash @project, (err, hash) =>
  59. @hash1 = hash
  60. done()
  61. it "should call the callback with a different hash value", ->
  62. @callback
  63. .neverCalledWith(null, @hash0)
  64. .should.equal true
  65. describe "when a doc is added", ->
  66. beforeEach (done) ->
  67. @docs.push { path: "/newdoc.tex", doc: {_id: "newdoc-id"}}
  68. @ClsiStateManager.computeHash @project, (err, hash) =>
  69. @hash1 = hash
  70. done()
  71. it "should call the callback with a different hash value", ->
  72. @callback
  73. .neverCalledWith(null, @hash0)
  74. .should.equal true
  75. describe "when a file is added", ->
  76. beforeEach (done) ->
  77. @files.push { path: "/newfile.tex", file: {_id: "newfile-id", rev: 123}}
  78. @ClsiStateManager.computeHash @project, (err, hash) =>
  79. @hash1 = hash
  80. done()
  81. it "should call the callback with a different hash value", ->
  82. @callback
  83. .neverCalledWith(null, @hash0)
  84. .should.equal true
  85. describe "when a doc is removed", ->
  86. beforeEach (done) ->
  87. @docs.pop()
  88. @ClsiStateManager.computeHash @project, (err, hash) =>
  89. @hash1 = hash
  90. done()
  91. it "should call the callback with a different hash value", ->
  92. @callback
  93. .neverCalledWith(null, @hash0)
  94. .should.equal true
  95. describe "when a file is removed", ->
  96. beforeEach (done) ->
  97. @files.pop()
  98. @ClsiStateManager.computeHash @project, (err, hash) =>
  99. @hash1 = hash
  100. done()
  101. it "should call the callback with a different hash value", ->
  102. @callback
  103. .neverCalledWith(null, @hash0)
  104. .should.equal true
  105. describe "when a file's revision is updated", ->
  106. beforeEach (done) ->
  107. @files[0].file.rev++
  108. @ClsiStateManager.computeHash @project, (err, hash) =>
  109. @hash1 = hash
  110. done()
  111. it "should call the callback with a different hash value", ->
  112. @callback
  113. .neverCalledWith(null, @hash0)
  114. .should.equal true
  115. describe "when a file's date is updated", ->
  116. beforeEach (done) ->
  117. @files[0].file.created = "zzzzzz"
  118. @ClsiStateManager.computeHash @project, (err, hash) =>
  119. @hash1 = hash
  120. done()
  121. it "should call the callback with a different hash value", ->
  122. @callback
  123. .neverCalledWith(null, @hash0)
  124. .should.equal true