SendingUpdatesTests.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197
  1. import { expect } from 'chai'
  2. import Settings from '@overleaf/settings'
  3. import assert from 'node:assert'
  4. import async from 'async'
  5. import crypto from 'node:crypto'
  6. import mongodb from 'mongodb-legacy'
  7. import nock from 'nock'
  8. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  9. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  10. const { ObjectId } = mongodb
  11. const MockHistoryStore = () => nock('http://127.0.0.1:3100')
  12. const MockFileStore = () => nock('http://127.0.0.1:3009')
  13. const MockWeb = () => nock('http://127.0.0.1:3000')
  14. // Some helper methods to make the tests more compact
  15. function slTextUpdate(historyId, doc, userId, v, ts, op) {
  16. return {
  17. projectHistoryId: historyId,
  18. doc: doc.id,
  19. op,
  20. v,
  21. meta: {
  22. user_id: userId,
  23. ts: ts.getTime(),
  24. pathname: doc.pathname,
  25. doc_length: doc.length,
  26. },
  27. }
  28. }
  29. function slAddDocUpdate(historyId, doc, userId, ts, docLines, ranges = {}) {
  30. return {
  31. projectHistoryId: historyId,
  32. pathname: doc.pathname,
  33. ranges,
  34. docLines,
  35. doc: doc.id,
  36. meta: { user_id: userId, ts: ts.getTime() },
  37. }
  38. }
  39. function slAddDocUpdateWithVersion(
  40. historyId,
  41. doc,
  42. userId,
  43. ts,
  44. docLines,
  45. projectVersion,
  46. ranges = {}
  47. ) {
  48. const result = slAddDocUpdate(historyId, doc, userId, ts, docLines, ranges)
  49. result.version = projectVersion
  50. return result
  51. }
  52. function slAddFileUpdate(historyId, file, userId, ts, projectId) {
  53. return {
  54. projectHistoryId: historyId,
  55. pathname: file.pathname,
  56. url: `http://127.0.0.1:3009/project/${projectId}/file/${file.id}`,
  57. file: file.id,
  58. ranges: undefined,
  59. meta: { user_id: userId, ts: ts.getTime() },
  60. }
  61. }
  62. function createdBlobFileUpdate(historyId, file, userId, ts, projectId) {
  63. return {
  64. projectHistoryId: historyId,
  65. pathname: file.pathname,
  66. createdBlob: true,
  67. url: null,
  68. file: file.id,
  69. hash: file.hash,
  70. ranges: undefined,
  71. meta: { user_id: userId, ts: ts.getTime() },
  72. }
  73. }
  74. function slRenameUpdate(historyId, doc, userId, ts, pathname, newPathname) {
  75. return {
  76. projectHistoryId: historyId,
  77. pathname,
  78. new_pathname: newPathname,
  79. doc: doc.id,
  80. meta: { user_id: userId, ts: ts.getTime() },
  81. }
  82. }
  83. function olUpdate(doc, userId, ts, operations, v) {
  84. return {
  85. v2Authors: [userId],
  86. timestamp: ts.toJSON(),
  87. authors: [],
  88. operations,
  89. v2DocVersions: {
  90. [doc.id]: {
  91. pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
  92. v: v || 1,
  93. },
  94. },
  95. }
  96. }
  97. function olTextOperation(doc, textOperation) {
  98. return {
  99. pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
  100. textOperation,
  101. }
  102. }
  103. function olAddCommentOperation(doc, commentId, pos, length) {
  104. return {
  105. pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
  106. commentId,
  107. ranges: [{ pos, length }],
  108. }
  109. }
  110. function olTextUpdate(doc, userId, ts, textOperation, v) {
  111. return olUpdate(doc, userId, ts, [olTextOperation(doc, textOperation)], v)
  112. }
  113. function olTextUpdates(doc, userId, ts, textOperations, v) {
  114. return olUpdate(
  115. doc,
  116. userId,
  117. ts,
  118. textOperations.map(textOperation => olTextOperation(doc, textOperation)),
  119. v
  120. )
  121. }
  122. function olRenameUpdate(doc, userId, ts, pathname, newPathname) {
  123. return {
  124. v2Authors: [userId],
  125. timestamp: ts.toJSON(),
  126. authors: [],
  127. operations: [
  128. {
  129. pathname,
  130. newPathname,
  131. },
  132. ],
  133. }
  134. }
  135. function olAddDocUpdate(doc, userId, ts, fileHash, rangesHash = undefined) {
  136. const update = {
  137. v2Authors: [userId],
  138. timestamp: ts.toJSON(),
  139. authors: [],
  140. operations: [
  141. {
  142. pathname: doc.pathname.replace(/^\//, ''), // Strip leading /
  143. file: {
  144. hash: fileHash,
  145. },
  146. },
  147. ],
  148. }
  149. if (rangesHash) {
  150. update.operations[0].file.rangesHash = rangesHash
  151. }
  152. return update
  153. }
  154. function olAddDocUpdateWithVersion(
  155. doc,
  156. userId,
  157. ts,
  158. fileHash,
  159. version,
  160. rangesHash = undefined
  161. ) {
  162. const result = olAddDocUpdate(doc, userId, ts, fileHash, rangesHash)
  163. result.projectVersion = version
  164. return result
  165. }
  166. function olAddFileUpdate(file, userId, ts, fileHash) {
  167. return {
  168. v2Authors: [userId],
  169. timestamp: ts.toJSON(),
  170. authors: [],
  171. operations: [
  172. {
  173. pathname: file.pathname.replace(/^\//, ''), // Strip leading /
  174. file: {
  175. hash: fileHash,
  176. },
  177. },
  178. ],
  179. }
  180. }
  181. describe('Sending Updates', function () {
  182. const historyId = new ObjectId().toString()
  183. beforeEach(function (done) {
  184. this.timestamp = new Date()
  185. ProjectHistoryApp.ensureRunning(error => {
  186. if (error) {
  187. return done(error)
  188. }
  189. this.userId = new ObjectId().toString()
  190. this.projectId = new ObjectId().toString()
  191. this.docId = new ObjectId().toString()
  192. this.doc = {
  193. id: this.docId,
  194. pathname: '/main.tex',
  195. length: 5,
  196. }
  197. MockHistoryStore().post('/api/projects').reply(200, {
  198. projectId: historyId,
  199. })
  200. MockWeb()
  201. .get(`/project/${this.projectId}/details`)
  202. .reply(200, {
  203. name: 'Test Project',
  204. overleaf: {
  205. history: {
  206. id: historyId,
  207. },
  208. },
  209. })
  210. ProjectHistoryClient.initializeProject(historyId, done)
  211. })
  212. })
  213. afterEach(function () {
  214. nock.cleanAll()
  215. })
  216. describe('basic update types', function () {
  217. beforeEach(function () {
  218. MockHistoryStore()
  219. .get(`/api/projects/${historyId}/latest/history`)
  220. .reply(200, {
  221. chunk: {
  222. startVersion: 0,
  223. history: {
  224. snapshot: {},
  225. changes: [],
  226. },
  227. },
  228. })
  229. })
  230. it('should send add doc updates to the history store', function (done) {
  231. const fileHash = '0a207c060e61f3b88eaee0a8cd0696f46fb155eb'
  232. const createBlob = MockHistoryStore()
  233. .put(`/api/projects/${historyId}/blobs/${fileHash}`, 'a\nb')
  234. .reply(201)
  235. const addFile = MockHistoryStore()
  236. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  237. expect(body).to.deep.equal([
  238. olAddDocUpdate(this.doc, this.userId, this.timestamp, fileHash),
  239. ])
  240. return true
  241. })
  242. .query({ end_version: 0 })
  243. .reply(204)
  244. async.series(
  245. [
  246. cb => {
  247. ProjectHistoryClient.pushRawUpdate(
  248. this.projectId,
  249. slAddDocUpdate(
  250. historyId,
  251. this.doc,
  252. this.userId,
  253. this.timestamp,
  254. 'a\nb'
  255. ),
  256. cb
  257. )
  258. },
  259. cb => {
  260. ProjectHistoryClient.flushProject(this.projectId, cb)
  261. },
  262. ],
  263. error => {
  264. if (error) {
  265. return done(error)
  266. }
  267. assert(
  268. createBlob.isDone(),
  269. '/api/projects/:historyId/blobs/:hash should have been called'
  270. )
  271. assert(
  272. addFile.isDone(),
  273. `/api/projects/${historyId}/changes should have been called`
  274. )
  275. done()
  276. }
  277. )
  278. })
  279. it('should send ranges to the history store', function (done) {
  280. const fileHash = '49e886093b3eacbc12b99a1eb5aeaa44a6b9d90e'
  281. const rangesHash = 'fa9a429ff518bc9e5b2507a96ff0646b566eca65'
  282. const historyRanges = {
  283. trackedChanges: [
  284. {
  285. range: { pos: 4, length: 3 },
  286. tracking: {
  287. type: 'delete',
  288. userId: 'user-id-1',
  289. ts: '2024-01-01T00:00:00.000Z',
  290. },
  291. },
  292. ],
  293. comments: [
  294. {
  295. ranges: [{ pos: 0, length: 3 }],
  296. id: 'comment-id-1',
  297. },
  298. ],
  299. }
  300. // We need to set up the ranges mock first, as we will call it last..
  301. const createRangesBlob = MockHistoryStore()
  302. .put(`/api/projects/${historyId}/blobs/${rangesHash}`, historyRanges)
  303. .reply(201)
  304. const createBlob = MockHistoryStore()
  305. .put(`/api/projects/${historyId}/blobs/${fileHash}`, 'foo barbaz')
  306. .reply(201)
  307. const addFile = MockHistoryStore()
  308. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  309. expect(body).to.deep.equal([
  310. olAddDocUpdate(
  311. this.doc,
  312. this.userId,
  313. this.timestamp,
  314. fileHash,
  315. rangesHash
  316. ),
  317. ])
  318. return true
  319. })
  320. .query({ end_version: 0 })
  321. .reply(204)
  322. async.series(
  323. [
  324. cb => {
  325. ProjectHistoryClient.pushRawUpdate(
  326. this.projectId,
  327. slAddDocUpdate(
  328. historyId,
  329. this.doc,
  330. this.userId,
  331. this.timestamp,
  332. 'foo barbaz',
  333. {
  334. changes: [
  335. {
  336. op: { p: 4, d: 'bar' },
  337. metadata: {
  338. ts: 1704067200000,
  339. user_id: 'user-id-1',
  340. },
  341. },
  342. ],
  343. comments: [
  344. {
  345. op: {
  346. p: 0,
  347. c: 'foo',
  348. t: 'comment-id-1',
  349. },
  350. metadata: { resolved: false },
  351. },
  352. ],
  353. }
  354. ),
  355. cb
  356. )
  357. },
  358. cb => {
  359. ProjectHistoryClient.flushProject(this.projectId, cb)
  360. },
  361. ],
  362. error => {
  363. if (error) {
  364. return done(error)
  365. }
  366. assert(
  367. createBlob.isDone(),
  368. '/api/projects/:historyId/blobs/:hash should have been called to create content blob'
  369. )
  370. assert(
  371. createRangesBlob.isDone(),
  372. '/api/projects/:historyId/blobs/:hash should have been called to create ranges blob'
  373. )
  374. assert(
  375. addFile.isDone(),
  376. `/api/projects/${historyId}/changes should have been called`
  377. )
  378. done()
  379. }
  380. )
  381. })
  382. it('should strip non-BMP characters in add doc updates before sending to the history store', function (done) {
  383. const fileHash = '11509fe05a41f9cdc51ea081342b5a4fc7c8d0fc'
  384. const createBlob = MockHistoryStore()
  385. .put(
  386. `/api/projects/${historyId}/blobs/${fileHash}`,
  387. 'a\nb\uFFFD\uFFFDc'
  388. )
  389. .reply(201)
  390. const addFile = MockHistoryStore()
  391. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  392. expect(body).to.deep.equal([
  393. olAddDocUpdate(this.doc, this.userId, this.timestamp, fileHash),
  394. ])
  395. return true
  396. })
  397. .query({ end_version: 0 })
  398. .reply(204)
  399. async.series(
  400. [
  401. cb => {
  402. ProjectHistoryClient.pushRawUpdate(
  403. this.projectId,
  404. slAddDocUpdate(
  405. historyId,
  406. this.doc,
  407. this.userId,
  408. this.timestamp,
  409. 'a\nb\uD800\uDC00c'
  410. ),
  411. cb
  412. )
  413. },
  414. cb => {
  415. ProjectHistoryClient.flushProject(this.projectId, cb)
  416. },
  417. ],
  418. error => {
  419. if (error) {
  420. return done(error)
  421. }
  422. assert(
  423. createBlob.isDone(),
  424. '/api/projects/:historyId/blobs/:hash should have been called'
  425. )
  426. assert(
  427. addFile.isDone(),
  428. `/api/projects/${historyId}/changes should have been called`
  429. )
  430. done()
  431. }
  432. )
  433. })
  434. it('should send text updates to the history store', function (done) {
  435. const createChange = MockHistoryStore()
  436. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  437. expect(body).to.deep.equal([
  438. olTextUpdate(this.doc, this.userId, this.timestamp, [3, '\nc', 2]),
  439. ])
  440. return true
  441. })
  442. .query({ end_version: 0 })
  443. .reply(204)
  444. async.series(
  445. [
  446. cb => {
  447. ProjectHistoryClient.pushRawUpdate(
  448. this.projectId,
  449. slTextUpdate(
  450. historyId,
  451. this.doc,
  452. this.userId,
  453. 1,
  454. this.timestamp,
  455. [{ p: 3, i: '\nc' }]
  456. ),
  457. cb
  458. )
  459. },
  460. cb => {
  461. ProjectHistoryClient.flushProject(this.projectId, cb)
  462. },
  463. ],
  464. error => {
  465. if (error) {
  466. return done(error)
  467. }
  468. assert(
  469. createChange.isDone(),
  470. `/api/projects/${historyId}/changes should have been called`
  471. )
  472. done()
  473. }
  474. )
  475. })
  476. it('should send renames to the history store', function (done) {
  477. const createChange = MockHistoryStore()
  478. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  479. expect(body).to.deep.equal([
  480. olRenameUpdate(
  481. this.doc,
  482. this.userId,
  483. this.timestamp,
  484. 'main.tex',
  485. 'main2.tex'
  486. ),
  487. ])
  488. return true
  489. })
  490. .query({ end_version: 0 })
  491. .reply(204)
  492. async.series(
  493. [
  494. cb => {
  495. ProjectHistoryClient.pushRawUpdate(
  496. this.projectId,
  497. slRenameUpdate(
  498. historyId,
  499. this.doc,
  500. this.userId,
  501. this.timestamp,
  502. '/main.tex',
  503. '/main2.tex'
  504. ),
  505. cb
  506. )
  507. },
  508. cb => {
  509. ProjectHistoryClient.flushProject(this.projectId, cb)
  510. },
  511. ],
  512. error => {
  513. if (error) {
  514. return done(error)
  515. }
  516. assert(
  517. createChange.isDone(),
  518. `/api/projects/${historyId}/changes should have been called`
  519. )
  520. done()
  521. }
  522. )
  523. })
  524. it('should not get file from filestore if no url provided', function (done) {
  525. const file = {
  526. id: new ObjectId().toString(),
  527. pathname: '/test.png',
  528. contents: Buffer.from([1, 2, 3]),
  529. hash: 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74',
  530. }
  531. const fileStoreRequest = MockFileStore()
  532. .get(`/project/${this.projectId}/file/${file.id}`)
  533. .reply(200, file.contents)
  534. const checkBlob = MockHistoryStore()
  535. .head(`/api/projects/${historyId}/blobs/${file.hash}`)
  536. .reply(200)
  537. const addFile = MockHistoryStore()
  538. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  539. expect(body).to.deep.equal([
  540. olAddFileUpdate(file, this.userId, this.timestamp, file.hash),
  541. ])
  542. return true
  543. })
  544. .query({ end_version: 0 })
  545. .reply(204)
  546. async.series(
  547. [
  548. cb => {
  549. ProjectHistoryClient.pushRawUpdate(
  550. this.projectId,
  551. createdBlobFileUpdate(
  552. historyId,
  553. file,
  554. this.userId,
  555. this.timestamp,
  556. this.projectId
  557. ),
  558. cb
  559. )
  560. },
  561. cb => {
  562. ProjectHistoryClient.flushProject(this.projectId, cb)
  563. },
  564. ],
  565. error => {
  566. if (error) {
  567. return done(error)
  568. }
  569. assert(
  570. !fileStoreRequest.isDone(),
  571. 'filestore should not have been called'
  572. )
  573. assert(
  574. checkBlob.isDone(),
  575. `HEAD /api/projects/${historyId}/blobs/${file.hash} should have been called`
  576. )
  577. assert(
  578. addFile.isDone(),
  579. `/api/projects/${historyId}/latest/files should have been called`
  580. )
  581. done()
  582. }
  583. )
  584. })
  585. it('should send add file updates to the history store', function (done) {
  586. const file = {
  587. id: new ObjectId().toString(),
  588. pathname: '/test.png',
  589. contents: Buffer.from([1, 2, 3]),
  590. hash: 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74',
  591. }
  592. const fileStoreRequest = MockFileStore()
  593. .get(`/project/${this.projectId}/file/${file.id}`)
  594. .reply(200, file.contents)
  595. const createBlob = MockHistoryStore()
  596. .put(`/api/projects/${historyId}/blobs/${file.hash}`, file.contents)
  597. .reply(201)
  598. const addFile = MockHistoryStore()
  599. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  600. expect(body).to.deep.equal([
  601. olAddFileUpdate(file, this.userId, this.timestamp, file.hash),
  602. ])
  603. return true
  604. })
  605. .query({ end_version: 0 })
  606. .reply(204)
  607. async.series(
  608. [
  609. cb => {
  610. ProjectHistoryClient.pushRawUpdate(
  611. this.projectId,
  612. slAddFileUpdate(
  613. historyId,
  614. file,
  615. this.userId,
  616. this.timestamp,
  617. this.projectId
  618. ),
  619. cb
  620. )
  621. },
  622. cb => {
  623. ProjectHistoryClient.flushProject(this.projectId, cb)
  624. },
  625. ],
  626. error => {
  627. if (error) {
  628. return done(error)
  629. }
  630. assert(
  631. fileStoreRequest.isDone(),
  632. `/project/${this.projectId}/file/${file.id} should have been called`
  633. )
  634. assert(
  635. createBlob.isDone(),
  636. `/api/projects/${historyId}/latest/files should have been called`
  637. )
  638. assert(
  639. addFile.isDone(),
  640. `/api/projects/${historyId}/latest/files should have been called`
  641. )
  642. done()
  643. }
  644. )
  645. })
  646. it('should send a stub to the history store when the file is large', function (done) {
  647. const fileContents = Buffer.alloc(Settings.maxFileSizeInBytes + 1, 'X')
  648. const fileSize = Buffer.byteLength(fileContents)
  649. const fileHash = crypto
  650. .createHash('sha1')
  651. .update('blob ' + fileSize + '\x00')
  652. .update(fileContents, 'utf8')
  653. .digest('hex')
  654. const file = {
  655. id: new ObjectId().toString(),
  656. pathname: '/large.png',
  657. contents: fileContents,
  658. hash: fileHash,
  659. }
  660. const stubContents = [
  661. 'FileTooLargeError v1',
  662. 'File too large to be stored in history service',
  663. `id project-${this.projectId}-file-${file.id}`,
  664. `size ${fileSize} bytes`,
  665. `hash ${fileHash}`,
  666. '\0', // null byte to make this a binary file
  667. ].join('\n')
  668. const stubHash = crypto
  669. .createHash('sha1')
  670. .update('blob ' + Buffer.byteLength(stubContents) + '\x00')
  671. .update(stubContents, 'utf8')
  672. .digest('hex')
  673. const stub = {
  674. id: file.id,
  675. pathname: file.pathname,
  676. contents: stubContents,
  677. hash: stubHash,
  678. }
  679. const fileStoreRequest = MockFileStore()
  680. .get(`/project/${this.projectId}/file/${file.id}`)
  681. .reply(200, file.contents)
  682. const createBlob = MockHistoryStore()
  683. .put(`/api/projects/${historyId}/blobs/${stub.hash}`, stub.contents)
  684. .reply(201)
  685. const addFile = MockHistoryStore()
  686. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  687. expect(body).to.deep.equal([
  688. olAddFileUpdate(stub, this.userId, this.timestamp, stub.hash),
  689. ])
  690. return true
  691. })
  692. .query({ end_version: 0 })
  693. .reply(204)
  694. async.series(
  695. [
  696. cb => {
  697. ProjectHistoryClient.pushRawUpdate(
  698. this.projectId,
  699. slAddFileUpdate(
  700. historyId,
  701. file,
  702. this.userId,
  703. this.timestamp,
  704. this.projectId
  705. ),
  706. cb
  707. )
  708. },
  709. cb => {
  710. ProjectHistoryClient.flushProject(this.projectId, cb)
  711. },
  712. ],
  713. error => {
  714. if (error) {
  715. return done(error)
  716. }
  717. assert(
  718. addFile.isDone(),
  719. `/api/projects/${historyId}/latest/files should have been called`
  720. )
  721. assert(
  722. createBlob.isDone(),
  723. `/api/projects/${historyId}/latest/files should have been called`
  724. )
  725. assert(
  726. fileStoreRequest.isDone(),
  727. `/project/${this.projectId}/file/${file.id} should have been called`
  728. )
  729. done()
  730. }
  731. )
  732. })
  733. it('should handle comment ops', function (done) {
  734. const createChange = MockHistoryStore()
  735. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  736. expect(body).to.deep.equal([
  737. olUpdate(this.doc, this.userId, this.timestamp, [
  738. olTextOperation(this.doc, [3, '\nc', 2]),
  739. olAddCommentOperation(this.doc, 'comment-id-1', 3, 2),
  740. ]),
  741. olUpdate(
  742. this.doc,
  743. this.userId,
  744. this.timestamp,
  745. [olAddCommentOperation(this.doc, 'comment-id-2', 2, 1)],
  746. 2
  747. ),
  748. ])
  749. return true
  750. })
  751. .query({ end_version: 0 })
  752. .reply(204)
  753. async.series(
  754. [
  755. cb => {
  756. ProjectHistoryClient.pushRawUpdate(
  757. this.projectId,
  758. slTextUpdate(
  759. historyId,
  760. this.doc,
  761. this.userId,
  762. 1,
  763. this.timestamp,
  764. [
  765. { p: 3, i: '\nc' },
  766. { p: 3, c: '\nc', t: 'comment-id-1' },
  767. ]
  768. ),
  769. cb
  770. )
  771. },
  772. cb => {
  773. ProjectHistoryClient.pushRawUpdate(
  774. this.projectId,
  775. slTextUpdate(
  776. historyId,
  777. this.doc,
  778. this.userId,
  779. 2,
  780. this.timestamp,
  781. [{ p: 2, c: 'b', t: 'comment-id-2' }]
  782. ),
  783. cb
  784. )
  785. },
  786. cb => {
  787. ProjectHistoryClient.flushProject(this.projectId, cb)
  788. },
  789. ],
  790. error => {
  791. if (error) {
  792. return done(error)
  793. }
  794. assert(
  795. createChange.isDone(),
  796. `/api/projects/${historyId}/changes should have been called`
  797. )
  798. done()
  799. }
  800. )
  801. })
  802. it('should be able to process lots of updates in batches', function (done) {
  803. const BATCH_SIZE = 500
  804. const createFirstChangeBatch = MockHistoryStore()
  805. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  806. expect(body).to.deep.equal([
  807. olTextUpdate(
  808. this.doc,
  809. this.userId,
  810. this.timestamp,
  811. ['a'.repeat(BATCH_SIZE), 6],
  812. BATCH_SIZE - 1
  813. ),
  814. ])
  815. return true
  816. })
  817. .query({ end_version: 0 })
  818. .reply(204)
  819. const createSecondChangeBatch = MockHistoryStore()
  820. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  821. expect(body).to.deep.equal([
  822. olTextUpdate(
  823. this.doc,
  824. this.userId,
  825. this.timestamp,
  826. ['a'.repeat(50), BATCH_SIZE + 6],
  827. BATCH_SIZE - 1 + 50
  828. ),
  829. ])
  830. return true
  831. })
  832. .query({ end_version: 500 })
  833. .reply(204)
  834. // these need mocking again for the second batch
  835. MockHistoryStore()
  836. .get(`/api/projects/${historyId}/latest/history`)
  837. .reply(200, {
  838. chunk: {
  839. startVersion: BATCH_SIZE,
  840. history: {
  841. snapshot: {},
  842. changes: [],
  843. },
  844. },
  845. })
  846. MockWeb()
  847. .get(`/project/${this.projectId}/details`)
  848. .reply(200, {
  849. name: 'Test Project',
  850. overleaf: {
  851. history: {
  852. id: historyId,
  853. },
  854. },
  855. })
  856. const pushChange = (n, cb) => {
  857. this.doc.length += 1
  858. ProjectHistoryClient.pushRawUpdate(
  859. this.projectId,
  860. slTextUpdate(historyId, this.doc, this.userId, n, this.timestamp, [
  861. { p: 0, i: 'a' },
  862. ]),
  863. cb
  864. )
  865. }
  866. async.series(
  867. [
  868. cb => {
  869. async.times(BATCH_SIZE + 50, pushChange, cb)
  870. },
  871. cb => {
  872. ProjectHistoryClient.flushProject(this.projectId, cb)
  873. },
  874. ],
  875. error => {
  876. if (error) {
  877. return done(error)
  878. }
  879. assert(
  880. createFirstChangeBatch.isDone(),
  881. `/api/projects/${historyId}/changes should have been called for the first batch`
  882. )
  883. assert(
  884. createSecondChangeBatch.isDone(),
  885. `/api/projects/${historyId}/changes should have been called for the second batch`
  886. )
  887. done()
  888. }
  889. )
  890. })
  891. })
  892. describe('compressing updates', function () {
  893. beforeEach(function () {
  894. MockHistoryStore()
  895. .get(`/api/projects/${historyId}/latest/history`)
  896. .reply(200, {
  897. chunk: {
  898. startVersion: 0,
  899. history: {
  900. snapshot: {},
  901. changes: [],
  902. },
  903. },
  904. })
  905. })
  906. it('should concat adjacent text updates', function (done) {
  907. const createChange = MockHistoryStore()
  908. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  909. expect(body).to.deep.equal([
  910. olTextUpdate(
  911. this.doc,
  912. this.userId,
  913. this.timestamp,
  914. [3, 'foobaz', 2],
  915. 2
  916. ),
  917. ])
  918. return true
  919. })
  920. .query({ end_version: 0 })
  921. .reply(204)
  922. async.series(
  923. [
  924. cb => {
  925. ProjectHistoryClient.pushRawUpdate(
  926. this.projectId,
  927. slTextUpdate(
  928. historyId,
  929. this.doc,
  930. this.userId,
  931. 1,
  932. this.timestamp,
  933. [
  934. { p: 3, i: 'foobar' },
  935. { p: 6, d: 'bar' },
  936. ]
  937. ),
  938. cb
  939. )
  940. this.doc.length += 3
  941. },
  942. cb => {
  943. ProjectHistoryClient.pushRawUpdate(
  944. this.projectId,
  945. slTextUpdate(
  946. historyId,
  947. this.doc,
  948. this.userId,
  949. 2,
  950. this.timestamp,
  951. [{ p: 6, i: 'baz' }]
  952. ),
  953. cb
  954. )
  955. },
  956. cb => {
  957. ProjectHistoryClient.flushProject(this.projectId, cb)
  958. },
  959. ],
  960. error => {
  961. if (error) {
  962. return done(error)
  963. }
  964. assert(
  965. createChange.isDone(),
  966. `/api/projects/${historyId}/changes should have been called`
  967. )
  968. done()
  969. }
  970. )
  971. })
  972. it('should take the timestamp of the first update', function (done) {
  973. const timestamp1 = new Date(this.timestamp)
  974. const timestamp2 = new Date(this.timestamp.getTime() + 10000)
  975. const createChange = MockHistoryStore()
  976. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  977. expect(body).to.deep.equal([
  978. olTextUpdate(
  979. this.doc,
  980. this.userId,
  981. timestamp1,
  982. [3, 'foobaz', 2],
  983. 2
  984. ),
  985. ])
  986. return true
  987. })
  988. .query({ end_version: 0 })
  989. .reply(204)
  990. async.series(
  991. [
  992. cb => {
  993. ProjectHistoryClient.pushRawUpdate(
  994. this.projectId,
  995. slTextUpdate(historyId, this.doc, this.userId, 1, timestamp1, [
  996. { p: 3, i: 'foo' },
  997. ]),
  998. cb
  999. )
  1000. this.doc.length += 3
  1001. },
  1002. cb => {
  1003. ProjectHistoryClient.pushRawUpdate(
  1004. this.projectId,
  1005. slTextUpdate(historyId, this.doc, this.userId, 2, timestamp2, [
  1006. { p: 6, i: 'baz' },
  1007. ]),
  1008. cb
  1009. )
  1010. },
  1011. cb => {
  1012. ProjectHistoryClient.flushProject(this.projectId, cb)
  1013. },
  1014. ],
  1015. error => {
  1016. if (error) {
  1017. return done(error)
  1018. }
  1019. assert(
  1020. createChange.isDone(),
  1021. `/api/projects/${historyId}/changes should have been called`
  1022. )
  1023. done()
  1024. }
  1025. )
  1026. })
  1027. it('should not concat updates more than 60 seconds apart', function (done) {
  1028. const timestamp1 = new Date(this.timestamp)
  1029. const timestamp2 = new Date(this.timestamp.getTime() + 120000)
  1030. const createChange = MockHistoryStore()
  1031. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1032. expect(body).to.deep.equal([
  1033. olTextUpdate(this.doc, this.userId, timestamp1, [3, 'foo', 2], 1),
  1034. olTextUpdate(this.doc, this.userId, timestamp2, [6, 'baz', 2], 2),
  1035. ])
  1036. return true
  1037. })
  1038. .query({ end_version: 0 })
  1039. .reply(204)
  1040. async.series(
  1041. [
  1042. cb => {
  1043. ProjectHistoryClient.pushRawUpdate(
  1044. this.projectId,
  1045. slTextUpdate(historyId, this.doc, this.userId, 1, timestamp1, [
  1046. { p: 3, i: 'foo' },
  1047. ]),
  1048. cb
  1049. )
  1050. this.doc.length += 3
  1051. },
  1052. cb => {
  1053. ProjectHistoryClient.pushRawUpdate(
  1054. this.projectId,
  1055. slTextUpdate(historyId, this.doc, this.userId, 2, timestamp2, [
  1056. { p: 6, i: 'baz' },
  1057. ]),
  1058. cb
  1059. )
  1060. },
  1061. cb => {
  1062. ProjectHistoryClient.flushProject(this.projectId, cb)
  1063. },
  1064. ],
  1065. error => {
  1066. if (error) {
  1067. return done(error)
  1068. }
  1069. assert(
  1070. createChange.isDone(),
  1071. `/api/projects/${historyId}/changes should have been called`
  1072. )
  1073. done()
  1074. }
  1075. )
  1076. })
  1077. it('should not concat updates with different user_ids', function (done) {
  1078. const userId1 = new ObjectId().toString()
  1079. const userId2 = new ObjectId().toString()
  1080. const createChange = MockHistoryStore()
  1081. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1082. expect(body).to.deep.equal([
  1083. olTextUpdate(this.doc, userId1, this.timestamp, [3, 'foo', 2], 1),
  1084. olTextUpdate(this.doc, userId2, this.timestamp, [6, 'baz', 2], 2),
  1085. ])
  1086. return true
  1087. })
  1088. .query({ end_version: 0 })
  1089. .reply(204)
  1090. async.series(
  1091. [
  1092. cb => {
  1093. ProjectHistoryClient.pushRawUpdate(
  1094. this.projectId,
  1095. slTextUpdate(historyId, this.doc, userId1, 1, this.timestamp, [
  1096. { p: 3, i: 'foo' },
  1097. ]),
  1098. cb
  1099. )
  1100. this.doc.length += 3
  1101. },
  1102. cb => {
  1103. ProjectHistoryClient.pushRawUpdate(
  1104. this.projectId,
  1105. slTextUpdate(historyId, this.doc, userId2, 2, this.timestamp, [
  1106. { p: 6, i: 'baz' },
  1107. ]),
  1108. cb
  1109. )
  1110. },
  1111. cb => {
  1112. ProjectHistoryClient.flushProject(this.projectId, cb)
  1113. },
  1114. ],
  1115. error => {
  1116. if (error) {
  1117. return done(error)
  1118. }
  1119. assert(
  1120. createChange.isDone(),
  1121. `/api/projects/${historyId}/changes should have been called`
  1122. )
  1123. done()
  1124. }
  1125. )
  1126. })
  1127. it('should not concat updates with different docs', function (done) {
  1128. const doc1 = {
  1129. id: new ObjectId().toString(),
  1130. pathname: '/doc1.tex',
  1131. length: 10,
  1132. }
  1133. const doc2 = {
  1134. id: new ObjectId().toString(),
  1135. pathname: '/doc2.tex',
  1136. length: 10,
  1137. }
  1138. const createChange = MockHistoryStore()
  1139. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1140. expect(body).to.deep.equal([
  1141. olTextUpdate(doc1, this.userId, this.timestamp, [3, 'foo', 7], 1),
  1142. olTextUpdate(doc2, this.userId, this.timestamp, [6, 'baz', 4], 2),
  1143. ])
  1144. return true
  1145. })
  1146. .query({ end_version: 0 })
  1147. .reply(204)
  1148. async.series(
  1149. [
  1150. cb => {
  1151. ProjectHistoryClient.pushRawUpdate(
  1152. this.projectId,
  1153. slTextUpdate(historyId, doc1, this.userId, 1, this.timestamp, [
  1154. { p: 3, i: 'foo' },
  1155. ]),
  1156. cb
  1157. )
  1158. },
  1159. cb => {
  1160. ProjectHistoryClient.pushRawUpdate(
  1161. this.projectId,
  1162. slTextUpdate(historyId, doc2, this.userId, 2, this.timestamp, [
  1163. { p: 6, i: 'baz' },
  1164. ]),
  1165. cb
  1166. )
  1167. },
  1168. cb => {
  1169. ProjectHistoryClient.flushProject(this.projectId, cb)
  1170. },
  1171. ],
  1172. error => {
  1173. if (error) {
  1174. return done(error)
  1175. }
  1176. assert(
  1177. createChange.isDone(),
  1178. `/api/projects/${historyId}/changes should have been called`
  1179. )
  1180. done()
  1181. }
  1182. )
  1183. })
  1184. it('should not send updates without any ops', function (done) {
  1185. const createChange = MockHistoryStore()
  1186. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1187. expect(body).to.deep.equal([])
  1188. return true
  1189. })
  1190. .query({ end_version: 0 })
  1191. .reply(204)
  1192. async.series(
  1193. [
  1194. cb => {
  1195. // These blank ops can get sent by doc-updater on setDocs from Dropbox that don't change anything
  1196. ProjectHistoryClient.pushRawUpdate(
  1197. this.projectId,
  1198. slTextUpdate(
  1199. historyId,
  1200. this.doc,
  1201. this.userId,
  1202. 1,
  1203. this.timestamp,
  1204. []
  1205. ),
  1206. cb
  1207. )
  1208. },
  1209. cb => {
  1210. ProjectHistoryClient.flushProject(this.projectId, cb)
  1211. },
  1212. ],
  1213. error => {
  1214. if (error) {
  1215. return done(error)
  1216. }
  1217. assert(
  1218. !createChange.isDone(),
  1219. `/api/projects/${historyId}/changes should not have been called`
  1220. )
  1221. done()
  1222. }
  1223. )
  1224. })
  1225. it('should not send ops that compress to nothing', function (done) {
  1226. const createChange = MockHistoryStore()
  1227. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1228. expect(body).to.deep.equal([])
  1229. return true
  1230. })
  1231. .query({ end_version: 0 })
  1232. .reply(204)
  1233. async.series(
  1234. [
  1235. cb => {
  1236. ProjectHistoryClient.pushRawUpdate(
  1237. this.projectId,
  1238. slTextUpdate(
  1239. historyId,
  1240. this.doc,
  1241. this.userId,
  1242. 1,
  1243. this.timestamp,
  1244. [{ i: 'foo', p: 3 }]
  1245. ),
  1246. cb
  1247. )
  1248. this.doc.length += 3
  1249. },
  1250. cb => {
  1251. ProjectHistoryClient.pushRawUpdate(
  1252. this.projectId,
  1253. slTextUpdate(
  1254. historyId,
  1255. this.doc,
  1256. this.userId,
  1257. 2,
  1258. this.timestamp,
  1259. [{ d: 'foo', p: 3 }]
  1260. ),
  1261. cb
  1262. )
  1263. },
  1264. cb => {
  1265. ProjectHistoryClient.flushProject(this.projectId, cb)
  1266. },
  1267. ],
  1268. error => {
  1269. if (error) {
  1270. return done(error)
  1271. }
  1272. assert(
  1273. !createChange.isDone(),
  1274. `/api/projects/${historyId}/changes should not have been called`
  1275. )
  1276. done()
  1277. }
  1278. )
  1279. })
  1280. it('should not send ops from a diff that are blank', function (done) {
  1281. this.doc.length = 300
  1282. // Test case taken from a real life document where it was generating blank insert and
  1283. // delete ops from a diff, and the blank delete was erroring on the OL history from
  1284. // a text operation like [42, 0, 512], where the 0 was invalid.
  1285. const createChange = MockHistoryStore()
  1286. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1287. expect(body).to.deep.equal([
  1288. olTextUpdates(this.doc, this.userId, this.timestamp, [
  1289. [
  1290. 87,
  1291. -1,
  1292. 67,
  1293. '|l|ll|}\n\\hline',
  1294. -4,
  1295. 30,
  1296. ' \\hline',
  1297. 87,
  1298. ' \\\\ \\hline',
  1299. 24,
  1300. ],
  1301. ]),
  1302. ])
  1303. return true
  1304. })
  1305. .query({ end_version: 0 })
  1306. .reply(204)
  1307. async.series(
  1308. [
  1309. cb => {
  1310. ProjectHistoryClient.pushRawUpdate(
  1311. this.projectId,
  1312. slTextUpdate(
  1313. historyId,
  1314. this.doc,
  1315. this.userId,
  1316. 1,
  1317. this.timestamp,
  1318. [
  1319. {
  1320. p: 73,
  1321. d: '\\begin{table}[h]\n\\centering\n\\caption{My caption}\n\\label{my-label}\n\\begin{tabular}{lll}\n & A & B \\\\\nLiter t up & 2 & 1 \\\\\nLiter Whiskey & 1 & 2 \\\\\nPris pr. liter & 200 & 250\n\\end{tabular}\n\\end{table}',
  1322. },
  1323. {
  1324. p: 73,
  1325. i: '\\begin{table}[]\n\\centering\n\\caption{My caption}\n\\label{my-label}\n\\begin{tabular}{|l|ll|}\n\\hline\n & A & B \\\\ \\hline\nLiter t up & 2 & 1 \\\\\nLiter Whiskey & 1 & 2 \\\\\nPris pr. liter & 200 & 250 \\\\ \\hline\n\\end{tabular}\n\\end{table}',
  1326. },
  1327. ]
  1328. ),
  1329. cb
  1330. )
  1331. },
  1332. cb => {
  1333. ProjectHistoryClient.flushProject(this.projectId, cb)
  1334. },
  1335. ],
  1336. error => {
  1337. if (error) {
  1338. return done(error)
  1339. }
  1340. assert(
  1341. createChange.isDone(),
  1342. `/api/projects/${historyId}/changes should have been called`
  1343. )
  1344. done()
  1345. }
  1346. )
  1347. })
  1348. it('should not concat text updates across project structure ops', function (done) {
  1349. const newDoc = {
  1350. id: new ObjectId().toString(),
  1351. pathname: '/main.tex',
  1352. hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
  1353. docLines: 'a\nb',
  1354. }
  1355. MockHistoryStore()
  1356. .put(`/api/projects/${historyId}/blobs/${newDoc.hash}`)
  1357. .reply(201)
  1358. const createChange = MockHistoryStore()
  1359. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1360. expect(body).to.deep.equal([
  1361. olTextUpdate(
  1362. this.doc,
  1363. this.userId,
  1364. this.timestamp,
  1365. [3, 'foo', 2],
  1366. 1
  1367. ),
  1368. olAddDocUpdate(newDoc, this.userId, this.timestamp, newDoc.hash),
  1369. olTextUpdate(
  1370. this.doc,
  1371. this.userId,
  1372. this.timestamp,
  1373. [6, 'baz', 2],
  1374. 2
  1375. ),
  1376. ])
  1377. return true
  1378. })
  1379. .query({ end_version: 0 })
  1380. .reply(204)
  1381. async.series(
  1382. [
  1383. cb => {
  1384. ProjectHistoryClient.pushRawUpdate(
  1385. this.projectId,
  1386. slTextUpdate(
  1387. historyId,
  1388. this.doc,
  1389. this.userId,
  1390. 1,
  1391. this.timestamp,
  1392. [
  1393. { p: 3, i: 'foobar' },
  1394. { p: 6, d: 'bar' },
  1395. ]
  1396. ),
  1397. cb
  1398. )
  1399. this.doc.length += 3
  1400. },
  1401. cb => {
  1402. ProjectHistoryClient.pushRawUpdate(
  1403. this.projectId,
  1404. slAddDocUpdate(
  1405. historyId,
  1406. newDoc,
  1407. this.userId,
  1408. this.timestamp,
  1409. newDoc.docLines
  1410. ),
  1411. cb
  1412. )
  1413. },
  1414. cb => {
  1415. ProjectHistoryClient.pushRawUpdate(
  1416. this.projectId,
  1417. slTextUpdate(
  1418. historyId,
  1419. this.doc,
  1420. this.userId,
  1421. 2,
  1422. this.timestamp,
  1423. [{ p: 6, i: 'baz' }]
  1424. ),
  1425. cb
  1426. )
  1427. },
  1428. cb => {
  1429. ProjectHistoryClient.flushProject(this.projectId, cb)
  1430. },
  1431. ],
  1432. error => {
  1433. if (error) {
  1434. return done(error)
  1435. }
  1436. assert(
  1437. createChange.isDone(),
  1438. `/api/projects/${historyId}/changes should have been called`
  1439. )
  1440. done()
  1441. }
  1442. )
  1443. })
  1444. it('should track the doc length when splitting ops', function (done) {
  1445. this.doc.length = 10
  1446. const createChange = MockHistoryStore()
  1447. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1448. expect(body).to.deep.equal([
  1449. olTextUpdate(this.doc, this.userId, this.timestamp, [3, -3, 4], 1),
  1450. olTextUpdate(
  1451. this.doc,
  1452. this.userId,
  1453. this.timestamp,
  1454. [3, 'barbaz', 4],
  1455. 2
  1456. ), // This has a base length of 10
  1457. ])
  1458. return true
  1459. })
  1460. .query({ end_version: 0 })
  1461. .reply(204)
  1462. async.series(
  1463. [
  1464. cb => {
  1465. ProjectHistoryClient.pushRawUpdate(
  1466. this.projectId,
  1467. slTextUpdate(
  1468. historyId,
  1469. this.doc,
  1470. this.userId,
  1471. 1,
  1472. this.timestamp,
  1473. [
  1474. { p: 3, d: 'foo' },
  1475. { p: 3, i: 'bar' }, // Make sure the length of the op generated from this is 7, not 10
  1476. ]
  1477. ),
  1478. cb
  1479. )
  1480. },
  1481. cb => {
  1482. ProjectHistoryClient.pushRawUpdate(
  1483. this.projectId,
  1484. slTextUpdate(
  1485. historyId,
  1486. this.doc,
  1487. this.userId,
  1488. 2,
  1489. this.timestamp,
  1490. [{ p: 6, i: 'baz' }]
  1491. ),
  1492. cb
  1493. )
  1494. },
  1495. cb => {
  1496. ProjectHistoryClient.flushProject(this.projectId, cb)
  1497. },
  1498. ],
  1499. error => {
  1500. if (error) {
  1501. return done(error)
  1502. }
  1503. assert(
  1504. createChange.isDone(),
  1505. `/api/projects/${historyId}/changes should have been called`
  1506. )
  1507. done()
  1508. }
  1509. )
  1510. })
  1511. })
  1512. describe('with bad pathnames', function () {
  1513. beforeEach(function () {
  1514. MockHistoryStore()
  1515. .get(`/api/projects/${historyId}/latest/history`)
  1516. .reply(200, {
  1517. chunk: {
  1518. startVersion: 0,
  1519. history: {
  1520. snapshot: {},
  1521. changes: [],
  1522. },
  1523. },
  1524. })
  1525. })
  1526. it('should replace \\ with _ and workaround * in pathnames', function (done) {
  1527. const doc = {
  1528. id: this.doc.id,
  1529. pathname: '\\main.tex',
  1530. hash: 'b07b6b7a27667965f733943737124395c7577bea',
  1531. docLines: 'aaabbbccc',
  1532. length: 9,
  1533. }
  1534. MockHistoryStore()
  1535. .put(`/api/projects/${historyId}/blobs/${doc.hash}`)
  1536. .reply(201)
  1537. const createChange = MockHistoryStore()
  1538. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1539. expect(body).to.deep.equal([
  1540. olAddDocUpdate(
  1541. { id: doc.id, pathname: '_main.tex' },
  1542. this.userId,
  1543. this.timestamp,
  1544. doc.hash
  1545. ),
  1546. olRenameUpdate(
  1547. { id: doc.id, pathname: '_main.tex' },
  1548. this.userId,
  1549. this.timestamp,
  1550. '_main.tex',
  1551. '_main2.tex'
  1552. ),
  1553. olTextUpdate(
  1554. { id: doc.id, pathname: '_main2.tex' },
  1555. this.userId,
  1556. this.timestamp,
  1557. [3, 'foo', 6],
  1558. 2
  1559. ),
  1560. olRenameUpdate(
  1561. { id: doc.id, pathname: '_main2.tex' },
  1562. this.userId,
  1563. this.timestamp,
  1564. '_main2.tex',
  1565. '_main__ASTERISK__.tex'
  1566. ),
  1567. ])
  1568. return true
  1569. })
  1570. .query({ end_version: 0 })
  1571. .reply(204)
  1572. async.series(
  1573. [
  1574. cb => {
  1575. ProjectHistoryClient.pushRawUpdate(
  1576. this.projectId,
  1577. slAddDocUpdate(
  1578. historyId,
  1579. doc,
  1580. this.userId,
  1581. this.timestamp,
  1582. doc.docLines
  1583. ),
  1584. cb
  1585. )
  1586. },
  1587. cb => {
  1588. ProjectHistoryClient.pushRawUpdate(
  1589. this.projectId,
  1590. slRenameUpdate(
  1591. historyId,
  1592. doc,
  1593. this.userId,
  1594. this.timestamp,
  1595. '/\\main.tex',
  1596. '/\\main2.tex'
  1597. ),
  1598. cb
  1599. )
  1600. doc.pathname = '\\main2.tex'
  1601. },
  1602. cb => {
  1603. ProjectHistoryClient.pushRawUpdate(
  1604. this.projectId,
  1605. slTextUpdate(historyId, doc, this.userId, 2, this.timestamp, [
  1606. { p: 3, i: 'foo' },
  1607. ]),
  1608. cb
  1609. )
  1610. },
  1611. cb => {
  1612. ProjectHistoryClient.pushRawUpdate(
  1613. this.projectId,
  1614. slRenameUpdate(
  1615. historyId,
  1616. doc,
  1617. this.userId,
  1618. this.timestamp,
  1619. '/\\main2.tex',
  1620. '/\\main*.tex'
  1621. ),
  1622. cb
  1623. )
  1624. doc.pathname = '\\main*.tex'
  1625. },
  1626. cb => {
  1627. ProjectHistoryClient.flushProject(this.projectId, cb)
  1628. },
  1629. ],
  1630. error => {
  1631. if (error) {
  1632. return done(error)
  1633. }
  1634. assert(
  1635. createChange.isDone(),
  1636. `/api/projects/${historyId}/changes should have been called`
  1637. )
  1638. done()
  1639. }
  1640. )
  1641. })
  1642. it('should workaround pathnames beginning with spaces', function (done) {
  1643. const doc = {
  1644. id: this.doc.id,
  1645. pathname: 'main.tex',
  1646. hash: 'b07b6b7a27667965f733943737124395c7577bea',
  1647. docLines: 'aaabbbccc',
  1648. length: 9,
  1649. }
  1650. MockHistoryStore()
  1651. .put(`/api/projects/${historyId}/blobs/${doc.hash}`)
  1652. .reply(201)
  1653. const createChange = MockHistoryStore()
  1654. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1655. expect(body).to.deep.equal([
  1656. olAddDocUpdate(
  1657. { id: doc.id, pathname: 'main.tex' },
  1658. this.userId,
  1659. this.timestamp,
  1660. doc.hash
  1661. ),
  1662. olRenameUpdate(
  1663. { id: doc.id },
  1664. this.userId,
  1665. this.timestamp,
  1666. 'main.tex',
  1667. 'foo/__SPACE__main.tex'
  1668. ),
  1669. ])
  1670. return true
  1671. })
  1672. .query({ end_version: 0 })
  1673. .reply(204)
  1674. async.series(
  1675. [
  1676. cb => {
  1677. ProjectHistoryClient.pushRawUpdate(
  1678. this.projectId,
  1679. slAddDocUpdate(
  1680. historyId,
  1681. doc,
  1682. this.userId,
  1683. this.timestamp,
  1684. doc.docLines
  1685. ),
  1686. cb
  1687. )
  1688. },
  1689. cb => {
  1690. ProjectHistoryClient.pushRawUpdate(
  1691. this.projectId,
  1692. slRenameUpdate(
  1693. historyId,
  1694. doc,
  1695. this.userId,
  1696. this.timestamp,
  1697. '/main.tex',
  1698. '/foo/ main.tex'
  1699. ),
  1700. cb
  1701. )
  1702. doc.pathname = '/foo/ main.tex'
  1703. },
  1704. cb => {
  1705. ProjectHistoryClient.flushProject(this.projectId, cb)
  1706. },
  1707. ],
  1708. error => {
  1709. if (error) {
  1710. return done(error)
  1711. }
  1712. assert(
  1713. createChange.isDone(),
  1714. `/api/projects/${historyId}/changes should have been called`
  1715. )
  1716. done()
  1717. }
  1718. )
  1719. })
  1720. })
  1721. describe('with bad response from filestore', function () {
  1722. beforeEach(function () {
  1723. MockHistoryStore()
  1724. .get(`/api/projects/${historyId}/latest/history`)
  1725. .reply(200, {
  1726. chunk: {
  1727. startVersion: 0,
  1728. history: {
  1729. snapshot: {},
  1730. changes: [],
  1731. },
  1732. },
  1733. })
  1734. })
  1735. it('should return a 500 if the filestore returns a 500', function (done) {
  1736. const file = {
  1737. id: new ObjectId().toString(),
  1738. pathname: '/test.png',
  1739. contents: Buffer.from([1, 2, 3]),
  1740. hash: 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74',
  1741. }
  1742. const fileStoreRequest = MockFileStore()
  1743. .get(`/project/${this.projectId}/file/${file.id}`)
  1744. .reply(500)
  1745. const createBlob = MockHistoryStore()
  1746. .put(`/api/projects/${historyId}/blobs/${file.hash}`, file.contents)
  1747. .reply(201)
  1748. const addFile = MockHistoryStore()
  1749. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1750. expect(body).to.deep.equal([
  1751. olAddFileUpdate(file, this.userId, this.timestamp, file.hash),
  1752. ])
  1753. return true
  1754. })
  1755. .query({ end_version: 0 })
  1756. .reply(204)
  1757. async.series(
  1758. [
  1759. cb => {
  1760. ProjectHistoryClient.pushRawUpdate(
  1761. this.projectId,
  1762. slAddFileUpdate(
  1763. historyId,
  1764. file,
  1765. this.userId,
  1766. this.timestamp,
  1767. this.projectId
  1768. ),
  1769. cb
  1770. )
  1771. },
  1772. cb => {
  1773. ProjectHistoryClient.flushProject(
  1774. this.projectId,
  1775. { allowErrors: true },
  1776. (error, res) => {
  1777. if (error) {
  1778. return cb(error)
  1779. }
  1780. expect(res.statusCode).to.equal(500)
  1781. cb()
  1782. }
  1783. )
  1784. },
  1785. ],
  1786. error => {
  1787. if (error) {
  1788. return done(error)
  1789. }
  1790. assert(
  1791. fileStoreRequest.isDone(),
  1792. `/project/${this.projectId}/file/${file.id} should have been called`
  1793. )
  1794. assert(
  1795. !createBlob.isDone(),
  1796. `/api/projects/${historyId}/latest/files should not have been called`
  1797. )
  1798. assert(
  1799. !addFile.isDone(),
  1800. `/api/projects/${historyId}/latest/files should not have been called`
  1801. )
  1802. done()
  1803. }
  1804. )
  1805. })
  1806. it('should return a 500 if the filestore request errors', function (done) {
  1807. const file = {
  1808. id: new ObjectId().toString(),
  1809. pathname: '/test.png',
  1810. contents: Buffer.from([1, 2, 3]),
  1811. hash: 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74',
  1812. }
  1813. const fileStoreRequest = MockFileStore()
  1814. .get(`/project/${this.projectId}/file/${file.id}`)
  1815. .replyWithError('oh no!')
  1816. const createBlob = MockHistoryStore()
  1817. .put(`/api/projects/${historyId}/blobs/${file.hash}`, file.contents)
  1818. .reply(201)
  1819. const addFile = MockHistoryStore()
  1820. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1821. expect(body).to.deep.equal([
  1822. olAddFileUpdate(file, this.userId, this.timestamp, file.hash),
  1823. ])
  1824. return true
  1825. })
  1826. .query({ end_version: 0 })
  1827. .reply(204)
  1828. async.series(
  1829. [
  1830. cb => {
  1831. ProjectHistoryClient.pushRawUpdate(
  1832. this.projectId,
  1833. slAddFileUpdate(
  1834. historyId,
  1835. file,
  1836. this.userId,
  1837. this.timestamp,
  1838. this.projectId
  1839. ),
  1840. cb
  1841. )
  1842. },
  1843. cb => {
  1844. ProjectHistoryClient.flushProject(
  1845. this.projectId,
  1846. { allowErrors: true },
  1847. (error, res) => {
  1848. if (error) {
  1849. return cb(error)
  1850. }
  1851. expect(res.statusCode).to.equal(500)
  1852. cb()
  1853. }
  1854. )
  1855. },
  1856. ],
  1857. error => {
  1858. if (error) {
  1859. return done(error)
  1860. }
  1861. assert(
  1862. fileStoreRequest.isDone(),
  1863. `/project/${this.projectId}/file/${file.id} should have been called`
  1864. )
  1865. assert(
  1866. !createBlob.isDone(),
  1867. `/api/projects/${historyId}/latest/files should not have been called`
  1868. )
  1869. assert(
  1870. !addFile.isDone(),
  1871. `/api/projects/${historyId}/latest/files should not have been called`
  1872. )
  1873. done()
  1874. }
  1875. )
  1876. })
  1877. })
  1878. describe('with an existing projectVersion field', function () {
  1879. beforeEach(function () {
  1880. MockHistoryStore()
  1881. .get(`/api/projects/${historyId}/latest/history`)
  1882. .reply(200, {
  1883. chunk: {
  1884. startVersion: 0,
  1885. history: {
  1886. snapshot: { projectVersion: '100.0' },
  1887. changes: [],
  1888. },
  1889. },
  1890. })
  1891. })
  1892. it('should discard project structure updates which have already been applied', function (done) {
  1893. const newDoc = []
  1894. for (let i = 0; i <= 2; i++) {
  1895. newDoc[i] = {
  1896. id: new ObjectId().toString(),
  1897. pathname: `/main${i}.tex`,
  1898. hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
  1899. docLines: 'a\nb',
  1900. }
  1901. }
  1902. MockHistoryStore()
  1903. .put(`/api/projects/${historyId}/blobs/${newDoc[0].hash}`)
  1904. .times(3)
  1905. .reply(201)
  1906. const createChange = MockHistoryStore()
  1907. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1908. expect(body).to.deep.equal([
  1909. olAddDocUpdateWithVersion(
  1910. newDoc[1],
  1911. this.userId,
  1912. this.timestamp,
  1913. newDoc[1].hash,
  1914. '101.0'
  1915. ),
  1916. olAddDocUpdateWithVersion(
  1917. newDoc[2],
  1918. this.userId,
  1919. this.timestamp,
  1920. newDoc[2].hash,
  1921. '102.0'
  1922. ),
  1923. ])
  1924. return true
  1925. })
  1926. .query({ end_version: 0 })
  1927. .reply(204)
  1928. async.series(
  1929. [
  1930. cb => {
  1931. ProjectHistoryClient.pushRawUpdate(
  1932. this.projectId,
  1933. slAddDocUpdateWithVersion(
  1934. historyId,
  1935. newDoc[0],
  1936. this.userId,
  1937. this.timestamp,
  1938. newDoc[0].docLines,
  1939. '100.0'
  1940. ),
  1941. cb
  1942. )
  1943. },
  1944. cb => {
  1945. ProjectHistoryClient.pushRawUpdate(
  1946. this.projectId,
  1947. slAddDocUpdateWithVersion(
  1948. historyId,
  1949. newDoc[1],
  1950. this.userId,
  1951. this.timestamp,
  1952. newDoc[1].docLines,
  1953. '101.0'
  1954. ),
  1955. cb
  1956. )
  1957. },
  1958. cb => {
  1959. ProjectHistoryClient.pushRawUpdate(
  1960. this.projectId,
  1961. slAddDocUpdateWithVersion(
  1962. historyId,
  1963. newDoc[2],
  1964. this.userId,
  1965. this.timestamp,
  1966. newDoc[2].docLines,
  1967. '102.0'
  1968. ),
  1969. cb
  1970. )
  1971. },
  1972. cb => {
  1973. ProjectHistoryClient.flushProject(this.projectId, cb)
  1974. },
  1975. ],
  1976. error => {
  1977. if (error) {
  1978. return done(error)
  1979. }
  1980. assert(
  1981. createChange.isDone(),
  1982. `/api/projects/${historyId}/changes should have been called`
  1983. )
  1984. done()
  1985. }
  1986. )
  1987. })
  1988. })
  1989. describe('with an existing docVersions field', function () {
  1990. beforeEach(function () {
  1991. MockHistoryStore()
  1992. .get(`/api/projects/${historyId}/latest/history`)
  1993. .reply(200, {
  1994. chunk: {
  1995. startVersion: 0,
  1996. history: {
  1997. snapshot: { v2DocVersions: { [this.doc.id]: { v: 100 } } }, // version 100 below already applied
  1998. changes: [],
  1999. },
  2000. },
  2001. })
  2002. })
  2003. it('should discard doc updates which have already been applied', function (done) {
  2004. const createChange = MockHistoryStore()
  2005. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  2006. expect(body).to.deep.equal([
  2007. olTextUpdate(
  2008. this.doc,
  2009. this.userId,
  2010. this.timestamp,
  2011. [6, 'baz', 2],
  2012. 101
  2013. ),
  2014. ])
  2015. return true
  2016. })
  2017. .query({ end_version: 0 })
  2018. .reply(204)
  2019. async.series(
  2020. [
  2021. cb => {
  2022. ProjectHistoryClient.pushRawUpdate(
  2023. this.projectId,
  2024. slTextUpdate(
  2025. historyId,
  2026. this.doc,
  2027. this.userId,
  2028. 100,
  2029. this.timestamp,
  2030. [
  2031. { p: 3, i: 'foobar' }, // these ops should be skipped
  2032. { p: 6, d: 'bar' },
  2033. ]
  2034. ),
  2035. cb
  2036. )
  2037. this.doc.length += 3
  2038. },
  2039. cb => {
  2040. ProjectHistoryClient.pushRawUpdate(
  2041. this.projectId,
  2042. slTextUpdate(
  2043. historyId,
  2044. this.doc,
  2045. this.userId,
  2046. 101,
  2047. this.timestamp,
  2048. [
  2049. { p: 6, i: 'baz' }, // this op should be applied
  2050. ]
  2051. ),
  2052. cb
  2053. )
  2054. },
  2055. cb => {
  2056. ProjectHistoryClient.flushProject(this.projectId, cb)
  2057. },
  2058. ],
  2059. error => {
  2060. if (error) {
  2061. return done(error)
  2062. }
  2063. assert(
  2064. createChange.isDone(),
  2065. `/api/projects/${historyId}/changes should have been called`
  2066. )
  2067. done()
  2068. }
  2069. )
  2070. })
  2071. })
  2072. })