ClsiStateManager.test.mjs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath = '../../../../app/src/Features/Compile/ClsiStateManager.mjs'
  4. describe('ClsiStateManager', function () {
  5. beforeEach(async function (ctx) {
  6. vi.doMock('@overleaf/settings', () => ({
  7. default: (ctx.settings = {}),
  8. }))
  9. vi.doMock(
  10. '../../../../app/src/Features/Project/ProjectEntityHandler',
  11. () => ({
  12. default: (ctx.ProjectEntityHandler = {}),
  13. })
  14. )
  15. ctx.ClsiStateManager = (await import(modulePath)).default
  16. ctx.project = 'project'
  17. ctx.options = {
  18. draft: true,
  19. isAutoCompile: false,
  20. buildId: 'original-build-id',
  21. }
  22. ctx.callback = sinon.stub()
  23. })
  24. describe('computeHash', function () {
  25. beforeEach(function (ctx) {
  26. ctx.docs = [
  27. { path: '/main.tex', doc: { _id: 'doc-id-1' } },
  28. { path: '/folder/sub.tex', doc: { _id: 'doc-id-2' } },
  29. ]
  30. ctx.files = [
  31. {
  32. path: '/figure.pdf',
  33. file: { _id: 'file-id-1', rev: 123, created: 'aaaaaa' },
  34. },
  35. {
  36. path: '/folder/fig2.pdf',
  37. file: { _id: 'file-id-2', rev: 456, created: 'bbbbbb' },
  38. },
  39. ]
  40. ctx.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  41. .stub()
  42. .returns({ docs: ctx.docs, files: ctx.files })
  43. ctx.hash0 = ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  44. })
  45. describe('with a sample project', function () {
  46. beforeEach(function () {})
  47. it('should return a hash value', function (ctx) {
  48. expect(
  49. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  50. ).to.equal('21b1ab73aa3892bec452baf8ffa0956179e1880f')
  51. })
  52. })
  53. describe('when the files and docs are in a different order', function () {
  54. beforeEach(function (ctx) {
  55. ;[ctx.docs[0], ctx.docs[1]] = [ctx.docs[1], ctx.docs[0]]
  56. ;[ctx.files[0], ctx.files[1]] = [ctx.files[1], ctx.files[0]]
  57. })
  58. it('should return the same hash value', function (ctx) {
  59. expect(
  60. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  61. ).to.equal(ctx.hash0)
  62. })
  63. })
  64. describe('when a doc is renamed', function () {
  65. beforeEach(function (ctx) {
  66. ctx.docs[0].path = '/new.tex'
  67. })
  68. it('should return a different hash value', function (ctx) {
  69. expect(
  70. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  71. ).not.to.equal(ctx.hash0)
  72. })
  73. })
  74. describe('when a file is renamed', function () {
  75. beforeEach(function (ctx) {
  76. ctx.files[0].path = '/newfigure.pdf'
  77. })
  78. it('should return a different hash value', function (ctx) {
  79. expect(
  80. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  81. ).not.to.equal(ctx.hash0)
  82. })
  83. })
  84. describe('when a doc is added', function () {
  85. beforeEach(function (ctx) {
  86. ctx.docs.push({ path: '/newdoc.tex', doc: { _id: 'newdoc-id' } })
  87. })
  88. it('should return a different hash value', function (ctx) {
  89. expect(
  90. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  91. ).not.to.equal(ctx.hash0)
  92. })
  93. })
  94. describe('when a file is added', function () {
  95. beforeEach(function (ctx) {
  96. ctx.files.push({
  97. path: '/newfile.tex',
  98. file: { _id: 'newfile-id', rev: 123 },
  99. })
  100. })
  101. it('should return a different hash value', function (ctx) {
  102. expect(
  103. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  104. ).not.to.equal(ctx.hash0)
  105. })
  106. })
  107. describe('when a doc is removed', function () {
  108. beforeEach(function (ctx) {
  109. ctx.docs.pop()
  110. })
  111. it('should return a different hash value', function (ctx) {
  112. expect(
  113. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  114. ).not.to.equal(ctx.hash0)
  115. })
  116. })
  117. describe('when a file is removed', function () {
  118. beforeEach(function (ctx) {
  119. ctx.files.pop()
  120. })
  121. it('should return a different hash value', function (ctx) {
  122. expect(
  123. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  124. ).not.to.equal(ctx.hash0)
  125. })
  126. })
  127. describe("when a file's revision is updated", function () {
  128. beforeEach(function (ctx) {
  129. ctx.files[0].file.rev++
  130. })
  131. it('should return a different hash value', function (ctx) {
  132. expect(
  133. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  134. ).not.to.equal(ctx.hash0)
  135. })
  136. })
  137. describe("when a file's date is updated", function () {
  138. beforeEach(function (ctx) {
  139. ctx.files[0].file.created = 'zzzzzz'
  140. })
  141. it('should return a different hash value', function (ctx) {
  142. expect(
  143. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  144. ).not.to.equal(ctx.hash0)
  145. })
  146. })
  147. describe('when the compile options are changed', function () {
  148. beforeEach(function (ctx) {
  149. ctx.options.draft = !ctx.options.draft
  150. })
  151. it('should return a different hash value', function (ctx) {
  152. expect(
  153. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  154. ).not.to.equal(ctx.hash0)
  155. })
  156. })
  157. describe('when the isAutoCompile option is changed', function () {
  158. beforeEach(function (ctx) {
  159. ctx.options.isAutoCompile = !ctx.options.isAutoCompile
  160. })
  161. it('should return the same hash value', function (ctx) {
  162. expect(
  163. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  164. ).to.equal(ctx.hash0)
  165. })
  166. })
  167. describe('when the buildId option is changed', function () {
  168. beforeEach(function (ctx) {
  169. ctx.options.buildId = 'new-build-id'
  170. })
  171. it('should return the same hash value', function (ctx) {
  172. expect(
  173. ctx.ClsiStateManager.computeHash(ctx.project, ctx.options)
  174. ).to.equal(ctx.hash0)
  175. })
  176. })
  177. })
  178. })