<commit_before>﻿using System;
using System.Globalization;
using System.Linq;
using AlphaDev.Web.Tests.Integration.Fixtures;
using AlphaDev.Web.Tests.Integration.Support;
using FluentAssertions;
using JetBrains.Annotations;
using Markdig;
using Omego.Extensions.DbContextExtensions;
using Omego.Extensions.EnumerableExtensions;
using Omego.Extensions.QueryableExtensions;
using Optional;
using Optional.Unsafe;
using Xunit.Abstractions;

namespace AlphaDev.Web.Tests.Integration.Features
{
    [UsedImplicitly]
    public partial class Homepage_feature : WebFeatureFixture
    {
        public Homepage_feature(ITestOutputHelper output, DatabaseWebServerFixture databaseWebServerFixture)
            : base(output, databaseWebServerFixture)
        {
        }

        private void When_i_go_to_the_homepage()
        {
            SiteTester.HomePage.GoTo();
        }

        private void Then_it_should_load()
        {
            SiteTester.HomePage.Title.Should().BeEquivalentTo("Home - AlphaDev");
        }

        private void Then_it_should_display_navigation_links()
        {
            SiteTester.HomePage.Navigation.Select(element => element.Text).Should()
                .BeEquivalentTo("Posts", "About", "Contact");
        }

        private void Then_it_should_display_the_latest_blog_post()
        {
            SiteTester.HomePage.LatestBlog.ValueOrFailure().Should().BeEquivalentTo(
                DatabasesFixture.BlogContextDatabaseFixture.BlogContext.Blogs.OrderByDescending(blog => blog.Created)
                    .ToList().Select(blog => new
                    {
                        Dates = new
                        {
                            Created = blog.Created.ToString(FullDateFormatString, CultureInfo.InvariantCulture)
                        }
                    })
                    .FirstOrThrow(new InvalidOperationException("No blogs found.")),
                options => options.Including(post => post.Dates.Created));
        }

        private void And_the_latest_blog_post_was(ModifiedState modifiedState)
        {
            var modified = modifiedState == ModifiedState.Modified ? new DateTime(2017, 7, 12) : (DateTime?) null;

            DatabasesFixture.BlogContextDatabaseFixture.BlogContext.Blogs.OrderByDescending(blog => blog.Created)
                .First().Modified = modified;

            DatabasesFixture.BlogContextDatabaseFixture.BlogContext.SaveChanges();
        }

        private void Then_it_should_display_the_blog_with_a_modification_date_if_it_exists()
        {
            SiteTester.HomePage.LatestBlog.ValueOrFailure().Should().BeEquivalentTo(
                DatabasesFixture.BlogContextDatabaseFixture.BlogContext.Blogs.OrderByDescending(blog => blog.Created)
                    .ToList().Select(blog => new
                    {
                        Dates = new
                        {
                            Modified = blog.Modified.ToOption().FlatMap(time =>
                                Option.Some(time.ToString(FullDateFormatString, CultureInfo.InvariantCulture)))
                        }
                    }).FirstOrThrow(new InvalidOperationException("No blogs found.")),
                options => options.ExcludingMissingMembers());
        }

        private void Given_the_website_has_a_problem()
        {
            DatabasesFixture.BlogContextDatabaseFixture.BlogContext.Database.EnsureDeleted();
        }

        private void Then_it_should_display_an_error()
        {
            SiteTester.Driver.Title.Should().BeEquivalentTo("Error - AlphaDev");
        }

        private void Then_an_error_should_be_logged()
        {
            Log.Should().Contain("[Error] An unhandled exception has occurred");
        }

