Feature: Character Tests
    Background:
        Given I have the environment variables:
            | PAGEFIND_SOURCE | public |
        Given I have a "public/index.html" file with the body:
            """
            <p data-result>Nothing</p>
            """

    Scenario: Pagefind matches special characters
        Given I have a "public/apiary/index.html" file with the body:
            """
            <h1>Béës</h1>
            """
        When I run my program
        Then I should see "Running Pagefind" in stdout
        Then I should see the file "public/_pagefind/pagefind.js"
        When I serve the "public" directory
        When I load "/"
        When I evaluate:
            """
            async function() {
                let pagefind = await import("/_pagefind/pagefind.js");

                let search = await pagefind.search("Béës");

                let pages = await Promise.all(search.results.map(r => r.data()));
                document.querySelector('[data-result]').innerText = pages.map(p => p.url).join(", ");
            }
            """
        Then There should be no logs
        Then The selector "[data-result]" should contain "/apiary/"

    Scenario: Pagefind doesn't match HTML entities as their text
        Given I have a "public/apiary/index.html" file with the body:
            """
            <h1>The &quot;bees&quot;</h1>
            """
        When I run my program
        Then I should see "Running Pagefind" in stdout
        Then I should see the file "public/_pagefind/pagefind.js"
        When I serve the "public" directory
        When I load "/"
        When I evaluate:
            """
            async function() {
                let pagefind = await import("/_pagefind/pagefind.js");

                let search = await pagefind.search("bees");

                let pages = await Promise.all(search.results.map(r => r.data()));
                document.querySelector('[data-result]').innerText = pages.map(p => p.content).join(", ");
            }
            """
        Then There should be no logs
        Then The selector "[data-result]" should contain 'The "bees"'

    Scenario: Pagefind can search for a hyphenated phrase
        Given I have a "public/ds/index.html" file with the body:
            """
            <h1>The beet-root</h1>
            """
        When I run my program
        Then I should see "Running Pagefind" in stdout
        Then I should see the file "public/_pagefind/pagefind.js"
        When I serve the "public" directory
        When I load "/"
        When I evaluate:
            """
            async function() {
                let pagefind = await import("/_pagefind/pagefind.js");

                let search = await pagefind.search("beet-root");

                let pages = await Promise.all(search.results.map(r => r.data()));
                document.querySelector('[data-result]').innerText = pages.map(p => p.url).join(", ");
            }
            """
        Then There should be no logs
        Then The selector "[data-result]" should contain '/ds/'

    Scenario: Pagefind does not return results for queries that normalize to nothing
        Given I have a "public/bundaberg/index.html" file with the body:
            """
            <h1>Invert can before opening</h1>
            """
        When I run my program
        Then I should see "Running Pagefind" in stdout
        Then I should see the file "public/_pagefind/pagefind.js"
        When I serve the "public" directory
        When I load "/"
        When I evaluate:
            """
            async function() {
                let pagefind = await import("/_pagefind/pagefind.js");

                // Preload some pages that Pagefind might then return as "all pages"
                await pagefind.preload("can");
                let search = await pagefind.search("*");

                let pages = await Promise.all(search.results.map(r => r.data()));
                document.querySelector('[data-result]').innerText = `[${pages.length}]`;
            }
            """
        Then There should be no logs
        Then The selector "[data-result]" should contain '[0]'
