LimitsTests.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const { expect } = require('chai')
  2. const modulePath = '../../../../app/js/Limits.js'
  3. const SandboxedModule = require('sandboxed-module')
  4. describe('Limits', function () {
  5. beforeEach(function () {
  6. return (this.Limits = SandboxedModule.require(modulePath))
  7. })
  8. describe('getTotalSizeOfLines', function () {
  9. it('should compute the character count for a document with multiple lines', function () {
  10. const count = this.Limits.getTotalSizeOfLines(['123', '4567'])
  11. expect(count).to.equal(9)
  12. })
  13. it('should compute the character count for a document with a single line', function () {
  14. const count = this.Limits.getTotalSizeOfLines(['123'])
  15. expect(count).to.equal(4)
  16. })
  17. it('should compute the character count for an empty document', function () {
  18. const count = this.Limits.getTotalSizeOfLines([])
  19. expect(count).to.equal(0)
  20. })
  21. })
  22. describe('docIsTooLarge', function () {
  23. describe('when the estimated size is below the limit', function () {
  24. it('should return false when the estimated size is below the limit', function () {
  25. const result = this.Limits.docIsTooLarge(128, ['hello', 'world'], 1024)
  26. expect(result).to.be.false
  27. })
  28. })
  29. describe('when the estimated size is at the limit', function () {
  30. it('should return false when the estimated size is at the limit', function () {
  31. const result = this.Limits.docIsTooLarge(1024, ['hello', 'world'], 1024)
  32. expect(result).to.be.false
  33. })
  34. })
  35. describe('when the estimated size is above the limit', function () {
  36. it('should return false when the actual character count is below the limit', function () {
  37. const result = this.Limits.docIsTooLarge(2048, ['hello', 'world'], 1024)
  38. expect(result).to.be.false
  39. })
  40. it('should return false when the actual character count is at the limit', function () {
  41. const result = this.Limits.docIsTooLarge(2048, ['x'.repeat(1023)], 1024)
  42. expect(result).to.be.false
  43. })
  44. it('should return true when the actual character count is above the limit by 1', function () {
  45. const count = this.Limits.docIsTooLarge(2048, ['x'.repeat(1024)], 1024)
  46. expect(count).to.be.true
  47. })
  48. it('should return true when the actual character count is above the limit', function () {
  49. const count = this.Limits.docIsTooLarge(2048, ['x'.repeat(2000)], 1024)
  50. expect(count).to.be.true
  51. })
  52. })
  53. describe('when the document has many lines', function () {
  54. it('should return false when the actual character count is below the limit ', function () {
  55. const count = this.Limits.docIsTooLarge(
  56. 2048,
  57. '1234567890'.repeat(100).split('0'),
  58. 1024
  59. )
  60. expect(count).to.be.false
  61. })
  62. it('should return true when the actual character count is above the limit', function () {
  63. const count = this.Limits.docIsTooLarge(
  64. 2048,
  65. '1234567890'.repeat(2000).split('0'),
  66. 1024
  67. )
  68. expect(count).to.be.true
  69. })
  70. })
  71. })
  72. describe('stringFileDataContentIsTooLarge', function () {
  73. it('should handle small docs', function () {
  74. expect(
  75. this.Limits.stringFileDataContentIsTooLarge({ content: '' }, 123)
  76. ).to.equal(false)
  77. })
  78. it('should handle docs at the limit', function () {
  79. expect(
  80. this.Limits.stringFileDataContentIsTooLarge(
  81. { content: 'x'.repeat(123) },
  82. 123
  83. )
  84. ).to.equal(false)
  85. })
  86. it('should handle docs above the limit', function () {
  87. expect(
  88. this.Limits.stringFileDataContentIsTooLarge(
  89. { content: 'x'.repeat(123 + 1) },
  90. 123
  91. )
  92. ).to.equal(true)
  93. })
  94. it('should handle docs above the limit and below with tracked-deletes removed', function () {
  95. expect(
  96. this.Limits.stringFileDataContentIsTooLarge(
  97. {
  98. content: 'x'.repeat(123 + 1),
  99. trackedChanges: [
  100. {
  101. range: { pos: 1, length: 1 },
  102. tracking: {
  103. type: 'delete',
  104. ts: '2025-06-16T14:31:44.910Z',
  105. userId: 'user-id',
  106. },
  107. },
  108. ],
  109. },
  110. 123
  111. )
  112. ).to.equal(false)
  113. })
  114. it('should handle docs above the limit and above with tracked-deletes removed', function () {
  115. expect(
  116. this.Limits.stringFileDataContentIsTooLarge(
  117. {
  118. content: 'x'.repeat(123 + 2),
  119. trackedChanges: [
  120. {
  121. range: { pos: 1, length: 1 },
  122. tracking: {
  123. type: 'delete',
  124. ts: '2025-06-16T14:31:44.910Z',
  125. userId: 'user-id',
  126. },
  127. },
  128. ],
  129. },
  130. 123
  131. )
  132. ).to.equal(true)
  133. })
  134. it('should handle docs above the limit and with tracked-inserts', function () {
  135. expect(
  136. this.Limits.stringFileDataContentIsTooLarge(
  137. {
  138. content: 'x'.repeat(123 + 1),
  139. trackedChanges: [
  140. {
  141. range: { pos: 1, length: 1 },
  142. tracking: {
  143. type: 'insert',
  144. ts: '2025-06-16T14:31:44.910Z',
  145. userId: 'user-id',
  146. },
  147. },
  148. ],
  149. },
  150. 123
  151. )
  152. ).to.equal(true)
  153. })
  154. })
  155. })