ASpellTests.js 4.1 KB

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