Files
pages/internal/server/search_options_test.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

157 lines
7.3 KiB
Go

package server
import (
"context"
"errors"
"html"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"pageswiki/internal/store"
)
func TestSearchCalendarDateOptions(t *testing.T) {
for _, spec := range []struct {
zone, date string
hours int64
}{
{"Asia/Tokyo", "2026-09-14", 24},
{"America/New_York", "2026-03-08", 23},
{"America/New_York", "2026-11-01", 25},
} {
location, err := time.LoadLocation(spec.zone)
if err != nil {
t.Fatal(err)
}
options, view, err := parseSearchOptions(url.Values{
"from": {spec.date}, "to": {spec.date}, "ext": {" .PDF "}, "date_field": {"created"},
}, location)
if err != nil || options.FromMS == nil || options.BeforeMS == nil {
t.Fatalf("calendar date: %+v %v", options, err)
}
if duration := *options.BeforeMS - *options.FromMS; duration != spec.hours*60*60*1000 {
t.Fatalf("%s %s day duration=%d", spec.zone, spec.date, duration)
}
if view.Extension != "pdf" || view.Timezone != spec.zone || !view.Applied || !view.Open || options.DateField != "created" {
t.Fatalf("normalized filter state: %+v %+v", view, options)
}
}
for _, values := range []url.Values{
{"file_type": {"not-a-type"}}, {"ext": {"pdf,py"}}, {"ext": {"../etc"}},
{"date_field": {"removed"}}, {"from": {"2026-02-30"}},
{"from": {"2026-09-15"}, "to": {"2026-09-14"}},
{"from": {"2026-09-14T00:00:00Z"}}, {"file_type": {"pdf", "image"}},
} {
if _, view, err := parseSearchOptions(values, time.UTC); !errors.Is(err, store.ErrInvalidSearchOptions) || !view.Open {
t.Fatalf("invalid filter accepted: values=%v view=%+v err=%v", values, view, err)
}
}
options, view, err := parseSearchOptions(url.Values{"date_field": {"created"}}, time.UTC)
if err != nil || options.HasFilters() || view.Applied {
t.Fatalf("date selector alone should not launch an unfiltered listing: %+v %+v %v", options, view, err)
}
}
func seedSearchCity(t *testing.T, s *Server, token, domain string) string {
t.Helper()
q, err := s.store.CreateZoneAtPath(domain, "test", "quartier", "", "nebula-campus", "Nebula Quartier", "City-only description celestialdistrict", "/city?#%")
if err != nil {
t.Fatal(err)
}
street, err := s.store.CreateZoneAtPath(domain, "test", "street", q, "nebula-way", "Nebula Street", "shared same-name street", "/city?#%/street")
if err != nil {
t.Fatal(err)
}
if _, err := s.store.CreateSpotAtPath(domain, "test", street, "nebula-spot", `Nebula <img src=x onerror=alert(1)>`, "Visible observatory", "/city?#%/street/spot"); err != nil {
t.Fatal(err)
}
if _, err := s.store.CreateZoneAtPath(domain, "test", "quartier", "", "nebula-secret", "CITY_HIDDEN_MARKER", "nebula", "/private-city"); err != nil {
t.Fatal(err)
}
principal, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
var role string
if err := s.store.DB.QueryRow(`SELECT b.role_uuid FROM auth_role_bindings b JOIN domain_memberships m ON m.uuid=b.membership_uuid WHERE m.domain_uuid=? AND m.principal_uuid=? LIMIT 1`, domain, principal.UUID).Scan(&role); err != nil {
t.Fatal(err)
}
for _, boundary := range []string{"/city?#%", "/city?#%/street", "/city?#%/street/spot"} {
if _, err := s.store.DB.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(?,?,'read','path',?,1)`, "city-search:"+boundary, role, boundary); err != nil {
t.Fatal(err)
}
}
return street
}
func TestSearchCityResultsPrecedePagesAndHaveSafeLinks(t *testing.T) {
s, token, domain := searchServerFixture(t)
seedSearchCity(t, s, token, domain)
response := configRequest(s, token, "GET", "/v/search?q=nebula&scope=/", nil)
body := response.Body.String()
if response.Code != http.StatusOK {
t.Fatalf("City search failed: %d %s", response.Code, body)
}
cityStart, pagesStart := strings.Index(body, `data-city-search-results`), strings.Index(body, `data-search-view=`)
if cityStart < 0 || pagesStart <= cityStart {
t.Fatal("City results are not above Page results")
}
city := body[cityStart:pagesStart]
for _, value := range []string{`data-city-search-kind="quartier"`, `data-city-search-kind="street"`, `data-city-search-kind="spot"`,
`href="/v/browser/city%3F%23%25"`, `href="/v/browser/city%3F%23%25/street"`, `href="/v/browser/city%3F%23%25/street/spot"`,
`<mark>Nebula</mark>`, `&lt;img src=x onerror=alert(1)&gt;`} {
if !strings.Contains(city, value) {
t.Errorf("missing City result element %q", value)
}
}
for _, value := range []string{`data-child-path`, `children-entry`, `CITY_HIDDEN_MARKER`, `<img src=x`, `href="/v/city/`} {
if strings.Contains(city, value) {
t.Errorf("City result has unsafe/unreadable/file-operation data %q", value)
}
}
// A City description-only match is a result even when no Page matches.
response = configRequest(s, token, "GET", "/v/search?q=celestialdistrict&scope=/", nil)
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `data-city-search-kind="quartier"`) || strings.Contains(response.Body.String(), `class="search-empty"`) {
t.Fatal("City-only results were rendered as no matches")
}
// Explicit file filters exclude City metadata, which is not a file.
response = configRequest(s, token, "GET", "/v/search?q=nebula&scope=/&file_type=html", nil)
if response.Code != http.StatusOK || strings.Contains(response.Body.String(), `data-city-search-results`) || !strings.Contains(response.Body.String(), `data-search-view=`) {
t.Fatal("file filter did not keep Page hits and exclude City metadata")
}
}
func TestSearchFilterOnlyAndRetryState(t *testing.T) {
s, token, domain := searchServerFixture(t)
if err := s.store.SetConfigInDomain(domain, "timezone", "Asia/Tokyo"); err != nil {
t.Fatal(err)
}
for _, route := range []string{"/v/search?scope=/&file_type=html", "/v/search?scope=/&ext=.HTML"} {
response := configRequest(s, token, "GET", route, nil)
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `data-search-view=`) || strings.Contains(response.Body.String(), "SECRET_EXCERPT") {
t.Fatalf("filter-only request: %d %s", response.Code, response.Body.String())
}
}
response := configRequest(s, token, "GET", "/v/search?scope=/&from=2026-09-15&to=2026-09-14", nil)
if response.Code != http.StatusBadRequest || !strings.Contains(response.Body.String(), translate("ja", "search.invalid_filters_title")) || !strings.Contains(response.Body.String(), `value="2026-09-15"`) {
t.Fatalf("invalid filters not retained in themed error: %d %s", response.Code, response.Body.String())
}
request := httptest.NewRequest("GET", "/v/search?q=nebula&scope=/&file_type=html&ext=html&date_field=created&from=2026-01-01&to=2026-01-31", nil)
request.Header.Set("Authorization", "Bearer "+token)
w := httptest.NewRecorder()
s.renderSearchFailure(w, request, "nebula", "/", "", "", context.DeadlineExceeded)
body := w.Body.String()
want := "/v/search?" + searchRetryValues(request, "nebula", "/", "", "").Encode()
if w.Code != http.StatusServiceUnavailable || !strings.Contains(body, `href="`+html.EscapeString(want)+`"`) || !strings.Contains(body, "Asia/Tokyo") {
t.Fatalf("timeout lost active filters/timezone: %d %s", w.Code, body)
}
reset := "/v/search?" + searchBaseValues("nebula", "/", "", "").Encode()
if !strings.Contains(body, `href="`+html.EscapeString(reset)+`"`) {
t.Fatal("clear filters must retain query and scope")
}
}