LatestSnapshotTests.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(async function () {
  13. await ProjectHistoryApp.ensureRunning()
  14. this.historyId = new ObjectId().toString()
  15. MockHistoryStore().post('/api/projects').reply(200, {
  16. projectId: this.historyId,
  17. })
  18. const v1Project = await ProjectHistoryClient.initializeProject(
  19. this.historyId
  20. )
  21. this.projectId = new ObjectId().toString()
  22. MockWeb()
  23. .get(`/project/${this.projectId}/details`)
  24. .reply(200, {
  25. name: 'Test Project',
  26. overleaf: { history: { id: v1Project.id } },
  27. })
  28. })
  29. afterEach(function () {
  30. nock.cleanAll()
  31. })
  32. it('should return the snapshot with applied changes, metadata and without full content', async function () {
  33. MockHistoryStore()
  34. .get(`/api/projects/${this.historyId}/latest/history`)
  35. .replyWithFile(200, fixture('chunks/0-3.json'))
  36. const fixtureData = JSON.parse(
  37. readFileSync(fixture('chunks/0-3.json'), 'utf8')
  38. )
  39. const changes = fixtureData.chunk.history.changes
  40. const lastTimestamp = changes[changes.length - 1].timestamp
  41. const body = await ProjectHistoryClient.getLatestSnapshot(this.projectId)
  42. expect(body).to.deep.equal({
  43. snapshot: {
  44. files: {
  45. 'main.tex': {
  46. hash: 'f28571f561d198b87c24cc6a98b78e87b665e22d',
  47. stringLength: 20649,
  48. operations: [{ textOperation: [1912, 'Hello world', 18726] }],
  49. metadata: { main: true },
  50. },
  51. 'foo.tex': {
  52. hash: '4f785a4c192155b240e3042b3a7388b47603f423',
  53. stringLength: 41,
  54. operations: [{ textOperation: [26, '\n\nFour five six'] }],
  55. },
  56. },
  57. timestamp: lastTimestamp,
  58. },
  59. version: 3,
  60. })
  61. })
  62. })