package web
import (
"strings"
"testing"
"craftdesign.group/quarry/internal/index"
)
// The set of searchable kinds is written down in four places: the
// index's SearchKinds, the ops validator's `oneof`, this package's
// scope chips, and hitOf's switch. Adding "doc" to the index left the
// validator behind, so the chip existed, the rows existed, the FTS
// matched — and every doc search returned nothing, because the op
// refused the kind before it reached the query.
//
// hitOf is covered by TestEverySearchKindLinksSomewhere; this covers
// the chips, which are the other half of "a reader can actually reach
// this kind".
func TestScopeChipsCoverEverySearchKind(t *testing.T) {
have := map[string]bool{}
for _, s := range searchScopes {
have[s.Kind] = true
}
if !have[""] {
t.Error(`searchScopes lost the "everything" chip`)
}
for _, kind := range index.SearchKinds {
if !have[kind] {
t.Errorf("index searches %q but no scope chip offers it — "+
"the kind is unreachable from the UI", kind)
}
}
for kind := range have {
if kind == "" {
continue
}
if !strings.Contains(strings.Join(index.SearchKinds, ","), kind) {
t.Errorf("scope chip %q is not a kind the index files — "+
"it can only ever return nothing", kind)
}
}
}
// A search page reached with no query is not blank: it says what the
// search covers and how to use it, on the fleet page and in a repository.
func TestSearchPagesHaveAnEmptyState(t *testing.T) {
srv := landingServer(t, landingIndex(t, false), "")
for _, path := range []string{"/search", "/r/alpha/search"} {
html := landingGet(t, srv, path)
if !strings.Contains(html, `class="fv-empty search-empty"`) {
t.Errorf("FAIL: %s with no query has no empty state", path)
}
if !strings.Contains(html, "check-ins") || !strings.Contains(html, "tickets") {
t.Errorf("FAIL: %s empty state does not say what search covers", path)
}
}
if html := landingGet(t, srv, "/search?q=alpha"); strings.Contains(html, "search-empty") {
t.Error("FAIL: the empty state is drawn alongside results")
}
t.Log("ok: both search pages explain themselves when there is no query")
}