                 metadata: {}
                 graph: 'foo'
           ]
-          #receive expects, done
           connection.once 'message', (message) ->
             data = JSON.parse message.utf8Data
             validateSchema data, '/graph/output/clear'

                  tag: 'awesome'
                graph: 'foo'
            ]
            connection.once 'message', (message) ->
              data = JSON.parse message.utf8Data
              validateSchema data, '/graph/output/changenode'
              chai.expect(data.payload.metadata).to.eql expects[0].payload.metadata

              done()

            send 'graph', 'changenode',
              id: 'Drop1'
              metadata:

        command: command
        payload: payload

    messageMatches = (msg, expected) ->
      return false unless msg.protocol is expected.protocol
      return false unless msg.command is expected.command
      true
    getPacketSchema = (packet) ->
      return "#{packet.protocol}/output/#{packet.command}"

    receive = (expects, done, allowExtraPackets = false) ->
      listener = (message) ->
        # Basic validation
        chai.expect(message.utf8Data).to.be.a 'string'
        data = JSON.parse message.utf8Data
        chai.expect(data.protocol).to.be.a 'string'
        chai.expect(data.command).to.be.a 'string'
        # Validate all received packets against schema
        validateSchema data, getPacketSchema data
        # Don't ever expect payloads to return a secret
        chai.expect(data.payload.secret, 'Payload should not contain secret').to.be.a 'undefined'
        if allowExtraPackets and not messageMatches data, expects[0]
          # Ignore messages we don't care about in context of the test
          connection.once 'message', listener
          return

        # Validate actual payload
        expected = expects.shift()
        chai.expect(getPacketSchema(data)).to.equal getPacketSchema expected

        # Clear values that can't be checked with deep equal. We already
        # know they passed schema validation
        if data.protocol is 'network'
          if data.command is 'started'
            delete data.payload.time
            delete expected.payload.time
          if data.command is 'stopped'
            delete data.payload.time
            delete expected.payload.time
            delete data.payload.uptime
            delete expected.payload.uptime
        if data.command is 'error'
          delete data.payload.stack
          delete expected.payload.stack

        # Don't ever expect payloads to return a secret
        delete expected.payload.secret

        chai.expect(data.payload).to.eql expected.payload
        # Received all expected packets
        return done() unless expects.length
