ProjectHistoryRedisManagerTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 forward history compatible ranges if history ranges support is enabled', async function () {
  155. this.rawUpdate.historyRangesSupport = true
  156. this.docLines = 'the quick fox jumps over the lazy dog'
  157. const ranges = {
  158. changes: [
  159. {
  160. op: { p: 4, i: 'quick' },
  161. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  162. },
  163. {
  164. op: { p: 9, d: ' brown' },
  165. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  166. },
  167. {
  168. op: { p: 14, i: 'jumps' },
  169. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  170. },
  171. ],
  172. comments: [
  173. {
  174. op: { p: 29, c: 'lazy', t: 'comment-1' },
  175. metadata: { resolved: false },
  176. },
  177. ],
  178. }
  179. this.rawUpdate.ranges = ranges
  180. this.rawUpdate.docLines = this.docLines
  181. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  182. this.project_id,
  183. this.projectHistoryId,
  184. 'doc',
  185. this.doc_id,
  186. this.user_id,
  187. this.rawUpdate,
  188. this.source
  189. )
  190. const historyCompatibleRanges = {
  191. comments: [
  192. {
  193. op: { p: 29, c: 'lazy', t: 'comment-1', hpos: 35 },
  194. metadata: { resolved: false },
  195. },
  196. ],
  197. changes: [
  198. {
  199. op: { p: 4, i: 'quick' },
  200. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  201. },
  202. {
  203. op: { p: 9, d: ' brown' },
  204. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  205. },
  206. {
  207. op: { p: 14, i: 'jumps', hpos: 20 },
  208. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  209. },
  210. ],
  211. }
  212. const update = {
  213. pathname: this.pathname,
  214. docLines: 'the quick brown fox jumps over the lazy dog',
  215. meta: {
  216. user_id: this.user_id,
  217. ts: new Date(),
  218. source: this.source,
  219. },
  220. version: this.version,
  221. projectHistoryId: this.projectHistoryId,
  222. ranges: historyCompatibleRanges,
  223. doc: this.doc_id,
  224. }
  225. expect(
  226. this.ProjectHistoryRedisManager.promises.queueOps
  227. ).to.have.been.calledWithExactly(this.project_id, JSON.stringify(update))
  228. })
  229. it('should not forward ranges if history ranges support is disabled', async function () {
  230. this.rawUpdate.historyRangesSupport = false
  231. const ranges = {
  232. changes: [
  233. {
  234. op: { p: 0, i: 'foo' },
  235. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  236. },
  237. {
  238. op: { p: 7, d: ' baz' },
  239. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  240. },
  241. ],
  242. comments: [
  243. {
  244. op: { p: 4, c: 'bar', t: 'comment-1' },
  245. metadata: { resolved: false },
  246. },
  247. ],
  248. }
  249. this.rawUpdate.ranges = ranges
  250. await this.ProjectHistoryRedisManager.promises.queueAddEntity(
  251. this.project_id,
  252. this.projectHistoryId,
  253. 'doc',
  254. this.doc_id,
  255. this.user_id,
  256. this.rawUpdate,
  257. this.source
  258. )
  259. const update = {
  260. pathname: this.pathname,
  261. docLines: this.docLines,
  262. meta: {
  263. user_id: this.user_id,
  264. ts: new Date(),
  265. source: this.source,
  266. },
  267. version: this.version,
  268. projectHistoryId: this.projectHistoryId,
  269. doc: this.doc_id,
  270. }
  271. this.ProjectHistoryRedisManager.promises.queueOps
  272. .calledWithExactly(this.project_id, JSON.stringify(update))
  273. .should.equal(true)
  274. })
  275. it('should not forward ranges if history ranges support is undefined', 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. })
  322. describe('queueResyncProjectStructure', function () {
  323. it('should queue an update', function () {})
  324. })
  325. describe('queueResyncDocContent', function () {
  326. beforeEach(function () {
  327. this.doc_id = 1234
  328. this.lines = ['one', 'two']
  329. this.ranges = {
  330. changes: [{ op: { i: 'ne', p: 1 } }, { op: { d: 'deleted', p: 3 } }],
  331. }
  332. this.resolvedCommentIds = ['comment-1']
  333. this.version = 2
  334. this.pathname = '/path'
  335. this.ProjectHistoryRedisManager.promises.queueOps = sinon
  336. .stub()
  337. .resolves()
  338. })
  339. describe('with a good doc', function () {
  340. beforeEach(async function () {
  341. this.update = {
  342. resyncDocContent: {
  343. content: 'one\ntwo',
  344. version: this.version,
  345. },
  346. projectHistoryId: this.projectHistoryId,
  347. path: this.pathname,
  348. doc: this.doc_id,
  349. meta: { ts: new Date() },
  350. }
  351. await this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
  352. this.project_id,
  353. this.projectHistoryId,
  354. this.doc_id,
  355. this.lines,
  356. this.ranges,
  357. this.resolvedCommentIds,
  358. this.version,
  359. this.pathname,
  360. false
  361. )
  362. })
  363. it('should check if the doc is too large', function () {
  364. this.Limits.docIsTooLarge
  365. .calledWith(
  366. JSON.stringify(this.update).length,
  367. this.lines,
  368. this.settings.max_doc_length
  369. )
  370. .should.equal(true)
  371. })
  372. it('should queue an update', function () {
  373. this.ProjectHistoryRedisManager.promises.queueOps
  374. .calledWithExactly(this.project_id, JSON.stringify(this.update))
  375. .should.equal(true)
  376. })
  377. })
  378. describe('with a doc that is too large', function () {
  379. beforeEach(async function () {
  380. this.Limits.docIsTooLarge.returns(true)
  381. await expect(
  382. this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
  383. this.project_id,
  384. this.projectHistoryId,
  385. this.doc_id,
  386. this.lines,
  387. this.ranges,
  388. this.resolvedCommentIds,
  389. this.version,
  390. this.pathname,
  391. false
  392. )
  393. ).to.be.rejected
  394. })
  395. it('should not queue an update if the doc is too large', function () {
  396. this.ProjectHistoryRedisManager.promises.queueOps.called.should.equal(
  397. false
  398. )
  399. })
  400. })
  401. describe('when history ranges support is enabled', function () {
  402. beforeEach(async function () {
  403. this.update = {
  404. resyncDocContent: {
  405. content: 'onedeleted\ntwo',
  406. version: this.version,
  407. ranges: this.ranges,
  408. resolvedCommentIds: this.resolvedCommentIds,
  409. },
  410. projectHistoryId: this.projectHistoryId,
  411. path: this.pathname,
  412. doc: this.doc_id,
  413. meta: { ts: new Date() },
  414. }
  415. await this.ProjectHistoryRedisManager.promises.queueResyncDocContent(
  416. this.project_id,
  417. this.projectHistoryId,
  418. this.doc_id,
  419. this.lines,
  420. this.ranges,
  421. this.resolvedCommentIds,
  422. this.version,
  423. this.pathname,
  424. true
  425. )
  426. })
  427. it('should include tracked deletes in the update', function () {
  428. this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWithExactly(
  429. this.project_id,
  430. JSON.stringify(this.update)
  431. )
  432. })
  433. it('should check the doc length without tracked deletes', function () {
  434. this.Limits.docIsTooLarge.should.have.been.calledWith(
  435. JSON.stringify(this.update).length,
  436. this.lines,
  437. this.settings.max_doc_length
  438. )
  439. })
  440. it('should queue an update', function () {
  441. this.ProjectHistoryRedisManager.promises.queueOps
  442. .calledWithExactly(this.project_id, JSON.stringify(this.update))
  443. .should.equal(true)
  444. })
  445. })
  446. })
  447. })