FlushingDocsTests.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const sinon = require("sinon");
  17. const chai = require("chai");
  18. chai.should();
  19. const {
  20. expect
  21. } = chai;
  22. const async = require("async");
  23. const MockWebApi = require("./helpers/MockWebApi");
  24. const DocUpdaterClient = require("./helpers/DocUpdaterClient");
  25. const DocUpdaterApp = require("./helpers/DocUpdaterApp");
  26. describe("Flushing a doc to Mongo", function() {
  27. before(function(done) {
  28. this.lines = ["one", "two", "three"];
  29. this.version = 42;
  30. this.update = {
  31. doc: this.doc_id,
  32. meta: { user_id: 'last-author-fake-id' },
  33. op: [{
  34. i: "one and a half\n",
  35. p: 4
  36. }],
  37. v: this.version
  38. };
  39. this.result = ["one", "one and a half", "two", "three"];
  40. return DocUpdaterApp.ensureRunning(done);
  41. });
  42. describe("when the updated doc exists in the doc updater", function() {
  43. before(function(done) {
  44. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  45. sinon.spy(MockWebApi, "setDocument");
  46. MockWebApi.insertDoc(this.project_id, this.doc_id, {lines: this.lines, version: this.version});
  47. return DocUpdaterClient.sendUpdates(this.project_id, this.doc_id, [this.update], error => {
  48. if (error != null) { throw error; }
  49. return setTimeout(() => {
  50. return DocUpdaterClient.flushDoc(this.project_id, this.doc_id, done);
  51. }
  52. , 200);
  53. });
  54. });
  55. after(function() { return MockWebApi.setDocument.restore(); });
  56. it("should flush the updated doc lines and version to the web api", function() {
  57. return MockWebApi.setDocument
  58. .calledWith(this.project_id, this.doc_id, this.result, this.version + 1)
  59. .should.equal(true);
  60. });
  61. return it("should flush the last update author and time to the web api", function() {
  62. const lastUpdatedAt = MockWebApi.setDocument.lastCall.args[5];
  63. parseInt(lastUpdatedAt).should.be.closeTo((new Date()).getTime(), 30000);
  64. const lastUpdatedBy = MockWebApi.setDocument.lastCall.args[6];
  65. return lastUpdatedBy.should.equal('last-author-fake-id');
  66. });
  67. });
  68. describe("when the doc does not exist in the doc updater", function() {
  69. before(function(done) {
  70. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  71. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  72. lines: this.lines
  73. });
  74. sinon.spy(MockWebApi, "setDocument");
  75. return DocUpdaterClient.flushDoc(this.project_id, this.doc_id, done);
  76. });
  77. after(function() { return MockWebApi.setDocument.restore(); });
  78. return it("should not flush the doc to the web api", function() { return MockWebApi.setDocument.called.should.equal(false); });
  79. });
  80. return describe("when the web api http request takes a long time on first request", function() {
  81. before(function(done) {
  82. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  83. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  84. lines: this.lines,
  85. version: this.version
  86. });
  87. let t = 30000;
  88. sinon.stub(MockWebApi, "setDocument", (project_id, doc_id, lines, version, ranges, lastUpdatedAt, lastUpdatedBy, callback) => {
  89. if (callback == null) { callback = function(error) {}; }
  90. setTimeout(callback, t);
  91. return t = 0;
  92. });
  93. return DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, done);
  94. });
  95. after(function() { return MockWebApi.setDocument.restore(); });
  96. return it("should still work", function(done) {
  97. const start = Date.now();
  98. return DocUpdaterClient.flushDoc(this.project_id, this.doc_id, (error, res, doc) => {
  99. res.statusCode.should.equal(204);
  100. const delta = Date.now() - start;
  101. expect(delta).to.be.below(20000);
  102. return done();
  103. });
  104. });
  105. });
  106. });