Get append end point working with a simple acceptance test

This commit is contained in:
James Allen 2014-01-27 17:51:09 +00:00
parent 533b8e59a3
commit 5dea123b13
7 changed files with 88 additions and 32 deletions

View file

@ -1,4 +1,6 @@
**.swp **.swp
node_modules/ node_modules/
app/js app/js
app.js
test/unit/js test/unit/js
test/acceptance/js

View file

@ -1,20 +1,26 @@
Settings = require "settings-sharelatex"
express = require "express" express = require "express"
app = express() app = express()
ConversoinManager = require "./app/js/ConversionManager" HistoryManager = require "./app/js/HistoryManager"
logger = require "logger-sharelatex" logger = require "logger-sharelatex"
logger.initialize("history") logger.initialize("history")
app.post express.bodyParser(), "/doc/:doc_id/flush", (req, res, next) -> app.post "/doc/:doc_id/history", express.bodyParser(), (req, res, next) ->
project_id = req.params.project_id doc_id = req.params.doc_id
docOps = req.body.docOps docOps = req.body.docOps
logger.log doc_id: doc_id, "compressing doc history" version = req.body.version
ConversionManager.convertAndSaveRawOps doc_id, docOps, (error) -> logger.log doc_id: doc_id, version: version, "compressing doc history"
HistoryManager.compressAndSaveRawUpdates doc_id, docOps, (error) ->
return next(error) if error? return next(error) if error?
res.send 204 # No content res.send 204
app.use (error, req, res, next) -> app.use (error, req, res, next) ->
logger.error err: error, "an internal error occured" logger.error err: error, "an internal error occured"
req.send 500 req.send 500
app.listen(3014) app.listen (Settings.port ||= 3014), (error) ->
if error?
logger.error err: error, "could not start history server"
logger.log "history api listening on port 3014"

View file

@ -1,6 +1,7 @@
{db, ObjectId} = require "./mongojs" {db, ObjectId} = require "./mongojs"
UpdateCompressor = require "./UpdateCompressor" UpdateCompressor = require "./UpdateCompressor"
logger = require "logger-sharelatex" logger = require "logger-sharelatex"
async = require "async"
module.exports = HistoryManager = module.exports = HistoryManager =
getLastCompressedUpdate: (doc_id, callback = (error, update) ->) -> getLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
@ -25,15 +26,19 @@ module.exports = HistoryManager =
else else
callback null, null callback null, null
insertCompressedUpdates: (doc_id, docUpdates, callback = (error) ->) -> insertCompressedUpdates: (doc_id, updates, callback = (error) ->) ->
db.docHistory.update { jobs = []
doc_id: ObjectId(doc_id) for update in updates
}, { do (update) ->
$push: jobs.push (callback) -> HistoryManager.insertCompressedUpdate doc_id, update, callback
docUpdates: async.series jobs, callback
$each: docUpdates
}, { insertCompressedUpdate: (doc_id, update, callback = (error) ->) ->
upsert: true logger.log doc_id: doc_id, update: update, "inserting compressed update"
db.docHistory.insert {
doc_id: ObjectId(doc_id.toString())
op: update.op
meta: update.meta
}, callback }, callback
compressAndSaveRawUpdates: (doc_id, rawUpdates, callback = (error) ->) -> compressAndSaveRawUpdates: (doc_id, rawUpdates, callback = (error) ->) ->
@ -41,12 +46,9 @@ module.exports = HistoryManager =
if length == 0 if length == 0
return callback() return callback()
HistoryManager.popLastCompressedUpdate doc_id, (error, lastCompressedUpdate) -> HistoryManager.popLastCompressedUpdate doc_id, (error, lastCompressedUpdate) ->
return callback(error) if error? return callback(error) if error?
compressedUpdates = UpdateCompressor.compressRawUpdates lastCompressedUpdate, rawUpdates compressedUpdates = UpdateCompressor.compressRawUpdates lastCompressedUpdate, rawUpdates
HistoryManager.insertCompressedUpdates doc_id, compressedUpdates, (error) -> HistoryManager.insertCompressedUpdates doc_id, compressedUpdates, (error) ->
return callback(error) if error? return callback(error) if error?
logger.log doc_id: doc_id, rawUpdatesLength: length, compressedUpdatesLength: compressedUpdates.length, "compressed doc updates" logger.log doc_id: doc_id, rawUpdatesLength: length, compressedUpdatesLength: compressedUpdates.length, "compressed doc updates"

View file

@ -1,3 +1,4 @@
module.exports = module.exports =
mongo: mongo:
url: 'mongodb://127.0.0.1/sharelatexTesting' url: 'mongodb://127.0.0.1/sharelatexTesting'
port: 3014

View file

@ -1,14 +1,15 @@
{ {
"name": "history-sharelatex", "name": "history-sharelatex",
"version": "0.0.1", "version": "0.0.1",
"dependencies": { "dependencies": {
"async": "", "async": "",
"chai": "", "chai": "",
"express": "3.3.5", "express": "3.3.5",
"sandboxed-module": "", "sandboxed-module": "",
"sinon": "", "sinon": "",
"mongojs": "0.7.2", "mongojs": "~0.9.11",
"settings": "git+ssh://git@bitbucket.org:sharelatex/settings-sharelatex.git#master", "settings": "git+ssh://git@bitbucket.org:sharelatex/settings-sharelatex.git#master",
"logger": "git+ssh://git@bitbucket.org:sharelatex/logger-sharelatex.git#bunyan" "logger": "git+ssh://git@bitbucket.org:sharelatex/logger-sharelatex.git#bunyan",
} "request": "~2.33.0"
}
} }

View file

@ -38,6 +38,12 @@ namespace 'compile' do
end end
puts 'finished app compile' puts 'finished app compile'
end end
sh %{coffee -c app.coffee} do |ok, res|
if ! ok
raise "error compiling root app file: #{res}"
end
puts 'finished root app file compile'
end
end end
desc "compiles unit tests" desc "compiles unit tests"

View file

@ -0,0 +1,38 @@
sinon = require "sinon"
chai = require("chai")
chai.should()
mongojs = require "../../../app/js/mongojs"
db = mongojs.db
ObjectId = mongojs.ObjectId
Settings = require "settings-sharelatex"
request = require "request"
describe "Appending doc ops to the history", ->
describe "when the history does not exist yet", ->
before (done) ->
@doc_id = ObjectId().toString()
@user_id = ObjectId().toString()
@updates = [{
op: [{ i: "f", p: 3 }]
meta: { ts: Date.now(), user_id: @user_id }
}, {
op: [{ i: "o", p: 4 }]
meta: { ts: Date.now(), user_id: @user_id }
}, {
op: [{ i: "o", p: 5 }]
meta: { ts: Date.now(), user_id: @user_id }
}]
@version = 3
request.post {
url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/history"
json:
version: @version
docOps: @updates
}, (@error, @response, @body) =>
done()
it "should return a successful response", ->
@response.statusCode.should.equal 204