overleaf/services/spelling/test/unit/js/ASpellTests.js

149 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-07-03 08:41:01 -04:00
/* eslint-disable
handle-callback-err,
no-undef
*/
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const chai = require('chai')
const should = chai.should()
const SandboxedModule = require('sandboxed-module')
const { assert } = require('chai')
2020-08-10 12:23:15 -04:00
describe('ASpell', function () {
beforeEach(function () {
2019-07-03 08:41:01 -04:00
return (this.ASpell = SandboxedModule.require('../../../app/js/ASpell', {
requires: {
'logger-sharelatex': {
log() {},
info() {},
err() {}
},
'metrics-sharelatex': {
gauge() {},
inc() {}
}
}
}))
})
2020-08-10 12:23:15 -04:00
afterEach(function () {
clearInterval(this.ASpell.cacheDump)
})
2019-07-03 08:41:01 -04:00
2020-08-10 12:23:15 -04:00
describe('a correctly spelled word', function () {
beforeEach(function (done) {
2019-07-03 08:41:01 -04:00
return this.ASpell.checkWords('en', ['word'], (error, result) => {
this.result = result
return done()
})
})
2020-08-10 12:23:15 -04:00
return it('should not correct the word', function () {
2019-07-03 08:41:01 -04:00
return this.result.length.should.equal(0)
})
})
2020-08-10 12:23:15 -04:00
describe('a misspelled word', function () {
beforeEach(function (done) {
2019-07-03 08:41:01 -04:00
return this.ASpell.checkWords('en', ['bussines'], (error, result) => {
this.result = result
return done()
})
})
2020-08-10 12:23:15 -04:00
return it('should correct the word', function () {
2019-07-03 08:41:01 -04:00
this.result.length.should.equal(1)
return this.result[0].suggestions.indexOf('business').should.not.equal(-1)
})
})
2020-08-10 12:23:15 -04:00
describe('multiple words', function () {
beforeEach(function (done) {
2019-07-03 08:41:01 -04:00
return this.ASpell.checkWords(
'en',
['bussines', 'word', 'neccesary'],
(error, result) => {
this.result = result
return done()
}
)
})
2020-08-10 12:23:15 -04:00
return it('should correct the incorrect words', function () {
2019-07-03 08:41:01 -04:00
this.result[0].index.should.equal(0)
this.result[0].suggestions.indexOf('business').should.not.equal(-1)
this.result[1].index.should.equal(2)
return this.result[1].suggestions
.indexOf('necessary')
.should.not.equal(-1)
})
})
2020-08-10 12:23:15 -04:00
describe('without a valid language', function () {
beforeEach(function (done) {
2019-07-03 08:41:01 -04:00
return this.ASpell.checkWords('notALang', ['banana'], (error, result) => {
this.error = error
this.result = result
return done()
})
})
2020-08-10 12:23:15 -04:00
return it('should return an error', function () {
2019-07-03 08:41:01 -04:00
return should.exist(this.error)
})
})
2020-08-10 12:23:15 -04:00
describe('when there are no suggestions', function () {
beforeEach(function (done) {
2019-07-03 08:41:01 -04:00
return this.ASpell.checkWords(
'en',
['asdkfjalkdjfadhfkajsdhfashdfjhadflkjadhflajsd'],
(error, result) => {
this.error = error
this.result = result
return done()
}
)
})
2020-08-10 12:23:15 -04:00
return it('should return a blank array', function () {
2019-07-03 08:41:01 -04:00
this.result.length.should.equal(1)
return assert.deepEqual(this.result[0].suggestions, [])
})
})
2020-08-10 12:23:15 -04:00
return describe('when the request times out', function () {
beforeEach(function (done) {
const words = __range__(0, 1000, true).map((i) => 'abcdefg')
2019-07-03 08:41:01 -04:00
this.ASpell.ASPELL_TIMEOUT = 1
this.start = Date.now()
return this.ASpell.checkWords('en', words, (error, result) => {
this.result = result
return done()
})
})
// Note that this test fails on OS X, due to differing pipe behaviour
// on killing the child process. It can be tested successfully on Travis
// or the CI server.
2020-08-10 12:23:15 -04:00
return it('should return in reasonable time', function () {
2019-07-03 08:41:01 -04:00
const delta = Date.now() - this.start
return delta.should.be.below(this.ASpell.ASPELL_TIMEOUT + 1000)
})
})
})
function __range__(left, right, inclusive) {
2020-06-03 04:52:36 -04:00
const range = []
const ascending = left < right
const end = !inclusive ? right : ascending ? right + 1 : right - 1
2019-07-03 08:41:01 -04:00
for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
range.push(i)
}
return range
}