MetaHandler.test.mjs 5.9 KB

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