ProjectHistoryRedisManagerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../../app/js/ProjectHistoryRedisManager.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. const tk = require('timekeeper')
  6. describe('ProjectHistoryRedisManager', function () {
  7. beforeEach(function () {
  8. this.project_id = 'project-id-123'
  9. this.projectHistoryId = 'history-id-123'
  10. this.user_id = 'user-id-123'
  11. this.rclient = {}
  12. this.source = 'editor'
  13. tk.freeze(new Date())
  14. this.Limits = {
  15. docIsTooLarge: sinon.stub().returns(false),
  16. }
  17. this.ProjectHistoryRedisManager = SandboxedModule.require(modulePath, {
  18. requires: {
  19. '@overleaf/settings': (this.settings = {
  20. max_doc_length: 123,
  21. redis: {
  22. project_history: {
  23. key_schema: {
  24. projectHistoryOps({ project_id: projectId }) {
  25. return `ProjectHistory:Ops:${projectId}`
  26. },
  27. projectHistoryFirstOpTimestamp({ project_id: projectId }) {
  28. return `ProjectHistory:FirstOpTimestamp:${projectId}`
  29. },
  30. },
  31. },
  32. },
  33. }),
  34. '@overleaf/redis-wrapper': {
  35. createClient: () => this.rclient,
  36. },
  37. './Metrics': (this.metrics = { summary: sinon.stub() }),
  38. './Limits': this.Limits,
  39. },
  40. })
  41. })
  42. afterEach(function () {
  43. tk.reset()
  44. })
  45. describe('queueOps', function () {
  46. beforeEach(async function () {
  47. this.ops = ['mock-op-1', 'mock-op-2']
  48. this.multi = { exec: sinon.stub().resolves([1]) }
  49. this.multi.rpush = sinon.stub()
  50. this.multi.setnx = sinon.stub()
  51. this.rclient.multi = () => this.multi
  52. await this.ProjectHistoryRedisManager.promises.queueOps(
  53. this.project_id,
  54. ...this.ops
  55. )
  56. })
  57. it('should queue an update', function () {
  58. this.multi.rpush
  59. .calledWithExactly(
  60. `ProjectHistory:Ops:${this.project_id}`,
  61. this.ops[0],
  62. this.ops[1]
  63. )
  64. .should.equal(true)
  65. })
  66. it('should set the queue timestamp if not present', function () {
  67. this.multi.setnx
  68. .calledWithExactly(
  69. `ProjectHistory:FirstOpTimestamp:${this.project_id}`,
  70. Date.now()
  71. )
  72. .should.equal(true)
  73. })
  74. })
  75. describe('queueRenameEntity', function () {
  76. beforeEach(async function () {
  77. this.file_id = 1234
  78. this.rawUpdate = {
  79. pathname: (this.pathname = '/old'),
  80. newPathname: (this.newPathname = '/new'),
  81. version: (this.version = 2),
  82. }
  83. this.ProjectHistoryRedisManager.promises.queueOps = sinon
  84. .stub()
  85. .resolves()
  86. await this.ProjectHistoryRedisManager.promises.queueRenameEntity(
  87. this.project_id,
  88. this.projectHistoryId,
  89. 'file',
  90. this.file_id,
  91. this.user_id,
  92. this.rawUpdate,
  93. this.source
  94. )
  95. })
  96. it('should queue an update', function () {
  97. const update = {
  98. pathname: this.pathname,
  99. new_pathname: this.newPathname,
  100. meta: {
  101. user_id: this.user_id,
  102. ts: new Date(),
  103. source: this.source,
  104. },
  105. version: this.version,
  106. projectHistoryId: this.projectHistoryId,
  107. file: this.file_id,
  108. }
  109. this.ProjectHistoryRedisManager.promises.queueOps
  110. .calledWithExactly(this.project_id, JSON.stringify(update))
  111. .should.equal(true)
  112. })
  113. })
  114. describe('queueAddEntity', function () {
  115. beforeEach(function () {
  116. this.doc_id = 1234
  117. this.rawUpdate = {
  118. pathname: (this.pathname = '/old'),
  119. docLines: (this.docLines = 'a\nb'),
  120. version: (this.version = 2),
  121. }
  122. this.ProjectHistoryRedisManager.promises.queueOps = sinon
  123. .stub()
  124. .resolves()
  125. })
  126. it('should queue an update', async function () {
  127. this.rawUpdate.url = this.url = 'filestore.example.com'
  128. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  129. this.project_id,
  130. this.projectHistoryId,
  131. 'doc',
  132. this.doc_id,
  133. this.user_id,
  134. this.rawUpdate,
  135. this.source
  136. )
  137. const update = {
  138. pathname: this.pathname,
  139. docLines: this.docLines,
  140. url: this.url,
  141. meta: {
  142. user_id: this.user_id,
  143. ts: new Date(),
  144. source: this.source,
  145. },
  146. version: this.version,
  147. projectHistoryId: this.projectHistoryId,
  148. createdBlob: false,
  149. doc: this.doc_id,
  150. }
  151. this.ProjectHistoryRedisManager.promises.queueOps
  152. .calledWithExactly(this.project_id, JSON.stringify(update))
  153. .should.equal(true)
  154. })
  155. it('should queue an update with file metadata', async function () {
  156. const metadata = {
  157. importedAt: '2024-07-30T09:14:45.928Z',
  158. provider: 'references-provider',
  159. }
  160. const projectId = 'project-id'
  161. const fileId = 'file-id'
  162. const url = `http://filestore/project/${projectId}/file/${fileId}`
  163. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  164. projectId,
  165. this.projectHistoryId,
  166. 'file',
  167. fileId,
  168. this.user_id,
  169. {
  170. pathname: 'foo.png',
  171. url,
  172. version: 42,
  173. hash: '1337',
  174. metadata,
  175. },
  176. this.source
  177. )
  178. const update = {
  179. pathname: 'foo.png',
  180. docLines: undefined,
  181. url,
  182. meta: {
  183. user_id: this.user_id,
  184. ts: new Date(),
  185. source: this.source,
  186. },
  187. version: 42,
  188. hash: '1337',
  189. metadata,
  190. projectHistoryId: this.projectHistoryId,
  191. createdBlob: false,
  192. file: fileId,
  193. }
  194. expect(
  195. this.ProjectHistoryRedisManager.promises.queueOps.args[0][1]
  196. ).to.equal(JSON.stringify(update))
  197. this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
  198. projectId,
  199. JSON.stringify(update)
  200. )
  201. })
  202. it('should forward history compatible ranges if history ranges support is enabled', async function () {
  203. this.rawUpdate.historyRangesSupport = true
  204. this.docLines = 'the quick fox jumps over the lazy dog'
  205. const ranges = {
  206. changes: [
  207. {
  208. op: { p: 4, i: 'quick' },
  209. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  210. },
  211. {
  212. op: { p: 9, d: ' brown' },
  213. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  214. },
  215. {
  216. op: { p: 14, i: 'jumps' },
  217. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  218. },
  219. ],
  220. comments: [
  221. {
  222. op: { p: 29, c: 'lazy', t: 'comment-1' },
  223. metadata: { resolved: false },
  224. },
  225. ],
  226. }
  227. this.rawUpdate.ranges = ranges
  228. this.rawUpdate.docLines = this.docLines
  229. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  230. this.project_id,
  231. this.projectHistoryId,
  232. 'doc',
  233. this.doc_id,
  234. this.user_id,
  235. this.rawUpdate,
  236. this.source
  237. )
  238. const historyCompatibleRanges = {
  239. comments: [
  240. {
  241. op: { p: 29, c: 'lazy', t: 'comment-1', hpos: 35 },
  242. metadata: { resolved: false },
  243. },
  244. ],
  245. changes: [
  246. {
  247. op: { p: 4, i: 'quick' },
  248. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  249. },
  250. {
  251. op: { p: 9, d: ' brown' },
  252. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  253. },
  254. {
  255. op: { p: 14, i: 'jumps', hpos: 20 },
  256. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  257. },
  258. ],
  259. }
  260. const update = {
  261. pathname: this.pathname,
  262. docLines: 'the quick brown fox jumps over the lazy dog',
  263. meta: {
  264. user_id: this.user_id,
  265. ts: new Date(),
  266. source: this.source,
  267. },
  268. version: this.version,
  269. projectHistoryId: this.projectHistoryId,
  270. createdBlob: false,
  271. ranges: historyCompatibleRanges,
  272. doc: this.doc_id,
  273. }
  274. expect(
  275. this.ProjectHistoryRedisManager.promises.queueOps
  276. ).to.have.been.calledWithExactly(this.project_id, JSON.stringify(update))
  277. })
  278. it('should not forward ranges if history ranges support is disabled', async function () {
  279. this.rawUpdate.historyRangesSupport = false
  280. const ranges = {
  281. changes: [
  282. {
  283. op: { p: 0, i: 'foo' },
  284. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  285. },
  286. {
  287. op: { p: 7, d: ' baz' },
  288. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  289. },
  290. ],
  291. comments: [
  292. {
  293. op: { p: 4, c: 'bar', t: 'comment-1' },
  294. metadata: { resolved: false },
  295. },
  296. ],
  297. }
  298. this.rawUpdate.ranges = ranges
  299. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  300. this.project_id,
  301. this.projectHistoryId,
  302. 'doc',
  303. this.doc_id,
  304. this.user_id,
  305. this.rawUpdate,
  306. this.source
  307. )
  308. const update = {
  309. pathname: this.pathname,
  310. docLines: this.docLines,
  311. meta: {
  312. user_id: this.user_id,
  313. ts: new Date(),
  314. source: this.source,
  315. },
  316. version: this.version,
  317. projectHistoryId: this.projectHistoryId,
  318. createdBlob: false,
  319. doc: this.doc_id,
  320. }
  321. this.ProjectHistoryRedisManager.promises.queueOps
  322. .calledWithExactly(this.project_id, JSON.stringify(update))
  323. .should.equal(true)
  324. })
  325. it('should not forward ranges if history ranges support is undefined', async function () {
  326. this.rawUpdate.historyRangesSupport = false
  327. const ranges = {
  328. changes: [
  329. {
  330. op: { p: 0, i: 'foo' },
  331. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  332. },
  333. {
  334. op: { p: 7, d: ' baz' },
  335. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  336. },
  337. ],
  338. comments: [
  339. {
  340. op: { p: 4, c: 'bar', t: 'comment-1' },
  341. metadata: { resolved: false },
  342. },
  343. ],
  344. }
  345. this.rawUpdate.ranges = ranges
  346. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  347. this.project_id,
  348. this.projectHistoryId,
  349. 'doc',
  350. this.doc_id,
  351. this.user_id,
  352. this.rawUpdate,
  353. this.source
  354. )
  355. const update = {
  356. pathname: this.pathname,
  357. docLines: this.docLines,
  358. meta: {
  359. user_id: this.user_id,
  360. ts: new Date(),
  361. source: this.source,
  362. },
  363. version: this.version,
  364. projectHistoryId: this.projectHistoryId,
  365. createdBlob: false,
  366. doc: this.doc_id,
  367. }
  368. this.ProjectHistoryRedisManager.promises.queueOps
  369. .calledWithExactly(this.project_id, JSON.stringify(update))
  370. .should.equal(true)
  371. })
  372. it('should pass "false" as the createdBlob field if not provided', async function () {
  373. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  374. this.project_id,
  375. this.projectHistoryId,
  376. 'doc',
  377. this.doc_id,
  378. this.user_id,
  379. this.rawUpdate,
  380. this.source
  381. )
  382. const update = {
  383. pathname: this.pathname,
  384. docLines: this.docLines,
  385. meta: {
  386. user_id: this.user_id,
  387. ts: new Date(),
  388. source: this.source,
  389. },
  390. version: this.version,
  391. projectHistoryId: this.projectHistoryId,
  392. createdBlob: false,
  393. doc: this.doc_id,
  394. }
  395. this.ProjectHistoryRedisManager.promises.queueOps
  396. .calledWithExactly(this.project_id, JSON.stringify(update))
  397. .should.equal(true)
  398. })
  399. it('should pass through the value of the createdBlob field', async function () {
  400. this.rawUpdate.createdBlob = true
  401. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  402. this.project_id,
  403. this.projectHistoryId,
  404. 'doc',
  405. this.doc_id,
  406. this.user_id,
  407. this.rawUpdate,
  408. this.source
  409. )
  410. const update = {
  411. pathname: this.pathname,
  412. docLines: this.docLines,
  413. meta: {
  414. user_id: this.user_id,
  415. ts: new Date(),
  416. source: this.source,
  417. },
  418. version: this.version,
  419. projectHistoryId: this.projectHistoryId,
  420. createdBlob: true,
  421. doc: this.doc_id,
  422. }
  423. this.ProjectHistoryRedisManager.promises.queueOps
  424. .calledWithExactly(this.project_id, JSON.stringify(update))
  425. .should.equal(true)
  426. })
  427. })
  428. describe('queueResyncProjectStructure', function () {
  429. it('should queue an update', function () {})
  430. })
  431. describe('queueResyncDocContent', function () {
  432. beforeEach(function () {
  433. this.doc_id = 1234
  434. this.lines = ['one', 'two']
  435. this.ranges = {
  436. changes: [{ op: { i: 'ne', p: 1 } }, { op: { d: 'deleted', p: 3 } }],
  437. }
  438. this.resolvedCommentIds = ['comment-1']
  439. this.version = 2
  440. this.pathname = '/path'
  441. this.ProjectHistoryRedisManager.promises.queueOps = sinon
  442. .stub()
  443. .resolves()
  444. })
  445. describe('with a good doc', function () {
  446. beforeEach(async function () {
  447. this.update = {
  448. resyncDocContent: {
  449. content: 'one\ntwo',
  450. version: this.version,
  451. },
  452. projectHistoryId: this.projectHistoryId,
  453. path: this.pathname,
  454. doc: this.doc_id,
  455. meta: { ts: new Date() },
  456. }
  457. await this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
  458. this.project_id,
  459. this.projectHistoryId,
  460. this.doc_id,
  461. this.lines,
  462. this.ranges,
  463. this.resolvedCommentIds,
  464. this.version,
  465. this.pathname,
  466. false
  467. )
  468. })
  469. it('should check if the doc is too large', function () {
  470. this.Limits.docIsTooLarge
  471. .calledWith(
  472. JSON.stringify(this.update).length,
  473. this.lines,
  474. this.settings.max_doc_length
  475. )
  476. .should.equal(true)
  477. })
  478. it('should queue an update', function () {
  479. this.ProjectHistoryRedisManager.promises.queueOps
  480. .calledWithExactly(this.project_id, JSON.stringify(this.update))
  481. .should.equal(true)
  482. })
  483. })
  484. describe('with a doc that is too large', function () {
  485. beforeEach(async function () {
  486. this.Limits.docIsTooLarge.returns(true)
  487. await expect(
  488. this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
  489. this.project_id,
  490. this.projectHistoryId,
  491. this.doc_id,
  492. this.lines,
  493. this.ranges,
  494. this.resolvedCommentIds,
  495. this.version,
  496. this.pathname,
  497. false
  498. )
  499. ).to.be.rejected
  500. })
  501. it('should not queue an update if the doc is too large', function () {
  502. this.ProjectHistoryRedisManager.promises.queueOps.called.should.equal(
  503. false
  504. )
  505. })
  506. })
  507. describe('when history ranges support is enabled', function () {
  508. beforeEach(async function () {
  509. this.update = {
  510. resyncDocContent: {
  511. content: 'onedeleted\ntwo',
  512. version: this.version,
  513. ranges: this.ranges,
  514. resolvedCommentIds: this.resolvedCommentIds,
  515. },
  516. projectHistoryId: this.projectHistoryId,
  517. path: this.pathname,
  518. doc: this.doc_id,
  519. meta: { ts: new Date() },
  520. }
  521. await this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
  522. this.project_id,
  523. this.projectHistoryId,
  524. this.doc_id,
  525. this.lines,
  526. this.ranges,
  527. this.resolvedCommentIds,
  528. this.version,
  529. this.pathname,
  530. true
  531. )
  532. })
  533. it('should include tracked deletes in the update', function () {
  534. this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
  535. this.project_id,
  536. JSON.stringify(this.update)
  537. )
  538. })
  539. it('should check the doc length without tracked deletes', function () {
  540. this.Limits.docIsTooLarge.should.have.been.calledWith(
  541. JSON.stringify(this.update).length,
  542. this.lines,
  543. this.settings.max_doc_length
  544. )
  545. })
  546. it('should queue an update', function () {
  547. this.ProjectHistoryRedisManager.promises.queueOps
  548. .calledWithExactly(this.project_id, JSON.stringify(this.update))
  549. .should.equal(true)
  550. })
  551. })
  552. })
  553. })