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

describe 'BuildFilename component', ->
  c = null
  ins = null
  out = null
  beforeEach (done) ->
    @timeout 10000
    loader = new noflo.ComponentLoader baseDir
    loader.load 'harpy/BuildFilename', (err, instance) ->
      return done err if err
      c = instance
      ins = noflo.internalSocket.createSocket()
      out = noflo.internalSocket.createSocket()
      c.inPorts.in.attach ins
      c.outPorts.out.attach out
      done()

  describe 'generating a filename for a simple article', ->
    it 'should produce the correct filename', (done) ->
      out.on 'data', (data) ->
        chai.expect(data).to.equal '_posts/2013-01-07-hello-world.md'
        done()

      ins.send
        metadata:
          title: 'Hello, World!'
          datePublished: '2013-01-07 11:12:00Z'

  describe 'generating a filename for a article with scandinavian chars', ->
    it 'should produce the correct filename', (done) ->
      out.on 'data', (data) ->
        chai.expect(data).to.equal '_posts/2015-01-07-ala-lyo-aalio-oolia-laikkyy.md'
        done()

      ins.send
        metadata:
          title: 'älä lyö ääliö ööliä läikkyy'
          datePublished: '2015-01-07 11:12:00Z'

  describe 'generating a filename for a article with Germanic chars', ->
    it 'should produce the correct filename', (done) ->
      out.on 'data', (data) ->
        chai.expect(data).to.equal '_posts/2015-10-07-fusse.md'
        done()

      ins.send
        metadata:
          title: 'Füße'
          datePublished: '2015-10-07 11:12:00Z'

  describe 'generating a filename for a article with Russian chars', ->
    it 'should produce the correct filename', (done) ->
      out.on 'data', (data) ->
        chai.expect(data).to.equal '_posts/2015-01-01-kontakty.md'
        done()

      ins.send
        metadata:
          title: 'Контакты'
          datePublished: '2015-01-01 11:12:00Z'

  describe 'generating a filename for a article with Turkish chars', ->
    it 'should produce the correct filename', (done) ->
      out.on 'data', (data) ->
        chai.expect(data).to.equal '_posts/2014-12-07-sanirim-hepimiz-ayni-seyi-dusunuyoruz.md'
        done()

      ins.send
        metadata:
          title: 'Sanırım hepimiz aynı şeyi düşünüyoruz.'
          datePublished: '2014-12-07 21:12:00Z'

  describe 'generating a filename for an article without title', ->
    it 'should produce the correct filename', (done) ->
      out.on 'data', (data) ->
        chai.expect(data).to.equal '_posts/2013-04-30-578ab9e0-01e7-11e4-9191-0800200c9a66.md'
        done()

      ins.send
        id: '578ab9e0-01e7-11e4-9191-0800200c9a66'
        metadata:
          datePublished: '2013-04-30 11:12:00Z'

  describe 'keeping existing filename intact', ->
    it 'should produce the correct filename with metadata.sourcePath', (done) ->
      out.on 'data', (data) ->
        chai.expect(data).to.equal '_foo/2013-01-07-hello-world.md'
        done()

      ins.send
        id: '578ab9e0-01e7-11e4-9191-0800200c9a66'
        metadata:
          datePublished: '2013-04-30 11:12:00Z'
          sourcePath: '_foo/2013-01-07-hello-world.md'
    it 'should produce the correct filename with path', (done) ->
      out.on 'data', (data) ->
        chai.expect(data).to.equal '_posts/2013-01-07-hello-world.md'
        done()

      ins.send
        id: '578ab9e0-01e7-11e4-9191-0800200c9a66'
        path: '_posts/2013-01-07-hello-world.md'
        metadata:
          datePublished: '2013-04-30 11:12:00Z'
