LimitsTests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. })