GettingADocumentTests.js 7.9 KB

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