GettingADiffTests.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require("sinon");
  13. const chai = require("chai");
  14. chai.should();
  15. const { expect } = chai;
  16. const mongojs = require("../../../app/js/mongojs");
  17. const { db } = mongojs;
  18. const { ObjectId } = mongojs;
  19. const Settings = require("settings-sharelatex");
  20. const TrackChangesApp = require("./helpers/TrackChangesApp");
  21. const TrackChangesClient = require("./helpers/TrackChangesClient");
  22. const MockDocUpdaterApi = require("./helpers/MockDocUpdaterApi");
  23. const MockWebApi = require("./helpers/MockWebApi");
  24. describe("Getting a diff", function() {
  25. beforeEach(function(done) {
  26. sinon.spy(MockDocUpdaterApi, "getDoc");
  27. this.now = Date.now();
  28. this.from = this.now - 100000000;
  29. this.to = this.now;
  30. this.user_id = ObjectId().toString();
  31. this.doc_id = ObjectId().toString();
  32. this.project_id = ObjectId().toString();
  33. MockWebApi.projects[this.project_id] = {features: {versioning: true}};
  34. MockWebApi.users[this.user_id] = (this.user = {
  35. email: "user@sharelatex.com",
  36. first_name: "Leo",
  37. last_name: "Lion",
  38. id: this.user_id
  39. });
  40. sinon.spy(MockWebApi, "getUserInfo");
  41. const twoMinutes = 2 * 60 * 1000;
  42. this.updates = [{
  43. op: [{ i: "one ", p: 0 }],
  44. meta: { ts: this.from - twoMinutes, user_id: this.user_id },
  45. v: 3
  46. }, {
  47. op: [{ i: "two ", p: 4 }],
  48. meta: { ts: this.from + twoMinutes, user_id: this.user_id },
  49. v: (this.fromVersion = 4)
  50. }, {
  51. op: [{ i: "three ", p: 8 }],
  52. meta: { ts: this.to - twoMinutes, user_id: this.user_id },
  53. v: (this.toVersion = 5)
  54. }, {
  55. op: [{ i: "four", p: 14 }],
  56. meta: { ts: this.to + twoMinutes, user_id: this.user_id },
  57. v: 6
  58. }];
  59. this.lines = ["one two three four"];
  60. this.expected_diff = [
  61. { u: "one " },
  62. { i: "two three ", meta: { start_ts: this.from + twoMinutes, end_ts: this.to - twoMinutes, user: this.user } }
  63. ];
  64. MockDocUpdaterApi.docs[this.doc_id] = {
  65. lines: this.lines,
  66. version: 7
  67. };
  68. TrackChangesApp.ensureRunning(() => {
  69. return TrackChangesClient.pushRawUpdates(this.project_id, this.doc_id, this.updates, error => {
  70. if (error != null) { throw error; }
  71. return TrackChangesClient.getDiff(this.project_id, this.doc_id, this.fromVersion, this.toVersion, (error, diff) => {
  72. if (error != null) { throw error; }
  73. this.diff = diff.diff;
  74. return done();
  75. });
  76. });
  77. });
  78. return null;
  79. });
  80. afterEach(function() {
  81. MockDocUpdaterApi.getDoc.restore();
  82. MockWebApi.getUserInfo.restore();
  83. return null;
  84. });
  85. it("should return the diff", function() {
  86. return expect(this.diff).to.deep.equal(this.expected_diff);
  87. });
  88. return it("should get the doc from the doc updater", function() {
  89. MockDocUpdaterApi.getDoc
  90. .calledWith(this.project_id, this.doc_id)
  91. .should.equal(true);
  92. return null;
  93. });
  94. });