| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- const sinon = require('sinon')
- const { setTimeout } = require('node:timers/promises')
- const Settings = require('@overleaf/settings')
- const rclientProjectHistory = require('@overleaf/redis-wrapper').createClient(
- Settings.redis.project_history
- )
- const ProjectHistoryKeys = Settings.redis.project_history.key_schema
- const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
- const MockWebApi = require('./helpers/MockWebApi')
- const DocUpdaterClient = require('./helpers/DocUpdaterClient')
- const DocUpdaterApp = require('./helpers/DocUpdaterApp')
- async function sendProjectUpdateAndWait(projectId, docId, update, version) {
- await DocUpdaterClient.sendProjectUpdate(projectId, docId, update, version)
- // It seems that we need to wait for a little while
- await setTimeout(200)
- }
- describe("Applying updates to a project's structure", function () {
- before(async function () {
- this.user_id = 'user-id-123'
- this.version = 1234
- await DocUpdaterApp.ensureRunning()
- })
- describe('renaming a file', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- this.fileUpdate = {
- type: 'rename-file',
- id: DocUpdaterClient.randomId(),
- pathname: '/file-path',
- newPathname: '/new-file-path',
- }
- this.updates = [this.fileUpdate]
- await sendProjectUpdateAndWait(
- this.project_id,
- this.user_id,
- this.updates,
- this.version
- )
- })
- it('should push the applied file renames to the project history api', function (done) {
- rclientProjectHistory.lrange(
- ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
- 0,
- -1,
- (error, updates) => {
- if (error) {
- return done(error)
- }
- const update = JSON.parse(updates[0])
- update.file.should.equal(this.fileUpdate.id)
- update.pathname.should.equal('/file-path')
- update.new_pathname.should.equal('/new-file-path')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.0`)
- done()
- }
- )
- })
- })
- describe('deleting a file', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- this.fileUpdate = {
- type: 'rename-file',
- id: DocUpdaterClient.randomId(),
- pathname: '/file-path',
- newPathname: '',
- }
- this.updates = [this.fileUpdate]
- await sendProjectUpdateAndWait(
- this.project_id,
- this.user_id,
- this.updates,
- this.version
- )
- })
- it('should push the applied file renames to the project history api', function (done) {
- rclientProjectHistory.lrange(
- ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
- 0,
- -1,
- (error, updates) => {
- if (error) {
- return done(error)
- }
- const update = JSON.parse(updates[0])
- update.file.should.equal(this.fileUpdate.id)
- update.pathname.should.equal('/file-path')
- update.new_pathname.should.equal('')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.0`)
- done()
- }
- )
- })
- })
- describe('renaming a document', function () {
- before(function () {
- this.update = {
- type: 'rename-doc',
- id: DocUpdaterClient.randomId(),
- pathname: '/doc-path',
- newPathname: '/new-doc-path',
- }
- this.updates = [this.update]
- })
- describe('when the document is not loaded', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- await sendProjectUpdateAndWait(
- this.project_id,
- this.user_id,
- this.updates,
- this.version
- )
- })
- it('should push the applied doc renames to the project history api', function (done) {
- rclientProjectHistory.lrange(
- ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
- 0,
- -1,
- (error, updates) => {
- if (error) {
- return done(error)
- }
- const update = JSON.parse(updates[0])
- update.doc.should.equal(this.update.id)
- update.pathname.should.equal('/doc-path')
- update.new_pathname.should.equal('/new-doc-path')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.0`)
- done()
- }
- )
- })
- })
- describe('when the document is loaded', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- MockWebApi.insertDoc(this.project_id, this.update.id, {})
- await DocUpdaterClient.preloadDoc(this.project_id, this.update.id)
- sinon.spy(MockWebApi, 'getDocument')
- await sendProjectUpdateAndWait(
- this.project_id,
- this.user_id,
- this.updates,
- this.version
- )
- })
- after(function () {
- MockWebApi.getDocument.restore()
- })
- it('should update the doc', async function () {
- const doc = await DocUpdaterClient.getDoc(
- this.project_id,
- this.update.id
- )
- doc.pathname.should.equal(this.update.newPathname)
- })
- it('should push the applied doc renames to the project history api', function (done) {
- rclientProjectHistory.lrange(
- ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
- 0,
- -1,
- (error, updates) => {
- if (error) {
- return done(error)
- }
- const update = JSON.parse(updates[0])
- update.doc.should.equal(this.update.id)
- update.pathname.should.equal('/doc-path')
- update.new_pathname.should.equal('/new-doc-path')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.0`)
- done()
- }
- )
- })
- })
- })
- describe('renaming multiple documents and files', function () {
- before(function () {
- this.docUpdate0 = {
- type: 'rename-doc',
- id: DocUpdaterClient.randomId(),
- pathname: '/doc-path0',
- newPathname: '/new-doc-path0',
- }
- this.docUpdate1 = {
- type: 'rename-doc',
- id: DocUpdaterClient.randomId(),
- pathname: '/doc-path1',
- newPathname: '/new-doc-path1',
- }
- this.fileUpdate0 = {
- type: 'rename-file',
- id: DocUpdaterClient.randomId(),
- pathname: '/file-path0',
- newPathname: '/new-file-path0',
- }
- this.fileUpdate1 = {
- type: 'rename-file',
- id: DocUpdaterClient.randomId(),
- pathname: '/file-path1',
- newPathname: '/new-file-path1',
- }
- this.updates = [
- this.docUpdate0,
- this.docUpdate1,
- this.fileUpdate0,
- this.fileUpdate1,
- ]
- })
- describe('when the documents are not loaded', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- await sendProjectUpdateAndWait(
- this.project_id,
- this.user_id,
- this.updates,
- this.version
- )
- })
- it('should push the applied doc renames to the project history api', function (done) {
- rclientProjectHistory.lrange(
- ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
- 0,
- -1,
- (error, updates) => {
- if (error) {
- return done(error)
- }
- let update = JSON.parse(updates[0])
- update.doc.should.equal(this.docUpdate0.id)
- update.pathname.should.equal('/doc-path0')
- update.new_pathname.should.equal('/new-doc-path0')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.0`)
- update = JSON.parse(updates[1])
- update.doc.should.equal(this.docUpdate1.id)
- update.pathname.should.equal('/doc-path1')
- update.new_pathname.should.equal('/new-doc-path1')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.1`)
- update = JSON.parse(updates[2])
- update.file.should.equal(this.fileUpdate0.id)
- update.pathname.should.equal('/file-path0')
- update.new_pathname.should.equal('/new-file-path0')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.2`)
- update = JSON.parse(updates[3])
- update.file.should.equal(this.fileUpdate1.id)
- update.pathname.should.equal('/file-path1')
- update.new_pathname.should.equal('/new-file-path1')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.3`)
- done()
- }
- )
- })
- })
- })
- describe('deleting a document', function () {
- before(function () {
- this.update = {
- type: 'rename-doc',
- id: DocUpdaterClient.randomId(),
- pathname: '/doc-path',
- newPathname: '',
- }
- this.updates = [this.update]
- })
- describe('when the document is not loaded', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- await sendProjectUpdateAndWait(
- this.project_id,
- this.user_id,
- this.updates,
- this.version
- )
- })
- it('should push the applied doc update to the project history api', function (done) {
- rclientProjectHistory.lrange(
- ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
- 0,
- -1,
- (error, updates) => {
- if (error) {
- return done(error)
- }
- const update = JSON.parse(updates[0])
- update.doc.should.equal(this.update.id)
- update.pathname.should.equal('/doc-path')
- update.new_pathname.should.equal('')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.0`)
- done()
- }
- )
- })
- })
- describe('when the document is loaded', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- MockWebApi.insertDoc(this.project_id, this.update.id, {})
- await DocUpdaterClient.preloadDoc(this.project_id, this.update.id)
- sinon.spy(MockWebApi, 'getDocument')
- await sendProjectUpdateAndWait(
- this.project_id,
- this.user_id,
- this.updates,
- this.version
- )
- })
- after(function () {
- MockWebApi.getDocument.restore()
- })
- it('should not modify the doc', async function () {
- const doc = await DocUpdaterClient.getDoc(
- this.project_id,
- this.update.id
- )
- doc.pathname.should.equal('/a/b/c.tex') // default pathname from MockWebApi
- })
- it('should push the applied doc update to the project history api', function (done) {
- rclientProjectHistory.lrange(
- ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
- 0,
- -1,
- (error, updates) => {
- if (error) {
- return done(error)
- }
- const update = JSON.parse(updates[0])
- update.doc.should.equal(this.update.id)
- update.pathname.should.equal('/doc-path')
- update.new_pathname.should.equal('')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.0`)
- done()
- }
- )
- })
- })
- })
- describe('adding a file', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- this.fileUpdate = {
- type: 'add-file',
- id: DocUpdaterClient.randomId(),
- pathname: '/file-path',
- url: 'filestore.example.com',
- }
- this.updates = [this.fileUpdate]
- await sendProjectUpdateAndWait(
- this.project_id,
- this.user_id,
- this.updates,
- this.version
- )
- })
- it('should push the file addition to the project history api', function (done) {
- rclientProjectHistory.lrange(
- ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
- 0,
- -1,
- (error, updates) => {
- if (error) {
- return done(error)
- }
- const update = JSON.parse(updates[0])
- update.file.should.equal(this.fileUpdate.id)
- update.pathname.should.equal('/file-path')
- update.url.should.equal('filestore.example.com')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.0`)
- done()
- }
- )
- })
- })
- describe('adding a doc', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- this.docUpdate = {
- type: 'add-doc',
- id: DocUpdaterClient.randomId(),
- pathname: '/file-path',
- docLines: 'a\nb',
- }
- this.updates = [this.docUpdate]
- await sendProjectUpdateAndWait(
- this.project_id,
- this.user_id,
- this.updates,
- this.version
- )
- })
- it('should push the doc addition to the project history api', function (done) {
- rclientProjectHistory.lrange(
- ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
- 0,
- -1,
- (error, updates) => {
- if (error) {
- return done(error)
- }
- const update = JSON.parse(updates[0])
- update.doc.should.equal(this.docUpdate.id)
- update.pathname.should.equal('/file-path')
- update.docLines.should.equal('a\nb')
- update.meta.user_id.should.equal(this.user_id)
- update.meta.ts.should.be.a('string')
- update.version.should.equal(`${this.version}.0`)
- done()
- }
- )
- })
- })
- describe('with enough updates to flush to the history service', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- this.user_id = DocUpdaterClient.randomId()
- this.version0 = 12345
- this.version1 = this.version0 + 1
- const updates = []
- for (let v = 0; v <= 599; v++) {
- // Should flush after 500 ops
- updates.push({
- type: 'add-doc',
- id: DocUpdaterClient.randomId(),
- pathname: '/file-' + v,
- docLines: 'a\nb',
- })
- }
- sinon.spy(MockProjectHistoryApi, 'flushProject')
- // Send updates in chunks to causes multiple flushes
- const projectId = this.project_id
- const userId = this.project_id
- await DocUpdaterClient.sendProjectUpdate(
- projectId,
- userId,
- updates.slice(0, 250),
- this.version0
- )
- await DocUpdaterClient.sendProjectUpdate(
- projectId,
- userId,
- updates.slice(250),
- this.version1
- )
- await setTimeout(200)
- })
- after(function () {
- MockProjectHistoryApi.flushProject.restore()
- })
- it('should flush project history', function () {
- MockProjectHistoryApi.flushProject
- .calledWith(this.project_id)
- .should.equal(true)
- })
- })
- describe('with too few updates to flush to the history service', function () {
- before(async function () {
- this.project_id = DocUpdaterClient.randomId()
- this.user_id = DocUpdaterClient.randomId()
- this.version0 = 12345
- this.version1 = this.version0 + 1
- const updates = []
- for (let v = 0; v <= 42; v++) {
- // Should flush after 500 ops
- updates.push({
- type: 'add-doc',
- id: DocUpdaterClient.randomId(),
- pathname: '/file-' + v,
- docLines: 'a\nb',
- })
- }
- sinon.spy(MockProjectHistoryApi, 'flushProject')
- // Send updates in chunks
- const projectId = this.project_id
- const userId = this.project_id
- await DocUpdaterClient.sendProjectUpdate(
- projectId,
- userId,
- updates.slice(0, 10),
- this.version0
- )
- await DocUpdaterClient.sendProjectUpdate(
- projectId,
- userId,
- updates.slice(10),
- this.version1
- )
- await setTimeout(200)
- })
- after(function () {
- MockProjectHistoryApi.flushProject.restore()
- })
- it('should not flush project history', function () {
- MockProjectHistoryApi.flushProject
- .calledWith(this.project_id)
- .should.equal(false)
- })
- })
- })
|