HistoryManagerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* eslint-disable
  2. mocha/no-nested-tests,
  3. no-return-assign,
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module');
  13. const sinon = require('sinon');
  14. require('chai').should();
  15. const modulePath = require('path').join(__dirname, '../../../../app/js/HistoryManager');
  16. describe("HistoryManager", function() {
  17. beforeEach(function() {
  18. this.HistoryManager = SandboxedModule.require(modulePath, { requires: {
  19. "request": (this.request = {}),
  20. "settings-sharelatex": (this.Settings = {
  21. apis: {
  22. project_history: {
  23. enabled: true,
  24. url: "http://project_history.example.com"
  25. },
  26. trackchanges: {
  27. url: "http://trackchanges.example.com"
  28. }
  29. }
  30. }),
  31. "logger-sharelatex": (this.logger = { log: sinon.stub(), error: sinon.stub(), debug: sinon.stub() }),
  32. "./DocumentManager": (this.DocumentManager = {}),
  33. "./HistoryRedisManager": (this.HistoryRedisManager = {}),
  34. "./RedisManager": (this.RedisManager = {}),
  35. "./ProjectHistoryRedisManager": (this.ProjectHistoryRedisManager = {}),
  36. "./Metrics": (this.metrics = {inc: sinon.stub()})
  37. }
  38. });
  39. this.project_id = "mock-project-id";
  40. this.doc_id = "mock-doc-id";
  41. return this.callback = sinon.stub();
  42. });
  43. describe("flushDocChangesAsync", function() {
  44. beforeEach(function() {
  45. return this.request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204});
  46. });
  47. describe("when the project uses track changes", function() {
  48. beforeEach(function() {
  49. this.RedisManager.getHistoryType = sinon.stub().yields(null, 'track-changes');
  50. return this.HistoryManager.flushDocChangesAsync(this.project_id, this.doc_id);
  51. });
  52. return it("should send a request to the track changes api", function() {
  53. return this.request.post
  54. .calledWith(`${this.Settings.apis.trackchanges.url}/project/${this.project_id}/doc/${this.doc_id}/flush`)
  55. .should.equal(true);
  56. });
  57. });
  58. describe("when the project uses project history and double flush is not disabled", function() {
  59. beforeEach(function() {
  60. this.RedisManager.getHistoryType = sinon.stub().yields(null, 'project-history');
  61. return this.HistoryManager.flushDocChangesAsync(this.project_id, this.doc_id);
  62. });
  63. return it("should send a request to the track changes api", function() {
  64. return this.request.post
  65. .called
  66. .should.equal(true);
  67. });
  68. });
  69. return describe("when the project uses project history and double flush is disabled", function() {
  70. beforeEach(function() {
  71. this.Settings.disableDoubleFlush = true;
  72. this.RedisManager.getHistoryType = sinon.stub().yields(null, 'project-history');
  73. return this.HistoryManager.flushDocChangesAsync(this.project_id, this.doc_id);
  74. });
  75. return it("should not send a request to the track changes api", function() {
  76. return this.request.post
  77. .called
  78. .should.equal(false);
  79. });
  80. });
  81. });
  82. describe("flushProjectChangesAsync", function() {
  83. beforeEach(function() {
  84. this.request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204});
  85. return this.HistoryManager.flushProjectChangesAsync(this.project_id);
  86. });
  87. return it("should send a request to the project history api", function() {
  88. return this.request.post
  89. .calledWith({url: `${this.Settings.apis.project_history.url}/project/${this.project_id}/flush`, qs:{background:true}})
  90. .should.equal(true);
  91. });
  92. });
  93. describe("flushProjectChanges", function() {
  94. describe("in the normal case", function() {
  95. beforeEach(function() {
  96. this.request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204});
  97. return this.HistoryManager.flushProjectChanges(this.project_id, {background:true});});
  98. return it("should send a request to the project history api", function() {
  99. return this.request.post
  100. .calledWith({url: `${this.Settings.apis.project_history.url}/project/${this.project_id}/flush`, qs:{background:true}})
  101. .should.equal(true);
  102. });
  103. });
  104. return describe("with the skip_history_flush option", function() {
  105. beforeEach(function() {
  106. this.request.post = sinon.stub();
  107. return this.HistoryManager.flushProjectChanges(this.project_id, {skip_history_flush:true});});
  108. return it("should not send a request to the project history api", function() {
  109. return this.request.post
  110. .called
  111. .should.equal(false);
  112. });
  113. });
  114. });
  115. describe("recordAndFlushHistoryOps", function() {
  116. beforeEach(function() {
  117. this.ops = [ 'mock-ops' ];
  118. this.project_ops_length = 10;
  119. this.doc_ops_length = 5;
  120. this.HistoryManager.flushProjectChangesAsync = sinon.stub();
  121. this.HistoryRedisManager.recordDocHasHistoryOps = sinon.stub().callsArg(3);
  122. return this.HistoryManager.flushDocChangesAsync = sinon.stub();
  123. });
  124. describe("with no ops", function() {
  125. beforeEach(function() {
  126. return this.HistoryManager.recordAndFlushHistoryOps(
  127. this.project_id, this.doc_id, [], this.doc_ops_length, this.project_ops_length, this.callback
  128. );
  129. });
  130. it("should not flush project changes", function() {
  131. return this.HistoryManager.flushProjectChangesAsync.called.should.equal(false);
  132. });
  133. it("should not record doc has history ops", function() {
  134. return this.HistoryRedisManager.recordDocHasHistoryOps.called.should.equal(false);
  135. });
  136. it("should not flush doc changes", function() {
  137. return this.HistoryManager.flushDocChangesAsync.called.should.equal(false);
  138. });
  139. return it("should call the callback", function() {
  140. return this.callback.called.should.equal(true);
  141. });
  142. });
  143. describe("with enough ops to flush project changes", function() {
  144. beforeEach(function() {
  145. this.HistoryManager.shouldFlushHistoryOps = sinon.stub();
  146. this.HistoryManager.shouldFlushHistoryOps.withArgs(this.project_ops_length).returns(true);
  147. this.HistoryManager.shouldFlushHistoryOps.withArgs(this.doc_ops_length).returns(false);
  148. return this.HistoryManager.recordAndFlushHistoryOps(
  149. this.project_id, this.doc_id, this.ops, this.doc_ops_length, this.project_ops_length, this.callback
  150. );
  151. });
  152. it("should flush project changes", function() {
  153. return this.HistoryManager.flushProjectChangesAsync
  154. .calledWith(this.project_id)
  155. .should.equal(true);
  156. });
  157. it("should record doc has history ops", function() {
  158. return this.HistoryRedisManager.recordDocHasHistoryOps
  159. .calledWith(this.project_id, this.doc_id, this.ops);
  160. });
  161. it("should not flush doc changes", function() {
  162. return this.HistoryManager.flushDocChangesAsync.called.should.equal(false);
  163. });
  164. return it("should call the callback", function() {
  165. return this.callback.called.should.equal(true);
  166. });
  167. });
  168. describe("with enough ops to flush doc changes", function() {
  169. beforeEach(function() {
  170. this.HistoryManager.shouldFlushHistoryOps = sinon.stub();
  171. this.HistoryManager.shouldFlushHistoryOps.withArgs(this.project_ops_length).returns(false);
  172. this.HistoryManager.shouldFlushHistoryOps.withArgs(this.doc_ops_length).returns(true);
  173. return this.HistoryManager.recordAndFlushHistoryOps(
  174. this.project_id, this.doc_id, this.ops, this.doc_ops_length, this.project_ops_length, this.callback
  175. );
  176. });
  177. it("should not flush project changes", function() {
  178. return this.HistoryManager.flushProjectChangesAsync.called.should.equal(false);
  179. });
  180. it("should record doc has history ops", function() {
  181. return this.HistoryRedisManager.recordDocHasHistoryOps
  182. .calledWith(this.project_id, this.doc_id, this.ops);
  183. });
  184. it("should flush doc changes", function() {
  185. return this.HistoryManager.flushDocChangesAsync
  186. .calledWith(this.project_id, this.doc_id)
  187. .should.equal(true);
  188. });
  189. return it("should call the callback", function() {
  190. return this.callback.called.should.equal(true);
  191. });
  192. });
  193. describe("when recording doc has history ops errors", function() {
  194. beforeEach(function() {
  195. this.error = new Error("error");
  196. this.HistoryRedisManager.recordDocHasHistoryOps =
  197. sinon.stub().callsArgWith(3, this.error);
  198. return this.HistoryManager.recordAndFlushHistoryOps(
  199. this.project_id, this.doc_id, this.ops, this.doc_ops_length, this.project_ops_length, this.callback
  200. );
  201. });
  202. it("should not flush doc changes", function() {
  203. return this.HistoryManager.flushDocChangesAsync.called.should.equal(false);
  204. });
  205. return it("should call the callback with the error", function() {
  206. return this.callback.calledWith(this.error).should.equal(true);
  207. });
  208. });
  209. return describe("shouldFlushHistoryOps", function() {
  210. it("should return false if the number of ops is not known", function() {
  211. return this.HistoryManager.shouldFlushHistoryOps(null, ['a', 'b', 'c'].length, 1).should.equal(false);
  212. });
  213. it("should return false if the updates didn't take us past the threshold", function() {
  214. // Currently there are 14 ops
  215. // Previously we were on 11 ops
  216. // We didn't pass over a multiple of 5
  217. this.HistoryManager.shouldFlushHistoryOps(14, ['a', 'b', 'c'].length, 5).should.equal(false);
  218. it("should return true if the updates took to the threshold", function() {});
  219. // Currently there are 15 ops
  220. // Previously we were on 12 ops
  221. // We've reached a new multiple of 5
  222. return this.HistoryManager.shouldFlushHistoryOps(15, ['a', 'b', 'c'].length, 5).should.equal(true);
  223. });
  224. return it("should return true if the updates took past the threshold", function() {
  225. // Currently there are 19 ops
  226. // Previously we were on 16 ops
  227. // We didn't pass over a multiple of 5
  228. return this.HistoryManager.shouldFlushHistoryOps(17, ['a', 'b', 'c'].length, 5).should.equal(true);
  229. });
  230. });
  231. });
  232. return describe("resyncProjectHistory", function() {
  233. beforeEach(function() {
  234. this.projectHistoryId = 'history-id-1234';
  235. this.docs = [{
  236. doc: this.doc_id,
  237. path: 'main.tex'
  238. }
  239. ];
  240. this.files = [{
  241. file: 'mock-file-id',
  242. path: 'universe.png',
  243. url: `www.filestore.test/${this.project_id}/mock-file-id`
  244. }
  245. ];
  246. this.ProjectHistoryRedisManager.queueResyncProjectStructure = sinon.stub().yields();
  247. this.DocumentManager.resyncDocContentsWithLock = sinon.stub().yields();
  248. return this.HistoryManager.resyncProjectHistory(this.project_id, this.projectHistoryId, this.docs, this.files, this.callback);
  249. });
  250. it("should queue a project structure reync", function() {
  251. return this.ProjectHistoryRedisManager.queueResyncProjectStructure
  252. .calledWith(this.project_id, this.projectHistoryId, this.docs, this.files)
  253. .should.equal(true);
  254. });
  255. it("should queue doc content reyncs", function() {
  256. return this.DocumentManager
  257. .resyncDocContentsWithLock
  258. .calledWith(this.project_id, this.doc_id)
  259. .should.equal(true);
  260. });
  261. return it("should call the callback", function() {
  262. return this.callback.called.should.equal(true);
  263. });
  264. });
  265. });