ProjectHistoryRedisManagerTests.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon');
  15. const chai = require('chai');
  16. const should = chai.should();
  17. const modulePath = "../../../../app/js/ProjectHistoryRedisManager.js";
  18. const SandboxedModule = require('sandboxed-module');
  19. const tk = require("timekeeper");
  20. describe("ProjectHistoryRedisManager", function() {
  21. beforeEach(function() {
  22. this.project_id = "project-id-123";
  23. this.projectHistoryId = "history-id-123";
  24. this.user_id = "user-id-123";
  25. this.callback = sinon.stub();
  26. this.rclient = {};
  27. tk.freeze(new Date());
  28. return this.ProjectHistoryRedisManager = SandboxedModule.require(modulePath, {
  29. requires: {
  30. "settings-sharelatex": (this.settings = {
  31. redis: {
  32. project_history: {
  33. key_schema: {
  34. projectHistoryOps({project_id}) { return `ProjectHistory:Ops:${project_id}`; },
  35. projectHistoryFirstOpTimestamp({project_id}) { return `ProjectHistory:FirstOpTimestamp:${project_id}`; }
  36. }
  37. }
  38. }
  39. }),
  40. "redis-sharelatex": {
  41. createClient: () => this.rclient
  42. },
  43. "logger-sharelatex": {
  44. log() {}
  45. },
  46. "./Metrics": (this.metrics = { summary: sinon.stub()})
  47. },
  48. globals: {
  49. JSON: (this.JSON = JSON)
  50. }
  51. }
  52. );
  53. });
  54. afterEach(function() { return tk.reset(); });
  55. describe("queueOps", function() {
  56. beforeEach(function() {
  57. this.ops = ["mock-op-1", "mock-op-2"];
  58. this.multi = {exec: sinon.stub()};
  59. this.multi.rpush = sinon.stub();
  60. this.multi.setnx = sinon.stub();
  61. this.rclient.multi = () => this.multi;
  62. // @rclient = multi: () => @multi
  63. return this.ProjectHistoryRedisManager.queueOps(this.project_id, ...Array.from(this.ops), this.callback);
  64. });
  65. it("should queue an update", function() {
  66. return this.multi.rpush
  67. .calledWithExactly(
  68. `ProjectHistory:Ops:${this.project_id}`,
  69. this.ops[0],
  70. this.ops[1]
  71. ).should.equal(true);
  72. });
  73. return it("should set the queue timestamp if not present", function() {
  74. return this.multi.setnx
  75. .calledWithExactly(
  76. `ProjectHistory:FirstOpTimestamp:${this.project_id}`,
  77. Date.now()
  78. ).should.equal(true);
  79. });
  80. });
  81. describe("queueRenameEntity", function() {
  82. beforeEach(function() {
  83. this.file_id = 1234;
  84. this.rawUpdate = {
  85. pathname: (this.pathname = '/old'),
  86. newPathname: (this.newPathname = '/new'),
  87. version: (this.version = 2)
  88. };
  89. this.ProjectHistoryRedisManager.queueOps = sinon.stub();
  90. return this.ProjectHistoryRedisManager.queueRenameEntity(this.project_id, this.projectHistoryId, 'file', this.file_id, this.user_id, this.rawUpdate, this.callback);
  91. });
  92. return it("should queue an update", function() {
  93. const update = {
  94. pathname: this.pathname,
  95. new_pathname: this.newPathname,
  96. meta: {
  97. user_id: this.user_id,
  98. ts: new Date()
  99. },
  100. version: this.version,
  101. projectHistoryId: this.projectHistoryId,
  102. file: this.file_id
  103. };
  104. return this.ProjectHistoryRedisManager.queueOps
  105. .calledWithExactly(this.project_id, this.JSON.stringify(update), this.callback)
  106. .should.equal(true);
  107. });
  108. });
  109. return describe("queueAddEntity", function() {
  110. beforeEach(function() {
  111. this.rclient.rpush = sinon.stub().yields();
  112. this.doc_id = 1234;
  113. this.rawUpdate = {
  114. pathname: (this.pathname = '/old'),
  115. docLines: (this.docLines = 'a\nb'),
  116. version: (this.version = 2),
  117. url: (this.url = 'filestore.example.com')
  118. };
  119. this.ProjectHistoryRedisManager.queueOps = sinon.stub();
  120. return this.ProjectHistoryRedisManager.queueAddEntity(this.project_id, this.projectHistoryId, 'doc', this.doc_id, this.user_id, this.rawUpdate, this.callback);
  121. });
  122. it("should queue an update", function() {
  123. const update = {
  124. pathname: this.pathname,
  125. docLines: this.docLines,
  126. url: this.url,
  127. meta: {
  128. user_id: this.user_id,
  129. ts: new Date()
  130. },
  131. version: this.version,
  132. projectHistoryId: this.projectHistoryId,
  133. doc: this.doc_id
  134. };
  135. return this.ProjectHistoryRedisManager.queueOps
  136. .calledWithExactly(this.project_id, this.JSON.stringify(update), this.callback)
  137. .should.equal(true);
  138. });
  139. describe("queueResyncProjectStructure", function() { return it("should queue an update", function() {}); });
  140. return describe("queueResyncDocContent", function() { return it("should queue an update", function() {}); });
  141. });
  142. });