ProjectRootDocManagerTests.coffee 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. chai = require('chai')
  2. should = chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../../app/js/Features/Project/ProjectRootDocManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe 'ProjectRootDocManager', ->
  7. beforeEach ->
  8. @project_id = "project-123"
  9. @sl_req_id = "sl-req-id-123"
  10. @callback = sinon.stub()
  11. @ProjectRootDocManager = SandboxedModule.require modulePath, requires:
  12. "./ProjectEntityHandler" : @ProjectEntityHandler = {}
  13. "./ProjectEntityUpdateHandler" : @ProjectEntityUpdateHandler = {}
  14. "./ProjectGetter" : @ProjectGetter = {}
  15. describe "setRootDocAutomatically", ->
  16. describe "when there is a suitable root doc", ->
  17. beforeEach (done)->
  18. @docs =
  19. "/chapter1.tex":
  20. _id: "doc-id-1"
  21. lines: ["something else","\\begin{document}", "Hello world", "\\end{document}"]
  22. "/main.tex":
  23. _id: "doc-id-2"
  24. lines: ["different line","\\documentclass{article}", "\\input{chapter1}"]
  25. "/nested/chapter1a.tex":
  26. _id: "doc-id-3"
  27. lines: ["Hello world"]
  28. "/nested/chapter1b.tex":
  29. _id: "doc-id-4"
  30. lines: ["Hello world"]
  31. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  32. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  33. @ProjectRootDocManager.setRootDocAutomatically @project_id, done
  34. it "should check the docs of the project", ->
  35. @ProjectEntityHandler.getAllDocs.calledWith(@project_id)
  36. .should.equal true
  37. it "should set the root doc to the doc containing a documentclass", ->
  38. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  39. .should.equal true
  40. describe "when the root doc is an Rtex file", ->
  41. beforeEach ->
  42. @docs =
  43. "/chapter1.tex":
  44. _id: "doc-id-1"
  45. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  46. "/main.Rtex":
  47. _id: "doc-id-2"
  48. lines: ["\\documentclass{article}", "\\input{chapter1}"]
  49. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  50. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  51. @ProjectRootDocManager.setRootDocAutomatically @project_id, @callback
  52. it "should set the root doc to the doc containing a documentclass", ->
  53. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  54. .should.equal true
  55. describe "when there is no suitable root doc", ->
  56. beforeEach (done)->
  57. @docs =
  58. "/chapter1.tex":
  59. _id: "doc-id-1"
  60. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  61. "/style.bst":
  62. _id: "doc-id-2"
  63. lines: ["%Example: \\documentclass{article}"]
  64. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  65. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  66. @ProjectRootDocManager.setRootDocAutomatically @project_id, done
  67. it "should not set the root doc to the doc containing a documentclass", ->
  68. @ProjectEntityUpdateHandler.setRootDoc.called.should.equal false
  69. describe "setRootDocFromName", ->
  70. describe "when there is a suitable root doc", ->
  71. beforeEach (done)->
  72. @docPaths =
  73. "doc-id-1": "/chapter1.tex"
  74. "doc-id-2": "/main.tex"
  75. "doc-id-3": "/nested/chapter1a.tex"
  76. "doc-id-4": "/nested/chapter1b.tex"
  77. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  78. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  79. @ProjectRootDocManager.setRootDocFromName @project_id, '/main.tex', done
  80. it "should check the docs of the project", ->
  81. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  82. .should.equal true
  83. it "should set the root doc to main.tex", ->
  84. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  85. .should.equal true
  86. describe "when there is a suitable root doc but the leading slash is missing", ->
  87. beforeEach (done)->
  88. @docPaths =
  89. "doc-id-1": "/chapter1.tex"
  90. "doc-id-2": "/main.tex"
  91. "doc-id-3": "/nested/chapter1a.tex"
  92. "doc-id-4": "/nested/chapter1b.tex"
  93. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  94. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  95. @ProjectRootDocManager.setRootDocFromName @project_id, 'main.tex', done
  96. it "should check the docs of the project", ->
  97. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  98. .should.equal true
  99. it "should set the root doc to main.tex", ->
  100. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  101. .should.equal true
  102. describe "when there is a suitable root doc with a basename match", ->
  103. beforeEach (done)->
  104. @docPaths =
  105. "doc-id-1": "/chapter1.tex"
  106. "doc-id-2": "/main.tex"
  107. "doc-id-3": "/nested/chapter1a.tex"
  108. "doc-id-4": "/nested/chapter1b.tex"
  109. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  110. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  111. @ProjectRootDocManager.setRootDocFromName @project_id, 'chapter1a.tex', done
  112. it "should check the docs of the project", ->
  113. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  114. .should.equal true
  115. it "should set the root doc using the basename", ->
  116. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-3")
  117. .should.equal true
  118. describe "when there is a suitable root doc but the filename is in quotes", ->
  119. beforeEach (done)->
  120. @docPaths =
  121. "doc-id-1": "/chapter1.tex"
  122. "doc-id-2": "/main.tex"
  123. "doc-id-3": "/nested/chapter1a.tex"
  124. "doc-id-4": "/nested/chapter1b.tex"
  125. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  126. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  127. @ProjectRootDocManager.setRootDocFromName @project_id, "'main.tex'", done
  128. it "should check the docs of the project", ->
  129. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  130. .should.equal true
  131. it "should set the root doc to main.tex", ->
  132. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  133. .should.equal true
  134. describe "when there is no suitable root doc", ->
  135. beforeEach (done)->
  136. @docPaths =
  137. "doc-id-1": "/chapter1.tex"
  138. "doc-id-2": "/main.tex"
  139. "doc-id-3": "/nested/chapter1a.tex"
  140. "doc-id-4": "/nested/chapter1b.tex"
  141. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  142. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  143. @ProjectRootDocManager.setRootDocFromName @project_id, "other.tex", done
  144. it "should not set the root doc", ->
  145. @ProjectEntityUpdateHandler.setRootDoc.called.should.equal false
  146. describe "ensureRootDocumentIsSet", ->
  147. beforeEach ->
  148. @project = {}
  149. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  150. @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArgWith(1, null)
  151. describe "when the root doc is set", ->
  152. beforeEach ->
  153. @project.rootDoc_id = "root-doc-id"
  154. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  155. it "should find the project with only the rootDoc_id fiel", ->
  156. @ProjectGetter.getProject
  157. .calledWith(@project_id, rootDoc_id: 1)
  158. .should.equal true
  159. it "should not try to update the project rootDoc_id", ->
  160. @ProjectRootDocManager.setRootDocAutomatically
  161. .called.should.equal false
  162. it "should call the callback", ->
  163. @callback.called.should.equal true
  164. describe "when the root doc is not set", ->
  165. beforeEach ->
  166. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  167. it "should find the project with only the rootDoc_id fiel", ->
  168. @ProjectGetter.getProject
  169. .calledWith(@project_id, rootDoc_id: 1)
  170. .should.equal true
  171. it "should update the project rootDoc_id", ->
  172. @ProjectRootDocManager.setRootDocAutomatically
  173. .calledWith(@project_id)
  174. .should.equal true
  175. it "should call the callback", ->
  176. @callback.called.should.equal true
  177. describe "when the project does not exist", ->
  178. beforeEach ->
  179. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, null)
  180. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  181. it "should call the callback with an error", ->
  182. @callback.calledWith(new Error("project not found")).should.equal true