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

# This is how we handle Embed.ly, Word docs, etc
describe 'NormalizeItem component', ->
  c = null
  ins = null
  compress = null
  out = null
  beforeEach (done) ->
    @timeout 10000
    loader = new noflo.ComponentLoader baseDir
    loader.load 'harpy/NormalizeItem', (err, instance) ->
      return done err if err
      c = instance
      ins = noflo.internalSocket.createSocket()
      compress = noflo.internalSocket.createSocket()
      out = noflo.internalSocket.createSocket()
      c.inPorts.in.attach ins
      c.inPorts.compress.attach compress
      c.outPorts.out.attach out
      done()

  describe 'normalizing a Embed.ly Article', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.title).to.be.a 'string'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'MediaObject'
        chai.expect(item.metadata.inLanguage).to.equal 'en'
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()

  describe 'normalizing a YouTube video', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly_youtube.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'VideoObject'
        chai.expect(item.metadata['title']).to.equal 'Flowhub promo'
        chai.expect(item.metadata['description']).to.equal 'http://flowhub.io/ - the interface for Flow-Based Programming'
        chai.expect(item.metadata['author']).to.be.an 'array'
        chai.expect(item.metadata['author'].length).to.equal 1
        chai.expect(item.metadata['author'][0].name).to.equal 'Forrest O.'
        chai.expect(item.metadata['author'][0].url).to.equal 'http://www.youtube.com/user/forresto'
        chai.expect(item.metadata['author'][0].avatar).to.be.an 'object'
        chai.expect(item.metadata['author'][0].avatar.src).to.equal 'https://yt3.ggpht.com/-ko1EowQzTBw/AAAAAAAAAAI/AAAAAAAAAAA/18M26CoDVFU/s88-c-k-no/photo.jpg'
        chai.expect(item.metadata.publisher).to.be.an 'object'
        chai.expect(item.metadata.publisher.name).to.equal 'YouTube'
        chai.expect(item.metadata.publisher.domain).to.equal 'www.youtube.com'
        chai.expect(item.html.indexOf('<iframe ')).to.not.equal -1
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()

  describe 'normalizing a Vimeo video', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly_vimeo.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'VideoObject'
        chai.expect(item.metadata['title']).to.equal 'OBLIVION GFX Montage'
        chai.expect(item.metadata['description']).to.equal 'The mighty Joseph Kosinski invited Munkowitz to the GFX party once again, this time for his spring blockbuster feature film OBLIVION\u2122... Predictably, the list of graphic assets to be created was obscene, so munko assembled and led another super team of GFX mercenaries and descended into the lovely confines of Crater Lake Productions to generate the aforementioned fuckload of graphic content...'
        chai.expect(item.metadata['author'][0].name).to.equal 'GMUNK'
        chai.expect(item.metadata['author'][0].url).to.equal 'http://vimeo.com/gmunk'
        chai.expect(item.metadata['author'][0].avatar).to.be.an 'object'
        chai.expect(item.metadata['author'][0].avatar.src).to.equal 'http://i.vimeocdn.com/portrait/4074818_75x75.jpg'
        chai.expect(item.metadata.inLanguage).to.equal 'da'
        chai.expect(item.html.indexOf('<iframe ')).to.not.equal -1
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()
  describe 'normalizing a Embed.ly Tweet with link', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly_tweet_link.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'Comment'
        chai.expect(item.metadata['author'][0].name).to.equal 'bergie'
        chai.expect(item.metadata['author'][0].avatar.src).to.equal 'https://pbs.twimg.com/profile_images/458947075074310145/DnOuJdV1_bigger.jpeg'
        chai.expect(item.metadata['author'][0].url).to.equal 'http://twitter.com/bergie'
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()

  describe 'normalizing a Embed.ly Tweet with image', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly_tweet_image.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'Photograph'
        chai.expect(item.metadata['author'][0].name).to.equal 'bergie'
        chai.expect(item.metadata['author'][0].avatar.src).to.equal 'https://pbs.twimg.com/profile_images/458947075074310145/DnOuJdV1_bigger.jpeg'
        chai.expect(item.metadata['author'][0].avatar.colors).to.be.an 'array'
        chai.expect(item.metadata.description).to.equal 'the latest issue of @netmag has "a story" on http://gridstylesheets.org by @d4tocchini'
        chai.expect(item.html).to.equal '<img alt="the latest issue of @netmag has &quot;a story&quot; on http://gridstylesheets.org by @d4tocchini" src="https://pbs.twimg.com/media/BqUoLwsCEAAiXi_.jpg:large">'
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()

  describe 'normalizing a Embed.ly Flickr image', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly_flickr_image.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'Photograph'
        chai.expect(item.metadata['author'][0].name).to.equal 'henribergius'
        chai.expect(item.metadata['author'][0].url).to.equal 'https://www.flickr.com/photos/bergie/'
        chai.expect(item.metadata.title).to.equal 'Good morning, Hawaii'
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()
  describe 'normalizing a Embed.ly G+ Post with image', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly_googleplus_image.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'Photograph'
        chai.expect(item.metadata['author'][0].avatar.src).to.equal 'https://lh3.googleusercontent.com/-rTgIpKqTdjs/AAAAAAAAAAI/AAAAAAAAAuY/fxgefuaWCuo/s46-c-k-no/photo.jpg'
        chai.expect(item.metadata['author'][0].avatar.colors).to.be.an 'array'
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()

  describe 'normalizing a Embed.ly G+ Post without image', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly_googleplus.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'Comment'
        chai.expect(item.metadata['author'][0].avatar.src).to.equal 'https://lh3.googleusercontent.com/-rTgIpKqTdjs/AAAAAAAAAAI/AAAAAAAAAuY/fxgefuaWCuo/s46-c-k-no/photo.jpg'
        chai.expect(item.metadata['author'][0].avatar.colors).to.be.an 'array'
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()

  describe 'normalizing a Embed.ly Facebook Post with image', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly_facebook.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'Photograph'
        chai.expect(item.metadata['author'][0].avatar.src).to.equal 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xaf1/v/t1.0-1/p50x50/10441181_10152467390833896_3682906746883958140_n.jpg?oh=55e1cfe6d3d157893190ed4e463a35fe&oe=54F5463E&__gda__=1421404290_ac207ebbf117394f6a54e15d6714cb26'
        chai.expect(item.metadata['author'][0].avatar.colors).to.be.an 'array'
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()
  describe 'normalizing a SoundCloud song', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly_soundcloud.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'AudioObject'
        chai.expect(item.metadata['title']).to.equal 'Zeds Dead - Lost You (feat. Twin Shadow & D\'Angelo Lacy) by Zeds Dead'
        chai.expect(item.metadata['description']).to.equal 'MUSIC VIDEO : https://www.youtube.com/watch?v=VJm7IPrBmLY Zeds Dead www.facebook.com/zedsdead www.twitter.com/whoszed www.instagram.com/zedsdeadofficial Twin Shadow www.facebook.com/twnshdw www.twitter.com/twinshadow www.instagram.com/thetwinshadow'
        chai.expect(item.metadata['author'][0].name).to.equal 'Zeds Dead'
        chai.expect(item.metadata['author'][0].url).to.equal 'http://soundcloud.com/zedsdead'
        chai.expect(item.html.indexOf('<iframe ')).to.not.equal -1
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()

  describe 'normalizing a content-free Apple marketing page', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/embedly_apple.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'MediaObject'
        chai.expect(item.metadata['title']).to.equal 'Apple - MacBook Air - Stickers'
        chai.expect(item.metadata['description']).to.equal 'MacBook Air. The notebook people love.'
        chai.expect(item.html.indexOf('<h1>')).to.not.equal -1
        chai.expect(item.html.indexOf('<img ')).to.not.equal -1
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()
  describe 'normalizing a Diffbot Article', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/diffbot.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.author).to.be.an 'array'
        chai.expect(item.metadata.title).to.be.a 'string'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'MediaObject'
        chai.expect(item.metadata.publisher).to.an 'object'
        chai.expect(item.metadata.publisher.name).to.equal 'interfacevision.com'
        chai.expect(item.metadata.publisher.domain).to.equal 'blog.interfacevision.com'
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send true
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()

  describe 'normalizing an item with content', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/payload.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.publisher).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata.description).to.be.a 'string'
        chai.expect(item.metadata.description.indexOf('<')).to.equal -1
        chai.expect(item.metadata['@type']).to.equal 'Article'
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send false
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()

  describe 'normalizing a gist', ->
    it 'should produce valid Schema.org metadata', (done) ->
      itemPath = path.resolve __dirname, 'fixtures/normalize/gist.json'

      received = false
      out.on 'data', (item) ->
        chai.expect(item).to.be.an 'object'
        chai.expect(item).to.have.keys [
          'id'
          'html'
          'metadata'
          'options'
        ]
        chai.expect(item.metadata).to.be.an 'object'
        chai.expect(item.metadata.publisher).to.be.an 'object'
        chai.expect(item.metadata.isBasedOnUrl).to.be.a 'string'
        chai.expect(item.metadata.title).to.be.a 'string'
        chai.expect(item.metadata['@type']).to.equal 'Code'
        received = true
      out.on 'disconnect', ->
        chai.expect(received).to.equal true
        done()

      compress.send false
      ins.send JSON.parse fs.readFileSync itemPath, 'utf-8'
      ins.disconnect()
