Merge pull request #28205 from overleaf/em-promisify-project-manager

Promisify ProjectManager

GitOrigin-RevId: d10807a9a7f11428dd6bfe41210bfd0ce32c37ba
This commit is contained in:
Eric Mc Sween
2025-09-03 08:06:42 +00:00
committed by Copybot
parent ce93941cf2
commit 2995b29829
5 changed files with 502 additions and 762 deletions
@@ -1,157 +1,125 @@
/* eslint-disable
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS206: Consider reworking classes to avoid initClass
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const { expect } = require('chai')
const sinon = require('sinon')
const modulePath = '../../../../app/js/ProjectManager.js'
const SandboxedModule = require('sandboxed-module')
const MODULE_PATH = '../../../../app/js/ProjectManager.js'
describe('ProjectManager - flushAndDeleteProject', function () {
beforeEach(function () {
let Timer
this.LockManager = {
getLock: sinon.stub().yields(),
releaseLock: sinon.stub().yields(),
this.project_id = 'project-id-123'
this.RedisManager = {
promises: {
getDocIdsInProject: sinon.stub(),
},
}
this.ProjectManager = SandboxedModule.require(modulePath, {
this.ProjectHistoryRedisManager = {}
this.DocumentManager = {
promises: {
flushAndDeleteDocWithLock: sinon.stub().resolves(),
},
}
this.HistoryManager = {
promises: {
flushProjectChanges: sinon.stub().resolves(),
},
}
this.Metrics = {
Timer: class Timer {},
}
this.Metrics.Timer.prototype.done = sinon.stub()
this.ProjectManager = SandboxedModule.require(MODULE_PATH, {
requires: {
'./RedisManager': (this.RedisManager = {}),
'./ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
'./DocumentManager': (this.DocumentManager = {}),
'./HistoryManager': (this.HistoryManager = {
flushProjectChanges: sinon.stub().callsArg(2),
}),
'./LockManager': this.LockManager,
'./Metrics': (this.Metrics = {
Timer: (Timer = (function () {
Timer = class Timer {
static initClass() {
this.prototype.done = sinon.stub()
}
}
Timer.initClass()
return Timer
})()),
}),
'./RedisManager': this.RedisManager,
'./ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
'./DocumentManager': this.DocumentManager,
'./HistoryManager': this.HistoryManager,
'./Metrics': this.Metrics,
},
})
this.project_id = 'project-id-123'
return (this.callback = sinon.stub())
})
describe('successfully', function () {
beforeEach(function (done) {
beforeEach(async function () {
this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
this.RedisManager.getDocIdsInProject = sinon
.stub()
.callsArgWith(1, null, this.doc_ids)
this.DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArg(3)
return this.ProjectManager.flushAndDeleteProjectWithLocks(
this.RedisManager.promises.getDocIdsInProject.resolves(this.doc_ids)
await this.ProjectManager.promises.flushAndDeleteProjectWithLocks(
this.project_id,
{},
error => {
this.callback(error)
return done()
}
{}
)
})
it('should get the doc ids in the project', function () {
return this.RedisManager.getDocIdsInProject
.calledWith(this.project_id)
.should.equal(true)
this.RedisManager.promises.getDocIdsInProject.should.have.been.calledWith(
this.project_id
)
})
it('should delete each doc in the project', function () {
return Array.from(this.doc_ids).map(docId =>
this.DocumentManager.flushAndDeleteDocWithLock
.calledWith(this.project_id, docId, {})
.should.equal(true)
)
for (const docId of this.doc_ids) {
this.DocumentManager.promises.flushAndDeleteDocWithLock.should.have.been.calledWith(
this.project_id,
docId,
{}
)
}
})
it('should flush project history', function () {
return this.HistoryManager.flushProjectChanges
.calledWith(this.project_id, {})
.should.equal(true)
this.HistoryManager.promises.flushProjectChanges.should.have.been.calledWith(
this.project_id,
{}
)
})
it('should call the callback without error', function () {
return this.callback.calledWith(null).should.equal(true)
})
return it('should time the execution', function () {
return this.Metrics.Timer.prototype.done.called.should.equal(true)
it('should time the execution', function () {
this.Metrics.Timer.prototype.done.called.should.equal(true)
})
})
return describe('when a doc errors', function () {
beforeEach(function (done) {
describe('when a doc errors', function () {
beforeEach(async function () {
this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
this.RedisManager.getDocIdsInProject = sinon
.stub()
.callsArgWith(1, null, this.doc_ids)
this.DocumentManager.flushAndDeleteDocWithLock = sinon.spy(
(projectId, docId, options, callback) => {
this.RedisManager.promises.getDocIdsInProject.resolves(this.doc_ids)
this.DocumentManager.promises.flushAndDeleteDocWithLock.callsFake(
async (projectId, docId, options) => {
if (docId === 'doc-id-1') {
return callback(
(this.error = new Error('oops, something went wrong'))
)
} else {
return callback()
throw new Error('oops, something went wrong')
}
}
)
return this.ProjectManager.flushAndDeleteProjectWithLocks(
this.project_id,
{},
error => {
this.callback(error)
return done()
}
)
await expect(
this.ProjectManager.promises.flushAndDeleteProjectWithLocks(
this.project_id,
{}
)
).to.be.rejected
})
it('should still flush each doc in the project', function () {
return Array.from(this.doc_ids).map(docId =>
this.DocumentManager.flushAndDeleteDocWithLock
.calledWith(this.project_id, docId, {})
.should.equal(true)
)
for (const docId of this.doc_ids) {
this.DocumentManager.promises.flushAndDeleteDocWithLock.should.have.been.calledWith(
this.project_id,
docId,
{}
)
}
})
it('should still flush project history', function () {
return this.HistoryManager.flushProjectChanges
.calledWith(this.project_id, {})
.should.equal(true)
this.HistoryManager.promises.flushProjectChanges.should.have.been.calledWith(
this.project_id,
{}
)
})
it('should record the error', function () {
return this.logger.error
.calledWith(
{ err: this.error, projectId: this.project_id, docId: 'doc-id-1' },
'error deleting doc'
)
.should.equal(true)
})
it('should call the callback with an error', function () {
return this.callback
.calledWith(sinon.match.instanceOf(Error))
.should.equal(true)
})
return it('should time the execution', function () {
return this.Metrics.Timer.prototype.done.called.should.equal(true)
it('should time the execution', function () {
this.Metrics.Timer.prototype.done.called.should.equal(true)
})
})
})