'use strict';

require('chai').should();
const Hexo = require('hexo');
const cheerio = require('cheerio');

describe('Sitemap generator', () => {
  const hexo = new Hexo(__dirname, {silent: true});
  hexo.config.sitemap = {
    path: 'sitemap.xml'
  };
  const Post = hexo.model('Post');
  const generator = require('../lib/generator').bind(hexo);
  const sitemapTmpl = require('../lib/template')(hexo.config);
  let posts = {};
  let locals = {};

  before(() => {
    return hexo.init().then(() => {
      return Post.insert([
        {source: 'foo', slug: 'foo', updated: 1e8},
        {source: 'bar', slug: 'bar', updated: 1e8 + 1},
        {source: 'baz', slug: 'baz', updated: 1e8 - 1}
      ]).then(data => {
        posts = data.sort((a, b) => b.updated - a.updated);
        locals = hexo.locals.toObject();
      });
    });
  });

  it('default', () => {
    const result = generator(locals);

    result.path.should.eql('sitemap.xml');
    result.data.should.eql(sitemapTmpl.render({
      config: hexo.config,
      posts: posts
    }));

    const $ = cheerio.load(result.data);

    $('url').each((index, element) => {
      $(element).children('loc').text().should.eql(posts[index].permalink);
      $(element).children('lastmod').text().should.eql(posts[index].updated.toISOString());
    });
  });

  describe('skip_render', () => {
    it('array', () => {
      hexo.config.skip_render = ['foo'];

      const result = generator(locals);
      result.data.should.not.contain('foo');
    });

    it('string', () => {
      hexo.config.skip_render = 'bar';

      const result = generator(locals);
      result.data.should.not.contain('bar');
    });

    it('off', () => {
      hexo.config.skip_render = null;

      const result = generator(locals);
      result.should.be.ok;
    });
  });
});

@@ -281,16 +281,51 @@
 Post');%0A
+  const Page = hexo.model('Page');%0A
   const 
@@ -457,16 +457,34 @@
 s = %7B%7D;%0A
+  let pages = %7B%7D;%0A
   let lo
@@ -822,16 +822,319 @@
 dated);%0A
+      %7D).then(() =%3E %7B%0A        return Page.insert(%5B%0A          %7Bsource: 'bio/index.md', path: 'bio/', updated: 1e8 - 3%7D,%0A          %7Bsource: 'about/index.md', path: 'about/', updated: 1e8 - 4%7D%0A        %5D);%0A      %7D).then(data =%3E %7B%0A        posts = Post.sort('-updated');%0A        pages = Page.sort('-update');%0A
         
@@ -1390,16 +1390,50 @@
 s: posts
+.toArray().concat(pages.toArray())
 %0A    %7D))
@@ -1475,16 +1475,87 @@
 t.data);
+%0A    const allPosts = Object.assign(%7B%7D, posts.data.concat(pages.data));
 %0A%0A    $(
@@ -1631,33 +1631,36 @@
 xt().should.eql(
-p
+allP
 osts%5Bindex%5D.perm
@@ -1722,17 +1722,20 @@
 uld.eql(
-p
+allP
 osts%5Bind

add test for pages