MetaHandlerTests.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* eslint-disable
  2. 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. requires: {
  56. '../Project/ProjectEntityHandler': this.ProjectEntityHandler,
  57. '../DocumentUpdater/DocumentUpdaterHandler': this
  58. .DocumentUpdaterHandler,
  59. './packageMapping': this.packageMapping
  60. }
  61. }))
  62. })
  63. describe('extractMetaFromDoc', function() {
  64. beforeEach(function() {
  65. return (this.lines = [
  66. '\\usepackage{foo}',
  67. '\\usepackage{amsmath, booktabs}',
  68. 'one',
  69. 'two',
  70. 'three \\label{aaa}',
  71. 'four five',
  72. '\\label{bbb}',
  73. 'six seven'
  74. ])
  75. })
  76. it('should extract all the labels and packages', function() {
  77. const docMeta = this.MetaHandler.extractMetaFromDoc(this.lines)
  78. return expect(docMeta).to.deep.equal({
  79. labels: ['aaa', 'bbb'],
  80. packages: {
  81. foo: [
  82. {
  83. caption: '\\bar',
  84. snippet: '\\bar',
  85. meta: 'foo-cmd',
  86. score: 12
  87. },
  88. {
  89. caption: '\\bat[]{}',
  90. snippet: '\\bar[$1]{$2}',
  91. meta: 'foo-cmd',
  92. score: 10
  93. }
  94. ]
  95. }
  96. })
  97. })
  98. })
  99. describe('extractMetaFromProjectDocs', function() {
  100. beforeEach(function() {
  101. return (this.docs = {
  102. doc_one: {
  103. _id: 'id_one',
  104. lines: ['one', '\\label{aaa} two', 'three']
  105. },
  106. doc_two: {
  107. _id: 'id_two',
  108. lines: ['four']
  109. },
  110. doc_three: {
  111. _id: 'id_three',
  112. lines: ['\\label{bbb}', 'five six', 'seven eight \\label{ccc} nine']
  113. },
  114. doc_four: {
  115. _id: 'id_four',
  116. lines: [
  117. '\\usepackage[width=\\textwidth]{baz}',
  118. '\\usepackage{amsmath}'
  119. ]
  120. },
  121. doc_five: {
  122. _id: 'id_five',
  123. lines: [
  124. '\\usepackage{foo,baz}',
  125. '\\usepackage[options=foo]{hello}',
  126. 'some text',
  127. '\\section{this}\\label{sec:intro}',
  128. 'In Section \\ref{sec:intro} we saw',
  129. 'nothing'
  130. ]
  131. }
  132. })
  133. })
  134. it('should extract all metadata', function() {
  135. const projectMeta = this.MetaHandler.extractMetaFromProjectDocs(this.docs)
  136. return expect(projectMeta).to.deep.equal({
  137. id_one: { labels: ['aaa'], packages: {} },
  138. id_two: { labels: [], packages: {} },
  139. id_three: { labels: ['bbb', 'ccc'], packages: {} },
  140. id_four: {
  141. labels: [],
  142. packages: {
  143. baz: [
  144. {
  145. caption: '\\longercommandtest{}',
  146. snippet: '\\longercommandtest{$1}',
  147. meta: 'baz-cmd',
  148. score: 50
  149. }
  150. ]
  151. }
  152. },
  153. id_five: {
  154. labels: ['sec:intro'],
  155. packages: {
  156. foo: [
  157. {
  158. caption: '\\bar',
  159. snippet: '\\bar',
  160. meta: 'foo-cmd',
  161. score: 12
  162. },
  163. {
  164. caption: '\\bat[]{}',
  165. snippet: '\\bar[$1]{$2}',
  166. meta: 'foo-cmd',
  167. score: 10
  168. }
  169. ],
  170. baz: [
  171. {
  172. caption: '\\longercommandtest{}',
  173. snippet: '\\longercommandtest{$1}',
  174. meta: 'baz-cmd',
  175. score: 50
  176. }
  177. ]
  178. }
  179. }
  180. })
  181. })
  182. })
  183. describe('getMetaForDoc', function() {
  184. beforeEach(function() {
  185. this.fakeLines = ['\\usepackage{abc}', 'one', '\\label{aaa}', 'two']
  186. this.fakeMeta = { labels: ['aaa'], packages: ['abc'] }
  187. this.DocumentUpdaterHandler.flushDocToMongo = sinon
  188. .stub()
  189. .callsArgWith(2, null)
  190. this.ProjectEntityHandler.getDoc = sinon
  191. .stub()
  192. .callsArgWith(2, null, this.fakeLines)
  193. this.MetaHandler.extractMetaFromDoc = sinon.stub().returns(this.fakeMeta)
  194. return (this.call = callback => {
  195. return this.MetaHandler.getMetaForDoc(
  196. this.projectId,
  197. this.docId,
  198. callback
  199. )
  200. })
  201. })
  202. it('should not produce an error', function(done) {
  203. return this.call((err, docMeta) => {
  204. expect(err).to.equal(null)
  205. return done()
  206. })
  207. })
  208. it('should produce docMeta', function(done) {
  209. return this.call((err, docMeta) => {
  210. expect(docMeta).to.equal(this.fakeMeta)
  211. return done()
  212. })
  213. })
  214. it('should call flushDocToMongo', function(done) {
  215. return this.call((err, docMeta) => {
  216. this.DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal(1)
  217. this.DocumentUpdaterHandler.flushDocToMongo
  218. .calledWith(this.projectId, this.docId)
  219. .should.equal(true)
  220. return done()
  221. })
  222. })
  223. it('should call getDoc', function(done) {
  224. return this.call((err, docMeta) => {
  225. this.ProjectEntityHandler.getDoc.callCount.should.equal(1)
  226. this.ProjectEntityHandler.getDoc
  227. .calledWith(this.projectId, this.docId)
  228. .should.equal(true)
  229. return done()
  230. })
  231. })
  232. it('should call extractMetaFromDoc', function(done) {
  233. return this.call((err, docMeta) => {
  234. this.MetaHandler.extractMetaFromDoc.callCount.should.equal(1)
  235. this.MetaHandler.extractMetaFromDoc
  236. .calledWith(this.fakeLines)
  237. .should.equal(true)
  238. return done()
  239. })
  240. })
  241. })
  242. describe('getAllMetaForProject', function() {
  243. beforeEach(function() {
  244. this.fakeDocs = {
  245. doc_one: {
  246. lines: ['\\usepackage[some-options,more=foo]{foo}', '\\label{aaa}']
  247. }
  248. }
  249. this.fakeMeta = {
  250. labels: ['aaa'],
  251. packages: {
  252. foo: [
  253. {
  254. caption: '\\bar',
  255. snippet: '\\bar',
  256. meta: 'foo-cmd',
  257. score: 12
  258. },
  259. {
  260. caption: '\\bat[]{}',
  261. snippet: '\\bar[$1]{$2}',
  262. meta: 'foo-cmd',
  263. score: 10
  264. }
  265. ]
  266. }
  267. }
  268. this.DocumentUpdaterHandler.flushProjectToMongo = sinon
  269. .stub()
  270. .callsArgWith(1, null)
  271. this.ProjectEntityHandler.getAllDocs = sinon
  272. .stub()
  273. .callsArgWith(1, null, this.fakeDocs)
  274. this.MetaHandler.extractMetaFromProjectDocs = sinon
  275. .stub()
  276. .returns(this.fakeMeta)
  277. return (this.call = callback => {
  278. return this.MetaHandler.getAllMetaForProject(this.projectId, callback)
  279. })
  280. })
  281. it('should not produce an error', function(done) {
  282. return this.call((err, projectMeta) => {
  283. expect(err).to.equal(null)
  284. return done()
  285. })
  286. })
  287. it('should produce projectMeta', function(done) {
  288. return this.call((err, projectMeta) => {
  289. expect(projectMeta).to.equal(this.fakeMeta)
  290. return done()
  291. })
  292. })
  293. it('should call getAllDocs', function(done) {
  294. return this.call((err, projectMeta) => {
  295. this.ProjectEntityHandler.getAllDocs.callCount.should.equal(1)
  296. this.ProjectEntityHandler.getAllDocs
  297. .calledWith(this.projectId)
  298. .should.equal(true)
  299. return done()
  300. })
  301. })
  302. it('should call extractMetaFromDoc', function(done) {
  303. return this.call((err, docMeta) => {
  304. this.MetaHandler.extractMetaFromProjectDocs.callCount.should.equal(1)
  305. this.MetaHandler.extractMetaFromProjectDocs
  306. .calledWith(this.fakeDocs)
  307. .should.equal(true)
  308. return done()
  309. })
  310. })
  311. })
  312. })