<commit_before>describe 'Global Projection Feature', ->

  firstContext = null
  secondContext = null

  beforeEach ->
    firstContext = createContextWithOneAggregate
      contextName: 'First'
      aggregateName: 'FirstContextAggregate'
      domainEventName: 'FirstContextAggregateCreated'

    secondContext = createContextWithOneAggregate
      contextName: 'Second'
      aggregateName: 'SecondContextAggregate'
      domainEventName: 'SecondContextAggregateCreated'

    Promise.all [
      firstContext.initialize()
      secondContext.initialize()
    ]


  describe 'given a global projection which handles events from multiple contexts', ->

    receivedDomainEventNames = null

    beforeEach ->
      receivedDomainEventNames = []
      class GlobalProjection
        initialize: (params, done) ->
          done()

        handleFirstContextAggregateCreated: (domainEvent) ->
          receivedDomainEventNames.push domainEvent.name

        handleSecondContextAggregateCreated: (domainEvent) ->
          receivedDomainEventNames.push domainEvent.name


      eventric.addGlobalProjection GlobalProjection


    describe 'initializing the projection', ->

      it 'should correctly replay those events given there are saved domain events from the first context', ->
        firstContext.command 'CreateAggregate'
        .then ->
          eventric.initializeGlobalProjections()
        .then ->
          expect(receivedDomainEventNames).to.deep.equal ['FirstContextAggregateCreated']


      it 'should correctly replay those events given there are saved domain events from the second context', ->
        secondContext.command 'CreateAggregate'
        .then ->
          eventric.initializeGlobalProjections()
        .then ->
          expect(receivedDomainEventNames).to.deep.equal ['SecondContextAggregateCreated']


      it 'should replay all events in correct order given there are saved domain events from all contexts', ->
        secondContext.command 'CreateAggregate'
        .then ->
          firstContext.command 'CreateAggregate'
        .then ->
          eventric.initializeGlobalProjections()
        .then ->
          expect(receivedDomainEventNames).to.deep.equal ['SecondContextAggregateCreated', 'FirstContextAggregateCreated']


    describe.only 'receiving domain events on the projection', ->

      it 'should correctly handle those events given new events are emitted on the first context', ->
        eventric.initializeGlobalProjections()
        .then -><commit_msg>Remove .only from global projection feature specs<commit_after>describe 'Global Projection Feature', ->

  firstContext = null
  secondContext = null

  beforeEach ->
    firstContext = createContextWithOneAggregate
      contextName: 'First'
      aggregateName: 'FirstContextAggregate'
      domainEventName: 'FirstContextAggregateCreated'

    secondContext = createContextWithOneAggregate
      contextName: 'Second'
      aggregateName: 'SecondContextAggregate'
      domainEventName: 'SecondContextAggregateCreated'

    Promise.all [
      firstContext.initialize()
      secondContext.initialize()
    ]


  describe 'given a global projection which handles events from multiple contexts', ->

    receivedDomainEventNames = null

    beforeEach ->
      receivedDomainEventNames = []
      class GlobalProjection
        initialize: (params, done) ->
          done()

        handleFirstContextAggregateCreated: (domainEvent) ->
          receivedDomainEventNames.push domainEvent.name

        handleSecondContextAggregateCreated: (domainEvent) ->
          receivedDomainEventNames.push domainEvent.name


      eventric.addGlobalProjection GlobalProjection


    describe 'initializing the projection', ->

      it 'should correctly replay those events given there are saved domain events from the first context', ->
        firstContext.command 'CreateAggregate'
        .then ->
          eventric.initializeGlobalProjections()
        .then ->
          expect(receivedDomainEventNames).to.deep.equal ['FirstContextAggregateCreated']


      it 'should correctly replay those events given there are saved domain events from the second context', ->
        secondContext.command 'CreateAggregate'
        .then ->
          eventric.initializeGlobalProjections()
        .then ->
          expect(receivedDomainEventNames).to.deep.equal ['SecondContextAggregateCreated']


      it 'should replay all events in correct order given there are saved domain events from all contexts', ->
        secondContext.command 'CreateAggregate'
        .then ->
          firstContext.command 'CreateAggregate'
        .then ->
          eventric.initializeGlobalProjections()
        .then ->
          expect(receivedDomainEventNames).to.deep.equal ['SecondContextAggregateCreated', 'FirstContextAggregateCreated']


    describe 'receiving domain events on the projection', ->

      it 'should correctly handle those events given new events are emitted on the first context', ->
        eventric.initializeGlobalProjections()
        .then ->
{'fraction_non_alphanumeric': 0.07262680086836393, 'fraction_numerical': 0.0, 'mean_word_length': 3.0838033843674455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '367739', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1173, 'n_words': 328}