<commit_before>import XCTest
import Vapor
import URI
import Fluent
import HTTP
import Foundation
@testable import SteamPress

class LeafViewFactoryTests: XCTestCase {
    
    // MARK: - allTests
    
    static var allTests = [
        ("testParametersAreSetCorrectlyOnAllTagsPage", testParametersAreSetCorrectlyOnAllTagsPage),
        ("testTagsPageGetsPassedAllTagsWithBlogCount", testTagsPageGetsPassedAllTagsWithBlogCount),
        ("testTagsPageGetsPassedTagsSortedByPageCount", testTagsPageGetsPassedTagsSortedByPageCount),
        ("testTwitterHandleSetOnAllTagsPageIfGiven", testTwitterHandleSetOnAllTagsPageIfGiven),
        ("testLoggedInUserSetOnAllTagsPageIfPassedIn", testLoggedInUserSetOnAllTagsPageIfPassedIn),
        ("testNoTagsGivenIfEmptyArrayPassedToAllTagsPage", testNoTagsGivenIfEmptyArrayPassedToAllTagsPage),
        ("testParametersAreSetCorrectlyOnAllAuthorsPage", testParametersAreSetCorrectlyOnAllAuthorsPage),
        ("testAuthorsPageGetsPassedAllAuthorsWithBlogCount", testAuthorsPageGetsPassedAllAuthorsWithBlogCount),
        ("testAuthorsPageGetsPassedAuthorsSortedByPageCount", testAuthorsPageGetsPassedAuthorsSortedByPageCount),
        ("testTwitterHandleSetOnAllAuthorsPageIfProvided", testTwitterHandleSetOnAllAuthorsPageIfProvided),
        ("testNoLoggedInUserPassedToAllAuthorsPageIfNoneProvided", testNoLoggedInUserPassedToAllAuthorsPageIfNoneProvided),
        ("testNoAuthorsGivenToAuthorsPageIfNonePassedToAllAuthorsPage", testNoAuthorsGivenToAuthorsPageIfNonePassedToAllAuthorsPage),
        ("testTagPageGetsTagWithCorrectParamsAndPostCount", testTagPageGetsTagWithCorrectParamsAndPostCount),
        ("testNoLoggedInUserPassedToTagPageIfNoneProvided", testNoLoggedInUserPassedToTagPageIfNoneProvided),
        ("testDisqusNamePassedToTagPageIfSet", testDisqusNamePassedToTagPageIfSet),
        ("testTwitterHandlePassedToTagPageIfSet", testTwitterHandlePassedToTagPageIfSet),
        ("testBlogPageGetsImageUrlIfOneInPostMarkdown", testBlogPageGetsImageUrlIfOneInPostMarkdown),
        ("testDescriptionOnBlogPostPageIsShortSnippetTextCleaned", testDescriptionOnBlogPostPageIsShortSnippetTextCleaned),
        ("testBlogPostPageGetsCorrectParameters", testBlogPostPageGetsCorrectParameters),
        ("testUserPassedToBlogPostPageIfUserPassedIn", testUserPassedToBlogPostPageIfUserPassedIn),
        ("testDisqusNamePassedToBlogPostPageIfPassedIn", testDisqusNamePassedToBlogPostPageIfPassedIn),
        ("testTwitterHandlePassedToBlogPostPageIfPassedIn", testTwitterHandlePassedToBlogPostPageIfPassedIn),
//        ("testBlogIndexPageGivenCorrectParameters", testBlogIndexPageGivenCorrectParameters),
//        ("testNoPostsPassedIntoBlogIndexIfNoneAvailable", testNoPostsPassedIntoBlogIndexIfNoneAvailable),
//        ("testNoAuthorsPassedIntoBlogIndexIfNoneCreated", testNoAuthorsPassedIntoBlogIndexIfNoneCreated),
//        ("testNoTagsPassedIntoBlogIndexIfNoneCreted", testNoTagsPassedIntoBlogIndexIfNoneCreted),
//        ("testUserPassedToBlogIndexIfUserPassedIn", testUserPassedToBlogIndexIfUserPassedIn),
//        ("testDisqusNamePassedToBlogIndexIfPassedIn", testDisqusNamePassedToBlogIndexIfPassedIn),
//        ("testTwitterHandlePassedToBlogIndexIfPassedIn", testTwitterHandlePassedToBlogIndexIfPassedIn),
        ("testAuthorViewHasCorrectParametersSet", testAuthorViewHasCorrectParametersSet),
        ("testAuthorViewMyProfileSetIfViewingMyProfile", testAuthorViewMyProfileSetIfViewingMyProfile),
        ("testAuthorViewHasNoPostsSetIfNoneCreated", testAuthorViewHasNoPostsSetIfNoneCreated),
        ("testAuthorViewGetsLoggedInUserIfProvider", testAuthorViewGetsLoggedInUserIfProvider),
        ("testAuthorViewGetsDisqusNameIfProvided", testAuthorViewGetsDisqusNameIfProvided),
        ("testAuthorViewGetsTwitterHandleIfProvided", testAuthorViewGetsTwitterHandleIfProvided),
        ("testPasswordViewGivenCorrectParameters", testPasswordViewGivenCorrectParameters),
        ("testPasswordViewHasCorrectParametersWhenError", testPasswordViewHasCorrectParametersWhenError),
        ("testLoginViewGetsCorrectParameters", testLoginViewGetsCorrectParameters),
        ("testLoginViewWhenErrored", testLoginViewWhenErrored),
        ("testLoginPageUsernamePasswordErrorsMarkedWhenNotSuppliedAndErrored", testLoginPageUsernamePasswordErrorsMarkedWhenNotSuppliedAndErrored),
        ("testBlogAdminViewGetsCorrectParameters", testBlogAdminViewGetsCorrectParameters),
        ("testNoPostsPassedToAdminViewIfNone", testNoPostsPassedToAdminViewIfNone),
        ("testAdminPageWithErrors", testAdminPageWithErrors),
        ("testCreateUserViewGetsCorrectParameters", testCreateUserViewGetsCorrectParameters),
        ("testCreateUserViewWhenErrors", testCreateUserViewWhenErrors),
        ("testCreateUserViewWhenNoNameOrUsernameSupplied", testCreateUserViewWhenNoNameOrUsernameSupplied),
        ("testCreateUserViewForEditing", testCreateUserViewForEditing),
        ("testCreateUserViewThrowsWhenTryingToEditWithoutUserId", testCreateUserViewThrowsWhenTryingToEditWithoutUserId),
        ("testCreateBlogPostViewGetsCorrectParameters", testCreateBlogPostViewGetsCorrectParameters),
        ("testCreateBlogPostViewWhenEditing", testCreateBlogPostViewWhenEditing),
        ("testEditBlogPostViewThrowsWithNoPostToEdit", testEditBlogPostViewThrowsWithNoPostToEdit),
        ("testCreateBlogPostViewWithErrorsAndNoTitleOrContentsSupplied", testCreateBlogPostViewWithErrorsAndNoTitleOrContentsSupplied),
        ("testDraftPassedThroughWhenEditingABlogPostThatHasNotBeenPublished", testDraftPassedThroughWhenEditingABlogPostThatHasNotBeenPublished),
        ("testAuthorViewGetsPostCount", testAuthorViewGetsPostCount),
        ("testAuthorViewGetsLongSnippetForPosts", testAuthorViewGetsLongSnippetForPosts),
        ]
    
