| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197 |
- import { expect } from 'chai'
- import Settings from '@overleaf/settings'
- import assert from 'node:assert'
- import async from 'async'
- import crypto from 'node:crypto'
- import mongodb from 'mongodb-legacy'
- import nock from 'nock'
- import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
- import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
- const { ObjectId } = mongodb
- const MockHistoryStore = () => nock('http://127.0.0.1:3100')
- const MockFileStore = () => nock('http://127.0.0.1:3009')
- const MockWeb = () => nock('http://127.0.0.1:3000')
- // Some helper methods to make the tests more compact
- function slTextUpdate(historyId, doc, userId, v, ts, op) {
- return {
- projectHistoryId: historyId,
- doc: doc.id,
- op,
- v,
- meta: {
- user_id: userId,
- ts: ts.getTime(),
- pathname: doc.pathname,
- doc_length: doc.length,
- },
- }
- }
- function slAddDocUpdate(historyId, doc, userId, ts, docLines, ranges = {}) {
- return {
- projectHistoryId: historyId,
- pathname: doc.pathname,
- ranges,
- docLines,
- doc: doc.id,
- meta: { user_id: userId, ts: ts.getTime() },
- }
- }
- function slAddDocUpdateWithVersion(
- historyId,
- doc,
- userId,
- ts,
- docLines,
- projectVersion,
- ranges = {}
- ) {
- const result = slAddDocUpdate(historyId, doc, userId, ts, docLines, ranges)
- result.version = projectVersion
- return result
- }
- function slAddFileUpdate(historyId, file, userId, ts, projectId) {
- return {
- projectHistoryId: historyId,
- pathname: file.pathname,
- url: `http://127.0.0.1:3009/project/${projectId}/file/${file.id}`,
- file: file.id,
- ranges: undefined,
- meta: { user_id: userId, ts: ts.getTime() },
- }
- }
- function createdBlobFileUpdate(historyId, file, userId, ts, projectId) {
- return {
- projectHistoryId: historyId,
- pathname: file.pathname,
- createdBlob: true,
- url: null,
- file: file.id,
- hash: file.hash,
- ranges: undefined,
- meta: { user_id: userId, ts: ts.getTime() },
- }
- }
- function slRenameUpdate(historyId, doc, userId, ts, pathname, newPathname) {
- return {
- projectHistoryId: historyId,
- pathname,
- new_pathname: newPathname,
- doc: doc.id,
- meta: { user_id: userId, ts: ts.getTime() },
- }
- }
- function olUpdate(doc, userId, ts, operations, v) {
- return {
- v2Authors: [userId],
- timestamp: ts.toJSON(),
- authors: [],
- operations,
- v2DocVersions: {
- [doc.id]: {
- pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
- v: v || 1,
- },
- },
- }
- }
- function olTextOperation(doc, textOperation) {
- return {
- pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
- textOperation,
- }
- }
- function olAddCommentOperation(doc, commentId, pos, length) {
- return {
- pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
- commentId,
- ranges: [{ pos, length }],
- }
- }
- function olTextUpdate(doc, userId, ts, textOperation, v) {
- return olUpdate(doc, userId, ts, [olTextOperation(doc, textOperation)], v)
- }
- function olTextUpdates(doc, userId, ts, textOperations, v) {
- return olUpdate(
- doc,
- userId,
- ts,
- textOperations.map(textOperation => olTextOperation(doc, textOperation)),
- v
- )
- }
- function olRenameUpdate(doc, userId, ts, pathname, newPathname) {
- return {
- v2Authors: [userId],
- timestamp: ts.toJSON(),
- authors: [],
- operations: [
- {
- pathname,
- newPathname,
- },
- ],
- }
- }
- function olAddDocUpdate(doc, userId, ts, fileHash, rangesHash = undefined) {
- const update = {
- v2Authors: [userId],
- timestamp: ts.toJSON(),
- authors: [],
- operations: [
- {
- pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
- file: {
- hash: fileHash,
- },
- },
- ],
- }
- if (rangesHash) {
- update.operations[0].file.rangesHash = rangesHash
- }
- return update
- }
- function olAddDocUpdateWithVersion(
- doc,
- userId,
- ts,
- fileHash,
- version,
- rangesHash = undefined
- ) {
- const result = olAddDocUpdate(doc, userId, ts, fileHash, rangesHash)
- result.projectVersion = version
- return result
- }
- function olAddFileUpdate(file, userId, ts, fileHash) {
- return {
- v2Authors: [userId],
- timestamp: ts.toJSON(),
- authors: [],
- operations: [
- {
- pathname: file.pathname.replace(/^\//, ''), // Strip leading /
- file: {
- hash: fileHash,
- },
- },
- ],
- }
- }
- describe('Sending Updates', function () {
- const historyId = new ObjectId().toString()
- beforeEach(function (done) {
- this.timestamp = new Date()
- ProjectHistoryApp.ensureRunning(error => {
- if (error) {
- return done(error)
- }
- this.userId = new ObjectId().toString()
- this.projectId = new ObjectId().toString()
- this.docId = new ObjectId().toString()
- this.doc = {
- id: this.docId,
- pathname: '/main.tex',
- length: 5,
- }
- MockHistoryStore().post('/api/projects').reply(200, {
- projectId: historyId,
- })
- MockWeb()
- .get(`/project/${this.projectId}/details`)
- .reply(200, {
- name: 'Test Project',
- overleaf: {
- history: {
- id: historyId,
- },
- },
- })
- ProjectHistoryClient.initializeProject(historyId, done)
- })
- })
- afterEach(function () {
- nock.cleanAll()
- })
- describe('basic update types', function () {
- beforeEach(function () {
- MockHistoryStore()
- .get(`/api/projects/${historyId}/latest/history`)
- .reply(200, {
- chunk: {
- startVersion: 0,
- history: {
- snapshot: {},
- changes: [],
- },
- },
- })
- })
- it('should send add doc updates to the history store', function (done) {
- const fileHash = '0a207c060e61f3b88eaee0a8cd0696f46fb155eb'
- const createBlob = MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${fileHash}`, 'a\nb')
- .reply(201)
- const addFile = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddDocUpdate(this.doc, this.userId, this.timestamp, fileHash),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddDocUpdate(
- historyId,
- this.doc,
- this.userId,
- this.timestamp,
- 'a\nb'
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createBlob.isDone(),
- '/api/projects/:historyId/blobs/:hash should have been called'
- )
- assert(
- addFile.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should send ranges to the history store', function (done) {
- const fileHash = '49e886093b3eacbc12b99a1eb5aeaa44a6b9d90e'
- const rangesHash = 'fa9a429ff518bc9e5b2507a96ff0646b566eca65'
- const historyRanges = {
- trackedChanges: [
- {
- range: { pos: 4, length: 3 },
- tracking: {
- type: 'delete',
- userId: 'user-id-1',
- ts: '2024-01-01T00:00:00.000Z',
- },
- },
- ],
- comments: [
- {
- ranges: [{ pos: 0, length: 3 }],
- id: 'comment-id-1',
- },
- ],
- }
- // We need to set up the ranges mock first, as we will call it last..
- const createRangesBlob = MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${rangesHash}`, historyRanges)
- .reply(201)
- const createBlob = MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${fileHash}`, 'foo barbaz')
- .reply(201)
- const addFile = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddDocUpdate(
- this.doc,
- this.userId,
- this.timestamp,
- fileHash,
- rangesHash
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddDocUpdate(
- historyId,
- this.doc,
- this.userId,
- this.timestamp,
- 'foo barbaz',
- {
- changes: [
- {
- op: { p: 4, d: 'bar' },
- metadata: {
- ts: 1704067200000,
- user_id: 'user-id-1',
- },
- },
- ],
- comments: [
- {
- op: {
- p: 0,
- c: 'foo',
- t: 'comment-id-1',
- },
- metadata: { resolved: false },
- },
- ],
- }
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createBlob.isDone(),
- '/api/projects/:historyId/blobs/:hash should have been called to create content blob'
- )
- assert(
- createRangesBlob.isDone(),
- '/api/projects/:historyId/blobs/:hash should have been called to create ranges blob'
- )
- assert(
- addFile.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should strip non-BMP characters in add doc updates before sending to the history store', function (done) {
- const fileHash = '11509fe05a41f9cdc51ea081342b5a4fc7c8d0fc'
- const createBlob = MockHistoryStore()
- .put(
- `/api/projects/${historyId}/blobs/${fileHash}`,
- 'a\nb\uFFFD\uFFFDc'
- )
- .reply(201)
- const addFile = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddDocUpdate(this.doc, this.userId, this.timestamp, fileHash),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddDocUpdate(
- historyId,
- this.doc,
- this.userId,
- this.timestamp,
- 'a\nb\uD800\uDC00c'
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createBlob.isDone(),
- '/api/projects/:historyId/blobs/:hash should have been called'
- )
- assert(
- addFile.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should send text updates to the history store', function (done) {
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(this.doc, this.userId, this.timestamp, [3, '\nc', 2]),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 1,
- this.timestamp,
- [{ p: 3, i: '\nc' }]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should send renames to the history store', function (done) {
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olRenameUpdate(
- this.doc,
- this.userId,
- this.timestamp,
- 'main.tex',
- 'main2.tex'
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slRenameUpdate(
- historyId,
- this.doc,
- this.userId,
- this.timestamp,
- '/main.tex',
- '/main2.tex'
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should not get file from filestore if no url provided', function (done) {
- const file = {
- id: new ObjectId().toString(),
- pathname: '/test.png',
- contents: Buffer.from([1, 2, 3]),
- hash: 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74',
- }
- const fileStoreRequest = MockFileStore()
- .get(`/project/${this.projectId}/file/${file.id}`)
- .reply(200, file.contents)
- const checkBlob = MockHistoryStore()
- .head(`/api/projects/${historyId}/blobs/${file.hash}`)
- .reply(200)
- const addFile = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddFileUpdate(file, this.userId, this.timestamp, file.hash),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- createdBlobFileUpdate(
- historyId,
- file,
- this.userId,
- this.timestamp,
- this.projectId
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- !fileStoreRequest.isDone(),
- 'filestore should not have been called'
- )
- assert(
- checkBlob.isDone(),
- `HEAD /api/projects/${historyId}/blobs/${file.hash} should have been called`
- )
- assert(
- addFile.isDone(),
- `/api/projects/${historyId}/latest/files should have been called`
- )
- done()
- }
- )
- })
- it('should send add file updates to the history store', function (done) {
- const file = {
- id: new ObjectId().toString(),
- pathname: '/test.png',
- contents: Buffer.from([1, 2, 3]),
- hash: 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74',
- }
- const fileStoreRequest = MockFileStore()
- .get(`/project/${this.projectId}/file/${file.id}`)
- .reply(200, file.contents)
- const createBlob = MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${file.hash}`, file.contents)
- .reply(201)
- const addFile = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddFileUpdate(file, this.userId, this.timestamp, file.hash),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddFileUpdate(
- historyId,
- file,
- this.userId,
- this.timestamp,
- this.projectId
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- fileStoreRequest.isDone(),
- `/project/${this.projectId}/file/${file.id} should have been called`
- )
- assert(
- createBlob.isDone(),
- `/api/projects/${historyId}/latest/files should have been called`
- )
- assert(
- addFile.isDone(),
- `/api/projects/${historyId}/latest/files should have been called`
- )
- done()
- }
- )
- })
- it('should send a stub to the history store when the file is large', function (done) {
- const fileContents = Buffer.alloc(Settings.maxFileSizeInBytes + 1, 'X')
- const fileSize = Buffer.byteLength(fileContents)
- const fileHash = crypto
- .createHash('sha1')
- .update('blob ' + fileSize + '\x00')
- .update(fileContents, 'utf8')
- .digest('hex')
- const file = {
- id: new ObjectId().toString(),
- pathname: '/large.png',
- contents: fileContents,
- hash: fileHash,
- }
- const stubContents = [
- 'FileTooLargeError v1',
- 'File too large to be stored in history service',
- `id project-${this.projectId}-file-${file.id}`,
- `size ${fileSize} bytes`,
- `hash ${fileHash}`,
- '\0', // null byte to make this a binary file
- ].join('\n')
- const stubHash = crypto
- .createHash('sha1')
- .update('blob ' + Buffer.byteLength(stubContents) + '\x00')
- .update(stubContents, 'utf8')
- .digest('hex')
- const stub = {
- id: file.id,
- pathname: file.pathname,
- contents: stubContents,
- hash: stubHash,
- }
- const fileStoreRequest = MockFileStore()
- .get(`/project/${this.projectId}/file/${file.id}`)
- .reply(200, file.contents)
- const createBlob = MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${stub.hash}`, stub.contents)
- .reply(201)
- const addFile = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddFileUpdate(stub, this.userId, this.timestamp, stub.hash),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddFileUpdate(
- historyId,
- file,
- this.userId,
- this.timestamp,
- this.projectId
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- addFile.isDone(),
- `/api/projects/${historyId}/latest/files should have been called`
- )
- assert(
- createBlob.isDone(),
- `/api/projects/${historyId}/latest/files should have been called`
- )
- assert(
- fileStoreRequest.isDone(),
- `/project/${this.projectId}/file/${file.id} should have been called`
- )
- done()
- }
- )
- })
- it('should handle comment ops', function (done) {
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olUpdate(this.doc, this.userId, this.timestamp, [
- olTextOperation(this.doc, [3, '\nc', 2]),
- olAddCommentOperation(this.doc, 'comment-id-1', 3, 2),
- ]),
- olUpdate(
- this.doc,
- this.userId,
- this.timestamp,
- [olAddCommentOperation(this.doc, 'comment-id-2', 2, 1)],
- 2
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 1,
- this.timestamp,
- [
- { p: 3, i: '\nc' },
- { p: 3, c: '\nc', t: 'comment-id-1' },
- ]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 2,
- this.timestamp,
- [{ p: 2, c: 'b', t: 'comment-id-2' }]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should be able to process lots of updates in batches', function (done) {
- const BATCH_SIZE = 500
- const createFirstChangeBatch = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(
- this.doc,
- this.userId,
- this.timestamp,
- ['a'.repeat(BATCH_SIZE), 6],
- BATCH_SIZE - 1
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- const createSecondChangeBatch = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(
- this.doc,
- this.userId,
- this.timestamp,
- ['a'.repeat(50), BATCH_SIZE + 6],
- BATCH_SIZE - 1 + 50
- ),
- ])
- return true
- })
- .query({ end_version: 500 })
- .reply(204)
- // these need mocking again for the second batch
- MockHistoryStore()
- .get(`/api/projects/${historyId}/latest/history`)
- .reply(200, {
- chunk: {
- startVersion: BATCH_SIZE,
- history: {
- snapshot: {},
- changes: [],
- },
- },
- })
- MockWeb()
- .get(`/project/${this.projectId}/details`)
- .reply(200, {
- name: 'Test Project',
- overleaf: {
- history: {
- id: historyId,
- },
- },
- })
- const pushChange = (n, cb) => {
- this.doc.length += 1
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(historyId, this.doc, this.userId, n, this.timestamp, [
- { p: 0, i: 'a' },
- ]),
- cb
- )
- }
- async.series(
- [
- cb => {
- async.times(BATCH_SIZE + 50, pushChange, cb)
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createFirstChangeBatch.isDone(),
- `/api/projects/${historyId}/changes should have been called for the first batch`
- )
- assert(
- createSecondChangeBatch.isDone(),
- `/api/projects/${historyId}/changes should have been called for the second batch`
- )
- done()
- }
- )
- })
- })
- describe('compressing updates', function () {
- beforeEach(function () {
- MockHistoryStore()
- .get(`/api/projects/${historyId}/latest/history`)
- .reply(200, {
- chunk: {
- startVersion: 0,
- history: {
- snapshot: {},
- changes: [],
- },
- },
- })
- })
- it('should concat adjacent text updates', function (done) {
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(
- this.doc,
- this.userId,
- this.timestamp,
- [3, 'foobaz', 2],
- 2
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 1,
- this.timestamp,
- [
- { p: 3, i: 'foobar' },
- { p: 6, d: 'bar' },
- ]
- ),
- cb
- )
- this.doc.length += 3
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 2,
- this.timestamp,
- [{ p: 6, i: 'baz' }]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should take the timestamp of the first update', function (done) {
- const timestamp1 = new Date(this.timestamp)
- const timestamp2 = new Date(this.timestamp.getTime() + 10000)
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(
- this.doc,
- this.userId,
- timestamp1,
- [3, 'foobaz', 2],
- 2
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(historyId, this.doc, this.userId, 1, timestamp1, [
- { p: 3, i: 'foo' },
- ]),
- cb
- )
- this.doc.length += 3
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(historyId, this.doc, this.userId, 2, timestamp2, [
- { p: 6, i: 'baz' },
- ]),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should not concat updates more than 60 seconds apart', function (done) {
- const timestamp1 = new Date(this.timestamp)
- const timestamp2 = new Date(this.timestamp.getTime() + 120000)
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(this.doc, this.userId, timestamp1, [3, 'foo', 2], 1),
- olTextUpdate(this.doc, this.userId, timestamp2, [6, 'baz', 2], 2),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(historyId, this.doc, this.userId, 1, timestamp1, [
- { p: 3, i: 'foo' },
- ]),
- cb
- )
- this.doc.length += 3
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(historyId, this.doc, this.userId, 2, timestamp2, [
- { p: 6, i: 'baz' },
- ]),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should not concat updates with different user_ids', function (done) {
- const userId1 = new ObjectId().toString()
- const userId2 = new ObjectId().toString()
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(this.doc, userId1, this.timestamp, [3, 'foo', 2], 1),
- olTextUpdate(this.doc, userId2, this.timestamp, [6, 'baz', 2], 2),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(historyId, this.doc, userId1, 1, this.timestamp, [
- { p: 3, i: 'foo' },
- ]),
- cb
- )
- this.doc.length += 3
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(historyId, this.doc, userId2, 2, this.timestamp, [
- { p: 6, i: 'baz' },
- ]),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should not concat updates with different docs', function (done) {
- const doc1 = {
- id: new ObjectId().toString(),
- pathname: '/doc1.tex',
- length: 10,
- }
- const doc2 = {
- id: new ObjectId().toString(),
- pathname: '/doc2.tex',
- length: 10,
- }
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(doc1, this.userId, this.timestamp, [3, 'foo', 7], 1),
- olTextUpdate(doc2, this.userId, this.timestamp, [6, 'baz', 4], 2),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(historyId, doc1, this.userId, 1, this.timestamp, [
- { p: 3, i: 'foo' },
- ]),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(historyId, doc2, this.userId, 2, this.timestamp, [
- { p: 6, i: 'baz' },
- ]),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should not send updates without any ops', function (done) {
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- // These blank ops can get sent by doc-updater on setDocs from Dropbox that don't change anything
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 1,
- this.timestamp,
- []
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- !createChange.isDone(),
- `/api/projects/${historyId}/changes should not have been called`
- )
- done()
- }
- )
- })
- it('should not send ops that compress to nothing', function (done) {
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 1,
- this.timestamp,
- [{ i: 'foo', p: 3 }]
- ),
- cb
- )
- this.doc.length += 3
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 2,
- this.timestamp,
- [{ d: 'foo', p: 3 }]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- !createChange.isDone(),
- `/api/projects/${historyId}/changes should not have been called`
- )
- done()
- }
- )
- })
- it('should not send ops from a diff that are blank', function (done) {
- this.doc.length = 300
- // Test case taken from a real life document where it was generating blank insert and
- // delete ops from a diff, and the blank delete was erroring on the OL history from
- // a text operation like [42, 0, 512], where the 0 was invalid.
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdates(this.doc, this.userId, this.timestamp, [
- [
- 87,
- -1,
- 67,
- '|l|ll|}\n\\hline',
- -4,
- 30,
- ' \\hline',
- 87,
- ' \\\\ \\hline',
- 24,
- ],
- ]),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 1,
- this.timestamp,
- [
- {
- p: 73,
- d: '\\begin{table}[h]\n\\centering\n\\caption{My caption}\n\\label{my-label}\n\\begin{tabular}{lll}\n & A & B \\\\\nLiter t up & 2 & 1 \\\\\nLiter Whiskey & 1 & 2 \\\\\nPris pr. liter & 200 & 250\n\\end{tabular}\n\\end{table}',
- },
- {
- p: 73,
- i: '\\begin{table}[]\n\\centering\n\\caption{My caption}\n\\label{my-label}\n\\begin{tabular}{|l|ll|}\n\\hline\n & A & B \\\\ \\hline\nLiter t up & 2 & 1 \\\\\nLiter Whiskey & 1 & 2 \\\\\nPris pr. liter & 200 & 250 \\\\ \\hline\n\\end{tabular}\n\\end{table}',
- },
- ]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should not concat text updates across project structure ops', function (done) {
- const newDoc = {
- id: new ObjectId().toString(),
- pathname: '/main.tex',
- hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
- docLines: 'a\nb',
- }
- MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${newDoc.hash}`)
- .reply(201)
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(
- this.doc,
- this.userId,
- this.timestamp,
- [3, 'foo', 2],
- 1
- ),
- olAddDocUpdate(newDoc, this.userId, this.timestamp, newDoc.hash),
- olTextUpdate(
- this.doc,
- this.userId,
- this.timestamp,
- [6, 'baz', 2],
- 2
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 1,
- this.timestamp,
- [
- { p: 3, i: 'foobar' },
- { p: 6, d: 'bar' },
- ]
- ),
- cb
- )
- this.doc.length += 3
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddDocUpdate(
- historyId,
- newDoc,
- this.userId,
- this.timestamp,
- newDoc.docLines
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 2,
- this.timestamp,
- [{ p: 6, i: 'baz' }]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should track the doc length when splitting ops', function (done) {
- this.doc.length = 10
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(this.doc, this.userId, this.timestamp, [3, -3, 4], 1),
- olTextUpdate(
- this.doc,
- this.userId,
- this.timestamp,
- [3, 'barbaz', 4],
- 2
- ), // This has a base length of 10
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 1,
- this.timestamp,
- [
- { p: 3, d: 'foo' },
- { p: 3, i: 'bar' }, // Make sure the length of the op generated from this is 7, not 10
- ]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 2,
- this.timestamp,
- [{ p: 6, i: 'baz' }]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- })
- describe('with bad pathnames', function () {
- beforeEach(function () {
- MockHistoryStore()
- .get(`/api/projects/${historyId}/latest/history`)
- .reply(200, {
- chunk: {
- startVersion: 0,
- history: {
- snapshot: {},
- changes: [],
- },
- },
- })
- })
- it('should replace \\ with _ and workaround * in pathnames', function (done) {
- const doc = {
- id: this.doc.id,
- pathname: '\\main.tex',
- hash: 'b07b6b7a27667965f733943737124395c7577bea',
- docLines: 'aaabbbccc',
- length: 9,
- }
- MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${doc.hash}`)
- .reply(201)
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddDocUpdate(
- { id: doc.id, pathname: '_main.tex' },
- this.userId,
- this.timestamp,
- doc.hash
- ),
- olRenameUpdate(
- { id: doc.id, pathname: '_main.tex' },
- this.userId,
- this.timestamp,
- '_main.tex',
- '_main2.tex'
- ),
- olTextUpdate(
- { id: doc.id, pathname: '_main2.tex' },
- this.userId,
- this.timestamp,
- [3, 'foo', 6],
- 2
- ),
- olRenameUpdate(
- { id: doc.id, pathname: '_main2.tex' },
- this.userId,
- this.timestamp,
- '_main2.tex',
- '_main__ASTERISK__.tex'
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddDocUpdate(
- historyId,
- doc,
- this.userId,
- this.timestamp,
- doc.docLines
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slRenameUpdate(
- historyId,
- doc,
- this.userId,
- this.timestamp,
- '/\\main.tex',
- '/\\main2.tex'
- ),
- cb
- )
- doc.pathname = '\\main2.tex'
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(historyId, doc, this.userId, 2, this.timestamp, [
- { p: 3, i: 'foo' },
- ]),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slRenameUpdate(
- historyId,
- doc,
- this.userId,
- this.timestamp,
- '/\\main2.tex',
- '/\\main*.tex'
- ),
- cb
- )
- doc.pathname = '\\main*.tex'
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- it('should workaround pathnames beginning with spaces', function (done) {
- const doc = {
- id: this.doc.id,
- pathname: 'main.tex',
- hash: 'b07b6b7a27667965f733943737124395c7577bea',
- docLines: 'aaabbbccc',
- length: 9,
- }
- MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${doc.hash}`)
- .reply(201)
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddDocUpdate(
- { id: doc.id, pathname: 'main.tex' },
- this.userId,
- this.timestamp,
- doc.hash
- ),
- olRenameUpdate(
- { id: doc.id },
- this.userId,
- this.timestamp,
- 'main.tex',
- 'foo/__SPACE__main.tex'
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddDocUpdate(
- historyId,
- doc,
- this.userId,
- this.timestamp,
- doc.docLines
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slRenameUpdate(
- historyId,
- doc,
- this.userId,
- this.timestamp,
- '/main.tex',
- '/foo/ main.tex'
- ),
- cb
- )
- doc.pathname = '/foo/ main.tex'
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- })
- describe('with bad response from filestore', function () {
- beforeEach(function () {
- MockHistoryStore()
- .get(`/api/projects/${historyId}/latest/history`)
- .reply(200, {
- chunk: {
- startVersion: 0,
- history: {
- snapshot: {},
- changes: [],
- },
- },
- })
- })
- it('should return a 500 if the filestore returns a 500', function (done) {
- const file = {
- id: new ObjectId().toString(),
- pathname: '/test.png',
- contents: Buffer.from([1, 2, 3]),
- hash: 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74',
- }
- const fileStoreRequest = MockFileStore()
- .get(`/project/${this.projectId}/file/${file.id}`)
- .reply(500)
- const createBlob = MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${file.hash}`, file.contents)
- .reply(201)
- const addFile = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddFileUpdate(file, this.userId, this.timestamp, file.hash),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddFileUpdate(
- historyId,
- file,
- this.userId,
- this.timestamp,
- this.projectId
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(
- this.projectId,
- { allowErrors: true },
- (error, res) => {
- if (error) {
- return cb(error)
- }
- expect(res.statusCode).to.equal(500)
- cb()
- }
- )
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- fileStoreRequest.isDone(),
- `/project/${this.projectId}/file/${file.id} should have been called`
- )
- assert(
- !createBlob.isDone(),
- `/api/projects/${historyId}/latest/files should not have been called`
- )
- assert(
- !addFile.isDone(),
- `/api/projects/${historyId}/latest/files should not have been called`
- )
- done()
- }
- )
- })
- it('should return a 500 if the filestore request errors', function (done) {
- const file = {
- id: new ObjectId().toString(),
- pathname: '/test.png',
- contents: Buffer.from([1, 2, 3]),
- hash: 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74',
- }
- const fileStoreRequest = MockFileStore()
- .get(`/project/${this.projectId}/file/${file.id}`)
- .replyWithError('oh no!')
- const createBlob = MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${file.hash}`, file.contents)
- .reply(201)
- const addFile = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddFileUpdate(file, this.userId, this.timestamp, file.hash),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddFileUpdate(
- historyId,
- file,
- this.userId,
- this.timestamp,
- this.projectId
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(
- this.projectId,
- { allowErrors: true },
- (error, res) => {
- if (error) {
- return cb(error)
- }
- expect(res.statusCode).to.equal(500)
- cb()
- }
- )
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- fileStoreRequest.isDone(),
- `/project/${this.projectId}/file/${file.id} should have been called`
- )
- assert(
- !createBlob.isDone(),
- `/api/projects/${historyId}/latest/files should not have been called`
- )
- assert(
- !addFile.isDone(),
- `/api/projects/${historyId}/latest/files should not have been called`
- )
- done()
- }
- )
- })
- })
- describe('with an existing projectVersion field', function () {
- beforeEach(function () {
- MockHistoryStore()
- .get(`/api/projects/${historyId}/latest/history`)
- .reply(200, {
- chunk: {
- startVersion: 0,
- history: {
- snapshot: { projectVersion: '100.0' },
- changes: [],
- },
- },
- })
- })
- it('should discard project structure updates which have already been applied', function (done) {
- const newDoc = []
- for (let i = 0; i <= 2; i++) {
- newDoc[i] = {
- id: new ObjectId().toString(),
- pathname: `/main${i}.tex`,
- hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
- docLines: 'a\nb',
- }
- }
- MockHistoryStore()
- .put(`/api/projects/${historyId}/blobs/${newDoc[0].hash}`)
- .times(3)
- .reply(201)
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olAddDocUpdateWithVersion(
- newDoc[1],
- this.userId,
- this.timestamp,
- newDoc[1].hash,
- '101.0'
- ),
- olAddDocUpdateWithVersion(
- newDoc[2],
- this.userId,
- this.timestamp,
- newDoc[2].hash,
- '102.0'
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddDocUpdateWithVersion(
- historyId,
- newDoc[0],
- this.userId,
- this.timestamp,
- newDoc[0].docLines,
- '100.0'
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddDocUpdateWithVersion(
- historyId,
- newDoc[1],
- this.userId,
- this.timestamp,
- newDoc[1].docLines,
- '101.0'
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slAddDocUpdateWithVersion(
- historyId,
- newDoc[2],
- this.userId,
- this.timestamp,
- newDoc[2].docLines,
- '102.0'
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- })
- describe('with an existing docVersions field', function () {
- beforeEach(function () {
- MockHistoryStore()
- .get(`/api/projects/${historyId}/latest/history`)
- .reply(200, {
- chunk: {
- startVersion: 0,
- history: {
- snapshot: { v2DocVersions: { [this.doc.id]: { v: 100 } } }, // version 100 below already applied
- changes: [],
- },
- },
- })
- })
- it('should discard doc updates which have already been applied', function (done) {
- const createChange = MockHistoryStore()
- .post(`/api/projects/${historyId}/legacy_changes`, body => {
- expect(body).to.deep.equal([
- olTextUpdate(
- this.doc,
- this.userId,
- this.timestamp,
- [6, 'baz', 2],
- 101
- ),
- ])
- return true
- })
- .query({ end_version: 0 })
- .reply(204)
- async.series(
- [
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 100,
- this.timestamp,
- [
- { p: 3, i: 'foobar' }, // these ops should be skipped
- { p: 6, d: 'bar' },
- ]
- ),
- cb
- )
- this.doc.length += 3
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 101,
- this.timestamp,
- [
- { p: 6, i: 'baz' }, // this op should be applied
- ]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.flushProject(this.projectId, cb)
- },
- ],
- error => {
- if (error) {
- return done(error)
- }
- assert(
- createChange.isDone(),
- `/api/projects/${historyId}/changes should have been called`
- )
- done()
- }
- )
- })
- })
- })
|