Skip to content
quarry Artifact [455cb1c3f2]
Login

Artifact [455cb1c3f2]

Artifact 455cb1c3f272f7cf1ac604fd8f39beb9280781d57d002be9ced850dfee4526c2:


package api

import (
	"reflect"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/index"
)

// The ops validator repeats the index's kind list as a literal `oneof`
// tag, which a struct tag cannot compute. Adding "doc" to SearchKinds
// left both tags behind and the op refused the kind before the query
// ran: rows indexed, chip rendered, every search empty.
//
// Checked by reflection so the tag and the list cannot drift again.
func TestSearchOpValidatorsMatchSearchKinds(t *testing.T) {
	want := strings.Join(index.SearchKinds, "|")
	for _, in := range []any{searchIn{}, searchAllIn{}} {
		typ := reflect.TypeOf(in)
		f, ok := typ.FieldByName("Kind")
		if !ok {
			t.Fatalf("%s has no Kind field", typ.Name())
		}
		tag := f.Tag.Get("validate")
		got := strings.TrimPrefix(tag, "oneof=")
		if got == tag {
			t.Fatalf("%s.Kind validate tag = %q, want a oneof", typ.Name(), tag)
		}
		if got != want {
			t.Errorf("%s.Kind accepts %q but the index files %q — "+
				"a kind missing here is refused before the query runs",
				typ.Name(), got, want)
		}
	}
}