GettingADocumentTests.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. const Settings = require('@overleaf/settings')
  16. const docUpdaterRedis = require('@overleaf/redis-wrapper').createClient(
  17. Settings.redis.documentupdater
  18. )
  19. const Keys = Settings.redis.documentupdater.key_schema
  20. describe('Getting a document', function () {
  21. before(function (done) {
  22. this.lines = ['one', 'two', 'three']
  23. this.version = 42
  24. return DocUpdaterApp.ensureRunning(done)
  25. })
  26. describe('when the document is not loaded', function () {
  27. before(function (done) {
  28. ;[this.project_id, this.doc_id] = Array.from([
  29. DocUpdaterClient.randomId(),
  30. DocUpdaterClient.randomId(),
  31. ])
  32. sinon.spy(MockWebApi, 'getDocument')
  33. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  34. lines: this.lines,
  35. version: this.version,
  36. })
  37. return DocUpdaterClient.getDoc(
  38. this.project_id,
  39. this.doc_id,
  40. (error, res, returnedDoc) => {
  41. if (error) return done(error)
  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. if (error) return done(error)
  85. this.returnedDoc = returnedDoc
  86. return done()
  87. }
  88. )
  89. }
  90. )
  91. })
  92. after(function () {
  93. return MockWebApi.getDocument.restore()
  94. })
  95. it('should not load the document from the web API', function () {
  96. return MockWebApi.getDocument.called.should.equal(false)
  97. })
  98. return it('should return the document lines', function () {
  99. return this.returnedDoc.lines.should.deep.equal(this.lines)
  100. })
  101. })
  102. describe('when the document is migrated (history-ot)', function () {
  103. before(function (done) {
  104. ;[this.project_id, this.doc_id] = Array.from([
  105. DocUpdaterClient.randomId(),
  106. DocUpdaterClient.randomId(),
  107. ])
  108. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  109. lines: this.lines,
  110. version: this.version,
  111. })
  112. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  113. if (error != null) {
  114. throw error
  115. }
  116. sinon.spy(MockWebApi, 'getDocument')
  117. docUpdaterRedis.set(
  118. Keys.docLines({ doc_id: this.doc_id }),
  119. JSON.stringify({ content: this.lines.join('\n') }),
  120. err => {
  121. if (err) return done(err)
  122. DocUpdaterClient.getDoc(
  123. this.project_id,
  124. this.doc_id,
  125. (error, res, body) => {
  126. if (error) return done(error)
  127. this.res = res
  128. this.body = body
  129. done()
  130. }
  131. )
  132. }
  133. )
  134. })
  135. })
  136. after(function () {
  137. MockWebApi.getDocument.restore()
  138. })
  139. it('should not load the document from the web API', function () {
  140. MockWebApi.getDocument.called.should.equal(false)
  141. })
  142. it('should return an error', function () {
  143. expect(this.res.statusCode).to.equal(422)
  144. expect(this.body).to.equal(
  145. 'refusing to process doc that was migrated to history-ot'
  146. )
  147. })
  148. })
  149. describe('when the request asks for some recent ops', function () {
  150. before(function (done) {
  151. ;[this.project_id, this.doc_id] = Array.from([
  152. DocUpdaterClient.randomId(),
  153. DocUpdaterClient.randomId(),
  154. ])
  155. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  156. lines: (this.lines = ['one', 'two', 'three']),
  157. })
  158. this.updates = __range__(0, 199, true).map(v => ({
  159. doc_id: this.doc_id,
  160. op: [{ i: v.toString(), p: 0 }],
  161. v,
  162. }))
  163. return DocUpdaterClient.sendUpdates(
  164. this.project_id,
  165. this.doc_id,
  166. this.updates,
  167. error => {
  168. if (error != null) {
  169. throw error
  170. }
  171. sinon.spy(MockWebApi, 'getDocument')
  172. return done()
  173. }
  174. )
  175. })
  176. after(function () {
  177. return MockWebApi.getDocument.restore()
  178. })
  179. describe('when the ops are loaded', function () {
  180. before(function (done) {
  181. return DocUpdaterClient.getDocAndRecentOps(
  182. this.project_id,
  183. this.doc_id,
  184. 190,
  185. (error, res, returnedDoc) => {
  186. if (error) return done(error)
  187. this.returnedDoc = returnedDoc
  188. return done()
  189. }
  190. )
  191. })
  192. return it('should return the recent ops', function () {
  193. this.returnedDoc.ops.length.should.equal(10)
  194. return Array.from(this.updates.slice(190, -1)).map((update, i) =>
  195. this.returnedDoc.ops[i].op.should.deep.equal(update.op)
  196. )
  197. })
  198. })
  199. return describe('when the ops are not all loaded', function () {
  200. before(function (done) {
  201. // We only track 100 ops
  202. return DocUpdaterClient.getDocAndRecentOps(
  203. this.project_id,
  204. this.doc_id,
  205. 10,
  206. (error, res, returnedDoc) => {
  207. if (error) return done(error)
  208. this.res = res
  209. this.returnedDoc = returnedDoc
  210. return done()
  211. }
  212. )
  213. })
  214. return it('should return UnprocessableEntity', function () {
  215. return this.res.statusCode.should.equal(422)
  216. })
  217. })
  218. })
  219. describe('when the document does not exist', function () {
  220. before(function (done) {
  221. ;[this.project_id, this.doc_id] = Array.from([
  222. DocUpdaterClient.randomId(),
  223. DocUpdaterClient.randomId(),
  224. ])
  225. return DocUpdaterClient.getDoc(
  226. this.project_id,
  227. this.doc_id,
  228. (error, res, doc) => {
  229. if (error) return done(error)
  230. this.statusCode = res.statusCode
  231. return done()
  232. }
  233. )
  234. })
  235. return it('should return 404', function () {
  236. return this.statusCode.should.equal(404)
  237. })
  238. })
  239. describe('when the web api returns an error', function () {
  240. before(function (done) {
  241. ;[this.project_id, this.doc_id] = Array.from([
  242. DocUpdaterClient.randomId(),
  243. DocUpdaterClient.randomId(),
  244. ])
  245. sinon
  246. .stub(MockWebApi, 'getDocument')
  247. .callsFake((projectId, docId, callback) => {
  248. if (callback == null) {
  249. callback = function () {}
  250. }
  251. return callback(new Error('oops'))
  252. })
  253. return DocUpdaterClient.getDoc(
  254. this.project_id,
  255. this.doc_id,
  256. (error, res, doc) => {
  257. if (error) return done(error)
  258. this.statusCode = res.statusCode
  259. return done()
  260. }
  261. )
  262. })
  263. after(function () {
  264. return MockWebApi.getDocument.restore()
  265. })
  266. return it('should return 500', function () {
  267. return this.statusCode.should.equal(500)
  268. })
  269. })
  270. return describe('when the web api http request takes a long time', function () {
  271. before(function (done) {
  272. this.timeout = 10000
  273. ;[this.project_id, this.doc_id] = Array.from([
  274. DocUpdaterClient.randomId(),
  275. DocUpdaterClient.randomId(),
  276. ])
  277. sinon
  278. .stub(MockWebApi, 'getDocument')
  279. .callsFake((projectId, docId, callback) => {
  280. if (callback == null) {
  281. callback = function () {}
  282. }
  283. return setTimeout(callback, 30000)
  284. })
  285. return done()
  286. })
  287. after(function () {
  288. return MockWebApi.getDocument.restore()
  289. })
  290. return it('should return quickly(ish)', function (done) {
  291. const start = Date.now()
  292. return DocUpdaterClient.getDoc(
  293. this.project_id,
  294. this.doc_id,
  295. (error, res, doc) => {
  296. if (error) return done(error)
  297. res.statusCode.should.equal(500)
  298. const delta = Date.now() - start
  299. expect(delta).to.be.below(20000)
  300. return done()
  301. }
  302. )
  303. })
  304. })
  305. })
  306. function __range__(left, right, inclusive) {
  307. const range = []
  308. const ascending = left < right
  309. const end = !inclusive ? right : ascending ? right + 1 : right - 1
  310. for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
  311. range.push(i)
  312. }
  313. return range
  314. }