2014-04-29 06:49:09 -04:00
|
|
|
sinon = require "sinon"
|
|
|
|
chai = require("chai")
|
|
|
|
chai.should()
|
|
|
|
{ObjectId} = require "mongojs"
|
2018-05-23 06:27:31 -04:00
|
|
|
DocstoreApp = require "./helpers/DocstoreApp"
|
2014-04-29 06:49:09 -04:00
|
|
|
|
|
|
|
DocstoreClient = require "./helpers/DocstoreClient"
|
|
|
|
|
|
|
|
describe "Applying updates to a doc", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@project_id = ObjectId()
|
2014-04-30 08:06:12 -04:00
|
|
|
@doc_id = ObjectId()
|
2014-04-29 06:49:09 -04:00
|
|
|
@originalLines = ["original", "lines"]
|
|
|
|
@newLines = ["new", "lines"]
|
2016-12-05 09:21:49 -05:00
|
|
|
@originalRanges = {
|
|
|
|
changes: [{
|
|
|
|
id: ObjectId().toString()
|
|
|
|
op: { i: "foo", p: 3 }
|
|
|
|
meta:
|
|
|
|
user_id: ObjectId().toString()
|
|
|
|
ts: new Date().toString()
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
@newRanges = {
|
|
|
|
changes: [{
|
|
|
|
id: ObjectId().toString()
|
|
|
|
op: { i: "bar", p: 6 }
|
|
|
|
meta:
|
|
|
|
user_id: ObjectId().toString()
|
|
|
|
ts: new Date().toString()
|
|
|
|
}]
|
|
|
|
}
|
2016-11-28 09:55:16 -05:00
|
|
|
@version = 42
|
2018-05-23 06:27:31 -04:00
|
|
|
DocstoreApp.ensureRunning =>
|
|
|
|
DocstoreClient.createDoc @project_id, @doc_id, @originalLines, @version, @originalRanges, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
done()
|
2016-12-05 09:21:49 -05:00
|
|
|
|
|
|
|
describe "when nothing has been updated", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
DocstoreClient.updateDoc @project_id, @doc_id, @originalLines, @version, @originalRanges, (error, res, @body) =>
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return modified = false", ->
|
|
|
|
@body.modified.should.equal false
|
|
|
|
|
|
|
|
it "should not update the doc in the API", (done) ->
|
|
|
|
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
|
|
|
|
doc.lines.should.deep.equal @originalLines
|
|
|
|
doc.version.should.equal @version
|
|
|
|
doc.ranges.should.deep.equal @originalRanges
|
2016-11-28 09:55:16 -05:00
|
|
|
done()
|
2014-04-29 06:49:09 -04:00
|
|
|
|
2016-12-02 10:17:38 -05:00
|
|
|
describe "when the lines have changed", ->
|
2014-04-29 06:49:09 -04:00
|
|
|
beforeEach (done) ->
|
2016-12-05 09:21:49 -05:00
|
|
|
DocstoreClient.updateDoc @project_id, @doc_id, @newLines, @version, @originalRanges, (error, res, @body) =>
|
2014-04-29 06:49:09 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return modified = true", ->
|
|
|
|
@body.modified.should.equal true
|
|
|
|
|
2015-02-20 09:28:16 -05:00
|
|
|
it "should return the rev", ->
|
|
|
|
@body.rev.should.equal 2
|
|
|
|
|
2016-12-05 09:21:49 -05:00
|
|
|
it "should update the doc in the API", (done) ->
|
2015-02-03 09:05:08 -05:00
|
|
|
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
|
2014-04-29 06:49:09 -04:00
|
|
|
doc.lines.should.deep.equal @newLines
|
2016-11-28 09:55:16 -05:00
|
|
|
doc.version.should.equal @version
|
2016-12-05 09:21:49 -05:00
|
|
|
doc.ranges.should.deep.equal @originalRanges
|
2014-04-29 06:49:09 -04:00
|
|
|
done()
|
|
|
|
|
2016-12-05 09:21:49 -05:00
|
|
|
describe "when the version has changed", ->
|
2014-04-29 06:49:09 -04:00
|
|
|
beforeEach (done) ->
|
2016-12-05 09:21:49 -05:00
|
|
|
DocstoreClient.updateDoc @project_id, @doc_id, @originalLines, @version + 1, @originalRanges, (error, res, @body) =>
|
2014-04-29 06:49:09 -04:00
|
|
|
done()
|
|
|
|
|
2016-12-05 09:21:49 -05:00
|
|
|
it "should return modified = true", ->
|
|
|
|
@body.modified.should.equal true
|
2014-04-29 06:49:09 -04:00
|
|
|
|
2016-12-05 09:21:49 -05:00
|
|
|
it "should return the rev", ->
|
2016-12-05 12:27:31 -05:00
|
|
|
@body.rev.should.equal 1
|
2016-12-05 09:21:49 -05:00
|
|
|
|
|
|
|
it "should update the doc in the API", (done) ->
|
2015-02-03 09:05:08 -05:00
|
|
|
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
|
2014-04-29 06:49:09 -04:00
|
|
|
doc.lines.should.deep.equal @originalLines
|
2016-12-05 09:21:49 -05:00
|
|
|
doc.version.should.equal @version + 1
|
|
|
|
doc.ranges.should.deep.equal @originalRanges
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "when the ranges have changed", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
DocstoreClient.updateDoc @project_id, @doc_id, @originalLines, @version, @newRanges, (error, res, @body) =>
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return modified = true", ->
|
|
|
|
@body.modified.should.equal true
|
|
|
|
|
|
|
|
it "should return the rev", ->
|
|
|
|
@body.rev.should.equal 2
|
|
|
|
|
|
|
|
it "should update the doc in the API", (done) ->
|
|
|
|
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
|
|
|
|
doc.lines.should.deep.equal @originalLines
|
|
|
|
doc.version.should.equal @version
|
|
|
|
doc.ranges.should.deep.equal @newRanges
|
2014-04-29 06:49:09 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
describe "when the doc does not exist", ->
|
|
|
|
beforeEach (done) ->
|
2015-02-20 09:28:16 -05:00
|
|
|
@missing_doc_id = ObjectId()
|
2016-12-05 09:21:49 -05:00
|
|
|
DocstoreClient.updateDoc @project_id, @missing_doc_id, @originalLines, 0, @originalRanges, (error, @res, @body) =>
|
2014-04-29 06:49:09 -04:00
|
|
|
done()
|
|
|
|
|
2015-02-20 09:28:16 -05:00
|
|
|
it "should create the doc", ->
|
|
|
|
@body.rev.should.equal 1
|
2014-04-29 06:49:09 -04:00
|
|
|
|
2015-02-20 09:28:16 -05:00
|
|
|
it "should be retreivable", (done)->
|
|
|
|
DocstoreClient.getDoc @project_id, @missing_doc_id, {}, (error, res, doc) =>
|
|
|
|
doc.lines.should.deep.equal @originalLines
|
2016-11-28 09:55:16 -05:00
|
|
|
doc.version.should.equal 0
|
2016-12-05 09:21:49 -05:00
|
|
|
doc.ranges.should.deep.equal @originalRanges
|
2014-04-29 06:49:09 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
describe "when malformed doc lines are provided", ->
|
|
|
|
describe "when the lines are not an array", ->
|
|
|
|
beforeEach (done) ->
|
2016-12-05 09:21:49 -05:00
|
|
|
DocstoreClient.updateDoc @project_id, @doc_id, { foo: "bar" }, @version, @originalRanges, (error, @res, @body) =>
|
2014-04-29 06:49:09 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return 400", ->
|
|
|
|
@res.statusCode.should.equal 400
|
|
|
|
|
|
|
|
it "should not update the doc in the API", (done) ->
|
2015-02-03 09:05:08 -05:00
|
|
|
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
|
2014-04-29 06:49:09 -04:00
|
|
|
doc.lines.should.deep.equal @originalLines
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "when the lines are not present", ->
|
|
|
|
beforeEach (done) ->
|
2016-12-05 09:21:49 -05:00
|
|
|
DocstoreClient.updateDoc @project_id, @doc_id, null, @version, @originalRanges, (error, @res, @body) =>
|
2014-04-29 06:49:09 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return 400", ->
|
|
|
|
@res.statusCode.should.equal 400
|
|
|
|
|
|
|
|
it "should not update the doc in the API", (done) ->
|
2015-02-03 09:05:08 -05:00
|
|
|
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
|
2014-04-29 06:49:09 -04:00
|
|
|
doc.lines.should.deep.equal @originalLines
|
|
|
|
done()
|
2016-12-02 10:17:38 -05:00
|
|
|
|
|
|
|
describe "when no version is provided", ->
|
|
|
|
beforeEach (done) ->
|
2016-12-05 09:21:49 -05:00
|
|
|
DocstoreClient.updateDoc @project_id, @doc_id, @originalLines, null, @originalRanges, (error, @res, @body) =>
|
2016-12-02 10:17:38 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return 400", ->
|
|
|
|
@res.statusCode.should.equal 400
|
|
|
|
|
|
|
|
it "should not update the doc in the API", (done) ->
|
|
|
|
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
|
|
|
|
doc.lines.should.deep.equal @originalLines
|
|
|
|
doc.version.should.equal @version
|
|
|
|
done()
|
2014-04-29 06:49:09 -04:00
|
|
|
|
2014-05-13 07:54:58 -04:00
|
|
|
describe "when the content is large", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
line = new Array(1025).join("x") # 1kb
|
|
|
|
@largeLines = Array.apply(null, Array(1024)).map(() -> line) # 1mb
|
2016-12-05 09:21:49 -05:00
|
|
|
DocstoreClient.updateDoc @project_id, @doc_id, @largeLines, @version, @originalRanges, (error, res, @body) =>
|
2014-05-13 07:54:58 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return modified = true", ->
|
|
|
|
@body.modified.should.equal true
|
|
|
|
|
|
|
|
it "should update the doc in the API", (done) ->
|
2015-02-03 09:05:08 -05:00
|
|
|
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
|
2014-05-13 07:54:58 -04:00
|
|
|
doc.lines.should.deep.equal @largeLines
|
|
|
|
done()
|