ASpellWorkerTests.js 3.2 KB

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