mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-23 17:19:37 +02:00
Merge pull request #30312 from overleaf/mj-mock-web-api-async
[document-updater] Remove callbacks from MockWebApi GitOrigin-RevId: 0189d5735f3e90160e593fb04043a10b1e922685
This commit is contained in:
committed by
Copybot
parent
ea4dcd4452
commit
82cb0e2839
@@ -147,14 +147,7 @@ describe('Getting a document', function () {
|
||||
|
||||
describe('when the web api returns an error', function () {
|
||||
before(function () {
|
||||
sinon
|
||||
.stub(MockWebApi, 'getDocument')
|
||||
.callsFake((projectId, docId, callback) => {
|
||||
if (callback == null) {
|
||||
callback = function () {}
|
||||
}
|
||||
callback(new Error('oops'))
|
||||
})
|
||||
sinon.stub(MockWebApi, 'getDocument').rejects(new Error('oops'))
|
||||
})
|
||||
|
||||
after(function () {
|
||||
@@ -173,14 +166,11 @@ describe('Getting a document', function () {
|
||||
describe('when the web api http request takes a long time', function () {
|
||||
before(function (done) {
|
||||
this.timeout = 10000
|
||||
sinon
|
||||
.stub(MockWebApi, 'getDocument')
|
||||
.callsFake((projectId, docId, callback) => {
|
||||
if (callback == null) {
|
||||
callback = function () {}
|
||||
}
|
||||
setTimeout(callback, 30000)
|
||||
sinon.stub(MockWebApi, 'getDocument').returns(
|
||||
new Promise(resolve => {
|
||||
setTimeout(() => resolve(null), 30000)
|
||||
})
|
||||
)
|
||||
done()
|
||||
})
|
||||
|
||||
|
||||
@@ -205,15 +205,7 @@ describe('Ranges', function () {
|
||||
|
||||
it('should flush the ranges to the persistence layer again', async function () {
|
||||
await DocUpdaterClient.flushDoc(this.project_id, this.doc.id)
|
||||
const doc = await new Promise((resolve, reject) => {
|
||||
MockWebApi.getDocument(this.project_id, this.doc.id, (error, doc) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
} else {
|
||||
resolve(doc)
|
||||
}
|
||||
})
|
||||
})
|
||||
const doc = await MockWebApi.getDocument(this.project_id, this.doc.id)
|
||||
const { changes } = doc.ranges
|
||||
changes[0].op.should.deep.equal({ i: '123', p: 1 })
|
||||
changes[1].op.should.deep.equal({ i: '456', p: 5 })
|
||||
|
||||
@@ -1,17 +1,7 @@
|
||||
/* eslint-disable
|
||||
no-return-assign,
|
||||
*/
|
||||
// TODO: This file was created by bulk-decaffeinate.
|
||||
// Fix any style issues and re-enable lint.
|
||||
/*
|
||||
* decaffeinate suggestions:
|
||||
* DS102: Remove unnecessary code created because of implicit returns
|
||||
* DS207: Consider shorter variations of null checks
|
||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
let MockWebApi
|
||||
const express = require('express')
|
||||
const bodyParser = require('body-parser')
|
||||
const { expressify } = require('@overleaf/promise-utils')
|
||||
const app = express()
|
||||
const MAX_REQUEST_SIZE = 2 * (2 * 1024 * 1024 + 64 * 1024)
|
||||
|
||||
@@ -33,19 +23,15 @@ module.exports = MockWebApi = {
|
||||
return (this.docs[`${projectId}:${docId}`] = doc)
|
||||
},
|
||||
|
||||
setDocument(
|
||||
async setDocument(
|
||||
projectId,
|
||||
docId,
|
||||
lines,
|
||||
version,
|
||||
ranges,
|
||||
lastUpdatedAt,
|
||||
lastUpdatedBy,
|
||||
callback
|
||||
lastUpdatedBy
|
||||
) {
|
||||
if (callback == null) {
|
||||
callback = function () {}
|
||||
}
|
||||
const doc =
|
||||
this.docs[`${projectId}:${docId}`] ||
|
||||
(this.docs[`${projectId}:${docId}`] = {})
|
||||
@@ -55,54 +41,51 @@ module.exports = MockWebApi = {
|
||||
doc.pathname = '/a/b/c.tex'
|
||||
doc.lastUpdatedAt = lastUpdatedAt
|
||||
doc.lastUpdatedBy = lastUpdatedBy
|
||||
return callback(null)
|
||||
},
|
||||
|
||||
getDocument(projectId, docId, callback) {
|
||||
if (callback == null) {
|
||||
callback = function () {}
|
||||
}
|
||||
return callback(null, this.docs[`${projectId}:${docId}`])
|
||||
async getDocument(projectId, docId) {
|
||||
return this.docs[`${projectId}:${docId}`]
|
||||
},
|
||||
|
||||
run() {
|
||||
app.get('/project/:project_id/doc/:doc_id', (req, res, next) => {
|
||||
return this.getDocument(
|
||||
req.params.project_id,
|
||||
req.params.doc_id,
|
||||
(error, doc) => {
|
||||
if (error != null) {
|
||||
return res.sendStatus(500)
|
||||
} else if (doc != null) {
|
||||
app.get(
|
||||
'/project/:project_id/doc/:doc_id',
|
||||
expressify(async (req, res, next) => {
|
||||
try {
|
||||
const doc = await this.getDocument(
|
||||
req.params.project_id,
|
||||
req.params.doc_id
|
||||
)
|
||||
if (doc != null) {
|
||||
return res.send(JSON.stringify(doc))
|
||||
} else {
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
} catch (error) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
app.post(
|
||||
'/project/:project_id/doc/:doc_id',
|
||||
bodyParser.json({ limit: MAX_REQUEST_SIZE }),
|
||||
(req, res, next) => {
|
||||
return MockWebApi.setDocument(
|
||||
req.params.project_id,
|
||||
req.params.doc_id,
|
||||
req.body.lines,
|
||||
req.body.version,
|
||||
req.body.ranges,
|
||||
req.body.lastUpdatedAt,
|
||||
req.body.lastUpdatedBy,
|
||||
error => {
|
||||
if (error != null) {
|
||||
return res.sendStatus(500)
|
||||
} else {
|
||||
return res.json({ rev: '123' })
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
expressify(async (req, res, next) => {
|
||||
try {
|
||||
await MockWebApi.setDocument(
|
||||
req.params.project_id,
|
||||
req.params.doc_id,
|
||||
req.body.lines,
|
||||
req.body.version,
|
||||
req.body.ranges,
|
||||
req.body.lastUpdatedAt,
|
||||
req.body.lastUpdatedBy
|
||||
)
|
||||
return res.json({ rev: '123' })
|
||||
} catch (error) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user