use lib 'lib';
use lib 'specs/support';
use BDD::Behave;
use BloginTest;
use Blogin::Site;

my $FIXTURE = 'specs/fixtures/page-layout'.IO;


sub build-page-layout(IO::Path:D $out) {
  Blogin::Site::build(
    content => $FIXTURE.add('content'),
    out     => $out,
    layouts => $FIXTURE.add('layouts'),
  );
}

describe 'a page choosing its own layout through front matter', {
  let(:out, { temp-dir('page-layout') });

  before-each { build-page-layout(out()) }
  after-each  { nuke(out()) }

  it 'renders the page through the named layout', {
    expect(out().add('pages/fancy.html').slurp.contains('<article class=\'landing\'>')).to.be-truthy;
  }

  it 'still renders the post body inside that layout', {
    expect(out().add('pages/fancy.html').slurp.contains('A fancy page.')).to.be-truthy;
  }

  it 'falls back to the default show layout without a layout field', {
    expect(out().add('pages/normal.html').slurp.contains('<article class=\'default\'>')).to.be-truthy;
  }
}

describe 'a root index page', {
  let(:out, { temp-dir('root-index') });

  before-each { build-page-layout(out()) }
  after-each  { nuke(out()) }

  it 'renders a content/index.md to the site root', {
    expect(out().add('index.html').e).to.be-truthy;
  }

  it 'renders that page body at the root', {
    expect(out().add('index.html').slurp.contains('The front page.')).to.be-truthy;
  }

  it 'does not write a slug page for the root index', {
    expect(out().add('welcome.html').e).to.be-falsy;
  }
}
