SyncTests.js 88 KB

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