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) } } }