2014-08-15 07:13:35 -04:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require 'chai'
|
|
|
|
expect = chai.expect
|
|
|
|
chai.should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
modulePath = require('path').join __dirname, '../../../app/js/SpellingAPIManager'
|
|
|
|
|
|
|
|
describe "SpellingAPIManager", ->
|
|
|
|
beforeEach ->
|
|
|
|
@token = "user-id-123"
|
|
|
|
@ASpell = {}
|
|
|
|
@learnedWords = ["lerned"]
|
|
|
|
@LearnedWordsManager =
|
|
|
|
getLearnedWords: sinon.stub().callsArgWith(1, null, @learnedWords)
|
|
|
|
learnWord: sinon.stub().callsArg(2)
|
|
|
|
|
|
|
|
@SpellingAPIManager = SandboxedModule.require modulePath, requires:
|
|
|
|
"./ASpell" : @ASpell
|
|
|
|
"./LearnedWordsManager" : @LearnedWordsManager
|
|
|
|
|
|
|
|
describe "runRequest", ->
|
|
|
|
beforeEach ->
|
|
|
|
@nonLearnedWords = ["some", "words", "htat", "are", "speled", "rong", "lerned"]
|
|
|
|
@allWords = @nonLearnedWords.concat(@learnedWords)
|
|
|
|
@misspellings = [
|
|
|
|
{ index: 2, suggestions: ["that"] }
|
|
|
|
{ index: 4, suggestions: ["spelled"] }
|
|
|
|
{ index: 5, suggestions: ["wrong", "ring"] }
|
|
|
|
{ index: 6, suggestions: ["learned"] }
|
|
|
|
]
|
|
|
|
@misspellingsWithoutLearnedWords = @misspellings.slice(0,3)
|
|
|
|
|
|
|
|
@ASpell.checkWords = (lang, word, callback) =>
|
|
|
|
callback null, @misspellings
|
|
|
|
sinon.spy @ASpell, "checkWords"
|
|
|
|
|
|
|
|
describe "with sensible JSON", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@SpellingAPIManager.runRequest @token, words: @allWords, (error, @result) => done()
|
|
|
|
|
|
|
|
it "should return the words that are spelled incorrectly and not learned", ->
|
|
|
|
expect(@result.misspellings).to.deep.equal @misspellingsWithoutLearnedWords
|
|
|
|
|
|
|
|
describe "with a missing words array", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@SpellingAPIManager.runRequest @token, {}, (@error, @result) => done()
|
|
|
|
|
|
|
|
it "should return an error", ->
|
2017-06-22 05:09:30 -04:00
|
|
|
expect(@error).to.exist
|
|
|
|
expect(@error).to.be.instanceof Error
|
|
|
|
expect(@error.message).to.equal "malformed JSON"
|
2014-08-15 07:13:35 -04:00
|
|
|
|
|
|
|
describe "with a missing token", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@SpellingAPIManager.runRequest null, words: @allWords, (@error, @result) => done()
|
|
|
|
|
|
|
|
it "should spell check without using any learned words", ->
|
|
|
|
@LearnedWordsManager.getLearnedWords.called.should.equal false
|
|
|
|
|
|
|
|
describe "without a language", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@SpellingAPIManager.runRequest @token, words: @allWords, (error, @result) => done()
|
|
|
|
|
|
|
|
it "should use en as the default", ->
|
|
|
|
@ASpell.checkWords.calledWith("en").should.equal true
|
|
|
|
|
|
|
|
describe "with a language", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@SpellingAPIManager.runRequest @token, {
|
|
|
|
words: @allWords
|
|
|
|
language: @language = "fr"
|
|
|
|
}, (error, @result) => done()
|
|
|
|
|
|
|
|
it "should use the language", ->
|
|
|
|
@ASpell.checkWords.calledWith(@language).should.equal true
|
|
|
|
|
|
|
|
describe "with a very large collection of words", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@manyWords = ("word" for i in [1..100000])
|
|
|
|
@SpellingAPIManager.runRequest @token, words: @manyWords, (error, @result) => done()
|
|
|
|
|
|
|
|
it "should truncate to 10,000 words", ->
|
|
|
|
@ASpell.checkWords.calledWith(sinon.match.any, @manyWords.slice(0, 10000)).should.equal true
|
|
|
|
|
2017-06-22 04:23:47 -04:00
|
|
|
describe 'with words from the whitelist', ->
|
|
|
|
beforeEach (done) ->
|
2017-06-23 05:00:47 -04:00
|
|
|
@whitelistWord = @SpellingAPIManager.whitelist[0]
|
|
|
|
@words = ["One", "Two", @whitelistWord]
|
2017-06-22 04:23:47 -04:00
|
|
|
@SpellingAPIManager.runRequest @token, words: @words, (error, @result) => done()
|
|
|
|
|
|
|
|
it 'should ignore the white-listed word', ->
|
2017-06-23 05:00:47 -04:00
|
|
|
expect(@result.misspellings.length).to.equal @misspellings.length-1
|
2017-06-22 04:23:47 -04:00
|
|
|
|
2014-08-15 07:13:35 -04:00
|
|
|
describe "learnWord", ->
|
|
|
|
describe "without a token", ->
|
|
|
|
beforeEach (done) -> @SpellingAPIManager.learnWord null, word: "banana", (@error) => done()
|
|
|
|
|
|
|
|
it "should return an error", ->
|
2017-06-22 05:09:30 -04:00
|
|
|
expect(@error).to.exist
|
|
|
|
expect(@error).to.be.instanceof Error
|
|
|
|
expect(@error.message).to.equal "no token provided"
|
2014-08-15 07:13:35 -04:00
|
|
|
|
|
|
|
describe "without a word", ->
|
|
|
|
beforeEach (done) -> @SpellingAPIManager.learnWord @token, {}, (@error) => done()
|
|
|
|
|
|
|
|
it "should return an error", ->
|
2017-06-22 05:09:30 -04:00
|
|
|
expect(@error).to.exist
|
|
|
|
expect(@error).to.be.instanceof Error
|
|
|
|
expect(@error.message).to.equal "malformed JSON"
|
2014-08-15 07:13:35 -04:00
|
|
|
|
|
|
|
describe "with a word and a token", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@word = "banana"
|
|
|
|
@SpellingAPIManager.learnWord @token, word: @word, (@error) => done()
|
|
|
|
|
|
|
|
it "should call LearnedWordsManager.learnWord", ->
|
|
|
|
@LearnedWordsManager.learnWord.calledWith(@token, @word).should.equal true
|
|
|
|
|
|
|
|
|