2019-10-02 11:29:18 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-undef
|
|
|
|
*/
|
|
|
|
const sinon = require('sinon')
|
2021-03-19 16:04:30 -04:00
|
|
|
const { expect } = require('chai')
|
2019-10-02 11:29:18 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const EventEmitter = require('events')
|
|
|
|
|
2020-08-10 12:23:15 -04:00
|
|
|
describe('ASpellWorker', function () {
|
|
|
|
beforeEach(function () {
|
2019-10-02 11:29:18 -04:00
|
|
|
this.child_process = {}
|
2019-10-03 04:41:16 -04:00
|
|
|
return (this.ASpellWorker = SandboxedModule.require(
|
|
|
|
'../../../app/js/ASpellWorker',
|
|
|
|
{
|
|
|
|
requires: {
|
2020-11-25 06:57:24 -05:00
|
|
|
'@overleaf/metrics': {
|
2019-10-03 04:41:16 -04:00
|
|
|
gauge() {},
|
2021-07-13 07:04:47 -04:00
|
|
|
inc() {},
|
2019-10-03 04:41:16 -04:00
|
|
|
},
|
2021-07-13 07:04:47 -04:00
|
|
|
child_process: this.child_process,
|
|
|
|
},
|
2019-10-02 11:29:18 -04:00
|
|
|
}
|
2019-10-03 04:41:16 -04:00
|
|
|
))
|
2019-10-02 11:29:18 -04:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:23:15 -04:00
|
|
|
describe('creating a worker', function () {
|
|
|
|
beforeEach(function () {
|
2019-10-02 11:29:18 -04:00
|
|
|
this.pipe = {
|
2019-10-03 04:41:16 -04:00
|
|
|
stdout: new EventEmitter(),
|
|
|
|
stderr: { on: sinon.stub() },
|
|
|
|
stdin: { on: sinon.stub() },
|
|
|
|
on: sinon.stub(),
|
2021-07-13 07:04:47 -04:00
|
|
|
pid: 12345,
|
2019-10-02 11:29:18 -04:00
|
|
|
}
|
|
|
|
this.child_process.spawn = sinon.stub().returns(this.pipe)
|
|
|
|
this.pipe.stdout.setEncoding = sinon.stub()
|
|
|
|
worker = new this.ASpellWorker('en')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:23:15 -04:00
|
|
|
describe('with normal aspell output', function () {
|
|
|
|
beforeEach(function () {
|
2019-10-02 11:29:18 -04:00
|
|
|
this.callback = worker.callback = sinon.stub()
|
|
|
|
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 12:23:15 -04:00
|
|
|
it('should call the callback', function () {
|
2019-10-02 11:29:18 -04:00
|
|
|
expect(this.callback.called).to.equal(true)
|
2019-10-03 04:41:16 -04:00
|
|
|
expect(
|
|
|
|
this.callback.calledWith(null, '& hello\n& world\nen\n')
|
|
|
|
).to.equal(true)
|
2019-10-02 11:29:18 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:23:15 -04:00
|
|
|
describe('with the aspell end marker split across chunks', function () {
|
|
|
|
beforeEach(function () {
|
2019-10-02 11:29:18 -04:00
|
|
|
this.callback = worker.callback = sinon.stub()
|
|
|
|
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 12:23:15 -04:00
|
|
|
it('should call the callback', function () {
|
2019-10-02 11:29:18 -04:00
|
|
|
expect(this.callback.called).to.equal(true)
|
2019-10-03 04:41:16 -04:00
|
|
|
expect(
|
|
|
|
this.callback.calledWith(null, '& hello\n& world\nen\n')
|
|
|
|
).to.equal(true)
|
2019-10-02 11:29:18 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:23:15 -04:00
|
|
|
describe('with the aspell end marker newline split across chunks', function () {
|
|
|
|
beforeEach(function () {
|
2019-10-02 11:29:18 -04:00
|
|
|
this.callback = worker.callback = sinon.stub()
|
|
|
|
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 12:23:15 -04:00
|
|
|
it('should call the callback', function () {
|
2019-10-02 11:29:18 -04:00
|
|
|
expect(this.callback.called).to.equal(true)
|
2019-10-03 04:41:16 -04:00
|
|
|
expect(this.callback.calledWith(null, '& hello\n& world\nen')).to.equal(
|
|
|
|
true
|
|
|
|
)
|
2019-10-02 11:29:18 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:23:15 -04:00
|
|
|
describe('with everything split across chunks', function () {
|
|
|
|
beforeEach(function () {
|
2019-10-02 11:29:18 -04:00
|
|
|
this.callback = worker.callback = sinon.stub()
|
2021-07-13 07:04:47 -04:00
|
|
|
'& hello\n& world\nen\n& goodbye'.split('').forEach(x => {
|
2019-10-02 11:29:18 -04:00
|
|
|
this.pipe.stdout.emit('data', x)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:23:15 -04:00
|
|
|
it('should call the callback', function () {
|
2019-10-02 11:29:18 -04:00
|
|
|
expect(this.callback.called).to.equal(true)
|
2019-10-03 04:41:16 -04:00
|
|
|
expect(this.callback.calledWith(null, '& hello\n& world\nen')).to.equal(
|
|
|
|
true
|
|
|
|
)
|
2019-10-02 11:29:18 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|