getProjectDocsTests.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* eslint-disable
  2. no-return-assign,
  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. * DS206: Consider reworking classes to avoid initClass
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon');
  14. const chai = require('chai');
  15. const should = chai.should();
  16. const modulePath = "../../../../app/js/ProjectManager.js";
  17. const SandboxedModule = require('sandboxed-module');
  18. const Errors = require("../../../../app/js/Errors.js");
  19. describe("ProjectManager - getProjectDocsAndFlushIfOld", function() {
  20. beforeEach(function() {
  21. let Timer;
  22. this.ProjectManager = SandboxedModule.require(modulePath, { requires: {
  23. "./RedisManager": (this.RedisManager = {}),
  24. "./ProjectHistoryRedisManager": (this.ProjectHistoryRedisManager = {}),
  25. "./DocumentManager": (this.DocumentManager = {}),
  26. "logger-sharelatex": (this.logger = { log: sinon.stub(), error: sinon.stub() }),
  27. "./HistoryManager": (this.HistoryManager = {}),
  28. "./Metrics": (this.Metrics = {
  29. Timer: (Timer = (function() {
  30. Timer = class Timer {
  31. static initClass() {
  32. this.prototype.done = sinon.stub();
  33. }
  34. };
  35. Timer.initClass();
  36. return Timer;
  37. })())
  38. })
  39. }
  40. }
  41. );
  42. this.project_id = "project-id-123";
  43. this.callback = sinon.stub();
  44. return this.doc_versions = [111, 222, 333];});
  45. describe("successfully", function() {
  46. beforeEach(function(done) {
  47. this.doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"];
  48. this.doc_lines = [["aaa","aaa"],["bbb","bbb"],["ccc","ccc"]];
  49. this.docs = [
  50. {_id: this.doc_ids[0], lines: this.doc_lines[0], v: this.doc_versions[0]},
  51. {_id: this.doc_ids[1], lines: this.doc_lines[1], v: this.doc_versions[1]},
  52. {_id: this.doc_ids[2], lines: this.doc_lines[2], v: this.doc_versions[2]}
  53. ];
  54. this.RedisManager.checkOrSetProjectState = sinon.stub().callsArgWith(2, null);
  55. this.RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, this.doc_ids);
  56. this.DocumentManager.getDocAndFlushIfOldWithLock = sinon.stub();
  57. this.DocumentManager.getDocAndFlushIfOldWithLock.withArgs(this.project_id, this.doc_ids[0])
  58. .callsArgWith(2, null, this.doc_lines[0], this.doc_versions[0]);
  59. this.DocumentManager.getDocAndFlushIfOldWithLock.withArgs(this.project_id, this.doc_ids[1])
  60. .callsArgWith(2, null, this.doc_lines[1], this.doc_versions[1]);
  61. this.DocumentManager.getDocAndFlushIfOldWithLock.withArgs(this.project_id, this.doc_ids[2])
  62. .callsArgWith(2, null, this.doc_lines[2], this.doc_versions[2]);
  63. return this.ProjectManager.getProjectDocsAndFlushIfOld(this.project_id, this.projectStateHash, this.excludeVersions, (error, docs) => {
  64. this.callback(error, docs);
  65. return done();
  66. });
  67. });
  68. it("should check the project state", function() {
  69. return this.RedisManager.checkOrSetProjectState
  70. .calledWith(this.project_id, this.projectStateHash)
  71. .should.equal(true);
  72. });
  73. it("should get the doc ids in the project", function() {
  74. return this.RedisManager.getDocIdsInProject
  75. .calledWith(this.project_id)
  76. .should.equal(true);
  77. });
  78. it("should call the callback without error", function() {
  79. return this.callback.calledWith(null, this.docs).should.equal(true);
  80. });
  81. return it("should time the execution", function() {
  82. return this.Metrics.Timer.prototype.done.called.should.equal(true);
  83. });
  84. });
  85. describe("when the state does not match", function() {
  86. beforeEach(function(done) {
  87. this.doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"];
  88. this.RedisManager.checkOrSetProjectState = sinon.stub().callsArgWith(2, null, true);
  89. return this.ProjectManager.getProjectDocsAndFlushIfOld(this.project_id, this.projectStateHash, this.excludeVersions, (error, docs) => {
  90. this.callback(error, docs);
  91. return done();
  92. });
  93. });
  94. it("should check the project state", function() {
  95. return this.RedisManager.checkOrSetProjectState
  96. .calledWith(this.project_id, this.projectStateHash)
  97. .should.equal(true);
  98. });
  99. it("should call the callback with an error", function() {
  100. return this.callback.calledWith(new Errors.ProjectStateChangedError("project state changed")).should.equal(true);
  101. });
  102. return it("should time the execution", function() {
  103. return this.Metrics.Timer.prototype.done.called.should.equal(true);
  104. });
  105. });
  106. describe("when a doc errors", function() {
  107. beforeEach(function(done) {
  108. this.doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"];
  109. this.RedisManager.checkOrSetProjectState = sinon.stub().callsArgWith(2, null);
  110. this.RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, this.doc_ids);
  111. this.DocumentManager.getDocAndFlushIfOldWithLock = sinon.stub();
  112. this.DocumentManager.getDocAndFlushIfOldWithLock.withArgs(this.project_id, "doc-id-1")
  113. .callsArgWith(2, null, ["test doc content"], this.doc_versions[1]);
  114. this.DocumentManager.getDocAndFlushIfOldWithLock.withArgs(this.project_id, "doc-id-2")
  115. .callsArgWith(2, (this.error = new Error("oops"))); // trigger an error
  116. return this.ProjectManager.getProjectDocsAndFlushIfOld(this.project_id, this.projectStateHash, this.excludeVersions, (error, docs) => {
  117. this.callback(error);
  118. return done();
  119. });
  120. });
  121. it("should record the error", function() {
  122. return this.logger.error
  123. .calledWith({err: this.error, project_id: this.project_id, doc_id: "doc-id-2"}, "error getting project doc lines in getProjectDocsAndFlushIfOld")
  124. .should.equal(true);
  125. });
  126. it("should call the callback with an error", function() {
  127. return this.callback.calledWith(new Error("oops")).should.equal(true);
  128. });
  129. return it("should time the execution", function() {
  130. return this.Metrics.Timer.prototype.done.called.should.equal(true);
  131. });
  132. });
  133. return describe("clearing the project state with clearProjectState", function() {
  134. beforeEach(function(done) {
  135. this.RedisManager.clearProjectState = sinon.stub().callsArg(1);
  136. return this.ProjectManager.clearProjectState(this.project_id, error => {
  137. this.callback(error);
  138. return done();
  139. });
  140. });
  141. it("should clear the project state", function() {
  142. return this.RedisManager.clearProjectState
  143. .calledWith(this.project_id)
  144. .should.equal(true);
  145. });
  146. return it("should call the callback", function() {
  147. return this.callback.called.should.equal(true);
  148. });
  149. });
  150. });