| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983 |
- import { expect } from 'chai'
- import Settings from '@overleaf/settings'
- import assert from 'assert'
- import async from 'async'
- import crypto from 'crypto'
- import { ObjectId } from 'mongodb'
- import nock from 'nock'
- import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
- import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
- const MockHistoryStore = () => nock('http://localhost:3100')
- const MockFileStore = () => nock('http://localhost:3009')
- const MockWeb = () => nock('http://localhost: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) {
- return {
- projectHistoryId: historyId,
- pathname: doc.pathname,
- docLines,
- doc: doc.id,
- meta: { user_id: userId, ts: ts.getTime() },
- }
- }
- function slAddDocUpdateWithVersion(
- historyId,
- doc,
- userId,
- ts,
- docLines,
- projectVersion
- ) {
- const result = slAddDocUpdate(historyId, doc, userId, ts, docLines)
- result.version = projectVersion
- return result
- }
- function slAddFileUpdate(historyId, file, userId, ts, projectId) {
- return {
- projectHistoryId: historyId,
- pathname: file.pathname,
- url: `http://localhost:3009/project/${projectId}/file/${file.id}`,
- file: file.id,
- 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 olTextUpdate(doc, userId, ts, textOperation, v) {
- return {
- v2Authors: [userId],
- timestamp: ts.toJSON(),
- authors: [],
- operations: [
- {
- pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
- textOperation,
- },
- ],
- v2DocVersions: {
- [doc.id]: {
- pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
- v: v || 1,
- },
- },
- }
- }
- function olTextUpdates(doc, userId, ts, textOperations, v) {
- return {
- v2Authors: [userId],
- timestamp: ts.toJSON(),
- authors: [],
- operations: textOperations.map(textOperation => ({
- // Strip leading /
- pathname: doc.pathname.replace(/^\//, ''),
- textOperation,
- })),
- v2DocVersions: {
- [doc.id]: {
- pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
- v: v || 1,
- },
- },
- }
- }
- function olRenameUpdate(doc, userId, ts, pathname, newPathname) {
- return {
- v2Authors: [userId],
- timestamp: ts.toJSON(),
- authors: [],
- operations: [
- {
- pathname,
- newPathname,
- },
- ],
- }
- }
- function olAddDocUpdate(doc, userId, ts, fileHash) {
- return {
- v2Authors: [userId],
- timestamp: ts.toJSON(),
- authors: [],
- operations: [
- {
- pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
- file: {
- hash: fileHash,
- },
- },
- ],
- }
- }
- function olAddDocUpdateWithVersion(doc, userId, ts, fileHash, version) {
- const result = olAddDocUpdate(doc, userId, ts, fileHash)
- 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 = ObjectId().toString()
- beforeEach(function (done) {
- this.timestamp = new Date()
- ProjectHistoryApp.ensureRunning(error => {
- if (error) {
- return done(error)
- }
- this.userId = ObjectId().toString()
- this.projectId = ObjectId().toString()
- this.docId = 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 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 send add file updates to the history store', function (done) {
- const file = {
- id: 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: 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 ignore comment ops', 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' },
- { p: 3, c: '\nc' },
- ]
- ),
- cb
- )
- },
- cb => {
- ProjectHistoryClient.pushRawUpdate(
- this.projectId,
- slTextUpdate(
- historyId,
- this.doc,
- this.userId,
- 2,
- this.timestamp,
- [{ p: 2, c: 'b' }]
- ),
- 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 = ObjectId().toString()
- const userId2 = 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: ObjectId().toString(),
- pathname: '/doc1.tex',
- length: 10,
- }
- const doc2 = {
- id: 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: 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: 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: 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: 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()
- }
- )
- })
- })
- })
|