noflo = require 'noflo'
path = require 'path'
chai = require 'chai'
baseDir = path.resolve __dirname, '../'

describe 'Publish component', ->
  c = null
  ins = null
  out = null
  loader = null
  before ->
    loader = new noflo.ComponentLoader baseDir
  beforeEach (done) ->
    @timeout 10000
    loader.load 'harpy/Publish', (err, instance) ->
      c = instance
      c.once 'ready', ->
        ins = noflo.internalSocket.createSocket()
        out = noflo.internalSocket.createSocket()
        c.inPorts.in.attach ins
        c.outPorts.out.attach out
        c.network.start()
        done()

  describe 'when instantiated', ->
    it 'should have an input port', ->
      chai.expect(c.inPorts.in).to.be.an 'object'
    it 'should have an output port', ->
      chai.expect(c.outPorts.out).to.be.an 'object'

  describe 'publishing a simple post', ->
    it 'should produce the desired push job', (done) ->
      @timeout 5000
      groups = []
      expected = [1, 'foo']
      out.on 'begingroup', (group) ->
        groups.push group
      out.on 'data', (data) ->
        chai.expect(data).to.have.keys [
          'path'
          'repo'
          'content'
          'branch'
          'operation'
          'message'
        ]
        chai.expect(groups).to.eql expected
        done()
      out.on 'endgroup', (group) ->
        groups.pop()
      ins.beginGroup grp for grp in expected
      ins.send
        id: '123'
        metadata:
          title: 'Hello world'
          datePublished: new Date
        content: [
          type: 'h1'
          html: '<h1>Hello world</h1>'
        ,
          type: 'text'
          html: '<p>Foo</p>'
        ]
        site: 'the-domains/example.net'
      ins.endGroup grp for grp in expected
