/*jshint expr: true*/
/*global describe, it, before, beforeEach, after, afterEach */
import { expect } from 'chai';
import slug from 'slug';

import PostProcessor from '../../post-watch/post-processor';


describe('Post Processor - converts strings into post objects', function () {
    describe('read()', function () {
        let postProcessor;
        beforeEach(function () {
            postProcessor = new PostProcessor();
        });
        it('should read a simple, valid document', function () {
            // Post contents:
            let doc = `{
    "title": "A test document",
    "author": "author@example.com",
    "slug": "a-test-document",
    "tags": ["test", "development"]
}

A Test document
================

Testing...
`;
            //
            let post = postProcessor.read(doc);
            expect(post).to.be.an('object');
            expect(post.tags).to.be.an('array');
            expect(post.tags).to.have.length(2);
            expect(post.tags).to.include('test');
            expect(post.tags).to.include('development');
            expect(post.title).to.equal('A test document');
            expect(post.contents).to.equal(`A Test document
================

Testing...
`);
        });

        it('should detect invalid JSON', function () {
            let doc = '{"title": {"blah": false}\n\nTest\n=====';
            let post;
            expect(()=> post = postProcessor.read(doc)).to.throw(SyntaxError);
        });

        it('should auto-generate a value for the slug', () => {
            let doc = `{"title":"A Foobar Blog Post","tags":[],"author":"foobar@example.com"}\n\nFoobar\n======`;
            let post = postProcessor.read(doc);
            expect(post).to.be.an('object');
            expect(post.slug).to.be.a('string');
            expect(post.slug).to.equal('a-foobar-blog-post');
        });

        it('should auto-generate a value for the date', () => {
            let doc = `{"title":"A Foobar Blog Post","tags":[],"author":"foobar@example.com"}\n\nFoobar\n======`;
            let post = postProcessor.read(doc);
            expect(post).to.be.an('object');
            expect(post.date instanceof Date).to.be.true;
            expect(post.date.getTime()).not.to.be.nan;
            expect(post.date.toISOString()).to.match(/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/);
        });

        it('should auto-generate a value for the publish boolean', () => {
            let doc = `{"title":"A Foobar Blog Post","tags":[],"author":"foobar@example.com"}\n\nFoobar\n======`;
            let post = postProcessor.read(doc);
            expect(post).to.be.an('object');
            expect(post.publish).to.be.a('boolean');
            expect(post.publish).to.be.true;
        });

        it('should not return a post when the post\'s publish flag is false', () => {
            let doc = `{"title":"A Foobar Blog Post","tags":[],"author":"foobar@example.com","publish":false}\n\nFoobar\n======`;
            let post = postProcessor.read(doc);
            expect(post).to.equal(null);
        });

    });
});
