MetaHandlerTests.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* eslint-disable
  2. node/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const chai = require('chai')
  14. chai.should()
  15. const { expect } = chai
  16. const sinon = require('sinon')
  17. const modulePath = '../../../../app/src/Features/Metadata/MetaHandler'
  18. const SandboxedModule = require('sandboxed-module')
  19. describe('MetaHandler', function() {
  20. beforeEach(function() {
  21. this.projectId = 'someprojectid'
  22. this.docId = 'somedocid'
  23. this.ProjectEntityHandler = {
  24. getAllDocs: sinon.stub(),
  25. getDoc: sinon.stub()
  26. }
  27. this.DocumentUpdaterHandler = {
  28. flushDocToMongo: sinon.stub()
  29. }
  30. this.packageMapping = {
  31. foo: [
  32. {
  33. caption: '\\bar',
  34. snippet: '\\bar',
  35. meta: 'foo-cmd',
  36. score: 12
  37. },
  38. {
  39. caption: '\\bat[]{}',
  40. snippet: '\\bar[$1]{$2}',
  41. meta: 'foo-cmd',
  42. score: 10
  43. }
  44. ],
  45. baz: [
  46. {
  47. caption: '\\longercommandtest{}',
  48. snippet: '\\longercommandtest{$1}',
  49. meta: 'baz-cmd',
  50. score: 50
  51. }
  52. ]
  53. }
  54. return (this.MetaHandler = SandboxedModule.require(modulePath, {
  55. globals: {
  56. console: console
  57. },
  58. requires: {
  59. '../Project/ProjectEntityHandler': this.ProjectEntityHandler,
  60. '../DocumentUpdater/DocumentUpdaterHandler': this
  61. .DocumentUpdaterHandler,
  62. './packageMapping': this.packageMapping
  63. }
  64. }))
  65. })
  66. describe('extractMetaFromDoc', function() {
  67. beforeEach(function() {
  68. return (this.lines = [
  69. '\\usepackage{foo}',
  70. '\\usepackage{amsmath, booktabs}',
  71. 'one',
  72. 'two',
  73. 'three \\label{aaa}',
  74. 'four five',
  75. '\\label{bbb}',
  76. 'six seven'
  77. ])
  78. })
  79. it('should extract all the labels and packages', function() {
  80. const docMeta = this.MetaHandler.extractMetaFromDoc(this.lines)
  81. return expect(docMeta).to.deep.equal({
  82. labels: ['aaa', 'bbb'],
  83. packages: {
  84. foo: [
  85. {
  86. caption: '\\bar',
  87. snippet: '\\bar',
  88. meta: 'foo-cmd',
  89. score: 12
  90. },
  91. {
  92. caption: '\\bat[]{}',
  93. snippet: '\\bar[$1]{$2}',
  94. meta: 'foo-cmd',
  95. score: 10
  96. }
  97. ]
  98. }
  99. })
  100. })
  101. })
  102. describe('extractMetaFromProjectDocs', function() {
  103. beforeEach(function() {
  104. return (this.docs = {
  105. doc_one: {
  106. _id: 'id_one',
  107. lines: ['one', '\\label{aaa} two', 'three']
  108. },
  109. doc_two: {
  110. _id: 'id_two',
  111. lines: ['four']
  112. },
  113. doc_three: {
  114. _id: 'id_three',
  115. lines: ['\\label{bbb}', 'five six', 'seven eight \\label{ccc} nine']
  116. },
  117. doc_four: {
  118. _id: 'id_four',
  119. lines: [
  120. '\\usepackage[width=\\textwidth]{baz}',
  121. '\\usepackage{amsmath}'
  122. ]
  123. },
  124. doc_five: {
  125. _id: 'id_five',
  126. lines: [
  127. '\\usepackage{foo,baz}',
  128. '\\usepackage[options=foo]{hello}',
  129. 'some text',
  130. '\\section{this}\\label{sec:intro}',
  131. 'In Section \\ref{sec:intro} we saw',
  132. 'nothing'
  133. ]
  134. }
  135. })
  136. })
  137. it('should extract all metadata', function() {
  138. const projectMeta = this.MetaHandler.extractMetaFromProjectDocs(this.docs)
  139. return expect(projectMeta).to.deep.equal({
  140. id_one: { labels: ['aaa'], packages: {} },
  141. id_two: { labels: [], packages: {} },
  142. id_three: { labels: ['bbb', 'ccc'], packages: {} },
  143. id_four: {
  144. labels: [],
  145. packages: {
  146. baz: [
  147. {
  148. caption: '\\longercommandtest{}',
  149. snippet: '\\longercommandtest{$1}',
  150. meta: 'baz-cmd',
  151. score: 50
  152. }
  153. ]
  154. }
  155. },
  156. id_five: {
  157. labels: ['sec:intro'],
  158. packages: {
  159. foo: [
  160. {
  161. caption: '\\bar',
  162. snippet: '\\bar',
  163. meta: 'foo-cmd',
  164. score: 12
  165. },
  166. {
  167. caption: '\\bat[]{}',
  168. snippet: '\\bar[$1]{$2}',
  169. meta: 'foo-cmd',
  170. score: 10
  171. }
  172. ],
  173. baz: [
  174. {
  175. caption: '\\longercommandtest{}',
  176. snippet: '\\longercommandtest{$1}',
  177. meta: 'baz-cmd',
  178. score: 50
  179. }
  180. ]
  181. }
  182. }
  183. })
  184. })
  185. })
  186. describe('getMetaForDoc', function() {
  187. beforeEach(function() {
  188. this.fakeLines = ['\\usepackage{abc}', 'one', '\\label{aaa}', 'two']
  189. this.fakeMeta = { labels: ['aaa'], packages: ['abc'] }
  190. this.DocumentUpdaterHandler.flushDocToMongo = sinon
  191. .stub()
  192. .callsArgWith(2, null)
  193. this.ProjectEntityHandler.getDoc = sinon
  194. .stub()
  195. .callsArgWith(2, null, this.fakeLines)
  196. this.MetaHandler.extractMetaFromDoc = sinon.stub().returns(this.fakeMeta)
  197. return (this.call = callback => {
  198. return this.MetaHandler.getMetaForDoc(
  199. this.projectId,
  200. this.docId,
  201. callback
  202. )
  203. })
  204. })
  205. it('should not produce an error', function(done) {
  206. return this.call((err, docMeta) => {
  207. expect(err).to.equal(null)
  208. return done()
  209. })
  210. })
  211. it('should produce docMeta', function(done) {
  212. return this.call((err, docMeta) => {
  213. expect(docMeta).to.equal(this.fakeMeta)
  214. return done()
  215. })
  216. })
  217. it('should call flushDocToMongo', function(done) {
  218. return this.call((err, docMeta) => {
  219. this.DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal(1)
  220. this.DocumentUpdaterHandler.flushDocToMongo
  221. .calledWith(this.projectId, this.docId)
  222. .should.equal(true)
  223. return done()
  224. })
  225. })
  226. it('should call getDoc', function(done) {
  227. return this.call((err, docMeta) => {
  228. this.ProjectEntityHandler.getDoc.callCount.should.equal(1)
  229. this.ProjectEntityHandler.getDoc
  230. .calledWith(this.projectId, this.docId)
  231. .should.equal(true)
  232. return done()
  233. })
  234. })
  235. it('should call extractMetaFromDoc', function(done) {
  236. return this.call((err, docMeta) => {
  237. this.MetaHandler.extractMetaFromDoc.callCount.should.equal(1)
  238. this.MetaHandler.extractMetaFromDoc
  239. .calledWith(this.fakeLines)
  240. .should.equal(true)
  241. return done()
  242. })
  243. })
  244. })
  245. describe('getAllMetaForProject', function() {
  246. beforeEach(function() {
  247. this.fakeDocs = {
  248. doc_one: {
  249. lines: ['\\usepackage[some-options,more=foo]{foo}', '\\label{aaa}']
  250. }
  251. }
  252. this.fakeMeta = {
  253. labels: ['aaa'],
  254. packages: {
  255. foo: [
  256. {
  257. caption: '\\bar',
  258. snippet: '\\bar',
  259. meta: 'foo-cmd',
  260. score: 12
  261. },
  262. {
  263. caption: '\\bat[]{}',
  264. snippet: '\\bar[$1]{$2}',
  265. meta: 'foo-cmd',
  266. score: 10
  267. }
  268. ]
  269. }
  270. }
  271. this.DocumentUpdaterHandler.flushProjectToMongo = sinon
  272. .stub()
  273. .callsArgWith(1, null)
  274. this.ProjectEntityHandler.getAllDocs = sinon
  275. .stub()
  276. .callsArgWith(1, null, this.fakeDocs)
  277. this.MetaHandler.extractMetaFromProjectDocs = sinon
  278. .stub()
  279. .returns(this.fakeMeta)
  280. return (this.call = callback => {
  281. return this.MetaHandler.getAllMetaForProject(this.projectId, callback)
  282. })
  283. })
  284. it('should not produce an error', function(done) {
  285. return this.call((err, projectMeta) => {
  286. expect(err).to.equal(null)
  287. return done()
  288. })
  289. })
  290. it('should produce projectMeta', function(done) {
  291. return this.call((err, projectMeta) => {
  292. expect(projectMeta).to.equal(this.fakeMeta)
  293. return done()
  294. })
  295. })
  296. it('should call getAllDocs', function(done) {
  297. return this.call((err, projectMeta) => {
  298. this.ProjectEntityHandler.getAllDocs.callCount.should.equal(1)
  299. this.ProjectEntityHandler.getAllDocs
  300. .calledWith(this.projectId)
  301. .should.equal(true)
  302. return done()
  303. })
  304. })
  305. it('should call extractMetaFromDoc', function(done) {
  306. return this.call((err, docMeta) => {
  307. this.MetaHandler.extractMetaFromProjectDocs.callCount.should.equal(1)
  308. this.MetaHandler.extractMetaFromProjectDocs
  309. .calledWith(this.fakeDocs)
  310. .should.equal(true)
  311. return done()
  312. })
  313. })
  314. })
  315. })