overleaf/services/document-updater/test/unit/js/ShareJsDB/ShareJsDBTests.js

151 lines
4.5 KiB
JavaScript
Raw Normal View History

/* eslint-disable
no-return-assign,
no-unused-vars,
*/
// 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
*/
const sinon = require('sinon')
const { expect } = require('chai')
const modulePath = '../../../../app/js/ShareJsDB.js'
const SandboxedModule = require('sandboxed-module')
const Errors = require('../../../../app/js/Errors')
describe('ShareJsDB', function () {
beforeEach(function () {
this.doc_id = 'document-id'
this.project_id = 'project-id'
this.doc_key = `${this.project_id}:${this.doc_id}`
this.callback = sinon.stub()
this.ShareJsDB = SandboxedModule.require(modulePath, {
requires: {
2020-05-15 18:29:49 +00:00
'./RedisManager': (this.RedisManager = {}),
2021-07-13 11:04:42 +00:00
'./Errors': Errors,
},
})
this.version = 42
this.lines = ['one', 'two', 'three']
return (this.db = new this.ShareJsDB(
this.project_id,
this.doc_id,
this.lines,
this.version
))
})
describe('getSnapshot', function () {
describe('successfully', function () {
beforeEach(function () {
return this.db.getSnapshot(this.doc_key, this.callback)
})
it('should return the doc lines', function () {
return this.callback.args[0][1].snapshot.should.equal(
this.lines.join('\n')
)
})
it('should return the doc version', function () {
return this.callback.args[0][1].v.should.equal(this.version)
})
return it('should return the type as text', function () {
return this.callback.args[0][1].type.should.equal('text')
})
})
return describe('when the key does not match', function () {
beforeEach(function () {
return this.db.getSnapshot('bad:key', this.callback)
})
return it('should return the callback with a NotFoundError', function () {
return this.callback
2020-05-15 18:29:49 +00:00
.calledWith(sinon.match.instanceOf(Errors.NotFoundError))
.should.equal(true)
})
})
})
describe('getOps', function () {
describe('with start == end', function () {
beforeEach(function () {
this.start = this.end = 42
return this.db.getOps(this.doc_key, this.start, this.end, this.callback)
})
return it('should return an empty array', function () {
return this.callback.calledWith(null, []).should.equal(true)
})
})
describe('with a non empty range', function () {
beforeEach(function () {
this.start = 35
this.end = 42
this.RedisManager.getPreviousDocOps = sinon
.stub()
.callsArgWith(3, null, this.ops)
return this.db.getOps(this.doc_key, this.start, this.end, this.callback)
})
it('should get the range from redis', function () {
return this.RedisManager.getPreviousDocOps
.calledWith(this.doc_id, this.start, this.end - 1)
.should.equal(true)
})
return it('should return the ops', function () {
return this.callback.calledWith(null, this.ops).should.equal(true)
})
})
return describe('with no specified end', function () {
beforeEach(function () {
this.start = 35
this.end = null
this.RedisManager.getPreviousDocOps = sinon
.stub()
.callsArgWith(3, null, this.ops)
return this.db.getOps(this.doc_key, this.start, this.end, this.callback)
})
return it('should get until the end of the list', function () {
return this.RedisManager.getPreviousDocOps
.calledWith(this.doc_id, this.start, -1)
.should.equal(true)
})
})
})
return describe('writeOps', function () {
return describe('writing an op', function () {
beforeEach(function () {
this.opData = {
op: { p: 20, t: 'foo' },
meta: { source: 'bar' },
2021-07-13 11:04:42 +00:00
v: this.version,
}
return this.db.writeOp(this.doc_key, this.opData, this.callback)
})
it('should write into appliedOps', function () {
return expect(this.db.appliedOps[this.doc_key]).to.deep.equal([
2021-07-13 11:04:42 +00:00
this.opData,
])
})
return it('should call the callback without an error', function () {
this.callback.called.should.equal(true)
return (this.callback.args[0][0] != null).should.equal(false)
})
})
})
})