        private void Then_it_should_display_blog_post_with_markdown_parsed_to_html()
        {
            SiteTester.HomePage.LatestBlog.ValueOrFailure().Should().BeEquivalentTo(
                DatabasesFixture.BlogContextDatabaseFixture.BlogContext.Blogs.OrderByDescending(blog => blog.Created)
                    .ToList().Select(blog => new
                    {
                        Content = Markdown.ToHtml(blog.Content).Trim()
                    })<commit_msg>wait half a second for test error to be logged<commit_after>﻿using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using AlphaDev.Web.Tests.Integration.Fixtures;
using AlphaDev.Web.Tests.Integration.Support;
using FluentAssertions;
using JetBrains.Annotations;
using Markdig;
using Omego.Extensions.DbContextExtensions;
using Omego.Extensions.EnumerableExtensions;
using Omego.Extensions.QueryableExtensions;
using Optional;
using Optional.Unsafe;
using Xunit.Abstractions;

namespace AlphaDev.Web.Tests.Integration.Features
{
    [UsedImplicitly]
    public partial class Homepage_feature : WebFeatureFixture
    {
        public Homepage_feature(ITestOutputHelper output, DatabaseWebServerFixture databaseWebServerFixture)
            : base(output, databaseWebServerFixture)
        {
        }

        private void When_i_go_to_the_homepage()
        {
            SiteTester.HomePage.GoTo();
        }

        private void Then_it_should_load()
        {
            SiteTester.HomePage.Title.Should().BeEquivalentTo("Home - AlphaDev");
        }

        private void Then_it_should_display_navigation_links()
        {
            SiteTester.HomePage.Navigation.Select(element => element.Text).Should()
                .BeEquivalentTo("Posts", "About", "Contact");
        }

        private void Then_it_should_display_the_latest_blog_post()
        {
            SiteTester.HomePage.LatestBlog.ValueOrFailure().Should().BeEquivalentTo(
                DatabasesFixture.BlogContextDatabaseFixture.BlogContext.Blogs.OrderByDescending(blog => blog.Created)
                    .ToList().Select(blog => new
                    {
                        Dates = new
                        {
                            Created = blog.Created.ToString(FullDateFormatString, CultureInfo.InvariantCulture)
                        }
                    })
                    .FirstOrThrow(new InvalidOperationException("No blogs found.")),
                options => options.Including(post => post.Dates.Created));
        }

        private void And_the_latest_blog_post_was(ModifiedState modifiedState)
        {
            var modified = modifiedState == ModifiedState.Modified ? new DateTime(2017, 7, 12) : (DateTime?) null;

            DatabasesFixture.BlogContextDatabaseFixture.BlogContext.Blogs.OrderByDescending(blog => blog.Created)
                .First().Modified = modified;

            DatabasesFixture.BlogContextDatabaseFixture.BlogContext.SaveChanges();
        }

        private void Then_it_should_display_the_blog_with_a_modification_date_if_it_exists()
        {
            SiteTester.HomePage.LatestBlog.ValueOrFailure().Should().BeEquivalentTo(
                DatabasesFixture.BlogContextDatabaseFixture.BlogContext.Blogs.OrderByDescending(blog => blog.Created)
                    .ToList().Select(blog => new
                    {
                        Dates = new
                        {
                            Modified = blog.Modified.ToOption().FlatMap(time =>
                                Option.Some(time.ToString(FullDateFormatString, CultureInfo.InvariantCulture)))
                        }
                    }).FirstOrThrow(new InvalidOperationException("No blogs found.")),
                options => options.ExcludingMissingMembers());
        }

        private void Given_the_website_has_a_problem()
        {
            DatabasesFixture.BlogContextDatabaseFixture.BlogContext.Database.EnsureDeleted();
        }

        private void Then_it_should_display_an_error()
        {
            SiteTester.Driver.Title.Should().BeEquivalentTo("Error - AlphaDev");
        }

        private void Then_an_error_should_be_logged()
        {
            // wait for error to be logged
            Thread.Sleep(500);
            Log.Should().Contain("[Error] An unhandled exception has occurred");
        }

        private void Then_it_should_display_blog_post_with_markdown_parsed_to_html()
        {
            SiteTester.HomePage.LatestBlog.ValueOrFailure().Should().BeEquivalentTo(
                DatabasesFixture.BlogContextDatabaseFixture.BlogContext.Blogs.OrderByDescending(blog => blog.Created)
                    .ToList().Select(blog => new
                    {
                        Content = Markdown.ToHtml(blog.Content).Trim()
                    })
{'fraction_non_alphanumeric': 0.0850320326150262, 'fraction_numerical': 0.0019801980198019802, 'mean_word_length': 2.1870824053452114, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3754897', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2161, 'n_words': 312}