ASpellWorkerTests.js 3.4 KB

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