TikzManager.test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { vi, describe, beforeEach, it } from 'vitest'
  2. import sinon from 'sinon'
  3. import path from 'node:path'
  4. const modulePath = path.join(import.meta.dirname, '../../../app/js/TikzManager')
  5. describe('TikzManager', () => {
  6. beforeEach(async ctx => {
  7. vi.doMock('../../../app/js/ResourceWriter', () => ({
  8. default: (ctx.ResourceWriter = {}),
  9. }))
  10. vi.doMock('../../../app/js/SafeReader', () => ({
  11. default: (ctx.SafeReader = {}),
  12. }))
  13. vi.doMock('fs', () => ({
  14. default: (ctx.fs = {}),
  15. }))
  16. ctx.TikzManager = (await import(modulePath)).default
  17. })
  18. describe('checkMainFile', () => {
  19. beforeEach(ctx => {
  20. ctx.compileDir = 'compile-dir'
  21. ctx.mainFile = 'main.tex'
  22. ctx.callback = sinon.stub()
  23. })
  24. describe('if there is already an output.tex file in the resources', () => {
  25. beforeEach(ctx => {
  26. ctx.resources = [{ path: 'main.tex' }, { path: 'output.tex' }]
  27. ctx.TikzManager.checkMainFile(
  28. ctx.compileDir,
  29. ctx.mainFile,
  30. ctx.resources,
  31. ctx.callback
  32. )
  33. })
  34. it('should call the callback with false ', ctx => {
  35. ctx.callback.calledWithExactly(null, false).should.equal(true)
  36. })
  37. })
  38. describe('if there is no output.tex file in the resources', () => {
  39. beforeEach(ctx => {
  40. ctx.resources = [{ path: 'main.tex' }]
  41. ctx.ResourceWriter.checkPath = sinon
  42. .stub()
  43. .withArgs(ctx.compileDir, ctx.mainFile)
  44. .callsArgWith(2, null, `${ctx.compileDir}/${ctx.mainFile}`)
  45. })
  46. describe('and the main file contains tikzexternalize', () => {
  47. beforeEach(ctx => {
  48. ctx.SafeReader.readFile = sinon
  49. .stub()
  50. .withArgs(`${ctx.compileDir}/${ctx.mainFile}`)
  51. .callsArgWith(3, null, 'hello \\tikzexternalize')
  52. ctx.TikzManager.checkMainFile(
  53. ctx.compileDir,
  54. ctx.mainFile,
  55. ctx.resources,
  56. ctx.callback
  57. )
  58. })
  59. it('should look at the file on disk', ctx => {
  60. ctx.SafeReader.readFile
  61. .calledWith(`${ctx.compileDir}/${ctx.mainFile}`)
  62. .should.equal(true)
  63. })
  64. it('should call the callback with true ', ctx => {
  65. ctx.callback.calledWithExactly(null, true).should.equal(true)
  66. })
  67. })
  68. describe('and the main file does not contain tikzexternalize', () => {
  69. beforeEach(ctx => {
  70. ctx.SafeReader.readFile = sinon
  71. .stub()
  72. .withArgs(`${ctx.compileDir}/${ctx.mainFile}`)
  73. .callsArgWith(3, null, 'hello')
  74. ctx.TikzManager.checkMainFile(
  75. ctx.compileDir,
  76. ctx.mainFile,
  77. ctx.resources,
  78. ctx.callback
  79. )
  80. })
  81. it('should look at the file on disk', ctx => {
  82. ctx.SafeReader.readFile
  83. .calledWith(`${ctx.compileDir}/${ctx.mainFile}`)
  84. .should.equal(true)
  85. })
  86. it('should call the callback with false', ctx => {
  87. ctx.callback.calledWithExactly(null, false).should.equal(true)
  88. })
  89. })
  90. describe('and the main file contains \\usepackage{pstool}', () => {
  91. beforeEach(ctx => {
  92. ctx.SafeReader.readFile = sinon
  93. .stub()
  94. .withArgs(`${ctx.compileDir}/${ctx.mainFile}`)
  95. .callsArgWith(3, null, 'hello \\usepackage[random-options]{pstool}')
  96. ctx.TikzManager.checkMainFile(
  97. ctx.compileDir,
  98. ctx.mainFile,
  99. ctx.resources,
  100. ctx.callback
  101. )
  102. })
  103. it('should look at the file on disk', ctx => {
  104. ctx.SafeReader.readFile
  105. .calledWith(`${ctx.compileDir}/${ctx.mainFile}`)
  106. .should.equal(true)
  107. })
  108. it('should call the callback with true ', ctx => {
  109. ctx.callback.calledWithExactly(null, true).should.equal(true)
  110. })
  111. })
  112. })
  113. })
  114. describe('injectOutputFile', () => {
  115. beforeEach(ctx => {
  116. ctx.rootDir = '/mock'
  117. ctx.filename = 'filename.tex'
  118. ctx.callback = sinon.stub()
  119. ctx.content = `\
  120. \\documentclass{article}
  121. \\usepackage{tikz}
  122. \\tikzexternalize
  123. \\begin{document}
  124. Hello world
  125. \\end{document}\
  126. `
  127. ctx.fs.readFile = sinon.stub().callsArgWith(2, null, ctx.content)
  128. ctx.fs.writeFile = sinon.stub().callsArg(3)
  129. ctx.ResourceWriter.checkPath = sinon
  130. .stub()
  131. .callsArgWith(2, null, `${ctx.rootDir}/${ctx.filename}`)
  132. ctx.TikzManager.injectOutputFile(ctx.rootDir, ctx.filename, ctx.callback)
  133. })
  134. it('should check the path', ctx => {
  135. ctx.ResourceWriter.checkPath
  136. .calledWith(ctx.rootDir, ctx.filename)
  137. .should.equal(true)
  138. })
  139. it('should read the file', ctx => {
  140. ctx.fs.readFile
  141. .calledWith(`${ctx.rootDir}/${ctx.filename}`, 'utf8')
  142. .should.equal(true)
  143. })
  144. it('should write out the same file as output.tex', ctx => {
  145. ctx.fs.writeFile
  146. .calledWith(`${ctx.rootDir}/output.tex`, ctx.content, { flag: 'wx' })
  147. .should.equal(true)
  148. })
  149. it('should call the callback', ctx => {
  150. ctx.callback.called.should.equal(true)
  151. })
  152. })
  153. })