MetaHandlerTests.coffee 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. chai = require('chai')
  2. chai.should()
  3. expect = chai.expect
  4. sinon = require("sinon")
  5. modulePath = "../../../../app/js/Features/Metadata/MetaHandler"
  6. SandboxedModule = require('sandboxed-module')
  7. describe 'MetaHandler', ->
  8. beforeEach ->
  9. @projectId = 'someprojectid'
  10. @docId = 'somedocid'
  11. @ProjectEntityHandler = {
  12. getAllDocs: sinon.stub()
  13. getDoc: sinon.stub()
  14. }
  15. @DocumentUpdaterHandler = {
  16. flushDocToMongo: sinon.stub()
  17. }
  18. @packageMapping =
  19. foo: [
  20. {
  21. caption: '\\bar'
  22. snippet: '\\bar'
  23. meta: 'foo-cmd'
  24. score: 12
  25. }, {
  26. caption: '\\bat[]{}'
  27. snippet: '\\bar[$1]{$2}'
  28. meta: 'foo-cmd'
  29. score: 10
  30. }
  31. ],
  32. baz: [
  33. {
  34. caption: '\\longercommandtest{}'
  35. snippet: '\\longercommandtest{$1}'
  36. meta: 'baz-cmd'
  37. score: 50
  38. }
  39. ]
  40. @MetaHandler = SandboxedModule.require modulePath, requires:
  41. '../Project/ProjectEntityHandler': @ProjectEntityHandler
  42. '../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler
  43. './packageMapping': @packageMapping
  44. describe 'extractMetaFromDoc', ->
  45. beforeEach ->
  46. @lines = [
  47. '\\usepackage{foo}'
  48. '\\usepackage{amsmath, booktabs}'
  49. 'one'
  50. 'two'
  51. 'three \\label{aaa}'
  52. 'four five'
  53. '\\label{bbb}'
  54. 'six seven'
  55. ]
  56. it 'should extract all the labels and packages', ->
  57. docMeta = @MetaHandler.extractMetaFromDoc @lines
  58. expect(docMeta).to.deep.equal {
  59. labels: ['aaa', 'bbb']
  60. packages:
  61. foo: [
  62. {
  63. caption: '\\bar'
  64. snippet: '\\bar'
  65. meta: 'foo-cmd'
  66. score: 12
  67. }, {
  68. caption: '\\bat[]{}'
  69. snippet: '\\bar[$1]{$2}'
  70. meta: 'foo-cmd'
  71. score: 10
  72. }
  73. ]
  74. }
  75. describe 'extractMetaFromProjectDocs', ->
  76. beforeEach ->
  77. @docs =
  78. 'doc_one':
  79. _id: 'id_one'
  80. lines: ['one', '\\label{aaa} two', 'three']
  81. 'doc_two':
  82. _id: 'id_two'
  83. lines: ['four']
  84. 'doc_three':
  85. _id: 'id_three'
  86. lines: [
  87. '\\label{bbb}'
  88. 'five six'
  89. 'seven eight \\label{ccc} nine'
  90. ]
  91. 'doc_four':
  92. _id: 'id_four'
  93. lines: [
  94. '\\usepackage[width=\\textwidth]{baz}'
  95. '\\usepackage{amsmath}'
  96. ]
  97. 'doc_five':
  98. _id: 'id_five'
  99. lines: [
  100. '\\usepackage{foo,baz}'
  101. '\\usepackage[options=foo]{hello}'
  102. 'some text'
  103. '\\section{this}\\label{sec:intro}'
  104. 'In Section \\ref{sec:intro} we saw'
  105. 'nothing'
  106. ]
  107. it 'should extract all metadata', ->
  108. projectMeta = @MetaHandler.extractMetaFromProjectDocs @docs
  109. expect(projectMeta).to.deep.equal {
  110. 'id_one': {labels: ['aaa'], packages: {}}
  111. 'id_two': {labels: [], packages: {}}
  112. 'id_three': {labels: ['bbb', 'ccc'], packages: {}}
  113. 'id_four':
  114. labels: []
  115. packages:
  116. baz: [{
  117. caption: '\\longercommandtest{}'
  118. snippet: '\\longercommandtest{$1}'
  119. meta: 'baz-cmd'
  120. score: 50}]
  121. 'id_five':
  122. labels: ['sec:intro']
  123. packages:
  124. foo: [
  125. {
  126. caption: '\\bar'
  127. snippet: '\\bar'
  128. meta: 'foo-cmd'
  129. score: 12
  130. }, {
  131. caption: '\\bat[]{}'
  132. snippet: '\\bar[$1]{$2}'
  133. meta: 'foo-cmd'
  134. score: 10
  135. }
  136. ]
  137. baz: [
  138. {
  139. caption: '\\longercommandtest{}'
  140. snippet: '\\longercommandtest{$1}'
  141. meta: 'baz-cmd'
  142. score: 50
  143. }
  144. ]
  145. }
  146. describe 'getMetaForDoc', ->
  147. beforeEach ->
  148. @fakeLines = ['\\usepackage{abc}', 'one', '\\label{aaa}', 'two']
  149. @fakeMeta = {labels: ['aaa'], packages: ['abc']}
  150. @DocumentUpdaterHandler.flushDocToMongo = sinon.stub().callsArgWith 2, null
  151. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith 2, null, @fakeLines
  152. @MetaHandler.extractMetaFromDoc = sinon.stub().returns @fakeMeta
  153. @call = (callback) =>
  154. @MetaHandler.getMetaForDoc @projectId, @docId, callback
  155. it 'should not produce an error', (done) ->
  156. @call (err, docMeta) =>
  157. expect(err).to.equal null
  158. done()
  159. it 'should produce docMeta', (done) ->
  160. @call (err, docMeta) =>
  161. expect(docMeta).to.equal @fakeMeta
  162. done()
  163. it 'should call flushDocToMongo', (done) ->
  164. @call (err, docMeta) =>
  165. @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 1
  166. @DocumentUpdaterHandler.flushDocToMongo.calledWith(@projectId, @docId).should.equal true
  167. done()
  168. it 'should call getDoc', (done) ->
  169. @call (err, docMeta) =>
  170. @ProjectEntityHandler.getDoc.callCount.should.equal 1
  171. @ProjectEntityHandler.getDoc.calledWith(@projectId, @docId).should.equal true
  172. done()
  173. it 'should call extractMetaFromDoc', (done) ->
  174. @call (err, docMeta) =>
  175. @MetaHandler.extractMetaFromDoc.callCount.should.equal 1
  176. @MetaHandler.extractMetaFromDoc.calledWith(@fakeLines).should.equal true
  177. done()
  178. describe 'getAllMetaForProject', ->
  179. beforeEach ->
  180. @fakeDocs =
  181. 'doc_one':
  182. lines: [
  183. '\\usepackage[some-options,more=foo]{foo}'
  184. '\\label{aaa}'
  185. ]
  186. @fakeMeta =
  187. labels: ['aaa']
  188. packages:
  189. foo: [
  190. {
  191. caption: '\\bar'
  192. snippet: '\\bar'
  193. meta: 'foo-cmd'
  194. score: 12
  195. }, {
  196. caption: '\\bat[]{}'
  197. snippet: '\\bar[$1]{$2}'
  198. meta: 'foo-cmd'
  199. score: 10
  200. }
  201. ]
  202. @DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith 1, null
  203. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith 1, null, @fakeDocs
  204. @MetaHandler.extractMetaFromProjectDocs = sinon.stub().returns @fakeMeta
  205. @call = (callback) =>
  206. @MetaHandler.getAllMetaForProject @projectId, callback
  207. it 'should not produce an error', (done) ->
  208. @call (err, projectMeta) =>
  209. expect(err).to.equal null
  210. done()
  211. it 'should produce projectMeta', (done) ->
  212. @call (err, projectMeta) =>
  213. expect(projectMeta).to.equal @fakeMeta
  214. done()
  215. it 'should call getAllDocs', (done) ->
  216. @call (err, projectMeta) =>
  217. @ProjectEntityHandler.getAllDocs.callCount.should.equal 1
  218. @ProjectEntityHandler.getAllDocs.calledWith(@projectId).should.equal true
  219. done()
  220. it 'should call extractMetaFromDoc', (done) ->
  221. @call (err, docMeta) =>
  222. @MetaHandler.extractMetaFromProjectDocs.callCount.should.equal 1
  223. @MetaHandler.extractMetaFromProjectDocs.calledWith(@fakeDocs).should.equal true
  224. done()