Files
pipelines/internal/pipeline/search_recent_test.go
T

153 lines
6.6 KiB
Go

package pipeline
import (
"context"
"reflect"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestSearchKindOnlyCanonicalRows(t *testing.T) {
store, _ := searchFixture(t)
ctx := context.Background()
for _, kind := range protocol.SearchKinds {
t.Run(kind, func(t *testing.T) {
for _, query := range []string{"", " \t\n "} {
response, err := store.Search(ctx, protocol.SearchRequest{Query: query, Kind: " " + kind + " "}, time.Now())
if err != nil {
t.Fatal(err)
}
if len(response.Results) == 0 || response.Kind != kind || response.Query != query {
t.Fatalf("missing kind-only results: %+v", response)
}
for _, hit := range response.Results {
if hit.Kind != kind || hit.Target == "" {
t.Fatalf("invalid canonical hit: %+v", hit)
}
}
}
})
}
}
func TestSearchKindOnlyValidationAndLimits(t *testing.T) {
store, _ := searchFixture(t)
ctx := context.Background()
for _, kind := range []string{"bogus", "PIPELINE", "pipeline' OR 1=1 --"} {
if _, err := store.Search(ctx, protocol.SearchRequest{Kind: kind}, time.Now()); err == nil {
t.Fatalf("blank text bypassed validation for %q", kind)
}
}
for _, query := range []string{"", " \n\t"} {
response, err := store.Search(ctx, protocol.SearchRequest{Query: query}, time.Now())
if err != nil || response.Results == nil || len(response.Results) != 0 || response.Truncated {
t.Fatalf("empty all-kind query must remain a no-op: %+v %v", response, err)
}
}
for _, limit := range []int{-1, 0, 1, protocol.SearchMaxLimit + 1} {
response, err := store.Search(ctx, protocol.SearchRequest{Kind: protocol.SearchKindField, Limit: limit}, time.Now())
if err != nil || response.Cap != searchLimit(limit) || len(response.Results) > response.Cap {
t.Fatalf("kind-only limit %d: %+v %v", limit, response, err)
}
if limit == 1 && (len(response.Results) != 1 || !response.Truncated) {
t.Fatalf("expected bounded/truncated fields: %+v", response)
}
}
}
func TestSearchKindOnlyRecencyThenNumericID(t *testing.T) {
store, run := searchFixture(t)
ctx := context.Background()
for i := 0; i < 11; i++ {
_, err := store.db.Exec(`INSERT INTO pipeline_runs(pipeline_id,version,request_key,workspace,allow_tools,write_paths,input_json,created_at)
SELECT pipeline_id,version,printf('recent-%d',?),workspace,allow_tools,write_paths,input_json,'2020-01-01 00:00:00' FROM pipeline_runs WHERE id=?`, i, run)
if err != nil {
t.Fatal(err)
}
}
// The oldest ID is newest by timestamp, followed by equal-time numeric IDs.
if _, err := store.db.Exec("UPDATE pipeline_runs SET created_at='2030-01-01 00:00:00' WHERE id=?", run); err != nil {
t.Fatal(err)
}
response, err := store.Search(ctx, protocol.SearchRequest{Kind: protocol.SearchKindRun, Limit: 4}, time.Now())
if err != nil {
t.Fatal(err)
}
var ids []int64
for _, hit := range response.Results {
ids = append(ids, hit.Run.ID)
}
if !reflect.DeepEqual(ids, []int64{run, run + 11, run + 10, run + 9}) || !response.Truncated {
t.Fatalf("recency/ID ordering or limit: %+v", response)
}
again, err := store.Search(ctx, protocol.SearchRequest{Kind: protocol.SearchKindRun, Limit: 4}, time.Now())
if err != nil || !reflect.DeepEqual(response, again) {
t.Fatalf("kind-only results changed between reads: %+v %v", again, err)
}
}
func TestSearchRecentCanonicalIndexes(t *testing.T) {
store, _ := searchFixture(t)
for _, tc := range []struct{ query, index string }{
{"SELECT pipeline_id,version FROM pipeline_versions ORDER BY created_at DESC,pipeline_id DESC,version DESC LIMIT 51", "search_versions_recent"},
{"SELECT p.id FROM pipelines p JOIN pipeline_versions v ON v.pipeline_id=p.id WHERE v.version=(SELECT MAX(version) FROM pipeline_versions WHERE pipeline_id=p.id) ORDER BY v.created_at DESC,v.pipeline_id DESC,v.version DESC LIMIT 51", "search_versions_recent"},
{"SELECT id FROM pipeline_runs ORDER BY created_at DESC,id DESC LIMIT 51", "search_runs_recent"},
{"SELECT run_id,sequence FROM run_steps ORDER BY started_at DESC,run_id DESC,sequence DESC LIMIT 51", "search_steps_recent"},
{"SELECT a.run_id,a.sequence FROM pipeline_runs r CROSS JOIN step_artifacts a ON a.run_id=r.id ORDER BY r.created_at DESC,r.id DESC,a.sequence DESC LIMIT 2001", "search_runs_recent"},
} {
rows, err := store.db.Query("EXPLAIN QUERY PLAN " + tc.query)
if err != nil {
t.Fatal(err)
}
var plan string
for rows.Next() {
var id, parent, unused int
var detail string
if err := rows.Scan(&id, &parent, &unused, &detail); err != nil {
t.Fatal(err)
}
plan += detail
}
rows.Close()
if !strings.Contains(plan, tc.index) || strings.Contains(plan, "USE TEMP B-TREE FOR ORDER BY") {
t.Fatalf("recent rows must use ordered canonical index: %s", plan)
}
}
}
func TestSearchKindOnlyMaximumResultCap(t *testing.T) {
store, run := searchFixture(t)
_, err := store.db.Exec(`WITH RECURSIVE n(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM n WHERE x<250)
INSERT INTO pipeline_runs(pipeline_id,version,request_key,workspace,allow_tools,write_paths,input_json)
SELECT r.pipeline_id,r.version,printf('cap-%d',n.x),r.workspace,r.allow_tools,r.write_paths,r.input_json FROM pipeline_runs r CROSS JOIN n WHERE r.id=?`, run)
if err != nil {
t.Fatal(err)
}
for _, limit := range []int{0, 1, 9999} {
response, err := store.Search(context.Background(), protocol.SearchRequest{Kind: protocol.SearchKindRun, Limit: limit}, time.Now())
if err != nil || len(response.Results) != searchLimit(limit) || !response.Truncated || response.ScannedSteps != 0 || response.ScannedBytes != 0 {
t.Fatalf("kind-only cap %d not enforced: %+v %v", limit, response, err)
}
}
}
func TestSearchKindOnlyPipelineIsLatestCanonicalVersion(t *testing.T) {
store, _ := searchFixture(t)
_, err := store.db.Exec(`INSERT INTO pipeline_versions(pipeline_id,version,source,content_hash,graph_json,created_at)
SELECT pipeline_id,version+1,source,content_hash||'-next',graph_json,'2031-01-01 00:00:00' FROM pipeline_versions`)
if err != nil {
t.Fatal(err)
}
response, err := store.Search(context.Background(), protocol.SearchRequest{Kind: protocol.SearchKindPipeline}, time.Now())
if err != nil || len(response.Results) != 1 || response.Results[0].Pipeline.Version != 2 {
t.Fatalf("pipeline browse must return latest canonical version only: %+v %v", response, err)
}
versions, err := store.Search(context.Background(), protocol.SearchRequest{Kind: protocol.SearchKindVersion}, time.Now())
if err != nil || len(versions.Results) != 2 || versions.Results[0].Version.Version != 2 {
t.Fatalf("version browse must retain history in recent order: %+v %v", versions, err)
}
}