MetaHandlerTests.js 9.1 KB

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