ProjectHistoryRedisManagerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. doc: this.doc_id,
  149. }
  150. this.ProjectHistoryRedisManager.promises.queueOps
  151. .calledWithExactly(this.project_id, JSON.stringify(update))
  152. .should.equal(true)
  153. })
  154. it('should queue an update with file metadata', async function () {
  155. const metadata = {
  156. importedAt: '2024-07-30T09:14:45.928Z',
  157. provider: 'references-provider',
  158. }
  159. const projectId = 'project-id'
  160. const fileId = 'file-id'
  161. const url = `http://filestore/project/${projectId}/file/${fileId}`
  162. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  163. projectId,
  164. this.projectHistoryId,
  165. 'file',
  166. fileId,
  167. this.user_id,
  168. {
  169. pathname: 'foo.png',
  170. url,
  171. version: 42,
  172. hash: '1337',
  173. metadata,
  174. },
  175. this.source
  176. )
  177. const update = {
  178. pathname: 'foo.png',
  179. docLines: undefined,
  180. url,
  181. meta: {
  182. user_id: this.user_id,
  183. ts: new Date(),
  184. source: this.source,
  185. },
  186. version: 42,
  187. hash: '1337',
  188. metadata,
  189. projectHistoryId: this.projectHistoryId,
  190. file: fileId,
  191. }
  192. expect(
  193. this.ProjectHistoryRedisManager.promises.queueOps.args[0][1]
  194. ).to.equal(JSON.stringify(update))
  195. this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
  196. projectId,
  197. JSON.stringify(update)
  198. )
  199. })
  200. it('should forward history compatible ranges if history ranges support is enabled', async function () {
  201. this.rawUpdate.historyRangesSupport = true
  202. this.docLines = 'the quick fox jumps over the lazy dog'
  203. const ranges = {
  204. changes: [
  205. {
  206. op: { p: 4, i: 'quick' },
  207. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  208. },
  209. {
  210. op: { p: 9, d: ' brown' },
  211. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  212. },
  213. {
  214. op: { p: 14, i: 'jumps' },
  215. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  216. },
  217. ],
  218. comments: [
  219. {
  220. op: { p: 29, c: 'lazy', t: 'comment-1' },
  221. metadata: { resolved: false },
  222. },
  223. ],
  224. }
  225. this.rawUpdate.ranges = ranges
  226. this.rawUpdate.docLines = this.docLines
  227. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  228. this.project_id,
  229. this.projectHistoryId,
  230. 'doc',
  231. this.doc_id,
  232. this.user_id,
  233. this.rawUpdate,
  234. this.source
  235. )
  236. const historyCompatibleRanges = {
  237. comments: [
  238. {
  239. op: { p: 29, c: 'lazy', t: 'comment-1', hpos: 35 },
  240. metadata: { resolved: false },
  241. },
  242. ],
  243. changes: [
  244. {
  245. op: { p: 4, i: 'quick' },
  246. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  247. },
  248. {
  249. op: { p: 9, d: ' brown' },
  250. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  251. },
  252. {
  253. op: { p: 14, i: 'jumps', hpos: 20 },
  254. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  255. },
  256. ],
  257. }
  258. const update = {
  259. pathname: this.pathname,
  260. docLines: 'the quick brown fox jumps over the lazy dog',
  261. meta: {
  262. user_id: this.user_id,
  263. ts: new Date(),
  264. source: this.source,
  265. },
  266. version: this.version,
  267. projectHistoryId: this.projectHistoryId,
  268. ranges: historyCompatibleRanges,
  269. doc: this.doc_id,
  270. }
  271. expect(
  272. this.ProjectHistoryRedisManager.promises.queueOps
  273. ).to.have.been.calledWithExactly(this.project_id, JSON.stringify(update))
  274. })
  275. it('should not forward ranges if history ranges support is disabled', async function () {
  276. this.rawUpdate.historyRangesSupport = false
  277. const ranges = {
  278. changes: [
  279. {
  280. op: { p: 0, i: 'foo' },
  281. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  282. },
  283. {
  284. op: { p: 7, d: ' baz' },
  285. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  286. },
  287. ],
  288. comments: [
  289. {
  290. op: { p: 4, c: 'bar', t: 'comment-1' },
  291. metadata: { resolved: false },
  292. },
  293. ],
  294. }
  295. this.rawUpdate.ranges = ranges
  296. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  297. this.project_id,
  298. this.projectHistoryId,
  299. 'doc',
  300. this.doc_id,
  301. this.user_id,
  302. this.rawUpdate,
  303. this.source
  304. )
  305. const update = {
  306. pathname: this.pathname,
  307. docLines: this.docLines,
  308. meta: {
  309. user_id: this.user_id,
  310. ts: new Date(),
  311. source: this.source,
  312. },
  313. version: this.version,
  314. projectHistoryId: this.projectHistoryId,
  315. doc: this.doc_id,
  316. }
  317. this.ProjectHistoryRedisManager.promises.queueOps
  318. .calledWithExactly(this.project_id, JSON.stringify(update))
  319. .should.equal(true)
  320. })
  321. it('should not forward ranges if history ranges support is undefined', async function () {
  322. this.rawUpdate.historyRangesSupport = false
  323. const ranges = {
  324. changes: [
  325. {
  326. op: { p: 0, i: 'foo' },
  327. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  328. },
  329. {
  330. op: { p: 7, d: ' baz' },
  331. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  332. },
  333. ],
  334. comments: [
  335. {
  336. op: { p: 4, c: 'bar', t: 'comment-1' },
  337. metadata: { resolved: false },
  338. },
  339. ],
  340. }
  341. this.rawUpdate.ranges = ranges
  342. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  343. this.project_id,
  344. this.projectHistoryId,
  345. 'doc',
  346. this.doc_id,
  347. this.user_id,
  348. this.rawUpdate,
  349. this.source
  350. )
  351. const update = {
  352. pathname: this.pathname,
  353. docLines: this.docLines,
  354. meta: {
  355. user_id: this.user_id,
  356. ts: new Date(),
  357. source: this.source,
  358. },
  359. version: this.version,
  360. projectHistoryId: this.projectHistoryId,
  361. doc: this.doc_id,
  362. }
  363. this.ProjectHistoryRedisManager.promises.queueOps
  364. .calledWithExactly(this.project_id, JSON.stringify(update))
  365. .should.equal(true)
  366. })
  367. })
  368. describe('queueResyncProjectStructure', function () {
  369. it('should queue an update', function () {})
  370. })
  371. describe('queueResyncDocContent', function () {
  372. beforeEach(function () {
  373. this.doc_id = 1234
  374. this.lines = ['one', 'two']
  375. this.ranges = {
  376. changes: [{ op: { i: 'ne', p: 1 } }, { op: { d: 'deleted', p: 3 } }],
  377. }
  378. this.resolvedCommentIds = ['comment-1']
  379. this.version = 2
  380. this.pathname = '/path'
  381. this.ProjectHistoryRedisManager.promises.queueOps = sinon
  382. .stub()
  383. .resolves()
  384. })
  385. describe('with a good doc', function () {
  386. beforeEach(async function () {
  387. this.update = {
  388. resyncDocContent: {
  389. content: 'one\ntwo',
  390. version: this.version,
  391. },
  392. projectHistoryId: this.projectHistoryId,
  393. path: this.pathname,
  394. doc: this.doc_id,
  395. meta: { ts: new Date() },
  396. }
  397. await this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
  398. this.project_id,
  399. this.projectHistoryId,
  400. this.doc_id,
  401. this.lines,
  402. this.ranges,
  403. this.resolvedCommentIds,
  404. this.version,
  405. this.pathname,
  406. false
  407. )
  408. })
  409. it('should check if the doc is too large', function () {
  410. this.Limits.docIsTooLarge
  411. .calledWith(
  412. JSON.stringify(this.update).length,
  413. this.lines,
  414. this.settings.max_doc_length
  415. )
  416. .should.equal(true)
  417. })
  418. it('should queue an update', function () {
  419. this.ProjectHistoryRedisManager.promises.queueOps
  420. .calledWithExactly(this.project_id, JSON.stringify(this.update))
  421. .should.equal(true)
  422. })
  423. })
  424. describe('with a doc that is too large', function () {
  425. beforeEach(async function () {
  426. this.Limits.docIsTooLarge.returns(true)
  427. await expect(
  428. this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
  429. this.project_id,
  430. this.projectHistoryId,
  431. this.doc_id,
  432. this.lines,
  433. this.ranges,
  434. this.resolvedCommentIds,
  435. this.version,
  436. this.pathname,
  437. false
  438. )
  439. ).to.be.rejected
  440. })
  441. it('should not queue an update if the doc is too large', function () {
  442. this.ProjectHistoryRedisManager.promises.queueOps.called.should.equal(
  443. false
  444. )
  445. })
  446. })
  447. describe('when history ranges support is enabled', function () {
  448. beforeEach(async function () {
  449. this.update = {
  450. resyncDocContent: {
  451. content: 'onedeleted\ntwo',
  452. version: this.version,
  453. ranges: this.ranges,
  454. resolvedCommentIds: this.resolvedCommentIds,
  455. },
  456. projectHistoryId: this.projectHistoryId,
  457. path: this.pathname,
  458. doc: this.doc_id,
  459. meta: { ts: new Date() },
  460. }
  461. await this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
  462. this.project_id,
  463. this.projectHistoryId,
  464. this.doc_id,
  465. this.lines,
  466. this.ranges,
  467. this.resolvedCommentIds,
  468. this.version,
  469. this.pathname,
  470. true
  471. )
  472. })
  473. it('should include tracked deletes in the update', function () {
  474. this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
  475. this.project_id,
  476. JSON.stringify(this.update)
  477. )
  478. })
  479. it('should check the doc length without tracked deletes', function () {
  480. this.Limits.docIsTooLarge.should.have.been.calledWith(
  481. JSON.stringify(this.update).length,
  482. this.lines,
  483. this.settings.max_doc_length
  484. )
  485. })
  486. it('should queue an update', function () {
  487. this.ProjectHistoryRedisManager.promises.queueOps
  488. .calledWithExactly(this.project_id, JSON.stringify(this.update))
  489. .should.equal(true)
  490. })
  491. })
  492. })
  493. })