GetChangesSince.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { expect } from 'chai'
  2. import mongodb from 'mongodb-legacy'
  3. import nock from 'nock'
  4. import Core from 'overleaf-editor-core'
  5. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  6. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  7. import latestChunk from '../fixtures/chunks/7-8.json' with { type: 'json' }
  8. import previousChunk from '../fixtures/chunks/4-6.json' with { type: 'json' }
  9. import firstChunk from '../fixtures/chunks/0-3.json' with { type: 'json' }
  10. const { ObjectId } = mongodb
  11. const MockHistoryStore = () => nock('http://127.0.0.1:3100')
  12. const MockWeb = () => nock('http://127.0.0.1:3000')
  13. const fixture = path => new URL(`../fixtures/${path}`, import.meta.url)
  14. describe('GetChangesSince', function () {
  15. let projectId, historyId
  16. beforeEach(function (done) {
  17. projectId = new ObjectId().toString()
  18. historyId = new ObjectId().toString()
  19. ProjectHistoryApp.ensureRunning(error => {
  20. if (error) throw error
  21. MockHistoryStore().post('/api/projects').reply(200, {
  22. projectId: historyId,
  23. })
  24. ProjectHistoryClient.initializeProject(historyId, (error, olProject) => {
  25. if (error) throw error
  26. MockWeb()
  27. .get(`/project/${projectId}/details`)
  28. .reply(200, {
  29. name: 'Test Project',
  30. overleaf: { history: { id: olProject.id } },
  31. })
  32. MockHistoryStore()
  33. .get(`/api/projects/${historyId}/latest/history`)
  34. .replyWithFile(200, fixture('chunks/7-8.json'))
  35. MockHistoryStore()
  36. .get(`/api/projects/${historyId}/versions/6/history`)
  37. .replyWithFile(200, fixture('chunks/4-6.json'))
  38. MockHistoryStore()
  39. .get(`/api/projects/${historyId}/versions/3/history`)
  40. .replyWithFile(200, fixture('chunks/0-3.json'))
  41. done()
  42. })
  43. })
  44. })
  45. afterEach(function () {
  46. nock.cleanAll()
  47. })
  48. function expectChangesSince(version, changes, done) {
  49. ProjectHistoryClient.getChangesSince(
  50. projectId,
  51. version,
  52. {},
  53. (error, got) => {
  54. if (error) throw error
  55. expect(got.map(c => Core.Change.fromRaw(c))).to.deep.equal(
  56. changes.map(c => Core.Change.fromRaw(c))
  57. )
  58. done()
  59. }
  60. )
  61. }
  62. it('should return zero changes since the latest version', function (done) {
  63. expectChangesSince(8, [], done)
  64. })
  65. it('should return one change when behind one version', function (done) {
  66. expectChangesSince(7, [latestChunk.chunk.history.changes[1]], done)
  67. })
  68. it('should return changes when at the chunk boundary', function (done) {
  69. expect(latestChunk.chunk.startVersion).to.equal(6)
  70. expectChangesSince(6, latestChunk.chunk.history.changes, done)
  71. })
  72. it('should return changes spanning multiple chunks', function (done) {
  73. expectChangesSince(
  74. 1,
  75. [
  76. ...firstChunk.chunk.history.changes.slice(1),
  77. ...previousChunk.chunk.history.changes,
  78. ...latestChunk.chunk.history.changes,
  79. ],
  80. done
  81. )
  82. })
  83. it('should return all changes when going back to the beginning', function (done) {
  84. expectChangesSince(
  85. 0,
  86. [
  87. ...firstChunk.chunk.history.changes,
  88. ...previousChunk.chunk.history.changes,
  89. ...latestChunk.chunk.history.changes,
  90. ],
  91. done
  92. )
  93. })
  94. it('should return an error when past the end version', function (done) {
  95. ProjectHistoryClient.getChangesSince(
  96. projectId,
  97. 9,
  98. { allowErrors: true },
  99. (error, body, statusCode) => {
  100. if (error) throw error
  101. expect(statusCode).to.equal(500)
  102. expect(body).to.deep.equal({ message: 'an internal error occurred' })
  103. done()
  104. }
  105. )
  106. })
  107. })