GettingADocumentTests.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS207: Consider shorter variations of null checks
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  9. */
  10. const sinon = require('sinon')
  11. const { expect } = require('chai')
  12. const MockWebApi = require('./helpers/MockWebApi')
  13. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  14. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  15. describe('Getting a document', function () {
  16. before(function (done) {
  17. this.lines = ['one', 'two', 'three']
  18. this.version = 42
  19. return DocUpdaterApp.ensureRunning(done)
  20. })
  21. describe('when the document is not loaded', function () {
  22. before(function (done) {
  23. ;[this.project_id, this.doc_id] = Array.from([
  24. DocUpdaterClient.randomId(),
  25. DocUpdaterClient.randomId(),
  26. ])
  27. sinon.spy(MockWebApi, 'getDocument')
  28. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  29. lines: this.lines,
  30. version: this.version,
  31. })
  32. return DocUpdaterClient.getDoc(
  33. this.project_id,
  34. this.doc_id,
  35. (error, res, returnedDoc) => {
  36. if (error) return done(error)
  37. this.returnedDoc = returnedDoc
  38. return done()
  39. }
  40. )
  41. })
  42. after(function () {
  43. return MockWebApi.getDocument.restore()
  44. })
  45. it('should load the document from the web API', function () {
  46. return MockWebApi.getDocument
  47. .calledWith(this.project_id, this.doc_id)
  48. .should.equal(true)
  49. })
  50. it('should return the document lines', function () {
  51. return this.returnedDoc.lines.should.deep.equal(this.lines)
  52. })
  53. return it('should return the document at its current version', function () {
  54. return this.returnedDoc.version.should.equal(this.version)
  55. })
  56. })
  57. describe('when the document is already loaded', function () {
  58. before(function (done) {
  59. ;[this.project_id, this.doc_id] = Array.from([
  60. DocUpdaterClient.randomId(),
  61. DocUpdaterClient.randomId(),
  62. ])
  63. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  64. lines: this.lines,
  65. version: this.version,
  66. })
  67. return DocUpdaterClient.preloadDoc(
  68. this.project_id,
  69. this.doc_id,
  70. error => {
  71. if (error != null) {
  72. throw error
  73. }
  74. sinon.spy(MockWebApi, 'getDocument')
  75. return DocUpdaterClient.getDoc(
  76. this.project_id,
  77. this.doc_id,
  78. (error, res, returnedDoc) => {
  79. if (error) return done(error)
  80. this.returnedDoc = returnedDoc
  81. return done()
  82. }
  83. )
  84. }
  85. )
  86. })
  87. after(function () {
  88. return MockWebApi.getDocument.restore()
  89. })
  90. it('should not load the document from the web API', function () {
  91. return MockWebApi.getDocument.called.should.equal(false)
  92. })
  93. return it('should return the document lines', function () {
  94. return this.returnedDoc.lines.should.deep.equal(this.lines)
  95. })
  96. })
  97. describe('when the request asks for some recent ops', function () {
  98. before(function (done) {
  99. ;[this.project_id, this.doc_id] = Array.from([
  100. DocUpdaterClient.randomId(),
  101. DocUpdaterClient.randomId(),
  102. ])
  103. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  104. lines: (this.lines = ['one', 'two', 'three']),
  105. })
  106. this.updates = __range__(0, 199, true).map(v => ({
  107. doc_id: this.doc_id,
  108. op: [{ i: v.toString(), p: 0 }],
  109. v,
  110. }))
  111. return DocUpdaterClient.sendUpdates(
  112. this.project_id,
  113. this.doc_id,
  114. this.updates,
  115. error => {
  116. if (error != null) {
  117. throw error
  118. }
  119. sinon.spy(MockWebApi, 'getDocument')
  120. return done()
  121. }
  122. )
  123. })
  124. after(function () {
  125. return MockWebApi.getDocument.restore()
  126. })
  127. describe('when the ops are loaded', function () {
  128. before(function (done) {
  129. return DocUpdaterClient.getDocAndRecentOps(
  130. this.project_id,
  131. this.doc_id,
  132. 190,
  133. (error, res, returnedDoc) => {
  134. if (error) return done(error)
  135. this.returnedDoc = returnedDoc
  136. return done()
  137. }
  138. )
  139. })
  140. return it('should return the recent ops', function () {
  141. this.returnedDoc.ops.length.should.equal(10)
  142. return Array.from(this.updates.slice(190, -1)).map((update, i) =>
  143. this.returnedDoc.ops[i].op.should.deep.equal(update.op)
  144. )
  145. })
  146. })
  147. return describe('when the ops are not all loaded', function () {
  148. before(function (done) {
  149. // We only track 100 ops
  150. return DocUpdaterClient.getDocAndRecentOps(
  151. this.project_id,
  152. this.doc_id,
  153. 10,
  154. (error, res, returnedDoc) => {
  155. if (error) return done(error)
  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. if (error) return done(error)
  178. this.statusCode = res.statusCode
  179. return done()
  180. }
  181. )
  182. })
  183. return it('should return 404', function () {
  184. return this.statusCode.should.equal(404)
  185. })
  186. })
  187. describe('when the web api returns an error', function () {
  188. before(function (done) {
  189. ;[this.project_id, this.doc_id] = Array.from([
  190. DocUpdaterClient.randomId(),
  191. DocUpdaterClient.randomId(),
  192. ])
  193. sinon
  194. .stub(MockWebApi, 'getDocument')
  195. .callsFake((projectId, docId, callback) => {
  196. if (callback == null) {
  197. callback = function () {}
  198. }
  199. return callback(new Error('oops'))
  200. })
  201. return DocUpdaterClient.getDoc(
  202. this.project_id,
  203. this.doc_id,
  204. (error, res, doc) => {
  205. if (error) return done(error)
  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((projectId, docId, callback) => {
  228. if (callback == null) {
  229. callback = function () {}
  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. if (error) return done(error)
  245. res.statusCode.should.equal(500)
  246. const delta = Date.now() - start
  247. expect(delta).to.be.below(20000)
  248. return done()
  249. }
  250. )
  251. })
  252. })
  253. })
  254. function __range__(left, right, inclusive) {
  255. const range = []
  256. const ascending = left < right
  257. const end = !inclusive ? right : ascending ? right + 1 : right - 1
  258. for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
  259. range.push(i)
  260. }
  261. return range
  262. }