TpdsProjectFlusherTests.js 7.9 KB

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