ASpellWorkerTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-undef
  4. */
  5. const sinon = require('sinon')
  6. const { expect } = require('chai')
  7. const SandboxedModule = require('sandboxed-module')
  8. const EventEmitter = require('events')
  9. describe('ASpellWorker', function () {
  10. beforeEach(function () {
  11. this.child_process = {}
  12. return (this.ASpellWorker = SandboxedModule.require(
  13. '../../../app/js/ASpellWorker',
  14. {
  15. requires: {
  16. '@overleaf/metrics': {
  17. gauge() {},
  18. inc() {}
  19. },
  20. child_process: this.child_process
  21. }
  22. }
  23. ))
  24. })
  25. describe('creating a worker', function () {
  26. beforeEach(function () {
  27. this.pipe = {
  28. stdout: new EventEmitter(),
  29. stderr: { on: sinon.stub() },
  30. stdin: { on: sinon.stub() },
  31. on: sinon.stub(),
  32. pid: 12345
  33. }
  34. this.child_process.spawn = sinon.stub().returns(this.pipe)
  35. this.pipe.stdout.setEncoding = sinon.stub()
  36. worker = new this.ASpellWorker('en')
  37. })
  38. describe('with normal aspell output', function () {
  39. beforeEach(function () {
  40. this.callback = worker.callback = sinon.stub()
  41. this.pipe.stdout.emit('data', '& hello\n')
  42. this.pipe.stdout.emit('data', '& world\n')
  43. this.pipe.stdout.emit('data', 'en\n')
  44. this.pipe.stdout.emit('data', '& goodbye')
  45. })
  46. it('should call the callback', function () {
  47. expect(this.callback.called).to.equal(true)
  48. expect(
  49. this.callback.calledWith(null, '& hello\n& world\nen\n')
  50. ).to.equal(true)
  51. })
  52. })
  53. describe('with the aspell end marker split across chunks', function () {
  54. beforeEach(function () {
  55. this.callback = worker.callback = sinon.stub()
  56. this.pipe.stdout.emit('data', '& hello\n')
  57. this.pipe.stdout.emit('data', '& world\ne')
  58. this.pipe.stdout.emit('data', 'n\n')
  59. this.pipe.stdout.emit('data', '& goodbye')
  60. })
  61. it('should call the callback', function () {
  62. expect(this.callback.called).to.equal(true)
  63. expect(
  64. this.callback.calledWith(null, '& hello\n& world\nen\n')
  65. ).to.equal(true)
  66. })
  67. })
  68. describe('with the aspell end marker newline split across chunks', function () {
  69. beforeEach(function () {
  70. this.callback = worker.callback = sinon.stub()
  71. this.pipe.stdout.emit('data', '& hello\n')
  72. this.pipe.stdout.emit('data', '& world\n')
  73. this.pipe.stdout.emit('data', 'en')
  74. this.pipe.stdout.emit('data', '\n& goodbye')
  75. })
  76. it('should call the callback', function () {
  77. expect(this.callback.called).to.equal(true)
  78. expect(this.callback.calledWith(null, '& hello\n& world\nen')).to.equal(
  79. true
  80. )
  81. })
  82. })
  83. describe('with everything split across chunks', function () {
  84. beforeEach(function () {
  85. this.callback = worker.callback = sinon.stub()
  86. '& hello\n& world\nen\n& goodbye'.split('').forEach((x) => {
  87. this.pipe.stdout.emit('data', x)
  88. })
  89. })
  90. it('should call the callback', function () {
  91. expect(this.callback.called).to.equal(true)
  92. expect(this.callback.calledWith(null, '& hello\n& world\nen')).to.equal(
  93. true
  94. )
  95. })
  96. })
  97. })
  98. })