MetaHandlerTests.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import esmock from 'esmock'
  4. const modulePath = '../../../../app/src/Features/Metadata/MetaHandler.mjs'
  5. describe('MetaHandler', function () {
  6. beforeEach(async function () {
  7. this.projectId = 'someprojectid'
  8. this.docId = 'somedocid'
  9. this.lines = [
  10. '\\usepackage{ foo, bar }',
  11. '\\usepackage{baz}',
  12. 'one',
  13. '\\label{aaa}',
  14. 'two',
  15. 'commented label % \\label{bbb}', // bbb should not be in the returned labels
  16. '\\label{ccc}%bar', // ccc should be in the returned labels
  17. '\\label{ddd} % bar', // ddd should be in the returned labels
  18. '\\label{ e,f,g }', // e,f,g should be in the returned labels
  19. ]
  20. this.docs = {
  21. [this.docId]: {
  22. _id: this.docId,
  23. lines: this.lines,
  24. },
  25. }
  26. this.ProjectEntityHandler = {
  27. promises: {
  28. getAllDocs: sinon.stub().resolves(this.docs),
  29. getDoc: sinon.stub().resolves(this.docs[this.docId]),
  30. },
  31. }
  32. this.DocumentUpdaterHandler = {
  33. promises: {
  34. flushDocToMongo: sinon.stub().resolves(),
  35. flushProjectToMongo: sinon.stub().resolves(),
  36. },
  37. }
  38. this.packageMapping = {
  39. foo: [
  40. {
  41. caption: '\\bar',
  42. snippet: '\\bar',
  43. meta: 'foo-cmd',
  44. score: 12,
  45. },
  46. {
  47. caption: '\\bat[]{}',
  48. snippet: '\\bar[$1]{$2}',
  49. meta: 'foo-cmd',
  50. score: 10,
  51. },
  52. ],
  53. baz: [
  54. {
  55. caption: '\\longercommandtest{}',
  56. snippet: '\\longercommandtest{$1}',
  57. meta: 'baz-cmd',
  58. score: 50,
  59. },
  60. ],
  61. }
  62. this.MetaHandler = await esmock.strict(modulePath, {
  63. '../../../../app/src/Features/Project/ProjectEntityHandler':
  64. this.ProjectEntityHandler,
  65. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler':
  66. this.DocumentUpdaterHandler,
  67. '../../../../app/src/Features/Metadata/packageMapping':
  68. this.packageMapping,
  69. })
  70. })
  71. describe('getMetaForDoc', function () {
  72. it('should extract all the labels and packages', async function () {
  73. const result = await this.MetaHandler.promises.getMetaForDoc(
  74. this.projectId,
  75. this.docId
  76. )
  77. expect(result).to.deep.equal({
  78. labels: ['aaa', 'ccc', 'ddd', 'e,f,g'],
  79. packages: {
  80. foo: this.packageMapping.foo,
  81. baz: this.packageMapping.baz,
  82. },
  83. packageNames: ['foo', 'bar', 'baz'],
  84. })
  85. this.DocumentUpdaterHandler.promises.flushDocToMongo.should.be.calledWith(
  86. this.projectId,
  87. this.docId
  88. )
  89. this.ProjectEntityHandler.promises.getDoc.should.be.calledWith(
  90. this.projectId,
  91. this.docId
  92. )
  93. })
  94. })
  95. describe('getAllMetaForProject', function () {
  96. it('should extract all metadata', async function () {
  97. this.ProjectEntityHandler.promises.getAllDocs = sinon.stub().resolves({
  98. doc_one: {
  99. _id: 'id_one',
  100. lines: ['one', '\\label{aaa} two', 'three'],
  101. },
  102. doc_two: {
  103. _id: 'id_two',
  104. lines: ['four'],
  105. },
  106. doc_three: {
  107. _id: 'id_three',
  108. lines: ['\\label{bbb}', 'five six', 'seven eight \\label{ccc} nine'],
  109. },
  110. doc_four: {
  111. _id: 'id_four',
  112. lines: [
  113. '\\usepackage[width=\\textwidth]{baz}',
  114. '\\usepackage{amsmath}',
  115. ],
  116. },
  117. doc_five: {
  118. _id: 'id_five',
  119. lines: [
  120. '\\usepackage{foo,baz}',
  121. '\\usepackage[options=foo]{hello}',
  122. 'some text',
  123. '\\section{this}\\label{sec:intro}',
  124. 'In Section \\ref{sec:intro} we saw',
  125. 'nothing',
  126. ],
  127. },
  128. })
  129. const result = await this.MetaHandler.promises.getAllMetaForProject(
  130. this.projectId
  131. )
  132. expect(result).to.deep.equal({
  133. id_one: {
  134. labels: ['aaa'],
  135. packages: {},
  136. packageNames: [],
  137. },
  138. id_two: {
  139. labels: [],
  140. packages: {},
  141. packageNames: [],
  142. },
  143. id_three: {
  144. labels: ['bbb', 'ccc'],
  145. packages: {},
  146. packageNames: [],
  147. },
  148. id_four: {
  149. labels: [],
  150. packages: {
  151. baz: [
  152. {
  153. caption: '\\longercommandtest{}',
  154. snippet: '\\longercommandtest{$1}',
  155. meta: 'baz-cmd',
  156. score: 50,
  157. },
  158. ],
  159. },
  160. packageNames: ['baz', 'amsmath'],
  161. },
  162. id_five: {
  163. labels: ['sec:intro'],
  164. packages: {
  165. foo: [
  166. {
  167. caption: '\\bar',
  168. snippet: '\\bar',
  169. meta: 'foo-cmd',
  170. score: 12,
  171. },
  172. {
  173. caption: '\\bat[]{}',
  174. snippet: '\\bar[$1]{$2}',
  175. meta: 'foo-cmd',
  176. score: 10,
  177. },
  178. ],
  179. baz: [
  180. {
  181. caption: '\\longercommandtest{}',
  182. snippet: '\\longercommandtest{$1}',
  183. meta: 'baz-cmd',
  184. score: 50,
  185. },
  186. ],
  187. },
  188. packageNames: ['foo', 'baz', 'hello'],
  189. },
  190. })
  191. this.DocumentUpdaterHandler.promises.flushProjectToMongo.should.be.calledWith(
  192. this.projectId
  193. )
  194. this.ProjectEntityHandler.promises.getAllDocs.should.be.calledWith(
  195. this.projectId
  196. )
  197. })
  198. })
  199. })