MetaHandlerTests.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. packageNames: ['foo', 'amsmath', 'booktabs'],
  95. })
  96. })
  97. })
  98. describe('extractMetaFromProjectDocs', function () {
  99. beforeEach(function () {
  100. return (this.docs = {
  101. doc_one: {
  102. _id: 'id_one',
  103. lines: ['one', '\\label{aaa} two', 'three'],
  104. },
  105. doc_two: {
  106. _id: 'id_two',
  107. lines: ['four'],
  108. },
  109. doc_three: {
  110. _id: 'id_three',
  111. lines: ['\\label{bbb}', 'five six', 'seven eight \\label{ccc} nine'],
  112. },
  113. doc_four: {
  114. _id: 'id_four',
  115. lines: [
  116. '\\usepackage[width=\\textwidth]{baz}',
  117. '\\usepackage{amsmath}',
  118. ],
  119. },
  120. doc_five: {
  121. _id: 'id_five',
  122. lines: [
  123. '\\usepackage{foo,baz}',
  124. '\\usepackage[options=foo]{hello}',
  125. 'some text',
  126. '\\section{this}\\label{sec:intro}',
  127. 'In Section \\ref{sec:intro} we saw',
  128. 'nothing',
  129. ],
  130. },
  131. })
  132. })
  133. it('should extract all metadata', function () {
  134. const projectMeta = this.MetaHandler.extractMetaFromProjectDocs(this.docs)
  135. return expect(projectMeta).to.deep.equal({
  136. id_one: { labels: ['aaa'], packages: {}, packageNames: [] },
  137. id_two: { labels: [], packages: {}, packageNames: [] },
  138. id_three: { labels: ['bbb', 'ccc'], packages: {}, packageNames: [] },
  139. id_four: {
  140. labels: [],
  141. packages: {
  142. baz: [
  143. {
  144. caption: '\\longercommandtest{}',
  145. snippet: '\\longercommandtest{$1}',
  146. meta: 'baz-cmd',
  147. score: 50,
  148. },
  149. ],
  150. },
  151. packageNames: ['baz', 'amsmath'],
  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. packageNames: ['foo', 'baz', 'hello'],
  180. },
  181. })
  182. })
  183. })
  184. describe('getMetaForDoc', function () {
  185. beforeEach(function () {
  186. this.fakeLines = [
  187. '\\usepackage{abc}',
  188. 'one',
  189. '\\label{aaa}',
  190. 'two',
  191. // bbb should not be in the returned labels
  192. 'commented label % \\label{bbb}',
  193. '\\label{ccc}%bar',
  194. '\\label{ddd} % bar',
  195. ]
  196. this.fakeMeta = {
  197. labels: ['aaa', 'ccc', 'ddd'],
  198. packages: { abc: [] },
  199. packageNames: ['abc'],
  200. }
  201. this.DocumentUpdaterHandler.flushDocToMongo = sinon
  202. .stub()
  203. .callsArgWith(2, null)
  204. this.ProjectEntityHandler.getDoc = sinon
  205. .stub()
  206. .callsArgWith(2, null, this.fakeLines)
  207. this.MetaHandler.extractMetaFromDoc = sinon.stub().returns(this.fakeMeta)
  208. return (this.call = callback => {
  209. return this.MetaHandler.getMetaForDoc(
  210. this.projectId,
  211. this.docId,
  212. callback
  213. )
  214. })
  215. })
  216. it('should not produce an error', function (done) {
  217. return this.call((err, docMeta) => {
  218. expect(err).to.equal(null)
  219. return done()
  220. })
  221. })
  222. it('should produce docMeta', function (done) {
  223. return this.call((err, docMeta) => {
  224. expect(docMeta).to.equal(this.fakeMeta)
  225. return done()
  226. })
  227. })
  228. it('should call flushDocToMongo', function (done) {
  229. return this.call((err, docMeta) => {
  230. this.DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal(1)
  231. this.DocumentUpdaterHandler.flushDocToMongo
  232. .calledWith(this.projectId, this.docId)
  233. .should.equal(true)
  234. return done()
  235. })
  236. })
  237. it('should call getDoc', function (done) {
  238. return this.call((err, docMeta) => {
  239. this.ProjectEntityHandler.getDoc.callCount.should.equal(1)
  240. this.ProjectEntityHandler.getDoc
  241. .calledWith(this.projectId, this.docId)
  242. .should.equal(true)
  243. return done()
  244. })
  245. })
  246. it('should call extractMetaFromDoc', function (done) {
  247. return this.call((err, docMeta) => {
  248. this.MetaHandler.extractMetaFromDoc.callCount.should.equal(1)
  249. this.MetaHandler.extractMetaFromDoc
  250. .calledWith(this.fakeLines)
  251. .should.equal(true)
  252. return done()
  253. })
  254. })
  255. })
  256. describe('getAllMetaForProject', function () {
  257. beforeEach(function () {
  258. this.fakeDocs = {
  259. doc_one: {
  260. lines: ['\\usepackage[some-options,more=foo]{foo}', '\\label{aaa}'],
  261. },
  262. }
  263. this.fakeMeta = {
  264. labels: ['aaa'],
  265. packages: {
  266. foo: [
  267. {
  268. caption: '\\bar',
  269. snippet: '\\bar',
  270. meta: 'foo-cmd',
  271. score: 12,
  272. },
  273. {
  274. caption: '\\bat[]{}',
  275. snippet: '\\bar[$1]{$2}',
  276. meta: 'foo-cmd',
  277. score: 10,
  278. },
  279. ],
  280. },
  281. packageNames: ['foo'],
  282. }
  283. this.DocumentUpdaterHandler.flushProjectToMongo = sinon
  284. .stub()
  285. .callsArgWith(1, null)
  286. this.ProjectEntityHandler.getAllDocs = sinon
  287. .stub()
  288. .callsArgWith(1, null, this.fakeDocs)
  289. this.MetaHandler.extractMetaFromProjectDocs = sinon
  290. .stub()
  291. .returns(this.fakeMeta)
  292. return (this.call = callback => {
  293. return this.MetaHandler.getAllMetaForProject(this.projectId, callback)
  294. })
  295. })
  296. it('should not produce an error', function (done) {
  297. return this.call((err, projectMeta) => {
  298. expect(err).to.equal(null)
  299. return done()
  300. })
  301. })
  302. it('should produce projectMeta', function (done) {
  303. return this.call((err, projectMeta) => {
  304. expect(projectMeta).to.equal(this.fakeMeta)
  305. return done()
  306. })
  307. })
  308. it('should call getAllDocs', function (done) {
  309. return this.call((err, projectMeta) => {
  310. this.ProjectEntityHandler.getAllDocs.callCount.should.equal(1)
  311. this.ProjectEntityHandler.getAllDocs
  312. .calledWith(this.projectId)
  313. .should.equal(true)
  314. return done()
  315. })
  316. })
  317. it('should call extractMetaFromDoc', function (done) {
  318. return this.call((err, docMeta) => {
  319. this.MetaHandler.extractMetaFromProjectDocs.callCount.should.equal(1)
  320. this.MetaHandler.extractMetaFromProjectDocs
  321. .calledWith(this.fakeDocs)
  322. .should.equal(true)
  323. return done()
  324. })
  325. })
  326. })
  327. })