    // MARK: - Properties
    private var viewFactory: LeafViewFactory!
    private var viewRenderer: CapturingViewRenderer!
//    private let database = Database(MemoryDriver())
    
    private let tagsURI = URI(scheme: "https", hostname: "test.com", path: "tags/")
    private let authorsURI = URI(scheme: "https", hostname: "test.com", path: "authors/")
    private var authorRequest: Request!
    private let tagURI = URI(scheme: "https", hostname: "test.com", path: "tags/tatooine/")
    private var tagRequest: Request!
    private let postURI = URI(scheme: "https", hostname: "test.com", path: "posts/test-post/")
    private let indexURI = URI(scheme: "https", hostname: "test.com", path: "/")
    private var indexRequest: Request!
    private let authorURI = URI(scheme: "https", hostname: "test.com", path: "authors/luke/")
    private let createPostURI = URI(scheme: "https", hostname: "test.com", path: "admin/createPost/")
    private let editPostURI = URI(scheme: "https", hostname: "test.com", path: "admin/posts/1/edit/")
    
    // MARK: - Overrides
    
    override func setUp() {
        viewRenderer = CapturingViewRenderer()
        viewFactory = LeafViewFactory(viewRenderer: viewRenderer)
        tagRequest = Request(method: .get, uri: tagURI)
        authorRequest = Request(method: .get, uri: authorURI)
        indexRequest = Request(method: .get, uri: indexURI)
        
        do {
            let database = try Database(MemoryDriver())
            BlogUser.database = database
            BlogPost.database = database
            BlogTag.database = database
            Pivot<BlogPost, BlogTag>.database = database
            try BlogUser.prepare(database)
            try BlogPost.prepare(database)
            try BlogTag.prepare(database)
            try BlogPostDraft.prepare(database)
            try BlogUserExtraInformation.prepare(database)
            Pivot<BlogPost, BlogTag>.database = database
        }
        catch {
            XCTFail()
        }
    }
    
