| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- const { expect } = require('chai')
- const sinon = require('sinon')
- const SandboxedModule = require('sandboxed-module')
- const { ObjectId } = require('mongodb')
- const { Project } = require('../helpers/models/Project')
- const MODULE_PATH =
- '../../../../app/src/Features/ThirdPartyDataStore/TpdsProjectFlusher'
- describe('TpdsProjectFlusher', function () {
- beforeEach(function () {
- this.project = { _id: ObjectId() }
- this.folder = { _id: ObjectId() }
- this.docs = {
- '/doc/one': {
- _id: 'mock-doc-1',
- lines: ['one'],
- rev: 5,
- folder: this.folder,
- },
- '/doc/two': {
- _id: 'mock-doc-2',
- lines: ['two'],
- rev: 6,
- folder: this.folder,
- },
- }
- this.files = {
- '/file/one': { _id: 'mock-file-1', rev: 7, folder: this.folder },
- '/file/two': { _id: 'mock-file-2', rev: 8, folder: this.folder },
- }
- this.DocumentUpdaterHandler = {
- promises: {
- flushProjectToMongo: sinon.stub().resolves(),
- },
- }
- this.ProjectGetter = {
- promises: {
- getProject: sinon.stub().resolves(this.project),
- },
- }
- this.ProjectEntityHandler = {
- promises: {
- getAllDocs: sinon.stub().withArgs(this.project._id).resolves(this.docs),
- getAllFiles: sinon
- .stub()
- .withArgs(this.project._id)
- .resolves(this.files),
- },
- }
- this.TpdsUpdateSender = {
- promises: {
- addDoc: sinon.stub().resolves(),
- addFile: sinon.stub().resolves(),
- },
- }
- this.ProjectMock = sinon.mock(Project)
- this.TpdsProjectFlusher = SandboxedModule.require(MODULE_PATH, {
- requires: {
- '../DocumentUpdater/DocumentUpdaterHandler':
- this.DocumentUpdaterHandler,
- '../Project/ProjectGetter': this.ProjectGetter,
- '../Project/ProjectEntityHandler': this.ProjectEntityHandler,
- '../../models/Project': { Project },
- './TpdsUpdateSender': this.TpdsUpdateSender,
- },
- })
- })
- afterEach(function () {
- this.ProjectMock.restore()
- })
- describe('flushProjectToTpds', function () {
- describe('usually', function () {
- beforeEach(async function () {
- await this.TpdsProjectFlusher.promises.flushProjectToTpds(
- this.project._id
- )
- })
- it('should flush the project from the doc updater', function () {
- expect(
- this.DocumentUpdaterHandler.promises.flushProjectToMongo
- ).to.have.been.calledWith(this.project._id)
- })
- it('should flush each doc to the TPDS', function () {
- for (const [path, doc] of Object.entries(this.docs)) {
- expect(this.TpdsUpdateSender.promises.addDoc).to.have.been.calledWith(
- {
- projectId: this.project._id,
- docId: doc._id,
- projectName: this.project.name,
- rev: doc.rev,
- path,
- folderId: this.folder._id,
- }
- )
- }
- })
- it('should flush each file to the TPDS', function () {
- for (const [path, file] of Object.entries(this.files)) {
- expect(
- this.TpdsUpdateSender.promises.addFile
- ).to.have.been.calledWith({
- projectId: this.project._id,
- fileId: file._id,
- projectName: this.project.name,
- rev: file.rev,
- path,
- folderId: this.folder._id,
- })
- }
- })
- })
- describe('when a TPDS flush is pending', function () {
- beforeEach(async function () {
- this.project.deferredTpdsFlushCounter = 2
- this.ProjectMock.expects('updateOne')
- .withArgs(
- {
- _id: this.project._id,
- deferredTpdsFlushCounter: { $lte: 2 },
- },
- { $set: { deferredTpdsFlushCounter: 0 } }
- )
- .chain('exec')
- .resolves()
- await this.TpdsProjectFlusher.promises.flushProjectToTpds(
- this.project._id
- )
- })
- it('resets the deferred flush counter', function () {
- this.ProjectMock.verify()
- })
- })
- })
- describe('deferProjectFlushToTpds', function () {
- beforeEach(async function () {
- this.ProjectMock.expects('updateOne')
- .withArgs(
- {
- _id: this.project._id,
- },
- { $inc: { deferredTpdsFlushCounter: 1 } }
- )
- .chain('exec')
- .resolves()
- await this.TpdsProjectFlusher.promises.deferProjectFlushToTpds(
- this.project._id
- )
- })
- it('increments the deferred flush counter', function () {
- this.ProjectMock.verify()
- })
- })
- describe('flushProjectToTpdsIfNeeded', function () {
- let cases = [0, undefined]
- cases.forEach(counterValue => {
- describe(`when the deferred flush counter is ${counterValue}`, function () {
- beforeEach(async function () {
- this.project.deferredTpdsFlushCounter = counterValue
- await this.TpdsProjectFlusher.promises.flushProjectToTpdsIfNeeded(
- this.project._id
- )
- })
- it("doesn't flush the project from the doc updater", function () {
- expect(this.DocumentUpdaterHandler.promises.flushProjectToMongo).not
- .to.have.been.called
- })
- it("doesn't flush any doc", function () {
- expect(this.TpdsUpdateSender.promises.addDoc).not.to.have.been.called
- })
- it("doesn't flush any file", function () {
- expect(this.TpdsUpdateSender.promises.addFile).not.to.have.been.called
- })
- })
- })
- cases = [1, 2]
- cases.forEach(counterValue => {
- describe(`when the deferred flush counter is ${counterValue}`, function () {
- beforeEach(async function () {
- this.project.deferredTpdsFlushCounter = counterValue
- this.ProjectMock.expects('updateOne')
- .withArgs(
- {
- _id: this.project._id,
- deferredTpdsFlushCounter: { $lte: counterValue },
- },
- { $set: { deferredTpdsFlushCounter: 0 } }
- )
- .chain('exec')
- .resolves()
- await this.TpdsProjectFlusher.promises.flushProjectToTpdsIfNeeded(
- this.project._id
- )
- })
- it('flushes the project from the doc updater', function () {
- expect(
- this.DocumentUpdaterHandler.promises.flushProjectToMongo
- ).to.have.been.calledWith(this.project._id)
- })
- it('flushes each doc to the TPDS', function () {
- for (const [path, doc] of Object.entries(this.docs)) {
- expect(
- this.TpdsUpdateSender.promises.addDoc
- ).to.have.been.calledWith({
- projectId: this.project._id,
- docId: doc._id,
- projectName: this.project.name,
- rev: doc.rev,
- path,
- folderId: this.folder._id,
- })
- }
- })
- it('flushes each file to the TPDS', function () {
- for (const [path, file] of Object.entries(this.files)) {
- expect(
- this.TpdsUpdateSender.promises.addFile
- ).to.have.been.calledWith({
- projectId: this.project._id,
- fileId: file._id,
- projectName: this.project.name,
- rev: file.rev,
- path,
- folderId: this.folder._id,
- })
- }
- })
- it('resets the deferred flush counter', function () {
- this.ProjectMock.verify()
- })
- })
- })
- })
- })
|