GettingUpdatesTests.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* eslint-disable
  2. chai-friendly/no-unused-expressions,
  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. const sinon = require("sinon");
  14. const chai = require("chai");
  15. chai.should();
  16. const { expect } = chai;
  17. const mongojs = require("../../../app/js/mongojs");
  18. const { db } = mongojs;
  19. const { ObjectId } = mongojs;
  20. const Settings = require("settings-sharelatex");
  21. const TrackChangesApp = require("./helpers/TrackChangesApp");
  22. const TrackChangesClient = require("./helpers/TrackChangesClient");
  23. const MockWebApi = require("./helpers/MockWebApi");
  24. describe("Getting updates", function() {
  25. before(function(done) {
  26. this.now = Date.now();
  27. this.to = this.now;
  28. this.user_id = ObjectId().toString();
  29. this.deleted_user_id = 'deleted_user';
  30. this.doc_id = ObjectId().toString();
  31. this.project_id = ObjectId().toString();
  32. this.minutes = 60 * 1000;
  33. this.hours = 60 * this.minutes;
  34. MockWebApi.projects[this.project_id] = {
  35. features: {
  36. versioning: true
  37. }
  38. };
  39. MockWebApi.users[this.user_id] = (this.user = {
  40. email: "user@sharelatex.com",
  41. first_name: "Leo",
  42. last_name: "Lion",
  43. id: this.user_id
  44. });
  45. sinon.spy(MockWebApi, "getUserInfo");
  46. this.updates = [];
  47. for (let i = 0; i <= 9; i++) {
  48. this.updates.push({
  49. op: [{ i: "a", p: 0 }],
  50. meta: { ts: this.now - ((9 - i) * this.hours) - (2 * this.minutes), user_id: this.user_id },
  51. v: (2 * i) + 1
  52. });
  53. this.updates.push({
  54. op: [{ i: "b", p: 0 }],
  55. meta: { ts: this.now - ((9 - i) * this.hours), user_id: this.user_id },
  56. v: (2 * i) + 2
  57. });
  58. }
  59. this.updates[0].meta.user_id = this.deleted_user_id;
  60. TrackChangesApp.ensureRunning(() => {
  61. return TrackChangesClient.pushRawUpdates(this.project_id, this.doc_id, this.updates, error => {
  62. if (error != null) { throw error; }
  63. return done();
  64. });
  65. });
  66. return null;
  67. });
  68. ({
  69. after() {
  70. MockWebApi.getUserInfo.restore();
  71. return null;
  72. }
  73. });
  74. describe("getting updates up to the limit", function() {
  75. before(function(done) {
  76. TrackChangesClient.getUpdates(this.project_id, { before: this.to + 1, min_count: 3 }, (error, body) => {
  77. if (error != null) { throw error; }
  78. this.updates = body.updates;
  79. return done();
  80. });
  81. return null;
  82. });
  83. it("should fetch the user details from the web api", function() {
  84. return MockWebApi.getUserInfo
  85. .calledWith(this.user_id)
  86. .should.equal(true);
  87. });
  88. return it("should return at least the min_count number of summarized updates", function() {
  89. const docs1 = {};
  90. docs1[this.doc_id] = {toV: 20, fromV: 19};
  91. const docs2 = {};
  92. docs2[this.doc_id] = {toV: 18, fromV: 17};
  93. const docs3 = {};
  94. docs3[this.doc_id] = {toV: 16, fromV: 15};
  95. return expect(this.updates.slice(0,3)).to.deep.equal([{
  96. docs: docs1,
  97. meta: {
  98. start_ts: this.to - (2 * this.minutes),
  99. end_ts: this.to,
  100. users: [this.user]
  101. }
  102. }, {
  103. docs: docs2,
  104. meta: {
  105. start_ts: this.to - (1 * this.hours) - (2 * this.minutes),
  106. end_ts: this.to - (1 * this.hours),
  107. users: [this.user]
  108. }
  109. }, {
  110. docs: docs3,
  111. meta: {
  112. start_ts: this.to - (2 * this.hours) - (2 * this.minutes),
  113. end_ts: this.to - (2 * this.hours),
  114. users: [this.user]
  115. }
  116. }]);
  117. });
  118. });
  119. return describe("getting updates beyond the end of the database", function() {
  120. before(function(done) {
  121. TrackChangesClient.getUpdates(this.project_id, { before: (this.to - (8 * this.hours)) + 1, min_count: 30 }, (error, body) => {
  122. if (error != null) { throw error; }
  123. this.updates = body.updates;
  124. return done();
  125. });
  126. return null;
  127. });
  128. return it("should return as many updates as it can", function() {
  129. const docs1 = {};
  130. docs1[this.doc_id] = {toV: 4, fromV: 3};
  131. const docs2 = {};
  132. docs2[this.doc_id] = {toV: 2, fromV: 1};
  133. return expect(this.updates).to.deep.equal([{
  134. docs: docs1,
  135. meta: {
  136. start_ts: this.to - (8 * this.hours) - (2 * this.minutes),
  137. end_ts: this.to - (8 * this.hours),
  138. users: [this.user]
  139. }
  140. }, {
  141. docs: docs2,
  142. meta: {
  143. start_ts: this.to - (9 * this.hours) - (2 * this.minutes),
  144. end_ts: this.to - (9 * this.hours),
  145. users: [this.user, null]
  146. }
  147. }]);
  148. });
  149. });
  150. });