SummarisedUpdatesTests.js 7.1 KB

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