ASpellTests.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* eslint-disable
  2. no-undef
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Sanity-check the conversion and remove this comment.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. const { expect, assert } = require('chai')
  12. const SandboxedModule = require('sandboxed-module')
  13. describe('ASpell', function () {
  14. beforeEach(function () {
  15. return (this.ASpell = SandboxedModule.require('../../../app/js/ASpell', {
  16. requires: {
  17. '@overleaf/metrics': {
  18. gauge() {},
  19. inc() {},
  20. },
  21. },
  22. }))
  23. })
  24. afterEach(function () {
  25. clearInterval(this.ASpell.cacheDump)
  26. })
  27. describe('a correctly spelled word', function () {
  28. beforeEach(function (done) {
  29. return this.ASpell.checkWords('en', ['word'], (error, result) => {
  30. if (error) return done(error)
  31. this.result = result
  32. return done()
  33. })
  34. })
  35. return it('should not correct the word', function () {
  36. return this.result.length.should.equal(0)
  37. })
  38. })
  39. describe('a misspelled word', function () {
  40. beforeEach(function (done) {
  41. return this.ASpell.checkWords('en', ['bussines'], (error, result) => {
  42. if (error) return done(error)
  43. this.result = result
  44. return done()
  45. })
  46. })
  47. return it('should correct the word', function () {
  48. this.result.length.should.equal(1)
  49. return this.result[0].suggestions.indexOf('business').should.not.equal(-1)
  50. })
  51. })
  52. describe('multiple words', function () {
  53. beforeEach(function (done) {
  54. return this.ASpell.checkWords(
  55. 'en',
  56. ['bussines', 'word', 'neccesary'],
  57. (error, result) => {
  58. if (error) return done(error)
  59. this.result = result
  60. return done()
  61. }
  62. )
  63. })
  64. return it('should correct the incorrect words', function () {
  65. this.result[0].index.should.equal(0)
  66. this.result[0].suggestions.indexOf('business').should.not.equal(-1)
  67. this.result[1].index.should.equal(2)
  68. return this.result[1].suggestions
  69. .indexOf('necessary')
  70. .should.not.equal(-1)
  71. })
  72. })
  73. describe('without a valid language', function () {
  74. beforeEach(function (done) {
  75. return this.ASpell.checkWords('notALang', ['banana'], (error, result) => {
  76. this.error = error
  77. this.result = result
  78. return done()
  79. })
  80. })
  81. return it('should return an error', function () {
  82. return expect(this.error).to.exist
  83. })
  84. })
  85. describe('when there are no suggestions', function () {
  86. beforeEach(function (done) {
  87. return this.ASpell.checkWords(
  88. 'en',
  89. ['asdkfjalkdjfadhfkajsdhfashdfjhadflkjadhflajsd'],
  90. (error, result) => {
  91. this.error = error
  92. this.result = result
  93. return done()
  94. }
  95. )
  96. })
  97. return it('should return a blank array', function () {
  98. this.result.length.should.equal(1)
  99. return assert.deepEqual(this.result[0].suggestions, [])
  100. })
  101. })
  102. return describe('when the request times out', function () {
  103. beforeEach(function (done) {
  104. const words = __range__(0, 1000, true).map(i => 'abcdefg')
  105. this.ASpell.ASPELL_TIMEOUT = 1
  106. this.start = Date.now()
  107. return this.ASpell.checkWords('en', words, (error, result) => {
  108. expect(error).to.exist
  109. this.result = result
  110. return done()
  111. })
  112. })
  113. // Note that this test fails on OS X, due to differing pipe behaviour
  114. // on killing the child process. It can be tested successfully on Travis
  115. // or the CI server.
  116. return it('should return in reasonable time', function () {
  117. const delta = Date.now() - this.start
  118. return delta.should.be.below(this.ASpell.ASPELL_TIMEOUT + 1000)
  119. })
  120. })
  121. })
  122. function __range__(left, right, inclusive) {
  123. const range = []
  124. const ascending = left < right
  125. const end = !inclusive ? right : ascending ? right + 1 : right - 1
  126. for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
  127. range.push(i)
  128. }
  129. return range
  130. }