decaffeinate: Convert ArchiveDocsTests.coffee and 6 other files to JS

This commit is contained in:
decaffeinate 2020-02-16 14:03:02 +00:00 committed by Simon Detheridge
parent 93c718dae9
commit 73b0bc023f
7 changed files with 1098 additions and 743 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,85 +1,122 @@
sinon = require "sinon" /*
chai = require("chai") * decaffeinate suggestions:
chai.should() * DS102: Remove unnecessary code created because of implicit returns
{db, ObjectId} = require "../../../app/js/mongojs" * DS207: Consider shorter variations of null checks
expect = chai.expect * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
DocstoreApp = require "./helpers/DocstoreApp" */
const sinon = require("sinon");
const chai = require("chai");
chai.should();
const {db, ObjectId} = require("../../../app/js/mongojs");
const {
expect
} = chai;
const DocstoreApp = require("./helpers/DocstoreApp");
DocstoreClient = require "./helpers/DocstoreClient" const DocstoreClient = require("./helpers/DocstoreClient");
describe "Deleting a doc", -> describe("Deleting a doc", function() {
beforeEach (done) -> beforeEach(function(done) {
@project_id = ObjectId() this.project_id = ObjectId();
@doc_id = ObjectId() this.doc_id = ObjectId();
@lines = ["original", "lines"] this.lines = ["original", "lines"];
@version = 42 this.version = 42;
@ranges = [] this.ranges = [];
DocstoreApp.ensureRunning => return DocstoreApp.ensureRunning(() => {
DocstoreClient.createDoc @project_id, @doc_id, @lines, @version, @ranges, (error) => return DocstoreClient.createDoc(this.project_id, this.doc_id, this.lines, this.version, this.ranges, error => {
throw error if error? if (error != null) { throw error; }
done() return done();
});
});
});
describe "when the doc exists", -> describe("when the doc exists", function() {
beforeEach (done) -> beforeEach(function(done) {
DocstoreClient.deleteDoc @project_id, @doc_id, (error, @res, doc) => return DocstoreClient.deleteDoc(this.project_id, this.doc_id, (error, res, doc) => {
done() this.res = res;
return done();
});
});
afterEach (done) -> afterEach(function(done) {
db.docs.remove({_id: @doc_id}, done) return db.docs.remove({_id: this.doc_id}, done);
});
it "should insert a deleted doc into the docs collection", (done) -> return it("should insert a deleted doc into the docs collection", function(done) {
db.docs.find _id: @doc_id, (error, docs) => return db.docs.find({_id: this.doc_id}, (error, docs) => {
docs[0]._id.should.deep.equal @doc_id docs[0]._id.should.deep.equal(this.doc_id);
docs[0].lines.should.deep.equal @lines docs[0].lines.should.deep.equal(this.lines);
docs[0].deleted.should.equal true docs[0].deleted.should.equal(true);
done() return done();
});
});
});
describe "when the doc does not exist", -> return describe("when the doc does not exist", () => it("should return a 404", function(done) {
it "should return a 404", (done) -> const missing_doc_id = ObjectId();
missing_doc_id = ObjectId() return DocstoreClient.deleteDoc(this.project_id, missing_doc_id, function(error, res, doc) {
DocstoreClient.deleteDoc @project_id, missing_doc_id, (error, res, doc) -> res.statusCode.should.equal(404);
res.statusCode.should.equal 404 return done();
done() });
}));
});
describe "Destroying a project's documents", -> describe("Destroying a project's documents", function() {
describe "when the doc exists", -> describe("when the doc exists", function() {
beforeEach (done) -> beforeEach(function(done) {
db.docOps.insert {doc_id: ObjectId(@doc_id), version: 1}, (err) -> return db.docOps.insert({doc_id: ObjectId(this.doc_id), version: 1}, function(err) {
return done(err) if err? if (err != null) { return done(err); }
DocstoreClient.destroyAllDoc @project_id, done return DocstoreClient.destroyAllDoc(this.project_id, done);
});
});
it "should remove the doc from the docs collection", (done) -> it("should remove the doc from the docs collection", function(done) {
db.docs.find _id: @doc_id, (err, docs) -> return db.docs.find({_id: this.doc_id}, function(err, docs) {
expect(err).not.to.exist expect(err).not.to.exist;
expect(docs).to.deep.equal [] expect(docs).to.deep.equal([]);
done() return done();
});
});
it "should remove the docOps from the docOps collection", (done) -> return it("should remove the docOps from the docOps collection", function(done) {
db.docOps.find doc_id: @doc_id, (err, docOps) -> return db.docOps.find({doc_id: this.doc_id}, function(err, docOps) {
expect(err).not.to.exist expect(err).not.to.exist;
expect(docOps).to.deep.equal [] expect(docOps).to.deep.equal([]);
done() return done();
});
});
});
describe "when the doc is archived", -> return describe("when the doc is archived", function() {
beforeEach (done) -> beforeEach(function(done) {
DocstoreClient.archiveAllDoc @project_id, (err) -> return DocstoreClient.archiveAllDoc(this.project_id, function(err) {
return done(err) if err? if (err != null) { return done(err); }
DocstoreClient.destroyAllDoc @project_id, done return DocstoreClient.destroyAllDoc(this.project_id, done);
});
});
it "should remove the doc from the docs collection", (done) -> it("should remove the doc from the docs collection", function(done) {
db.docs.find _id: @doc_id, (err, docs) -> return db.docs.find({_id: this.doc_id}, function(err, docs) {
expect(err).not.to.exist expect(err).not.to.exist;
expect(docs).to.deep.equal [] expect(docs).to.deep.equal([]);
done() return done();
});
});
it "should remove the docOps from the docOps collection", (done) -> it("should remove the docOps from the docOps collection", function(done) {
db.docOps.find doc_id: @doc_id, (err, docOps) -> return db.docOps.find({doc_id: this.doc_id}, function(err, docOps) {
expect(err).not.to.exist expect(err).not.to.exist;
expect(docOps).to.deep.equal [] expect(docOps).to.deep.equal([]);
done() return done();
});
});
it "should remove the doc contents from s3", (done) -> return it("should remove the doc contents from s3", function(done) {
DocstoreClient.getS3Doc @project_id, @doc_id, (error, res, s3_doc) => return DocstoreClient.getS3Doc(this.project_id, this.doc_id, (error, res, s3_doc) => {
throw error if error? if (error != null) { throw error; }
expect(res.statusCode).to.equal 404 expect(res.statusCode).to.equal(404);
done() return done();
});
});
});
});

