GettingADocumentTests.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  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. chai.should();
  17. const {
  18. expect
  19. } = chai;
  20. const MockWebApi = require("./helpers/MockWebApi");
  21. const DocUpdaterClient = require("./helpers/DocUpdaterClient");
  22. const DocUpdaterApp = require("./helpers/DocUpdaterApp");
  23. describe("Getting a document", function() {
  24. before(function(done) {
  25. this.lines = ["one", "two", "three"];
  26. this.version = 42;
  27. return DocUpdaterApp.ensureRunning(done);
  28. });
  29. describe("when the document is not loaded", function() {
  30. before(function(done) {
  31. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  32. sinon.spy(MockWebApi, "getDocument");
  33. MockWebApi.insertDoc(this.project_id, this.doc_id, {lines: this.lines, version: this.version});
  34. return DocUpdaterClient.getDoc(this.project_id, this.doc_id, (error, res, returnedDoc) => { this.returnedDoc = returnedDoc; return done(); });
  35. });
  36. after(function() { return MockWebApi.getDocument.restore(); });
  37. it("should load the document from the web API", function() {
  38. return MockWebApi.getDocument
  39. .calledWith(this.project_id, this.doc_id)
  40. .should.equal(true);
  41. });
  42. it("should return the document lines", function() {
  43. return this.returnedDoc.lines.should.deep.equal(this.lines);
  44. });
  45. return it("should return the document at its current version", function() {
  46. return this.returnedDoc.version.should.equal(this.version);
  47. });
  48. });
  49. describe("when the document is already loaded", function() {
  50. before(function(done) {
  51. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  52. MockWebApi.insertDoc(this.project_id, this.doc_id, {lines: this.lines, version: this.version});
  53. return DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  54. if (error != null) { throw error; }
  55. sinon.spy(MockWebApi, "getDocument");
  56. return DocUpdaterClient.getDoc(this.project_id, this.doc_id, (error, res, returnedDoc) => { this.returnedDoc = returnedDoc; return done(); });
  57. });
  58. });
  59. after(function() { return MockWebApi.getDocument.restore(); });
  60. it("should not load the document from the web API", function() { return MockWebApi.getDocument.called.should.equal(false); });
  61. return it("should return the document lines", function() {
  62. return this.returnedDoc.lines.should.deep.equal(this.lines);
  63. });
  64. });
  65. describe("when the request asks for some recent ops", function() {
  66. before(function(done) {
  67. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  68. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  69. lines: (this.lines = ["one", "two", "three"])
  70. });
  71. this.updates = __range__(0, 199, true).map((v) => ({
  72. doc_id: this.doc_id,
  73. op: [{i: v.toString(), p: 0}],
  74. v
  75. }));
  76. return DocUpdaterClient.sendUpdates(this.project_id, this.doc_id, this.updates, error => {
  77. if (error != null) { throw error; }
  78. sinon.spy(MockWebApi, "getDocument");
  79. return done();
  80. });
  81. });
  82. after(function() { return MockWebApi.getDocument.restore(); });
  83. describe("when the ops are loaded", function() {
  84. before(function(done) {
  85. return DocUpdaterClient.getDocAndRecentOps(this.project_id, this.doc_id, 190, (error, res, returnedDoc) => { this.returnedDoc = returnedDoc; return done(); });
  86. });
  87. return it("should return the recent ops", function() {
  88. this.returnedDoc.ops.length.should.equal(10);
  89. return Array.from(this.updates.slice(190, -1)).map((update, i) =>
  90. this.returnedDoc.ops[i].op.should.deep.equal(update.op));
  91. });
  92. });
  93. return describe("when the ops are not all loaded", function() {
  94. before(function(done) {
  95. // We only track 100 ops
  96. return DocUpdaterClient.getDocAndRecentOps(this.project_id, this.doc_id, 10, (error, res, returnedDoc) => { this.res = res; this.returnedDoc = returnedDoc; return done(); });
  97. });
  98. return it("should return UnprocessableEntity", function() {
  99. return this.res.statusCode.should.equal(422);
  100. });
  101. });
  102. });
  103. describe("when the document does not exist", function() {
  104. before(function(done) {
  105. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  106. return DocUpdaterClient.getDoc(this.project_id, this.doc_id, (error, res, doc) => {
  107. this.statusCode = res.statusCode;
  108. return done();
  109. });
  110. });
  111. return it("should return 404", function() {
  112. return this.statusCode.should.equal(404);
  113. });
  114. });
  115. describe("when the web api returns an error", function() {
  116. before(function(done) {
  117. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  118. sinon.stub(MockWebApi, "getDocument", (project_id, doc_id, callback) => {
  119. if (callback == null) { callback = function(error, doc) {}; }
  120. return callback(new Error("oops"));
  121. });
  122. return DocUpdaterClient.getDoc(this.project_id, this.doc_id, (error, res, doc) => {
  123. this.statusCode = res.statusCode;
  124. return done();
  125. });
  126. });
  127. after(function() { return MockWebApi.getDocument.restore(); });
  128. return it("should return 500", function() {
  129. return this.statusCode.should.equal(500);
  130. });
  131. });
  132. return describe("when the web api http request takes a long time", function() {
  133. before(function(done) {
  134. this.timeout = 10000;
  135. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  136. sinon.stub(MockWebApi, "getDocument", (project_id, doc_id, callback) => {
  137. if (callback == null) { callback = function(error, doc) {}; }
  138. return setTimeout(callback, 30000);
  139. });
  140. return done();
  141. });
  142. after(function() { return MockWebApi.getDocument.restore(); });
  143. return it("should return quickly(ish)", function(done) {
  144. const start = Date.now();
  145. return DocUpdaterClient.getDoc(this.project_id, this.doc_id, (error, res, doc) => {
  146. res.statusCode.should.equal(500);
  147. const delta = Date.now() - start;
  148. expect(delta).to.be.below(20000);
  149. return done();
  150. });
  151. });
  152. });
  153. });
  154. function __range__(left, right, inclusive) {
  155. const range = [];
  156. const ascending = left < right;
  157. const end = !inclusive ? right : ascending ? right + 1 : right - 1;
  158. for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
  159. range.push(i);
  160. }
  161. return range;
  162. }