|
| 1 | +import { expect } from 'chai' |
| 2 | +import sinon from 'sinon' |
| 3 | +import event from '../../../lib/event.js' |
| 4 | +import MetaStep from '../../../lib/step/meta.js' |
| 5 | +import Step from '../../../lib/step.js' |
| 6 | + |
| 7 | +describe('MetaStep Logging', () => { |
| 8 | + let eventStub |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + event.cleanDispatcher() |
| 12 | + eventStub = { |
| 13 | + stepStarted: sinon.spy(), |
| 14 | + stepFinished: sinon.spy(), |
| 15 | + } |
| 16 | + event.dispatcher.on(event.step.started, eventStub.stepStarted) |
| 17 | + event.dispatcher.on(event.step.finished, eventStub.stepFinished) |
| 18 | + }) |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + event.cleanDispatcher() |
| 22 | + }) |
| 23 | + |
| 24 | + it('should emit step.started and step.finished for MetaStep without child steps', () => { |
| 25 | + const metaStep = new MetaStep('I', 'doSomething') |
| 26 | + const fn = () => { |
| 27 | + // MetaStep that doesn't call any child I steps |
| 28 | + console.log('Just a console.log') |
| 29 | + } |
| 30 | + |
| 31 | + metaStep.run(fn) |
| 32 | + |
| 33 | + // Should emit step.started and step.finished |
| 34 | + expect(eventStub.stepStarted.calledOnce).to.be.true |
| 35 | + expect(eventStub.stepFinished.calledOnce).to.be.true |
| 36 | + expect(eventStub.stepStarted.firstCall.args[0]).to.equal(metaStep) |
| 37 | + expect(eventStub.stepFinished.firstCall.args[0]).to.equal(metaStep) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should NOT emit events for MetaStep WITH child steps', () => { |
| 41 | + const metaStep = new MetaStep('I', 'doSomethingWithChild') |
| 42 | + const fn = () => { |
| 43 | + // Simulate a child step being registered |
| 44 | + const childStep = new Step({ helper: 'test' }, 'childAction') |
| 45 | + event.emit(event.step.before, childStep) |
| 46 | + } |
| 47 | + |
| 48 | + metaStep.run(fn) |
| 49 | + |
| 50 | + // Should NOT emit step.started and step.finished for the MetaStep |
| 51 | + // because it has child steps |
| 52 | + expect(eventStub.stepStarted.called).to.be.false |
| 53 | + expect(eventStub.stepFinished.called).to.be.false |
| 54 | + }) |
| 55 | + |
| 56 | + it('should emit events for async MetaStep without child steps', async () => { |
| 57 | + const metaStep = new MetaStep('I', 'doSomethingAsync') |
| 58 | + const fn = async () => { |
| 59 | + // Async MetaStep that doesn't call any child I steps |
| 60 | + await Promise.resolve() |
| 61 | + console.log('Just an async operation') |
| 62 | + } |
| 63 | + |
| 64 | + await metaStep.run(fn) |
| 65 | + |
| 66 | + // Should emit step.started and step.finished |
| 67 | + expect(eventStub.stepStarted.calledOnce).to.be.true |
| 68 | + expect(eventStub.stepFinished.calledOnce).to.be.true |
| 69 | + expect(eventStub.stepStarted.firstCall.args[0]).to.equal(metaStep) |
| 70 | + expect(eventStub.stepFinished.firstCall.args[0]).to.equal(metaStep) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should NOT emit events for async MetaStep WITH child steps', async () => { |
| 74 | + const metaStep = new MetaStep('I', 'doSomethingAsyncWithChild') |
| 75 | + const fn = async () => { |
| 76 | + // Simulate a child step being registered |
| 77 | + const childStep = new Step({ helper: 'test' }, 'childAction') |
| 78 | + event.emit(event.step.before, childStep) |
| 79 | + await Promise.resolve() |
| 80 | + } |
| 81 | + |
| 82 | + await metaStep.run(fn) |
| 83 | + |
| 84 | + // Should NOT emit step.started and step.finished for the MetaStep |
| 85 | + // because it has child steps |
| 86 | + expect(eventStub.stepStarted.called).to.be.false |
| 87 | + expect(eventStub.stepFinished.called).to.be.false |
| 88 | + }) |
| 89 | + |
| 90 | + it('should set status to success when MetaStep completes without error', () => { |
| 91 | + const metaStep = new MetaStep('I', 'doSomething') |
| 92 | + const fn = () => { |
| 93 | + return 'success' |
| 94 | + } |
| 95 | + |
| 96 | + metaStep.run(fn) |
| 97 | + |
| 98 | + expect(metaStep.status).to.equal('success') |
| 99 | + }) |
| 100 | + |
| 101 | + it('should set status to failed when MetaStep throws error', () => { |
| 102 | + const metaStep = new MetaStep('I', 'doSomethingThatFails') |
| 103 | + const fn = () => { |
| 104 | + throw new Error('Test error') |
| 105 | + } |
| 106 | + |
| 107 | + try { |
| 108 | + metaStep.run(fn) |
| 109 | + } catch (err) { |
| 110 | + // Expected to throw |
| 111 | + } |
| 112 | + |
| 113 | + expect(metaStep.status).to.equal('failed') |
| 114 | + }) |
| 115 | +}) |
0 commit comments