text: import EscalationEventDefinition from '../../src/eventDefinitions/EscalationEventDefinition';
  import Environment from '../../src/Environment';
  import {ActivityBroker} from '../../src/EventBroker';
  import {Logger} from '../helpers/testHelpers';
  
  describe('EscalationEventDefinition', () => {
    let event;
    beforeEach(() => {
      event = {
        id: 'event',
        environment: Environment({Logger}),
        broker: ActivityBroker(this).broker,
      };
    });
  
    describe('catching', () => {
      it('publishes wait event on parent broker', () => {
        const catchSignal = EscalationEventDefinition(event, {
          type: 'bpmn:EscalationEventDefinition',
        });
  
        const messages = [];
        event.broker.subscribeTmp('event', 'activity.*', (_, msg) => {
          messages.push(msg);
        }, {noAck: true});
  
        catchSignal.execute({
          fields: {},
          content: {
            executionId: 'event_1_0',
            index: 0,
            parent: {
              id: 'bound',
              executionId: 'event_1',
              path: [{
                id: 'theProcess',
                executionId: 'theProcess_0'
              }]
            },
          },
        });
  
        expect(messages).to.have.length(1);
        expect(messages[0].fields).to.have.property('routingKey', 'activity.wait');
        expect(messages[0].content).to.have.property('executionId', 'event_1');
        expect(messages[0].content.parent).to.have.property('id', 'theProcess');
        expect(messages[0].content.parent).to.have.property('executionId', 'theProcess_0');
      });
  
      it('completes and clears listeners when escalation is caught', () => {
        const catchSignal = EscalationEventDefinition(event, {
          type: 'bpmn:EscalationEventDefinition',
        });
  
        const messages = [];
        event.broker.subscribeTmp('execution', 'execute.completed', (_, msg) => {
          messages.push(msg);
        }, {noAck: true, consumerTag: '_test-tag'});
  
        catchSignal.execute({
          fields: {},
          content: {
            executionId: 'event_1_0',
            index: 0,
            parent: {
              id: 'bound',
              executionId: 'event_1',
              path: [{
                id: 'theProcess',
                executionId: 'theProcess_0'
              }]
            },
          },
        });
  
        event.broker.publish('api', 'activity.escalate.event_1', {});
        event.broker.cancel('_test-tag');
  
        expect(messages).to.have.length(1);
  
        expect(event.broker).to.have.property('consumerCount', 0);
      });
  
      it('completes and clears listeners if escalated before execution', () => {
        const catchSignal = EscalationEventDefinition(event, {
          type: 'bpmn:EscalationEventDefinition',
        });
  
        event.broker.publish('api', 'activity.escalate.event_1', {});
  
        const messages = [];
        event.broker.subscribeTmp('execution', 'execute.completed', (_, msg) => {
          messages.push(msg);
        }, {noAck: true, consumerTag: '_test-tag'});
  
        catchSignal.execute({
          fields: {},
          content: {
            executionId: 'event_1_0',
            index: 0,
            parent: {
              id: 'bound',
              executionId: 'event_1',
              path: [{
                id: 'theProcess',
                executionId: 'theProcess_0'
              }]
            },
          },
        });
  
        event.broker.cancel('_test-tag');
  
        expect(messages).to.have.length(1);
  
        expect(event.broker).to.have.property('consumerCount', 0);
      });
  
      it('completes and clears listeners if discarded', () => {
        const catchSignal = EscalationEventDefinition(event, {
          type: 'bpmn:EscalationEventDefinition',
        });
  
        const messages = [];
        event.broker.subscribeTmp('execution', 'execute.discard', (_, msg) => {
          messages.push(msg);
        }, {noAck: true, consumerTag: '_test-tag'});
  
        catchSignal.execute({
          fields: {},
          content: {
            executionId: 'event_1_0',
            index: 0,
            parent: {
              id: 'bound',
              executionId: 'event_1',
              path: [{
                id: 'theProcess',
                executionId: 'theProcess_0'
              }]
            },
          },
        });
  
        event.broker.publish('api', 'activity.discard.event_1_0', {}, {type: 'discard'});
  
        event.broker.cancel('_test-tag');
  
        expect(messages).to.have.length(1);
  
        expect(event.broker).to.have.property('consumerCount', 0);
      });
  
      it('stops and clears listeners if stopped', () => {
        const catchSignal = EscalationEventDefinition(event, {
          type: 'bpmn:EscalationEventDefinition',
        });
  
        const messages = [];
        event.broker.subscribeTmp('execution', 'execute.discard', (_, msg) => {
          messages.push(msg);
        }, {noAck: true, consumerTag: '_test-tag'});
  
        catchSignal.execute({
          fields: {},
          content: {
            executionId: 'event_1_0',
            index: 0,
            parent: {
              id: 'bound',
              executionId: 'event_1',
              path: [{
                id: 'theProcess',
                executionId: 'theProcess_0'
              }]
            },
          },
        });
  
        event.broker.publish('api', 'activity.discard.event_1_0', {}, {type: 'discard'});
  
        event.broker.cancel('_test-tag');
  
        expect(messages).to.have.length(1);
  
        expect(event.broker).to.have.property('consumerCount', 0);
      });
    });
  
    describe('throwing', () => {
      it('publishes escalation event on parent broker', () => {
        event.isThrowing = true;
  
        const definition = EscalationEventDefinition(event, {
          type: 'bpmn:EscalationEventDefinition',
        });
  
        const messages = [];
        event.broker.subscribeTmp('event', 'activity.*', (_, msg) => {
          messages.push(msg);
        }, {noAck: true});
  
        definition.execute({
          fields: {},
          content: {
            executionId: 'event_1_0',
            index: 0,
            parent: {
              id: 'intermediate',
              executionId: 'event_1',
              path: [{
                id: 'theProcess',
                executionId: 'theProcess_0'
              }]
            },
          },
        });
  
        expect(messages).to.have.length(1);
        expect(messages[0].fields).to.have.property('routingKey', 'activity.escalate');
        expect(messages[0].content).to.have.property('executionId', 'event_1');
        expect(messages[0].content.parent).to.have.property('id', 'theProcess');
        expect(messages[0].content.parent).to.have.property('executionId', 'theProcess_0');
      });
    });
  });
  