var chai = require('chai');
var mock = require('chai-kerouac-middleware');
var sitemap = require('../../lib');


describe('middleware/sitemapindex', function() {
  
  it('should include sitemap', function(done) {
    chai.kerouac.use(sitemap.index())
      .request(function(page) {
        page.absoluteURL = '/sitemap_index.xml';
        page.locals = {};
        page.locals.sitemaps = [
          { fullURL: 'http://www.example.com/sitemap.xml', isSitemap: true }
        ];
      })
      .finish(function() {
        var expected = [
          '<?xml version="1.0" encoding="UTF-8"?>',
          '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
          '  <sitemap>',
          '    <loc>http://www.example.com/sitemap.xml</loc>',
          '  </sitemap>',
          '</sitemapindex>',
          ''
        ].join("\n");
    
        expect(this.body).to.equal(expected);
        expect(this.isSitemap).to.equal(true);
        expect(this.locals.sitemaps[0].isInSitemap).to.equal(true);
        done();
      })
      .generate();
  }); // should include sitemap
  
  it('should include multiple sitemaps', function(done) {
    chai.kerouac.use(sitemap.index())
      .request(function(page) {
        page.locals = {};
        page.locals.sitemaps = [
          { fullURL: 'http://www.example.com/sitemap.xml', isSitemap: true },
          { fullURL: 'http://www.example.com/blog/sitemap.xml', isSitemap: true }
        ];
      })
      .finish(function() {
        var expected = [
          '<?xml version="1.0" encoding="UTF-8"?>',
          '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
          '  <sitemap>',
          '    <loc>http://www.example.com/sitemap.xml</loc>',
          '  </sitemap>',
          '  <sitemap>',
          '    <loc>http://www.example.com/blog/sitemap.xml</loc>',
          '  </sitemap>',
          '</sitemapindex>',
          ''
        ].join("\n");
    
        expect(this.body).to.equal(expected);
        expect(this.isSitemap).to.equal(true);
        expect(this.locals.sitemaps[0].isInSitemap).to.equal(true);
        expect(this.locals.sitemaps[1].isInSitemap).to.equal(true);
        done();
      })
      .generate();
  }); // should include multiple sitemaps
  
  it('should not include sitemaps which are already in a sitemap', function(done) {
    chai.kerouac.use(sitemap.index())
      .request(function(page) {
        page.locals = {};
        page.locals.sitemaps = [
          { fullURL: 'http://www.example.com/sitemap.xml', isSitemap: true },
          { fullURL: 'http://www.example.com/blog/sitemap.xml', isSitemap: true, isInSitemap: true }
        ];
      })
      .finish(function() {
        var expected = [
          '<?xml version="1.0" encoding="UTF-8"?>',
          '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
          '  <sitemap>',
          '    <loc>http://www.example.com/sitemap.xml</loc>',
          '  </sitemap>',
          '</sitemapindex>',
          ''
        ].join("\n");
    
        expect(this.body).to.equal(expected);
        done();
      })
      .generate();
  }); // should not include sitemaps which are already in a sitemap
  
  it('should error when fully-qualified URL is not known', function(done) {
    chai.kerouac.use(sitemap.index())
      .request(function(page) {
        page.locals = {};
        page.locals.sitemaps = [
          { absoluteURL: '/sitemap.xml', isSitemap: true }
        ];
      })
      .next(function(err) {
        expect(err).to.be.an.instanceOf(Error);
        expect(err.message).to.equal('Unable to add "/sitemap.xml" to sitemap, set \'base url\' setting and try again');
        done();
      })
      .generate();
  }); // should error when fully-qualified URL is not known
  
});

@@ -1208,32 +1208,81 @@
 unction(page) %7B%0A
+        page.absoluteURL = '/sitemap_index.xml';%0A
         page.loc
@@ -1283,32 +1283,32 @@
 ge.locals = %7B%7D;%0A
-
         page.loc
@@ -2304,24 +2304,665 @@
 sitemaps%0A  %0A
+  it('should include no sitemaps when locals are not available', function(done) %7B%0A    chai.kerouac.use(sitemap.index())%0A      .request(function(page) %7B%0A        page.absoluteURL = '/sitemap_index.xml';%0A      %7D)%0A      .finish(function() %7B%0A        var expected = %5B%0A          '%3C?xml version=%221.0%22 encoding=%22UTF-8%22?%3E',%0A          '%3Csitemapindex xmlns=%22http://www.sitemaps.org/schemas/sitemap/0.9%22/%3E',%0A          ''%0A        %5D.join(%22%5Cn%22);%0A    %0A        expect(this.body).to.equal(expected);%0A        expect(this.isSitemap).to.equal(true);%0A        done();%0A      %7D)%0A      .generate();%0A  %7D); // should include no sitemaps when locals are not available%0A  %0A
   it('should
@@ -3095,32 +3095,81 @@
 unction(page) %7B%0A
+        page.absoluteURL = '/sitemap_index.xml';%0A
         page.loc
@@ -4044,32 +4044,32 @@
 itemap.index())%0A
-
       .request(f
@@ -4076,32 +4076,81 @@
 unction(page) %7B%0A
+        page.absoluteURL = '/sitemap_index.xml';%0A
         page.loc

Add tests.