2022-12-20 12:11:25 +00:00
|
|
|
import sinon from 'sinon'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import esmock from 'esmock'
|
|
|
|
import EventEmitter from 'events'
|
2019-10-02 15:29:18 +00:00
|
|
|
|
2020-08-10 16:23:15 +00:00
|
|
|
describe('ASpellWorker', function () {
|
2022-12-20 12:11:25 +00:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.pipe = {
|
|
|
|
stdout: new EventEmitter(),
|
|
|
|
stderr: { on: sinon.stub() },
|
|
|
|
stdin: { on: sinon.stub() },
|
|
|
|
on: sinon.stub(),
|
|
|
|
pid: 12345,
|
|
|
|
}
|
|
|
|
this.pipe.stdout.setEncoding = sinon.stub()
|
|
|
|
this.child_process = {
|
|
|
|
spawn: sinon.stub().returns(this.pipe),
|
|
|
|
}
|
|
|
|
const { ASpellWorker } = await esmock('../../../app/js/ASpellWorker', {
|
|
|
|
'@overleaf/metrics': {
|
|
|
|
gauge() {},
|
|
|
|
inc() {},
|
|
|
|
},
|
|
|
|
child_process: this.child_process,
|
|
|
|
})
|
|
|
|
this.ASpellWorker = ASpellWorker
|
2019-10-02 15:29:18 +00:00
|
|
|
})
|
|
|
|
|
2020-08-10 16:23:15 +00:00
|
|
|
describe('creating a worker', function () {
|
|
|
|
beforeEach(function () {
|
2022-12-20 12:11:25 +00:00
|
|
|
this.worker = new this.ASpellWorker('en')
|
2019-10-02 15:29:18 +00:00
|
|
|
})
|
|
|
|
|
2020-08-10 16:23:15 +00:00
|
|
|
describe('with normal aspell output', function () {
|
|
|
|
beforeEach(function () {
|
2022-12-20 12:11:25 +00:00
|
|
|
this.callback = this.worker.callback = sinon.stub()
|
2019-10-02 15:29:18 +00:00
|
|
|
this.pipe.stdout.emit('data', '& hello\n')
|
|
|
|
this.pipe.stdout.emit('data', '& world\n')
|
|
|
|
this.pipe.stdout.emit('data', 'en\n')
|
|
|
|
this.pipe.stdout.emit('data', '& goodbye')
|
|
|
|
})
|
|
|
|
|
2020-08-10 16:23:15 +00:00
|
|
|
it('should call the callback', function () {
|
2019-10-02 15:29:18 +00:00
|
|
|
expect(this.callback.called).to.equal(true)
|
2019-10-03 08:41:16 +00:00
|
|
|
expect(
|
|
|
|
this.callback.calledWith(null, '& hello\n& world\nen\n')
|
|
|
|
).to.equal(true)
|
2019-10-02 15:29:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 16:23:15 +00:00
|
|
|
describe('with the aspell end marker split across chunks', function () {
|
|
|
|
beforeEach(function () {
|
2022-12-20 12:11:25 +00:00
|
|
|
this.callback = this.worker.callback = sinon.stub()
|
2019-10-02 15:29:18 +00:00
|
|
|
this.pipe.stdout.emit('data', '& hello\n')
|
|
|
|
this.pipe.stdout.emit('data', '& world\ne')
|
|
|
|
this.pipe.stdout.emit('data', 'n\n')
|
|
|
|
this.pipe.stdout.emit('data', '& goodbye')
|
|
|
|
})
|
|
|
|
|
2020-08-10 16:23:15 +00:00
|
|
|
it('should call the callback', function () {
|
2019-10-02 15:29:18 +00:00
|
|
|
expect(this.callback.called).to.equal(true)
|
2019-10-03 08:41:16 +00:00
|
|
|
expect(
|
|
|
|
this.callback.calledWith(null, '& hello\n& world\nen\n')
|
|
|
|
).to.equal(true)
|
2019-10-02 15:29:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 16:23:15 +00:00
|
|
|
describe('with the aspell end marker newline split across chunks', function () {
|
|
|
|
beforeEach(function () {
|
2022-12-20 12:11:25 +00:00
|
|
|
this.callback = this.worker.callback = sinon.stub()
|
2019-10-02 15:29:18 +00:00
|
|
|
this.pipe.stdout.emit('data', '& hello\n')
|
|
|
|
this.pipe.stdout.emit('data', '& world\n')
|
|
|
|
this.pipe.stdout.emit('data', 'en')
|
|
|
|
this.pipe.stdout.emit('data', '\n& goodbye')
|
|
|
|
})
|
|
|
|
|
2020-08-10 16:23:15 +00:00
|
|
|
it('should call the callback', function () {
|
2019-10-02 15:29:18 +00:00
|
|
|
expect(this.callback.called).to.equal(true)
|
2019-10-03 08:41:16 +00:00
|
|
|
expect(this.callback.calledWith(null, '& hello\n& world\nen')).to.equal(
|
|
|
|
true
|
|
|
|
)
|
2019-10-02 15:29:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 16:23:15 +00:00
|
|
|
describe('with everything split across chunks', function () {
|
|
|
|
beforeEach(function () {
|
2022-12-20 12:11:25 +00:00
|
|
|
this.callback = this.worker.callback = sinon.stub()
|
2021-07-13 11:04:47 +00:00
|
|
|
'& hello\n& world\nen\n& goodbye'.split('').forEach(x => {
|
2019-10-02 15:29:18 +00:00
|
|
|
this.pipe.stdout.emit('data', x)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 16:23:15 +00:00
|
|
|
it('should call the callback', function () {
|
2019-10-02 15:29:18 +00:00
|
|
|
expect(this.callback.called).to.equal(true)
|
2019-10-03 08:41:16 +00:00
|
|
|
expect(this.callback.calledWith(null, '& hello\n& world\nen')).to.equal(
|
|
|
|
true
|
|
|
|
)
|
2019-10-02 15:29:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|