    // MARK: - Tests
    
    func testParametersAreSetCorrectlyOnAllTagsPage() throws {
        let tags = [BlogTag(name: "tag1"), BlogTag(name: "tag2")]
        for tag in tags {
            try tag.save()
        }
        _ = try viewFactory.allTagsView(uri: tagsURI, allTags: tags, user: nil, siteTwitterHandle: nil)
        
        XCTAssertEqual(viewRenderer.capturedContext?["tags"]?.array?.count, 2)
        XCTAssertEqual((viewRenderer.capturedContext?["tags"]?.array?.first)?["name"], "tag1")
        XCTAssertEqual((viewRenderer.capturedContext?["tags"]?.array?[1])?["name"], "tag2")
        XCTAssertEqual(viewRenderer.capturedContext?["uri"]?.string, "https://test.com:443/tags/")
        XCTAssertNil(viewRenderer.capturedContext?["site_twitter_handle"]?.string)
        XCTAssertNil(viewRenderer.capturedContext?["user"])
        XCTAssertEqual(viewRenderer.leafPath, "blog/tags")
    }
    
    func testTagsPageGetsPassedAllTagsWithBlogCount() throws {
        let tag = BlogTag(name: "test tag")
        try tag.save()
        let post1 = TestDataBuilder.anyPost()<commit_msg>Prepare the pivot correctly<commit_after>import XCTest
import Vapor
import URI
import Fluent
import HTTP
import Foundation
@testable import SteamPress

class LeafViewFactoryTests: XCTestCase {
    
    // MARK: - allTests
    
    static var allTests = [
        ("testParametersAreSetCorrectlyOnAllTagsPage", testParametersAreSetCorrectlyOnAllTagsPage),
        ("testTagsPageGetsPassedAllTagsWithBlogCount", testTagsPageGetsPassedAllTagsWithBlogCount),
        ("testTagsPageGetsPassedTagsSortedByPageCount", testTagsPageGetsPassedTagsSortedByPageCount),
        ("testTwitterHandleSetOnAllTagsPageIfGiven", testTwitterHandleSetOnAllTagsPageIfGiven),
        ("testLoggedInUserSetOnAllTagsPageIfPassedIn", testLoggedInUserSetOnAllTagsPageIfPassedIn),
        ("testNoTagsGivenIfEmptyArrayPassedToAllTagsPage", testNoTagsGivenIfEmptyArrayPassedToAllTagsPage),
        ("testParametersAreSetCorrectlyOnAllAuthorsPage", testParametersAreSetCorrectlyOnAllAuthorsPage),
        ("testAuthorsPageGetsPassedAllAuthorsWithBlogCount", testAuthorsPageGetsPassedAllAuthorsWithBlogCount),
        ("testAuthorsPageGetsPassedAuthorsSortedByPageCount", testAuthorsPageGetsPassedAuthorsSortedByPageCount),
        ("testTwitterHandleSetOnAllAuthorsPageIfProvided", testTwitterHandleSetOnAllAuthorsPageIfProvided),
        ("testNoLoggedInUserPassedToAllAuthorsPageIfNoneProvided", testNoLoggedInUserPassedToAllAuthorsPageIfNoneProvided),
        ("testNoAuthorsGivenToAuthorsPageIfNonePassedToAllAuthorsPage", testNoAuthorsGivenToAuthorsPageIfNonePassedToAllAuthorsPage),
        ("testTagPageGetsTagWithCorrectParamsAndPostCount", testTagPageGetsTagWithCorrectParamsAndPostCount),
        ("testNoLoggedInUserPassedToTagPageIfNoneProvided", testNoLoggedInUserPassedToTagPageIfNoneProvided),
        ("testDisqusNamePassedToTagPageIfSet", testDisqusNamePassedToTagPageIfSet),
        ("testTwitterHandlePassedToTagPageIfSet", testTwitterHandlePassedToTagPageIfSet),
        ("testBlogPageGetsImageUrlIfOneInPostMarkdown", testBlogPageGetsImageUrlIfOneInPostMarkdown),
        ("testDescriptionOnBlogPostPageIsShortSnippetTextCleaned", testDescriptionOnBlogPostPageIsShortSnippetTextCleaned),
        ("testBlogPostPageGetsCorrectParameters", testBlogPostPageGetsCorrectParameters),
        ("testUserPassedToBlogPostPageIfUserPassedIn", testUserPassedToBlogPostPageIfUserPassedIn),
        ("testDisqusNamePassedToBlogPostPageIfPassedIn", testDisqusNamePassedToBlogPostPageIfPassedIn),
        ("testTwitterHandlePassedToBlogPostPageIfPassedIn", testTwitterHandlePassedToBlogPostPageIfPassedIn),
//        ("testBlogIndexPageGivenCorrectParameters", testBlogIndexPageGivenCorrectParameters),
//        ("testNoPostsPassedIntoBlogIndexIfNoneAvailable", testNoPostsPassedIntoBlogIndexIfNoneAvailable),
//        ("testNoAuthorsPassedIntoBlogIndexIfNoneCreated", testNoAuthorsPassedIntoBlogIndexIfNoneCreated),
//        ("testNoTagsPassedIntoBlogIndexIfNoneCreted", testNoTagsPassedIntoBlogIndexIfNoneCreted),
//        ("testUserPassedToBlogIndexIfUserPassedIn", testUserPassedToBlogIndexIfUserPassedIn),
//        ("testDisqusNamePassedToBlogIndexIfPassedIn", testDisqusNamePassedToBlogIndexIfPassedIn),
//        ("testTwitterHandlePassedToBlogIndexIfPassedIn", testTwitterHandlePassedToBlogIndexIfPassedIn),
        ("testAuthorViewHasCorrectParametersSet", testAuthorViewHasCorrectParametersSet),
        ("testAuthorViewMyProfileSetIfViewingMyProfile", testAuthorViewMyProfileSetIfViewingMyProfile),
        ("testAuthorViewHasNoPostsSetIfNoneCreated", testAuthorViewHasNoPostsSetIfNoneCreated),
        ("testAuthorViewGetsLoggedInUserIfProvider", testAuthorViewGetsLoggedInUserIfProvider),
        ("testAuthorViewGetsDisqusNameIfProvided", testAuthorViewGetsDisqusNameIfProvided),
        ("testAuthorViewGetsTwitterHandleIfProvided", testAuthorViewGetsTwitterHandleIfProvided),
        ("testPasswordViewGivenCorrectParameters", testPasswordViewGivenCorrectParameters),
        ("testPasswordViewHasCorrectParametersWhenError", testPasswordViewHasCorrectParametersWhenError),
        ("testLoginViewGetsCorrectParameters", testLoginViewGetsCorrectParameters),
        ("testLoginViewWhenErrored", testLoginViewWhenErrored),
        ("testLoginPageUsernamePasswordErrorsMarkedWhenNotSuppliedAndErrored", testLoginPageUsernamePasswordErrorsMarkedWhenNotSuppliedAndErrored),
        ("testBlogAdminViewGetsCorrectParameters", testBlogAdminViewGetsCorrectParameters),
        ("testNoPostsPassedToAdminViewIfNone", testNoPostsPassedToAdminViewIfNone),
        ("testAdminPageWithErrors", testAdminPageWithErrors),
        ("testCreateUserViewGetsCorrectParameters", testCreateUserViewGetsCorrectParameters),
        ("testCreateUserViewWhenErrors", testCreateUserViewWhenErrors),
        ("testCreateUserViewWhenNoNameOrUsernameSupplied", testCreateUserViewWhenNoNameOrUsernameSupplied),
        ("testCreateUserViewForEditing", testCreateUserViewForEditing),
        ("testCreateUserViewThrowsWhenTryingToEditWithoutUserId", testCreateUserViewThrowsWhenTryingToEditWithoutUserId),
        ("testCreateBlogPostViewGetsCorrectParameters", testCreateBlogPostViewGetsCorrectParameters),
        ("testCreateBlogPostViewWhenEditing", testCreateBlogPostViewWhenEditing),
        ("testEditBlogPostViewThrowsWithNoPostToEdit", testEditBlogPostViewThrowsWithNoPostToEdit),
        ("testCreateBlogPostViewWithErrorsAndNoTitleOrContentsSupplied", testCreateBlogPostViewWithErrorsAndNoTitleOrContentsSupplied),
        ("testDraftPassedThroughWhenEditingABlogPostThatHasNotBeenPublished", testDraftPassedThroughWhenEditingABlogPostThatHasNotBeenPublished),
        ("testAuthorViewGetsPostCount", testAuthorViewGetsPostCount),
        ("testAuthorViewGetsLongSnippetForPosts", testAuthorViewGetsLongSnippetForPosts),
        ]
    
    // MARK: - Properties
    private var viewFactory: LeafViewFactory!
    private var viewRenderer: CapturingViewRenderer!
//    private let database = Database(MemoryDriver())
    
    private let tagsURI = URI(scheme: "https", hostname: "test.com", path: "tags/")
    private let authorsURI = URI(scheme: "https", hostname: "test.com", path: "authors/")
    private var authorRequest: Request!
    private let tagURI = URI(scheme: "https", hostname: "test.com", path: "tags/tatooine/")
    private var tagRequest: Request!
    private let postURI = URI(scheme: "https", hostname: "test.com", path: "posts/test-post/")
    private let indexURI = URI(scheme: "https", hostname: "test.com", path: "/")
    private var indexRequest: Request!
    private let authorURI = URI(scheme: "https", hostname: "test.com", path: "authors/luke/")
    private let createPostURI = URI(scheme: "https", hostname: "test.com", path: "admin/createPost/")
    private let editPostURI = URI(scheme: "https", hostname: "test.com", path: "admin/posts/1/edit/")
    
    // MARK: - Overrides
    
    override func setUp() {
        viewRenderer = CapturingViewRenderer()
        viewFactory = LeafViewFactory(viewRenderer: viewRenderer)
        tagRequest = Request(method: .get, uri: tagURI)
        authorRequest = Request(method: .get, uri: authorURI)
        indexRequest = Request(method: .get, uri: indexURI)
        
        do {
            let database = try Database(MemoryDriver())
            BlogUser.database = database
            BlogPost.database = database
            BlogTag.database = database
            Pivot<BlogPost, BlogTag>.database = database
            try BlogUser.prepare(database)
            try BlogPost.prepare(database)
            try BlogTag.prepare(database)
            try BlogPostDraft.prepare(database)
            try BlogUserExtraInformation.prepare(database)
            try Pivot<BlogPost, BlogTag>.prepare(database)
        }
        catch {
            XCTFail()
        }
    }
    
    // MARK: - Tests
    
    func testParametersAreSetCorrectlyOnAllTagsPage() throws {
        let tags = [BlogTag(name: "tag1"), BlogTag(name: "tag2")]
        for tag in tags {
            try tag.save()
        }
        _ = try viewFactory.allTagsView(uri: tagsURI, allTags: tags, user: nil, siteTwitterHandle: nil)
        
        XCTAssertEqual(viewRenderer.capturedContext?["tags"]?.array?.count, 2)
        XCTAssertEqual((viewRenderer.capturedContext?["tags"]?.array?.first)?["name"], "tag1")
        XCTAssertEqual((viewRenderer.capturedContext?["tags"]?.array?[1])?["name"], "tag2")
        XCTAssertEqual(viewRenderer.capturedContext?["uri"]?.string, "https://test.com:443/tags/")
        XCTAssertNil(viewRenderer.capturedContext?["site_twitter_handle"]?.string)
        XCTAssertNil(viewRenderer.capturedContext?["user"])
        XCTAssertEqual(viewRenderer.leafPath, "blog/tags")
    }
    
    func testTagsPageGetsPassedAllTagsWithBlogCount() throws {
        let tag = BlogTag(name: "test tag")
        try tag.save()
        let post1 = TestDataBuilder.anyPost()
{'fraction_non_alphanumeric': 0.08417565139263253, 'fraction_numerical': 0.0012353998203054807, 'mean_word_length': 5.264157580021105, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1362824', 'n_tokens_mistral': 5194, 'n_tokens_neox': 4902, 'n_words': 672}