SummarisedUpdatesTests.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* eslint-disable
  2. no-undef,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import sinon from 'sinon'
  14. import { expect } from 'chai'
  15. import Settings from '@overleaf/settings'
  16. import request from 'request'
  17. import assert from 'node:assert'
  18. import mongodb from 'mongodb-legacy'
  19. import nock from 'nock'
  20. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  21. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  22. const { ObjectId } = mongodb
  23. const MockHistoryStore = () => nock('http://127.0.0.1:3100')
  24. const MockFileStore = () => nock('http://127.0.0.1:3009')
  25. const MockWeb = () => nock('http://127.0.0.1:3000')
  26. const fixture = path => new URL(`../fixtures/${path}`, import.meta.url)
  27. describe('Summarized updates', function () {
  28. beforeEach(function (done) {
  29. this.projectId = new ObjectId().toString()
  30. this.historyId = new ObjectId().toString()
  31. return ProjectHistoryApp.ensureRunning(error => {
  32. if (error != null) {
  33. throw error
  34. }
  35. MockHistoryStore().post('/api/projects').reply(200, {
  36. projectId: this.historyId,
  37. })
  38. return ProjectHistoryClient.initializeProject(
  39. this.historyId,
  40. (error, olProject) => {
  41. if (error != null) {
  42. throw error
  43. }
  44. MockWeb()
  45. .get(`/project/${this.projectId}/details`)
  46. .reply(200, {
  47. name: 'Test Project',
  48. overleaf: { history: { id: olProject.id } },
  49. })
  50. MockHistoryStore()
  51. .get(`/api/projects/${this.historyId}/latest/history`)
  52. .replyWithFile(200, fixture('chunks/7-8.json'))
  53. MockHistoryStore()
  54. .get(`/api/projects/${this.historyId}/versions/6/history`)
  55. .replyWithFile(200, fixture('chunks/4-6.json'))
  56. MockHistoryStore()
  57. .get(`/api/projects/${this.historyId}/versions/3/history`)
  58. .replyWithFile(200, fixture('chunks/0-3.json'))
  59. return done()
  60. }
  61. )
  62. })
  63. })
  64. afterEach(function () {
  65. return nock.cleanAll()
  66. })
  67. it('should return the latest summarized updates from a single chunk', function (done) {
  68. return ProjectHistoryClient.getSummarizedUpdates(
  69. this.projectId,
  70. { min_count: 1 },
  71. (error, updates) => {
  72. if (error != null) {
  73. throw error
  74. }
  75. expect(updates).to.deep.equal({
  76. nextBeforeTimestamp: 6,
  77. updates: [
  78. {
  79. fromV: 6,
  80. toV: 8,
  81. meta: {
  82. users: ['5a5637efdac84e81b71014c4', 31],
  83. start_ts: 1512383567277,
  84. end_ts: 1512383572877,
  85. },
  86. pathnames: ['bar.tex', 'main.tex'],
  87. project_ops: [],
  88. labels: [],
  89. },
  90. ],
  91. })
  92. return done()
  93. }
  94. )
  95. })
  96. it('should return the latest summarized updates, with min_count spanning multiple chunks', function (done) {
  97. return ProjectHistoryClient.getSummarizedUpdates(
  98. this.projectId,
  99. { min_count: 5 },
  100. (error, updates) => {
  101. if (error != null) {
  102. throw error
  103. }
  104. expect(updates).to.deep.equal({
  105. updates: [
  106. {
  107. fromV: 6,
  108. toV: 8,
  109. meta: {
  110. users: ['5a5637efdac84e81b71014c4', 31],
  111. start_ts: 1512383567277,
  112. end_ts: 1512383572877,
  113. },
  114. pathnames: ['bar.tex', 'main.tex'],
  115. project_ops: [],
  116. labels: [],
  117. },
  118. {
  119. fromV: 5,
  120. toV: 6,
  121. meta: {
  122. users: [31],
  123. start_ts: 1512383366120,
  124. end_ts: 1512383366120,
  125. },
  126. pathnames: [],
  127. project_ops: [
  128. {
  129. atV: 5,
  130. rename: {
  131. pathname: 'foo.tex',
  132. newPathname: 'bar.tex',
  133. },
  134. },
  135. ],
  136. labels: [],
  137. },
  138. {
  139. fromV: 2,
  140. toV: 5,
  141. meta: {
  142. users: [31],
  143. start_ts: 1512383313724,
  144. end_ts: 1512383362905,
  145. },
  146. pathnames: ['foo.tex'],
  147. project_ops: [],
  148. labels: [],
  149. },
  150. {
  151. fromV: 1,
  152. toV: 2,
  153. meta: {
  154. users: [31],
  155. start_ts: 1512383246874,
  156. end_ts: 1512383246874,
  157. },
  158. pathnames: [],
  159. project_ops: [
  160. {
  161. atV: 1,
  162. rename: {
  163. pathname: 'bar.tex',
  164. newPathname: 'foo.tex',
  165. },
  166. },
  167. ],
  168. labels: [],
  169. },
  170. {
  171. fromV: 0,
  172. toV: 1,
  173. meta: {
  174. users: [31],
  175. start_ts: 1512383015633,
  176. end_ts: 1512383015633,
  177. },
  178. pathnames: ['main.tex'],
  179. project_ops: [],
  180. labels: [],
  181. },
  182. ],
  183. })
  184. return done()
  185. }
  186. )
  187. })
  188. it('should return the summarized updates from a before version at the start of a chunk', function (done) {
  189. MockHistoryStore()
  190. .get(`/api/projects/${this.historyId}/versions/4/history`)
  191. .replyWithFile(200, fixture('chunks/4-6.json'))
  192. return ProjectHistoryClient.getSummarizedUpdates(
  193. this.projectId,
  194. { before: 4 },
  195. (error, updates) => {
  196. if (error != null) {
  197. throw error
  198. }
  199. expect(updates.updates[0].toV).to.equal(4)
  200. return done()
  201. }
  202. )
  203. })
  204. it('should return the summarized updates from a before version in the middle of a chunk', function (done) {
  205. MockHistoryStore()
  206. .get(`/api/projects/${this.historyId}/versions/5/history`)
  207. .replyWithFile(200, fixture('chunks/4-6.json'))
  208. return ProjectHistoryClient.getSummarizedUpdates(
  209. this.projectId,
  210. { before: 5 },
  211. (error, updates) => {
  212. if (error != null) {
  213. throw error
  214. }
  215. expect(updates.updates[0].toV).to.equal(5)
  216. return done()
  217. }
  218. )
  219. })
  220. return it('should return the summarized updates from a before version at the end of a chunk', function (done) {
  221. MockHistoryStore()
  222. .get(`/api/projects/${this.historyId}/versions/6/history`)
  223. .replyWithFile(200, fixture('chunks/4-6.json'))
  224. return ProjectHistoryClient.getSummarizedUpdates(
  225. this.projectId,
  226. { before: 6 },
  227. (error, updates) => {
  228. if (error != null) {
  229. throw error
  230. }
  231. expect(updates.updates[0].toV).to.equal(6)
  232. return done()
  233. }
  234. )
  235. })
  236. })