package api
import (
"slices"
"testing"
)
// A file named by the words comes before one that only holds them, and
// that before one whose folders do; shorter paths first within each.
// Every word must match, case aside, and the count is all the matches.
func TestFindPathsRanksTheFileNameFirst(t *testing.T) {
names := []string{
"internal/linecount/counts.go",
"internal/ingest/linecounts_test.go",
"internal/zitcli/linecount.go",
"internal/web/cardstat.go",
"docs/LineCount.md",
"internal/index/oldlinecount.go",
}
got, n := findPaths(names, "linecount", 10)
want := []string{
"docs/LineCount.md",
"internal/zitcli/linecount.go",
"internal/ingest/linecounts_test.go",
"internal/index/oldlinecount.go",
"internal/linecount/counts.go",
}
if !slices.Equal(got, want) || n != 5 {
t.Errorf("FAIL: got %v (%d), want %v (5)", got, n, want)
}
if got, n := findPaths(names, "linecount test", 10); len(got) != 1 || got[0] != "internal/ingest/linecounts_test.go" || n != 1 {
t.Errorf("FAIL: two words found %v (%d)", got, n)
}
if got, n := findPaths(names, "linecount", 2); len(got) != 2 || n != 5 {
t.Errorf("FAIL: a limit of 2 returned %d of %d", len(got), n)
}
if got, n := findPaths(names, " ", 10); len(got) != 0 || n != 0 {
t.Errorf("FAIL: no words found %v", got)
}
}