MetaHandler.test.mjs 5.8 KB

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