SummarisedUpdatesTests.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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('Summarized updates', function () {
  11. beforeEach(async function () {
  12. this.projectId = new ObjectId().toString()
  13. this.historyId = new ObjectId().toString()
  14. await ProjectHistoryApp.ensureRunning()
  15. MockHistoryStore().post('/api/projects').reply(200, {
  16. projectId: this.historyId,
  17. })
  18. const olProject = await ProjectHistoryClient.initializeProject(
  19. this.historyId
  20. )
  21. MockWeb()
  22. .get(`/project/${this.projectId}/details`)
  23. .reply(200, {
  24. name: 'Test Project',
  25. overleaf: { history: { id: olProject.id } },
  26. })
  27. MockHistoryStore()
  28. .get(`/api/projects/${this.historyId}/latest/history`)
  29. .replyWithFile(200, fixture('chunks/7-8.json'))
  30. MockHistoryStore()
  31. .get(`/api/projects/${this.historyId}/versions/6/history`)
  32. .replyWithFile(200, fixture('chunks/4-6.json'))
  33. MockHistoryStore()
  34. .get(`/api/projects/${this.historyId}/versions/3/history`)
  35. .replyWithFile(200, fixture('chunks/0-3.json'))
  36. })
  37. afterEach(function () {
  38. return nock.cleanAll()
  39. })
  40. it('should return the latest summarized updates from a single chunk', async function () {
  41. const updates = await ProjectHistoryClient.getSummarizedUpdates(
  42. this.projectId,
  43. { min_count: 1 }
  44. )
  45. expect(updates).to.deep.equal({
  46. nextBeforeTimestamp: 6,
  47. updates: [
  48. {
  49. fromV: 6,
  50. toV: 8,
  51. meta: {
  52. users: ['5a5637efdac84e81b71014c4', 31],
  53. start_ts: 1512383567277,
  54. end_ts: 1512383572877,
  55. },
  56. pathnames: ['bar.tex', 'main.tex'],
  57. project_ops: [],
  58. labels: [],
  59. },
  60. ],
  61. })
  62. })
  63. it('should return the latest summarized updates, with min_count spanning multiple chunks', async function () {
  64. const updates = await ProjectHistoryClient.getSummarizedUpdates(
  65. this.projectId,
  66. { min_count: 5 }
  67. )
  68. expect(updates).to.deep.equal({
  69. updates: [
  70. {
  71. fromV: 6,
  72. toV: 8,
  73. meta: {
  74. users: ['5a5637efdac84e81b71014c4', 31],
  75. start_ts: 1512383567277,
  76. end_ts: 1512383572877,
  77. },
  78. pathnames: ['bar.tex', 'main.tex'],
  79. project_ops: [],
  80. labels: [],
  81. },
  82. {
  83. fromV: 5,
  84. toV: 6,
  85. meta: {
  86. users: [31],
  87. start_ts: 1512383366120,
  88. end_ts: 1512383366120,
  89. },
  90. pathnames: [],
  91. project_ops: [
  92. {
  93. atV: 5,
  94. rename: {
  95. pathname: 'foo.tex',
  96. newPathname: 'bar.tex',
  97. },
  98. },
  99. ],
  100. labels: [],
  101. },
  102. {
  103. fromV: 2,
  104. toV: 5,
  105. meta: {
  106. users: [31],
  107. start_ts: 1512383313724,
  108. end_ts: 1512383362905,
  109. },
  110. pathnames: ['foo.tex'],
  111. project_ops: [],
  112. labels: [],
  113. },
  114. {
  115. fromV: 1,
  116. toV: 2,
  117. meta: {
  118. users: [31],
  119. start_ts: 1512383246874,
  120. end_ts: 1512383246874,
  121. },
  122. pathnames: [],
  123. project_ops: [
  124. {
  125. atV: 1,
  126. rename: {
  127. pathname: 'bar.tex',
  128. newPathname: 'foo.tex',
  129. },
  130. },
  131. ],
  132. labels: [],
  133. },
  134. {
  135. fromV: 0,
  136. toV: 1,
  137. meta: {
  138. users: [31],
  139. start_ts: 1512383015633,
  140. end_ts: 1512383015633,
  141. },
  142. pathnames: ['main.tex'],
  143. project_ops: [],
  144. labels: [],
  145. },
  146. ],
  147. })
  148. })
  149. it('should return the summarized updates from a before version at the start of a chunk', async function () {
  150. MockHistoryStore()
  151. .get(`/api/projects/${this.historyId}/versions/4/history`)
  152. .replyWithFile(200, fixture('chunks/4-6.json'))
  153. const updates = await ProjectHistoryClient.getSummarizedUpdates(
  154. this.projectId,
  155. { before: 4 }
  156. )
  157. expect(updates.updates[0].toV).to.equal(4)
  158. })
  159. it('should return the summarized updates from a before version in the middle of a chunk', async function () {
  160. MockHistoryStore()
  161. .get(`/api/projects/${this.historyId}/versions/5/history`)
  162. .replyWithFile(200, fixture('chunks/4-6.json'))
  163. const updates = await ProjectHistoryClient.getSummarizedUpdates(
  164. this.projectId,
  165. { before: 5 }
  166. )
  167. expect(updates.updates[0].toV).to.equal(5)
  168. })
  169. it('should return the summarized updates from a before version at the end of a chunk', async function () {
  170. MockHistoryStore()
  171. .get(`/api/projects/${this.historyId}/versions/6/history`)
  172. .replyWithFile(200, fixture('chunks/4-6.json'))
  173. const updates = await ProjectHistoryClient.getSummarizedUpdates(
  174. this.projectId,
  175. { before: 6 }
  176. )
  177. expect(updates.updates[0].toV).to.equal(6)
  178. })
  179. })