TpdsProjectFlusher.test.mjs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import mongodb from 'mongodb-legacy'
  4. import { Project } from '../../../../app/src/models/Project.mjs'
  5. const { ObjectId } = mongodb
  6. const MODULE_PATH =
  7. '../../../../app/src/Features/ThirdPartyDataStore/TpdsProjectFlusher'
  8. describe('TpdsProjectFlusher', function () {
  9. beforeEach(async function (ctx) {
  10. ctx.project = { _id: new ObjectId(), overleaf: { history: { id: 42 } } }
  11. ctx.folder = { _id: new ObjectId() }
  12. ctx.docs = {
  13. '/doc/one': {
  14. _id: 'mock-doc-1',
  15. lines: ['one'],
  16. rev: 5,
  17. folder: ctx.folder,
  18. },
  19. '/doc/two': {
  20. _id: 'mock-doc-2',
  21. lines: ['two'],
  22. rev: 6,
  23. folder: ctx.folder,
  24. },
  25. }
  26. ctx.files = {
  27. '/file/one': {
  28. _id: 'mock-file-1',
  29. rev: 7,
  30. folder: ctx.folder,
  31. hash: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
  32. },
  33. '/file/two': {
  34. _id: 'mock-file-2',
  35. rev: 8,
  36. folder: ctx.folder,
  37. hash: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
  38. },
  39. }
  40. ctx.DocumentUpdaterHandler = {
  41. promises: {
  42. flushProjectToMongo: sinon.stub().resolves(),
  43. },
  44. }
  45. ctx.ProjectGetter = {
  46. promises: {
  47. getProject: sinon.stub().resolves(ctx.project),
  48. },
  49. }
  50. ctx.ProjectEntityHandler = {
  51. promises: {
  52. getAllDocs: sinon.stub().withArgs(ctx.project._id).resolves(ctx.docs),
  53. getAllFiles: sinon.stub().withArgs(ctx.project._id).resolves(ctx.files),
  54. },
  55. }
  56. ctx.TpdsUpdateSender = {
  57. promises: {
  58. addDoc: sinon.stub().resolves(),
  59. addFile: sinon.stub().resolves(),
  60. },
  61. }
  62. ctx.ProjectMock = sinon.mock(Project)
  63. vi.doMock(
  64. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler',
  65. () => ({
  66. default: ctx.DocumentUpdaterHandler,
  67. })
  68. )
  69. vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
  70. default: ctx.ProjectGetter,
  71. }))
  72. vi.doMock(
  73. '../../../../app/src/Features/Project/ProjectEntityHandler',
  74. () => ({
  75. default: ctx.ProjectEntityHandler,
  76. })
  77. )
  78. vi.doMock('../../../../app/src/models/Project', () => ({
  79. Project,
  80. }))
  81. vi.doMock(
  82. '../../../../app/src/Features/ThirdPartyDataStore/TpdsUpdateSender',
  83. () => ({
  84. default: ctx.TpdsUpdateSender,
  85. })
  86. )
  87. ctx.TpdsProjectFlusher = (await import(MODULE_PATH)).default
  88. })
  89. afterEach(function (ctx) {
  90. ctx.ProjectMock.restore()
  91. })
  92. describe('flushProjectToTpds', function () {
  93. describe('usually', function () {
  94. beforeEach(async function (ctx) {
  95. await ctx.TpdsProjectFlusher.promises.flushProjectToTpds(
  96. ctx.project._id
  97. )
  98. })
  99. it('should flush the project from the doc updater', function (ctx) {
  100. expect(
  101. ctx.DocumentUpdaterHandler.promises.flushProjectToMongo
  102. ).to.have.been.calledWith(ctx.project._id)
  103. })
  104. it('should flush each doc to the TPDS', function (ctx) {
  105. for (const [path, doc] of Object.entries(ctx.docs)) {
  106. expect(ctx.TpdsUpdateSender.promises.addDoc).to.have.been.calledWith({
  107. projectId: ctx.project._id,
  108. docId: doc._id,
  109. projectName: ctx.project.name,
  110. rev: doc.rev,
  111. path,
  112. folderId: ctx.folder._id,
  113. })
  114. }
  115. })
  116. it('should flush each file to the TPDS', function (ctx) {
  117. for (const [path, file] of Object.entries(ctx.files)) {
  118. expect(ctx.TpdsUpdateSender.promises.addFile).to.have.been.calledWith(
  119. {
  120. projectId: ctx.project._id,
  121. historyId: ctx.project.overleaf.history.id,
  122. fileId: file._id,
  123. hash: file.hash,
  124. projectName: ctx.project.name,
  125. rev: file.rev,
  126. path,
  127. folderId: ctx.folder._id,
  128. }
  129. )
  130. }
  131. })
  132. })
  133. describe('when a TPDS flush is pending', function () {
  134. beforeEach(async function (ctx) {
  135. ctx.project.deferredTpdsFlushCounter = 2
  136. ctx.ProjectMock.expects('updateOne')
  137. .withArgs(
  138. {
  139. _id: ctx.project._id,
  140. deferredTpdsFlushCounter: { $lte: 2 },
  141. },
  142. { $set: { deferredTpdsFlushCounter: 0 } }
  143. )
  144. .chain('exec')
  145. .resolves()
  146. await ctx.TpdsProjectFlusher.promises.flushProjectToTpds(
  147. ctx.project._id
  148. )
  149. })
  150. it('resets the deferred flush counter', function (ctx) {
  151. ctx.ProjectMock.verify()
  152. })
  153. })
  154. })
  155. describe('deferProjectFlushToTpds', function () {
  156. beforeEach(async function (ctx) {
  157. ctx.ProjectMock.expects('updateOne')
  158. .withArgs(
  159. {
  160. _id: ctx.project._id,
  161. },
  162. { $inc: { deferredTpdsFlushCounter: 1 } }
  163. )
  164. .chain('exec')
  165. .resolves()
  166. await ctx.TpdsProjectFlusher.promises.deferProjectFlushToTpds(
  167. ctx.project._id
  168. )
  169. })
  170. it('increments the deferred flush counter', function (ctx) {
  171. ctx.ProjectMock.verify()
  172. })
  173. })
  174. describe('flushProjectToTpdsIfNeeded', function () {
  175. let cases = [0, undefined]
  176. cases.forEach(counterValue => {
  177. describe(`when the deferred flush counter is ${counterValue}`, function () {
  178. beforeEach(async function (ctx) {
  179. ctx.project.deferredTpdsFlushCounter = counterValue
  180. await ctx.TpdsProjectFlusher.promises.flushProjectToTpdsIfNeeded(
  181. ctx.project
  182. )
  183. })
  184. it("doesn't flush the project from the doc updater", function (ctx) {
  185. expect(ctx.DocumentUpdaterHandler.promises.flushProjectToMongo).not.to
  186. .have.been.called
  187. })
  188. it("doesn't flush any doc", function (ctx) {
  189. expect(ctx.TpdsUpdateSender.promises.addDoc).not.to.have.been.called
  190. })
  191. it("doesn't flush any file", function (ctx) {
  192. expect(ctx.TpdsUpdateSender.promises.addFile).not.to.have.been.called
  193. })
  194. })
  195. })
  196. cases = [1, 2]
  197. cases.forEach(counterValue => {
  198. describe(`when the deferred flush counter is ${counterValue}`, function () {
  199. beforeEach(async function (ctx) {
  200. ctx.project.deferredTpdsFlushCounter = counterValue
  201. ctx.ProjectMock.expects('updateOne')
  202. .withArgs(
  203. {
  204. _id: ctx.project._id,
  205. deferredTpdsFlushCounter: { $lte: counterValue },
  206. },
  207. { $set: { deferredTpdsFlushCounter: 0 } }
  208. )
  209. .chain('exec')
  210. .resolves()
  211. await ctx.TpdsProjectFlusher.promises.flushProjectToTpdsIfNeeded(
  212. ctx.project
  213. )
  214. })
  215. it('flushes the project from the doc updater', function (ctx) {
  216. expect(
  217. ctx.DocumentUpdaterHandler.promises.flushProjectToMongo
  218. ).to.have.been.calledWith(ctx.project._id)
  219. })
  220. it('flushes each doc to the TPDS', function (ctx) {
  221. for (const [path, doc] of Object.entries(ctx.docs)) {
  222. expect(
  223. ctx.TpdsUpdateSender.promises.addDoc
  224. ).to.have.been.calledWith({
  225. projectId: ctx.project._id,
  226. docId: doc._id,
  227. projectName: ctx.project.name,
  228. rev: doc.rev,
  229. path,
  230. folderId: ctx.folder._id,
  231. })
  232. }
  233. })
  234. it('flushes each file to the TPDS', function (ctx) {
  235. for (const [path, file] of Object.entries(ctx.files)) {
  236. expect(
  237. ctx.TpdsUpdateSender.promises.addFile
  238. ).to.have.been.calledWith({
  239. projectId: ctx.project._id,
  240. historyId: ctx.project.overleaf.history.id,
  241. fileId: file._id,
  242. hash: file.hash,
  243. projectName: ctx.project.name,
  244. rev: file.rev,
  245. path,
  246. folderId: ctx.folder._id,
  247. })
  248. }
  249. })
  250. it('resets the deferred flush counter', function (ctx) {
  251. ctx.ProjectMock.verify()
  252. })
  253. })
  254. })
  255. })
  256. })