c700b28e6a
One query now finds anything in the system over the wire and from the CLI. - protocol: add Search/SearchResults message types and a SearchRequest (query, optional kind filter, optional limit) / SearchResponse (typed, ranked matches plus truncation and scan-bound counters) pair, with typed payloads for pipeline, version, run, step/visit, field text and artifact. - store: add Store.Search, a read-only query over pipelines, pipeline_versions, pipeline_runs, run_steps (+step_control) and step_artifacts. Ranks exact name (3) > name substring (2) > body text (1), breaks ties by recency then a stable kind/target order, and reports the matched field. Bounds work with a 50-default/200-max result cap, a 2000-row per-kind scan limit and a 2000-step / 1 MiB field-text scan budget. - terms: a bare status word or lone date expression (today, yesterday, this week, YYYY-MM-DD) selects runs; anything else stays free text. - server: dispatch protocol.Search to stores[0].Search; read-only and unconfirmed. - cli: add `pipelines search QUERY [--type KIND] [--limit N] [--json]`, help text and a BROWSE line. - store: add OpenReadOnly so the live-database check never runs DDL. - docs: document the grammar, kinds, ranking and bounds in docs/cli.md, scope the TUI omnisearch to pipelines in docs/ui-ux-review.md, and add a search bullet to the packaged operations reference. - tests: protocol round trip, store/ranking/term-parsing/limit/scan-bound unit tests, a server end-to-end test, and an env-guarded live check. No new dependencies; go.mod/go.sum unchanged.
120 lines
3.5 KiB
Go
120 lines
3.5 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// TestLiveSearchRealDatabase is an env-guarded read-only check against a real
|
|
// database. It opens the file read-only (no DDL, no journal change, no writes),
|
|
// derives queries from the data itself, and reports one result of each kind it
|
|
// can observe. Run it with:
|
|
//
|
|
// PIPELINES_LIVE_SEARCH_DB=/path/to/pipelines.db go test ./internal/pipeline -run LiveSearch -v
|
|
func TestLiveSearchRealDatabase(t *testing.T) {
|
|
path := os.Getenv("PIPELINES_LIVE_SEARCH_DB")
|
|
if path == "" {
|
|
t.Skip("set PIPELINES_LIVE_SEARCH_DB to a real pipelines.db")
|
|
}
|
|
store, err := OpenReadOnly(path)
|
|
if err != nil {
|
|
t.Fatalf("open read-only: %v", err)
|
|
}
|
|
defer store.Close()
|
|
requireSearchSchema(t, store)
|
|
ctx := context.Background()
|
|
pipelines, err := store.List(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(pipelines) == 0 {
|
|
t.Skip("database has no pipelines")
|
|
}
|
|
now := time.Now()
|
|
seen := map[string]protocol.SearchResult{}
|
|
record := func(query string) {
|
|
response, err := store.Search(ctx, protocol.SearchRequest{Query: query}, now)
|
|
if err != nil {
|
|
t.Fatalf("search %q: %v", query, err)
|
|
}
|
|
for _, hit := range response.Results {
|
|
if _, ok := seen[hit.Kind]; !ok {
|
|
seen[hit.Kind] = hit
|
|
}
|
|
}
|
|
}
|
|
queries := []string{}
|
|
for _, p := range pipelines {
|
|
queries = append(queries, p.Name)
|
|
}
|
|
// Derive stage/field/artifact queries from a bounded number of recent runs.
|
|
runs, err := store.ListRuns(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < len(runs) && i < 5; i++ {
|
|
detail, err := store.GetRun(ctx, runs[i].ID)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for j := 0; j < len(detail.Steps) && j < 3; j++ {
|
|
step := detail.Steps[j]
|
|
if step.Stage != "" {
|
|
queries = append(queries, step.Stage)
|
|
}
|
|
for _, artifact := range step.Artifacts {
|
|
queries = append(queries, artifact.Name)
|
|
}
|
|
for _, key := range []string{"summary", "result", "report", "message", "evidence", "reason"} {
|
|
if text, ok := step.Output[key].(string); ok {
|
|
for _, word := range strings.Fields(text) {
|
|
if len([]rune(word)) >= 6 {
|
|
queries = append(queries, word)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for _, query := range queries {
|
|
record(query)
|
|
}
|
|
if len(seen) == 0 {
|
|
t.Fatal("live database returned no matches for any derived query")
|
|
}
|
|
for _, kind := range protocol.SearchKinds {
|
|
if hit, ok := seen[kind]; ok {
|
|
t.Logf("kind=%s target=%s score=%d matched=%s", kind, hit.Target, hit.Score, hit.MatchedField)
|
|
} else {
|
|
t.Logf("kind=%s not observed in this database", kind)
|
|
}
|
|
}
|
|
if _, ok := seen[protocol.SearchKindRun]; !ok {
|
|
t.Fatal("live database returned no run matches")
|
|
}
|
|
}
|
|
|
|
// requireSearchSchema skips (rather than fails) when the supplied database
|
|
// predates the tables search reads, so pointing the check at a stale local file
|
|
// reports the mismatch instead of a confusing "no such table" error.
|
|
func requireSearchSchema(t *testing.T, store *Store) {
|
|
t.Helper()
|
|
for _, table := range []string{"pipelines", "pipeline_versions", "pipeline_runs", "run_steps", "step_control", "step_artifacts"} {
|
|
var name string
|
|
err := store.db.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name=?", table).Scan(&name)
|
|
if err == sql.ErrNoRows {
|
|
t.Skipf("database lacks the current search schema (missing %q); point PIPELINES_LIVE_SEARCH_DB at a current database", table)
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|