ProjectRootDocManagerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. @docPaths =
  10. "doc-id-1": "/chapter1.tex"
  11. "doc-id-2": "/main.tex"
  12. "doc-id-3": "/nested/chapter1a.tex"
  13. "doc-id-4": "/nested/chapter1b.tex"
  14. @sl_req_id = "sl-req-id-123"
  15. @callback = sinon.stub()
  16. @ProjectRootDocManager = SandboxedModule.require modulePath, requires:
  17. "./ProjectEntityHandler" : @ProjectEntityHandler = {}
  18. "./ProjectEntityUpdateHandler" : @ProjectEntityUpdateHandler = {}
  19. "./ProjectGetter" : @ProjectGetter = {}
  20. describe "setRootDocAutomatically", ->
  21. describe "when there is a suitable root doc", ->
  22. beforeEach (done)->
  23. @docs =
  24. "/chapter1.tex":
  25. _id: "doc-id-1"
  26. lines: ["something else","\\begin{document}", "Hello world", "\\end{document}"]
  27. "/main.tex":
  28. _id: "doc-id-2"
  29. lines: ["different line","\\documentclass{article}", "\\input{chapter1}"]
  30. "/nested/chapter1a.tex":
  31. _id: "doc-id-3"
  32. lines: ["Hello world"]
  33. "/nested/chapter1b.tex":
  34. _id: "doc-id-4"
  35. lines: ["Hello world"]
  36. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  37. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  38. @ProjectRootDocManager.setRootDocAutomatically @project_id, done
  39. it "should check the docs of the project", ->
  40. @ProjectEntityHandler.getAllDocs.calledWith(@project_id)
  41. .should.equal true
  42. it "should set the root doc to the doc containing a documentclass", ->
  43. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  44. .should.equal true
  45. describe "when the root doc is an Rtex file", ->
  46. beforeEach ->
  47. @docs =
  48. "/chapter1.tex":
  49. _id: "doc-id-1"
  50. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  51. "/main.Rtex":
  52. _id: "doc-id-2"
  53. lines: ["\\documentclass{article}", "\\input{chapter1}"]
  54. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  55. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  56. @ProjectRootDocManager.setRootDocAutomatically @project_id, @callback
  57. it "should set the root doc to the doc containing a documentclass", ->
  58. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  59. .should.equal true
  60. describe "when there is no suitable root doc", ->
  61. beforeEach (done)->
  62. @docs =
  63. "/chapter1.tex":
  64. _id: "doc-id-1"
  65. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  66. "/style.bst":
  67. _id: "doc-id-2"
  68. lines: ["%Example: \\documentclass{article}"]
  69. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  70. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  71. @ProjectRootDocManager.setRootDocAutomatically @project_id, done
  72. it "should not set the root doc to the doc containing a documentclass", ->
  73. @ProjectEntityUpdateHandler.setRootDoc.called.should.equal false
  74. describe "setRootDocFromName", ->
  75. describe "when there is a suitable root doc", ->
  76. beforeEach (done)->
  77. @docPaths =
  78. "doc-id-1": "/chapter1.tex"
  79. "doc-id-2": "/main.tex"
  80. "doc-id-3": "/nested/chapter1a.tex"
  81. "doc-id-4": "/nested/chapter1b.tex"
  82. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  83. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  84. @ProjectRootDocManager.setRootDocFromName @project_id, '/main.tex', done
  85. it "should check the docs of the project", ->
  86. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  87. .should.equal true
  88. it "should set the root doc to main.tex", ->
  89. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  90. .should.equal true
  91. describe "when there is a suitable root doc but the leading slash is missing", ->
  92. beforeEach (done)->
  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. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  105. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  106. @ProjectRootDocManager.setRootDocFromName @project_id, 'chapter1a.tex', done
  107. it "should check the docs of the project", ->
  108. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  109. .should.equal true
  110. it "should set the root doc using the basename", ->
  111. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-3")
  112. .should.equal true
  113. describe "when there is a suitable root doc but the filename is in quotes", ->
  114. beforeEach (done)->
  115. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  116. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  117. @ProjectRootDocManager.setRootDocFromName @project_id, "'main.tex'", done
  118. it "should check the docs of the project", ->
  119. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  120. .should.equal true
  121. it "should set the root doc to main.tex", ->
  122. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  123. .should.equal true
  124. describe "when there is no suitable root doc", ->
  125. beforeEach (done)->
  126. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  127. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  128. @ProjectRootDocManager.setRootDocFromName @project_id, "other.tex", done
  129. it "should not set the root doc", ->
  130. @ProjectEntityUpdateHandler.setRootDoc.called.should.equal false
  131. describe "ensureRootDocumentIsSet", ->
  132. beforeEach ->
  133. @project = {}
  134. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  135. @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArgWith(1, null)
  136. describe "when the root doc is set", ->
  137. beforeEach ->
  138. @project.rootDoc_id = "root-doc-id"
  139. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  140. it "should find the project fetching only the rootDoc_id field", ->
  141. @ProjectGetter.getProject
  142. .calledWith(@project_id, rootDoc_id: 1)
  143. .should.equal true
  144. it "should not try to update the project rootDoc_id", ->
  145. @ProjectRootDocManager.setRootDocAutomatically
  146. .called.should.equal false
  147. it "should call the callback", ->
  148. @callback.called.should.equal true
  149. describe "when the root doc is not set", ->
  150. beforeEach ->
  151. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  152. it "should find the project with only the rootDoc_id fiel", ->
  153. @ProjectGetter.getProject
  154. .calledWith(@project_id, rootDoc_id: 1)
  155. .should.equal true
  156. it "should update the project rootDoc_id", ->
  157. @ProjectRootDocManager.setRootDocAutomatically
  158. .calledWith(@project_id)
  159. .should.equal true
  160. it "should call the callback", ->
  161. @callback.called.should.equal true
  162. describe "when the project does not exist", ->
  163. beforeEach ->
  164. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, null)
  165. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  166. it "should call the callback with an error", ->
  167. @callback.calledWith(new Error("project not found")).should.equal true
  168. describe "ensureRootDocumentIsValid", ->
  169. beforeEach ->
  170. @project = {}
  171. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  172. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().yields()
  173. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  174. @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArgWith(1, null)
  175. describe "when the root doc is set", ->
  176. describe "when the root doc is valid", ->
  177. beforeEach ->
  178. @project.rootDoc_id = "doc-id-2"
  179. @ProjectRootDocManager.ensureRootDocumentIsValid(@project_id, @callback)
  180. it "should find the project fetching only the rootDoc_id field", ->
  181. @ProjectGetter.getProject
  182. .calledWith(@project_id, rootDoc_id: 1)
  183. .should.equal true
  184. it "should not try to update the project rootDoc_id", ->
  185. @ProjectRootDocManager.setRootDocAutomatically
  186. .called.should.equal false
  187. it "should call the callback", ->
  188. @callback.called.should.equal true
  189. describe "when the root doc is not valid", ->
  190. beforeEach ->
  191. @project.rootDoc_id = "bogus-doc-id"
  192. @ProjectRootDocManager.ensureRootDocumentIsValid(@project_id, @callback)
  193. it "should find the project fetching only the rootDoc_id field", ->
  194. @ProjectGetter.getProject
  195. .calledWith(@project_id, rootDoc_id: 1)
  196. .should.equal true
  197. it "should null the rootDoc_id field", ->
  198. @ProjectEntityUpdateHandler.setRootDoc
  199. .calledWith(@project_id, null)
  200. .should.equal true
  201. it "should try to find a new rootDoc", ->
  202. @ProjectRootDocManager.setRootDocAutomatically
  203. .called.should.equal true
  204. it "should call the callback", ->
  205. @callback.called.should.equal true
  206. describe "when the root doc is not set", ->
  207. beforeEach ->
  208. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  209. it "should find the project fetching only the rootDoc_id fiel", ->
  210. @ProjectGetter.getProject
  211. .calledWith(@project_id, rootDoc_id: 1)
  212. .should.equal true
  213. it "should update the project rootDoc_id", ->
  214. @ProjectRootDocManager.setRootDocAutomatically
  215. .calledWith(@project_id)
  216. .should.equal true
  217. it "should call the callback", ->
  218. @callback.called.should.equal true
  219. describe "when the project does not exist", ->
  220. beforeEach ->
  221. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, null)
  222. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  223. it "should call the callback with an error", ->
  224. @callback.calledWith(new Error("project not found")).should.equal true