MetaHandler.test.mjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. documentClass: null,
  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. documentClass: null,
  148. },
  149. id_two: {
  150. labels: [],
  151. packages: {},
  152. packageNames: [],
  153. documentClass: null,
  154. },
  155. id_three: {
  156. labels: ['bbb', 'ccc'],
  157. packages: {},
  158. packageNames: [],
  159. documentClass: null,
  160. },
  161. id_four: {
  162. labels: [],
  163. packages: {
  164. baz: [
  165. {
  166. caption: '\\longercommandtest{}',
  167. snippet: '\\longercommandtest{$1}',
  168. meta: 'baz-cmd',
  169. score: 50,
  170. },
  171. ],
  172. },
  173. packageNames: ['baz', 'amsmath'],
  174. documentClass: null,
  175. },
  176. id_five: {
  177. labels: ['sec:intro'],
  178. packages: {
  179. foo: [
  180. {
  181. caption: '\\bar',
  182. snippet: '\\bar',
  183. meta: 'foo-cmd',
  184. score: 12,
  185. },
  186. {
  187. caption: '\\bat[]{}',
  188. snippet: '\\bar[$1]{$2}',
  189. meta: 'foo-cmd',
  190. score: 10,
  191. },
  192. ],
  193. baz: [
  194. {
  195. caption: '\\longercommandtest{}',
  196. snippet: '\\longercommandtest{$1}',
  197. meta: 'baz-cmd',
  198. score: 50,
  199. },
  200. ],
  201. },
  202. packageNames: ['foo', 'baz', 'hello'],
  203. documentClass: null,
  204. },
  205. })
  206. ctx.DocumentUpdaterHandler.promises.flushProjectToMongo.should.be.calledWith(
  207. ctx.projectId
  208. )
  209. ctx.ProjectEntityHandler.promises.getAllDocs.should.be.calledWith(
  210. ctx.projectId
  211. )
  212. })
  213. })
  214. })