MetaHandlerTests.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 { 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 = ['\\usepackage{abc}', 'one', '\\label{aaa}', 'two']
  184. this.fakeMeta = { labels: ['aaa'], packages: ['abc'] }
  185. this.DocumentUpdaterHandler.flushDocToMongo = sinon
  186. .stub()
  187. .callsArgWith(2, null)
  188. this.ProjectEntityHandler.getDoc = sinon
  189. .stub()
  190. .callsArgWith(2, null, this.fakeLines)
  191. this.MetaHandler.extractMetaFromDoc = sinon.stub().returns(this.fakeMeta)
  192. return (this.call = callback => {
  193. return this.MetaHandler.getMetaForDoc(
  194. this.projectId,
  195. this.docId,
  196. callback
  197. )
  198. })
  199. })
  200. it('should not produce an error', function (done) {
  201. return this.call((err, docMeta) => {
  202. expect(err).to.equal(null)
  203. return done()
  204. })
  205. })
  206. it('should produce docMeta', function (done) {
  207. return this.call((err, docMeta) => {
  208. expect(docMeta).to.equal(this.fakeMeta)
  209. return done()
  210. })
  211. })
  212. it('should call flushDocToMongo', function (done) {
  213. return this.call((err, docMeta) => {
  214. this.DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal(1)
  215. this.DocumentUpdaterHandler.flushDocToMongo
  216. .calledWith(this.projectId, this.docId)
  217. .should.equal(true)
  218. return done()
  219. })
  220. })
  221. it('should call getDoc', function (done) {
  222. return this.call((err, docMeta) => {
  223. this.ProjectEntityHandler.getDoc.callCount.should.equal(1)
  224. this.ProjectEntityHandler.getDoc
  225. .calledWith(this.projectId, this.docId)
  226. .should.equal(true)
  227. return done()
  228. })
  229. })
  230. it('should call extractMetaFromDoc', function (done) {
  231. return this.call((err, docMeta) => {
  232. this.MetaHandler.extractMetaFromDoc.callCount.should.equal(1)
  233. this.MetaHandler.extractMetaFromDoc
  234. .calledWith(this.fakeLines)
  235. .should.equal(true)
  236. return done()
  237. })
  238. })
  239. })
  240. describe('getAllMetaForProject', function () {
  241. beforeEach(function () {
  242. this.fakeDocs = {
  243. doc_one: {
  244. lines: ['\\usepackage[some-options,more=foo]{foo}', '\\label{aaa}'],
  245. },
  246. }
  247. this.fakeMeta = {
  248. labels: ['aaa'],
  249. packages: {
  250. foo: [
  251. {
  252. caption: '\\bar',
  253. snippet: '\\bar',
  254. meta: 'foo-cmd',
  255. score: 12,
  256. },
  257. {
  258. caption: '\\bat[]{}',
  259. snippet: '\\bar[$1]{$2}',
  260. meta: 'foo-cmd',
  261. score: 10,
  262. },
  263. ],
  264. },
  265. }
  266. this.DocumentUpdaterHandler.flushProjectToMongo = sinon
  267. .stub()
  268. .callsArgWith(1, null)
  269. this.ProjectEntityHandler.getAllDocs = sinon
  270. .stub()
  271. .callsArgWith(1, null, this.fakeDocs)
  272. this.MetaHandler.extractMetaFromProjectDocs = sinon
  273. .stub()
  274. .returns(this.fakeMeta)
  275. return (this.call = callback => {
  276. return this.MetaHandler.getAllMetaForProject(this.projectId, callback)
  277. })
  278. })
  279. it('should not produce an error', function (done) {
  280. return this.call((err, projectMeta) => {
  281. expect(err).to.equal(null)
  282. return done()
  283. })
  284. })
  285. it('should produce projectMeta', function (done) {
  286. return this.call((err, projectMeta) => {
  287. expect(projectMeta).to.equal(this.fakeMeta)
  288. return done()
  289. })
  290. })
  291. it('should call getAllDocs', function (done) {
  292. return this.call((err, projectMeta) => {
  293. this.ProjectEntityHandler.getAllDocs.callCount.should.equal(1)
  294. this.ProjectEntityHandler.getAllDocs
  295. .calledWith(this.projectId)
  296. .should.equal(true)
  297. return done()
  298. })
  299. })
  300. it('should call extractMetaFromDoc', function (done) {
  301. return this.call((err, docMeta) => {
  302. this.MetaHandler.extractMetaFromProjectDocs.callCount.should.equal(1)
  303. this.MetaHandler.extractMetaFromProjectDocs
  304. .calledWith(this.fakeDocs)
  305. .should.equal(true)
  306. return done()
  307. })
  308. })
  309. })
  310. })