| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388 |
- const sinon = require('sinon')
- const { expect } = require('chai')
- const modulePath = '../../../../app/js/DocumentManager.js'
- const SandboxedModule = require('sandboxed-module')
- const Errors = require('../../../../app/js/Errors')
- const tk = require('timekeeper')
- describe('DocumentManager', function () {
- beforeEach(function () {
- tk.freeze(new Date())
- this.Metrics = {
- Timer: class Timer {},
- inc: sinon.stub(),
- }
- this.Metrics.Timer.prototype.done = sinon.stub()
- this.RedisManager = {
- promises: {
- clearUnflushedTime: sinon.stub().resolves(),
- getDoc: sinon.stub(),
- getPreviousDocOps: sinon.stub(),
- putDocInMemory: sinon.stub().resolves(),
- removeDocFromMemory: sinon.stub().resolves(),
- renameDoc: sinon.stub().resolves(),
- updateCommentState: sinon.stub().resolves(),
- updateDocument: sinon.stub().resolves(),
- },
- }
- this.ProjectHistoryRedisManager = {
- promises: {
- queueOps: sinon.stub().resolves(),
- queueResyncDocContent: sinon.stub().resolves(),
- },
- }
- this.PersistenceManager = {
- promises: {
- getDoc: sinon.stub(),
- setDoc: sinon.stub().resolves(),
- },
- }
- this.HistoryManager = {
- flushProjectChangesAsync: sinon.stub(),
- }
- this.DiffCodec = {
- diffAsShareJsOp: sinon.stub(),
- }
- this.UpdateManager = {
- promises: {
- applyUpdate: sinon.stub().resolves(),
- },
- }
- this.HistoryOTUpdateManager = {
- applyUpdate: sinon.stub().resolves(),
- }
- this.RangesManager = {
- acceptChanges: sinon.stub(),
- deleteComment: sinon.stub(),
- }
- this.Settings = {
- max_doc_length: 2 * 1024 * 1024, // 2mb
- maxUnflushedAgeMs: 300 * 1000, // 5 minutes
- }
- this.DocumentManager = SandboxedModule.require(modulePath, {
- requires: {
- './RedisManager': this.RedisManager,
- './ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
- './PersistenceManager': this.PersistenceManager,
- './HistoryManager': this.HistoryManager,
- './Metrics': this.Metrics,
- './DiffCodec': this.DiffCodec,
- './UpdateManager': this.UpdateManager,
- './HistoryOTUpdateManager': this.HistoryOTUpdateManager,
- './RangesManager': this.RangesManager,
- './Errors': Errors,
- '@overleaf/settings': this.Settings,
- },
- })
- this.project_id = 'project-id-123'
- this.projectHistoryId = 'history-id-123'
- this.doc_id = 'doc-id-123'
- this.user_id = 1234
- this.lines = ['one', 'two', 'three']
- this.version = 42
- this.ranges = { comments: 'mock', entries: 'mock' }
- this.resolvedCommentIds = ['comment-1']
- this.pathname = '/a/b/c.tex'
- this.unflushedTime = Date.now()
- this.lastUpdatedAt = Date.now()
- this.lastUpdatedBy = 'last-author-id'
- this.source = 'external-source'
- this.historyRangesSupport = false
- })
- afterEach(function () {
- tk.reset()
- })
- describe('flushAndDeleteDoc', function () {
- describe('successfully', function () {
- beforeEach(async function () {
- this.DocumentManager.promises.flushDocIfLoaded = sinon.stub().resolves()
- await this.DocumentManager.promises.flushAndDeleteDoc(
- this.project_id,
- this.doc_id,
- {}
- )
- })
- it('should flush the doc', function () {
- this.DocumentManager.promises.flushDocIfLoaded
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should remove the doc from redis', function () {
- this.RedisManager.promises.removeDocFromMemory
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- })
- describe('when a flush error occurs', function () {
- beforeEach(async function () {
- this.DocumentManager.promises.flushDocIfLoaded = sinon
- .stub()
- .rejects(new Error('boom!'))
- await expect(
- this.DocumentManager.promises.flushAndDeleteDoc(
- this.project_id,
- this.doc_id,
- {}
- )
- ).to.be.rejected
- })
- it('should not remove the doc from redis', function () {
- this.RedisManager.promises.removeDocFromMemory.called.should.equal(
- false
- )
- })
- describe('when ignoring flush errors', function () {
- it('should remove the doc from redis', async function () {
- await this.DocumentManager.promises.flushAndDeleteDoc(
- this.project_id,
- this.doc_id,
- { ignoreFlushErrors: true }
- )
- this.RedisManager.promises.removeDocFromMemory.called.should.equal(
- true
- )
- })
- })
- })
- })
- describe('flushDocIfLoaded', function () {
- describe('when the doc is in Redis', function () {
- beforeEach(async function () {
- this.RedisManager.promises.getDoc.resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- unflushedTime: this.unflushedTime,
- lastUpdatedAt: this.lastUpdatedAt,
- lastUpdatedBy: this.lastUpdatedBy,
- })
- await this.DocumentManager.promises.flushDocIfLoaded(
- this.project_id,
- this.doc_id
- )
- })
- it('should get the doc from redis', function () {
- this.RedisManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should write the doc lines to the persistence layer', function () {
- this.PersistenceManager.promises.setDoc.should.have.been.calledWith(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- this.ranges,
- this.lastUpdatedAt,
- this.lastUpdatedBy
- )
- })
- })
- describe('when the document is not in Redis', function () {
- beforeEach(async function () {
- this.RedisManager.promises.getDoc.resolves({
- lines: null,
- version: null,
- ranges: null,
- })
- await this.DocumentManager.promises.flushDocIfLoaded(
- this.project_id,
- this.doc_id
- )
- })
- it('should get the doc from redis', function () {
- this.RedisManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should not write anything to the persistence layer', function () {
- this.PersistenceManager.promises.setDoc.called.should.equal(false)
- })
- })
- })
- describe('getDocAndRecentOps', function () {
- describe('with a previous version specified', function () {
- beforeEach(async function () {
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- type: 'sharejs-text-ot',
- })
- this.RedisManager.promises.getPreviousDocOps.resolves(this.ops)
- this.result = await this.DocumentManager.promises.getDocAndRecentOps(
- this.project_id,
- this.doc_id,
- this.fromVersion
- )
- })
- it('should get the doc', function () {
- this.DocumentManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should get the doc ops', function () {
- this.RedisManager.promises.getPreviousDocOps
- .calledWith(this.doc_id, this.fromVersion, this.version)
- .should.equal(true)
- })
- it('should return the doc info', function () {
- expect(this.result).to.deep.equal({
- lines: this.lines,
- version: this.version,
- ops: this.ops,
- ranges: this.ranges,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- type: 'sharejs-text-ot',
- })
- })
- })
- describe('with no previous version specified', function () {
- beforeEach(async function () {
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- type: 'sharejs-text-ot',
- })
- this.RedisManager.promises.getPreviousDocOps.resolves(this.ops)
- this.result = await this.DocumentManager.promises.getDocAndRecentOps(
- this.project_id,
- this.doc_id,
- -1
- )
- })
- it('should get the doc', function () {
- this.DocumentManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should not need to get the doc ops', function () {
- this.RedisManager.promises.getPreviousDocOps.called.should.equal(false)
- })
- it('should return the doc info', function () {
- expect(this.result).to.deep.equal({
- lines: this.lines,
- version: this.version,
- ops: [],
- ranges: this.ranges,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- type: 'sharejs-text-ot',
- })
- })
- })
- })
- describe('getDoc', function () {
- describe('when the doc exists in Redis', function () {
- beforeEach(async function () {
- this.RedisManager.promises.getDoc.resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- resolvedCommentIds: this.resolvedCommentIds,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- unflushedTime: this.unflushedTime,
- lastUpdatedAt: this.lastUpdatedAt,
- lastUpdatedBy: this.lastUpdatedBy,
- historyRangesSupport: this.historyRangesSupport,
- })
- this.result = await this.DocumentManager.promises.getDoc(
- this.project_id,
- this.doc_id
- )
- })
- it('should get the doc from Redis', function () {
- this.RedisManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should return the doc info', function () {
- expect(this.result).to.deep.equal({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- resolvedCommentIds: this.resolvedCommentIds,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- unflushedTime: this.unflushedTime,
- alreadyLoaded: true,
- historyRangesSupport: this.historyRangesSupport,
- type: 'sharejs-text-ot',
- })
- })
- })
- describe('when the doc does not exist in Redis', function () {
- beforeEach(async function () {
- this.RedisManager.promises.getDoc.resolves({
- lines: null,
- version: null,
- ranges: null,
- pathname: null,
- projectHistoryId: null,
- })
- this.PersistenceManager.promises.getDoc.resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- resolvedCommentIds: this.resolvedCommentIds,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- historyRangesSupport: this.historyRangesSupport,
- })
- this.result = await this.DocumentManager.promises.getDoc(
- this.project_id,
- this.doc_id
- )
- })
- it('should try to get the doc from Redis', function () {
- this.RedisManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should get the doc from the PersistenceManager', function () {
- this.PersistenceManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should set the doc in Redis', function () {
- this.RedisManager.promises.putDocInMemory
- .calledWith(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- this.ranges,
- this.resolvedCommentIds,
- this.pathname,
- this.projectHistoryId,
- this.historyRangesSupport
- )
- .should.equal(true)
- })
- it('should return doc info', function () {
- expect(this.result).to.deep.equal({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- resolvedCommentIds: this.resolvedCommentIds,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- unflushedTime: null,
- alreadyLoaded: false,
- historyRangesSupport: this.historyRangesSupport,
- type: 'sharejs-text-ot',
- })
- })
- })
- })
- describe('setDoc', function () {
- describe('with plain tex lines', function () {
- beforeEach(function () {
- this.beforeLines = ['before', 'lines']
- this.afterLines = ['after', 'lines']
- this.ops = [
- { i: 'foo', p: 4 },
- { d: 'bar', p: 42 },
- ]
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.beforeLines,
- version: this.version,
- ranges: this.ranges,
- resolvedCommentIds: this.resolvedCommentIds,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- unflushedTime: this.unflushedTime,
- alreadyLoaded: true,
- })
- this.DiffCodec.diffAsShareJsOp.returns(this.ops)
- this.DocumentManager.promises.flushDocIfLoaded = sinon.stub().resolves()
- this.DocumentManager.promises.flushAndDeleteDoc = sinon
- .stub()
- .resolves()
- })
- describe('when not loaded but with the same content', function () {
- beforeEach(async function () {
- this.DiffCodec.diffAsShareJsOp.returns([])
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.beforeLines,
- version: this.version,
- ranges: this.ranges,
- resolvedCommentIds: this.resolvedCommentIds,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- unflushedTime: this.unflushedTime,
- alreadyLoaded: false,
- })
- await this.DocumentManager.promises.setDoc(
- this.project_id,
- this.doc_id,
- this.beforeLines,
- this.source,
- this.user_id,
- false,
- true
- )
- })
- it('should not apply the diff as a ShareJS op', function () {
- this.UpdateManager.promises.applyUpdate.called.should.equal(false)
- })
- it('should increment the external update metric', function () {
- this.Metrics.inc
- .calledWith('external-update', 1, {
- status: 'noop',
- method: 'evict',
- path: this.source,
- })
- .should.equal(true)
- })
- it('should flush and delete the doc from redis', function () {
- this.DocumentManager.promises.flushAndDeleteDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- })
- describe('when already loaded with the same content', function () {
- beforeEach(async function () {
- this.DiffCodec.diffAsShareJsOp.returns([])
- await this.DocumentManager.promises.setDoc(
- this.project_id,
- this.doc_id,
- this.beforeLines,
- this.source,
- this.user_id,
- false,
- true
- )
- })
- it('should not apply the diff as a ShareJS op', function () {
- this.UpdateManager.promises.applyUpdate.called.should.equal(false)
- })
- it('should increment the external update metric', function () {
- this.Metrics.inc
- .calledWith('external-update', 1, {
- status: 'noop',
- method: 'flush',
- path: this.source,
- })
- .should.equal(true)
- })
- it('should flush the doc to Mongo', function () {
- this.DocumentManager.promises.flushDocIfLoaded
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- })
- describe('when already loaded', function () {
- beforeEach(async function () {
- await this.DocumentManager.promises.setDoc(
- this.project_id,
- this.doc_id,
- this.afterLines,
- this.source,
- this.user_id,
- false,
- true
- )
- })
- it('should get the current doc lines', function () {
- this.DocumentManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should return a diff of the old and new lines', function () {
- this.DiffCodec.diffAsShareJsOp
- .calledWith(this.beforeLines, this.afterLines)
- .should.equal(true)
- })
- it('should apply the diff as a ShareJS op', function () {
- this.UpdateManager.promises.applyUpdate
- .calledWith(this.project_id, this.doc_id, {
- doc: this.doc_id,
- v: this.version,
- op: this.ops,
- meta: {
- type: 'external',
- source: this.source,
- user_id: this.user_id,
- },
- })
- .should.equal(true)
- })
- it('should increment the external update metric', function () {
- this.Metrics.inc
- .calledWith('external-update', 1, {
- status: 'diff',
- method: 'flush',
- path: this.source,
- })
- .should.equal(true)
- })
- it('should flush the doc to Mongo', function () {
- this.DocumentManager.promises.flushDocIfLoaded
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should not flush the project history', function () {
- this.HistoryManager.flushProjectChangesAsync.called.should.equal(
- false
- )
- })
- })
- describe('when not already loaded', function () {
- beforeEach(async function () {
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.beforeLines,
- version: this.version,
- pathname: this.pathname,
- unflushedTime: null,
- alreadyLoaded: false,
- })
- await this.DocumentManager.promises.setDoc(
- this.project_id,
- this.doc_id,
- this.afterLines,
- this.source,
- this.user_id,
- false,
- true
- )
- })
- it('should flush and delete the doc from the doc updater', function () {
- this.DocumentManager.promises.flushAndDeleteDoc
- .calledWith(this.project_id, this.doc_id, {})
- .should.equal(true)
- })
- it('should increment the external update metric', function () {
- this.Metrics.inc
- .calledWith('external-update', 1, {
- status: 'diff',
- method: 'evict',
- path: this.source,
- })
- .should.equal(true)
- })
- it('should not flush the project history', function () {
- this.HistoryManager.flushProjectChangesAsync
- .calledWithExactly(this.project_id)
- .should.equal(true)
- })
- })
- describe('without new lines', function () {
- beforeEach(async function () {
- await expect(
- this.DocumentManager.promises.setDoc(
- this.project_id,
- this.doc_id,
- null,
- this.source,
- this.user_id,
- false,
- true
- )
- ).to.be.rejectedWith('No lines were provided to setDoc')
- })
- it('should not try to get the doc lines', function () {
- this.DocumentManager.promises.getDoc.called.should.equal(false)
- })
- })
- describe('with the undoing flag', function () {
- beforeEach(async function () {
- // Copy ops so we don't interfere with other tests
- this.ops = [
- { i: 'foo', p: 4 },
- { d: 'bar', p: 42 },
- ]
- this.DiffCodec.diffAsShareJsOp.returns(this.ops)
- await this.DocumentManager.promises.setDoc(
- this.project_id,
- this.doc_id,
- this.afterLines,
- this.source,
- this.user_id,
- true,
- true
- )
- })
- it('should set the undo flag on each op', function () {
- this.ops.map(op => op.u.should.equal(true))
- })
- })
- describe('with the external flag', function () {
- beforeEach(async function () {
- this.undoing = false
- // Copy ops so we don't interfere with other tests
- this.ops = [
- { i: 'foo', p: 4 },
- { d: 'bar', p: 42 },
- ]
- this.DiffCodec.diffAsShareJsOp.returns(this.ops)
- await this.DocumentManager.promises.setDoc(
- this.project_id,
- this.doc_id,
- this.afterLines,
- this.source,
- this.user_id,
- this.undoing,
- true
- )
- })
- it('should add the external type to update metadata', function () {
- this.UpdateManager.promises.applyUpdate
- .calledWith(this.project_id, this.doc_id, {
- doc: this.doc_id,
- v: this.version,
- op: this.ops,
- meta: {
- type: 'external',
- source: this.source,
- user_id: this.user_id,
- },
- })
- .should.equal(true)
- })
- })
- describe('without the external flag', function () {
- beforeEach(async function () {
- this.undoing = false
- // Copy ops so we don't interfere with other tests
- this.ops = [
- { i: 'foo', p: 4 },
- { d: 'bar', p: 42 },
- ]
- this.DiffCodec.diffAsShareJsOp.returns(this.ops)
- await this.DocumentManager.promises.setDoc(
- this.project_id,
- this.doc_id,
- this.afterLines,
- this.source,
- this.user_id,
- this.undoing,
- false
- )
- })
- it('should not add the external type to update metadata', function () {
- this.UpdateManager.promises.applyUpdate
- .calledWith(this.project_id, this.doc_id, {
- doc: this.doc_id,
- v: this.version,
- op: this.ops,
- meta: {
- source: this.source,
- user_id: this.user_id,
- },
- })
- .should.equal(true)
- })
- })
- })
- })
- describe('acceptChanges', function () {
- beforeEach(function () {
- this.change_id = 'mock-change-id'
- this.change_ids = [
- 'mock-change-id-1',
- 'mock-change-id-2',
- 'mock-change-id-3',
- 'mock-change-id-4',
- ]
- this.version = 34
- this.lines = ['original', 'lines']
- this.ranges = {
- entries: 'mock',
- comments: 'mock',
- changes: [
- { id: 'mock-change-id', metadata: { user_id: 'mock-user-id-0' } },
- { id: 'mock-change-id-1', metadata: { user_id: 'mock-user-id-1' } },
- { id: 'mock-change-id-2', metadata: { user_id: 'mock-user-id-2' } },
- { id: 'mock-change-id-3', metadata: { user_id: 'mock-user-id-3' } },
- { id: 'mock-change-id-4', metadata: { user_id: 'mock-user-id-4' } },
- { id: 'other-change-id', metadata: { user_id: 'other-user-id' } },
- ],
- }
- this.updated_ranges = { entries: 'updated', comments: 'updated' }
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- })
- this.RangesManager.acceptChanges.returns(this.updated_ranges)
- })
- describe('successfully with a single change', function () {
- beforeEach(async function () {
- this.result = await this.DocumentManager.promises.acceptChanges(
- this.project_id,
- this.doc_id,
- [this.change_id]
- )
- })
- it("should get the document's current ranges", function () {
- this.DocumentManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should apply the accept change to the ranges', function () {
- this.RangesManager.acceptChanges.should.have.been.calledWith(
- this.project_id,
- this.doc_id,
- [this.change_id],
- this.ranges
- )
- })
- it('should save the updated ranges', function () {
- this.RedisManager.promises.updateDocument
- .calledWith(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- [],
- this.updated_ranges,
- {}
- )
- .should.equal(true)
- })
- it('should return the change contributors', function () {
- expect(this.result).to.deep.equal(['mock-user-id-0'])
- })
- })
- describe('successfully with multiple changes', function () {
- beforeEach(async function () {
- this.result = await this.DocumentManager.promises.acceptChanges(
- this.project_id,
- this.doc_id,
- this.change_ids
- )
- })
- it('should apply the accept change to the ranges', function () {
- this.RangesManager.acceptChanges
- .calledWith(
- this.project_id,
- this.doc_id,
- this.change_ids,
- this.ranges
- )
- .should.equal(true)
- })
- it('should return the change contributors', function () {
- expect(this.result).to.deep.equal([
- 'mock-user-id-1',
- 'mock-user-id-2',
- 'mock-user-id-3',
- 'mock-user-id-4',
- ])
- })
- })
- describe('when the doc is not found', function () {
- beforeEach(async function () {
- this.DocumentManager.promises.getDoc = sinon
- .stub()
- .resolves({ lines: null, version: null, ranges: null })
- await expect(
- this.DocumentManager.promises.acceptChanges(
- this.project_id,
- this.doc_id,
- [this.change_id]
- )
- ).to.be.rejectedWith(Errors.NotFoundError)
- })
- it('should not save anything', function () {
- this.RedisManager.promises.updateDocument.called.should.equal(false)
- })
- })
- })
- describe('getComment', function () {
- beforeEach(function () {
- this.ranges.comments = [
- {
- id: 'mock-comment-id-1',
- },
- {
- id: 'mock-comment-id-2',
- },
- ]
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- })
- })
- describe('when comment exists', function () {
- beforeEach(async function () {
- await expect(
- this.DocumentManager.promises.getComment(
- this.project_id,
- this.doc_id,
- 'mock-comment-id-1'
- )
- ).to.eventually.deep.equal({ id: 'mock-comment-id-1' })
- })
- it("should get the document's current ranges", function () {
- this.DocumentManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- })
- describe('when comment doesnt exists', function () {
- beforeEach(async function () {
- await expect(
- this.DocumentManager.promises.getComment(
- this.project_id,
- this.doc_id,
- 'mock-comment-id-x'
- )
- ).to.be.rejectedWith(Errors.NotFoundError)
- })
- it("should get the document's current ranges", function () {
- this.DocumentManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- })
- describe('when the doc is not found', function () {
- beforeEach(async function () {
- this.DocumentManager.promises.getDoc = sinon
- .stub()
- .resolves({ lines: null, version: null, ranges: null })
- await expect(
- this.DocumentManager.promises.acceptChanges(
- this.project_id,
- this.doc_id,
- [this.change_id]
- )
- ).to.be.rejectedWith(Errors.NotFoundError)
- })
- })
- })
- describe('deleteComment', function () {
- beforeEach(function () {
- this.comment_id = 'mock-comment-id'
- this.version = 34
- this.lines = ['original', 'lines']
- this.ranges = { comments: ['one', 'two', 'three'] }
- this.resolvedCommentIds = ['comment1']
- this.updated_ranges = { comments: ['one', 'three'] }
- this.historyRangesSupport = true
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- resolvedCommentIds: this.resolvedCommentIds,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- unflushedTime: Date.now() - 1e9,
- alreadyLoaded: true,
- historyRangesSupport: this.historyRangesSupport,
- })
- this.RangesManager.deleteComment.returns(this.updated_ranges)
- })
- describe('successfully', function () {
- beforeEach(async function () {
- await this.DocumentManager.promises.deleteComment(
- this.project_id,
- this.doc_id,
- this.comment_id,
- this.user_id
- )
- })
- it("should get the document's current ranges", function () {
- this.DocumentManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should delete the comment from the ranges', function () {
- this.RangesManager.deleteComment
- .calledWith(this.comment_id, this.ranges)
- .should.equal(true)
- })
- it('should save the updated ranges', function () {
- this.RedisManager.promises.updateDocument
- .calledWith(
- this.project_id,
- this.doc_id,
- this.lines,
- this.version,
- [],
- this.updated_ranges,
- {}
- )
- .should.equal(true)
- })
- it('should unset the comment resolved state', function () {
- this.RedisManager.promises.updateCommentState.should.have.been.calledWith(
- this.doc_id,
- this.comment_id,
- false
- )
- })
- it('should queue the delete comment operation', function () {
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWith(
- this.project_id,
- JSON.stringify({
- pathname: this.pathname,
- deleteComment: this.comment_id,
- meta: {
- ts: new Date(),
- user_id: this.user_id,
- },
- })
- )
- })
- })
- describe('when the doc is not found', function () {
- beforeEach(async function () {
- this.DocumentManager.promises.getDoc = sinon
- .stub()
- .resolves({ lines: null, version: null, ranges: null })
- await expect(
- this.DocumentManager.promises.acceptChanges(
- this.project_id,
- this.doc_id,
- [this.comment_id]
- )
- ).to.be.rejectedWith(Errors.NotFoundError)
- })
- it('should not save anything', function () {
- this.RedisManager.promises.updateDocument.called.should.equal(false)
- })
- })
- })
- describe('getDocAndFlushIfOld', function () {
- beforeEach(function () {
- this.DocumentManager.promises.flushDocIfLoaded = sinon.stub().resolves()
- })
- describe('when the doc is in Redis', function () {
- describe('and has changes to be flushed', function () {
- beforeEach(async function () {
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- projectHistoryId: this.projectHistoryId,
- pathname: this.pathname,
- unflushedTime: Date.now() - 2 * this.Settings.maxUnflushedAgeMs, // document is older than max unflushed age
- alreadyLoaded: true,
- })
- this.result = await this.DocumentManager.promises.getDocAndFlushIfOld(
- this.project_id,
- this.doc_id
- )
- })
- it('should get the doc', function () {
- this.DocumentManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should flush the doc', function () {
- this.DocumentManager.promises.flushDocIfLoaded
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should return the lines and versions', function () {
- expect(this.result).to.deep.equal({
- lines: this.lines,
- version: this.version,
- })
- })
- })
- describe("and has only changes that don't need to be flushed", function () {
- beforeEach(async function () {
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- pathname: this.pathname,
- unflushedTime: Date.now() - 0.5 * this.Settings.maxUnflushedAgeMs, // document is not old enough to trigger flush
- alreadyLoaded: true,
- })
- this.result = await this.DocumentManager.promises.getDocAndFlushIfOld(
- this.project_id,
- this.doc_id
- )
- })
- it('should get the doc', function () {
- this.DocumentManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should not flush the doc', function () {
- this.DocumentManager.promises.flushDocIfLoaded.called.should.equal(
- false
- )
- })
- it('should return the lines and versions', function () {
- expect(this.result).to.deep.equal({
- lines: this.lines,
- version: this.version,
- })
- })
- })
- })
- describe('when the doc is not in Redis', function () {
- beforeEach(async function () {
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- alreadyLoaded: false,
- })
- this.result = await this.DocumentManager.promises.getDocAndFlushIfOld(
- this.project_id,
- this.doc_id
- )
- })
- it('should get the doc', function () {
- this.DocumentManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should not flush the doc', function () {
- this.DocumentManager.promises.flushDocIfLoaded.called.should.equal(
- false
- )
- })
- it('should return the lines and versions', function () {
- expect(this.result).to.deep.equal({
- lines: this.lines,
- version: this.version,
- })
- })
- })
- })
- describe('renameDoc', function () {
- beforeEach(function () {
- this.update = 'some-update'
- })
- describe('successfully', function () {
- beforeEach(async function () {
- await this.DocumentManager.promises.renameDoc(
- this.project_id,
- this.doc_id,
- this.user_id,
- this.update,
- this.projectHistoryId
- )
- })
- it('should rename the document', function () {
- this.RedisManager.promises.renameDoc
- .calledWith(
- this.project_id,
- this.doc_id,
- this.user_id,
- this.update,
- this.projectHistoryId
- )
- .should.equal(true)
- })
- })
- })
- describe('resyncDocContents', function () {
- describe('when doc is loaded in redis', function () {
- beforeEach(async function () {
- this.pathnameFromProjectStructureUpdate = '/foo/bar.tex'
- this.RedisManager.promises.getDoc.resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- resolvedCommentIds: this.resolvedCommentIds,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- historyRangesSupport: this.historyRangesSupport,
- })
- await this.DocumentManager.promises.resyncDocContents(
- this.project_id,
- this.doc_id,
- this.pathnameFromProjectStructureUpdate
- )
- })
- it('gets the doc contents from redis', function () {
- this.RedisManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('queues a resync doc content update', function () {
- this.ProjectHistoryRedisManager.promises.queueResyncDocContent
- .calledWith(
- this.project_id,
- this.projectHistoryId,
- this.doc_id,
- this.lines,
- this.ranges,
- this.resolvedCommentIds,
- this.version,
- this.pathnameFromProjectStructureUpdate,
- this.historyRangesSupport
- )
- .should.equal(true)
- })
- })
- describe('when doc is not loaded in redis', function () {
- beforeEach(async function () {
- this.pathnameFromProjectStructureUpdate = '/foo/bar.tex'
- this.RedisManager.promises.getDoc.resolves({})
- this.PersistenceManager.promises.getDoc.resolves({
- lines: this.lines,
- version: this.version,
- ranges: this.ranges,
- resolvedCommentIds: this.resolvedCommentIds,
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- historyRangesSupport: this.historyRangesSupport,
- })
- await this.DocumentManager.promises.resyncDocContents(
- this.project_id,
- this.doc_id,
- this.pathnameFromProjectStructureUpdate
- )
- })
- it('tries to get the doc contents from redis', function () {
- this.RedisManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('gets the doc contents from web', function () {
- this.PersistenceManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id, { peek: true })
- .should.equal(true)
- })
- it('queues a resync doc content update', function () {
- this.ProjectHistoryRedisManager.promises.queueResyncDocContent
- .calledWith(
- this.project_id,
- this.projectHistoryId,
- this.doc_id,
- this.lines,
- this.ranges,
- this.resolvedCommentIds,
- this.version,
- this.pathnameFromProjectStructureUpdate,
- this.historyRangesSupport
- )
- .should.equal(true)
- })
- })
- describe('when a doc has no ranges in docstore', function () {
- beforeEach(async function () {
- this.pathnameFromProjectStructureUpdate = '/foo/bar.tex'
- this.RedisManager.promises.getDoc.resolves({})
- this.PersistenceManager.promises.getDoc.resolves({
- lines: this.lines,
- version: this.version,
- ranges: undefined,
- resolvedCommentIds: [],
- pathname: this.pathname,
- projectHistoryId: this.projectHistoryId,
- historyRangesSupport: this.historyRangesSupport,
- })
- await this.DocumentManager.promises.resyncDocContents(
- this.project_id,
- this.doc_id,
- this.pathnameFromProjectStructureUpdate
- )
- })
- it('gets the doc contents from web', function () {
- this.PersistenceManager.promises.getDoc
- .calledWith(this.project_id, this.doc_id, { peek: true })
- .should.equal(true)
- })
- it('queues a resync doc content update with an empty ranges object', function () {
- this.ProjectHistoryRedisManager.promises.queueResyncDocContent
- .calledWith(
- this.project_id,
- this.projectHistoryId,
- this.doc_id,
- this.lines,
- {},
- [],
- this.version,
- this.pathnameFromProjectStructureUpdate,
- this.historyRangesSupport
- )
- .should.equal(true)
- })
- })
- })
- describe('appendToDoc', function () {
- describe('sucessfully', function () {
- beforeEach(async function () {
- this.lines = ['one', 'two', 'three']
- this.DocumentManager.promises.setDoc = sinon
- .stub()
- .resolves({ rev: '123' })
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.lines,
- })
- this.result = await this.DocumentManager.promises.appendToDoc(
- this.project_id,
- this.doc_id,
- ['four', 'five', 'six'],
- this.source,
- this.user_id
- )
- })
- it('should call setDoc with concatenated lines', function () {
- this.DocumentManager.promises.setDoc
- .calledWith(
- this.project_id,
- this.doc_id,
- ['one', 'two', 'three', 'four', 'five', 'six'],
- this.source,
- this.user_id,
- false,
- false
- )
- .should.equal(true)
- })
- it('should return output from setDoc', function () {
- this.result.should.deep.equal({ rev: '123' })
- })
- })
- describe('when doc would become too big', function () {
- beforeEach(async function () {
- this.Settings.max_doc_length = 100
- this.lines = ['one', 'two', 'three']
- this.DocumentManager.promises.setDoc = sinon
- .stub()
- .resolves({ rev: '123' })
- this.DocumentManager.promises.getDoc = sinon.stub().resolves({
- lines: this.lines,
- })
- })
- it('should fail with FileTooLarge error', async function () {
- expect(
- this.DocumentManager.promises.appendToDoc(
- this.project_id,
- this.doc_id,
- ['x'.repeat(1000)],
- this.source,
- this.user_id
- )
- ).to.eventually.be.rejectedWith(Errors.FileTooLargeError)
- })
- })
- })
- })
|