diff --git a/services/document-updater/app/coffee/ShareJsDB.coffee b/services/document-updater/app/coffee/ShareJsDB.coffee index a21c8aea7f..3e5dfe303f 100644 --- a/services/document-updater/app/coffee/ShareJsDB.coffee +++ b/services/document-updater/app/coffee/ShareJsDB.coffee @@ -1,8 +1,6 @@ Keys = require('./UpdateKeys') -Settings = require('settings-sharelatex') RedisManager = require "./RedisManager" Errors = require "./Errors" -logger = require "logger-sharelatex" module.exports = class ShareJsDB constructor: (@project_id, @doc_id, @lines, @version) -> diff --git a/services/document-updater/test/unit/coffee/ShareJsDB/GetOpsTests.coffee b/services/document-updater/test/unit/coffee/ShareJsDB/GetOpsTests.coffee deleted file mode 100644 index 5621f39a85..0000000000 --- a/services/document-updater/test/unit/coffee/ShareJsDB/GetOpsTests.coffee +++ /dev/null @@ -1,55 +0,0 @@ -sinon = require('sinon') -chai = require('chai') -should = chai.should() -modulePath = "../../../../app/js/ShareJsDB.js" -SandboxedModule = require('sandboxed-module') - -describe "ShareJsDB.getOps", -> - beforeEach -> - @doc_id = "document-id" - @project_id = "project-id" - @doc_key = "#{@project_id}:#{@doc_id}" - @callback = sinon.stub() - @ops = [{p: 20, t: "foo"}] - @redis_ops = (JSON.stringify(op) for op in @ops) - @ShareJsDB = SandboxedModule.require modulePath, requires: - "./RedisManager": @RedisManager = {} - "./DocumentManager":{} - "logger-sharelatex": {} - @db = new @ShareJsDB() - - describe "with start == end", -> - beforeEach -> - @start = @end = 42 - @db.getOps @doc_key, @start, @end, @callback - - it "should return an empty array", -> - @callback.calledWith(null, []).should.equal true - - describe "with a non empty range", -> - beforeEach -> - @start = 35 - @end = 42 - @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops) - @db.getOps @doc_key, @start, @end, @callback - - it "should get the range from redis", -> - @RedisManager.getPreviousDocOps - .calledWith(@doc_id, @start, @end-1) - .should.equal true - - it "should return the ops", -> - @callback.calledWith(null, @ops).should.equal true - - describe "with no specified end", -> - beforeEach -> - @start = 35 - @end = null - @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops) - @db.getOps @doc_key, @start, @end, @callback - - it "should get until the end of the list", -> - @RedisManager.getPreviousDocOps - .calledWith(@doc_id, @start, -1) - .should.equal true - diff --git a/services/document-updater/test/unit/coffee/ShareJsDB/GetSnapshotTests.coffee b/services/document-updater/test/unit/coffee/ShareJsDB/GetSnapshotTests.coffee deleted file mode 100644 index f2527b01a2..0000000000 --- a/services/document-updater/test/unit/coffee/ShareJsDB/GetSnapshotTests.coffee +++ /dev/null @@ -1,87 +0,0 @@ -sinon = require('sinon') -chai = require('chai') -should = chai.should() -expect = chai.expect -modulePath = "../../../../app/js/ShareJsDB.js" -SandboxedModule = require('sandboxed-module') -Errors = require "../../../../app/js/Errors" - -describe "ShareJsDB.getSnapshot", -> - beforeEach -> - @doc_id = "document-id" - @project_id = "project-id" - @doc_key = "#{@project_id}:#{@doc_id}" - @callback = sinon.stub() - @ShareJsDB = SandboxedModule.require modulePath, requires: - "./DocumentManager": @DocumentManager = {} - "./RedisManager": {} - "./DocOpsManager": {} - "logger-sharelatex": {} - @db = new @ShareJsDB() - - @version = 42 - - describe "with a text document", -> - beforeEach -> - @lines = ["one", "two", "three"] - - describe "successfully", -> - beforeEach -> - @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version) - @db.getSnapshot @doc_key, @callback - - it "should get the doc", -> - @DocumentManager.getDoc - .calledWith(@project_id, @doc_id) - .should.equal true - - it "should return the doc lines", -> - @callback.args[0][1].snapshot.should.equal @lines.join("\n") - - it "should return the doc version", -> - @callback.args[0][1].v.should.equal @version - - it "should return the type as text", -> - @callback.args[0][1].type.should.equal "text" - - describe "when the doclines do not exist", -> - beforeEach -> - @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, null, null) - @db.getSnapshot @doc_key, @callback - - it "should return the callback with a NotFoundError", -> - @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true - - describe "when getDoc returns an error", -> - beforeEach -> - @DocumentManager.getDoc = sinon.stub().callsArgWith(2, @error = new Error("oops"), null, null) - @db.getSnapshot @doc_key, @callback - - it "should return the callback with an error", -> - @callback.calledWith(@error).should.equal true - - describe "with a JSON document", -> - beforeEach -> - @lines = [{text: "one"}, {text:"two"}, {text:"three"}] - - describe "successfully", -> - beforeEach -> - @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version) - @db.getSnapshot @doc_key, @callback - - it "should get the doc", -> - @DocumentManager.getDoc - .calledWith(@project_id, @doc_id) - .should.equal true - - it "should return the doc lines", -> - expect(@callback.args[0][1].snapshot).to.deep.equal lines: @lines - - it "should return the doc version", -> - @callback.args[0][1].v.should.equal @version - - it "should return the type as text", -> - @callback.args[0][1].type.should.equal "json" - - - diff --git a/services/document-updater/test/unit/coffee/ShareJsDB/ShareJsDBTests.coffee b/services/document-updater/test/unit/coffee/ShareJsDB/ShareJsDBTests.coffee new file mode 100644 index 0000000000..aa03d9fb1e --- /dev/null +++ b/services/document-updater/test/unit/coffee/ShareJsDB/ShareJsDBTests.coffee @@ -0,0 +1,93 @@ +sinon = require('sinon') +chai = require('chai') +should = chai.should() +expect = chai.expect +modulePath = "../../../../app/js/ShareJsDB.js" +SandboxedModule = require('sandboxed-module') +Errors = require "../../../../app/js/Errors" + +describe "ShareJsDB", -> + beforeEach -> + @doc_id = "document-id" + @project_id = "project-id" + @doc_key = "#{@project_id}:#{@doc_id}" + @callback = sinon.stub() + @ShareJsDB = SandboxedModule.require modulePath, requires: + "./RedisManager": @RedisManager = {} + + @version = 42 + @lines = ["one", "two", "three"] + @db = new @ShareJsDB(@project_id, @doc_id, @lines, @version) + + describe "getSnapshot", -> + describe "successfully", -> + beforeEach -> + @db.getSnapshot @doc_key, @callback + + it "should return the doc lines", -> + @callback.args[0][1].snapshot.should.equal @lines.join("\n") + + it "should return the doc version", -> + @callback.args[0][1].v.should.equal @version + + it "should return the type as text", -> + @callback.args[0][1].type.should.equal "text" + + describe "when the key does not match", -> + beforeEach -> + @db.getSnapshot "bad:key", @callback + + it "should return the callback with a NotFoundError", -> + @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true + + describe "getOps", -> + describe "with start == end", -> + beforeEach -> + @start = @end = 42 + @db.getOps @doc_key, @start, @end, @callback + + it "should return an empty array", -> + @callback.calledWith(null, []).should.equal true + + describe "with a non empty range", -> + beforeEach -> + @start = 35 + @end = 42 + @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops) + @db.getOps @doc_key, @start, @end, @callback + + it "should get the range from redis", -> + @RedisManager.getPreviousDocOps + .calledWith(@doc_id, @start, @end-1) + .should.equal true + + it "should return the ops", -> + @callback.calledWith(null, @ops).should.equal true + + describe "with no specified end", -> + beforeEach -> + @start = 35 + @end = null + @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops) + @db.getOps @doc_key, @start, @end, @callback + + it "should get until the end of the list", -> + @RedisManager.getPreviousDocOps + .calledWith(@doc_id, @start, -1) + .should.equal true + + describe "writeOps", -> + describe "writing an op", -> + beforeEach -> + @opData = + op: {p: 20, t: "foo"} + meta: {source: "bar"} + v: @version + @db.writeOp @doc_key, @opData, @callback + + it "should write into appliedOps", -> + expect(@db.appliedOps[@doc_key]).to.deep.equal [@opData] + + it "should call the callback without an error", -> + @callback.called.should.equal true + (@callback.args[0][0]?).should.equal false diff --git a/services/document-updater/test/unit/coffee/ShareJsDB/WriteOpsTests.coffee b/services/document-updater/test/unit/coffee/ShareJsDB/WriteOpsTests.coffee deleted file mode 100644 index 838f63034e..0000000000 --- a/services/document-updater/test/unit/coffee/ShareJsDB/WriteOpsTests.coffee +++ /dev/null @@ -1,39 +0,0 @@ -sinon = require('sinon') -chai = require('chai') -expect = chai.expect -should = chai.should() -modulePath = "../../../../app/js/ShareJsDB.js" -SandboxedModule = require('sandboxed-module') - -describe "ShareJsDB.writeOps", -> - beforeEach -> - @project_id = "project-id" - @doc_id = "document-id" - @doc_key = "#{@project_id}:#{@doc_id}" - @callback = sinon.stub() - @opData = - op: {p: 20, t: "foo"} - meta: {source: "bar"} - @ShareJsDB = SandboxedModule.require modulePath, requires: - "./RedisManager": @RedisManager = {} - "./DocOpsManager": @DocOpsManager = {} - "./DocumentManager": {} - "logger-sharelatex": @logger = {error: sinon.stub()} - @db = new @ShareJsDB() - - describe "writing an op", -> - beforeEach -> - @version = 42 - @opData.v = @version - @db.writeOp @doc_key, @opData, @callback - - it "should write into appliedOps", -> - expect(@db.appliedOps[@doc_key]).to.deep.equal [@opData] - - it "should call the callback without an error", -> - @callback.called.should.equal true - (@callback.args[0][0]?).should.equal false - - - -