ApplyingUpdatesToProjectStructureTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. const sinon = require('sinon')
  2. const { setTimeout } = require('node:timers/promises')
  3. const Settings = require('@overleaf/settings')
  4. const rclientProjectHistory = require('@overleaf/redis-wrapper').createClient(
  5. Settings.redis.project_history
  6. )
  7. const ProjectHistoryKeys = Settings.redis.project_history.key_schema
  8. const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
  9. const MockWebApi = require('./helpers/MockWebApi')
  10. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  11. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  12. async function sendProjectUpdateAndWait(projectId, docId, update, version) {
  13. await DocUpdaterClient.sendProjectUpdate(projectId, docId, update, version)
  14. // It seems that we need to wait for a little while
  15. await setTimeout(200)
  16. }
  17. describe("Applying updates to a project's structure", function () {
  18. before(async function () {
  19. this.user_id = 'user-id-123'
  20. this.version = 1234
  21. await DocUpdaterApp.ensureRunning()
  22. })
  23. describe('renaming a file', function () {
  24. before(async function () {
  25. this.project_id = DocUpdaterClient.randomId()
  26. this.fileUpdate = {
  27. type: 'rename-file',
  28. id: DocUpdaterClient.randomId(),
  29. pathname: '/file-path',
  30. newPathname: '/new-file-path',
  31. }
  32. this.updates = [this.fileUpdate]
  33. await sendProjectUpdateAndWait(
  34. this.project_id,
  35. this.user_id,
  36. this.updates,
  37. this.version
  38. )
  39. })
  40. it('should push the applied file renames to the project history api', function (done) {
  41. rclientProjectHistory.lrange(
  42. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  43. 0,
  44. -1,
  45. (error, updates) => {
  46. if (error) {
  47. return done(error)
  48. }
  49. const update = JSON.parse(updates[0])
  50. update.file.should.equal(this.fileUpdate.id)
  51. update.pathname.should.equal('/file-path')
  52. update.new_pathname.should.equal('/new-file-path')
  53. update.meta.user_id.should.equal(this.user_id)
  54. update.meta.ts.should.be.a('string')
  55. update.version.should.equal(`${this.version}.0`)
  56. done()
  57. }
  58. )
  59. })
  60. })
  61. describe('deleting a file', function () {
  62. before(async function () {
  63. this.project_id = DocUpdaterClient.randomId()
  64. this.fileUpdate = {
  65. type: 'rename-file',
  66. id: DocUpdaterClient.randomId(),
  67. pathname: '/file-path',
  68. newPathname: '',
  69. }
  70. this.updates = [this.fileUpdate]
  71. await sendProjectUpdateAndWait(
  72. this.project_id,
  73. this.user_id,
  74. this.updates,
  75. this.version
  76. )
  77. })
  78. it('should push the applied file renames to the project history api', function (done) {
  79. rclientProjectHistory.lrange(
  80. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  81. 0,
  82. -1,
  83. (error, updates) => {
  84. if (error) {
  85. return done(error)
  86. }
  87. const update = JSON.parse(updates[0])
  88. update.file.should.equal(this.fileUpdate.id)
  89. update.pathname.should.equal('/file-path')
  90. update.new_pathname.should.equal('')
  91. update.meta.user_id.should.equal(this.user_id)
  92. update.meta.ts.should.be.a('string')
  93. update.version.should.equal(`${this.version}.0`)
  94. done()
  95. }
  96. )
  97. })
  98. })
  99. describe('renaming a document', function () {
  100. before(function () {
  101. this.update = {
  102. type: 'rename-doc',
  103. id: DocUpdaterClient.randomId(),
  104. pathname: '/doc-path',
  105. newPathname: '/new-doc-path',
  106. }
  107. this.updates = [this.update]
  108. })
  109. describe('when the document is not loaded', function () {
  110. before(async function () {
  111. this.project_id = DocUpdaterClient.randomId()
  112. await sendProjectUpdateAndWait(
  113. this.project_id,
  114. this.user_id,
  115. this.updates,
  116. this.version
  117. )
  118. })
  119. it('should push the applied doc renames to the project history api', function (done) {
  120. rclientProjectHistory.lrange(
  121. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  122. 0,
  123. -1,
  124. (error, updates) => {
  125. if (error) {
  126. return done(error)
  127. }
  128. const update = JSON.parse(updates[0])
  129. update.doc.should.equal(this.update.id)
  130. update.pathname.should.equal('/doc-path')
  131. update.new_pathname.should.equal('/new-doc-path')
  132. update.meta.user_id.should.equal(this.user_id)
  133. update.meta.ts.should.be.a('string')
  134. update.version.should.equal(`${this.version}.0`)
  135. done()
  136. }
  137. )
  138. })
  139. })
  140. describe('when the document is loaded', function () {
  141. before(async function () {
  142. this.project_id = DocUpdaterClient.randomId()
  143. MockWebApi.insertDoc(this.project_id, this.update.id, {})
  144. await DocUpdaterClient.preloadDoc(this.project_id, this.update.id)
  145. sinon.spy(MockWebApi, 'getDocument')
  146. await sendProjectUpdateAndWait(
  147. this.project_id,
  148. this.user_id,
  149. this.updates,
  150. this.version
  151. )
  152. })
  153. after(function () {
  154. MockWebApi.getDocument.restore()
  155. })
  156. it('should update the doc', async function () {
  157. const doc = await DocUpdaterClient.getDoc(
  158. this.project_id,
  159. this.update.id
  160. )
  161. doc.pathname.should.equal(this.update.newPathname)
  162. })
  163. it('should push the applied doc renames to the project history api', function (done) {
  164. rclientProjectHistory.lrange(
  165. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  166. 0,
  167. -1,
  168. (error, updates) => {
  169. if (error) {
  170. return done(error)
  171. }
  172. const update = JSON.parse(updates[0])
  173. update.doc.should.equal(this.update.id)
  174. update.pathname.should.equal('/doc-path')
  175. update.new_pathname.should.equal('/new-doc-path')
  176. update.meta.user_id.should.equal(this.user_id)
  177. update.meta.ts.should.be.a('string')
  178. update.version.should.equal(`${this.version}.0`)
  179. done()
  180. }
  181. )
  182. })
  183. })
  184. })
  185. describe('renaming multiple documents and files', function () {
  186. before(function () {
  187. this.docUpdate0 = {
  188. type: 'rename-doc',
  189. id: DocUpdaterClient.randomId(),
  190. pathname: '/doc-path0',
  191. newPathname: '/new-doc-path0',
  192. }
  193. this.docUpdate1 = {
  194. type: 'rename-doc',
  195. id: DocUpdaterClient.randomId(),
  196. pathname: '/doc-path1',
  197. newPathname: '/new-doc-path1',
  198. }
  199. this.fileUpdate0 = {
  200. type: 'rename-file',
  201. id: DocUpdaterClient.randomId(),
  202. pathname: '/file-path0',
  203. newPathname: '/new-file-path0',
  204. }
  205. this.fileUpdate1 = {
  206. type: 'rename-file',
  207. id: DocUpdaterClient.randomId(),
  208. pathname: '/file-path1',
  209. newPathname: '/new-file-path1',
  210. }
  211. this.updates = [
  212. this.docUpdate0,
  213. this.docUpdate1,
  214. this.fileUpdate0,
  215. this.fileUpdate1,
  216. ]
  217. })
  218. describe('when the documents are not loaded', function () {
  219. before(async function () {
  220. this.project_id = DocUpdaterClient.randomId()
  221. await sendProjectUpdateAndWait(
  222. this.project_id,
  223. this.user_id,
  224. this.updates,
  225. this.version
  226. )
  227. })
  228. it('should push the applied doc renames to the project history api', function (done) {
  229. rclientProjectHistory.lrange(
  230. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  231. 0,
  232. -1,
  233. (error, updates) => {
  234. if (error) {
  235. return done(error)
  236. }
  237. let update = JSON.parse(updates[0])
  238. update.doc.should.equal(this.docUpdate0.id)
  239. update.pathname.should.equal('/doc-path0')
  240. update.new_pathname.should.equal('/new-doc-path0')
  241. update.meta.user_id.should.equal(this.user_id)
  242. update.meta.ts.should.be.a('string')
  243. update.version.should.equal(`${this.version}.0`)
  244. update = JSON.parse(updates[1])
  245. update.doc.should.equal(this.docUpdate1.id)
  246. update.pathname.should.equal('/doc-path1')
  247. update.new_pathname.should.equal('/new-doc-path1')
  248. update.meta.user_id.should.equal(this.user_id)
  249. update.meta.ts.should.be.a('string')
  250. update.version.should.equal(`${this.version}.1`)
  251. update = JSON.parse(updates[2])
  252. update.file.should.equal(this.fileUpdate0.id)
  253. update.pathname.should.equal('/file-path0')
  254. update.new_pathname.should.equal('/new-file-path0')
  255. update.meta.user_id.should.equal(this.user_id)
  256. update.meta.ts.should.be.a('string')
  257. update.version.should.equal(`${this.version}.2`)
  258. update = JSON.parse(updates[3])
  259. update.file.should.equal(this.fileUpdate1.id)
  260. update.pathname.should.equal('/file-path1')
  261. update.new_pathname.should.equal('/new-file-path1')
  262. update.meta.user_id.should.equal(this.user_id)
  263. update.meta.ts.should.be.a('string')
  264. update.version.should.equal(`${this.version}.3`)
  265. done()
  266. }
  267. )
  268. })
  269. })
  270. })
  271. describe('deleting a document', function () {
  272. before(function () {
  273. this.update = {
  274. type: 'rename-doc',
  275. id: DocUpdaterClient.randomId(),
  276. pathname: '/doc-path',
  277. newPathname: '',
  278. }
  279. this.updates = [this.update]
  280. })
  281. describe('when the document is not loaded', function () {
  282. before(async function () {
  283. this.project_id = DocUpdaterClient.randomId()
  284. await sendProjectUpdateAndWait(
  285. this.project_id,
  286. this.user_id,
  287. this.updates,
  288. this.version
  289. )
  290. })
  291. it('should push the applied doc update to the project history api', function (done) {
  292. rclientProjectHistory.lrange(
  293. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  294. 0,
  295. -1,
  296. (error, updates) => {
  297. if (error) {
  298. return done(error)
  299. }
  300. const update = JSON.parse(updates[0])
  301. update.doc.should.equal(this.update.id)
  302. update.pathname.should.equal('/doc-path')
  303. update.new_pathname.should.equal('')
  304. update.meta.user_id.should.equal(this.user_id)
  305. update.meta.ts.should.be.a('string')
  306. update.version.should.equal(`${this.version}.0`)
  307. done()
  308. }
  309. )
  310. })
  311. })
  312. describe('when the document is loaded', function () {
  313. before(async function () {
  314. this.project_id = DocUpdaterClient.randomId()
  315. MockWebApi.insertDoc(this.project_id, this.update.id, {})
  316. await DocUpdaterClient.preloadDoc(this.project_id, this.update.id)
  317. sinon.spy(MockWebApi, 'getDocument')
  318. await sendProjectUpdateAndWait(
  319. this.project_id,
  320. this.user_id,
  321. this.updates,
  322. this.version
  323. )
  324. })
  325. after(function () {
  326. MockWebApi.getDocument.restore()
  327. })
  328. it('should not modify the doc', async function () {
  329. const doc = await DocUpdaterClient.getDoc(
  330. this.project_id,
  331. this.update.id
  332. )
  333. doc.pathname.should.equal('/a/b/c.tex') // default pathname from MockWebApi
  334. })
  335. it('should push the applied doc update to the project history api', function (done) {
  336. rclientProjectHistory.lrange(
  337. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  338. 0,
  339. -1,
  340. (error, updates) => {
  341. if (error) {
  342. return done(error)
  343. }
  344. const update = JSON.parse(updates[0])
  345. update.doc.should.equal(this.update.id)
  346. update.pathname.should.equal('/doc-path')
  347. update.new_pathname.should.equal('')
  348. update.meta.user_id.should.equal(this.user_id)
  349. update.meta.ts.should.be.a('string')
  350. update.version.should.equal(`${this.version}.0`)
  351. done()
  352. }
  353. )
  354. })
  355. })
  356. })
  357. describe('adding a file', function () {
  358. before(async function () {
  359. this.project_id = DocUpdaterClient.randomId()
  360. this.fileUpdate = {
  361. type: 'add-file',
  362. id: DocUpdaterClient.randomId(),
  363. pathname: '/file-path',
  364. url: 'filestore.example.com',
  365. }
  366. this.updates = [this.fileUpdate]
  367. await sendProjectUpdateAndWait(
  368. this.project_id,
  369. this.user_id,
  370. this.updates,
  371. this.version
  372. )
  373. })
  374. it('should push the file addition to the project history api', function (done) {
  375. rclientProjectHistory.lrange(
  376. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  377. 0,
  378. -1,
  379. (error, updates) => {
  380. if (error) {
  381. return done(error)
  382. }
  383. const update = JSON.parse(updates[0])
  384. update.file.should.equal(this.fileUpdate.id)
  385. update.pathname.should.equal('/file-path')
  386. update.url.should.equal('filestore.example.com')
  387. update.meta.user_id.should.equal(this.user_id)
  388. update.meta.ts.should.be.a('string')
  389. update.version.should.equal(`${this.version}.0`)
  390. done()
  391. }
  392. )
  393. })
  394. })
  395. describe('adding a doc', function () {
  396. before(async function () {
  397. this.project_id = DocUpdaterClient.randomId()
  398. this.docUpdate = {
  399. type: 'add-doc',
  400. id: DocUpdaterClient.randomId(),
  401. pathname: '/file-path',
  402. docLines: 'a\nb',
  403. }
  404. this.updates = [this.docUpdate]
  405. await sendProjectUpdateAndWait(
  406. this.project_id,
  407. this.user_id,
  408. this.updates,
  409. this.version
  410. )
  411. })
  412. it('should push the doc addition to the project history api', function (done) {
  413. rclientProjectHistory.lrange(
  414. ProjectHistoryKeys.projectHistoryOps({ project_id: this.project_id }),
  415. 0,
  416. -1,
  417. (error, updates) => {
  418. if (error) {
  419. return done(error)
  420. }
  421. const update = JSON.parse(updates[0])
  422. update.doc.should.equal(this.docUpdate.id)
  423. update.pathname.should.equal('/file-path')
  424. update.docLines.should.equal('a\nb')
  425. update.meta.user_id.should.equal(this.user_id)
  426. update.meta.ts.should.be.a('string')
  427. update.version.should.equal(`${this.version}.0`)
  428. done()
  429. }
  430. )
  431. })
  432. })
  433. describe('with enough updates to flush to the history service', function () {
  434. before(async function () {
  435. this.project_id = DocUpdaterClient.randomId()
  436. this.user_id = DocUpdaterClient.randomId()
  437. this.version0 = 12345
  438. this.version1 = this.version0 + 1
  439. const updates = []
  440. for (let v = 0; v <= 599; v++) {
  441. // Should flush after 500 ops
  442. updates.push({
  443. type: 'add-doc',
  444. id: DocUpdaterClient.randomId(),
  445. pathname: '/file-' + v,
  446. docLines: 'a\nb',
  447. })
  448. }
  449. sinon.spy(MockProjectHistoryApi, 'flushProject')
  450. // Send updates in chunks to causes multiple flushes
  451. const projectId = this.project_id
  452. const userId = this.project_id
  453. await DocUpdaterClient.sendProjectUpdate(
  454. projectId,
  455. userId,
  456. updates.slice(0, 250),
  457. this.version0
  458. )
  459. await DocUpdaterClient.sendProjectUpdate(
  460. projectId,
  461. userId,
  462. updates.slice(250),
  463. this.version1
  464. )
  465. await setTimeout(200)
  466. })
  467. after(function () {
  468. MockProjectHistoryApi.flushProject.restore()
  469. })
  470. it('should flush project history', function () {
  471. MockProjectHistoryApi.flushProject
  472. .calledWith(this.project_id)
  473. .should.equal(true)
  474. })
  475. })
  476. describe('with too few updates to flush to the history service', function () {
  477. before(async function () {
  478. this.project_id = DocUpdaterClient.randomId()
  479. this.user_id = DocUpdaterClient.randomId()
  480. this.version0 = 12345
  481. this.version1 = this.version0 + 1
  482. const updates = []
  483. for (let v = 0; v <= 42; v++) {
  484. // Should flush after 500 ops
  485. updates.push({
  486. type: 'add-doc',
  487. id: DocUpdaterClient.randomId(),
  488. pathname: '/file-' + v,
  489. docLines: 'a\nb',
  490. })
  491. }
  492. sinon.spy(MockProjectHistoryApi, 'flushProject')
  493. // Send updates in chunks
  494. const projectId = this.project_id
  495. const userId = this.project_id
  496. await DocUpdaterClient.sendProjectUpdate(
  497. projectId,
  498. userId,
  499. updates.slice(0, 10),
  500. this.version0
  501. )
  502. await DocUpdaterClient.sendProjectUpdate(
  503. projectId,
  504. userId,
  505. updates.slice(10),
  506. this.version1
  507. )
  508. await setTimeout(200)
  509. })
  510. after(function () {
  511. MockProjectHistoryApi.flushProject.restore()
  512. })
  513. it('should not flush project history', function () {
  514. MockProjectHistoryApi.flushProject
  515. .calledWith(this.project_id)
  516. .should.equal(false)
  517. })
  518. })
  519. })