ASpellTests.js 4.0 KB

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