SyncTests.js 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761
  1. import nock from 'nock'
  2. import { expect } from 'chai'
  3. import assert from 'node:assert'
  4. import logger from '@overleaf/logger'
  5. import Settings from '@overleaf/settings'
  6. import {
  7. Snapshot,
  8. File,
  9. FileMap,
  10. StringFileData,
  11. Change,
  12. } from 'overleaf-editor-core'
  13. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  14. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  15. import sinon from 'sinon'
  16. import { getFailure } from './helpers/ProjectHistoryClient.js'
  17. import { fetchNothing, RequestFailedError } from '@overleaf/fetch-utils'
  18. import { _getBlobHashFromString } from '../../../app/js/HashManager.js'
  19. import { db, ObjectId } from '../../../app/js/mongodb.js'
  20. const EMPTY_FILE_HASH = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
  21. const MockHistoryStore = () => nock('http://127.0.0.1:3100')
  22. const MockFileStore = () => nock('http://127.0.0.1:3009')
  23. const MockWeb = () => nock('http://127.0.0.1:3000')
  24. describe('Syncing with web and doc-updater', function () {
  25. const historyId = new ObjectId().toString()
  26. let loggerWarn, loggerError
  27. beforeEach(function () {
  28. loggerWarn = sinon.spy(logger, 'warn')
  29. loggerError = sinon.spy(logger, 'error')
  30. })
  31. afterEach(function () {
  32. loggerWarn.restore()
  33. loggerError.restore()
  34. })
  35. beforeEach(async function () {
  36. this.timestamp = new Date()
  37. await ProjectHistoryApp.ensureRunning()
  38. this.project_id = new ObjectId().toString()
  39. this.doc_id = new ObjectId().toString()
  40. this.file_id = new ObjectId().toString()
  41. await db.projects.insertOne({ _id: new ObjectId(this.project_id) })
  42. MockHistoryStore().post('/api/projects').reply(200, {
  43. projectId: historyId,
  44. })
  45. MockWeb()
  46. .get(`/project/${this.project_id}/details`)
  47. .reply(200, {
  48. name: 'Test Project',
  49. overleaf: {
  50. history: {
  51. id: historyId,
  52. },
  53. },
  54. })
  55. await ProjectHistoryClient.initializeProject(historyId)
  56. })
  57. afterEach(function () {
  58. nock.cleanAll()
  59. })
  60. describe('resyncing project history', function () {
  61. describe('without project-history enabled', function () {
  62. beforeEach(function () {
  63. MockWeb().post(`/project/${this.project_id}/history/resync`).reply(404)
  64. })
  65. it('404s if project-history is not enabled', async function () {
  66. try {
  67. await fetchNothing(
  68. `http://127.0.0.1:3054/project/${this.project_id}/resync`,
  69. {
  70. method: 'POST',
  71. }
  72. )
  73. } catch (error) {
  74. if (error instanceof RequestFailedError) {
  75. expect(error.response.status).to.equal(404)
  76. } else {
  77. throw error
  78. }
  79. }
  80. })
  81. })
  82. describe('with project-history enabled', function () {
  83. beforeEach(function () {
  84. MockWeb().post(`/project/${this.project_id}/history/resync`).reply(204)
  85. })
  86. describe('when a doc is missing', function () {
  87. it('should send add doc updates to the history store', async function () {
  88. MockHistoryStore()
  89. .get(`/api/projects/${historyId}/latest/history`)
  90. .reply(200, {
  91. chunk: {
  92. history: {
  93. snapshot: {
  94. files: {
  95. persistedDoc: { hash: EMPTY_FILE_HASH, stringLength: 0 },
  96. },
  97. },
  98. changes: [],
  99. },
  100. startVersion: 0,
  101. },
  102. })
  103. MockHistoryStore()
  104. .get(`/api/projects/${historyId}/blobs/${EMPTY_FILE_HASH}`)
  105. .reply(200, '')
  106. const createBlob = MockHistoryStore()
  107. .put(`/api/projects/${historyId}/blobs/${EMPTY_FILE_HASH}`, '')
  108. .reply(201)
  109. const addFile = MockHistoryStore()
  110. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  111. expect(body).to.deep.equal([
  112. {
  113. v2Authors: [],
  114. authors: [],
  115. timestamp: this.timestamp.toJSON(),
  116. operations: [
  117. {
  118. pathname: 'main.tex',
  119. file: {
  120. hash: EMPTY_FILE_HASH,
  121. },
  122. },
  123. ],
  124. origin: { kind: 'test-origin' },
  125. },
  126. ])
  127. return true
  128. })
  129. .query({ end_version: 0 })
  130. .reply(204)
  131. await ProjectHistoryClient.resyncHistory(this.project_id)
  132. const update = {
  133. projectHistoryId: historyId,
  134. resyncProjectStructure: {
  135. docs: [
  136. { path: '/main.tex', doc: this.doc_id },
  137. { path: '/persistedDoc', doc: 'other-doc-id' },
  138. ],
  139. files: [],
  140. },
  141. meta: {
  142. ts: this.timestamp,
  143. },
  144. }
  145. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  146. await ProjectHistoryClient.flushProject(this.project_id)
  147. assert(
  148. createBlob.isDone(),
  149. '/api/projects/:historyId/blobs/:hash should have been called'
  150. )
  151. assert(
  152. addFile.isDone(),
  153. `/api/projects/${historyId}/changes should have been called`
  154. )
  155. })
  156. })
  157. describe('when a file is missing', function () {
  158. it('should send add file updates to the history store', async function () {
  159. MockHistoryStore()
  160. .get(`/api/projects/${historyId}/latest/history`)
  161. .reply(200, {
  162. chunk: {
  163. history: {
  164. snapshot: {
  165. files: {
  166. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  167. },
  168. },
  169. changes: [],
  170. },
  171. startVersion: 0,
  172. },
  173. })
  174. const fileContents = Buffer.from([1, 2, 3])
  175. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  176. MockFileStore()
  177. .get(`/project/${this.project_id}/file/${this.file_id}`)
  178. .reply(200, fileContents)
  179. const headBlob = MockHistoryStore()
  180. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  181. .reply(404)
  182. const createBlob = MockHistoryStore()
  183. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  184. .reply(201)
  185. const addFile = MockHistoryStore()
  186. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  187. expect(body).to.deep.equal([
  188. {
  189. v2Authors: [],
  190. authors: [],
  191. timestamp: this.timestamp.toJSON(),
  192. operations: [
  193. {
  194. pathname: 'test.png',
  195. file: {
  196. hash: fileHash,
  197. },
  198. },
  199. ],
  200. origin: { kind: 'test-origin' },
  201. },
  202. ])
  203. return true
  204. })
  205. .query({ end_version: 0 })
  206. .reply(204)
  207. await ProjectHistoryClient.resyncHistory(this.project_id)
  208. const update = {
  209. projectHistoryId: historyId,
  210. resyncProjectStructure: {
  211. docs: [],
  212. files: [
  213. {
  214. file: this.file_id,
  215. path: '/test.png',
  216. _hash: fileHash,
  217. url: `http://127.0.0.1:3009/project/${this.project_id}/file/${this.file_id}`,
  218. },
  219. { path: '/persistedFile' },
  220. ],
  221. },
  222. meta: {
  223. ts: this.timestamp,
  224. },
  225. }
  226. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  227. await ProjectHistoryClient.flushProject(this.project_id)
  228. assert(!loggerWarn.called, 'no warning logged on 404')
  229. assert(
  230. headBlob.isDone(),
  231. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  232. )
  233. assert(
  234. createBlob.isDone(),
  235. '/api/projects/:historyId/blobs/:hash should have been called'
  236. )
  237. assert(
  238. addFile.isDone(),
  239. `/api/projects/${historyId}/changes should have been called`
  240. )
  241. })
  242. it('should skip HEAD on blob without hash', async function () {
  243. MockHistoryStore()
  244. .get(`/api/projects/${historyId}/latest/history`)
  245. .reply(200, {
  246. chunk: {
  247. history: {
  248. snapshot: {
  249. files: {
  250. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  251. },
  252. },
  253. changes: [],
  254. },
  255. startVersion: 0,
  256. },
  257. })
  258. const fileContents = Buffer.from([1, 2, 3])
  259. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  260. MockFileStore()
  261. .get(`/project/${this.project_id}/file/${this.file_id}`)
  262. .reply(200, fileContents)
  263. const headBlob = MockHistoryStore()
  264. .head(`/api/projects/${historyId}/blobs/undefined`)
  265. .reply(500)
  266. const createBlob = MockHistoryStore()
  267. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  268. .reply(201)
  269. const addFile = MockHistoryStore()
  270. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  271. expect(body).to.deep.equal([
  272. {
  273. v2Authors: [],
  274. authors: [],
  275. timestamp: this.timestamp.toJSON(),
  276. operations: [
  277. {
  278. pathname: 'test.png',
  279. file: {
  280. hash: fileHash,
  281. },
  282. },
  283. ],
  284. origin: { kind: 'test-origin' },
  285. },
  286. ])
  287. return true
  288. })
  289. .query({ end_version: 0 })
  290. .reply(204)
  291. await ProjectHistoryClient.resyncHistory(this.project_id)
  292. const update = {
  293. projectHistoryId: historyId,
  294. resyncProjectStructure: {
  295. docs: [],
  296. files: [
  297. {
  298. file: this.file_id,
  299. path: '/test.png',
  300. url: `http://127.0.0.1:3009/project/${this.project_id}/file/${this.file_id}`,
  301. },
  302. { path: '/persistedFile' },
  303. ],
  304. },
  305. meta: {
  306. ts: this.timestamp,
  307. },
  308. }
  309. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  310. await ProjectHistoryClient.flushProject(this.project_id)
  311. assert(!loggerWarn.called, 'no warning logged on 404')
  312. assert(
  313. !headBlob.isDone(),
  314. 'HEAD /api/projects/:historyId/blobs/:hash should have been skipped'
  315. )
  316. assert(
  317. createBlob.isDone(),
  318. '/api/projects/:historyId/blobs/:hash should have been called'
  319. )
  320. assert(
  321. addFile.isDone(),
  322. `/api/projects/${historyId}/changes should have been called`
  323. )
  324. })
  325. it('should record error when checking blob fails with 500', async function () {
  326. MockHistoryStore()
  327. .get(`/api/projects/${historyId}/latest/history`)
  328. .reply(200, {
  329. chunk: {
  330. history: {
  331. snapshot: {
  332. files: {
  333. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  334. },
  335. },
  336. changes: [],
  337. },
  338. startVersion: 0,
  339. },
  340. })
  341. const fileContents = Buffer.from([1, 2, 3])
  342. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  343. MockFileStore()
  344. .get(`/project/${this.project_id}/file/${this.file_id}`)
  345. .reply(200, fileContents)
  346. const headBlob = MockHistoryStore()
  347. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  348. .reply(500)
  349. const createBlob = MockHistoryStore()
  350. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  351. .reply(201)
  352. const addFile = MockHistoryStore()
  353. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  354. expect(body).to.deep.equal([
  355. {
  356. v2Authors: [],
  357. authors: [],
  358. timestamp: this.timestamp.toJSON(),
  359. operations: [
  360. {
  361. pathname: 'test.png',
  362. file: {
  363. hash: fileHash,
  364. },
  365. },
  366. ],
  367. origin: { kind: 'test-origin' },
  368. },
  369. ])
  370. return true
  371. })
  372. .query({ end_version: 0 })
  373. .reply(204)
  374. await ProjectHistoryClient.resyncHistory(this.project_id)
  375. const update = {
  376. projectHistoryId: historyId,
  377. resyncProjectStructure: {
  378. docs: [],
  379. files: [
  380. {
  381. file: this.file_id,
  382. path: '/test.png',
  383. _hash: fileHash,
  384. url: `http://127.0.0.1:3009/project/${this.project_id}/file/${this.file_id}`,
  385. },
  386. { path: '/persistedFile' },
  387. ],
  388. },
  389. meta: {
  390. ts: this.timestamp,
  391. },
  392. }
  393. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  394. const res = await ProjectHistoryClient.flushProject(this.project_id, {
  395. allowErrors: true,
  396. })
  397. assert(res.statusCode === 500, 'resync should have failed')
  398. assert(
  399. loggerError.calledWithMatch(
  400. sinon.match.any,
  401. 'error checking whether blob exists'
  402. ),
  403. 'error logged on 500'
  404. )
  405. assert(
  406. headBlob.isDone(),
  407. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  408. )
  409. assert(
  410. !createBlob.isDone(),
  411. '/api/projects/:historyId/blobs/:hash should have been skipped'
  412. )
  413. assert(
  414. !addFile.isDone(),
  415. `/api/projects/${historyId}/changes should have been skipped`
  416. )
  417. })
  418. it('should skip blob write when blob exists', async function () {
  419. MockHistoryStore()
  420. .get(`/api/projects/${historyId}/latest/history`)
  421. .reply(200, {
  422. chunk: {
  423. history: {
  424. snapshot: {
  425. files: {
  426. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  427. },
  428. },
  429. changes: [],
  430. },
  431. startVersion: 0,
  432. },
  433. })
  434. const fileContents = Buffer.from([1, 2, 3])
  435. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  436. MockFileStore()
  437. .get(`/project/${this.project_id}/file/${this.file_id}`)
  438. .reply(200, fileContents)
  439. const headBlob = MockHistoryStore()
  440. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  441. .reply(200)
  442. const createBlob = MockHistoryStore()
  443. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  444. .reply(201)
  445. const addFile = MockHistoryStore()
  446. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  447. expect(body).to.deep.equal([
  448. {
  449. v2Authors: [],
  450. authors: [],
  451. timestamp: this.timestamp.toJSON(),
  452. operations: [
  453. {
  454. pathname: 'test.png',
  455. file: {
  456. hash: fileHash,
  457. },
  458. },
  459. ],
  460. origin: { kind: 'test-origin' },
  461. },
  462. ])
  463. return true
  464. })
  465. .query({ end_version: 0 })
  466. .reply(204)
  467. await ProjectHistoryClient.resyncHistory(this.project_id)
  468. const update = {
  469. projectHistoryId: historyId,
  470. resyncProjectStructure: {
  471. docs: [],
  472. files: [
  473. {
  474. file: this.file_id,
  475. path: '/test.png',
  476. _hash: fileHash,
  477. url: `http://127.0.0.1:3009/project/${this.project_id}/file/${this.file_id}`,
  478. },
  479. { path: '/persistedFile' },
  480. ],
  481. },
  482. meta: {
  483. ts: this.timestamp,
  484. },
  485. }
  486. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  487. await ProjectHistoryClient.flushProject(this.project_id)
  488. assert(!loggerWarn.called, 'no warning logged on 404')
  489. assert(
  490. headBlob.isDone(),
  491. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  492. )
  493. assert(
  494. !createBlob.isDone(),
  495. '/api/projects/:historyId/blobs/:hash should have been skipped'
  496. )
  497. assert(
  498. addFile.isDone(),
  499. `/api/projects/${historyId}/changes should have been called`
  500. )
  501. })
  502. it('should add file w/o url', async function () {
  503. MockHistoryStore()
  504. .get(`/api/projects/${historyId}/latest/history`)
  505. .reply(200, {
  506. chunk: {
  507. history: {
  508. snapshot: {
  509. files: {
  510. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  511. },
  512. },
  513. changes: [],
  514. },
  515. startVersion: 0,
  516. },
  517. })
  518. const fileContents = Buffer.from([1, 2, 3])
  519. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  520. MockFileStore()
  521. .get(`/project/${this.project_id}/file/${this.file_id}`)
  522. .reply(200, fileContents)
  523. const headBlob = MockHistoryStore()
  524. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  525. .reply(200)
  526. const createBlob = MockHistoryStore()
  527. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  528. .reply(201)
  529. const addFile = MockHistoryStore()
  530. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  531. expect(body).to.deep.equal([
  532. {
  533. v2Authors: [],
  534. authors: [],
  535. timestamp: this.timestamp.toJSON(),
  536. operations: [
  537. {
  538. pathname: 'test.png',
  539. file: {
  540. hash: fileHash,
  541. },
  542. },
  543. ],
  544. origin: { kind: 'test-origin' },
  545. },
  546. ])
  547. return true
  548. })
  549. .query({ end_version: 0 })
  550. .reply(204)
  551. await ProjectHistoryClient.resyncHistory(this.project_id)
  552. const update = {
  553. projectHistoryId: historyId,
  554. resyncProjectStructure: {
  555. docs: [],
  556. files: [
  557. {
  558. file: this.file_id,
  559. path: '/test.png',
  560. _hash: fileHash,
  561. createdBlob: true,
  562. },
  563. { path: '/persistedFile' },
  564. ],
  565. },
  566. meta: {
  567. ts: this.timestamp,
  568. },
  569. }
  570. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  571. await ProjectHistoryClient.flushProject(this.project_id)
  572. assert(!loggerWarn.called, 'no warning logged on 404')
  573. assert(
  574. headBlob.isDone(),
  575. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  576. )
  577. assert(
  578. !createBlob.isDone(),
  579. '/api/projects/:historyId/blobs/:hash should have been skipped'
  580. )
  581. assert(
  582. addFile.isDone(),
  583. `/api/projects/${historyId}/changes should have been called`
  584. )
  585. })
  586. describe('with filestore disabled', function () {
  587. before(function () {
  588. Settings.apis.filestore.enabled = false
  589. })
  590. after(function () {
  591. Settings.apis.filestore.enabled = true
  592. })
  593. it('should record error when blob is missing', async function () {
  594. MockHistoryStore()
  595. .get(`/api/projects/${historyId}/latest/history`)
  596. .reply(200, {
  597. chunk: {
  598. history: {
  599. snapshot: {
  600. files: {
  601. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  602. },
  603. },
  604. changes: [],
  605. },
  606. startVersion: 0,
  607. },
  608. })
  609. const fileContents = Buffer.from([1, 2, 3])
  610. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  611. MockFileStore()
  612. .get(`/project/${this.project_id}/file/${this.file_id}`)
  613. .reply(200, fileContents)
  614. const headBlob = MockHistoryStore()
  615. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  616. .times(3) // three retries
  617. .reply(404)
  618. const createBlob = MockHistoryStore()
  619. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  620. .reply(201)
  621. const addFile = MockHistoryStore()
  622. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  623. expect(body).to.deep.equal([
  624. {
  625. v2Authors: [],
  626. authors: [],
  627. timestamp: this.timestamp.toJSON(),
  628. operations: [
  629. {
  630. pathname: 'test.png',
  631. file: {
  632. hash: fileHash,
  633. },
  634. },
  635. ],
  636. origin: { kind: 'test-origin' },
  637. },
  638. ])
  639. return true
  640. })
  641. .query({ end_version: 0 })
  642. .reply(204)
  643. await ProjectHistoryClient.resyncHistory(this.project_id)
  644. const update = {
  645. projectHistoryId: historyId,
  646. resyncProjectStructure: {
  647. docs: [],
  648. files: [
  649. {
  650. file: this.file_id,
  651. path: '/test.png',
  652. _hash: fileHash,
  653. url: `http://127.0.0.1:3009/project/${this.project_id}/file/${this.file_id}`,
  654. },
  655. { path: '/persistedFile' },
  656. ],
  657. },
  658. meta: {
  659. ts: this.timestamp,
  660. },
  661. }
  662. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  663. const res = await ProjectHistoryClient.flushProject(
  664. this.project_id,
  665. {
  666. allowErrors: true,
  667. }
  668. )
  669. assert(res.statusCode === 500, 'resync should have failed')
  670. assert(
  671. loggerError.calledWithMatch(
  672. sinon.match.any,
  673. 'blocking filestore read'
  674. ),
  675. 'error logged on 500'
  676. )
  677. assert(
  678. headBlob.isDone(),
  679. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  680. )
  681. assert(
  682. !createBlob.isDone(),
  683. '/api/projects/:historyId/blobs/:hash should have been skipped'
  684. )
  685. assert(
  686. !addFile.isDone(),
  687. `/api/projects/${historyId}/changes should have been skipped`
  688. )
  689. })
  690. })
  691. })
  692. describe('when a file hash mismatches', function () {
  693. it('should remove and re-add file w/o url', async function () {
  694. MockHistoryStore()
  695. .get(`/api/projects/${historyId}/latest/history`)
  696. .reply(200, {
  697. chunk: {
  698. history: {
  699. snapshot: {
  700. files: {
  701. 'test.png': { hash: EMPTY_FILE_HASH, byteLength: 0 },
  702. },
  703. },
  704. changes: [],
  705. },
  706. startVersion: 0,
  707. },
  708. })
  709. const fileContents = Buffer.from([1, 2, 3])
  710. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  711. MockFileStore()
  712. .get(`/project/${this.project_id}/file/${this.file_id}`)
  713. .reply(200, fileContents)
  714. const headBlob = MockHistoryStore()
  715. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  716. .reply(200)
  717. const createBlob = MockHistoryStore()
  718. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  719. .reply(201)
  720. const addFile = MockHistoryStore()
  721. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  722. expect(body).to.deep.equal([
  723. {
  724. v2Authors: [],
  725. authors: [],
  726. timestamp: this.timestamp.toJSON(),
  727. operations: [
  728. {
  729. pathname: 'test.png',
  730. newPathname: '',
  731. },
  732. ],
  733. origin: { kind: 'test-origin' },
  734. },
  735. {
  736. v2Authors: [],
  737. authors: [],
  738. timestamp: this.timestamp.toJSON(),
  739. operations: [
  740. {
  741. pathname: 'test.png',
  742. file: {
  743. hash: fileHash,
  744. },
  745. },
  746. ],
  747. origin: { kind: 'test-origin' },
  748. },
  749. ])
  750. return true
  751. })
  752. .query({ end_version: 0 })
  753. .reply(204)
  754. await ProjectHistoryClient.resyncHistory(this.project_id)
  755. const update = {
  756. projectHistoryId: historyId,
  757. resyncProjectStructure: {
  758. docs: [],
  759. files: [
  760. {
  761. file: this.file_id,
  762. path: '/test.png',
  763. _hash: fileHash,
  764. createdBlob: true,
  765. },
  766. ],
  767. },
  768. meta: {
  769. ts: this.timestamp,
  770. },
  771. }
  772. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  773. await ProjectHistoryClient.flushProject(this.project_id)
  774. assert(!loggerWarn.called, 'no warning logged on 404')
  775. assert(
  776. headBlob.isDone(),
  777. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  778. )
  779. assert(
  780. !createBlob.isDone(),
  781. '/api/projects/:historyId/blobs/:hash should have been skipped'
  782. )
  783. assert(
  784. addFile.isDone(),
  785. `/api/projects/${historyId}/changes should have been called`
  786. )
  787. })
  788. })
  789. describe("when a file exists which shouldn't", function () {
  790. it('should send remove file updates to the history store', async function () {
  791. MockHistoryStore()
  792. .get(`/api/projects/${historyId}/latest/history`)
  793. .reply(200, {
  794. chunk: {
  795. history: {
  796. snapshot: {
  797. files: {
  798. docToKeep: { hash: EMPTY_FILE_HASH, stringLength: 0 },
  799. docToDelete: { hash: EMPTY_FILE_HASH, stringLength: 0 },
  800. },
  801. },
  802. changes: [],
  803. },
  804. startVersion: 0,
  805. },
  806. })
  807. MockHistoryStore()
  808. .get(`/api/projects/${historyId}/blobs/${EMPTY_FILE_HASH}`)
  809. .reply(200, '')
  810. .get(`/api/projects/${historyId}/blobs/${EMPTY_FILE_HASH}`)
  811. .reply(200, '') // blob is requested once for each file
  812. const deleteFile = MockHistoryStore()
  813. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  814. expect(body).to.deep.equal([
  815. {
  816. v2Authors: [],
  817. authors: [],
  818. timestamp: this.timestamp.toJSON(),
  819. operations: [
  820. {
  821. pathname: 'docToDelete',
  822. newPathname: '',
  823. },
  824. ],
  825. origin: { kind: 'test-origin' },
  826. },
  827. ])
  828. return true
  829. })
  830. .query({ end_version: 0 })
  831. .reply(204)
  832. await ProjectHistoryClient.resyncHistory(this.project_id)
  833. const update = {
  834. projectHistoryId: historyId,
  835. resyncProjectStructure: {
  836. docs: [{ path: 'docToKeep' }],
  837. files: [],
  838. },
  839. meta: {
  840. ts: this.timestamp,
  841. },
  842. }
  843. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  844. await ProjectHistoryClient.flushProject(this.project_id)
  845. assert(
  846. deleteFile.isDone(),
  847. `/api/projects/${historyId}/changes should have been called`
  848. )
  849. })
  850. })
  851. describe("when a doc's contents is not up to date", function () {
  852. beforeEach(function () {
  853. MockHistoryStore()
  854. .get(`/api/projects/${historyId}/latest/history`)
  855. .reply(200, {
  856. chunk: {
  857. history: {
  858. snapshot: {
  859. files: {
  860. 'main.tex': {
  861. hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
  862. stringLength: 3,
  863. },
  864. },
  865. },
  866. changes: [],
  867. },
  868. startVersion: 0,
  869. },
  870. })
  871. MockHistoryStore()
  872. .get(
  873. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  874. )
  875. .reply(200, 'a\nb')
  876. })
  877. it('should send test updates to the history store', async function () {
  878. const beforeResync = new Date()
  879. const addFile = MockHistoryStore()
  880. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  881. expect(body).to.deep.equal([
  882. {
  883. v2Authors: [],
  884. authors: [],
  885. timestamp: this.timestamp.toJSON(),
  886. operations: [
  887. {
  888. pathname: 'main.tex',
  889. textOperation: [3, '\nc'],
  890. },
  891. ],
  892. origin: { kind: 'test-origin' },
  893. },
  894. ])
  895. return true
  896. })
  897. .query({ end_version: 0 })
  898. .reply(204)
  899. await ProjectHistoryClient.resyncHistory(this.project_id)
  900. const update1 = {
  901. projectHistoryId: historyId,
  902. resyncProjectStructure: {
  903. docs: [{ path: '/main.tex' }],
  904. files: [],
  905. },
  906. meta: {
  907. ts: this.timestamp,
  908. },
  909. }
  910. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  911. const update2 = {
  912. path: '/main.tex',
  913. projectHistoryId: historyId,
  914. resyncDocContent: {
  915. content: 'a\nb\nc',
  916. },
  917. doc: this.doc_id,
  918. meta: {
  919. ts: this.timestamp,
  920. },
  921. }
  922. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  923. await ProjectHistoryClient.flushProject(this.project_id)
  924. assert(
  925. addFile.isDone(),
  926. `/api/projects/${historyId}/changes should have been called`
  927. )
  928. const project = await db.projects.findOne({
  929. _id: new ObjectId(this.project_id),
  930. })
  931. assert(
  932. project.overleaf.history.lastResyncedAt > beforeResync,
  933. 'lastResyncedAt should have been updated when resync finished'
  934. )
  935. })
  936. it('should strip non-BMP characters in updates before sending to the history store', async function () {
  937. const addFile = MockHistoryStore()
  938. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  939. expect(body).to.deep.equal([
  940. {
  941. v2Authors: [],
  942. authors: [],
  943. timestamp: this.timestamp.toJSON(),
  944. operations: [
  945. {
  946. pathname: 'main.tex',
  947. textOperation: [3, '\n\uFFFD\uFFFDc'],
  948. },
  949. ],
  950. origin: { kind: 'test-origin' },
  951. },
  952. ])
  953. return true
  954. })
  955. .query({ end_version: 0 })
  956. .reply(204)
  957. await ProjectHistoryClient.resyncHistory(this.project_id)
  958. const update1 = {
  959. projectHistoryId: historyId,
  960. resyncProjectStructure: {
  961. docs: [{ path: '/main.tex' }],
  962. files: [],
  963. },
  964. meta: {
  965. ts: this.timestamp,
  966. },
  967. }
  968. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  969. const update2 = {
  970. path: '/main.tex',
  971. projectHistoryId: historyId,
  972. resyncDocContent: {
  973. content: 'a\nb\n\uD800\uDC00c',
  974. },
  975. doc: this.doc_id,
  976. meta: {
  977. ts: this.timestamp,
  978. },
  979. }
  980. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  981. await ProjectHistoryClient.flushProject(this.project_id)
  982. assert(
  983. addFile.isDone(),
  984. `/api/projects/${historyId}/changes should have been called`
  985. )
  986. })
  987. it('should add comments in the history store', async function () {
  988. const commentId = 'comment-id'
  989. const addComment = MockHistoryStore()
  990. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  991. expect(body).to.deep.equal([
  992. {
  993. v2Authors: [],
  994. authors: [],
  995. timestamp: this.timestamp.toJSON(),
  996. operations: [
  997. {
  998. pathname: 'main.tex',
  999. commentId,
  1000. ranges: [{ pos: 1, length: 10 }],
  1001. },
  1002. ],
  1003. origin: { kind: 'test-origin' },
  1004. },
  1005. ])
  1006. return true
  1007. })
  1008. .query({ end_version: 0 })
  1009. .reply(204)
  1010. await ProjectHistoryClient.resyncHistory(this.project_id)
  1011. const update1 = {
  1012. projectHistoryId: historyId,
  1013. resyncProjectStructure: {
  1014. docs: [{ path: '/main.tex' }],
  1015. files: [],
  1016. },
  1017. meta: {
  1018. ts: this.timestamp,
  1019. },
  1020. }
  1021. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1022. const update2 = {
  1023. path: '/main.tex',
  1024. projectHistoryId: historyId,
  1025. resyncDocContent: {
  1026. content: 'a\nb',
  1027. ranges: {
  1028. comments: [
  1029. {
  1030. id: commentId,
  1031. op: {
  1032. c: 'a',
  1033. p: 0,
  1034. hpos: 1,
  1035. hlen: 10,
  1036. t: commentId,
  1037. },
  1038. meta: {
  1039. user_id: 'user-id',
  1040. ts: this.timestamp,
  1041. },
  1042. },
  1043. ],
  1044. },
  1045. },
  1046. doc: this.doc_id,
  1047. meta: {
  1048. ts: this.timestamp,
  1049. },
  1050. }
  1051. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  1052. await ProjectHistoryClient.flushProject(this.project_id)
  1053. assert(
  1054. addComment.isDone(),
  1055. `/api/projects/${historyId}/changes should have been called`
  1056. )
  1057. })
  1058. it('should add comments in the history store (history-ot)', async function () {
  1059. const commentId = 'comment-id'
  1060. const addComment = MockHistoryStore()
  1061. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1062. expect(body).to.deep.equal([
  1063. {
  1064. v2Authors: [],
  1065. authors: [],
  1066. timestamp: this.timestamp.toJSON(),
  1067. operations: [
  1068. {
  1069. pathname: 'main.tex',
  1070. commentId,
  1071. ranges: [{ pos: 1, length: 10 }],
  1072. },
  1073. ],
  1074. origin: { kind: 'test-origin' },
  1075. },
  1076. ])
  1077. return true
  1078. })
  1079. .query({ end_version: 0 })
  1080. .reply(204)
  1081. await ProjectHistoryClient.resyncHistory(this.project_id)
  1082. const update1 = {
  1083. projectHistoryId: historyId,
  1084. resyncProjectStructure: {
  1085. docs: [{ path: '/main.tex' }],
  1086. files: [],
  1087. },
  1088. meta: {
  1089. ts: this.timestamp,
  1090. },
  1091. }
  1092. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1093. const update2 = {
  1094. path: '/main.tex',
  1095. projectHistoryId: historyId,
  1096. resyncDocContent: {
  1097. content: 'a\nb',
  1098. historyOTRanges: {
  1099. comments: [
  1100. {
  1101. id: commentId,
  1102. ranges: [
  1103. {
  1104. pos: 1,
  1105. length: 10,
  1106. },
  1107. ],
  1108. },
  1109. ],
  1110. },
  1111. },
  1112. doc: this.doc_id,
  1113. meta: {
  1114. ts: this.timestamp,
  1115. },
  1116. }
  1117. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  1118. await ProjectHistoryClient.flushProject(this.project_id)
  1119. assert(
  1120. addComment.isDone(),
  1121. `/api/projects/${historyId}/changes should have been called`
  1122. )
  1123. })
  1124. it('should add tracked changes in the history store', async function () {
  1125. const fixTrackedChange = MockHistoryStore()
  1126. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1127. expect(body).to.deep.equal([
  1128. {
  1129. v2Authors: [],
  1130. authors: [],
  1131. timestamp: this.timestamp.toJSON(),
  1132. operations: [
  1133. {
  1134. pathname: 'main.tex',
  1135. textOperation: [
  1136. {
  1137. r: 1,
  1138. tracking: {
  1139. ts: this.timestamp.toJSON(),
  1140. type: 'delete',
  1141. userId: 'user-id',
  1142. },
  1143. },
  1144. {
  1145. r: 1,
  1146. tracking: {
  1147. ts: this.timestamp.toJSON(),
  1148. type: 'insert',
  1149. userId: 'user-id',
  1150. },
  1151. },
  1152. 1,
  1153. ],
  1154. },
  1155. ],
  1156. origin: { kind: 'test-origin' },
  1157. },
  1158. ])
  1159. return true
  1160. })
  1161. .query({ end_version: 0 })
  1162. .reply(204)
  1163. await ProjectHistoryClient.resyncHistory(this.project_id)
  1164. const update1 = {
  1165. projectHistoryId: historyId,
  1166. resyncProjectStructure: {
  1167. docs: [{ path: '/main.tex' }],
  1168. files: [],
  1169. },
  1170. meta: {
  1171. ts: this.timestamp,
  1172. },
  1173. }
  1174. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1175. const update2 = {
  1176. path: '/main.tex',
  1177. projectHistoryId: historyId,
  1178. resyncDocContent: {
  1179. content: 'a\nb',
  1180. ranges: {
  1181. changes: [
  1182. {
  1183. id: 'id1',
  1184. op: {
  1185. d: 'a',
  1186. p: 0,
  1187. },
  1188. metadata: {
  1189. user_id: 'user-id',
  1190. ts: this.timestamp,
  1191. },
  1192. },
  1193. {
  1194. id: 'id2',
  1195. op: {
  1196. i: '\n',
  1197. p: 0,
  1198. hpos: 1,
  1199. },
  1200. metadata: {
  1201. user_id: 'user-id',
  1202. ts: this.timestamp,
  1203. },
  1204. },
  1205. ],
  1206. },
  1207. },
  1208. doc: this.doc_id,
  1209. meta: {
  1210. ts: this.timestamp,
  1211. },
  1212. }
  1213. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  1214. await ProjectHistoryClient.flushProject(this.project_id)
  1215. assert(
  1216. fixTrackedChange.isDone(),
  1217. `/api/projects/${historyId}/changes should have been called`
  1218. )
  1219. })
  1220. it('should add tracked changes in the history store (history-ot)', async function () {
  1221. const fixTrackedChange = MockHistoryStore()
  1222. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1223. expect(body).to.deep.equal([
  1224. {
  1225. v2Authors: [],
  1226. authors: [],
  1227. timestamp: this.timestamp.toJSON(),
  1228. operations: [
  1229. {
  1230. pathname: 'main.tex',
  1231. textOperation: [
  1232. {
  1233. r: 1,
  1234. tracking: {
  1235. ts: this.timestamp.toJSON(),
  1236. type: 'delete',
  1237. userId: 'user-id',
  1238. },
  1239. },
  1240. {
  1241. r: 1,
  1242. tracking: {
  1243. ts: this.timestamp.toJSON(),
  1244. type: 'insert',
  1245. userId: 'user-id',
  1246. },
  1247. },
  1248. 1,
  1249. ],
  1250. },
  1251. ],
  1252. origin: { kind: 'test-origin' },
  1253. },
  1254. ])
  1255. return true
  1256. })
  1257. .query({ end_version: 0 })
  1258. .reply(204)
  1259. await ProjectHistoryClient.resyncHistory(this.project_id)
  1260. const update1 = {
  1261. projectHistoryId: historyId,
  1262. resyncProjectStructure: {
  1263. docs: [{ path: '/main.tex' }],
  1264. files: [],
  1265. },
  1266. meta: {
  1267. ts: this.timestamp,
  1268. },
  1269. }
  1270. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1271. const update2 = {
  1272. path: '/main.tex',
  1273. projectHistoryId: historyId,
  1274. resyncDocContent: {
  1275. content: 'a\nb',
  1276. historyOTRanges: {
  1277. trackedChanges: [
  1278. {
  1279. range: { pos: 0, length: 1 },
  1280. tracking: {
  1281. ts: this.timestamp.toJSON(),
  1282. type: 'delete',
  1283. userId: 'user-id',
  1284. },
  1285. },
  1286. {
  1287. range: { pos: 1, length: 1 },
  1288. tracking: {
  1289. ts: this.timestamp.toJSON(),
  1290. type: 'insert',
  1291. userId: 'user-id',
  1292. },
  1293. },
  1294. ],
  1295. },
  1296. },
  1297. doc: this.doc_id,
  1298. meta: {
  1299. ts: this.timestamp,
  1300. },
  1301. }
  1302. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  1303. await ProjectHistoryClient.flushProject(this.project_id)
  1304. assert(
  1305. fixTrackedChange.isDone(),
  1306. `/api/projects/${historyId}/changes should have been called`
  1307. )
  1308. })
  1309. })
  1310. describe("when a doc's ranges are out of sync", function () {
  1311. const commentId = 'comment-id'
  1312. beforeEach(function () {
  1313. MockHistoryStore()
  1314. .get(`/api/projects/${historyId}/latest/history`)
  1315. .reply(200, {
  1316. chunk: {
  1317. history: {
  1318. snapshot: {
  1319. files: {
  1320. 'main.tex': {
  1321. hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
  1322. rangesHash: '0a207c060e61f3b88eaee0a8cd0696f46fb155ec',
  1323. stringLength: 3,
  1324. },
  1325. },
  1326. },
  1327. changes: [],
  1328. },
  1329. startVersion: 0,
  1330. },
  1331. })
  1332. MockHistoryStore()
  1333. .get(
  1334. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  1335. )
  1336. .reply(200, 'a\nb')
  1337. MockHistoryStore()
  1338. .get(
  1339. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155ec`
  1340. )
  1341. .reply(
  1342. 200,
  1343. JSON.stringify({
  1344. comments: [{ id: commentId, ranges: [{ pos: 0, length: 3 }] }],
  1345. trackedChanges: [
  1346. {
  1347. range: { pos: 0, length: 1 },
  1348. tracking: {
  1349. ts: this.timestamp.toJSON(),
  1350. type: 'delete',
  1351. userId: 'user-id',
  1352. },
  1353. },
  1354. {
  1355. range: { pos: 2, length: 1 },
  1356. tracking: {
  1357. ts: this.timestamp.toJSON(),
  1358. type: 'insert',
  1359. userId: 'user-id',
  1360. },
  1361. },
  1362. ],
  1363. })
  1364. )
  1365. })
  1366. it('should fix comments in the history store', async function () {
  1367. const addComment = MockHistoryStore()
  1368. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1369. expect(body).to.deep.equal([
  1370. {
  1371. v2Authors: [],
  1372. authors: [],
  1373. timestamp: this.timestamp.toJSON(),
  1374. operations: [
  1375. {
  1376. pathname: 'main.tex',
  1377. commentId,
  1378. ranges: [{ pos: 1, length: 2 }],
  1379. },
  1380. ],
  1381. origin: { kind: 'test-origin' },
  1382. },
  1383. ])
  1384. return true
  1385. })
  1386. .query({ end_version: 0 })
  1387. .reply(204)
  1388. await ProjectHistoryClient.resyncHistory(this.project_id)
  1389. const update1 = {
  1390. projectHistoryId: historyId,
  1391. resyncProjectStructure: {
  1392. docs: [{ path: '/main.tex' }],
  1393. files: [],
  1394. },
  1395. meta: {
  1396. ts: this.timestamp,
  1397. },
  1398. }
  1399. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1400. const update2 = {
  1401. path: '/main.tex',
  1402. projectHistoryId: historyId,
  1403. resyncDocContent: {
  1404. content: 'a\nb',
  1405. ranges: {
  1406. comments: [
  1407. {
  1408. id: commentId,
  1409. op: {
  1410. c: 'a',
  1411. p: 0,
  1412. hpos: 1,
  1413. hlen: 2,
  1414. t: commentId,
  1415. },
  1416. meta: {
  1417. user_id: 'user-id',
  1418. ts: this.timestamp,
  1419. },
  1420. },
  1421. ],
  1422. changes: [
  1423. {
  1424. id: 'id1',
  1425. op: {
  1426. d: 'a',
  1427. p: 0,
  1428. },
  1429. metadata: {
  1430. user_id: 'user-id',
  1431. ts: this.timestamp,
  1432. },
  1433. },
  1434. {
  1435. id: 'id2',
  1436. op: {
  1437. i: '\n',
  1438. p: 1,
  1439. hpos: 2,
  1440. },
  1441. metadata: {
  1442. user_id: 'user-id',
  1443. ts: this.timestamp,
  1444. },
  1445. },
  1446. ],
  1447. },
  1448. },
  1449. doc: this.doc_id,
  1450. meta: {
  1451. ts: this.timestamp,
  1452. },
  1453. }
  1454. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  1455. await ProjectHistoryClient.flushProject(this.project_id)
  1456. assert(
  1457. addComment.isDone(),
  1458. `/api/projects/${historyId}/changes should have been called`
  1459. )
  1460. })
  1461. it('should fix resolved state for comments in the history store', async function () {
  1462. const addComment = MockHistoryStore()
  1463. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1464. expect(body).to.deep.equal([
  1465. {
  1466. v2Authors: [],
  1467. authors: [],
  1468. timestamp: this.timestamp.toJSON(),
  1469. operations: [
  1470. {
  1471. pathname: 'main.tex',
  1472. commentId,
  1473. resolved: true,
  1474. },
  1475. ],
  1476. origin: { kind: 'test-origin' },
  1477. },
  1478. ])
  1479. return true
  1480. })
  1481. .query({ end_version: 0 })
  1482. .reply(204)
  1483. await ProjectHistoryClient.resyncHistory(this.project_id)
  1484. const update1 = {
  1485. projectHistoryId: historyId,
  1486. resyncProjectStructure: {
  1487. docs: [{ path: '/main.tex' }],
  1488. files: [],
  1489. },
  1490. meta: {
  1491. ts: this.timestamp,
  1492. },
  1493. }
  1494. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1495. const update2 = {
  1496. path: '/main.tex',
  1497. projectHistoryId: historyId,
  1498. resyncDocContent: {
  1499. content: 'a\nb',
  1500. resolvedCommentIds: [commentId],
  1501. ranges: {
  1502. comments: [
  1503. {
  1504. id: commentId,
  1505. op: {
  1506. c: 'a',
  1507. p: 0,
  1508. hpos: 0,
  1509. hlen: 3,
  1510. t: commentId,
  1511. },
  1512. meta: {
  1513. user_id: 'user-id',
  1514. ts: this.timestamp,
  1515. },
  1516. },
  1517. ],
  1518. changes: [
  1519. {
  1520. id: 'id1',
  1521. op: {
  1522. d: 'a',
  1523. p: 0,
  1524. },
  1525. metadata: {
  1526. user_id: 'user-id',
  1527. ts: this.timestamp,
  1528. },
  1529. },
  1530. {
  1531. id: 'id2',
  1532. op: {
  1533. i: '\n',
  1534. p: 1,
  1535. hpos: 2,
  1536. },
  1537. metadata: {
  1538. user_id: 'user-id',
  1539. ts: this.timestamp,
  1540. },
  1541. },
  1542. ],
  1543. },
  1544. },
  1545. doc: this.doc_id,
  1546. meta: {
  1547. ts: this.timestamp,
  1548. },
  1549. }
  1550. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  1551. await ProjectHistoryClient.flushProject(this.project_id)
  1552. assert(
  1553. addComment.isDone(),
  1554. `/api/projects/${historyId}/changes should have been called`
  1555. )
  1556. })
  1557. it('should fix comments in the history store (history-ot)', async function () {
  1558. const addComment = MockHistoryStore()
  1559. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1560. expect(body).to.deep.equal([
  1561. {
  1562. v2Authors: [],
  1563. authors: [],
  1564. timestamp: this.timestamp.toJSON(),
  1565. operations: [
  1566. {
  1567. pathname: 'main.tex',
  1568. commentId,
  1569. ranges: [{ pos: 1, length: 2 }],
  1570. },
  1571. ],
  1572. origin: { kind: 'test-origin' },
  1573. },
  1574. ])
  1575. return true
  1576. })
  1577. .query({ end_version: 0 })
  1578. .reply(204)
  1579. await ProjectHistoryClient.resyncHistory(this.project_id)
  1580. const update1 = {
  1581. projectHistoryId: historyId,
  1582. resyncProjectStructure: {
  1583. docs: [{ path: '/main.tex' }],
  1584. files: [],
  1585. },
  1586. meta: {
  1587. ts: this.timestamp,
  1588. },
  1589. }
  1590. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1591. const update2 = {
  1592. path: '/main.tex',
  1593. projectHistoryId: historyId,
  1594. resyncDocContent: {
  1595. content: 'a\nb',
  1596. historyOTRanges: {
  1597. comments: [
  1598. {
  1599. id: commentId,
  1600. ranges: [
  1601. {
  1602. pos: 1,
  1603. length: 2,
  1604. },
  1605. ],
  1606. },
  1607. ],
  1608. trackedChanges: [
  1609. {
  1610. range: { pos: 0, length: 1 },
  1611. tracking: {
  1612. ts: this.timestamp.toJSON(),
  1613. type: 'delete',
  1614. userId: 'user-id',
  1615. },
  1616. },
  1617. {
  1618. range: { pos: 2, length: 1 },
  1619. tracking: {
  1620. ts: this.timestamp.toJSON(),
  1621. type: 'insert',
  1622. userId: 'user-id',
  1623. },
  1624. },
  1625. ],
  1626. },
  1627. },
  1628. doc: this.doc_id,
  1629. meta: {
  1630. ts: this.timestamp,
  1631. },
  1632. }
  1633. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  1634. await ProjectHistoryClient.flushProject(this.project_id)
  1635. assert(
  1636. addComment.isDone(),
  1637. `/api/projects/${historyId}/changes should have been called`
  1638. )
  1639. })
  1640. it('should fix resolved state for comments in the history store (history-ot)', async function () {
  1641. const addComment = MockHistoryStore()
  1642. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1643. expect(body).to.deep.equal([
  1644. {
  1645. v2Authors: [],
  1646. authors: [],
  1647. timestamp: this.timestamp.toJSON(),
  1648. operations: [
  1649. {
  1650. pathname: 'main.tex',
  1651. commentId,
  1652. resolved: true,
  1653. },
  1654. ],
  1655. origin: { kind: 'test-origin' },
  1656. },
  1657. ])
  1658. return true
  1659. })
  1660. .query({ end_version: 0 })
  1661. .reply(204)
  1662. await ProjectHistoryClient.resyncHistory(this.project_id)
  1663. const update1 = {
  1664. projectHistoryId: historyId,
  1665. resyncProjectStructure: {
  1666. docs: [{ path: '/main.tex' }],
  1667. files: [],
  1668. },
  1669. meta: {
  1670. ts: this.timestamp,
  1671. },
  1672. }
  1673. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1674. const update2 = {
  1675. path: '/main.tex',
  1676. projectHistoryId: historyId,
  1677. resyncDocContent: {
  1678. content: 'a\nb',
  1679. historyOTRanges: {
  1680. comments: [
  1681. {
  1682. id: commentId,
  1683. ranges: [
  1684. {
  1685. pos: 0,
  1686. length: 3,
  1687. },
  1688. ],
  1689. resolved: true,
  1690. },
  1691. ],
  1692. trackedChanges: [
  1693. {
  1694. range: { pos: 0, length: 1 },
  1695. tracking: {
  1696. ts: this.timestamp.toJSON(),
  1697. type: 'delete',
  1698. userId: 'user-id',
  1699. },
  1700. },
  1701. {
  1702. range: { pos: 2, length: 1 },
  1703. tracking: {
  1704. ts: this.timestamp.toJSON(),
  1705. type: 'insert',
  1706. userId: 'user-id',
  1707. },
  1708. },
  1709. ],
  1710. },
  1711. },
  1712. doc: this.doc_id,
  1713. meta: {
  1714. ts: this.timestamp,
  1715. },
  1716. }
  1717. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  1718. await ProjectHistoryClient.flushProject(this.project_id)
  1719. assert(
  1720. addComment.isDone(),
  1721. `/api/projects/${historyId}/changes should have been called`
  1722. )
  1723. })
  1724. it('should fix tracked changes in the history store', async function () {
  1725. const fixTrackedChange = MockHistoryStore()
  1726. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1727. expect(body).to.deep.equal([
  1728. {
  1729. v2Authors: [],
  1730. authors: [],
  1731. timestamp: this.timestamp.toJSON(),
  1732. operations: [
  1733. {
  1734. pathname: 'main.tex',
  1735. textOperation: [
  1736. 1,
  1737. {
  1738. r: 1,
  1739. tracking: {
  1740. ts: this.timestamp.toJSON(),
  1741. type: 'insert',
  1742. userId: 'user-id',
  1743. },
  1744. },
  1745. {
  1746. r: 1,
  1747. tracking: {
  1748. type: 'none',
  1749. },
  1750. },
  1751. ],
  1752. },
  1753. ],
  1754. origin: { kind: 'test-origin' },
  1755. },
  1756. ])
  1757. return true
  1758. })
  1759. .query({ end_version: 0 })
  1760. .reply(204)
  1761. await ProjectHistoryClient.resyncHistory(this.project_id)
  1762. const update1 = {
  1763. projectHistoryId: historyId,
  1764. resyncProjectStructure: {
  1765. docs: [{ path: '/main.tex' }],
  1766. files: [],
  1767. },
  1768. meta: {
  1769. ts: this.timestamp,
  1770. },
  1771. }
  1772. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1773. const update2 = {
  1774. path: '/main.tex',
  1775. projectHistoryId: historyId,
  1776. resyncDocContent: {
  1777. content: 'a\nb',
  1778. ranges: {
  1779. comments: [
  1780. {
  1781. id: commentId,
  1782. op: {
  1783. c: 'a',
  1784. p: 0,
  1785. hpos: 0,
  1786. hlen: 3,
  1787. t: commentId,
  1788. },
  1789. meta: {
  1790. user_id: 'user-id',
  1791. ts: this.timestamp,
  1792. },
  1793. },
  1794. ],
  1795. changes: [
  1796. {
  1797. id: 'id1',
  1798. op: {
  1799. d: 'a',
  1800. p: 0,
  1801. },
  1802. metadata: {
  1803. user_id: 'user-id',
  1804. ts: this.timestamp,
  1805. },
  1806. },
  1807. {
  1808. id: 'id2',
  1809. op: {
  1810. i: '\n',
  1811. p: 0,
  1812. hpos: 1,
  1813. },
  1814. metadata: {
  1815. user_id: 'user-id',
  1816. ts: this.timestamp,
  1817. },
  1818. },
  1819. ],
  1820. },
  1821. },
  1822. doc: this.doc_id,
  1823. meta: {
  1824. ts: this.timestamp,
  1825. },
  1826. }
  1827. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  1828. await ProjectHistoryClient.flushProject(this.project_id)
  1829. assert(
  1830. fixTrackedChange.isDone(),
  1831. `/api/projects/${historyId}/changes should have been called`
  1832. )
  1833. })
  1834. it('should fix tracked changes in the history store (history-ot)', async function () {
  1835. const fixTrackedChange = MockHistoryStore()
  1836. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1837. expect(body).to.deep.equal([
  1838. {
  1839. v2Authors: [],
  1840. authors: [],
  1841. timestamp: this.timestamp.toJSON(),
  1842. operations: [
  1843. {
  1844. pathname: 'main.tex',
  1845. textOperation: [
  1846. 1,
  1847. {
  1848. r: 1,
  1849. tracking: {
  1850. ts: this.timestamp.toJSON(),
  1851. type: 'insert',
  1852. userId: 'user-id',
  1853. },
  1854. },
  1855. {
  1856. r: 1,
  1857. tracking: {
  1858. type: 'none',
  1859. },
  1860. },
  1861. ],
  1862. },
  1863. ],
  1864. origin: { kind: 'test-origin' },
  1865. },
  1866. ])
  1867. return true
  1868. })
  1869. .query({ end_version: 0 })
  1870. .reply(204)
  1871. await ProjectHistoryClient.resyncHistory(this.project_id)
  1872. const update1 = {
  1873. projectHistoryId: historyId,
  1874. resyncProjectStructure: {
  1875. docs: [{ path: '/main.tex' }],
  1876. files: [],
  1877. },
  1878. meta: {
  1879. ts: this.timestamp,
  1880. },
  1881. }
  1882. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1883. const update2 = {
  1884. path: '/main.tex',
  1885. projectHistoryId: historyId,
  1886. resyncDocContent: {
  1887. content: 'a\nb',
  1888. historyOTRanges: {
  1889. comments: [
  1890. {
  1891. id: commentId,
  1892. ranges: [
  1893. {
  1894. pos: 0,
  1895. length: 3,
  1896. },
  1897. ],
  1898. },
  1899. ],
  1900. trackedChanges: [
  1901. {
  1902. range: { pos: 0, length: 1 },
  1903. tracking: {
  1904. ts: this.timestamp.toJSON(),
  1905. type: 'delete',
  1906. userId: 'user-id',
  1907. },
  1908. },
  1909. {
  1910. range: { pos: 1, length: 1 },
  1911. tracking: {
  1912. ts: this.timestamp.toJSON(),
  1913. type: 'insert',
  1914. userId: 'user-id',
  1915. },
  1916. },
  1917. ],
  1918. },
  1919. },
  1920. doc: this.doc_id,
  1921. meta: {
  1922. ts: this.timestamp,
  1923. },
  1924. }
  1925. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  1926. await ProjectHistoryClient.flushProject(this.project_id)
  1927. assert(
  1928. fixTrackedChange.isDone(),
  1929. `/api/projects/${historyId}/changes should have been called`
  1930. )
  1931. })
  1932. it('should fix both comments and tracked changes in the history store (history-ot)', async function () {
  1933. const fixTrackedChange = MockHistoryStore()
  1934. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1935. expect(body).to.deep.equal([
  1936. // not merged due to comment operation using history-ot and tracked-changes operation using sharejs ot
  1937. {
  1938. v2Authors: [],
  1939. authors: [],
  1940. timestamp: this.timestamp.toJSON(),
  1941. operations: [
  1942. {
  1943. pathname: 'main.tex',
  1944. commentId,
  1945. ranges: [{ pos: 1, length: 2 }],
  1946. },
  1947. ],
  1948. origin: { kind: 'test-origin' },
  1949. },
  1950. {
  1951. v2Authors: [],
  1952. authors: [],
  1953. timestamp: this.timestamp.toJSON(),
  1954. operations: [
  1955. {
  1956. pathname: 'main.tex',
  1957. textOperation: [
  1958. 1,
  1959. {
  1960. r: 1,
  1961. tracking: {
  1962. ts: this.timestamp.toJSON(),
  1963. type: 'insert',
  1964. userId: 'user-id',
  1965. },
  1966. },
  1967. {
  1968. r: 1,
  1969. tracking: {
  1970. type: 'none',
  1971. },
  1972. },
  1973. ],
  1974. },
  1975. ],
  1976. origin: { kind: 'test-origin' },
  1977. },
  1978. ])
  1979. return true
  1980. })
  1981. .query({ end_version: 0 })
  1982. .reply(204)
  1983. await ProjectHistoryClient.resyncHistory(this.project_id)
  1984. const update1 = {
  1985. projectHistoryId: historyId,
  1986. resyncProjectStructure: {
  1987. docs: [{ path: '/main.tex' }],
  1988. files: [],
  1989. },
  1990. meta: {
  1991. ts: this.timestamp,
  1992. },
  1993. }
  1994. await ProjectHistoryClient.pushRawUpdate(this.project_id, update1)
  1995. const update2 = {
  1996. path: '/main.tex',
  1997. projectHistoryId: historyId,
  1998. resyncDocContent: {
  1999. content: 'a\nb',
  2000. historyOTRanges: {
  2001. comments: [
  2002. {
  2003. id: commentId,
  2004. ranges: [
  2005. {
  2006. pos: 1,
  2007. length: 2,
  2008. },
  2009. ],
  2010. },
  2011. ],
  2012. trackedChanges: [
  2013. {
  2014. range: { pos: 0, length: 1 },
  2015. tracking: {
  2016. ts: this.timestamp.toJSON(),
  2017. type: 'delete',
  2018. userId: 'user-id',
  2019. },
  2020. },
  2021. {
  2022. range: { pos: 1, length: 1 },
  2023. tracking: {
  2024. ts: this.timestamp.toJSON(),
  2025. type: 'insert',
  2026. userId: 'user-id',
  2027. },
  2028. },
  2029. ],
  2030. },
  2031. },
  2032. doc: this.doc_id,
  2033. meta: {
  2034. ts: this.timestamp,
  2035. },
  2036. }
  2037. await ProjectHistoryClient.pushRawUpdate(this.project_id, update2)
  2038. await ProjectHistoryClient.flushProject(this.project_id)
  2039. assert(
  2040. fixTrackedChange.isDone(),
  2041. `/api/projects/${historyId}/changes should have been called`
  2042. )
  2043. })
  2044. })
  2045. describe('resyncProjectStructureOnly', function () {
  2046. it('should handle structure only updates', async function () {
  2047. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  2048. MockHistoryStore()
  2049. .get(`/api/projects/${historyId}/latest/history`)
  2050. .reply(200, {
  2051. chunk: {
  2052. history: {
  2053. snapshot: {
  2054. files: {
  2055. 'main.tex': {
  2056. hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
  2057. stringLength: 3,
  2058. },
  2059. },
  2060. },
  2061. changes: [],
  2062. },
  2063. startVersion: 0,
  2064. },
  2065. })
  2066. const docContentRequest = MockHistoryStore()
  2067. .get(
  2068. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  2069. )
  2070. .reply(200, 'a\nb')
  2071. MockHistoryStore()
  2072. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  2073. .reply(200)
  2074. const addFile = MockHistoryStore()
  2075. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  2076. expect(body).to.deep.equal([
  2077. {
  2078. v2Authors: [],
  2079. authors: [],
  2080. timestamp: this.timestamp.toJSON(),
  2081. operations: [
  2082. {
  2083. pathname: 'test.png',
  2084. file: {
  2085. hash: fileHash,
  2086. },
  2087. },
  2088. ],
  2089. origin: { kind: 'test-origin' },
  2090. },
  2091. ])
  2092. return true
  2093. })
  2094. .query({ end_version: 0 })
  2095. .reply(204)
  2096. // allow a 2nd resync
  2097. MockWeb()
  2098. .post(`/project/${this.project_id}/history/resync`)
  2099. .reply(204)
  2100. await ProjectHistoryClient.resyncHistory(this.project_id)
  2101. const update = {
  2102. projectHistoryId: historyId,
  2103. resyncProjectStructureOnly: true,
  2104. resyncProjectStructure: {
  2105. docs: [{ path: '/main.tex' }],
  2106. files: [
  2107. {
  2108. file: this.file_id,
  2109. path: '/test.png',
  2110. _hash: fileHash,
  2111. createdBlob: true,
  2112. },
  2113. ],
  2114. },
  2115. meta: {
  2116. ts: this.timestamp,
  2117. },
  2118. }
  2119. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  2120. await ProjectHistoryClient.flushProject(this.project_id)
  2121. // fails when previous resync did not finish
  2122. await ProjectHistoryClient.resyncHistory(this.project_id)
  2123. assert(
  2124. addFile.isDone(),
  2125. `/api/projects/${historyId}/changes should have been called`
  2126. )
  2127. assert(
  2128. !docContentRequest.isDone(),
  2129. 'should not have requested doc content'
  2130. )
  2131. })
  2132. it('should reject partial resync on docs', async function () {
  2133. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  2134. MockHistoryStore()
  2135. .get(`/api/projects/${historyId}/latest/history`)
  2136. .reply(200, {
  2137. chunk: {
  2138. history: {
  2139. snapshot: {
  2140. files: {
  2141. 'main.tex': {
  2142. hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
  2143. stringLength: 3,
  2144. },
  2145. },
  2146. },
  2147. changes: [],
  2148. },
  2149. startVersion: 0,
  2150. },
  2151. })
  2152. const docContentRequest = MockHistoryStore()
  2153. .get(
  2154. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  2155. )
  2156. .reply(200, 'a\nb')
  2157. MockHistoryStore()
  2158. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  2159. .reply(200)
  2160. const addFile = MockHistoryStore()
  2161. .post(`/api/projects/${historyId}/legacy_changes`)
  2162. .query({ end_version: 0 })
  2163. .reply(204)
  2164. // allow a 2nd resync
  2165. MockWeb()
  2166. .post(`/project/${this.project_id}/history/resync`)
  2167. .reply(204)
  2168. await ProjectHistoryClient.resyncHistory(this.project_id)
  2169. const update = {
  2170. projectHistoryId: historyId,
  2171. resyncProjectStructureOnly: true,
  2172. resyncProjectStructure: {
  2173. docs: [{ path: '/main-renamed.tex' }],
  2174. files: [
  2175. {
  2176. file: this.file_id,
  2177. path: '/test.png',
  2178. _hash: fileHash,
  2179. createdBlob: true,
  2180. },
  2181. ],
  2182. },
  2183. meta: {
  2184. ts: this.timestamp,
  2185. },
  2186. }
  2187. await ProjectHistoryClient.pushRawUpdate(this.project_id, update)
  2188. const res = await ProjectHistoryClient.flushProject(this.project_id, {
  2189. allowErrors: true,
  2190. })
  2191. expect(res.statusCode).to.equal(500)
  2192. expect(loggerError).to.have.been.calledWith(
  2193. sinon.match({
  2194. err: {
  2195. name: 'NeedFullProjectStructureResyncError',
  2196. message: 'aborting partial resync: touched doc',
  2197. },
  2198. })
  2199. )
  2200. const failure = await new Promise((resolve, reject) => {
  2201. getFailure(this.project_id, (err, failure) => {
  2202. if (err) return reject(err)
  2203. resolve(failure)
  2204. })
  2205. })
  2206. expect(failure).to.include({
  2207. error:
  2208. 'NeedFullProjectStructureResyncError: aborting partial resync: touched doc',
  2209. })
  2210. // fails when previous resync did not finish
  2211. await ProjectHistoryClient.resyncHistory(this.project_id)
  2212. assert(!addFile.isDone(), 'should not have persisted changes')
  2213. assert(
  2214. !docContentRequest.isDone(),
  2215. 'should not have requested doc content'
  2216. )
  2217. })
  2218. })
  2219. describe('stuck sync state (missing resyncDocContent updates)', function () {
  2220. beforeEach(function () {
  2221. MockHistoryStore()
  2222. .get(`/api/projects/${historyId}/latest/history`)
  2223. .reply(200, {
  2224. chunk: {
  2225. history: {
  2226. snapshot: {
  2227. files: {
  2228. 'main.tex': {
  2229. hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
  2230. stringLength: 3,
  2231. },
  2232. },
  2233. },
  2234. changes: [],
  2235. },
  2236. startVersion: 0,
  2237. },
  2238. })
  2239. MockHistoryStore()
  2240. .get(
  2241. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  2242. )
  2243. .reply(200, 'a\nb')
  2244. })
  2245. it('should get stuck when resyncDocContent update is missing', async function () {
  2246. // Step 1: Start a resync
  2247. await ProjectHistoryClient.resyncHistory(this.project_id)
  2248. // Step 2: Push resyncProjectStructure update (lists docs to sync)
  2249. // but do NOT push the corresponding resyncDocContent update
  2250. const structureUpdate = {
  2251. projectHistoryId: historyId,
  2252. resyncProjectStructure: {
  2253. docs: [{ path: '/main.tex' }],
  2254. files: [],
  2255. },
  2256. meta: {
  2257. ts: this.timestamp,
  2258. },
  2259. }
  2260. await ProjectHistoryClient.pushRawUpdate(
  2261. this.project_id,
  2262. structureUpdate
  2263. )
  2264. // Step 3: Flush — processes structure update, adds /main.tex to resyncDocContents
  2265. // This succeeds (204) but leaves sync in an incomplete state
  2266. await ProjectHistoryClient.flushProject(this.project_id)
  2267. // Step 4: Verify the sync state is stuck
  2268. const syncState = await ProjectHistoryClient.getSyncState(
  2269. this.project_id
  2270. )
  2271. expect(syncState).to.not.be.null
  2272. expect(syncState.resyncProjectStructure).to.equal(false)
  2273. expect(syncState.resyncDocContents).to.deep.equal(['/main.tex'])
  2274. expect(syncState.resyncPendingSince).to.be.a('date')
  2275. // Sync thinks it's ongoing because resyncDocContents is not empty
  2276. // Step 5: Push a normal text update (simulating a user edit)
  2277. const textUpdate = {
  2278. doc: this.doc_id,
  2279. op: [{ p: 3, i: '\nc' }],
  2280. v: 1,
  2281. meta: {
  2282. ts: this.timestamp,
  2283. pathname: '/main.tex',
  2284. doc_length: 3,
  2285. },
  2286. }
  2287. await ProjectHistoryClient.pushRawUpdate(this.project_id, textUpdate)
  2288. // Step 6: Flush again — the text update should be silently skipped
  2289. // because the doc is in resyncDocContents (sync "ongoing")
  2290. await ProjectHistoryClient.flushProject(this.project_id)
  2291. // Step 7: Verify the sync state is STILL stuck (text update was skipped)
  2292. const syncStateAfter = await ProjectHistoryClient.getSyncState(
  2293. this.project_id
  2294. )
  2295. expect(syncStateAfter.resyncProjectStructure).to.equal(false)
  2296. expect(syncStateAfter.resyncDocContents).to.deep.equal(['/main.tex'])
  2297. // Step 7b: Verify resync-pending reports ongoing but not yet stuck
  2298. const pending = await ProjectHistoryClient.getResyncPending(
  2299. this.project_id
  2300. )
  2301. expect(pending.resyncPending).to.equal(true)
  2302. expect(pending.syncStuck).to.equal(false)
  2303. // Step 8: Verify a normal resync FAILS because sync is "ongoing"
  2304. try {
  2305. await fetchNothing(
  2306. `http://127.0.0.1:3054/project/${this.project_id}/resync`,
  2307. {
  2308. method: 'POST',
  2309. json: { origin: { kind: 'test-origin' } },
  2310. }
  2311. )
  2312. assert.fail('resync should have failed')
  2313. } catch (error) {
  2314. if (error instanceof RequestFailedError) {
  2315. expect(error.response.status).to.equal(500)
  2316. } else {
  2317. throw error
  2318. }
  2319. }
  2320. })
  2321. it('should recover with a hard resync (force=true)', async function () {
  2322. // Set up the stuck state (same as above)
  2323. await ProjectHistoryClient.resyncHistory(this.project_id)
  2324. const structureUpdate = {
  2325. projectHistoryId: historyId,
  2326. resyncProjectStructure: {
  2327. docs: [{ path: '/main.tex' }],
  2328. files: [],
  2329. },
  2330. meta: {
  2331. ts: this.timestamp,
  2332. },
  2333. }
  2334. await ProjectHistoryClient.pushRawUpdate(
  2335. this.project_id,
  2336. structureUpdate
  2337. )
  2338. await ProjectHistoryClient.flushProject(this.project_id)
  2339. // Verify stuck
  2340. const syncState = await ProjectHistoryClient.getSyncState(
  2341. this.project_id
  2342. )
  2343. expect(syncState.resyncDocContents).to.deep.equal(['/main.tex'])
  2344. // Mock the web resync endpoint for the hard resync
  2345. MockWeb()
  2346. .post(`/project/${this.project_id}/history/resync`)
  2347. .reply(204)
  2348. // Hard resync should clear the stuck state and succeed
  2349. const response = await fetchNothing(
  2350. `http://127.0.0.1:3054/project/${this.project_id}/resync?force=true`,
  2351. {
  2352. method: 'POST',
  2353. json: { origin: { kind: 'test-origin' } },
  2354. }
  2355. )
  2356. expect(response.status).to.equal(204)
  2357. // After hard resync, sync state should be cleared or reset
  2358. const syncStateAfter = await ProjectHistoryClient.getSyncState(
  2359. this.project_id
  2360. )
  2361. // Hard resync clears and restarts — at this point the new resync
  2362. // should be in progress (resyncProjectStructure = true) or cleared
  2363. // depending on whether web sent updates
  2364. if (syncStateAfter) {
  2365. expect(syncStateAfter.resyncProjectStructure).to.equal(true)
  2366. }
  2367. })
  2368. it('should auto-recover when stuck (null resyncPendingSince)', async function () {
  2369. // Inject a stuck sync state: ongoing but no resyncPendingSince
  2370. // (legacy state from before the timestamp field was added)
  2371. await ProjectHistoryClient.injectStuckSyncState(this.project_id, [
  2372. '/main.tex',
  2373. ])
  2374. // Verify the state is recognised as stuck
  2375. const pending = await ProjectHistoryClient.getResyncPending(
  2376. this.project_id
  2377. )
  2378. expect(pending.resyncPending).to.equal(true)
  2379. expect(pending.syncStuck).to.equal(true)
  2380. // Mock the web callback that requestResync triggers
  2381. MockWeb()
  2382. .post(`/project/${this.project_id}/history/resync`)
  2383. .reply(204)
  2384. // A plain resync (no force) should detect stuck, clear, and restart
  2385. await ProjectHistoryClient.resyncHistory(this.project_id)
  2386. // stuckClearCount incremented and a new structure sync started
  2387. const syncState = await ProjectHistoryClient.getSyncState(
  2388. this.project_id
  2389. )
  2390. expect(syncState.stuckClearCount).to.equal(1)
  2391. expect(syncState.resyncProjectStructure).to.equal(true)
  2392. expect(syncState.lastStuckDocPaths).to.deep.equal(['/main.tex'])
  2393. })
  2394. })
  2395. })
  2396. })
  2397. // Regression: UpdateCompressor.compressUpdates optimizes a delete+insert
  2398. // at the same position by diffing the two strings and retaining common
  2399. // substrings. The TC retain ops are computed by the SyncManager based on
  2400. // the original (unoptimized) content diff. When the optimized ops retain
  2401. // characters that the original diff deleted, tracked changes on those
  2402. // characters survive — but the TC retains don't clear them, producing a
  2403. // ghost tracked change.
  2404. describe('resync with tracked changes overlapping content diff', function () {
  2405. const TIMESTAMP = '2025-01-01T00:00:00.000Z'
  2406. const persisted = {
  2407. content: 'lblcdhqcmihkrvzlifscqmwytt\n\ndgakoxboqpzdbbjtom',
  2408. trackedChanges: [
  2409. {
  2410. range: { pos: 2, length: 3 },
  2411. tracking: { type: 'insert', userId: 'user-2', ts: TIMESTAMP },
  2412. },
  2413. {
  2414. range: { pos: 12, length: 4 },
  2415. tracking: { type: 'delete', userId: 'user-2', ts: TIMESTAMP },
  2416. },
  2417. {
  2418. range: { pos: 26, length: 3 },
  2419. tracking: { type: 'delete', userId: 'user-2', ts: TIMESTAMP },
  2420. },
  2421. {
  2422. range: { pos: 32, length: 5 },
  2423. tracking: { type: 'delete', userId: 'user-2', ts: TIMESTAMP },
  2424. },
  2425. ],
  2426. }
  2427. const expected = {
  2428. content: 'lblchfjaqdhqcmihkrvzliftt\ndoqpzdbom',
  2429. trackedChanges: [
  2430. {
  2431. range: { pos: 0, length: 4 },
  2432. tracking: { type: 'insert', userId: 'user-2', ts: TIMESTAMP },
  2433. },
  2434. {
  2435. range: { pos: 11, length: 1 },
  2436. tracking: { type: 'delete', userId: 'user-2', ts: TIMESTAMP },
  2437. },
  2438. {
  2439. range: { pos: 17, length: 5 },
  2440. tracking: { type: 'delete', userId: 'user-2', ts: TIMESTAMP },
  2441. },
  2442. {
  2443. range: { pos: 22, length: 3 },
  2444. tracking: { type: 'insert', userId: 'user-1', ts: TIMESTAMP },
  2445. },
  2446. {
  2447. range: { pos: 26, length: 5 },
  2448. tracking: { type: 'delete', userId: 'user-1', ts: TIMESTAMP },
  2449. },
  2450. ],
  2451. }
  2452. it('should not leave ghost tracked changes after resync', async function () {
  2453. const rangesBlob = JSON.stringify({
  2454. comments: [],
  2455. trackedChanges: persisted.trackedChanges,
  2456. })
  2457. const contentHash = _getBlobHashFromString(persisted.content)
  2458. const rangesHash = _getBlobHashFromString(rangesBlob)
  2459. MockHistoryStore()
  2460. .get(`/api/projects/${historyId}/latest/history`)
  2461. .reply(200, {
  2462. chunk: {
  2463. history: {
  2464. snapshot: {
  2465. files: {
  2466. 'main.tex': {
  2467. hash: contentHash,
  2468. stringLength: persisted.content.length,
  2469. rangesHash,
  2470. },
  2471. },
  2472. },
  2473. changes: [],
  2474. },
  2475. startVersion: 0,
  2476. },
  2477. })
  2478. MockHistoryStore()
  2479. .get(`/api/projects/${historyId}/blobs/${contentHash}`)
  2480. .reply(200, persisted.content)
  2481. MockHistoryStore()
  2482. .get(`/api/projects/${historyId}/blobs/${rangesHash}`)
  2483. .reply(200, rangesBlob)
  2484. MockHistoryStore()
  2485. .put(/\/api\/projects\/[^/]+\/blobs\/[0-9a-f]+/)
  2486. .optionally()
  2487. .reply(201)
  2488. const allCapturedChanges = []
  2489. MockHistoryStore()
  2490. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  2491. allCapturedChanges.push(...body)
  2492. return true
  2493. })
  2494. .query(true)
  2495. .times(5)
  2496. .optionally()
  2497. .reply(204)
  2498. MockWeb().post(`/project/${this.project_id}/history/resync`).reply(204)
  2499. await ProjectHistoryClient.resyncHistory(this.project_id)
  2500. await ProjectHistoryClient.pushRawUpdate(this.project_id, {
  2501. projectHistoryId: historyId,
  2502. resyncProjectStructure: {
  2503. docs: [{ path: '/main.tex' }],
  2504. files: [],
  2505. },
  2506. meta: { ts: this.timestamp },
  2507. })
  2508. await ProjectHistoryClient.pushRawUpdate(this.project_id, {
  2509. path: '/main.tex',
  2510. projectHistoryId: historyId,
  2511. resyncDocContent: {
  2512. content: expected.content,
  2513. historyOTRanges: {
  2514. comments: [],
  2515. trackedChanges: expected.trackedChanges,
  2516. },
  2517. },
  2518. doc: this.doc_id,
  2519. meta: { ts: this.timestamp },
  2520. })
  2521. await ProjectHistoryClient.flushProject(this.project_id)
  2522. const fileData = new StringFileData(
  2523. persisted.content,
  2524. [],
  2525. persisted.trackedChanges
  2526. )
  2527. const snapshot = new Snapshot(
  2528. new FileMap({ 'main.tex': new File(fileData) })
  2529. )
  2530. for (const rawChange of allCapturedChanges) {
  2531. Change.fromRaw(rawChange).applyTo(snapshot)
  2532. }
  2533. const file = snapshot.getFile('main.tex')
  2534. assert.strictEqual(file.getContent(), expected.content, 'content')
  2535. assert.deepStrictEqual(
  2536. file.getTrackedChanges().toRaw(),
  2537. expected.trackedChanges,
  2538. `TC mismatch.\n` +
  2539. ` expected: ${JSON.stringify(expected.trackedChanges)}\n` +
  2540. ` actual: ${JSON.stringify(file.getTrackedChanges().toRaw())}`
  2541. )
  2542. })
  2543. })
  2544. })