| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613 |
- import { vi, expect } from 'vitest'
- import sinon from 'sinon'
- import path from 'node:path'
- import mongodb from 'mongodb-legacy'
- import nock from 'nock'
- const { ObjectId } = mongodb
- const modulePath = path.join(
- import.meta.dirname,
- '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler'
- )
- describe('DocumentUpdaterHandler', function () {
- beforeEach(async function (ctx) {
- ctx.project_id = 'project-id-923'
- ctx.projectHistoryId = 'ol-project-id-1'
- ctx.doc_id = 'doc-id-394'
- ctx.lines = ['one', 'two', 'three']
- ctx.version = 42
- ctx.user_id = 'mock-user-id-123'
- ctx.project = { _id: ctx.project_id }
- ctx.projectEntityHandler = {}
- ctx.settings = {
- apis: {
- documentupdater: {
- url: 'http://document_updater.example.com',
- },
- project_history: {
- url: 'http://project_history.example.com',
- },
- },
- moduleImportSequence: [],
- }
- ctx.source = 'dropbox'
- ctx.docUpdaterMock = nock(ctx.settings.apis.documentupdater.url)
- ctx.ProjectGetter = {
- promises: {
- getProjectWithoutLock: sinon.stub(),
- },
- }
- ctx.ProjectGetter.promises.getProjectWithoutLock
- .withArgs(ctx.project_id)
- .resolves(ctx.project)
- vi.doMock('@overleaf/settings', () => ({
- default: ctx.settings,
- }))
- vi.doMock(
- '../../../../app/src/Features/Project/ProjectEntityHandler',
- () => ({
- default: ctx.projectEntityHandler,
- })
- )
- vi.doMock('../../../../app/src/models/Project', () => ({
- Project: (ctx.Project = {}),
- }))
- vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
- default: ctx.ProjectGetter,
- }))
- vi.doMock('../../../../app/src/Features/Project/ProjectLocator', () => ({
- default: {},
- }))
- vi.doMock('@overleaf/metrics', () => ({
- default: {
- Timer: class {
- done() {}
- },
- },
- }))
- vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
- default: {
- promises: {
- hooks: {
- fire: sinon.stub().resolves(),
- },
- },
- },
- }))
- ctx.handler = (await import(modulePath)).default
- })
- afterEach(function () {
- nock.cleanAll()
- })
- describe('flushProjectToMongo', function () {
- describe('successfully', function () {
- beforeEach(async function (ctx) {
- ctx.docUpdaterMock.post(`/project/${ctx.project_id}/flush`).reply(204)
- await ctx.handler.promises.flushProjectToMongo(ctx.project_id)
- })
- it('should flush the document from the document updater', function (ctx) {
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/flush`)
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(ctx.handler.promises.flushProjectToMongo(ctx.project_id))
- .to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock.post(`/project/${ctx.project_id}/flush`).reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(ctx.handler.promises.flushProjectToMongo(ctx.project_id))
- .to.be.rejected
- })
- })
- })
- describe('flushProjectToMongoAndDelete', function () {
- describe('successfully', function () {
- beforeEach(async function (ctx) {
- ctx.docUpdaterMock.delete(`/project/${ctx.project_id}`).reply(204)
- await ctx.handler.promises.flushProjectToMongoAndDelete(ctx.project_id)
- })
- it('should delete the project from the document updater', function (ctx) {
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .delete(`/project/${ctx.project_id}`)
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.flushProjectToMongoAndDelete(ctx.project_id)
- ).to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock.delete(`/project/${ctx.project_id}`).reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.flushProjectToMongoAndDelete(ctx.project_id)
- ).to.be.rejected
- })
- })
- })
- describe('flushDocToMongo', function () {
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/flush`)
- .reply(204)
- })
- it('should flush the document from the document updater', async function (ctx) {
- await ctx.handler.promises.flushDocToMongo(ctx.project_id, ctx.doc_id)
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/flush`)
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.flushDocToMongo(ctx.project_id, ctx.doc_id)
- ).to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/flush`)
- .reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.flushDocToMongo(ctx.project_id, ctx.doc_id)
- ).to.be.rejected
- })
- })
- })
- describe('deleteDoc', function () {
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .delete(`/project/${ctx.project_id}/doc/${ctx.doc_id}`)
- .reply(204)
- })
- it('should delete the document from the document updater', async function (ctx) {
- await ctx.handler.promises.deleteDoc(ctx.project_id, ctx.doc_id)
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .delete(`/project/${ctx.project_id}/doc/${ctx.doc_id}`)
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(ctx.handler.promises.deleteDoc(ctx.project_id, ctx.doc_id))
- .to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .delete(`/project/${ctx.project_id}/doc/${ctx.doc_id}`)
- .reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(ctx.handler.promises.deleteDoc(ctx.project_id, ctx.doc_id))
- .to.be.rejected
- })
- })
- describe("with 'ignoreFlushErrors' option", function () {
- it('when option is true, should send a `ignore_flush_errors=true` URL query to document-updater', async function (ctx) {
- ctx.docUpdaterMock
- .delete(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}?ignore_flush_errors=true`
- )
- .reply(204)
- await ctx.handler.promises.deleteDoc(ctx.project_id, ctx.doc_id, true)
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- it("when option is false, shouldn't send any URL query to document-updater", async function (ctx) {
- ctx.docUpdaterMock
- .delete(`/project/${ctx.project_id}/doc/${ctx.doc_id}`)
- .reply(204)
- await ctx.handler.promises.deleteDoc(ctx.project_id, ctx.doc_id, false)
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- })
- describe('setDocument', function () {
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}`, {
- lines: ctx.lines,
- source: ctx.source,
- user_id: ctx.user_id,
- })
- .reply(204)
- })
- it('should set the document in the document updater', async function (ctx) {
- await ctx.handler.promises.setDocument(
- ctx.project_id,
- ctx.doc_id,
- ctx.user_id,
- ctx.lines,
- ctx.source
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}`, {
- lines: ctx.lines,
- source: ctx.source,
- user_id: ctx.user_id,
- })
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.setDocument(
- ctx.project_id,
- ctx.doc_id,
- ctx.user_id,
- ctx.lines,
- ctx.source
- )
- ).to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}`, {
- lines: ctx.lines,
- source: ctx.source,
- user_id: ctx.user_id,
- })
- .reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.setDocument(
- ctx.project_id,
- ctx.doc_id,
- ctx.user_id,
- ctx.lines,
- ctx.source
- )
- ).to.be.rejected
- })
- })
- })
- describe('getComment', function () {
- beforeEach(function (ctx) {
- ctx.comment = { id: new ObjectId().toString() }
- ctx.docUpdaterMock
- .get(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.comment.id}`
- )
- .reply(200, ctx.comment)
- })
- it('should get the comment from the document updater', async function (ctx) {
- const body = await ctx.handler.promises.getComment(
- ctx.project_id,
- ctx.doc_id,
- ctx.comment.id
- )
- expect(body).to.deep.equal(ctx.comment)
- })
- })
- describe('getDocument', function () {
- beforeEach(function (ctx) {
- ctx.doc = {
- lines: ctx.lines,
- version: ctx.version,
- ops: ['mock-op-1', 'mock-op-2'],
- ranges: { mock: 'ranges' },
- }
- ctx.fromVersion = 2
- })
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .get(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}?fromVersion=${ctx.fromVersion}`
- )
- .reply(200, ctx.doc)
- })
- it('should return the lines and version', async function (ctx) {
- const doc = await ctx.handler.promises.getDocument(
- ctx.project_id,
- ctx.doc_id,
- ctx.fromVersion
- )
- expect(doc).to.deep.equal(ctx.doc)
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .get(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}?fromVersion=${ctx.fromVersion}`
- )
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.getDocument(
- ctx.project_id,
- ctx.doc_id,
- ctx.fromVersion
- )
- ).to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .get(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}?fromVersion=${ctx.fromVersion}`
- )
- .reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.getDocument(
- ctx.project_id,
- ctx.doc_id,
- ctx.fromVersion
- )
- ).to.be.rejected
- })
- })
- })
- describe('getProjectDocsIfMatch', function () {
- beforeEach(function (ctx) {
- ctx.project_state_hash = '1234567890abcdef'
- ctx.doc0 = {
- _id: ctx.doc_id,
- lines: ctx.lines,
- v: ctx.version,
- }
- ctx.docs = [ctx.doc0, ctx.doc0, ctx.doc0]
- })
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(
- `/project/${ctx.project_id}/get_and_flush_if_old?state=${ctx.project_state_hash}`
- )
- .reply(200, ctx.docs)
- })
- it('should call the callback with the documents', async function (ctx) {
- const docs = await ctx.handler.promises.getProjectDocsIfMatch(
- ctx.project_id,
- ctx.project_state_hash
- )
- expect(docs).to.deep.equal(ctx.docs)
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(
- `/project/${ctx.project_id}/get_and_flush_if_old?state=${ctx.project_state_hash}`
- )
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.getProjectDocsIfMatch(
- ctx.project_id,
- ctx.project_state_hash
- )
- ).to.be.rejected
- })
- })
- describe('when the document updater returns a conflict error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(
- `/project/${ctx.project_id}/get_and_flush_if_old?state=${ctx.project_state_hash}`
- )
- .reply(409)
- })
- it('should return no documents', async function (ctx) {
- const response = await ctx.handler.promises.getProjectDocsIfMatch(
- ctx.project_id,
- ctx.project_state_hash
- )
- expect(response).to.be.undefined
- })
- })
- })
- describe('clearProjectState', function () {
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/clearState`)
- .reply(200)
- })
- it('should clear the project state from the document updater', async function (ctx) {
- await ctx.handler.promises.clearProjectState(ctx.project_id)
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/clearState`)
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(ctx.handler.promises.clearProjectState(ctx.project_id)).to
- .be.rejected
- })
- })
- describe('when the document updater returns an error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/clearState`)
- .reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(ctx.handler.promises.clearProjectState(ctx.project_id)).to
- .be.rejected
- })
- })
- })
- describe('acceptChanges', function () {
- beforeEach(function (ctx) {
- ctx.change_id = 'mock-change-id-1'
- })
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/change/accept`, {
- change_ids: [ctx.change_id],
- })
- .reply(200)
- })
- it('should accept the change in the document updater', async function (ctx) {
- await ctx.handler.promises.acceptChanges(ctx.project_id, ctx.doc_id, [
- ctx.change_id,
- ])
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/change/accept`)
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.acceptChanges(ctx.project_id, ctx.doc_id, [
- ctx.change_id,
- ])
- ).to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/change/accept`)
- .reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.acceptChanges(ctx.project_id, ctx.doc_id, [
- ctx.change_id,
- ])
- ).to.be.rejected
- })
- })
- })
- describe('deleteThread', function () {
- beforeEach(function (ctx) {
- ctx.thread_id = 'mock-thread-id-1'
- })
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .delete(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}`
- )
- .reply(200)
- })
- it('should delete the thread in the document updater', async function (ctx) {
- await ctx.handler.promises.deleteThread(
- ctx.project_id,
- ctx.doc_id,
- ctx.thread_id,
- ctx.user_id
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .delete(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}`
- )
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.deleteThread(
- ctx.project_id,
- ctx.doc_id,
- ctx.thread_id,
- ctx.user_id
- )
- ).to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .delete(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}`
- )
- .reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.deleteThread(
- ctx.project_id,
- ctx.doc_id,
- ctx.thread_id,
- ctx.user_id
- )
- ).to.be.rejected
- })
- })
- })
- describe('resolveThread', function () {
- beforeEach(function (ctx) {
- ctx.thread_id = 'mock-thread-id-1'
- })
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/resolve`
- )
- .reply(200)
- })
- it('should resolve the thread in the document updater', async function (ctx) {
- await ctx.handler.promises.resolveThread(
- ctx.project_id,
- ctx.doc_id,
- ctx.thread_id,
- ctx.user_id
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/resolve`
- )
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.resolveThread(
- ctx.project_id,
- ctx.doc_id,
- ctx.thread_id,
- ctx.user_id
- )
- ).to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/resolve`
- )
- .reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.resolveThread(
- ctx.project_id,
- ctx.doc_id,
- ctx.thread_id,
- ctx.user_id
- )
- ).to.be.rejected
- })
- })
- })
- describe('reopenThread', function () {
- beforeEach(function (ctx) {
- ctx.thread_id = 'mock-thread-id-1'
- })
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/reopen`
- )
- .reply(200)
- })
- it('should reopen the thread in the document updater', async function (ctx) {
- await ctx.handler.promises.reopenThread(
- ctx.project_id,
- ctx.doc_id,
- ctx.thread_id,
- ctx.user_id
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/reopen`
- )
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.reopenThread(
- ctx.project_id,
- ctx.doc_id,
- ctx.thread_id,
- ctx.user_id
- )
- ).to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(
- `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/reopen`
- )
- .reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.reopenThread(
- ctx.project_id,
- ctx.doc_id,
- ctx.thread_id,
- ctx.user_id
- )
- ).to.be.rejected
- })
- })
- })
- describe('updateProjectStructure ', function () {
- beforeEach(function (ctx) {
- ctx.user_id = 1234
- ctx.version = 999
- })
- describe('with project history disabled', function () {
- beforeEach(function (ctx) {
- ctx.settings.apis.project_history.sendProjectStructureOps = false
- })
- it('returns early', async function (ctx) {
- await ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- {},
- ctx.source
- )
- })
- })
- describe('with project history enabled', function () {
- beforeEach(function (ctx) {
- ctx.settings.apis.project_history.sendProjectStructureOps = true
- })
- describe('when an entity has changed name', function () {
- it('should send the structure update to the document updater', async function (ctx) {
- const docIdA = new ObjectId()
- const docIdB = new ObjectId()
- const changes = {
- oldDocs: [
- { path: '/old_a', doc: { _id: docIdA } },
- { path: '/old_b', doc: { _id: docIdB } },
- ],
- // create new instances of the same ObjectIds so that == doesn't pass
- newDocs: [
- {
- path: '/old_a',
- doc: { _id: new ObjectId(docIdA.toString()) },
- },
- {
- path: '/new_b',
- doc: { _id: new ObjectId(docIdB.toString()) },
- },
- ],
- newProject: { version: ctx.version },
- }
- const updates = [
- {
- type: 'rename-doc',
- id: docIdB.toString(),
- pathname: '/old_b',
- newPathname: '/new_b',
- },
- ]
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}`, {
- updates,
- userId: ctx.user_id,
- version: ctx.version,
- projectHistoryId: ctx.projectHistoryId,
- source: ctx.source,
- })
- .reply(204)
- await ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- changes,
- ctx.source
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when a doc has been added', function () {
- it('should send the structure update to the document updater', async function (ctx) {
- const docId = new ObjectId()
- const changes = {
- newDocs: [{ path: '/foo', docLines: 'a\nb', doc: { _id: docId } }],
- newProject: { version: ctx.version },
- }
- const updates = [
- {
- type: 'add-doc',
- id: docId.toString(),
- pathname: '/foo',
- docLines: 'a\nb',
- historyRangesSupport: false,
- hash: undefined,
- ranges: undefined,
- metadata: undefined,
- createdBlob: true,
- },
- ]
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}`, {
- updates,
- userId: ctx.user_id,
- version: ctx.version,
- projectHistoryId: ctx.projectHistoryId,
- source: ctx.source,
- })
- .reply(204)
- await ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- changes,
- ctx.source
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when a file has been added', function () {
- it('should send the structure update to the document updater', async function (ctx) {
- const fileId = new ObjectId()
- const changes = {
- newFiles: [
- {
- path: '/bar',
- file: { _id: fileId, hash: '12345' },
- },
- ],
- newProject: { version: ctx.version },
- }
- const updates = [
- {
- type: 'add-file',
- id: fileId.toString(),
- pathname: '/bar',
- docLines: undefined,
- historyRangesSupport: false,
- hash: '12345',
- ranges: undefined,
- createdBlob: true,
- metadata: undefined,
- },
- ]
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}`, {
- updates,
- userId: ctx.user_id,
- version: ctx.version,
- projectHistoryId: ctx.projectHistoryId,
- source: ctx.source,
- })
- .reply(204)
- await ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- changes,
- ctx.source
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when an entity has been deleted', function () {
- it('should end the structure update to the document updater', async function (ctx) {
- const docId = new ObjectId()
- const changes = {
- oldDocs: [{ path: '/foo', docLines: 'a\nb', doc: { _id: docId } }],
- newProject: { version: ctx.version },
- }
- const updates = [
- {
- type: 'rename-doc',
- id: docId.toString(),
- pathname: '/foo',
- newPathname: '',
- },
- ]
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}`, {
- updates,
- userId: ctx.user_id,
- version: ctx.version,
- projectHistoryId: ctx.projectHistoryId,
- source: ctx.source,
- })
- .reply(204)
- await ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- changes,
- ctx.source
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when a file is converted to a doc', function () {
- it('should send the delete first', async function (ctx) {
- const docId = new ObjectId()
- const fileId = new ObjectId()
- const changes = {
- oldFiles: [
- {
- path: '/foo.doc',
- file: { _id: fileId },
- },
- ],
- newDocs: [
- {
- path: '/foo.doc',
- docLines: 'hello there',
- doc: { _id: docId },
- },
- ],
- newProject: { version: ctx.version },
- }
- const updates = [
- {
- type: 'rename-file',
- id: fileId.toString(),
- pathname: '/foo.doc',
- newPathname: '',
- },
- {
- type: 'add-doc',
- id: docId.toString(),
- pathname: '/foo.doc',
- docLines: 'hello there',
- historyRangesSupport: false,
- hash: undefined,
- ranges: undefined,
- metadata: undefined,
- createdBlob: true,
- },
- ]
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}`, {
- updates,
- userId: ctx.user_id,
- version: ctx.version,
- projectHistoryId: ctx.projectHistoryId,
- source: ctx.source,
- })
- .reply(204)
- await ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- changes,
- ctx.source
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the project version is missing', function () {
- it('should call the callback with an error', async function (ctx) {
- const docId = new ObjectId()
- const changes = {
- oldDocs: [{ path: '/foo', docLines: 'a\nb', doc: { _id: docId } }],
- }
- await expect(
- ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- changes,
- ctx.source
- )
- ).to.be.rejectedWith('did not receive project version in changes')
- })
- })
- describe('when ranges are present', function () {
- beforeEach(function (ctx) {
- ctx.docId = new ObjectId()
- ctx.ranges = {
- changes: [
- {
- op: { p: 0, i: 'foo' },
- metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
- },
- {
- op: { p: 7, d: ' baz' },
- metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
- },
- ],
- comments: [
- {
- op: { p: 4, c: 'bar', t: 'comment-1' },
- metadata: { resolved: false },
- },
- ],
- }
- ctx.changes = {
- newDocs: [
- {
- path: '/foo',
- docLines: 'foo\nbar',
- doc: { _id: ctx.docId },
- ranges: ctx.ranges,
- },
- ],
- newProject: { version: ctx.version },
- }
- })
- it('should forward ranges', async function (ctx) {
- const updates = [
- {
- type: 'add-doc',
- id: ctx.docId.toString(),
- pathname: '/foo',
- docLines: 'foo\nbar',
- historyRangesSupport: false,
- hash: undefined,
- ranges: ctx.ranges,
- metadata: undefined,
- createdBlob: true,
- },
- ]
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}`, {
- updates,
- userId: ctx.user_id,
- version: ctx.version,
- projectHistoryId: ctx.projectHistoryId,
- source: ctx.source,
- })
- .reply(204)
- await ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- ctx.changes,
- ctx.source
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- it('should include flag when history ranges support is enabled', async function (ctx) {
- ctx.ProjectGetter.promises.getProjectWithoutLock
- .withArgs(ctx.project_id)
- .resolves({
- _id: ctx.project_id,
- overleaf: { history: { rangesSupportEnabled: true } },
- })
- const updates = [
- {
- type: 'add-doc',
- id: ctx.docId.toString(),
- pathname: '/foo',
- docLines: 'foo\nbar',
- historyRangesSupport: true,
- hash: undefined,
- ranges: ctx.ranges,
- metadata: undefined,
- createdBlob: true,
- },
- ]
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}`, {
- updates,
- userId: ctx.user_id,
- version: ctx.version,
- projectHistoryId: ctx.projectHistoryId,
- source: ctx.source,
- })
- .reply(204)
- await ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- ctx.changes,
- ctx.source
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('with filestore disabled', function () {
- beforeEach(function (ctx) {
- ctx.fileId = new ObjectId()
- const updates = [
- {
- type: 'add-file',
- id: ctx.fileId.toString(),
- pathname: '/bar',
- docLines: undefined,
- historyRangesSupport: false,
- hash: '12345',
- ranges: undefined,
- createdBlob: true,
- metadata: undefined,
- },
- ]
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}`, {
- updates,
- userId: ctx.user_id,
- version: ctx.version,
- projectHistoryId: ctx.projectHistoryId,
- source: ctx.source,
- })
- .reply(204)
- })
- it('should add files without URL and with createdBlob', async function (ctx) {
- ctx.changes = {
- newFiles: [
- {
- path: '/bar',
- file: { _id: ctx.fileId, hash: '12345' },
- },
- ],
- newProject: { version: ctx.version },
- }
- await ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- ctx.changes,
- ctx.source
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- it('should flag files without hash', async function (ctx) {
- ctx.fileId = new ObjectId()
- ctx.changes = {
- newFiles: [
- {
- path: '/bar',
- file: { _id: ctx.fileId },
- },
- ],
- newProject: { version: ctx.version },
- }
- await expect(
- ctx.handler.promises.updateProjectStructure(
- ctx.project_id,
- ctx.projectHistoryId,
- ctx.user_id,
- ctx.changes,
- ctx.source
- )
- ).to.be.rejected
- })
- })
- })
- })
- describe('resyncProjectHistory', function () {
- it('should add docs', async function (ctx) {
- const docId1 = new ObjectId()
- const docId2 = new ObjectId()
- const docs = [
- { doc: { _id: docId1 }, path: 'main.tex' },
- { doc: { _id: docId2 }, path: 'references.bib' },
- ]
- const files = []
- const projectId = new ObjectId()
- const projectHistoryId = 99
- ctx.docUpdaterMock
- .post(`/project/${projectId}/history/resync`, {
- docs: [
- { doc: docId1.toString(), path: 'main.tex' },
- { doc: docId2.toString(), path: 'references.bib' },
- ],
- files: [],
- projectHistoryId,
- })
- .reply(200)
- await ctx.handler.promises.resyncProjectHistory(
- projectId,
- projectHistoryId,
- docs,
- files,
- {}
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- it('should add files', async function (ctx) {
- const fileId1 = new ObjectId()
- const fileId2 = new ObjectId()
- const fileId3 = new ObjectId()
- const fileCreated2 = new Date()
- const fileCreated3 = new Date()
- const otherProjectId = new ObjectId().toString()
- const files = [
- { file: { _id: fileId1, hash: '42' }, path: '1.png' },
- {
- file: {
- _id: fileId2,
- hash: '1337',
- created: fileCreated2,
- linkedFileData: {
- provider: 'references-provider',
- },
- },
- path: '1.bib',
- },
- {
- file: {
- _id: fileId3,
- hash: '21',
- created: fileCreated3,
- linkedFileData: {
- provider: 'project_output_file',
- build_id: '1234-abc',
- clsiServerId: 'server-1',
- source_project_id: otherProjectId,
- source_output_file_path: 'foo/bar.txt',
- },
- },
- path: 'bar.txt',
- },
- ]
- const docs = []
- const projectId = new ObjectId()
- const projectHistoryId = 99
- ctx.docUpdaterMock
- .post(`/project/${projectId}/history/resync`, {
- docs: [],
- files: [
- {
- file: fileId1.toString(),
- _hash: '42',
- path: '1.png',
- createdBlob: true,
- },
- {
- file: fileId2.toString(),
- _hash: '1337',
- path: '1.bib',
- createdBlob: true,
- metadata: {
- importedAt: fileCreated2.toISOString(),
- provider: 'references-provider',
- },
- },
- {
- file: fileId3.toString(),
- _hash: '21',
- path: 'bar.txt',
- createdBlob: true,
- metadata: {
- importedAt: fileCreated3.toISOString(),
- provider: 'project_output_file',
- source_project_id: otherProjectId.toString(),
- source_output_file_path: 'foo/bar.txt',
- // build_id and clsiServerId are omitted
- },
- },
- ],
- projectHistoryId,
- })
- .reply(200)
- await ctx.handler.promises.resyncProjectHistory(
- projectId,
- projectHistoryId,
- docs,
- files,
- {}
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- it('should add files without URL', async function (ctx) {
- const fileId1 = new ObjectId()
- const fileId2 = new ObjectId()
- const fileId3 = new ObjectId()
- const fileCreated2 = new Date()
- const fileCreated3 = new Date()
- const otherProjectId = new ObjectId().toString()
- const files = [
- { file: { _id: fileId1, hash: '42' }, path: '1.png' },
- {
- file: {
- _id: fileId2,
- hash: '1337',
- created: fileCreated2,
- linkedFileData: {
- provider: 'references-provider',
- },
- },
- path: '1.bib',
- },
- {
- file: {
- _id: fileId3,
- hash: '21',
- created: fileCreated3,
- linkedFileData: {
- provider: 'project_output_file',
- build_id: '1234-abc',
- clsiServerId: 'server-1',
- source_project_id: otherProjectId,
- source_output_file_path: 'foo/bar.txt',
- },
- },
- path: 'bar.txt',
- },
- ]
- const docs = []
- const projectId = new ObjectId()
- const projectHistoryId = 99
- ctx.docUpdaterMock
- .post(`/project/${projectId}/history/resync`, {
- docs: [],
- files: [
- {
- file: fileId1.toString(),
- _hash: '42',
- path: '1.png',
- createdBlob: true,
- },
- {
- file: fileId2.toString(),
- _hash: '1337',
- path: '1.bib',
- createdBlob: true,
- metadata: {
- importedAt: fileCreated2.toISOString(),
- provider: 'references-provider',
- },
- },
- {
- file: fileId3.toString(),
- _hash: '21',
- path: 'bar.txt',
- createdBlob: true,
- metadata: {
- importedAt: fileCreated3.toISOString(),
- provider: 'project_output_file',
- source_project_id: otherProjectId,
- source_output_file_path: 'foo/bar.txt',
- // build_id and clsiServerId are omitted
- },
- },
- ],
- projectHistoryId,
- })
- .reply(200)
- await ctx.handler.promises.resyncProjectHistory(
- projectId,
- projectHistoryId,
- docs,
- files,
- {}
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- it('should flag files with missing hashes', async function (ctx) {
- const fileId1 = new ObjectId()
- const fileId2 = new ObjectId()
- const fileId3 = new ObjectId()
- const fileCreated2 = new Date()
- const fileCreated3 = new Date()
- const otherProjectId = new ObjectId().toString()
- const files = [
- { file: { _id: fileId1, hash: '42' }, path: '1.png' },
- {
- file: {
- _id: fileId2,
- created: fileCreated2,
- linkedFileData: {
- provider: 'references-provider',
- },
- },
- path: '1.bib',
- },
- {
- file: {
- _id: fileId3,
- hash: '21',
- created: fileCreated3,
- linkedFileData: {
- provider: 'project_output_file',
- build_id: '1234-abc',
- clsiServerId: 'server-1',
- source_project_id: otherProjectId,
- source_output_file_path: 'foo/bar.txt',
- },
- },
- path: 'bar.txt',
- },
- ]
- const docs = []
- const projectId = new ObjectId()
- const projectHistoryId = 99
- await expect(
- ctx.handler.promises.resyncProjectHistory(
- projectId,
- projectHistoryId,
- docs,
- files,
- {}
- )
- ).to.be.rejected
- })
- })
- describe('appendToDocument', function () {
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.body = { rev: 1 }
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/append`, {
- lines: ctx.lines,
- source: ctx.source,
- user_id: ctx.user_id,
- })
- .reply(200)
- })
- it('should append to the document in the document updater', async function (ctx) {
- await ctx.handler.promises.appendToDocument(
- ctx.project_id,
- ctx.doc_id,
- ctx.user_id,
- ctx.lines,
- ctx.source
- )
- expect(ctx.docUpdaterMock.isDone()).to.be.true
- })
- })
- describe('when the document updater API returns an error', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/append`)
- .replyWithError('boom')
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.appendToDocument(
- ctx.project_id,
- ctx.doc_id,
- ctx.user_id,
- ctx.lines,
- ctx.source
- )
- ).to.be.rejected
- })
- })
- describe('when the document updater returns a failure error code', function () {
- beforeEach(function (ctx) {
- ctx.docUpdaterMock
- .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/append`)
- .reply(500)
- })
- it('should reject with an error', async function (ctx) {
- await expect(
- ctx.handler.promises.appendToDocument(
- ctx.project_id,
- ctx.doc_id,
- ctx.user_id,
- ctx.lines,
- ctx.source
- )
- ).to.be.rejected
- })
- })
- })
- })
|