View file

@ -1,63 +1,83 @@
sinon = require "sinon" /*
chai = require("chai") * decaffeinate suggestions:
chai.should() * DS101: Remove unnecessary use of Array.from
{ObjectId} = require "mongojs" * DS102: Remove unnecessary code created because of implicit returns
async = require "async" * DS207: Consider shorter variations of null checks
DocstoreApp = require "./helpers/DocstoreApp" * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require("sinon");
const chai = require("chai");
chai.should();
const {ObjectId} = require("mongojs");
const async = require("async");
const DocstoreApp = require("./helpers/DocstoreApp");
DocstoreClient = require "./helpers/DocstoreClient" const DocstoreClient = require("./helpers/DocstoreClient");
describe "Getting all docs", -> describe("Getting all docs", function() {
beforeEach (done) -> beforeEach(function(done) {
@project_id = ObjectId() this.project_id = ObjectId();
@docs = [{ this.docs = [{
_id: ObjectId() _id: ObjectId(),
lines: ["one", "two", "three"] lines: ["one", "two", "three"],
ranges: {"mock": "one"} ranges: {"mock": "one"},
rev: 2 rev: 2
}, { }, {
_id: ObjectId() _id: ObjectId(),
lines: ["aaa", "bbb", "ccc"] lines: ["aaa", "bbb", "ccc"],
ranges: {"mock": "two"} ranges: {"mock": "two"},
rev: 4 rev: 4
}, { }, {
_id: ObjectId() _id: ObjectId(),
lines: ["111", "222", "333"] lines: ["111", "222", "333"],
ranges: {"mock": "three"} ranges: {"mock": "three"},
rev: 6 rev: 6
}] }];
@deleted_doc = { this.deleted_doc = {
_id: ObjectId() _id: ObjectId(),
lines: ["deleted"] lines: ["deleted"],
ranges: {"mock": "four"} ranges: {"mock": "four"},
rev: 8 rev: 8
};
const version = 42;
const jobs = Array.from(this.docs).map((doc) =>
(doc => {
return callback => {
return DocstoreClient.createDoc(this.project_id, doc._id, doc.lines, version, doc.ranges, callback);
};
})(doc));
jobs.push(cb => {
return DocstoreClient.createDoc(this.project_id, this.deleted_doc._id, this.deleted_doc.lines, version, this.deleted_doc.ranges, err=> {
return DocstoreClient.deleteDoc(this.project_id, this.deleted_doc._id, cb);
});
});
jobs.unshift(cb => DocstoreApp.ensureRunning(cb));
return async.series(jobs, done);
});
it("getAllDocs should return all the (non-deleted) docs", function(done) {
return DocstoreClient.getAllDocs(this.project_id, (error, res, docs) => {
if (error != null) { throw error; }
docs.length.should.equal(this.docs.length);
for (let i = 0; i < docs.length; i++) {
const doc = docs[i];
doc.lines.should.deep.equal(this.docs[i].lines);
} }
version = 42 return done();
jobs = for doc in @docs });
do (doc) => });
(callback) =>
DocstoreClient.createDoc @project_id, doc._id, doc.lines, version, doc.ranges, callback
jobs.push (cb) =>
DocstoreClient.createDoc @project_id, @deleted_doc._id, @deleted_doc.lines, version, @deleted_doc.ranges, (err)=>
DocstoreClient.deleteDoc @project_id, @deleted_doc._id, cb
jobs.unshift (cb)->
DocstoreApp.ensureRunning cb
async.series jobs, done
it "getAllDocs should return all the (non-deleted) docs", (done) -> return it("getAllRanges should return all the (non-deleted) doc ranges", function(done) {
DocstoreClient.getAllDocs @project_id, (error, res, docs) => return DocstoreClient.getAllRanges(this.project_id, (error, res, docs) => {
throw error if error? if (error != null) { throw error; }
docs.length.should.equal @docs.length docs.length.should.equal(this.docs.length);
for doc, i in docs for (let i = 0; i < docs.length; i++) {
doc.lines.should.deep.equal @docs[i].lines const doc = docs[i];
done() doc.ranges.should.deep.equal(this.docs[i].ranges);
}
it "getAllRanges should return all the (non-deleted) doc ranges", (done) -> return done();
DocstoreClient.getAllRanges @project_id, (error, res, docs) => });
throw error if error? });
docs.length.should.equal @docs.length });
for doc, i in docs
doc.ranges.should.deep.equal @docs[i].ranges
done()

View file

@ -1,63 +1,83 @@
sinon = require "sinon" /*
chai = require("chai") * decaffeinate suggestions:
chai.should() * DS102: Remove unnecessary code created because of implicit returns
{ObjectId} = require "mongojs" * DS207: Consider shorter variations of null checks
DocstoreApp = require "./helpers/DocstoreApp" * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require("sinon");
const chai = require("chai");
chai.should();
const {ObjectId} = require("mongojs");
const DocstoreApp = require("./helpers/DocstoreApp");
DocstoreClient = require "./helpers/DocstoreClient" const DocstoreClient = require("./helpers/DocstoreClient");
describe "Getting a doc", -> describe("Getting a doc", function() {
beforeEach (done) -> beforeEach(function(done) {
@project_id = ObjectId() this.project_id = ObjectId();
@doc_id = ObjectId() this.doc_id = ObjectId();
@lines = ["original", "lines"] this.lines = ["original", "lines"];
@version = 42 this.version = 42;
@ranges = { this.ranges = {
changes: [{ changes: [{
id: ObjectId().toString() id: ObjectId().toString(),
op: { i: "foo", p: 3 } op: { i: "foo", p: 3 },
meta: meta: {
user_id: ObjectId().toString() user_id: ObjectId().toString(),
ts: new Date().toString() ts: new Date().toString()
}]
} }
DocstoreApp.ensureRunning => }]
DocstoreClient.createDoc @project_id, @doc_id, @lines, @version, @ranges, (error) => };
throw error if error? return DocstoreApp.ensureRunning(() => {
done() return DocstoreClient.createDoc(this.project_id, this.doc_id, this.lines, this.version, this.ranges, error => {
if (error != null) { throw error; }
return done();
});
});
});
describe "when the doc exists", -> describe("when the doc exists", () => it("should get the doc lines and version", function(done) {
it "should get the doc lines and version", (done) -> return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => doc.lines.should.deep.equal(this.lines);
doc.lines.should.deep.equal @lines doc.version.should.equal(this.version);
doc.version.should.equal @version doc.ranges.should.deep.equal(this.ranges);
doc.ranges.should.deep.equal @ranges return done();
done() });
}));
describe "when the doc does not exist", -> describe("when the doc does not exist", () => it("should return a 404", function(done) {
it "should return a 404", (done) -> const missing_doc_id = ObjectId();
missing_doc_id = ObjectId() return DocstoreClient.getDoc(this.project_id, missing_doc_id, {}, function(error, res, doc) {
DocstoreClient.getDoc @project_id, missing_doc_id, {}, (error, res, doc) -> res.statusCode.should.equal(404);
res.statusCode.should.equal 404 return done();
done() });
}));
describe "when the doc is a deleted doc", -> return describe("when the doc is a deleted doc", function() {
beforeEach (done) -> beforeEach(function(done) {
@deleted_doc_id = ObjectId() this.deleted_doc_id = ObjectId();
DocstoreClient.createDoc @project_id, @deleted_doc_id, @lines, @version, @ranges, (error) => return DocstoreClient.createDoc(this.project_id, this.deleted_doc_id, this.lines, this.version, this.ranges, error => {
throw error if error? if (error != null) { throw error; }
DocstoreClient.deleteDoc @project_id, @deleted_doc_id, done return DocstoreClient.deleteDoc(this.project_id, this.deleted_doc_id, done);
});
});
it "should return the doc", (done) -> it("should return the doc", function(done) {
DocstoreClient.getDoc @project_id, @deleted_doc_id, {include_deleted:true},(error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.deleted_doc_id, {include_deleted:true},(error, res, doc) => {
doc.lines.should.deep.equal @lines doc.lines.should.deep.equal(this.lines);
doc.version.should.equal @version doc.version.should.equal(this.version);
doc.ranges.should.deep.equal @ranges doc.ranges.should.deep.equal(this.ranges);
doc.deleted.should.equal true doc.deleted.should.equal(true);
done() return done();
});
});
it "should return a 404 when the query string is not set", (done)-> return it("should return a 404 when the query string is not set", function(done){
DocstoreClient.getDoc @project_id, @deleted_doc_id, {},(error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.deleted_doc_id, {},(error, res, doc) => {
res.statusCode.should.equal 404 res.statusCode.should.equal(404);
done() return done();
});
});
});
});

View file

@ -1,225 +1,332 @@
sinon = require "sinon" /*
chai = require("chai") * decaffeinate suggestions:
chai.should() * DS102: Remove unnecessary code created because of implicit returns
{ObjectId} = require "mongojs" * DS207: Consider shorter variations of null checks
DocstoreApp = require "./helpers/DocstoreApp" * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require("sinon");
const chai = require("chai");
chai.should();
const {ObjectId} = require("mongojs");
const DocstoreApp = require("./helpers/DocstoreApp");
DocstoreClient = require "./helpers/DocstoreClient" const DocstoreClient = require("./helpers/DocstoreClient");
describe "Applying updates to a doc", -> describe("Applying updates to a doc", function() {
beforeEach (done) -> beforeEach(function(done) {
@project_id = ObjectId() this.project_id = ObjectId();
@doc_id = ObjectId() this.doc_id = ObjectId();
@originalLines = ["original", "lines"] this.originalLines = ["original", "lines"];
@newLines = ["new", "lines"] this.newLines = ["new", "lines"];
@originalRanges = { this.originalRanges = {
changes: [{ changes: [{
id: ObjectId().toString() id: ObjectId().toString(),
op: { i: "foo", p: 3 } op: { i: "foo", p: 3 },
meta: meta: {
user_id: ObjectId().toString() user_id: ObjectId().toString(),
ts: new Date().toString() ts: new Date().toString()
}]
} }
@newRanges = { }]
};
this.newRanges = {
changes: [{ changes: [{
id: ObjectId().toString() id: ObjectId().toString(),
op: { i: "bar", p: 6 } op: { i: "bar", p: 6 },
meta: meta: {
user_id: ObjectId().toString() user_id: ObjectId().toString(),
ts: new Date().toString() ts: new Date().toString()
}]
} }
@version = 42 }]
DocstoreApp.ensureRunning => };
DocstoreClient.createDoc @project_id, @doc_id, @originalLines, @version, @originalRanges, (error) => this.version = 42;
throw error if error? return DocstoreApp.ensureRunning(() => {
done() return DocstoreClient.createDoc(this.project_id, this.doc_id, this.originalLines, this.version, this.originalRanges, error => {
if (error != null) { throw error; }
return done();
});
});
});
describe "when nothing has been updated", -> describe("when nothing has been updated", function() {
beforeEach (done) -> beforeEach(function(done) {
DocstoreClient.updateDoc @project_id, @doc_id, @originalLines, @version, @originalRanges, (error, res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, this.originalLines, this.version, this.originalRanges, (error, res, body) => {
done() this.body = body;
return done();
});
});
it "should return modified = false", -> it("should return modified = false", function() {
@body.modified.should.equal false return this.body.modified.should.equal(false);
});
it "should not update the doc in the API", (done) -> return it("should not update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @originalLines doc.lines.should.deep.equal(this.originalLines);
doc.version.should.equal @version doc.version.should.equal(this.version);
doc.ranges.should.deep.equal @originalRanges doc.ranges.should.deep.equal(this.originalRanges);
done() return done();
});
});
});
describe "when the lines have changed", -> describe("when the lines have changed", function() {
beforeEach (done) -> beforeEach(function(done) {
DocstoreClient.updateDoc @project_id, @doc_id, @newLines, @version, @originalRanges, (error, res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, this.newLines, this.version, this.originalRanges, (error, res, body) => {
done() this.body = body;
return done();
});
});
it "should return modified = true", -> it("should return modified = true", function() {
@body.modified.should.equal true return this.body.modified.should.equal(true);
});
it "should return the rev", -> it("should return the rev", function() {
@body.rev.should.equal 2 return this.body.rev.should.equal(2);
});
it "should update the doc in the API", (done) -> return it("should update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @newLines doc.lines.should.deep.equal(this.newLines);
doc.version.should.equal @version doc.version.should.equal(this.version);
doc.ranges.should.deep.equal @originalRanges doc.ranges.should.deep.equal(this.originalRanges);
done() return done();
});
});
});
describe "when the version has changed", -> describe("when the version has changed", function() {
beforeEach (done) -> beforeEach(function(done) {
DocstoreClient.updateDoc @project_id, @doc_id, @originalLines, @version + 1, @originalRanges, (error, res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, this.originalLines, this.version + 1, this.originalRanges, (error, res, body) => {
done() this.body = body;
return done();
});
});
it "should return modified = true", -> it("should return modified = true", function() {
@body.modified.should.equal true return this.body.modified.should.equal(true);
});
it "should return the rev", -> it("should return the rev", function() {
@body.rev.should.equal 1 return this.body.rev.should.equal(1);
});
it "should update the doc in the API", (done) -> return it("should update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @originalLines doc.lines.should.deep.equal(this.originalLines);
doc.version.should.equal @version + 1 doc.version.should.equal(this.version + 1);
doc.ranges.should.deep.equal @originalRanges doc.ranges.should.deep.equal(this.originalRanges);
done() return done();
});
});
});
describe "when the ranges have changed", -> describe("when the ranges have changed", function() {
beforeEach (done) -> beforeEach(function(done) {
DocstoreClient.updateDoc @project_id, @doc_id, @originalLines, @version, @newRanges, (error, res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, this.originalLines, this.version, this.newRanges, (error, res, body) => {
done() this.body = body;
return done();
});
});
it "should return modified = true", -> it("should return modified = true", function() {
@body.modified.should.equal true return this.body.modified.should.equal(true);
});
it "should return the rev", -> it("should return the rev", function() {
@body.rev.should.equal 2 return this.body.rev.should.equal(2);
});
it "should update the doc in the API", (done) -> return it("should update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @originalLines doc.lines.should.deep.equal(this.originalLines);
doc.version.should.equal @version doc.version.should.equal(this.version);
doc.ranges.should.deep.equal @newRanges doc.ranges.should.deep.equal(this.newRanges);
done() return done();
});
});
});
describe "when the doc does not exist", -> describe("when the doc does not exist", function() {
beforeEach (done) -> beforeEach(function(done) {
@missing_doc_id = ObjectId() this.missing_doc_id = ObjectId();
DocstoreClient.updateDoc @project_id, @missing_doc_id, @originalLines, 0, @originalRanges, (error, @res, @body) => return DocstoreClient.updateDoc(this.project_id, this.missing_doc_id, this.originalLines, 0, this.originalRanges, (error, res, body) => {
done() this.res = res;
this.body = body;
return done();
});
});
it "should create the doc", -> it("should create the doc", function() {
@body.rev.should.equal 1 return this.body.rev.should.equal(1);
});
it "should be retreivable", (done)-> return it("should be retreivable", function(done){
DocstoreClient.getDoc @project_id, @missing_doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.missing_doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @originalLines doc.lines.should.deep.equal(this.originalLines);
doc.version.should.equal 0 doc.version.should.equal(0);
doc.ranges.should.deep.equal @originalRanges doc.ranges.should.deep.equal(this.originalRanges);
done() return done();
});
});
});
describe "when malformed doc lines are provided", -> describe("when malformed doc lines are provided", function() {
describe "when the lines are not an array", -> describe("when the lines are not an array", function() {
beforeEach (done) -> beforeEach(function(done) {
DocstoreClient.updateDoc @project_id, @doc_id, { foo: "bar" }, @version, @originalRanges, (error, @res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, { foo: "bar" }, this.version, this.originalRanges, (error, res, body) => {
done() this.res = res;
this.body = body;
return done();
});
});
it "should return 400", -> it("should return 400", function() {
@res.statusCode.should.equal 400 return this.res.statusCode.should.equal(400);
});
it "should not update the doc in the API", (done) -> return it("should not update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @originalLines doc.lines.should.deep.equal(this.originalLines);
done() return done();
});
});
});
describe "when the lines are not present", -> return describe("when the lines are not present", function() {
beforeEach (done) -> beforeEach(function(done) {
DocstoreClient.updateDoc @project_id, @doc_id, null, @version, @originalRanges, (error, @res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, null, this.version, this.originalRanges, (error, res, body) => {
done() this.res = res;
this.body = body;
return done();
});
});
it "should return 400", -> it("should return 400", function() {
@res.statusCode.should.equal 400 return this.res.statusCode.should.equal(400);
});
it "should not update the doc in the API", (done) -> return it("should not update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @originalLines doc.lines.should.deep.equal(this.originalLines);
done() return done();
});
});
});
});
describe "when no version is provided", -> describe("when no version is provided", function() {
beforeEach (done) -> beforeEach(function(done) {
DocstoreClient.updateDoc @project_id, @doc_id, @originalLines, null, @originalRanges, (error, @res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, this.originalLines, null, this.originalRanges, (error, res, body) => {
done() this.res = res;
this.body = body;
return done();
});
});
it "should return 400", -> it("should return 400", function() {
@res.statusCode.should.equal 400 return this.res.statusCode.should.equal(400);
});
it "should not update the doc in the API", (done) -> return it("should not update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @originalLines doc.lines.should.deep.equal(this.originalLines);
doc.version.should.equal @version doc.version.should.equal(this.version);
done() return done();
});
});
});
describe "when the content is large", -> describe("when the content is large", function() {
beforeEach (done) -> beforeEach(function(done) {
line = new Array(1025).join("x") # 1kb const line = new Array(1025).join("x"); // 1kb
@largeLines = Array.apply(null, Array(1024)).map(() -> line) # 1mb this.largeLines = Array.apply(null, Array(1024)).map(() => line); // 1mb
DocstoreClient.updateDoc @project_id, @doc_id, @largeLines, @version, @originalRanges, (error, res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, this.largeLines, this.version, this.originalRanges, (error, res, body) => {
done() this.body = body;
return done();
});
});
it "should return modified = true", -> it("should return modified = true", function() {
@body.modified.should.equal true return this.body.modified.should.equal(true);
});
it "should update the doc in the API", (done) -> return it("should update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @largeLines doc.lines.should.deep.equal(this.largeLines);
done() return done();
});
});
});
describe "when there is a large json payload", -> describe("when there is a large json payload", function() {
beforeEach (done) -> beforeEach(function(done) {
line = new Array(1025).join("x") # 1kb const line = new Array(1025).join("x"); // 1kb
@largeLines = Array.apply(null, Array(1024)).map(() -> line) # 1kb this.largeLines = Array.apply(null, Array(1024)).map(() => line); // 1kb
@originalRanges.padding = Array.apply(null, Array(2049)).map(() -> line) # 2mb + 1kb this.originalRanges.padding = Array.apply(null, Array(2049)).map(() => line); // 2mb + 1kb
DocstoreClient.updateDoc @project_id, @doc_id, @largeLines, @version, @originalRanges, (error, @res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, this.largeLines, this.version, this.originalRanges, (error, res, body) => {
done() this.res = res;
this.body = body;
return done();
});
});
it "should return modified = true", -> it("should return modified = true", function() {
@body.modified.should.equal true return this.body.modified.should.equal(true);
});
it "should update the doc in the API", (done) -> return it("should update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @largeLines doc.lines.should.deep.equal(this.largeLines);
done() return done();
});
});
});
describe "when the document body is too large", -> describe("when the document body is too large", function() {
beforeEach (done) -> beforeEach(function(done) {
line = new Array(1025).join("x") # 1kb const line = new Array(1025).join("x"); // 1kb
@largeLines = Array.apply(null, Array(2049)).map(() -> line) # 2mb + 1kb this.largeLines = Array.apply(null, Array(2049)).map(() => line); // 2mb + 1kb
DocstoreClient.updateDoc @project_id, @doc_id, @largeLines, @version, @originalRanges, (error, @res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, this.largeLines, this.version, this.originalRanges, (error, res, body) => {
done() this.res = res;
this.body = body;
return done();
});
});
it "should return 413", -> it("should return 413", function() {
@res.statusCode.should.equal 413 return this.res.statusCode.should.equal(413);
});
it "should report body too large", -> it("should report body too large", function() {
@res.body.should.equal 'document body too large' return this.res.body.should.equal('document body too large');
});
it "should not update the doc in the API", (done) -> return it("should not update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @originalLines doc.lines.should.deep.equal(this.originalLines);
done() return done();
});
});
});
describe "when the json payload is too large", -> return describe("when the json payload is too large", function() {
beforeEach (done) -> beforeEach(function(done) {
line = new Array(1025).join("x") # 1kb const line = new Array(1025).join("x"); // 1kb
@largeLines = Array.apply(null, Array(1024)).map(() -> line) # 1kb this.largeLines = Array.apply(null, Array(1024)).map(() => line); // 1kb
@originalRanges.padding = Array.apply(null, Array(4096)).map(() -> line) # 4mb this.originalRanges.padding = Array.apply(null, Array(4096)).map(() => line); // 4mb
DocstoreClient.updateDoc @project_id, @doc_id, @largeLines, @version, @originalRanges, (error, @res, @body) => return DocstoreClient.updateDoc(this.project_id, this.doc_id, this.largeLines, this.version, this.originalRanges, (error, res, body) => {
done() this.res = res;
this.body = body;
return done();
});
});
it "should not update the doc in the API", (done) -> return it("should not update the doc in the API", function(done) {
DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) => return DocstoreClient.getDoc(this.project_id, this.doc_id, {}, (error, res, doc) => {
doc.lines.should.deep.equal @originalLines doc.lines.should.deep.equal(this.originalLines);
done() return done();
});
});
});
});

View file

@ -1,21 +1,39 @@
app = require('../../../../app') /*
require("logger-sharelatex").logger.level("error") * decaffeinate suggestions:
settings = require("settings-sharelatex") * DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const app = require('../../../../app');
require("logger-sharelatex").logger.level("error");
const settings = require("settings-sharelatex");
module.exports = module.exports = {
running: false running: false,
initing: false initing: false,
callbacks: [] callbacks: [],
ensureRunning: (callback = (error) ->) -> ensureRunning(callback) {
if @running if (callback == null) { callback = function(error) {}; }
return callback() if (this.running) {
else if @initing return callback();
@callbacks.push callback } else if (this.initing) {
else return this.callbacks.push(callback);
@initing = true } else {
@callbacks.push callback this.initing = true;
app.listen settings.internal.docstore.port, "localhost", (error) => this.callbacks.push(callback);
throw error if error? return app.listen(settings.internal.docstore.port, "localhost", error => {
@running = true if (error != null) { throw error; }
for callback in @callbacks this.running = true;
callback() return (() => {
const result = [];
for (callback of Array.from(this.callbacks)) {
result.push(callback());
}
return result;
})();
});
}
}
};

View file

@ -1,57 +1,84 @@
request = require("request").defaults(jar: false) /*
{db, ObjectId} = require("../../../../app/js/mongojs") * decaffeinate suggestions:
settings = require("settings-sharelatex") * DS102: Remove unnecessary code created because of implicit returns
DocArchiveManager = require("../../../../app/js/DocArchiveManager.js") * DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let DocstoreClient;
const request = require("request").defaults({jar: false});
const {db, ObjectId} = require("../../../../app/js/mongojs");
const settings = require("settings-sharelatex");
const DocArchiveManager = require("../../../../app/js/DocArchiveManager.js");
module.exports = DocstoreClient = module.exports = (DocstoreClient = {
createDoc: (project_id, doc_id, lines, version, ranges, callback = (error) ->) -> createDoc(project_id, doc_id, lines, version, ranges, callback) {
DocstoreClient.updateDoc project_id, doc_id, lines, version, ranges, callback if (callback == null) { callback = function(error) {}; }
return DocstoreClient.updateDoc(project_id, doc_id, lines, version, ranges, callback);
},
getDoc: (project_id, doc_id, qs, callback = (error, res, body) ->) -> getDoc(project_id, doc_id, qs, callback) {
request.get { if (callback == null) { callback = function(error, res, body) {}; }
url: "http://localhost:#{settings.internal.docstore.port}/project/#{project_id}/doc/#{doc_id}" return request.get({
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
json: true,
qs
}, callback);
},
getAllDocs(project_id, callback) {
if (callback == null) { callback = function(error, res, body) {}; }
return request.get({
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc`,
json: true json: true
qs:qs }, callback);
}, callback },
getAllDocs: (project_id, callback = (error, res, body) ->) -> getAllRanges(project_id, callback) {
request.get { if (callback == null) { callback = function(error, res, body) {}; }
url: "http://localhost:#{settings.internal.docstore.port}/project/#{project_id}/doc" return request.get({
url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/ranges`,
json: true json: true
}, callback }, callback);
},
getAllRanges: (project_id, callback = (error, res, body) ->) -> updateDoc(project_id, doc_id, lines, version, ranges, callback) {
request.get { if (callback == null) { callback = function(error, res, body) {}; }
url: "http://localhost:#{settings.internal.docstore.port}/project/#{project_id}/ranges" return request.post({
json: true url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
}, callback json: {
lines,
version,
ranges
}
}, callback);
},
updateDoc: (project_id, doc_id, lines, version, ranges, callback = (error, res, body) ->) -> deleteDoc(project_id, doc_id, callback) {
request.post { if (callback == null) { callback = function(error, res, body) {}; }
url: "http://localhost:#{settings.internal.docstore.port}/project/#{project_id}/doc/#{doc_id}" return request.del({
json: url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`
lines: lines }, callback);
version: version },
ranges: ranges
}, callback
deleteDoc: (project_id, doc_id, callback = (error, res, body) ->) -> archiveAllDoc(project_id, callback) {
request.del { if (callback == null) { callback = function(error, res, body) {}; }
url: "http://localhost:#{settings.internal.docstore.port}/project/#{project_id}/doc/#{doc_id}" return request.post({
}, callback url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/archive`
}, callback);
},
archiveAllDoc: (project_id, callback = (error, res, body) ->) -> destroyAllDoc(project_id, callback) {
request.post { if (callback == null) { callback = function(error, res, body) {}; }
url: "http://localhost:#{settings.internal.docstore.port}/project/#{project_id}/archive" return request.post({
}, callback url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/destroy`
}, callback);
},
destroyAllDoc: (project_id, callback = (error, res, body) ->) -> getS3Doc(project_id, doc_id, callback) {
request.post { if (callback == null) { callback = function(error, res, body) {}; }
url: "http://localhost:#{settings.internal.docstore.port}/project/#{project_id}/destroy" const options = DocArchiveManager.buildS3Options(project_id+"/"+doc_id);
}, callback options.json = true;
return request.get(options, callback);
getS3Doc: (project_id, doc_id, callback = (error, res, body) ->) -> }
options = DocArchiveManager.buildS3Options(project_id+"/"+doc_id) });
options.json = true
request.get options, callback