GettingADocumentTests.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 { expect } = chai
  18. const MockWebApi = require('./helpers/MockWebApi')
  19. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  20. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  21. describe('Getting a document', function () {
  22. before(function (done) {
  23. this.lines = ['one', 'two', 'three']
  24. this.version = 42
  25. return DocUpdaterApp.ensureRunning(done)
  26. })
  27. describe('when the document is not loaded', function () {
  28. before(function (done) {
  29. ;[this.project_id, this.doc_id] = Array.from([
  30. DocUpdaterClient.randomId(),
  31. DocUpdaterClient.randomId()
  32. ])
  33. sinon.spy(MockWebApi, 'getDocument')
  34. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  35. lines: this.lines,
  36. version: this.version
  37. })
  38. return DocUpdaterClient.getDoc(
  39. this.project_id,
  40. this.doc_id,
  41. (error, res, returnedDoc) => {
  42. this.returnedDoc = returnedDoc
  43. return done()
  44. }
  45. )
  46. })
  47. after(function () {
  48. return MockWebApi.getDocument.restore()
  49. })
  50. it('should load the document from the web API', function () {
  51. return MockWebApi.getDocument
  52. .calledWith(this.project_id, this.doc_id)
  53. .should.equal(true)
  54. })
  55. it('should return the document lines', function () {
  56. return this.returnedDoc.lines.should.deep.equal(this.lines)
  57. })
  58. return it('should return the document at its current version', function () {
  59. return this.returnedDoc.version.should.equal(this.version)
  60. })
  61. })
  62. describe('when the document is already loaded', function () {
  63. before(function (done) {
  64. ;[this.project_id, this.doc_id] = Array.from([
  65. DocUpdaterClient.randomId(),
  66. DocUpdaterClient.randomId()
  67. ])
  68. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  69. lines: this.lines,
  70. version: this.version
  71. })
  72. return DocUpdaterClient.preloadDoc(
  73. this.project_id,
  74. this.doc_id,
  75. (error) => {
  76. if (error != null) {
  77. throw error
  78. }
  79. sinon.spy(MockWebApi, 'getDocument')
  80. return DocUpdaterClient.getDoc(
  81. this.project_id,
  82. this.doc_id,
  83. (error, res, returnedDoc) => {
  84. this.returnedDoc = returnedDoc
  85. return done()
  86. }
  87. )
  88. }
  89. )
  90. })
  91. after(function () {
  92. return MockWebApi.getDocument.restore()
  93. })
  94. it('should not load the document from the web API', function () {
  95. return MockWebApi.getDocument.called.should.equal(false)
  96. })
  97. return it('should return the document lines', function () {
  98. return this.returnedDoc.lines.should.deep.equal(this.lines)
  99. })
  100. })
  101. describe('when the request asks for some recent ops', function () {
  102. before(function (done) {
  103. ;[this.project_id, this.doc_id] = Array.from([
  104. DocUpdaterClient.randomId(),
  105. DocUpdaterClient.randomId()
  106. ])
  107. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  108. lines: (this.lines = ['one', 'two', 'three'])
  109. })
  110. this.updates = __range__(0, 199, true).map((v) => ({
  111. doc_id: this.doc_id,
  112. op: [{ i: v.toString(), p: 0 }],
  113. v
  114. }))
  115. return DocUpdaterClient.sendUpdates(
  116. this.project_id,
  117. this.doc_id,
  118. this.updates,
  119. (error) => {
  120. if (error != null) {
  121. throw error
  122. }
  123. sinon.spy(MockWebApi, 'getDocument')
  124. return done()
  125. }
  126. )
  127. })
  128. after(function () {
  129. return MockWebApi.getDocument.restore()
  130. })
  131. describe('when the ops are loaded', function () {
  132. before(function (done) {
  133. return DocUpdaterClient.getDocAndRecentOps(
  134. this.project_id,
  135. this.doc_id,
  136. 190,
  137. (error, res, returnedDoc) => {
  138. this.returnedDoc = returnedDoc
  139. return done()
  140. }
  141. )
  142. })
  143. return it('should return the recent ops', function () {
  144. this.returnedDoc.ops.length.should.equal(10)
  145. return Array.from(this.updates.slice(190, -1)).map((update, i) =>
  146. this.returnedDoc.ops[i].op.should.deep.equal(update.op)
  147. )
  148. })
  149. })
  150. return describe('when the ops are not all loaded', function () {
  151. before(function (done) {
  152. // We only track 100 ops
  153. return DocUpdaterClient.getDocAndRecentOps(
  154. this.project_id,
  155. this.doc_id,
  156. 10,
  157. (error, res, returnedDoc) => {
  158. this.res = res
  159. this.returnedDoc = returnedDoc
  160. return done()
  161. }
  162. )
  163. })
  164. return it('should return UnprocessableEntity', function () {
  165. return this.res.statusCode.should.equal(422)
  166. })
  167. })
  168. })
  169. describe('when the document does not exist', function () {
  170. before(function (done) {
  171. ;[this.project_id, this.doc_id] = Array.from([
  172. DocUpdaterClient.randomId(),
  173. DocUpdaterClient.randomId()
  174. ])
  175. return DocUpdaterClient.getDoc(
  176. this.project_id,
  177. this.doc_id,
  178. (error, res, doc) => {
  179. this.statusCode = res.statusCode
  180. return done()
  181. }
  182. )
  183. })
  184. return it('should return 404', function () {
  185. return this.statusCode.should.equal(404)
  186. })
  187. })
  188. describe('when the web api returns an error', function () {
  189. before(function (done) {
  190. ;[this.project_id, this.doc_id] = Array.from([
  191. DocUpdaterClient.randomId(),
  192. DocUpdaterClient.randomId()
  193. ])
  194. sinon
  195. .stub(MockWebApi, 'getDocument')
  196. .callsFake((project_id, doc_id, callback) => {
  197. if (callback == null) {
  198. callback = function (error, doc) {}
  199. }
  200. return callback(new Error('oops'))
  201. })
  202. return DocUpdaterClient.getDoc(
  203. this.project_id,
  204. this.doc_id,
  205. (error, res, doc) => {
  206. this.statusCode = res.statusCode
  207. return done()
  208. }
  209. )
  210. })
  211. after(function () {
  212. return MockWebApi.getDocument.restore()
  213. })
  214. return it('should return 500', function () {
  215. return this.statusCode.should.equal(500)
  216. })
  217. })
  218. return describe('when the web api http request takes a long time', function () {
  219. before(function (done) {
  220. this.timeout = 10000
  221. ;[this.project_id, this.doc_id] = Array.from([
  222. DocUpdaterClient.randomId(),
  223. DocUpdaterClient.randomId()
  224. ])
  225. sinon
  226. .stub(MockWebApi, 'getDocument')
  227. .callsFake((project_id, doc_id, callback) => {
  228. if (callback == null) {
  229. callback = function (error, doc) {}
  230. }
  231. return setTimeout(callback, 30000)
  232. })
  233. return done()
  234. })
  235. after(function () {
  236. return MockWebApi.getDocument.restore()
  237. })
  238. return it('should return quickly(ish)', function (done) {
  239. const start = Date.now()
  240. return DocUpdaterClient.getDoc(
  241. this.project_id,
  242. this.doc_id,
  243. (error, res, doc) => {
  244. res.statusCode.should.equal(500)
  245. const delta = Date.now() - start
  246. expect(delta).to.be.below(20000)
  247. return done()
  248. }
  249. )
  250. })
  251. })
  252. })
  253. function __range__(left, right, inclusive) {
  254. const range = []
  255. const ascending = left < right
  256. const end = !inclusive ? right : ascending ? right + 1 : right - 1
  257. for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
  258. range.push(i)
  259. }
  260. return range
  261. }