package server
import (
"encoding/json"
"strings"
"testing"
"time"
"pageswiki/internal/store"
)
func TestCitySearchContentDestinations(t *testing.T) {
s, token, domain := searchServerFixture(t)
street := seedSearchCity(t, s, token, domain)
createUserSpacePage(t, s, domain, "/city?#%", "
Portal
")
if _, err := s.store.CreateSpotAtPath(domain, "test", street, "nebula-way-undefined", "Undefined", "nebula", "/city?#%/street/_undefined"); err != nil {
t.Fatal(err)
}
principal, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
if _, err := s.store.DB.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms)
SELECT 'entrance-grant',b.role_uuid,'read','path','/city?#%/street/_undefined',1 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); err != nil {
t.Fatal(err)
}
s.mux.HandleFunc("/_/api/v1/search/suggest", s.handleSearchSuggestAPI)
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
s.mux.HandleFunc("/", s.handleSemanticRoute)
w := configRequest(s, token, "GET", "/_/api/v1/search/suggest?q=nebula", nil)
var payload struct {
Items []searchSuggestionItem `json:"items"`
}
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &payload) != nil {
t.Fatalf("suggest: %d %s", w.Code, w.Body.String())
}
foundPortal, foundEntrance := false, false
for _, item := range payload.Items {
if strings.HasPrefix(item.Href, "/v/city/") || strings.Contains(item.Href, "_undefined") {
t.Fatalf("management/virtual link: %+v", item)
}
if item.Kind == "quartier" {
foundPortal = item.Href == "/city%3F%23%25"
}
if item.Kind == "spot" && item.Label == "Nebula Street" {
foundEntrance = item.Href == "/v/browser/city%3F%23%25/street"
}
}
if !foundPortal || !foundEntrance {
t.Fatalf("destinations missing: %+v", payload.Items)
}
for _, target := range []struct{ url, marker string }{
{"/city%3F%23%25", "Portal
"},
{"/v/browser/city%3F%23%25/street", "browser-layout"},
} {
opened := configRequest(s, token, "GET", target.url, nil)
if opened.Code != 200 || !strings.Contains(opened.Body.String(), target.marker) || strings.Contains(opened.Body.String(), `class="city-node-detail"`) {
t.Fatalf("destination %s: %d", target.url, opened.Code)
}
}
if denied := configRequest(s, token, "GET", "/v/browser/private-city", nil); denied.Code != 403 {
t.Fatalf("unreadable folder: %d", denied.Code)
}
w = configRequest(s, token, "GET", "/v/search?q=nebula", nil)
if w.Code != 200 || !strings.Contains(w.Body.String(), `href="/city%3F%23%25"`) || strings.Contains(w.Body.String(), `href="/v/city/`) {
t.Fatalf("search: %d", w.Code)
}
}
func TestCitySearchViewAndSuggestionsAgree(t *testing.T) {
for _, hasPage := range []bool{false, true} {
result := store.CitySearchResult{Kind: "spot", Name: "notes-undefined", Title: "Undefined", PathPrefix: "/日本語 #?%/_undefined", NavigationPath: "/日本語 #?%", StreetTitle: "Notes", HasPage: hasPage}
view := citySearchResultView(result, "notes", time.UTC)
suggestion := citySuggestion(result, "notes")
if view.Href != suggestion.Href || view.Href != view.PathHref || suggestion.Label != "Notes" {
t.Fatalf("inconsistent: %+v %+v", view, suggestion)
}
if strings.Contains(view.Href, "#") || strings.Contains(view.Href, "?") || strings.Contains(view.Href, "_undefined") {
t.Fatal(view.Href)
}
}
}