SizeCheckTests.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. const { expect } = require('chai')
  2. const Settings = require('@overleaf/settings')
  3. const MockWebApi = require('./helpers/MockWebApi')
  4. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  5. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  6. describe('SizeChecks', function () {
  7. before(function (done) {
  8. DocUpdaterApp.ensureRunning(done)
  9. })
  10. beforeEach(function () {
  11. this.version = 0
  12. this.update = {
  13. doc: this.doc_id,
  14. op: [
  15. {
  16. i: 'insert some more lines that will bring it above the limit\n',
  17. p: 42,
  18. },
  19. ],
  20. v: this.version,
  21. }
  22. this.project_id = DocUpdaterClient.randomId()
  23. this.doc_id = DocUpdaterClient.randomId()
  24. })
  25. describe('when a doc is above the doc size limit already', function () {
  26. beforeEach(function () {
  27. this.lines = ['x'.repeat(Settings.max_doc_length)] // including the extra newline, this will be over the limit
  28. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  29. lines: this.lines,
  30. v: this.version,
  31. })
  32. })
  33. it('should error when fetching the doc', function (done) {
  34. DocUpdaterClient.getDoc(this.project_id, this.doc_id, (error, res) => {
  35. if (error) return done(error)
  36. expect(res.statusCode).to.equal(500)
  37. done()
  38. })
  39. })
  40. describe('when trying to update', function () {
  41. beforeEach(function (done) {
  42. const update = {
  43. doc: this.doc_id,
  44. op: this.update.op,
  45. v: this.version,
  46. }
  47. DocUpdaterClient.sendUpdate(
  48. this.project_id,
  49. this.doc_id,
  50. update,
  51. error => {
  52. if (error != null) {
  53. throw error
  54. }
  55. setTimeout(done, 200)
  56. }
  57. )
  58. })
  59. it('should still error when fetching the doc', function (done) {
  60. DocUpdaterClient.getDoc(this.project_id, this.doc_id, (error, res) => {
  61. if (error) return done(error)
  62. expect(res.statusCode).to.equal(500)
  63. done()
  64. })
  65. })
  66. })
  67. })
  68. describe('when the stringified JSON is above the doc size limit but the doc character count is not', function () {
  69. beforeEach(function () {
  70. let charsRemaining = Settings.max_doc_length
  71. this.lines = []
  72. // Take the maximum allowed doc length and split it into N lines of 63 characters + a newline.
  73. // The character count will be exactly max_doc_length
  74. // The JSON stringified size will exceed max_doc_length, due to the JSON formatting of the array.
  75. // This document should be allowed, because we use the character count as the limit, not the JSON size.
  76. while (charsRemaining > 0) {
  77. const charstoAdd = Math.min(charsRemaining - 1, 63) // allow for additional newline
  78. this.lines.push('x'.repeat(charstoAdd))
  79. charsRemaining -= charstoAdd + 1
  80. }
  81. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  82. lines: this.lines,
  83. v: this.version,
  84. })
  85. })
  86. it('should be able to fetch the doc', function (done) {
  87. DocUpdaterClient.getDoc(
  88. this.project_id,
  89. this.doc_id,
  90. (error, res, doc) => {
  91. if (error) return done(error)
  92. expect(doc.lines).to.deep.equal(this.lines)
  93. done()
  94. }
  95. )
  96. })
  97. describe('when trying to update', function () {
  98. beforeEach(function (done) {
  99. const update = {
  100. doc: this.doc_id,
  101. op: this.update.op,
  102. v: this.version,
  103. }
  104. DocUpdaterClient.sendUpdate(
  105. this.project_id,
  106. this.doc_id,
  107. update,
  108. error => {
  109. if (error != null) {
  110. throw error
  111. }
  112. setTimeout(done, 200)
  113. }
  114. )
  115. })
  116. it('should not update the doc', function (done) {
  117. DocUpdaterClient.getDoc(
  118. this.project_id,
  119. this.doc_id,
  120. (error, res, doc) => {
  121. if (error) return done(error)
  122. expect(doc.lines).to.deep.equal(this.lines)
  123. done()
  124. }
  125. )
  126. })
  127. })
  128. })
  129. describe('when a doc is just below the doc size limit', function () {
  130. beforeEach(function () {
  131. this.lines = ['x'.repeat(Settings.max_doc_length - 1)] // character count is exactly max_doc_length after including the newline
  132. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  133. lines: this.lines,
  134. v: this.version,
  135. })
  136. })
  137. it('should be able to fetch the doc', function (done) {
  138. DocUpdaterClient.getDoc(
  139. this.project_id,
  140. this.doc_id,
  141. (error, res, doc) => {
  142. if (error) return done(error)
  143. expect(doc.lines).to.deep.equal(this.lines)
  144. done()
  145. }
  146. )
  147. })
  148. describe('when trying to update', function () {
  149. beforeEach(function (done) {
  150. const update = {
  151. doc: this.doc_id,
  152. op: this.update.op,
  153. v: this.version,
  154. }
  155. DocUpdaterClient.sendUpdate(
  156. this.project_id,
  157. this.doc_id,
  158. update,
  159. error => {
  160. if (error != null) {
  161. throw error
  162. }
  163. setTimeout(done, 200)
  164. }
  165. )
  166. })
  167. it('should not update the doc', function (done) {
  168. DocUpdaterClient.getDoc(
  169. this.project_id,
  170. this.doc_id,
  171. (error, res, doc) => {
  172. if (error) return done(error)
  173. expect(doc.lines).to.deep.equal(this.lines)
  174. done()
  175. }
  176. )
  177. })
  178. })
  179. })
  180. })