// This tests basic search behavior.

fail-on-js-error: true
go-to: |DOC_PATH| + "search/index.html"

define-function: (
    "open-search",
    [],
    block {
        assert-css: ("#mdbook-search-wrapper", {"display": "none"})
        press-key: 's'
        wait-for-css-false: ("#mdbook-search-wrapper", {"display": "none"})
    }
)

call-function: ("open-search", {})
assert-text: ("#mdbook-searchresults-header", "")
write: "extraordinary"
wait-for-text: ("#mdbook-searchresults-header", "2 search results for 'extraordinary':")
// Close the search display
press-key: 'Escape'
wait-for-css: ("#mdbook-search-wrapper", {"display": "none"})
// Reopening the search should show the last value
call-function: ("open-search", {})
assert-text: ("#mdbook-searchresults-header", "2 search results for 'extraordinary':")
// Navigate to a sub-chapter
go-to: "./inner/chapter_2.html"
assert-text: ("#mdbook-searchresults-header", "")
call-function: ("open-search", {})
write: "kaleidoscope"
wait-for-text: ("#mdbook-searchresults-header", "2 search results for 'kaleidoscope':")

// Now we test search shortcuts and more page changes.
go-to: |DOC_PATH| + "search/index.html"

// This check is to ensure that the search bar is inside the search wrapper.
assert: "#mdbook-search-wrapper #mdbook-searchbar"
assert-css: ("#mdbook-search-wrapper", {"display": "none"})

// Now we make sure the search input appear with the `S` shortcut.
press-key: 's'
wait-for-css-false: ("#mdbook-search-wrapper", {"display": "none"})
// We ensure the search bar has the focus.
wait-for: "#mdbook-searchbar:focus"
// Pressing a key will therefore update the search input.
press-key: 't'
assert-text: ("#mdbook-searchbar", "t")

// Now we press `Escape` to ensure that the search input disappears again.
press-key: 'Escape'
wait-for-css: ("#mdbook-search-wrapper", {"display": "none"})

// Making it appear by clicking on the search button.
click: "#mdbook-search-toggle"
wait-for-css: ("#mdbook-search-wrapper", {"display": "block"})
// We ensure the search bar has the focus.
assert: "#mdbook-searchbar:focus"

// We input "thunder".
write: "thunder"
// The results should now appear.
wait-for-text: ("#mdbook-searchresults-header", "1 search result for 'thunder':")
assert: "#mdbook-searchresults"
// Ensure that the URL was updated as well.
assert-document-property: ({"URL": "?search=thunder"}, ENDS_WITH)

// Now we ensure that when we land on the page with a "search in progress", the search results are
// loaded and that the search input has focus.
go-to: |DOC_PATH| + "search/index.html?search=thunder"
wait-for-text: ("#mdbook-searchresults-header", "1 search result for 'thunder':")
assert: "#mdbook-searchbar:focus"
assert: "#mdbook-searchresults"

// And now we press `Escape` to close everything.
press-key: 'Escape'
wait-for-css: ("#mdbook-search-wrapper", {"display": "none"})

// Search result links should have a `?highlight=` query param, which will highlight
// the search term when we navigate to the destination page.
go-to: |DOC_PATH| + "search/index.html?search=kale"
assert: '[href*="highlight=kale"]'

// On navigation, text on the destination page that matches the search term should be highlighted.
go-to: |DOC_PATH| + "search/index.html?highlight=kale"
wait-for: '[data-markjs="true"]'
assert-text: ('[data-markjs="true"]', "kale", ALL)

// Clicking on any of the highlights should dismiss them.
click: '[data-markjs="true"]'
assert-false: '[data-markjs="true"]'
// The `?highlight=` URL query param should also have been removed
// (so that highlights don't unexpectedly appear again.)
assert-window-property-false: ({"location": "highlight=kale"}, CONTAINS)
// Verify this by clicking on a heading to navigate to its anchor. This would otherwise preserve
// search params, and if the query param were not removed, highlights would appear again.
click: 'main h1 a'
assert-false: '[data-markjs="true"]'
