package store import ( "context" "errors" "fmt" "reflect" "strings" "testing" "time" ) func citySearchAllowAll(string) bool { return true } func citySearchInsertZone(t *testing.T, st *Store, id, domain, kind, parent, name, title, description, path string) { t.Helper() cityTestExec(t, st, `INSERT INTO zones(uuid,domain_uuid,kind,parent_uuid,name,title,description,path_prefix,state,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,?,?,?,'active',1000,2000)`, id, domain, kind, parent, name, title, description, path) } func citySearchInsertSpot(t *testing.T, st *Store, id, domain, street, name, title, description, path string) { t.Helper() cityTestExec(t, st, `INSERT INTO spots(uuid,domain_uuid,name,title,summary,path_prefix,state,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,?,'active',1000,2000)`, id, domain, name, title, description, path) cityTestExec(t, st, `INSERT INTO street_spots(street_uuid,spot_uuid,created_at_ms) VALUES(?,?,1000)`, street, id) } func citySearchIDs(results []CitySearchResult) []string { ids := make([]string, len(results)) for i, result := range results { ids[i] = result.UUID } return ids } func TestCitySearchMetadataRankingAndMatchSources(t *testing.T) { st := cityReadFixture(t) // No Page table remains: this search must not expand a CityMap or bodies. cityTestExec(t, st, `DROP TABLE paths`) citySearchInsertZone(t, st, "q-description", "d", "quartier", "", "description", "Description", "needle in charter", "/a-description") citySearchInsertZone(t, st, "q-path", "d", "quartier", "", "path", "Path", "", "/b-needle") citySearchInsertZone(t, st, "q-title", "d", "quartier", "", "title", "NeEdLe quartier", "", "/c-title") citySearchInsertZone(t, st, "street", "d", "street", "q-title", "needle-street", "Street", "", "/c-title/street") citySearchInsertSpot(t, st, "spot", "d", "street", "spot", "Spot", strings.Repeat("前", 500)+"needle 日本語", "/c-title/street/spot") results, err := st.SearchCityInDomain(context.Background(), "d", "/", "NEEDLE", 30, citySearchAllowAll, SearchOptions{}) if err != nil { t.Fatal(err) } if got, want := citySearchIDs(results), []string{"q-title", "q-path", "q-description", "street", "spot"}; !reflect.DeepEqual(got, want) { t.Fatalf("ranked hits = %v, want %v", got, want) } if !results[0].NameMatch || results[0].PathMatch || results[0].DescriptionMatch || !results[1].PathMatch || !results[2].DescriptionMatch { t.Fatalf("match sources = %#v", results) } spot := results[4] if spot.Kind != "spot" || spot.QuartierTitle != "NeEdLe quartier" || spot.StreetTitle != "Street" || !strings.Contains(spot.Description, "needle 日本語") || len([]rune(spot.Description)) > 512 { t.Fatalf("spot metadata/excerpt = %#v", spot) } if spot.CreatedAtMS != 1000 || spot.UpdatedAtMS != 2000 { t.Fatalf("timestamps = %#v", spot) } } func TestCitySearchScopeUsesLiteralBoundaries(t *testing.T) { st := cityReadFixture(t) citySearchInsertZone(t, st, "literal", "d", "quartier", "", "literal", "日本語 %_", "a'b\\c", "/Life_%/日本語") citySearchInsertZone(t, st, "child", "d", "street", "literal", "child", "日本語 %_", "", "/Life_%/日本語/child") citySearchInsertZone(t, st, "sibling", "d", "quartier", "", "sibling", "日本語", "", "/Life_%/日本語-other") citySearchInsertZone(t, st, "lower", "d", "quartier", "", "lower", "日本語 %_", "", "/life_%/日本語") citySearchInsertZone(t, st, "wildcard", "d", "quartier", "", "wildcard", "日本語", "", "/Life-xyz/日本語") for _, tt := range []struct { scope, query string ids []string }{ {"/Life_%/日本語", "日本語", []string{"literal", "child"}}, {"/Life_%/日本語/child", "日本語", []string{"child"}}, {"/Life_%/日本語/child/deeper", "日本語", []string{}}, {"/Life_%/日本語", "%_", []string{"literal", "child"}}, {"/Life_%/日本語", "a'b\\c", []string{"literal"}}, {"/", "' OR 1=1 --", []string{}}, } { t.Run(tt.scope+"_"+tt.query, func(t *testing.T) { results, err := st.SearchCityInDomain(context.Background(), "d", tt.scope, tt.query, 30, citySearchAllowAll, SearchOptions{}) if err != nil || !reflect.DeepEqual(citySearchIDs(results), tt.ids) { t.Fatalf("hits = %v, %v; want %v", citySearchIDs(results), err, tt.ids) } }) } } func TestCitySearchRequiresReadableActiveSameDomainAncestors(t *testing.T) { st := cityReadFixture(t) citySearchInsertZone(t, st, "q-one", "d", "quartier", "", "one", "needle One", "", "/one") citySearchInsertZone(t, st, "q-two", "d", "quartier", "", "two", "needle Two", "", "/two") // Street names are only unique within their Quartier. Keep both hits and // their distinct UUID/context instead of deduplicating by name. citySearchInsertZone(t, st, "st-one", "d", "street", "q-one", "same-name", "needle Street", "", "/one/street") citySearchInsertZone(t, st, "st-two", "d", "street", "q-two", "same-name", "needle Street", "", "/two/street") citySearchInsertSpot(t, st, "spot-one", "d", "st-one", "one-spot", "needle Spot", "", "/one/street/spot") citySearchInsertSpot(t, st, "spot-two", "d", "st-two", "two-spot", "needle Spot", "", "/two/street/spot") citySearchInsertZone(t, st, "foreign-q", "foreign", "quartier", "", "foreign", "needle Foreign", "", "/foreign") citySearchInsertZone(t, st, "cross-street", "d", "street", "foreign-q", "cross", "needle Cross", "", "/foreign/street") citySearchInsertSpot(t, st, "cross-spot", "d", "cross-street", "cross-spot", "needle Cross", "", "/foreign/street/spot") citySearchInsertSpot(t, st, "foreign-spot", "foreign", "st-one", "foreign-spot", "needle Foreign", "", "/one/street/foreign") citySearchInsertZone(t, st, "archived-q", "d", "quartier", "", "archived", "needle Archived", "", "/archived") citySearchInsertZone(t, st, "archived-child", "d", "street", "archived-q", "archived-child", "needle Child", "", "/archived/child") citySearchInsertSpot(t, st, "archived-spot", "d", "archived-child", "archived-spot", "needle Child", "", "/archived/child/spot") cityTestExec(t, st, `UPDATE zones SET state='archived' WHERE uuid='archived-q'`) for i, path := range []string{"", "relative", strings.Repeat("/", 4097)} { id := fmt.Sprintf("invalid-%d", i) citySearchInsertZone(t, st, id, "d", "quartier", "", id, "needle Invalid", "", path) citySearchInsertZone(t, st, id+"-child", "d", "street", id, id+"-child", "needle Invalid", "", "/invalid-child/"+id) } search := func(allowed func(string) bool, limit int) []CitySearchResult { t.Helper() results, err := st.SearchCityInDomain(context.Background(), "d", "/", "needle", limit, allowed, SearchOptions{}) if err != nil { t.Fatal(err) } return results } all := search(citySearchAllowAll, 30) if got, want := citySearchIDs(all), []string{"q-one", "q-two", "st-one", "st-two", "spot-one", "spot-two"}; !reflect.DeepEqual(got, want) { t.Fatalf("active same-domain metadata = %v, want %v", got, want) } if all[2].QuartierTitle != "needle One" || all[3].QuartierTitle != "needle Two" { t.Fatalf("same-name context lost: %#v", all) } // The child alone being readable must not disclose an unreadable ancestor. visible := search(func(path string) bool { return path != "/one" && path != "/two/street" }, 30) if got, want := citySearchIDs(visible), []string{"q-two"}; !reflect.DeepEqual(got, want) { t.Fatalf("parent ACL filter = %v, want %v", got, want) } visible = search(func(path string) bool { return strings.HasPrefix(path, "/two") }, 1) if got, want := citySearchIDs(visible), []string{"q-two"}; !reflect.DeepEqual(got, want) { t.Fatalf("ACL must precede visible limit: %v, want %v", got, want) } cityTestExec(t, st, `UPDATE zones SET title='' WHERE uuid IN ('q-one','st-one'); UPDATE zones SET state='archived' WHERE uuid='st-two'; UPDATE spots SET state='archived' WHERE uuid='spot-one'`) results, err := st.SearchCityInDomain(context.Background(), "d", "/", "", 30, citySearchAllowAll, SearchOptions{FromMS: new(int64)}) if err != nil || !reflect.DeepEqual(citySearchIDs(results), []string{"q-one", "q-two", "st-one"}) || results[2].QuartierTitle != "one" { t.Fatalf("own/parent archived and title fallback: %#v, %v", results, err) } } func TestCitySearchDateFiltersAndFileExclusion(t *testing.T) { st := cityReadFixture(t) citySearchInsertZone(t, st, "quarter", "d", "quartier", "", "quarter", "Quarter", "", "/quarter") from, before := int64(1000), int64(2000) for _, tt := range []struct { name string query string options SearchOptions want int }{ {"no query or filters", "", SearchOptions{}, 0}, {"date field alone", "", SearchOptions{DateField: "created"}, 0}, {"created inclusive from", "", SearchOptions{DateField: "created", FromMS: &from, BeforeMS: &before}, 1}, {"updated exclusive before", "", SearchOptions{FromMS: &from, BeforeMS: &before}, 0}, {"updated inclusive from", "", SearchOptions{FromMS: &before}, 1}, {"keyword and date", "quarter", SearchOptions{BeforeMS: &before}, 0}, {"file type", "quarter", SearchOptions{FileType: "pdf"}, 0}, {"extension", "quarter", SearchOptions{Extension: "py"}, 0}, } { t.Run(tt.name, func(t *testing.T) { results, err := st.SearchCityInDomain(context.Background(), "d", "/", tt.query, 30, citySearchAllowAll, tt.options) if err != nil || len(results) != tt.want { t.Fatalf("hits = %v, %v; want %d", citySearchIDs(results), err, tt.want) } }) } for _, options := range []SearchOptions{ {DateField: "updated_at_ms); DROP TABLE zones;--"}, {FromMS: &before, BeforeMS: &from}, {FromMS: &from, BeforeMS: &from}, } { if _, err := st.SearchCityInDomain(context.Background(), "d", "/", "quarter", 30, citySearchAllowAll, options); !errors.Is(err, ErrInvalidSearchOptions) { t.Fatalf("invalid options = %#v: %v", options, err) } } } func TestCitySearchBoundedCandidatesAndFiltersBeforeCap(t *testing.T) { st := cityReadFixture(t) cityTestExec(t, st, `WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM n WHERE i<=?) INSERT INTO zones(uuid,domain_uuid,kind,parent_uuid,name,title,path_prefix,state,created_at_ms,updated_at_ms) SELECT printf('q-%04d',i),'d','quartier','',printf('q-%04d',i),'needle',printf('/q-%04d',i),'active',1000,1000 FROM n`, CitySearchCandidateLimit) allowedCalls := 0 results, err := st.SearchCityInDomain(context.Background(), "d", "/", "needle", 30, func(path string) bool { allowedCalls++ return path == fmt.Sprintf("/q-%04d", CitySearchCandidateLimit+1) }, SearchOptions{}) if err != nil || len(results) != 0 || allowedCalls != CitySearchCandidateLimit { t.Fatalf("candidate budget: got %v, calls=%d, err=%v", citySearchIDs(results), allowedCalls, err) } cityTestExec(t, st, `UPDATE zones SET updated_at_ms=3000 WHERE uuid=?`, fmt.Sprintf("q-%04d", CitySearchCandidateLimit+1)) from := int64(2000) results, err = st.SearchCityInDomain(context.Background(), "d", "/", "", 30, citySearchAllowAll, SearchOptions{FromMS: &from}) if err != nil || len(results) != 1 { t.Fatalf("date filter must run before cap: %v, %v", citySearchIDs(results), err) } results, err = st.SearchCityInDomain(context.Background(), "d", "/", "needle", 10000, citySearchAllowAll, SearchOptions{}) if err != nil || len(results) != CitySearchResultLimit { t.Fatalf("visible budget: got %d, %v", len(results), err) } } func TestCitySearchCancellationFailureAndConnectionRelease(t *testing.T) { st := cityReadFixture(t) citySearchInsertZone(t, st, "quarter", "d", "quartier", "", "quarter", "needle", "", "/quarter") st.DB.SetMaxOpenConns(1) ctx, cancel := context.WithCancel(context.Background()) cancel() if _, err := st.SearchCityInDomain(ctx, "d", "/", "needle", 30, citySearchAllowAll, SearchOptions{}); !errors.Is(err, context.Canceled) { t.Fatalf("canceled search = %v", err) } ctx, cancel = context.WithTimeout(context.Background(), time.Second) defer cancel() results, err := st.SearchCityInDomain(ctx, "d", "/", "needle", 30, func(string) bool { // Authorization is outside the search transaction, even with pool=1. var value int if err := st.DB.QueryRowContext(ctx, `SELECT 1`).Scan(&value); err != nil { t.Errorf("authorization cannot use released read connection: %v", err) return false } return value == 1 }, SearchOptions{}) if err != nil || len(results) != 1 { t.Fatalf("single-connection search = %v, %v", results, err) } assertCityReadConnectionReturned(t, st) cancelContext, stop := context.WithCancel(context.Background()) defer stop() results, err = st.SearchCityInDomain(cancelContext, "d", "/", "needle", 1, func(string) bool { stop(); return true }, SearchOptions{}) if !errors.Is(err, context.Canceled) || len(results) != 0 { t.Fatalf("cancellation during authorization returned partial success: %#v, %v", results, err) } cityTestExec(t, st, `DROP TABLE spots`) results, err = st.SearchCityInDomain(context.Background(), "d", "/", "needle", 30, citySearchAllowAll, SearchOptions{}) if err == nil || len(results) != 0 { t.Fatalf("failed query must not return partial success: %v, %v", results, err) } assertCityReadConnectionReturned(t, st) if _, err := st.SearchCityInDomain(context.Background(), "d", "/", strings.Repeat("a", 513), 30, citySearchAllowAll, SearchOptions{}); err == nil { t.Fatal("oversized query accepted") } if _, err := st.SearchCityInDomain(context.Background(), "d", "/", strings.Repeat("word ", 17), 30, citySearchAllowAll, SearchOptions{}); err == nil { t.Fatal("too many query terms accepted") } }