| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678 |
- const sinon = require('sinon')
- const { expect } = require('chai')
- const modulePath = '../../../../app/js/ProjectHistoryRedisManager.js'
- const SandboxedModule = require('sandboxed-module')
- const tk = require('timekeeper')
- describe('ProjectHistoryRedisManager', function () {
- beforeEach(function () {
- this.project_id = 'project-id-123'
- this.projectHistoryId = 'history-id-123'
- this.user_id = 'user-id-123'
- this.rclient = {}
- this.source = 'editor'
- tk.freeze(new Date())
- this.Limits = {
- docIsTooLarge: sinon.stub().returns(false),
- stringFileDataContentIsTooLarge: sinon.stub().returns(false),
- }
- this.ProjectHistoryRedisManager = SandboxedModule.require(modulePath, {
- requires: {
- '@overleaf/settings': (this.settings = {
- max_doc_length: 123,
- redis: {
- project_history: {
- key_schema: {
- projectHistoryOps({ project_id: projectId }) {
- return `ProjectHistory:Ops:${projectId}`
- },
- projectHistoryFirstOpTimestamp({ project_id: projectId }) {
- return `ProjectHistory:FirstOpTimestamp:${projectId}`
- },
- },
- },
- },
- }),
- '@overleaf/redis-wrapper': {
- createClient: () => this.rclient,
- },
- './Metrics': (this.metrics = { summary: sinon.stub() }),
- './Limits': this.Limits,
- },
- })
- })
- afterEach(function () {
- tk.reset()
- })
- describe('queueOps', function () {
- beforeEach(async function () {
- this.ops = ['mock-op-1', 'mock-op-2']
- this.multi = { exec: sinon.stub().resolves([1]) }
- this.multi.rpush = sinon.stub()
- this.multi.setnx = sinon.stub()
- this.rclient.multi = () => this.multi
- await this.ProjectHistoryRedisManager.promises.queueOps(
- this.project_id,
- ...this.ops
- )
- })
- it('should queue an update', function () {
- this.multi.rpush.should.have.been.calledWithExactly(
- `ProjectHistory:Ops:${this.project_id}`,
- this.ops[0],
- this.ops[1]
- )
- })
- it('should set the queue timestamp if not present', function () {
- this.multi.setnx.should.have.been.calledWithExactly(
- `ProjectHistory:FirstOpTimestamp:${this.project_id}`,
- Date.now()
- )
- })
- })
- describe('queueRenameEntity', function () {
- beforeEach(async function () {
- this.file_id = 1234
- this.rawUpdate = {
- pathname: (this.pathname = '/old'),
- newPathname: (this.newPathname = '/new'),
- version: (this.version = 2),
- }
- this.ProjectHistoryRedisManager.promises.queueOps = sinon
- .stub()
- .resolves()
- await this.ProjectHistoryRedisManager.promises.queueRenameEntity(
- this.project_id,
- this.projectHistoryId,
- 'file',
- this.file_id,
- this.user_id,
- this.rawUpdate,
- this.source
- )
- })
- it('should queue an update', function () {
- const update = {
- pathname: this.pathname,
- new_pathname: this.newPathname,
- meta: {
- user_id: this.user_id,
- ts: new Date(),
- source: this.source,
- },
- version: this.version,
- projectHistoryId: this.projectHistoryId,
- file: this.file_id,
- }
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(update)
- )
- })
- })
- describe('queueAddEntity', function () {
- beforeEach(function () {
- this.doc_id = 1234
- this.rawUpdate = {
- pathname: (this.pathname = '/old'),
- docLines: (this.docLines = 'a\nb'),
- version: (this.version = 2),
- }
- this.ProjectHistoryRedisManager.promises.queueOps = sinon
- .stub()
- .resolves()
- })
- it('should queue an update', async function () {
- this.rawUpdate.url = this.url = 'filestore.example.com'
- await this.ProjectHistoryRedisManager.promises.queueAddEntity(
- this.project_id,
- this.projectHistoryId,
- 'doc',
- this.doc_id,
- this.user_id,
- this.rawUpdate,
- this.source
- )
- const update = {
- pathname: this.pathname,
- docLines: this.docLines,
- url: this.url,
- meta: {
- user_id: this.user_id,
- ts: new Date(),
- source: this.source,
- },
- version: this.version,
- projectHistoryId: this.projectHistoryId,
- createdBlob: false,
- doc: this.doc_id,
- }
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(update)
- )
- })
- it('should queue an update with file metadata', async function () {
- const metadata = {
- importedAt: '2024-07-30T09:14:45.928Z',
- provider: 'references-provider',
- }
- const projectId = 'project-id'
- const fileId = 'file-id'
- const url = `http://filestore/project/${projectId}/file/${fileId}`
- await this.ProjectHistoryRedisManager.promises.queueAddEntity(
- projectId,
- this.projectHistoryId,
- 'file',
- fileId,
- this.user_id,
- {
- pathname: 'foo.png',
- url,
- version: 42,
- hash: '1337',
- metadata,
- },
- this.source
- )
- const update = {
- pathname: 'foo.png',
- docLines: undefined,
- url,
- meta: {
- user_id: this.user_id,
- ts: new Date(),
- source: this.source,
- },
- version: 42,
- hash: '1337',
- metadata,
- projectHistoryId: this.projectHistoryId,
- createdBlob: false,
- file: fileId,
- }
- expect(
- this.ProjectHistoryRedisManager.promises.queueOps.args[0][1]
- ).to.equal(JSON.stringify(update))
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- projectId,
- JSON.stringify(update)
- )
- })
- it('should forward history compatible ranges if history ranges support is enabled', async function () {
- this.rawUpdate.historyRangesSupport = true
- this.docLines = 'the quick fox jumps over the lazy dog'
- const ranges = {
- changes: [
- {
- op: { p: 4, i: 'quick' },
- metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
- },
- {
- op: { p: 9, d: ' brown' },
- metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
- },
- {
- op: { p: 14, i: 'jumps' },
- metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
- },
- ],
- comments: [
- {
- op: { p: 29, c: 'lazy', t: 'comment-1' },
- metadata: { resolved: false },
- },
- ],
- }
- this.rawUpdate.ranges = ranges
- this.rawUpdate.docLines = this.docLines
- await this.ProjectHistoryRedisManager.promises.queueAddEntity(
- this.project_id,
- this.projectHistoryId,
- 'doc',
- this.doc_id,
- this.user_id,
- this.rawUpdate,
- this.source
- )
- const historyCompatibleRanges = {
- comments: [
- {
- op: { p: 29, c: 'lazy', t: 'comment-1', hpos: 35 },
- metadata: { resolved: false },
- },
- ],
- changes: [
- {
- op: { p: 4, i: 'quick' },
- metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
- },
- {
- op: { p: 9, d: ' brown' },
- metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
- },
- {
- op: { p: 14, i: 'jumps', hpos: 20 },
- metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
- },
- ],
- }
- const update = {
- pathname: this.pathname,
- docLines: 'the quick brown fox jumps over the lazy dog',
- meta: {
- user_id: this.user_id,
- ts: new Date(),
- source: this.source,
- },
- version: this.version,
- projectHistoryId: this.projectHistoryId,
- createdBlob: false,
- ranges: historyCompatibleRanges,
- doc: this.doc_id,
- }
- expect(
- this.ProjectHistoryRedisManager.promises.queueOps
- ).to.have.been.calledWithExactly(this.project_id, JSON.stringify(update))
- })
- it('should not forward ranges if history ranges support is disabled', async function () {
- this.rawUpdate.historyRangesSupport = false
- const 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 },
- },
- ],
- }
- this.rawUpdate.ranges = ranges
- await this.ProjectHistoryRedisManager.promises.queueAddEntity(
- this.project_id,
- this.projectHistoryId,
- 'doc',
- this.doc_id,
- this.user_id,
- this.rawUpdate,
- this.source
- )
- const update = {
- pathname: this.pathname,
- docLines: this.docLines,
- meta: {
- user_id: this.user_id,
- ts: new Date(),
- source: this.source,
- },
- version: this.version,
- projectHistoryId: this.projectHistoryId,
- createdBlob: false,
- doc: this.doc_id,
- }
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(update)
- )
- })
- it('should not forward ranges if history ranges support is undefined', async function () {
- this.rawUpdate.historyRangesSupport = false
- const 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 },
- },
- ],
- }
- this.rawUpdate.ranges = ranges
- await this.ProjectHistoryRedisManager.promises.queueAddEntity(
- this.project_id,
- this.projectHistoryId,
- 'doc',
- this.doc_id,
- this.user_id,
- this.rawUpdate,
- this.source
- )
- const update = {
- pathname: this.pathname,
- docLines: this.docLines,
- meta: {
- user_id: this.user_id,
- ts: new Date(),
- source: this.source,
- },
- version: this.version,
- projectHistoryId: this.projectHistoryId,
- createdBlob: false,
- doc: this.doc_id,
- }
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(update)
- )
- })
- it('should pass "false" as the createdBlob field if not provided', async function () {
- await this.ProjectHistoryRedisManager.promises.queueAddEntity(
- this.project_id,
- this.projectHistoryId,
- 'doc',
- this.doc_id,
- this.user_id,
- this.rawUpdate,
- this.source
- )
- const update = {
- pathname: this.pathname,
- docLines: this.docLines,
- meta: {
- user_id: this.user_id,
- ts: new Date(),
- source: this.source,
- },
- version: this.version,
- projectHistoryId: this.projectHistoryId,
- createdBlob: false,
- doc: this.doc_id,
- }
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(update)
- )
- })
- it('should pass through the value of the createdBlob field', async function () {
- this.rawUpdate.createdBlob = true
- await this.ProjectHistoryRedisManager.promises.queueAddEntity(
- this.project_id,
- this.projectHistoryId,
- 'doc',
- this.doc_id,
- this.user_id,
- this.rawUpdate,
- this.source
- )
- const update = {
- pathname: this.pathname,
- docLines: this.docLines,
- meta: {
- user_id: this.user_id,
- ts: new Date(),
- source: this.source,
- },
- version: this.version,
- projectHistoryId: this.projectHistoryId,
- createdBlob: true,
- doc: this.doc_id,
- }
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(update)
- )
- })
- })
- describe('queueResyncProjectStructure', function () {
- it('should queue an update', function () {})
- })
- describe('queueResyncDocContent', function () {
- beforeEach(function () {
- this.doc_id = 1234
- this.lines = ['one', 'two']
- this.ranges = {
- changes: [{ op: { i: 'ne', p: 1 } }, { op: { d: 'deleted', p: 3 } }],
- }
- this.resolvedCommentIds = ['comment-1']
- this.version = 2
- this.pathname = '/path'
- this.ProjectHistoryRedisManager.promises.queueOps = sinon
- .stub()
- .resolves()
- })
- describe('with a good doc', function () {
- beforeEach(async function () {
- this.update = {
- resyncDocContent: {
- version: this.version,
- content: 'one\ntwo',
- },
- projectHistoryId: this.projectHistoryId,
- path: this.pathname,
- doc: this.doc_id,
- meta: { ts: new Date() },
- }
- await this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
- this.project_id,
- this.projectHistoryId,
- this.doc_id,
- this.lines,
- this.ranges,
- this.resolvedCommentIds,
- this.version,
- this.pathname,
- false
- )
- })
- it('should check if the doc is too large', function () {
- this.Limits.docIsTooLarge.should.have.been.calledWith(
- JSON.stringify(this.update).length,
- this.lines,
- this.settings.max_doc_length
- )
- })
- it('should queue an update', function () {
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(this.update)
- )
- })
- })
- describe('with a doc that is too large', function () {
- beforeEach(async function () {
- this.Limits.docIsTooLarge.returns(true)
- await expect(
- this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
- this.project_id,
- this.projectHistoryId,
- this.doc_id,
- this.lines,
- this.ranges,
- this.resolvedCommentIds,
- this.version,
- this.pathname,
- false
- )
- ).to.be.rejected
- })
- it('should not queue an update if the doc is too large', function () {
- this.ProjectHistoryRedisManager.promises.queueOps.should.not.have.been
- .called
- })
- })
- describe('when history ranges support is enabled', function () {
- beforeEach(async function () {
- this.update = {
- resyncDocContent: {
- version: this.version,
- ranges: this.ranges,
- resolvedCommentIds: this.resolvedCommentIds,
- content: 'onedeleted\ntwo',
- },
- projectHistoryId: this.projectHistoryId,
- path: this.pathname,
- doc: this.doc_id,
- meta: { ts: new Date() },
- }
- await this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
- this.project_id,
- this.projectHistoryId,
- this.doc_id,
- this.lines,
- this.ranges,
- this.resolvedCommentIds,
- this.version,
- this.pathname,
- true
- )
- })
- it('should include tracked deletes in the update', function () {
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(this.update)
- )
- })
- it('should check the doc length without tracked deletes', function () {
- this.Limits.docIsTooLarge.should.have.been.calledWith(
- JSON.stringify(this.update).length,
- this.lines,
- this.settings.max_doc_length
- )
- })
- it('should queue an update', function () {
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(this.update)
- )
- })
- })
- describe('history-ot', function () {
- beforeEach(async function () {
- this.lines = {
- content: 'onedeleted\ntwo',
- comments: [{ id: 'id1', ranges: [{ pos: 0, length: 3 }] }],
- trackedChanges: [
- {
- range: { pos: 3, length: 7 },
- tracking: {
- type: 'delete',
- userId: 'user-id',
- ts: '2025-06-16T14:31:44.910Z',
- },
- },
- ],
- }
- this.update = {
- resyncDocContent: {
- version: this.version,
- historyOTRanges: {
- comments: this.lines.comments,
- trackedChanges: this.lines.trackedChanges,
- },
- content: this.lines.content,
- },
- projectHistoryId: this.projectHistoryId,
- path: this.pathname,
- doc: this.doc_id,
- meta: { ts: new Date() },
- }
- await this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
- this.project_id,
- this.projectHistoryId,
- this.doc_id,
- this.lines,
- this.ranges,
- this.resolvedCommentIds,
- this.version,
- this.pathname,
- true
- )
- })
- it('should include tracked deletes in the update', function () {
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(this.update)
- )
- })
- it('should check the doc length without tracked deletes', function () {
- this.Limits.stringFileDataContentIsTooLarge.should.have.been.calledWith(
- this.lines,
- this.settings.max_doc_length
- )
- })
- it('should queue an update', function () {
- this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
- this.project_id,
- JSON.stringify(this.update)
- )
- })
- })
- })
- })
|