LatestSnapshotTests.js 2.2 KB

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