 
     beforeEach ->
       Transmitter.startTransmission (tr) =>
-        @createBackwardChannel().init(tr)
+        @createFlatChannel().init(tr)
         @createNestedChannel().init(tr)
 
 


        Transmitter.startTransmission (tr) =>
          tr.reverseOrder = order is 'reverse'
          @createBackwardChannel().init(tr)
          @createNestedChannel().init(tr)
          @createDerivedChannel().init(tr)



describe 'Flattening connection', ->

  beforeEach ->
    @define 'serializedVar', new Transmitter.Nodes.Variable()
    @define 'flatVar', new Transmitter.Nodes.Variable()
    @define 'nestedVar', new Transmitter.Nodes.Variable()
    @define 'nestedBackwardChannelVar',
      new Transmitter.ChannelNodes.ChannelVariable()
    @define 'nestedForwardChannelVar',
      new Transmitter.ChannelNodes.ChannelVariable()


    @createFlatChannel = =>
      new Transmitter.Channels.CompositeChannel()
        .defineChannel =>
          new Transmitter.Channels.SimpleChannel()
            .inForwardDirection()
            .fromSources @flatVar, @nestedVar
            .toTarget @serializedVar
            .withTransform ([flatPayload, nestedPayload]) ->
              flatPayload.merge(nestedPayload).map ([value, nestedObject]) ->
                {name: nestedObject?.name ? null, value: value ? null}

        .defineChannel =>
          new Transmitter.Channels.SimpleChannel()
            .inBackwardDirection()
            .fromSource @serializedVar
            .toTarget @nestedVar
            .withTransform (payload) ->
              payload.map (serialized) ->
                if serialized?
                  return new NestedObject(serialized.name)
                else
                  null


    @createNestedChannel = =>
      new Transmitter.Channels.CompositeChannel()
        .defineChannel =>
          new Transmitter.Channels.SimpleChannel()
            .fromSource @nestedVar
            .toConnectionTarget @nestedBackwardChannelVar
            .withTransform (payload) =>
              payload.map (nestedObject) =>
                valueVarDynamic = [nestedObject?.valueVar].filter (v) -> v?
                new Transmitter.Channels.SimpleChannel()
                  .inBackwardDirection()
                  .fromSource @serializedVar
                  .toDynamicTargets valueVarDynamic
                  .withTransform (serializedPayload) ->
                    [serializedPayload.map (serialized) -> serialized?.value]

        .defineChannel =>
          new Transmitter.Channels.SimpleChannel()
            .fromSource @nestedVar
            .toConnectionTarget @nestedForwardChannelVar
            .withTransform (payload) =>
              payload.map (nestedObject) =>
                valueVarDynamic = [nestedObject?.valueVar].filter (v) -> v?
                new Transmitter.Channels.SimpleChannel()
                  .inForwardDirection()
                  .fromDynamicSources valueVarDynamic
                  .toTarget @flatVar
                  .withTransform (valuePayloads) =>
                    valuePayloads.merge().map ([value]) -> value


  describe 'initialization', ->

    specify 'has default const value after initialization', ->
      Transmitter.startTransmission (tr) =>
        @createFlatChannel().init(tr)

      # Separate transmissions to test channel init querying
      Transmitter.startTransmission (tr) =>
