| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271 |
- import { vi, expect } from 'vitest'
- import sinon from 'sinon'
- import Errors from '../../../../app/src/Features/Errors/Errors.js'
- import tk from 'timekeeper'
- import moment from 'moment'
- const modulePath = '../../../../app/src/Features/History/RestoreManager'
- function nestedMapWithSetToObject(m) {
- return Object.fromEntries(
- Array.from(m.entries()).map(([key, set]) => [key, Array.from(set)])
- )
- }
- describe('RestoreManager', function () {
- beforeEach(async function (ctx) {
- tk.freeze(Date.now()) // freeze the time for these tests
- ctx.fsPath = '/tmp/path/on/disk'
- ctx.blobStream = 'blob-stream'
- vi.doMock('../../../../app/src/Features/Errors/Errors.js', () => ({
- default: Errors,
- }))
- vi.doMock(
- '../../../../app/src/Features/History/HistoryManager.mjs',
- () => ({
- default: (ctx.HistoryManager = {
- promises: {
- getContentAtVersion: sinon.stub().resolves({
- // Raw snapshot data that will be passed to Snapshot.fromRaw
- files: {
- 'main.tex': {
- hash: 'abcdef1234567890abcdef1234567890abcdef12',
- stringLength: 100,
- metadata: {
- editorId: 'test-editor',
- },
- },
- 'foo.tex': {
- hash: 'abcdef1234567890abcdef1234567890abcdef12',
- stringLength: 100,
- metadata: {
- editorId: 'test-editor',
- },
- },
- 'folder/file.tex': {
- hash: 'abcdef1234567890abcdef1234567890abcdef12',
- stringLength: 100,
- metadata: {
- editorId: 'test-editor',
- },
- },
- 'foo.png': {
- hash: 'abcdef1234567890abcdef1234567890abcdef12',
- stringLength: 100,
- metadata: {
- provider: 'bar',
- },
- },
- 'linkedFile.bib': {
- hash: 'abcdef1234567890abcdef1234567890abcdef12',
- stringLength: 100,
- metadata: {
- provider: 'mendeley',
- },
- },
- 'withMainTrue.tex': {
- hash: 'abcdef1234567890abcdef1234567890abcdef12',
- stringLength: 100,
- metadata: {
- main: true,
- },
- },
- },
- timestamp: new Date().toISOString(),
- }),
- requestBlob: sinon.stub().resolves({ stream: ctx.blobStream }),
- },
- }),
- })
- )
- vi.doMock('../../../../app/src/infrastructure/Metrics.js', () => ({
- default: {
- revertFileDurationSeconds: {
- startTimer: sinon.stub().returns(sinon.stub()),
- },
- revertProjectDurationSeconds: {
- startTimer: sinon.stub().returns(sinon.stub()),
- },
- },
- }))
- vi.doMock('@overleaf/settings', () => ({
- default: {},
- }))
- vi.doMock('../../../../app/src/infrastructure/FileWriter', () => ({
- default: (ctx.FileWriter = {
- promises: {
- writeStreamToDisk: sinon.stub().resolves(ctx.fsPath),
- writeContentToDisk: sinon.stub().resolves(ctx.fsPath),
- },
- }),
- }))
- vi.doMock(
- '../../../../app/src/Features/Uploads/FileSystemImportManager',
- () => ({
- default: (ctx.FileSystemImportManager = {
- promises: {},
- }),
- })
- )
- vi.doMock('../../../../app/src/Features/Editor/EditorController', () => ({
- default: (ctx.EditorController = {
- promises: {},
- }),
- }))
- vi.doMock('../../../../app/src/Features/Project/ProjectLocator', () => ({
- default: (ctx.ProjectLocator = { promises: {} }),
- }))
- vi.doMock(
- '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler',
- () => ({
- default: (ctx.DocumentUpdaterHandler = {
- promises: { flushProjectToMongo: sinon.stub().resolves() },
- }),
- })
- )
- vi.doMock('../../../../app/src/Features/Docstore/DocstoreManager', () => ({
- default: (ctx.DocstoreManager = {
- promises: { getCommentThreadIds: sinon.stub().resolves({}) },
- }),
- }))
- vi.doMock('../../../../app/src/Features/Chat/ChatApiHandler', () => ({
- default: (ctx.ChatApiHandler = { promises: {} }),
- }))
- vi.doMock('../../../../app/src/Features/Chat/ChatManager', () => ({
- default: (ctx.ChatManager = { promises: {} }),
- }))
- vi.doMock(
- '../../../../app/src/Features/Editor/EditorRealTimeController',
- () => ({
- default: (ctx.EditorRealTimeController = {}),
- })
- )
- vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
- default: (ctx.ProjectGetter = { promises: {} }),
- }))
- vi.doMock(
- '../../../../app/src/Features/Project/ProjectEntityHandler',
- () => ({
- default: (ctx.ProjectEntityHandler = {
- promises: {},
- }),
- })
- )
- vi.doMock('overleaf-editor-core', () => ({
- Snapshot: {
- fromRaw: sinon.stub().callsFake(snapshotData => ({
- getFile: pathname => ({
- getStringLength: sinon.stub().returns(100),
- getByteLength: sinon.stub().returns(100),
- getContent: sinon.stub().returns('foo\nbar\nbaz'),
- isEditable: sinon.stub().returns(true),
- getMetadata: sinon
- .stub()
- .returns(snapshotData?.files?.[pathname]?.metadata),
- getHash: sinon.stub().returns((ctx.hash = 'somehash')),
- }),
- getFilePathnames: sinon
- .stub()
- .returns(Object.keys(snapshotData.files || {})),
- getTimestamp: sinon
- .stub()
- .returns(
- snapshotData?.timestamp
- ? new Date(snapshotData.timestamp)
- : new Date()
- ),
- })),
- },
- getDocUpdaterCompatibleRanges: (ctx.getDocUpdaterCompatibleRanges = sinon
- .stub()
- .returns({
- changes: ctx.tracked_changes || [],
- comments: ctx.comments || [],
- })),
- }))
- ctx.RestoreManager = (await import(modulePath)).default
- ctx.user_id = 'mock-user-id'
- ctx.project_id = 'mock-project-id'
- ctx.version = 42
- // Add missing method mocks to RestoreManager
- ctx.RestoreManager.promises._getUpdatesFromHistory = sinon.stub().resolves([
- {
- toV: ctx.version,
- meta: { end_ts: new Date('2024-01-01T00:00:00.000Z') },
- },
- ])
- ctx.RestoreManager.promises._writeFileVersionToDisk = sinon
- .stub()
- .resolves('/tmp/mock-file-path')
- })
- afterEach(function () {
- tk.reset()
- })
- describe('restoreFileFromV2', function () {
- beforeEach(function (ctx) {
- ctx.RestoreManager.promises._writeFileVersionToDisk = sinon
- .stub()
- .resolves((ctx.fsPath = '/tmp/path/on/disk'))
- ctx.RestoreManager.promises._findOrCreateFolder = sinon
- .stub()
- .resolves((ctx.folder_id = 'mock-folder-id'))
- ctx.FileSystemImportManager.promises.addEntity = sinon
- .stub()
- .resolves((ctx.entity = 'mock-entity'))
- })
- describe('with a file not in a folder', function () {
- beforeEach(async function (ctx) {
- ctx.pathname = 'foo.tex'
- ctx.result = await ctx.RestoreManager.promises.restoreFileFromV2(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- })
- it('should write the file version to disk', function (ctx) {
- ctx.RestoreManager.promises._writeFileVersionToDisk
- .calledWith(ctx.project_id, ctx.version, ctx.pathname)
- .should.equal(true)
- })
- it('should find the root folder', function (ctx) {
- ctx.RestoreManager.promises._findOrCreateFolder
- .calledWith(ctx.project_id, '', ctx.user_id)
- .should.equal(true)
- })
- it('should add the entity', function (ctx) {
- ctx.FileSystemImportManager.promises.addEntity
- .calledWith(
- ctx.user_id,
- ctx.project_id,
- ctx.folder_id,
- 'foo.tex',
- ctx.fsPath,
- false
- )
- .should.equal(true)
- })
- it('should return the entity', function (ctx) {
- expect(ctx.result).to.equal(ctx.entity)
- })
- })
- describe('with a file in a folder', function () {
- beforeEach(async function (ctx) {
- ctx.pathname = 'foo/bar.tex'
- await ctx.RestoreManager.promises.restoreFileFromV2(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- })
- it('should find the folder', function (ctx) {
- ctx.RestoreManager.promises._findOrCreateFolder
- .calledWith(ctx.project_id, 'foo', ctx.user_id)
- .should.equal(true)
- })
- it('should add the entity by its basename', function (ctx) {
- ctx.FileSystemImportManager.promises.addEntity
- .calledWith(
- ctx.user_id,
- ctx.project_id,
- ctx.folder_id,
- 'bar.tex',
- ctx.fsPath,
- false
- )
- .should.equal(true)
- })
- })
- })
- describe('_findOrCreateFolder', function () {
- beforeEach(async function (ctx) {
- ctx.EditorController.promises.mkdirp = sinon.stub().resolves({
- newFolders: [],
- lastFolder: { _id: (ctx.folder_id = 'mock-folder-id') },
- })
- ctx.result = await ctx.RestoreManager.promises._findOrCreateFolder(
- ctx.project_id,
- 'folder/name',
- ctx.user_id
- )
- })
- it('should look up or create the folder', function (ctx) {
- ctx.EditorController.promises.mkdirp
- .calledWith(ctx.project_id, 'folder/name', ctx.user_id)
- .should.equal(true)
- })
- it('should return the folder_id', function (ctx) {
- expect(ctx.result).to.equal(ctx.folder_id)
- })
- })
- describe('_addEntityWithUniqueName', function () {
- beforeEach(function (ctx) {
- ctx.addEntityWithName = sinon.stub()
- ctx.filename = 'foo.tex'
- })
- describe('with a valid name', function () {
- beforeEach(async function (ctx) {
- ctx.addEntityWithName.resolves((ctx.entity = 'mock-entity'))
- ctx.result = await ctx.RestoreManager.promises._addEntityWithUniqueName(
- ctx.addEntityWithName,
- ctx.filename
- )
- })
- it('should add the entity', function (ctx) {
- ctx.addEntityWithName.calledWith(ctx.filename).should.equal(true)
- })
- it('should return the entity', function (ctx) {
- expect(ctx.result).to.equal(ctx.entity)
- })
- })
- describe('with a duplicate name', function () {
- beforeEach(async function (ctx) {
- ctx.addEntityWithName.rejects(new Errors.DuplicateNameError())
- ctx.addEntityWithName
- .onSecondCall()
- .resolves((ctx.entity = 'mock-entity'))
- ctx.result = await ctx.RestoreManager.promises._addEntityWithUniqueName(
- ctx.addEntityWithName,
- ctx.filename
- )
- })
- it('should try to add the entity with its original name', function (ctx) {
- ctx.addEntityWithName.calledWith('foo.tex').should.equal(true)
- })
- it('should try to add the entity with a unique name', function (ctx) {
- const date = moment(new Date()).format('Do MMM YY H:mm:ss')
- ctx.addEntityWithName
- .calledWith(`foo (Restored on ${date}).tex`)
- .should.equal(true)
- })
- it('should return the entity', function (ctx) {
- expect(ctx.result).to.equal(ctx.entity)
- })
- })
- })
- describe('revertFile', function () {
- beforeEach(function (ctx) {
- ctx.ProjectGetter.promises.getProject = sinon.stub()
- ctx.ProjectGetter.promises.getProject.withArgs(ctx.project_id).resolves({
- overleaf: { history: { rangesSupportEnabled: true } },
- rootDoc_id: 'root-doc-id',
- })
- ctx.RestoreManager.promises._findOrCreateFolder = sinon
- .stub()
- .resolves((ctx.folder_id = 'mock-folder-id'))
- ctx.FileSystemImportManager.promises.addEntity = sinon
- .stub()
- .resolves((ctx.entity = 'mock-entity'))
- })
- describe('reverting a project without ranges support', function () {
- beforeEach(function (ctx) {
- ctx.ProjectGetter.promises.getProject = sinon.stub().resolves({
- overleaf: { history: { rangesSupportEnabled: false } },
- })
- })
- it('should throw an error', async function (ctx) {
- await expect(
- ctx.RestoreManager.promises.revertFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- ).to.eventually.be.rejectedWith('project does not have ranges support')
- })
- })
- describe('reverting a document with ranges', function () {
- beforeEach(function (ctx) {
- ctx.pathname = 'foo.tex'
- ctx.comments = [
- {
- id: 'comment-in-other-doc',
- op: { t: 'comment-in-other-doc', p: 0, c: 'foo' },
- },
- {
- id: 'single-comment',
- op: { t: 'single-comment', p: 10, c: 'bar' },
- },
- {
- id: 'deleted-comment',
- op: { t: 'deleted-comment', p: 20, c: 'baz' },
- },
- ]
- ctx.remappedComments = [
- {
- id: 'duplicate-comment',
- op: { t: 'duplicate-comment', p: 0, c: 'foo' },
- },
- {
- id: 'single-comment',
- op: { t: 'single-comment', p: 10, c: 'bar' },
- },
- ]
- ctx.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
- ctx.DocstoreManager.promises.getCommentThreadIds = sinon
- .stub()
- .resolves({ 'other-doc': [ctx.comments[0].op.t] })
- ctx.ChatApiHandler.promises.duplicateCommentThreads = sinon
- .stub()
- .resolves({
- newThreads: {
- 'comment-in-other-doc': {
- duplicateId: 'duplicate-comment',
- },
- },
- })
- ctx.ChatApiHandler.promises.generateThreadData = sinon.stub().resolves(
- (ctx.threadData = {
- 'single-comment': {
- messages: [
- {
- content: 'message-content',
- timestamp: '2024-01-01T00:00:00.000Z',
- user_id: 'user-1',
- },
- ],
- },
- 'duplicate-comment': {
- messages: [
- {
- content: 'another message',
- timestamp: '2024-01-01T00:00:00.000Z',
- user_id: 'user-1',
- },
- ],
- },
- })
- )
- ctx.ChatManager.promises.injectUserInfoIntoThreads = sinon
- .stub()
- .resolves(ctx.threadData)
- ctx.EditorRealTimeController.emitToRoom = sinon.stub()
- ctx.tracked_changes = [
- {
- op: { pos: 4, i: 'bar' },
- metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
- },
- {
- op: { pos: 8, d: 'qux' },
- metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-2' },
- },
- ]
- ctx.getDocUpdaterCompatibleRanges.returns({
- changes: ctx.tracked_changes,
- comments: ctx.comments,
- })
- ctx.RestoreManager.promises._getUpdatesFromHistory = sinon
- .stub()
- .resolves([
- { toV: ctx.version, meta: { end_ts: (ctx.endTs = new Date()) } },
- ])
- ctx.EditorController.promises.addDocWithRanges = sinon
- .stub()
- .resolves((ctx.addedFile = { _id: 'mock-doc', type: 'doc' }))
- })
- describe("when reverting a file that doesn't current exist", function () {
- beforeEach(async function (ctx) {
- ctx.data = await ctx.RestoreManager.promises.revertFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- })
- it('should flush the document before fetching ranges', function (ctx) {
- expect(
- ctx.DocumentUpdaterHandler.promises.flushProjectToMongo
- ).to.have.been.calledBefore(
- ctx.DocstoreManager.promises.getCommentThreadIds
- )
- })
- it('should import the file', function (ctx) {
- expect(
- ctx.EditorController.promises.addDocWithRanges
- ).to.have.been.calledWith(
- ctx.project_id,
- ctx.folder_id,
- 'foo.tex',
- ['foo', 'bar', 'baz'],
- { changes: ctx.tracked_changes, comments: ctx.remappedComments }
- )
- })
- it('should return the created entity', function (ctx) {
- expect(ctx.data).to.deep.equal(ctx.addedFile)
- })
- })
- describe('with an existing file in the current project', function () {
- beforeEach(async function (ctx) {
- ctx.ProjectGetter.promises.getProject = sinon.stub()
- ctx.ProjectGetter.promises.getProject
- .withArgs(ctx.project_id)
- .resolves({
- overleaf: { history: { rangesSupportEnabled: true } },
- rootDoc_id: 'root-doc-id',
- })
- ctx.ProjectLocator.promises.findElementByPath = sinon
- .stub()
- .resolves({ type: 'file', element: { _id: 'mock-file-id' } })
- ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
- ctx.data = await ctx.RestoreManager.promises.revertFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- })
- it('should delete the existing file', async function (ctx) {
- expect(
- ctx.EditorController.promises.deleteEntity
- ).to.have.been.calledWith(
- ctx.project_id,
- 'mock-file-id',
- 'file',
- {
- kind: 'file-restore',
- path: ctx.pathname,
- version: ctx.version,
- timestamp: new Date(ctx.endTs).toISOString(),
- },
- ctx.user_id
- )
- })
- })
- describe('with an existing document in the current project', function () {
- beforeEach(async function (ctx) {
- ctx.ProjectGetter.promises.getProject = sinon.stub()
- ctx.ProjectGetter.promises.getProject
- .withArgs(ctx.project_id)
- .resolves({
- overleaf: { history: { rangesSupportEnabled: true } },
- rootDoc_id: 'root-doc-id',
- })
- ctx.ProjectLocator.promises.findElementByPath = sinon
- .stub()
- .resolves({ type: 'doc', element: { _id: 'mock-file-id' } })
- ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
- ctx.data = await ctx.RestoreManager.promises.revertFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- })
- it('should delete the existing document', async function (ctx) {
- expect(
- ctx.EditorController.promises.deleteEntity
- ).to.have.been.calledWith(
- ctx.project_id,
- 'mock-file-id',
- 'doc',
- {
- kind: 'file-restore',
- path: ctx.pathname,
- version: ctx.version,
- timestamp: new Date(ctx.endTs).toISOString(),
- },
- ctx.user_id
- )
- })
- it('should flush the document before fetching ranges', function (ctx) {
- expect(
- ctx.DocumentUpdaterHandler.promises.flushProjectToMongo
- ).to.have.been.calledBefore(
- ctx.DocstoreManager.promises.getCommentThreadIds
- )
- })
- it('should import the file', function (ctx) {
- expect(
- ctx.EditorController.promises.addDocWithRanges
- ).to.have.been.calledWith(
- ctx.project_id,
- ctx.folder_id,
- 'foo.tex',
- ['foo', 'bar', 'baz'],
- { changes: ctx.tracked_changes, comments: ctx.remappedComments },
- {
- kind: 'file-restore',
- path: ctx.pathname,
- version: ctx.version,
- timestamp: new Date(ctx.endTs).toISOString(),
- }
- )
- })
- it('should return the created entity', function (ctx) {
- expect(ctx.data).to.deep.equal(ctx.addedFile)
- })
- })
- describe('with comments in same doc', function () {
- // copy of the above, addition: inject and later inspect threadIds set
- beforeEach(async function (ctx) {
- ctx.ProjectGetter.promises.getProject = sinon.stub()
- ctx.ProjectGetter.promises.getProject
- .withArgs(ctx.project_id)
- .resolves({
- overleaf: { history: { rangesSupportEnabled: true } },
- rootDoc_id: 'root-doc-id',
- })
- ctx.ProjectLocator.promises.findElementByPath = sinon
- .stub()
- .resolves({ type: 'doc', element: { _id: 'mock-file-id' } })
- ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
- ctx.ChatApiHandler.promises.generateThreadData = sinon
- .stub()
- .resolves(
- (ctx.threadData = {
- [ctx.comments[0].op.t]: {
- messages: [
- {
- content: 'message',
- timestamp: '2024-01-01T00:00:00.000Z',
- user_id: 'user-1',
- },
- ],
- },
- [ctx.comments[1].op.t]: {
- messages: [
- {
- content: 'other message',
- timestamp: '2024-01-01T00:00:00.000Z',
- user_id: 'user-1',
- },
- ],
- },
- })
- )
- ctx.threadIds = new Map([
- [
- 'mock-file-id',
- new Set([ctx.comments[0].op.t, ctx.comments[1].op.t]),
- ],
- ])
- // Comments are updated in-place. Look up threads before reverting.
- ctx.afterThreadIds = {
- // mock-file-id removed
- [ctx.addedFile._id]: [ctx.comments[0].op.t, ctx.comments[1].op.t],
- }
- ctx.data = await ctx.RestoreManager.promises._revertSingleFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname,
- ctx.threadIds,
- {
- getFile: sinon.stub().returns({
- getMetadata: sinon.stub().returns(undefined),
- getContent: sinon.stub().returns('foo\nbar\nbaz'),
- isEditable: sinon.stub().returns(true),
- }),
- },
- {
- origin: {
- kind: 'file-restore',
- path: 'foo.tex',
- timestamp: new Date(ctx.endTs).toISOString(),
- version: 42,
- },
- }
- )
- })
- it('should import the file with original comments minus the deleted one', function (ctx) {
- expect(
- ctx.EditorController.promises.addDocWithRanges
- ).to.have.been.calledWith(
- ctx.project_id,
- ctx.folder_id,
- 'foo.tex',
- ['foo', 'bar', 'baz'],
- {
- changes: ctx.tracked_changes,
- comments: ctx.comments.slice(0, 2),
- },
- {
- kind: 'file-restore',
- path: ctx.pathname,
- version: ctx.version,
- timestamp: new Date(ctx.endTs).toISOString(),
- }
- )
- })
- it('should add the seen thread ids to the map', function (ctx) {
- expect(nestedMapWithSetToObject(ctx.threadIds)).to.deep.equal(
- ctx.afterThreadIds
- )
- })
- })
- describe('with remapped comments during revertProject', function () {
- // copy of the above, addition: inject and later inspect threadIds set
- beforeEach(async function (ctx) {
- ctx.ProjectGetter.promises.getProject = sinon.stub()
- ctx.ProjectGetter.promises.getProject
- .withArgs(ctx.project_id)
- .resolves({
- overleaf: { history: { rangesSupportEnabled: true } },
- rootDoc_id: 'root-doc-id',
- })
- ctx.ProjectLocator.promises.findElementByPath = sinon
- .stub()
- .resolves({ type: 'doc', element: { _id: 'mock-file-id' } })
- ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
- ctx.threadIds = new Map([
- ['other-doc', new Set([ctx.comments[0].op.t])],
- ])
- // Comments are updated in-place. Look up threads before reverting.
- ctx.afterThreadIds = {
- // mock-file-id removed
- 'other-doc': [ctx.comments[0].op.t],
- [ctx.addedFile._id]: [
- ctx.remappedComments[0].op.t,
- ctx.remappedComments[1].op.t,
- ],
- }
- ctx.data = await ctx.RestoreManager.promises._revertSingleFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname,
- ctx.threadIds,
- {
- getFile: sinon.stub().returns({
- getMetadata: sinon.stub().returns(undefined),
- getContent: sinon.stub().returns('foo\nbar\nbaz'),
- isEditable: sinon.stub().returns(true),
- }),
- },
- {
- origin: {
- kind: 'file-restore',
- path: 'foo.tex',
- timestamp: new Date(ctx.endTs).toISOString(),
- version: 42,
- },
- }
- )
- })
- it('should import the file', function (ctx) {
- expect(
- ctx.EditorController.promises.addDocWithRanges
- ).to.have.been.calledWith(
- ctx.project_id,
- ctx.folder_id,
- 'foo.tex',
- ['foo', 'bar', 'baz'],
- { changes: ctx.tracked_changes, comments: ctx.remappedComments },
- {
- kind: 'file-restore',
- path: ctx.pathname,
- version: ctx.version,
- timestamp: new Date(ctx.endTs).toISOString(),
- }
- )
- })
- it('should add the seen thread ids to the map', function (ctx) {
- expect(nestedMapWithSetToObject(ctx.threadIds)).to.deep.equal(
- ctx.afterThreadIds
- )
- })
- })
- describe('when restored file has the same id as root doc', function () {
- beforeEach(async function (ctx) {
- ctx.ProjectGetter.promises.getProject = sinon.stub()
- ctx.ProjectGetter.promises.getProject
- .withArgs(ctx.project_id)
- .resolves({
- overleaf: { history: { rangesSupportEnabled: true } },
- rootDoc_id: 'root-doc-id',
- })
- ctx.ProjectLocator.promises.findElementByPath = sinon
- .stub()
- .resolves({ type: 'doc', element: { _id: 'root-doc-id' } })
- ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
- ctx.EditorController.promises.addDocWithRanges = sinon
- .stub()
- .resolves((ctx.addedFile = { _id: 'new-doc-id', type: 'doc' }))
- ctx.EditorController.promises.setRootDoc = sinon.stub().resolves()
- ctx.data = await ctx.RestoreManager.promises.revertFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- })
- it('should delete the existing root document', async function (ctx) {
- expect(
- ctx.EditorController.promises.deleteEntity
- ).to.have.been.calledWith(
- ctx.project_id,
- 'root-doc-id',
- 'doc',
- {
- kind: 'file-restore',
- path: ctx.pathname,
- version: ctx.version,
- timestamp: new Date(ctx.endTs).toISOString(),
- },
- ctx.user_id
- )
- })
- it('should import the file', function (ctx) {
- expect(
- ctx.EditorController.promises.addDocWithRanges
- ).to.have.been.calledWith(
- ctx.project_id,
- ctx.folder_id,
- 'foo.tex',
- ['foo', 'bar', 'baz'],
- { changes: ctx.tracked_changes, comments: ctx.remappedComments },
- {
- kind: 'file-restore',
- path: ctx.pathname,
- version: ctx.version,
- timestamp: new Date(ctx.endTs).toISOString(),
- }
- )
- })
- it('should return the created entity with root doc id', function (ctx) {
- expect(ctx.data).to.deep.equal(ctx.addedFile)
- expect(ctx.data._id).to.equal('new-doc-id')
- })
- it('should set the restored document as the new root doc', function (ctx) {
- expect(
- ctx.EditorController.promises.setRootDoc
- ).to.have.been.calledWith(ctx.project_id, ctx.addedFile._id)
- })
- })
- })
- describe('reverting a file or document with metadata', function () {
- beforeEach(function (ctx) {
- ctx.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
- ctx.EditorController.promises.addDocWithRanges = sinon.stub()
- ctx.RestoreManager.promises._getUpdatesFromHistory = sinon
- .stub()
- .resolves([
- { toV: ctx.version, meta: { end_ts: (ctx.endTs = new Date()) } },
- ])
- ctx.EditorController.promises.upsertFile = sinon
- .stub()
- .resolves({ _id: 'mock-file-id', type: 'file' })
- ctx.EditorController.promises.addDocWithRanges = sinon
- .stub()
- .resolves((ctx.addedFile = { _id: 'mock-doc-id', type: 'doc' }))
- ctx.DocstoreManager.promises.getCommentThreadIds = sinon
- .stub()
- .resolves({})
- ctx.ChatApiHandler.promises.generateThreadData = sinon
- .stub()
- .resolves({})
- ctx.ChatManager.promises.injectUserInfoIntoThreads = sinon
- .stub()
- .resolves({})
- ctx.EditorRealTimeController.emitToRoom = sinon.stub()
- })
- describe('when reverting a linked file', function () {
- beforeEach(async function (ctx) {
- ctx.pathname = 'foo.png'
- ctx.result = await ctx.RestoreManager.promises.revertFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- })
- it('should revert it as a file', function (ctx) {
- expect(ctx.result).to.deep.equal({
- _id: 'mock-file-id',
- type: 'file',
- })
- })
- it('should upload to the project as a file', function (ctx) {
- expect(
- ctx.EditorController.promises.upsertFile
- ).to.have.been.calledWith(
- ctx.project_id,
- 'mock-folder-id',
- 'foo.png',
- ctx.fsPath,
- { provider: 'bar' },
- {
- kind: 'file-restore',
- path: ctx.pathname,
- version: ctx.version,
- timestamp: new Date(ctx.endTs).toISOString(),
- },
- ctx.user_id
- )
- })
- it('should not try to add a document', function (ctx) {
- expect(ctx.EditorController.promises.addDocWithRanges).to.not.have
- .been.called
- })
- })
- describe('when reverting a linked document with provider', function () {
- beforeEach(async function (ctx) {
- ctx.pathname = 'linkedFile.bib'
- ctx.result = await ctx.RestoreManager.promises.revertFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- })
- it('should revert it as a file', function (ctx) {
- expect(ctx.result).to.deep.equal({
- _id: 'mock-file-id',
- type: 'file',
- })
- })
- it('should upload to the project as a file', function (ctx) {
- expect(
- ctx.EditorController.promises.upsertFile
- ).to.have.been.calledWith(
- ctx.project_id,
- 'mock-folder-id',
- 'linkedFile.bib',
- ctx.fsPath,
- { provider: 'mendeley' },
- {
- kind: 'file-restore',
- path: ctx.pathname,
- version: ctx.version,
- timestamp: new Date(ctx.endTs).toISOString(),
- },
- ctx.user_id
- )
- })
- it('should not try to add a document', function (ctx) {
- expect(ctx.EditorController.promises.addDocWithRanges).to.not.have
- .been.called
- })
- })
- describe('when reverting a linked document with { main: true }', function () {
- beforeEach(async function (ctx) {
- ctx.pathname = 'withMainTrue.tex'
- ctx.result = await ctx.RestoreManager.promises.revertFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- })
- it('should revert it as a document', function (ctx) {
- expect(ctx.result).to.deep.equal({
- _id: 'mock-doc-id',
- type: 'doc',
- })
- })
- it('should not upload to the project as a file', function (ctx) {
- expect(ctx.EditorController.promises.upsertFile).to.not.have.been
- .called
- })
- it('should add the document', function (ctx) {
- expect(
- ctx.EditorController.promises.addDocWithRanges
- ).to.have.been.calledWith(
- ctx.project_id,
- ctx.folder_id,
- 'withMainTrue.tex',
- ['foo', 'bar', 'baz'],
- { changes: [], comments: [] }
- )
- })
- })
- })
- describe('when reverting a binary file', function () {
- beforeEach(async function (ctx) {
- ctx.pathname = 'foo.png'
- ctx.EditorController.promises.upsertFile = sinon
- .stub()
- .resolves({ _id: 'mock-file-id', type: 'file' })
- ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
- ctx.RestoreManager.promises._getUpdatesFromHistory = sinon
- .stub()
- .resolves([{ toV: ctx.version, meta: { end_ts: Date.now() } }])
- })
- it('should return the created entity if file exists', async function (ctx) {
- ctx.ProjectLocator.promises.findElementByPath = sinon
- .stub()
- .resolves({ type: 'file', element: { _id: 'existing-file-id' } })
- const revertRes = await ctx.RestoreManager.promises.revertFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- expect(revertRes).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
- })
- it('should return the created entity if file does not exists', async function (ctx) {
- ctx.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
- const revertRes = await ctx.RestoreManager.promises.revertFile(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- ctx.pathname
- )
- expect(revertRes).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
- })
- })
- })
- describe('revertProject', function () {
- beforeEach(function (ctx) {
- ctx.ProjectGetter.promises.getProject = sinon.stub()
- ctx.ProjectGetter.promises.getProject
- .withArgs(ctx.project_id)
- .resolves({ overleaf: { history: { rangesSupportEnabled: true } } })
- ctx.RestoreManager.promises._revertSingleFile = sinon.stub().resolves({
- _id: 'mock-doc-id',
- type: 'doc',
- })
- ctx.RestoreManager.promises._getProjectPathsAtVersion = sinon
- .stub()
- .resolves([])
- ctx.ProjectEntityHandler.promises.getAllEntities = sinon
- .stub()
- .resolves({ docs: [], files: [] })
- ctx.EditorController.promises.deleteEntityWithPath = sinon
- .stub()
- .resolves()
- ctx.RestoreManager.promises._getUpdatesFromHistory = sinon
- .stub()
- .resolves([
- { toV: ctx.version, meta: { end_ts: (ctx.end_ts = Date.now()) } },
- ])
- })
- describe('reverting a project without ranges support', function () {
- beforeEach(function (ctx) {
- ctx.ProjectGetter.promises.getProject = sinon.stub().resolves({
- overleaf: { history: { rangesSupportEnabled: false } },
- })
- })
- it('should throw an error', async function (ctx) {
- await expect(
- ctx.RestoreManager.promises.revertProject(
- ctx.user_id,
- ctx.project_id,
- ctx.version
- )
- ).to.eventually.be.rejectedWith('project does not have ranges support')
- })
- })
- describe('for a project with overlap in current files and old files', function () {
- beforeEach(async function (ctx) {
- ctx.HistoryManager.promises.getContentAtVersion = sinon
- .stub()
- .resolves({
- files: {
- 'main.tex': {
- hash: 'abcdef1234567890abcdef1234567890abcdef12',
- stringLength: 100,
- metadata: {
- editorId: 'test-editor',
- },
- },
- 'figures/image.png': {
- hash: 'abcdef1234567890abcdef1234567890abcdef12',
- stringLength: 100,
- metadata: {
- provider: 'bar',
- },
- },
- 'since-deleted.tex': {
- hash: 'abcdef1234567890abcdef1234567890abcdef12',
- stringLength: 100,
- metadata: {
- editorId: 'test-editor',
- },
- },
- },
- timestamp: new Date().toISOString(),
- })
- ctx.ProjectEntityHandler.promises.getAllEntities = sinon
- .stub()
- .resolves({
- docs: [{ path: '/main.tex' }, { path: '/new-file.tex' }],
- files: [{ path: '/figures/image.png' }],
- })
- await ctx.RestoreManager.promises.revertProject(
- ctx.user_id,
- ctx.project_id,
- ctx.version
- )
- ctx.origin = {
- kind: 'project-restore',
- version: ctx.version,
- timestamp: new Date(ctx.end_ts).toISOString(),
- }
- })
- it('should delete the old files', function (ctx) {
- expect(
- ctx.EditorController.promises.deleteEntityWithPath
- ).to.have.been.calledWith(
- ctx.project_id,
- 'new-file.tex',
- ctx.origin,
- ctx.user_id
- )
- })
- it('should not delete the current files', function (ctx) {
- expect(
- ctx.EditorController.promises.deleteEntityWithPath
- ).to.not.have.been.calledWith(
- ctx.project_id,
- 'main.tex',
- ctx.origin,
- ctx.user_id
- )
- expect(
- ctx.EditorController.promises.deleteEntityWithPath
- ).to.not.have.been.calledWith(
- ctx.project_id,
- 'figures/image.png',
- ctx.origin,
- ctx.user_id
- )
- })
- it('should revert the old files', function (ctx) {
- expect(
- ctx.RestoreManager.promises._revertSingleFile
- ).to.have.been.calledWith(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- 'main.tex'
- )
- expect(
- ctx.RestoreManager.promises._revertSingleFile
- ).to.have.been.calledWith(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- 'figures/image.png'
- )
- expect(
- ctx.RestoreManager.promises._revertSingleFile
- ).to.have.been.calledWith(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- 'since-deleted.tex'
- )
- })
- it('should not revert the current files', function (ctx) {
- expect(
- ctx.RestoreManager.promises._revertSingleFile
- ).to.not.have.been.calledWith(
- ctx.user_id,
- ctx.project_id,
- ctx.version,
- 'new-file.tex'
- )
- })
- })
- })
- })
|