LatestSnapshotTests.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { expect } from 'chai'
  2. import mongodb from 'mongodb-legacy'
  3. import nock from 'nock'
  4. import { readFileSync } from 'node:fs'
  5. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  6. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  7. const { ObjectId } = mongodb
  8. const MockHistoryStore = () => nock('http://127.0.0.1:3100')
  9. const MockWeb = () => nock('http://127.0.0.1:3000')
  10. const fixture = path => new URL(`../fixtures/${path}`, import.meta.url)
  11. describe('LatestSnapshot', function () {
  12. beforeEach(function (done) {
  13. ProjectHistoryApp.ensureRunning(error => {
  14. if (error) {
  15. throw error
  16. }
  17. this.historyId = new ObjectId().toString()
  18. MockHistoryStore().post('/api/projects').reply(200, {
  19. projectId: this.historyId,
  20. })
  21. ProjectHistoryClient.initializeProject(
  22. this.historyId,
  23. (error, v1Project) => {
  24. if (error) {
  25. throw error
  26. }
  27. this.projectId = new ObjectId().toString()
  28. MockWeb()
  29. .get(`/project/${this.projectId}/details`)
  30. .reply(200, {
  31. name: 'Test Project',
  32. overleaf: { history: { id: v1Project.id } },
  33. })
  34. done()
  35. }
  36. )
  37. })
  38. })
  39. afterEach(function () {
  40. nock.cleanAll()
  41. })
  42. it('should return the snapshot with applied changes, metadata and without full content', function (done) {
  43. MockHistoryStore()
  44. .get(`/api/projects/${this.historyId}/latest/history`)
  45. .replyWithFile(200, fixture('chunks/0-3.json'))
  46. const fixtureData = JSON.parse(
  47. readFileSync(fixture('chunks/0-3.json'), 'utf8')
  48. )
  49. const changes = fixtureData.chunk.history.changes
  50. const lastTimestamp = changes[changes.length - 1].timestamp
  51. ProjectHistoryClient.getLatestSnapshot(this.projectId, (error, body) => {
  52. if (error) {
  53. throw error
  54. }
  55. expect(body).to.deep.equal({
  56. snapshot: {
  57. files: {
  58. 'main.tex': {
  59. hash: 'f28571f561d198b87c24cc6a98b78e87b665e22d',
  60. stringLength: 20649,
  61. operations: [{ textOperation: [1912, 'Hello world', 18726] }],
  62. metadata: { main: true },
  63. },
  64. 'foo.tex': {
  65. hash: '4f785a4c192155b240e3042b3a7388b47603f423',
  66. stringLength: 41,
  67. operations: [{ textOperation: [26, '\n\nFour five six'] }],
  68. },
  69. },
  70. timestamp: lastTimestamp,
  71. },
  72. version: 3,
  73. })
  74. done()
  75. })
  76. })
  77. })